From nobody Sun May 24 19:34:52 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8BC652E06E6; Fri, 22 May 2026 03:59:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779422402; cv=none; b=d6SH6YA5NwaSrGkJvE28nvTWnhhzBRXpcglhILtiU5wDOx7KBaxIJtwkaJk2yMXaPt6Qw7wGBvis+72JRP4EZKadU8VZzEPTo25PXQ4DN4X9qjm69zd081Z6JoRjXAgbKYEl9YDz63Yqnx9+xrOJ0hgVnLjuicUVxQaXy9p8wLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779422402; c=relaxed/simple; bh=J//TQ/8P8eQmnsdm5WQGNvahdWrPBxYth+MOpI61WxA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RgjmH9jdeBWmowz/pYIwd9IoH8akizGAZZpcB70huBBrPWtNxVmTdMXfc/BIiN29w5Df7zJzQMCtaALxa/Ufm2GSqd/GRlG0NAX6ONSakCqORkFRHnUjcKbhUJp5X1aRtXhO9S3nO298wOSsbclr+oil69WFmcVDTNr1RbYCrbk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=W2nIMeOz; arc=none smtp.client-ip=220.197.31.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="W2nIMeOz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=va FDWIDrARSeOO2/BdaMZMml4ig3Sw7shfp2cqf4rV4=; b=W2nIMeOzK4hrldWmc7 Ao1x+1ztc9ywjUb+qJCI9K6/t4bu5+f+2Gl9GM1eqFZvKiu78BSg1BiVIH4SctN2 ptd8jXtZm8KfSTgxXNwkTnknHaIYSeY3rv1kcsSM9pxaXPTOV3XNQdqkOP+d+HXo 12EFSVW/e6EUs+28i7HiEwSKo= Received: from liubaolin-ThinkPad-E15-Gen-2 (unknown []) by gzga-smtp-mtada-g0-1 (Coremail) with SMTP id _____wDX3wWO1A9qtnXKCg--.18860S3; Fri, 22 May 2026 11:59:14 +0800 (CST) From: Baolin Liu To: tytso@mit.edu, adilger.kernel@dilger.ca, libaokun@linux.alibaba.com, jack@suse.cz, ojaswin@linux.ibm.com, ritesh.list@gmail.com, yi.zhang@huawei.com, corbet@lwn.net, skhan@linuxfoundation.org Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, Baolin Liu , Andreas Dilger Subject: [PATCH v5 v5 1/3] ext4: add blocks_allocated to mb_stats output Date: Fri, 22 May 2026 11:59:03 +0800 Message-ID: <20260522035905.1145743-2-liubaolin12138@163.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260522035905.1145743-1-liubaolin12138@163.com> References: <20260522035905.1145743-1-liubaolin12138@163.com> 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 X-CM-TRANSID: _____wDX3wWO1A9qtnXKCg--.18860S3 X-Coremail-Antispam: 1Uf129KBjvdXoWrtr1Utr4xurW8GFykAryxKrg_yoWkGrX_Ka 40yr18ur45Jr4xuF18AF4S9390k3W0vFn8W3sxJrs3W3WDW3y8Ka98Ar4rXw1xGr42krW5 A3Z3WrW8JryvvjkaLaAFLSUrUUUUjb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IUbCzutUUUUU== X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6RK3gmoP1JIibQAA3i Content-Type: text/plain; charset="utf-8" From: Baolin Liu Add blocks_allocated to /proc/fs/ext4//mb_stats so that the reported statistics match the mballoc summary printed at unmount time. Reviewed-by: Ojaswin Mujoo Reviewed-by: Andreas Dilger Reviewed-by: Ritesh Harjani (IBM) Reviewed-by: Zhang Yi Signed-off-by: Baolin Liu --- fs/ext4/mballoc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index bb58eafb87bc..d36b0f7b5d7d 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -3217,6 +3217,8 @@ int ext4_seq_mb_stats_show(struct seq_file *seq, void= *offset) "\tTo enable, please write \"1\" to sysfs file mb_stats.\n"); return 0; } + seq_printf(seq, "\tblocks_allocated: %u\n", + atomic_read(&sbi->s_bal_allocated)); seq_printf(seq, "\treqs: %u\n", atomic_read(&sbi->s_bal_reqs)); seq_printf(seq, "\tsuccess: %u\n", atomic_read(&sbi->s_bal_success)); =20 --=20 2.51.0 From nobody Sun May 24 19:34:52 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B1543563E8; Fri, 22 May 2026 04:00:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779422412; cv=none; b=HlJ0SnhvTjrYMYnztgnF35+v9bCinsKA7Xa14NdlPYQGTcZXZvfadney8s8G4cvStKcBrX+wWz0p34ygjKwLVvlMdoQ/s3Q3UjrBFEn/5MhKlzunRJzAz1ss/f+aQ4deOHEgLllvn02DloXyne4gfEbkFvFJc8ybI3fmaq0ONhM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779422412; c=relaxed/simple; bh=ys/WFercO1BG0GEj8pgLXktAqsELqBX339nbqtz3Hl8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gUrUCtZtnBOYiqz82nmcGHKRRtPccrv+fZiZa0i20rLYQCiwnv0lLSVqUWq80tyFVxF27Y6B/Irp5t0C7c9HWKHR2t/R+Z7GVtIAZU4sIcFoiP9JLHX+IiRR+qYSjFhQRzldL/yD77Enr9KZfEbQ6Xj4qbuKO2K3Pqc5cO/l2MM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=nF6UpPhY; arc=none smtp.client-ip=220.197.31.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="nF6UpPhY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=SM YDAw6Simc31ovebDOSR++gQdumOKs9k/YiBIZ8Vpg=; b=nF6UpPhYTmVw4RqCSo W8ihopEJJfCB63FkIeDm5BGOkUG3QuQyp5+ZC5NoIjxfyojlBxkoR55H82C9xPV+ EAEqUPiXXL4kOrWDyMMVy7pd5pddDAb7DZSuIwjPYxsUsUt6hJCX6wR8eUtbRIRS /YhrVrHjtoz2w5fZHZPWFhTrg= Received: from liubaolin-ThinkPad-E15-Gen-2 (unknown []) by gzga-smtp-mtada-g0-1 (Coremail) with SMTP id _____wDX3wWO1A9qtnXKCg--.18860S4; Fri, 22 May 2026 11:59:17 +0800 (CST) From: Baolin Liu To: tytso@mit.edu, adilger.kernel@dilger.ca, libaokun@linux.alibaba.com, jack@suse.cz, ojaswin@linux.ibm.com, ritesh.list@gmail.com, yi.zhang@huawei.com, corbet@lwn.net, skhan@linuxfoundation.org Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, Baolin Liu Subject: [PATCH v5 v5 2/3] ext4: use READ_ONCE/WRITE_ONCE for s_mb_stats Date: Fri, 22 May 2026 11:59:04 +0800 Message-ID: <20260522035905.1145743-3-liubaolin12138@163.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260522035905.1145743-1-liubaolin12138@163.com> References: <20260522035905.1145743-1-liubaolin12138@163.com> 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 X-CM-TRANSID: _____wDX3wWO1A9qtnXKCg--.18860S4 X-Coremail-Antispam: 1Uf129KBjvJXoW3XFyDJrykGF1DAr18CFW7Arb_yoW3Jr4fpr 47ZFyUAr47Zw1xCFsrWan8W3ZYk3yIkFy7JrWfu34F9FyagFyayryaqFyjyF9rArWDCFsY v3yqyrWDGrWIka7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UMuWJUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6RW4g2oP1JUi5AAA3l Content-Type: text/plain; charset="utf-8" From: Baolin Liu Use READ_ONCE()/WRITE_ONCE() for concurrent accesses to s_mb_stats. Signed-off-by: Baolin Liu --- fs/ext4/mballoc.c | 24 ++++++++++++------------ fs/ext4/sysfs.c | 25 ++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index d36b0f7b5d7d..fed6d854877b 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -924,7 +924,7 @@ static int ext4_mb_scan_groups_xa_range(struct ext4_all= ocation_context *ac, xa_for_each_range(xa, group, grp, start, end - 1) { int err; =20 - if (sbi->s_mb_stats) + if (READ_ONCE(sbi->s_mb_stats)) atomic64_inc(&sbi->s_bal_cX_groups_considered[cr]); =20 err =3D ext4_mb_scan_group(ac, grp->bb_group); @@ -980,7 +980,7 @@ static int ext4_mb_scan_groups_p2_aligned(struct ext4_a= llocation_context *ac, goto wrap_around; } =20 - if (sbi->s_mb_stats) + if (READ_ONCE(sbi->s_mb_stats)) atomic64_inc(&sbi->s_bal_cX_failed[ac->ac_criteria]); =20 /* Increment cr and search again if no group is found */ @@ -1031,7 +1031,7 @@ static int ext4_mb_scan_groups_goal_fast(struct ext4_= allocation_context *ac, goto wrap_around; } =20 - if (sbi->s_mb_stats) + if (READ_ONCE(sbi->s_mb_stats)) atomic64_inc(&sbi->s_bal_cX_failed[ac->ac_criteria]); /* * CR_BEST_AVAIL_LEN works based on the concept that we have @@ -1135,7 +1135,7 @@ static int ext4_mb_scan_groups_best_avail(struct ext4= _allocation_context *ac, =20 /* Reset goal length to original goal length before falling into CR_GOAL_= LEN_SLOW */ ac->ac_g_ex.fe_len =3D ac->ac_orig_goal_len; - if (sbi->s_mb_stats) + if (READ_ONCE(sbi->s_mb_stats)) atomic64_inc(&sbi->s_bal_cX_failed[ac->ac_criteria]); ac->ac_criteria =3D CR_GOAL_LEN_SLOW; =20 @@ -1184,7 +1184,7 @@ static int ext4_mb_scan_groups_linear(struct ext4_all= ocation_context *ac, ac->ac_criteria++; =20 /* Processed all groups and haven't found blocks */ - if (sbi->s_mb_stats && i =3D=3D ngroups) + if (READ_ONCE(sbi->s_mb_stats) && i =3D=3D ngroups) atomic64_inc(&sbi->s_bal_cX_failed[cr]); =20 return 0; @@ -2541,7 +2541,7 @@ void ext4_mb_simple_scan_group(struct ext4_allocation= _context *ac, =20 BUG_ON(ac->ac_f_ex.fe_len !=3D ac->ac_g_ex.fe_len); =20 - if (EXT4_SB(sb)->s_mb_stats) + if (READ_ONCE(EXT4_SB(sb)->s_mb_stats)) atomic_inc(&EXT4_SB(sb)->s_bal_2orders); =20 break; @@ -2786,7 +2786,7 @@ static int ext4_mb_good_group_nolock(struct ext4_allo= cation_context *ac, =20 if (!grp) return -EFSCORRUPTED; - if (sbi->s_mb_stats) + if (READ_ONCE(sbi->s_mb_stats)) atomic64_inc(&sbi->s_bal_cX_groups_considered[ac->ac_criteria]); if (should_lock) { ext4_lock_group(sb, group); @@ -3097,7 +3097,7 @@ ext4_mb_regular_allocator(struct ext4_allocation_cont= ext *ac) } } =20 - if (sbi->s_mb_stats && ac->ac_status =3D=3D AC_STATUS_FOUND) { + if (READ_ONCE(sbi->s_mb_stats) && ac->ac_status =3D=3D AC_STATUS_FOUND) { atomic64_inc(&sbi->s_bal_cX_hits[ac->ac_criteria]); if (ac->ac_flags & EXT4_MB_STREAM_ALLOC && ac->ac_b_ex.fe_group =3D=3D ac->ac_g_ex.fe_group) @@ -3210,7 +3210,7 @@ int ext4_seq_mb_stats_show(struct seq_file *seq, void= *offset) struct ext4_sb_info *sbi =3D EXT4_SB(sb); =20 seq_puts(seq, "mballoc:\n"); - if (!sbi->s_mb_stats) { + if (!READ_ONCE(sbi->s_mb_stats)) { seq_puts(seq, "\tmb stats collection turned off.\n"); seq_puts( seq, @@ -3787,7 +3787,7 @@ int ext4_mb_init(struct super_block *sb) =20 sbi->s_mb_max_to_scan =3D MB_DEFAULT_MAX_TO_SCAN; sbi->s_mb_min_to_scan =3D MB_DEFAULT_MIN_TO_SCAN; - sbi->s_mb_stats =3D MB_DEFAULT_STATS; + WRITE_ONCE(sbi->s_mb_stats, MB_DEFAULT_STATS); sbi->s_mb_stream_request =3D MB_DEFAULT_STREAM_THRESHOLD; sbi->s_mb_order2_reqs =3D MB_DEFAULT_ORDER2_REQS; sbi->s_mb_best_avail_max_trim_order =3D MB_DEFAULT_BEST_AVAIL_TRIM_ORDER; @@ -3929,7 +3929,7 @@ void ext4_mb_release(struct super_block *sb) kfree(sbi->s_mb_offsets); kfree(sbi->s_mb_maxs); iput(sbi->s_buddy_cache); - if (sbi->s_mb_stats) { + if (READ_ONCE(sbi->s_mb_stats)) { ext4_msg(sb, KERN_INFO, "mballoc: %u blocks %u reqs (%u success)", atomic_read(&sbi->s_bal_allocated), @@ -4694,7 +4694,7 @@ static void ext4_mb_collect_stats(struct ext4_allocat= ion_context *ac) { struct ext4_sb_info *sbi =3D EXT4_SB(ac->ac_sb); =20 - if (sbi->s_mb_stats && ac->ac_g_ex.fe_len >=3D 1) { + if (READ_ONCE(sbi->s_mb_stats) && ac->ac_g_ex.fe_len >=3D 1) { atomic_inc(&sbi->s_bal_reqs); atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated); if (ac->ac_b_ex.fe_len >=3D ac->ac_o_ex.fe_len) diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c index afe12bcc1603..47e06c32c6fb 100644 --- a/fs/ext4/sysfs.c +++ b/fs/ext4/sysfs.c @@ -41,6 +41,7 @@ typedef enum { attr_pointer_atomic, attr_journal_task, attr_err_report_sec, + attr_mb_stats, } attr_id_t; =20 typedef enum { @@ -241,6 +242,7 @@ EXT4_ATTR_FUNC(session_write_kbytes, 0444); EXT4_ATTR_FUNC(lifetime_write_kbytes, 0444); EXT4_ATTR_FUNC(reserved_clusters, 0644); EXT4_ATTR_FUNC(sra_exceeded_retry_limit, 0444); +EXT4_ATTR_FUNC(mb_stats, 0644); =20 EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead, ext4_sb_info, s_inode_readahead_blks); @@ -250,7 +252,6 @@ EXT4_ATTR_OFFSET(mb_best_avail_max_trim_order, 0644, mb= _order, ext4_sb_info, s_mb_best_avail_max_trim_order); EXT4_ATTR_OFFSET(err_report_sec, 0644, err_report_sec, ext4_sb_info, s_err= _report_sec); EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal); -EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats); EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan); EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan); EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs); @@ -451,6 +452,24 @@ static ssize_t ext4_generic_attr_show(struct ext4_attr= *a, return 0; } =20 +static ssize_t mb_stats_show(struct ext4_sb_info *sbi, char *buf) +{ + return sysfs_emit(buf, "%u\n", READ_ONCE(sbi->s_mb_stats)); +} + +static ssize_t mb_stats_store(struct ext4_sb_info *sbi, + const char *buf, size_t len) +{ + unsigned int t; + int ret; + + ret =3D kstrtouint(skip_spaces(buf), 0, &t); + if (ret) + return ret; + WRITE_ONCE(sbi->s_mb_stats, t); + return len; +} + static ssize_t ext4_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) { @@ -475,6 +494,8 @@ static ssize_t ext4_attr_show(struct kobject *kobj, return sysfs_emit(buf, "%llu\n", (unsigned long long) percpu_counter_sum(&sbi->s_sra_exceeded_retry_limit)); + case attr_mb_stats: + return mb_stats_show(sbi, buf); case attr_feature: return sysfs_emit(buf, "supported\n"); case attr_first_error_time: @@ -559,6 +580,8 @@ static ssize_t ext4_attr_store(struct kobject *kobj, return inode_readahead_blks_store(sbi, buf, len); case attr_trigger_test_error: return trigger_test_error(sbi, buf, len); + case attr_mb_stats: + return mb_stats_store(sbi, buf, len); case attr_err_report_sec: return err_report_sec_store(sbi, buf, len); default: --=20 2.51.0 From nobody Sun May 24 19:34:52 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADE8F28F948; Fri, 22 May 2026 04:00:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779422424; cv=none; b=Bf2YjcyM9jsPhx00SiK+1D9mod/AjMxQWM5F/antwiPJ2CpOJYOSLT+QKQIwTgOob06+xRNFLvxMm08AAa3kV0yt94qWFx6iphweOSpndGC+oGCmOItZ07CN5yt2DDzQYOIEIT9NT/nJxqNfXMZ8oNBbn8vR1DxWMZgVvhKt/88= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779422424; c=relaxed/simple; bh=usrhubYvV6DbP7IaKA+XoWkdgfWfWsU/BecHNkIWpHs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I/cyKpgp5HoqEz/We8fNfZXxf+uifEox5W9Lk9wOqldhwWGIdHEZFHdf6qruDobbGSWkODUnal+xDZ4E070Xj2BmAaHG1NeCls5GfnDOAJ/BzoIyjWTEhxvo7Ol10Xt6km9Ud6UbuRZfNUAGfP8ejR+ko7L4PEfr0r5w/js8RXg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=Ez4x85CZ; arc=none smtp.client-ip=117.135.210.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="Ez4x85CZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=hD L4307WehZFu9ngGDPsnnlOD6nltomLHMa43Oq2eJE=; b=Ez4x85CZdwYuy4x8M3 SXElqY63zbz4M8I9lFrlc/nStMaT7aqmBPg2Qc+Q1yDwFF9e9wRBwZ62RwBJjHwD H1OEhLF5Gwk2ZIoe8ECxG3bkFNNeBNj0nW/H4givj740aiSF6DGTxSRPDLan80oi QmNegyEl0LGNiRooPltxzG8gQ= Received: from liubaolin-ThinkPad-E15-Gen-2 (unknown []) by gzga-smtp-mtada-g0-1 (Coremail) with SMTP id _____wDX3wWO1A9qtnXKCg--.18860S5; Fri, 22 May 2026 11:59:19 +0800 (CST) From: Baolin Liu To: tytso@mit.edu, adilger.kernel@dilger.ca, libaokun@linux.alibaba.com, jack@suse.cz, ojaswin@linux.ibm.com, ritesh.list@gmail.com, yi.zhang@huawei.com, corbet@lwn.net, skhan@linuxfoundation.org Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, Baolin Liu , Andreas Dilger Subject: [PATCH v5 v5 3/3] ext4: allow controlling mballoc stats through proc mb_stats Date: Fri, 22 May 2026 11:59:05 +0800 Message-ID: <20260522035905.1145743-4-liubaolin12138@163.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260522035905.1145743-1-liubaolin12138@163.com> References: <20260522035905.1145743-1-liubaolin12138@163.com> 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 X-CM-TRANSID: _____wDX3wWO1A9qtnXKCg--.18860S5 X-Coremail-Antispam: 1Uf129KBjvJXoW3Gw1kXFy3Ar4rXw13uw47XFb_yoW3Kr45pF nxZa4xGr1xXa4UCws7Gr4xXw13C3WxC347tr1I9340gr9IvryFyF1ftrW0yFy5GrWUAa95 XF4j9ryUGr42k37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jgfHUUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6Ri5hGoP1JgjQQAA3H Content-Type: text/plain; charset="utf-8" From: Baolin Liu Make /proc/fs/ext4//mb_stats writable. Writing 0 disables mballoc statistics collection, writing 1 enables it, and writing -1 clears the current statistics before enabling collection. Update the ext4 documentation for proc mb_stats, document that the sysfs mb_stats entry is deprecated, and point proc.rst to Documentation/admin-guide/ext4.rst for ext4-specific /proc entries. Reviewed-by: Ojaswin Mujoo Reviewed-by: Andreas Dilger Reviewed-by: Ritesh Harjani (IBM) Reviewed-by: Zhang Yi Reviewed-by: Baokun Li Reviewed-by: Ted Tso Signed-off-by: Baolin Liu --- Documentation/ABI/testing/sysfs-fs-ext4 | 3 +- Documentation/admin-guide/ext4.rst | 9 ++++- Documentation/filesystems/proc.rst | 13 +------ fs/ext4/ext4.h | 1 + fs/ext4/mballoc.c | 31 +++++++++++++++- fs/ext4/sysfs.c | 48 +++++++++++++++++++++++-- 6 files changed, 88 insertions(+), 17 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-fs-ext4 b/Documentation/ABI/te= sting/sysfs-fs-ext4 index 2edd0a6672d3..7bf06c533343 100644 --- a/Documentation/ABI/testing/sysfs-fs-ext4 +++ b/Documentation/ABI/testing/sysfs-fs-ext4 @@ -5,7 +5,8 @@ Description: Controls whether the multiblock allocator should collect statistics, which are shown during the unmount. 1 means to collect statistics, 0 means not to collect - statistics + statistics. This sysfs entry is deprecated, and users + should prefer /proc/fs/ext4//mb_stats. =20 What: /sys/fs/ext4//mb_group_prealloc Date: March 2008 diff --git a/Documentation/admin-guide/ext4.rst b/Documentation/admin-guide= /ext4.rst index ac0c709ea9e7..ca76e981b2aa 100644 --- a/Documentation/admin-guide/ext4.rst +++ b/Documentation/admin-guide/ext4.rst @@ -436,6 +436,12 @@ Files in /proc/fs/ext4/ mb_groups details of multiblock allocator buddy cache of free blocks =20 + mb_stats + reports runtime statistics from the multiblock allocator + (mballoc). Writing 0 disables statistics collection, writing + 1 enables statistics collection, and writing -1 clears the + current statistics and enables statistics collection. + /sys entries =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 @@ -493,7 +499,8 @@ Files in /sys/fs/ext4/: mb_stats Controls whether the multiblock allocator should collect statistic= s, which are shown during the unmount. 1 means to collect statistics,= 0 - means not to collect statistics. + means not to collect statistics. This sysfs entry is deprecated, a= nd + users should prefer /proc/fs/ext4//mb_stats. =20 mb_stream_req Files which have fewer blocks than this tunable parameter will have diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems= /proc.rst index b0c0d1b45b99..dd487004b862 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -1623,18 +1623,7 @@ softirq. 1.8 Ext4 file system parameters ------------------------------- =20 -Information about mounted ext4 file systems can be found in -/proc/fs/ext4. Each mounted filesystem will have a directory in -/proc/fs/ext4 based on its device name (i.e., /proc/fs/ext4/hdc or -/proc/fs/ext4/sda9 or /proc/fs/ext4/dm-0). The files in each per-device -directory are shown in Table 1-12, below. - -.. table:: Table 1-12: Files in /proc/fs/ext4/ - - =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D - File Content - mb_groups details of multiblock allocator buddy cache of free blocks - =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +See Documentation/admin-guide/ext4.rst for ext4-specific /proc entries. =20 1.9 /proc/consoles ------------------- diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index df96bcd53a59..483438d5742b 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -2995,6 +2995,7 @@ int ext4_fc_record_regions(struct super_block *sb, in= t ino, extern const struct seq_operations ext4_mb_seq_groups_ops; extern const struct seq_operations ext4_mb_seq_structs_summary_ops; extern int ext4_seq_mb_stats_show(struct seq_file *seq, void *offset); +extern void ext4_mb_stats_clear(struct ext4_sb_info *sbi); extern int ext4_mb_init(struct super_block *); extern void ext4_mb_release(struct super_block *); extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *, diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index fed6d854877b..3f6454ada587 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -3214,7 +3214,7 @@ int ext4_seq_mb_stats_show(struct seq_file *seq, void= *offset) seq_puts(seq, "\tmb stats collection turned off.\n"); seq_puts( seq, - "\tTo enable, please write \"1\" to sysfs file mb_stats.\n"); + "\tTo enable, please write \"1\" to proc file mb_stats.\n"); return 0; } seq_printf(seq, "\tblocks_allocated: %u\n", @@ -4723,6 +4723,35 @@ static void ext4_mb_collect_stats(struct ext4_alloca= tion_context *ac) trace_ext4_mballoc_prealloc(ac); } =20 +void ext4_mb_stats_clear(struct ext4_sb_info *sbi) +{ + int i; + + atomic_set(&sbi->s_bal_reqs, 0); + atomic_set(&sbi->s_bal_success, 0); + atomic_set(&sbi->s_bal_allocated, 0); + atomic_set(&sbi->s_bal_groups_scanned, 0); + + for (i =3D 0; i < EXT4_MB_NUM_CRS; i++) { + atomic64_set(&sbi->s_bal_cX_hits[i], 0); + atomic64_set(&sbi->s_bal_cX_groups_considered[i], 0); + atomic_set(&sbi->s_bal_cX_ex_scanned[i], 0); + atomic64_set(&sbi->s_bal_cX_failed[i], 0); + } + + atomic_set(&sbi->s_bal_ex_scanned, 0); + atomic_set(&sbi->s_bal_goals, 0); + atomic_set(&sbi->s_bal_stream_goals, 0); + atomic_set(&sbi->s_bal_len_goals, 0); + atomic_set(&sbi->s_bal_2orders, 0); + atomic_set(&sbi->s_bal_breaks, 0); + atomic_set(&sbi->s_mb_lost_chunks, 0); + atomic_set(&sbi->s_mb_buddies_generated, 0); + atomic64_set(&sbi->s_mb_generation_time, 0); + atomic_set(&sbi->s_mb_preallocated, 0); + atomic_set(&sbi->s_mb_discarded, 0); +} + /* * Called on failure; free up any blocks from the inode PA for this * context. We don't need this for MB_GROUP_PA because we only change diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c index 47e06c32c6fb..76e6c346231a 100644 --- a/fs/ext4/sysfs.c +++ b/fs/ext4/sysfs.c @@ -53,6 +53,50 @@ typedef enum { static const char proc_dirname[] =3D "fs/ext4"; static struct proc_dir_entry *ext4_proc_root; =20 +static int ext4_mb_stats_open(struct inode *inode, struct file *file) +{ + return single_open(file, ext4_seq_mb_stats_show, pde_data(inode)); +} + +static ssize_t ext4_mb_stats_write(struct file *file, const char __user *b= uf, + size_t count, loff_t *ppos) +{ + struct super_block *sb =3D pde_data(file_inode(file)); + struct ext4_sb_info *sbi =3D EXT4_SB(sb); + int val; + int ret; + + ret =3D kstrtoint_from_user(buf, count, 0, &val); + if (ret) + return ret; + + switch (val) { + case -1: + WRITE_ONCE(sbi->s_mb_stats, 0); + ext4_mb_stats_clear(sbi); + WRITE_ONCE(sbi->s_mb_stats, 1); + break; + case 1: + WRITE_ONCE(sbi->s_mb_stats, 1); + break; + case 0: + WRITE_ONCE(sbi->s_mb_stats, 0); + break; + default: + return -EINVAL; + } + + return count; +} + +static const struct proc_ops ext4_mb_stats_proc_ops =3D { + .proc_open =3D ext4_mb_stats_open, + .proc_read =3D seq_read, + .proc_lseek =3D seq_lseek, + .proc_release =3D single_release, + .proc_write =3D ext4_mb_stats_write, +}; + struct ext4_attr { struct attribute attr; short attr_id; @@ -666,8 +710,8 @@ int ext4_register_sysfs(struct super_block *sb) ext4_fc_info_show, sb); proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc, &ext4_mb_seq_groups_ops, sb); - proc_create_single_data("mb_stats", 0444, sbi->s_proc, - ext4_seq_mb_stats_show, sb); + proc_create_data("mb_stats", 0644, sbi->s_proc, + &ext4_mb_stats_proc_ops, sb); proc_create_seq_data("mb_structs_summary", 0444, sbi->s_proc, &ext4_mb_seq_structs_summary_ops, sb); } --=20 2.51.0