pdflush.c 6.3 KB

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