slow-work.c 12 KB

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