Skip to contentSkip to conversation

add execute_in_process_context() API

PATCH In review James Bottomley 6 participants 12 messages Feb 7, 2006 – Feb 14, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add execute_in_process_context() API

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.

include/linux/workqueue.h +1 -0
Index: BUILD-2.6/include/linux/workqueue.h
===================================================================
@@ -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
kernel/workqueue.c +58 -0

70 lines not on this page · raw message

#1 lore raw

[SCSI] fix wrong context bugs in SCSI

This is the second half of the execute_process_context() API addition:
an actual user

James

--

[SCSI] fix wrong context bugs in SCSI

There's a bug in releasing scsi_device where the release function actually frees the block queue. However, the block queue release calls flush_work(), which requires process context (the scsi_device structure may release from irq context). Update the release function to invoke via the execute_in_process_context() API.

Also clean up the scsi_target structure releasing via this API.

drivers/scsi/scsi_scan.c +4 -22
Index: BUILD-2.6/drivers/scsi/scsi_scan.c
===================================================================
@@ -385,19 +385,12 @@
 	return found_target;
 }
 
-struct work_queue_wrapper {
-	struct work_struct	work;
-	struct scsi_target	*starget;
-};
-
-static void scsi_target_reap_work(void *data) {
-	struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
-	struct scsi_target *starget = wqw->starget;
+static void scsi_target_reap_usercontext(void *data)
+{
+	struct scsi_target *starget = data;
 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
 	unsigned long flags;
 
-	kfree(wqw);
-
 	spin_lock_irqsave(shost->host_lock, flags);
 
 	if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
@@ -426,18 +419,7 @@
  */
 void scsi_target_reap(struct scsi_target *starget)
 {
-	struct work_queue_wrapper *wqw = 
-		kzalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
-
-	if (!wqw) {
-		starget_printk(KERN_ERR, starget,
-			       "Failed to allocate memory in scsi_reap_target()\n");
-		return;
-	}
-
-	INIT_WORK(&wqw->work, scsi_target_reap_work, wqw);
-	wqw->starget = starget;
-	schedule_work(&wqw->work);
+	execute_in_process_context(scsi_target_reap_usercontext, starget);
 }
 
 /**
drivers/scsi/scsi_sysfs.c +8 -1
Index: BUILD-2.6/drivers/scsi/scsi_sysfs.c
===================================================================
@@ -217,8 +217,9 @@
 	put_device(&sdev->sdev_gendev);
 }
 
-static void scsi_device_dev_release(struct device *dev)
+static void scsi_device_dev_release_usercontext(void *data)
 {
+	struct device *dev = data;
 	struct scsi_device *sdev;
 	struct device *parent;
 	struct scsi_target *starget;
@@ -237,6 +238,7 @@
 
 	if (sdev->request_queue) {
 		sdev->request_queue->queuedata = NULL;
+		/* user context needed to free queue */
 		scsi_free_queue(sdev->request_queue);
 		/* temporary expedient, try to catch use of queue lock
 		 * after free of sdev */
@@ -252,6 +254,11 @@
 		put_device(parent);
 }
 
+static void scsi_device_dev_release(struct device *dev)
+{
+	execute_in_process_context(scsi_device_dev_release_usercontext,	dev);
+}
+
 static struct class sdev_class = {
 	.name		= "scsi_device",
 	.release	= scsi_device_cls_release,
#2 lore raw

Re: [SCSI] fix wrong context bugs in SCSI

Quoted 2 lines · James Bottomley

This is the second half of the execute_process_context() API addition:
an actual user

I just tried this out on my ppc64 box. I recently noticed that the target reaping via workqueue change that went in not too long ago resulted in *really* slow user initiated wildcard scans for ipr adapters - in the neighborhood of 6 minutes... Your patch brings that back down to 8 seconds.

Brian

Quoted 106 lines

James

--

[SCSI] fix wrong context bugs in SCSI

There's a bug in releasing scsi_device where the release function actually frees the block queue. However, the block queue release calls flush_work(), which requires process context (the scsi_device structure may release from irq context). Update the release function to invoke via the execute_in_process_context() API.

Also clean up the scsi_target structure releasing via this API.

Index: BUILD-2.6/drivers/scsi/scsi_scan.c
===================================================================

drivers/scsi/scsi_scan.c +4 -22
@@ -385,19 +385,12 @@
 	return found_target;
 }
 
-struct work_queue_wrapper {
-	struct work_struct	work;
-	struct scsi_target	*starget;
-};
-
-static void scsi_target_reap_work(void *data) {
-	struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
-	struct scsi_target *starget = wqw->starget;
+static void scsi_target_reap_usercontext(void *data)
+{
+	struct scsi_target *starget = data;
 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
 	unsigned long flags;
 
-	kfree(wqw);
-
 	spin_lock_irqsave(shost->host_lock, flags);
 
 	if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
@@ -426,18 +419,7 @@
  */
 void scsi_target_reap(struct scsi_target *starget)
 {
-	struct work_queue_wrapper *wqw = 
-		kzalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
-
-	if (!wqw) {
-		starget_printk(KERN_ERR, starget,
-			       "Failed to allocate memory in scsi_reap_target()\n");
-		return;
-	}
-
-	INIT_WORK(&wqw->work, scsi_target_reap_work, wqw);
-	wqw->starget = starget;
-	schedule_work(&wqw->work);
+	execute_in_process_context(scsi_target_reap_usercontext, starget);
 }
 
 /**

Index: BUILD-2.6/drivers/scsi/scsi_sysfs.c
===================================================================

drivers/scsi/scsi_sysfs.c +8 -2
@@ -217,8 +217,9 @@
 	put_device(&sdev->sdev_gendev);
 }
 
-static void scsi_device_dev_release(struct device *dev)
+static void scsi_device_dev_release_usercontext(void *data)
 {
+	struct device *dev = data;
 	struct scsi_device *sdev;
 	struct device *parent;
 	struct scsi_target *starget;
@@ -237,6 +238,7 @@
 
 	if (sdev->request_queue) {
 		sdev->request_queue->queuedata = NULL;
+		/* user context needed to free queue */
 		scsi_free_queue(sdev->request_queue);
 		/* temporary expedient, try to catch use of queue lock
 		 * after free of sdev */
@@ -252,6 +254,11 @@
 		put_device(parent);
 }
 
+static void scsi_device_dev_release(struct device *dev)
+{
+	execute_in_process_context(scsi_device_dev_release_usercontext,	dev);
+}
+
 static struct class sdev_class = {
 	.name		= "scsi_device",
 	.release	= scsi_device_cls_release,
 
 
-

To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
Brian King
eServer Storage I/O
IBM Linux Technology Center
#3 lore raw

Re: [SCSI] fix wrong context bugs in SCSI

Quoted 5 lines · Brian King

I just tried this out on my ppc64 box. I recently noticed that the target reaping via workqueue change that went in not too long ago resulted in *really* slow user initiated wildcard scans for ipr adapters - in the neighborhood of 6 minutes... Your patch brings that back down to 8 seconds.

Yes, that's because the original fix used a workqueue for everything (whether it needed it or not). The new API checks the context first before invoking a workqueue. By and large, for SCSI, we actually have user context most of the time, so the old fix was rather heavy handed.

James

#4 lore raw

Re: [SCSI] fix wrong context bugs in SCSI

In reply to James Bottomley

drivers/scsi/scsi_sysfs.c line 261

Quoted 5 lines · James Bottomley
drivers/scsi/scsi_sysfs.c +5 -0
+static void scsi_device_dev_release(struct device *dev)
+{
+	execute_in_process_context(scsi_device_dev_release_usercontext,	dev);
+}
+

Hmm, this (and further up) could fail, yet you don't check.

I don't think this API is very nice to be honest, there's no good way to handle failures - you can't just sleep and loop retry the execute if you are in_interrupt(). I'd prefer passing in a work_queue_work (with a better name :-) that has been allocated at a reliable time during initialization.

Signature · 1 line
Jens Axboe
#5 lore raw

Re: [SCSI] fix wrong context bugs in SCSI

Quoted 1 line · Jens Axboe

Hmm, this (and further up) could fail, yet you don't check.

By and large, you have process context, so this isn't going to be a problem.

Quoted 5 lines

I don't think this API is very nice to be honest, there's no good way to handle failures - you can't just sleep and loop retry the execute if you are in_interrupt(). I'd prefer passing in a work_queue_work (with a better name :-) that has been allocated at a reliable time during initialization.

Yes, I agree ... however, the failure is less prevalent in the new code than the old. The problem is that we may need to execute multiple puts for a single target from irq contex, so under this scheme you need a wqw (potentially) for every get.

I could solve this by binding the API more tightly into the device model, so the generic device contains the wqw and it is told that the release function of the final put must be called in process context, but that's an awful lot of code changes.

James

#6 lore raw

Re: [SCSI] fix wrong context bugs in SCSI

Quoted 17 lines · James Bottomley
Quoted 1 line · Jens Axboe

Hmm, this (and further up) could fail, yet you don't check.

By and large, you have process context, so this isn't going to be a problem.

Quoted 5 lines

I don't think this API is very nice to be honest, there's no good way to handle failures - you can't just sleep and loop retry the execute if you are in_interrupt(). I'd prefer passing in a work_queue_work (with a better name :-) that has been allocated at a reliable time during initialization.

Yes, I agree ... however, the failure is less prevalent in the new code than the old. The problem is that we may need to execute multiple puts for a single target from irq contex, so under this scheme you need a wqw (potentially) for every get.

Quoted 4 lines

I could solve this by binding the API more tightly into the device model, so the generic device contains the wqw and it is told that the release function of the final put must be called in process context, but that's an awful lot of code changes.

Yeah it does get a lot more complicated. I guess I'm fine with the current change, but please just keep it in SCSI then. It's not the sort of thing you'd want to advertise as an exported API.

Signature · 1 line
Jens Axboe
#7 lore raw

Re: [SCSI] fix wrong context bugs in SCSI

Quoted 3 lines · Jens Axboe

Yeah it does get a lot more complicated. I guess I'm fine with the current change, but please just keep it in SCSI then. It's not the sort of thing you'd want to advertise as an exported API.

OK, this pulls the API into scsi for 2.6.16

James

---

[PATCH] add scsi_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. Unfortunately, it requires memory allocation in interrupt context, but it's better than what we have previously. The true solution will require a bit of re-engineering, so isn't appropriate for 2.6.16.

drivers/scsi/scsi_lib.c +59 -0
Index: BUILD-2.6/drivers/scsi/scsi_lib.c
===================================================================
@@ -16,6 +16,7 @@
 #include <linux/init.h>
 #include <linux/pci.h>
 #include <linux/delay.h>
+#include <linux/hardirq.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_dbg.h>
@@ -2248,3 +2249,61 @@
 		device_for_each_child(dev, NULL, target_unblock);
 }
 EXPORT_SYMBOL_GPL(scsi_target_unblock);
+
+
+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);
+}
+
+/**
+ * scsi_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 scsi_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(scsi_execute_in_process_context);
include/scsi/scsi.h +2 -0
Index: BUILD-2.6/include/scsi/scsi.h
===================================================================
@@ -433,4 +433,6 @@
 /* Used to obtain the PCI location of a device */
 #define SCSI_IOCTL_GET_PCI		0x5387
 
