pdflush.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * mm/pdflush.c - worker threads for writing back filesystem data
  3. *
  4. * Copyright (C) 2002, Linus Torvalds.
  5. *
  6. * 09Apr2002 akpm@zip.com.au
  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. /*
  24. * Minimum and maximum number of pdflush instances
  25. */
  26. #define MIN_PDFLUSH_THREADS 2
  27. #define MAX_PDFLUSH_THREADS 8
  28. static void start_one_pdflush_thread(void);
  29. /*
  30. * The pdflush threads are worker threads for writing back dirty data.
  31. * Ideally, we'd like one thread per active disk spindle. But the disk
  32. * topology is very hard to divine at this level. Instead, we take
  33. * care in various places to prevent more than one pdflush thread from
  34. * performing writeback against a single filesystem. pdflush threads
  35. * have the PF_FLUSHER flag set in current->flags to aid in this.
  36. */
  37. /*
  38. * All the pdflush threads. Protected by pdflush_lock
  39. */
  40. static LIST_HEAD(pdflush_list);
  41. static DEFINE_SPINLOCK(pdflush_lock);
  42. /*
  43. * The count of currently-running pdflush threads. Protected
  44. * by pdflush_lock.
  45. *
  46. * Readable by sysctl, but not writable. Published to userspace at
  47. * /proc/sys/vm/nr_pdflush_threads.
  48. */
  49. int nr_pdflush_threads = 0;
  50. /*
  51. * The time at which the pdflush thread pool last went empty
  52. */
  53. static unsigned long last_empty_jifs;
  54. /*
  55. * The pdflush thread.
  56. *
  57. * Thread pool management algorithm:
  58. *
  59. * - The minimum and maximum number of pdflush instances are bound
  60. * by MIN_PDFLUSH_THREADS and MAX_PDFLUSH_THREADS.
  61. *
  62. * - If there have been no idle pdflush instances for 1 second, create
  63. * a new one.
  64. *
  65. * - If the least-recently-went-to-sleep pdflush thread has been asleep
  66. * for more than one second, terminate a thread.
  67. */
  68. /*
  69. * A structure for passing work to a pdflush thread. Also for passing
  70. * state information between pdflush threads. Protected by pdflush_lock.
  71. */
  72. struct pdflush_work {
  73. struct task_struct *who; /* The thread */
  74. void (*fn)(unsigned long); /* A callback function */
  75. unsigned long arg0; /* An argument to the callback */
  76. struct list_head list; /* On pdflush_list, when idle */
  77. unsigned long when_i_went_to_sleep;
  78. };
  79. static int __pdflush(struct pdflush_work *my_work)
  80. {
  81. current->flags |= PF_FLUSHER | PF_SWAPWRITE;
  82. my_work->fn = NULL;
  83. my_work->who = current;
  84. INIT_LIST_HEAD(&my_work->list);
  85. spin_lock_irq(&pdflush_lock);
  86. nr_pdflush_threads++;
  87. for ( ; ; ) {
  88. struct pdflush_work *pdf;
  89. set_current_state(TASK_INTERRUPTIBLE);
  90. list_move(&my_work->list, &pdflush_list);
  91. my_work->when_i_went_to_sleep = jiffies;
  92. spin_unlock_irq(&pdflush_lock);
  93. schedule();
  94. if (try_to_freeze()) {
  95. spin_lock_irq(&pdflush_lock);
  96. continue;
  97. }
  98. spin_lock_irq(&pdflush_lock);
  99. if (!list_empty(&my_work->list)) {
  100. printk("pdflush: bogus wakeup!\n");
  101. my_work->fn = NULL;
  102. continue;
  103. }
  104. if (my_work->fn == NULL) {
  105. printk("pdflush: NULL work function\n");
  106. continue;
  107. }
  108. spin_unlock_irq(&pdflush_lock);
  109. (*my_work->fn)(my_work->arg0);
  110. /*
  111. * Thread creation: For how long have there been zero
  112. * available threads?
  113. */
  114. if (jiffies - last_empty_jifs > 1 * HZ) {
  115. /* unlocked list_empty() test is OK here */
  116. if (list_empty(&pdflush_list)) {
  117. /* unlocked test is OK here */
  118. if (nr_pdflush_threads < MAX_PDFLUSH_THREADS)
  119. start_one_pdflush_thread();
  120. }
  121. }
  122. spin_lock_irq(&pdflush_lock);
  123. my_work->fn = NULL;
  124. /*
  125. * Thread destruction: For how long has the sleepiest
  126. * thread slept?
  127. */
  128. if (list_empty(&pdflush_list))
  129. continue;
  130. if (nr_pdflush_threads <= MIN_PDFLUSH_THREADS)
  131. continue;
  132. pdf = list_entry(pdflush_list.prev, struct pdflush_work, list);
  133. if (jiffies - pdf->when_i_went_to_sleep > 1 * HZ) {
  134. /* Limit exit rate */
  135. pdf->when_i_went_to_sleep = jiffies;
  136. break; /* exeunt */
  137. }
  138. }
  139. nr_pdflush_threads--;
  140. spin_unlock_irq(&pdflush_lock);
  141. return 0;
  142. }
  143. /*
  144. * Of course, my_work wants to be just a local in __pdflush(). It is
  145. * separated out in this manner to hopefully prevent the compiler from
  146. * performing unfortunate optimisations against the auto variables. Because
  147. * these are visible to other tasks and CPUs. (No problem has actually
  148. * been observed. This is just paranoia).
  149. */
  150. static int pdflush(void *dummy)
  151. {
  152. struct pdflush_work my_work;
  153. cpumask_t cpus_allowed;
  154. /*
  155. * pdflush can spend a lot of time doing encryption via dm-crypt. We
  156. * don't want to do that at keventd's priority.
  157. */
  158. set_user_nice(current, 0);
  159. /*
  160. * Some configs put our parent kthread in a limited cpuset,
  161. * which kthread() overrides, forcing cpus_allowed == CPU_MASK_ALL.
  162. * Our needs are more modest - cut back to our cpusets cpus_allowed.
  163. * This is needed as pdflush's are dynamically created and destroyed.
  164. * The boottime pdflush's are easily placed w/o these 2 lines.
  165. */
  166. cpus_allowed = cpuset_cpus_allowed(current);
  167. set_cpus_allowed(current, cpus_allowed);
  168. return __pdflush(&my_work);
  169. }
  170. /*
  171. * Attempt to wake up a pdflush thread, and get it to do some work for you.
  172. * Returns zero if it indeed managed to find a worker thread, and passed your
  173. * payload to it.
  174. */
  175. int pdflush_operation(void (*fn)(unsigned long), unsigned long arg0)
  176. {
  177. unsigned long flags;
  178. int ret = 0;
  179. if (fn == NULL)
  180. BUG(); /* Hard to diagnose if it's deferred */
  181. spin_lock_irqsave(&pdflush_lock, flags);
  182. if (list_empty(&pdflush_list)) {
  183. spin_unlock_irqrestore(&pdflush_lock, flags);
  184. ret = -1;
  185. } else {
  186. struct pdflush_work *pdf;
  187. pdf = list_entry(pdflush_list.next, struct pdflush_work, list);
  188. list_del_init(&pdf->list);
  189. if (list_empty(&pdflush_list))
  190. last_empty_jifs = jiffies;
  191. pdf->fn = fn;
  192. pdf->arg0 = arg0;
  193. wake_up_process(pdf->who);
  194. spin_unlock_irqrestore(&pdflush_lock, flags);
  195. }
  196. return ret;
  197. }
  198. static void start_one_pdflush_thread(void)
  199. {
  200. kthread_run(pdflush, NULL, "pdflush");
  201. }
  202. static int __init pdflush_init(void)
  203. {
  204. int i;
  205. for (i = 0; i < MIN_PDFLUSH_THREADS; i++)
  206. start_one_pdflush_thread();
  207. return 0;
  208. }
  209. module_init(pdflush_init);