Skip to contentSkip to conversation

ext4/lib-crc: LBS performance part 1 - incremental CRC32c for bitmap checksums

RFC PATCH In review Baokun Li 3 participants 29 messages May 8 – Jun 5 7 messages not in the archive

Subsystems (3)
lore
Share & tools
lore

Baokun Li

author reviewer
#0 lore raw

cover ext4/lib-crc: LBS performance part 1 - incremental CRC32c for bitmap checksums

Motivation
==========

In [1] we added large block size (LBS) support to ext4. After enabling LBS we observed several performance bottlenecks:

1. Checksum computation (bitmap, extent block, dir block) becomes significantly more expensive as the block size grows.
2. Free-bit searches (_find_next_bit) over large bitmaps become costly.

CRC32c is linear over GF(2), so when a contiguous range of bits in a buffer is flipped the new checksum can be derived from the old one without re-scanning the entire buffer:

New_CRC = Old_CRC ^ CRC(flip_mask << trailing_bits)

This series introduces crc32c_flip_range() in lib/crc and applies it to ext4's inode and block bitmap checksum paths, reducing checksum overhead from O(N) to O(log N) per update.

For dir blocks and extent blocks, each modification touches a 12-264 byte region; by computing the CRC of the modified region before and after the change we can derive the delta to the overall checksum. A crc32c_splice() API implementing this approach has been developed locally and will be posted shortly.

For the _find_next_bit bottleneck under LBS, a per-block-group free space rb-tree can accelerate lookups. A local prototype exists and is still being tested; feedback and alternative approaches are welcome.

[1]: https://lore.kernel.org/all/[email protected]

Benchmark (full roadmap projection)
====================================

Single-process sequential fallocate of 64K blocks. All throughput values are in GB/s; percentages in parentheses show improvement over the unpatched baseline. "+crc_splice" and "+free_space_tree" columns show expected gains from follow-up series (not included here).

* Blocks per group up to 65528 (default e2fsprogs limit)

   +--------+---------+-----------------+-----------------+---------------------+
   | Blksz  | Before  | +crc_flip_range | +crc_splice     | +free_space_tree    |
   +--------+---------+-----------------+-----------------+---------------------+
   | 1k     | 14.9    | 15.0 (+0.7%)    | 15.2 (+2.0%)    | 15.3 (+2.7%)        |
   | 2k     | 17.5    | 17.8 (+1.7%)    | 18.2 (+4.0%)    | 18.7 (+6.9%)        |
   | 4k     | 16.8    | 17.4 (+3.6%)    | 18.3 (+8.9%)    | 18.4 (+9.5%)        |
   | 8k     | 15.5    | 16.5 (+6.5%)    | 18.3 (+18.1%)   | 18.2 (+17.4%)       |
   | 16k    | 12.6    | 13.2 (+4.8%)    | 15.9 (+26.2%)   | 15.9 (+26.2%)       |
   | 32k    | 8.99    | 9.60 (+6.8%)    | 12.3 (+36.8%)   | 12.5 (+39.0%)       |
   | 64k    | 8.24    | 8.54 (+3.6%)    | 14.0 (+69.9%)   | 19.4 (+135%)        |
   +--------+---------+-----------------+-----------------+---------------------+

* Blocks per group up to 524288 (e2fsprogs limit lifted)

   +--------+---------+-----------------+-----------------+---------------------+
   | Blksz  | Before  | +crc_flip_range | +crc_splice     | +free_space_tree    |
   +--------+---------+-----------------+-----------------+---------------------+
   | 1k     | 15.0    | 14.9 (-0.7%)    | 15.5 (+3.3%)    | 15.6 (+4.0%)        |
   | 2k     | 17.4    | 17.7 (+1.7%)    | 17.9 (+2.9%)    | 18.2 (+4.6%)        |
   | 4k     | 16.7    | 17.3 (+3.6%)    | 18.4 (+10.2%)   | 18.7 (+12.0%)       |
   | 8k     | 15.7    | 16.4 (+4.5%)    | 19.1 (+21.7%)   | 19.3 (+22.9%)       |
   | 16k    | 13.5    | 15.4 (+14.1%)   | 18.7 (+38.5%)   | 19.0 (+40.7%)       |
   | 32k    | 9.64    | 12.3 (+27.6%)   | 17.7 (+83.5%)   | 17.7 (+83.5%)       |
   | 64k    | 2.84    | 3.17 (+11.6%)   | 3.48 (+22.5%)   | 19.8 (+597%)        |
   +--------+---------+-----------------+-----------------+---------------------+

Patch Overview
==============

* Patches 1-3 (lib/crc): Introduce crc32c_flip_range() with O(log N) complexity using precomputed GF(2) shift matrices and nibble-indexed lookup tables (~9.8KB, fits in L1 cache). Add kunit tests and benchmarks.

* Patch 4: Fix incorrect free clusters accounting when allocated blocks overlap with filesystem metadata on a corrupted filesystem.

* Patches 5-7: Extract block bitmap checksum helpers, add the incremental update wrapper ext4_block_bitmap_csum_set_range(), and use it in ext4_mb_mark_context().

* Patches 8-10: Extract inode bitmap checksum helpers, add ext4_inode_bitmap_csum_set_fast(), and use it in ext4_free_inode().

* Patch 11: Fix missing bg_used_dirs_count update during fast commit replay.

* Patches 12-13: Factor out ext4_might_init_block_bitmap() and merge bitmap modification with GDP update under a single group lock in ext4_mark_inode_used(), eliminating a race window.

* Patches 14-15: Rename 'ino' to 'bit' in __ext4_new_inode() for clarity, then merge bitmap modification and GDP update under a single group lock with incremental CRC.

* Patches 16-17: Extract ext4_update_inode_group_desc() and ext4_get_flex_group() helpers to reduce code duplication.

Testing
=======

"kvm-xfstests -c ext4/all -g auto" has been executed with no new failures.

crc32c_flip_range() micro-benchmark on Intel Xeon (Ice Lake) with CRC32c hardware acceleration:

bitmap: 1024 2048 4096 8192 16384 32768 65536

  flip(ns):      48    53    57    63     68     73     78
  full(ns):      45    88   182   357    709   1421   2853

speedup: 0.9x 1.6x 3.1x 5.6x 10.3x 19.3x 36.3x

Comments and questions are, as always, welcome.

Thanks,
Baokun

Baokun Li (17):
lib/crc: add crc32c_flip_range() for incremental CRC update lib/crc: crc_kunit: add kunit test for crc32c_flip_range() lib/crc: crc_kunit: add benchmark for crc32c_flip_range()
ext4: fix incorrect block bitmap free clusters update on metadata
overlap
ext4: extract block bitmap checksum get and store helpers
ext4: add ext4_block_bitmap_csum_set_range() for incremental checksum
update
ext4: use fast incremental CRC update in ext4_mb_mark_context()
ext4: extract inode bitmap checksum get and store helpers
ext4: add ext4_inode_bitmap_csum_set_fast() for incremental checksum
update
ext4: use fast incremental CRC update in ext4_free_inode()
ext4: fix missing bg_used_dirs_count update in fast commit replay
ext4: factor out ext4_might_init_block_bitmap() helper
ext4: use fast incremental CRC update in ext4_mark_inode_used()
ext4: rename ino to bit in __ext4_new_inode()
ext4: use fast incremental CRC update in __ext4_new_inode()
ext4: extract ext4_update_inode_group_desc() to reduce duplication
ext4: add ext4_get_flex_group() helper to simplify flex group lookups

 fs/ext4/bitmap.c                | 109 ++++++++--
 fs/ext4/ext4.h                  |  15 +-
 fs/ext4/fast_commit.c           |  13 +-
 fs/ext4/ialloc.c                | 343 ++++++++++++++------------------
 fs/ext4/mballoc.c               |  28 ++-
 fs/ext4/resize.c                |   4 +-
 fs/ext4/super.c                 |   4 +-
 include/linux/crc32.h           |  25 +++
 lib/crc/.gitignore              |   2 +
 lib/crc/Makefile                |  13 +-
 lib/crc/crc32c-incr.c           | 140 +++++++++++++
 lib/crc/gen_crc32c_incr_table.c | 141 +++++++++++++
 lib/crc/tests/crc_kunit.c       | 137 +++++++++++++
 13 files changed, 746 insertions(+), 228 deletions(-)
 create mode 100644 lib/crc/crc32c-incr.c
 create mode 100644 lib/crc/gen_crc32c_incr_table.c
Signature · 1 line
2.43.7
author reviewer
#1 lore raw

RFC PATCH 1/17 lib/crc: add crc32c_flip_range() for incremental CRC update

When a contiguous range of bits in a buffer is flipped, the CRC32c checksum can be updated incrementally without re-scanning the entire buffer, by exploiting the linearity of CRCs over GF(2):

New_CRC = Old_CRC ^ CRC(flip_mask << trailing_bits)

Introduce crc32c_flip_range() which computes this delta using precomputed GF(2) shift matrices and nibble-indexed lookup tables. The implementation decomposes nbits and trailing_bits into power-of-2 components and combines them via the CRC concatenation property:

  CRC(A || B) = shift(CRC(A), len(B)) ^ CRC(B)

This gives O(log N) complexity with only ~9.8KB of static tables (fits in L1 cache). The current maximum supported buffer size is 64KB (INCR_MAX_ORDER = 19, i.e. 2^19 bits = 524288 bits = 64KB).

This is useful for filesystems like ext4, where bitmap updates involve flipping a contiguous range of bits, and recalculating the full CRC after every update is wasteful.

Benchmark results on Intel Xeon (Ice Lake) with CRC32c hardware acceleration:

bitmap: 1024 2048 4096 8192 16384 32768 65536

  flip(ns):      48    53    57    63     68     73     78
  full(ns):      45    88   182   357    709   1421   2853

speedup: 0.9x 1.6x 3.1x 5.6x 10.3x 19.3x 36.3x

---
 include/linux/crc32.h           |  25 ++++++
 lib/crc/.gitignore              |   2 +
 lib/crc/Makefile                |  13 ++-
 lib/crc/crc32c-incr.c           | 140 +++++++++++++++++++++++++++++++
 lib/crc/gen_crc32c_incr_table.c | 141 ++++++++++++++++++++++++++++++++
 5 files changed, 318 insertions(+), 3 deletions(-)
 create mode 100644 lib/crc/crc32c-incr.c
 create mode 100644 lib/crc/gen_crc32c_incr_table.c
include/linux/crc32.h +25 -0
index da78b215ff2e..034f73f0f5dc 100644
@@ -81,6 +81,31 @@ u32 crc32_be(u32 crc, const void *p, size_t len);
  */
 u32 crc32c(u32 crc, const void *p, size_t len);
 
+/**
+ * crc32c_flip_range - Update CRC32c after flipping a range of bits
+ * @old_crc:    Existing CRC32c value of the buffer (pre-flip).
+ * @total_bits: Total size of the buffer in bits (e.g., 524288 for 64KB).
+ * @bit_off:    Starting bit offset of the modified range.
+ * @nbits:      Length of the flipped bit sequence.
+ *
+ * This function calculates the new CRC32c value when a contiguous range of
+ * bits is flipped (XORed with 1s) without re-scanning the entire buffer.
+ * It leverages the linearity of CRCs in Galois Field GF(2):
+ *
+ * New_CRC = Old_CRC ^ CRC(Mask_of_Ones << Trailing_Bits)
+ *
+ * The complexity is O(log nbits + log trailing_bits), making it
+ * significantly faster than recomputing the CRC for large buffers.
+ *
+ * Note: @total_bits must not exceed 524288 (2^19 bits = 64KB).  Callers
+ * must ensure that @bit_off + @nbits <= @total_bits.  Behavior is
+ * undefined if these constraints are violated.
+ *
+ * Return: The updated CRC32c value.
+ */
+u32 crc32c_flip_range(u32 old_crc, u32 total_bits,
+		      u32 bit_off, u32 nbits);
+
 /*
  * crc32_optimizations() returns flags that indicate which CRC32 library
  * functions are using architecture-specific optimizations.  Unlike
lib/crc/.gitignore +2 -0
index a9e48103c9fb..4e2b9524426d 100644
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 /crc32table.h
+/crc32c-incr-table.h
 /crc64table.h
 /gen_crc32table
+/gen_crc32c_incr_table
 /gen_crc64table
lib/crc/Makefile +10 -3
index ff213590e4e3..2c255ac029d0 100644
@@ -21,7 +21,7 @@ crc-t10dif-$(CONFIG_X86) += x86/crc16-msb-pclmul.o
 endif
 
 obj-$(CONFIG_CRC32) += crc32.o
-crc32-y := crc32-main.o
+crc32-y := crc32-main.o crc32c-incr.o
 ifeq ($(CONFIG_CRC32_ARCH),y)
 CFLAGS_crc32-main.o += -I$(src)/$(SRCARCH)
 crc32-$(CONFIG_ARM) += arm/crc32-core.o
@@ -49,20 +49,27 @@ endif # CONFIG_CRC64_ARCH
 
 obj-y += tests/
 
-hostprogs := gen_crc32table gen_crc64table
-clean-files := crc32table.h crc64table.h
+hostprogs := gen_crc32table gen_crc32c_incr_table gen_crc64table
+clean-files := crc32table.h crc32c-incr-table.h crc64table.h
 
 $(obj)/crc32-main.o: $(obj)/crc32table.h
+$(obj)/crc32c-incr.o: $(obj)/crc32c-incr-table.h
 $(obj)/crc64-main.o: $(obj)/crc64table.h
 
 quiet_cmd_crc32 = GEN     $@
       cmd_crc32 = $< > $@
 
+quiet_cmd_crc32c_incr = GEN     $@
+      cmd_crc32c_incr = $< > $@
+
 quiet_cmd_crc64 = GEN     $@
       cmd_crc64 = $< > $@
 
 $(obj)/crc32table.h: $(obj)/gen_crc32table
 	$(call cmd,crc32)
 
+$(obj)/crc32c-incr-table.h: $(obj)/gen_crc32c_incr_table
+	$(call cmd,crc32c_incr)
+
 $(obj)/crc64table.h: $(obj)/gen_crc64table
 	$(call cmd,crc64)
lib/crc/crc32c-incr.c new file +140 -0

140 lines not on this page · raw message

lib/crc/gen_crc32c_incr_table.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.43.7

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#3 lore raw

Re: [PATCH RFC 01/17] lib/crc: add crc32c_flip_range() for incremental CRC update

Quoted 52 lines · 在 2026/5/9 04:40, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Integer overflow in bit_off + nbits bypasses bounds checks, leading to out-of-bounds kernel memory reads. --

commit 23c49b9c6860184b707e1c64499ef573ebaa8332
Author: Baokun Li <[email protected]>

lib/crc: add crc32c_flip_range() for incremental CRC update

This commit introduces crc32c_flip_range() to compute a CRC32c delta for flipped bits using precomputed GF(2) shift matrices. It gives O(log N) complexity for incremental updates, which is useful for situations like bitmap updates in filesystems.

Quoted 5 lines
lib/crc/crc32c-incr.c new file +0 -0
new file mode 100644
index 0000000000000..b6258231cc0df

[ ... ]

Quoted 5 lines
lib/crc/crc32c-incr.c new file +5 -0
+u32 crc32c_flip_range(u32 old_crc, u32 total_bits,
+		      u32 bit_off, u32 nbits)
+{
+	u32 delta, trailing_bits;
+

lib/crc/crc32c-incr.c line 126

Quoted 10 lines
lib/crc/crc32c-incr.c new file +10 -0
+	if (!nbits)
+		return old_crc;
+
+	/*
+	 * total_bits must not exceed 2^INCR_MAX_ORDER bits (64KB).
+	 * bit_off + nbits must not exceed total_bits.
+	 */
+	if (WARN_ON_ONCE(total_bits > (1UL << INCR_MAX_ORDER)))
+		return old_crc;
+	if (WARN_ON_ONCE(bit_off + nbits > total_bits))

