Skip to contentSkip to conversation

ext4: fix wrong unit of ratelimit_interval_ms sysfs attributes

PATCH Superseded Sang-Heon Jeon 3 participants 3 messages Nov 16 – Nov 18 ext4 file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: fix wrong unit of ratelimit_interval_ms sysfs attributes

The interval of ratelimit_state is in jiffies. But the ratelimit_interval_ms sysfs attributes are read and written without converting the interval, so the values are in milliseconds only if HZ is 1000.

If HZ is 250, the default interval of 5 seconds is read as 1250, and writing 5000 sets the interval to 20 seconds.

So use jiffies_to_msecs() and msecs_to_jiffies() when reading and writing. Also reject values which are too large for the interval.

Fixes efbed4dc5857 ("ext4: add ratelimiting to ext4 messages")
---
 fs/ext4/sysfs.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
fs/ext4/sysfs.c +20 -3
index 923b375e017f..7cc3a2e663f0 100644
@@ -33,6 +33,7 @@ typedef enum {
 	attr_mb_order,
 	attr_feature,
 	attr_pointer_pi,
+	attr_pointer_pi_msecs,
 	attr_pointer_ui,
 	attr_pointer_ul,
 	attr_pointer_u64,
@@ -215,6 +216,11 @@ static struct ext4_attr ext4_attr_##_name = {			\
 #define EXT4_RW_ATTR_SBI_PI(_name,_elname)      \
 	EXT4_ATTR_OFFSET(_name, 0644, pointer_pi, ext4_sb_info, _elname)
 
+#define EXT4_RATELIMIT_INTERVAL_MAX_MS	(INT_MAX / DIV_ROUND_UP(HZ, MSEC_PER_SEC))
+
+#define EXT4_RW_ATTR_SBI_PI_MSECS(_name, _elname)	\
+	EXT4_ATTR_OFFSET(_name, 0644, pointer_pi_msecs, ext4_sb_info, _elname)
+
 #define EXT4_RW_ATTR_SBI_UI(_name,_elname)	\
 	EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
 
@@ -258,11 +264,11 @@ EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
 EXT4_RW_ATTR_SBI_UI(mb_max_linear_groups, s_mb_max_linear_groups);
 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
 EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
-EXT4_RW_ATTR_SBI_PI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
+EXT4_RW_ATTR_SBI_PI_MSECS(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
 EXT4_RW_ATTR_SBI_PI(err_ratelimit_burst, s_err_ratelimit_state.burst);
-EXT4_RW_ATTR_SBI_PI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
+EXT4_RW_ATTR_SBI_PI_MSECS(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
 EXT4_RW_ATTR_SBI_PI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
-EXT4_RW_ATTR_SBI_PI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
+EXT4_RW_ATTR_SBI_PI_MSECS(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
 EXT4_RW_ATTR_SBI_PI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
 #ifdef CONFIG_EXT4_DEBUG
 EXT4_RW_ATTR_SBI_UL(simulate_fail, s_simulate_fail);
@@ -434,6 +440,9 @@ static ssize_t ext4_generic_attr_show(struct ext4_attr *a,
 		if (a->attr_ptr == ptr_ext4_super_block_offset)
 			return sysfs_emit(buf, "%u\n", le32_to_cpup(ptr));
 		return sysfs_emit(buf, "%u\n", *((unsigned int *) ptr));
+	case attr_pointer_pi_msecs:
+		return sysfs_emit(buf, "%u\n",
+				  jiffies_to_msecs(*((unsigned int *) ptr)));
 	case attr_pointer_ul:
 	case attr_err_report_sec:
 		return sysfs_emit(buf, "%lu\n", *((unsigned long *) ptr));
@@ -509,6 +518,14 @@ static ssize_t ext4_generic_attr_store(struct ext4_attr *a,
 			return -EINVAL;
 		*((unsigned int *) ptr) = t;
 		return len;
+	case attr_pointer_pi_msecs:
+		ret = kstrtouint(skip_spaces(buf), 0, &t);
+		if (ret)
+			return ret;
+		if (t > EXT4_RATELIMIT_INTERVAL_MAX_MS)
+			return -EINVAL;
+		*((unsigned int *) ptr) = msecs_to_jiffies(t);
+		return len;
 	case attr_pointer_ui:
 		ret = kstrtouint(skip_spaces(buf), 0, &t);
 		if (ret)

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

The interval of ratelimit_state is in jiffies. But the ratelimit_interval_ms sysfs attributes are read and written without converting the interval, so the values are in milliseconds only if HZ is 1000.

Quoted 8 lines

If HZ is 250, the default interval of 5 seconds is read as 1250, and writing 5000 sets the interval to 20 seconds.

So use jiffies_to_msecs() and msecs_to_jiffies() when reading and writing. Also reject values which are too large for the interval.

Fixes efbed4dc5857 ("ext4: add ratelimiting to ext4 messages")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Sang-Heon Jeon
Quoted 30 lines · Sang-Heon Jeon

The interval of ratelimit_state is in jiffies. But the ratelimit_interval_ms sysfs attributes are read and written without converting the interval, so the values are in milliseconds only if HZ is 1000.

If HZ is 250, the default interval of 5 seconds is read as 1250, and writing 5000 sets the interval to 20 seconds.

So use jiffies_to_msecs() and msecs_to_jiffies() when reading and writing. Also reject values which are too large for the interval.

Fixes efbed4dc5857 ("ext4: add ratelimiting to ext4 messages")
---
 fs/ext4/sysfs.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
fs/ext4/sysfs.c +1 -0
index 923b375e017f..7cc3a2e663f0 100644
@@ -33,6 +33,7 @@ typedef enum {
 	attr_mb_order,
 	attr_feature,
 	attr_pointer_pi,
+	attr_pointer_pi_msecs,
 	attr_pointer_ui,
 	attr_pointer_ul,
 	attr_pointer_u64,

fs/ext4/sysfs.c static struct ext4_attr ext4_attr_##_name = { \ · line 219

Quoted 4 lines
fs/ext4/sysfs.c +1 -0
@@ -215,6 +216,11 @@ static struct ext4_attr ext4_attr_##_name = {			\
 #define EXT4_RW_ATTR_SBI_PI(_name,_elname)      \
 	EXT4_ATTR_OFFSET(_name, 0644, pointer_pi, ext4_sb_info, _elname)
 
+#define EXT4_RATELIMIT_INTERVAL_MAX_MS	(INT_MAX / DIV_ROUND_UP(HZ, MSEC_PER_SEC))

I was staring at this for a while before I understood what's going on here. Please add here a comment like:
/* Limit value so that we fit into int after a conversion to jiffies */

Otherwise the patch looks good. Feel free to add:

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR