From 3dc775e8855999415011b6498658cb55cb14fa2c Mon Sep 17 00:00:00 2001 From: Jiri Slaby Date: Apr 17 2024 11:31:08 +0000 Subject: scsi: sg: Avoid race in error handling & drop bogus warn (bsc#1012628). --- diff --git a/patches.kernel.org/6.8.7-117-scsi-sg-Avoid-race-in-error-handling-drop-bogus.patch b/patches.kernel.org/6.8.7-117-scsi-sg-Avoid-race-in-error-handling-drop-bogus.patch new file mode 100644 index 0000000..11baaf6 --- /dev/null +++ b/patches.kernel.org/6.8.7-117-scsi-sg-Avoid-race-in-error-handling-drop-bogus.patch @@ -0,0 +1,106 @@ +From: Alexander Wetzel +Date: Mon, 1 Apr 2024 21:10:38 +0200 +Subject: [PATCH] scsi: sg: Avoid race in error handling & drop bogus warn +References: bsc#1012628 +Patch-mainline: 6.8.7 +Git-commit: d4e655c49f474deffaf5ed7e65034b8167ee39c8 + +commit d4e655c49f474deffaf5ed7e65034b8167ee39c8 upstream. + +Commit 27f58c04a8f4 ("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/5375B275-D137-4D5F-BE25-6AF8ACAE41EF@linux.ibm.com +Fixes: 27f58c04a8f4 ("scsi: sg: Avoid sg device teardown race") +Cc: stable@vger.kernel.org +Signed-off-by: Alexander Wetzel +Link: https://lore.kernel.org/r/20240401191038.18359-1-Alexander@wetzel-home.de +Tested-by: Sachin Sant +Reviewed-by: Bart Van Assche +Signed-off-by: Martin K. Petersen +Signed-off-by: Greg Kroah-Hartman +Signed-off-by: Jiri Slaby +--- + drivers/scsi/sg.c | 18 ++++++++++-------- + 1 file changed, 10 insertions(+), 8 deletions(-) + +diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c +index ff6894ce..b2d02dac 100644 +--- a/drivers/scsi/sg.c ++++ b/drivers/scsi/sg.c +@@ -285,6 +285,7 @@ sg_open(struct inode *inode, struct file *filp) + int dev = iminor(inode); + int flags = filp->f_flags; + struct request_queue *q; ++ struct scsi_device *device; + Sg_device *sdp; + Sg_fd *sfp; + int retval; +@@ -301,11 +302,12 @@ sg_open(struct inode *inode, struct file *filp) + + /* This driver's module count bumped by fops_get in */ + /* Prevent the device driver from vanishing while we sleep */ +- retval = scsi_device_get(sdp->device); ++ device = sdp->device; ++ retval = scsi_device_get(device); + if (retval) + goto sg_put; + +- retval = scsi_autopm_get_device(sdp->device); ++ retval = scsi_autopm_get_device(device); + if (retval) + goto sdp_put; + +@@ -313,7 +315,7 @@ sg_open(struct inode *inode, struct file *filp) + * check if O_NONBLOCK. Permits SCSI commands to be issued + * during error recovery. Tread carefully. */ + if (!((flags & O_NONBLOCK) || +- scsi_block_when_processing_errors(sdp->device))) { ++ scsi_block_when_processing_errors(device))) { + retval = -ENXIO; + /* we are in error recovery for this device */ + goto error_out; +@@ -344,7 +346,7 @@ sg_open(struct inode *inode, struct file *filp) + + if (sdp->open_cnt < 1) { /* no existing opens */ + sdp->sgdebug = 0; +- q = sdp->device->request_queue; ++ q = device->request_queue; + sdp->sg_tablesize = queue_max_segments(q); + } + sfp = sg_add_sfp(sdp); +@@ -370,10 +372,11 @@ sg_open(struct inode *inode, struct file *filp) + error_mutex_locked: + mutex_unlock(&sdp->open_rel_lock); + error_out: +- scsi_autopm_put_device(sdp->device); ++ scsi_autopm_put_device(device); + sdp_put: +- scsi_device_put(sdp->device); +- goto sg_put; ++ kref_put(&sdp->d_ref, sg_device_destroy); ++ scsi_device_put(device); ++ return retval; + } + + /* Release resources associated with a successful sg_open() +@@ -2233,7 +2236,6 @@ sg_remove_sfp_usercontext(struct work_struct *work) + "sg_remove_sfp: sfp=0x%p\n", sfp)); + kfree(sfp); + +- WARN_ON_ONCE(kref_read(&sdp->d_ref) != 1); + kref_put(&sdp->d_ref, sg_device_destroy); + scsi_device_put(device); + module_put(THIS_MODULE); +-- +2.44.0 + diff --git a/series.conf b/series.conf index bd66fca..2b810e8 100644 --- a/series.conf +++ b/series.conf @@ -1690,6 +1690,7 @@ patches.kernel.org/6.8.7-114-fs-proc-remove-redundant-comments-from-proc-boo.patch patches.kernel.org/6.8.7-115-fs-proc-Skip-bootloader-comment-if-no-embedded-.patch patches.kernel.org/6.8.7-116-scsi-sg-Avoid-sg-device-teardown-race.patch + patches.kernel.org/6.8.7-117-scsi-sg-Avoid-race-in-error-handling-drop-bogus.patch ######################################################## # Build fixes that apply to the vanilla kernel too.