Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling
Date: Wed, 11 Nov 2026 23:46:58 -0500	[thread overview]
Message-ID: <20261112044712.1577296-10-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>

err_report_sec_store() has several problems:

 - Writing 0 calls timer_delete_sync() and then jumps past the
   assignment, so s_err_report_sec keeps its old value.  Reading the
   attribute still returns the old interval, writing the old interval
   back to re-enable reporting is treated as a no-op, and the periodic
   report is restarted by the next error even though it was disabled.

 - Changing the interval from 0 to a non-zero value calls
   timer_setup() on s_err_report.  The timer is already initialized at
   mount time, and it may still be pending (e.g. armed by
   ext4_update_super() or the timer callback racing with the store), in
   which case re-initializing it corrupts the timer list.

 - Any non-zero write arms the timer even if no errors have ever been
   recorded, so print_daily_error_info() wakes up and re-arms itself
   forever without having anything to report.

 - Concurrent writers are not serialized, so the timer state can end
   up inconsistent with the stored interval.

Rework the function to always store the new value (with WRITE_ONCE(),
since the timer callback and ext4_update_super() read it locklessly),
drop the timer_setup() call, delete the timer when the interval is set
to 0, and otherwise only (re)arm it when errors have been recorded;
ext4_update_super() starts it when the first error is recorded.
Serialize writers with a function-local mutex.  s_error_notify_mutex
cannot be used for this, since ext4_unregister_sysfs() holds it across
kobject_del(), which waits for active sysfs store callbacks to finish.

Also read s_err_report_sec once with READ_ONCE() in
print_daily_error_info(), so the value tested is the value used to
re-arm the timer.

Fixes: d518215c2719 ("ext4: add sysfs attribute err_report_sec to control s_err_report timer")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
 fs/ext4/super.c      |  7 ++++--
 fs/ext4/sysfs-test.c | 55 ++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/sysfs.c      | 21 ++++++++++-------
 3 files changed, 73 insertions(+), 10 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 5f704c43c901..e89f37419266 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -3756,6 +3756,7 @@ void print_daily_error_info(struct timer_list *t)
 	struct ext4_sb_info *sbi = timer_container_of(sbi, t, s_err_report);
 	struct super_block *sb = sbi->s_sb;
 	struct ext4_super_block *es = sbi->s_es;
+	unsigned long err_report_sec;
 
 	if (es->s_error_count)
 		/* fsck newer than v1.41.13 is needed to clean this condition. */
@@ -3792,8 +3793,10 @@ void print_daily_error_info(struct timer_list *t)
 		printk(KERN_CONT "\n");
 	}
 
-	if (sbi->s_err_report_sec)
-		mod_timer(&sbi->s_err_report, jiffies + secs_to_jiffies(sbi->s_err_report_sec));
+	err_report_sec = READ_ONCE(sbi->s_err_report_sec);
+	if (err_report_sec)
+		mod_timer(&sbi->s_err_report,
+			  jiffies + secs_to_jiffies(err_report_sec));
 }
 
 /* Find next suitable group and run ext4_init_inode_table */
diff --git a/fs/ext4/sysfs-test.c b/fs/ext4/sysfs-test.c
index c13785420aa1..4aefcdc4480a 100644
--- a/fs/ext4/sysfs-test.c
+++ b/fs/ext4/sysfs-test.c
@@ -477,6 +477,60 @@ static void test_validated_rw_attrs(struct kunit *test)
 			(ssize_t)-EINVAL);
 }
 
