linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Chris Wright <chrisw@sous-sol.org>
To: linux-kernel@vger.kernel.org, stable@kernel.org
Cc: Justin Forbes <jmforbes@linuxtx.org>,
	Zwane Mwaikambo <zwane@arm.linux.org.uk>,
	"Theodore Ts'o" <tytso@mit.edu>,
	Randy Dunlap <rdunlap@xenotime.net>,
	Dave Jones <davej@redhat.com>,
	Chuck Wolber <chuckw@quantumlinux.com>,
	Chris Wedgwood <reviews@ml.cw.f00f.org>,
	Michael Krufky <mkrufky@linuxtv.org>,
	Chuck Ebbert <cebbert@redhat.com>,
	Domenico Andreoli <cavokz@gmail.com>, Willy Tarreau <w@1wt.eu>,
	Rodrigo Rubira Branco <rbranco@la.checkpoint.com>,
	Jake Edge <jake@lwn.net>, Eugene Teo <eteo@redhat.com>,
	torvalds@linux-foundation.org, akpm@linux-foundation.org,
	alan@lxorguk.ukuu.org.uk, Tony Battersby <tonyb@cybernetics.com>,
	FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>,
	Douglas Gilbert <dgilbert@interlog.com>,
	James Bottomley <James.Bottomley@HansenPartnership.com>
Subject: [patch 26/45] SCSI: sg: fix races with ioctl(SG_IO)
Date: Tue, 31 Mar 2009 16:11:11 -0700	[thread overview]
Message-ID: <20090331231616.871423172@sous-sol.org> (raw)
In-Reply-To: 20090331231045.719396245@sous-sol.org

[-- Attachment #1: scsi-sg-fix-races-with-ioctl.patch --]
[-- Type: text/plain, Size: 4220 bytes --]

-stable review patch.  If anyone has any objections, please let us know.
---------------------

From: Tony Battersby <tonyb@cybernetics.com>

upstream commit: a2dd3b4cea335713b58996bb07b3abcde1175f47

sg_io_owned needs to be set before the command is sent to the midlevel;
otherwise, a quickly-completing command may cause a different CPU
to see "srp->done == 1 && !srp->sg_io_owned", which would lead to
incorrect behavior.

Check srp->done and set srp->orphan while holding rq_list_lock to
prevent races with sg_rq_end_io().

There is no need to check sfp->closed from read/write/ioctl/poll/etc.
since the kernel guarantees that this won't happen.

The usefulness of sg_srp_done() was questionable before; now it is
definitely not needed.

Signed-off-by: Tony Battersby <tonyb@cybernetics.com>
Acked-by: Douglas Gilbert <dgilbert@interlog.com>
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
---
 drivers/scsi/sg.c |   39 ++++++++++++++-------------------------
 1 file changed, 14 insertions(+), 25 deletions(-)

--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -189,7 +189,7 @@ static ssize_t sg_new_read(Sg_fd * sfp, 
 			   Sg_request * srp);
 static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
 			const char __user *buf, size_t count, int blocking,
-			int read_only, Sg_request **o_srp);
+			int read_only, int sg_io_owned, Sg_request **o_srp);
 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
 			   unsigned char *cmnd, int timeout, int blocking);
 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
