debugfs: Add simple min/max "files" to debugfs to fix sched debug code.
Subsystems (4)
v2 cover debugfs: Add simple min/max "files" to debugfs to fix sched debug code.
v2:
Apologies. I sent this the first time without including lkml.
v1:
This originally started as an attempt to solve a divide by zero issue in sched debug code that was introduced when a sysctl value with non-zero checking was moved to a simple u32 debugfs file. In looking at ways to solve this, it was mentioned I should look at providing general debugfs files with min/max checking.
One problem was that a check for greater than zero for say a u8 succeeds for a number like 256 (but stores a zero anyway) as the upper bits that don't fit into storage are silently dropped. Therefore values greater than the storage capacity must also fail. Getting an error when what the user wrote is not what was actually stored, seems like a useful requirement for the other simple files and so I moved the check into there.
To enable easy testing, a test module and test script are provided which can validate the new functions as well as check the new limits on the older functions. This was stuck under selftests, but it is not currently tied into the testing infrastructure.
Documentation/filesystems/debugfs.rst | 47 +++++++++++++++-- fs/debugfs/file.c | 195 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/debugfs.h | 60 +++++++++++++++++++++ kernel/sched/debug.c | 9 +++- tools/testing/selftests/debugfs/Makefile | 15 ++++++ tools/testing/selftests/debugfs/minmax_test.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/testing/selftests/debugfs/test_minmax.sh | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 609 insertions(+), 4 deletions(-)
PATCH v2 1/4 debugfs: return EINVAL if write to unsigned simple files exceed storage.
Writes to the debug files created by "debugfs_create_*" (u8/u16/u32/u64), (x8/x16/x32/x64) should not silently succeed if the value exceeds the storage space for the type and upper written bits are lost. Absent an error, a read should return the last written value. Current behaviour is to down cast the storage type thus losing upper bits (thus u64/x64 files are unaffected).
This patch ensures the written value fits into the specified storage space returning EINVAL on error.
--- Documentation/filesystems/debugfs.rst | 7 ++++--- fs/debugfs/file.c | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-)
Documentation/filesystems/debugfs.rst +4 -3
@@ -85,9 +85,10 @@ created with any of:: struct dentry *parent, u64 *value); These files support both reading and writing the given value; if a specific -file should not be written to, simply set the mode bits accordingly. The -values in these files are in decimal; if hexadecimal is more appropriate, -the following functions can be used instead:: +file should not be written to, simply set the mode bits accordingly. Written +values that exceed the storage for the type return EINVAL. The values in these +files are in decimal; if hexadecimal is more appropriate, the following +functions can be used instead:: void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, u8 *value);
fs/debugfs/file.c +6 -0
@@ -429,6 +429,8 @@ static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,static int debugfs_u8_set (void *data,u64 val) { +if (val > (1 <<sizeof (u8) *8 ) -1 ) +return -EINVAL ; *(u8 *)data = val;return 0 ; }
@@ -465,6 +467,8 @@ EXPORT_SYMBOL_GPL(debugfs_create_u8);static int debugfs_u16_set (void *data,u64 val) { +if (val > (1 <<sizeof (u16) *8 ) -1 ) +return -EINVAL ; *(u16 *)data = val;return 0 ; }
@@ -501,6 +505,8 @@ EXPORT_SYMBOL_GPL(debugfs_create_u16);static int debugfs_u32_set (void *data,u64 val) { +if (val > (1ull <<sizeof (u32) *8 ) -1 ) +return -EINVAL ; *(u32 *)data = val;return 0 ; }
Signature · 1 line
2.31.1
Greg KH
maintainerRe: [PATCH 1/4] debugfs: return EINVAL if write to unsigned simple files exceed storage.
Quoted 39 lines · chris hyser
Writes to the debug files created by "debugfs_create_*" (u8/u16/u32/u64), (x8/x16/x32/x64) should not silently succeed if the value exceeds the storage space for the type and upper written bits are lost. Absent an error, a read should return the last written value. Current behaviour is to down cast the storage type thus losing upper bits (thus u64/x64 files are unaffected).
This patch ensures the written value fits into the specified storage space returning EINVAL on error.
Signed-off-by Chris Hyser <[email protected]>--- Documentation/filesystems/debugfs.rst | 7 ++++--- fs/debugfs/file.c | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-)Documentation/filesystems/debugfs.rst +4
-3@@ -85,9 +85,10 @@ created with any of:: struct dentry *parent, u64 *value); These files support both reading and writing the given value; if a specific -file should not be written to, simply set the mode bits accordingly. The -values in these files are in decimal; if hexadecimal is more appropriate, -the following functions can be used instead:: +file should not be written to, simply set the mode bits accordingly. Written +values that exceed the storage for the type return EINVAL. The values in these +files are in decimal; if hexadecimal is more appropriate, the following +functions can be used instead:: void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, u8 *value);fs/debugfs/file.c +0
-0
fs/debugfs/file.c static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, · line 433
Quoted 5 lines
fs/debugfs/file.c +2
-0@@ -429,6 +429,8 @@ static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,static int debugfs_u8_set (void *data,u64 val) { +if (val > (1 <<sizeof (u8) *8 ) -1 ) +return -EINVAL ;
We do have U8_MAX and friends, please don't reinvent the wheel.
But really, why? This is debugfs, if userspace messes something up like this, why not just cast and be done with it?
In other words, what existing workflow is now going to break with this patchset? :)
thanks,
greg k-h
Re: [PATCH 1/4] debugfs: return EINVAL if write to unsigned simple files exceed storage.
Quoted 41 lines · Greg KH
Quoted 40 lines · chris hyser
Writes to the debug files created by "debugfs_create_*" (u8/u16/u32/u64), (x8/x16/x32/x64) should not silently succeed if the value exceeds the storage space for the type and upper written bits are lost. Absent an error, a read should return the last written value. Current behaviour is to down cast the storage type thus losing upper bits (thus u64/x64 files are unaffected).
This patch ensures the written value fits into the specified storage space returning EINVAL on error.
Signed-off-by Chris Hyser <[email protected]>--- Documentation/filesystems/debugfs.rst | 7 ++++--- fs/debugfs/file.c | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-)Documentation/filesystems/debugfs.rst +4
-3@@ -85,9 +85,10 @@ created with any of:: struct dentry *parent, u64 *value); These files support both reading and writing the given value; if a specific -file should not be written to, simply set the mode bits accordingly. The -values in these files are in decimal; if hexadecimal is more appropriate, -the following functions can be used instead:: +file should not be written to, simply set the mode bits accordingly. Written +values that exceed the storage for the type return EINVAL. The values in these +files are in decimal; if hexadecimal is more appropriate, the following +functions can be used instead:: void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, u8 *value);fs/debugfs/file.c +0
-0@@ -429,6 +429,8 @@ static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
Quoted 8 lines
fs/debugfs/file.c static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, · line 433
Quoted 4 lines
fs/debugfs/file.c +2
-0static int debugfs_u8_set (void *data,u64 val) { +if (val > (1 <<sizeof (u8) *8 ) -1 ) +return -EINVAL ;We do have
U8_MAXand friends, please don't reinvent the whee > But really, why? This is debugfs, if userspace messes something up like this, why not just cast and be done with it?
So PeterZ asked much the same question when I submitted something for the divide by zero bug mentioned. Just don't write 0. Ultimately, he recommended looking into the more generic min/max stuff.
Now, if it was only "really" debug I think even then catching an easy to catch error is useful. Unfortunately, many distros ship with this stuff turned on and tools access these things ... which is the answer to your next question, what might this break. I'm not sure something (likely accidentally) relying on silently dropping bits really works, but it would appear to break with this change.
Quoted 3 lines
In other words, what existing workflow is now going to break with this patchset? :)
I would answer this with your point that this is debugfs. I remember when a bunch of the sched debug sysctls were originally moved to debugfs files. That broke tools etc, but the argument was that it is debugfs. You should not use it in production and the API is what it is. Not an exact analogy for sure, but that was my thinking.
Honestly, I wasn't really sure if this patch would fly, but simple checks and feedback even in debugging seemed useful to me.
Are you open to the min/max files patch? I can resend w/o basing on this patch.
-chrish
PATCH v2 2/4 debugfs: Provide min/max checking for simple u8, u16, u32, u64 debugfs files.
In reply to chris hyserThis patch extends the simple file interface to include min/max checking files. Writes to a file are checked such that the written value must satisfy the specified check (as well as fit into the specified storage):
minmax: 'min' <= value <= 'max' min: 'min' <= value max: value <= 'max'
Failure of the check returns EINVAL.
While the same checks could be done by providing a custom "set(void *data, u64 val) function" in DEFINE_DEBUGFS_ATTRIBUTE() for each file needing said check, each instance would require a private struct file_operations.
Using the same trick as the unsigned simple files (u8/u16/u32/u64), this patch supports "unlimited" users with only two struct file_operations per unsigned type. As min/max checking only applies to set/writing, read-only files make no sense.
Various macros are provided to simplify setting up the params struct.
--- Documentation/filesystems/debugfs.rst | 39 ++++++ fs/debugfs/file.c | 189 ++++++++++++++++++++++++++ include/linux/debugfs.h | 60 ++++++++ 3 files changed, 288 insertions(+)
Documentation/filesystems/debugfs.rst +39 -0
@@ -99,6 +99,45 @@ functions can be used instead:: void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, u64 *value); +Some use cases require min/max checking of the written values preserving the +initial value on failure and returning EINVAL for the write. + + void debugfs_create_u8_minmax(const char *name, umode_t mode, + struct dentry *parent, + struct debugfs_minmax_params *mnxp); + void debugfs_create_u16_minmax(const char *name, umode_t mode, + struct dentry *parent, + struct debugfs_minmax_params *mnxp); + void debugfs_create_u32_minmax(const char *name, umode_t mode, + struct dentry *parent, + struct debugfs_minmax_params *mnxp); + void debugfs_create_u64_minmax(const char *name, umode_t mode, + struct dentry *parent, + struct debugfs_minmax_params *mnxp); + +These functions are called with the following parameter structure, flags and +helper macros. The parameter structure must be available for the duration of +the file, thus requiring global or malloced memory. Failure of the specified +check(s) return EINVAL. + +struct debugfs_minmax_params { + void *value; + u64 min; + u64 max; + u8 flags; +}; + +Flags are defined as such: + DEBUGFS_ATTR_MIN 1 + DEBUGFS_ATTR_MAX 2 + DEBUGFS_ATTR_MINMAX (DEBUGFS_ATTR_MIN | DEBUGFS_ATTR_MAX) + +Additional helper macros provide for + DEBUGFS_MINMAX_ATTRIBUTES_BASE(name_parm_ptr, value, min, max, flags); + DEBUGFS_MIN_ATTRIBUTES(name_parm_ptr, value, min); + DEBUGFS_MAX_ATTRIBUTES(name_parm_ptr, value, max); + DEBUGFS_MINMAX_ATTRIBUTES(name_parm_ptr, value, min, max); + These functions are useful as long as the developer knows the size of the value to be exported. Some types can have different widths on different architectures, though, complicating the situation somewhat. There are
fs/debugfs/file.c +189 -0
195 lines not on this page · raw message
include/linux/debugfs.h +60 -0
78 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v2 3/4 debugfs: provide testing for the min/max simple debug files.
In reply to chris hyserThis patch contains a module and a bash script which tests the new simple "unsigned min/max checking" files.
The module creates the debugfs files and encodes the test type and test parameters in the file name which the script decodes and tests.
Currently the files were placed under testscripts though auto testing is not yet enabled. The hope is that this eases testing of the patches.
--- tools/testing/selftests/debugfs/Makefile | 15 ++ tools/testing/selftests/debugfs/minmax_test.c | 140 +++++++++++++++++ .../testing/selftests/debugfs/test_minmax.sh | 147 ++++++++++++++++++ 3 files changed, 302 insertions(+) create mode 100644 tools/testing/selftests/debugfs/Makefile create mode 100644 tools/testing/selftests/debugfs/minmax_test.c create mode 100755 tools/testing/selftests/debugfs/test_minmax.sh
tools/testing/selftests/debugfs/Makefile new file +15 -0
@@ -0,0 +1,15 @@ +obj-m := minmax_test.o + +KDIR := ../../../.. +PWD := $(shell pwd) +WARN_FLAGS += -Wall + +.PHONY: all +all: + $(MAKE) -C $(KDIR) M=$(PWD) modules + +clean: + $(MAKE) -C $(KDIR) M=$(PWD) clean + + +include ../lib.mk
tools/testing/selftests/debugfs/minmax_test.c new file +140 -0
140 lines not on this page · raw message
tools/testing/selftests/debugfs/test_minmax.sh new file +147 -0
147 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v2 4/4 sched/numa: Fix divide by zero for sysctl_numa_balancing_scan_size.
In reply to chris hyserCommit 6419265899d9 ("sched/fair: Fix division by zero sysctl_numa_balancing_scan_size") prevented a divide by zero by using sysctl mechanisms to return EINVAL for a sysctl_numa_balancing_scan_size value of zero. When moved from a sysctl to a debugfs file, this checking was lost.
This patch puts zero checking back in place using the debugfs minmax checking mechanism.
--- kernel/sched/debug.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/sched/debug.c +8 -1
@@ -216,6 +216,13 @@ static const struct file_operations sched_scaling_fops = {#endif /* SMP */ +#ifdef CONFIG_NUMA_BALANCING + +/* prevent divide by zero */ +DEBUGFS_MIN_ATTRIBUTES (scan_size_mb_params, &sysctl_numa_balancing_scan_size,1 ); + +#endif /* CONFIG_NUMA_BALANCING */ +#ifdef CONFIG_PREEMPT_DYNAMIC static ssize_t sched_dynamic_write(struct file *filp, const char __user *ubuf,
@@ -371,7 +378,7 @@ static __init int sched_init_debug(void)debugfs_create_u32 ("scan_delay_ms" ,0644 , numa, &sysctl_numa_balancing_scan_delay);debugfs_create_u32 ("scan_period_min_ms" ,0644 , numa, &sysctl_numa_balancing_scan_period_min);debugfs_create_u32 ("scan_period_max_ms" ,0644 , numa, &sysctl_numa_balancing_scan_period_max); -debugfs_create_u32 ("scan_size_mb" ,0644 , numa, &sysctl_numa_balancing_scan_size); +debugfs_create_u32_minmax ("scan_size_mb" ,0644 , numa, &scan_size_mb_params);debugfs_create_u32 ("hot_threshold_ms" ,0644 , numa, &sysctl_numa_balancing_hot_threshold);#endif
Signature · 1 line
2.31.1
Greg KH
maintainerQuoted 17 lines · chris hyser
v2:
Apologies. I sent this the first time without including lkml.v1:
This originally started as an attempt to solve a divide by zero issue in sched debug code that was introduced when a sysctl value with non-zero checking was moved to a simple u32 debugfs file. In looking at ways to solve this, it was mentioned I should look at providing general debugfs files with min/max checking.One problem was that a check for greater than zero for say a u8 succeeds for a number like 256 (but stores a zero anyway) as the upper bits that don't fit into storage are silently dropped. Therefore values greater than the storage capacity must also fail. Getting an error when what the user wrote is not what was actually stored, seems like a useful requirement for the other simple files and so I moved the check into there.
Quoted 4 lines
To enable easy testing, a test module and test script are provided which can validate the new functions as well as check the new limits on the older functions. This was stuck under selftests, but it is not currently tied into the testing infrastructure.
This is a lot of new infrastructure for a single debugfs file that you could just check for in the file write callback instead.
I'm all for cool new features, but wow, this seems like major overkill. Are you _SURE_ you need it all?
thanks,
greg k-h
Quoted 23 lines · Greg KH
Quoted 17 lines · chris hyser
v2:
Apologies. I sent this the first time without including lkml.v1:
This originally started as an attempt to solve a divide by zero issue in sched debug code that was introduced when a sysctl value with non-zero checking was moved to a simple u32 debugfs file. In looking at ways to solve this, it was mentioned I should look at providing general debugfs files with min/max checking.One problem was that a check for greater than zero for say a u8 succeeds for a number like 256 (but stores a zero anyway) as the upper bits that don't fit into storage are silently dropped. Therefore values greater than the storage capacity must also fail. Getting an error when what the user wrote is not what was actually stored, seems like a useful requirement for the other simple files and so I moved the check into there.
Quoted 4 lines
To enable easy testing, a test module and test script are provided which can validate the new functions as well as check the new limits on the older functions. This was stuck under selftests, but it is not currently tied into the testing infrastructure.
Quoted 5 lines
This is a lot of new infrastructure for a single debugfs file that you could just check for in the file write callback instead.
I'm all for cool new features, but wow, this seems like major overkill. Are you _SURE_ you need it all?
The current use case obviously does not require all of this. The idea was instead of a one off fix, it might be useful to provide a generic solution to a common class of problems.
So sending this out hopefully would help determine the perceived usefulness.
-chrish
Quoted 24 lines · chris hyser
Quoted 23 lines · Greg KH
Quoted 21 lines · chris hyser
v2:
Apologies. I sent this the first time without including lkml.v1:
This originally started as an attempt to solve a divide by zero issue in sched debug code that was introduced when a sysctl value with non-zero checking was moved to a simple u32 debugfs file. In looking at ways to solve this, it was mentioned I should look at providing general debugfs files with min/max checking.One problem was that a check for greater than zero for say a u8 succeeds for a number like 256 (but stores a zero anyway) as the upper bits that don't fit into storage are silently dropped. Therefore values greater than the storage capacity must also fail. Getting an error when what the user wrote is not what was actually stored, seems like a useful requirement for the other simple files and so I moved the check into there.
To enable easy testing, a test module and test script are provided which can validate the new functions as well as check the new limits on the older functions. This was stuck under selftests, but it is not currently tied into the testing infrastructure.
Quoted 5 lines
Quoted 5 lines
This is a lot of new infrastructure for a single debugfs file that you could just check for in the file write callback instead.
I'm all for cool new features, but wow, this seems like major overkill. Are you _SURE_ you need it all?
I do want to clarify about the size of this. It is 4 new create file functions, 8 static get/sets, a new struct and some simple macros. In keeping the style of the new code similar to prior, the lines of function comments for the new creates() almost equals the lines of code.
This doesn't seem to me to be as much overkill as say the xsigned version of these files which consumes 12 struct file_operations simply to provide a different string format.
-chrish