public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] fix blktrace debugfs entries leakage
@ 2023-06-08  2:41 Yu Kuai
  2023-06-08  2:41 ` [PATCH v3 1/2] scsi: sg: " Yu Kuai
  2023-06-08  2:41 ` [PATCH v3 2/2] block: " Yu Kuai
  0 siblings, 2 replies; 7+ messages in thread
From: Yu Kuai @ 2023-06-08  2:41 UTC (permalink / raw)
  To: hch, axboe, dgilbert, jejb, martin.petersen
  Cc: linux-block, linux-kernel, linux-scsi, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Yu Kuai <yukuai3@huawei.com>

Changes in v3:
 - add a new patch to handle /dev/sg

Changes in v2:
 - cleanup bltkrace in disk_release() instead of blk_free_queue()

Yu Kuai (2):
  scsi: sg: fix blktrace debugfs entries leakage
  block: fix blktrace debugfs entries leakage

 block/genhd.c     | 5 ++++-
 drivers/scsi/sg.c | 8 ++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 1/2] scsi: sg: fix blktrace debugfs entries leakage
  2023-06-08  2:41 [PATCH v3 0/2] fix blktrace debugfs entries leakage Yu Kuai
@ 2023-06-08  2:41 ` Yu Kuai
  2023-06-08  6:12   ` Christoph Hellwig
  2023-06-08 16:02   ` kernel test robot
  2023-06-08  2:41 ` [PATCH v3 2/2] block: " Yu Kuai
  1 sibling, 2 replies; 7+ messages in thread
From: Yu Kuai @ 2023-06-08  2:41 UTC (permalink / raw)
  To: hch, axboe, dgilbert, jejb, martin.petersen
  Cc: linux-block, linux-kernel, linux-scsi, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Yu Kuai <yukuai3@huawei.com>

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 <yukuai3@huawei.com>
---
 drivers/scsi/sg.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 037f8c98a6d3..ed4e2f9b3d64 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -1496,6 +1496,10 @@ sg_add_device(struct device *cl_dev)
 	int error;
 	unsigned long iflags;
 
+	error = scsi_device_get(scsidp);
+	if (error)
+		return error;
+
 	error = -ENOMEM;
 	cdev = cdev_alloc();
 	if (!cdev) {
@@ -1553,6 +1557,7 @@ sg_add_device(struct device *cl_dev)
 out:
 	if (cdev)
 		cdev_del(cdev);
+	scsi_device_put(scsidp);
 	return error;
 }
 
@@ -1567,6 +1572,9 @@ sg_device_destroy(struct kref *kref)
 	 * any other cleanup.
 	 */
 