@@ -561,7 +561,8 @@ sg_write(struct file *filp, const char _
 		return -EFAULT;
 	blocking = !(filp->f_flags & O_NONBLOCK);
 	if (old_hdr.reply_len < 0)
-		return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
+		return sg_new_write(sfp, filp, buf, count,
+				    blocking, 0, 0, NULL);
 	if (count < (SZ_SG_HEADER + 6))
 		return -EIO;	/* The minimum scsi command length is 6 bytes. */
 
@@ -642,7 +643,7 @@ sg_write(struct file *filp, const char _
 
 static ssize_t
 sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
-		 size_t count, int blocking, int read_only,
+		 size_t count, int blocking, int read_only, int sg_io_owned,
 		 Sg_request **o_srp)
 {
 	int k;
@@ -662,6 +663,7 @@ sg_new_write(Sg_fd *sfp, struct file *fi
 		SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
 		return -EDOM;
 	}
+	srp->sg_io_owned = sg_io_owned;
 	hp = &srp->header;
 	if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
 		sg_remove_request(sfp, srp);
@@ -766,18 +768,6 @@ sg_common_write(Sg_fd * sfp, Sg_request 
 }
 
 static int
-sg_srp_done(Sg_request *srp, Sg_fd *sfp)
-{
-	unsigned long iflags;
-	int done;
-
-	read_lock_irqsave(&sfp->rq_list_lock, iflags);
-	done = srp->done;
-	read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
-	return done;
-}
-
-static int
 sg_ioctl(struct inode *inode, struct file *filp,
 	 unsigned int cmd_in, unsigned long arg)
 {
@@ -809,27 +799,26 @@ sg_ioctl(struct inode *inode, struct fil
 				return -EFAULT;
 			result =
 			    sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
-					 blocking, read_only, &srp);
+					 blocking, read_only, 1, &srp);
 			if (result < 0)
 				return result;
-			srp->sg_io_owned = 1;
 			while (1) {
 				result = 0;	/* following macro to beat race condition */
 				__wait_event_interruptible(sfp->read_wait,
-					(sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
-							   result);
+					(srp->done || sdp->detached),
+					result);
 				if (sdp->detached)
 					return -ENODEV;
-				if (sfp->closed)
-					return 0;	/* request packet dropped already */
-				if (0 == result)
+				write_lock_irq(&sfp->rq_list_lock);
+				if (srp->done) {
+					srp->done = 2;
+					write_unlock_irq(&sfp->rq_list_lock);
 					break;
+				}
 				srp->orphan = 1;
+				write_unlock_irq(&sfp->rq_list_lock);
 				return result;	/* -ERESTARTSYS because signal hit process */
 			}
-			write_lock_irqsave(&sfp->rq_list_lock, iflags);
-			srp->done = 2;
-			write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
 			result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
 			return (result < 0) ? result : 0;
 		}


  parent reply	other threads:[~2009-03-31 23:23 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-31 23:10 [patch 00/45] 2.6.29.1 -stable review Chris Wright
2009-03-31 23:10 ` [patch 01/45] netfilter: nf_conntrack_tcp: fix unaligned memory access in tcp_sack Chris Wright
2009-03-31 23:10 ` [patch 02/45] udp: Wrong locking code in udp seq_file infrastructure Chris Wright
2009-03-31 23:10 ` [patch 03/45] dnet: drivers/net/dnet.c needs <linux/io.h> Chris Wright
2009-03-31 23:10 ` [patch 04/45] bridge: bad error handling when adding invalid ether address Chris Wright
2009-03-31 23:10 ` [patch 05/45] GRO: Disable GRO on legacy netif_rx path Chris Wright
2009-03-31 23:10 ` [patch 06/45] ipv6: Plug sk_buff leak in ipv6_rcv (net/ipv6/ip6_input.c) Chris Wright
2009-03-31 23:10 ` [patch 07/45] xfrm: spin_lock() should be spin_unlock() in xfrm_state.c Chris Wright
2009-03-31 23:10 ` [patch 08/45] USB: EHCI: add software retry for transaction errors Chris Wright
2009-03-31 23:10 ` [patch 09/45] USB: fix USB_STORAGE_CYPRESS_ATACB Chris Wright
2009-03-31 23:10 ` [patch 10/45] USB: usb-storage: increase max_sectors for tape drives Chris Wright
2009-03-31 23:10 ` [patch 11/45] USB: gadget: fix rndis regression Chris Wright
2009-03-31 23:10 ` [patch 12/45] USB: add quirk to avoid config and interface strings Chris Wright
2009-03-31 23:10 ` [patch 13/45] KVM: VMX: Dont allow uninhibited access to EFER on i386 Chris Wright
2009-03-31 23:10 ` [patch 14/45] KVM: SVM: set accessed bit for VMCB segment selectors Chris Wright
2009-03-31 23:11 ` [patch 15/45] ath9k: downgrade xmit queue full message to xmit debug Chris Wright
2009-03-31 23:11 ` [patch 16/45] cifs: fix buffer format byte on NT Rename/hardlink Chris Wright
2009-03-31 23:11 ` [patch 17/45] ath5k: use spin_lock_irqsave for beacon lock Chris Wright
2009-03-31 23:11 ` [patch 18/45] ath9k: fix dma mapping leak of rx buffer upon rmmod Chris Wright
2009-03-31 23:11 ` [patch 19/45] b43: fix b43_plcp_get_bitrate_idx_ofdm return type Chris Wright
2009-03-31 23:11 ` [patch 20/45] ath5k: disable MIB interrupts Chris Wright
2009-03-31 23:11 ` [patch 21/45] ath5k: warn and correct rate for unknown hw rate indexes Chris Wright
2009-03-31 23:11 ` [patch 22/45] CIFS: Fix memory overwrite when saving nativeFileSystem field during mount Chris Wright
2009-03-31 23:11 ` [patch 23/45] cfg80211: force last_request to be set for OLD_REG if regdom is EU Chris Wright
2009-03-31 23:11 ` [patch 24/45] DVB: firedtv: FireDTV S2 problems with tuning solved Chris Wright
2009-03-31 23:11 ` [patch 25/45] SCSI: sg: fix races during device removal Chris Wright
2009-03-31 23:31   ` Linus Torvalds
2009-04-01  0:10     ` James Bottomley
2009-04-01  0:18       ` Linus Torvalds
2009-04-01  1:15         ` Chris Wright
2009-04-01  1:54           ` FUJITA Tomonori
2009-04-01 15:18             ` Tony Battersby
2009-03-31 23:11 ` Chris Wright [this message]
2009-03-31 23:11 ` [patch 27/45] SCSI: sg: avoid blk_put_request/blk_rq_unmap_user in interrupt Chris Wright
2009-03-31 23:11 ` [patch 28/45] ARM: pxa: fix overlay being un-necessarily initialized on pxa25x Chris Wright
2009-03-31 23:11 ` [patch 29/45] ARM: 5428/1: Module relocation update for R_ARM_V4BX Chris Wright
2009-03-31 23:11 ` [patch 30/45] ARM: cumana: Fix a long standing bogon Chris Wright
2009-03-31 23:11 ` [patch 31/45] ARM: fix leak in iop13xx/pci Chris Wright
2009-03-31 23:11 ` [patch 32/45] ARM: twl4030 - leak fix Chris Wright
2009-03-31 23:11 ` [patch 33/45] ARM: 5435/1: fix compile warning in sanity_check_meminfo() Chris Wright
2009-03-31 23:11 ` [patch 34/45] fuse: fix fuse_file_lseek returning with lock held Chris Wright
2009-03-31 23:11 ` [patch 35/45] Add a missing unlock_kernel() in raw_open() Chris Wright
2009-03-31 23:11 ` [patch 36/45] x86, PAT, PCI: Change vma prot in pci_mmap to reflect inherited prot Chris Wright
2009-03-31 23:11 ` [patch 37/45] x86, uv: fix cpumask iterator in uv_bau_init() Chris Wright
2009-03-31 23:11 ` [patch 38/45] x86: fix 64k corruption-check Chris Wright
2009-03-31 23:11 ` [patch 39/45] x86: ptrace, bts: fix an unreachable statement Chris Wright
2009-03-31 23:11 ` [patch 40/45] x86: mtrr: dont modify RdDram/WrDram bits of fixed MTRRs Chris Wright
2009-03-31 23:11 ` [patch 41/45] VM, x86, PAT: Change is_linear_pfn_mapping to not use vm_pgoff Chris Wright
2009-03-31 23:11 ` [patch 42/45] lguest: wire up pte_update/pte_update_defer Chris Wright
2009-03-31 23:11 ` [patch 43/45] lguest: fix spurious BUG_ON() on invalid guest stack Chris Wright
2009-03-31 23:11 ` [patch 44/45] cfg80211: fix incorrect assumption on last_request for 11d Chris Wright
2009-03-31 23:11 ` [patch 45/45] KVM: MMU: Fix another largepage memory leak Chris Wright
2009-04-01  3:47 ` [patch 00/45] 2.6.29.1 -stable review David Miller
2009-04-01  4:42   ` Michael Krufky
2009-04-02  6:57     ` Chris Wright
2009-04-02  6:57 ` [PATCH 46/45] sparc64: Fix MM refcount check in smp_flush_tlb_pending() Chris Wright
2009-04-02  6:57 ` [PATCH 47/45] sparc64: Flush TLB before releasing pages Chris Wright
2009-04-02  6:58 ` [PATCH 48/45] sparc64: Fix reset hangs on Niagara systems Chris Wright
2009-04-02  6:58 ` [PATCH 49/45] V4L: v4l2-common: remove incorrect MODULE test Chris Wright

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090331231616.871423172@sous-sol.org \
    --to=chrisw@sous-sol.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=cavokz@gmail.com \
    --cc=cebbert@redhat.com \
    --cc=chuckw@quantumlinux.com \
    --cc=davej@redhat.com \
    --cc=dgilbert@interlog.com \
    --cc=eteo@redhat.com \
    --cc=fujita.tomonori@lab.ntt.co.jp \
    --cc=jake@lwn.net \
    --cc=jmforbes@linuxtx.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkrufky@linuxtv.org \
    --cc=rbranco@la.checkpoint.com \
    --cc=rdunlap@xenotime.net \
    --cc=reviews@ml.cw.f00f.org \
    --cc=stable@kernel.org \
    --cc=tonyb@cybernetics.com \
    --cc=torvalds@linux-foundation.org \
    --cc=tytso@mit.edu \
    --cc=w@1wt.eu \
    --cc=zwane@arm.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).