task_work.c 3.1 KB

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