lkml.org 
[lkml]   [2006]   [Feb]   [7]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    Subject[PATCH] add execute_in_process_context() API
    From
    Date
    SCSI needs this for our scheme to avoid executing generic device release
    calls from interrupt context (SCSI patch using this to follow).

    If no-one objects, I'd like to slide this into the scsi-rc-fixes-2.6
    tree for 2.6.16.

    James

    --

    [PATCH] 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>

    Index: BUILD-2.6/include/linux/workqueue.h
    ===================================================================
    --- BUILD-2.6.orig/include/linux/workqueue.h 2006-02-07 09:22:30.000000000 -0600
    +++ BUILD-2.6/include/linux/workqueue.h 2006-02-07 10:22:29.000000000 -0600
    @@ -74,6 +74,7 @@
    void cancel_rearming_delayed_work(struct work_struct *work);
    void cancel_rearming_delayed_workqueue(struct workqueue_struct *,
    struct work_struct *);
    +int execute_in_process_context(void (*fn)(void *), void *);

    /*
    * Kill off a pending schedule_delayed_work(). Note that the work callback
    Index: BUILD-2.6/kernel/workqueue.c
    ===================================================================
    --- BUILD-2.6.orig/kernel/workqueue.c 2006-02-07 09:22:30.000000000 -0600
    +++ BUILD-2.6/kernel/workqueue.c 2006-02-07 11:07:47.000000000 -0600
    @@ -27,6 +27,7 @@
    #include <linux/cpu.h>
    #include <linux/notifier.h>
    #include <linux/kthread.h>
    +#include <linux/hardirq.h>

    /*
    * The per-CPU workqueue (if single thread, we always use the first
    @@ -476,6 +477,63 @@
    }
    EXPORT_SYMBOL(cancel_rearming_delayed_work);

    +struct work_queue_work {
    + struct work_struct work;
    + void (*fn)(void *);
    + void *data;
    +};
    +
    +static void execute_in_process_context_work(void *data)
    +{
    + void (*fn)(void *data);
    + struct work_queue_work *wqw = data;
    +
    + fn = wqw->fn;
    + data = wqw->data;
    +
    + kfree(wqw);
    +
    + fn(data);
    +}
    +
    +/**
    + * execute_in_process_context - reliably execute the routine with user context
    + * @fn: the function to execute
    + * @data: data to pass to the function
    + *
    + * Executes the function immediately if process context is available,
    + * otherwise schedules the function for delayed execution.
    + *
    + * Returns: 0 - function was executed
    + * 1 - function was scheduled for execution
    + * <0 - error
    + */
    +int execute_in_process_context(void (*fn)(void *data), void *data)
    +{
    + struct work_queue_work *wqw;
    +
    + if (!in_interrupt()) {
    + fn(data);
    + return 0;
    + }
    +
    + wqw = kmalloc(sizeof(struct work_queue_work), GFP_ATOMIC);
    +
    + if (unlikely(!wqw)) {
    + printk(KERN_ERR "Failed to allocate memory\n");
    + WARN_ON(1);
    + return -ENOMEM;
    + }
    +
    + INIT_WORK(&wqw->work, execute_in_process_context_work, wqw);
    + wqw->fn = fn;
    + wqw->data = data;
    + schedule_work(&wqw->work);
    +
    + return 1;
    +}
    +EXPORT_SYMBOL_GPL(execute_in_process_context);
    +
    int keventd_up(void)
    {
    return keventd_wq != NULL;

    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/

    \
     
     \ /
      Last update: 2006-02-07 21:02    [W:4.199 / U:0.008 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site