]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/slow-work.c
SLOW_WORK: Add delayed_slow_work support
[net-next-2.6.git] / kernel / slow-work.c
CommitLineData
07fe7cb7
DH
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.
8f0aa2f2
DH
10 *
11 * See Documentation/slow-work.txt
07fe7cb7
DH
12 */
13
14#include <linux/module.h>
15#include <linux/slow-work.h>
16#include <linux/kthread.h>
17#include <linux/freezer.h>
18#include <linux/wait.h>
07fe7cb7 19
109d9272
DH
20#define SLOW_WORK_CULL_TIMEOUT (5 * HZ) /* cull threads 5s after running out of
21 * things to do */
22#define SLOW_WORK_OOM_TIMEOUT (5 * HZ) /* can't start new threads for 5s after
23 * OOM */
24
3d7a641e
DH
25#define SLOW_WORK_THREAD_LIMIT 255 /* abs maximum number of slow-work threads */
26
109d9272
DH
27static void slow_work_cull_timeout(unsigned long);
28static void slow_work_oom_timeout(unsigned long);
29
12e22c5e 30#ifdef CONFIG_SYSCTL
8d65af78 31static int slow_work_min_threads_sysctl(struct ctl_table *, int,
12e22c5e
DH
32 void __user *, size_t *, loff_t *);
33
8d65af78 34static int slow_work_max_threads_sysctl(struct ctl_table *, int ,
12e22c5e
DH
35 void __user *, size_t *, loff_t *);
36#endif
37
07fe7cb7
DH
38/*
39 * The pool of threads has at least min threads in it as long as someone is
40 * using the facility, and may have as many as max.
41 *
42 * A portion of the pool may be processing very slow operations.
43 */
44static unsigned slow_work_min_threads = 2;
45static unsigned slow_work_max_threads = 4;
46static unsigned vslow_work_proportion = 50; /* % of threads that may process
47 * very slow work */
12e22c5e
DH
48
49#ifdef CONFIG_SYSCTL
50static const int slow_work_min_min_threads = 2;
3d7a641e 51static int slow_work_max_max_threads = SLOW_WORK_THREAD_LIMIT;
12e22c5e
DH
52static const int slow_work_min_vslow = 1;
53static const int slow_work_max_vslow = 99;
54
55ctl_table slow_work_sysctls[] = {
56 {
57 .ctl_name = CTL_UNNUMBERED,
58 .procname = "min-threads",
59 .data = &slow_work_min_threads,
60 .maxlen = sizeof(unsigned),
61 .mode = 0644,
62 .proc_handler = slow_work_min_threads_sysctl,
63 .extra1 = (void *) &slow_work_min_min_threads,
64 .extra2 = &slow_work_max_threads,
65 },
66 {
67 .ctl_name = CTL_UNNUMBERED,
68 .procname = "max-threads",
69 .data = &slow_work_max_threads,
70 .maxlen = sizeof(unsigned),
71 .mode = 0644,
72 .proc_handler = slow_work_max_threads_sysctl,
73 .extra1 = &slow_work_min_threads,
74 .extra2 = (void *) &slow_work_max_max_threads,
75 },
76 {
77 .ctl_name = CTL_UNNUMBERED,
78 .procname = "vslow-percentage",
79 .data = &vslow_work_proportion,
80 .maxlen = sizeof(unsigned),
81 .mode = 0644,
82 .proc_handler = &proc_dointvec_minmax,
83 .extra1 = (void *) &slow_work_min_vslow,
84 .extra2 = (void *) &slow_work_max_vslow,
85 },
86 { .ctl_name = 0 }
87};
88#endif
89
90/*
91 * The active state of the thread pool
92 */
07fe7cb7
DH
93static atomic_t slow_work_thread_count;
94static atomic_t vslow_work_executing_count;
95
109d9272
DH
96static bool slow_work_may_not_start_new_thread;
97static bool slow_work_cull; /* cull a thread due to lack of activity */
98static DEFINE_TIMER(slow_work_cull_timer, slow_work_cull_timeout, 0, 0);
99static DEFINE_TIMER(slow_work_oom_timer, slow_work_oom_timeout, 0, 0);
100static struct slow_work slow_work_new_thread; /* new thread starter */
101
3d7a641e
DH
102/*
103 * slow work ID allocation (use slow_work_queue_lock)
104 */
105static DECLARE_BITMAP(slow_work_ids, SLOW_WORK_THREAD_LIMIT);
106
107/*
108 * Unregistration tracking to prevent put_ref() from disappearing during module
109 * unload
110 */
111#ifdef CONFIG_MODULES
112static struct module *slow_work_thread_processing[SLOW_WORK_THREAD_LIMIT];
113static struct module *slow_work_unreg_module;
114static struct slow_work *slow_work_unreg_work_item;
115static DECLARE_WAIT_QUEUE_HEAD(slow_work_unreg_wq);
116static DEFINE_MUTEX(slow_work_unreg_sync_lock);
117#endif
118
07fe7cb7
DH
119/*
120 * The queues of work items and the lock governing access to them. These are
121 * shared between all the CPUs. It doesn't make sense to have per-CPU queues
122 * as the number of threads bears no relation to the number of CPUs.
123 *
124 * There are two queues of work items: one for slow work items, and one for
125 * very slow work items.
126 */
127static LIST_HEAD(slow_work_queue);
128static LIST_HEAD(vslow_work_queue);
129static DEFINE_SPINLOCK(slow_work_queue_lock);
130
131/*
132 * The thread controls. A variable used to signal to the threads that they
133 * should exit when the queue is empty, a waitqueue used by the threads to wait
134 * for signals, and a completion set by the last thread to exit.
135 */
136static bool slow_work_threads_should_exit;
137static DECLARE_WAIT_QUEUE_HEAD(slow_work_thread_wq);
138static DECLARE_COMPLETION(slow_work_last_thread_exited);
139
140/*
141 * The number of users of the thread pool and its lock. Whilst this is zero we
142 * have no threads hanging around, and when this reaches zero, we wait for all
143 * active or queued work items to complete and kill all the threads we do have.
144 */
145static int slow_work_user_count;
146static DEFINE_MUTEX(slow_work_user_lock);
147
4d8bb2cb
JA
148static inline int slow_work_get_ref(struct slow_work *work)
149{
150 if (work->ops->get_ref)
151 return work->ops->get_ref(work);
152
153 return 0;
154}
155
156static inline void slow_work_put_ref(struct slow_work *work)
157{
158 if (work->ops->put_ref)
159 work->ops->put_ref(work);
160}
161
07fe7cb7
DH
162/*
163 * Calculate the maximum number of active threads in the pool that are
164 * permitted to process very slow work items.
165 *
166 * The answer is rounded up to at least 1, but may not equal or exceed the
167 * maximum number of the threads in the pool. This means we always have at
168 * least one thread that can process slow work items, and we always have at
169 * least one thread that won't get tied up doing so.
170 */
171static unsigned slow_work_calc_vsmax(void)
172{
173 unsigned vsmax;
174
175 vsmax = atomic_read(&slow_work_thread_count) * vslow_work_proportion;
176 vsmax /= 100;
177 vsmax = max(vsmax, 1U);
178 return min(vsmax, slow_work_max_threads - 1);
179}
180
181/*
182 * Attempt to execute stuff queued on a slow thread. Return true if we managed
183 * it, false if there was nothing to do.
184 */
3d7a641e 185static bool slow_work_execute(int id)
07fe7cb7 186{
3d7a641e
DH
187#ifdef CONFIG_MODULES
188 struct module *module;
189#endif
07fe7cb7
DH
190 struct slow_work *work = NULL;
191 unsigned vsmax;
192 bool very_slow;
193
194 vsmax = slow_work_calc_vsmax();
195
109d9272
DH
196 /* see if we can schedule a new thread to be started if we're not
197 * keeping up with the work */
198 if (!waitqueue_active(&slow_work_thread_wq) &&
199 (!list_empty(&slow_work_queue) || !list_empty(&vslow_work_queue)) &&
200 atomic_read(&slow_work_thread_count) < slow_work_max_threads &&
201 !slow_work_may_not_start_new_thread)
202 slow_work_enqueue(&slow_work_new_thread);
203
07fe7cb7
DH
204 /* find something to execute */
205 spin_lock_irq(&slow_work_queue_lock);
206 if (!list_empty(&vslow_work_queue) &&
207 atomic_read(&vslow_work_executing_count) < vsmax) {
208 work = list_entry(vslow_work_queue.next,
209 struct slow_work, link);
210 if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
211 BUG();
212 list_del_init(&work->link);
213 atomic_inc(&vslow_work_executing_count);
214 very_slow = true;
215 } else if (!list_empty(&slow_work_queue)) {
216 work = list_entry(slow_work_queue.next,
217 struct slow_work, link);
218 if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
219 BUG();
220 list_del_init(&work->link);
221 very_slow = false;
222 } else {
223 very_slow = false; /* avoid the compiler warning */
224 }
3d7a641e
DH
225
226#ifdef CONFIG_MODULES
227 if (work)
228 slow_work_thread_processing[id] = work->owner;
229#endif
230
07fe7cb7
DH
231 spin_unlock_irq(&slow_work_queue_lock);
232
233 if (!work)
234 return false;
235
236 if (!test_and_clear_bit(SLOW_WORK_PENDING, &work->flags))
237 BUG();
238
01609502
JA
239 /* don't execute if the work is in the process of being cancelled */
240 if (!test_bit(SLOW_WORK_CANCELLING, &work->flags))
241 work->ops->execute(work);
07fe7cb7
DH
242
243 if (very_slow)
244 atomic_dec(&vslow_work_executing_count);
245 clear_bit_unlock(SLOW_WORK_EXECUTING, &work->flags);
246
01609502
JA
247 /* wake up anyone waiting for this work to be complete */
248 wake_up_bit(&work->flags, SLOW_WORK_EXECUTING);
249
07fe7cb7
DH
250 /* if someone tried to enqueue the item whilst we were executing it,
251 * then it'll be left unenqueued to avoid multiple threads trying to
252 * execute it simultaneously
253 *
254 * there is, however, a race between us testing the pending flag and
255 * getting the spinlock, and between the enqueuer setting the pending
256 * flag and getting the spinlock, so we use a deferral bit to tell us
257 * if the enqueuer got there first
258 */
259 if (test_bit(SLOW_WORK_PENDING, &work->flags)) {
260 spin_lock_irq(&slow_work_queue_lock);
261
262 if (!test_bit(SLOW_WORK_EXECUTING, &work->flags) &&
263 test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags))
264 goto auto_requeue;
265
266 spin_unlock_irq(&slow_work_queue_lock);
267 }
268
3d7a641e 269 /* sort out the race between module unloading and put_ref() */
4d8bb2cb 270 slow_work_put_ref(work);
3d7a641e
DH
271
272#ifdef CONFIG_MODULES
273 module = slow_work_thread_processing[id];
274 slow_work_thread_processing[id] = NULL;
275 smp_mb();
276 if (slow_work_unreg_work_item == work ||
277 slow_work_unreg_module == module)
278 wake_up_all(&slow_work_unreg_wq);
279#endif
280
07fe7cb7
DH
281 return true;
282
283auto_requeue:
284 /* we must complete the enqueue operation
285 * - we transfer our ref on the item back to the appropriate queue
286 * - don't wake another thread up as we're awake already
287 */
288 if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
289 list_add_tail(&work->link, &vslow_work_queue);
290 else
291 list_add_tail(&work->link, &slow_work_queue);
292 spin_unlock_irq(&slow_work_queue_lock);
3d7a641e 293 slow_work_thread_processing[id] = NULL;
07fe7cb7
DH
294 return true;
295}
296
297/**
298 * slow_work_enqueue - Schedule a slow work item for processing
299 * @work: The work item to queue
300 *
301 * Schedule a slow work item for processing. If the item is already undergoing
302 * execution, this guarantees not to re-enter the execution routine until the
303 * first execution finishes.
304 *
305 * The item is pinned by this function as it retains a reference to it, managed
306 * through the item operations. The item is unpinned once it has been
307 * executed.
308 *
309 * An item may hog the thread that is running it for a relatively large amount
310 * of time, sufficient, for example, to perform several lookup, mkdir, create
311 * and setxattr operations. It may sleep on I/O and may sleep to obtain locks.
312 *
313 * Conversely, if a number of items are awaiting processing, it may take some
314 * time before any given item is given attention. The number of threads in the
315 * pool may be increased to deal with demand, but only up to a limit.
316 *
317 * If SLOW_WORK_VERY_SLOW is set on the work item, then it will be placed in
318 * the very slow queue, from which only a portion of the threads will be
319 * allowed to pick items to execute. This ensures that very slow items won't
320 * overly block ones that are just ordinarily slow.
321 *
01609502
JA
322 * Returns 0 if successful, -EAGAIN if not (or -ECANCELED if cancelled work is
323 * attempted queued)
07fe7cb7
DH
324 */
325int slow_work_enqueue(struct slow_work *work)
326{
327 unsigned long flags;
01609502
JA
328 int ret;
329
330 if (test_bit(SLOW_WORK_CANCELLING, &work->flags))
331 return -ECANCELED;
07fe7cb7
DH
332
333 BUG_ON(slow_work_user_count <= 0);
334 BUG_ON(!work);
335 BUG_ON(!work->ops);
07fe7cb7
DH
336
337 /* when honouring an enqueue request, we only promise that we will run
338 * the work function in the future; we do not promise to run it once
339 * per enqueue request
340 *
341 * we use the PENDING bit to merge together repeat requests without
342 * having to disable IRQs and take the spinlock, whilst still
343 * maintaining our promise
344 */
345 if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
346 spin_lock_irqsave(&slow_work_queue_lock, flags);
347
01609502
JA
348 if (unlikely(test_bit(SLOW_WORK_CANCELLING, &work->flags)))
349 goto cancelled;
350
07fe7cb7
DH
351 /* we promise that we will not attempt to execute the work
352 * function in more than one thread simultaneously
353 *
354 * this, however, leaves us with a problem if we're asked to
355 * enqueue the work whilst someone is executing the work
356 * function as simply queueing the work immediately means that
357 * another thread may try executing it whilst it is already
358 * under execution
359 *
360 * to deal with this, we set the ENQ_DEFERRED bit instead of
361 * enqueueing, and the thread currently executing the work
362 * function will enqueue the work item when the work function
363 * returns and it has cleared the EXECUTING bit
364 */
365 if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
366 set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
367 } else {
01609502
JA
368 ret = slow_work_get_ref(work);
369 if (ret < 0)
370 goto failed;
07fe7cb7
DH
371 if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
372 list_add_tail(&work->link, &vslow_work_queue);
373 else
374 list_add_tail(&work->link, &slow_work_queue);
375 wake_up(&slow_work_thread_wq);
376 }
377
378 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
379 }
380 return 0;
381
01609502
JA
382cancelled:
383 ret = -ECANCELED;
384failed:
07fe7cb7 385 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
01609502 386 return ret;
07fe7cb7
DH
387}
388EXPORT_SYMBOL(slow_work_enqueue);
389
01609502
JA
390static int slow_work_wait(void *word)
391{
392 schedule();
393 return 0;
394}
395
396/**
397 * slow_work_cancel - Cancel a slow work item
398 * @work: The work item to cancel
399 *
400 * This function will cancel a previously enqueued work item. If we cannot
401 * cancel the work item, it is guarenteed to have run when this function
402 * returns.
403 */
404void slow_work_cancel(struct slow_work *work)
405{
406 bool wait = true, put = false;
407
408 set_bit(SLOW_WORK_CANCELLING, &work->flags);
6b8268b1
JA
409 smp_mb();
410
411 /* if the work item is a delayed work item with an active timer, we
412 * need to wait for the timer to finish _before_ getting the spinlock,
413 * lest we deadlock against the timer routine
414 *
415 * the timer routine will leave DELAYED set if it notices the
416 * CANCELLING flag in time
417 */
418 if (test_bit(SLOW_WORK_DELAYED, &work->flags)) {
419 struct delayed_slow_work *dwork =
420 container_of(work, struct delayed_slow_work, work);
421 del_timer_sync(&dwork->timer);
422 }
01609502
JA
423
424 spin_lock_irq(&slow_work_queue_lock);
425
6b8268b1
JA
426 if (test_bit(SLOW_WORK_DELAYED, &work->flags)) {
427 /* the timer routine aborted or never happened, so we are left
428 * holding the timer's reference on the item and should just
429 * drop the pending flag and wait for any ongoing execution to
430 * finish */
431 struct delayed_slow_work *dwork =
432 container_of(work, struct delayed_slow_work, work);
433
434 BUG_ON(timer_pending(&dwork->timer));
435 BUG_ON(!list_empty(&work->link));
436
437 clear_bit(SLOW_WORK_DELAYED, &work->flags);
438 put = true;
439 clear_bit(SLOW_WORK_PENDING, &work->flags);
440
441 } else if (test_bit(SLOW_WORK_PENDING, &work->flags) &&
442 !list_empty(&work->link)) {
01609502
JA
443 /* the link in the pending queue holds a reference on the item
444 * that we will need to release */
445 list_del_init(&work->link);
446 wait = false;
447 put = true;
448 clear_bit(SLOW_WORK_PENDING, &work->flags);
449
450 } else if (test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags)) {
451 /* the executor is holding our only reference on the item, so
452 * we merely need to wait for it to finish executing */
453 clear_bit(SLOW_WORK_PENDING, &work->flags);
454 }
455
456 spin_unlock_irq(&slow_work_queue_lock);
457
458 /* the EXECUTING flag is set by the executor whilst the spinlock is set
459 * and before the item is dequeued - so assuming the above doesn't
460 * actually dequeue it, simply waiting for the EXECUTING flag to be
461 * released here should be sufficient */
462 if (wait)
463 wait_on_bit(&work->flags, SLOW_WORK_EXECUTING, slow_work_wait,
464 TASK_UNINTERRUPTIBLE);
465
466 clear_bit(SLOW_WORK_CANCELLING, &work->flags);
467 if (put)
468 slow_work_put_ref(work);
469}
470EXPORT_SYMBOL(slow_work_cancel);
471
6b8268b1
JA
472/*
473 * Handle expiry of the delay timer, indicating that a delayed slow work item
474 * should now be queued if not cancelled
475 */
476static void delayed_slow_work_timer(unsigned long data)
477{
478 struct slow_work *work = (struct slow_work *) data;
479 unsigned long flags;
480 bool queued = false, put = false;
481
482 spin_lock_irqsave(&slow_work_queue_lock, flags);
483 if (likely(!test_bit(SLOW_WORK_CANCELLING, &work->flags))) {
484 clear_bit(SLOW_WORK_DELAYED, &work->flags);
485
486 if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
487 /* we discard the reference the timer was holding in
488 * favour of the one the executor holds */
489 set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
490 put = true;
491 } else {
492 if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
493 list_add_tail(&work->link, &vslow_work_queue);
494 else
495 list_add_tail(&work->link, &slow_work_queue);
496 queued = true;
497 }
498 }
499
500 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
501 if (put)
502 slow_work_put_ref(work);
503 if (queued)
504 wake_up(&slow_work_thread_wq);
505}
506
507/**
508 * delayed_slow_work_enqueue - Schedule a delayed slow work item for processing
509 * @dwork: The delayed work item to queue
510 * @delay: When to start executing the work, in jiffies from now
511 *
512 * This is similar to slow_work_enqueue(), but it adds a delay before the work
513 * is actually queued for processing.
514 *
515 * The item can have delayed processing requested on it whilst it is being
516 * executed. The delay will begin immediately, and if it expires before the
517 * item finishes executing, the item will be placed back on the queue when it
518 * has done executing.
519 */
520int delayed_slow_work_enqueue(struct delayed_slow_work *dwork,
521 unsigned long delay)
522{
523 struct slow_work *work = &dwork->work;
524 unsigned long flags;
525 int ret;
526
527 if (delay == 0)
528 return slow_work_enqueue(&dwork->work);
529
530 BUG_ON(slow_work_user_count <= 0);
531 BUG_ON(!work);
532 BUG_ON(!work->ops);
533
534 if (test_bit(SLOW_WORK_CANCELLING, &work->flags))
535 return -ECANCELED;
536
537 if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
538 spin_lock_irqsave(&slow_work_queue_lock, flags);
539
540 if (test_bit(SLOW_WORK_CANCELLING, &work->flags))
541 goto cancelled;
542
543 /* the timer holds a reference whilst it is pending */
544 ret = work->ops->get_ref(work);
545 if (ret < 0)
546 goto cant_get_ref;
547
548 if (test_and_set_bit(SLOW_WORK_DELAYED, &work->flags))
549 BUG();
550 dwork->timer.expires = jiffies + delay;
551 dwork->timer.data = (unsigned long) work;
552 dwork->timer.function = delayed_slow_work_timer;
553 add_timer(&dwork->timer);
554
555 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
556 }
557
558 return 0;
559
560cancelled:
561 ret = -ECANCELED;
562cant_get_ref:
563 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
564 return ret;
565}
566EXPORT_SYMBOL(delayed_slow_work_enqueue);
567
009789f0
CP
568/*
569 * Schedule a cull of the thread pool at some time in the near future
570 */
571static void slow_work_schedule_cull(void)
572{
573 mod_timer(&slow_work_cull_timer,
574 round_jiffies(jiffies + SLOW_WORK_CULL_TIMEOUT));
575}
576
109d9272
DH
577/*
578 * Worker thread culling algorithm
579 */
580static bool slow_work_cull_thread(void)
581{
582 unsigned long flags;
583 bool do_cull = false;
584
585 spin_lock_irqsave(&slow_work_queue_lock, flags);
586
587 if (slow_work_cull) {
588 slow_work_cull = false;
589
590 if (list_empty(&slow_work_queue) &&
591 list_empty(&vslow_work_queue) &&
592 atomic_read(&slow_work_thread_count) >
593 slow_work_min_threads) {
009789f0 594 slow_work_schedule_cull();
109d9272
DH
595 do_cull = true;
596 }
597 }
598
599 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
600 return do_cull;
601}
602
07fe7cb7
DH
603/*
604 * Determine if there is slow work available for dispatch
605 */
606static inline bool slow_work_available(int vsmax)
607{
608 return !list_empty(&slow_work_queue) ||
609 (!list_empty(&vslow_work_queue) &&
610 atomic_read(&vslow_work_executing_count) < vsmax);
611}
612
613/*
614 * Worker thread dispatcher
615 */
616static int slow_work_thread(void *_data)
617{
3d7a641e 618 int vsmax, id;
07fe7cb7
DH
619
620 DEFINE_WAIT(wait);
621
622 set_freezable();
623 set_user_nice(current, -5);
624
3d7a641e
DH
625 /* allocate ourselves an ID */
626 spin_lock_irq(&slow_work_queue_lock);
627 id = find_first_zero_bit(slow_work_ids, SLOW_WORK_THREAD_LIMIT);
628 BUG_ON(id < 0 || id >= SLOW_WORK_THREAD_LIMIT);
629 __set_bit(id, slow_work_ids);
630 spin_unlock_irq(&slow_work_queue_lock);
631
632 sprintf(current->comm, "kslowd%03u", id);
633
07fe7cb7
DH
634 for (;;) {
635 vsmax = vslow_work_proportion;
636 vsmax *= atomic_read(&slow_work_thread_count);
637 vsmax /= 100;
638
b415c49a
ON
639 prepare_to_wait_exclusive(&slow_work_thread_wq, &wait,
640 TASK_INTERRUPTIBLE);
07fe7cb7
DH
641 if (!freezing(current) &&
642 !slow_work_threads_should_exit &&
109d9272
DH
643 !slow_work_available(vsmax) &&
644 !slow_work_cull)
07fe7cb7
DH
645 schedule();
646 finish_wait(&slow_work_thread_wq, &wait);
647
648 try_to_freeze();
649
650 vsmax = vslow_work_proportion;
651 vsmax *= atomic_read(&slow_work_thread_count);
652 vsmax /= 100;
653
3d7a641e 654 if (slow_work_available(vsmax) && slow_work_execute(id)) {
07fe7cb7 655 cond_resched();
109d9272
DH
656 if (list_empty(&slow_work_queue) &&
657 list_empty(&vslow_work_queue) &&
658 atomic_read(&slow_work_thread_count) >
659 slow_work_min_threads)
009789f0 660 slow_work_schedule_cull();
07fe7cb7
DH
661 continue;
662 }
663
664 if (slow_work_threads_should_exit)
665 break;
109d9272
DH
666
667 if (slow_work_cull && slow_work_cull_thread())
668 break;
07fe7cb7
DH
669 }
670
3d7a641e
DH
671 spin_lock_irq(&slow_work_queue_lock);
672 __clear_bit(id, slow_work_ids);
673 spin_unlock_irq(&slow_work_queue_lock);
674
07fe7cb7
DH
675 if (atomic_dec_and_test(&slow_work_thread_count))
676 complete_and_exit(&slow_work_last_thread_exited, 0);
677 return 0;
678}
679
109d9272
DH
680/*
681 * Handle thread cull timer expiration
682 */
683static void slow_work_cull_timeout(unsigned long data)
684{
685 slow_work_cull = true;
686 wake_up(&slow_work_thread_wq);
687}
688
109d9272
DH
689/*
690 * Start a new slow work thread
691 */
692static void slow_work_new_thread_execute(struct slow_work *work)
693{
694 struct task_struct *p;
695
696 if (slow_work_threads_should_exit)
697 return;
698
699 if (atomic_read(&slow_work_thread_count) >= slow_work_max_threads)
700 return;
701
702 if (!mutex_trylock(&slow_work_user_lock))
703 return;
704
705 slow_work_may_not_start_new_thread = true;
706 atomic_inc(&slow_work_thread_count);
707 p = kthread_run(slow_work_thread, NULL, "kslowd");
708 if (IS_ERR(p)) {
709 printk(KERN_DEBUG "Slow work thread pool: OOM\n");
710 if (atomic_dec_and_test(&slow_work_thread_count))
711 BUG(); /* we're running on a slow work thread... */
712 mod_timer(&slow_work_oom_timer,
009789f0 713 round_jiffies(jiffies + SLOW_WORK_OOM_TIMEOUT));
109d9272
DH
714 } else {
715 /* ratelimit the starting of new threads */
716 mod_timer(&slow_work_oom_timer, jiffies + 1);
717 }
718
719 mutex_unlock(&slow_work_user_lock);
720}
721
722static const struct slow_work_ops slow_work_new_thread_ops = {
3d7a641e 723 .owner = THIS_MODULE,
109d9272
DH
724 .execute = slow_work_new_thread_execute,
725};
726
727/*
728 * post-OOM new thread start suppression expiration
729 */
730static void slow_work_oom_timeout(unsigned long data)
731{
732 slow_work_may_not_start_new_thread = false;
733}
734
12e22c5e
DH
735#ifdef CONFIG_SYSCTL
736/*
737 * Handle adjustment of the minimum number of threads
738 */
739static int slow_work_min_threads_sysctl(struct ctl_table *table, int write,
8d65af78 740 void __user *buffer,
12e22c5e
DH
741 size_t *lenp, loff_t *ppos)
742{
8d65af78 743 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
12e22c5e
DH
744 int n;
745
746 if (ret == 0) {
747 mutex_lock(&slow_work_user_lock);
748 if (slow_work_user_count > 0) {
749 /* see if we need to start or stop threads */
750 n = atomic_read(&slow_work_thread_count) -
751 slow_work_min_threads;
752
753 if (n < 0 && !slow_work_may_not_start_new_thread)
754 slow_work_enqueue(&slow_work_new_thread);
755 else if (n > 0)
009789f0 756 slow_work_schedule_cull();
12e22c5e
DH
757 }
758 mutex_unlock(&slow_work_user_lock);
759 }
760
761 return ret;
762}
763
764/*
765 * Handle adjustment of the maximum number of threads
766 */
767static int slow_work_max_threads_sysctl(struct ctl_table *table, int write,
8d65af78 768 void __user *buffer,
12e22c5e
DH
769 size_t *lenp, loff_t *ppos)
770{
8d65af78 771 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
12e22c5e
DH
772 int n;
773
774 if (ret == 0) {
775 mutex_lock(&slow_work_user_lock);
776 if (slow_work_user_count > 0) {
777 /* see if we need to stop threads */
778 n = slow_work_max_threads -
779 atomic_read(&slow_work_thread_count);
780
781 if (n < 0)
009789f0 782 slow_work_schedule_cull();
12e22c5e
DH
783 }
784 mutex_unlock(&slow_work_user_lock);
785 }
786
787 return ret;
788}
789#endif /* CONFIG_SYSCTL */
790
07fe7cb7
DH
791/**
792 * slow_work_register_user - Register a user of the facility
3d7a641e 793 * @module: The module about to make use of the facility
07fe7cb7
DH
794 *
795 * Register a user of the facility, starting up the initial threads if there
796 * aren't any other users at this point. This will return 0 if successful, or
797 * an error if not.
798 */
3d7a641e 799int slow_work_register_user(struct module *module)
07fe7cb7
DH
800{
801 struct task_struct *p;
802 int loop;
803
804 mutex_lock(&slow_work_user_lock);
805
806 if (slow_work_user_count == 0) {
807 printk(KERN_NOTICE "Slow work thread pool: Starting up\n");
808 init_completion(&slow_work_last_thread_exited);
809
810 slow_work_threads_should_exit = false;
109d9272
DH
811 slow_work_init(&slow_work_new_thread,
812 &slow_work_new_thread_ops);
813 slow_work_may_not_start_new_thread = false;
814 slow_work_cull = false;
07fe7cb7
DH
815
816 /* start the minimum number of threads */
817 for (loop = 0; loop < slow_work_min_threads; loop++) {
818 atomic_inc(&slow_work_thread_count);
819 p = kthread_run(slow_work_thread, NULL, "kslowd");
820 if (IS_ERR(p))
821 goto error;
822 }
823 printk(KERN_NOTICE "Slow work thread pool: Ready\n");
824 }
825
826 slow_work_user_count++;
827 mutex_unlock(&slow_work_user_lock);
828 return 0;
829
830error:
831 if (atomic_dec_and_test(&slow_work_thread_count))
832 complete(&slow_work_last_thread_exited);
833 if (loop > 0) {
834 printk(KERN_ERR "Slow work thread pool:"
835 " Aborting startup on ENOMEM\n");
836 slow_work_threads_should_exit = true;
837 wake_up_all(&slow_work_thread_wq);
838 wait_for_completion(&slow_work_last_thread_exited);
839 printk(KERN_ERR "Slow work thread pool: Aborted\n");
840 }
841 mutex_unlock(&slow_work_user_lock);
842 return PTR_ERR(p);
843}
844EXPORT_SYMBOL(slow_work_register_user);
845
3d7a641e
DH
846/*
847 * wait for all outstanding items from the calling module to complete
848 * - note that more items may be queued whilst we're waiting
849 */
850static void slow_work_wait_for_items(struct module *module)
851{
852 DECLARE_WAITQUEUE(myself, current);
853 struct slow_work *work;
854 int loop;
855
856 mutex_lock(&slow_work_unreg_sync_lock);
857 add_wait_queue(&slow_work_unreg_wq, &myself);
858
859 for (;;) {
860 spin_lock_irq(&slow_work_queue_lock);
861
862 /* first of all, we wait for the last queued item in each list
863 * to be processed */
864 list_for_each_entry_reverse(work, &vslow_work_queue, link) {
865 if (work->owner == module) {
866 set_current_state(TASK_UNINTERRUPTIBLE);
867 slow_work_unreg_work_item = work;
868 goto do_wait;
869 }
870 }
871 list_for_each_entry_reverse(work, &slow_work_queue, link) {
872 if (work->owner == module) {
873 set_current_state(TASK_UNINTERRUPTIBLE);
874 slow_work_unreg_work_item = work;
875 goto do_wait;
876 }
877 }
878
879 /* then we wait for the items being processed to finish */
880 slow_work_unreg_module = module;
881 smp_mb();
882 for (loop = 0; loop < SLOW_WORK_THREAD_LIMIT; loop++) {
883 if (slow_work_thread_processing[loop] == module)
884 goto do_wait;
885 }
886 spin_unlock_irq(&slow_work_queue_lock);
887 break; /* okay, we're done */
888
889 do_wait:
890 spin_unlock_irq(&slow_work_queue_lock);
891 schedule();
892 slow_work_unreg_work_item = NULL;
893 slow_work_unreg_module = NULL;
894 }
895
896 remove_wait_queue(&slow_work_unreg_wq, &myself);
897 mutex_unlock(&slow_work_unreg_sync_lock);
898}
899
07fe7cb7
DH
900/**
901 * slow_work_unregister_user - Unregister a user of the facility
3d7a641e 902 * @module: The module whose items should be cleared
07fe7cb7
DH
903 *
904 * Unregister a user of the facility, killing all the threads if this was the
905 * last one.
3d7a641e
DH
906 *
907 * This waits for all the work items belonging to the nominated module to go
908 * away before proceeding.
07fe7cb7 909 */
3d7a641e 910void slow_work_unregister_user(struct module *module)
07fe7cb7 911{
3d7a641e
DH
912 /* first of all, wait for all outstanding items from the calling module
913 * to complete */
914 if (module)
915 slow_work_wait_for_items(module);
916
917 /* then we can actually go about shutting down the facility if need
918 * be */
07fe7cb7
DH
919 mutex_lock(&slow_work_user_lock);
920
921 BUG_ON(slow_work_user_count <= 0);
922
923 slow_work_user_count--;
924 if (slow_work_user_count == 0) {
925 printk(KERN_NOTICE "Slow work thread pool: Shutting down\n");
926 slow_work_threads_should_exit = true;
418df63c
JC
927 del_timer_sync(&slow_work_cull_timer);
928 del_timer_sync(&slow_work_oom_timer);
07fe7cb7
DH
929 wake_up_all(&slow_work_thread_wq);
930 wait_for_completion(&slow_work_last_thread_exited);
931 printk(KERN_NOTICE "Slow work thread pool:"
932 " Shut down complete\n");
933 }
934
935 mutex_unlock(&slow_work_user_lock);
936}
937EXPORT_SYMBOL(slow_work_unregister_user);
938
939/*
940 * Initialise the slow work facility
941 */
942static int __init init_slow_work(void)
943{
944 unsigned nr_cpus = num_possible_cpus();
945
12e22c5e 946 if (slow_work_max_threads < nr_cpus)
07fe7cb7 947 slow_work_max_threads = nr_cpus;
12e22c5e
DH
948#ifdef CONFIG_SYSCTL
949 if (slow_work_max_max_threads < nr_cpus * 2)
950 slow_work_max_max_threads = nr_cpus * 2;
951#endif
07fe7cb7
DH
952 return 0;
953}
954
955subsys_initcall(init_slow_work);