From: "Jörn Engel" <joern@logfs.org>
To: Doug Gilbert <dgilbert@interlog.com>
Cc: "James E.J. Bottomley" <JBottomley@parallels.com>,
linux-scsi@vger.kernel.org
Subject: [PATCH 07/10] sg: protect sdp->exclude
Date: Thu, 12 Apr 2012 17:34:19 -0400 [thread overview]
Message-ID: <20120412213418.GG17388@logfs.org> (raw)
In-Reply-To: <20120412213217.GA17388@logfs.org>
sdp->exclude was previously protected by the BKL. The sg_mutex, which
replaced the BKL, only semi-protected it, as it was missing from
sg_release() and sg_proc_seq_show_debug(). Take an explicit spinlock
for it.
Signed-off-by: Joern Engel <joern@logfs.org>
---
drivers/scsi/sg.c | 36 +++++++++++++++++++++++++++++-------
1 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 758e7b4..a70018e 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -105,6 +105,7 @@ static int sg_add(struct device *, struct class_interface *);
static void sg_remove(struct device *, struct class_interface *);
static DEFINE_MUTEX(sg_mutex);
+static DEFINE_SPINLOCK(sg_open_exclusive_lock);
static DEFINE_IDR(sg_index_idr);
static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
@@ -173,7 +174,8 @@ typedef struct sg_device { /* holds the state of each scsi generic device */
u32 index; /* device index number */
struct list_head sfds;
volatile char detached; /* 0->attached, 1->detached pending removal */
- volatile char exclude; /* opened for exclusive access */
+ /* exclude protected by sg_open_exclusive_lock */
+ char exclude; /* opened for exclusive access */
char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
struct gendisk *disk;
struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
@@ -221,6 +223,26 @@ static int sg_allow_access(struct file *filp, unsigned char *cmd)
return blk_verify_command(cmd, filp->f_mode & FMODE_WRITE);
}
+static int get_exclude(Sg_device *sdp)
+{
+ unsigned long flags;
+ int ret;
+
+ spin_lock_irqsave(&sg_open_exclusive_lock, flags);
+ ret = sdp->exclude;
+ spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
+ return ret;
+}
+
+static void set_exclude(Sg_device *sdp, char val)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&sg_open_exclusive_lock, flags);
+ sdp->exclude = val;
+ spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
+}
+
static int
sg_open(struct inode *inode, struct file *filp)
{
@@ -269,17 +291,17 @@ sg_open(struct inode *inode, struct file *filp)
goto error_out;
}
res = wait_event_interruptible(sdp->o_excl_wait,
- ((!list_empty(&sdp->sfds) || sdp->exclude) ? 0 : (sdp->exclude = 1)));
+ ((!list_empty(&sdp->sfds) || get_exclude(sdp)) ? 0 : set_exclude(sdp, 1), 1));
if (res) {
retval = res; /* -ERESTARTSYS because signal hit process */
goto error_out;
}
- } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */
+ } else if (get_exclude(sdp)) { /* some other fd has an exclusive lock on dev */
if (flags & O_NONBLOCK) {
retval = -EBUSY;
goto error_out;
}
- res = wait_event_interruptible(sdp->o_excl_wait, !sdp->exclude);
+ res = wait_event_interruptible(sdp->o_excl_wait, !get_exclude(sdp));
if (res) {
retval = res; /* -ERESTARTSYS because signal hit process */
goto error_out;
@@ -298,7 +320,7 @@ sg_open(struct inode *inode, struct file *filp)
filp->private_data = sfp;
else {
if (flags & O_EXCL) {
- sdp->exclude = 0; /* undo if error */
+ set_exclude(sdp, 0); /* undo if error */
wake_up_interruptible(&sdp->o_excl_wait);
}
retval = -ENOMEM;
@@ -329,7 +351,7 @@ sg_release(struct inode *inode, struct file *filp)
return -ENXIO;
SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
- sdp->exclude = 0;
+ set_exclude(sdp, 0);
wake_up_interruptible(&sdp->o_excl_wait);
scsi_autopm_put_device(sdp->device);
@@ -2602,7 +2624,7 @@ static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
scsidp->lun,
scsidp->host->hostt->emulated);
seq_printf(s, " sg_tablesize=%d excl=%d\n",
- sdp->sg_tablesize, sdp->exclude);
+ sdp->sg_tablesize, get_exclude(sdp));
sg_proc_debug_helper(s, sdp);
}
read_unlock_irqrestore(&sg_index_lock, iflags);
--
1.7.9.1
next prev parent reply other threads:[~2012-04-12 23:30 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-12 21:32 [PATCH 01/10] sg: remove unnecessary indentation Jörn Engel
2012-04-12 21:32 ` [PATCH 02/10] sg: remove while (1) non-loop Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
2012-04-12 21:33 ` [PATCH 03/10] " Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
2012-04-12 21:33 ` [PATCH 04/10] sg: use wait_event_interruptible() Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
2012-04-12 21:33 ` [PATCH 05/10] sg: remove closed flag Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
2012-04-12 21:33 ` [PATCH 06/10] sg: prevent unwoken sleep Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
2012-04-12 21:34 ` Jörn Engel [this message]
2012-04-23 22:13 ` [PATCH 07/10] sg: protect sdp->exclude Douglas Gilbert
2012-04-24 20:13 ` [PATCH v2 " Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
2012-04-24 20:16 ` [PATCH " Jörn Engel
2012-04-12 21:34 ` [PATCH 08/10] sg: completely protect sfds Jörn Engel
2012-04-25 15:17 ` [PATCH v2 " Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
2012-04-12 21:35 ` [PATCH 09/10] sg: remove sg_mutex Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
2012-04-12 21:35 ` [PATCH 10/10] sg: constify sg_proc_leaf_arr Jörn Engel
2012-05-16 21:58 ` Douglas Gilbert
2012-05-07 19:44 ` [PATCH 01/10] sg: remove unnecessary indentation Jörn Engel
2012-05-11 14:43 ` Jörn Engel
2012-05-16 21:57 ` Douglas Gilbert
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=20120412213418.GG17388@logfs.org \
--to=joern@logfs.org \
--cc=JBottomley@parallels.com \
--cc=dgilbert@interlog.com \
--cc=linux-scsi@vger.kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.