public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Michal Hocko <mhocko@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>
Cc: Mel Gorman <mgorman@suse.de>,
	Frederic Weisbecker <fweisbecker@suse.de>,
	Ingo Molnar <mingo@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Michal Hocko <mhocko@suse.com>
Subject: [RFC PATCH v2 2/5] kernel: allow to configure PREEMPT_NONE, PREEMPT_VOLUNTARY on kernel command line
Date: Fri,  9 Oct 2020 14:29:23 +0200	[thread overview]
Message-ID: <20201009122926.29962-3-mhocko@kernel.org> (raw)
In-Reply-To: <20201009122926.29962-1-mhocko@kernel.org>

From: Michal Hocko <mhocko@suse.com>

Many people are still relying on pre built distribution kernels and so
distributions have to provide mutliple kernel flavors to offer different
preemption models. Most of them are providing PREEMPT_NONE for typical
server deployments and PREEMPT_VOLUNTARY for desktop users.

Having two different kernel binaries differing only by the preemption
mode seems rather wasteful and inflexible. Especially when the difference
between PREEMPT_NONE and PREEMPT_VOLUNTARY is really minimal. Both only
allow explicit scheduling points while running in the kernel and it is
only might_sleep which adds additional preemption points for
PREEMPT_VOLUNTARY.

Add a kernel command line parameter preempt_mode=[none, voluntary] which
allows to override the default compile time preemption mode
(CONFIG_PREEMPT_NONE resp. CONFIG_PREEMPT_VOLUTARY). Voluntary preempt
mode is guarded by a jump label to prevent any potential runtime overhead.

Add an explicit include of jump_label to gpio/consumer.h to make sure
all its consumers will get static_branch_likely) as kernel.h cannot
include it directly.

Signed-off-by: Michal Hocko <mhocko@suse.com>
---
 .../admin-guide/kernel-parameters.txt         |  5 ++++
 include/linux/gpio/consumer.h                 |  1 +
 include/linux/kernel.h                        | 13 ++++++--
 kernel/sched/core.c                           | 30 +++++++++++++++++++
 4 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index a1068742a6df..96bb74faeb50 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3873,6 +3873,11 @@
 			Format: {"off"}
 			Disable Hardware Transactional Memory
 
+	preempt_mode={none,voluntary}
+			Set the preemption mode.
+			none - equivalent to CONFIG_PREEMPT_NONE
+			voluntary - equivalent to CONFIG_PREEMPT_VOLUNTARY
+
 	print-fatal-signals=
 			[KNL] debug: print fatal signals
 
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
index 901aab89d025..d64e6dda5755 100644
--- a/include/linux/gpio/consumer.h
+++ b/include/linux/gpio/consumer.h
@@ -3,6 +3,7 @@
 #define __LINUX_GPIO_CONSUMER_H
 
 #include <linux/bits.h>
+#include <linux/jump_label.h>
 #include <linux/bug.h>
 #include <linux/compiler_types.h>
 #include <linux/err.h>
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index c25b8e41c0ea..d2d37bd5ecd5 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -14,6 +14,7 @@
 #include <linux/typecheck.h>
 #include <linux/printk.h>
 #include <linux/build_bug.h>
+#include <linux/jump_label_type.h>
 #include <asm/byteorder.h>
 #include <asm/div64.h>
 #include <uapi/linux/kernel.h>
@@ -192,11 +193,19 @@ struct completion;
 struct pt_regs;
 struct user;
 
+#ifndef CONFIG_PREEMPTION
 #ifdef CONFIG_PREEMPT_VOLUNTARY
+DECLARE_STATIC_KEY_TRUE(preempt_voluntary_key);
+#else
+DECLARE_STATIC_KEY_FALSE(preempt_voluntary_key);
+#endif
+
 extern int _cond_resched(void);
-# define might_resched() _cond_resched()
+# define might_resched() \
+	do { if (static_branch_likely(&preempt_voluntary_key)) _cond_resched(); } while (0)
 #else
-# define might_resched() do { } while (0)
+# define might_resched() \
+	do { } while (0)
 #endif
 
 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 2d95dc3f4644..07d37d862637 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -43,6 +43,14 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(sched_update_nr_running_tp);
 
 DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
 
