public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: John Stultz <jstultz@google.com>
To: LKML <linux-kernel@vger.kernel.org>
Cc: John Stultz <jstultz@google.com>,
	Joel Fernandes <joelaf@google.com>,
	 Qais Yousef <qyousef@google.com>, Ingo Molnar <mingo@redhat.com>,
	 Peter Zijlstra <peterz@infradead.org>,
	Juri Lelli <juri.lelli@redhat.com>,
	 Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	 Valentin Schneider <vschneid@redhat.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	 Ben Segall <bsegall@google.com>,
	Zimuzo Ezeozue <zezeozue@google.com>,
	 Youssef Esmat <youssefesmat@google.com>,
	Mel Gorman <mgorman@suse.de>,
	 Daniel Bristot de Oliveira <bristot@redhat.com>,
	Will Deacon <will@kernel.org>, Waiman Long <longman@redhat.com>,
	 Boqun Feng <boqun.feng@gmail.com>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	 Metin Kaya <Metin.Kaya@arm.com>,
	Xuewen Yan <xuewen.yan94@gmail.com>,
	 K Prateek Nayak <kprateek.nayak@amd.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	kernel-team@android.com
Subject: [PATCH v7 17/23] sched: Initial sched_football test implementation
Date: Tue, 19 Dec 2023 16:18:28 -0800	[thread overview]
Message-ID: <20231220001856.3710363-18-jstultz@google.com> (raw)
In-Reply-To: <20231220001856.3710363-1-jstultz@google.com>

Reimplementation of the sched_football test from LTP:
https://github.com/linux-test-project/ltp/blob/master/testcases/realtime/func/sched_football/sched_football.c

But reworked to run in the kernel and utilize mutexes
to illustrate proper boosting of low priority mutex
holders.

TODO:
* Need a rt_mutex version so it can work w/o proxy-execution
* Need a better place to put it

Cc: Joel Fernandes <joelaf@google.com>
Cc: Qais Yousef <qyousef@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Juri Lelli <juri.lelli@redhat.com>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>
Cc: Valentin Schneider <vschneid@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ben Segall <bsegall@google.com>
Cc: Zimuzo Ezeozue <zezeozue@google.com>
Cc: Youssef Esmat <youssefesmat@google.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Daniel Bristot de Oliveira <bristot@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: Waiman Long <longman@redhat.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Metin Kaya <Metin.Kaya@arm.com>
Cc: Xuewen Yan <xuewen.yan94@gmail.com>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: kernel-team@android.com
Signed-off-by: John Stultz <jstultz@google.com>
---
 kernel/sched/Makefile              |   1 +
 kernel/sched/test_sched_football.c | 242 +++++++++++++++++++++++++++++
 lib/Kconfig.debug                  |  14 ++
 3 files changed, 257 insertions(+)
 create mode 100644 kernel/sched/test_sched_football.c

diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index 976092b7bd45..2729d565dfd7 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -32,3 +32,4 @@ obj-y += core.o
 obj-y += fair.o
 obj-y += build_policy.o
 obj-y += build_utility.o
