slow-work.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. #include <linux/module.h>
  12. #include <linux/slow-work.h>
  13. #include <linux/kthread.h>
  14. #include <linux/freezer.h>
  15. #include <linux/wait.h>
  16. #include <asm/system.h>
  17. #define SLOW_WORK_CULL_TIMEOUT (5 * HZ) /* cull threads 5s after running out of
  18. * things to do */
  19. #define SLOW_WORK_OOM_TIMEOUT (5 * HZ) /* can't start new threads for 5s after
  20. * OOM */
  21. static void slow_work_cull_timeout(unsigned long);
  22. static void slow_work_oom_timeout(unsigned long);
  23. /*
  24. * The pool of threads has at least min threads in it as long as someone is
  25. * using the facility, and may have as many as max.
  26. *
  27. * A portion of the pool may be processing very slow operations.
  28. */
  29. static unsigned slow_work_min_threads = 2;
  30. static unsigned slow_work_max_threads = 4;
  31. static unsigned vslow_work_proportion = 50; /* % of threads that may process
  32. * very slow work */
  33. static atomic_t slow_work_thread_count;
  34. static atomic_t vslow_work_executing_count;
  35. static bool slow_work_may_not_start_new_thread;
  36. static bool slow_work_cull; /* cull a thread due to lack of activity */
  37. static DEFINE_TIMER(slow_work_cull_timer, slow_work_cull_timeout, 0, 0);
  38. static DEFINE_TIMER(slow_work_oom_timer, slow_work_oom_timeout, 0, 0);
  39. static struct slow_work slow_work_new_thread; /* new thread starter */
  40. /*
  41. * The queues of work items and the lock governing access to them. These are
  42. * shared between all the CPUs. It doesn't make sense to have per-CPU queues
  43. * as the number of threads bears no relation to the number of CPUs.
  44. *
  45. * There are two queues of work items: one for slow work items, and one for
  46. * very slow work items.
  47. */
  48. static LIST_HEAD(slow_work_queue);
  49. static LIST_HEAD(vslow_work_queue);
  50. static DEFINE_SPINLOCK(slow_work_queue_lock);
  51. /*
  52. * The thread controls. A variable used to signal to the threads that they
  53. * should exit when the queue is empty, a waitqueue used by the threads to wait
  54. * for signals, and a completion set by the last thread to exit.
  55. */
  56. static bool slow_work_threads_should_exit;
  57. static DECLARE_WAIT_QUEUE_HEAD(slow_work_thread_wq);
  58. static DECLARE_COMPLETION(slow_work_last_thread_exited);
  59. /*
  60. * The number of users of the thread pool and its lock. Whilst this is zero we
  61. * have no threads hanging around, and when this reaches zero, we wait for all
  62. * active or queued work items to complete and kill all the threads we do have.
  63. */
  64. static int slow_work_user_count;
  65. static DEFINE_MUTEX(slow_work_user_lock);
  66. /*
  67. * Calculate the maximum number of active threads in the pool that are
  68. * permitted to process very slow work items.
  69. *
  70. * The answer is rounded up to at least 1, but may not equal or exceed the
  71. * maximum number of the threads in the pool. This means we always have at
  72. * least one thread that can process slow work items, and we always have at
  73. * least one thread that won't get tied up doing so.
  74. */
  75. static unsigned slow_work_calc_vsmax(void)
  76. {
  77. unsigned vsmax;
  78. vsmax = atomic_read(&slow_work_thread_count) * vslow_work_proportion;
  79. vsmax /= 100;
  80. vsmax = max(vsmax, 1U);
  81. return min(vsmax, slow_work_max_threads - 1);
  82. }
  83. /*
  84. * Attempt to execute stuff queued on a slow thread. Return true if we managed
  85. * it, false if there was nothing to do.
  86. */
  87. static bool slow_work_execute(void)
  88. {
  89. struct slow_work *work = NULL;
  90. unsigned vsmax;
  91. bool very_slow;
  92. vsmax = slow_work_calc_vsmax();
  93. /* see if we can schedule a new thread to be started if we're not
  94. * keeping up with the work */
  95. if (!waitqueue_active(&slow_work_thread_wq) &&
  96. (!list_empty(&slow_work_queue) || !list_empty(&vslow_work_queue)) &&
  97. atomic_read(&slow_work_thread_count) < slow_work_max_threads &&
  98. !slow_work_may_not_start_new_thread)
  99. slow_work_enqueue(&slow_work_new_thread);
  100. /* find something to execute */
  101. spin_lock_irq(&slow_work_queue_lock);
  102. if (!list_empty(&vslow_work_queue) &&
  103. atomic_read(&vslow_work_executing_count) < vsmax) {
  104. work = list_entry(vslow_work_queue.next,
  105. struct slow_work, link);
  106. if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
  107. BUG();
  108. list_del_init(&work->link);
  109. atomic_inc(&vslow_work_executing_count);
  110. very_slow = true;
  111. } else if (!list_empty(&slow_work_queue)) {
  112. work = list_entry(slow_work_queue.next,
  113. struct slow_work, link);
  114. if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
  115. BUG();
  116. list_del_init(&work->link);
  117. very_slow = false;
  118. } else {
  119. very_slow = false; /* avoid the compiler warning */
  120. }
  121. spin_unlock_irq(&slow_work_queue_lock);
  122. if (!work)
  123. return false;
  124. if (!test_and_clear_bit(SLOW_WORK_PENDING, &work->flags))
  125. BUG();
  126. work->ops->execute(work);
  127. if (very_slow)
  128. atomic_dec(&vslow_work_executing_count);
  129. clear_bit_unlock(SLOW_WORK_EXECUTING, &work->flags);
  130. /* if someone tried to enqueue the item whilst we were executing it,
  131. * then it'll be left unenqueued to avoid multiple threads trying to
  132. * execute it simultaneously
  133. *
  134. * there is, however, a race between us testing the pending flag and
  135. * getting the spinlock, and between the enqueuer setting the pending
  136. * flag and getting the spinlock, so we use a deferral bit to tell us
  137. * if the enqueuer got there first
  138. */
  139. if (test_bit(SLOW_WORK_PENDING, &work->flags)) {
  140. spin_lock_irq(&slow_work_queue_lock);
  141. if (!test_bit(SLOW_WORK_EXECUTING, &work->flags) &&
  142. test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags))
  143. goto auto_requeue;
  144. spin_unlock_irq(&slow_work_queue_lock);
  145. }
  146. work->ops->put_ref(work);
  147. return true;
  148. auto_requeue:
  149. /* we must complete the enqueue operation
  150. * - we transfer our ref on the item back to the appropriate queue
  151. * - don't wake another thread up as we're awake already
  152. */
  153. if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
  154. list_add_tail(&work->link, &vslow_work_queue);
  155. else
  156. list_add_tail(&work->link, &slow_work_queue);
  157. spin_unlock_irq(&slow_work_queue_lock);
  158. return true;
  159. }
  160. /**
  161. * slow_work_enqueue - Schedule a slow work item for processing
  162. * @work: The work item to queue
  163. *
  164. * Schedule a slow work item for processing. If the item is already undergoing
  165. * execution, this guarantees not to re-enter the execution routine until the
  166. * first execution finishes.
  167. *
  168. * The item is pinned by this function as it retains a reference to it, managed
  169. * through the item operations. The item is unpinned once it has been
  170. * executed.
  171. *
  172. * An item may hog the thread that is running it for a relatively large amount
  173. * of time, sufficient, for example, to perform several lookup, mkdir, create
  174. * and setxattr operations. It may sleep on I/O and may sleep to obtain locks.
  175. *
  176. * Conversely, if a number of items are awaiting processing, it may take some
  177. * time before any given item is given attention. The number of threads in the
  178. * pool may be increased to deal with demand, but only up to a limit.
  179. *
  180. * If SLOW_WORK_VERY_SLOW is set on the work item, then it will be placed in
  181. * the very slow queue, from which only a portion of the threads will be
  182. * allowed to pick items to execute. This ensures that very slow items won't
  183. * overly block ones that are just ordinarily slow.
  184. *
  185. * Returns 0 if successful, -EAGAIN if not.
  186. */
  187. int slow_work_enqueue(struct slow_work *work)
  188. {
  189. unsigned long flags;
  190. BUG_ON(slow_work_user_count <= 0);
  191. BUG_ON(!work);
  192. BUG_ON(!work->ops);
  193. BUG_ON(!work->ops->get_ref);
  194. /* when honouring an enqueue request, we only promise that we will run
  195. * the work function in the future; we do not promise to run it once
  196. * per enqueue request
  197. *
  198. * we use the PENDING bit to merge together repeat requests without
  199. * having to disable IRQs and take the spinlock, whilst still
  200. * maintaining our promise
  201. */
  202. if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
  203. spin_lock_irqsave(&slow_work_queue_lock, flags);
  204. /* we promise that we will not attempt to execute the work
  205. * function in more than one thread simultaneously
  206. *
  207. * this, however, leaves us with a problem if we're asked to
  208. * enqueue the work whilst someone is executing the work
  209. * function as simply queueing the work immediately means that
  210. * another thread may try executing it whilst it is already
  211. * under execution
  212. *
  213. * to deal with this, we set the ENQ_DEFERRED bit instead of
  214. * enqueueing, and the thread currently executing the work
  215. * function will enqueue the work item when the work function
  216. * returns and it has cleared the EXECUTING bit
  217. */
  218. if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
  219. set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
  220. } else {
  221. if (work->ops->get_ref(work) < 0)
  222. goto cant_get_ref;
  223. if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
  224. list_add_tail(&work->link, &vslow_work_queue);
  225. else
  226. list_add_tail(&work->link, &slow_work_queue);
  227. wake_up(&slow_work_thread_wq);
  228. }
  229. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  230. }
  231. return 0;
  232. cant_get_ref:
  233. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  234. return -EAGAIN;
  235. }
  236. EXPORT_SYMBOL(slow_work_enqueue);
  237. /*
  238. * Worker thread culling algorithm
  239. */
  240. static bool slow_work_cull_thread(void)
  241. {
  242. unsigned long flags;
  243. bool do_cull = false;
  244. spin_lock_irqsave(&slow_work_queue_lock, flags);
  245. if (slow_work_cull) {
  246. slow_work_cull = false;
  247. if (list_empty(&slow_work_queue) &&
  248. list_empty(&vslow_work_queue) &&
  249. atomic_read(&slow_work_thread_count) >
  250. slow_work_min_threads) {
  251. mod_timer(&slow_work_cull_timer,
  252. jiffies + SLOW_WORK_CULL_TIMEOUT);
  253. do_cull = true;
  254. }
  255. }
  256. spin_unlock_irqrestore(&slow_work_queue_lock, flags);
  257. return do_cull;
  258. }
  259. /*
  260. * Determine if there is slow work available for dispatch
  261. */
  262. static inline bool slow_work_available(int vsmax)
  263. {
  264. return !list_empty(&slow_work_queue) ||
  265. (!list_empty(&vslow_work_queue) &&
  266. atomic_read(&vslow_work_executing_count) < vsmax);
  267. }
  268. /*
  269. * Worker thread dispatcher
  270. */
  271. static int slow_work_thread(void *_data)
  272. {
  273. int vsmax;
  274. DEFINE_WAIT(wait);
  275. set_freezable();
  276. set_user_nice(current, -5);
  277. for (;;) {
  278. vsmax = vslow_work_proportion;
  279. vsmax *= atomic_read(&slow_work_thread_count);
  280. vsmax /= 100;
  281. prepare_to_wait(&slow_work_thread_wq, &wait,
  282. TASK_INTERRUPTIBLE);
  283. if (!freezing(current) &&
  284. !slow_work_threads_should_exit &&
  285. !slow_work_available(vsmax) &&
  286. !slow_work_cull)
  287. schedule();
  288. finish_wait(&slow_work_thread_wq, &wait);
  289. try_to_freeze();
  290. vsmax = vslow_work_proportion;
  291. vsmax *= atomic_read(&slow_work_thread_count);
  292. vsmax /= 100;
  293. if (slow_work_available(vsmax) && slow_work_execute()) {
  294. cond_resched();
  295. if (list_empty(&slow_work_queue) &&
  296. list_empty(&vslow_work_queue) &&
  297. atomic_read(&slow_work_thread_count) >
  298. slow_work_min_threads)
  299. mod_timer(&slow_work_cull_timer,
  300. jiffies + SLOW_WORK_CULL_TIMEOUT);
  301. continue;
  302. }
  303. if (slow_work_threads_should_exit)
  304. break;
  305. if (slow_work_cull && slow_work_cull_thread())
  306. break;
  307. }
  308. if (atomic_dec_and_test(&slow_work_thread_count))
  309. complete_and_exit(&slow_work_last_thread_exited, 0);
  310. return 0;
  311. }
  312. /*
  313. * Handle thread cull timer expiration
  314. */
  315. static void slow_work_cull_timeout(unsigned long data)
  316. {
  317. slow_work_cull = true;
  318. wake_up(&slow_work_thread_wq);
  319. }
  320. /*
  321. * Get a reference on slow work thread starter
  322. */
  323. static int slow_work_new_thread_get_ref(struct slow_work *work)
  324. {
  325. return 0;
  326. }
  327. /*
  328. * Drop a reference on slow work thread starter
  329. */
  330. static void slow_work_new_thread_put_ref(struct slow_work *work)
  331. {
  332. }
  333. /*
  334. * Start a new slow work thread
  335. */
  336. static void slow_work_new_thread_execute(struct slow_work *work)
  337. {
  338. struct task_struct *p;
  339. if (slow_work_threads_should_exit)
  340. return;
  341. if (atomic_read(&slow_work_thread_count) >= slow_work_max_threads)
  342. return;
  343. if (!mutex_trylock(&slow_work_user_lock))
  344. return;
  345. slow_work_may_not_start_new_thread = true;
  346. atomic_inc(&slow_work_thread_count);
  347. p = kthread_run(slow_work_thread, NULL, "kslowd");
  348. if (IS_ERR(p)) {
  349. printk(KERN_DEBUG "Slow work thread pool: OOM\n");
  350. if (atomic_dec_and_test(&slow_work_thread_count))
  351. BUG(); /* we're running on a slow work thread... */
  352. mod_timer(&slow_work_oom_timer,
  353. jiffies + SLOW_WORK_OOM_TIMEOUT);
  354. } else {
  355. /* ratelimit the starting of new threads */
  356. mod_timer(&slow_work_oom_timer, jiffies + 1);
  357. }
  358. mutex_unlock(&slow_work_user_lock);
  359. }
  360. static const struct slow_work_ops slow_work_new_thread_ops = {
  361. .get_ref = slow_work_new_thread_get_ref,
  362. .put_ref = slow_work_new_thread_put_ref,
  363. .execute = slow_work_new_thread_execute,
  364. };
  365. /*
  366. * post-OOM new thread start suppression expiration
  367. */
  368. static void slow_work_oom_timeout(unsigned long data)
  369. {
  370. slow_work_may_not_start_new_thread = false;
  371. }
  372. /**
  373. * slow_work_register_user - Register a user of the facility
  374. *
  375. * Register a user of the facility, starting up the initial threads if there
  376. * aren't any other users at this point. This will return 0 if successful, or
  377. * an error if not.
  378. */
  379. int slow_work_register_user(void)
  380. {
  381. struct task_struct *p;
  382. int loop;
  383. mutex_lock(&slow_work_user_lock);
  384. if (slow_work_user_count == 0) {
  385. printk(KERN_NOTICE "Slow work thread pool: Starting up\n");
  386. init_completion(&slow_work_last_thread_exited);
  387. slow_work_threads_should_exit = false;
  388. slow_work_init(&slow_work_new_thread,
  389. &slow_work_new_thread_ops);
  390. slow_work_may_not_start_new_thread = false;
  391. slow_work_cull = false;
  392. /* start the minimum number of threads */
  393. for (loop = 0; loop < slow_work_min_threads; loop++) {
  394. atomic_inc(&slow_work_thread_count);
  395. p = kthread_run(slow_work_thread, NULL, "kslowd");
  396. if (IS_ERR(p))
  397. goto error;
  398. }
  399. printk(KERN_NOTICE "Slow work thread pool: Ready\n");
  400. }
  401. slow_work_user_count++;
  402. mutex_unlock(&slow_work_user_lock);
  403. return 0;
  404. error:
  405. if (atomic_dec_and_test(&slow_work_thread_count))
  406. complete(&slow_work_last_thread_exited);
  407. if (loop > 0) {
  408. printk(KERN_ERR "Slow work thread pool:"
  409. " Aborting startup on ENOMEM\n");
  410. slow_work_threads_should_exit = true;
  411. wake_up_all(&slow_work_thread_wq);
  412. wait_for_completion(&slow_work_last_thread_exited);
  413. printk(KERN_ERR "Slow work thread pool: Aborted\n");
  414. }
  415. mutex_unlock(&slow_work_user_lock);
  416. return PTR_ERR(p);
  417. }
  418. EXPORT_SYMBOL(slow_work_register_user);
  419. /**
  420. * slow_work_unregister_user - Unregister a user of the facility
  421. *
  422. * Unregister a user of the facility, killing all the threads if this was the
  423. * last one.
  424. */
  425. void slow_work_unregister_user(void)
  426. {
  427. mutex_lock(&slow_work_user_lock);
  428. BUG_ON(slow_work_user_count <= 0);
  429. slow_work_user_count--;
  430. if (slow_work_user_count == 0) {
  431. printk(KERN_NOTICE "Slow work thread pool: Shutting down\n");
  432. slow_work_threads_should_exit = true;
  433. wake_up_all(&slow_work_thread_wq);
  434. wait_for_completion(&slow_work_last_thread_exited);
  435. printk(KERN_NOTICE "Slow work thread pool:"
  436. " Shut down complete\n");
  437. }
  438. del_timer_sync(&slow_work_cull_timer);
  439. mutex_unlock(&slow_work_user_lock);
  440. }
  441. EXPORT_SYMBOL(slow_work_unregister_user);
  442. /*
  443. * Initialise the slow work facility
  444. */
  445. static int __init init_slow_work(void)
  446. {
  447. unsigned nr_cpus = num_possible_cpus();
  448. if (nr_cpus > slow_work_max_threads)
  449. slow_work_max_threads = nr_cpus;
  450. return 0;
  451. }
  452. subsys_initcall(init_slow_work);