+	blk_trace_remove(sdp->device->request_queue);
+	scsi_device_put(sdp->device);
+
 	write_lock_irqsave(&sg_index_lock, flags);
 	idr_remove(&sg_index_idr, sdp->index);
 	write_unlock_irqrestore(&sg_index_lock, flags);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 2/2] block: fix blktrace debugfs entries leakage
  2023-06-08  2:41 [PATCH v3 0/2] fix blktrace debugfs entries leakage Yu Kuai
  2023-06-08  2:41 ` [PATCH v3 1/2] scsi: sg: " Yu Kuai
@ 2023-06-08  2:41 ` Yu Kuai
  2023-06-08  6:13   ` Christoph Hellwig
  1 sibling, 1 reply; 7+ messages in thread
From: Yu Kuai @ 2023-06-08  2:41 UTC (permalink / raw)
  To: hch, axboe, dgilbert, jejb, martin.petersen
  Cc: linux-block, linux-kernel, linux-scsi, yukuai3, yukuai1, yi.zhang,
	yangerkun

From: Yu Kuai <yukuai3@huawei.com>

Commit 99d055b4fd4b ("block: remove per-disk debugfs files in
blk_unregister_queue") moves blk_trace_shutdown() from
blk_release_queue() to blk_unregister_queue(), this is safe if blktrace
is created through sysfs, however, there is a regression in corner
case.

blktrace can still be enabled after del_gendisk() through ioctl if
the disk is opened before del_gendisk(), and if blktrace is not shutdown
through ioctl before closing the disk, debugfs entries will be leaked.

Fix this problem by shutdown blktrace in disk_release(), this is safe
because blk_trace_remove() is reentrant.

Fixes: 99d055b4fd4b ("block: remove per-disk debugfs files in blk_unregister_queue")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 block/genhd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/block/genhd.c b/block/genhd.c
index 3537b7d7c484..134cb6eb8e91 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -25,8 +25,9 @@
 #include <linux/pm_runtime.h>
 #include <linux/badblocks.h>
 #include <linux/part_stat.h>
-#include "blk-throttle.h"
+#include <linux/blktrace_api.h>
 
+#include "blk-throttle.h"
 #include "blk.h"
 #include "blk-mq-sched.h"
 #include "blk-rq-qos.h"
@@ -1171,6 +1172,8 @@ static void disk_release(struct device *dev)
 	might_sleep();
 	WARN_ON_ONCE(disk_live(disk));
 
+	blk_trace_remove(disk->queue);
+
 	/*
 	 * To undo the all initialization from blk_mq_init_allocated_queue in
 	 * case of a probe failure where add_disk is never called we have to
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 1/2] scsi: sg: fix blktrace debugfs entries leakage
  2023-06-08  2:41 ` [PATCH v3 1/2] scsi: sg: " Yu Kuai
@ 2023-06-08  6:12   ` Christoph Hellwig
  2023-06-08 16:02   ` kernel test robot
  1 sibling, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2023-06-08  6:12 UTC (permalink / raw)
  To: Yu Kuai
  Cc: hch, axboe, dgilbert, jejb, martin.petersen, linux-block,
	linux-kernel, linux-scsi, yukuai3, yi.zhang, yangerkun

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 2/2] block: fix blktrace debugfs entries leakage
  2023-06-08  2:41 ` [PATCH v3 2/2] block: " Yu Kuai
@ 2023-06-08  6:13   ` Christoph Hellwig
  0 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2023-06-08  6:13 UTC (permalink / raw)
  To: Yu Kuai
  Cc: hch, axboe, dgilbert, jejb, martin.petersen, linux-block,
	linux-kernel, linux-scsi, yukuai3, yi.zhang, yangerkun

Looks good (at least for now, I think the SG_IO support on /dev/sg
really needs to go away or be completely separate from the disk one).

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 1/2] scsi: sg: fix blktrace debugfs entries leakage
  2023-06-08  2:41 ` [PATCH v3 1/2] scsi: sg: " Yu Kuai
  2023-06-08  6:12   ` Christoph Hellwig
@ 2023-06-08 16:02   ` kernel test robot
  2023-06-09  1:12     ` Yu Kuai
  1 sibling, 1 reply; 7+ messages in thread
From: kernel test robot @ 2023-06-08 16:02 UTC (permalink / raw)
  To: Yu Kuai, hch, axboe, dgilbert, jejb, martin.petersen
  Cc: oe-kbuild-all, linux-block, linux-kernel, linux-scsi, yukuai3,
	yukuai1, yi.zhang, yangerkun

Hi Yu,

kernel test robot noticed the following build warnings:

