[RFC PATCH v1 7/8] lib: Add test_proxy_exec, kernel mutexes that user space can hold

From: Mathieu Desnoyers

Date: Mon Nov 16 2026 - 14:27:14 EST


Proxy execution acts on tasks blocked on a struct mutex. To test it,
user space must block tasks on a kernel mutex in a chosen way:
- hold the mutex for a chosen time;
- block on it from a chosen task and CPU;
- take two mutexes in opposite orders.

No system call gives that control. The mutexes that system calls take
are held for a short time that user space does not choose. Their
locking can change with the subsystem. And user space cannot make two
tasks deadlock on two kernel mutexes.

Add a test module with eight mutexes and a debugfs file. A write to the
file runs one command:
- hold a mutex while busy or asleep;
- take a mutex, interruptibly or not;
- take two mutexes, one inside the other. The inner lock is killable,
so that a test can end a deadlock it made.
The write returns when the command is done.

The selftest added by the next patch uses it.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
---
lib/Kconfig.debug | 11 ++++
lib/Makefile | 1 +
lib/test_proxy_exec.c | 132 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 144 insertions(+)
create mode 100644 lib/test_proxy_exec.c

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 2d22638ec13a..8d032d7fa387 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1361,6 +1361,17 @@ config TEST_CSD_LOCK

If unsure, say N.

