pdflush.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 time at which the pdflush thread pool last went empty
  53. */
  54. static unsigned long last_empty_jifs;
  55. /*
  56. * The pdflush thread.
  57. *
  58. * Thread pool management algorithm:
  59. *
  60. * - The minimum and maximum number of pdflush instances are bound
  61. * by MIN_PDFLUSH_THREADS and MAX_PDFLUSH_THREADS.
  62. *
  63. * - If there have been no idle pdflush instances for 1 second, create
  64. * a new one.
  65. *
  66. * - If the least-recently-went-to-sleep pdflush thread has been asleep
  67. * for more than one second, terminate a thread.
  68. */
  69. /*
  70. * A structure for passing work to a pdflush thread. Also for passing
  71. * state information between pdflush threads. Protected by pdflush_lock.
  72. */
  73. struct pdflush_work {
  74. struct task_struct *who; /* The thread */
  75. void (*fn)(unsigned long); /* A callback function */
  76. unsigned long arg0; /* An argument to the callback */
  77. struct list_head list; /* On pdflush_list, when idle */
  78. unsigned long when_i_went_to_sleep;
  79. };
  80. static int __pdflush(struct pdflush_work *my_work)
  81. {
  82. current->flags |= PF_FLUSHER | PF_SWAPWRITE;
  83. set_freezable();
  84. my_work->fn = NULL;
  85. my_work->who = current;
  86. INIT_LIST_HEAD(&my_work->list);
  87. spin_lock_irq(&pdflush_lock);
  88. for ( ; ; ) {
  89. struct pdflush_work *pdf;
  90. set_current_state(TASK_INTERRUPTIBLE);
  91. list_move(&my_work->list, &pdflush_list);
  92. my_work->when_i_went_to_sleep = jiffies;
  93. spin_unlock_irq(&pdflush_lock);
  94. schedule();
  95. try_to_freeze();
  96. spin_lock_irq(&pdflush_lock);
  97. if (!list_empty(&my_work->list)) {
  98. /*
  99. * Someone woke us up, but without removing our control
  100. * structure from the global list. swsusp will do this
  101. * in try_to_freeze()->refrigerator(). Handle it.
  102. */
  103. my_work->fn = NULL;
  104. continue;
  105. }
  106. if (my_work->fn == NULL) {
  107. printk("pdflush: bogus wakeup\n");
  108. continue;
  109. }
  110. spin_unlock_irq(&pdflush_lock);
  111. (*my_work->fn)(my_work->arg0);
  112. spin_lock_irq(&pdflush_lock);
  113. /*
  114. * Thread creation: For how long have there been zero
  115. * available threads?
  116. *
  117. * To throttle creation, we reset last_empty_jifs.
  118. */
  119. if (time_after(jiffies, last_empty_jifs + 1 * HZ)) {
  120. if (list_empty(&pdflush_list)) {
  121. if (nr_pdflush_threads < MAX_PDFLUSH_THREADS) {
  122. last_empty_jifs = jiffies;
  123. nr_pdflush_threads++;
  124. spin_unlock_irq(&pdflush_lock);
  125. start_one_pdflush_thread();
  126. spin_lock_irq(&pdflush_lock);
  127. }
  128. }
  129. }
  130. my_work->fn = NULL;
  131. /*
  132. * Thread destruction: For how long has the sleepiest
  133. * thread slept?
  134. */
  135. if (list_empty(&pdflush_list))
  136. continue;
  137. if (nr_pdflush_threads <= MIN_PDFLUSH_THREADS)
  138. continue;
  139. pdf = list_entry(pdflush_list.prev, struct pdflush_work, list);
  140. if (time_after(jiffies, pdf->when_i_went_to_sleep + 1 * HZ)) {
  141. /* Limit exit rate */
  142. pdf->when_i_went_to_sleep = jiffies;
  143. break; /* exeunt */
  144. }
  145. }
  146. nr_pdflush_threads--;
  147. spin_unlock_irq(&pdflush_lock);
  148. return 0;
  149. }
  150. /*
  151. * Of course, my_work wants to be just a local in __pdflush(). It is
  152. * separated out in this manner to hopefully prevent the compiler from
  153. * performing unfortunate optimisations against the auto variables. Because
  154. * these are visible to other tasks and CPUs. (No problem has actually
  155. * been observed. This is just paranoia).
  156. */
  157. static int pdflush(void *dummy)
  158. {
  159. struct pdflush_work my_work;
  160. cpumask_var_t cpus_allowed;
  161. /*
  162. * Since the caller doesn't even check kthread_run() worked, let's not
  163. * freak out too much if this fails.
  164. */
  165. if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
  166. printk(KERN_WARNING "pdflush failed to allocate cpumask\n");
  167. return 0;
  168. }
  169. /*
  170. * pdflush can spend a lot of time doing encryption via dm-crypt. We
  171. * don't want to do that at keventd's priority.
  172. */
  173. set_user_nice(current, 0);
  174. /*
  175. * Some configs put our parent kthread in a limited cpuset,
  176. * which kthread() overrides, forcing cpus_allowed == cpu_all_mask.
  177. * Our needs are more modest - cut back to our cpusets cpus_allowed.
  178. * This is needed as pdflush's are dynamically created and destroyed.
  179. * The boottime pdflush's are easily placed w/o these 2 lines.
  180. */
  181. cpuset_cpus_allowed(current, cpus_allowed);
  182. set_cpus_allowed_ptr(current, cpus_allowed);
  183. free_cpumask_var(cpus_allowed);
  184. return __pdflush(&my_work);
  185. }
  186. /*
  187. * Attempt to wake up a pdflush thread, and get it to do some work for you.
  188. * Returns zero if it indeed managed to find a worker thread, and passed your
  189. * payload to it.
  190. */
  191. int pdflush_operation(void (*fn)(unsigned long), unsigned long arg0)
  192. {
  193. unsigned long flags;
  194. int ret = 0;
  195. BUG_ON(fn == NULL); /* Hard to diagnose if it's deferred */
  196. spin_lock_irqsave(&pdflush_lock, flags);
  197. if (list_empty(&pdflush_list)) {
  198. ret = -1;
  199. } else {
  200. struct pdflush_work *pdf;
  201. pdf = list_entry(pdflush_list.next, struct pdflush_work, list);
  202. list_del_init(&pdf->list);
  203. if (list_empty(&pdflush_list))
  204. last_empty_jifs = jiffies;
  205. pdf->fn = fn;
  206. pdf->arg0 = arg0;
  207. wake_up_process(pdf->who);
  208. }
  209. spin_unlock_irqrestore(&pdflush_lock, flags);
  210. return ret;
  211. }
  212. static void start_one_pdflush_thread(void)
  213. {
  214. struct task_struct *k;
  215. k = kthread_run(pdflush, NULL, "pdflush");
  216. if (unlikely(IS_ERR(k))) {
  217. spin_lock_irq(&pdflush_lock);
  218. nr_pdflush_threads--;
  219. spin_unlock_irq(&pdflush_lock);
  220. }
  221. }
  222. static int __init pdflush_init(void)
  223. {
  224. int i;
  225. /*
  226. * Pre-set nr_pdflush_threads... If we fail to create,
  227. * the count will be decremented.
  228. */
  229. nr_pdflush_threads = MIN_PDFLUSH_THREADS;
  230. for (i = 0; i < MIN_PDFLUSH_THREADS; i++)
  231. start_one_pdflush_thread();
  232. return 0;
  233. }
  234. module_init(pdflush_init);