slow-work.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /* Worker thread pool for slow items, such as filesystem lookups or mkdirs
  2. *
  3. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. *
  11. * See Documentation/slow-work.txt
  12. */
  13. #include <linux/module.h>
  14. #include <linux/slow-work.h>
  15. #include <linux/kthread.h>
  16. #include <linux/freezer.h>
  17. #include <linux/wait.h>
  18. #include <linux/debugfs.h>
  19. #include "slow-work.h"
  20. static void slow_work_cull_timeout(unsigned long);
  21. static void slow_work_oom_timeout(unsigned long);
  22. #ifdef CONFIG_SYSCTL
  23. static int slow_work_min_threads_sysctl(struct ctl_table *, int,
  24. void __user *, size_t *, loff_t *);
  25. static int slow_work_max_threads_sysctl(struct ctl_table *, int ,
  26. void __user *, size_t *, loff_t *);
  27. #endif
  28. /*
  29. * The pool of threads has at least min threads in it as long as someone is
  30. * using the facility, and may have as many as max.
  31. *
  32. * A portion of the pool may be processing very slow operations.
  33. */
  34. static unsigned slow_work_min_threads = 2;
  35. static unsigned slow_work_max_threads = 4;
  36. static unsigned vslow_work_proportion = 50; /* % of threads that may process
  37. * very slow work */
  38. #ifdef CONFIG_SYSCTL
  39. static const int slow_work_min_min_threads = 2;
  40. static int slow_work_max_max_threads = SLOW_WORK_THREAD_LIMIT;
  41. static const int slow_work_min_vslow = 1;
  42. static const int slow_work_max_vslow = 99;
  43. ctl_table slow_work_sysctls[] = {
  44. {
  45. .ctl_name = CTL_UNNUMBERED,
  46. .procname = "min-threads",
  47. .data = &slow_work_min_threads,
  48. .maxlen = sizeof(unsigned),
  49. .mode = 0644,
  50. .proc_handler = slow_work_min_threads_sysctl,
  51. .extra1 = (void *) &slow_work_min_min_threads,
  52. .extra2 = &slow_work_max_threads,
  53. },
  54. {
  55. .ctl_name = CTL_UNNUMBERED,
  56. .procname = "max-threads",
  57. .data = &slow_work_max_threads,
  58. .maxlen = sizeof(unsigned),
  59. .mode = 0644,
  60. .proc_handler = slow_work_max_threads_sysctl,
  61. .extra1 = &slow_work_min_threads,
  62. .extra2 = (void *) &slow_work_max_max_threads,
  63. },
  64. {
  65. .ctl_name = CTL_UNNUMBERED,
  66. .procname = "vslow-percentage",
  67. .data = &vslow_work_proportion,
  68. .maxlen = sizeof(unsigned),
  69. .mode = 0644,
  70. .proc_handler = &proc_dointvec_minmax,
  71. .extra1 = (void *) &slow_work_min_vslow,
  72. .extra2 = (void *) &slow_work_max_vslow,
  73. },
  74. { .ctl_name = 0 }
  75. };
  76. #endif
  77. /*
  78. * The active state of the thread pool
  79. */
  80. static atomic_t slow_work_thread_count;
  81. static atomic_t vslow_work_executing_count;
  82. static bool slow_work_may_not_start_new_thread;
  83. static bool slow_work_cull; /* cull a thread due to lack of activity */
  84. static DEFINE_TIMER(slow_work_cull_timer, slow_work_cull_timeout, 0, 0);
  85. static DEFINE_TIMER(slow_work_oom_timer, slow_work_oom_timeout, 0, 0);
  86. static struct slow_work slow_work_new_thread; /* new thread starter */
  87. /*
  88. * slow work ID allocation (use slow_work_queue_lock)
  89. */
  90. static DECLARE_BITMAP(slow_work_ids, SLOW_WORK_THREAD_LIMIT);
  91. /*
  92. * Unregistration tracking to prevent put_ref() from disappearing during module
  93. * unload
  94. */
  95. #ifdef CONFIG_MODULES
  96. static struct module *slow_work_thread_processing[SLOW_WORK_THREAD_LIMIT];
  97. static struct module *slow_work_unreg_module;
  98. static struct slow_work *slow_work_unreg_work_item;
  99. static DECLARE_WAIT_QUEUE_HEAD(slow_work_unreg_wq);
  100. static DEFINE_MUTEX(slow_work_unreg_sync_lock);
  101. static void slow_work_set_thread_processing(int id, struct slow_work *work)
  102. {
  103. if (work)
  104. slow_work_thread_processing[id] = work->owner;
  105. }
  106. static void slow_work_done_thread_processing(int id, struct slow_work *work)
  107. {
  108. struct module *module = slow_work_thread_processing[id];
  109. slow_work_thread_processing[id] = NULL;
  110. smp_mb();
  111. if (slow_work_unreg_work_item == work ||
  112. slow_work_unreg_module == module)
  113. wake_up_all(&slow_work_unreg_wq);
  114. }
  115. static void slow_work_clear_thread_processing(int id)
  116. {
  117. slow_work_thread_processing[id] = NULL;
  118. }
  119. #else
  120. static void slow_work_set_thread_processing(int id, struct slow_work *work) {}
  121. static void slow_work_done_thread_processing(int id, struct slow_work *work) {}
  122. static void slow_work_clear_thread_processing(int id) {}
  123. #endif
  124. /*
  125. * Data for tracking currently executing items for indication through /proc
  126. */
  127. #ifdef CONFIG_SLOW_WORK_DEBUG
  128. struct slow_work *slow_work_execs[SLOW_WORK_THREAD_LIMIT];
  129. pid_t slow_work_pids[SLOW_WORK_THREAD_LIMIT];
  130. DEFINE_RWLOCK(slow_work_execs_lock);
  131. #endif
  132. /*
  133. * The queues of work items and the lock governing access to them. These are
  134. * shared between all the CPUs. It doesn't make sense to have per-CPU queues
  135. * as the number of threads bears no relation to the number of CPUs.
  136. *
  137. * There are two queues of work items: one for slow work items, and one for
  138. * very slow work items.
  139. */
  140. LIST_HEAD(slow_work_queue);
  141. LIST_HEAD(vslow_work_queue);
  142. DEFINE_SPINLOCK(slow_work_queue_lock);
  143. /*
  144. * The following are two wait queues that get pinged when a work item is placed
  145. * on an empty queue. These allow work items that are hogging a thread by
  146. * sleeping in a way that could be deferred to yield their thread and enqueue
  147. * themselves.
  148. */
  149. static DECLARE_WAIT_QUEUE_HEAD(slow_work_queue_waits_for_occupation);
  150. static DECLARE_WAIT_QUEUE_HEAD(vslow_work_queue_waits_for_occupation);
  151. /*
  152. * The thread controls. A variable used to signal to the threads that they
  153. * should exit when the queue is empty, a waitqueue used by the threads to wait
  154. * for signals, and a completion set by the last thread to exit.
  155. */
  156. static bool slow_work_threads_should_exit;
  157. static DECLARE_WAIT_QUEUE_HEAD(slow_work_thread_wq);
  158. static DECLARE_COMPLETION(slow_work_last_thread_exited);
  159. /*
  160. * The number of users of the thread pool and its lock. Whilst this is zero we
  161. * have no threads hanging around, and when this reaches zero, we wait for all
  162. * active or queued work items to complete and kill all the threads we do have.
  163. */
  164. static int slow_work_user_count;
  165. static DEFINE_MUTEX(slow_work_user_lock);
  166. static inline int slow_work_get_ref(struct slow_work *work)
  167. {
  168. if (work->ops->get_ref)
  169. return work->ops->get_ref(work);
  170. return 0;
  171. }
  172. static inline void slow_work_put_ref(struct slow_work *work)
  173. {
  174. if (work->ops->put_ref)
  175. work->ops->put_ref(work);
  176. }
  177. /*
  178. * Calculate the maximum number of active threads in the pool that are
  179. * permitted to process very slow work items.
  180. *
  181. * The answer is rounded up to at least 1, but may not equal or exceed the
  182. * maximum number of the threads in the pool. This means we always have at
  183. * least one thread that can process slow work items, and we always have at
  184. * least one thread that won't get tied up doing so.
  185. */
  186. static unsigned slow_work_calc_vsmax(void)
  187. {
  188. unsigned vsmax;
  189. vsmax = atomic_read(&slow_work_thread_count) * vslow_work_proportion;
  190. vsmax /= 100;
  191. vsmax = max(vsmax, 1U);
  192. return min(vsmax, slow_work_max_threads - 1);
  193. }
  194. /*
  195. * Attempt to execute stuff queued on a slow thread. Return true if we managed
  196. * it, false if there was nothing to do.
  197. */
  198. static noinline bool slow_work_execute(int id)
  199. {
  200. struct slow_work *work = NULL;
  201. unsigned vsmax;
  202. bool very_slow;
  203. vsmax = slow_work_calc_vsmax();
  204. /* see if we can schedule a new thread to be started if we're not
  205. * keeping up with the work */
  206. if (!waitqueue_active(&slow_work_thread_wq) &&
  207. (!list_empty(&slow_work_queue) || !list_empty(&vslow_work_queue)) &&
  208. atomic_read(&slow_work_thread_count) < slow_work_max_threads &&
  209. !slow_work_may_not_start_new_thread)
  210. slow_work_enqueue(&slow_work_new_thread);
  211. /* find something to execute */
  212. spin_lock_irq(&slow_work_queue_lock);
  213. if (!list_empty(&vslow_work_queue) &&
  214. atomic_read(&vslow_work_executing_count) < vsmax) {
  215. work = list_entry(vslow_work_queue.next,
  216. struct slow_work, link);
  217. if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
  218. BUG();
  219. list_del_init(&work->link);
  220. atomic_inc(&vslow_work_executing_count);
  221. very_slow = true;
  222. } else if (!list_empty(&slow_work_queue)) {
  223. work = list_entry(slow_work_queue.next,
  224. struct slow_work, link);
  225. if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
  226. BUG();
  227. list_del_init(&work->link);
  228. very_slow = false;
  229. } else {
  230. very_slow = false; /* avoid the compiler warning */
  231. }
  232. slow_work_set_thread_processing(id, work);
  233. if (work) {
  234. slow_work_mark_time(work);
  235. slow_work_begin_exec(id, work);
  236. }
  237. spin_unlock_irq(&slow_work_queue_lock);
  238. if (!work)
  239. return false;
  240. if (!test_and_clear_bit(SLOW_WORK_PENDING, &work->flags))
  241. BUG();
  242. /* don't execute if the work is in the process of being cancelled */
  243. if (!test_bit(SLOW_WORK_CANCELLING, &work->flags))
  244. work->ops->execute(work);
  245. if (very_slow)
  246. atomic_dec(&vslow_work_executing_count);
  247. clear_bit_unlock(SLOW_WORK_EXECUTING, &work->flags);
  248. /* wake up anyone waiting for this work to be complete */
  249. wake_up_bit(&work->flags, SLOW_WORK_EXECUTING);
  250. slow_work_end_exec(id, work);
  251. /* if someone tried to enqueue the item whilst we were executing it,
  252. * then it'll be left unenqueued to avoid multiple threads trying to
  253. * execute it simultaneously
  254. *
  255. * there is, however, a race between us testing the pending flag and
  256. * getting the spinlock, and between the enqueuer setting the pending
  257. * flag and getting the spinlock, so we use a deferral bit to tell us
  258. * if the enqueuer got there first
  259. */
  260. if (test_bit(SLOW_WORK_PENDING, &work->flags)) {
  261. spin_lock_irq(&slow_work_queue_lock);
  262. if (!test_bit(SLOW_WORK_EXECUTING, &work->flags) &&
  263. test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags))
  264. goto auto_requeue;
  265. spin_unlock_irq(&slow_work_queue_lock);
  266. }
  267. /* sort out the race between module unloading and put_ref() */
  268. slow_work_put_ref(work);
  269. slow_work_done_thread_processing(id, work);
  270. return true;
  271. auto_requeue:
  272. /* we must complete the enqueue operation
  273. * - we transfer our ref on the item back to the appropriate queue
  274. * - don't wake another thread up as we're awake already
  275. */
  276. slow_work_mark_time(work);
  277. if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
  278. list_add_tail(&work->link, &vslow_work_queue);
  279. else
  280. list_add_tail(&work->link, &slow_work_queue);
  281. spin_unlock_irq(&slow_work_queue_lock);
  282. slow_work_clear_thread_processing(id);
  283. return true;
  284. }
  285. /**
  286. * slow_work_sleep_till_thread_needed - Sleep till thread needed by other work
  287. * work: The work item under execution that wants to sleep
  288. * _timeout: Scheduler sleep timeout
  289. *
  290. * Allow a requeueable work item to sleep on a slow-work processor thread until
  291. * that thread is needed to do some other work or the sleep is interrupted by
  292. * some other event.
  293. *
  294. * The caller must set up a wake up event before calling this and must have set
  295. * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
  296. * condition before calling this function as no test is made here.
  297. *
  298. * False is returned if there is nothing on the queue; true is returned if the
  299. * work item should be requeued
  300. */
  301. bool slow_work_sleep_till_thread_needed(struct slow_work *work,
  302. signed long *_timeout)
  303. {
  304. wait_queue_head_t *wfo_wq;
  305. struct list_head *queue;
  306. DEFINE_WAIT(wait);
  307. if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags)) {
  308. wfo_wq = &vslow_work_queue_waits_for_occupation;
  309. queue = &vslow_work_queue;
  310. } else {
  311. wfo_wq = &slow_work_queue_waits_for_occupation;
  312. queue = &slow_work_queue;
  313. }
  314. if (!list_empty(queue))
  315. return true;
  316. add_wait_queue_exclusive(wfo_wq, &wait);
  317. if (list_empty(queue))
  318. *_timeout = schedule_timeout(*_timeout);
  319. finish_wait(wfo_wq, &wait);
  320. return !list_empty(queue);
  321. }
  322. EXPORT_SYMBOL(slow_work_sleep_till_thread_needed);
  323. /**
  324. * slow_work_enqueue - Schedule a slow work item for processing
  325. * @work: The work item to queue
  326. *
  327. * Schedule a slow work item for processing. If the item is already undergoing
  328. * execution, this guarantees not to re-enter the execution routine until the
  329. * first execution finishes.
  330. *
  331. * The item is pinned by this function as it retains a reference to it, managed
  332. * through the item operations. The item is unpinned once it has been
  333. * executed.
  334. *
  335. * An item may hog the thread that is running it for a relatively large amount
  336. * of time, sufficient, for example, to perform several lookup, mkdir, create
  337. * and setxattr operations. It may sleep on I/O and may sleep to obtain locks.
  338. *
  339. * Conversely, if a number of items are awaiting processing, it may take some
  340. * time before any given item is given attention. The number of threads in the
  341. * pool may be increased to deal with demand, but only up to a limit.
  342. *
  343. * If SLOW_WORK_VERY_SLOW is set on the work item, then it will be placed in
  344. * the very slow queue, from which only a portion of the threads will be
  345. * allowed to pick items to execute. This ensures that very slow items won't
  346. * overly block ones that are just ordinarily slow.
  347. *
  348. * Returns 0 if successful, -EAGAIN if not (or -ECANCELED if cancelled work is
  349. * attempted queued)
  350. */
  351. int slow_work_enqueue(struct slow_work *work)
  352. {
  353. wait_queue_head_t *wfo_wq;
  354. struct list_head *queue;
  355. unsigned long flags;
  356. int ret;
  357. if (test_bit(SLOW_WORK_CANCELLING, &work->flags))
  358. return -ECANCELED;
  359. BUG_ON(slow_work_user_count <= 0);
  360. BUG_ON(!work);
  361. BUG_ON(!work->ops);
  362. /* when honouring an enqueue request, we only promise that we will run
  363. * the work function in the future; we do not promise to run it once
  364. * per enqueue request
  365. *
  366. * we use the PENDING bit to merge together repeat requests without
  367. * having to disable IRQs and take the spinlock, whilst still
  368. * maintaining our promise
  369. */
  370. if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
  371. if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags)) {
  372. wfo_wq = &vslow_work_queue_waits_for_occupation;
  373. queue = &vslow_work_queue;
  374. } else {
  375. wfo_wq = &slow_work_queue_waits_for_occupation;
  376. queue = &slow_work_queue;
  377. }
  378. spin_lock_irqsave(&slow_work_queue_lock, flags);
  379. if (unlikely(test_bit(SLOW_WORK_CANCELLING, &work->flags)))
  380. goto cancelled;
  381. /* we promise that we will not attempt to execute the work
  382. * function in more than one thread simultaneously
  383. *
  384. * this, however, leaves us with a problem if we're asked to
  385. * enqueue the work whilst someone is executing the work
  386. * function as simply queueing the work immediately means that
  387. * another thread may try executing it whilst it is already
  388. * under execution
  389. *
  390. * to deal with this, we set the ENQ_DEFERRED bit instead of
  391. * enqueueing, and the thread currently executing the work
  392. * function will enqueue the work item when the work function
  393. * returns and it has cleared the EXECUTING bit
  394. */
  395. if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
  396. set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
  397. } else {
  398. ret = slow_work_get_ref(work);
  399. if (ret < 0)
  400. goto failed;
  401. slow_work_mark_time(work);
  402. list_add_tail(&work->link, queue);
  403. wake_up(&slow_work_thread_wq);
  404. /* if someone who could be requeued is sleeping on a
  405. * thread, then ask them to yield their thread */
  406. if (work->link.prev == queue)
  407. wake_up(wfo_wq);
  408. }
  409. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  410. }
  411. return 0;
  412. cancelled:
  413. ret = -ECANCELED;
  414. failed:
  415. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  416. return ret;
  417. }
  418. EXPORT_SYMBOL(slow_work_enqueue);
  419. static int slow_work_wait(void *word)
  420. {
  421. schedule();
  422. return 0;
  423. }
  424. /**
  425. * slow_work_cancel - Cancel a slow work item
  426. * @work: The work item to cancel
  427. *
  428. * This function will cancel a previously enqueued work item. If we cannot
  429. * cancel the work item, it is guarenteed to have run when this function
  430. * returns.
  431. */
  432. void slow_work_cancel(struct slow_work *work)
  433. {
  434. bool wait = true, put = false;
  435. set_bit(SLOW_WORK_CANCELLING, &work->flags);
  436. smp_mb();
  437. /* if the work item is a delayed work item with an active timer, we
  438. * need to wait for the timer to finish _before_ getting the spinlock,
  439. * lest we deadlock against the timer routine
  440. *
  441. * the timer routine will leave DELAYED set if it notices the
  442. * CANCELLING flag in time
  443. */
  444. if (test_bit(SLOW_WORK_DELAYED, &work->flags)) {
  445. struct delayed_slow_work *dwork =
  446. container_of(work, struct delayed_slow_work, work);
  447. del_timer_sync(&dwork->timer);
  448. }
  449. spin_lock_irq(&slow_work_queue_lock);
  450. if (test_bit(SLOW_WORK_DELAYED, &work->flags)) {
  451. /* the timer routine aborted or never happened, so we are left
  452. * holding the timer's reference on the item and should just
  453. * drop the pending flag and wait for any ongoing execution to
  454. * finish */
  455. struct delayed_slow_work *dwork =
  456. container_of(work, struct delayed_slow_work, work);
  457. BUG_ON(timer_pending(&dwork->timer));
  458. BUG_ON(!list_empty(&work->link));
  459. clear_bit(SLOW_WORK_DELAYED, &work->flags);
  460. put = true;
  461. clear_bit(SLOW_WORK_PENDING, &work->flags);
  462. } else if (test_bit(SLOW_WORK_PENDING, &work->flags) &&
  463. !list_empty(&work->link)) {
  464. /* the link in the pending queue holds a reference on the item
  465. * that we will need to release */
  466. list_del_init(&work->link);
  467. wait = false;
  468. put = true;
  469. clear_bit(SLOW_WORK_PENDING, &work->flags);
  470. } else if (test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags)) {
  471. /* the executor is holding our only reference on the item, so
  472. * we merely need to wait for it to finish executing */
  473. clear_bit(SLOW_WORK_PENDING, &work->flags);
  474. }
  475. spin_unlock_irq(&slow_work_queue_lock);
  476. /* the EXECUTING flag is set by the executor whilst the spinlock is set
  477. * and before the item is dequeued - so assuming the above doesn't
  478. * actually dequeue it, simply waiting for the EXECUTING flag to be
  479. * released here should be sufficient */
  480. if (wait)
  481. wait_on_bit(&work->flags, SLOW_WORK_EXECUTING, slow_work_wait,
  482. TASK_UNINTERRUPTIBLE);
  483. clear_bit(SLOW_WORK_CANCELLING, &work->flags);
  484. if (put)
  485. slow_work_put_ref(work);
  486. }
  487. EXPORT_SYMBOL(slow_work_cancel);
  488. /*
  489. * Handle expiry of the delay timer, indicating that a delayed slow work item
  490. * should now be queued if not cancelled
  491. */
  492. static void delayed_slow_work_timer(unsigned long data)
  493. {
  494. wait_queue_head_t *wfo_wq;
  495. struct list_head *queue;
  496. struct slow_work *work = (struct slow_work *) data;
  497. unsigned long flags;
  498. bool queued = false, put = false, first = false;
  499. if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags)) {
  500. wfo_wq = &vslow_work_queue_waits_for_occupation;
  501. queue = &vslow_work_queue;
  502. } else {
  503. wfo_wq = &slow_work_queue_waits_for_occupation;
  504. queue = &slow_work_queue;
  505. }
  506. spin_lock_irqsave(&slow_work_queue_lock, flags);
  507. if (likely(!test_bit(SLOW_WORK_CANCELLING, &work->flags))) {
  508. clear_bit(SLOW_WORK_DELAYED, &work->flags);
  509. if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
  510. /* we discard the reference the timer was holding in
  511. * favour of the one the executor holds */
  512. set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
  513. put = true;
  514. } else {
  515. slow_work_mark_time(work);
  516. list_add_tail(&work->link, queue);
  517. queued = true;
  518. if (work->link.prev == queue)
  519. first = true;
  520. }
  521. }
  522. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  523. if (put)
  524. slow_work_put_ref(work);
  525. if (first)
  526. wake_up(wfo_wq);
  527. if (queued)
  528. wake_up(&slow_work_thread_wq);
  529. }
  530. /**
  531. * delayed_slow_work_enqueue - Schedule a delayed slow work item for processing
  532. * @dwork: The delayed work item to queue
  533. * @delay: When to start executing the work, in jiffies from now
  534. *
  535. * This is similar to slow_work_enqueue(), but it adds a delay before the work
  536. * is actually queued for processing.
  537. *
  538. * The item can have delayed processing requested on it whilst it is being
  539. * executed. The delay will begin immediately, and if it expires before the
  540. * item finishes executing, the item will be placed back on the queue when it
  541. * has done executing.
  542. */
  543. int delayed_slow_work_enqueue(struct delayed_slow_work *dwork,
  544. unsigned long delay)
  545. {
  546. struct slow_work *work = &dwork->work;
  547. unsigned long flags;
  548. int ret;
  549. if (delay == 0)
  550. return slow_work_enqueue(&dwork->work);
  551. BUG_ON(slow_work_user_count <= 0);
  552. BUG_ON(!work);
  553. BUG_ON(!work->ops);
  554. if (test_bit(SLOW_WORK_CANCELLING, &work->flags))
  555. return -ECANCELED;
  556. if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
  557. spin_lock_irqsave(&slow_work_queue_lock, flags);
  558. if (test_bit(SLOW_WORK_CANCELLING, &work->flags))
  559. goto cancelled;
  560. /* the timer holds a reference whilst it is pending */
  561. ret = work->ops->get_ref(work);
  562. if (ret < 0)
  563. goto cant_get_ref;
  564. if (test_and_set_bit(SLOW_WORK_DELAYED, &work->flags))
  565. BUG();
  566. dwork->timer.expires = jiffies + delay;
  567. dwork->timer.data = (unsigned long) work;
  568. dwork->timer.function = delayed_slow_work_timer;
  569. add_timer(&dwork->timer);
  570. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  571. }
  572. return 0;
  573. cancelled:
  574. ret = -ECANCELED;
  575. cant_get_ref:
  576. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  577. return ret;
  578. }
  579. EXPORT_SYMBOL(delayed_slow_work_enqueue);
  580. /*
  581. * Schedule a cull of the thread pool at some time in the near future
  582. */
  583. static void slow_work_schedule_cull(void)
  584. {
  585. mod_timer(&slow_work_cull_timer,
  586. round_jiffies(jiffies + SLOW_WORK_CULL_TIMEOUT));
  587. }
  588. /*
  589. * Worker thread culling algorithm
  590. */
  591. static bool slow_work_cull_thread(void)
  592. {
  593. unsigned long flags;
  594. bool do_cull = false;
  595. spin_lock_irqsave(&slow_work_queue_lock, flags);
  596. if (slow_work_cull) {
  597. slow_work_cull = false;
  598. if (list_empty(&slow_work_queue) &&
  599. list_empty(&vslow_work_queue) &&
  600. atomic_read(&slow_work_thread_count) >
  601. slow_work_min_threads) {
  602. slow_work_schedule_cull();
  603. do_cull = true;
  604. }
  605. }
  606. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  607. return do_cull;
  608. }
  609. /*
  610. * Determine if there is slow work available for dispatch
  611. */
  612. static inline bool slow_work_available(int vsmax)
  613. {
  614. return !list_empty(&slow_work_queue) ||
  615. (!list_empty(&vslow_work_queue) &&
  616. atomic_read(&vslow_work_executing_count) < vsmax);
  617. }
  618. /*
  619. * Worker thread dispatcher
  620. */
  621. static int slow_work_thread(void *_data)
  622. {
  623. int vsmax, id;
  624. DEFINE_WAIT(wait);
  625. set_freezable();
  626. set_user_nice(current, -5);
  627. /* allocate ourselves an ID */
  628. spin_lock_irq(&slow_work_queue_lock);
  629. id = find_first_zero_bit(slow_work_ids, SLOW_WORK_THREAD_LIMIT);
  630. BUG_ON(id < 0 || id >= SLOW_WORK_THREAD_LIMIT);
  631. __set_bit(id, slow_work_ids);
  632. slow_work_set_thread_pid(id, current->pid);
  633. spin_unlock_irq(&slow_work_queue_lock);
  634. sprintf(current->comm, "kslowd%03u", id);
  635. for (;;) {
  636. vsmax = vslow_work_proportion;
  637. vsmax *= atomic_read(&slow_work_thread_count);
  638. vsmax /= 100;
  639. prepare_to_wait_exclusive(&slow_work_thread_wq, &wait,
  640. TASK_INTERRUPTIBLE);
  641. if (!freezing(current) &&
  642. !slow_work_threads_should_exit &&
  643. !slow_work_available(vsmax) &&
  644. !slow_work_cull)
  645. schedule();
  646. finish_wait(&slow_work_thread_wq, &wait);
  647. try_to_freeze();
  648. vsmax = vslow_work_proportion;
  649. vsmax *= atomic_read(&slow_work_thread_count);
  650. vsmax /= 100;
  651. if (slow_work_available(vsmax) && slow_work_execute(id)) {
  652. cond_resched();
  653. if (list_empty(&slow_work_queue) &&
  654. list_empty(&vslow_work_queue) &&
  655. atomic_read(&slow_work_thread_count) >
  656. slow_work_min_threads)
  657. slow_work_schedule_cull();
  658. continue;
  659. }
  660. if (slow_work_threads_should_exit)
  661. break;
  662. if (slow_work_cull && slow_work_cull_thread())
  663. break;
  664. }
  665. spin_lock_irq(&slow_work_queue_lock);
  666. slow_work_set_thread_pid(id, 0);
  667. __clear_bit(id, slow_work_ids);
  668. spin_unlock_irq(&slow_work_queue_lock);
  669. if (atomic_dec_and_test(&slow_work_thread_count))
  670. complete_and_exit(&slow_work_last_thread_exited, 0);
  671. return 0;
  672. }
  673. /*
  674. * Handle thread cull timer expiration
  675. */
  676. static void slow_work_cull_timeout(unsigned long data)
  677. {
  678. slow_work_cull = true;
  679. wake_up(&slow_work_thread_wq);
  680. }
  681. /*
  682. * Start a new slow work thread
  683. */
  684. static void slow_work_new_thread_execute(struct slow_work *work)
  685. {
  686. struct task_struct *p;
  687. if (slow_work_threads_should_exit)
  688. return;
  689. if (atomic_read(&slow_work_thread_count) >= slow_work_max_threads)
  690. return;
  691. if (!mutex_trylock(&slow_work_user_lock))
  692. return;
  693. slow_work_may_not_start_new_thread = true;
  694. atomic_inc(&slow_work_thread_count);
  695. p = kthread_run(slow_work_thread, NULL, "kslowd");
  696. if (IS_ERR(p)) {
  697. printk(KERN_DEBUG "Slow work thread pool: OOM\n");
  698. if (atomic_dec_and_test(&slow_work_thread_count))
  699. BUG(); /* we're running on a slow work thread... */
  700. mod_timer(&slow_work_oom_timer,
  701. round_jiffies(jiffies + SLOW_WORK_OOM_TIMEOUT));
  702. } else {
  703. /* ratelimit the starting of new threads */
  704. mod_timer(&slow_work_oom_timer, jiffies + 1);
  705. }
  706. mutex_unlock(&slow_work_user_lock);
  707. }
  708. static const struct slow_work_ops slow_work_new_thread_ops = {
  709. .owner = THIS_MODULE,
  710. .execute = slow_work_new_thread_execute,
  711. #ifdef CONFIG_SLOW_WORK_DEBUG
  712. .desc = slow_work_new_thread_desc,
  713. #endif
  714. };
  715. /*
  716. * post-OOM new thread start suppression expiration
  717. */
  718. static void slow_work_oom_timeout(unsigned long data)
  719. {
  720. slow_work_may_not_start_new_thread = false;
  721. }
  722. #ifdef CONFIG_SYSCTL
  723. /*
  724. * Handle adjustment of the minimum number of threads
  725. */
  726. static int slow_work_min_threads_sysctl(struct ctl_table *table, int write,
  727. void __user *buffer,
  728. size_t *lenp, loff_t *ppos)
  729. {
  730. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  731. int n;
  732. if (ret == 0) {
  733. mutex_lock(&slow_work_user_lock);
  734. if (slow_work_user_count > 0) {
  735. /* see if we need to start or stop threads */
  736. n = atomic_read(&slow_work_thread_count) -
  737. slow_work_min_threads;
  738. if (n < 0 && !slow_work_may_not_start_new_thread)
  739. slow_work_enqueue(&slow_work_new_thread);
  740. else if (n > 0)
  741. slow_work_schedule_cull();
  742. }
  743. mutex_unlock(&slow_work_user_lock);
  744. }
  745. return ret;
  746. }
  747. /*
  748. * Handle adjustment of the maximum number of threads
  749. */
  750. static int slow_work_max_threads_sysctl(struct ctl_table *table, int write,
  751. void __user *buffer,
  752. size_t *lenp, loff_t *ppos)
  753. {
  754. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  755. int n;
  756. if (ret == 0) {
  757. mutex_lock(&slow_work_user_lock);
  758. if (slow_work_user_count > 0) {
  759. /* see if we need to stop threads */
  760. n = slow_work_max_threads -
  761. atomic_read(&slow_work_thread_count);
  762. if (n < 0)
  763. slow_work_schedule_cull();
  764. }
  765. mutex_unlock(&slow_work_user_lock);
  766. }
  767. return ret;
  768. }
  769. #endif /* CONFIG_SYSCTL */
  770. /**
  771. * slow_work_register_user - Register a user of the facility
  772. * @module: The module about to make use of the facility
  773. *
  774. * Register a user of the facility, starting up the initial threads if there
  775. * aren't any other users at this point. This will return 0 if successful, or
  776. * an error if not.
  777. */
  778. int slow_work_register_user(struct module *module)
  779. {
  780. struct task_struct *p;
  781. int loop;
  782. mutex_lock(&slow_work_user_lock);
  783. if (slow_work_user_count == 0) {
  784. printk(KERN_NOTICE "Slow work thread pool: Starting up\n");
  785. init_completion(&slow_work_last_thread_exited);
  786. slow_work_threads_should_exit = false;
  787. slow_work_init(&slow_work_new_thread,
  788. &slow_work_new_thread_ops);
  789. slow_work_may_not_start_new_thread = false;
  790. slow_work_cull = false;
  791. /* start the minimum number of threads */
  792. for (loop = 0; loop < slow_work_min_threads; loop++) {
  793. atomic_inc(&slow_work_thread_count);
  794. p = kthread_run(slow_work_thread, NULL, "kslowd");
  795. if (IS_ERR(p))
  796. goto error;
  797. }
  798. printk(KERN_NOTICE "Slow work thread pool: Ready\n");
  799. }
  800. slow_work_user_count++;
  801. mutex_unlock(&slow_work_user_lock);
  802. return 0;
  803. error:
  804. if (atomic_dec_and_test(&slow_work_thread_count))
  805. complete(&slow_work_last_thread_exited);
  806. if (loop > 0) {
  807. printk(KERN_ERR "Slow work thread pool:"
  808. " Aborting startup on ENOMEM\n");
  809. slow_work_threads_should_exit = true;
  810. wake_up_all(&slow_work_thread_wq);
  811. wait_for_completion(&slow_work_last_thread_exited);
  812. printk(KERN_ERR "Slow work thread pool: Aborted\n");
  813. }
  814. mutex_unlock(&slow_work_user_lock);
  815. return PTR_ERR(p);
  816. }
  817. EXPORT_SYMBOL(slow_work_register_user);
  818. /*
  819. * wait for all outstanding items from the calling module to complete
  820. * - note that more items may be queued whilst we're waiting
  821. */
  822. static void slow_work_wait_for_items(struct module *module)
  823. {
  824. #ifdef CONFIG_MODULES
  825. DECLARE_WAITQUEUE(myself, current);
  826. struct slow_work *work;
  827. int loop;
  828. mutex_lock(&slow_work_unreg_sync_lock);
  829. add_wait_queue(&slow_work_unreg_wq, &myself);
  830. for (;;) {
  831. spin_lock_irq(&slow_work_queue_lock);
  832. /* first of all, we wait for the last queued item in each list
  833. * to be processed */
  834. list_for_each_entry_reverse(work, &vslow_work_queue, link) {
  835. if (work->owner == module) {
  836. set_current_state(TASK_UNINTERRUPTIBLE);
  837. slow_work_unreg_work_item = work;
  838. goto do_wait;
  839. }
  840. }
  841. list_for_each_entry_reverse(work, &slow_work_queue, link) {
  842. if (work->owner == module) {
  843. set_current_state(TASK_UNINTERRUPTIBLE);
  844. slow_work_unreg_work_item = work;
  845. goto do_wait;
  846. }
  847. }
  848. /* then we wait for the items being processed to finish */
  849. slow_work_unreg_module = module;
  850. smp_mb();
  851. for (loop = 0; loop < SLOW_WORK_THREAD_LIMIT; loop++) {
  852. if (slow_work_thread_processing[loop] == module)
  853. goto do_wait;
  854. }
  855. spin_unlock_irq(&slow_work_queue_lock);
  856. break; /* okay, we're done */
  857. do_wait:
  858. spin_unlock_irq(&slow_work_queue_lock);
  859. schedule();
  860. slow_work_unreg_work_item = NULL;
  861. slow_work_unreg_module = NULL;
  862. }
  863. remove_wait_queue(&slow_work_unreg_wq, &myself);
  864. mutex_unlock(&slow_work_unreg_sync_lock);
  865. #endif /* CONFIG_MODULES */
  866. }
  867. /**
  868. * slow_work_unregister_user - Unregister a user of the facility
  869. * @module: The module whose items should be cleared
  870. *
  871. * Unregister a user of the facility, killing all the threads if this was the
  872. * last one.
  873. *
  874. * This waits for all the work items belonging to the nominated module to go
  875. * away before proceeding.
  876. */
  877. void slow_work_unregister_user(struct module *module)
  878. {
  879. /* first of all, wait for all outstanding items from the calling module
  880. * to complete */
  881. if (module)
  882. slow_work_wait_for_items(module);
  883. /* then we can actually go about shutting down the facility if need
  884. * be */
  885. mutex_lock(&slow_work_user_lock);
  886. BUG_ON(slow_work_user_count <= 0);
  887. slow_work_user_count--;
  888. if (slow_work_user_count == 0) {
  889. printk(KERN_NOTICE "Slow work thread pool: Shutting down\n");
  890. slow_work_threads_should_exit = true;
  891. del_timer_sync(&slow_work_cull_timer);
  892. del_timer_sync(&slow_work_oom_timer);
  893. wake_up_all(&slow_work_thread_wq);
  894. wait_for_completion(&slow_work_last_thread_exited);
  895. printk(KERN_NOTICE "Slow work thread pool:"
  896. " Shut down complete\n");
  897. }
  898. mutex_unlock(&slow_work_user_lock);
  899. }
  900. EXPORT_SYMBOL(slow_work_unregister_user);
  901. /*
  902. * Initialise the slow work facility
  903. */
  904. static int __init init_slow_work(void)
  905. {
  906. unsigned nr_cpus = num_possible_cpus();
  907. if (slow_work_max_threads < nr_cpus)
  908. slow_work_max_threads = nr_cpus;
  909. #ifdef CONFIG_SYSCTL
  910. if (slow_work_max_max_threads < nr_cpus * 2)
  911. slow_work_max_max_threads = nr_cpus * 2;
  912. #endif
  913. #ifdef CONFIG_SLOW_WORK_DEBUG
  914. {
  915. struct dentry *dbdir;
  916. dbdir = debugfs_create_dir("slow_work", NULL);
  917. if (dbdir && !IS_ERR(dbdir))
  918. debugfs_create_file("runqueue", S_IFREG | 0400, dbdir,
  919. NULL, &slow_work_runqueue_fops);
  920. }
  921. #endif
  922. return 0;
  923. }
  924. subsys_initcall(init_slow_work);