+config TEST_PROXY_EXEC
+ tristate "Test module for proxy execution"
+ depends on DEBUG_FS
+ help
+ This builds the "test_proxy_exec" module. It creates kernel
+ mutexes that user space can hold, take and chain through
+ debugfs. The selftests in tools/testing/selftests/sched/proxy_exec
+ use it to block tasks on a mutex in a chosen way.
+
+ If unsure, say N.
+
config TEST_LOCKUP
tristate "Test module to generate lockups"
depends on m
diff --git a/lib/Makefile b/lib/Makefile
index a2afe954805c..8add2a1473df 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -101,6 +101,7 @@ obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o
obj-$(CONFIG_TEST_CSD_LOCK) += test_csd_lock.o
obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o
+obj-$(CONFIG_TEST_PROXY_EXEC) += test_proxy_exec.o
obj-$(CONFIG_TEST_HMM) += test_hmm.o
obj-$(CONFIG_TEST_FREE_PAGES) += test_free_pages.o
obj-$(CONFIG_TEST_REF_TRACKER) += test_ref_tracker.o
diff --git a/lib/test_proxy_exec.c b/lib/test_proxy_exec.c
new file mode 100644
index 000000000000..d3f7af3222e8
--- /dev/null
+++ b/lib/test_proxy_exec.c
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test module for proxy execution: kernel mutexes that user space can
+ * hold, take and chain.
+ *
+ * Proxy execution acts on tasks blocked on a struct mutex. User space
+ * cannot hold a kernel mutex for a chosen time, nor make two tasks take
+ * two of them in a chosen order. This module lets it do both. See
+ * tools/testing/selftests/sched/proxy_exec.
+ *
+ * Write one command to <debugfs>/test_proxy_exec/op:
+ * hold I MS lock mutex I, busy-wait MS ms, unlock
+ * hold_sleep I MS lock mutex I, sleep MS ms, unlock
+ * take I lock mutex I, unlock
+ * take_intr I mutex_lock_interruptible(I), unlock if acquired
+ * pair A B MS lock A, busy-wait MS ms, lock B killable, unlock both
+ * The write returns when the command is done. It fails with EINTR if a
+ * signal interrupted take_intr or the inner lock of pair.
+ *
+ * Two tasks that run pair with the mutexes in opposite orders deadlock.
+ * A fatal signal ends the deadlock.
+ */
+#include <linux/debugfs.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/ktime.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/uaccess.h>
+
+#define TPE_NR_MUTEX 8
+#define TPE_MAX_MS 10000
+
+static struct mutex tpe_mutex[TPE_NR_MUTEX];
+/* One lockdep class per mutex: "pair" nests them. */
+static struct lock_class_key tpe_key[TPE_NR_MUTEX];
+static struct dentry *tpe_dir;
+
+/*
+ * Busy-wait @ms milliseconds. The caller can be preempted. cond_resched()
+ * gives a kernel without preemption a place to do it.
+ */
+static void tpe_busy_wait(unsigned int ms)
+{
+ ktime_t end = ktime_add_ms(ktime_get(), ms);
+
+ while (ktime_before(ktime_get(), end)) {
+ cpu_relax();
+ cond_resched();
+ }
+}
+
+static ssize_t tpe_op_write(struct file *file, const char __user *ubuf,
+ size_t len, loff_t *ppos)
+{
+ unsigned int a, b, ms = 0;
+ char buf[64];
+ int ret = 0;
+
+ if (len >= sizeof(buf))
+ return -EINVAL;
+ if (copy_from_user(buf, ubuf, len))
+ return -EFAULT;
+ buf[len] = '\0';
+
+ if (sscanf(buf, "hold_sleep %u %u", &a, &ms) == 2) {
+ if (a >= TPE_NR_MUTEX || ms > TPE_MAX_MS)
+ return -EINVAL;
+ mutex_lock(&tpe_mutex[a]);
+ msleep(ms);
+ mutex_unlock(&tpe_mutex[a]);
+ } else if (sscanf(buf, "hold %u %u", &a, &ms) == 2) {
+ if (a >= TPE_NR_MUTEX || ms > TPE_MAX_MS)
+ return -EINVAL;
+ mutex_lock(&tpe_mutex[a]);
+ tpe_busy_wait(ms);
+ mutex_unlock(&tpe_mutex[a]);
+ } else if (sscanf(buf, "take_intr %u", &a) == 1) {
+ if (a >= TPE_NR_MUTEX)
+ return -EINVAL;
+ ret = mutex_lock_interruptible(&tpe_mutex[a]);
+ if (!ret)
+ mutex_unlock(&tpe_mutex[a]);
+ } else if (sscanf(buf, "take %u", &a) == 1) {
+ if (a >= TPE_NR_MUTEX)
+ return -EINVAL;
+ mutex_lock(&tpe_mutex[a]);
+ mutex_unlock(&tpe_mutex[a]);
+ } else if (sscanf(buf, "pair %u %u %u", &a, &b, &ms) == 3) {
+ if (a >= TPE_NR_MUTEX || b >= TPE_NR_MUTEX || a == b ||
+ ms > TPE_MAX_MS)
+ return -EINVAL;
+ mutex_lock(&tpe_mutex[a]);
+ tpe_busy_wait(ms);
+ ret = mutex_lock_killable(&tpe_mutex[b]);
+ if (!ret)
+ mutex_unlock(&tpe_mutex[b]);
+ mutex_unlock(&tpe_mutex[a]);
+ } else {
+ return -EINVAL;
+ }
+ return ret ? ret : len;
+}
+
+static const struct file_operations tpe_op_fops = {
+ .owner = THIS_MODULE,
+ .write = tpe_op_write,
+ .llseek = noop_llseek,
+};
+
+static int __init tpe_init(void)
+{
+ int i;
+
+ for (i = 0; i < TPE_NR_MUTEX; i++) {
+ mutex_init(&tpe_mutex[i]);
+ lockdep_set_class(&tpe_mutex[i], &tpe_key[i]);
+ }
+ tpe_dir = debugfs_create_dir("test_proxy_exec", NULL);
+ debugfs_create_file("op", 0200, tpe_dir, NULL, &tpe_op_fops);
+ return 0;
+}
+module_init(tpe_init);
+
+static void __exit tpe_exit(void)
+{
+ debugfs_remove_recursive(tpe_dir);
+}
+module_exit(tpe_exit);
+
+MODULE_DESCRIPTION("Kernel mutexes for the proxy execution selftests");
+MODULE_LICENSE("GPL");
--
2.47.3