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