+#ifdef CONFIG_PREEMPT_VOLUNTARY
+DEFINE_STATIC_KEY_TRUE(preempt_voluntary_key);
+#else
+/* PREEMPT_NONE vs PREEMPT_VOLUNTARY */
+DEFINE_STATIC_KEY_FALSE(preempt_voluntary_key);
+#endif
+EXPORT_SYMBOL(preempt_voluntary_key);
+
 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_JUMP_LABEL)
 /*
  * Debugging: various feature bits
@@ -8482,3 +8490,25 @@ void call_trace_sched_update_nr_running(struct rq *rq, int count)
 {
         trace_sched_update_nr_running_tp(rq, count);
 }
+
+#ifndef CONFIG_PREEMPTION
+static int __init setup_non_preempt_mode(char *str)
+{
+	if (!strcmp(str, "none")) {
+		if (IS_ENABLED(CONFIG_PREEMPT_VOLUNTARY)) {
+			static_branch_disable(&preempt_voluntary_key);
+			pr_info("Switching to PREEMPT_NONE mode.");
+		}
+	} else if (!strcmp(str, "voluntary")) {
+		if (!IS_ENABLED(CONFIG_PREEMPT_VOLUNTARY)) {
+			static_branch_enable(&preempt_voluntary_key);
+			pr_info("Switching to PREEMPT_VOLUNTARY mode.");
+		}
+	} else {
+		pr_warn("Unsupported preempt mode %s\n", str);
+		return 1;
+	}
+	return 0;
+}
+__setup("preempt_mode=", setup_non_preempt_mode);
+#endif
-- 
2.28.0


  parent reply	other threads:[~2020-10-09 12:30 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-07 12:04 [RFC PATCH] kernel: allow to configure PREEMPT_NONE, PREEMPT_VOLUNTARY on kernel command line Michal Hocko
2020-10-07 12:19 ` Peter Zijlstra
2020-10-07 12:29   ` Michal Hocko
2020-10-07 13:01     ` Mel Gorman
2020-10-07 12:21 ` Peter Zijlstra
2020-10-07 12:35   ` Michal Hocko
2020-10-09  9:47     ` Peter Zijlstra
2020-10-09 10:14       ` Michal Hocko
2020-10-09 10:20         ` Peter Zijlstra
2020-10-09 10:48           ` Michal Hocko
2020-10-09 11:17             ` Michal Hocko
2020-10-09 11:26               ` Michal Hocko
2020-10-09 11:39             ` Peter Zijlstra
2020-10-09  9:12 ` Michal Hocko
2020-10-09  9:42   ` Peter Zijlstra
2020-10-09 10:10     ` Michal Hocko
2020-10-09 10:14       ` Peter Zijlstra
2020-10-09 10:37         ` Michal Hocko
2020-10-09 11:42           ` Peter Zijlstra
2020-10-09 12:29 ` [RFC PATCH v2 0/5] allow overriding default preempt mode from " Michal Hocko
2020-10-09 12:29   ` [RFC PATCH v2 1/5] jump_label: split out declaration parts into its own headers Michal Hocko
2020-10-09 12:29   ` Michal Hocko [this message]
2020-10-09 12:29   ` [RFC PATCH v2 3/5] kernel: ARCH_NO_PREEMPT shouldn't exclude PREEMPT_VOLUNTARY Michal Hocko
2020-10-09 12:29   ` [RFC PATCH v2 4/5] kernel: introduce CONFIG_PREEMPT_DYNAMIC Michal Hocko
2020-10-09 12:29   ` [RFC PATCH v2 5/5] kernel: drop PREEMPT_NONE compile time option Michal Hocko
2020-10-09 12:50   ` [RFC PATCH v2 0/5] allow overriding default preempt mode from command line Peter Zijlstra
2020-10-09 13:03     ` Michal Hocko
2020-10-09 13:22       ` Peter Zijlstra
2020-10-09 17:45   ` Peter Zijlstra
2020-10-27 12:22     ` Frederic Weisbecker
2020-10-27 12:28       ` Peter Zijlstra

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=20201009122926.29962-3-mhocko@kernel.org \
    --to=mhocko@kernel.org \
    --cc=fweisbecker@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /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