Skip to contentSkip to conversation

sched,livepatch: Untangle cond_resched() and live-patching

PATCH Applied Sebastian Andrzej Siewior 7 participants 11 messages May 9, 2025 – May 14, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched,livepatch: Untangle cond_resched() and live-patching

From: Peter Zijlstra <[email protected]>

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

Compile tested only.

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

---
v1…v2: https://lore.kernel.org/all/[email protected]/
- Updated comments in __klp_sched_try_switch()
- Replaced unlikely with likely in __klp_sched_try_switch()
- Dropped RFC

 include/linux/livepatch_sched.h | 14 ++++-----
 include/linux/sched.h           |  6 ----
 kernel/livepatch/transition.c   | 52 ++++++++++-----------------------
 kernel/sched/core.c             | 50 +++++--------------------------
 4 files changed, 29 insertions(+), 93 deletions(-)
include/linux/livepatch_sched.h +5 -9
index 013794fb5da08..065c185f27638 100644
@@ -3,27 +3,23 @@
 #define _LINUX_LIVEPATCH_SCHED_H_
 
 #include <linux/jump_label.h>
-#include <linux/static_call_types.h>
+#include <linux/sched.h>
 
 #ifdef CONFIG_LIVEPATCH
 
 void __klp_sched_try_switch(void);
 
-#if !defined(CONFIG_PREEMPT_DYNAMIC) || !defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
-
 DECLARE_STATIC_KEY_FALSE(klp_sched_try_switch_key);
 
-static __always_inline void klp_sched_try_switch(void)
+static __always_inline void klp_sched_try_switch(struct task_struct *curr)
 {
-	if (static_branch_unlikely(&klp_sched_try_switch_key))
+	if (static_branch_unlikely(&klp_sched_try_switch_key) &&
+	    READ_ONCE(curr->__state) & TASK_FREEZABLE)
 		__klp_sched_try_switch();
 }
 
-#endif /* !CONFIG_PREEMPT_DYNAMIC || !CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */
-
 #else /* !CONFIG_LIVEPATCH */
-static inline void klp_sched_try_switch(void) {}
-static inline void __klp_sched_try_switch(void) {}
+static inline void klp_sched_try_switch(struct task_struct *curr) {}
 #endif /* CONFIG_LIVEPATCH */
 
 #endif /* _LINUX_LIVEPATCH_SCHED_H_ */
include/linux/sched.h +0 -6
index f96ac19828934..b98195991031c 100644
@@ -44,7 +44,6 @@
 #include <linux/seqlock_types.h>
 #include <linux/kcsan.h>
 #include <linux/rv.h>
-#include <linux/livepatch_sched.h>
 #include <linux/uidgid_types.h>
 #include <linux/tracepoint-defs.h>
 #include <asm/kmap_size.h>
@@ -2089,9 +2088,6 @@ extern int __cond_resched(void);
 
 #if defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
 
-void sched_dynamic_klp_enable(void);
-void sched_dynamic_klp_disable(void);
-
 DECLARE_STATIC_CALL(cond_resched, __cond_resched);
 
 static __always_inline int _cond_resched(void)
@@ -2112,7 +2108,6 @@ static __always_inline int _cond_resched(void)
 
 static inline int _cond_resched(void)
 {
-	klp_sched_try_switch();
 	return __cond_resched();
 }
 
@@ -2122,7 +2117,6 @@ static inline int _cond_resched(void)
 
 static inline int _cond_resched(void)
 {
-	klp_sched_try_switch();
 	return 0;
 }
 
kernel/livepatch/transition.c +16 -36

90 lines not on this page · raw message

kernel/sched/core.c +8 -42

110 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer Acked-by on v2 1/1
#1 lore raw

Hi,

thanks for the updated version.

Quoted 10 lines · Sebastian Andrzej Siewior

From: Peter Zijlstra <[email protected]>

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

Compile tested only.

livepatch selftests pass and I also ran some more.

Quoted 4 lines

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

A nit below if there is an another version, otherwise Petr might fix it when merging.