+obj-$(CONFIG_SCHED_RT_INVARIENT_TEST) += test_sched_football.o
diff --git a/kernel/sched/test_sched_football.c b/kernel/sched/test_sched_football.c
new file mode 100644
index 000000000000..9742c45c0fe0
--- /dev/null
+++ b/kernel/sched/test_sched_football.c
@@ -0,0 +1,242 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Module-based test case for RT scheduling invariant
+ *
+ * A reimplementation of my old sched_football test
+ * found in LTP:
+ *   https://github.com/linux-test-project/ltp/blob/master/testcases/realtime/func/sched_football/sched_football.c
+ *
+ * Similar to that test, this tries to validate the RT
+ * scheduling invariant, that the across N available cpus, the
+ * top N priority tasks always running.
+ *
+ * This is done via having N offsensive players that are
+ * medium priority, which constantly are trying to increment the
+ * ball_pos counter.
+ *
+ * Blocking this, are N defensive players that are higher
+ * priority which just spin on the cpu, preventing the medium
+ * priroity tasks from running.
+ *
+ * To complicate this, there are also N defensive low priority
+ * tasks. These start first and each aquire one of N mutexes.
+ * The high priority defense tasks will later try to grab the
+ * mutexes and block, opening a window for the offsensive tasks
+ * to run and increment the ball. If priority inheritance or
+ * proxy execution is used, the low priority defense players
+ * should be boosted to the high priority levels, and will
+ * prevent the mid priority offensive tasks from running.
+ *
+ * Copyright © International Business Machines  Corp., 2007, 2008
+ * Copyright (C) Google, 2023
+ *
+ * Authors: John Stultz <jstultz@google.com>
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/kthread.h>
+#include <linux/delay.h>
+#include <linux/sched/rt.h>
+#include <linux/spinlock.h>
+#include <linux/mutex.h>
+#include <linux/rwsem.h>
+#include <linux/smp.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/sched.h>
+#include <uapi/linux/sched/types.h>
+#include <linux/rtmutex.h>
+
+atomic_t players_ready;
+atomic_t ball_pos;
+int players_per_team;
+bool game_over;
+
+struct mutex *mutex_low_list;
+struct mutex *mutex_mid_list;
+
+static inline
+struct task_struct *create_fifo_thread(int (*threadfn)(void *data), void *data,
+				       char *name, int prio)
+{
+	struct task_struct *kth;
+	struct sched_attr attr = {
+		.size		= sizeof(struct sched_attr),
+		.sched_policy	= SCHED_FIFO,
+		.sched_nice	= 0,
+		.sched_priority	= prio,
+	};
+	int ret;
+
+	kth = kthread_create(threadfn, data, name);
+	if (IS_ERR(kth)) {
+		pr_warn("%s eerr, kthread_create failed\n", __func__);
+		return kth;
+	}
+	ret = sched_setattr_nocheck(kth, &attr);
+	if (ret) {
+		kthread_stop(kth);
+		pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
+		return ERR_PTR(ret);
+	}
+
+	wake_up_process(kth);
+	return kth;
+}
+
+int defense_low_thread(void *arg)
+{
+	long tnum = (long)arg;
+
+	atomic_inc(&players_ready);
+	mutex_lock(&mutex_low_list[tnum]);
+	while (!READ_ONCE(game_over)) {
+		if (kthread_should_stop())
+			break;
+		schedule();
+	}
+	mutex_unlock(&mutex_low_list[tnum]);
+	return 0;
+}
+
+int defense_mid_thread(void *arg)
+{
+	long tnum = (long)arg;
+
+	atomic_inc(&players_ready);
+	mutex_lock(&mutex_mid_list[tnum]);
+	mutex_lock(&mutex_low_list[tnum]);
+	while (!READ_ONCE(game_over)) {
+		if (kthread_should_stop())
+			break;
+		schedule();
+	}
+	mutex_unlock(&mutex_low_list[tnum]);
+	mutex_unlock(&mutex_mid_list[tnum]);
+	return 0;
+}
+
+int offense_thread(void *)
+{
+	atomic_inc(&players_ready);
+	while (!READ_ONCE(game_over)) {
+		if (kthread_should_stop())
+			break;
+		schedule();
+		atomic_inc(&ball_pos);
+	}
+	return 0;
+}
+
+int defense_hi_thread(void *arg)
+{
+	long tnum = (long)arg;
+
+	atomic_inc(&players_ready);
+	mutex_lock(&mutex_mid_list[tnum]);
+	while (!READ_ONCE(game_over)) {
+		if (kthread_should_stop())
+			break;
+		schedule();
+	}
+	mutex_unlock(&mutex_mid_list[tnum]);
+	return 0;
+}
+
+int crazy_fan_thread(void *)
+{
+	int count = 0;
+
+	atomic_inc(&players_ready);
+	while (!READ_ONCE(game_over)) {
+		if (kthread_should_stop())
+			break;
+		schedule();
+		udelay(1000);
+		msleep(2);
+		count++;
+	}
+	return 0;
+}
+
+int ref_thread(void *arg)
+{
+	struct task_struct *kth;
+	long game_time = (long)arg;
+	unsigned long final_pos;
+	long i;
+
+	pr_info("%s: started ref, game_time: %ld secs !\n", __func__,
+		game_time);
+
+	/* Create low  priority defensive team */
+	for (i = 0; i < players_per_team; i++)
+		kth = create_fifo_thread(defense_low_thread, (void *)i,
+					 "defese-low-thread", 2);
+	/* Wait for the defense threads to start */
+	while (atomic_read(&players_ready) < players_per_team)
+		msleep(1);
+
+	for (i = 0; i < players_per_team; i++)
+		kth = create_fifo_thread(defense_mid_thread,
+					 (void *)(players_per_team - i - 1),
+					 "defese-mid-thread", 3);
+	/* Wait for the defense threads to start */
+	while (atomic_read(&players_ready) < players_per_team * 2)
+		msleep(1);
+
+	/* Create mid priority offensive team */
+	for (i = 0; i < players_per_team; i++)
+		kth = create_fifo_thread(offense_thread, NULL,
+					 "offense-thread", 5);
+	/* Wait for the offense threads to start */
+	while (atomic_read(&players_ready) < players_per_team * 3)
+		msleep(1);
+
+	/* Create high priority defensive team */
+	for (i = 0; i < players_per_team; i++)
+		kth = create_fifo_thread(defense_hi_thread, (void *)i,
+					 "defese-hi-thread", 10);
+	/* Wait for the defense threads to start */
+	while (atomic_read(&players_ready) < players_per_team * 4)
+		msleep(1);
+
+	/* Create high priority defensive team */
+	for (i = 0; i < players_per_team; i++)
+		kth = create_fifo_thread(crazy_fan_thread, NULL,
+					 "crazy-fan-thread", 15);
+	/* Wait for the defense threads to start */
+	while (atomic_read(&players_ready) < players_per_team * 5)
+		msleep(1);
+
+	pr_info("%s: all players checked in! Starting game.\n", __func__);
+	atomic_set(&ball_pos, 0);
+	msleep(game_time * 1000);
+	final_pos = atomic_read(&ball_pos);
+	pr_info("%s: final ball_pos: %ld\n", __func__, final_pos);
+	WARN_ON(final_pos != 0);
+	game_over = true;
+	return 0;
+}
+
+static int __init test_sched_football_init(void)
+{
+	struct task_struct *kth;
+	int i;
+
+	players_per_team = num_online_cpus();
+
+	mutex_low_list = kmalloc_array(players_per_team,  sizeof(struct mutex), GFP_ATOMIC);
+	mutex_mid_list = kmalloc_array(players_per_team,  sizeof(struct mutex), GFP_ATOMIC);
+
+	for (i = 0; i < players_per_team; i++) {
+		mutex_init(&mutex_low_list[i]);
+		mutex_init(&mutex_mid_list[i]);
+	}
+
+	kth = create_fifo_thread(ref_thread, (void *)10, "ref-thread", 20);
+
+	return 0;
+}
+module_init(test_sched_football_init);
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 4405f81248fb..1d90059d190f 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1238,6 +1238,20 @@ config SCHED_DEBUG
 	  that can help debug the scheduler. The runtime overhead of this
 	  option is minimal.
 
