Skip to contentSkip to conversation

selftests: add a new test driver for sysfs

PATCH Superseded Luis Chamberlain 4 participants 11 messages Jul 2, 2021 – Jul 4, 2021

Subsystems (8)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover selftests: add a new test driver for sysfs

I had posted a patch to fix a theoretical race with sysfs and device removal [0]. While the issue is no longer present with the patch present, the zram driver has already a lot of enhancements, so much so, that the race alone is very difficult to reproduce. Likewise, the zram driver had a series of other races on module removal which I recently posted fixes for [1], and it makes it unclear if these paper over the possible theoretical sysfs race. Although we even have gdb output from an actual race where this issue presented itself, there are other races which could happen before that and so what we realy need is a clean separate driver where we can experiment and try to reproduce unusual races.

This adds such a driver, a new sysfs_test driver, along with a set of new tests for it. We take hint of observed issues with the sysfs on the zram driver, and build sandbox based where wher can try to poke holes at the kernel with.

There are two main races we're after trying to reproduce:

1) proving the deadlock is real
2) allowing for enough slack for us to try to see if we can reproduce the syfs / device removal race

In order to tackle the second race, we need a bit of help from kernefs, given that the race is difficult to reproduce. So we add fault injection support to kernfs, which allows us to trigger all possible races on write.

This should be enough evidence for us to drop the suggested patch for sysfs for the second race. The first race however which leads to a deadlock is clearly explained now and I hope this shows how we need a generic solution.

[0] https://lkml.kernel.org/r/[email protected]
[1] https://lkml.kernel.org/r/[email protected]

Luis Chamberlain (4):
selftests: add tests_sysfs module
kernfs: add initial failure injection support
test_sysfs: add support to use kernfs failure injection
test_sysfs: demonstrate deadlock fix

 .../fault-injection/fault-injection.rst       |   22 +
 MAINTAINERS                                   |    9 +-
 fs/kernfs/Makefile                            |    1 +
 fs/kernfs/failure-injection.c                 |   82 +
 fs/kernfs/file.c                              |   13 +
 fs/kernfs/kernfs-internal.h                   |   73 +
 include/linux/kernfs.h                        |    5 +
 lib/Kconfig.debug                             |   23 +
 lib/Makefile                                  |    1 +
 lib/test_sysfs.c                              | 1037 +++++++++++++
 tools/testing/selftests/sysfs/Makefile        |   12 +
 tools/testing/selftests/sysfs/config          |    5 +
 tools/testing/selftests/sysfs/sysfs.sh        | 1376 +++++++++++++++++
 13 files changed, 2658 insertions(+), 1 deletion(-)
 create mode 100644 fs/kernfs/failure-injection.c
 create mode 100644 lib/test_sysfs.c
 create mode 100644 tools/testing/selftests/sysfs/Makefile
 create mode 100644 tools/testing/selftests/sysfs/config
 create mode 100755 tools/testing/selftests/sysfs/sysfs.sh
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/4 selftests: add tests_sysfs module

This adds a new selftest module which can be used to test sysfs, which would otherwise require using an existing driver. This lets us muck with a template driver to test breaking things without affecting system behaviour or requiring the dependencies of a real device driver.

A series of 28 tests are added. Support for using two device types are supported:

* misc
* block

Contrary to sysctls, sysfs requires a full write to happen at once, and so we reduce the digit tests to single writes. Two main sysfs knobs are provided for testing reading/storing, one which doesn't inclur any delays and another which can incur programmed delays. What locks are held, if any, are configurable, at module load time, or through dynamic configuration at run time.

Since sysfs is a technically filesystem, but a pseudo one, which requires a kernel user, our test_sysfs module and respective test script embraces fstests format for tests in the kernel ring bufffer. Likewise, a scraper for kernel crashes is provided which matches what fstests does as well.

Two tests are kept disabled as they currently cause a deadlock, and so this provides a mechanism to easily show proof and demo how the deadlock can happen:

Demos the deadlock with a device specific lock
./tools/testing/selftests/sysfs/sysfs.sh -t 0027

Demos the deadlock with rtnl_lock()
./tools/testing/selftests/sysfs/sysfs.sh -t 0028

Two separate solutions to the deadlock issue have been proposed, and so now its a matter of either documenting this limitation or eventually adopting a generic fix.

This selftests will shortly be expanded upon with more tests which require further kernel changes in order to provide better test coverage.

---
 MAINTAINERS                            |    7 +
 lib/Kconfig.debug                      |   10 +
 lib/Makefile                           |    1 +
 lib/test_sysfs.c                       |  953 +++++++++++++++++++
 tools/testing/selftests/sysfs/Makefile |   12 +
 tools/testing/selftests/sysfs/config   |    2 +
 tools/testing/selftests/sysfs/sysfs.sh | 1202 ++++++++++++++++++++++++
 7 files changed, 2187 insertions(+)
 create mode 100644 lib/test_sysfs.c
 create mode 100644 tools/testing/selftests/sysfs/Makefile
 create mode 100644 tools/testing/selftests/sysfs/config
 create mode 100755 tools/testing/selftests/sysfs/sysfs.sh
MAINTAINERS +7 -0
index 80ae7dbf8723..76f6bee86770 100644
@@ -17705,6 +17705,13 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/mmc/host/sdhci-pci-dwc-mshc.c
 
