From nobody Tue Apr 7 12:41:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D126C433FE for ; Mon, 17 Oct 2022 06:31:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230033AbiJQGb0 (ORCPT ); Mon, 17 Oct 2022 02:31:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230048AbiJQGbV (ORCPT ); Mon, 17 Oct 2022 02:31:21 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52A3C5724F; Sun, 16 Oct 2022 23:31:19 -0700 (PDT) Received: from canpemm500010.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MrRph3HC9z1P7fB; Mon, 17 Oct 2022 14:26:36 +0800 (CST) Received: from huawei.com (10.175.127.227) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 17 Oct 2022 14:31:16 +0800 From: Ye Bin To: , , , CC: , Ye Bin Subject: [PATCH -next 1/3] blktrace: introduce 'blk_trace_swicth_state' helper Date: Mon, 17 Oct 2022 14:53:19 +0800 Message-ID: <20221017065321.2846017-2-yebin10@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221017065321.2846017-1-yebin10@huawei.com> References: <20221017065321.2846017-1-yebin10@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500010.china.huawei.com (7.192.105.118) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce 'blk_trace_swicth_state' helper. No functional changed. Signed-off-by: Ye Bin --- kernel/trace/blktrace.c | 74 ++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c index 7f5eb295fe19..edd83e213580 100644 --- a/kernel/trace/blktrace.c +++ b/kernel/trace/blktrace.c @@ -353,6 +353,39 @@ static void blk_trace_cleanup(struct request_queue *q,= struct blk_trace *bt) put_probe_ref(); } =20 +static int blk_trace_switch_state(struct blk_trace *bt, int start) +{ + /* + * For starting a trace, we can transition from a setup or stopped + * trace. For stopping a trace, the state must be running + */ + if (start) { + if (bt->trace_state =3D=3D Blktrace_setup || + bt->trace_state =3D=3D Blktrace_stopped) { + blktrace_seq++; + smp_mb(); + bt->trace_state =3D 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; + } + } else { + if (bt->trace_state =3D=3D Blktrace_running) { + bt->trace_state =3D 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 int __blk_trace_remove(struct request_queue *q) { struct blk_trace *bt; @@ -658,7 +691,6 @@ static int compat_blk_trace_setup(struct request_queue = *q, char *name, =20 static int __blk_trace_startstop(struct request_queue *q, int start) { - int ret; struct blk_trace *bt; =20 bt =3D rcu_dereference_protected(q->blk_trace, @@ -666,36 +698,7 @@ static int __blk_trace_startstop(struct request_queue = *q, int start) if (bt =3D=3D NULL) return -EINVAL; =20 - /* - * For starting a trace, we can transition from a setup or stopped - * trace. For stopping a trace, the state must be running - */ - ret =3D -EINVAL; - if (start) { - if (bt->trace_state =3D=3D Blktrace_setup || - bt->trace_state =3D=3D Blktrace_stopped) { - blktrace_seq++; - smp_mb(); - bt->trace_state =3D 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 =3D 0; - } - } else { - if (bt->trace_state =3D=3D Blktrace_running) { - bt->trace_state =3D 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 =3D 0; - } - } - - return ret; + return blk_trace_switch_state(bt, start); } =20 int blk_trace_startstop(struct request_queue *q, int start) @@ -1614,13 +1617,8 @@ static int blk_trace_remove_queue(struct request_que= ue *q) if (bt =3D=3D NULL) return -EINVAL; =20 - if (bt->trace_state =3D=3D Blktrace_running) { - bt->trace_state =3D 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); - } + if (bt->trace_state =3D=3D Blktrace_running) + blk_trace_switch_state(bt, 0); =20 put_probe_ref(); synchronize_rcu(); --=20 2.31.1 From nobody Tue Apr 7 12:41:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50A94C4332F for ; Mon, 17 Oct 2022 06:31:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230179AbiJQGbg (ORCPT ); Mon, 17 Oct 2022 02:31:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230080AbiJQGbV (ORCPT ); Mon, 17 Oct 2022 02:31:21 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BA935724C; Sun, 16 Oct 2022 23:31:19 -0700 (PDT) Received: from canpemm500010.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MrRrK3l3lzpWGt; Mon, 17 Oct 2022 14:28:01 +0800 (CST) Received: from huawei.com (10.175.127.227) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 17 Oct 2022 14:31:17 +0800 From: Ye Bin To: , , , CC: , Ye Bin Subject: [PATCH -next 2/3] blktrace: fix possible memleak in '__blk_trace_remove' Date: Mon, 17 Oct 2022 14:53:20 +0800 Message-ID: <20221017065321.2846017-3-yebin10@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221017065321.2846017-1-yebin10@huawei.com> References: <20221017065321.2846017-1-yebin10@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500010.china.huawei.com (7.192.105.118) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 re= lay_switch_subbuf" "https://syzkaller.appspot.com/bug?id=3D13849f0d9b1b818b087341691be6cc3ac6a= 6bfb7" 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 =3D rcu_replace_pointer(q->blk_trace, NULL, lockdep_is_held(&q->debugfs_mutex)); if (!bt) return -EINVAL; if (bt->trace_state !=3D 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_lo= ck', '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', first stop block tr= ace when block trace state is 'Blktrace_running' in '__blk_trace_remove'. Signed-off-by: Ye Bin --- kernel/trace/blktrace.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c index edd83e213580..0d93a0110ab5 100644 --- a/kernel/trace/blktrace.c +++ b/kernel/trace/blktrace.c @@ -395,8 +395,10 @@ static int __blk_trace_remove(struct request_queue *q) if (!bt) return -EINVAL; =20 - if (bt->trace_state !=3D Blktrace_running) - blk_trace_cleanup(q, bt); + if (bt->trace_state =3D=3D Blktrace_running) + blk_trace_switch_state(bt, 0); + + blk_trace_cleanup(q, bt); =20 return 0; } --=20 2.31.1 From nobody Tue Apr 7 12:41:59 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0867BC433FE for ; Mon, 17 Oct 2022 06:31:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230173AbiJQGbd (ORCPT ); Mon, 17 Oct 2022 02:31:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230103AbiJQGbV (ORCPT ); Mon, 17 Oct 2022 02:31:21 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBE3756BA0; Sun, 16 Oct 2022 23:31:19 -0700 (PDT) Received: from canpemm500010.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MrRrK6sLtzpWH0; Mon, 17 Oct 2022 14:28:01 +0800 (CST) Received: from huawei.com (10.175.127.227) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 17 Oct 2022 14:31:17 +0800 From: Ye Bin To: , , , CC: , Ye Bin Subject: [PATCH -next 3/3] blktrace: remove unnessary stop block trace in 'blk_trace_shutdown' Date: Mon, 17 Oct 2022 14:53:21 +0800 Message-ID: <20221017065321.2846017-4-yebin10@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221017065321.2846017-1-yebin10@huawei.com> References: <20221017065321.2846017-1-yebin10@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500010.china.huawei.com (7.192.105.118) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" As previous commit, '__blk_trace_remove' 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 --- 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 0d93a0110ab5..f33ec0f5750a 100644 --- a/kernel/trace/blktrace.c +++ b/kernel/trace/blktrace.c @@ -777,10 +777,8 @@ int blk_trace_ioctl(struct block_device *bdev, unsigne= d 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); - } } =20 #ifdef CONFIG_BLK_CGROUP --=20 2.31.1