Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit db59133

Browse files
YuKuai-huaweiaxboe
authored andcommitted
scsi: sg: fix blktrace debugfs entries leakage
sg_ioctl() support to enable blktrace, which will create debugfs entries "/sys/kernel/debug/block/sgx/", however, there is no guarantee that user will remove these entries through ioctl, and deleting sg device doesn't cleanup these blktrace entries. This problem can be fixed by cleanup blktrace while releasing request_queue, however, it's not a good idea to do this special handling in common layer just for sg device. Fix this problem by shutdown bltkrace in sg_device_destroy(), where the device is deleted and all the users close the device, also grab a scsi_device reference from sg_add_device() to prevent scsi_device to be freed before sg_device_destroy(); Signed-off-by: Yu Kuai <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent cbe7cff commit db59133

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

drivers/scsi/sg.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ sg_add_device(struct device *cl_dev)
14971497
int error;
14981498
unsigned long iflags;
14991499

1500+
error = scsi_device_get(scsidp);
1501+
if (error)
1502+
return error;
1503+
15001504
error = -ENOMEM;
15011505
cdev = cdev_alloc();
15021506
if (!cdev) {
@@ -1554,20 +1558,25 @@ sg_add_device(struct device *cl_dev)
15541558
out:
15551559
if (cdev)
15561560
cdev_del(cdev);
1561+
scsi_device_put(scsidp);
15571562
return error;
15581563
}
15591564

15601565
static void
15611566
sg_device_destroy(struct kref *kref)
15621567
{
15631568
struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
1569+
struct request_queue *q = sdp->device->request_queue;
15641570
unsigned long flags;
15651571

15661572
/* CAUTION! Note that the device can still be found via idr_find()
15671573
* even though the refcount is 0. Therefore, do idr_remove() BEFORE
15681574
* any other cleanup.
15691575
*/
15701576

1577+
blk_trace_remove(q);
1578+
scsi_device_put(sdp->device);
1579+
15711580
write_lock_irqsave(&sg_index_lock, flags);
15721581
idr_remove(&sg_index_idr, sdp->index);
15731582
write_unlock_irqrestore(&sg_index_lock, flags);

0 commit comments

Comments
 (0)