slow-work.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. #define SLOW_WORK_CULL_TIMEOUT (5 * HZ) /* cull threads 5s after running out of
  19. * things to do */
  20. #define SLOW_WORK_OOM_TIMEOUT (5 * HZ) /* can't start new threads for 5s after
  21. * OOM */
  22. #define SLOW_WORK_THREAD_LIMIT 255 /* abs maximum number of slow-work threads */
  23. static void slow_work_cull_timeout(unsigned long);
  24. static void slow_work_oom_timeout(unsigned long);
  25. #ifdef CONFIG_SYSCTL
  26. static int slow_work_min_threads_sysctl(struct ctl_table *, int,
  27. void __user *, size_t *, loff_t *);
  28. static int slow_work_max_threads_sysctl(struct ctl_table *, int ,
  29. void __user *, size_t *, loff_t *);
  30. #endif
  31. /*
  32. * The pool of threads has at least min threads in it as long as someone is
  33. * using the facility, and may have as many as max.
  34. *
  35. * A portion of the pool may be processing very slow operations.
  36. */
  37. static unsigned slow_work_min_threads = 2;
  38. static unsigned slow_work_max_threads = 4;
  39. static unsigned vslow_work_proportion = 50; /* % of threads that may process
  40. * very slow work */
  41. #ifdef CONFIG_SYSCTL
  42. static const int slow_work_min_min_threads = 2;
  43. static int slow_work_max_max_threads = SLOW_WORK_THREAD_LIMIT;
  44. static const int slow_work_min_vslow = 1;
  45. static const int slow_work_max_vslow = 99;
  46. ctl_table slow_work_sysctls[] = {
  47. {
  48. .ctl_name = CTL_UNNUMBERED,
  49. .procname = "min-threads",
  50. .data = &slow_work_min_threads,
  51. .maxlen = sizeof(unsigned),
  52. .mode = 0644,
  53. .proc_handler = slow_work_min_threads_sysctl,
  54. .extra1 = (void *) &slow_work_min_min_threads,
  55. .extra2 = &slow_work_max_threads,
  56. },
  57. {
  58. .ctl_name = CTL_UNNUMBERED,
  59. .procname = "max-threads",
  60. .data = &slow_work_max_threads,
  61. .maxlen = sizeof(unsigned),
  62. .mode = 0644,
  63. .proc_handler = slow_work_max_threads_sysctl,
  64. .extra1 = &slow_work_min_threads,
  65. .extra2 = (void *) &slow_work_max_max_threads,
  66. },
  67. {
  68. .ctl_name = CTL_UNNUMBERED,
  69. .procname = "vslow-percentage",
  70. .data = &vslow_work_proportion,
  71. .maxlen = sizeof(unsigned),
  72. .mode = 0644,
  73. .proc_handler = &proc_dointvec_minmax,
  74. .extra1 = (void *) &slow_work_min_vslow,
  75. .extra2 = (void *) &slow_work_max_vslow,
  76. },
  77. { .ctl_name = 0 }
  78. };
  79. #endif
  80. /*
  81. * The active state of the thread pool
  82. */
  83. static atomic_t slow_work_thread_count;
  84. static atomic_t vslow_work_executing_count;
  85. static bool slow_work_may_not_start_new_thread;
  86. static bool slow_work_cull; /* cull a thread due to lack of activity */
  87. static DEFINE_TIMER(slow_work_cull_timer, slow_work_cull_timeout, 0, 0);
  88. static DEFINE_TIMER(slow_work_oom_timer, slow_work_oom_timeout, 0, 0);
  89. static struct slow_work slow_work_new_thread; /* new thread starter */
  90. /*
  91. * slow work ID allocation (use slow_work_queue_lock)
  92. */
  93. static DECLARE_BITMAP(slow_work_ids, SLOW_WORK_THREAD_LIMIT);
  94. /*
  95. * Unregistration tracking to prevent put_ref() from disappearing during module
  96. * unload
  97. */
  98. #ifdef CONFIG_MODULES
  99. static struct module *slow_work_thread_processing[SLOW_WORK_THREAD_LIMIT];
  100. static struct module *slow_work_unreg_module;
  101. static struct slow_work *slow_work_unreg_work_item;
  102. static DECLARE_WAIT_QUEUE_HEAD(slow_work_unreg_wq);
  103. static DEFINE_MUTEX(slow_work_unreg_sync_lock);
  104. #endif
  105. /*
  106. * The queues of work items and the lock governing access to them. These are
  107. * shared between all the CPUs. It doesn't make sense to have per-CPU queues
  108. * as the number of threads bears no relation to the number of CPUs.
  109. *
  110. * There are two queues of work items: one for slow work items, and one for
  111. * very slow work items.
  112. */
  113. static LIST_HEAD(slow_work_queue);
  114. static LIST_HEAD(vslow_work_queue);
  115. static DEFINE_SPINLOCK(slow_work_queue_lock);
  116. /*
  117. * The thread controls. A variable used to signal to the threads that they
  118. * should exit when the queue is empty, a waitqueue used by the threads to wait
  119. * for signals, and a completion set by the last thread to exit.
  120. */
  121. static bool slow_work_threads_should_exit;
  122. static DECLARE_WAIT_QUEUE_HEAD(slow_work_thread_wq);
  123. static DECLARE_COMPLETION(slow_work_last_thread_exited);
  124. /*
  125. * The number of users of the thread pool and its lock. Whilst this is zero we
  126. * have no threads hanging around, and when this reaches zero, we wait for all
  127. * active or queued work items to complete and kill all the threads we do have.
  128. */
  129. static int slow_work_user_count;
  130. static DEFINE_MUTEX(slow_work_user_lock);
  131. static inline int slow_work_get_ref(struct slow_work *work)
  132. {
  133. if (work->ops->get_ref)
  134. return work->ops->get_ref(work);
  135. return 0;
  136. }
  137. static inline void slow_work_put_ref(struct slow_work *work)
  138. {
  139. if (work->ops->put_ref)
  140. work->ops->put_ref(work);
  141. }
  142. /*
  143. * Calculate the maximum number of active threads in the pool that are
  144. * permitted to process very slow work items.
  145. *
  146. * The answer is rounded up to at least 1, but may not equal or exceed the
  147. * maximum number of the threads in the pool. This means we always have at
  148. * least one thread that can process slow work items, and we always have at
  149. * least one thread that won't get tied up doing so.
  150. */
  151. static unsigned slow_work_calc_vsmax(void)
  152. {
  153. unsigned vsmax;
  154. vsmax = atomic_read(&slow_work_thread_count) * vslow_work_proportion;
  155. vsmax /= 100;
  156. vsmax = max(vsmax, 1U);
  157. return min(vsmax, slow_work_max_threads - 1);
  158. }
  159. /*
  160. * Attempt to execute stuff queued on a slow thread. Return true if we managed
  161. * it, false if there was nothing to do.
  162. */
  163. static bool slow_work_execute(int id)
  164. {
  165. #ifdef CONFIG_MODULES
  166. struct module *module;
  167. #endif
  168. struct slow_work *work = NULL;
  169. unsigned vsmax;
  170. bool very_slow;
  171. vsmax = slow_work_calc_vsmax();
  172. /* see if we can schedule a new thread to be started if we're not
  173. * keeping up with the work */
  174. if (!waitqueue_active(&slow_work_thread_wq) &&
  175. (!list_empty(&slow_work_queue) || !list_empty(&vslow_work_queue)) &&
  176. atomic_read(&slow_work_thread_count) < slow_work_max_threads &&
  177. !slow_work_may_not_start_new_thread)
  178. slow_work_enqueue(&slow_work_new_thread);
  179. /* find something to execute */
  180. spin_lock_irq(&slow_work_queue_lock);
  181. if (!list_empty(&vslow_work_queue) &&
  182. atomic_read(&vslow_work_executing_count) < vsmax) {
  183. work = list_entry(vslow_work_queue.next,
  184. struct slow_work, link);
  185. if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
  186. BUG();
  187. list_del_init(&work->link);
  188. atomic_inc(&vslow_work_executing_count);
  189. very_slow = true;
  190. } else if (!list_empty(&slow_work_queue)) {
  191. work = list_entry(slow_work_queue.next,
  192. struct slow_work, link);
  193. if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
  194. BUG();
  195. list_del_init(&work->link);
  196. very_slow = false;
  197. } else {
  198. very_slow = false; /* avoid the compiler warning */
  199. }
  200. #ifdef CONFIG_MODULES
  201. if (work)
  202. slow_work_thread_processing[id] = work->owner;
  203. #endif
  204. spin_unlock_irq(&slow_work_queue_lock);
  205. if (!work)
  206. return false;
  207. if (!test_and_clear_bit(SLOW_WORK_PENDING, &work->flags))
  208. BUG();
  209. /* don't execute if the work is in the process of being cancelled */
  210. if (!test_bit(SLOW_WORK_CANCELLING, &work->flags))
  211. work->ops->execute(work);
  212. if (very_slow)
  213. atomic_dec(&vslow_work_executing_count);
  214. clear_bit_unlock(SLOW_WORK_EXECUTING, &work->flags);
  215. /* wake up anyone waiting for this work to be complete */
  216. wake_up_bit(&work->flags, SLOW_WORK_EXECUTING);
  217. /* if someone tried to enqueue the item whilst we were executing it,
  218. * then it'll be left unenqueued to avoid multiple threads trying to
  219. * execute it simultaneously
  220. *
  221. * there is, however, a race between us testing the pending flag and
  222. * getting the spinlock, and between the enqueuer setting the pending
  223. * flag and getting the spinlock, so we use a deferral bit to tell us
  224. * if the enqueuer got there first
  225. */
  226. if (test_bit(SLOW_WORK_PENDING, &work->flags)) {
  227. spin_lock_irq(&slow_work_queue_lock);
  228. if (!test_bit(SLOW_WORK_EXECUTING, &work->flags) &&
  229. test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags))
  230. goto auto_requeue;
  231. spin_unlock_irq(&slow_work_queue_lock);
  232. }
  233. /* sort out the race between module unloading and put_ref() */
  234. slow_work_put_ref(work);
  235. #ifdef CONFIG_MODULES
  236. module = slow_work_thread_processing[id];
  237. slow_work_thread_processing[id] = NULL;
  238. smp_mb();
  239. if (slow_work_unreg_work_item == work ||
  240. slow_work_unreg_module == module)
  241. wake_up_all(&slow_work_unreg_wq);
  242. #endif
  243. return true;
  244. auto_requeue:
  245. /* we must complete the enqueue operation
  246. * - we transfer our ref on the item back to the appropriate queue
  247. * - don't wake another thread up as we're awake already
  248. */
  249. if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
  250. list_add_tail(&work->link, &vslow_work_queue);
  251. else
  252. list_add_tail(&work->link, &slow_work_queue);
  253. spin_unlock_irq(&slow_work_queue_lock);
  254. slow_work_thread_processing[id] = NULL;
  255. return true;
  256. }
  257. /**
  258. * slow_work_enqueue - Schedule a slow work item for processing
  259. * @work: The work item to queue
  260. *
  261. * Schedule a slow work item for processing. If the item is already undergoing
  262. * execution, this guarantees not to re-enter the execution routine until the
  263. * first execution finishes.
  264. *
  265. * The item is pinned by this function as it retains a reference to it, managed
  266. * through the item operations. The item is unpinned once it has been
  267. * executed.
  268. *
  269. * An item may hog the thread that is running it for a relatively large amount
  270. * of time, sufficient, for example, to perform several lookup, mkdir, create
  271. * and setxattr operations. It may sleep on I/O and may sleep to obtain locks.
  272. *
  273. * Conversely, if a number of items are awaiting processing, it may take some
  274. * time before any given item is given attention. The number of threads in the
  275. * pool may be increased to deal with demand, but only up to a limit.
  276. *
  277. * If SLOW_WORK_VERY_SLOW is set on the work item, then it will be placed in
  278. * the very slow queue, from which only a portion of the threads will be
  279. * allowed to pick items to execute. This ensures that very slow items won't
  280. * overly block ones that are just ordinarily slow.
  281. *
  282. * Returns 0 if successful, -EAGAIN if not (or -ECANCELED if cancelled work is
  283. * attempted queued)
  284. */
  285. int slow_work_enqueue(struct slow_work *work)
  286. {
  287. unsigned long flags;
  288. int ret;
  289. if (test_bit(SLOW_WORK_CANCELLING, &work->flags))
  290. return -ECANCELED;
  291. BUG_ON(slow_work_user_count <= 0);
  292. BUG_ON(!work);
  293. BUG_ON(!work->ops);
  294. /* when honouring an enqueue request, we only promise that we will run
  295. * the work function in the future; we do not promise to run it once
  296. * per enqueue request
  297. *
  298. * we use the PENDING bit to merge together repeat requests without
  299. * having to disable IRQs and take the spinlock, whilst still
  300. * maintaining our promise
  301. */
  302. if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
  303. spin_lock_irqsave(&slow_work_queue_lock, flags);
  304. if (unlikely(test_bit(SLOW_WORK_CANCELLING, &work->flags)))
  305. goto cancelled;
  306. /* we promise that we will not attempt to execute the work
  307. * function in more than one thread simultaneously
  308. *
  309. * this, however, leaves us with a problem if we're asked to
  310. * enqueue the work whilst someone is executing the work
  311. * function as simply queueing the work immediately means that
  312. * another thread may try executing it whilst it is already
  313. * under execution
  314. *
  315. * to deal with this, we set the ENQ_DEFERRED bit instead of
  316. * enqueueing, and the thread currently executing the work
  317. * function will enqueue the work item when the work function
  318. * returns and it has cleared the EXECUTING bit
  319. */
  320. if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
  321. set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
  322. } else {
  323. ret = slow_work_get_ref(work);
  324. if (ret < 0)
  325. goto failed;
  326. if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
  327. list_add_tail(&work->link, &vslow_work_queue);
  328. else
  329. list_add_tail(&work->link, &slow_work_queue);
  330. wake_up(&slow_work_thread_wq);
  331. }
  332. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  333. }
  334. return 0;
  335. cancelled:
  336. ret = -ECANCELED;
  337. failed:
  338. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  339. return ret;
  340. }
  341. EXPORT_SYMBOL(slow_work_enqueue);
  342. static int slow_work_wait(void *word)
  343. {
  344. schedule();
  345. return 0;
  346. }
  347. /**
  348. * slow_work_cancel - Cancel a slow work item
  349. * @work: The work item to cancel
  350. *
  351. * This function will cancel a previously enqueued work item. If we cannot
  352. * cancel the work item, it is guarenteed to have run when this function
  353. * returns.
  354. */
  355. void slow_work_cancel(struct slow_work *work)
  356. {
  357. bool wait = true, put = false;
  358. set_bit(SLOW_WORK_CANCELLING, &work->flags);
  359. spin_lock_irq(&slow_work_queue_lock);
  360. if (test_bit(SLOW_WORK_PENDING, &work->flags) &&
  361. !list_empty(&work->link)) {
  362. /* the link in the pending queue holds a reference on the item
  363. * that we will need to release */
  364. list_del_init(&work->link);
  365. wait = false;
  366. put = true;
  367. clear_bit(SLOW_WORK_PENDING, &work->flags);
  368. } else if (test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags)) {
  369. /* the executor is holding our only reference on the item, so
  370. * we merely need to wait for it to finish executing */
  371. clear_bit(SLOW_WORK_PENDING, &work->flags);
  372. }
  373. spin_unlock_irq(&slow_work_queue_lock);
  374. /* the EXECUTING flag is set by the executor whilst the spinlock is set
  375. * and before the item is dequeued - so assuming the above doesn't
  376. * actually dequeue it, simply waiting for the EXECUTING flag to be
  377. * released here should be sufficient */
  378. if (wait)
  379. wait_on_bit(&work->flags, SLOW_WORK_EXECUTING, slow_work_wait,
  380. TASK_UNINTERRUPTIBLE);
  381. clear_bit(SLOW_WORK_CANCELLING, &work->flags);
  382. if (put)
  383. slow_work_put_ref(work);
  384. }
  385. EXPORT_SYMBOL(slow_work_cancel);
  386. /*
  387. * Schedule a cull of the thread pool at some time in the near future
  388. */
  389. static void slow_work_schedule_cull(void)
  390. {
  391. mod_timer(&slow_work_cull_timer,
  392. round_jiffies(jiffies + SLOW_WORK_CULL_TIMEOUT));
  393. }
  394. /*
  395. * Worker thread culling algorithm
  396. */
  397. static bool slow_work_cull_thread(void)
  398. {
  399. unsigned long flags;
  400. bool do_cull = false;
  401. spin_lock_irqsave(&slow_work_queue_lock, flags);
  402. if (slow_work_cull) {
  403. slow_work_cull = false;
  404. if (list_empty(&slow_work_queue) &&
  405. list_empty(&vslow_work_queue) &&
  406. atomic_read(&slow_work_thread_count) >
  407. slow_work_min_threads) {
  408. slow_work_schedule_cull();
  409. do_cull = true;
  410. }
  411. }
  412. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  413. return do_cull;
  414. }
  415. /*
  416. * Determine if there is slow work available for dispatch
  417. */
  418. static inline bool slow_work_available(int vsmax)
  419. {
  420. return !list_empty(&slow_work_queue) ||
  421. (!list_empty(&vslow_work_queue) &&
  422. atomic_read(&vslow_work_executing_count) < vsmax);
  423. }
  424. /*
  425. * Worker thread dispatcher
  426. */
  427. static int slow_work_thread(void *_data)
  428. {
  429. int vsmax, id;
  430. DEFINE_WAIT(wait);
  431. set_freezable();
  432. set_user_nice(current, -5);
  433. /* allocate ourselves an ID */
  434. spin_lock_irq(&slow_work_queue_lock);
  435. id = find_first_zero_bit(slow_work_ids, SLOW_WORK_THREAD_LIMIT);
  436. BUG_ON(id < 0 || id >= SLOW_WORK_THREAD_LIMIT);
  437. __set_bit(id, slow_work_ids);
  438. spin_unlock_irq(&slow_work_queue_lock);
  439. sprintf(current->comm, "kslowd%03u", id);
  440. for (;;) {
  441. vsmax = vslow_work_proportion;
  442. vsmax *= atomic_read(&slow_work_thread_count);
  443. vsmax /= 100;
  444. prepare_to_wait_exclusive(&slow_work_thread_wq, &wait,
  445. TASK_INTERRUPTIBLE);
  446. if (!freezing(current) &&
  447. !slow_work_threads_should_exit &&
  448. !slow_work_available(vsmax) &&
  449. !slow_work_cull)
  450. schedule();
  451. finish_wait(&slow_work_thread_wq, &wait);
  452. try_to_freeze();
  453. vsmax = vslow_work_proportion;
  454. vsmax *= atomic_read(&slow_work_thread_count);
  455. vsmax /= 100;
  456. if (slow_work_available(vsmax) && slow_work_execute(id)) {
  457. cond_resched();
  458. if (list_empty(&slow_work_queue) &&
  459. list_empty(&vslow_work_queue) &&
  460. atomic_read(&slow_work_thread_count) >
  461. slow_work_min_threads)
  462. slow_work_schedule_cull();
  463. continue;
  464. }
  465. if (slow_work_threads_should_exit)
  466. break;
  467. if (slow_work_cull && slow_work_cull_thread())
  468. break;
  469. }
  470. spin_lock_irq(&slow_work_queue_lock);
  471. __clear_bit(id, slow_work_ids);
  472. spin_unlock_irq(&slow_work_queue_lock);
  473. if (atomic_dec_and_test(&slow_work_thread_count))
  474. complete_and_exit(&slow_work_last_thread_exited, 0);
  475. return 0;
  476. }
  477. /*
  478. * Handle thread cull timer expiration
  479. */
  480. static void slow_work_cull_timeout(unsigned long data)
  481. {
  482. slow_work_cull = true;
  483. wake_up(&slow_work_thread_wq);
  484. }
  485. /*
  486. * Start a new slow work thread
  487. */
  488. static void slow_work_new_thread_execute(struct slow_work *work)
  489. {
  490. struct task_struct *p;
  491. if (slow_work_threads_should_exit)
  492. return;
  493. if (atomic_read(&slow_work_thread_count) >= slow_work_max_threads)
  494. return;
  495. if (!mutex_trylock(&slow_work_user_lock))
  496. return;
  497. slow_work_may_not_start_new_thread = true;
  498. atomic_inc(&slow_work_thread_count);
  499. p = kthread_run(slow_work_thread, NULL, "kslowd");
  500. if (IS_ERR(p)) {
  501. printk(KERN_DEBUG "Slow work thread pool: OOM\n");
  502. if (atomic_dec_and_test(&slow_work_thread_count))
  503. BUG(); /* we're running on a slow work thread... */
  504. mod_timer(&slow_work_oom_timer,
  505. round_jiffies(jiffies + SLOW_WORK_OOM_TIMEOUT));
  506. } else {
  507. /* ratelimit the starting of new threads */
  508. mod_timer(&slow_work_oom_timer, jiffies + 1);
  509. }
  510. mutex_unlock(&slow_work_user_lock);
  511. }
  512. static const struct slow_work_ops slow_work_new_thread_ops = {
  513. .owner = THIS_MODULE,
  514. .execute = slow_work_new_thread_execute,
  515. };
  516. /*
  517. * post-OOM new thread start suppression expiration
  518. */
  519. static void slow_work_oom_timeout(unsigned long data)
  520. {
  521. slow_work_may_not_start_new_thread = false;
  522. }
  523. #ifdef CONFIG_SYSCTL
  524. /*
  525. * Handle adjustment of the minimum number of threads
  526. */
  527. static int slow_work_min_threads_sysctl(struct ctl_table *table, int write,
  528. void __user *buffer,
  529. size_t *lenp, loff_t *ppos)
  530. {
  531. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  532. int n;
  533. if (ret == 0) {
  534. mutex_lock(&slow_work_user_lock);
  535. if (slow_work_user_count > 0) {
  536. /* see if we need to start or stop threads */
  537. n = atomic_read(&slow_work_thread_count) -
  538. slow_work_min_threads;
  539. if (n < 0 && !slow_work_may_not_start_new_thread)
  540. slow_work_enqueue(&slow_work_new_thread);
  541. else if (n > 0)
  542. slow_work_schedule_cull();
  543. }
  544. mutex_unlock(&slow_work_user_lock);
  545. }
  546. return ret;
  547. }
  548. /*
  549. * Handle adjustment of the maximum number of threads
  550. */
  551. static int slow_work_max_threads_sysctl(struct ctl_table *table, int write,
  552. void __user *buffer,
  553. size_t *lenp, loff_t *ppos)
  554. {
  555. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  556. int n;
  557. if (ret == 0) {
  558. mutex_lock(&slow_work_user_lock);
  559. if (slow_work_user_count > 0) {
  560. /* see if we need to stop threads */
  561. n = slow_work_max_threads -
  562. atomic_read(&slow_work_thread_count);
  563. if (n < 0)
  564. slow_work_schedule_cull();
  565. }
  566. mutex_unlock(&slow_work_user_lock);
  567. }
  568. return ret;
  569. }
  570. #endif /* CONFIG_SYSCTL */
  571. /**
  572. * slow_work_register_user - Register a user of the facility
  573. * @module: The module about to make use of the facility
  574. *
  575. * Register a user of the facility, starting up the initial threads if there
  576. * aren't any other users at this point. This will return 0 if successful, or
  577. * an error if not.
  578. */
  579. int slow_work_register_user(struct module *module)
  580. {
  581. struct task_struct *p;
  582. int loop;
  583. mutex_lock(&slow_work_user_lock);
  584. if (slow_work_user_count == 0) {
  585. printk(KERN_NOTICE "Slow work thread pool: Starting up\n");
  586. init_completion(&slow_work_last_thread_exited);
  587. slow_work_threads_should_exit = false;
  588. slow_work_init(&slow_work_new_thread,
  589. &slow_work_new_thread_ops);
  590. slow_work_may_not_start_new_thread = false;
  591. slow_work_cull = false;
  592. /* start the minimum number of threads */
  593. for (loop = 0; loop < slow_work_min_threads; loop++) {
  594. atomic_inc(&slow_work_thread_count);
  595. p = kthread_run(slow_work_thread, NULL, "kslowd");
  596. if (IS_ERR(p))
  597. goto error;
  598. }
  599. printk(KERN_NOTICE "Slow work thread pool: Ready\n");
  600. }
  601. slow_work_user_count++;
  602. mutex_unlock(&slow_work_user_lock);
  603. return 0;
  604. error:
  605. if (atomic_dec_and_test(&slow_work_thread_count))
  606. complete(&slow_work_last_thread_exited);
  607. if (loop > 0) {
  608. printk(KERN_ERR "Slow work thread pool:"
  609. " Aborting startup on ENOMEM\n");
  610. slow_work_threads_should_exit = true;
  611. wake_up_all(&slow_work_thread_wq);
  612. wait_for_completion(&slow_work_last_thread_exited);
  613. printk(KERN_ERR "Slow work thread pool: Aborted\n");
  614. }
  615. mutex_unlock(&slow_work_user_lock);
  616. return PTR_ERR(p);
  617. }
  618. EXPORT_SYMBOL(slow_work_register_user);
  619. /*
  620. * wait for all outstanding items from the calling module to complete
  621. * - note that more items may be queued whilst we're waiting
  622. */
  623. static void slow_work_wait_for_items(struct module *module)
  624. {
  625. DECLARE_WAITQUEUE(myself, current);
  626. struct slow_work *work;
  627. int loop;
  628. mutex_lock(&slow_work_unreg_sync_lock);
  629. add_wait_queue(&slow_work_unreg_wq, &myself);
  630. for (;;) {
  631. spin_lock_irq(&slow_work_queue_lock);
  632. /* first of all, we wait for the last queued item in each list
  633. * to be processed */
  634. list_for_each_entry_reverse(work, &vslow_work_queue, link) {
  635. if (work->owner == module) {
  636. set_current_state(TASK_UNINTERRUPTIBLE);
  637. slow_work_unreg_work_item = work;
  638. goto do_wait;
  639. }
  640. }
  641. list_for_each_entry_reverse(work, &slow_work_queue, link) {
  642. if (work->owner == module) {
  643. set_current_state(TASK_UNINTERRUPTIBLE);
  644. slow_work_unreg_work_item = work;
  645. goto do_wait;
  646. }
  647. }
  648. /* then we wait for the items being processed to finish */
  649. slow_work_unreg_module = module;
  650. smp_mb();
  651. for (loop = 0; loop < SLOW_WORK_THREAD_LIMIT; loop++) {
  652. if (slow_work_thread_processing[loop] == module)
  653. goto do_wait;
  654. }
  655. spin_unlock_irq(&slow_work_queue_lock);
  656. break; /* okay, we're done */
  657. do_wait:
  658. spin_unlock_irq(&slow_work_queue_lock);
  659. schedule();
  660. slow_work_unreg_work_item = NULL;
  661. slow_work_unreg_module = NULL;
  662. }
  663. remove_wait_queue(&slow_work_unreg_wq, &myself);
  664. mutex_unlock(&slow_work_unreg_sync_lock);
  665. }
  666. /**
  667. * slow_work_unregister_user - Unregister a user of the facility
  668. * @module: The module whose items should be cleared
  669. *
  670. * Unregister a user of the facility, killing all the threads if this was the
  671. * last one.
  672. *
  673. * This waits for all the work items belonging to the nominated module to go
  674. * away before proceeding.
  675. */
  676. void slow_work_unregister_user(struct module *module)
  677. {
  678. /* first of all, wait for all outstanding items from the calling module
  679. * to complete */
  680. if (module)
  681. slow_work_wait_for_items(module);
  682. /* then we can actually go about shutting down the facility if need
  683. * be */
  684. mutex_lock(&slow_work_user_lock);
  685. BUG_ON(slow_work_user_count <= 0);
  686. slow_work_user_count--;
  687. if (slow_work_user_count == 0) {
  688. printk(KERN_NOTICE "Slow work thread pool: Shutting down\n");
  689. slow_work_threads_should_exit = true;
  690. del_timer_sync(&slow_work_cull_timer);
  691. del_timer_sync(&slow_work_oom_timer);
  692. wake_up_all(&slow_work_thread_wq);
  693. wait_for_completion(&slow_work_last_thread_exited);
  694. printk(KERN_NOTICE "Slow work thread pool:"
  695. " Shut down complete\n");
  696. }
  697. mutex_unlock(&slow_work_user_lock);
  698. }
  699. EXPORT_SYMBOL(slow_work_unregister_user);
  700. /*
  701. * Initialise the slow work facility
  702. */
  703. static int __init init_slow_work(void)
  704. {
  705. unsigned nr_cpus = num_possible_cpus();
  706. if (slow_work_max_threads < nr_cpus)
  707. slow_work_max_threads = nr_cpus;
  708. #ifdef CONFIG_SYSCTL
  709. if (slow_work_max_max_threads < nr_cpus * 2)
  710. slow_work_max_max_threads = nr_cpus * 2;
  711. #endif
  712. return 0;
  713. }
  714. subsys_initcall(init_slow_work);