Could this addition overflow since both bit_off and nbits are u32?

If an external caller provides a very large nbits (such as 0xFFFFFFFF) and a small bit_off, their sum wraps around to a small value, which passes the greater-than total_bits check.

If this occurs, the function proceeds to call crc32c_incr_get_ones_delta() with the large nbits value:

lib/crc/crc32c-incr.c line 132

Quoted 6 lines
lib/crc/crc32c-incr.c new file +6 -0
+		return old_crc;
+
+	trailing_bits = total_bits - (bit_off + nbits);
+
+	/* 1. Calculate CRC of the flip-mask (all 1s of length nbits) */
+	delta = crc32c_incr_get_ones_delta(nbits);

Is it possible this leads to an out-of-bounds read?

Quoted 9 lines

Looking at crc32c_incr_get_ones_delta(), passing a large nbits like 0xFFFFFFFF results in __fls(num_bits) evaluating to 31. This would be used to access indexes in crc32c_incr_ones_lookup[] (size 20) and crc32c_incr_nibble_table[] (size 19), reading past the end of the arrays.

A bounds check like nbits > total_bits || bit_off > total_bits - nbits might prevent this overflow.

Indeed, I will fix this issue in the next version.

Thanks,
Baokun

maintainer
#4 lore raw

Re: [PATCH RFC 01/17] lib/crc: add crc32c_flip_range() for incremental CRC update

In reply to Baokun Li
Quoted 12 lines · Baokun Li

When a contiguous range of bits in a buffer is flipped, the CRC32c checksum can be updated incrementally without re-scanning the entire buffer, by exploiting the linearity of CRCs over GF(2):

New_CRC = Old_CRC ^ CRC(flip_mask << trailing_bits)

Introduce crc32c_flip_range() which computes this delta using precomputed GF(2) shift matrices and nibble-indexed lookup tables. The implementation decomposes nbits and trailing_bits into power-of-2 components and combines them via the CRC concatenation property:

Quoted 5 lines
  CRC(A || B) = shift(CRC(A), len(B)) ^ CRC(B)

This gives O(log N) complexity with only ~9.8KB of static tables (fits in L1 cache). The current maximum supported buffer size is 64KB (INCR_MAX_ORDER = 19, i.e. 2^19 bits = 524288 bits = 64KB).

It will be a little while before I can do a full review of this, but just a high-level comment: "only ~9.8KB of static tables (fits in L1 cache)" isn't ideal. Large tables tend to microbenchmark well, then have worse real-world performance due to lots of other things contending for the L1 cache.

Another consideration is that basically every Linux kernel has CONFIG_CRC32 enabled, regardless of whether they would actually find this new functionality useful.

I'm not necessarily saying this should be its own option, especially if it's useful for ext4 even in the non-LBS case. But I do think it would be nice if it could be a bit smaller and more memory-optimized.

Anyway, I'll look into the algorithm more when I have time.

- Eric

author reviewer
#5 lore raw

Re: [PATCH RFC 01/17] lib/crc: add crc32c_flip_range() for incremental CRC update

Hi Eric,
Thanks for the feedback!

Quoted 15 lines · Eric Biggers
Quoted 14 lines · Baokun Li

When a contiguous range of bits in a buffer is flipped, the CRC32c checksum can be updated incrementally without re-scanning the entire buffer, by exploiting the linearity of CRCs over GF(2):

New_CRC = Old_CRC ^ CRC(flip_mask << trailing_bits)

Introduce crc32c_flip_range() which computes this delta using precomputed GF(2) shift matrices and nibble-indexed lookup tables. The implementation decomposes nbits and trailing_bits into power-of-2 components and combines them via the CRC concatenation property:

  CRC(A || B) = shift(CRC(A), len(B)) ^ CRC(B)
Quoted 8 lines
Quoted 3 lines

This gives O(log N) complexity with only ~9.8KB of static tables (fits in L1 cache). The current maximum supported buffer size is 64KB (INCR_MAX_ORDER = 19, i.e. 2^19 bits = 524288 bits = 64KB).

It will be a little while before I can do a full review of this, but just a high-level comment: "only ~9.8KB of static tables (fits in L1 cache)" isn't ideal. Large tables tend to microbenchmark well, then have worse real-world performance due to lots of other things contending for the L1 cache.

You're right, and that's exactly the trap I fell into when picking the initial size.  I went with the variant that had the best kunit microbenchmark while still fitting in a typical L1 -- the nibble-indexed (4-bit) tables.  I've now re-measured all three candidate table sizes:

=== crc32c_flip_range benchmark (ns, speedup vs full) === bitmap  full  1bit(2.5KB)  2bit(4.9KB)  4bit(9.8KB) 1024      46   165 (0.3x)    82 (0.6x)    48 (1.0x) 2048      88   180 (0.5x)    88 (1.0x)    53 (1.7x) 4096     181   194 (0.9x)    98 (1.8x)    58 (3.1x) 8192     358   207 (1.7x)   104 (3.4x)    63 (5.7x) 16384    707   222 (3.2x)   112 (6.3x)   68 (10.4x) 32768   1424   234 (6.1x)  121 (11.8x)   73 (19.5x) 65536   2846  248 (11.5x)  129 (22.1x)   79 (36.0x)

One thing worth mentioning: the upcoming crc32c_splice() API reuses the same GF(2) shift tables for byte-granular CRC updates (extent blocks, dir blocks, etc.).  It's being posted as a separate series because the ext4 integration is more involved, but roughly:

  u32 crc32c_splice(const void *buf, u32 buflen, u32 old_crc, u32 old_region_crc, u32 offset, u32 len) {
      u32 new_region_crc, delta, trail_bits;

      [...]
      new_region_crc = crc32c(0, (const u8 *)buf + offset, len); delta = old_region_crc ^ new_region_crc;

      if (!delta)
          return old_crc;

      trail_bits = (buflen - offset - len) * 8;
      delta = gf2_shift_crc(delta, trail_bits);

      return old_crc ^ delta;
  }

The splice kunit numbers, for completeness:

=== crc32c_splice benchmark (ns, speedup vs full) === blk_regio  full  splice(1bit)  splice(2bit)  splice(4bit) 1024_12      46      8 (5.8x)      9 (5.1x)      9 (5.1x) 1024_32      46     15 (3.1x)     14 (3.3x)     15 (3.1x) 1024_64      46     20 (2.3x)     19 (2.4x)     20 (2.3x) 1024_128     46     30 (1.5x)     31 (1.5x)     30 (1.5x) 1024_264     46     53 (0.9x)     53 (0.9x)     53 (0.9x)
2048_12      88     8 (11.0x)     8 (11.0x)     8 (11.0x) 2048_32      88     15 (5.9x)     13 (6.8x)     15 (5.9x) 2048_64      89     20 (4.5x)     20 (4.5x)     20 (4.5x) 2048_128     89     31 (2.9x)     30 (3.0x)     30 (3.0x) 2048_264     88     53 (1.7x)     53 (1.7x)     53 (1.7x)
4096_12     181     9 (20.1x)     7 (25.9x)     9 (20.1x) 4096_32     181    14 (12.9x)    15 (12.1x)    15 (12.1x) 4096_64     181     20 (9.1x)     20 (9.1x)     19 (9.5x) 4096_128    181     31 (5.8x)     31 (5.8x)     30 (6.0x) 4096_264    182     54 (3.4x)     53 (3.4x)     54 (3.4x)
8192_12     358     9 (39.8x)     8 (44.8x)    10 (35.8x) 8192_32     358    15 (23.9x)    15 (23.9x)    15 (23.9x) 8192_64     358    21 (17.0x)    20 (17.9x)    21 (17.0x) 8192_128    358    32 (11.2x)    31 (11.5x)    31 (11.5x) 8192_264    358     54 (6.6x)     53 (6.8x)     53 (6.8x)
16384_12    707    10 (70.7x)     8 (88.4x)     8 (88.4x) 16384_32    706    15 (47.1x)    15 (47.1x)    15 (47.1x) 16384_64    706    21 (33.6x)    19 (37.2x)    19 (37.2x) 16384_128   707    30 (23.6x)    31 (22.8x)    30 (23.6x) 16384_264   707    54 (13.1x)    53 (13.3x)    53 (13.3x)
32768_12   1422   10 (142.2x)    9 (158.0x)    9 (158.0x) 32768_32   1422    15 (94.8x)    15 (94.8x)    15 (94.8x) 32768_64   1422    20 (71.1x)    19 (74.8x)    20 (71.1x) 32768_128  1422    31 (45.9x)    31 (45.9x)    31 (45.9x) 32768_264  1422    53 (26.8x)    53 (26.8x)    54 (26.3x)
65536_12   2841   10 (284.1x)    9 (315.7x)    8 (355.1x) 65536_32   2840   14 (202.9x)   15 (189.3x)   14 (202.9x) 65536_64   2840   21 (135.2x)   19 (149.5x)   20 (142.0x) 65536_128  2845    30 (94.8x)    31 (91.8x)    31 (91.8x) 65536_264  2841    53 (53.6x)    53 (53.6x)    53 (53.6x)

But, as you point out, what really matters is the real-world impact once the tables are competing for L1 with everything else.  I tested all three table sizes on an ext4 fio workload (single-process sequential fallocate of 64K extents) across a range of filesystem block sizes.  Results below, with both +flip_range alone and +flip_range+splice applied:

=== default mkfs, single-process (GB/s) ===
config  base  raw-bit-flip  raw-bit-splice   2-bit-flip  2-bit-splice 4-bit-flip  4-bit-splice
S_1k    15.4   15.3(-0.6%)     15.3(-0.6%)  15.1(-1.9%)   15.8(+2.6%) 15.0(-2.6%)   15.5(+0.6%)
S_2k    17.6   17.7(+0.6%)     17.9(+1.7%)  17.6(+0.0%)   18.3(+4.0%) 17.2(-2.3%)   18.6(+5.7%)
S_4k    16.9   17.0(+0.6%)    18.6(+10.1%)  17.4(+3.0%)   18.4(+8.9%) 17.3(+2.4%)  18.7(+10.7%)
S_8k    15.8   16.3(+3.2%)    18.1(+14.6%)  16.6(+5.1%)  18.3(+15.8%) 16.4(+3.8%)  17.8(+12.7%)
S_16k   12.5   13.1(+4.8%)    15.4(+23.2%)  13.0(+4.0%)  15.5(+24.0%) 12.9(+3.2%)  15.6(+24.8%)
S_32k   8.93   9.37(+4.9%)    12.5(+40.0%)  9.10(+1.9%)  13.1(+46.7%) 9.07(+1.6%)  12.5(+40.0%)
S_64k   8.17   8.43(+3.2%)    14.3(+75.0%)  8.64(+5.8%)  14.6(+78.7%) 8.39(+2.7%)  14.8(+81.2%)

So the larger tables do measure a bit faster, but the gain over 2-bit is about 3% while the .rodata footprint doubles.  All three variants land within run-to-run noise on the real workload, which matches your prediction exactly.

Based on this I'd lean toward the 2-bit (4.9 KB) variant for v2 as the better trade-off.  Would you prefer that, or the smaller 1-bit (2.5 KB) version?  The ext4 numbers say either is fine; 2-bit just keeps a little more headroom on the microbench in case other consumers show up later.

Quoted 3 lines

Another consideration is that basically every Linux kernel has CONFIG_CRC32 enabled, regardless of whether they would actually find this new functionality useful.

Agreed.  As large-block hardware becomes more common I expect other filesystems beyond ext4 to hit the same large-buffer CRC overhead, so I deliberately put this in lib/crc as a general-purpose API rather than burying it inside ext4.  But you're right that it shouldn't be unconditionally compiled in.  For v2 I'll add CONFIG_CRC32_INCR, selected by consumers (initially just ext4), so kernels that don't need it pay zero .text/.rodata cost.

Quoted 3 lines

I'm not necessarily saying this should be its own option, especially if it's useful for ext4 even in the non-LBS case. But I do think it would be nice if it could be a bit smaller and more memory-optimized.

The non-LBS case does see some benefit, but it's modest -- the incremental update mostly matters once group-descriptor-size CRCs become large.  The good news is that the regression on small-block configs is essentially zero (see the S_1k / S_2k rows above), so I left it unconditionally enabled in the current series to keep things simple.

If there's concern about that, I'm happy to either gate it on a sysfs/mount-option knob, or restrict it to LBS-only paths in ext4.

Quoted 3 lines

Anyway, I'll look into the algorithm more when I have time.

Thanks again for taking the time on this -- the current series is still rough around the edges and I'd appreciate any further feedback once you get to a deeper review.

Cheers,
Baokun

author reviewer
#6 lore raw

RFC PATCH 2/17 lib/crc: crc_kunit: add kunit test for crc32c_flip_range()

