pdflush.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * mm/pdflush.c - worker threads for writing back filesystem data
  3. *
  4. * Copyright (C) 2002, Linus Torvalds.
  5. *
  6. * 09Apr2002 Andrew Morton
  7. * Initial version
  8. * 29Feb2004 kaos@sgi.com
  9. * Move worker thread creation to kthread to avoid chewing
  10. * up stack space with nested calls to kernel_thread.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/list.h>
  14. #include <linux/signal.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/gfp.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/fs.h> /* Needed by writeback.h */
  20. #include <linux/writeback.h> /* Prototypes pdflush_operation() */
  21. #include <linux/kthread.h>
  22. #include <linux/cpuset.h>
  23. #include <linux/freezer.h>
  24. /*
  25. * Minimum and maximum number of pdflush instances
  26. */
  27. #define MIN_PDFLUSH_THREADS 2
  28. #define MAX_PDFLUSH_THREADS 8
  29. static void start_one_pdflush_thread(void);
  30. /*
  31. * The pdflush threads are worker threads for writing back dirty data.
  32. * Ideally, we'd like one thread per active disk spindle. But the disk
  33. * topology is very hard to divine at this level. Instead, we take
  34. * care in various places to prevent more than one pdflush thread from
  35. * performing writeback against a single filesystem. pdflush threads
  36. * have the PF_FLUSHER flag set in current->flags to aid in this.
  37. */
  38. /*
  39. * All the pdflush threads. Protected by pdflush_lock
  40. */
  41. static LIST_HEAD(pdflush_list);
  42. static DEFINE_SPINLOCK(pdflush_lock);
  43. /*
  44. * The count of currently-running pdflush threads. Protected
  45. * by pdflush_lock.
  46. *
  47. * Readable by sysctl, but not writable. Published to userspace at
  48. * /proc/sys/vm/nr_pdflush_threads.
  49. */
  50. int nr_pdflush_threads = 0;
  51. /*
  52. * The max/min number of pdflush threads. R/W by sysctl at
  53. * /proc/sys/vm/nr_pdflush_threads_max/min
  54. */
  55. int nr_pdflush_threads_max __read_mostly = MAX_PDFLUSH_THREADS;
  56. int nr_pdflush_threads_min __read_mostly = MIN_PDFLUSH_THREADS;
  57. /*
  58. * The time at which the pdflush thread pool last went empty
  59. */
  60. static unsigned long last_empty_jifs;
  61. /*
  62. * The pdflush thread.
  63. *
  64. * Thread pool management algorithm:
  65. *
  66. * - The minimum and maximum number of pdflush instances are bound
  67. * by nr_pdflush_threads_min and nr_pdflush_threads_max.
  68. *
  69. * - If there have been no idle pdflush instances for 1 second, create
  70. * a new one.
  71. *
  72. * - If the least-recently-went-to-sleep pdflush thread has been asleep
  73. * for more than one second, terminate a thread.
  74. */
  75. /*
  76. * A structure for passing work to a pdflush thread. Also for passing
  77. * state information between pdflush threads. Protected by pdflush_lock.
  78. */
  79. struct pdflush_work {
  80. struct task_struct *who; /* The thread */
  81. void (*fn)(unsigned long); /* A callback function */
  82. unsigned long arg0; /* An argument to the callback */
  83. struct list_head list; /* On pdflush_list, when idle */
  84. unsigned long when_i_went_to_sleep;
  85. };
  86. static int __pdflush(struct pdflush_work *my_work)
  87. {
  88. current->flags |= PF_FLUSHER | PF_SWAPWRITE;
  89. set_freezable();
  90. my_work->fn = NULL;
  91. my_work->who = current;
  92. INIT_LIST_HEAD(&my_work->list);
  93. spin_lock_irq(&pdflush_lock);
  94. for ( ; ; ) {
  95. struct pdflush_work *pdf;
  96. set_current_state(TASK_INTERRUPTIBLE);
  97. list_move(&my_work->list, &pdflush_list);
  98. my_work->when_i_went_to_sleep = jiffies;
  99. spin_unlock_irq(&pdflush_lock);
  100. schedule();
  101. try_to_freeze();
  102. spin_lock_irq(&pdflush_lock);
  103. if (!list_empty(&my_work->list)) {
  104. /*
  105. * Someone woke us up, but without removing our control
  106. * structure from the global list. swsusp will do this
  107. * in try_to_freeze()->refrigerator(). Handle it.
  108. */
  109. my_work->fn = NULL;
  110. continue;
  111. }
  112. if (my_work->fn == NULL) {
  113. printk("pdflush: bogus wakeup\n");
  114. continue;
  115. }
  116. spin_unlock_irq(&pdflush_lock);
  117. (*my_work->fn)(my_work->arg0);
  118. spin_lock_irq(&pdflush_lock);
  119. /*
  120. * Thread creation: For how long have there been zero
  121. * available threads?
  122. *
  123. * To throttle creation, we reset last_empty_jifs.
  124. */
  125. if (time_after(jiffies, last_empty_jifs + 1 * HZ)) {
  126. if (list_empty(&pdflush_list) &&
  127. nr_pdflush_threads < nr_pdflush_threads_max) {
  128. last_empty_jifs = jiffies;
  129. nr_pdflush_threads++;
  130. spin_unlock_irq(&pdflush_lock);
  131. start_one_pdflush_thread();
  132. spin_lock_irq(&pdflush_lock);
  133. }
  134. }
  135. my_work->fn = NULL;
  136. /*
  137. * Thread destruction: For how long has the sleepiest
  138. * thread slept?
  139. */
  140. if (list_empty(&pdflush_list))
  141. continue;
  142. if (nr_pdflush_threads <= nr_pdflush_threads_min)
  143. continue;
  144. pdf = list_entry(pdflush_list.prev, struct pdflush_work, list);
  145. if (time_after(jiffies, pdf->when_i_went_to_sleep + 1 * HZ)) {
  146. /* Limit exit rate */
  147. pdf->when_i_went_to_sleep = jiffies;
  148. break; /* exeunt */
  149. }
  150. }
  151. nr_pdflush_threads--;
  152. spin_unlock_irq(&pdflush_lock);
  153. return 0;
  154. }
  155. /*
  156. * Of course, my_work wants to be just a local in __pdflush(). It is
  157. * separated out in this manner to hopefully prevent the compiler from
  158. * performing unfortunate optimisations against the auto variables. Because
  159. * these are visible to other tasks and CPUs. (No problem has actually
  160. * been observed. This is just paranoia).
  161. */
  162. static int pdflush(void *dummy)
  163. {
  164. struct pdflush_work my_work;
  165. cpumask_var_t cpus_allowed;
  166. /*
  167. * Since the caller doesn't even check kthread_run() worked, let's not
  168. * freak out too much if this fails.
  169. */
  170. if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
  171. printk(KERN_WARNING "pdflush failed to allocate cpumask\n");
  172. return 0;
  173. }
  174. /*
  175. * pdflush can spend a lot of time doing encryption via dm-crypt. We
  176. * don't want to do that at keventd's priority.
  177. */
  178. set_user_nice(current, 0);
  179. /*
  180. * Some configs put our parent kthread in a limited cpuset,
  181. * which kthread() overrides, forcing cpus_allowed == cpu_all_mask.
  182. * Our needs are more modest - cut back to our cpusets cpus_allowed.
  183. * This is needed as pdflush's are dynamically created and destroyed.
  184. * The boottime pdflush's are easily placed w/o these 2 lines.
  185. */
  186. cpuset_cpus_allowed(current, cpus_allowed);
  187. set_cpus_allowed_ptr(current, cpus_allowed);
  188. free_cpumask_var(cpus_allowed);
  189. return __pdflush(&my_work);
  190. }
  191. /*
  192. * Attempt to wake up a pdflush thread, and get it to do some work for you.
  193. * Returns zero if it indeed managed to find a worker thread, and passed your
  194. * payload to it.
  195. */
  196. int pdflush_operation(void (*fn)(unsigned long), unsigned long arg0)
  197. {
  198. unsigned long flags;
  199. int ret = 0;
  200. BUG_ON(fn == NULL); /* Hard to diagnose if it's deferred */
  201. spin_lock_irqsave(&pdflush_lock, flags);
  202. if (list_empty(&pdflush_list)) {
  203. ret = -1;
  204. } else {
  205. struct pdflush_work *pdf;
  206. pdf = list_entry(pdflush_list.next, struct pdflush_work, list);
  207. list_del_init(&pdf->list);
  208. if (list_empty(&pdflush_list))
  209. last_empty_jifs = jiffies;
  210. pdf->fn = fn;
  211. pdf->arg0 = arg0;
  212. wake_up_process(pdf->who);
  213. }
  214. spin_unlock_irqrestore(&pdflush_lock, flags);
  215. return ret;
  216. }
  217. static void start_one_pdflush_thread(void)
  218. {
  219. struct task_struct *k;
  220. k = kthread_run(pdflush, NULL, "pdflush");
  221. if (unlikely(IS_ERR(k))) {
  222. spin_lock_irq(&pdflush_lock);
  223. nr_pdflush_threads--;
  224. spin_unlock_irq(&pdflush_lock);
  225. }
  226. }
  227. static int __init pdflush_init(void)
  228. {
  229. int i;
  230. /*
  231. * Pre-set nr_pdflush_threads... If we fail to create,
  232. * the count will be decremented.
  233. */
  234. nr_pdflush_threads = nr_pdflush_threads_min;
  235. for (i = 0; i < nr_pdflush_threads_min; i++)
  236. start_one_pdflush_thread();
  237. return 0;
  238. }
  239. module_init(pdflush_init);