* [PATCH v2 0/3] fix possible memleak in '__blk_trace_remove'
@ 2022-10-18 14:51 Ye Bin
2022-10-18 14:51 ` [PATCH v2 1/3] blktrace: introduce 'blk_trace_{start,stop}' helper Ye Bin
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Ye Bin @ 2022-10-18 14:51 UTC (permalink / raw)
To: axboe, rostedt, mhiramat, linux-block; +Cc: linux-kernel, yebin
diff v2 VS v1:
1. Introduce 'blk_trace_{start,stop}' helper instead of 'blk_trace_switch_state'.
2. Move stop block trace from '__blk_trace_remove' to 'blk_trace_cleanup'.
Ye Bin (3):
blktrace: introduce 'blk_trace_{start,stop}' helper
blktrace: fix possible memleak in '__blk_trace_remove'
blktrace: remove unnessary stop block trace in 'blk_trace_shutdown'
kernel/trace/blktrace.c | 90 +++++++++++++++++++++--------------------
1 file changed, 47 insertions(+), 43 deletions(-)
--
2.31.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/3] blktrace: introduce 'blk_trace_{start,stop}' helper
2022-10-18 14:51 [PATCH v2 0/3] fix possible memleak in '__blk_trace_remove' Ye Bin
@ 2022-10-18 14:51 ` Ye Bin
2022-10-18 15:52 ` Christoph Hellwig
2022-10-18 14:51 ` [PATCH v2 2/3] blktrace: fix possible memleak in '__blk_trace_remove' Ye Bin
2022-10-18 14:51 ` [PATCH v2 3/3] blktrace: remove unnessary stop block trace in 'blk_trace_shutdown' Ye Bin
2 siblings, 1 reply; 7+ messages in thread
From: Ye Bin @ 2022-10-18 14:51 UTC (permalink / raw)
To: axboe, rostedt, mhiramat, linux-block; +Cc: linux-kernel, yebin
Introduce 'blk_trace_{start,stop}' helper. No functional changed.
Signed-off-by: Ye Bin <yebin@huaweicloud.com>
---
kernel/trace/blktrace.c | 82 ++++++++++++++++++++++-------------------
1 file changed, 44 insertions(+), 38 deletions(-)
diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index 7f5eb295fe19..f07a03c1e052 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -346,6 +346,45 @@ static void put_probe_ref(void)
mutex_unlock(&blk_probe_mutex);
}
+static int blk_trace_start(struct blk_trace *bt)
+{
+ /*
+ * For starting a trace, we can transition from a setup or stopped
+ * trace.
+ */
+ if (bt->trace_state == Blktrace_setup ||
+ bt->trace_state == Blktrace_stopped) {
+ blktrace_seq++;
+ smp_mb();
+ bt->trace_state = Blktrace_running;
+ raw_spin_lock_irq(&running_trace_lock);
+ list_add(&bt->running_list, &running_trace_list);
+ raw_spin_unlock_irq(&running_trace_lock);
+
+ trace_note_time(bt);
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
+static int blk_trace_stop(struct blk_trace *bt)
+{
+ /*
+ * For stopping a trace, the state must be running
+ */
+ if (bt->trace_state == Blktrace_running) {
+ bt->trace_state = Blktrace_stopped;
+ raw_spin_lock_irq(&running_trace_lock);
+ list_del_init(&bt->running_list);
+ raw_spin_unlock_irq(&running_trace_lock);
+ relay_flush(bt->rchan);
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
static void blk_trace_cleanup(struct request_queue *q, struct blk_trace *bt)
{
synchronize_rcu();
@@ -658,7 +697,6 @@ static int compat_blk_trace_setup(struct request_queue *q, char *name,
static int __blk_trace_startstop(struct request_queue *q, int start)
{
- int ret;
struct blk_trace *bt;
bt = rcu_dereference_protected(q->blk_trace,
@@ -666,36 +704,10 @@ static int __blk_trace_startstop(struct request_queue *q, int start)
if (bt == NULL)
return -EINVAL;
- /*
- * For starting a trace, we can transition from a setup or stopped
- * trace. For stopping a trace, the state must be running
- */
- ret = -EINVAL;
- if (start) {
- if (bt->trace_state == Blktrace_setup ||
- bt->trace_state == Blktrace_stopped) {
- blktrace_seq++;
- smp_mb();
- bt->trace_state = Blktrace_running;
- raw_spin_lock_irq(&running_trace_lock);
- list_add(&bt->running_list, &running_trace_list);
- raw_spin_unlock_irq(&running_trace_lock);
-
- trace_note_time(bt);
- ret = 0;
- }
- } else {
- if (bt->trace_state == Blktrace_running) {
- bt->trace_state = Blktrace_stopped;
- raw_spin_lock_irq(&running_trace_lock);
- list_del_init(&bt->running_list);
- raw_spin_unlock_irq(&running_trace_lock);
- relay_flush(bt->rchan);
- ret = 0;
- }
- }
-
- return ret;
+ if (start)
+ return blk_trace_start(bt);
+ else
+ return blk_trace_stop(bt);
}
int blk_trace_startstop(struct request_queue *q, int start)
@@ -1614,13 +1626,7 @@ static int blk_trace_remove_queue(struct request_queue *q)
if (bt == NULL)
return -EINVAL;
- if (bt->trace_state == Blktrace_running) {
- bt->trace_state = Blktrace_stopped;
- raw_spin_lock_irq(&running_trace_lock);
- list_del_init(&bt->running_list);
- raw_spin_unlock_irq(&running_trace_lock);
- relay_flush(bt->rchan);
- }
+ blk_trace_stop(bt);
put_probe_ref();
synchronize_rcu();
--
2.31.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/3] blktrace: fix possible memleak in '__blk_trace_remove'
2022-10-18 14:51 [PATCH v2 0/3] fix possible memleak in '__blk_trace_remove' Ye Bin
2022-10-18 14:51 ` [PATCH v2 1/3] blktrace: introduce 'blk_trace_{start,stop}' helper Ye Bin
@ 2022-10-18 14:51 ` Ye Bin
2022-10-18 15:52 ` Christoph Hellwig
2022-10-18 14:51 ` [PATCH v2 3/3] blktrace: remove unnessary stop block trace in 'blk_trace_shutdown' Ye Bin
2 siblings, 1 reply; 7+ messages in thread
From: Ye Bin @ 2022-10-18 14:51 UTC (permalink / raw)
To: axboe, rostedt, mhiramat, linux-block; +Cc: linux-kernel, yebin
When test as follows:
step1: ioctl(sda, BLKTRACESETUP, &arg)
step2: ioctl(sda, BLKTRACESTART, NULL)
step3: ioctl(sda, BLKTRACETEARDOWN, NULL)
step4: ioctl(sda, BLKTRACESETUP, &arg)
Got issue as follows:
debugfs: File 'dropped' in directory 'sda' already present!
debugfs: File 'msg' in directory 'sda' already present!
debugfs: File 'trace0' in directory 'sda' already present!
And also find syzkaller report issue like "KASAN: use-after-free Read in relay_switch_subbuf"
"https://syzkaller.appspot.com/bug?id=13849f0d9b1b818b087341691be6cc3ac6a6bfb7"
If remove block trace without stop(BLKTRACESTOP) block trace, '__blk_trace_remove'
will just set 'q->blk_trace' with NULL. However, debugfs file isn't removed, so
will report file already present when call BLKTRACESETUP.
static int __blk_trace_remove(struct request_queue *q)
{
struct blk_trace *bt;
bt = rcu_replace_pointer(q->blk_trace, NULL,
lockdep_is_held(&q->debugfs_mutex));
if (!bt)
return -EINVAL;
if (bt->trace_state != Blktrace_running)
blk_trace_cleanup(q, bt);
return 0;
}
If do test as follows:
step1: ioctl(sda, BLKTRACESETUP, &arg)
step2: ioctl(sda, BLKTRACESTART, NULL)
step3: ioctl(sda, BLKTRACETEARDOWN, NULL)
step4: remove sda
There will remove debugfs directory which will remove recursively all file
under directory.
>> blk_release_queue
>> debugfs_remove_recursive(q->debugfs_dir)
So all files which created in 'do_blk_trace_setup' are removed, and
'dentry->d_inode' is NULL. But 'q->blk_trace' is still in 'running_trace_lock',
'trace_note_tsk' will traverse 'running_trace_lock' all nodes.
>>trace_note_tsk
>> trace_note
>> relay_reserve
>> relay_switch_subbuf
>> d_inode(buf->dentry)->i_size
To solve above issues, reference commit '5afedf670caf', call 'blk_trace_cleanup'
unconditionally in '__blk_trace_remove' and first stop block trace in
'blk_trace_cleanup'.
Signed-off-by: Ye Bin <yebin@huaweicloud.com>
---
kernel/trace/blktrace.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index f07a03c1e052..030e5716661e 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -387,6 +387,7 @@ static int blk_trace_stop(struct blk_trace *bt)
static void blk_trace_cleanup(struct request_queue *q, struct blk_trace *bt)
{
+ blk_trace_stop(bt);
synchronize_rcu();
blk_trace_free(q, bt);
put_probe_ref();
@@ -401,8 +402,7 @@ static int __blk_trace_remove(struct request_queue *q)
if (!bt)
return -EINVAL;
- if (bt->trace_state != Blktrace_running)
- blk_trace_cleanup(q, bt);
+ blk_trace_cleanup(q, bt);
return 0;
}
--
2.31.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/3] blktrace: remove unnessary stop block trace in 'blk_trace_shutdown'
2022-10-18 14:51 [PATCH v2 0/3] fix possible memleak in '__blk_trace_remove' Ye Bin
2022-10-18 14:51 ` [PATCH v2 1/3] blktrace: introduce 'blk_trace_{start,stop}' helper Ye Bin
2022-10-18 14:51 ` [PATCH v2 2/3] blktrace: fix possible memleak in '__blk_trace_remove' Ye Bin
@ 2022-10-18 14:51 ` Ye Bin
2022-10-18 15:53 ` Christoph Hellwig
2 siblings, 1 reply; 7+ messages in thread
From: Ye Bin @ 2022-10-18 14:51 UTC (permalink / raw)
To: axboe, rostedt, mhiramat, linux-block; +Cc: linux-kernel, yebin
As previous commit, 'blk_trace_cleanup' will stop block trace if
block trace's state is 'Blktrace_running'.
So remove unnessary stop block trace in 'blk_trace_shutdown'.
Signed-off-by: Ye Bin <yebin@huaweicloud.com>
---
kernel/trace/blktrace.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index 030e5716661e..2be8c747f796 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -784,10 +784,8 @@ int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
void blk_trace_shutdown(struct request_queue *q)
{
if (rcu_dereference_protected(q->blk_trace,
- lockdep_is_held(&q->debugfs_mutex))) {
- __blk_trace_startstop(q, 0);
+ lockdep_is_held(&q->debugfs_mutex)))
__blk_trace_remove(q);
- }
}
#ifdef CONFIG_BLK_CGROUP
--
2.31.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/3] blktrace: introduce 'blk_trace_{start,stop}' helper
2022-10-18 14:51 ` [PATCH v2 1/3] blktrace: introduce 'blk_trace_{start,stop}' helper Ye Bin
@ 2022-10-18 15:52 ` Christoph Hellwig
0 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2022-10-18 15:52 UTC (permalink / raw)
To: Ye Bin; +Cc: axboe, rostedt, mhiramat, linux-block, linux-kernel
> +static int blk_trace_start(struct blk_trace *bt)
> +{
> + /*
> + * For starting a trace, we can transition from a setup or stopped
> + * trace.
> + */
That's pretty obvious from the check, isn't it?
> + if (bt->trace_state == Blktrace_setup ||
> + bt->trace_state == Blktrace_stopped) {
I'd invert the check and return early from the function for the error
case so that the real starting code is in the main path.
> +static int blk_trace_stop(struct blk_trace *bt)
> +{
> + /*
> + * For stopping a trace, the state must be running
> + */
> + if (bt->trace_state == Blktrace_running) {
The same two comments apply here as well.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/3] blktrace: fix possible memleak in '__blk_trace_remove'
2022-10-18 14:51 ` [PATCH v2 2/3] blktrace: fix possible memleak in '__blk_trace_remove' Ye Bin
@ 2022-10-18 15:52 ` Christoph Hellwig
0 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2022-10-18 15:52 UTC (permalink / raw)
To: Ye Bin; +Cc: axboe, rostedt, mhiramat, linux-block, linux-kernel
Looks good:
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 3/3] blktrace: remove unnessary stop block trace in 'blk_trace_shutdown'
2022-10-18 14:51 ` [PATCH v2 3/3] blktrace: remove unnessary stop block trace in 'blk_trace_shutdown' Ye Bin
@ 2022-10-18 15:53 ` Christoph Hellwig
0 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2022-10-18 15:53 UTC (permalink / raw)
To: Ye Bin; +Cc: axboe, rostedt, mhiramat, linux-block, linux-kernel
Looks good:
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-10-18 15:53 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-18 14:51 [PATCH v2 0/3] fix possible memleak in '__blk_trace_remove' Ye Bin
2022-10-18 14:51 ` [PATCH v2 1/3] blktrace: introduce 'blk_trace_{start,stop}' helper Ye Bin
2022-10-18 15:52 ` Christoph Hellwig
2022-10-18 14:51 ` [PATCH v2 2/3] blktrace: fix possible memleak in '__blk_trace_remove' Ye Bin
2022-10-18 15:52 ` Christoph Hellwig
2022-10-18 14:51 ` [PATCH v2 3/3] blktrace: remove unnessary stop block trace in 'blk_trace_shutdown' Ye Bin
2022-10-18 15:53 ` Christoph Hellwig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).