[auto build test WARNING on axboe-block/for-next]
[also build test WARNING on linus/master v6.4-rc5 next-20230608]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yu-Kuai/scsi-sg-fix-blktrace-debugfs-entries-leakage/20230608-104735
base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
patch link:    https://lore.kernel.org/r/20230608024159.1282953-2-yukuai1%40huaweicloud.com
patch subject: [PATCH v3 1/2] scsi: sg: fix blktrace debugfs entries leakage
config: i386-randconfig-r002-20230608 (https://download.01.org/0day-ci/archive/20230608/202306082353.o2lpbQcL-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build):
        git remote add axboe-block https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git
        git fetch axboe-block for-next
        git checkout axboe-block/for-next
        b4 shazam https://lore.kernel.org/r/20230608024159.1282953-2-yukuai1@huaweicloud.com
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=i386 olddefconfig
        make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202306082353.o2lpbQcL-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from drivers/scsi/sg.c:45:
   drivers/scsi/sg.c: In function 'sg_device_destroy':
>> include/linux/blktrace_api.h:88:57: warning: statement with no effect [-Wunused-value]
      88 | # define blk_trace_remove(q)                            (-ENOTTY)
         |                                                         ^
   drivers/scsi/sg.c:1575:9: note: in expansion of macro 'blk_trace_remove'
    1575 |         blk_trace_remove(sdp->device->request_queue);
         |         ^~~~~~~~~~~~~~~~


vim +88 include/linux/blktrace_api.h

157f9c00e88529 Arnaldo Carvalho de Melo 2009-01-26  81  
2056a782f8e7e6 Jens Axboe               2006-03-23  82  #else /* !CONFIG_BLK_DEV_IO_TRACE */
2056a782f8e7e6 Jens Axboe               2006-03-23  83  # define blk_trace_ioctl(bdev, cmd, arg)		(-ENOTTY)
2056a782f8e7e6 Jens Axboe               2006-03-23  84  # define blk_trace_shutdown(q)				do { } while (0)
a54895fa057c67 Christoph Hellwig        2020-12-03  85  # define blk_add_driver_data(rq, data, len)		do {} while (0)
d0deef5b14af7d Shawn Du                 2009-04-14  86  # define blk_trace_setup(q, name, dev, bdev, arg)	(-ENOTTY)
6da127ad0918f9 Christof Schmitt         2008-01-11  87  # define blk_trace_startstop(q, start)			(-ENOTTY)
6da127ad0918f9 Christof Schmitt         2008-01-11 @88  # define blk_trace_remove(q)				(-ENOTTY)
9d5f09a424a67d Alan D. Brunelle         2008-05-27  89  # define blk_add_trace_msg(q, fmt, ...)			do { } while (0)
35fe6d763229e8 Shaohua Li               2017-07-12  90  # define blk_add_cgroup_trace_msg(q, cg, fmt, ...)	do { } while (0)
59fa0224cfea31 Shaohua Li               2016-05-09  91  # define blk_trace_note_message_enabled(q)		(false)
2056a782f8e7e6 Jens Axboe               2006-03-23  92  #endif /* CONFIG_BLK_DEV_IO_TRACE */
d0deef5b14af7d Shawn Du                 2009-04-14  93  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 1/2] scsi: sg: fix blktrace debugfs entries leakage
  2023-06-08 16:02   ` kernel test robot
@ 2023-06-09  1:12     ` Yu Kuai
  0 siblings, 0 replies; 7+ messages in thread
From: Yu Kuai @ 2023-06-09  1:12 UTC (permalink / raw)
  To: kernel test robot, Yu Kuai, hch, axboe, dgilbert, jejb,
	martin.petersen
  Cc: oe-kbuild-all, linux-block, linux-kernel, linux-scsi, yi.zhang,
	yangerkun, yukuai (C)

Hi,