Quoted 7 lines
kernel/livepatch/transition.c +3 -0
@@ -365,27 +356,20 @@ static bool klp_try_switch_task(struct task_struct *task)
 
 void __klp_sched_try_switch(void)
 {
+	/*
+	 * This function is called from __schedule() while a context switch is
+	 * about to happen. Preemption is already disabled and klp_mutex

kernel/livepatch/transition.c static bool klp_try_switch_task(struct task_struct *task) · line 371

Quoted 10 lines
kernel/livepatch/transition.c +8 -0
+	 * can't be acquired.
+	 * Disabled preemption is used to prevent racing with other callers of
+	 * klp_try_switch_task(). Thanks to task_call_func() they won't be
+	 * able to switch to this task while it's running.
+	 */
+	lockdep_assert_preemption_disabled();
+
+	/* Make sure current didn't get patched */
      if (likely(!klp_patch_pending(current)))
               return;

This last comment is not precise. If !klp_patch_pending(), there is nothing to do. Fast way out. So if it was up to me, I would remove the line all together.

Miroslav

maintainer
#2 lore raw
Quoted 21 lines · Miroslav Benes

Hi,

thanks for the updated version.

Quoted 10 lines · Sebastian Andrzej Siewior

From: Peter Zijlstra <[email protected]>

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

Compile tested only.

livepatch selftests pass and I also ran some more.

Quoted 2 lines

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

Quoted 7 lines
Quoted 2 lines

A nit below if there is an another version, otherwise Petr might fix it when merging.

Petr or Peter?

That is, who are we expecting to merge this :-)

Anyway, I've zapped the line in my copy.

Quoted 23 lines
Quoted 7 lines
kernel/livepatch/transition.c +3 -0
@@ -365,27 +356,20 @@ static bool klp_try_switch_task(struct task_struct *task)
 
 void __klp_sched_try_switch(void)
 {
+	/*
+	 * This function is called from __schedule() while a context switch is
+	 * about to happen. Preemption is already disabled and klp_mutex

kernel/livepatch/transition.c static bool klp_try_switch_task(struct task_struct *task) · line 371

Quoted 10 lines
kernel/livepatch/transition.c +8 -0
+	 * can't be acquired.
+	 * Disabled preemption is used to prevent racing with other callers of
+	 * klp_try_switch_task(). Thanks to task_call_func() they won't be
+	 * able to switch to this task while it's running.
+	 */
+	lockdep_assert_preemption_disabled();
+
+	/* Make sure current didn't get patched */
      if (likely(!klp_patch_pending(current)))
               return;

This last comment is not precise. If !klp_patch_pending(), there is nothing to do. Fast way out. So if it was up to me, I would remove the line all together.

Miroslav

maintainer
#3 lore raw
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Miroslav Benes

Hi,

thanks for the updated version.

Quoted 10 lines · Sebastian Andrzej Siewior

From: Peter Zijlstra <[email protected]>

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

Compile tested only.

livepatch selftests pass and I also ran some more.

Quoted 4 lines

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

Quoted 8 lines
Quoted 4 lines

A nit below if there is an another version, otherwise Petr might fix it when merging.

Petr or Peter?

That is, who are we expecting to merge this :-)

Petr Mladek if it goes through the live patching tree, you if tip. Feel free to pick it up :).

Quoted 1 line

Anyway, I've zapped the line in my copy.

Thanks!

Miroslav

maintainer Reviewed-by Tested-by
#4 lore raw
Quoted 32 lines · Miroslav Benes
Quoted 30 lines · Peter Zijlstra
Quoted 28 lines · Miroslav Benes

Hi,

thanks for the updated version.

Quoted 10 lines · Sebastian Andrzej Siewior

From: Peter Zijlstra <[email protected]>

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

Compile tested only.

livepatch selftests pass and I also ran some more.

Quoted 4 lines

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

A nit below if there is an another version, otherwise Petr might fix it when merging.

Quoted 6 lines
Quoted 3 lines

Petr or Peter?

That is, who are we expecting to merge this :-)

Petr Mladek if it goes through the live patching tree, you if tip. Feel free to pick it up :).

IMHO, it might be easier when it goes via tip. Peter, feel free to take it.

The patch does not create any conflict with the klp tree. But I guess that there might be some dependent patches in tip...

That said, I could take it via the livepatch tree if Peter preferred it from some reasons.

Anyway, the patch looks good and passes the tests. Feel free to use:

Best Regards,
Petr

maintainer
#6 lore raw
In reply to Miroslav Benes
Quoted 24 lines · Miroslav Benes
Quoted 22 lines · Sebastian Andrzej Siewior

From: Peter Zijlstra <[email protected]>

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