In reply to Baokun Li

Add kunit tests for crc32c_flip_range(), validating correctness against naive full-buffer CRC recomputation. All tests use a 64KB buffer and a non-zero CRC seed to match real-world usage (e.g. ext4 metadata checksums):

- ones_lookup[0] single-bit verification.
- num_bits=0 no-op, first/last byte, full 64KB flip.
- Random single-bit flips (100 iterations).
- Random multi-bit contiguous ranges (100 iterations).

---
 lib/crc/tests/crc_kunit.c | 85 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)
lib/crc/tests/crc_kunit.c +85 -0
index 9428cd913625..46f9df5b58e4 100644
@@ -470,6 +470,90 @@ static void crc64_nvme_benchmark(struct kunit *test)
 }
 #endif /* CONFIG_CRC64 */
 
+/*
+ * Test crc32c_flip_range() against naive full-buffer CRC recomputation.
+ * All tests use a 64KB buffer (2^19 bits = INCR_MAX_ORDER limit)
+ * and a non-zero seed to match real-world usage (e.g. ext4 checksums).
+ */
+static void crc32c_flip_range_test(struct kunit *test)
+{
+	size_t buflen = 65536;
+	size_t total_bits = buflen * 8;
+	u32 seed = 0x12345678;
+	u32 expected, flip_crc;
+	size_t start, num_bits, b, pos;
+	u8 *buf;
+	int i;
+
+	buf = kunit_kmalloc(test, buflen, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, buf);
+
+	/* Test 1: Single bit at bit 0 (verifies ones_lookup[0]) */
+	buf[0] = 0x00;
+	expected = crc32c(seed, buf, 1);
+	buf[0] = 0x01;
+	flip_crc = crc32c_flip_range(expected, 8, 0, 1);
+	expected = crc32c(seed, buf, 1);
+	KUNIT_ASSERT_EQ_MSG(test, expected, flip_crc, "Single bit at bit 0");
+
+	/* Test 2: num_bits=0 should be a no-op */
+	memset(buf, 0, buflen);
+	expected = crc32c(seed, buf, buflen);
+	flip_crc = crc32c_flip_range(expected, total_bits, 0, 0);
+	KUNIT_ASSERT_EQ_MSG(test, expected, flip_crc,
+			    "num_bits=0: expected=0x%08x got=0x%08x",
+			    expected, flip_crc);
+
+	/* Test 3: Boundary flips - first byte, last byte, all bits */
+	buf[0] = 0xFF;
+	flip_crc = crc32c_flip_range(expected, total_bits, 0, 8);
+	expected = crc32c(seed, buf, buflen);
+	KUNIT_ASSERT_EQ_MSG(test, expected, flip_crc, "Flip first byte");
+
+	buf[buflen - 1] = 0xFF;
+	flip_crc = crc32c_flip_range(expected, total_bits, (buflen - 1) * 8, 8);
+	expected = crc32c(seed, buf, buflen);
+	KUNIT_ASSERT_EQ_MSG(test, expected, flip_crc, "Flip last byte");
+
+	memset(buf, 0, buflen);
+	expected = crc32c(seed, buf, buflen);
+	memset(buf, 0xFF, buflen);
+	flip_crc = crc32c_flip_range(expected, total_bits, 0, total_bits);
+	expected = crc32c(seed, buf, buflen);
+	KUNIT_ASSERT_EQ_MSG(test, expected, flip_crc, "Flip all 64KB bits");
+
+	/* Test 4: Random single-bit flips (100 iterations) */
+	memset(buf, 0, buflen);
+	expected = crc32c(seed, buf, buflen);
+	for (i = 0; i < 100; i++) {
+		start = rand32() % total_bits;
+		buf[start / 8] ^= (1 << (start % 8));
+
+		flip_crc = crc32c_flip_range(expected, total_bits, start, 1);
+		expected = crc32c(seed, buf, buflen);
+		KUNIT_ASSERT_EQ_MSG(test, expected, flip_crc,
+				    "Single bit at %zu: expected=0x%08x got=0x%08x",
+				    start, expected, flip_crc);
+	}
+
+	/* Test 5: Random multi-bit ranges (100 iterations) */
+	for (i = 0; i < 100; i++) {
+		num_bits = (rand32() % (total_bits - 1)) + 1;
+		start = rand32() % (total_bits - num_bits + 1);
+		for (b = 0; b < num_bits; b++) {
+			pos = start + b;
+			buf[pos / 8] ^= (1 << (pos % 8));
+		}
+
+		flip_crc = crc32c_flip_range(expected, total_bits, start, num_bits);
+		expected = crc32c(seed, buf, buflen);
+
+		KUNIT_ASSERT_EQ_MSG(test, expected, flip_crc,
+				    "Range [%zu, +%zu): expected=0x%08x got=0x%08x",
+				    start, num_bits, expected, flip_crc);
+	}
+}
+
 static struct kunit_case crc_test_cases[] = {
 #if IS_REACHABLE(CONFIG_CRC7)
 	KUNIT_CASE(crc7_be_test),
@@ -490,6 +574,7 @@ static struct kunit_case crc_test_cases[] = {
 	KUNIT_CASE(crc32_be_benchmark),
 	KUNIT_CASE(crc32c_test),
 	KUNIT_CASE(crc32c_benchmark),
+	KUNIT_CASE(crc32c_flip_range_test),
 #endif
 #if IS_REACHABLE(CONFIG_CRC64)
 	KUNIT_CASE(crc64_be_test),
Signature · 1 line
2.43.7
author reviewer
#7 lore raw

RFC PATCH 3/17 lib/crc: crc_kunit: add benchmark for crc32c_flip_range()

In reply to Baokun Li

Add a kunit benchmark comparing crc32c_flip_range() against full crc32c recomputation across bitmap sizes from 1KB to 64KB. The benchmark reports per-call latency in nanoseconds and the speedup ratio.

Sample results (x86_64, Intel(R) Xeon(R) Platinum 8331C):

bitmap=1024: flip_range=48 ns, full_crc=45 ns, speedup=0.9x bitmap=2048: flip_range=53 ns, full_crc=88 ns, speedup=1.6x bitmap=4096: flip_range=57 ns, full_crc=182 ns, speedup=3.1x bitmap=8192: flip_range=63 ns, full_crc=357 ns, speedup=5.6x bitmap=16384: flip_range=68 ns, full_crc=709 ns, speedup=10.3x bitmap=32768: flip_range=73 ns, full_crc=1421 ns, speedup=19.3x bitmap=65536: flip_range=78 ns, full_crc=2853 ns, speedup=36.3x

---
 lib/crc/tests/crc_kunit.c | 52 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
lib/crc/tests/crc_kunit.c +52 -0
index 46f9df5b58e4..8e8b541b37d3 100644
@@ -554,6 +554,57 @@ static void crc32c_flip_range_test(struct kunit *test)
 	}
 }
 
+/*
+ * Benchmark crc32c_flip_range vs full crc32c recomputation
+ */
+static void crc32c_flip_range_benchmark(struct kunit *test)
+{
+	static const size_t bitmap_sizes[] = {
+		1024, 2048, 4096, 8192, 16384, 32768, 65536,
+	};
+	size_t i, j, num_iters, buflen, total_bits;
+	volatile u32 crc;
+	u64 t_flip, t_full;
+	u8 *buf;
+
+	if (!IS_ENABLED(CONFIG_CRC_BENCHMARK))
+		kunit_skip(test, "not enabled");
+
+	buf = kunit_kzalloc(test, 65536, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, buf);
+
+	for (i = 0; i < ARRAY_SIZE(bitmap_sizes); i++) {
+		buflen = bitmap_sizes[i];
+		total_bits = buflen * 8;
+		num_iters = 10000000 / (buflen + 128);
+
+		/* Benchmark crc32c_flip_range */
+		crc = crc32c(0, buf, buflen);
+		preempt_disable();
+		t_flip = ktime_get_ns();
+		for (j = 0; j < num_iters; j++)
+			crc = crc32c_flip_range(crc, total_bits, 100, 100);
+		t_flip = ktime_get_ns() - t_flip;
+		preempt_enable();
+
+		/* Benchmark full crc32c recomputation */
+		preempt_disable();
+		t_full = ktime_get_ns();
+		for (j = 0; j < num_iters; j++)
+			crc = crc32c(0, buf, buflen);
+		t_full = ktime_get_ns() - t_full;
+		preempt_enable();
+
+		kunit_info(test,
+			   "bitmap=%zu: flip_range=%llu ns, full_crc=%llu ns, speedup=%llu.%01llux\n",
+			   buflen,
+			   div64_u64(t_flip, num_iters),
+			   div64_u64(t_full, num_iters),
+			   div64_u64(t_full * 10, t_flip ? t_flip : 1) / 10,
+			   div64_u64(t_full * 10, t_flip ? t_flip : 1) % 10);
+	}
+}
+
 static struct kunit_case crc_test_cases[] = {
 #if IS_REACHABLE(CONFIG_CRC7)
 	KUNIT_CASE(crc7_be_test),
@@ -575,6 +626,7 @@ static struct kunit_case crc_test_cases[] = {
 	KUNIT_CASE(crc32c_test),
 	KUNIT_CASE(crc32c_benchmark),
 	KUNIT_CASE(crc32c_flip_range_test),
+	KUNIT_CASE(crc32c_flip_range_benchmark),
 #endif
 #if IS_REACHABLE(CONFIG_CRC64)
 	KUNIT_CASE(crc64_be_test),
Signature · 1 line
2.43.7

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#9 lore raw

Re: [PATCH RFC 03/17] lib/crc: crc_kunit: add benchmark for crc32c_flip_range()

Quoted 32 lines · 在 2026/5/9 04:54, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Native 64-bit division (/ 10) and modulo (% 10) on u64 values will cause build failures on 32-bit architectures. --

commit d1411c693c30ea46d5821fc2f72f158dd0cea8d2
Author: Baokun Li <[email protected]>

lib/crc: crc_kunit: add benchmark for crc32c_flip_range()

This commit adds a kunit benchmark to compare crc32c_flip_range() against full crc32c recomputation for various bitmap sizes.

Quoted 3 lines
lib/crc/tests/crc_kunit.c +0 -0

[ ... ]

Quoted 2 lines
lib/crc/tests/crc_kunit.c +2 -0
+static void crc32c_flip_range_benchmark(struct kunit *test)
+{

[ ... ]

lib/crc/tests/crc_kunit.c static void crc32c_flip_range_test(struct kunit *test) · line 604

Quoted 7 lines
lib/crc/tests/crc_kunit.c +7 -0
+		kunit_info(test,
+			   "bitmap=%zu: flip_range=%llu ns, full_crc=%llu ns, speedup=%llu.%01llux\n",
+			   buflen,
+			   div64_u64(t_flip, num_iters),
+			   div64_u64(t_full, num_iters),
+			   div64_u64(t_full * 10, t_flip ? t_flip : 1) / 10,
+			   div64_u64(t_full * 10, t_flip ? t_flip : 1) % 10);

Since div64_u64() returns a u64, will applying the native division (/ 10) and modulo (% 10) operators directly to this return value cause build failures on 32-bit architectures?

Quoted 7 lines

The kernel typically prevents native 64-bit division to catch performance issues, which results in linker errors for __udivdi3 and __umoddi3 on 32-bit builds.

Should this use a helper like div_u64_rem() instead?

Yes, it should. I’ll update it to use the div_u64_rem() helper.

Thanks,
Baokun

author reviewer
#10 lore raw

RFC PATCH 4/17 ext4: fix incorrect block bitmap free clusters update on metadata overlap

In reply to Baokun Li

In ext4_mb_mark_diskspace_used(), when the allocator detects that the allocated blocks overlap with filesystem metadata, it enters an error recovery path that marks these blocks as used in the bitmap via ext4_mb_mark_context() with flags=0.

Without EXT4_MB_BITMAP_MARKED_CHECK, ext4_mb_mark_context() assumes all bits in the range will be flipped, so it sets changed=len unconditionally. However, in a corrupted filesystem, some of these metadata blocks may already be marked as used (bit=1) in the bitmap. Since mb_set_bits() is idempotent (sets bits to 1 regardless of current state), bits that are already set won't actually change, but the free clusters count is still decremented by the full range length, leading to an inaccurate free clusters count.

Fix this by passing EXT4_MB_BITMAP_MARKED_CHECK, which correctly counts only the bits that actually changed state.

Fixes 2f94711b098b ("ext4: call ext4_mb_mark_context in ext4_mb_mark_diskspace_used")
---
 fs/ext4/mballoc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/mballoc.c +1 -1
index ed1bd00e11cd..ff2023c9f52c 100644
@@ -4228,7 +4228,7 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, handle_t *handle
 					   ac->ac_b_ex.fe_group,
 					   ac->ac_b_ex.fe_start,
 					   ac->ac_b_ex.fe_len,
-					   0, NULL);
+					   EXT4_MB_BITMAP_MARKED_CHECK, NULL);
 		if (!err)
 			err = -EFSCORRUPTED;
 		return err;
Signature · 1 line
2.43.7

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#12 lore raw

Re: [PATCH RFC 04/17] ext4: fix incorrect block bitmap free clusters update on metadata overlap

Quoted 46 lines · 在 2026/5/9 05:17, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Incomplete Error Recovery / Buddy Cache Inconsistency
- [High] State Inconsistency / Global Counter Desync
--

commit 3b816cdf85c67263b3c35b6097c935d14091d27d
Author: Baokun Li <[email protected]>

ext4: fix incorrect block bitmap free clusters update on metadata overlap

In ext4_mb_mark_diskspace_used(), when the allocator detects that the allocated blocks overlap with filesystem metadata, it enters an error recovery path that marks these blocks as used in the bitmap via ext4_mb_mark_context() with flags=0.

[ ... ]

fs/ext4/mballoc.c ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, handle_t *handle · line 4231

Quoted 9 lines
fs/ext4/mballoc.c +1 -1
@@ -4228,7 +4228,7 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, handle_t *handle
 					   ac->ac_b_ex.fe_group,
 					   ac->ac_b_ex.fe_start,
 					   ac->ac_b_ex.fe_len,
-					   0, NULL);
+					   EXT4_MB_BITMAP_MARKED_CHECK, NULL);

