task_work.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/irq_work.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/task_work.h>
  5. #include <linux/resume_user_mode.h>
  6. static struct callback_head work_exited; /* all we need is ->next == NULL */
  7. #ifdef CONFIG_IRQ_WORK
  8. static void task_work_set_notify_irq(struct irq_work *entry)
  9. {
  10. test_and_set_tsk_thread_flag(current, TIF_NOTIFY_RESUME);
  11. }
  12. static DEFINE_PER_CPU(struct irq_work, irq_work_NMI_resume) =
  13. IRQ_WORK_INIT_HARD(task_work_set_notify_irq);
  14. #endif
  15. /**
  16. * task_work_add - ask the @task to execute @work->func()
  17. * @task: the task which should run the callback
  18. * @work: the callback to run
  19. * @notify: how to notify the targeted task
  20. *
  21. * Queue @work for task_work_run() below and notify the @task if @notify
  22. * is @TWA_RESUME, @TWA_SIGNAL, @TWA_SIGNAL_NO_IPI or @TWA_NMI_CURRENT.
  23. *
  24. * @TWA_SIGNAL works like signals, in that the it will interrupt the targeted
  25. * task and run the task_work, regardless of whether the task is currently
  26. * running in the kernel or userspace.
  27. * @TWA_SIGNAL_NO_IPI works like @TWA_SIGNAL, except it doesn't send a
  28. * reschedule IPI to force the targeted task to reschedule and run task_work.
  29. * This can be advantageous if there's no strict requirement that the
  30. * task_work be run as soon as possible, just whenever the task enters the
  31. * kernel anyway.
  32. * @TWA_RESUME work is run only when the task exits the kernel and returns to
  33. * user mode, or before entering guest mode.
  34. * @TWA_NMI_CURRENT works like @TWA_RESUME, except it can only be used for the
  35. * current @task and if the current context is NMI.
  36. *
  37. * Fails if the @task is exiting/exited and thus it can't process this @work.
  38. * Otherwise @work->func() will be called when the @task goes through one of
  39. * the aforementioned transitions, or exits.
  40. *
  41. * If the targeted task is exiting, then an error is returned and the work item
  42. * is not queued. It's up to the caller to arrange for an alternative mechanism
  43. * in that case.
  44. *
  45. * Note: there is no ordering guarantee on works queued here. The task_work
  46. * list is LIFO.
  47. *
  48. * RETURNS:
  49. * 0 if succeeds or -ESRCH.
  50. */
  51. int task_work_add(struct task_struct *task, struct callback_head *work,
  52. enum task_work_notify_mode notify)
  53. {
  54. struct callback_head *head;
  55. int flags = notify & TWA_FLAGS;
  56. notify &= ~TWA_FLAGS;
  57. if (notify == TWA_NMI_CURRENT) {
  58. if (WARN_ON_ONCE(task != current))
  59. return -EINVAL;
  60. if (!IS_ENABLED(CONFIG_IRQ_WORK))
  61. return -EINVAL;
  62. } else {
  63. /*
  64. * Record the work call stack in order to print it in KASAN
  65. * reports.
  66. *
  67. * Note that stack allocation can fail if TWAF_NO_ALLOC flag
  68. * is set and new page is needed to expand the stack buffer.
  69. */
  70. if (flags & TWAF_NO_ALLOC)
  71. kasan_record_aux_stack_noalloc(work);
  72. else
  73. kasan_record_aux_stack(work);
  74. }
  75. head = READ_ONCE(task->task_works);
  76. do {
  77. if (unlikely(head == &work_exited))
  78. return -ESRCH;
  79. work->next = head;
  80. } while (!try_cmpxchg(&task->task_works, &head, work));
  81. switch (notify) {
  82. case TWA_NONE:
  83. break;
  84. case TWA_RESUME:
  85. set_notify_resume(task);
  86. break;
  87. case TWA_SIGNAL:
  88. set_notify_signal(task);
  89. break;
  90. case TWA_SIGNAL_NO_IPI:
  91. __set_notify_signal(task);
  92. break;
  93. #ifdef CONFIG_IRQ_WORK
  94. case TWA_NMI_CURRENT:
  95. irq_work_queue(this_cpu_ptr(&irq_work_NMI_resume));
  96. break;
  97. #endif
  98. default:
  99. WARN_ON_ONCE(1);
  100. break;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * task_work_cancel_match - cancel a pending work added by task_work_add()
  106. * @task: the task which should execute the work
  107. * @match: match function to call
  108. * @data: data to be passed in to match function
  109. *
  110. * RETURNS:
  111. * The found work or NULL if not found.
  112. */
  113. struct callback_head *
  114. task_work_cancel_match(struct task_struct *task,
  115. bool (*match)(struct callback_head *, void *data),
  116. void *data)
  117. {
  118. struct callback_head **pprev = &task->task_works;
  119. struct callback_head *work;
  120. unsigned long flags;
  121. if (likely(!task_work_pending(task)))
  122. return NULL;
  123. /*
  124. * If cmpxchg() fails we continue without updating pprev.
  125. * Either we raced with task_work_add() which added the
  126. * new entry before this work, we will find it again. Or
  127. * we raced with task_work_run(), *pprev == NULL/exited.
  128. */
  129. raw_spin_lock_irqsave(&task->pi_lock, flags);
  130. work = READ_ONCE(*pprev);
  131. while (work) {
  132. if (!match(work, data)) {
  133. pprev = &work->next;
  134. work = READ_ONCE(*pprev);
  135. } else if (try_cmpxchg(pprev, &work, work->next))
  136. break;
  137. }
  138. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  139. return work;
  140. }
  141. static bool task_work_func_match(struct callback_head *cb, void *data)
  142. {
  143. return cb->func == data;
  144. }
  145. /**
  146. * task_work_cancel_func - cancel a pending work matching a function added by task_work_add()
  147. * @task: the task which should execute the func's work
  148. * @func: identifies the func to match with a work to remove
  149. *
  150. * Find the last queued pending work with ->func == @func and remove
  151. * it from queue.
  152. *
  153. * RETURNS:
  154. * The found work or NULL if not found.
  155. */
  156. struct callback_head *
  157. task_work_cancel_func(struct task_struct *task, task_work_func_t func)
  158. {
  159. return task_work_cancel_match(task, task_work_func_match, func);
  160. }
  161. static bool task_work_match(struct callback_head *cb, void *data)
  162. {
  163. return cb == data;
  164. }
  165. /**
  166. * task_work_cancel - cancel a pending work added by task_work_add()
  167. * @task: the task which should execute the work
  168. * @cb: the callback to remove if queued
  169. *
  170. * Remove a callback from a task's queue if queued.
  171. *
  172. * RETURNS:
  173. * True if the callback was queued and got cancelled, false otherwise.
  174. */
  175. bool task_work_cancel(struct task_struct *task, struct callback_head *cb)
  176. {
  177. struct callback_head *ret;
  178. ret = task_work_cancel_match(task, task_work_match, cb);
  179. return ret == cb;
  180. }
  181. /**
  182. * task_work_run - execute the works added by task_work_add()
  183. *
  184. * Flush the pending works. Should be used by the core kernel code.
  185. * Called before the task returns to the user-mode or stops, or when
  186. * it exits. In the latter case task_work_add() can no longer add the
  187. * new work after task_work_run() returns.
  188. */
  189. void task_work_run(void)
  190. {
  191. struct task_struct *task = current;
  192. struct callback_head *work, *head, *next;
  193. for (;;) {
  194. /*
  195. * work->func() can do task_work_add(), do not set
  196. * work_exited unless the list is empty.
  197. */
  198. work = READ_ONCE(task->task_works);
  199. do {
  200. head = NULL;
  201. if (!work) {
  202. if (task->flags & PF_EXITING)
  203. head = &work_exited;
  204. else
  205. break;
  206. }
  207. } while (!try_cmpxchg(&task->task_works, &work, head));
  208. if (!work)
  209. break;
  210. /*
  211. * Synchronize with task_work_cancel_match(). It can not remove
  212. * the first entry == work, cmpxchg(task_works) must fail.
  213. * But it can remove another entry from the ->next list.
  214. */
  215. raw_spin_lock_irq(&task->pi_lock);
  216. raw_spin_unlock_irq(&task->pi_lock);
  217. do {
  218. next = work->next;
  219. work->func(work);
  220. work = next;
  221. cond_resched();
  222. } while (work);
  223. }
  224. }