Skip to content

Commit 1fa44ec

Browse files
James BottomleyJames Bottomley
authored andcommitted
[SCSI] add execute_in_process_context() API
We have several points in the SCSI stack (primarily for our device functions) where we need to guarantee process context, but (given the place where the last reference was released) we cannot guarantee this. This API gets around the issue by executing the function directly if the caller has process context, but scheduling a workqueue to execute in process context if the caller doesn't have it. Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>
1 parent ba3af0a commit 1fa44ec

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎include/linux/workqueue.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ struct work_struct {
2020
struct timer_list timer;
2121
};
2222

23+
struct execute_work {
24+
struct work_struct work;
25+
};
26+
2327
#define __WORK_INITIALIZER(n, f, d) { \
2428
.entry = { &(n).entry, &(n).entry }, \
2529
.func = (f), \
@@ -74,6 +78,8 @@ extern void init_workqueues(void);
7478
void cancel_rearming_delayed_work(struct work_struct *work);
7579
void cancel_rearming_delayed_workqueue(struct workqueue_struct *,
7680
struct work_struct *);
81+
int execute_in_process_context(void (*fn)(void *), void *,
82+
struct execute_work *);
7783

7884
/*
7985
* Kill off a pending schedule_delayed_work(). Note that the work callback

‎kernel/workqueue.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/cpu.h>
2828
#include <linux/notifier.h>
2929
#include <linux/kthread.h>
30+
#include <linux/hardirq.h>
3031

3132
/*
3233
* The per-CPU workqueue (if single thread, we always use the first
@@ -476,6 +477,34 @@ void cancel_rearming_delayed_work(struct work_struct *work)
476477
}
477478
EXPORT_SYMBOL(cancel_rearming_delayed_work);
478479

480+
/**
481+
* execute_in_process_context - reliably execute the routine with user context
482+
* @fn: the function to execute
483+
* @data: data to pass to the function
484+
* @ew: guaranteed storage for the execute work structure (must
485+
* be available when the work executes)
486+
*
487+
* Executes the function immediately if process context is available,
488+
* otherwise schedules the function for delayed execution.
489+
*
490+
* Returns: 0 - function was executed
491+
* 1 - function was scheduled for execution
492+
*/
493+
int execute_in_process_context(void (*fn)(void *data), void *data,
494+
struct execute_work *ew)
495+
{
496+
if (!in_interrupt()) {
497+
fn(data);
498+
return 0;
499+
}
500+
501+
INIT_WORK(&ew->work, fn, data);
502+
schedule_work(&ew->work);
503+
504+
return 1;
505+
}
506+
EXPORT_SYMBOL_GPL(execute_in_process_context);
507+
479508
int keventd_up(void)
480509
{
481510
return keventd_wq != NULL;

0 commit comments

Comments
 (0)