在 2023/06/09 0:02, kernel test robot 写道:
> Hi Yu,
> 
> kernel test robot noticed the following build warnings:
> 
> [auto build test WARNING on axboe-block/for-next]
> [also build test WARNING on linus/master v6.4-rc5 next-20230608]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
> 
> url:    https://github.com/intel-lab-lkp/linux/commits/Yu-Kuai/scsi-sg-fix-blktrace-debugfs-entries-leakage/20230608-104735
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
> patch link:    https://lore.kernel.org/r/20230608024159.1282953-2-yukuai1%40huaweicloud.com
> patch subject: [PATCH v3 1/2] scsi: sg: fix blktrace debugfs entries leakage
> config: i386-randconfig-r002-20230608 (https://download.01.org/0day-ci/archive/20230608/202306082353.o2lpbQcL-lkp@intel.com/config)
> compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> reproduce (this is a W=1 build):
>          git remote add axboe-block https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git
>          git fetch axboe-block for-next
>          git checkout axboe-block/for-next
>          b4 shazam https://lore.kernel.org/r/20230608024159.1282953-2-yukuai1@huaweicloud.com
>          # save the config file
>          mkdir build_dir && cp config build_dir/.config
>          make W=1 O=build_dir ARCH=i386 olddefconfig
>          make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
> 
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202306082353.o2lpbQcL-lkp@intel.com/
> 
> All warnings (new ones prefixed by >>):
> 
>     In file included from drivers/scsi/sg.c:45:
>     drivers/scsi/sg.c: In function 'sg_device_destroy':
>>> include/linux/blktrace_api.h:88:57: warning: statement with no effect [-Wunused-value]
>        88 | # define blk_trace_remove(q)                            (-ENOTTY)
>           |        

So, this warning happens in all the caller of blk_trace_remove() when
CONFIG_BLK_DEV_IO_TRACE is disabled that doesn't handle the return
value. I'll use blk_trace_shutdown() instead to avoid this warning.
                                                  ^
Thanks,
Kuai
>     drivers/scsi/sg.c:1575:9: note: in expansion of macro 'blk_trace_remove'
>      1575 |         blk_trace_remove(sdp->device->request_queue);
>           |         ^~~~~~~~~~~~~~~~
> 
> 
> vim +88 include/linux/blktrace_api.h
> 
> 157f9c00e88529 Arnaldo Carvalho de Melo 2009-01-26  81
> 2056a782f8e7e6 Jens Axboe               2006-03-23  82  #else /* !CONFIG_BLK_DEV_IO_TRACE */
> 2056a782f8e7e6 Jens Axboe               2006-03-23  83  # define blk_trace_ioctl(bdev, cmd, arg)		(-ENOTTY)
> 2056a782f8e7e6 Jens Axboe               2006-03-23  84  # define blk_trace_shutdown(q)				do { } while (0)
> a54895fa057c67 Christoph Hellwig        2020-12-03  85  # define blk_add_driver_data(rq, data, len)		do {} while (0)
> d0deef5b14af7d Shawn Du                 2009-04-14  86  # define blk_trace_setup(q, name, dev, bdev, arg)	(-ENOTTY)
> 6da127ad0918f9 Christof Schmitt         2008-01-11  87  # define blk_trace_startstop(q, start)			(-ENOTTY)
> 6da127ad0918f9 Christof Schmitt         2008-01-11 @88  # define blk_trace_remove(q)				(-ENOTTY)
> 9d5f09a424a67d Alan D. Brunelle         2008-05-27  89  # define blk_add_trace_msg(q, fmt, ...)			do { } while (0)
> 35fe6d763229e8 Shaohua Li               2017-07-12  90  # define blk_add_cgroup_trace_msg(q, cg, fmt, ...)	do { } while (0)
> 59fa0224cfea31 Shaohua Li               2016-05-09  91  # define blk_trace_note_message_enabled(q)		(false)
> 2056a782f8e7e6 Jens Axboe               2006-03-23  92  #endif /* CONFIG_BLK_DEV_IO_TRACE */
> d0deef5b14af7d Shawn Du                 2009-04-14  93
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-06-09  1:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-08  2:41 [PATCH v3 0/2] fix blktrace debugfs entries leakage Yu Kuai
2023-06-08  2:41 ` [PATCH v3 1/2] scsi: sg: " Yu Kuai
2023-06-08  6:12   ` Christoph Hellwig
2023-06-08 16:02   ` kernel test robot
2023-06-09  1:12     ` Yu Kuai
2023-06-08  2:41 ` [PATCH v3 2/2] block: " Yu Kuai
2023-06-08  6:13   ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox