Skip to content

Commit 3d7a641

Browse files
committed
SLOW_WORK: Wait for outstanding work items belonging to a module to clear
Wait for outstanding slow work items belonging to a module to clear when unregistering that module as a user of the facility. This prevents the put_ref code of a work item from being taken away before it returns. Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 66b00a7 commit 3d7a641

8 files changed

Lines changed: 150 additions & 16 deletions

File tree

‎Documentation/slow-work.txt‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ USING SLOW WORK ITEMS
6464
Firstly, a module or subsystem wanting to make use of slow work items must
6565
register its interest:
6666

67-
int ret = slow_work_register_user();
67+
int ret = slow_work_register_user(struct module *module);
6868

69-
This will return 0 if successful, or a -ve error upon failure.
69+
This will return 0 if successful, or a -ve error upon failure. The module
70+
pointer should be the module interested in using this facility (almost
71+
certainly THIS_MODULE).
7072

7173

7274
Slow work items may then be set up by:
@@ -110,7 +112,12 @@ operation. When all a module's slow work items have been processed, and the
110112
module has no further interest in the facility, it should unregister its
111113
interest:
112114

113-
slow_work_unregister_user();
115+
slow_work_unregister_user(struct module *module);
116+
117+
The module pointer is used to wait for all outstanding work items for that
118+
module before completing the unregistration. This prevents the put_ref() code
119+
from being taken away before it completes. module should almost certainly be
120+
THIS_MODULE.
114121

115122

116123
===============

‎fs/fscache/main.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static int __init fscache_init(void)
4848
{
4949
int ret;
5050

51-
ret = slow_work_register_user();
51+
ret = slow_work_register_user(THIS_MODULE);
5252
if (ret < 0)
5353
goto error_slow_work;
5454

@@ -80,7 +80,7 @@ static int __init fscache_init(void)
8080
error_cookie_jar:
8181
fscache_proc_cleanup();
8282
error_proc:
83-
slow_work_unregister_user();
83+
slow_work_unregister_user(THIS_MODULE);
8484
error_slow_work:
8585
return ret;
8686
}
@@ -97,7 +97,7 @@ static void __exit fscache_exit(void)
9797
kobject_put(fscache_root);
9898
kmem_cache_destroy(fscache_cookie_jar);
9999
fscache_proc_cleanup();
100-
slow_work_unregister_user();
100+
slow_work_unregister_user(THIS_MODULE);
101101
printk(KERN_NOTICE "FS-Cache: Unloaded\n");
102102
}
103103

‎fs/fscache/object.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static void fscache_enqueue_dependents(struct fscache_object *);
4545
static void fscache_dequeue_object(struct fscache_object *);
4646

4747
const struct slow_work_ops fscache_object_slow_work_ops = {
48+
.owner = THIS_MODULE,
4849
.get_ref = fscache_object_slow_work_get_ref,
4950
.put_ref = fscache_object_slow_work_put_ref,
5051
.execute = fscache_object_slow_work_execute,

‎fs/fscache/operation.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ static void fscache_op_execute(struct slow_work *work)
453453
}
454454

455455
const struct slow_work_ops fscache_op_slow_work_ops = {
456+
.owner = THIS_MODULE,
456457
.get_ref = fscache_op_get_ref,
457458
.put_ref = fscache_op_put_ref,
458459
.execute = fscache_op_execute,

‎fs/gfs2/main.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static int __init init_gfs2_fs(void)
114114
if (error)
115115
goto fail_unregister;
116116

117-
error = slow_work_register_user();
117+
error = slow_work_register_user(THIS_MODULE);
118118
if (error)
119119
goto fail_slow;
120120

@@ -163,7 +163,7 @@ static void __exit exit_gfs2_fs(void)
163163
gfs2_unregister_debugfs();
164164
unregister_filesystem(&gfs2_fs_type);
165165
unregister_filesystem(&gfs2meta_fs_type);
166-
slow_work_unregister_user();
166+
slow_work_unregister_user(THIS_MODULE);
167167

168168
kmem_cache_destroy(gfs2_quotad_cachep);
169169
kmem_cache_destroy(gfs2_rgrpd_cachep);

‎fs/gfs2/recovery.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ static void gfs2_recover_work(struct slow_work *work)
593593
}
594594

