Skip to content

Commit d4e655c

Browse files
alexw65500martinkpetersen
authored andcommitted
scsi: sg: Avoid race in error handling & drop bogus warn
Commit 27f58c0 ("scsi: sg: Avoid sg device teardown race") introduced an incorrect WARN_ON_ONCE() and missed a sequence where sg_device_destroy() was used after scsi_device_put(). sg_device_destroy() is accessing the parent scsi_device request_queue which will already be set to NULL when the preceding call to scsi_device_put() removed the last reference to the parent scsi_device. Drop the incorrect WARN_ON_ONCE() - allowing more than one concurrent access to the sg device - and make sure sg_device_destroy() is not used after scsi_device_put() in the error handling. Link: https://lore.kernel.org/all/[email protected] Fixes: 27f58c0 ("scsi: sg: Avoid sg device teardown race") Cc: [email protected] Signed-off-by: Alexander Wetzel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Tested-by: Sachin Sant <[email protected]> Reviewed-by: Bart Van Assche <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 2a26a11 commit d4e655c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

drivers/scsi/sg.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ sg_open(struct inode *inode, struct file *filp)
285285
int dev = iminor(inode);
286286
int flags = filp->f_flags;
287287
struct request_queue *q;
288+
struct scsi_device *device;
288289
Sg_device *sdp;
289290
Sg_fd *sfp;
290291
int retval;
@@ -301,19 +302,20 @@ sg_open(struct inode *inode, struct file *filp)
301302

302303
/* This driver's module count bumped by fops_get in <linux/fs.h> */
303304
/* Prevent the device driver from vanishing while we sleep */
304-
retval = scsi_device_get(sdp->device);
305+
device = sdp->device;
306+
retval = scsi_device_get(device);
305307
if (retval)
306308
goto sg_put;
307309

308-
retval = scsi_autopm_get_device(sdp->device);
310+
retval = scsi_autopm_get_device(device);
309311
if (retval)
310312
goto sdp_put;
311313

312314
/* scsi_block_when_processing_errors() may block so bypass
313315
* check if O_NONBLOCK. Permits SCSI commands to be issued
314316
* during error recovery. Tread carefully. */
315317
if (!((flags & O_NONBLOCK) ||
316-
scsi_block_when_processing_errors(sdp->device))) {
318+
scsi_block_when_processing_errors(device))) {
317319
retval = -ENXIO;
318320
/* we are in error recovery for this device */
319321
goto error_out;
@@ -344,7 +346,7 @@ sg_open(struct inode *inode, struct file *filp)
344346

345347
if (sdp->open_cnt < 1) { /* no existing opens */
346348
sdp->sgdebug = 0;
347-
q = sdp->device->request_queue;
349+
q = device->request_queue;
348350
sdp->sg_tablesize = queue_max_segments(q);
349351
}
350352
sfp = sg_add_sfp(sdp);
@@ -370,10 +372,11 @@ sg_open(struct inode *inode, struct file *filp)
370372
error_mutex_locked:
371373
mutex_unlock(&sdp->open_rel_lock);
372374
error_out:
373-
scsi_autopm_put_device(sdp->device);
375+
scsi_autopm_put_device(device);
374376
sdp_put:
375-
scsi_device_put(sdp->device);
376-
goto sg_put;
377+
kref_put(&sdp->d_ref, sg_device_destroy);
378+
scsi_device_put(device);
379+
return retval;
377380
}
378381

379382
/* Release resources associated with a successful sg_open()
@@ -2233,7 +2236,6 @@ sg_remove_sfp_usercontext(struct work_struct *work)
22332236
"sg_remove_sfp: sfp=0x%p\n", sfp));
22342237
kfree(sfp);
22352238

2236-
WARN_ON_ONCE(kref_read(&sdp->d_ref) != 1);
22372239
kref_put(&sdp->d_ref, sg_device_destroy);
22382240
scsi_device_put(device);
22392241
module_put(THIS_MODULE);

0 commit comments

Comments
 (0)