aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2009-04-03 11:42:35 -0400
committerDavid Howells <dhowells@redhat.com>2009-04-03 11:42:35 -0400
commit07fe7cb7c7c179f473fd9c823348fd3eb5dad369 (patch)
tree516c959de929a4c39870629e550b3307601fa73f
parent8fe74cf053de7ad2124a894996f84fa890a81093 (diff)
Create a dynamically sized pool of threads for doing very slow work items
Create a dynamically sized pool of threads for doing very slow work items, such as invoking mkdir() or rmdir() - things that may take a long time and may sleep, holding mutexes/semaphores and hogging a thread, and are thus unsuitable for workqueues. The number of threads is always at least a settable minimum, but more are started when there's more work to do, up to a limit. Because of the nature of the load, it's not suitable for a 1-thread-per-CPU type pool. A system with one CPU may well want several threads. This is used by FS-Cache to do slow caching operations in the background, such as looking up, creating or deleting cache objects. Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: Serge Hallyn <serue@us.ibm.com> Acked-by: Steve Dickson <steved@redhat.com> Acked-by: Trond Myklebust <Trond.Myklebust@netapp.com> Acked-by: Al Viro <viro@zeniv.linux.org.uk> Tested-by: Daire Byrne <Daire.Byrne@framestore.com>
-rw-r--r--include/linux/slow-work.h88
-rw-r--r--init/Kconfig12
-rw-r--r--kernel/Makefile1
-rw-r--r--kernel/slow-work.c388
4 files changed, 489 insertions, 0 deletions
diff --git a/include/linux/slow-work.h b/include/linux/slow-work.h
new file mode 100644
index 000000000000..4dd754af393e
--- /dev/null
+++ b/include/linux/slow-work.h
@@ -0,0 +1,88 @@
1/* Worker thread pool for slow items, such as filesystem lookups or mkdirs
2 *
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#ifndef _LINUX_SLOW_WORK_H
13#define _LINUX_SLOW_WORK_H
14
15#ifdef CONFIG_SLOW_WORK
16
17struct slow_work;
18
19/*
20 * The operations used to support slow work items
21 */
22struct slow_work_ops {
23 /* get a ref on a work item
24 * - return 0 if successful, -ve if not
25 */
26 int (*get_ref)(struct slow_work *work);
27
28 /* discard a ref to a work item */
29 void (*put_ref)(struct slow_work *work);
30
31 /* execute a work item */
32 void (*execute)(struct slow_work *work);
33};
34
35/*
36 * A slow work item
37 * - A reference is held on the parent object by the thread pool when it is
38 * queued
39 */
40struct slow_work {
41 unsigned long flags;
42#define SLOW_WORK_PENDING 0 /* item pending (further) execution */
43#define SLOW_WORK_EXECUTING 1 /* item currently executing */
44#define SLOW_WORK_ENQ_DEFERRED 2 /* item enqueue deferred */
45#define SLOW_WORK_VERY_SLOW 3 /* item is very slow */
46 const struct slow_work_ops *ops; /* operations table for this item */
47 struct list_head link; /* link in queue */
48};
49
50/**
51 * slow_work_init - Initialise a slow work item
52 * @work: The work item to initialise
53 * @ops: The operations to use to handle the slow work item
54 *
55 * Initialise a slow work item.
56 */
57static inline void slow_work_init(struct slow_work *work,
58 const struct slow_work_ops *ops)
59{
60 work->flags = 0;
61 work->ops = ops;
62 INIT_LIST_HEAD(&work->link);
63}
64
65/**
66 * slow_work_init - Initialise a very slow work item
67 * @work: The work item to initialise
68 * @ops: The operations to use to handle the slow work item
69 *
70 * Initialise a very slow work item. This item will be restricted such that
71 * only a certain number of the pool threads will be able to execute items of
72 * this type.
73 */
74static inline void vslow_work_init(struct slow_work *work,
75 const struct slow_work_ops *ops)
76{
77 work->flags = 1 << SLOW_WORK_VERY_SLOW;
78 work->ops = ops;
79 INIT_LIST_HEAD(&work->link);
80}
81
82extern int slow_work_enqueue(struct slow_work *work);
83extern int slow_work_register_user(void);
84extern void slow_work_unregister_user(void);
85
86
87#endif /* CONFIG_SLOW_WORK */
88#endif /* _LINUX_SLOW_WORK_H */
diff --git a/init/Kconfig b/init/Kconfig
index 1398a14b0191..236a79377b8e 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1014,6 +1014,18 @@ config MARKERS
1014 1014
1015source "arch/Kconfig" 1015source "arch/Kconfig"
1016 1016
1017config SLOW_WORK
1018 default n
1019 bool "Enable slow work thread pool"
1020 help
1021 The slow work thread pool provides a number of dynamically allocated
1022 threads that can be used by the kernel to perform operations that
1023 take a relatively long time.
1024
1025 An example of this would be CacheFiles doing a path lookup followed
1026 by a series of mkdirs and a create call, all of which have to touch
1027 disk.
1028
1017endmenu # General setup 1029endmenu # General setup
1018 1030
1019config HAVE_GENERIC_DMA_COHERENT 1031config HAVE_GENERIC_DMA_COHERENT
diff --git a/kernel/Makefile b/kernel/Makefile
index e4791b3ba55d..bab1dffe37e9 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -93,6 +93,7 @@ obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += dma-coherent.o
93obj-$(CONFIG_FUNCTION_TRACER) += trace/ 93obj-$(CONFIG_FUNCTION_TRACER) += trace/
94obj-$(CONFIG_TRACING) += trace/ 94obj-$(CONFIG_TRACING) += trace/
95obj-$(CONFIG_SMP) += sched_cpupri.o 95obj-$(CONFIG_SMP) += sched_cpupri.o
96obj-$(CONFIG_SLOW_WORK) += slow-work.o
96 97
97ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y) 98ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y)
98# According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is 99# According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is
diff --git a/kernel/slow-work.c b/kernel/slow-work.c
new file mode 100644
index 000000000000..5a7392734c82
--- /dev/null
+++ b/kernel/slow-work.c
@@ -0,0 +1,388 @@
1/* Worker thread pool for slow items, such as filesystem lookups or mkdirs
2 *
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/slow-work.h>
14#include <linux/kthread.h>
15#include <linux/freezer.h>
16#include <linux/wait.h>
17#include <asm/system.h>
18
19/*
20 * The pool of threads has at least min threads in it as long as someone is
21 * using the facility, and may have as many as max.
22 *
23 * A portion of the pool may be processing very slow operations.
24 */
25static unsigned slow_work_min_threads = 2;
26static unsigned slow_work_max_threads = 4;
27static unsigned vslow_work_proportion = 50; /* % of threads that may process
28 * very slow work */
29static atomic_t slow_work_thread_count;
30static atomic_t vslow_work_executing_count;
31
32/*
33 * The queues of work items and the lock governing access to them. These are
34 * shared between all the CPUs. It doesn't make sense to have per-CPU queues
35 * as the number of threads bears no relation to the number of CPUs.
36 *
37 * There are two queues of work items: one for slow work items, and one for
38 * very slow work items.
39 */
40static LIST_HEAD(slow_work_queue);
41static LIST_HEAD(vslow_work_queue);
42static DEFINE_SPINLOCK(slow_work_queue_lock);
43
44/*
45 * The thread controls. A variable used to signal to the threads that they
46 * should exit when the queue is empty, a waitqueue used by the threads to wait
47 * for signals, and a completion set by the last thread to exit.
48 */
49static bool slow_work_threads_should_exit;
50static DECLARE_WAIT_QUEUE_HEAD(slow_work_thread_wq);
51static DECLARE_COMPLETION(slow_work_last_thread_exited);
52
53/*
54 * The number of users of the thread pool and its lock. Whilst this is zero we
55 * have no threads hanging around, and when this reaches zero, we wait for all
56 * active or queued work items to complete and kill all the threads we do have.
57 */
58static int slow_work_user_count;
59static DEFINE_MUTEX(slow_work_user_lock);
60
61/*
62 * Calculate the maximum number of active threads in the pool that are
63 * permitted to process very slow work items.
64 *
65 * The answer is rounded up to at least 1, but may not equal or exceed the
66 * maximum number of the threads in the pool. This means we always have at
67 * least one thread that can process slow work items, and we always have at
68 * least one thread that won't get tied up doing so.
69 */
70static unsigned slow_work_calc_vsmax(void)
71{
72 unsigned vsmax;
73
74 vsmax = atomic_read(&slow_work_thread_count) * vslow_work_proportion;
75 vsmax /= 100;
76 vsmax = max(vsmax, 1U);
77 return min(vsmax, slow_work_max_threads - 1);
78}
79
80/*
81 * Attempt to execute stuff queued on a slow thread. Return true if we managed
82 * it, false if there was nothing to do.
83 */
84static bool slow_work_execute(void)
85{
86 struct slow_work *work = NULL;
87 unsigned vsmax;
88 bool very_slow;
89
90 vsmax = slow_work_calc_vsmax();
91
92 /* find something to execute */
93 spin_lock_irq(&slow_work_queue_lock);
94 if (!list_empty(&vslow_work_queue) &&
95 atomic_read(&vslow_work_executing_count) < vsmax) {
96 work = list_entry(vslow_work_queue.next,
97 struct slow_work, link);
98 if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
99 BUG();
100 list_del_init(&work->link);
101 atomic_inc(&vslow_work_executing_count);
102 very_slow = true;
103 } else if (!list_empty(&slow_work_queue)) {
104 work = list_entry(slow_work_queue.next,
105 struct slow_work, link);
106 if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
107 BUG();
108 list_del_init(&work->link);
109 very_slow = false;
110 } else {
111 very_slow = false; /* avoid the compiler warning */
112 }
113 spin_unlock_irq(&slow_work_queue_lock);
114
115 if (!work)
116 return false;
117
118 if (!test_and_clear_bit(SLOW_WORK_PENDING, &work->flags))
119 BUG();
120
121 work->ops->execute(work);
122
123 if (very_slow)
124 atomic_dec(&vslow_work_executing_count);
125 clear_bit_unlock(SLOW_WORK_EXECUTING, &work->flags);
126
127 /* if someone tried to enqueue the item whilst we were executing it,
128 * then it'll be left unenqueued to avoid multiple threads trying to
129 * execute it simultaneously
130 *
131 * there is, however, a race between us testing the pending flag and
132 * getting the spinlock, and between the enqueuer setting the pending
133 * flag and getting the spinlock, so we use a deferral bit to tell us
134 * if the enqueuer got there first
135 */
136 if (test_bit(SLOW_WORK_PENDING, &work->flags)) {
137 spin_lock_irq(&slow_work_queue_lock);
138
139 if (!test_bit(SLOW_WORK_EXECUTING, &work->flags) &&
140 test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags))
141 goto auto_requeue;
142
143 spin_unlock_irq(&slow_work_queue_lock);
144 }
145
146 work->ops->put_ref(work);
147 return true;
148
149auto_requeue:
150 /* we must complete the enqueue operation
151 * - we transfer our ref on the item back to the appropriate queue
152 * - don't wake another thread up as we're awake already
153 */
154 if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
155 list_add_tail(&work->link, &vslow_work_queue);
156 else
157 list_add_tail(&work->link, &slow_work_queue);
158 spin_unlock_irq(&slow_work_queue_lock);
159 return true;
160}
161
162/**
163 * slow_work_enqueue - Schedule a slow work item for processing
164 * @work: The work item to queue
165 *
166 * Schedule a slow work item for processing. If the item is already undergoing
167 * execution, this guarantees not to re-enter the execution routine until the
168 * first execution finishes.
169 *
170 * The item is pinned by this function as it retains a reference to it, managed
171 * through the item operations. The item is unpinned once it has been
172 * executed.
173 *
174 * An item may hog the thread that is running it for a relatively large amount
175 * of time, sufficient, for example, to perform several lookup, mkdir, create
176 * and setxattr operations. It may sleep on I/O and may sleep to obtain locks.
177 *
178 * Conversely, if a number of items are awaiting processing, it may take some
179 * time before any given item is given attention. The number of threads in the
180 * pool may be increased to deal with demand, but only up to a limit.
181 *
182 * If SLOW_WORK_VERY_SLOW is set on the work item, then it will be placed in
183 * the very slow queue, from which only a portion of the threads will be
184 * allowed to pick items to execute. This ensures that very slow items won't
185 * overly block ones that are just ordinarily slow.
186 *
187 * Returns 0 if successful, -EAGAIN if not.
188 */
189int slow_work_enqueue(struct slow_work *work)
190{
191 unsigned long flags;
192
193 BUG_ON(slow_work_user_count <= 0);
194 BUG_ON(!work);
195 BUG_ON(!work->ops);
196 BUG_ON(!work->ops->get_ref);
197
198 /* when honouring an enqueue request, we only promise that we will run
199 * the work function in the future; we do not promise to run it once
200 * per enqueue request
201 *
202 * we use the PENDING bit to merge together repeat requests without
203 * having to disable IRQs and take the spinlock, whilst still
204 * maintaining our promise
205 */
206 if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
207 spin_lock_irqsave(&slow_work_queue_lock, flags);
208
209 /* we promise that we will not attempt to execute the work
210 * function in more than one thread simultaneously
211 *
212 * this, however, leaves us with a problem if we're asked to
213 * enqueue the work whilst someone is executing the work
214 * function as simply queueing the work immediately means that
215 * another thread may try executing it whilst it is already
216 * under execution
217 *
218 * to deal with this, we set the ENQ_DEFERRED bit instead of
219 * enqueueing, and the thread currently executing the work
220 * function will enqueue the work item when the work function
221 * returns and it has cleared the EXECUTING bit
222 */
223 if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
224 set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
225 } else {
226 if (work->ops->get_ref(work) < 0)
227 goto cant_get_ref;
228 if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
229 list_add_tail(&work->link, &vslow_work_queue);
230 else
231 list_add_tail(&work->link, &slow_work_queue);
232 wake_up(&slow_work_thread_wq);
233 }
234
235 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
236 }
237 return 0;
238
239cant_get_ref:
240 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
241 return -EAGAIN;
242}
243EXPORT_SYMBOL(slow_work_enqueue);
244
245/*
246 * Determine if there is slow work available for dispatch
247 */
248static inline bool slow_work_available(int vsmax)
249{
250 return !list_empty(&slow_work_queue) ||
251 (!list_empty(&vslow_work_queue) &&
252 atomic_read(&vslow_work_executing_count) < vsmax);
253}
254
255/*
256 * Worker thread dispatcher
257 */
258static int slow_work_thread(void *_data)
259{
260 int vsmax;
261
262 DEFINE_WAIT(wait);
263
264 set_freezable();
265 set_user_nice(current, -5);
266
267 for (;;) {
268 vsmax = vslow_work_proportion;
269 vsmax *= atomic_read(&slow_work_thread_count);
270 vsmax /= 100;
271
272 prepare_to_wait(&slow_work_thread_wq, &wait,
273 TASK_INTERRUPTIBLE);
274 if (!freezing(current) &&
275 !slow_work_threads_should_exit &&
276 !slow_work_available(vsmax))
277 schedule();
278 finish_wait(&slow_work_thread_wq, &wait);
279
280 try_to_freeze();
281
282 vsmax = vslow_work_proportion;
283 vsmax *= atomic_read(&slow_work_thread_count);
284 vsmax /= 100;
285
286 if (slow_work_available(vsmax) && slow_work_execute()) {
287 cond_resched();
288 continue;
289 }
290
291 if (slow_work_threads_should_exit)
292 break;
293 }
294
295 if (atomic_dec_and_test(&slow_work_thread_count))
296 complete_and_exit(&slow_work_last_thread_exited, 0);
297 return 0;
298}
299
300/**
301 * slow_work_register_user - Register a user of the facility
302 *
303 * Register a user of the facility, starting up the initial threads if there
304 * aren't any other users at this point. This will return 0 if successful, or
305 * an error if not.
306 */
307int slow_work_register_user(void)
308{
309 struct task_struct *p;
310 int loop;
311
312 mutex_lock(&slow_work_user_lock);
313
314 if (slow_work_user_count == 0) {
315 printk(KERN_NOTICE "Slow work thread pool: Starting up\n");
316 init_completion(&slow_work_last_thread_exited);
317
318 slow_work_threads_should_exit = false;
319
320 /* start the minimum number of threads */
321 for (loop = 0; loop < slow_work_min_threads; loop++) {
322 atomic_inc(&slow_work_thread_count);
323 p = kthread_run(slow_work_thread, NULL, "kslowd");
324 if (IS_ERR(p))
325 goto error;
326 }
327 printk(KERN_NOTICE "Slow work thread pool: Ready\n");
328 }
329
330 slow_work_user_count++;
331 mutex_unlock(&slow_work_user_lock);
332 return 0;
333
334error:
335 if (atomic_dec_and_test(&slow_work_thread_count))
336 complete(&slow_work_last_thread_exited);
337 if (loop > 0) {
338 printk(KERN_ERR "Slow work thread pool:"
339 " Aborting startup on ENOMEM\n");
340 slow_work_threads_should_exit = true;
341 wake_up_all(&slow_work_thread_wq);
342 wait_for_completion(&slow_work_last_thread_exited);
343 printk(KERN_ERR "Slow work thread pool: Aborted\n");
344 }
345 mutex_unlock(&slow_work_user_lock);
346 return PTR_ERR(p);
347}
348EXPORT_SYMBOL(slow_work_register_user);
349
350/**
351 * slow_work_unregister_user - Unregister a user of the facility
352 *
353 * Unregister a user of the facility, killing all the threads if this was the
354 * last one.
355 */
356void slow_work_unregister_user(void)
357{
358 mutex_lock(&slow_work_user_lock);
359
360 BUG_ON(slow_work_user_count <= 0);
361
362 slow_work_user_count--;
363 if (slow_work_user_count == 0) {
364 printk(KERN_NOTICE "Slow work thread pool: Shutting down\n");
365 slow_work_threads_should_exit = true;
366 wake_up_all(&slow_work_thread_wq);
367 wait_for_completion(&slow_work_last_thread_exited);
368 printk(KERN_NOTICE "Slow work thread pool:"
369 " Shut down complete\n");
370 }
371
372 mutex_unlock(&slow_work_user_lock);
373}
374EXPORT_SYMBOL(slow_work_unregister_user);
375
376/*
377 * Initialise the slow work facility
378 */
379static int __init init_slow_work(void)
380{
381 unsigned nr_cpus = num_possible_cpus();
382
383 if (nr_cpus > slow_work_max_threads)
384 slow_work_max_threads = nr_cpus;
385 return 0;
386}
387
388subsys_initcall(init_slow_work);