This isn't a regression introduced by this patch, but by passing NULL for the changed pointer, the global sbi->s_freeclusters_counter is not decremented for these leaked blocks.

Because the group descriptors are decremented to reflect the leaked blocks but the global counter is not, does this cause the global free space counter to permanently overestimate available space?

Could this allow ext4_claim_free_clusters() to succeed when there is actually no space, leading to delayed allocation ENOSPC errors during writeback?

Additionally, if ext4_mb_mark_diskspace_used() returns -EFSCORRUPTED, the caller ext4_mb_new_blocks() will handle the failure by invoking ext4_discard_allocated_blocks().

Since ext4_discard_allocated_blocks() calls mb_free_blocks() to clear the bits in the in-memory buddy cache and/or restores them to the preallocation space, doesn't this completely defeat the attempt to leak the blocks here?

Quoted 7 lines

Will the next allocation request for this block group find the identical overlapping blocks in the buddy cache, allocate them, and hit the same metadata overlap check again?

Could this be fixed by clearing ac->ac_b_ex.fe_len and ac->ac_f_ex.fe_len before returning the error, to prevent the rollback function from freeing the blocks in the buddy cache?

Indeed, there are some issues with this error path handling itself, and I will add a patch to fix it in the next version.

Thanks,
Baokun

Quoted 4 lines
Quoted 4 lines
fs/ext4/mballoc.c +0 -0
 		if (!err)
 			err = -EFSCORRUPTED;
 		return err;
author reviewer
#13 lore raw

RFC PATCH 5/17 ext4: extract block bitmap checksum get and store helpers

In reply to Baokun Li

Add ext4_block_bitmap_csum_get() and ext4_block_bitmap_csum_store() helpers, and use EXT4_DESC_SIZE(sb) instead of sbi->s_desc_size for consistency. No functional change.

---
 fs/ext4/bitmap.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
fs/ext4/bitmap.c +22 -9
index 87760fabdd2e..46affc9e80ca 100644
@@ -58,11 +58,29 @@ void ext4_inode_bitmap_csum_set(struct super_block *sb,
 		gdp->bg_inode_bitmap_csum_hi = cpu_to_le16(csum >> 16);
 }
 