+SYSFS TEST DRIVER
+M:	Luis Chamberlain <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	lib/test_sysfs.c
+F:	tools/testing/selftests/sysfs/
+
 SYSTEM CONFIGURATION (SYSCON)
 M:	Lee Jones <[email protected]>
 M:	Arnd Bergmann <[email protected]>
lib/Kconfig.debug +10 -0
index d1467658361f..f9b9074c40f4 100644
@@ -2343,6 +2343,16 @@ config TEST_SYSCTL
 
 	  If unsure, say N.
 
+config TEST_SYSFS
+	tristate "sysfs test driver"
+	depends on SYSFS
+	help
+	  This builds the "test_sysfs" module. This driver enables to test the
+	  sysfs file system safely without affecting production knobs which
+	  might alter system functionality.
+
+	  If unsure, say N.
+
 config BITFIELD_KUNIT
 	tristate "KUnit test bitfield functions at runtime"
 	depends on KUNIT
lib/Makefile +1 -0
index f982b8f94007..f151cb9e428a 100644
@@ -61,6 +61,7 @@ obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
 obj-$(CONFIG_TEST_BITOPS) += test_bitops.o
 CFLAGS_test_bitops.o += -Werror
 obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
+obj-$(CONFIG_TEST_SYSFS) += test_sysfs.o
 obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o
 obj-$(CONFIG_TEST_IDA) += test_ida.o
 obj-$(CONFIG_KASAN_KUNIT_TEST) += test_kasan.o
lib/test_sysfs.c new file +953 -0

953 lines not on this page · raw message

tools/testing/selftests/sysfs/Makefile new file +12 -0
new file mode 100644
index 000000000000..fde99caa2338
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Makefile for sysfs selftests.
+
+# No binaries, but make sure arg-less "make" doesn't trigger "run_tests".
+all:
+
+TEST_PROGS := sysfs.sh
+
+include ../lib.mk
+
+# Nothing to clean up.
+clean:
tools/testing/selftests/sysfs/config new file +2 -0
new file mode 100644
index 000000000000..9196f452ecd5
@@ -0,0 +1,2 @@
+CONFIG_SYSFS=m
+CONFIG_TEST_SYSFS=m
tools/testing/selftests/sysfs/sysfs.sh new file +1202 -0

1,202 lines not on this page · raw message

Signature · 1 line
2.27.0
#7 lore raw

PATCH 2/4 kernfs: add initial failure injection support

In reply to Luis Chamberlain

This adds initial failure injection support to kernfs. We start off with debug knobs which when enabled allow test drivers, such as test_sysfs, to then make use of these to try to force certain difficult races to take place with a high degree of certainty.

This only adds runtime code *iff* the new bool CONFIG_FAIL_KERNFS_KNOBS is enabled in your kernel. If you don't have this enabled this provides no new functional. When CONFIG_FAIL_KERNFS_KNOBS is disabled the new routine kernfs_debug_should_wait() ends up being transformed to if (false), and so the compiler should optimize these out as dead code producing no new effective binary changes.

We start off with enabling failure injections in kernfs by allowing us to alter the way kernfs_fop_write_iter() behaves. We allow for the routine kernfs_fop_write_iter() to wait for a certain condition in the kernel to occur, after which it will sleep a predefined amount of time. This lets kernfs users to time exactly when it want kernfs_fop_write_iter() to complete, allowing for developing race conditions and test for correctness in kernfs.

You'd boot with this enabled on your kernel command line:

fail_kernfs_fop_write_iter=1,100,0,1

The values are <interval,probability,size,times>, we don't care for size, so for now we ignore it. The above ensures a failure will trigger only once.

*How* we allow for this routine to change behaviour is left to knobs we expose under debugfs:

 # ls -1 /sys/kernel/debug/kernfs/config_fail_kernfs_fop_write_iter/
wait_after_active
wait_after_mutex
wait_at_start
wait_before_mutex

A debugfs entry also exists to allow us to sleep a configurabler amount of time after the completion:

/sys/kernel/debug/kernfs/sleep_after_wait_ms

These two sets of knobs allow us to construct races and demonstrate how the kernfs active reference should suffice to project against races.

Enabling CONFIG_FAULT_INJECTION_DEBUG_FS enables us to configure the differnt fault injection parametres for the new fail_kernfs_fop_write_iter fault injection at run time:

ls -1 /sys/kernel/debug/kernfs/fail_kernfs_fop_write_iter/ interval
probability
space
task-filter
times
verbose
verbose_ratelimit_burst verbose_ratelimit_interval_ms

---
 .../fault-injection/fault-injection.rst       | 22 +++++
 MAINTAINERS                                   |  2 +-
 fs/kernfs/Makefile                            |  1 +
 fs/kernfs/failure-injection.c                 | 82 +++++++++++++++++++
 fs/kernfs/file.c                              | 13 +++
 fs/kernfs/kernfs-internal.h                   | 73 +++++++++++++++++
 include/linux/kernfs.h                        |  5 ++
 lib/Kconfig.debug                             | 10 +++
 8 files changed, 207 insertions(+), 1 deletion(-)
 create mode 100644 fs/kernfs/failure-injection.c
