1 /* Worker thread pool for slow items, such as filesystem lookups or mkdirs
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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>
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.
23 * A portion of the pool may be processing very slow operations.
25 static unsigned slow_work_min_threads = 2;
26 static unsigned slow_work_max_threads = 4;
27 static unsigned vslow_work_proportion = 50; /* % of threads that may process
29 static atomic_t slow_work_thread_count;
30 static atomic_t vslow_work_executing_count;
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.
37 * There are two queues of work items: one for slow work items, and one for
38 * very slow work items.
40 static LIST_HEAD(slow_work_queue);
41 static LIST_HEAD(vslow_work_queue);
42 static DEFINE_SPINLOCK(slow_work_queue_lock);
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.
49 static bool slow_work_threads_should_exit;
50 static DECLARE_WAIT_QUEUE_HEAD(slow_work_thread_wq);
51 static DECLARE_COMPLETION(slow_work_last_thread_exited);
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.
58 static int slow_work_user_count;
59 static DEFINE_MUTEX(slow_work_user_lock);
62 * Calculate the maximum number of active threads in the pool that are
63 * permitted to process very slow work items.
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.
70 static unsigned slow_work_calc_vsmax(void)
74 vsmax = atomic_read(&slow_work_thread_count) * vslow_work_proportion;
76 vsmax = max(vsmax, 1U);
77 return min(vsmax, slow_work_max_threads - 1);
81 * Attempt to execute stuff queued on a slow thread. Return true if we managed
82 * it, false if there was nothing to do.
84 static bool slow_work_execute(void)
86 struct slow_work *work = NULL;
90 vsmax = slow_work_calc_vsmax();
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))
100 list_del_init(&work->link);
101 atomic_inc(&vslow_work_executing_count);
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))
108 list_del_init(&work->link);
111 very_slow = false; /* avoid the compiler warning */
113 spin_unlock_irq(&slow_work_queue_lock);
118 if (!test_and_clear_bit(SLOW_WORK_PENDING, &work->flags))
121 work->ops->execute(work);
124 atomic_dec(&vslow_work_executing_count);
125 clear_bit_unlock(SLOW_WORK_EXECUTING, &work->flags);
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
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
136 if (test_bit(SLOW_WORK_PENDING, &work->flags)) {
137 spin_lock_irq(&slow_work_queue_lock);
139 if (!test_bit(SLOW_WORK_EXECUTING, &work->flags) &&
140 test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags))
143 spin_unlock_irq(&slow_work_queue_lock);
146 work->ops->put_ref(work);
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
154 if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
155 list_add_tail(&work->link, &vslow_work_queue);
157 list_add_tail(&work->link, &slow_work_queue);
158 spin_unlock_irq(&slow_work_queue_lock);
163 * slow_work_enqueue - Schedule a slow work item for processing
164 * @work: The work item to queue
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.
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
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.
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.
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.
187 * Returns 0 if successful, -EAGAIN if not.
189 int slow_work_enqueue(struct slow_work *work)
193 BUG_ON(slow_work_user_count <= 0);
196 BUG_ON(!work->ops->get_ref);
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
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
206 if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
207 spin_lock_irqsave(&slow_work_queue_lock, flags);
209 /* we promise that we will not attempt to execute the work
210 * function in more than one thread simultaneously
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
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
223 if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
224 set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
226 if (work->ops->get_ref(work) < 0)
228 if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
229 list_add_tail(&work->link, &vslow_work_queue);
231 list_add_tail(&work->link, &slow_work_queue);
232 wake_up(&slow_work_thread_wq);
235 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
240 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
243 EXPORT_SYMBOL(slow_work_enqueue);
246 * Determine if there is slow work available for dispatch
248 static inline bool slow_work_available(int vsmax)
250 return !list_empty(&slow_work_queue) ||
251 (!list_empty(&vslow_work_queue) &&
252 atomic_read(&vslow_work_executing_count) < vsmax);
256 * Worker thread dispatcher
258 static int slow_work_thread(void *_data)
265 set_user_nice(current, -5);
268 vsmax = vslow_work_proportion;
269 vsmax *= atomic_read(&slow_work_thread_count);
272 prepare_to_wait(&slow_work_thread_wq, &wait,
274 if (!freezing(current) &&
275 !slow_work_threads_should_exit &&
276 !slow_work_available(vsmax))
278 finish_wait(&slow_work_thread_wq, &wait);
282 vsmax = vslow_work_proportion;
283 vsmax *= atomic_read(&slow_work_thread_count);
286 if (slow_work_available(vsmax) && slow_work_execute()) {
291 if (slow_work_threads_should_exit)
295 if (atomic_dec_and_test(&slow_work_thread_count))
296 complete_and_exit(&slow_work_last_thread_exited, 0);
301 * slow_work_register_user - Register a user of the facility
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
307 int slow_work_register_user(void)
309 struct task_struct *p;
312 mutex_lock(&slow_work_user_lock);
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);
318 slow_work_threads_should_exit = false;
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");
327 printk(KERN_NOTICE "Slow work thread pool: Ready\n");
330 slow_work_user_count++;
331 mutex_unlock(&slow_work_user_lock);
335 if (atomic_dec_and_test(&slow_work_thread_count))
336 complete(&slow_work_last_thread_exited);
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");
345 mutex_unlock(&slow_work_user_lock);
348 EXPORT_SYMBOL(slow_work_register_user);
351 * slow_work_unregister_user - Unregister a user of the facility
353 * Unregister a user of the facility, killing all the threads if this was the
356 void slow_work_unregister_user(void)
358 mutex_lock(&slow_work_user_lock);
360 BUG_ON(slow_work_user_count <= 0);
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");
372 mutex_unlock(&slow_work_user_lock);
374 EXPORT_SYMBOL(slow_work_unregister_user);
377 * Initialise the slow work facility
379 static int __init init_slow_work(void)
381 unsigned nr_cpus = num_possible_cpus();
383 if (nr_cpus > slow_work_max_threads)
384 slow_work_max_threads = nr_cpus;
388 subsys_initcall(init_slow_work);