Bug Analysis & Technical Explanation
Overview and Context
The reported kernel warning occurs in fs/jbd2/transaction.c:334 inside start_this_handle():
JBD2: syz.0.0 wants too many credits credits:341 rsv_credits:0 max:334
WARNING: fs/jbd2/transaction.c:334 at start_this_handle+0x1e3f/0x21f0 fs/jbd2/transaction.c:334
This warning is triggered when a subsystem attempts to start a JBD2 journal handle requesting more credits than the maximum transaction payload capacity (jbd2_max_user_trans_buffers(journal)).
Root Cause Mechanism
-
Journal Size and Maximum Credits Computation (fs/jbd2/journal.c, fs/jbd2/transaction.c):
- The filesystem was formatted with a 1024-byte block size and the minimum allowable JBD2 journal length:
JBD2_MIN_JOURNAL_BLOCKS = 1024 blocks (include/linux/jbd2.h:66).
- In JBD2,
j_max_transaction_buffers is computed by jbd2_journal_get_max_txn_bufs() as:
$$\lfloor (j_total_len - j_fc_wbufsize) / 3 \rfloor = \lfloor 1024 / 3 \rfloor = 341 \text{ blocks}$$
- JBD2 further deducts transaction overhead buffers (1 commit block + transaction descriptor blocks):
j_transaction_overhead_buffers = 7 blocks.
- Consequently,
jbd2_max_user_trans_buffers(journal) (fs/jbd2/transaction.c:191-195) is:
$$341 - 7 = 334 \text{ blocks}$$
- JBD2 enforces this as a hard invariant in
start_this_handle() (fs/jbd2/transaction.c:328-336): if any handle requests blocks + rsv_blocks > jbd2_max_user_trans_buffers(journal), a warning is issued (WARN_ON(1)) and -ENOSPC is returned.
-
Credit Calculation in __ext4_xattr_set_credits() (fs/ext4/xattr.c:950-1031):
- In
ext4_xattr_set() (fs/ext4/xattr.c:2552-2557), ext4_xattr_set_credits() calls __ext4_xattr_set_credits() to compute the required journal credits for a setxattr operation:
- Base credits for owner inode and xattr block updates:
7 credits.
- With the
ea_inode feature enabled, storing a large extended attribute (value_len = 65296 bytes, near XATTR_SIZE_MAX = 65536) requires allocating a new ea_inode. For a 1KB block filesystem, blocks = ((value_len + blocksize - 1) >> blkbits) + 1 = 65. Credits allocated: 4 (metadata) + 65 * 2 (bitmaps & group descriptors) + 65 (data blocks) = 199 credits.
- When
flags does not specify XATTR_CREATE (is_create = false), ext4_xattr_set() must also account for dereferencing/freeing a potential old ea_inode of up to XATTR_SIZE_MAX: blocks = (65536 >> 10) + 1 = 65. Credits allocated: 4 + 65 * 2 = 134 credits.
- Adding an additional credit for an existing referenced
ea_inode or quota update yields:
$$7 + 199 + 134 + 1 = 341 \text{ credits}$$
-
The Defect:
__ext4_xattr_set_credits() calculates a valid worst-case credit requirement (341 credits) that strictly exceeds the maximum transaction size allowed by the journal (334 credits).
- Ext4 does not validate during mount (
fs/ext4/super.c) that the journal is sufficiently large to accommodate the worst-case transaction requirements of the enabled ea_inode feature on a 1KB block filesystem.
- Nor does
ext4_xattr_set() / __ext4_xattr_set_credits() check jbd2_max_user_trans_buffers() or split/cap large xattr journal transactions before calling ext4_journal_start().
- As a result,
ext4_journal_start() calls jbd2__journal_start(), which encounters 341 > 334 in start_this_handle(), triggering the WARN_ON(1).