public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] 2.4.21 fix race condition in sg.c
@ 2003-06-27 13:52 Tony Battersby
  2003-06-27 14:10 ` James Bottomley
  0 siblings, 1 reply; 10+ messages in thread
From: Tony Battersby @ 2003-06-27 13:52 UTC (permalink / raw)
  To: marcelo, alan; +Cc: linux-scsi, dougg

Marcelo & Alan:

This patch against 2.4.21 fixes a race condition in the SCSI generic driver.
It has been approved by Doug Gilbert for inclusion.  A similar fix is also
needed for 2.5, although I don't have the 2.5 source tree available to make
a patch.

Note: I have added smp_wmb() since the last time I sent this patch.  I think
it is necessary for 100% correctness.

Anthony J. Battersby
Cybernetics

--- drivers/scsi/sg.c.orig	Fri Jun 13 10:51:36 2003
+++ drivers/scsi/sg.c	Fri Jun 27 09:27:40 2003
@@ -1241,7 +1241,6 @@
     SRpnt->sr_request.rq_dev = MKDEV(0, 0);  /* "sg" _disowns_ request blk */
 
     srp->my_cmdp = NULL;
-    srp->done = 1;
     read_unlock(&sg_dev_arr_lock);
 
     SCSI_LOG_TIMEOUT(4, printk("sg...bh: dev=%d, pack_id=%d, res=0x%x\n",
@@ -1274,6 +1273,9 @@
     }
     /* Rely on write phase to clean out srp status values, so no "else" */
 
+    smp_wmb();
+    srp->done = 1;
+
     scsi_release_request(SRpnt);
     SRpnt = NULL;
     if (sfp->closed) { /* whoops this fd already released, cleanup */


^ permalink raw reply	[flat|nested] 10+ messages in thread
* [PATCH] 2.4.21 fix race condition in sg.c
@ 2003-06-26 15:18 Tony Battersby
  2003-06-26 22:15 ` Douglas Gilbert
  0 siblings, 1 reply; 10+ messages in thread
From: Tony Battersby @ 2003-06-26 15:18 UTC (permalink / raw)
  To: dgilbert, linux-scsi

The function sg_cmd_done_bh() sets srp->done = 1 before setting other fields
to be returned to userspace (such as srp->header.resid).  This is a race
condition since other code assumes that done == 1 means that all fields to
be returned to userspace (via read()) have already been set.  I am seeing
this race condition manifest in a program that uses poll() to wait for any
one of several outstanding commands to complete.  The symptom is that every
once in a while the resid value returned for the command is 0 rather than
the correct value, since poll() showed that the command was complete and
read() returned the completion status for it before sg_cmd_done_bh() had
gotten to the line "srp->header.resid = SCpnt->resid".

This patch against 2.4.21 fixes the problem.

--- drivers/scsi/sg.c.orig	Fri Jun 13 10:51:36 2003
+++ drivers/scsi/sg.c	Thu Jun 26 11:07:28 2003
@@ -1241,7 +1241,6 @@
     SRpnt->sr_request.rq_dev = MKDEV(0, 0);  /* "sg" _disowns_ request blk
*/

     srp->my_cmdp = NULL;
-    srp->done = 1;
     read_unlock(&sg_dev_arr_lock);

     SCSI_LOG_TIMEOUT(4, printk("sg...bh: dev=%d, pack_id=%d, res=0x%x\n",
@@ -1274,6 +1273,8 @@
     }
     /* Rely on write phase to clean out srp status values, so no "else" */

+    srp->done = 1;
+
     scsi_release_request(SRpnt);
     SRpnt = NULL;
     if (sfp->closed) { /* whoops this fd already released, cleanup */

---------------

Anthony J. Battersby
Cybernetics


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2003-06-27 15:24 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-06-27 13:52 [PATCH] 2.4.21 fix race condition in sg.c Tony Battersby
2003-06-27 14:10 ` James Bottomley
2003-06-27 14:31   ` Alan Cox
2003-06-27 14:47     ` James Bottomley
2003-06-27 14:57   ` Tony Battersby
2003-06-27 15:00     ` Jeff Garzik
2003-06-27 15:11     ` James Bottomley
2003-06-27 15:38       ` Tony Battersby
  -- strict thread matches above, loose matches on Subject: below --
2003-06-26 15:18 Tony Battersby
2003-06-26 22:15 ` Douglas Gilbert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox