From mboxrd@z Thu Jan 1 00:00:00 1970 From: James Bottomley Subject: Re: Linux 2.6.16-rc3 Date: Fri, 17 Feb 2006 16:42:43 -0800 Message-ID: <1140223363.3231.9.camel@mulgrave.il.steeleye.com> References: <20060212190520.244fcaec.akpm@osdl.org> <20060213203800.GC22441@kroah.com> <1139934883.14115.4.camel@mulgrave.il.steeleye.com> <1140054960.3037.5.camel@mulgrave.il.steeleye.com> <20060216171200.GD29443@flint.arm.linux.org.uk> <1140112653.3178.9.camel@mulgrave.il.steeleye.com> <20060216180939.GF29443@flint.arm.linux.org.uk> <1140113671.3178.16.camel@mulgrave.il.steeleye.com> <20060216181803.GG29443@flint.arm.linux.org.uk> <1140116969.3178.24.camel@mulgrave.il.steeleye.com> <20060216200138.GA4203@suse.de> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit Return-path: Received: from stat9.steeleye.com ([209.192.50.41]:26601 "EHLO hancock.sc.steeleye.com") by vger.kernel.org with ESMTP id S1750860AbWBRAoJ (ORCPT ); Fri, 17 Feb 2006 19:44:09 -0500 In-Reply-To: <20060216200138.GA4203@suse.de> Sender: linux-scsi-owner@vger.kernel.org List-Id: linux-scsi@vger.kernel.org To: Jens Axboe Cc: Russell King , Greg KH , Andrew Morton , Linus Torvalds , linux-kernel@vger.kernel.org, "Brown, Len" , "David S. Miller" , linux-acpi@vger.kernel.org, linux-usb-devel@lists.sourceforge.net, "Yu, Luming" , Ben Castricum , sanjoy@mrao.cam.ac.uk, Helge Hafting , "Carlo E. Prelz" , Gerrit Bruchh?user , Nicolas.Mailhot@LaPoste.net, Jaroslav Kysela , Takashi Iwai , Patrizio Bassi , Bj?rn Nilsson , Andrey Borzenkov , "P. Christeas" , ghrt , jinhong hu , Andrew Vasquez linux On Thu, 2006-02-16 at 21:01 +0100, Jens Axboe wrote: > That's what I suggested in the first place as well. I still think it's a > good idea, fwiw :) OK smarty pants ... some of us are a bit slower on the uptake. How about this then. It won't solve the target problem, but it will solve the device put. 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 Index: BUILD-2.6/include/linux/workqueue.h =================================================================== --- BUILD-2.6.orig/include/linux/workqueue.h 2006-02-17 13:02:00.000000000 -0600 +++ BUILD-2.6/include/linux/workqueue.h 2006-02-17 17:57:52.000000000 -0600 @@ -20,6 +20,12 @@ struct timer_list timer; }; +struct execute_work { + struct work_struct work; + void (*fn)(void *); + void *data; +}; + #define __WORK_INITIALIZER(n, f, d) { \ .entry = { &(n).entry, &(n).entry }, \ .func = (f), \ @@ -74,6 +80,8 @@ 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 *, + struct execute_work *); /* * 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-17 13:02:01.000000000 -0600 +++ BUILD-2.6/kernel/workqueue.c 2006-02-17 18:00:15.000000000 -0600 @@ -27,6 +27,7 @@ #include #include #include +#include /* * The per-CPU workqueue (if single thread, we always use the first @@ -476,6 +477,45 @@ } EXPORT_SYMBOL(cancel_rearming_delayed_work); +static void execute_in_process_context_work(void *data) +{ + void (*fn)(void *data); + struct execute_work *ew = data; + + fn = ew->fn; + data = ew->data; + + 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 + */ +int execute_in_process_context(void (*fn)(void *data), void *data, + struct execute_work *ew) +{ + if (!in_interrupt()) { + fn(data); + return 0; + } + + INIT_WORK(&ew->work, execute_in_process_context_work, ew); + ew->fn = fn; + ew->data = data; + schedule_work(&ew->work); + + return 1; +} +EXPORT_SYMBOL_GPL(execute_in_process_context); + int keventd_up(void) { return keventd_wq != NULL;