595595
struct slow_work_ops gfs2_recover_ops = {
596+
.owner = THIS_MODULE,
596597
.get_ref = gfs2_recover_get_ref,
597598
.put_ref = gfs2_recover_put_ref,
598599
.execute = gfs2_recover_work,

‎include/linux/slow-work.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ struct slow_work;
2424
* The operations used to support slow work items
2525
*/
2626
struct slow_work_ops {
27+
/* owner */
28+
struct module *owner;
29+
2730
/* get a ref on a work item
2831
* - return 0 if successful, -ve if not
2932
*/
@@ -42,6 +45,7 @@ struct slow_work_ops {
4245
* queued
4346
*/
4447
struct slow_work {
48+
struct module *owner; /* the owning module */
4549
unsigned long flags;
4650
#define SLOW_WORK_PENDING 0 /* item pending (further) execution */
4751
#define SLOW_WORK_EXECUTING 1 /* item currently executing */
@@ -84,8 +88,8 @@ static inline void vslow_work_init(struct slow_work *work,
8488
}
8589

8690
extern int slow_work_enqueue(struct slow_work *work);
87-
extern int slow_work_register_user(void);
88-
extern void slow_work_unregister_user(void);
91+
extern int slow_work_register_user(struct module *owner);
92+
extern void slow_work_unregister_user(struct module *owner);
8993

9094
#ifdef CONFIG_SYSCTL
9195
extern ctl_table slow_work_sysctls[];

‎kernel/slow-work.c‎

Lines changed: 126 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define SLOW_WORK_OOM_TIMEOUT (5 * HZ) /* can't start new threads for 5s after
2323
* OOM */
2424

25+
#define SLOW_WORK_THREAD_LIMIT 255 /* abs maximum number of slow-work threads */
26+
2527
static void slow_work_cull_timeout(unsigned long);
2628
static void slow_work_oom_timeout(unsigned long);
2729

@@ -46,7 +48,7 @@ static unsigned vslow_work_proportion = 50; /* % of threads that may process
4648

4749
#ifdef CONFIG_SYSCTL
4850
static const int slow_work_min_min_threads = 2;
49-
static int slow_work_max_max_threads = 255;
51+
static int slow_work_max_max_threads = SLOW_WORK_THREAD_LIMIT;
5052
static const int slow_work_min_vslow = 1;
5153
static const int slow_work_max_vslow = 99;
5254

@@ -97,6 +99,23 @@ static DEFINE_TIMER(slow_work_cull_timer, slow_work_cull_timeout, 0, 0);
9799
static DEFINE_TIMER(slow_work_oom_timer, slow_work_oom_timeout, 0, 0);
98100
static struct slow_work slow_work_new_thread; /* new thread starter */
99101

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+
100119
/*
101120
* The queues of work items and the lock governing access to them. These are
102121
* shared between all the CPUs. It doesn't make sense to have per-CPU queues
@@ -149,8 +168,11 @@ static unsigned slow_work_calc_vsmax(void)
149168
* Attempt to execute stuff queued on a slow thread. Return true if we managed
150169
* it, false if there was nothing to do.
151170
*/
152-
static bool slow_work_execute(void)
171+
static bool slow_work_execute(int id)
153172
{
173+
#ifdef CONFIG_MODULES
174+
struct module *module;
175+
#endif
154176
struct slow_work *work = NULL;
155177
unsigned vsmax;
156178
bool very_slow;
@@ -186,6 +208,12 @@ static bool slow_work_execute(void)
186208
} else {
187209
very_slow = false; /* avoid the compiler warning */
188210
}
211+
212+
#ifdef CONFIG_MODULES
213+
if (work)
214+
slow_work_thread_processing[id] = work->owner;
215+
#endif
216+
189217
spin_unlock_irq(&slow_work_queue_lock);
190218

191219
if (!work)
@@ -219,7 +247,18 @@ static bool slow_work_execute(void)
219247
spin_unlock_irq(&slow_work_queue_lock);
220248
}
221249

250+
/* sort out the race between module unloading and put_ref() */
222251
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+
223262
return true;
224263

225264
auto_requeue:
@@ -232,6 +271,7 @@ static bool slow_work_execute(void)
232271
else
233272
list_add_tail(&work->link, &slow_work_queue);
234273
spin_unlock_irq(&slow_work_queue_lock);
274+
slow_work_thread_processing[id] = NULL;
235275
return true;
236276
}
237277

@@ -368,13 +408,22 @@ static inline bool slow_work_available(int vsmax)
368408
*/
369409
static int slow_work_thread(void *_data)
370410
{
371-
int vsmax;
411+
int vsmax, id;
372412

373413
DEFINE_WAIT(wait);
374414

375415
set_freezable();
376416
set_user_nice(current, -5);
377417

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+
378427
for (;;) {
379428
vsmax = vslow_work_proportion;
380429
vsmax *= atomic_read(&slow_work_thread_count);
@@ -395,7 +444,7 @@ static int slow_work_thread(void *_data)
395444
vsmax *= atomic_read(&slow_work_thread_count);
396445
vsmax /= 100;
397446

398-
if (slow_work_available(vsmax) && slow_work_execute()) {
447+
if (slow_work_available(vsmax) && slow_work_execute(id)) {
399448
cond_resched();
400449
if (list_empty(&slow_work_queue) &&
401450
list_empty(&vslow_work_queue) &&
@@ -412,6 +461,10 @@ static int slow_work_thread(void *_data)
412461
break;
413462
}
414463

464+
spin_lock_irq(&slow_work_queue_lock);
465+
__clear_bit(id, slow_work_ids);
466+
spin_unlock_irq(&slow_work_queue_lock);
467+
415468
if (atomic_dec_and_test(&slow_work_thread_count))
416469
complete_and_exit(&slow_work_last_thread_exited, 0);
417470
return 0;
@@ -475,6 +528,7 @@ static void slow_work_new_thread_execute(struct slow_work *work)
475528
}
476529

477530
static const struct slow_work_ops slow_work_new_thread_ops = {
531+
.owner = THIS_MODULE,
478532
.get_ref = slow_work_new_thread_get_ref,
479533
.put_ref = slow_work_new_thread_put_ref,
480534
.execute = slow_work_new_thread_execute,
@@ -546,12 +600,13 @@ static int slow_work_max_threads_sysctl(struct ctl_table *table, int write,
546600

547601
/**
548602
* slow_work_register_user - Register a user of the facility
603+
* @module: The module about to make use of the facility
549604
*
550605
* Register a user of the facility, starting up the initial threads if there
551606
* aren't any other users at this point. This will return 0 if successful, or
552607
* an error if not.
553608
*/
554-
int slow_work_register_user(void)
609+
int slow_work_register_user(struct module *module)
555610
{
556611
struct task_struct *p;
557612
int loop;
@@ -598,14 +653,79 @@ int slow_work_register_user(void)
598653
}
599654
EXPORT_SYMBOL(slow_work_register_user);
600655

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+
601710
/**
602711
* slow_work_unregister_user - Unregister a user of the facility
712+
* @module: The module whose items should be cleared
603713
*
604714
* Unregister a user of the facility, killing all the threads if this was the
605715
* last one.
716+
*
717+
* This waits for all the work items belonging to the nominated module to go
718+
* away before proceeding.
606719
*/
607-
void slow_work_unregister_user(void)
720+
void slow_work_unregister_user(struct module *module)
608721
{
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 */
609729
mutex_lock(&slow_work_user_lock);
610730

611731
BUG_ON(slow_work_user_count <= 0);

0 commit comments

Comments
 (0)