mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: jstultz@google.com, peterz@infradead.org, mingo@redhat.com,
	juri.lelli@redhat.com, vincent.guittot@linaro.org
Cc: linux-kernel@vger.kernel.org,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	dietmar.eggemann@arm.com, rostedt@goodmis.org,
	bsegall@google.com, mgorman@suse.de, vschneid@redhat.com,
	kprateek.nayak@amd.com, connoro@google.com, arighi@nvidia.com,
	akpm@linux-foundation.org, shuah@kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [RFC PATCH v1 7/8] lib: Add test_proxy_exec, kernel mutexes that user space can hold
Date: Mon, 16 Nov 2026 14:26:37 -0500	[thread overview]
Message-ID: <20261116192638.233125-8-mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <20261116192638.233125-1-mathieu.desnoyers@efficios.com>

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@efficios.com>
---
 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


  parent reply	other threads:[~2026-11-16 19:26 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
2026-11-16 19:26 ` [RFC PATCH v1 1/8] sched/proxy: Reschedule when a blocked task is woken under a proxy Mathieu Desnoyers
2026-11-16 21:12   ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 2/8] sched/proxy: Wake blocked RT and deadline tasks through the full path Mathieu Desnoyers
2026-11-16 21:35   ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 3/8] sched/proxy: Stop the chain walk when the blocked_on chain is a cycle Mathieu Desnoyers
2026-11-16 20:36   ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 4/8] sched/proxy: Tell the class about a proxy migration Mathieu Desnoyers
2026-11-16 20:42   ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating Mathieu Desnoyers
2026-11-16 19:26 ` [RFC PATCH v1 6/8] sched/deadline: Use the root domain a proxy-migrated task runs in Mathieu Desnoyers
2026-11-16 19:26 ` Mathieu Desnoyers [this message]
2026-11-16 19:26 ` [RFC PATCH v1 8/8] selftests/sched: Add tests of proxy execution Mathieu Desnoyers
2026-11-16 20:27 ` [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests John Stultz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116192638.233125-8-mathieu.desnoyers@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=akpm@linux-foundation.org \
    --cc=arighi@nvidia.com \
    --cc=bsegall@google.com \
    --cc=connoro@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jstultz@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®