sched: Add a generic function to return the preemption string
authorSebastian Andrzej Siewior <bigeasy@linutronix.de>
Fri, 14 Mar 2025 16:08:02 +0000 (17:08 +0100)
committerPeter Zijlstra <peterz@infradead.org>
Mon, 17 Mar 2025 10:23:38 +0000 (11:23 +0100)
The individual architectures often add the preemption model to the begin
of the backtrace. This is the case on X86 or ARM64 for the "die" case
but not for regular warning. With the addition of DYNAMIC_PREEMPT for
PREEMPT_RT we end up with CONFIG_PREEMPT and CONFIG_PREEMPT_RT set
simultaneously. That means that everyone who tried to add that piece of
information gets it wrong for PREEMPT_RT because PREEMPT is checked
first.

Provide a generic function which returns the current scheduling model
considering LAZY preempt and the current state of PREEMPT_DYNAMIC.

The resulting strings are:
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
┃   Model   ┃  -RT -DYN    ┃     +RT -DYN      ┃     -RT +DYN       ┃     +RT +DYN      ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
│NONE       │ NONE         │ n/a               │ PREEMPT(none)      │ n/a               │
├───────────┼──────────────┼───────────────────┼────────────────────┼───────────────────┤
│VOLUNTARY  │ VOLUNTARY    │ n/a               │ PREEMPT(voluntary) │ n/a               │
├───────────┼──────────────┼───────────────────┼────────────────────┼───────────────────┤
│FULL       │ PREEMPT      │ PREEMPT_RT        │ PREEMPT(full)      │ PREEMPT_{RT,full} │
├───────────┼──────────────┼───────────────────┼────────────────────┼───────────────────┤
│LAZY       │ PREEMPT_LAZY │ PREEMPT_{RT,LAZY} │ PREEMPT(lazy)      │ PREEMPT_{RT,lazy} │
└───────────┴──────────────┴───────────────────┴────────────────────┴───────────────────┘

[ The dynamic building of the string can lead to an empty string if the
  function is invoked simultaneously on two CPUs. ]

Co-developed-by: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Signed-off-by: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Co-developed-by: "Steven Rostedt (Google)" <rostedt@goodmis.org>
Signed-off-by: "Steven Rostedt (Google)" <rostedt@goodmis.org>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Shrikanth Hegde <sshegde@linux.ibm.com>
Link: https://lore.kernel.org/r/20250314160810.2373416-2-bigeasy@linutronix.de
include/linux/preempt.h
kernel/sched/core.c
kernel/sched/debug.c
kernel/sched/sched.h

index ca86235ac15c010346d371515036650825c30a2e..3e9808f2b5491cc827fb6b6ac1eae296894cac39 100644 (file)
@@ -515,6 +515,8 @@ static inline bool preempt_model_rt(void)
        return IS_ENABLED(CONFIG_PREEMPT_RT);
 }
 
+extern const char *preempt_model_str(void);
+
 /*
  * Does the preemption model allow non-cooperative preemption?
  *
index 03d7b63dc3e5491a3b2d62284085117c4eeb751c..c734724f7ff7d895351016c54726082469cc6380 100644 (file)
@@ -7646,10 +7646,57 @@ PREEMPT_MODEL_ACCESSOR(lazy);
 
 #else /* !CONFIG_PREEMPT_DYNAMIC: */
 
+#define preempt_dynamic_mode -1
+
 static inline void preempt_dynamic_init(void) { }
 
 #endif /* CONFIG_PREEMPT_DYNAMIC */
 
+const char *preempt_modes[] = {
+       "none", "voluntary", "full", "lazy", NULL,
+};
+
+const char *preempt_model_str(void)
+{
+       bool brace = IS_ENABLED(CONFIG_PREEMPT_RT) &&
+               (IS_ENABLED(CONFIG_PREEMPT_DYNAMIC) ||
+                IS_ENABLED(CONFIG_PREEMPT_LAZY));
+       static char buf[128];
+
+       if (IS_ENABLED(CONFIG_PREEMPT_BUILD)) {
+               struct seq_buf s;
+
+               seq_buf_init(&s, buf, sizeof(buf));
+               seq_buf_puts(&s, "PREEMPT");
+
+               if (IS_ENABLED(CONFIG_PREEMPT_RT))
+                       seq_buf_printf(&s, "%sRT%s",
+                                      brace ? "_{" : "_",
+                                      brace ? "," : "");
+
+               if (IS_ENABLED(CONFIG_PREEMPT_DYNAMIC)) {
+                       seq_buf_printf(&s, "(%s)%s",
+                                      preempt_dynamic_mode > 0 ?
+                                      preempt_modes[preempt_dynamic_mode] : "undef",
+                                      brace ? "}" : "");
+                       return seq_buf_str(&s);
+               }
+
+               if (IS_ENABLED(CONFIG_PREEMPT_LAZY)) {
+                       seq_buf_printf(&s, "LAZY%s",
+                                      brace ? "}" : "");
+                       return seq_buf_str(&s);
+               }
+
+               return seq_buf_str(&s);
+       }
+
+       if (IS_ENABLED(CONFIG_PREEMPT_VOLUNTARY_BUILD))
+               return "VOLUNTARY";
+
+       return "NONE";
+}
+
 int io_schedule_prepare(void)
 {
        int old_iowait = current->in_iowait;
index ef047add7f9e622828bc3b25c8d0ec043ce66df1..39be73969d28455649848a11bafd7a00476b7bc8 100644 (file)
@@ -244,11 +244,13 @@ static ssize_t sched_dynamic_write(struct file *filp, const char __user *ubuf,
 
 static int sched_dynamic_show(struct seq_file *m, void *v)
 {
-       static const char * preempt_modes[] = {
-               "none", "voluntary", "full", "lazy",
-       };
-       int j = ARRAY_SIZE(preempt_modes) - !IS_ENABLED(CONFIG_ARCH_HAS_PREEMPT_LAZY);
        int i = IS_ENABLED(CONFIG_PREEMPT_RT) * 2;
+       int j;
+
+       /* Count entries in NULL terminated preempt_modes */
+       for (j = 0; preempt_modes[j]; j++)
+               ;
+       j -= !IS_ENABLED(CONFIG_ARCH_HAS_PREEMPT_LAZY);
 
        for (; i < j; i++) {
                if (preempt_dynamic_mode == i)
index 0212a0c5534af668ffa55ed7851616163eb8cc8d..e8915ad5652c79e0995ca43e7c9945995e515dde 100644 (file)
@@ -3633,6 +3633,7 @@ extern int preempt_dynamic_mode;
 extern int sched_dynamic_mode(const char *str);
 extern void sched_dynamic_update(int mode);
 #endif
+extern const char *preempt_modes[];
 
 #ifdef CONFIG_SCHED_MM_CID