kthread.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /* Kernel thread helper functions.
  2. * Copyright (C) 2004 IBM Corporation, Rusty Russell.
  3. *
  4. * Creation is done via kthreadd, so that we get a clean environment
  5. * even if we're invoked from userspace (think modprobe, hotplug cpu,
  6. * etc.).
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/kthread.h>
  10. #include <linux/completion.h>
  11. #include <linux/err.h>
  12. #include <linux/cpuset.h>
  13. #include <linux/unistd.h>
  14. #include <linux/file.h>
  15. #include <linux/export.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/freezer.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/uaccess.h>
  21. #include <trace/events/sched.h>
  22. static DEFINE_SPINLOCK(kthread_create_lock);
  23. static LIST_HEAD(kthread_create_list);
  24. struct task_struct *kthreadd_task;
  25. struct kthread_create_info
  26. {
  27. /* Information passed to kthread() from kthreadd. */
  28. int (*threadfn)(void *data);
  29. void *data;
  30. int node;
  31. /* Result passed back to kthread_create() from kthreadd. */
  32. struct task_struct *result;
  33. struct completion *done;
  34. struct list_head list;
  35. };
  36. struct kthread {
  37. unsigned long flags;
  38. unsigned int cpu;
  39. void *data;
  40. struct completion parked;
  41. struct completion exited;
  42. };
  43. enum KTHREAD_BITS {
  44. KTHREAD_IS_PER_CPU = 0,
  45. KTHREAD_SHOULD_STOP,
  46. KTHREAD_SHOULD_PARK,
  47. KTHREAD_IS_PARKED,
  48. };
  49. #define __to_kthread(vfork) \
  50. container_of(vfork, struct kthread, exited)
  51. static inline struct kthread *to_kthread(struct task_struct *k)
  52. {
  53. return __to_kthread(k->vfork_done);
  54. }
  55. static struct kthread *to_live_kthread(struct task_struct *k)
  56. {
  57. struct completion *vfork = ACCESS_ONCE(k->vfork_done);
  58. if (likely(vfork) && try_get_task_stack(k))
  59. return __to_kthread(vfork);
  60. return NULL;
  61. }
  62. /**
  63. * kthread_should_stop - should this kthread return now?
  64. *
  65. * When someone calls kthread_stop() on your kthread, it will be woken
  66. * and this will return true. You should then return, and your return
  67. * value will be passed through to kthread_stop().
  68. */
  69. bool kthread_should_stop(void)
  70. {
  71. return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
  72. }
  73. EXPORT_SYMBOL(kthread_should_stop);
  74. /**
  75. * kthread_should_park - should this kthread park now?
  76. *
  77. * When someone calls kthread_park() on your kthread, it will be woken
  78. * and this will return true. You should then do the necessary
  79. * cleanup and call kthread_parkme()
  80. *
  81. * Similar to kthread_should_stop(), but this keeps the thread alive
  82. * and in a park position. kthread_unpark() "restarts" the thread and
  83. * calls the thread function again.
  84. */
  85. bool kthread_should_park(void)
  86. {
  87. return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags);
  88. }
  89. EXPORT_SYMBOL_GPL(kthread_should_park);
  90. /**
  91. * kthread_freezable_should_stop - should this freezable kthread return now?
  92. * @was_frozen: optional out parameter, indicates whether %current was frozen
  93. *
  94. * kthread_should_stop() for freezable kthreads, which will enter
  95. * refrigerator if necessary. This function is safe from kthread_stop() /
  96. * freezer deadlock and freezable kthreads should use this function instead
  97. * of calling try_to_freeze() directly.
  98. */
  99. bool kthread_freezable_should_stop(bool *was_frozen)
  100. {
  101. bool frozen = false;
  102. might_sleep();
  103. if (unlikely(freezing(current)))
  104. frozen = __refrigerator(true);
  105. if (was_frozen)
  106. *was_frozen = frozen;
  107. return kthread_should_stop();
  108. }
  109. EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
  110. /**
  111. * kthread_data - return data value specified on kthread creation
  112. * @task: kthread task in question
  113. *
  114. * Return the data value specified when kthread @task was created.
  115. * The caller is responsible for ensuring the validity of @task when
  116. * calling this function.
  117. */
  118. void *kthread_data(struct task_struct *task)
  119. {
  120. return to_kthread(task)->data;
  121. }
  122. /**
  123. * kthread_probe_data - speculative version of kthread_data()
  124. * @task: possible kthread task in question
  125. *
  126. * @task could be a kthread task. Return the data value specified when it
  127. * was created if accessible. If @task isn't a kthread task or its data is
  128. * inaccessible for any reason, %NULL is returned. This function requires
  129. * that @task itself is safe to dereference.
  130. */
  131. void *kthread_probe_data(struct task_struct *task)
  132. {
  133. struct kthread *kthread = to_kthread(task);
  134. void *data = NULL;
  135. probe_kernel_read(&data, &kthread->data, sizeof(data));
  136. return data;
  137. }
  138. static void __kthread_parkme(struct kthread *self)
  139. {
  140. __set_current_state(TASK_PARKED);
  141. while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
  142. if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
  143. complete(&self->parked);
  144. schedule();
  145. __set_current_state(TASK_PARKED);
  146. }
  147. clear_bit(KTHREAD_IS_PARKED, &self->flags);
  148. __set_current_state(TASK_RUNNING);
  149. }
  150. void kthread_parkme(void)
  151. {
  152. __kthread_parkme(to_kthread(current));
  153. }
  154. EXPORT_SYMBOL_GPL(kthread_parkme);
  155. static int kthread(void *_create)
  156. {
  157. /* Copy data: it's on kthread's stack */
  158. struct kthread_create_info *create = _create;
  159. int (*threadfn)(void *data) = create->threadfn;
  160. void *data = create->data;
  161. struct completion *done;
  162. struct kthread self;
  163. int ret;
  164. self.flags = 0;
  165. self.data = data;
  166. init_completion(&self.exited);
  167. init_completion(&self.parked);
  168. current->vfork_done = &self.exited;
  169. /* If user was SIGKILLed, I release the structure. */
  170. done = xchg(&create->done, NULL);
  171. if (!done) {
  172. kfree(create);
  173. do_exit(-EINTR);
  174. }
  175. /* OK, tell user we're spawned, wait for stop or wakeup */
  176. __set_current_state(TASK_UNINTERRUPTIBLE);
  177. create->result = current;
  178. complete(done);
  179. schedule();
  180. ret = -EINTR;
  181. if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) {
  182. __kthread_parkme(&self);
  183. ret = threadfn(data);
  184. }
  185. /* we can't just return, we must preserve "self" on stack */
  186. do_exit(ret);
  187. }
  188. /* called from do_fork() to get node information for about to be created task */
  189. int tsk_fork_get_node(struct task_struct *tsk)
  190. {
  191. #ifdef CONFIG_NUMA
  192. if (tsk == kthreadd_task)
  193. return tsk->pref_node_fork;
  194. #endif
  195. return NUMA_NO_NODE;
  196. }
  197. static void create_kthread(struct kthread_create_info *create)
  198. {
  199. int pid;
  200. #ifdef CONFIG_NUMA
  201. current->pref_node_fork = create->node;
  202. #endif
  203. /* We want our own signal handler (we take no signals by default). */
  204. pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
  205. if (pid < 0) {
  206. /* If user was SIGKILLed, I release the structure. */
  207. struct completion *done = xchg(&create->done, NULL);
  208. if (!done) {
  209. kfree(create);
  210. return;
  211. }
  212. create->result = ERR_PTR(pid);
  213. complete(done);
  214. }
  215. }
  216. /**
  217. * kthread_create_on_node - create a kthread.
  218. * @threadfn: the function to run until signal_pending(current).
  219. * @data: data ptr for @threadfn.
  220. * @node: task and thread structures for the thread are allocated on this node
  221. * @namefmt: printf-style name for the thread.
  222. *
  223. * Description: This helper function creates and names a kernel
  224. * thread. The thread will be stopped: use wake_up_process() to start
  225. * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and
  226. * is affine to all CPUs.
  227. *
  228. * If thread is going to be bound on a particular cpu, give its node
  229. * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
  230. * When woken, the thread will run @threadfn() with @data as its
  231. * argument. @threadfn() can either call do_exit() directly if it is a
  232. * standalone thread for which no one will call kthread_stop(), or
  233. * return when 'kthread_should_stop()' is true (which means
  234. * kthread_stop() has been called). The return value should be zero
  235. * or a negative error number; it will be passed to kthread_stop().
  236. *
  237. * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
  238. */
  239. struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
  240. void *data, int node,
  241. const char namefmt[],
  242. ...)
  243. {
  244. DECLARE_COMPLETION_ONSTACK(done);
  245. struct task_struct *task;
  246. struct kthread_create_info *create = kmalloc(sizeof(*create),
  247. GFP_KERNEL);
  248. if (!create)
  249. return ERR_PTR(-ENOMEM);
  250. create->threadfn = threadfn;
  251. create->data = data;
  252. create->node = node;
  253. create->done = &done;
  254. spin_lock(&kthread_create_lock);
  255. list_add_tail(&create->list, &kthread_create_list);
  256. spin_unlock(&kthread_create_lock);
  257. wake_up_process(kthreadd_task);
  258. /*
  259. * Wait for completion in killable state, for I might be chosen by
  260. * the OOM killer while kthreadd is trying to allocate memory for
  261. * new kernel thread.
  262. */
  263. if (unlikely(wait_for_completion_killable(&done))) {
  264. /*
  265. * If I was SIGKILLed before kthreadd (or new kernel thread)
  266. * calls complete(), leave the cleanup of this structure to
  267. * that thread.
  268. */
  269. if (xchg(&create->done, NULL))
  270. return ERR_PTR(-EINTR);
  271. /*
  272. * kthreadd (or new kernel thread) will call complete()
  273. * shortly.
  274. */
  275. wait_for_completion(&done);
  276. }
  277. task = create->result;
  278. if (!IS_ERR(task)) {
  279. static const struct sched_param param = { .sched_priority = 0 };
  280. va_list args;
  281. va_start(args, namefmt);
  282. vsnprintf(task->comm, sizeof(task->comm), namefmt, args);
  283. va_end(args);
  284. /*
  285. * root may have changed our (kthreadd's) priority or CPU mask.
  286. * The kernel thread should not inherit these properties.
  287. */
  288. sched_setscheduler_nocheck(task, SCHED_NORMAL, &param);
  289. set_cpus_allowed_ptr(task, cpu_all_mask);
  290. }
  291. kfree(create);
  292. return task;
  293. }
  294. EXPORT_SYMBOL(kthread_create_on_node);
  295. static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, long state)
  296. {
  297. unsigned long flags;
  298. if (!wait_task_inactive(p, state)) {
  299. WARN_ON(1);
  300. return;
  301. }
  302. /* It's safe because the task is inactive. */
  303. raw_spin_lock_irqsave(&p->pi_lock, flags);
  304. do_set_cpus_allowed(p, mask);
  305. p->flags |= PF_NO_SETAFFINITY;
  306. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  307. }
  308. static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
  309. {
  310. __kthread_bind_mask(p, cpumask_of(cpu), state);
  311. }
  312. void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
  313. {
  314. __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
  315. }
  316. /**
  317. * kthread_bind - bind a just-created kthread to a cpu.
  318. * @p: thread created by kthread_create().
  319. * @cpu: cpu (might not be online, must be possible) for @k to run on.
  320. *
  321. * Description: This function is equivalent to set_cpus_allowed(),
  322. * except that @cpu doesn't need to be online, and the thread must be
  323. * stopped (i.e., just returned from kthread_create()).
  324. */
  325. void kthread_bind(struct task_struct *p, unsigned int cpu)
  326. {
  327. __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
  328. }
  329. EXPORT_SYMBOL(kthread_bind);
  330. /**
  331. * kthread_create_on_cpu - Create a cpu bound kthread
  332. * @threadfn: the function to run until signal_pending(current).
  333. * @data: data ptr for @threadfn.
  334. * @cpu: The cpu on which the thread should be bound,
  335. * @namefmt: printf-style name for the thread. Format is restricted
  336. * to "name.*%u". Code fills in cpu number.
  337. *
  338. * Description: This helper function creates and names a kernel thread
  339. * The thread will be woken and put into park mode.
  340. */
  341. struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
  342. void *data, unsigned int cpu,
  343. const char *namefmt)
  344. {
  345. struct task_struct *p;
  346. p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
  347. cpu);
  348. if (IS_ERR(p))
  349. return p;
  350. kthread_bind(p, cpu);
  351. /* CPU hotplug need to bind once again when unparking the thread. */
  352. set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
  353. to_kthread(p)->cpu = cpu;
  354. return p;
  355. }
  356. static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)
  357. {
  358. clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
  359. /*
  360. * We clear the IS_PARKED bit here as we don't wait
  361. * until the task has left the park code. So if we'd
  362. * park before that happens we'd see the IS_PARKED bit
  363. * which might be about to be cleared.
  364. */
  365. if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
  366. /*
  367. * Newly created kthread was parked when the CPU was offline.
  368. * The binding was lost and we need to set it again.
  369. */
  370. if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
  371. __kthread_bind(k, kthread->cpu, TASK_PARKED);
  372. wake_up_state(k, TASK_PARKED);
  373. }
  374. }
  375. /**
  376. * kthread_unpark - unpark a thread created by kthread_create().
  377. * @k: thread created by kthread_create().
  378. *
  379. * Sets kthread_should_park() for @k to return false, wakes it, and
  380. * waits for it to return. If the thread is marked percpu then its
  381. * bound to the cpu again.
  382. */
  383. void kthread_unpark(struct task_struct *k)
  384. {
  385. struct kthread *kthread = to_live_kthread(k);
  386. if (kthread) {
  387. __kthread_unpark(k, kthread);
  388. put_task_stack(k);
  389. }
  390. }
  391. EXPORT_SYMBOL_GPL(kthread_unpark);
  392. /**
  393. * kthread_park - park a thread created by kthread_create().
  394. * @k: thread created by kthread_create().
  395. *
  396. * Sets kthread_should_park() for @k to return true, wakes it, and
  397. * waits for it to return. This can also be called after kthread_create()
  398. * instead of calling wake_up_process(): the thread will park without
  399. * calling threadfn().
  400. *
  401. * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
  402. * If called by the kthread itself just the park bit is set.
  403. */
  404. int kthread_park(struct task_struct *k)
  405. {
  406. struct kthread *kthread = to_live_kthread(k);
  407. int ret = -ENOSYS;
  408. if (kthread) {
  409. if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
  410. set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
  411. if (k != current) {
  412. wake_up_process(k);
  413. wait_for_completion(&kthread->parked);
  414. }
  415. }
  416. put_task_stack(k);
  417. ret = 0;
  418. }
  419. return ret;
  420. }
  421. EXPORT_SYMBOL_GPL(kthread_park);
  422. /**
  423. * kthread_stop - stop a thread created by kthread_create().
  424. * @k: thread created by kthread_create().
  425. *
  426. * Sets kthread_should_stop() for @k to return true, wakes it, and
  427. * waits for it to exit. This can also be called after kthread_create()
  428. * instead of calling wake_up_process(): the thread will exit without
  429. * calling threadfn().
  430. *
  431. * If threadfn() may call do_exit() itself, the caller must ensure
  432. * task_struct can't go away.
  433. *
  434. * Returns the result of threadfn(), or %-EINTR if wake_up_process()
  435. * was never called.
  436. */
  437. int kthread_stop(struct task_struct *k)
  438. {
  439. struct kthread *kthread;
  440. int ret;
  441. trace_sched_kthread_stop(k);
  442. get_task_struct(k);
  443. kthread = to_live_kthread(k);
  444. if (kthread) {
  445. set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
  446. __kthread_unpark(k, kthread);
  447. wake_up_process(k);
  448. wait_for_completion(&kthread->exited);
  449. put_task_stack(k);
  450. }
  451. ret = k->exit_code;
  452. put_task_struct(k);
  453. trace_sched_kthread_stop_ret(ret);
  454. return ret;
  455. }
  456. EXPORT_SYMBOL(kthread_stop);
  457. int kthreadd(void *unused)
  458. {
  459. struct task_struct *tsk = current;
  460. /* Setup a clean context for our children to inherit. */
  461. set_task_comm(tsk, "kthreadd");
  462. ignore_signals(tsk);
  463. set_cpus_allowed_ptr(tsk, cpu_all_mask);
  464. set_mems_allowed(node_states[N_MEMORY]);
  465. current->flags |= PF_NOFREEZE;
  466. for (;;) {
  467. set_current_state(TASK_INTERRUPTIBLE);
  468. if (list_empty(&kthread_create_list))
  469. schedule();
  470. __set_current_state(TASK_RUNNING);
  471. spin_lock(&kthread_create_lock);
  472. while (!list_empty(&kthread_create_list)) {
  473. struct kthread_create_info *create;
  474. create = list_entry(kthread_create_list.next,
  475. struct kthread_create_info, list);
  476. list_del_init(&create->list);
  477. spin_unlock(&kthread_create_lock);
  478. create_kthread(create);
  479. spin_lock(&kthread_create_lock);
  480. }
  481. spin_unlock(&kthread_create_lock);
  482. }
  483. return 0;
  484. }
  485. void __kthread_init_worker(struct kthread_worker *worker,
  486. const char *name,
  487. struct lock_class_key *key)
  488. {
  489. spin_lock_init(&worker->lock);
  490. lockdep_set_class_and_name(&worker->lock, key, name);
  491. INIT_LIST_HEAD(&worker->work_list);
  492. worker->task = NULL;
  493. }
  494. EXPORT_SYMBOL_GPL(__kthread_init_worker);
  495. /**
  496. * kthread_worker_fn - kthread function to process kthread_worker
  497. * @worker_ptr: pointer to initialized kthread_worker
  498. *
  499. * This function can be used as @threadfn to kthread_create() or
  500. * kthread_run() with @worker_ptr argument pointing to an initialized
  501. * kthread_worker. The started kthread will process work_list until
  502. * the it is stopped with kthread_stop(). A kthread can also call
  503. * this function directly after extra initialization.
  504. *
  505. * Different kthreads can be used for the same kthread_worker as long
  506. * as there's only one kthread attached to it at any given time. A
  507. * kthread_worker without an attached kthread simply collects queued
  508. * kthread_works.
  509. */
  510. int kthread_worker_fn(void *worker_ptr)
  511. {
  512. struct kthread_worker *worker = worker_ptr;
  513. struct kthread_work *work;
  514. WARN_ON(worker->task);
  515. worker->task = current;
  516. repeat:
  517. set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
  518. if (kthread_should_stop()) {
  519. __set_current_state(TASK_RUNNING);
  520. spin_lock_irq(&worker->lock);
  521. worker->task = NULL;
  522. spin_unlock_irq(&worker->lock);
  523. return 0;
  524. }
  525. work = NULL;
  526. spin_lock_irq(&worker->lock);
  527. if (!list_empty(&worker->work_list)) {
  528. work = list_first_entry(&worker->work_list,
  529. struct kthread_work, node);
  530. list_del_init(&work->node);
  531. }
  532. worker->current_work = work;
  533. spin_unlock_irq(&worker->lock);
  534. if (work) {
  535. __set_current_state(TASK_RUNNING);
  536. work->func(work);
  537. } else if (!freezing(current))
  538. schedule();
  539. try_to_freeze();
  540. goto repeat;
  541. }
  542. EXPORT_SYMBOL_GPL(kthread_worker_fn);
  543. /* insert @work before @pos in @worker */
  544. static void kthread_insert_work(struct kthread_worker *worker,
  545. struct kthread_work *work,
  546. struct list_head *pos)
  547. {
  548. lockdep_assert_held(&worker->lock);
  549. list_add_tail(&work->node, pos);
  550. work->worker = worker;
  551. if (!worker->current_work && likely(worker->task))
  552. wake_up_process(worker->task);
  553. }
  554. /**
  555. * kthread_queue_work - queue a kthread_work
  556. * @worker: target kthread_worker
  557. * @work: kthread_work to queue
  558. *
  559. * Queue @work to work processor @task for async execution. @task
  560. * must have been created with kthread_worker_create(). Returns %true
  561. * if @work was successfully queued, %false if it was already pending.
  562. */
  563. bool kthread_queue_work(struct kthread_worker *worker,
  564. struct kthread_work *work)
  565. {
  566. bool ret = false;
  567. unsigned long flags;
  568. spin_lock_irqsave(&worker->lock, flags);
  569. if (list_empty(&work->node)) {
  570. kthread_insert_work(worker, work, &worker->work_list);
  571. ret = true;
  572. }
  573. spin_unlock_irqrestore(&worker->lock, flags);
  574. return ret;
  575. }
  576. EXPORT_SYMBOL_GPL(kthread_queue_work);
  577. struct kthread_flush_work {
  578. struct kthread_work work;
  579. struct completion done;
  580. };
  581. static void kthread_flush_work_fn(struct kthread_work *work)
  582. {
  583. struct kthread_flush_work *fwork =
  584. container_of(work, struct kthread_flush_work, work);
  585. complete(&fwork->done);
  586. }
  587. /**
  588. * kthread_flush_work - flush a kthread_work
  589. * @work: work to flush
  590. *
  591. * If @work is queued or executing, wait for it to finish execution.
  592. */
  593. void kthread_flush_work(struct kthread_work *work)
  594. {
  595. struct kthread_flush_work fwork = {
  596. KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
  597. COMPLETION_INITIALIZER_ONSTACK(fwork.done),
  598. };
  599. struct kthread_worker *worker;
  600. bool noop = false;
  601. retry:
  602. worker = work->worker;
  603. if (!worker)
  604. return;
  605. spin_lock_irq(&worker->lock);
  606. if (work->worker != worker) {
  607. spin_unlock_irq(&worker->lock);
  608. goto retry;
  609. }
  610. if (!list_empty(&work->node))
  611. kthread_insert_work(worker, &fwork.work, work->node.next);
  612. else if (worker->current_work == work)
  613. kthread_insert_work(worker, &fwork.work,
  614. worker->work_list.next);
  615. else
  616. noop = true;
  617. spin_unlock_irq(&worker->lock);
  618. if (!noop)
  619. wait_for_completion(&fwork.done);
  620. }
  621. EXPORT_SYMBOL_GPL(kthread_flush_work);
  622. /**
  623. * kthread_flush_worker - flush all current works on a kthread_worker
  624. * @worker: worker to flush
  625. *
  626. * Wait until all currently executing or pending works on @worker are
  627. * finished.
  628. */
  629. void kthread_flush_worker(struct kthread_worker *worker)
  630. {
  631. struct kthread_flush_work fwork = {
  632. KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
  633. COMPLETION_INITIALIZER_ONSTACK(fwork.done),
  634. };
  635. kthread_queue_work(worker, &fwork.work);
  636. wait_for_completion(&fwork.done);
  637. }
  638. EXPORT_SYMBOL_GPL(kthread_flush_worker);