[PATCH] Add a driver for the Technisat Skystar2 DVB card
[linux-2.6/history.git] / mm / pdflush.c
blobcebfb8c42f25d2145512a8de90d8516bddfe1e40
1 /*
2 * mm/pdflush.c - worker threads for writing back filesystem data
4 * Copyright (C) 2002, Linus Torvalds.
6 * 09Apr2002 akpm@zip.com.au
7 * Initial version
8 */
10 #include <linux/sched.h>
11 #include <linux/list.h>
12 #include <linux/signal.h>
13 #include <linux/spinlock.h>
14 #include <linux/gfp.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/suspend.h>
18 #include <linux/fs.h> // Needed by writeback.h
19 #include <linux/writeback.h> // Prototypes pdflush_operation()
23 * Minimum and maximum number of pdflush instances
25 #define MIN_PDFLUSH_THREADS 2
26 #define MAX_PDFLUSH_THREADS 8
28 static void start_one_pdflush_thread(void);
32 * The pdflush threads are worker threads for writing back dirty data.
33 * Ideally, we'd like one thread per active disk spindle. But the disk
34 * topology is very hard to divine at this level. Instead, we take
35 * care in various places to prevent more than one pdflush thread from
36 * performing writeback against a single filesystem. pdflush threads
37 * have the PF_FLUSHER flag set in current->flags to aid in this.
41 * All the pdflush threads. Protected by pdflush_lock
43 static LIST_HEAD(pdflush_list);
44 static spinlock_t pdflush_lock = SPIN_LOCK_UNLOCKED;
47 * The count of currently-running pdflush threads. Protected
48 * by pdflush_lock.
50 * Readable by sysctl, but not writable. Published to userspace at
51 * /proc/sys/vm/nr_pdflush_threads.
53 int nr_pdflush_threads = 0;
56 * The time at which the pdflush thread pool last went empty
58 static unsigned long last_empty_jifs;
61 * The pdflush thread.
63 * Thread pool management algorithm:
65 * - The minimum and maximum number of pdflush instances are bound
66 * by MIN_PDFLUSH_THREADS and MAX_PDFLUSH_THREADS.
68 * - If there have been no idle pdflush instances for 1 second, create
69 * a new one.
71 * - If the least-recently-went-to-sleep pdflush thread has been asleep
72 * for more than one second, terminate a thread.
76 * A structure for passing work to a pdflush thread. Also for passing
77 * state information between pdflush threads. Protected by pdflush_lock.
79 struct pdflush_work {
80 struct task_struct *who; /* The thread */
81 void (*fn)(unsigned long); /* A callback function */
82 unsigned long arg0; /* An argument to the callback */
83 struct list_head list; /* On pdflush_list, when idle */
84 unsigned long when_i_went_to_sleep;
87 static int __pdflush(struct pdflush_work *my_work)
89 daemonize("pdflush");
91 current->flags |= PF_FLUSHER;
92 my_work->fn = NULL;
93 my_work->who = current;
94 INIT_LIST_HEAD(&my_work->list);
96 spin_lock_irq(&pdflush_lock);
97 nr_pdflush_threads++;
98 for ( ; ; ) {
99 struct pdflush_work *pdf;
101 set_current_state(TASK_INTERRUPTIBLE);
102 list_move(&my_work->list, &pdflush_list);
103 my_work->when_i_went_to_sleep = jiffies;
104 spin_unlock_irq(&pdflush_lock);
106 schedule();
107 if (current->flags & PF_FREEZE) {
108 refrigerator(PF_IOTHREAD);
109 spin_lock_irq(&pdflush_lock);
110 continue;
113 spin_lock_irq(&pdflush_lock);
114 if (!list_empty(&my_work->list)) {
115 printk("pdflush: bogus wakeup!\n");
116 my_work->fn = NULL;
117 continue;
119 if (my_work->fn == NULL) {
120 printk("pdflush: NULL work function\n");
121 continue;
123 spin_unlock_irq(&pdflush_lock);
125 (*my_work->fn)(my_work->arg0);
128 * Thread creation: For how long have there been zero
129 * available threads?
131 if (jiffies - last_empty_jifs > 1 * HZ) {
132 /* unlocked list_empty() test is OK here */
133 if (list_empty(&pdflush_list)) {
134 /* unlocked test is OK here */
135 if (nr_pdflush_threads < MAX_PDFLUSH_THREADS)
136 start_one_pdflush_thread();
140 spin_lock_irq(&pdflush_lock);
141 my_work->fn = NULL;
144 * Thread destruction: For how long has the sleepiest
145 * thread slept?
147 if (list_empty(&pdflush_list))
148 continue;
149 if (nr_pdflush_threads <= MIN_PDFLUSH_THREADS)
150 continue;
151 pdf = list_entry(pdflush_list.prev, struct pdflush_work, list);
152 if (jiffies - pdf->when_i_went_to_sleep > 1 * HZ) {
153 /* Limit exit rate */
154 pdf->when_i_went_to_sleep = jiffies;
155 break; /* exeunt */
158 nr_pdflush_threads--;
159 spin_unlock_irq(&pdflush_lock);
160 return 0;
164 * Of course, my_work wants to be just a local in __pdflush(). It is
165 * separated out in this manner to hopefully prevent the compiler from
166 * performing unfortunate optimisations against the auto variables. Because
167 * these are visible to other tasks and CPUs. (No problem has actually
168 * been observed. This is just paranoia).
170 static int pdflush(void *dummy)
172 struct pdflush_work my_work;
173 return __pdflush(&my_work);
177 * Attempt to wake up a pdflush thread, and get it to do some work for you.
178 * Returns zero if it indeed managed to find a worker thread, and passed your
179 * payload to it.
181 int pdflush_operation(void (*fn)(unsigned long), unsigned long arg0)
183 unsigned long flags;
184 int ret = 0;
186 if (fn == NULL)
187 BUG(); /* Hard to diagnose if it's deferred */
189 spin_lock_irqsave(&pdflush_lock, flags);
190 if (list_empty(&pdflush_list)) {
191 spin_unlock_irqrestore(&pdflush_lock, flags);
192 ret = -1;
193 } else {
194 struct pdflush_work *pdf;
196 pdf = list_entry(pdflush_list.next, struct pdflush_work, list);
197 list_del_init(&pdf->list);
198 if (list_empty(&pdflush_list))
199 last_empty_jifs = jiffies;
200 pdf->fn = fn;
201 pdf->arg0 = arg0;
202 wake_up_process(pdf->who);
203 spin_unlock_irqrestore(&pdflush_lock, flags);
205 return ret;
208 static void start_one_pdflush_thread(void)
210 kernel_thread(pdflush, NULL, CLONE_KERNEL);
213 static int __init pdflush_init(void)
215 int i;
217 for (i = 0; i < MIN_PDFLUSH_THREADS; i++)
218 start_one_pdflush_thread();
219 return 0;
222 module_init(pdflush_init);