From nobody Sun Feb 8 05:53:52 2026 Received: from mail-yx1-f65.google.com (mail-yx1-f65.google.com [74.125.224.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29108500946 for ; Sat, 10 Jan 2026 20:33:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768077225; cv=none; b=q208br2PGKNtHuYbxQIEv8+OApdpgAPSFmTl/a19KSZCXOGDZa43OCR3AUilbAzL+kE3wCRa0q/ra9otGcrgp9VSZNrUslUaQhPvIlZikji8osjuixIGsZNxGChyoCEBSUbqunupDMOGyqjovlafLwmlhDSJ4cudGBGpmEHe1ic= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768077225; c=relaxed/simple; bh=qA4Qh4+2GHwSNaTQLaDtoV60/NRTMzxmifxRYVm/pQM=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=rCbjacCK1SWnhk0HtNAEShbrW6UpF3F9yuxYpVl77htb+oIpV7sdgjANjPLaJc249C2hnqSFxMJCsazBQwxX5Bff+aG8FXTXmoYfp+PF07mNE9OsFV0tF4t0vO2mT4LQKp+P7zhPK+H3MvKQ+mNM7uhWqM9eQQkMT9JNZyEp/54= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=f8EMJTE7; arc=none smtp.client-ip=74.125.224.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="f8EMJTE7" Received: by mail-yx1-f65.google.com with SMTP id 956f58d0204a3-64471fcdef0so4982084d50.1 for ; Sat, 10 Jan 2026 12:33:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768077223; x=1768682023; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=7jyLzqOg69IlsYf0ayGsuQaCXyS420tWXAZAo8eRZCU=; b=f8EMJTE7Htl2MfzKNnDBtfxSGfPDsyFy0hS4CfRu6WrNGKQNKKmRxevmVF85hxtjps tyTnOsHLvhg8OIHb5/dT6jboMoV9h5jQ2GRaJKG4KACT9o7KFv0jdB85JVX502QzFd4m 6cw/E+m9Z71KYbCogVorZbPBfGc/sKDOQQQGbdM2Y/QiGUd/MbmawoQPwOMk/n+4V6uB OQuwnPORoqXnjpOA9BXxzRRQv8hQJT4f2669bcVeCURhHshFl9JLNO/nPH6mrdccyE99 oYCzYRS5UGTs3r3uh9mSui0zNLFcwUFNMKMl2y0alCWFQ621eyuXBGVDf8Fbxolm/1JS kbqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768077223; x=1768682023; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=7jyLzqOg69IlsYf0ayGsuQaCXyS420tWXAZAo8eRZCU=; b=wLsedFvzQDc7q1YWmxt6VJne/hW9oKQ9BTP0GqcL37hFh32D6+uWr1kILp93a3XpTw B4xqSdjiUcEtueMShpaTikWgMqQAAgV4CMc9oPqpUrh79195p+hHBahTzbqzeW9zVQV7 VV6gf7w3/UCe7linHDun/ERtBitFQO3q0kjvc78s5FHiPgesTnduW2EVa+ME9ACS3L0u jAZ1ZDQNJg2DA3SMWJVQ0gAE6KayCA3FGEgBl2Iow/LUIl3iwYRYCHWOrcO8O6LG8E8K p1EDpPa6TNo14thEllhVm9xXK/EHqUYcr4Bg49C9ZDB5WLmceQDukHUXf/fokPjbAnQH gSyg== X-Forwarded-Encrypted: i=1; AJvYcCV09tRTgWbVZy9oUQLDsi1egGsoR4NCoAE4FWS5BMvq+oAR1pZpB81BMAmlS0l0GfWqGdIuZkieahLrqHY=@vger.kernel.org X-Gm-Message-State: AOJu0YxPS8kZUOWV+5sqGWtzWrwVnDzykplEExqg46go/B/H0xnUZMpa 7p6Yg5isttfDmF8TMp2hY9bnFH458b7YIDbFkr3mCwSjk9KhDU552XJC X-Gm-Gg: AY/fxX6iOtW7FILIEvhRcW8HF8OyqDaRosmdRlRe6iuUADBpWtgge31eE0SMv3PTnAj jMB54okTfT67xyA345lb6h4BGvlDuda+X6XaEpUq7DhDpDy3NHIzANb0OcxpgzZ/OwppUw/I+Jy UjuT4Hm1TfHMs3hZAb09VqgJVQazU3KE7DS/v5aPXUfwv0ph6ES+fvwkF42aEXybDNoaDiOZKJ5 meoZiaYMLTB44G2oAqYNNk1Xn8TLBYgDypinAuR1qvQCziu3M3ljCi0XFov1BKoN7FKh50gRHUJ fYB/90ddwzMklwUbfSXdWho5htQeVMlVIkJWcGvx2tWHZk4L7hZvePL5d+YKXcrhvzwCWsHmNRm tsamEVeGF13r5CO56zpgnpA62Q1rbnalU1MwGjcvkb0ggYSiGJ6EZvUZ0VEcYIQ2DzZcTcIwDl6 g2EEOzqktZMZ8CrNVZYOHFxv7i4ykU7hmS X-Google-Smtp-Source: AGHT+IHP0DwyL4sk124vfJjhzyvVlvegUoSf6mDk2LmtqQEqZeGr3f3ysWRLuFO2Hb/poY5LFANeCw== X-Received: by 2002:a05:690c:7349:b0:786:5afa:375c with SMTP id 00721157ae682-790b5857c92mr264232327b3.67.1768077223138; Sat, 10 Jan 2026 12:33:43 -0800 (PST) Received: from newman.cs.purdue.edu ([128.10.127.250]) by smtp.gmail.com with ESMTPSA id 00721157ae682-790aa5762f6sm53927017b3.15.2026.01.10.12.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Jan 2026 12:33:42 -0800 (PST) From: Jiasheng Jiang To: Chris Mason , David Sterba Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, Jiasheng Jiang Subject: [PATCH] btrfs: reset block group size class when reservations are freed Date: Sat, 10 Jan 2026 20:33:40 +0000 Message-Id: <20260110203340.12443-1-jiashengjiangcool@gmail.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Differential analysis of block-group.c shows an inconsistency between btrfs_add_reserved_bytes() and btrfs_free_reserved_bytes(). When space is reserved, btrfs_use_block_group_size_class() is called to set a block group's size class, specializing it for a specific allocation size to reduce fragmentation. However, when these reservations are subsequently freed (e.g., due to an error or transaction abort), btrfs_free_reserved_bytes() fails to perform the corresponding cleanup. This leads to a state leak where a block group remains stuck with a specific size class even if it contains no used or reserved bytes. This stale state causes find_free_extent to unnecessarily skip these block groups for mismatched size requests, leading to suboptimal allocation behavior. Fix this by resetting the size class to BTRFS_BG_SZ_NONE in btrfs_free_reserved_bytes() when the block group becomes completely empty. Signed-off-by: Jiasheng Jiang --- fs/btrfs/block-group.c | 11 +++++++++++ fs/btrfs/block-group.h | 1 + 2 files changed, 12 insertions(+) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 08b14449fabe..1ecac4613a3e 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -3865,6 +3865,10 @@ void btrfs_free_reserved_bytes(struct btrfs_block_gr= oup *cache, u64 num_bytes, =20 spin_lock(&space_info->lock); spin_lock(&cache->lock); + + if (btrfs_block_group_should_use_size_class(cache)) + btrfs_maybe_reset_size_class(cache); + bg_ro =3D cache->ro; cache->reserved -=3D num_bytes; if (is_delalloc) @@ -4717,3 +4721,10 @@ bool btrfs_block_group_should_use_size_class(const s= truct btrfs_block_group *bg) return false; return true; } + +void btrfs_maybe_reset_size_class(struct btrfs_block_group *bg) +{ + lockdep_assert_held(&bg->lock); + if (bg->used =3D=3D 0 && bg->reserved =3D=3D 0) + bg->size_class =3D BTRFS_BG_SZ_NONE; +} diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h index 5f933455118c..7e02db8a8bc6 100644 --- a/fs/btrfs/block-group.h +++ b/fs/btrfs/block-group.h @@ -395,5 +395,6 @@ int btrfs_use_block_group_size_class(struct btrfs_block= _group *bg, enum btrfs_block_group_size_class size_class, bool force_wrong_size_class); bool btrfs_block_group_should_use_size_class(const struct btrfs_block_grou= p *bg); +void btrfs_maybe_reset_size_class(struct btrfs_block_group *bg); =20 #endif /* BTRFS_BLOCK_GROUP_H */ --=20 2.25.1