pdflush.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #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. my_work->fn = NULL;
  84. my_work->who = current;
  85. INIT_LIST_HEAD(&my_work->list);
  86. spin_lock_irq(&pdflush_lock);
  87. nr_pdflush_threads++;
  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. /*
  113. * Thread creation: For how long have there been zero
  114. * available threads?
  115. */
  116. if (jiffies - last_empty_jifs > 1 * HZ) {
  117. /* unlocked list_empty() test is OK here */
  118. if (list_empty(&pdflush_list)) {
  119. /* unlocked test is OK here */
  120. if (nr_pdflush_threads < MAX_PDFLUSH_THREADS)
  121. start_one_pdflush_thread();
  122. }
  123. }
  124. spin_lock_irq(&pdflush_lock);
  125. my_work->fn = NULL;
  126. /*
  127. * Thread destruction: For how long has the sleepiest
  128. * thread slept?
  129. */
  130. if (list_empty(&pdflush_list))
  131. continue;
  132. if (nr_pdflush_threads <= MIN_PDFLUSH_THREADS)
  133. continue;
  134. pdf = list_entry(pdflush_list.prev, struct pdflush_work, list);
  135. if (jiffies - pdf->when_i_went_to_sleep > 1 * HZ) {
  136. /* Limit exit rate */
  137. pdf->when_i_went_to_sleep = jiffies;
  138. break; /* exeunt */
  139. }
  140. }
  141. nr_pdflush_threads--;
  142. spin_unlock_irq(&pdflush_lock);
  143. return 0;
  144. }
  145. /*
  146. * Of course, my_work wants to be just a local in __pdflush(). It is
  147. * separated out in this manner to hopefully prevent the compiler from
  148. * performing unfortunate optimisations against the auto variables. Because
  149. * these are visible to other tasks and CPUs. (No problem has actually
  150. * been observed. This is just paranoia).
  151. */
  152. static int pdflush(void *dummy)
  153. {
  154. struct pdflush_work my_work;
  155. cpumask_t cpus_allowed;
  156. /*
  157. * pdflush can spend a lot of time doing encryption via dm-crypt. We
  158. * don't want to do that at keventd's priority.
  159. */
  160. set_user_nice(current, 0);
  161. /*
  162. * Some configs put our parent kthread in a limited cpuset,
  163. * which kthread() overrides, forcing cpus_allowed == CPU_MASK_ALL.
  164. * Our needs are more modest - cut back to our cpusets cpus_allowed.
  165. * This is needed as pdflush's are dynamically created and destroyed.
  166. * The boottime pdflush's are easily placed w/o these 2 lines.
  167. */
  168. cpus_allowed = cpuset_cpus_allowed(current);
  169. set_cpus_allowed(current, cpus_allowed);
  170. return __pdflush(&my_work);
  171. }
  172. /*
  173. * Attempt to wake up a pdflush thread, and get it to do some work for you.
  174. * Returns zero if it indeed managed to find a worker thread, and passed your
  175. * payload to it.
  176. */
  177. int pdflush_operation(void (*fn)(unsigned long), unsigned long arg0)
  178. {
  179. unsigned long flags;
  180. int ret = 0;
  181. BUG_ON(fn == NULL); /* Hard to diagnose if it's deferred */
  182. spin_lock_irqsave(&pdflush_lock, flags);
  183. if (list_empty(&pdflush_list)) {
  184. spin_unlock_irqrestore(&pdflush_lock, flags);
  185. ret = -1;
  186. } else {
  187. struct pdflush_work *pdf;
  188. pdf = list_entry(pdflush_list.next, struct pdflush_work, list);
  189. list_del_init(&pdf->list);
  190. if (list_empty(&pdflush_list))
  191. last_empty_jifs = jiffies;
  192. pdf->fn = fn;
  193. pdf->arg0 = arg0;
  194. wake_up_process(pdf->who);
  195. spin_unlock_irqrestore(&pdflush_lock, flags);
  196. }
  197. return ret;
  198. }
  199. static void start_one_pdflush_thread(void)
  200. {
  201. kthread_run(pdflush, NULL, "pdflush");
  202. }
  203. static int __init pdflush_init(void)
  204. {
  205. int i;
  206. for (i = 0; i < MIN_PDFLUSH_THREADS; i++)
  207. start_one_pdflush_thread();
  208. return 0;
  209. }
  210. module_init(pdflush_init);