* [PATCH 2/4] debugfs: Provide min/max checking for simple u8, u16, u32, u64 debugfs files.
2023-05-30 19:40 [PATCH v2 0/4] debugfs: Add simple min/max "files" to debugfs to fix sched debug code chris hyser
2023-05-30 19:40 ` [PATCH 1/4] debugfs: return EINVAL if write to unsigned simple files exceed storage chris hyser
@ 2023-05-30 19:40 ` chris hyser
2023-05-30 19:40 ` [PATCH 3/4] debugfs: provide testing for the min/max simple debug files chris hyser
` (2 subsequent siblings)
4 siblings, 0 replies; 10+ messages in thread
From: chris hyser @ 2023-05-30 19:40 UTC (permalink / raw)
To: chris.hyser, linux-kernel, peterz, gregkh, Rafael J. Wysocki
This 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.
Signed-off-by: Chris Hyser <chris.hyser@oracle.com>
---
Documentation/filesystems/debugfs.rst | 39 ++++++
fs/debugfs/file.c | 189 ++++++++++++++++++++++++++
include/linux/debugfs.h | 60 ++++++++
3 files changed, 288 insertions(+)
diff --git a/Documentation/filesystems/debugfs.rst b/Documentation/filesystems/debugfs.rst
index 6f1ac8d7f108..31d186e952eb 100644
--- a/Documentation/filesystems/debugfs.rst
+++ b/Documentation/filesystems/debugfs.rst
@@ -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
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index 743ddd04f8d8..bd655b286da6 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -578,6 +578,195 @@ void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
}
EXPORT_SYMBOL_GPL(debugfs_create_u64);
+static int debugfs_minmax_chk(struct debugfs_minmax_params *mnxp, u64 val)
+{
+ if ((mnxp->flags & 0x1) && val < mnxp->min)
+ return -EINVAL;
+ if ((mnxp->flags & 0x2) && val > mnxp->max)
+ return -EINVAL;
+ return 0;
+}
+
+static int debugfs_u8_minmax_set(void *data, u64 val)
+{
+ struct debugfs_minmax_params *mnxp = data;
+ int err = debugfs_minmax_chk(mnxp, val);
+
+ if (err)
+ return err;
+ return debugfs_u8_set(mnxp->value, val);
+}
+
+static int debugfs_u8_minmax_get(void *data, u64 *val)
+{
+ struct debugfs_minmax_params *mnxp = data;
+
+ return debugfs_u16_get(mnxp->value, val);
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_minmax, debugfs_u8_minmax_get, debugfs_u8_minmax_set, "%llu\n");
+DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_minmax_wo, NULL, debugfs_u8_minmax_set, "%llu\n");
+
+/**
+ * debugfs_create_u8_minmax - create a debugfs file that is used to both read
+ * and write an unsigned 8-bit value if it satisfies a min check, max check
+ * or both.
+ * @name: a pointer to a string containing the name of the file to create.
+ * @mode: the permission that the file should have
+ * @parent: a pointer to the parent dentry for this file. This should be a
+ * directory dentry if set. If this parameter is %NULL, then the
+ * file will be created in the root of the debugfs filesystem.
+ * @mnxp: a pointer to the parameter struct which holds the test limits, the
+ * test to perform and a pointer to the variable to display and modify.
+ *
+ * This function creates a file in debugfs with the given name that contains
+ * the value of the specified variable. If the @mode variable is so
+ * set, it can be read from, and written to.
+ */
+void debugfs_create_u8_minmax(const char *name, umode_t mode,
+ struct dentry *parent,
+ struct debugfs_minmax_params *mnxp)
+{
+ debugfs_create_mode_unsafe(name, mode, parent, mnxp, &fops_u8_minmax,
+ NULL, &fops_u8_minmax_wo);
+}
+EXPORT_SYMBOL_GPL(debugfs_create_u8_minmax);
+
+static int debugfs_u16_minmax_set(void *data, u64 val)
+{
+ struct debugfs_minmax_params *mnxp = data;
+ int err = debugfs_minmax_chk(mnxp, val);
+
+ if (err)
+ return err;
+ return debugfs_u16_set(mnxp->value, val);
+}
+
+static int debugfs_u16_minmax_get(void *data, u64 *val)
+{
+ struct debugfs_minmax_params *mnxp = data;
+
+ return debugfs_u16_get(mnxp->value, val);
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_minmax, debugfs_u16_minmax_get, debugfs_u16_minmax_set, "%llu\n");
+DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_minmax_wo, NULL, debugfs_u16_minmax_set, "%llu\n");
+
+/**
+ * debugfs_create_u16_minmax - create a debugfs file that is used to both read
+ * and write an unsigned 16-bit value if it satisfies a min check, max check
+ * or both.
+ * @name: a pointer to a string containing the name of the file to create.
+ * @mode: the permission that the file should have
+ * @parent: a pointer to the parent dentry for this file. This should be a
+ * directory dentry if set. If this parameter is %NULL, then the
+ * file will be created in the root of the debugfs filesystem.
+ * @mnxp: a pointer to the parameter struct which holds the test limits, the
+ * test to perform and a pointer to the variable to display and modify.
+ *
+ * This function creates a file in debugfs with the given name that contains
+ * the value of the specified variable. If the @mode variable is so
+ * set, it can be read from, and written to.
+ */
+void debugfs_create_u16_minmax(const char *name, umode_t mode,
+ struct dentry *parent,
+ struct debugfs_minmax_params *mnxp)
+{
+ debugfs_create_mode_unsafe(name, mode, parent, mnxp, &fops_u16_minmax,
+ NULL, &fops_u16_minmax_wo);
+}
+EXPORT_SYMBOL_GPL(debugfs_create_u16_minmax);
+
+static int debugfs_u32_minmax_set(void *data, u64 val)
+{
+ struct debugfs_minmax_params *mnxp = data;
+ int err = debugfs_minmax_chk(mnxp, val);
+
+ if (err)
+ return err;
+ return debugfs_u32_set(mnxp->value, val);
+}
+
+static int debugfs_u32_minmax_get(void *data, u64 *val)
+{
+ struct debugfs_minmax_params *mnxp = data;
+
+ return debugfs_u32_get(mnxp->value, val);
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_minmax, debugfs_u32_minmax_get, debugfs_u32_minmax_set, "%llu\n");
+DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_minmax_wo, NULL, debugfs_u32_minmax_set, "%llu\n");
+
+/**
+ * debugfs_create_u32_minmax - create a debugfs file that is used to both read
+ * and write an unsigned 32-bit value if it satisfies a min check, max check
+ * or both.
+ * @name: a pointer to a string containing the name of the file to create.
+ * @mode: the permission that the file should have
+ * @parent: a pointer to the parent dentry for this file. This should be a
+ * directory dentry if set. If this parameter is %NULL, then the
+ * file will be created in the root of the debugfs filesystem.
+ * @mnxp: a pointer to the parameter struct which holds the test limits, the
+ * test to perform and a pointer to the variable to display and modify.
+ *
+ * This function creates a file in debugfs with the given name that contains
+ * the value of the specified variable. If the @mode variable is so
+ * set, it can be read from, and written to.
+ */
+void debugfs_create_u32_minmax(const char *name, umode_t mode,
+ struct dentry *parent,
+ struct debugfs_minmax_params *mnxp)
+{
+ debugfs_create_mode_unsafe(name, mode, parent, mnxp, &fops_u32_minmax,
+ NULL, &fops_u32_minmax_wo);
+}
+EXPORT_SYMBOL_GPL(debugfs_create_u32_minmax);
+
+static int debugfs_u64_minmax_set(void *data, u64 val)
+{
+ struct debugfs_minmax_params *mnxp = data;
+ int err = debugfs_minmax_chk(mnxp, val);
+
+ if (err)
+ return err;
+ return debugfs_u64_set(mnxp->value, val);
+}
+
+static int debugfs_u64_minmax_get(void *data, u64 *val)
+{
+ struct debugfs_minmax_params *mnxp = data;
+
+ return debugfs_u64_get(mnxp->value, val);
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_minmax, debugfs_u64_minmax_get, debugfs_u64_minmax_set, "%llu\n");
+DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_minmax_wo, NULL, debugfs_u64_minmax_set, "%llu\n");
+
+/**
+ * debugfs_create_u64_minmax - create a debugfs file that is used both to read
+ * and write an unsigned 64-bit value if it satisfies a min check, max check
+ * or both.
+ * @name: a pointer to a string containing the name of the file to create.
+ * @mode: the permission that the file should have
+ * @parent: a pointer to the parent dentry for this file. This should be a
+ * directory dentry if set. If this parameter is %NULL, then the
+ * file will be created in the root of the debugfs filesystem.
+ * @mnxp: a pointer to the parameter struct which holds the test limits, the
+ * test to perform and a pointer to the variable to display and modify.
+ *
+ * This function creates a file in debugfs with the given name that contains
+ * the value of the specified variable. If the @mode variable is so
+ * set, it can be read from, and written to.
+ */
+void debugfs_create_u64_minmax(const char *name, umode_t mode,
+ struct dentry *parent,
+ struct debugfs_minmax_params *mnxp)
+{
+ debugfs_create_mode_unsafe(name, mode, parent, mnxp, &fops_u64_minmax,
+ NULL, &fops_u64_minmax_wo);
+}
+EXPORT_SYMBOL_GPL(debugfs_create_u64_minmax);
+
static int debugfs_ulong_set(void *data, u64 val)
{
*(unsigned long *)data = val;
diff --git a/include/linux/debugfs.h b/include/linux/debugfs.h
index ea2d919fd9c7..322fecb22a0a 100644
--- a/include/linux/debugfs.h
+++ b/include/linux/debugfs.h
@@ -43,6 +43,36 @@ struct debugfs_u32_array {
u32 n_elements;
};
+struct debugfs_minmax_params {
+ void *value;
+ u64 min;
+ u64 max;
+ u8 flags;
+};
+
+/* debugfs_minmax_params "flag" values
+ */
+#define DEBUGFS_ATTR_MIN 1
+#define DEBUGFS_ATTR_MAX 2
+#define DEBUGFS_ATTR_MINMAX (DEBUGFS_ATTR_MIN | DEBUGFS_ATTR_MAX)
+
+#define DEBUGFS_MINMAX_ATTRIBUTES_BASE(__name, __value, __min, __max, __flags) \
+static struct debugfs_minmax_params __name = { \
+ .value = (__value), \
+ .min = (__min), \
+ .max = (__max), \
+ .flags = (__flags), \
+}
+
+#define DEBUGFS_MIN_ATTRIBUTES(__name, __value, __min) \
+ DEBUGFS_MINMAX_ATTRIBUTES_BASE(__name, __value, __min, 0, DEBUGFS_ATTR_MIN)
+
+#define DEBUGFS_MAX_ATTRIBUTES(__name, __value, __max) \
+ DEBUGFS_MINMAX_ATTRIBUTES_BASE(__name, __value, 0, __max, DEBUGFS_ATTR_MAX)
+
+#define DEBUGFS_MINMAX_ATTRIBUTES(__name, __value, __min, __max) \
+ DEBUGFS_MINMAX_ATTRIBUTES_BASE(__name, __value, __min, __max, DEBUGFS_ATTR_MINMAX)
+
extern struct dentry *arch_debugfs_dir;
#define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \
@@ -122,6 +152,20 @@ void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
u32 *value);
void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
u64 *value);
+
+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);
+
void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
unsigned long *value);
void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
@@ -287,6 +331,22 @@ static inline void debugfs_create_u32(const char *name, umode_t mode,
static inline void debugfs_create_u64(const char *name, umode_t mode,
struct dentry *parent, u64 *value) { }
+static inline void debugfs_create_u8_minmax(const char *name, umode_t mode,
+ struct dentry *parent,
+ struct debugfs_minmax_params *mnxp) { }
+
+static inline void debugfs_create_u16_minmax(const char *name, umode_t mode,
+ struct dentry *parent,
+ struct debugfs_minmax_params *mnxp) { }
+
+static inline void debugfs_create_u32_minmax(const char *name, umode_t mode,
+ struct dentry *parent,
+ struct debugfs_minmax_params *mnxp) { }
+
+static inline void debugfs_create_u64_minmax(const char *name, umode_t mode,
+ struct dentry *parent,
+ struct debugfs_minmax_params *mnxp) { }
+
static inline void debugfs_create_ulong(const char *name, umode_t mode,
struct dentry *parent,
unsigned long *value) { }
--
2.31.1
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 3/4] debugfs: provide testing for the min/max simple debug files.
2023-05-30 19:40 [PATCH v2 0/4] debugfs: Add simple min/max "files" to debugfs to fix sched debug code chris hyser
2023-05-30 19:40 ` [PATCH 1/4] debugfs: return EINVAL if write to unsigned simple files exceed storage chris hyser
2023-05-30 19:40 ` [PATCH 2/4] debugfs: Provide min/max checking for simple u8, u16, u32, u64 debugfs files chris hyser
@ 2023-05-30 19:40 ` chris hyser
2023-05-30 19:40 ` [PATCH 4/4] sched/numa: Fix divide by zero for sysctl_numa_balancing_scan_size chris hyser
2023-05-30 20:18 ` [PATCH v2 0/4] debugfs: Add simple min/max "files" to debugfs to fix sched debug code Greg KH
4 siblings, 0 replies; 10+ messages in thread
From: chris hyser @ 2023-05-30 19:40 UTC (permalink / raw)
To: chris.hyser, linux-kernel, peterz, gregkh, Rafael J. Wysocki
This 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.
Signed-off-by: Chris Hyser <chris.hyser@oracle.com>
---
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
diff --git a/tools/testing/selftests/debugfs/Makefile b/tools/testing/selftests/debugfs/Makefile
new file mode 100644
index 000000000000..5dddaaf6c7a2
--- /dev/null
+++ b/tools/testing/selftests/debugfs/Makefile
@@ -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
diff --git a/tools/testing/selftests/debugfs/minmax_test.c b/tools/testing/selftests/debugfs/minmax_test.c
new file mode 100644
index 000000000000..0e0f6bcd6302
--- /dev/null
+++ b/tools/testing/selftests/debugfs/minmax_test.c
@@ -0,0 +1,140 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test debugfs minmax simple files.
+ *
+ * Copyright (c) 2023 Oracle and/or its affiliates.
+ * Author: Chris Hyser <chris.hyser@oracle.com>
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses>.
+ */
+
+#include <linux/debugfs.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+MODULE_LICENSE("GPL");
+
+#define MIN_MAX 1
+
+static struct dentry *dir = NULL;
+
+#ifdef MIN_MAX
+u8 test_max_8 = 250;
+DEBUGFS_MAX_ATTRIBUTES(test_max_8_p, &test_max_8, 252);
+
+u8 test_min_8 = 250;
+DEBUGFS_MIN_ATTRIBUTES(test_min_8_p, &test_min_8, 25);
+
+u8 test_minmax_8 = 250;
+DEBUGFS_MINMAX_ATTRIBUTES(test_minmax_8_p, &test_minmax_8, 100, 253);
+
+
+u16 test_max_16 = 250;
+DEBUGFS_MAX_ATTRIBUTES(test_max_16_p, &test_max_16, 0x1fff);
+
+u16 test_min_16 = 250;
+DEBUGFS_MIN_ATTRIBUTES(test_min_16_p, &test_min_16, 100);
+
+u16 test_minmax_16 = 250;
+DEBUGFS_MINMAX_ATTRIBUTES(test_minmax_16_p, &test_minmax_16, 100, 300);
+
+
+u32 test_max_32 = 250;
+DEBUGFS_MAX_ATTRIBUTES(test_max_32_p, &test_max_32, 0x1fffffff);
+
+u32 test_min_32 = 250;
+DEBUGFS_MIN_ATTRIBUTES(test_min_32_p, &test_min_32, 100);
+
+u32 test_minmax_32 = 250;
+DEBUGFS_MINMAX_ATTRIBUTES(test_minmax_32_p, &test_minmax_32, 100, 0x1fffffff);
+
+/* test setting everything directly */
+u32 test_minmax_32_s = 250;
+static struct debugfs_minmax_params test_minmax_32_ps = {
+ .value = &test_minmax_32_s,
+ .min = 100,
+ .max = 0x1fffffff,
+ .flags = 3,
+};
+
+u64 test_max_64 = 250;
+DEBUGFS_MAX_ATTRIBUTES(test_max_64_p, &test_max_64, 0x1fffffff);
+
+u64 test_min_64 = 250;
+DEBUGFS_MIN_ATTRIBUTES(test_min_64_p, &test_min_64, 100);
+
+u64 test_minmax_64 = 250;
+DEBUGFS_MINMAX_ATTRIBUTES(test_minmax_64_p, &test_minmax_64, 100, 300);
+#endif
+
+u8 test_reg_8 = 250;
+u16 test_reg_16 = 250;
+u32 test_reg_32 = 250;
+u64 test_reg_64 = 250;
+
+u8 test_regx_8 = 250;
+u16 test_regx_16 = 250;
+u32 test_regx_32 = 250;
+u64 test_regx_64 = 250;
+
+
+/* File names consist of test_<test_type>_<1st param>_<optional sec
+ * param>_<storage size in bits>.
+ */
+
+int init_module(void)
+{
+ pr_err("minmax_test: init_module()\n");
+ dir = debugfs_create_dir("minmax_test", 0);
+ if (dir == NULL) {
+ printk(KERN_ALERT "minmax_test: can't create dir 'minmax_test'\n");
+ return -1;
+ }
+
+#ifdef MIN_MAX
+ debugfs_create_u8_minmax("test_max_252_8", 0644, dir, &test_max_8_p);
+ debugfs_create_u8_minmax("test_min_25_8", 0644, dir, &test_min_8_p);
+ debugfs_create_u8_minmax("test_minmax_100_253_8", 0644, dir, &test_minmax_8_p);
+
+ debugfs_create_u16_minmax("test_max_0x1fff_16", 0644, dir, &test_max_16_p);
+ debugfs_create_u16_minmax("test_min_100_16", 0644, dir, &test_min_16_p);
+ debugfs_create_u16_minmax("test_minmax_100_300_16", 0644, dir, &test_minmax_16_p);
+
+ debugfs_create_u32_minmax("test_max_0x1fffffff_32", 0644, dir, &test_max_32_p);
+ debugfs_create_u32_minmax("test_min_100_32", 0644, dir, &test_min_32_p);
+ debugfs_create_u32_minmax("test_minmax_100_0x1fffffff_32", 0644, dir, &test_minmax_32_p);
+ debugfs_create_u32_minmax("testss_minmax_100_0x1fffffff_32", 0644, dir, &test_minmax_32_ps);
+
+ debugfs_create_u64_minmax("test_max_0x1fffffff_64", 0644, dir, &test_max_64_p);
+ debugfs_create_u64_minmax("test_min_100_64", 0644, dir, &test_min_64_p);
+ debugfs_create_u64_minmax("test_minmax_100_300_64", 0644, dir, &test_minmax_64_p);
+#endif
+
+ debugfs_create_u8("test_reg_8", 0644, dir, &test_reg_8);
+ debugfs_create_u16("test_reg_16", 0644, dir, &test_reg_16);
+ debugfs_create_u32("test_reg_32", 0644, dir, &test_reg_32);
+ debugfs_create_u64("test_reg_64", 0644, dir, &test_reg_64);
+
+ debugfs_create_x8("test_regx_8", 0644, dir, &test_regx_8);
+ debugfs_create_x16("test_regx_16", 0644, dir, &test_regx_16);
+ debugfs_create_x32("test_regx_32", 0644, dir, &test_regx_32);
+ debugfs_create_x64("test_regx_64", 0644, dir, &test_regx_64);
+ return 0;
+}
+
+void cleanup_module(void)
+{
+ pr_err("minmax_test: cleanup_module()\n");
+ debugfs_remove_recursive(dir);
+}
diff --git a/tools/testing/selftests/debugfs/test_minmax.sh b/tools/testing/selftests/debugfs/test_minmax.sh
new file mode 100755
index 000000000000..67ed7b9abac2
--- /dev/null
+++ b/tools/testing/selftests/debugfs/test_minmax.sh
@@ -0,0 +1,147 @@
+#!/bin/bash
+
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Test debugfs minmax simple files.
+#
+# Copyright (c) 2023 Oracle and/or its affiliates.
+# Author: Chris Hyser <chris.hyser@oracle.com>
+#
+# This library is free software; you can redistribute it and/or modify it
+# under the terms of version 2.1 of the GNU Lesser General Public License as
+# published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+# for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this library; if not, see <http://www.gnu.org/licenses>.
+
+rmmod minmax_test 2>/dev/null
+insmod minmax_test.ko
+
+cd /sys/kernel/debug/minmax_test
+pwd
+
+function split_wds()
+{
+ words=()
+ local IFS=_
+ for w in $1; do
+ words+=($w)
+ done
+}
+
+function test_max()
+{
+ echo $2 > $1
+ local res=$?
+ local rv=$(cat $1)
+ if ! (( res == 0 && $2 == rv )); then
+ return 1
+ fi
+
+ local max=$(($2 + 1))
+ echo $max > $1 2>/dev/null
+ res=$?
+ rv=$(cat $1)
+ if ! [[ $res -ne 0 && $2 -eq $rv ]]; then
+ return 1
+ fi
+ return 0
+}
+
+function test_min()
+{
+ echo $2 > $1
+ local res=$?
+ local rv=$(cat $1)
+ if ! [[ $res -eq 0 && $2 -eq $rv ]]; then
+ return 1
+ fi
+
+ local min=$(($2 - 1))
+ echo $min > $1 2>/dev/null
+ res=$?
+ rv=$(cat $1)
+ if ! [[ $res -ne 0 && $2 -eq $rv ]]; then
+ return 1
+ fi
+ return 0
+}
+
+function test_minmax()
+{
+ test_min $1 ${words[2]}
+ if [[ $? -ne 0 ]]; then
+ echo "$1: min error"
+ return 1
+ fi
+ test_max $1 ${words[3]}
+ if [[ $? -ne 0 ]]; then
+ echo "$1: max error"
+ return 1
+ fi
+ return 0
+}
+
+function test_reg()
+{
+ if [[ $2 -eq 64 ]]; then
+ local V=313
+
+ echo $V > $1 2>/dev/null
+
+ local rv=$(cat $1)
+ if ! (( rv == V )); then
+ return 1
+ fi
+ return 0
+ fi
+
+ local max=$(((1 << $2) - 1))
+ test_max $1 $max
+ if [[ $? -ne 0 ]]; then
+ echo "$1: reg/x error"
+ return 1
+ fi
+ return 0
+}
+
+rc=0
+for f in *; do
+ split_wds $f
+ echo file: $f
+
+ if [[ ${words[1]} == "min" ]]; then
+ test_min $f ${words[2]}
+ if [[ $? -ne 0 ]]; then
+ echo "$f: min error"
+ rc=1
+ fi
+ elif [[ ${words[1]} == "max" ]]; then
+ test_max $f ${words[2]}
+ if [[ $? -ne 0 ]]; then
+ echo "$f: max error"
+ rc=1
+ fi
+ elif [[ ${words[1]} == "minmax" ]]; then
+ test_minmax $f ${words[2]} ${words[3]}
+ if [[ $? -ne 0 ]]; then
+ echo "$f: minmax error"
+ rc=1
+ fi
+ elif [[ ${words[1]} == "reg" || ${words[1]} == "regx" ]]; then
+ test_reg $f ${words[2]}
+ if [[ $? -ne 0 ]]; then
+ echo "$f: reg/x error"
+ rc=1
+ fi
+ fi
+done
+
+rmmod minmax_test
+
+exit $rc
--
2.31.1
^ permalink raw reply related [flat|nested] 10+ messages in thread