Documentation/fault-injection/fault-injection.rst +22 -0
index 31ecfe44e5b4..1cbeb1d16a23 100644
@@ -24,6 +24,28 @@ Available fault injection capabilities
 
   injects futex deadlock and uaddr fault errors.
 
+- fail_kernfs_fop_write_iter
+
+  Allows for failures to be enabled inside kernfs_fop_write_iter(). Enabling
+  this does not immediately enable any errors to occur. You must configure
+  how you want this routine to fail or change behaviour by using the debugfs
+  knobs for it:
+
+  # ls -1 /sys/kernel/debug/kernfs/config_fail_kernfs_fop_write_iter/
+  wait_after_active
+  wait_after_mutex
+  wait_at_start
+  wait_before_mutex
+
+  You can also configure how long to sleep after a wait under
+
+  /sys/kernel/debug/kernfs/sleep_after_wait_ms
+
+  If you enable CONFIG_FAULT_INJECTION_DEBUG_FS the fail_add_disk failure
+  injection parameters are placed under:
+
+  /sys/kernel/debug/kernfs/fail_kernfs_fop_write_iter/
+
 - fail_make_request
 
   injects disk IO errors on devices permitted by setting
MAINTAINERS +1 -1
index 76f6bee86770..41e12dfaa528 100644
@@ -10065,7 +10065,7 @@ M:	Greg Kroah-Hartman <[email protected]>
 M:	Tejun Heo <[email protected]>
 S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git
-F:	fs/kernfs/
+F:	fs/kernfs/*
 F:	include/linux/kernfs.h
 
 KEXEC
fs/kernfs/Makefile +1 -0
index 4ca54ff54c98..bc5b32ca39f9 100644
@@ -4,3 +4,4 @@
 #
 
 obj-y		:= mount.o inode.o dir.o file.o symlink.o
+obj-$(CONFIG_FAIL_KERNFS_KNOBS)    += failure-injection.o
fs/kernfs/failure-injection.c new file +82 -0

82 lines not on this page · raw message

fs/kernfs/file.c +13 -0
index c75719312147..d45954600f11 100644
@@ -259,6 +259,9 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)
 	const struct kernfs_ops *ops;
 	char *buf;
 
+	if (kernfs_debug_should_wait(kernfs_fop_write_iter, at_start))
+		kernfs_debug_wait();
+
 	if (of->atomic_write_len) {
 		if (len > of->atomic_write_len)
 			return -E2BIG;
@@ -280,17 +283,27 @@ static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)
 	}
 	buf[len] = '\0';	/* guarantee string termination */
 
+	if (kernfs_debug_should_wait(kernfs_fop_write_iter, before_mutex))
+		kernfs_debug_wait();
+
 	/*
 	 * @of->mutex nests outside active ref and is used both to ensure that
 	 * the ops aren't called concurrently for the same open file.
 	 */
 	mutex_lock(&of->mutex);
+
+	if (kernfs_debug_should_wait(kernfs_fop_write_iter, after_mutex))
+		kernfs_debug_wait();
+
 	if (!kernfs_get_active(of->kn)) {
 		mutex_unlock(&of->mutex);
 		len = -ENODEV;
 		goto out_free;
 	}
 
+	if (kernfs_debug_should_wait(kernfs_fop_write_iter, after_active))
+		kernfs_debug_wait();
+
 	ops = kernfs_ops(of->kn);
 	if (ops->write)
 		len = ops->write(of, buf, len, iocb->ki_pos);
fs/kernfs/kernfs-internal.h +73 -0

83 lines not on this page · raw message

include/linux/kernfs.h +5 -0
index 9e8ca8743c26..0da36b8e18e1 100644
@@ -410,6 +410,11 @@ void kernfs_init(void);
 
 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
 						   u64 id);
+
+#ifdef CONFIG_FAIL_KERNFS_KNOBS
+extern struct completion kernfs_debug_wait_completion;
+#endif
+
 #else	/* CONFIG_KERNFS */
 
 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
lib/Kconfig.debug +10 -0
index f9b9074c40f4..9d3c0d0db0f3 100644
@@ -1917,6 +1917,16 @@ config FAULT_INJECTION_USERCOPY
 	  Provides fault-injection capability to inject failures
 	  in usercopy functions (copy_from_user(), get_user(), ...).
 
+config FAIL_KERNFS_KNOBS
+	bool "Fault-injection support in kernfs"
+	depends on FAULT_INJECTION
+	help
+	  Provide fault-injection capability for kernfs. This only enables
+	  the error injection functionality. To use it you must configure which
+	  which path you want to trigger on error on using debugfs under
+	  /sys/kernel/debug/kernfs/config_fail_kernfs_fop_write_iter/. By
+	  default all of these are disabled.
+
 config FAIL_MAKE_REQUEST
 	bool "Fault-injection capability for disk IO"
 	depends on FAULT_INJECTION && BLOCK
Signature · 1 line
2.27.0
#9 lore raw

PATCH 3/4 test_sysfs: add support to use kernfs failure injection

In reply to Luis Chamberlain

This extends test_sysfs with support for using the failure injection wait completion and knobs to force a few race conditions which demonstrates that kernfs active reference protection is sufficient for kobject / device protection at higher layers.

This adds 4 new tests which tries to remove the device attribute store operation in 4 different situations:

1) at the start of kernfs_kernfs_fop_write_iter()
2) before the of->mutex is held in kernfs_kernfs_fop_write_iter()
3) after the of->mutex is held in kernfs_kernfs_fop_write_iter()
4) after the kernfs node active reference is taken

A write fails in call cases.

No null dereferences are reproduced, even though this has been observed in some complex testing cases [0]. If this issue really exists we should have enough tools on the sysfs_test toolbox now to try to reproduce this easily without having to poke around other drivers.

[0] https://lkml.kernel.org/r/[email protected]

---
 lib/Kconfig.debug                      |   3 +
 lib/test_sysfs.c                       |  31 +++++
 tools/testing/selftests/sysfs/config   |   3 +
 tools/testing/selftests/sysfs/sysfs.sh | 174 +++++++++++++++++++++++++
 4 files changed, 211 insertions(+)
lib/Kconfig.debug +3 -0
index 9d3c0d0db0f3..61bf0bad15ec 100644
@@ -2356,6 +2356,9 @@ config TEST_SYSCTL
 config TEST_SYSFS
 	tristate "sysfs test driver"
 	depends on SYSFS
+	select FAULT_INJECTION
+	select FAULT_INJECTION_DEBUG_FS
+	select FAIL_KERNFS_KNOBS
 	help
 	  This builds the "test_sysfs" module. This driver enables to test the
 	  sysfs file system safely without affecting production knobs which
lib/test_sysfs.c +31 -0
index 8e844e30e8f9..96af668edd4a 100644
@@ -38,6 +38,11 @@
 #include <linux/rtnetlink.h>
 #include <linux/genhd.h>
 #include <linux/blkdev.h>
+#include <linux/kernfs.h>
+
+#ifdef CONFIG_FAIL_KERNFS_KNOBS
+MODULE_IMPORT_NS(KERNFS_DEBUG_PRIVATE);
+#endif
 
 static bool enable_lock;
 module_param(enable_lock, bool_enable_only, 0644);
@@ -82,6 +87,13 @@ static bool enable_verbose_rmmod;
 module_param(enable_verbose_rmmod, bool_enable_only, 0644);
 MODULE_PARM_DESC(enable_verbose_rmmod, "enable verbose print messages on rmmod");
 