+static inline __u32 ext4_block_bitmap_csum_get(struct super_block *sb,
+					       struct ext4_group_desc *gdp)
+{
+	__u32 csum = le16_to_cpu(gdp->bg_block_bitmap_csum_lo);
+
+	if (EXT4_DESC_SIZE(sb) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_END)
+		csum |= (__u32)le16_to_cpu(gdp->bg_block_bitmap_csum_hi) << 16;
+	return csum;
+}
+
+static inline void ext4_block_bitmap_csum_store(struct super_block *sb,
+						struct ext4_group_desc *gdp,
+						__u32 csum)
+{
+	gdp->bg_block_bitmap_csum_lo = cpu_to_le16(csum & 0xFFFF);
+	if (EXT4_DESC_SIZE(sb) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_END)
+		gdp->bg_block_bitmap_csum_hi = cpu_to_le16(csum >> 16);
+}
+
 int ext4_block_bitmap_csum_verify(struct super_block *sb,
 				  struct ext4_group_desc *gdp,
 				  struct buffer_head *bh)
 {
-	__u32 hi;
 	__u32 provided, calculated;
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	int sz = EXT4_CLUSTERS_PER_GROUP(sb) / 8;
@@ -70,12 +88,9 @@ int ext4_block_bitmap_csum_verify(struct super_block *sb,
 	if (!ext4_has_feature_metadata_csum(sb))
 		return 1;
 
-	provided = le16_to_cpu(gdp->bg_block_bitmap_csum_lo);
+	provided = ext4_block_bitmap_csum_get(sb, gdp);
 	calculated = ext4_chksum(sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
-	if (sbi->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_END) {
-		hi = le16_to_cpu(gdp->bg_block_bitmap_csum_hi);
-		provided |= (hi << 16);
-	} else
+	if (EXT4_DESC_SIZE(sb) < EXT4_BG_BLOCK_BITMAP_CSUM_HI_END)
 		calculated &= 0xFFFF;
 
 	return provided == calculated;
@@ -93,7 +108,5 @@ void ext4_block_bitmap_csum_set(struct super_block *sb,
 		return;
 
 	csum = ext4_chksum(sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
-	gdp->bg_block_bitmap_csum_lo = cpu_to_le16(csum & 0xFFFF);
-	if (sbi->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_END)
-		gdp->bg_block_bitmap_csum_hi = cpu_to_le16(csum >> 16);
+	ext4_block_bitmap_csum_store(sb, gdp, csum);
 }
Signature · 1 line
2.43.7
author reviewer
#14 lore raw

RFC PATCH 6/17 ext4: add ext4_block_bitmap_csum_set_range() for incremental checksum update

In reply to Baokun Li

Add a helper function ext4_block_bitmap_csum_set_range() that updates the block bitmap checksum using crc32c_flip_range() for incremental CRC calculation. Unlike ext4_block_bitmap_csum_set() which re-scans the entire bitmap buffer, this function efficiently computes the CRC delta for a range of flipped bits, avoiding the cost of a full CRC recalculation.

---
 fs/ext4/bitmap.c | 24 ++++++++++++++++++++++++
 fs/ext4/ext4.h   |  3 +++
 2 files changed, 27 insertions(+)
fs/ext4/bitmap.c +24 -0
index 46affc9e80ca..00b0a3c74859 100644
@@ -110,3 +110,27 @@ void ext4_block_bitmap_csum_set(struct super_block *sb,
 	csum = ext4_chksum(sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
 	ext4_block_bitmap_csum_store(sb, gdp, csum);
 }
+
+/*
+ * Update block bitmap checksum using incremental CRC calculation.
+ *
+ * This function assumes that ALL bits in the range [offset, offset+len)
+ * have been flipped (XORed with 1). It uses crc32c_flip_range() to
+ * efficiently compute the CRC delta without re-scanning the entire bitmap.
+ * The csum_seed cancels out in the XOR delta, so it is not needed here.
+ */
+void ext4_block_bitmap_csum_set_range(struct super_block *sb,
+				      struct ext4_group_desc *gdp,
+				      ext4_grpblk_t offset, ext4_grpblk_t len)
+{
+	__u32 new_csum, old_csum;
+
+	if (!ext4_has_feature_metadata_csum(sb))
+		return;
+
+	old_csum = ext4_block_bitmap_csum_get(sb, gdp);
+	new_csum = crc32c_flip_range(old_csum, EXT4_CLUSTERS_PER_GROUP(sb),
+				     offset, len);
+
+	ext4_block_bitmap_csum_store(sb, gdp, new_csum);
+}
fs/ext4/ext4.h +3 -0
index 94283a991e5c..c423a9a04047 100644
@@ -2770,6 +2770,9 @@ int ext4_inode_bitmap_csum_verify(struct super_block *sb,
 void ext4_block_bitmap_csum_set(struct super_block *sb,
 				struct ext4_group_desc *gdp,
 				struct buffer_head *bh);
+void ext4_block_bitmap_csum_set_range(struct super_block *sb,
+				      struct ext4_group_desc *gdp,
+				      ext4_grpblk_t offset, ext4_grpblk_t len);
 int ext4_block_bitmap_csum_verify(struct super_block *sb,
 				  struct ext4_group_desc *gdp,
 				  struct buffer_head *bh);
Signature · 1 line
2.43.7

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#16 lore raw

Re: [PATCH RFC 06/17] ext4: add ext4_block_bitmap_csum_set_range() for incremental checksum update

Quoted 42 lines · 在 2026/5/9 05:46, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ext4_block_bitmap_csum_set_range() passes an incorrect total_bits value to crc32c_flip_range() when EXT4_CLUSTERS_PER_GROUP(sb) is not a multiple of 8, leading to corrupted checksums. --

commit 49b9ace366232b1d340c2393c53c09a694007211
Author: Baokun Li <[email protected]>

ext4: add ext4_block_bitmap_csum_set_range() for incremental checksum update

This commit adds a helper function to incrementally update the block bitmap checksum using crc32c_flip_range(). This efficiently computes the CRC delta for a range of flipped bits without re-scanning the entire bitmap buffer.

Quoted 4 lines
fs/ext4/bitmap.c +0 -0
index 46affc9e80ca3..00b0a3c748596 100644

[ ... ]

fs/ext4/bitmap.c void ext4_block_bitmap_csum_set(struct super_block *sb, · line 133

Quoted 12 lines
fs/ext4/bitmap.c +12 -0
+void ext4_block_bitmap_csum_set_range(struct super_block *sb,
+				      struct ext4_group_desc *gdp,
+				      ext4_grpblk_t offset, ext4_grpblk_t len)
+{
+	__u32 new_csum, old_csum;
+
+	if (!ext4_has_feature_metadata_csum(sb))
+		return;
+
+	old_csum = ext4_block_bitmap_csum_get(sb, gdp);
+	new_csum = crc32c_flip_range(old_csum, EXT4_CLUSTERS_PER_GROUP(sb),
+				     offset, len);

Does passing EXT4_CLUSTERS_PER_GROUP(sb) here cause a checksum mismatch if the clusters per group is not a multiple of 8?

In ext4_block_bitmap_csum_set(), the full checksum is calculated over exactly EXT4_CLUSTERS_PER_GROUP(sb) / 8 bytes, ignoring fractional trailing bits:

fs/ext4/bitmap.c:ext4_block_bitmap_csum_set() {
    int sz = EXT4_CLUSTERS_PER_GROUP(sb) / 8;
    ...
    csum = ext4_chksum(sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
}
Quoted 8 lines

If crc32c_flip_range() uses the unaligned bit count as total_bits, the trailing_bits shift calculation will use the wrong baseline length. This could result in an incorrect checksum delta being applied for bit flips within the byte-aligned region.

Would this also erroneously apply a CRC delta if an allocation or free occurs in the fractional trailing bits that the full recalculation ignores?

When formatting, it is ensured that EXT4_CLUSTERS_PER_GROUP is a multiple of 8, so this is an implicit guarantee, and there are no explicit checks in the kernel or fsck.

To be safe, in the next version, I will, like ext4_block_bitmap_csum_set, align it down to 8.

Thanks,
Baokun

author reviewer
#17 lore raw

Re: [PATCH RFC 06/17] ext4: add ext4_block_bitmap_csum_set_range() for incremental checksum update

In reply to a message not in the archive
Quoted 47 lines · 在 2026/5/9 05:46, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ext4_block_bitmap_csum_set_range() passes an incorrect total_bits value to crc32c_flip_range() when EXT4_CLUSTERS_PER_GROUP(sb) is not a multiple of 8, leading to corrupted checksums. --

commit 49b9ace366232b1d340c2393c53c09a694007211
Author: Baokun Li <[email protected]>

ext4: add ext4_block_bitmap_csum_set_range() for incremental checksum update

This commit adds a helper function to incrementally update the block bitmap checksum using crc32c_flip_range(). This efficiently computes the CRC delta for a range of flipped bits without re-scanning the entire bitmap buffer.

Quoted 4 lines
fs/ext4/bitmap.c +0 -0
index 46affc9e80ca3..00b0a3c748596 100644

[ ... ]

fs/ext4/bitmap.c void ext4_block_bitmap_csum_set(struct super_block *sb, · line 133

Quoted 12 lines
fs/ext4/bitmap.c +12 -0
+void ext4_block_bitmap_csum_set_range(struct super_block *sb,
+				      struct ext4_group_desc *gdp,
+				      ext4_grpblk_t offset, ext4_grpblk_t len)
+{
+	__u32 new_csum, old_csum;
+
+	if (!ext4_has_feature_metadata_csum(sb))
+		return;
+
+	old_csum = ext4_block_bitmap_csum_get(sb, gdp);
+	new_csum = crc32c_flip_range(old_csum, EXT4_CLUSTERS_PER_GROUP(sb),
+				     offset, len);

Does passing EXT4_CLUSTERS_PER_GROUP(sb) here cause a checksum mismatch if the clusters per group is not a multiple of 8?

In ext4_block_bitmap_csum_set(), the full checksum is calculated over exactly EXT4_CLUSTERS_PER_GROUP(sb) / 8 bytes, ignoring fractional trailing bits:

fs/ext4/bitmap.c:ext4_block_bitmap_csum_set() {
    int sz = EXT4_CLUSTERS_PER_GROUP(sb) / 8;
    ...
    csum = ext4_chksum(sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
}

If crc32c_flip_range() uses the unaligned bit count as total_bits, the trailing_bits shift calculation will use the wrong baseline length. This could result in an incorrect checksum delta being applied for bit flips within the byte-aligned region.

Quoted 6 lines

Would this also erroneously apply a CRC delta if an allocation or free occurs in the fractional trailing bits that the full recalculation ignores?

fs/ext4/bitmap.c void ext4_block_bitmap_csum_set(struct super_block *sb, · line 136

Quoted 3 lines
fs/ext4/bitmap.c +3 -0
+
+	ext4_block_bitmap_csum_store(sb, gdp, new_csum);
+}

Yes, I missed the case of errors=continue. In the next version, I will revert to full calculation when EXT4_ERROR_FS is set.

Thanks,
Baokun

author reviewer
#18 lore raw

RFC PATCH 7/17 ext4: use fast incremental CRC update in ext4_mb_mark_context()

In reply to Baokun Li

Use ext4_block_bitmap_csum_set_range() in ext4_mb_mark_context() for fast incremental block bitmap checksum updates. Instead of re-scanning the entire bitmap after every allocation or free, the incremental update computes the CRC delta for the modified bit range in O(log N) time.

Add a fast_crc flag that is set when EXT4_MB_BITMAP_MARKED_CHECK is not used. When fast_crc is true, all bits in the range are guaranteed to flip, so the incremental CRC via ext4_block_bitmap_csum_set_range() is correct. Otherwise, fall back to ext4_block_bitmap_csum_set() for a full CRC recalculation, since idempotent operations (mb_set_bits/mb_clear_bits with EXT4_MB_BITMAP_MARKED_CHECK) may leave some bits unchanged.

For the BLOCK_UNINIT case, the bitmap was just initialized and there is no valid old checksum, so fast_crc is forced to false to ensure a full CRC recalculation establishes a correct baseline.

---
 fs/ext4/mballoc.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
fs/ext4/mballoc.c +21 -1
index ff2023c9f52c..77f6309916d1 100644
@@ -4095,6 +4095,7 @@ ext4_mb_mark_context(handle_t *handle, struct super_block *sb, bool state,
 	struct buffer_head *gdp_bh;
 	int err;
 	unsigned int i, already, changed = len;
+	bool fast_crc;
 
 	KUNIT_STATIC_STUB_REDIRECT(ext4_mb_mark_context,
 				   handle, sb, state, group, blkoff, len,
@@ -4127,12 +4128,28 @@ ext4_mb_mark_context(handle_t *handle, struct super_block *sb, bool state,
 			goto out_err;
 	}
 
+	/*
+	 * fast_crc: Use incremental CRC update via crc32c_flip_range().
+	 * This is only valid when all bits in [blkoff, blkoff+len) are
+	 * guaranteed to be in the opposite state (i.e., every bit will
+	 * actually flip). When EXT4_MB_BITMAP_MARKED_CHECK is set,
+	 * mb_set_bits/mb_clear_bits are idempotent, so some bits may not
+	 * change and incremental CRC would produce incorrect results.
+	 */
+	fast_crc = !(flags & EXT4_MB_BITMAP_MARKED_CHECK);
+
 	ext4_lock_group(sb, group);
 	if (ext4_has_group_desc_csum(sb) &&
 	    (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
 		gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
 		ext4_free_group_clusters_set(sb, gdp,
 			ext4_free_clusters_after_init(sb, group, gdp));
+		/*
+		 * The bitmap was just initialized, so the old checksum
+		 * is invalid for incremental CRC update. Fall back to
+		 * full recalculation.
+		 */
+		fast_crc = false;
 	}
 
 	if (flags & EXT4_MB_BITMAP_MARKED_CHECK) {
@@ -4154,7 +4171,10 @@ ext4_mb_mark_context(handle_t *handle, struct super_block *sb, bool state,
 			ext4_free_group_clusters(sb, gdp) + changed);
 	}
 
-	ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh);
+	if (fast_crc)
+		ext4_block_bitmap_csum_set_range(sb, gdp, blkoff, len);
+	else
+		ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh);
 	ext4_group_desc_csum_set(sb, group, gdp);
 	ext4_unlock_group(sb, group);
 	if (ret_changed)
Signature · 1 line
2.43.7

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#20 lore raw

Re: [PATCH RFC 07/17] ext4: use fast incremental CRC update in ext4_mb_mark_context()

Quoted 64 lines · 在 2026/5/9 06:31, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The fast incremental CRC update in ext4_mb_mark_context() corrupts the block bitmap checksum when a double-free or double-allocation occurs. --

commit 09892d15c378eafad6a406a3bb75ec38cfead08f
Author: Baokun Li <[email protected]>

ext4: use fast incremental CRC update in ext4_mb_mark_context()

This commit introduces a fast incremental block bitmap checksum update using ext4_block_bitmap_csum_set_range() to avoid re-scanning the entire bitmap. It falls back to a full recalculation only when EXT4_MB_BITMAP_MARKED_CHECK or BLOCK_UNINIT are present.

Quoted 4 lines
fs/ext4/mballoc.c +0 -0
index ff2023c9f52c6..77f6309916d1e 100644

[ ... ]

fs/ext4/mballoc.c ext4_mb_mark_context(handle_t *handle, struct super_block *sb, bool state, · line 4139

Quoted 13 lines
fs/ext4/mballoc.c +9 -0
@@ -4127,12 +4128,28 @@ ext4_mb_mark_context(handle_t *handle, struct super_block *sb, bool state,
 			goto out_err;
 	}
 
+	/*
+	 * fast_crc: Use incremental CRC update via crc32c_flip_range().
+	 * This is only valid when all bits in [blkoff, blkoff+len) are
+	 * guaranteed to be in the opposite state (i.e., every bit will
+	 * actually flip). When EXT4_MB_BITMAP_MARKED_CHECK is set,
+	 * mb_set_bits/mb_clear_bits are idempotent, so some bits may not
+	 * change and incremental CRC would produce incorrect results.
+	 */
+	fast_crc = !(flags & EXT4_MB_BITMAP_MARKED_CHECK);

Since EXT4_MB_BITMAP_MARKED_CHECK is typically not passed during normal operations like ext4_free_blocks() (unless AGGRESSIVE_CHECK is defined), fast_crc defaults to true.

If a block is double-freed or double-allocated due to an earlier filesystem corruption or bug, mb_clear_bits() and mb_set_bits() will leave the already-modified bits unchanged.

Because fast_crc evaluates to true in this case, does this apply the incremental CRC update assuming the bits flipped, resulting in a bogus checksum that no longer matches the block bitmap data?

[ ... ]

fs/ext4/mballoc.c ext4_mb_mark_context(handle_t *handle, struct super_block *sb, bool state,

Quoted 13 lines
fs/ext4/mballoc.c +2 -1
 	if (state) {
 		mb_set_bits(bitmap_bh->b_data, blkoff, len);
 		ext4_free_group_clusters_set(sb, gdp,
 			ext4_free_group_clusters(sb, gdp) - changed);
 	} else {
 		mb_clear_bits(bitmap_bh->b_data, blkoff, len);
 		ext4_free_group_clusters_set(sb, gdp,
 			ext4_free_group_clusters(sb, gdp) + changed);
 	}
 
-	ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh);
+	if (fast_crc)
+		ext4_block_bitmap_csum_set_range(sb, gdp, blkoff, len);

While the buddy allocator might subsequently detect the double-free (for example, in mb_free_blocks() via ext4_grp_locked_error()), it appears the block bitmap buffer has already been dirtied with the corrupted CRC here.

Quoted 4 lines

Could this cause the entire block group to fail checksum validation on the next mount, unnecessarily escalating a localized error into a metadata corruption that requires fsck?

Yes, I missed the case of errors=continue. In the next version, I will revert to full calculation when EXT4_ERROR_FS is set.

Thanks,
Baokun

author reviewer
#21 lore raw

RFC PATCH 8/17 ext4: extract inode bitmap checksum get and store helpers

In reply to Baokun Li

Add ext4_inode_bitmap_csum_get() and ext4_inode_bitmap_csum_store() helpers, and use EXT4_DESC_SIZE(sb) instead of sbi->s_desc_size for consistency. No functional change.

---
 fs/ext4/bitmap.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
fs/ext4/bitmap.c +22 -9
index 00b0a3c74859..008acc439301 100644
@@ -16,11 +16,29 @@ unsigned int ext4_count_free(char *bitmap, unsigned int numchars)
 	return numchars * BITS_PER_BYTE - memweight(bitmap, numchars);
 }
 
+static inline __u32 ext4_inode_bitmap_csum_get(struct super_block *sb,
+					       struct ext4_group_desc *gdp)
+{
+	__u32 csum = le16_to_cpu(gdp->bg_inode_bitmap_csum_lo);
+
+	if (EXT4_DESC_SIZE(sb) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
+		csum |= (__u32)le16_to_cpu(gdp->bg_inode_bitmap_csum_hi) << 16;
+	return csum;
+}
+
+static inline void ext4_inode_bitmap_csum_store(struct super_block *sb,
+						struct ext4_group_desc *gdp,
+						__u32 csum)
+{
+	gdp->bg_inode_bitmap_csum_lo = cpu_to_le16(csum & 0xFFFF);
+	if (EXT4_DESC_SIZE(sb) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
+		gdp->bg_inode_bitmap_csum_hi = cpu_to_le16(csum >> 16);
+}
+
 int ext4_inode_bitmap_csum_verify(struct super_block *sb,
 				  struct ext4_group_desc *gdp,
 				  struct buffer_head *bh)
 {
-	__u32 hi;
 	__u32 provided, calculated;
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	int sz;
@@ -29,12 +47,9 @@ int ext4_inode_bitmap_csum_verify(struct super_block *sb,
 		return 1;
 
 	sz = EXT4_INODES_PER_GROUP(sb) >> 3;
-	provided = le16_to_cpu(gdp->bg_inode_bitmap_csum_lo);
+	provided = ext4_inode_bitmap_csum_get(sb, gdp);
 	calculated = ext4_chksum(sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
-	if (sbi->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END) {
-		hi = le16_to_cpu(gdp->bg_inode_bitmap_csum_hi);
-		provided |= (hi << 16);
-	} else
+	if (EXT4_DESC_SIZE(sb) < EXT4_BG_INODE_BITMAP_CSUM_HI_END)
 		calculated &= 0xFFFF;
 
 	return provided == calculated;
@@ -53,9 +68,7 @@ void ext4_inode_bitmap_csum_set(struct super_block *sb,
 
 	sz = EXT4_INODES_PER_GROUP(sb) >> 3;
 	csum = ext4_chksum(sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
-	gdp->bg_inode_bitmap_csum_lo = cpu_to_le16(csum & 0xFFFF);
-	if (sbi->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
-		gdp->bg_inode_bitmap_csum_hi = cpu_to_le16(csum >> 16);
+	ext4_inode_bitmap_csum_store(sb, gdp, csum);
 }
 
 static inline __u32 ext4_block_bitmap_csum_get(struct super_block *sb,
Signature · 1 line
2.43.7
author reviewer
#22 lore raw

RFC PATCH 9/17 ext4: add ext4_inode_bitmap_csum_set_fast() for incremental checksum update

In reply to Baokun Li

Add a helper function ext4_inode_bitmap_csum_set_fast() that uses crc32c_flip_range() to incrementally update the inode bitmap checksum after flipping a single bit at the given offset. This avoids a full bitmap CRC rescan, computing the CRC delta in O(log N) time.

---
 fs/ext4/bitmap.c | 23 +++++++++++++++++++++++
 fs/ext4/ext4.h   |  3 +++
 2 files changed, 26 insertions(+)
fs/ext4/bitmap.c +23 -0
index 008acc439301..ea47ca0d7046 100644
@@ -71,6 +71,29 @@ void ext4_inode_bitmap_csum_set(struct super_block *sb,
 	ext4_inode_bitmap_csum_store(sb, gdp, csum);
 }
 
+/*
+ * Update inode bitmap checksum for a single flipped bit.
+ *
+ * Use crc32c_flip_range() to incrementally update the checksum after
+ * flipping the bit at @offset, avoiding a full bitmap CRC rescan.
+ * The csum_seed cancels out in the XOR delta, so it is not needed here.
+ */
+void ext4_inode_bitmap_csum_set_fast(struct super_block *sb,
+				     struct ext4_group_desc *gdp,
+				     ext4_grpblk_t offset)
+{
+	__u32 new_csum, old_csum;
+
+	if (!ext4_has_feature_metadata_csum(sb))
+		return;
+
+	old_csum = ext4_inode_bitmap_csum_get(sb, gdp);
+	new_csum = crc32c_flip_range(old_csum, EXT4_INODES_PER_GROUP(sb),
+				     offset, 1);
+
+	ext4_inode_bitmap_csum_store(sb, gdp, new_csum);
+}
+
 static inline __u32 ext4_block_bitmap_csum_get(struct super_block *sb,
 					       struct ext4_group_desc *gdp)
 {
fs/ext4/ext4.h +3 -0
index c423a9a04047..e6739d5af490 100644
@@ -2764,6 +2764,9 @@ extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
 void ext4_inode_bitmap_csum_set(struct super_block *sb,
 				struct ext4_group_desc *gdp,
 				struct buffer_head *bh);
+void ext4_inode_bitmap_csum_set_fast(struct super_block *sb,
+				     struct ext4_group_desc *gdp,
+				     ext4_grpblk_t offset);
 int ext4_inode_bitmap_csum_verify(struct super_block *sb,
 				  struct ext4_group_desc *gdp,
 				  struct buffer_head *bh);
Signature · 1 line
2.43.7

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#24 lore raw

Re: [PATCH RFC 09/17] ext4: add ext4_inode_bitmap_csum_set_fast() for incremental checksum update

Quoted 57 lines · 在 2026/5/9 06:58, [email protected] 写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Mismatched checksum calculation size in ext4_inode_bitmap_csum_set_fast() for filesystems with s_inodes_per_group not aligned to 8. --

commit 5850c42dcf8baa647b659620b26a9dfeda77192c
Author: Baokun Li <[email protected]>

ext4: add ext4_inode_bitmap_csum_set_fast() for incremental checksum update

This commit adds a helper function to incrementally update the inode bitmap checksum using crc32c_flip_range() after flipping a single bit.

Quoted 3 lines
fs/ext4/bitmap.c +0 -0

[ ... ]

Quoted 13 lines
fs/ext4/bitmap.c +9 -0
@@ -71,6 +71,29 @@ void ext4_inode_bitmap_csum_set(struct super_block *sb,
 	ext4_inode_bitmap_csum_store(sb, gdp, csum);
 }
 
+/*
+ * Update inode bitmap checksum for a single flipped bit.
+ *
+ * Use crc32c_flip_range() to incrementally update the checksum after
+ * flipping the bit at @offset, avoiding a full bitmap CRC rescan.
+ * The csum_seed cancels out in the XOR delta, so it is not needed here.
+ */
+void ext4_inode_bitmap_csum_set_fast(struct super_block *sb,
+				     struct ext4_group_desc *gdp,

fs/ext4/bitmap.c void ext4_inode_bitmap_csum_set(struct super_block *sb, · line 92

Quoted 10 lines
fs/ext4/bitmap.c +10 -0
+				     ext4_grpblk_t offset)
+{
+	__u32 new_csum, old_csum;
+
+	if (!ext4_has_feature_metadata_csum(sb))
+		return;
+
+	old_csum = ext4_inode_bitmap_csum_get(sb, gdp);
+	new_csum = crc32c_flip_range(old_csum, EXT4_INODES_PER_GROUP(sb),
+				     offset, 1);

Does this code properly handle filesystems where s_inodes_per_group is not a multiple of 8?

Looking at the full recalculation path in ext4_inode_bitmap_csum_set(), the checksum is calculated over a byte-aligned size:

fs/ext4/bitmap.c:ext4_inode_bitmap_csum_set() {
    ...
    sz = EXT4_INODES_PER_GROUP(sb) >> 3;
    csum = ext4_chksum(sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
    ...
}

Since ext4_inode_bitmap_csum_set() effectively truncates the length to a multiple of 8 bytes, while the fast path uses the exact EXT4_INODES_PER_GROUP value as total_bits, could this mismatch cause the trailing_bits calculation in crc32c_flip_range() to shift the XOR delta incorrectly?

Quoted 7 lines

If a filesystem image has an unaligned s_inodes_per_group, it appears this could result in a checksum mismatch on subsequent verifications.

Additionally, if the offset falls in the unaligned bits (for example, if offset >= sz * 8), would this fast path apply a delta while the full rescan ignores those bits entirely?

When formatting, it is ensured that EXT4_INODES_PER_GROUP is a multiple of 8, so this is an implicit guarantee, and there are no explicit checks in the kernel or fsck.

To be safe, in the next version, I will, like ext4_inode_bitmap_csum_set, align it down to 8.

Thanks,
Baokun

author reviewer
#25 lore raw

RFC PATCH 10/17 ext4: use fast incremental CRC update in ext4_free_inode()

In reply to Baokun Li

Replace ext4_inode_bitmap_csum_set() with the newly added ext4_inode_bitmap_csum_set_fast() in ext4_free_inode() for incremental inode bitmap checksum update.

This is safe because:
- At inode free time, the inode bitmap checksum has already been initialized, so the old checksum is always valid.
- The bitmap buffer modification and checksum update are protected by the same group lock, ensuring the old checksum is consistent with the bitmap content before the bit flip.

---
 fs/ext4/ialloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ialloc.c +1 -1
index 3fd8f0099852..55eb69fbb4c9 100644
@@ -327,7 +327,7 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
 		if (percpu_counter_initialized(&sbi->s_dirs_counter))
 			percpu_counter_dec(&sbi->s_dirs_counter);
 	}
-	ext4_inode_bitmap_csum_set(sb, gdp, bitmap_bh);
+	ext4_inode_bitmap_csum_set_fast(sb, gdp, bit);
 	ext4_group_desc_csum_set(sb, block_group, gdp);
 	ext4_unlock_group(sb, block_group);
 
Signature · 1 line
2.43.7

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#27 lore raw

Re: [PATCH RFC 10/17] ext4: use fast incremental CRC update in ext4_free_inode()

Quoted 9 lines · On Fri, May 08, 2026 at 11:33:05PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ext4_inode_bitmap_csum_set_fast() corrupts the inode bitmap checksum if EXT4_INODES_PER_GROUP(sb) is not a multiple of 8.

fs/ext4/ialloc.c void ext4_free_inode(handle_t *handle, struct inode *inode) · line 330

Quoted 2 lines
fs/ext4/ialloc.c +1 -1
-	ext4_inode_bitmap_csum_set(sb, gdp, bitmap_bh);
+	ext4_inode_bitmap_csum_set_fast(sb, gdp, bit);

Can this corrupt the inode bitmap checksum if EXT4_INODES_PER_GROUP(sb) is not a multiple of 8?

Quoted 7 lines

In ext4_inode_bitmap_csum_set(), the CRC32c checksum is computed over exactly EXT4_INODES_PER_GROUP(sb) >> 3 bytes. If s_inodes_per_group is not a multiple of 8, any trailing fractional bits are strictly excluded from the checksum calculation.

Because ext4_fill_super() does not enforce that s_inodes_per_group is a multiple of 8, a crafted filesystem can have an unaligned s_inodes_per_group.

The reason why ext4_fill_super() doesn't enforce that s_inodes_per_group is a multiple of 8 was that a long time ago, back when Android was allergic to GPLv2 in userspace, they implemented their own version of mke2fs (and didn't run fsck on the file system, sigh). Their MIT licensed version of make_ext4fs would occasionally make file systems that were not a multiple of 8, and this ran afoul of e2fsck[1] if someone actually tried to repair a corrupted Android user data file system (as opposed to just wiping the flash and starting from scratch).

[1] https://sourceforge.net/p/e2fsprogs/bugs/292/

This was fixed long ago (over a decade ago), and so at this point, I'm pretty sure any such mobile handsets are in the landfill, so we probably should fix this by adding a check in ext4_fill_super() and a corresponding check in e2fsck.

- Ted

author reviewer
#28 lore raw

Re: [PATCH RFC 10/17] ext4: use fast incremental CRC update in ext4_free_inode()

Quoted 26 lines · Theodore Tso
Quoted 8 lines · On Fri, May 08, 2026 at 11:33:05PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ext4_inode_bitmap_csum_set_fast() corrupts the inode bitmap checksum if EXT4_INODES_PER_GROUP(sb) is not a multiple of 8.

fs/ext4/ialloc.c void ext4_free_inode(handle_t *handle, struct inode *inode) · line 330

Quoted 2 lines
fs/ext4/ialloc.c +1 -1
-	ext4_inode_bitmap_csum_set(sb, gdp, bitmap_bh);
+	ext4_inode_bitmap_csum_set_fast(sb, gdp, bit);

Can this corrupt the inode bitmap checksum if EXT4_INODES_PER_GROUP(sb) is not a multiple of 8?

Quoted 7 lines

In ext4_inode_bitmap_csum_set(), the CRC32c checksum is computed over exactly EXT4_INODES_PER_GROUP(sb) >> 3 bytes. If s_inodes_per_group is not a multiple of 8, any trailing fractional bits are strictly excluded from the checksum calculation.

Because ext4_fill_super() does not enforce that s_inodes_per_group is a multiple of 8, a crafted filesystem can have an unaligned s_inodes_per_group.

The reason why ext4_fill_super() doesn't enforce that s_inodes_per_group is a multiple of 8 was that a long time ago, back when Android was allergic to GPLv2 in userspace, they implemented their own version of mke2fs (and didn't run fsck on the file system, sigh). Their MIT licensed version of make_ext4fs would occasionally make file systems that were not a multiple of 8, and this ran afoul of e2fsck[1] if someone actually tried to repair a corrupted Android user data file system (as opposed to just wiping the flash and starting from scratch).

Quoted 8 lines

[1] https://sourceforge.net/p/e2fsprogs/bugs/292/

This was fixed long ago (over a decade ago), and so at this point, I'm pretty sure any such mobile handsets are in the landfill, so we probably should fix this by adding a check in ext4_fill_super() and a corresponding check in e2fsck.

- Ted

Hi Ted,

Thank you for your information and suggestions.

I will send two fix patches to synchronize the checks in mke2fs with ext4_fill_super and e2fsck.

Thanks,
Baokun

author reviewer
#29 lore raw

RFC PATCH 11/17 ext4: fix missing bg_used_dirs_count update in fast commit replay

In reply to Baokun Li

ext4_mark_inode_used() did not update bg_used_dirs_count for directory inodes during fast commit replay because it lacked the inode mode. Add a mode parameter and pass it from both ext4_fc_replay_inode() (from raw_fc_inode) and ext4_fc_replay_create() (after ext4_iget).

Fixes 8016e29f4362 ("ext4: fast commit recovery path")
---
 fs/ext4/ext4.h        |  2 +-
 fs/ext4/fast_commit.c | 13 +++++++------
 fs/ext4/ialloc.c      | 13 ++++++++++++-
 3 files changed, 20 insertions(+), 8 deletions(-)
fs/ext4/ext4.h +1 -1
index e6739d5af490..f48cb9d998ab 100644
@@ -2941,7 +2941,7 @@ extern int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
 			  struct dx_hash_info *hinfo);
 
 /* ialloc.c */
-extern int ext4_mark_inode_used(struct super_block *sb, int ino);
+extern int ext4_mark_inode_used(struct super_block *sb, int ino, umode_t mode);
 extern struct inode *__ext4_new_inode(struct mnt_idmap *, handle_t *,
 				      struct inode *, umode_t,
 				      const struct qstr *qstr, __u32 goal,
fs/ext4/fast_commit.c +7 -6
index b3c22636251d..f68d7b2eb0db 100644
@@ -1578,7 +1578,7 @@ static int ext4_fc_replay_inode(struct super_block *sb,
 	ret = sync_dirty_buffer(iloc.bh);
 	if (ret)
 		goto out_brelse;
-	ret = ext4_mark_inode_used(sb, ino);
+	ret = ext4_mark_inode_used(sb, ino, le16_to_cpu(raw_fc_inode->i_mode));
 	if (ret)
 		goto out_brelse;
 
@@ -1635,11 +1635,7 @@ static int ext4_fc_replay_create(struct super_block *sb,
 	trace_ext4_fc_replay(sb, EXT4_FC_TAG_CREAT, darg.ino,
 			darg.parent_ino, darg.dname_len);
 
-	/* This takes care of update group descriptor and other metadata */
-	ret = ext4_mark_inode_used(sb, darg.ino);
-	if (ret)
-		goto out;
-
+	/* Inode already on disk from TAG_INODE replay; iget first for mode. */
 	inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
 	if (IS_ERR(inode)) {
 		ext4_debug("inode %d not found.", darg.ino);
@@ -1648,6 +1644,11 @@ static int ext4_fc_replay_create(struct super_block *sb,
 		goto out;
 	}
 
+	/* This takes care of update group descriptor and other metadata */
+	ret = ext4_mark_inode_used(sb, darg.ino, inode->i_mode);
+	if (ret)
+		goto out;
+
 	if (S_ISDIR(inode->i_mode)) {
 		/*
 		 * If we are creating a directory, we need to make sure that the
fs/ext4/ialloc.c +12 -1
index 55eb69fbb4c9..5896cdfb2ccf 100644
@@ -756,11 +756,12 @@ static int find_inode_bit(struct super_block *sb, ext4_group_t group,
 	return 1;
 }
 
-int ext4_mark_inode_used(struct super_block *sb, int ino)
+int ext4_mark_inode_used(struct super_block *sb, int ino, umode_t mode)
 {
 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
 	struct buffer_head *inode_bitmap_bh = NULL, *group_desc_bh = NULL;
 	struct ext4_group_desc *gdp;
+	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	ext4_group_t group;
 	int bit;
 	int err;
@@ -858,6 +859,16 @@ int ext4_mark_inode_used(struct super_block *sb, int ino)
 	}
 
 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
+	if (S_ISDIR(mode)) {
+		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
+		if (sbi->s_log_groups_per_flex) {
+			ext4_group_t f = ext4_flex_group(sbi, group);
+
+			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
+							f)->used_dirs);
+		}
+	}
+
 	if (ext4_has_group_desc_csum(sb)) {
 		ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
 		ext4_group_desc_csum_set(sb, group, gdp);
Signature · 1 line
2.43.7
author reviewer
#30 lore raw

RFC PATCH 12/17 ext4: factor out ext4_might_init_block_bitmap() helper

In reply to Baokun Li

Extract the BLOCK_UNINIT initialization logic from ext4_mark_inode_used() and __ext4_new_inode() into a shared ext4_might_init_block_bitmap() helper.

Both call sites perform the same sequence: check EXT4_BG_BLOCK_UNINIT, read the block bitmap, dirty it, then clear the flag and establish the correct block bitmap checksum under the group lock. The only difference is whether a journal handle is available (NULL during fast commit replay in ext4_mark_inode_used()).

No functional change.

---
 fs/ext4/ialloc.c | 129 +++++++++++++++++++++--------------------------
 1 file changed, 58 insertions(+), 71 deletions(-)
fs/ext4/ialloc.c +58 -71
index 5896cdfb2ccf..90896b7f8c73 100644
@@ -756,6 +756,58 @@ static int find_inode_bit(struct super_block *sb, ext4_group_t group,
 	return 1;
 }
 
+/*
+ * If the block bitmap for @group is not yet initialized (EXT4_BG_BLOCK_UNINIT),
+ * read it into memory, dirty it, and clear the UNINIT flag under the group lock
+ * so that the on-disk checksum is established.  @handle may be NULL during fast
+ * commit replay (no journal credits needed in that path).
+ */
+static int ext4_might_init_block_bitmap(handle_t *handle,
+					struct super_block *sb,
+					ext4_group_t group,
+					struct ext4_group_desc *gdp)
+{
+	int err;
+	struct buffer_head *block_bitmap_bh;
+
+	if (!ext4_has_group_desc_csum(sb) ||
+	    !(gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))
+		return 0;
+
+	block_bitmap_bh = ext4_read_block_bitmap(sb, group);
+	if (IS_ERR(block_bitmap_bh))
+		return PTR_ERR(block_bitmap_bh);
+
+	if (handle) {
+		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
+		err = ext4_journal_get_write_access(handle, sb,
+				block_bitmap_bh, EXT4_JTR_NONE);
+		if (err)
+			goto out_brelse;
+	}
+
+	BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
+	err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
+	if (!handle)
+		sync_dirty_buffer(block_bitmap_bh);
+
+	/* recheck and clear flag under lock if we still need to */
+	ext4_lock_group(sb, group);
+	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
+		gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
+		ext4_free_group_clusters_set(sb, gdp,
+			ext4_free_clusters_after_init(sb, group, gdp));
+		ext4_block_bitmap_csum_set(sb, gdp, block_bitmap_bh);
+		ext4_group_desc_csum_set(sb, group, gdp);
+	}
+	ext4_unlock_group(sb, group);
+
+out_brelse:
+	brelse(block_bitmap_bh);
+	ext4_std_error(sb, err);
+	return err;
+}
+
 int ext4_mark_inode_used(struct super_block *sb, int ino, umode_t mode)
 {
 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
@@ -801,38 +853,9 @@ int ext4_mark_inode_used(struct super_block *sb, int ino, umode_t mode)
 	}
 
 	/* We may have to initialize the block bitmap if it isn't already */
-	if (ext4_has_group_desc_csum(sb) &&
-	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
-		struct buffer_head *block_bitmap_bh;
-
-		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
-		if (IS_ERR(block_bitmap_bh)) {
-			err = PTR_ERR(block_bitmap_bh);
-			goto out;
-		}
-
-		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
-		err = ext4_handle_dirty_metadata(NULL, NULL, block_bitmap_bh);
-		sync_dirty_buffer(block_bitmap_bh);
-
-		/* recheck and clear flag under lock if we still need to */
-		ext4_lock_group(sb, group);
-		if (ext4_has_group_desc_csum(sb) &&
-		    (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
-			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
-			ext4_free_group_clusters_set(sb, gdp,
-				ext4_free_clusters_after_init(sb, group, gdp));
-			ext4_block_bitmap_csum_set(sb, gdp, block_bitmap_bh);
-			ext4_group_desc_csum_set(sb, group, gdp);
-		}
-		ext4_unlock_group(sb, group);
-		brelse(block_bitmap_bh);
-
-		if (err) {
-			ext4_std_error(sb, err);
-			goto out;
-		}
-	}
+	err = ext4_might_init_block_bitmap(NULL, sb, group, gdp);
+	if (err)
+		goto out;
 
 	/* Update the relevant bg descriptor fields */
 	if (ext4_has_group_desc_csum(sb)) {
@@ -1154,45 +1177,9 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 	}
 
 	/* We may have to initialize the block bitmap if it isn't already */
-	if (ext4_has_group_desc_csum(sb) &&
-	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
-		struct buffer_head *block_bitmap_bh;
-
-		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
-		if (IS_ERR(block_bitmap_bh)) {
-			err = PTR_ERR(block_bitmap_bh);
-			goto out;
-		}
-		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
-		err = ext4_journal_get_write_access(handle, sb, block_bitmap_bh,
-						    EXT4_JTR_NONE);
-		if (err) {
-			brelse(block_bitmap_bh);
-			ext4_std_error(sb, err);
-			goto out;
-		}
-
-		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
-		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
-
-		/* recheck and clear flag under lock if we still need to */
-		ext4_lock_group(sb, group);
-		if (ext4_has_group_desc_csum(sb) &&
-		    (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
-			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
-			ext4_free_group_clusters_set(sb, gdp,
-				ext4_free_clusters_after_init(sb, group, gdp));
-			ext4_block_bitmap_csum_set(sb, gdp, block_bitmap_bh);
-			ext4_group_desc_csum_set(sb, group, gdp);
-		}
-		ext4_unlock_group(sb, group);
-		brelse(block_bitmap_bh);
-
-		if (err) {
-			ext4_std_error(sb, err);
-			goto out;
-		}
-	}
+	err = ext4_might_init_block_bitmap(handle, sb, group, gdp);
+	if (err)
+		goto out;
 
 	/* Update the relevant bg descriptor fields */
 	if (ext4_has_group_desc_csum(sb)) {
Signature · 1 line
2.43.7
author reviewer
#31 lore raw

RFC PATCH 13/17 ext4: use fast incremental CRC update in ext4_mark_inode_used()

In reply to Baokun Li

Move the bitmap modification, GDP update, and checksum update into a single group lock acquisition in ext4_mark_inode_used(), eliminating the race window where another thread could interleave a full recomputation between bitmap modification and checksum update.

Add a fast_crc flag to select between incremental and full CRC update. When EXT4_BG_INODE_UNINIT is set, the stored checksum in the group descriptor is not a valid CRC of the bitmap -- mkfs leaves it as zero for UNINIT groups, and ext4_read_inode_bitmap() memsets the buffer to zero without updating the gdp checksum. So fast_crc is forced to false to fall back to ext4_inode_bitmap_csum_set() for a full recalculation that establishes a correct baseline.

For non-UNINIT groups, ext4_inode_bitmap_csum_set_fast() computes the CRC delta for the single flipped bit in O(log N) time.

---
 fs/ext4/ialloc.c | 69 ++++++++++++++++++++++++------------------------
 1 file changed, 35 insertions(+), 34 deletions(-)
fs/ext4/ialloc.c +35 -34
index 90896b7f8c73..e209e27f827f 100644
@@ -838,35 +838,37 @@ int ext4_mark_inode_used(struct super_block *sb, int ino, umode_t mode)
 		goto out;
 	}
 
-	ext4_set_bit(bit, inode_bitmap_bh->b_data);
-
-	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
-	err = ext4_handle_dirty_metadata(NULL, NULL, inode_bitmap_bh);
-	if (err) {
-		ext4_std_error(sb, err);
-		goto out;
-	}
-	err = sync_dirty_buffer(inode_bitmap_bh);
-	if (err) {
-		ext4_std_error(sb, err);
-		goto out;
-	}
-
 	/* We may have to initialize the block bitmap if it isn't already */
 	err = ext4_might_init_block_bitmap(NULL, sb, group, gdp);
 	if (err)
 		goto out;
 
+	ext4_lock_group(sb, group);
+	/* Fast commit replay is single-threaded, no need for test_and_set */
+	ext4_set_bit(bit, inode_bitmap_bh->b_data);
+
 	/* Update the relevant bg descriptor fields */
+	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
+	if (S_ISDIR(mode)) {
+		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
+		if (sbi->s_log_groups_per_flex) {
+			ext4_group_t f = ext4_flex_group(sbi, group);
+
+			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
+							f)->used_dirs);
+		}
+	}
+
 	if (ext4_has_group_desc_csum(sb)) {
-		int free;
+		bool fast_crc = true;
+		int free = EXT4_INODES_PER_GROUP(sb) -
+				ext4_itable_unused_count(sb, gdp);
 
-		ext4_lock_group(sb, group); /* while we modify the bg desc */
-		free = EXT4_INODES_PER_GROUP(sb) -
-			ext4_itable_unused_count(sb, gdp);
 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
 			free = 0;
+			/* Incremental CRC needs a valid checksum baseline */
+			fast_crc = false;
 		}
 
 		/*
@@ -877,27 +879,26 @@ int ext4_mark_inode_used(struct super_block *sb, int ino, umode_t mode)
 		if (bit >= free)
 			ext4_itable_unused_set(sb, gdp,
 					(EXT4_INODES_PER_GROUP(sb) - bit - 1));
-	} else {
-		ext4_lock_group(sb, group);
+		if (fast_crc)
+			ext4_inode_bitmap_csum_set_fast(sb, gdp, bit);
+		else
+			ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
+		ext4_group_desc_csum_set(sb, group, gdp);
 	}
 
-	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
-	if (S_ISDIR(mode)) {
-		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
-		if (sbi->s_log_groups_per_flex) {
-			ext4_group_t f = ext4_flex_group(sbi, group);
+	ext4_unlock_group(sb, group);
 
-			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
-							f)->used_dirs);
-		}
+	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
+	err = ext4_handle_dirty_metadata(NULL, NULL, inode_bitmap_bh);
+	if (err) {
+		ext4_std_error(sb, err);
+		goto out;
 	}
-
-	if (ext4_has_group_desc_csum(sb)) {
-		ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
-		ext4_group_desc_csum_set(sb, group, gdp);
+	err = sync_dirty_buffer(inode_bitmap_bh);
+	if (err) {
+		ext4_std_error(sb, err);
+		goto out;
 	}
-
-	ext4_unlock_group(sb, group);
 	err = ext4_handle_dirty_metadata(NULL, NULL, group_desc_bh);
 	sync_dirty_buffer(group_desc_bh);
 out:
Signature · 1 line
2.43.7
author reviewer
#32 lore raw

RFC PATCH 14/17 ext4: rename ino to bit in __ext4_new_inode()

In reply to Baokun Li

In __ext4_new_inode(), the variable 'ino' actually holds a zero-based bit position within the inode bitmap, not an absolute inode number. Rename it to 'bit' to better reflect its semantics and avoid confusion with inode->i_ino.

With this rename, the previous 'ino++' before calculating i_ino is no longer needed; instead compute i_ino directly as 'bit + 1'.

---
 fs/ext4/ialloc.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
fs/ext4/ialloc.c +13 -13
index e209e27f827f..8b75b331b26e 100644
@@ -974,7 +974,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 	struct buffer_head *inode_bitmap_bh = NULL;
 	struct buffer_head *group_desc_bh;
 	ext4_group_t ngroups, group = 0;
-	unsigned long ino = 0;
+	unsigned long bit = 0;
 	struct inode *inode;
 	struct ext4_group_desc *gdp = NULL;
 	struct ext4_inode_info *ei;
@@ -1050,7 +1050,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 
 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
-		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
+		bit = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
 		ret2 = 0;
 		goto got_group;
 	}
@@ -1071,7 +1071,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 	 * unless we get unlucky and it turns out the group we selected
 	 * had its last inode grabbed by someone else.
 	 */
-	for (i = 0; i < ngroups; i++, ino = 0) {
+	for (i = 0; i < ngroups; i++, bit = 0) {
 		err = -EIO;
 
 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
@@ -1105,13 +1105,13 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 		    EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
 			goto next_group;
 
-		ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
+		ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &bit);
 		if (!ret2)
 			goto next_group;
 
-		if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
+		if (group == 0 && (bit + 1) < EXT4_FIRST_INO(sb)) {
 			ext4_error(sb, "reserved inode found cleared - "
-				   "inode=%lu", ino + 1);
+				   "inode=%lu", bit + 1);
 			ext4_mark_group_bitmap_corrupted(sb, group,
 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
 			goto next_group;
@@ -1136,21 +1136,20 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 			goto out;
 		}
 		ext4_lock_group(sb, group);
-		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
+		ret2 = ext4_test_and_set_bit(bit, inode_bitmap_bh->b_data);
 		if (ret2) {
 			/* Someone already took the bit. Repeat the search
 			 * with lock held.
 			 */
-			ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
+			ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &bit);
 			if (ret2) {
-				ext4_set_bit(ino, inode_bitmap_bh->b_data);
+				ext4_set_bit(bit, inode_bitmap_bh->b_data);
 				ret2 = 0;
 			} else {
 				ret2 = 1; /* we didn't grab the inode */
 			}
 		}
 		ext4_unlock_group(sb, group);
-		ino++;		/* the inode bitmap is zero-based */
 		if (!ret2)
 			goto got; /* we grabbed the inode! */
 
@@ -1210,9 +1209,9 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 		 * relative inode number in this group. if it is greater
 		 * we need to update the bg_itable_unused count
 		 */
-		if (ino > free)
+		if (bit >= free)
 			ext4_itable_unused_set(sb, gdp,
-					(EXT4_INODES_PER_GROUP(sb) - ino));
+					(EXT4_INODES_PER_GROUP(sb) - bit - 1));
 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY))
 			up_read(&grp->alloc_sem);
 	} else {
@@ -1252,7 +1251,8 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 						flex_group)->free_inodes);
 	}
 
-	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
+	/* the inode bitmap is zero-based */
+	inode->i_ino = bit + 1 + group * EXT4_INODES_PER_GROUP(sb);
 	/* This is the optimal IO size (for stat), not the fs block size */
 	inode->i_blocks = 0;
 	simple_inode_init_ts(inode);
Signature · 1 line
2.43.7
author reviewer
#33 lore raw

RFC PATCH 15/17 ext4: use fast incremental CRC update in __ext4_new_inode()

In reply to Baokun Li

Merge the bitmap modification and group descriptor update into a single group lock acquisition in __ext4_new_inode(). Previously the bitmap bit was set under one lock/unlock pair, and the GDP fields (UNINIT, itable_unused, free_inodes, dirs, csum) were updated under a separate lock/unlock pair with a gap in between. Another thread could modify the bitmap and update the checksum during that gap, making incremental CRC incorrect.

Now the full sequence -- set bit, update free inodes, clear UNINIT, update itable_unused, and compute checksum -- happens atomically under the same ext4_lock_group(). The alloc_sem is acquired before the group lock to maintain correct locking order with itable lazyinit.

Use ext4_inode_bitmap_csum_set_fast() for the normal path where the stored checksum is valid. When EXT4_BG_INODE_UNINIT is set, fall back to ext4_inode_bitmap_csum_set() for a full recalculation to establish a correct baseline (mkfs leaves the checksum as zero for UNINIT groups).

---
 fs/ext4/ialloc.c | 129 +++++++++++++++++++++++------------------------
 1 file changed, 63 insertions(+), 66 deletions(-)
fs/ext4/ialloc.c +63 -66
index 8b75b331b26e..9dd1cdb367ba 100644
@@ -1135,7 +1135,25 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 			ext4_std_error(sb, err);
 			goto out;
 		}
+
+		BUFFER_TRACE(group_desc_bh, "get_write_access");
+		err = ext4_journal_get_write_access(handle, sb, group_desc_bh,
+						    EXT4_JTR_NONE);
+		if (err) {
+			ext4_std_error(sb, err);
+			goto out;
+		}
+
+		/* We may have to initialize the block bitmap if it isn't already */
+		err = ext4_might_init_block_bitmap(handle, sb, group, gdp);
+		if (err)
+			goto out;
+
+		if (ext4_has_group_desc_csum(sb) &&
+		    !(sbi->s_mount_state & EXT4_FC_REPLAY))
+			down_read(&grp->alloc_sem);
 		ext4_lock_group(sb, group);
+
 		ret2 = ext4_test_and_set_bit(bit, inode_bitmap_bh->b_data);
 		if (ret2) {
 			/* Someone already took the bit. Repeat the search
@@ -1147,9 +1165,54 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 				ret2 = 0;
 			} else {
 				ret2 = 1; /* we didn't grab the inode */
+				goto unlock_group;
+			}
+		}
+
+		/* Update the relevant bg descriptor fields */
+		ext4_free_inodes_set(sb, gdp,
+				     ext4_free_inodes_count(sb, gdp) - 1);
+		if (S_ISDIR(mode)) {
+			ext4_used_dirs_set(sb, gdp,
+					   ext4_used_dirs_count(sb, gdp) + 1);
+			if (sbi->s_log_groups_per_flex) {
+				ext4_group_t f = ext4_flex_group(sbi, group);
+				atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
+								f)->used_dirs);
+			}
+		}
+
+		if (ext4_has_group_desc_csum(sb)) {
+			bool fast_crc = true;
+			int free = EXT4_INODES_PER_GROUP(sb) -
+					ext4_itable_unused_count(sb, gdp);
+
+			if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
+				gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
+				free = 0;
+				/* Incremental CRC needs a valid csum baseline */
+				fast_crc = false;
 			}
+			/*
+			 * Check the relative inode number against the
+			 * last used relative inode number in this group.
+			 * If it is greater we need to update the
+			 * bg_itable_unused count.
+			 */
+			if (bit >= free)
+				ext4_itable_unused_set(sb, gdp,
+					EXT4_INODES_PER_GROUP(sb) - bit - 1);
+			if (fast_crc)
+				ext4_inode_bitmap_csum_set_fast(sb, gdp, bit);
+			else
+				ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
+			ext4_group_desc_csum_set(sb, group, gdp);
 		}
+unlock_group:
 		ext4_unlock_group(sb, group);
+		if (ext4_has_group_desc_csum(sb) &&
+		    !(sbi->s_mount_state & EXT4_FC_REPLAY))
+			up_read(&grp->alloc_sem);
 		if (!ret2)
 			goto got; /* we grabbed the inode! */
 
@@ -1168,72 +1231,6 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 		goto out;
 	}
 
-	BUFFER_TRACE(group_desc_bh, "get_write_access");
-	err = ext4_journal_get_write_access(handle, sb, group_desc_bh,
-					    EXT4_JTR_NONE);
-	if (err) {
-		ext4_std_error(sb, err);
-		goto out;
-	}
-
-	/* We may have to initialize the block bitmap if it isn't already */
-	err = ext4_might_init_block_bitmap(handle, sb, group, gdp);
-	if (err)
-		goto out;
-
-	/* Update the relevant bg descriptor fields */
-	if (ext4_has_group_desc_csum(sb)) {
-		int free;
-		struct ext4_group_info *grp = NULL;
-
-		if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
-			grp = ext4_get_group_info(sb, group);
-			if (!grp) {
-				err = -EFSCORRUPTED;
-				goto out;
-			}
-			down_read(&grp->alloc_sem); /*
-						     * protect vs itable
-						     * lazyinit
-						     */
-		}
-		ext4_lock_group(sb, group); /* while we modify the bg desc */
-		free = EXT4_INODES_PER_GROUP(sb) -
-			ext4_itable_unused_count(sb, gdp);
-		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
-			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
-			free = 0;
-		}
-		/*
-		 * Check the relative inode number against the last used
-		 * relative inode number in this group. if it is greater
-		 * we need to update the bg_itable_unused count
-		 */
-		if (bit >= free)
-			ext4_itable_unused_set(sb, gdp,
-					(EXT4_INODES_PER_GROUP(sb) - bit - 1));
-		if (!(sbi->s_mount_state & EXT4_FC_REPLAY))
-			up_read(&grp->alloc_sem);
-	} else {
-		ext4_lock_group(sb, group);
-	}
-
-	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
-	if (S_ISDIR(mode)) {
-		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
-		if (sbi->s_log_groups_per_flex) {
-			ext4_group_t f = ext4_flex_group(sbi, group);
-
-			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
-							f)->used_dirs);
-		}
-	}
-	if (ext4_has_group_desc_csum(sb)) {
-		ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
-		ext4_group_desc_csum_set(sb, group, gdp);
-	}
-	ext4_unlock_group(sb, group);
-
 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
 	if (err) {
Signature · 1 line
2.43.7
author reviewer
#34 lore raw

RFC PATCH 16/17 ext4: extract ext4_update_inode_group_desc() to reduce duplication

In reply to Baokun Li

ext4_mark_inode_used() and __ext4_new_inode() contain nearly identical code blocks for updating group descriptor fields after inode allocation (UNINIT flag clearing, itable_unused update, inode bitmap checksum, group desc checksum). Extract the common logic into ext4_update_inode_group_desc() to eliminate duplication.

---
 fs/ext4/ialloc.c | 136 ++++++++++++++++++++---------------------------
 1 file changed, 57 insertions(+), 79 deletions(-)
fs/ext4/ialloc.c +57 -79
index 9dd1cdb367ba..25430c572818 100644
@@ -808,12 +808,63 @@ static int ext4_might_init_block_bitmap(handle_t *handle,
 	return err;
 }
 
+/*
+ * Update group descriptor checksums and itable_unused after allocating
+ * inode @bit (0-based relative inode number within the group).
+ * Must be called with the group lock held.
+ */
+static void ext4_update_inode_group_desc(struct super_block *sb,
+					 ext4_group_t group,
+					 struct ext4_group_desc *gdp,
+					 struct buffer_head *inode_bitmap_bh,
+					 int bit, umode_t mode)
+{
+	int free;
+	struct ext4_sb_info *sbi = EXT4_SB(sb);
+	bool fast_crc = true;
+
+	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
+	if (S_ISDIR(mode)) {
+		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
+		if (sbi->s_log_groups_per_flex) {
+			ext4_group_t f = ext4_flex_group(sbi, group);
+
+			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
+							f)->used_dirs);
+		}
+	}
+
+	if (!ext4_has_group_desc_csum(sb))
+		return;
+
+	free = EXT4_INODES_PER_GROUP(sb) - ext4_itable_unused_count(sb, gdp);
+	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
+		gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
+		free = 0;
+		/* Incremental CRC needs a valid checksum baseline */
+		fast_crc = false;
+	}
+
+	/*
+	 * Check the relative inode number against the last used
+	 * relative inode number in this group. If it is greater
+	 * we need to update the bg_itable_unused count.
+	 */
+	if (bit >= free)
+		ext4_itable_unused_set(sb, gdp,
+				       EXT4_INODES_PER_GROUP(sb) - bit - 1);
+	if (fast_crc)
+		ext4_inode_bitmap_csum_set_fast(sb, gdp, bit);
+	else
+		ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
+	ext4_group_desc_csum_set(sb, group, gdp);
+}
+
 int ext4_mark_inode_used(struct super_block *sb, int ino, umode_t mode)
 {
 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
 	struct buffer_head *inode_bitmap_bh = NULL, *group_desc_bh = NULL;
 	struct ext4_group_desc *gdp;
-	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	ext4_group_t group;
 	int bit;
 	int err;
@@ -848,44 +899,8 @@ int ext4_mark_inode_used(struct super_block *sb, int ino, umode_t mode)
 	ext4_set_bit(bit, inode_bitmap_bh->b_data);
 
 	/* Update the relevant bg descriptor fields */
-	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
-	if (S_ISDIR(mode)) {
-		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
-		if (sbi->s_log_groups_per_flex) {
-			ext4_group_t f = ext4_flex_group(sbi, group);
-
-			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
-							f)->used_dirs);
-		}
-	}
-
-	if (ext4_has_group_desc_csum(sb)) {
-		bool fast_crc = true;
-		int free = EXT4_INODES_PER_GROUP(sb) -
-				ext4_itable_unused_count(sb, gdp);
-
-		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
-			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
-			free = 0;
-			/* Incremental CRC needs a valid checksum baseline */
-			fast_crc = false;
-		}
-
-		/*
-		 * Check the relative inode number against the last used
-		 * relative inode number in this group. if it is greater
-		 * we need to update the bg_itable_unused count
-		 */
-		if (bit >= free)
-			ext4_itable_unused_set(sb, gdp,
-					(EXT4_INODES_PER_GROUP(sb) - bit - 1));
-		if (fast_crc)
-			ext4_inode_bitmap_csum_set_fast(sb, gdp, bit);
-		else
-			ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
-		ext4_group_desc_csum_set(sb, group, gdp);
-	}
-
+	ext4_update_inode_group_desc(sb, group, gdp,
+				     inode_bitmap_bh, bit, mode);
 	ext4_unlock_group(sb, group);
 
 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
@@ -1165,50 +1180,13 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 				ret2 = 0;
 			} else {
 				ret2 = 1; /* we didn't grab the inode */
-				goto unlock_group;
 			}
 		}
 
 		/* Update the relevant bg descriptor fields */
-		ext4_free_inodes_set(sb, gdp,
-				     ext4_free_inodes_count(sb, gdp) - 1);
-		if (S_ISDIR(mode)) {
-			ext4_used_dirs_set(sb, gdp,
-					   ext4_used_dirs_count(sb, gdp) + 1);
-			if (sbi->s_log_groups_per_flex) {
-				ext4_group_t f = ext4_flex_group(sbi, group);
-				atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
-								f)->used_dirs);
-			}
-		}
-
-		if (ext4_has_group_desc_csum(sb)) {
-			bool fast_crc = true;
-			int free = EXT4_INODES_PER_GROUP(sb) -
-					ext4_itable_unused_count(sb, gdp);
-
-			if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
-				gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
-				free = 0;
-				/* Incremental CRC needs a valid csum baseline */
-				fast_crc = false;
-			}
-			/*
-			 * Check the relative inode number against the
-			 * last used relative inode number in this group.
-			 * If it is greater we need to update the
-			 * bg_itable_unused count.
-			 */
-			if (bit >= free)
-				ext4_itable_unused_set(sb, gdp,
-					EXT4_INODES_PER_GROUP(sb) - bit - 1);
-			if (fast_crc)
-				ext4_inode_bitmap_csum_set_fast(sb, gdp, bit);
-			else
-				ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
-			ext4_group_desc_csum_set(sb, group, gdp);
-		}
-unlock_group:
+		if (!ret2)
+			ext4_update_inode_group_desc(sb, group, gdp,
+						     inode_bitmap_bh, bit, mode);
 		ext4_unlock_group(sb, group);
 		if (ext4_has_group_desc_csum(sb) &&
 		    !(sbi->s_mount_state & EXT4_FC_REPLAY))
