task_work.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * Note: there is no ordering guarantee on works queued here.
  20. *
  21. * RETURNS:
  22. * 0 if succeeds or -ESRCH.
  23. */
  24. int
  25. task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
  26. {
  27. struct callback_head *head;
  28. do {
  29. head = READ_ONCE(task->task_works);
  30. if (unlikely(head == &work_exited))
  31. return -ESRCH;
  32. work->next = head;
  33. } while (cmpxchg(&task->task_works, head, work) != head);
  34. if (notify)
  35. set_notify_resume(task);
  36. return 0;
  37. }
  38. /**
  39. * task_work_cancel - cancel a pending work added by task_work_add()
  40. * @task: the task which should execute the work
  41. * @func: identifies the work to remove
  42. *
  43. * Find the last queued pending work with ->func == @func and remove
  44. * it from queue.
  45. *
  46. * RETURNS:
  47. * The found work or NULL if not found.
  48. */
  49. struct callback_head *
  50. task_work_cancel(struct task_struct *task, task_work_func_t func)
  51. {
  52. struct callback_head **pprev = &task->task_works;
  53. struct callback_head *work;
  54. unsigned long flags;
  55. if (likely(!task->task_works))
  56. return NULL;
  57. /*
  58. * If cmpxchg() fails we continue without updating pprev.
  59. * Either we raced with task_work_add() which added the
  60. * new entry before this work, we will find it again. Or
  61. * we raced with task_work_run(), *pprev == NULL/exited.
  62. */
  63. raw_spin_lock_irqsave(&task->pi_lock, flags);
  64. while ((work = lockless_dereference(*pprev))) {
  65. if (work->func != func)
  66. pprev = &work->next;
  67. else if (cmpxchg(pprev, work, work->next) == work)
  68. break;
  69. }
  70. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  71. return work;
  72. }
  73. /**
  74. * task_work_run - execute the works added by task_work_add()
  75. *
  76. * Flush the pending works. Should be used by the core kernel code.
  77. * Called before the task returns to the user-mode or stops, or when
  78. * it exits. In the latter case task_work_add() can no longer add the
  79. * new work after task_work_run() returns.
  80. */
  81. void task_work_run(void)
  82. {
  83. struct task_struct *task = current;
  84. struct callback_head *work, *head, *next;
  85. for (;;) {
  86. /*
  87. * work->func() can do task_work_add(), do not set
  88. * work_exited unless the list is empty.
  89. */
  90. do {
  91. work = READ_ONCE(task->task_works);
  92. head = !work && (task->flags & PF_EXITING) ?
  93. &work_exited : NULL;
  94. } while (cmpxchg(&task->task_works, work, head) != work);
  95. if (!work)
  96. break;
  97. /*
  98. * Synchronize with task_work_cancel(). It can't remove
  99. * the first entry == work, cmpxchg(task_works) should
  100. * fail, but it can play with *work and other entries.
  101. */
  102. raw_spin_unlock_wait(&task->pi_lock);
  103. do {
  104. next = work->next;
  105. work->func(work);
  106. work = next;
  107. cond_resched();
  108. } while (work);
  109. }
  110. }