+#ifdef CONFIG_FAIL_KERNFS_KNOBS
+static bool enable_completion_on_rmmod;
+module_param(enable_completion_on_rmmod, bool_enable_only, 0644);
+MODULE_PARM_DESC(enable_completion_on_rmmod,
+		 "enable sending a kernfs completion on rmmod");
+#endif
+
 static int sysfs_test_major;
 
 /**
@@ -289,6 +301,12 @@ static ssize_t config_show(struct device *dev,
 			"enable_verbose_writes:\t%s\n",
 			enable_verbose_writes ? "true" : "false");
 
+#ifdef CONFIG_FAIL_KERNFS_KNOBS
+	len += snprintf(buf+len, PAGE_SIZE - len,
+			"enable_completion_on_rmmod:\t%s\n",
+			enable_completion_on_rmmod ? "true" : "false");
+#endif
+
 	test_dev_config_unlock(test_dev);
 
 	return len;
@@ -936,10 +954,23 @@ static int __init test_sysfs_init(void)
 }
 module_init(test_sysfs_init);
 
+#ifdef CONFIG_FAIL_KERNFS_KNOBS
+/* The goal is to race our device removal with a pending kernfs -> store call */
+static void test_sysfs_kernfs_send_completion_rmmod(void)
+{
+	if (!enable_completion_on_rmmod)
+		return;
+	complete(&kernfs_debug_wait_completion);
+}
+#else
+static inline void test_sysfs_kernfs_send_completion_rmmod(void) {}
+#endif
+
 static void __exit test_sysfs_exit(void)
 {
 	if (enable_debugfs)
 		debugfs_remove(debugfs_dir);
+	test_sysfs_kernfs_send_completion_rmmod();
 	if (delay_rmmod_ms)
 		msleep(delay_rmmod_ms);
 	unregister_test_dev_sysfs(first_test_dev);
tools/testing/selftests/sysfs/config +3 -0
index 9196f452ecd5..2876a229f95b 100644
@@ -1,2 +1,5 @@
 CONFIG_SYSFS=m
 CONFIG_TEST_SYSFS=m
+CONFIG_FAULT_INJECTION=y
+CONFIG_FAULT_INJECTION_DEBUG_FS=y
+CONFIG_FAIL_KERNFS_KNOBS=y
tools/testing/selftests/sysfs/sysfs.sh +174 -0

210 lines not on this page · raw message

Signature · 1 line
2.27.0
#10 lore raw

PATCH 4/4 test_sysfs: demonstrate deadlock fix

In reply to Luis Chamberlain

Two mechanisms have been proposed to fix the sysfs deadlock issue. The first approach proposed is by optionally allowing drivers to specify a module and augmenting attributes with module information [0]. A secondary approach is to use macros on drivers which needs this, in the meantime. This embraces the secondary approach, in lieu of agreement of a generic solution. This should be enough to allow for room for experimentation and demonstration of the issue.

This then also enables the two test cases which we have disabled as otherwise they would deadlock your system.

./tools/testing/selftests/sysfs/sysfs.sh -t 0027 Running test: sysfs_test_0027 - run #0
Test for possible rmmod deadlock while writing x ... ok

./tools/testing/selftests/sysfs/sysfs.sh -t 0028
Running test: sysfs_test_0028 - run #0
Test for possible rmmod deadlock using rtnl_lock while writing x ... ok

[0] https://lkml.kernel.org/r/[email protected]

---
 lib/test_sysfs.c                       | 71 ++++++++++++++++++++++----
 tools/testing/selftests/sysfs/sysfs.sh |  4 +-
 2 files changed, 64 insertions(+), 11 deletions(-)
lib/test_sysfs.c +62 -9
index 96af668edd4a..0a77f2887c87 100644
@@ -94,6 +94,59 @@ MODULE_PARM_DESC(enable_completion_on_rmmod,
 		 "enable sending a kernfs completion on rmmod");
 #endif
 
+#undef __ATTR_RO
+#undef __ATTR_RW
+#undef __ATTR_WO
+
+#define __ATTR_RO(_name) {						\
+	.attr	= { .name = __stringify(_name), .mode = 0444 },		\
+	.show	= module_##_name##_show,						\
+}
+#define __ATTR_RW(_name) __ATTR(_name, 0644, module_##_name##_show, module_##_name##_store)
+#define __ATTR_WO(_name) {						\
+	.attr	= { .name = __stringify(_name), .mode = 0200 },		\
+	.store	= module_##_name##_store,				\
+}
+
+#define MODULE_DEVICE_ATTR_FUNC_STORE(_name) \
+static ssize_t module_ ## _name ## _store(struct device *dev, \
+				   struct device_attribute *attr, \
+				   const char *buf, size_t len) \
+{ \
+	ssize_t __ret; \
+	if (!try_module_get(THIS_MODULE)) \
+		return -ENODEV; \
+	__ret = _name ## _store(dev, attr, buf, len); \
+	module_put(THIS_MODULE); \
+	return __ret; \
+}
+
+#define MODULE_DEVICE_ATTR_FUNC_SHOW(_name) \
+static ssize_t module_ ## _name ## _show(struct device *dev, \
+					 struct device_attribute *attr, \
+					 char *buf) \
+{ \
+	ssize_t __ret; \
+	if (!try_module_get(THIS_MODULE)) \
+		return -ENODEV; \
+	__ret = _name ## _show(dev, attr, buf); \
+	module_put(THIS_MODULE); \
+	return __ret; \
+}
+
+#define MODULE_DEVICE_ATTR_WO(_name) \
+MODULE_DEVICE_ATTR_FUNC_STORE(_name); \
+static DEVICE_ATTR_WO(_name)
+
+#define MODULE_DEVICE_ATTR_RW(_name) \
+MODULE_DEVICE_ATTR_FUNC_STORE(_name); \
+MODULE_DEVICE_ATTR_FUNC_SHOW(_name); \
+static DEVICE_ATTR_RW(_name)
+
+#define MODULE_DEVICE_ATTR_RO(_name) \
+MODULE_DEVICE_ATTR_FUNC_SHOW(_name); \
+static DEVICE_ATTR_RO(_name)
+
 static int sysfs_test_major;
 
 /**
@@ -311,7 +364,7 @@ static ssize_t config_show(struct device *dev,
 
 	return len;
 }
-static DEVICE_ATTR_RO(config);
+MODULE_DEVICE_ATTR_RO(config);
 
 static ssize_t reset_store(struct device *dev,
 			   struct device_attribute *attr,
@@ -336,7 +389,7 @@ static ssize_t reset_store(struct device *dev,
 
 	return count;
 }
-static DEVICE_ATTR_WO(reset);
+MODULE_DEVICE_ATTR_WO(reset);
 
 static void test_dev_busy_alloc(struct sysfs_test_device *test_dev)
 {
@@ -388,7 +441,7 @@ static ssize_t test_dev_x_show(struct device *dev,
 
 	return ret;
 }
-static DEVICE_ATTR_RW(test_dev_x);
+MODULE_DEVICE_ATTR_RW(test_dev_x);
 
 static ssize_t test_dev_y_store(struct device *dev,
 				struct device_attribute *attr,
@@ -432,7 +485,7 @@ static ssize_t test_dev_y_show(struct device *dev,
 
 	return ret;
 }
-static DEVICE_ATTR_RW(test_dev_y);
+MODULE_DEVICE_ATTR_RW(test_dev_y);
 
 static ssize_t config_enable_lock_store(struct device *dev,
 					struct device_attribute *attr,
@@ -477,7 +530,7 @@ static ssize_t config_enable_lock_show(struct device *dev,
 
 	return ret;
 }
-static DEVICE_ATTR_RW(config_enable_lock);
+MODULE_DEVICE_ATTR_RW(config_enable_lock);
 
 static ssize_t config_enable_lock_on_rmmod_store(struct device *dev,
 						 struct device_attribute *attr,
@@ -519,7 +572,7 @@ static ssize_t config_enable_lock_on_rmmod_show(struct device *dev,
 
 	return ret;
 }
-static DEVICE_ATTR_RW(config_enable_lock_on_rmmod);
+MODULE_DEVICE_ATTR_RW(config_enable_lock_on_rmmod);
 
 static ssize_t config_use_rtnl_lock_store(struct device *dev,
 					  struct device_attribute *attr,
@@ -558,7 +611,7 @@ static ssize_t config_use_rtnl_lock_show(struct device *dev,
 
 	return snprintf(buf, PAGE_SIZE, "%d\n", config->use_rtnl_lock);
 }
-static DEVICE_ATTR_RW(config_use_rtnl_lock);
+MODULE_DEVICE_ATTR_RW(config_use_rtnl_lock);
 
 static ssize_t config_write_delay_msec_y_store(struct device *dev,
 					       struct device_attribute *attr,
@@ -592,7 +645,7 @@ static ssize_t config_write_delay_msec_y_show(struct device *dev,
 
 	return snprintf(buf, PAGE_SIZE, "%d\n", config->write_delay_msec_y);
 }
-static DEVICE_ATTR_RW(config_write_delay_msec_y);
+MODULE_DEVICE_ATTR_RW(config_write_delay_msec_y);
 
 static ssize_t config_enable_busy_alloc_store(struct device *dev,
 					      struct device_attribute *attr,
@@ -626,7 +679,7 @@ static ssize_t config_enable_busy_alloc_show(struct device *dev,
 
 	return snprintf(buf, PAGE_SIZE, "%d\n", config->enable_busy_alloc);
 }
-static DEVICE_ATTR_RW(config_enable_busy_alloc);
+MODULE_DEVICE_ATTR_RW(config_enable_busy_alloc);
 
 #define TEST_SYSFS_DEV_ATTR(name)		(&dev_attr_##name.attr)
 
tools/testing/selftests/sysfs/sysfs.sh +2 -2
index f27ea61e0e95..2de9f37cb00b 100755
@@ -60,8 +60,8 @@ ALL_TESTS="$ALL_TESTS 0023:1:1:test_dev_y:block"
 ALL_TESTS="$ALL_TESTS 0024:1:1:test_dev_x:block"
 ALL_TESTS="$ALL_TESTS 0025:1:1:test_dev_y:block"
 ALL_TESTS="$ALL_TESTS 0026:1:1:test_dev_y:block"
-ALL_TESTS="$ALL_TESTS 0027:1:0:test_dev_x:block" # deadlock test
-ALL_TESTS="$ALL_TESTS 0028:1:0:test_dev_x:block" # deadlock test with rntl_lock
+ALL_TESTS="$ALL_TESTS 0027:1:1:test_dev_x:block" # deadlock test
+ALL_TESTS="$ALL_TESTS 0028:1:1:test_dev_x:block" # deadlock test with rntl_lock
 ALL_TESTS="$ALL_TESTS 0029:1:1:test_dev_x:block" # kernfs race removal of store
 ALL_TESTS="$ALL_TESTS 0030:1:1:test_dev_x:block" # kernfs race removal before mutex
 ALL_TESTS="$ALL_TESTS 0031:1:1:test_dev_x:block" # kernfs race removal after mutex
Signature · 1 line
2.27.0
maintainer
#2 lore raw

Re: [PATCH 1/4] selftests: add tests_sysfs module

In reply to Luis Chamberlain

lib/test_sysfs.c line 12

Quoted 13 lines · Luis Chamberlain
lib/test_sysfs.c new file +12 -0
@@ -0,0 +1,953 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * sysfs test driver
+ *
+ * Copyright (C) 2021 Luis Chamberlain <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or at your option any
+ * later version; or, when distributed separately from the Linux kernel or
+ * when incorporated into other software packages, subject to the following
+ * license:

This boilerplate should not be here, only the spdx line is needed.

lib/test_sysfs.c line 16

Quoted 4 lines
lib/test_sysfs.c new file +4 -0
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of copyleft-next (version 0.3.1 or later) as published
+ * at http://copyleft-next.org/.

Please no, this is a totally different license :(

#8 lore raw

Re: [PATCH 2/4] kernfs: add initial failure injection support

In reply to Luis Chamberlain
I love your patch! Yet something to improve:

Hi Luis,

I love your patch! Yet something to improve:

[auto build test ERROR on kselftest/next]
[also build test ERROR on driver-core/driver-core-testing char-misc/char-misc-testing linus/master v5.13 next-20210701] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Luis-Chamberlain/selftests-add-a-new-test-driver-for-sysfs/20210702-130705
base:   https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
config: um-x86_64_defconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/fcddd9018e5563327904b362642d8f5156b73298
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Luis-Chamberlain/selftests-add-a-new-test-driver-for-sysfs/20210702-130705 git checkout fcddd9018e5563327904b362642d8f5156b73298
# save the attached .config to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=um SUBARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

fs/kernfs/file.c: In function 'kernfs_fop_write_iter':

>> fs/kernfs/file.c:262:62: error: macro "kernfs_debug_should_wait" passed 2 arguments, but takes just 1
     262 |  if (kernfs_debug_should_wait(kernfs_fop_write_iter, at_start))
         |                                                              ^
   In file included from fs/kernfs/file.c:19:
   fs/kernfs/kernfs-internal.h:199: note: macro "kernfs_debug_should_wait" defined here
     199 | #define kernfs_debug_should_wait(when) (false)
         | 
>> fs/kernfs/file.c:262:6: error: 'kernfs_debug_should_wait' undeclared (first use in this function); did you mean 'kernfs_debug_wait'?
     262 |  if (kernfs_debug_should_wait(kernfs_fop_write_iter, at_start))
         |      ^~~~~~~~~~~~~~~~~~~~~~~~
         |      kernfs_debug_wait

fs/kernfs/file.c:262:6: note: each undeclared identifier is reported only once for each function it appears in fs/kernfs/file.c:286:66: error: macro "kernfs_debug_should_wait" passed 2 arguments, but takes just 1
286 | if (kernfs_debug_should_wait(kernfs_fop_write_iter, before_mutex))
| ^
In file included from fs/kernfs/file.c:19:
fs/kernfs/kernfs-internal.h:199: note: macro "kernfs_debug_should_wait" defined here
199 | #define kernfs_debug_should_wait(when) (false)
|
fs/kernfs/file.c:295:65: error: macro "kernfs_debug_should_wait" passed 2 arguments, but takes just 1
295 | if (kernfs_debug_should_wait(kernfs_fop_write_iter, after_mutex))
| ^
In file included from fs/kernfs/file.c:19:
fs/kernfs/kernfs-internal.h:199: note: macro "kernfs_debug_should_wait" defined here
199 | #define kernfs_debug_should_wait(when) (false)
|
fs/kernfs/file.c:304:66: error: macro "kernfs_debug_should_wait" passed 2 arguments, but takes just 1
304 | if (kernfs_debug_should_wait(kernfs_fop_write_iter, after_active))
| ^
In file included from fs/kernfs/file.c:19:
fs/kernfs/kernfs-internal.h:199: note: macro "kernfs_debug_should_wait" defined here
199 | #define kernfs_debug_should_wait(when) (false)
|

vim +/kernfs_debug_should_wait +262 fs/kernfs/file.c

244
245 /*
246 * Copy data in from userland and pass it to the matching kernfs write
247 * operation.
248 *
249 * There is no easy way for us to know if userspace is only doing a partial
250 * write, so we don't support them. We expect the entire buffer to come on
251 * the first write. Hint: if you're writing a value, first read the file,
252 * modify only the the value you're changing, then write entire buffer
253 * back.
254 */
255 static ssize_t kernfs_fop_write_iter(struct kiocb *iocb, struct iov_iter *iter)
256 {
257 struct kernfs_open_file *of = kernfs_of(iocb->ki_filp);
258 ssize_t len = iov_iter_count(iter);
259 const struct kernfs_ops *ops;
260 char *buf;
261

 > 262		if (kernfs_debug_should_wait(kernfs_fop_write_iter, at_start))

263 kernfs_debug_wait();
264
265 if (of->atomic_write_len) {
266 if (len > of->atomic_write_len)
267 return -E2BIG;
268 } else {
269 len = min_t(size_t, len, PAGE_SIZE);
270 }
271
272 buf = of->prealloc_buf;
273 if (buf)
274 mutex_lock(&of->prealloc_mutex);
275 else
276 buf = kmalloc(len + 1, GFP_KERNEL);
277 if (!buf)
278 return -ENOMEM;
279
280 if (copy_from_iter(buf, len, iter) != len) {
281 len = -EFAULT;
282 goto out_free;
283 }
284 buf[len] = '\0'; /* guarantee string termination */
285
286 if (kernfs_debug_should_wait(kernfs_fop_write_iter, before_mutex))
287 kernfs_debug_wait();
288
289 /*
290 * @of->mutex nests outside active ref and is used both to ensure that
291 * the ops aren't called concurrently for the same open file.
292 */
293 mutex_lock(&of->mutex);
294
295 if (kernfs_debug_should_wait(kernfs_fop_write_iter, after_mutex))
296 kernfs_debug_wait();
297
298 if (!kernfs_get_active(of->kn)) {
299 mutex_unlock(&of->mutex);
300 len = -ENODEV;
301 goto out_free;
302 }
303
304 if (kernfs_debug_should_wait(kernfs_fop_write_iter, after_active))
305 kernfs_debug_wait();
306
307 ops = kernfs_ops(of->kn);
308 if (ops->write)
309 len = ops->write(of, buf, len, iocb->ki_pos);
310 else
311 len = -EINVAL;
312
313 kernfs_put_active(of->kn);
314 mutex_unlock(&of->mutex);
315
316 if (len > 0)
317 iocb->ki_pos += len;
318
319 out_free:
320 if (buf == of->prealloc_buf)
321 mutex_unlock(&of->prealloc_mutex);
322 else
323 kfree(buf);
324 return len;
325 }
326

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw

