From mboxrd@z Thu Jan 1 00:00:00 1970 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 lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8FE69C433EF for ; Wed, 27 Apr 2022 12:51:08 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.94.2) (envelope-from ) id 1njh8L-0001hn-Ew; Wed, 27 Apr 2022 12:51:06 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1njh8K-0001hh-8c for linux-f2fs-devel@lists.sourceforge.net; Wed, 27 Apr 2022 12:51:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=zNp0CtI/f4lUOFReW26HAgTiMRpavPn9jORpk8+aDMs=; b=ODgRjZHnY8WF6iHPb6xBkGMF4a RCnmL8gyIeyrTsnUzrOB+AEvfrFriOPKuno0sVmo1QJwDNNLVMqvk3YFyFkkIF0JJyZTbimAymDsr 4hl8r7btPFpRx4oGiYXY8/wVEaLFw4HC9oKbnfpQ2cewciEhiHpE391tayGrRwB9jeU4=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=zNp0CtI/f4lUOFReW26HAgTiMRpavPn9jORpk8+aDMs=; b=S qsFiUwwl/AIKQgexEbZRTTqopLDXtiobr7t+Qe0EvwZyxr0VqV2q3o5/vZkVg9PTkUOEABfgxE//4 /Q8OfC5oYIOlQeL329WX9D9zxQQE49UFT7ouYeX3fDnQbqgNHA4gmCATS6VydVea6we4vTQ5g8a5d 4FaWwJGIL7X3vHHg=; Received: from dfw.source.kernel.org ([139.178.84.217]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.94.2) id 1njh8H-0003sO-0a for linux-f2fs-devel@lists.sourceforge.net; Wed, 27 Apr 2022 12:51:05 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CF07F61B57; Wed, 27 Apr 2022 12:50:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E0D2C385A9; Wed, 27 Apr 2022 12:50:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651063850; bh=l6Uv1aghVwRUkGWE9Fea1FBCo3Be8k7dY9Jva4qOpe8=; h=From:To:Cc:Subject:Date:From; b=aNEhdpxQS99prDfh72nIUYHNrqoiN1aK5jb9lW6gWbnc9L5LSTyP7sOfIttEHHPwF kRPdd9I0pSfnM5//n4z0758Kfdu+ueWjNqpCUL8nS1aCGiGlXwSuCWnM5UbJZTWiQ4 ALuEx6n4IiPXSZP1CZEFAHV03D2Pj53sdswcYkZrMijV2vMpmL5Hk0xkSijtfDbKuX 17aB7vr3VWGRzP+5g/MLal4vWGWJ0bJWJ3DXD7gGQ+TasQcwjs/NeXf91AARFwTLHa 0Rbw2reBnut8MISQanW19xpgZhVLxnwiCMG4Skyi7ud32fbLWpPZwJ/kpopOashCAi 3ndOdBiRirH4A== From: Chao Yu To: jaegeuk@kernel.org Date: Wed, 27 Apr 2022 01:06:02 +0800 Message-Id: <20220426170602.7431-1-chao@kernel.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Headers-End: 1njh8H-0003sO-0a Subject: [f2fs-dev] [PATCH] f2fs: fix to avoid f2fs_bug_on() in dec_valid_node_count() X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, Ming Yan Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net As Yanming reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215897 I have encountered a bug in F2FS file system in kernel v5.17. The kernel should enable CONFIG_KASAN=y and CONFIG_KASAN_INLINE=y. You can reproduce the bug by running the following commands: The kernel message is shown below: kernel BUG at fs/f2fs/f2fs.h:2511! Call Trace: f2fs_remove_inode_page+0x2a2/0x830 f2fs_evict_inode+0x9b7/0x1510 evict+0x282/0x4e0 do_unlinkat+0x33a/0x540 __x64_sys_unlinkat+0x8e/0xd0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae The root cause is: .total_valid_block_count or .total_valid_node_count could fuzzed to zero, then once dec_valid_node_count() was called, it will cause BUG_ON(), this patch fixes to print warning info and set SBI_NEED_FSCK into CP instead of panic. Cc: stable@vger.kernel.org Reported-by: Ming Yan Signed-off-by: Chao Yu --- fs/f2fs/f2fs.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index f3eda4f13646..56adc3b68e14 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -2605,11 +2605,17 @@ static inline void dec_valid_node_count(struct f2fs_sb_info *sbi, { spin_lock(&sbi->stat_lock); - f2fs_bug_on(sbi, !sbi->total_valid_block_count); - f2fs_bug_on(sbi, !sbi->total_valid_node_count); + if (unlikely(!sbi->total_valid_block_count || + !sbi->total_valid_node_count)) { + f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u", + sbi->total_valid_block_count, + sbi->total_valid_node_count); + set_sbi_flag(sbi, SBI_NEED_FSCK); + } else { + sbi->total_valid_block_count--; + sbi->total_valid_node_count--; + } - sbi->total_valid_node_count--; - sbi->total_valid_block_count--; if (sbi->reserved_blocks && sbi->current_reserved_blocks < sbi->reserved_blocks) sbi->current_reserved_blocks++; -- 2.32.0 _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel From mboxrd@z Thu Jan 1 00:00:00 1970 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 DCB18C433EF for ; Wed, 27 Apr 2022 12:50:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234995AbiD0MyF (ORCPT ); Wed, 27 Apr 2022 08:54:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235034AbiD0MyC (ORCPT ); Wed, 27 Apr 2022 08:54:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C6971BA79F; Wed, 27 Apr 2022 05:50:51 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E20DA61B41; Wed, 27 Apr 2022 12:50:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E0D2C385A9; Wed, 27 Apr 2022 12:50:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651063850; bh=l6Uv1aghVwRUkGWE9Fea1FBCo3Be8k7dY9Jva4qOpe8=; h=From:To:Cc:Subject:Date:From; b=aNEhdpxQS99prDfh72nIUYHNrqoiN1aK5jb9lW6gWbnc9L5LSTyP7sOfIttEHHPwF kRPdd9I0pSfnM5//n4z0758Kfdu+ueWjNqpCUL8nS1aCGiGlXwSuCWnM5UbJZTWiQ4 ALuEx6n4IiPXSZP1CZEFAHV03D2Pj53sdswcYkZrMijV2vMpmL5Hk0xkSijtfDbKuX 17aB7vr3VWGRzP+5g/MLal4vWGWJ0bJWJ3DXD7gGQ+TasQcwjs/NeXf91AARFwTLHa 0Rbw2reBnut8MISQanW19xpgZhVLxnwiCMG4Skyi7ud32fbLWpPZwJ/kpopOashCAi 3ndOdBiRirH4A== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , stable@vger.kernel.org, Ming Yan , Chao Yu Subject: [PATCH] f2fs: fix to avoid f2fs_bug_on() in dec_valid_node_count() Date: Wed, 27 Apr 2022 01:06:02 +0800 Message-Id: <20220426170602.7431-1-chao@kernel.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As Yanming reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215897 I have encountered a bug in F2FS file system in kernel v5.17. The kernel should enable CONFIG_KASAN=y and CONFIG_KASAN_INLINE=y. You can reproduce the bug by running the following commands: The kernel message is shown below: kernel BUG at fs/f2fs/f2fs.h:2511! Call Trace: f2fs_remove_inode_page+0x2a2/0x830 f2fs_evict_inode+0x9b7/0x1510 evict+0x282/0x4e0 do_unlinkat+0x33a/0x540 __x64_sys_unlinkat+0x8e/0xd0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae The root cause is: .total_valid_block_count or .total_valid_node_count could fuzzed to zero, then once dec_valid_node_count() was called, it will cause BUG_ON(), this patch fixes to print warning info and set SBI_NEED_FSCK into CP instead of panic. Cc: stable@vger.kernel.org Reported-by: Ming Yan Signed-off-by: Chao Yu --- fs/f2fs/f2fs.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index f3eda4f13646..56adc3b68e14 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -2605,11 +2605,17 @@ static inline void dec_valid_node_count(struct f2fs_sb_info *sbi, { spin_lock(&sbi->stat_lock); - f2fs_bug_on(sbi, !sbi->total_valid_block_count); - f2fs_bug_on(sbi, !sbi->total_valid_node_count); + if (unlikely(!sbi->total_valid_block_count || + !sbi->total_valid_node_count)) { + f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u", + sbi->total_valid_block_count, + sbi->total_valid_node_count); + set_sbi_flag(sbi, SBI_NEED_FSCK); + } else { + sbi->total_valid_block_count--; + sbi->total_valid_node_count--; + } - sbi->total_valid_node_count--; - sbi->total_valid_block_count--; if (sbi->reserved_blocks && sbi->current_reserved_blocks < sbi->reserved_blocks) sbi->current_reserved_blocks++; -- 2.32.0