+int scsi_execute_in_process_context(void (*fn)(void *data), void *data);
+
 #endif /* _SCSI_SCSI_H */
#8 lore raw

Re: [SCSI] fix wrong context bugs in SCSI

And the second part

James

---

[SCSI] fix wrong context bugs in SCSI

There's a bug in releasing scsi_device where the release function actually frees the block queue. However, the block queue release calls flush_work(), which requires process context (the scsi_device structure may release from irq context). Update the release function to invoke via the execute_in_process_context() API.

Also clean up the scsi_target structure releasing via this API.

drivers/scsi/scsi_scan.c +4 -22
Index: BUILD-2.6/drivers/scsi/scsi_scan.c
===================================================================
@@ -387,19 +387,12 @@
 	return found_target;
 }
 
-struct work_queue_wrapper {
-	struct work_struct	work;
-	struct scsi_target	*starget;
-};
-
-static void scsi_target_reap_work(void *data) {
-	struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
-	struct scsi_target *starget = wqw->starget;
+static void scsi_target_reap_usercontext(void *data)
+{
+	struct scsi_target *starget = data;
 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
 	unsigned long flags;
 
-	kfree(wqw);
-
 	spin_lock_irqsave(shost->host_lock, flags);
 
 	if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
@@ -428,18 +421,7 @@
  */
 void scsi_target_reap(struct scsi_target *starget)
 {
-	struct work_queue_wrapper *wqw = 
-		kzalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
-
-	if (!wqw) {
-		starget_printk(KERN_ERR, starget,
-			       "Failed to allocate memory in scsi_reap_target()\n");
-		return;
-	}
-
-	INIT_WORK(&wqw->work, scsi_target_reap_work, wqw);
-	wqw->starget = starget;
-	schedule_work(&wqw->work);
+	scsi_execute_in_process_context(scsi_target_reap_usercontext, starget);
 }
 
 /**
drivers/scsi/scsi_sysfs.c +8 -1
Index: BUILD-2.6/drivers/scsi/scsi_sysfs.c
===================================================================
@@ -217,8 +217,9 @@
 	put_device(&sdev->sdev_gendev);
 }
 
-static void scsi_device_dev_release(struct device *dev)
+static void scsi_device_dev_release_usercontext(void *data)
 {
+	struct device *dev = data;
 	struct scsi_device *sdev;
 	struct device *parent;
 	struct scsi_target *starget;
@@ -237,6 +238,7 @@
 
 	if (sdev->request_queue) {
 		sdev->request_queue->queuedata = NULL;
+		/* user context needed to free queue */
 		scsi_free_queue(sdev->request_queue);
 		/* temporary expedient, try to catch use of queue lock
 		 * after free of sdev */
@@ -252,6 +254,11 @@
 		put_device(parent);
 }
 
+static void scsi_device_dev_release(struct device *dev)
+{
+	scsi_execute_in_process_context(scsi_device_dev_release_usercontext,	dev);
+}
+
 static struct class sdev_class = {
 	.name		= "scsi_device",
 	.release	= scsi_device_cls_release,
#9 lore raw
In reply to James Bottomley
Quoted 14 lines · James Bottomley
kernel/workqueue.c +14 -0
+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);

kernel/workqueue.c line 534

Quoted 10 lines
kernel/workqueue.c +10 -0
+		return -ENOMEM;
+	}
+
+	INIT_WORK(&wqw->work, execute_in_process_context_work, wqw);
+	wqw->fn = fn;
+	wqw->data = data;
+	schedule_work(&wqw->work);
+
+	return 1;
+}

After the workqueue has run, what free's wqw ?

Dave

#10 lore raw
Quoted 20 lines · Dave Jones
Quoted 18 lines · James Bottomley
kernel/workqueue.c +18 -0
+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);
Quoted 9 lines

kernel/workqueue.c line 534

Quoted 6 lines
kernel/workqueue.c +6 -0
+	wqw->fn = fn;
+	wqw->data = data;
+	schedule_work(&wqw->work);
+
+	return 1;
+}

After the workqueue has run, what free's wqw ?

The callback (execute_in_process_context_work())

The trap with this patch is that the caller has to run flush_scheduled_work() at the right time. But hopefully anyone who's using it knows that by now.

#11 lore raw
In reply to James Bottomley
Quoted 2 lines · James Bottomley

SCSI needs this for our scheme to avoid executing generic device release calls from interrupt context (SCSI patch using this to follow).

Shouldn't we rather fix the SCSI low level drivers or SCSI transport layers to trigger device releases only from process context? (Instead of SCSI core transparently falling back to a workqueue, that is.)

I know only one of these drivers, hence I don't know which are actually affected and how much work that would be.

Signature · 3 lines
Stefan Richter
-=====-=-==- --=- -=---
http://arcgraph.de/sr/