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