From nobody Sun Dec 22 06:33:37 2024 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 A5CE9CE7B0E for ; Thu, 28 Sep 2023 08:05:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231258AbjI1IFL (ORCPT ); Thu, 28 Sep 2023 04:05:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230449AbjI1IE4 (ORCPT ); Thu, 28 Sep 2023 04:04:56 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F76CB3; Thu, 28 Sep 2023 01:04:54 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Rx5cK6X23z4f3nJK; Thu, 28 Sep 2023 16:04:49 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.124.27]) by APP4 (Coremail) with SMTP id gCh0CgC3Td2eMxVlgAtdBg--.36922S10; Thu, 28 Sep 2023 16:04:52 +0800 (CST) From: Kemeng Shi To: ritesh.list@gmail.com, tytso@mit.edu, adilger.kernel@dilger.ca Cc: ojaswin@linux.ibm.com, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v8 08/12] ext4: Separate block bitmap and buddy bitmap freeing in ext4_group_add_blocks() Date: Fri, 29 Sep 2023 00:04:03 +0800 Message-Id: <20230928160407.142069-9-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20230928160407.142069-1-shikemeng@huaweicloud.com> References: <20230928160407.142069-1-shikemeng@huaweicloud.com> MIME-Version: 1.0 Content-Transfer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r1lIxAIcVC2z280aV CY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU0TqcUUUUUU== X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch separates block bitmap and buddy bitmap freeing in order to update block bitmap with ext4_mb_mark_context in following patch. The reason why this can be sperated is explained in previous submit. Put the explanation here to simplify the code archeology to ext4_group_add_blocks(): Separated freeing is safe with concurrent allocation as long as: 1. Firstly allocate block in buddy bitmap, and then in block bitmap. 2. Firstly free block in block bitmap, and then buddy bitmap. Then freed block will only be available to allocation when both buddy bitmap and block bitmap are updated by freeing. Allocation obeys rule 1 already, just do sperated freeing with rule 2. Separated freeing has no race with generate_buddy as: Once ext4_mb_load_buddy_gfp is executed successfully, the update-to-date buddy page can be found in sbi->s_buddy_cache and no more buddy initialization of the buddy page will be executed concurrently until buddy page is unloaded. As we always do free in "load buddy, free, unload buddy" sequence, separated freeing has no race with generate_buddy. Signed-off-by: Kemeng Shi Reviewed-by: Ritesh Harjani (IBM) --- fs/ext4/mballoc.c | 54 +++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index 767a975be2ca..27fa3b3b931f 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -6685,35 +6685,39 @@ int ext4_group_add_blocks(handle_t *handle, struct = super_block *sb, ext4_warning(sb, "too many blocks added to group %u", block_group); err =3D -EINVAL; - goto error_return; + goto error_out; + } + + err =3D ext4_mb_load_buddy(sb, block_group, &e4b); + if (err) + goto error_out; + + if (!ext4_sb_block_valid(sb, NULL, block, count)) { + ext4_error(sb, "Adding blocks in system zones - " + "Block =3D %llu, count =3D %lu", + block, count); + err =3D -EINVAL; + goto error_clean; } =20 bitmap_bh =3D ext4_read_block_bitmap(sb, block_group); if (IS_ERR(bitmap_bh)) { err =3D PTR_ERR(bitmap_bh); bitmap_bh =3D NULL; - goto error_return; + goto error_clean; } =20 desc =3D ext4_get_group_desc(sb, block_group, &gd_bh); if (!desc) { err =3D -EIO; - goto error_return; - } - - if (!ext4_sb_block_valid(sb, NULL, block, count)) { - ext4_error(sb, "Adding blocks in system zones - " - "Block =3D %llu, count =3D %lu", - block, count); - err =3D -EINVAL; - goto error_return; + goto error_clean; } =20 BUFFER_TRACE(bitmap_bh, "getting write access"); err =3D ext4_journal_get_write_access(handle, sb, bitmap_bh, EXT4_JTR_NONE); if (err) - goto error_return; + goto error_clean; =20 /* * We are about to modify some metadata. Call the journal APIs @@ -6723,7 +6727,7 @@ int ext4_group_add_blocks(handle_t *handle, struct su= per_block *sb, BUFFER_TRACE(gd_bh, "get_write_access"); err =3D ext4_journal_get_write_access(handle, sb, gd_bh, EXT4_JTR_NONE); if (err) - goto error_return; + goto error_clean; =20 for (i =3D 0, clusters_freed =3D 0; i < cluster_count; i++) { BUFFER_TRACE(bitmap_bh, "clear bit"); @@ -6736,26 +6740,14 @@ int ext4_group_add_blocks(handle_t *handle, struct = super_block *sb, } } =20 - err =3D ext4_mb_load_buddy(sb, block_group, &e4b); - if (err) - goto error_return; - - /* - * need to update group_info->bb_free and bitmap - * with group lock held. generate_buddy look at - * them with group lock_held - */ ext4_lock_group(sb, block_group); mb_clear_bits(bitmap_bh->b_data, bit, cluster_count); - mb_free_blocks(NULL, &e4b, bit, cluster_count); free_clusters_count =3D clusters_freed + ext4_free_group_clusters(sb, desc); ext4_free_group_clusters_set(sb, desc, free_clusters_count); ext4_block_bitmap_csum_set(sb, desc, bitmap_bh); ext4_group_desc_csum_set(sb, block_group, desc); ext4_unlock_group(sb, block_group); - percpu_counter_add(&sbi->s_freeclusters_counter, - clusters_freed); =20 if (sbi->s_log_groups_per_flex) { ext4_group_t flex_group =3D ext4_flex_group(sbi, block_group); @@ -6764,8 +6756,6 @@ int ext4_group_add_blocks(handle_t *handle, struct su= per_block *sb, flex_group)->free_clusters); } =20 - ext4_mb_unload_buddy(&e4b); - /* We dirtied the bitmap block */ BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); err =3D ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); @@ -6776,8 +6766,16 @@ int ext4_group_add_blocks(handle_t *handle, struct s= uper_block *sb, if (!err) err =3D ret; =20 -error_return: + ext4_lock_group(sb, block_group); + mb_free_blocks(NULL, &e4b, bit, cluster_count); + ext4_unlock_group(sb, block_group); + percpu_counter_add(&sbi->s_freeclusters_counter, + clusters_freed); + +error_clean: brelse(bitmap_bh); + ext4_mb_unload_buddy(&e4b); +error_out: ext4_std_error(sb, err); return err; } --=20 2.30.0