1
2
3
4
5
6
7
8
9
10
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/fs.h>
21#include <linux/writeback.h>
22#include <linux/kthread.h>
23
24
25
26
27
28#define MIN_PDFLUSH_THREADS 2
29#define MAX_PDFLUSH_THREADS 8
30
31static void start_one_pdflush_thread(void);
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46static LIST_HEAD(pdflush_list);
47static DEFINE_SPINLOCK(pdflush_lock);
48
49
50
51
52
53
54
55
56int nr_pdflush_threads = 0;
57
58
59
60
61static unsigned long last_empty_jifs;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82struct pdflush_work {
83 struct task_struct *who;
84 void (*fn)(unsigned long);
85 unsigned long arg0;
86 struct list_head list;
87 unsigned long when_i_went_to_sleep;
88};
89
90static int __pdflush(struct pdflush_work *my_work)
91{
92 current->flags |= PF_FLUSHER;
93 my_work->fn = NULL;
94 my_work->who = current;
95 INIT_LIST_HEAD(&my_work->list);
96
97 spin_lock_irq(&pdflush_lock);
98 nr_pdflush_threads++;
99 for ( ; ; ) {
100 struct pdflush_work *pdf;
101
102 set_current_state(TASK_INTERRUPTIBLE);
103 list_move(&my_work->list, &pdflush_list);
104 my_work->when_i_went_to_sleep = jiffies;
105 spin_unlock_irq(&pdflush_lock);
106
107 schedule();
108 if (try_to_freeze(PF_FREEZE)) {
109 spin_lock_irq(&pdflush_lock);
110 continue;
111 }
112
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;
118 }
119 if (my_work->fn == NULL) {
120 printk("pdflush: NULL work function\n");
121 continue;
122 }
123 spin_unlock_irq(&pdflush_lock);
124
125 (*my_work->fn)(my_work->arg0);
126
127
128
129
130
131 if (jiffies - last_empty_jifs > 1 * HZ) {
132
133 if (list_empty(&pdflush_list)) {
134
135 if (nr_pdflush_threads < MAX_PDFLUSH_THREADS)
136 start_one_pdflush_thread();
137 }
138 }
139
140 spin_lock_irq(&pdflush_lock);
141 my_work->fn = NULL;
142
143
144
145
146
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
154 pdf->when_i_went_to_sleep = jiffies;
155 break;
156 }
157 }
158 nr_pdflush_threads--;
159 spin_unlock_irq(&pdflush_lock);
160 return 0;
161}
162
163
164
165
166
167
168
169
170static int pdflush(void *dummy)
171{
172 struct pdflush_work my_work;
173
174
175
176
177
178 set_user_nice(current, 0);
179 return __pdflush(&my_work);
180}
181
182
183
184
185
186
187int pdflush_operation(void (*fn)(unsigned long), unsigned long arg0)
188{
189 unsigned long flags;
190 int ret = 0;
191
192 if (fn == NULL)
193 BUG();
194
195 spin_lock_irqsave(&pdflush_lock, flags);
196 if (list_empty(&pdflush_list)) {
197 spin_unlock_irqrestore(&pdflush_lock, flags);
198 ret = -1;
199 } else {
200 struct pdflush_work *pdf;
201
202 pdf = list_entry(pdflush_list.next, struct pdflush_work, list);
203 list_del_init(&pdf->list);
204 if (list_empty(&pdflush_list))
205 last_empty_jifs = jiffies;
206 pdf->fn = fn;
207 pdf->arg0 = arg0;
208 wake_up_process(pdf->who);
209 spin_unlock_irqrestore(&pdflush_lock, flags);
210 }
211 return ret;
212}
213
214static void start_one_pdflush_thread(void)
215{
216 kthread_run(pdflush, NULL, "pdflush");
217}
218
219static int __init pdflush_init(void)
220{
221 int i;
222
223 for (i = 0; i < MIN_PDFLUSH_THREADS; i++)
224 start_one_pdflush_thread();
225 return 0;
226}
227
228module_init(pdflush_init);
229