Skip to contentSkip to conversation

sg: atomize check and set sdp->exclude

PATCH New vaughan 1 participant 1 message Jun 5, 2013

Subsystems (2)
lore
Share & tools
lore

vaughan

author
#0 lore raw

PATCH 1/1 sg: atomize check and set sdp->exclude

Check and set sdp->exclude should be atomic when set in sg_open().

---
 drivers/scsi/sg.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/scsi/sg.c +16 -1
index 25b5455..0ede08f 100644
@@ -245,6 +245,21 @@ static int set_exclude(Sg_device *sdp, char val)
     return val;
 }
 
+/* Check if we can set exclude and then set, return 1 if success */
+static int try_set_exclude(Sg_device *sdp)
+{
+    unsigned long flags;
+
+    spin_lock_irqsave(&sg_open_exclusive_lock, flags);
+    if (sdp->exclude) {
+        spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
+        return 0;
+    }
+    sdp->exclude = 1;
+    spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
+    return 1;
+}
+
 static int sfds_list_empty(Sg_device *sdp)
 {
     unsigned long flags;
@@ -303,7 +318,7 @@ sg_open(struct inode *inode, struct file *filp)
             goto error_out;
         }
         res = wait_event_interruptible(sdp->o_excl_wait,
-                       ((!sfds_list_empty(sdp) || get_exclude(sdp)) ? 0 : set_exclude(sdp, 1)));
+            ((!sfds_list_empty(sdp) || get_exclude(sdp)) ? 0 : try_set_exclude(sdp)));
         if (res) {
             retval = res;    /* -ERESTARTSYS because signal hit process */
             goto error_out;
Signature · 1 line
1.7.11.7