Re: [PATCH 1/4] selftests: add tests_sysfs module

In reply to Greg KH
Quoted 16 lines · Greg KH

lib/test_sysfs.c line 12

Quoted 13 lines · Luis Chamberlain
lib/test_sysfs.c new file +12 -0
@@ -0,0 +1,953 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * sysfs test driver
+ *
+ * Copyright (C) 2021 Luis Chamberlain <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or at your option any
+ * later version; or, when distributed separately from the Linux kernel or
+ * when incorporated into other software packages, subject to the following
+ * license:

This boilerplate should not be here, only the spdx line is needed.

As per Documentation/process/license-rules.rst we use the SPDX license tag for the license that applies but it also states about dual licensing:

"Aside from that, individual files can be provided under a dual license, e.g. one of the compatible GPL variants and alternatively under a permissive license like BSD, MIT etc."

Let me know if things should change somehow here to clarify this better.

Quoted 6 lines

lib/test_sysfs.c line 16

Quoted 4 lines
lib/test_sysfs.c new file +4 -0
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of copyleft-next (version 0.3.1 or later) as published
+ * at http://copyleft-next.org/.

Please no, this is a totally different license :(

Dual licensing copyleft-next / GPLv2 was discussed in 2016 and I have been using it since for my new drivers. As far as the kernel is concerned only the GPLv2 applies and this is cleary clarified with the MODULE_LICENSE("GPL") as per Linus' preference [0] on this topic. Later due to Ted's and Alans's request I ironed out an "or" language clause to use [1]. This was also vetted by 2 attorneys at SUSE, and one at Red Hat [2]. The first driver submission under this dual strategy was lib/test_sysctl.c through commit 9308f2f9e7f05 ("test_sysctl: add dedicated proc sysctl test driver") merged in July 2017. Shortly after that I also added test_kmod through commit d9c6a72d6fa29 ("kmod: add test driver to stress test the module loader") in the same month. These two drivers went in just a few months before the SPDX license pratice kicked in.

And so we already have this practice in place of dual GPLv2 / copyleft-next. What was missing was the SPDX tag. I can go and update the other 2 drivers to reflect this as well, but as far as I can tell, due to the dual licensing the boilerplace is still needed in this case.

Let me know!

[0] https://lore.kernel.org/lkml/CA+55aFyhxcvD+q7tp+-yrSFDKfR0mOHgyEAe=f_94aKLsOu0Og@mail.gmail.com/
[1] https://lkml.kernel.org/r/[email protected]
[2] https://lore.kernel.org/lkml/[email protected]/

Luis

maintainer
#4 lore raw

Re: [PATCH 1/4] selftests: add tests_sysfs module

Quoted 22 lines · Luis Chamberlain
Quoted 16 lines · Greg KH

lib/test_sysfs.c line 12

Quoted 13 lines · Luis Chamberlain
lib/test_sysfs.c new file +12 -0
@@ -0,0 +1,953 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * sysfs test driver
+ *
+ * Copyright (C) 2021 Luis Chamberlain <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or at your option any
+ * later version; or, when distributed separately from the Linux kernel or
+ * when incorporated into other software packages, subject to the following
+ * license:

This boilerplate should not be here, only the spdx line is needed.

As per Documentation/process/license-rules.rst we use the SPDX license tag for the license that applies but it also states about dual licensing:

Quoted 5 lines

"Aside from that, individual files can be provided under a dual license, e.g. one of the compatible GPL variants and alternatively under a permissive license like BSD, MIT etc."

Let me know if things should change somehow here to clarify this better.

The spdx line is not matching the actual license for the file, which is wrong.

And "copyright-left" is not a valid license according to our list of valid licenses in the LICENSES directory, so please do not add it to kernel code when it is obviously not needed.

And given that this is directly interacting with sysfs, which is GPLv2-only, trying to claim a different license on the code that tests it is going to be a total mess for any lawyer who wants to look into this. Just keep it simple please.

thanks,

greg k-h

#5 lore raw

Re: [PATCH 1/4] selftests: add tests_sysfs module

Quoted 23 lines · Greg KH
Quoted 22 lines · Luis Chamberlain
Quoted 16 lines · Greg KH

lib/test_sysfs.c line 12

Quoted 13 lines · Luis Chamberlain
lib/test_sysfs.c new file +12 -0
@@ -0,0 +1,953 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * sysfs test driver
+ *
+ * Copyright (C) 2021 Luis Chamberlain <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or at your option any
+ * later version; or, when distributed separately from the Linux kernel or
+ * when incorporated into other software packages, subject to the following
+ * license:

This boilerplate should not be here, only the spdx line is needed.

As per Documentation/process/license-rules.rst we use the SPDX license tag for the license that applies but it also states about dual licensing:

Quoted 8 lines
Quoted 5 lines

"Aside from that, individual files can be provided under a dual license, e.g. one of the compatible GPL variants and alternatively under a permissive license like BSD, MIT etc."

Let me know if things should change somehow here to clarify this better.

The spdx line is not matching the actual license for the file, which is wrong.

We don't have spdx license tag yet for copyleft-next, and although when using dual gplv2 or copyleft-next gplv2 applies I did fail to see can use spdx for dual licensing such as:

# SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause

Quoted 3 lines

And "copyright-left" is not a valid license according to our list of valid licenses in the LICENSES directory, so please do not add it to kernel code when it is obviously not needed.

You mean copyleft-next. Yes I'd have to add that. Given that we already have two test drivers with that license I'll go ahead and add that.

Quoted 4 lines

And given that this is directly interacting with sysfs, which is GPLv2-only, trying to claim a different license on the code that tests it is going to be a total mess for any lawyer who wants to look into this. Just keep it simple please.

The faul injection code I added follows the exact license for sysfs. The only interaction with the test_sysfs and sysfs is an exported symbol for a completion structure. The other dual gpl OR copyleft-next test drivers already present in the kernel also use exported symbols too, so I see nothing new here.

Luis