task_work.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = ACCESS_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. /*
  56. * If cmpxchg() fails we continue without updating pprev.
  57. * Either we raced with task_work_add() which added the
  58. * new entry before this work, we will find it again. Or
  59. * we raced with task_work_run(), *pprev == NULL/exited.
  60. */
  61. raw_spin_lock_irqsave(&task->pi_lock, flags);
  62. while ((work = ACCESS_ONCE(*pprev))) {
  63. smp_read_barrier_depends();
  64. if (work->func != func)
  65. pprev = &work->next;
  66. else if (cmpxchg(pprev, work, work->next) == work)
  67. break;
  68. }
  69. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  70. return work;
  71. }
  72. /**
  73. * task_work_run - execute the works added by task_work_add()
  74. *
  75. * Flush the pending works. Should be used by the core kernel code.
  76. * Called before the task returns to the user-mode or stops, or when
  77. * it exits. In the latter case task_work_add() can no longer add the
  78. * new work after task_work_run() returns.
  79. */
  80. void task_work_run(void)
  81. {
  82. struct task_struct *task = current;
  83. struct callback_head *work, *head, *next;
  84. for (;;) {
  85. /*
  86. * work->func() can do task_work_add(), do not set
  87. * work_exited unless the list is empty.
  88. */
  89. do {
  90. work = ACCESS_ONCE(task->task_works);
  91. head = !work && (task->flags & PF_EXITING) ?
  92. &work_exited : NULL;
  93. } while (cmpxchg(&task->task_works, work, head) != work);
  94. if (!work)
  95. break;
  96. /*
  97. * Synchronize with task_work_cancel(). It can't remove
  98. * the first entry == work, cmpxchg(task_works) should
  99. * fail, but it can play with *work and other entries.
  100. */
  101. raw_spin_unlock_wait(&task->pi_lock);
  102. smp_mb();
  103. do {
  104. next = work->next;
  105. work->func(work);
  106. work = next;
  107. cond_resched();
  108. } while (work);
  109. }
  110. }