sched,livepatch: Untangle cond_resched() and live-patching
Subsystems (2)
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
@@ -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
@@ -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
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()withTASK_FREEZABLEset. 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]Signed-off-by Peter Zijlstra (Intel) <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
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
Peter Zijlstra
maintainerQuoted 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()withTASK_FREEZABLEset. 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
Signed-off-by Peter Zijlstra (Intel) <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>Acked-by Miroslav Benes <[email protected]>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
Miroslav Benes
maintainerQuoted 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()withTASK_FREEZABLEset. 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]Signed-off-by Peter Zijlstra (Intel) <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Quoted 8 lines
Quoted 4 lines
Acked-by Miroslav Benes <[email protected]>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
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()withTASK_FREEZABLEset. 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]Signed-off-by Peter Zijlstra (Intel) <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>Acked-by Miroslav Benes <[email protected]>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
Peter Zijlstra
maintainerQuoted 2 lines · Petr Mladek
IMHO, it might be easier when it goes via tip. Peter, feel free to take it.
Done!
Quoted 2 lines
Reviewed-by Petr Mladek <[email protected]>Tested-by Petr Mladek <[email protected]>
For some reason b4 didn't pick up these tags, added them manually and force pushed it again.
Petr Mladek
maintainerQuoted 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()withTASK_FREEZABLEset. 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
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()withTASK_FREEZABLEset. 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]Signed-off-by Peter Zijlstra (Intel) <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
Signature · 1 line
Josh
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()withTASK_FREEZABLEset. 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]Signed-off-by Peter Zijlstra (Intel) <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>
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
@@ -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
@@ -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
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
@@ -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
@@ -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