SLOW_WORK: Wait for outstanding work items belonging to a module to clear
[safe/jmp/linux-2.6] / kernel / slow-work.c
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  * See Documentation/slow-work.txt
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>
19
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
25 #define SLOW_WORK_THREAD_LIMIT  255     /* abs maximum number of slow-work threads */
26
27 static void slow_work_cull_timeout(unsigned long);
28 static void slow_work_oom_timeout(unsigned long);
29
30 #ifdef CONFIG_SYSCTL
31 static int slow_work_min_threads_sysctl(struct ctl_table *, int,
32                                         void __user *, size_t *, loff_t *);
33
34 static int slow_work_max_threads_sysctl(struct ctl_table *, int ,
35                                         void __user *, size_t *, loff_t *);
36 #endif
37
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  */
44 static unsigned slow_work_min_threads = 2;
45 static unsigned slow_work_max_threads = 4;
46 static unsigned vslow_work_proportion = 50; /* % of threads that may process
47                                              * very slow work */
48
49 #ifdef CONFIG_SYSCTL
50 static const int slow_work_min_min_threads = 2;
51 static int slow_work_max_max_threads = SLOW_WORK_THREAD_LIMIT;
52 static const int slow_work_min_vslow = 1;
53 static const int slow_work_max_vslow = 99;
54
55 ctl_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  */
93 static atomic_t slow_work_thread_count;
94 static atomic_t vslow_work_executing_count;
95
96 static bool slow_work_may_not_start_new_thread;
97 static bool slow_work_cull; /* cull a thread due to lack of activity */
98 static DEFINE_TIMER(slow_work_cull_timer, slow_work_cull_timeout, 0, 0);
99 static DEFINE_TIMER(slow_work_oom_timer, slow_work_oom_timeout, 0, 0);
100 static struct slow_work slow_work_new_thread; /* new thread starter */
101
102 /*
103  * slow work ID allocation (use slow_work_queue_lock)
104  */
105 static 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
112 static struct module *slow_work_thread_processing[SLOW_WORK_THREAD_LIMIT];
113 static struct module *slow_work_unreg_module;
114 static struct slow_work *slow_work_unreg_work_item;
115 static DECLARE_WAIT_QUEUE_HEAD(slow_work_unreg_wq);
116 static DEFINE_MUTEX(slow_work_unreg_sync_lock);
117 #endif
118
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  */
127 static LIST_HEAD(slow_work_queue);
128 static LIST_HEAD(vslow_work_queue);
129 static 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  */
136 static bool slow_work_threads_should_exit;
137 static DECLARE_WAIT_QUEUE_HEAD(slow_work_thread_wq);
138 static 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  */
145 static int slow_work_user_count;
146 static DEFINE_MUTEX(slow_work_user_lock);
147
148 /*
149  * Calculate the maximum number of active threads in the pool that are
150  * permitted to process very slow work items.
151  *
152  * The answer is rounded up to at least 1, but may not equal or exceed the
153  * maximum number of the threads in the pool.  This means we always have at
154  * least one thread that can process slow work items, and we always have at
155  * least one thread that won't get tied up doing so.
156  */
157 static unsigned slow_work_calc_vsmax(void)
158 {
159         unsigned vsmax;
160
161         vsmax = atomic_read(&slow_work_thread_count) * vslow_work_proportion;
162         vsmax /= 100;
163         vsmax = max(vsmax, 1U);
164         return min(vsmax, slow_work_max_threads - 1);
165 }
166
167 /*
168  * Attempt to execute stuff queued on a slow thread.  Return true if we managed
169  * it, false if there was nothing to do.
170  */
171 static bool slow_work_execute(int id)
172 {
173 #ifdef CONFIG_MODULES
174         struct module *module;
175 #endif
176         struct slow_work *work = NULL;
177         unsigned vsmax;
178         bool very_slow;
179
180         vsmax = slow_work_calc_vsmax();
181
182         /* see if we can schedule a new thread to be started if we're not
183          * keeping up with the work */
184         if (!waitqueue_active(&slow_work_thread_wq) &&
185             (!list_empty(&slow_work_queue) || !list_empty(&vslow_work_queue)) &&
186             atomic_read(&slow_work_thread_count) < slow_work_max_threads &&
187             !slow_work_may_not_start_new_thread)
188                 slow_work_enqueue(&slow_work_new_thread);
189
190         /* find something to execute */
191         spin_lock_irq(&slow_work_queue_lock);
192         if (!list_empty(&vslow_work_queue) &&
193             atomic_read(&vslow_work_executing_count) < vsmax) {
194                 work = list_entry(vslow_work_queue.next,
195                                   struct slow_work, link);
196                 if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
197                         BUG();
198                 list_del_init(&work->link);
199                 atomic_inc(&vslow_work_executing_count);
200                 very_slow = true;
201         } else if (!list_empty(&slow_work_queue)) {
202                 work = list_entry(slow_work_queue.next,
203                                   struct slow_work, link);
204                 if (test_and_set_bit_lock(SLOW_WORK_EXECUTING, &work->flags))
205                         BUG();
206                 list_del_init(&work->link);
207                 very_slow = false;
208         } else {
209                 very_slow = false; /* avoid the compiler warning */
210         }
211
212 #ifdef CONFIG_MODULES
213         if (work)
214                 slow_work_thread_processing[id] = work->owner;
215 #endif
216
217         spin_unlock_irq(&slow_work_queue_lock);
218
219         if (!work)
220                 return false;
221
222         if (!test_and_clear_bit(SLOW_WORK_PENDING, &work->flags))
223                 BUG();
224
225         work->ops->execute(work);
226
227         if (very_slow)
228                 atomic_dec(&vslow_work_executing_count);
229         clear_bit_unlock(SLOW_WORK_EXECUTING, &work->flags);
230
231         /* if someone tried to enqueue the item whilst we were executing it,
232          * then it'll be left unenqueued to avoid multiple threads trying to
233          * execute it simultaneously
234          *
235          * there is, however, a race between us testing the pending flag and
236          * getting the spinlock, and between the enqueuer setting the pending
237          * flag and getting the spinlock, so we use a deferral bit to tell us
238          * if the enqueuer got there first
239          */
240         if (test_bit(SLOW_WORK_PENDING, &work->flags)) {
241                 spin_lock_irq(&slow_work_queue_lock);
242
243                 if (!test_bit(SLOW_WORK_EXECUTING, &work->flags) &&
244                     test_and_clear_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags))
245                         goto auto_requeue;
246
247                 spin_unlock_irq(&slow_work_queue_lock);
248         }
249
250         /* sort out the race between module unloading and put_ref() */
251         work->ops->put_ref(work);
252
253 #ifdef CONFIG_MODULES
254         module = slow_work_thread_processing[id];
255         slow_work_thread_processing[id] = NULL;
256         smp_mb();
257         if (slow_work_unreg_work_item == work ||
258             slow_work_unreg_module == module)
259                 wake_up_all(&slow_work_unreg_wq);
260 #endif
261
262         return true;
263
264 auto_requeue:
265         /* we must complete the enqueue operation
266          * - we transfer our ref on the item back to the appropriate queue
267          * - don't wake another thread up as we're awake already
268          */
269         if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
270                 list_add_tail(&work->link, &vslow_work_queue);
271         else
272                 list_add_tail(&work->link, &slow_work_queue);
273         spin_unlock_irq(&slow_work_queue_lock);
274         slow_work_thread_processing[id] = NULL;
275         return true;
276 }
277
278 /**
279  * slow_work_enqueue - Schedule a slow work item for processing
280  * @work: The work item to queue
281  *
282  * Schedule a slow work item for processing.  If the item is already undergoing
283  * execution, this guarantees not to re-enter the execution routine until the
284  * first execution finishes.
285  *
286  * The item is pinned by this function as it retains a reference to it, managed
287  * through the item operations.  The item is unpinned once it has been
288  * executed.
289  *
290  * An item may hog the thread that is running it for a relatively large amount
291  * of time, sufficient, for example, to perform several lookup, mkdir, create
292  * and setxattr operations.  It may sleep on I/O and may sleep to obtain locks.
293  *
294  * Conversely, if a number of items are awaiting processing, it may take some
295  * time before any given item is given attention.  The number of threads in the
296  * pool may be increased to deal with demand, but only up to a limit.
297  *
298  * If SLOW_WORK_VERY_SLOW is set on the work item, then it will be placed in
299  * the very slow queue, from which only a portion of the threads will be
300  * allowed to pick items to execute.  This ensures that very slow items won't
301  * overly block ones that are just ordinarily slow.
302  *
303  * Returns 0 if successful, -EAGAIN if not.
304  */
305 int slow_work_enqueue(struct slow_work *work)
306 {
307         unsigned long flags;
308
309         BUG_ON(slow_work_user_count <= 0);
310         BUG_ON(!work);
311         BUG_ON(!work->ops);
312         BUG_ON(!work->ops->get_ref);
313
314         /* when honouring an enqueue request, we only promise that we will run
315          * the work function in the future; we do not promise to run it once
316          * per enqueue request
317          *
318          * we use the PENDING bit to merge together repeat requests without
319          * having to disable IRQs and take the spinlock, whilst still
320          * maintaining our promise
321          */
322         if (!test_and_set_bit_lock(SLOW_WORK_PENDING, &work->flags)) {
323                 spin_lock_irqsave(&slow_work_queue_lock, flags);
324
325                 /* we promise that we will not attempt to execute the work
326                  * function in more than one thread simultaneously
327                  *
328                  * this, however, leaves us with a problem if we're asked to
329                  * enqueue the work whilst someone is executing the work
330                  * function as simply queueing the work immediately means that
331                  * another thread may try executing it whilst it is already
332                  * under execution
333                  *
334                  * to deal with this, we set the ENQ_DEFERRED bit instead of
335                  * enqueueing, and the thread currently executing the work
336                  * function will enqueue the work item when the work function
337                  * returns and it has cleared the EXECUTING bit
338                  */
339                 if (test_bit(SLOW_WORK_EXECUTING, &work->flags)) {
340                         set_bit(SLOW_WORK_ENQ_DEFERRED, &work->flags);
341                 } else {
342                         if (work->ops->get_ref(work) < 0)
343                                 goto cant_get_ref;
344                         if (test_bit(SLOW_WORK_VERY_SLOW, &work->flags))
345                                 list_add_tail(&work->link, &vslow_work_queue);
346                         else
347                                 list_add_tail(&work->link, &slow_work_queue);
348                         wake_up(&slow_work_thread_wq);
349                 }
350
351                 spin_unlock_irqrestore(&slow_work_queue_lock, flags);
352         }
353         return 0;
354
355 cant_get_ref:
356         spin_unlock_irqrestore(&slow_work_queue_lock, flags);
357         return -EAGAIN;
358 }
359 EXPORT_SYMBOL(slow_work_enqueue);
360
361 /*
362  * Schedule a cull of the thread pool at some time in the near future
363  */
364 static void slow_work_schedule_cull(void)
365 {
366         mod_timer(&slow_work_cull_timer,
367                   round_jiffies(jiffies + SLOW_WORK_CULL_TIMEOUT));
368 }
369
370 /*
371  * Worker thread culling algorithm
372  */
373 static bool slow_work_cull_thread(void)
374 {
375         unsigned long flags;
376         bool do_cull = false;
377
378         spin_lock_irqsave(&slow_work_queue_lock, flags);
379
380         if (slow_work_cull) {
381                 slow_work_cull = false;
382
383                 if (list_empty(&slow_work_queue) &&
384                     list_empty(&vslow_work_queue) &&
385                     atomic_read(&slow_work_thread_count) >
386                     slow_work_min_threads) {
387                         slow_work_schedule_cull();
388                         do_cull = true;
389                 }
390         }
391
392         spin_unlock_irqrestore(&slow_work_queue_lock, flags);
393         return do_cull;
394 }
395
396 /*
397  * Determine if there is slow work available for dispatch
398  */
399 static inline bool slow_work_available(int vsmax)
400 {
401         return !list_empty(&slow_work_queue) ||
402                 (!list_empty(&vslow_work_queue) &&
403                  atomic_read(&vslow_work_executing_count) < vsmax);
404 }
405
406 /*
407  * Worker thread dispatcher
408  */
409 static int slow_work_thread(void *_data)
410 {
411         int vsmax, id;
412
413         DEFINE_WAIT(wait);
414
415         set_freezable();
416         set_user_nice(current, -5);
417
418         /* allocate ourselves an ID */
419         spin_lock_irq(&slow_work_queue_lock);
420         id = find_first_zero_bit(slow_work_ids, SLOW_WORK_THREAD_LIMIT);
421         BUG_ON(id < 0 || id >= SLOW_WORK_THREAD_LIMIT);
422         __set_bit(id, slow_work_ids);
423         spin_unlock_irq(&slow_work_queue_lock);
424
425         sprintf(current->comm, "kslowd%03u", id);
426
427         for (;;) {
428                 vsmax = vslow_work_proportion;
429                 vsmax *= atomic_read(&slow_work_thread_count);
430                 vsmax /= 100;
431
432                 prepare_to_wait_exclusive(&slow_work_thread_wq, &wait,
433                                           TASK_INTERRUPTIBLE);
434                 if (!freezing(current) &&
435                     !slow_work_threads_should_exit &&
436                     !slow_work_available(vsmax) &&
437                     !slow_work_cull)
438                         schedule();
439                 finish_wait(&slow_work_thread_wq, &wait);
440
441                 try_to_freeze();
442
443                 vsmax = vslow_work_proportion;
444                 vsmax *= atomic_read(&slow_work_thread_count);
445                 vsmax /= 100;
446
447                 if (slow_work_available(vsmax) && slow_work_execute(id)) {
448                         cond_resched();
449                         if (list_empty(&slow_work_queue) &&
450                             list_empty(&vslow_work_queue) &&
451                             atomic_read(&slow_work_thread_count) >
452                             slow_work_min_threads)
453                                 slow_work_schedule_cull();
454                         continue;
455                 }
456
457                 if (slow_work_threads_should_exit)
458                         break;
459
460                 if (slow_work_cull && slow_work_cull_thread())
461                         break;
462         }
463
464         spin_lock_irq(&slow_work_queue_lock);
465         __clear_bit(id, slow_work_ids);
466         spin_unlock_irq(&slow_work_queue_lock);
467
468         if (atomic_dec_and_test(&slow_work_thread_count))
469                 complete_and_exit(&slow_work_last_thread_exited, 0);
470         return 0;
471 }
472
473 /*
474  * Handle thread cull timer expiration
475  */
476 static void slow_work_cull_timeout(unsigned long data)
477 {
478         slow_work_cull = true;
479         wake_up(&slow_work_thread_wq);
480 }
481
482 /*
483  * Get a reference on slow work thread starter
484  */
485 static int slow_work_new_thread_get_ref(struct slow_work *work)
486 {
487         return 0;
488 }
489
490 /*
491  * Drop a reference on slow work thread starter
492  */
493 static void slow_work_new_thread_put_ref(struct slow_work *work)
494 {
495 }
496
497 /*
498  * Start a new slow work thread
499  */
500 static void slow_work_new_thread_execute(struct slow_work *work)
501 {
502         struct task_struct *p;
503
504         if (slow_work_threads_should_exit)
505                 return;
506
507         if (atomic_read(&slow_work_thread_count) >= slow_work_max_threads)
508                 return;
509
510         if (!mutex_trylock(&slow_work_user_lock))
511                 return;
512
513         slow_work_may_not_start_new_thread = true;
514         atomic_inc(&slow_work_thread_count);
515         p = kthread_run(slow_work_thread, NULL, "kslowd");
516         if (IS_ERR(p)) {
517                 printk(KERN_DEBUG "Slow work thread pool: OOM\n");
518                 if (atomic_dec_and_test(&slow_work_thread_count))
519                         BUG(); /* we're running on a slow work thread... */
520                 mod_timer(&slow_work_oom_timer,
521                           round_jiffies(jiffies + SLOW_WORK_OOM_TIMEOUT));
522         } else {
523                 /* ratelimit the starting of new threads */
524                 mod_timer(&slow_work_oom_timer, jiffies + 1);
525         }
526
527         mutex_unlock(&slow_work_user_lock);
528 }
529
530 static const struct slow_work_ops slow_work_new_thread_ops = {
531         .owner          = THIS_MODULE,
532         .get_ref        = slow_work_new_thread_get_ref,
533         .put_ref        = slow_work_new_thread_put_ref,
534         .execute        = slow_work_new_thread_execute,
535 };
536
537 /*
538  * post-OOM new thread start suppression expiration
539  */
540 static void slow_work_oom_timeout(unsigned long data)
541 {
542         slow_work_may_not_start_new_thread = false;
543 }
544
545 #ifdef CONFIG_SYSCTL
546 /*
547  * Handle adjustment of the minimum number of threads
548  */
549 static int slow_work_min_threads_sysctl(struct ctl_table *table, int write,
550                                         void __user *buffer,
551                                         size_t *lenp, loff_t *ppos)
552 {
553         int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
554         int n;
555
556         if (ret == 0) {
557                 mutex_lock(&slow_work_user_lock);
558                 if (slow_work_user_count > 0) {
559                         /* see if we need to start or stop threads */
560                         n = atomic_read(&slow_work_thread_count) -
561                                 slow_work_min_threads;
562
563                         if (n < 0 && !slow_work_may_not_start_new_thread)
564                                 slow_work_enqueue(&slow_work_new_thread);
565                         else if (n > 0)
566                                 slow_work_schedule_cull();
567                 }
568                 mutex_unlock(&slow_work_user_lock);
569         }
570
571         return ret;
572 }
573
574 /*
575  * Handle adjustment of the maximum number of threads
576  */
577 static int slow_work_max_threads_sysctl(struct ctl_table *table, int write,
578                                         void __user *buffer,
579                                         size_t *lenp, loff_t *ppos)
580 {
581         int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
582         int n;
583
584         if (ret == 0) {
585                 mutex_lock(&slow_work_user_lock);
586                 if (slow_work_user_count > 0) {
587                         /* see if we need to stop threads */
588                         n = slow_work_max_threads -
589                                 atomic_read(&slow_work_thread_count);
590
591                         if (n < 0)
592                                 slow_work_schedule_cull();
593                 }
594                 mutex_unlock(&slow_work_user_lock);
595         }
596
597         return ret;
598 }
599 #endif /* CONFIG_SYSCTL */
600
601 /**
602  * slow_work_register_user - Register a user of the facility
603  * @module: The module about to make use of the facility
604  *
605  * Register a user of the facility, starting up the initial threads if there
606  * aren't any other users at this point.  This will return 0 if successful, or
607  * an error if not.
608  */
609 int slow_work_register_user(struct module *module)
610 {
611         struct task_struct *p;
612         int loop;
613
614         mutex_lock(&slow_work_user_lock);
615
616         if (slow_work_user_count == 0) {
617                 printk(KERN_NOTICE "Slow work thread pool: Starting up\n");
618                 init_completion(&slow_work_last_thread_exited);
619
620                 slow_work_threads_should_exit = false;
621                 slow_work_init(&slow_work_new_thread,
622                                &slow_work_new_thread_ops);
623                 slow_work_may_not_start_new_thread = false;
624                 slow_work_cull = false;
625
626                 /* start the minimum number of threads */
627                 for (loop = 0; loop < slow_work_min_threads; loop++) {
628                         atomic_inc(&slow_work_thread_count);
629                         p = kthread_run(slow_work_thread, NULL, "kslowd");
630                         if (IS_ERR(p))
631                                 goto error;
632                 }
633                 printk(KERN_NOTICE "Slow work thread pool: Ready\n");
634         }
635
636         slow_work_user_count++;
637         mutex_unlock(&slow_work_user_lock);
638         return 0;
639
640 error:
641         if (atomic_dec_and_test(&slow_work_thread_count))
642                 complete(&slow_work_last_thread_exited);
643         if (loop > 0) {
644                 printk(KERN_ERR "Slow work thread pool:"
645                        " Aborting startup on ENOMEM\n");
646                 slow_work_threads_should_exit = true;
647                 wake_up_all(&slow_work_thread_wq);
648                 wait_for_completion(&slow_work_last_thread_exited);
649                 printk(KERN_ERR "Slow work thread pool: Aborted\n");
650         }
651         mutex_unlock(&slow_work_user_lock);
652         return PTR_ERR(p);
653 }
654 EXPORT_SYMBOL(slow_work_register_user);
655
656 /*
657  * wait for all outstanding items from the calling module to complete
658  * - note that more items may be queued whilst we're waiting
659  */
660 static void slow_work_wait_for_items(struct module *module)
661 {
662         DECLARE_WAITQUEUE(myself, current);
663         struct slow_work *work;
664         int loop;
665
666         mutex_lock(&slow_work_unreg_sync_lock);
667         add_wait_queue(&slow_work_unreg_wq, &myself);
668
669         for (;;) {
670                 spin_lock_irq(&slow_work_queue_lock);
671
672                 /* first of all, we wait for the last queued item in each list
673                  * to be processed */
674                 list_for_each_entry_reverse(work, &vslow_work_queue, link) {
675                         if (work->owner == module) {
676                                 set_current_state(TASK_UNINTERRUPTIBLE);
677                                 slow_work_unreg_work_item = work;
678                                 goto do_wait;
679                         }
680                 }
681                 list_for_each_entry_reverse(work, &slow_work_queue, link) {
682                         if (work->owner == module) {
683                                 set_current_state(TASK_UNINTERRUPTIBLE);
684                                 slow_work_unreg_work_item = work;
685                                 goto do_wait;
686                         }
687                 }
688
689                 /* then we wait for the items being processed to finish */
690                 slow_work_unreg_module = module;
691                 smp_mb();
692                 for (loop = 0; loop < SLOW_WORK_THREAD_LIMIT; loop++) {
693                         if (slow_work_thread_processing[loop] == module)
694                                 goto do_wait;
695                 }
696                 spin_unlock_irq(&slow_work_queue_lock);
697                 break; /* okay, we're done */
698
699         do_wait:
700                 spin_unlock_irq(&slow_work_queue_lock);
701                 schedule();
702                 slow_work_unreg_work_item = NULL;
703                 slow_work_unreg_module = NULL;
704         }
705
706         remove_wait_queue(&slow_work_unreg_wq, &myself);
707         mutex_unlock(&slow_work_unreg_sync_lock);
708 }
709
710 /**
711  * slow_work_unregister_user - Unregister a user of the facility
712  * @module: The module whose items should be cleared
713  *
714  * Unregister a user of the facility, killing all the threads if this was the
715  * last one.
716  *
717  * This waits for all the work items belonging to the nominated module to go
718  * away before proceeding.
719  */
720 void slow_work_unregister_user(struct module *module)
721 {
722         /* first of all, wait for all outstanding items from the calling module
723          * to complete */
724         if (module)
725                 slow_work_wait_for_items(module);
726
727         /* then we can actually go about shutting down the facility if need
728          * be */
729         mutex_lock(&slow_work_user_lock);
730
731         BUG_ON(slow_work_user_count <= 0);
732
733         slow_work_user_count--;
734         if (slow_work_user_count == 0) {
735                 printk(KERN_NOTICE "Slow work thread pool: Shutting down\n");
736                 slow_work_threads_should_exit = true;
737                 del_timer_sync(&slow_work_cull_timer);
738                 del_timer_sync(&slow_work_oom_timer);
739                 wake_up_all(&slow_work_thread_wq);
740                 wait_for_completion(&slow_work_last_thread_exited);
741                 printk(KERN_NOTICE "Slow work thread pool:"
742                        " Shut down complete\n");
743         }
744
745         mutex_unlock(&slow_work_user_lock);
746 }
747 EXPORT_SYMBOL(slow_work_unregister_user);
748
749 /*
750  * Initialise the slow work facility
751  */
752 static int __init init_slow_work(void)
753 {
754         unsigned nr_cpus = num_possible_cpus();
755
756         if (slow_work_max_threads < nr_cpus)
757                 slow_work_max_threads = nr_cpus;
758 #ifdef CONFIG_SYSCTL
759         if (slow_work_max_max_threads < nr_cpus * 2)
760                 slow_work_max_max_threads = nr_cpus * 2;
761 #endif
762         return 0;
763 }
764
765 subsys_initcall(init_slow_work);