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