slow-work.c 30 KB

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