ext4: call ext4_mb_mark_free_simple to free continuous bits in found chunk
authorKemeng Shi <shikemeng@huaweicloud.com>
Wed, 24 Apr 2024 06:19:02 +0000 (14:19 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 3 May 2024 04:12:32 +0000 (00:12 -0400)
In mb_mark_used, we will find free chunk and mark it inuse. For chunk
in mid of passed range, we could simply mark whole chunk inuse. For chunk
at end of range, we may need to mark a continuous bits at end of part of
chunk inuse and keep rest part of chunk free. To only mark a part of
chunk inuse, we firstly mark whole chunk inuse and then mark a continuous
range at end of chunk free.
Function mb_mark_used does several times of "mb_find_buddy; mb_clear_bit;
..." to mark a continuous range free which can be done by simply calling
ext4_mb_mark_free_simple which free continuous bits in a more effective
way.
Just call ext4_mb_mark_free_simple in mb_mark_used to use existing and
effective code to free continuous blocks in chunk at end of passed range.

Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20240424061904.987525-4-shikemeng@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/mballoc.c

index 33e6a3ebdb55fa1b9968adbfeca99c6f7ef1d860..9a2b0bd0dcbc62b4b3e3c9dc208f61cc7359a8f4 100644 (file)
@@ -2044,13 +2044,12 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
        int ord;
        int mlen = 0;
        int max = 0;
-       int cur;
        int start = ex->fe_start;
        int len = ex->fe_len;
        unsigned ret = 0;
        int len0 = len;
        void *buddy;
-       bool split = false;
+       int ord_start, ord_end;
 
        BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
        BUG_ON(e4b->bd_group != ex->fe_group);
@@ -2075,16 +2074,12 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 
        /* let's maintain buddy itself */
        while (len) {
-               if (!split)
-                       ord = mb_find_order_for_block(e4b, start);
+               ord = mb_find_order_for_block(e4b, start);
 
                if (((start >> ord) << ord) == start && len >= (1 << ord)) {
                        /* the whole chunk may be allocated at once! */
                        mlen = 1 << ord;
-                       if (!split)
-                               buddy = mb_find_buddy(e4b, ord, &max);
-                       else
-                               split = false;
+                       buddy = mb_find_buddy(e4b, ord, &max);
                        BUG_ON((start >> ord) >= max);
                        mb_set_bit(start >> ord, buddy);
                        e4b->bd_info->bb_counters[ord]--;
@@ -2098,20 +2093,29 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
                if (ret == 0)
                        ret = len | (ord << 16);
 
-               /* we have to split large buddy */
                BUG_ON(ord <= 0);
                buddy = mb_find_buddy(e4b, ord, &max);
                mb_set_bit(start >> ord, buddy);
                e4b->bd_info->bb_counters[ord]--;
 
-               ord--;
-               cur = (start >> ord) & ~1U;
-               buddy = mb_find_buddy(e4b, ord, &max);
-               mb_clear_bit(cur, buddy);
-               mb_clear_bit(cur + 1, buddy);
-               e4b->bd_info->bb_counters[ord]++;
-               e4b->bd_info->bb_counters[ord]++;
-               split = true;
+               ord_start = (start >> ord) << ord;
+               ord_end = ord_start + (1 << ord);
+               /* first chunk */
+               if (start > ord_start)
+                       ext4_mb_mark_free_simple(e4b->bd_sb, e4b->bd_buddy,
+                                                ord_start, start - ord_start,
+                                                e4b->bd_info);
+
+               /* last chunk */
+               if (start + len < ord_end) {
+                       ext4_mb_mark_free_simple(e4b->bd_sb, e4b->bd_buddy,
+                                                start + len,
+                                                ord_end - (start + len),
+                                                e4b->bd_info);
+                       break;
+               }
+               len = start + len - ord_end;
+               start = ord_end;
        }
        mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);