Signature · 1 line
2.43.7
author reviewer
#35 lore raw

RFC PATCH 17/17 ext4: add ext4_get_flex_group() helper to simplify flex group lookups

In reply to Baokun Li

Introduce ext4_get_flex_group() that combines ext4_flex_group() and sbi_array_rcu_deref() into a single call, replacing the repeated pattern across ialloc.c, mballoc.c, resize.c, and super.c.

No functional change.

---
 fs/ext4/ext4.h    |  7 +++++++
 fs/ext4/ialloc.c  | 19 +++++--------------
 fs/ext4/mballoc.c |  4 +---
 fs/ext4/resize.c  |  4 +---
 fs/ext4/super.c   |  4 +---
 5 files changed, 15 insertions(+), 23 deletions(-)
fs/ext4/ext4.h +7 -0
index f48cb9d998ab..e38ada51972a 100644
@@ -3457,6 +3457,13 @@ static inline unsigned int ext4_flex_bg_size(struct ext4_sb_info *sbi)
 	return 1 << sbi->s_log_groups_per_flex;
 }
 
+static inline struct flex_groups *ext4_get_flex_group(struct ext4_sb_info *sbi,
+						      ext4_group_t block_group)
+{
+	return sbi_array_rcu_deref(sbi, s_flex_groups,
+				   ext4_flex_group(sbi, block_group));
+}
+
 static inline loff_t ext4_get_maxbytes(struct inode *inode)
 {
 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
fs/ext4/ialloc.c +5 -14
index 25430c572818..d88160afd6b6 100644
@@ -336,8 +336,7 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
 	if (sbi->s_log_groups_per_flex) {
 		struct flex_groups *fg;
 
-		fg = sbi_array_rcu_deref(sbi, s_flex_groups,
-					 ext4_flex_group(sbi, block_group));
+		fg = ext4_get_flex_group(sbi, block_group);
 		atomic_inc(&fg->free_inodes);
 		if (is_directory)
 			atomic_dec(&fg->used_dirs);
@@ -826,12 +825,8 @@ static void ext4_update_inode_group_desc(struct super_block *sb,
 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
 	if (S_ISDIR(mode)) {
 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
-		if (sbi->s_log_groups_per_flex) {
-			ext4_group_t f = ext4_flex_group(sbi, group);
-
-			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
-							f)->used_dirs);
-		}
+		if (sbi->s_log_groups_per_flex)
+			atomic_inc(&ext4_get_flex_group(sbi, group)->used_dirs);
 	}
 
 	if (!ext4_has_group_desc_csum(sb))
@@ -997,7 +992,6 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 	int ret2, err;
 	struct inode *ret;
 	ext4_group_t i;
-	ext4_group_t flex_group;
 	struct ext4_group_info *grp = NULL;
 	bool encrypt = false;
 
@@ -1220,11 +1214,8 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 	if (S_ISDIR(mode))
 		percpu_counter_inc(&sbi->s_dirs_counter);
 
-	if (sbi->s_log_groups_per_flex) {
-		flex_group = ext4_flex_group(sbi, group);
-		atomic_dec(&sbi_array_rcu_deref(sbi, s_flex_groups,
-						flex_group)->free_inodes);
-	}
+	if (sbi->s_log_groups_per_flex)
+		atomic_dec(&ext4_get_flex_group(sbi, group)->free_inodes);
 
 	/* the inode bitmap is zero-based */
 	inode->i_ino = bit + 1 + group * EXT4_INODES_PER_GROUP(sb);
fs/ext4/mballoc.c +1 -3
index 77f6309916d1..9e30c9eefd35 100644
@@ -4181,9 +4181,7 @@ ext4_mb_mark_context(handle_t *handle, struct super_block *sb, bool state,
 		*ret_changed = changed;
 
 	if (sbi->s_log_groups_per_flex) {
-		ext4_group_t flex_group = ext4_flex_group(sbi, group);
-		struct flex_groups *fg = sbi_array_rcu_deref(sbi,
-					   s_flex_groups, flex_group);
+		struct flex_groups *fg = ext4_get_flex_group(sbi, group);
 
 		if (state)
 			atomic64_sub(changed, &fg->free_clusters);
fs/ext4/resize.c +1 -3
index 2c5b851c552a..8d2cd1bc17bb 100644
@@ -1495,11 +1495,9 @@ static void ext4_update_super(struct super_block *sb,
 	ext4_debug("free blocks count %llu",
 		   percpu_counter_read(&sbi->s_freeclusters_counter));
 	if (ext4_has_feature_flex_bg(sb) && sbi->s_log_groups_per_flex) {
-		ext4_group_t flex_group;
 		struct flex_groups *fg;
 
-		flex_group = ext4_flex_group(sbi, group_data[0].group);
-		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
+		fg = ext4_get_flex_group(sbi, group_data[0].group);
 		atomic64_add(EXT4_NUM_B2C(sbi, free_blocks),
 			     &fg->free_clusters);
 		atomic_add(EXT4_INODES_PER_GROUP(sb) * flex_gd->count,
fs/ext4/super.c +1 -3
index 6a77db4d3124..064e06163716 100644
@@ -3211,7 +3211,6 @@ static int ext4_fill_flex_info(struct super_block *sb)
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	struct ext4_group_desc *gdp = NULL;
 	struct flex_groups *fg;
-	ext4_group_t flex_group;
 	int i, err;
 
 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
@@ -3227,8 +3226,7 @@ static int ext4_fill_flex_info(struct super_block *sb)
 	for (i = 0; i < sbi->s_groups_count; i++) {
 		gdp = ext4_get_group_desc(sb, i, NULL);
 
-		flex_group = ext4_flex_group(sbi, i);
-		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
+		fg = ext4_get_flex_group(sbi, i);
 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
 		atomic64_add(ext4_free_group_clusters(sb, gdp),
 			     &fg->free_clusters);
Signature · 1 line
2.43.7