task_work.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <linux/spinlock.h>
  2. #include <linux/task_work.h>
  3. #include <linux/tracehook.h>
  4. static struct callback_head work_exited; /* all we need is ->next == NULL */
  5. /**
  6. * task_work_add - ask the @task to execute @work->func()
  7. * @task: the task which should run the callback
  8. * @work: the callback to run
  9. * @notify: send the notification if true
  10. *
  11. * Queue @work for task_work_run() below and notify the @task if @notify.
  12. * Fails if the @task is exiting/exited and thus it can't process this @work.
  13. * Otherwise @work->func() will be called when the @task returns from kernel
  14. * mode or exits.
  15. *
  16. * This is like the signal handler which runs in kernel mode, but it doesn't
  17. * try to wake up the @task.
  18. *
  19. * RETURNS:
  20. * 0 if succeeds or -ESRCH.
  21. */
  22. int
  23. task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
  24. {
  25. struct callback_head *head;
  26. do {
  27. head = ACCESS_ONCE(task->task_works);
  28. if (unlikely(head == &work_exited))
  29. return -ESRCH;
  30. work->next = head;
  31. } while (cmpxchg(&task->task_works, head, work) != head);
  32. if (notify)
  33. set_notify_resume(task);
  34. return 0;
  35. }
  36. /**
  37. * task_work_cancel - cancel a pending work added by task_work_add()
  38. * @task: the task which should execute the work
  39. * @func: identifies the work to remove
  40. *
  41. * Find the last queued pending work with ->func == @func and remove
  42. * it from queue.
  43. *
  44. * RETURNS:
  45. * The found work or NULL if not found.
  46. */
  47. struct callback_head *
  48. task_work_cancel(struct task_struct *task, task_work_func_t func)
  49. {
  50. struct callback_head **pprev = &task->task_works;
  51. struct callback_head *work;
  52. unsigned long flags;
  53. /*
  54. * If cmpxchg() fails we continue without updating pprev.
  55. * Either we raced with task_work_add() which added the
  56. * new entry before this work, we will find it again. Or
  57. * we raced with task_work_run(), *pprev == NULL/exited.
  58. */
  59. raw_spin_lock_irqsave(&task->pi_lock, flags);
  60. while ((work = ACCESS_ONCE(*pprev))) {
  61. smp_read_barrier_depends();
  62. if (work->func != func)
  63. pprev = &work->next;
  64. else if (cmpxchg(pprev, work, work->next) == work)
  65. break;
  66. }
  67. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  68. return work;
  69. }
  70. /**
  71. * task_work_run - execute the works added by task_work_add()
  72. *
  73. * Flush the pending works. Should be used by the core kernel code.
  74. * Called before the task returns to the user-mode or stops, or when
  75. * it exits. In the latter case task_work_add() can no longer add the
  76. * new work after task_work_run() returns.
  77. */
  78. void task_work_run(void)
  79. {
  80. struct task_struct *task = current;
  81. struct callback_head *work, *head, *next;
  82. for (;;) {
  83. /*
  84. * work->func() can do task_work_add(), do not set
  85. * work_exited unless the list is empty.
  86. */
  87. do {
  88. work = ACCESS_ONCE(task->task_works);
  89. head = !work && (task->flags & PF_EXITING) ?
  90. &work_exited : NULL;
  91. } while (cmpxchg(&task->task_works, work, head) != work);
  92. if (!work)
  93. break;
  94. /*
  95. * Synchronize with task_work_cancel(). It can't remove
  96. * the first entry == work, cmpxchg(task_works) should
  97. * fail, but it can play with *work and other entries.
  98. */
  99. raw_spin_unlock_wait(&task->pi_lock);
  100. smp_mb();
  101. /* Reverse the list to run the works in fifo order */
  102. head = NULL;
  103. do {
  104. next = work->next;
  105. work->next = head;
  106. head = work;
  107. work = next;
  108. } while (work);
  109. work = head;
  110. do {
  111. next = work->next;
  112. work->func(work);
  113. work = next;
  114. cond_resched();
  115. } while (work);
  116. }
  117. }