+config SCHED_RT_INVARIENT_TEST
+	tristate "RT invarient scheduling tester"
+	depends on DEBUG_KERNEL
+	help
+	  This option provides a kernel module that runs tests to make
+	  sure the RT invarient holds (top N priority tasks run on N
+	  available cpus).
+
+	  Say Y here if you want kernel rt scheduling tests
+	  to be built into the kernel.
+	  Say M if you want this test to build as a module.
+	  Say N if you are unsure.
+
+
 config SCHED_INFO
 	bool
 	default n
-- 
2.43.0.472.g3155946c3a-goog


  parent reply	other threads:[~2023-12-20  0:19 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-20  0:18 [PATCH v7 00/23] Proxy Execution: A generalized form of Priority Inheritance v7 John Stultz
2023-12-20  0:18 ` [PATCH v7 01/23] sched: Unify runtime accounting across classes John Stultz
2023-12-20  0:18 ` [PATCH v7 02/23] locking/mutex: Remove wakeups from under mutex::wait_lock John Stultz
2023-12-20  0:18 ` [PATCH v7 03/23] locking/mutex: Make mutex::wait_lock irq safe John Stultz
2023-12-20  0:18 ` [PATCH v7 04/23] locking/mutex: Expose __mutex_owner() John Stultz
2023-12-20  0:18 ` [PATCH v7 05/23] locking/mutex: Rework task_struct::blocked_on John Stultz
2023-12-21 10:13   ` Metin Kaya
2023-12-21 17:52     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 06/23] sched: Add CONFIG_SCHED_PROXY_EXEC & boot argument to enable/disable John Stultz
2023-12-20  1:04   ` Randy Dunlap
2023-12-21 17:05     ` John Stultz
2023-12-28 15:06   ` Metin Kaya
2024-01-10 22:36     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 07/23] locking/mutex: Switch to mutex handoffs for CONFIG_SCHED_PROXY_EXEC John Stultz
2023-12-20  0:18 ` [PATCH v7 08/23] sched: Split scheduler and execution contexts John Stultz
2023-12-21 10:43   ` Metin Kaya
2023-12-21 18:23     ` John Stultz
2024-01-03 14:49   ` Valentin Schneider
2024-01-10 22:24     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 09/23] sched: Fix runtime accounting w/ split exec & sched contexts John Stultz
2024-01-03 13:47   ` Valentin Schneider
2023-12-20  0:18 ` [PATCH v7 10/23] sched: Split out __sched() deactivate task logic into a helper John Stultz
2023-12-21 12:30   ` Metin Kaya
2023-12-21 18:49     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 11/23] sched: Add a initial sketch of the find_proxy_task() function John Stultz
2023-12-21 12:55   ` Metin Kaya
2023-12-21 19:12     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 12/23] sched: Fix proxy/current (push,pull)ability John Stultz
2023-12-21 15:03   ` Metin Kaya
2023-12-21 21:02     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 13/23] sched: Start blocked_on chain processing in find_proxy_task() John Stultz
2023-12-21 15:30   ` Metin Kaya
2023-12-20  0:18 ` [PATCH v7 14/23] sched: Handle blocked-waiter migration (and return migration) John Stultz
2023-12-21 16:12   ` Metin Kaya
2023-12-21 19:46     ` John Stultz
2024-01-02 15:33     ` Phil Auld
2024-01-04 23:33       ` John Stultz
2023-12-20  0:18 ` [PATCH v7 15/23] sched: Add blocked_donor link to task for smarter mutex handoffs John Stultz
2023-12-20  0:18 ` [PATCH v7 16/23] sched: Add deactivated (sleeping) owner handling to find_proxy_task() John Stultz
2023-12-22  8:33   ` Metin Kaya
2024-01-04 23:25     ` John Stultz
2023-12-20  0:18 ` John Stultz [this message]
2023-12-20  0:59   ` [PATCH v7 17/23] sched: Initial sched_football test implementation Randy Dunlap
2023-12-20  2:37     ` John Stultz
2023-12-22  9:32   ` Metin Kaya
2024-01-05  5:20     ` John Stultz
2023-12-28 15:19   ` Metin Kaya
2024-01-05  5:22     ` John Stultz
2023-12-28 16:36   ` Metin Kaya
2024-01-05  5:25     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 18/23] sched: Add push_task_chain helper John Stultz
2023-12-22 10:32   ` Metin Kaya
2023-12-20  0:18 ` [PATCH v7 19/23] sched: Consolidate pick_*_task to task_is_pushable helper John Stultz
2023-12-22 10:23   ` Metin Kaya
2024-01-04 23:44     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 20/23] sched: Push execution and scheduler context split into deadline and rt paths John Stultz
2023-12-22 11:33   ` Metin Kaya
2024-01-05  0:01     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 21/23] sched: Add find_exec_ctx helper John Stultz
2023-12-22 11:57   ` Metin Kaya
2024-01-05  3:12     ` John Stultz
2023-12-20  0:18 ` [PATCH v7 22/23] sched: Refactor dl/rt find_lowest/latest_rq logic John Stultz
2023-12-22 13:52   ` Metin Kaya
2023-12-20  0:18 ` [PATCH v7 23/23] sched: Fix rt/dl load balancing via chain level balance John Stultz
2023-12-22 14:51   ` Metin Kaya
2024-01-05  3:42     ` John Stultz
2023-12-21  8:35 ` [PATCH v7 00/23] Proxy Execution: A generalized form of Priority Inheritance v7 Metin Kaya
2023-12-21 17:13   ` 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=20231220001856.3710363-18-jstultz@google.com \
    --to=jstultz@google.com \
    --cc=Metin.Kaya@arm.com \
    --cc=boqun.feng@gmail.com \
    --cc=bristot@redhat.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=joelaf@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kernel-team@android.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=qyousef@google.com \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=will@kernel.org \
    --cc=xuewen.yan94@gmail.com \
    --cc=youssefesmat@google.com \
    --cc=zezeozue@google.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