Skip to content

Commit 56fce58

Browse files
author
Ewan D. Milne
committed
scsi: sg: Avoid sg device teardown race
JIRA: https://issues.redhat.com/browse/RHEL-35659 Upstream Status: From upstream linux mainline sg_remove_sfp_usercontext() must not use sg_device_destroy() after calling 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. The resulting NULL pointer exception will then crash the kernel. Link: https://lore.kernel.org/r/20240305150509.23896-1-Alexander@wetzel-home.de Fixes: db59133 ("scsi: sg: fix blktrace debugfs entries leakage") Cc: stable@vger.kernel.org Signed-off-by: Alexander Wetzel <Alexander@wetzel-home.de> Link: https://lore.kernel.org/r/20240320213032.18221-1-Alexander@wetzel-home.de Reviewed-by: Bart Van Assche <bvanassche@acm.org> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com> (cherry picked from commit 27f58c0) Signed-off-by: Ewan D. Milne <emilne@redhat.com>
1 parent 3681c87 commit 56fce58

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

drivers/scsi/sg.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,7 @@ sg_remove_sfp_usercontext(struct work_struct *work)
21852185
{
21862186
struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
21872187
struct sg_device *sdp = sfp->parentdp;
2188+
struct scsi_device *device = sdp->device;
21882189
Sg_request *srp;
21892190
unsigned long iflags;
21902191

@@ -2210,8 +2211,9 @@ sg_remove_sfp_usercontext(struct work_struct *work)
22102211
"sg_remove_sfp: sfp=0x%p\n", sfp));
22112212
kfree(sfp);
22122213

2213-
scsi_device_put(sdp->device);
2214+
WARN_ON_ONCE(kref_read(&sdp->d_ref) != 1);
22142215
kref_put(&sdp->d_ref, sg_device_destroy);
2216+
scsi_device_put(device);
22152217
module_put(THIS_MODULE);
22162218
}
22172219

0 commit comments

Comments
 (0)