kernel/livepatch/transition.c +9 -0
@@ -365,27 +356,20 @@ static bool klp_try_switch_task(struct task_struct *task)
 
 void __klp_sched_try_switch(void)
 {
+	/*
+	 * This function is called from __schedule() while a context switch is
+	 * about to happen. Preemption is already disabled and klp_mutex
+	 * can't be acquired.
+	 * Disabled preemption is used to prevent racing with other callers of
+	 * klp_try_switch_task(). Thanks to task_call_func() they won't be
+	 * able to switch to this task while it's running.
+	 */
+	lockdep_assert_preemption_disabled();
Quoted 8 lines

kernel/livepatch/transition.c static bool klp_try_switch_task(struct task_struct *task) · line 371

Quoted 4 lines
kernel/livepatch/transition.c +2 -0
+
+	/* Make sure current didn't get patched */
      if (likely(!klp_patch_pending(current)))
               return;

This last comment is not precise. If !klp_patch_pending(), there is nothing to do. Fast way out. So if it was up to me, I would remove the line all together.

I think that this is not just a speedup. Right below is a read memory barrier. It says that we need to read TIF_PATCH_PENDING here to make sure that klp_target_state is correct after the barrier.

Anyway, I agree that the comment is confusing and might be removed. The comment above the memory barrier might/should be enough.

Best Regards,
Petr

maintainer Acked-by on v2 1/1
#7 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 9 lines · Sebastian Andrzej Siewior

From: Peter Zijlstra <[email protected]>

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

Quoted 6 lines

Compile tested only.

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

Signature · 1 line
Josh
Reviewed-by on v2 1/1
#8 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 9 lines · Sebastian Andrzej Siewior

From: Peter Zijlstra <[email protected]>

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

Quoted 6 lines

Compile tested only.

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

merge bot Applied
#9 lore raw
In reply to Sebastian Andrzej Siewior
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     fe2ece0700df81ee1fc91d51d80a1a3f37e59806
Gitweb:        https://git.kernel.org/tip/fe2ece0700df81ee1fc91d51d80a1a3f37e59806
Author:        Peter Zijlstra <[email protected]>
AuthorDate:    Fri, 09 May 2025 13:36:59 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 14 May 2025 13:09:50 +02:00

sched,livepatch: Untangle cond_resched() and live-patching

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

---
 include/linux/livepatch_sched.h | 14 +++------
 include/linux/sched.h           |  6 +----
 kernel/livepatch/transition.c   | 49 ++++++++-----------------------
 kernel/sched/core.c             | 50 +++++---------------------------
 4 files changed, 27 insertions(+), 92 deletions(-)
include/linux/livepatch_sched.h +5 -9
index 013794f..065c185 100644
@@ -3,27 +3,23 @@
 #define _LINUX_LIVEPATCH_SCHED_H_
 
 #include <linux/jump_label.h>
-#include <linux/static_call_types.h>
+#include <linux/sched.h>
 
 #ifdef CONFIG_LIVEPATCH
 
 void __klp_sched_try_switch(void);
 
-#if !defined(CONFIG_PREEMPT_DYNAMIC) || !defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
-
 DECLARE_STATIC_KEY_FALSE(klp_sched_try_switch_key);
 
-static __always_inline void klp_sched_try_switch(void)
+static __always_inline void klp_sched_try_switch(struct task_struct *curr)
 {
-	if (static_branch_unlikely(&klp_sched_try_switch_key))
+	if (static_branch_unlikely(&klp_sched_try_switch_key) &&
+	    READ_ONCE(curr->__state) & TASK_FREEZABLE)
 		__klp_sched_try_switch();
 }
 
-#endif /* !CONFIG_PREEMPT_DYNAMIC || !CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */
-
 #else /* !CONFIG_LIVEPATCH */
-static inline void klp_sched_try_switch(void) {}
-static inline void __klp_sched_try_switch(void) {}
+static inline void klp_sched_try_switch(struct task_struct *curr) {}
 #endif /* CONFIG_LIVEPATCH */
 
 #endif /* _LINUX_LIVEPATCH_SCHED_H_ */
include/linux/sched.h +0 -6
index f96ac19..b981959 100644
@@ -44,7 +44,6 @@
 #include <linux/seqlock_types.h>
 #include <linux/kcsan.h>
 #include <linux/rv.h>
-#include <linux/livepatch_sched.h>
 #include <linux/uidgid_types.h>
 #include <linux/tracepoint-defs.h>
 #include <asm/kmap_size.h>
@@ -2089,9 +2088,6 @@ extern int __cond_resched(void);
 
 #if defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
 
-void sched_dynamic_klp_enable(void);
-void sched_dynamic_klp_disable(void);
-
 DECLARE_STATIC_CALL(cond_resched, __cond_resched);
 
 static __always_inline int _cond_resched(void)
@@ -2112,7 +2108,6 @@ static __always_inline int _cond_resched(void)
 
 static inline int _cond_resched(void)
 {
-	klp_sched_try_switch();
 	return __cond_resched();
 }
 
@@ -2122,7 +2117,6 @@ static inline int _cond_resched(void)
 
 static inline int _cond_resched(void)
 {
-	klp_sched_try_switch();
 	return 0;
 }
 
kernel/livepatch/transition.c +14 -35

87 lines not on this page · raw message

kernel/sched/core.c +8 -42

110 lines not on this page · raw message

merge bot Applied
#10 lore raw
In reply to Sebastian Andrzej Siewior
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     676e8cf70cb0533e1118e29898c9a9c33ae3a10f
Gitweb:        https://git.kernel.org/tip/676e8cf70cb0533e1118e29898c9a9c33ae3a10f
Author:        Peter Zijlstra <[email protected]>
AuthorDate:    Fri, 09 May 2025 13:36:59 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 14 May 2025 13:16:24 +02:00

sched,livepatch: Untangle cond_resched() and live-patching

With the goal of deprecating / removing VOLUNTARY preempt, live-patch needs to stop relying on cond_resched() to make forward progress.

Instead, rely on schedule() with TASK_FREEZABLE set. Just like live-patching, the freezer needs to be able to stop tasks in a safe / known state.

[bigeasy: use likely() in __klp_sched_try_switch() and update comments]

---
 include/linux/livepatch_sched.h | 14 +++------
 include/linux/sched.h           |  6 +----
 kernel/livepatch/transition.c   | 49 ++++++++-----------------------
 kernel/sched/core.c             | 50 +++++---------------------------
 4 files changed, 27 insertions(+), 92 deletions(-)
include/linux/livepatch_sched.h +5 -9
index 013794f..065c185 100644
@@ -3,27 +3,23 @@
 #define _LINUX_LIVEPATCH_SCHED_H_
 
 #include <linux/jump_label.h>
-#include <linux/static_call_types.h>
+#include <linux/sched.h>
 
 #ifdef CONFIG_LIVEPATCH
 
 void __klp_sched_try_switch(void);
 
-#if !defined(CONFIG_PREEMPT_DYNAMIC) || !defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
-
 DECLARE_STATIC_KEY_FALSE(klp_sched_try_switch_key);
 
-static __always_inline void klp_sched_try_switch(void)
+static __always_inline void klp_sched_try_switch(struct task_struct *curr)
 {
-	if (static_branch_unlikely(&klp_sched_try_switch_key))
+	if (static_branch_unlikely(&klp_sched_try_switch_key) &&
+	    READ_ONCE(curr->__state) & TASK_FREEZABLE)
 		__klp_sched_try_switch();
 }
 
-#endif /* !CONFIG_PREEMPT_DYNAMIC || !CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */
-
 #else /* !CONFIG_LIVEPATCH */
-static inline void klp_sched_try_switch(void) {}
-static inline void __klp_sched_try_switch(void) {}
+static inline void klp_sched_try_switch(struct task_struct *curr) {}
 #endif /* CONFIG_LIVEPATCH */
 
 #endif /* _LINUX_LIVEPATCH_SCHED_H_ */
include/linux/sched.h +0 -6
index f96ac19..b981959 100644
@@ -44,7 +44,6 @@
 #include <linux/seqlock_types.h>
 #include <linux/kcsan.h>
 #include <linux/rv.h>
-#include <linux/livepatch_sched.h>
 #include <linux/uidgid_types.h>
 #include <linux/tracepoint-defs.h>
 #include <asm/kmap_size.h>
@@ -2089,9 +2088,6 @@ extern int __cond_resched(void);
 
 #if defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
 
-void sched_dynamic_klp_enable(void);
-void sched_dynamic_klp_disable(void);
-
 DECLARE_STATIC_CALL(cond_resched, __cond_resched);
 
 static __always_inline int _cond_resched(void)
@@ -2112,7 +2108,6 @@ static __always_inline int _cond_resched(void)
 
 static inline int _cond_resched(void)
 {
-	klp_sched_try_switch();
 	return __cond_resched();
 }
 
@@ -2122,7 +2117,6 @@ static inline int _cond_resched(void)
 
 static inline int _cond_resched(void)
 {
-	klp_sched_try_switch();
 	return 0;
 }
 
kernel/livepatch/transition.c +14 -35

87 lines not on this page · raw message

kernel/sched/core.c +8 -42

110 lines not on this page · raw message