pdflush.c 5.8 KB

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