From nobody Fri Dec 19 01:16:50 2025 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 04E26CDB474 for ; Mon, 23 Oct 2023 01:26:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233055AbjJWB00 (ORCPT ); Sun, 22 Oct 2023 21:26:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229623AbjJWB0U (ORCPT ); Sun, 22 Oct 2023 21:26:20 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 254ABF1; Sun, 22 Oct 2023 18:26:18 -0700 (PDT) Received: from dggpeml500021.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4SDHWg2JSXz15NkJ; Mon, 23 Oct 2023 09:23:27 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpeml500021.china.huawei.com (7.185.36.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Mon, 23 Oct 2023 09:26:15 +0800 From: Baokun Li To: CC: , , , , , , , , Subject: [PATCH v2 1/4] ext4: unify the type of flexbg_size to unsigned int Date: Mon, 23 Oct 2023 09:30:54 +0800 Message-ID: <20231023013057.2117948-2-libaokun1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20231023013057.2117948-1-libaokun1@huawei.com> References: <20231023013057.2117948-1-libaokun1@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500021.china.huawei.com (7.185.36.21) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The maximum value of flexbg_size is 2^31, but the maximum value of int is (2^31 - 1), so overflow may occur when the type of flexbg_size is declared as int. For example, when uninit_mask is initialized in ext4_alloc_group_tables(), if flexbg_size =3D=3D 2^31, the initialized uninit_mask is incorrect, and t= his may causes set_flexbg_block_bitmap() to trigger a BUG_ON(). Therefore, the flexbg_size type is declared as unsigned int to avoid overflow and memory waste. Signed-off-by: Baokun Li Reviewed-by: Jan Kara --- fs/ext4/resize.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c index 4fe061edefdd..c6d4539d4c1f 100644 --- a/fs/ext4/resize.c +++ b/fs/ext4/resize.c @@ -228,7 +228,7 @@ struct ext4_new_flex_group_data { * * Returns NULL on failure otherwise address of the allocated structure. */ -static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg= _size) +static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned int flexbg_= size) { struct ext4_new_flex_group_data *flex_gd; =20 @@ -283,7 +283,7 @@ static void free_flex_gd(struct ext4_new_flex_group_dat= a *flex_gd) */ static int ext4_alloc_group_tables(struct super_block *sb, struct ext4_new_flex_group_data *flex_gd, - int flexbg_size) + unsigned int flexbg_size) { struct ext4_new_group_data *group_data =3D flex_gd->groups; ext4_fsblk_t start_blk; @@ -384,12 +384,12 @@ static int ext4_alloc_group_tables(struct super_block= *sb, group =3D group_data[0].group; =20 printk(KERN_DEBUG "EXT4-fs: adding a flex group with " - "%d groups, flexbg size is %d:\n", flex_gd->count, + "%u groups, flexbg size is %u:\n", flex_gd->count, flexbg_size); =20 for (i =3D 0; i < flex_gd->count; i++) { ext4_debug( - "adding %s group %u: %u blocks (%d free, %d mdata blocks)\n", + "adding %s group %u: %u blocks (%u free, %u mdata blocks)\n", ext4_bg_has_super(sb, group + i) ? "normal" : "no-super", group + i, group_data[i].blocks_count, @@ -1606,7 +1606,7 @@ static int ext4_flex_group_add(struct super_block *sb, static int ext4_setup_next_flex_gd(struct super_block *sb, struct ext4_new_flex_group_data *flex_gd, ext4_fsblk_t n_blocks_count, - unsigned long flexbg_size) + unsigned int flexbg_size) { struct ext4_sb_info *sbi =3D EXT4_SB(sb); struct ext4_super_block *es =3D sbi->s_es; @@ -1990,8 +1990,9 @@ int ext4_resize_fs(struct super_block *sb, ext4_fsblk= _t n_blocks_count) ext4_fsblk_t o_blocks_count; ext4_fsblk_t n_blocks_count_retry =3D 0; unsigned long last_update_time =3D 0; - int err =3D 0, flexbg_size =3D 1 << sbi->s_log_groups_per_flex; + int err =3D 0; int meta_bg; + unsigned int flexbg_size =3D ext4_flex_bg_size(sbi); =20 /* See if the device is actually as big as what was requested */ bh =3D ext4_sb_bread(sb, n_blocks_count - 1, 0); --=20 2.31.1