+static void test_err_report_sec_timer(struct kunit *test)
+{
+	struct sysfs_test_ctx *ctx = test->priv;
+	struct ext4_sb_info *sbi = &ctx->sbi;
+	struct ext4_super_block *es = &ctx->es;
+	char *buf;
+
+	buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, buf);
+
+	/* Mount-time default; no errors recorded yet */
+	sbi->s_err_report_sec = 86400;
+	es->s_error_count = 0;
+
+	/* Changing the interval on a clean fs must not arm the timer */
+	KUNIT_EXPECT_EQ(test,
+			ext4_sysfs_attr_store_test(sbi, "err_report_sec",
+						   "3600", 4),
+			(ssize_t)4);
+	KUNIT_EXPECT_EQ(test, sbi->s_err_report_sec, 3600UL);
+	KUNIT_EXPECT_FALSE(test, timer_pending(&sbi->s_err_report));
+
+	/* With recorded errors a new interval (re)arms the timer */
+	es->s_error_count = cpu_to_le32(1);
+	KUNIT_EXPECT_EQ(test,
+			ext4_sysfs_attr_store_test(sbi, "err_report_sec",
+						   "7200", 4),
+			(ssize_t)4);
+	KUNIT_EXPECT_EQ(test, sbi->s_err_report_sec, 7200UL);
+	KUNIT_EXPECT_TRUE(test, timer_pending(&sbi->s_err_report));
+
+	/* Writing 0 disables the timer and is reflected on read */
+	KUNIT_EXPECT_EQ(test,
+			ext4_sysfs_attr_store_test(sbi, "err_report_sec",
+						   "0", 1),
+			(ssize_t)1);
+	KUNIT_EXPECT_EQ(test, sbi->s_err_report_sec, 0UL);
+	KUNIT_EXPECT_FALSE(test, timer_pending(&sbi->s_err_report));
+	KUNIT_EXPECT_GT(test,
+			ext4_sysfs_attr_show_test(sbi, "err_report_sec", buf),
+			(ssize_t)0);
+	KUNIT_EXPECT_STREQ(test, buf, "0\n");
+
+	/* Re-enabling with the old value re-arms the existing timer */
+	KUNIT_EXPECT_EQ(test,
+			ext4_sysfs_attr_store_test(sbi, "err_report_sec",
+						   "7200", 4),
+			(ssize_t)4);
+	KUNIT_EXPECT_EQ(test, sbi->s_err_report_sec, 7200UL);
+	KUNIT_EXPECT_TRUE(test, timer_pending(&sbi->s_err_report));
+	KUNIT_EXPECT_TRUE(test,
+			  sbi->s_err_report.function == sysfs_test_err_timer);
+}
+
 static void test_computed_ro_and_error_time_attrs(struct kunit *test)
 {
 	struct sysfs_test_ctx *ctx = test->priv;
@@ -635,6 +689,7 @@ static void test_sysfs_register_notify_and_unregister(struct kunit *test)
 static struct kunit_case ext4_sysfs_test_cases[] = {
 	KUNIT_CASE(test_generic_pointer_attrs),
 	KUNIT_CASE(test_validated_rw_attrs),
+	KUNIT_CASE(test_err_report_sec_timer),
 	KUNIT_CASE(test_computed_ro_and_error_time_attrs),
 	KUNIT_CASE(test_feature_attrs_and_trigger_error),
 	KUNIT_CASE(test_sysfs_register_notify_and_unregister),
diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
index faa76f021099..0168ea6faac7 100644
--- a/fs/ext4/sysfs.c
+++ b/fs/ext4/sysfs.c
@@ -134,6 +134,7 @@ static ssize_t trigger_test_error(struct ext4_sb_info *sbi,
 static ssize_t err_report_sec_store(struct ext4_sb_info *sbi,
 				    const char *buf, size_t count)
 {
+	static DEFINE_MUTEX(err_report_sec_mutex);
 	unsigned long t;
 	int ret;
 
@@ -145,19 +146,23 @@ static ssize_t err_report_sec_store(struct ext4_sb_info *sbi,
 	if (t > (365*24*60*60))
 		return -EINVAL;
 
+	mutex_lock(&err_report_sec_mutex);
 	if (sbi->s_err_report_sec == t)		/*nothing to do*/
 		goto out;
-	else if (!sbi->s_err_report_sec && t) {
-		timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
-	} else if (sbi->s_err_report_sec && !t) {
-		timer_delete_sync(&sbi->s_err_report);
-		goto out;
-	}
 
-	sbi->s_err_report_sec = t;
-	mod_timer(&sbi->s_err_report, jiffies + secs_to_jiffies(sbi->s_err_report_sec));
+	WRITE_ONCE(sbi->s_err_report_sec, t);
+	/*
+	 * The timer is initialized at mount time.  Only (re)arm it if errors
+	 * have been recorded; otherwise ext4_update_super() will start it
+	 * when the first error is written to the superblock.
+	 */
+	if (!t)
+		timer_delete_sync(&sbi->s_err_report);
+	else if (sbi->s_es->s_error_count)
+		mod_timer(&sbi->s_err_report, jiffies + secs_to_jiffies(t));
 
 out:
+	mutex_unlock(&err_report_sec_mutex);
 	return count;
 }
 
-- 
2.56.0.542.g3ef89b7020-goog


  parent reply	other threads:[~2026-11-12  4:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12  4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12  4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12  4:46 ` [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM Theodore Ts'o
2026-11-12  4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12  4:46 ` [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs Theodore Ts'o
2026-11-12  4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12  4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12  4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12  4:46 ` Theodore Ts'o [this message]
2026-11-12  4:46 ` [PATCH 10/23] ext4: fix inode table accounting in count_overhead() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12  4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12  4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12  4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12  4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12  4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12  4:47 ` [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load Theodore Ts'o
2026-11-12  4:47 ` [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges Theodore Ts'o

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112044712.1577296-10-tytso@mit.edu \
    --to=tytso@mit.edu \
    --cc=linux-ext4@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox