Kernel broken on processors without performance counters
Subsystems (10)
Hi
I found out that the patch a66734297f78707ce39d756b656bfae861d53f62 breaks the kernel on processors without performance counters, such as AMD K6-3. Reverting the patch fixes the problem.
The static key rdpmc_always_available somehow gets set (I couldn't really find out what is setting it, the function set_attr_rdpmc is not executed), cr4_set_bits(X86_CR4_PCE) is executed and that results in a crash on boot when attempting to execute init, because the proecssor doesn't support that bit in CR4.
Mikulas
Peter Zijlstra
maintainerQuoted 11 lines · Mikulas Patocka
Hi
I found out that the patch
a66734297f78707ce39d756b656bfae861d53f62breaks the kernel on processors without performance counters, such as AMD K6-3. Reverting the patch fixes the problem.The static key
rdpmc_always_availablesomehow gets set (I couldn't really find out what is setting it, the functionset_attr_rdpmcis not executed),cr4_set_bits(X86_CR4_PCE)is executed and that results in a crash on boot when attempting to execute init, because the proecssor doesn't support that bit in CR4.
Urgh, the static key trainwreck bites again.
One is not supposed to mix static_key_true() and STATIC_KEY_INIT_FALSE.
Does this make it go again?
--- arch/x86/include/asm/mmu_context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/mmu_context.h +1 -1
@@ -23,7 +23,7 @@ extern struct static_key rdpmc_always_available;static inline void load_mm_cr4 (struct mm_struct *mm) { -if (static_key_true (&rdpmc_always_available) || +if (static_key_false (&rdpmc_always_available) ||atomic_read (&mm->context.perf_rdpmc_allowed))cr4_set_bits (X86_CR4_PCE );else
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Mikulas Patocka
Hi
I found out that the patch
a66734297f78707ce39d756b656bfae861d53f62breaks the kernel on processors without performance counters, such as AMD K6-3. Reverting the patch fixes the problem.The static key
rdpmc_always_availablesomehow gets set (I couldn't really find out what is setting it, the functionset_attr_rdpmcis not executed),cr4_set_bits(X86_CR4_PCE)is executed and that results in a crash on boot when attempting to execute init, because the proecssor doesn't support that bit in CR4.
Quoted 5 lines
Urgh, the static key trainwreck bites again.
One is not supposed to mix
static_key_true()andSTATIC_KEY_INIT_FALSE.Does this make it go again?
Yes, this patch fixes the problem.
Mikulas
Quoted 18 lines
--- arch/x86/include/asm/mmu_context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/include/asm/mmu_context.h +1
-1@@ -23,7 +23,7 @@ extern struct static_key rdpmc_always_available;static inline void load_mm_cr4 (struct mm_struct *mm) { -if (static_key_true (&rdpmc_always_available) || +if (static_key_false (&rdpmc_always_available) ||atomic_read (&mm->context.perf_rdpmc_allowed))cr4_set_bits (X86_CR4_PCE );else
Peter Zijlstra
maintainerPATCH 1/1 x86: Fix static_key in load_mm_cr4()
Mikulas reported his K6-3 not booting. This is because the static_key confusion struck and bit Andy, this wants to be static_key_false().
--- arch/x86/include/asm/mmu_context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/mmu_context.h +1 -1
@@ -23,7 +23,7 @@ extern struct static_key rdpmc_always_available;static inline void load_mm_cr4 (struct mm_struct *mm) { -if (static_key_true (&rdpmc_always_available) || +if (static_key_false (&rdpmc_always_available) ||atomic_read (&mm->context.perf_rdpmc_allowed))cr4_set_bits (X86_CR4_PCE );else
Re: [PATCH] x86: Fix static_key in load_mm_cr4()
Quoted 9 lines · Peter Zijlstra
Mikulas reported his K6-3 not booting. This is because the
static_keyconfusion struck and bit Andy, this wants to bestatic_key_false().Fixes a66734297f78 ("perf/x86: Add /sys/devices/cpu/rdpmc=2 to allow rdpmc for all tasks")Cc Andy Lutomirski <[email protected]>Reported-by Mikulas Patocka <[email protected]>Tested-by Mikulas Patocka <[email protected]>Signed-off-by Peter Zijlstra (Intel) <[email protected]>
You should also add
Cc: [email protected] # v4.0
so that it will be backported to 4.0 and 4.1.
Quoted 18 lines
--- arch/x86/include/asm/mmu_context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/include/asm/mmu_context.h +1
-1@@ -23,7 +23,7 @@ extern struct static_key rdpmc_always_available;static inline void load_mm_cr4 (struct mm_struct *mm) { -if (static_key_true (&rdpmc_always_available) || +if (static_key_false (&rdpmc_always_available) ||atomic_read (&mm->context.perf_rdpmc_allowed))cr4_set_bits (X86_CR4_PCE );else
tip-bot for Peter Zijlstra
merge bot[tip:perf/urgent] x86, perf: Fix static_key bug in load_mm_cr4()
In reply to Peter Zijlstrax86, perf: Fix static_key bug in load_mm_cr4()
Commit-ID: a833581e372a4adae2319d8dc379493edbc444e9 Gitweb: http://git.kernel.org/tip/a833581e372a4adae2319d8dc379493edbc444e9 Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 9 Jul 2015 19:23:38 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 10 Jul 2015 10:24:38 +0200
x86, perf: Fix static_key bug in load_mm_cr4()
Mikulas reported his K6-3 not booting. This is because the static_key API confusion struck and bit Andy, this wants to be static_key_false().
--- arch/x86/include/asm/mmu_context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/mmu_context.h +1 -1
@@ -23,7 +23,7 @@ extern struct static_key rdpmc_always_available;static inline void load_mm_cr4 (struct mm_struct *mm) { -if (static_key_true (&rdpmc_always_available) || +if (static_key_false (&rdpmc_always_available) ||atomic_read (&mm->context.perf_rdpmc_allowed))cr4_set_bits (X86_CR4_PCE );else
Quoted 28 lines · Peter Zijlstra
Quoted 11 lines · Mikulas Patocka
Hi
I found out that the patch
a66734297f78707ce39d756b656bfae861d53f62breaks the kernel on processors without performance counters, such as AMD K6-3. Reverting the patch fixes the problem.The static key
rdpmc_always_availablesomehow gets set (I couldn't really find out what is setting it, the functionset_attr_rdpmcis not executed),cr4_set_bits(X86_CR4_PCE)is executed and that results in a crash on boot when attempting to execute init, because the proecssor doesn't support that bit in CR4.Urgh, the static key trainwreck bites again.
One is not supposed to mix
static_key_true()andSTATIC_KEY_INIT_FALSE.Does this make it go again?
--- arch/x86/include/asm/mmu_context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/include/asm/mmu_context.h +0
-0
arch/x86/include/asm/mmu_context.h extern struct static_key rdpmc_always_available; · line 26
Quoted 5 lines
arch/x86/include/asm/mmu_context.h +1
-1@@ -23,7 +23,7 @@ extern struct static_key rdpmc_always_available;static inline void load_mm_cr4 (struct mm_struct *mm) { - if (static_key_true(&rdpmc_always_available) || + if (static_key_false(&rdpmc_always_available) ||
In what universe is "static_key_false" a reasonable name for a function that returns true if a static key is true?
Can we rename that function? And could we maybe make static keys type safe? I.e. there would be a type that starts out true and a type that starts out false.
--Andy
Quoted 36 lines · Andy Lutomirski
Quoted 27 lines · Peter Zijlstra
Quoted 11 lines · Mikulas Patocka
Hi
I found out that the patch
a66734297f78707ce39d756b656bfae861d53f62breaks the kernel on processors without performance counters, such as AMD K6-3. Reverting the patch fixes the problem.The static key
rdpmc_always_availablesomehow gets set (I couldn't really find out what is setting it, the functionset_attr_rdpmcis not executed),cr4_set_bits(X86_CR4_PCE)is executed and that results in a crash on boot when attempting to execute init, because the proecssor doesn't support that bit in CR4.Urgh, the static key trainwreck bites again.
One is not supposed to mix
static_key_true()andSTATIC_KEY_INIT_FALSE.Does this make it go again?
--- arch/x86/include/asm/mmu_context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/include/asm/mmu_context.h +0
-0arch/x86/include/asm/mmu_context.h extern struct static_key rdpmc_always_available; · line 26
Quoted 5 lines
arch/x86/include/asm/mmu_context.h +1
-1@@ -23,7 +23,7 @@ extern struct static_key rdpmc_always_available;static inline void load_mm_cr4 (struct mm_struct *mm) { - if (static_key_true(&rdpmc_always_available) || + if (static_key_false(&rdpmc_always_available) ||In what universe is "
static_key_false" a reasonable name for a function that returns true if a static key is true?
Quoted 3 lines
Can we rename that function? And could we maybe make static keys type safe? I.e. there would be a type that starts out true and a type that starts out false.
So the 'static_key_false' is really branch is initially false. We had a naming discussion before, but if ppl think its confusing, 'static_key_init_false', or 'static_key_default_false' might be better, or other ideas.... I agree its confusing.
In terms of getting the type to match so we don't have these mismatches, I think we could introduce 'struct static_key_false' and 'struct static_key_true' with proper initializers. However, 'static_key_slow_inc()/dec()' would also have to add the true/false modifier. Or maybe we do:
struct static_key_false {
struct static_key key;
} random_key;and then the 'static_key_sloc_inc()/dec()' would just take a &random_key.key....
If we were to change this, I don't think it would be too hard to introduce the new API, convert subsystems over time and then drop the old one.
Thanks,
-Jason
Quoted 37 lines · Jason Baron
Quoted 36 lines · Andy Lutomirski
Quoted 27 lines · Peter Zijlstra
Quoted 11 lines · Mikulas Patocka
Hi
I found out that the patch
a66734297f78707ce39d756b656bfae861d53f62breaks the kernel on processors without performance counters, such as AMD K6-3. Reverting the patch fixes the problem.The static key
rdpmc_always_availablesomehow gets set (I couldn't really find out what is setting it, the functionset_attr_rdpmcis not executed),cr4_set_bits(X86_CR4_PCE)is executed and that results in a crash on boot when attempting to execute init, because the proecssor doesn't support that bit in CR4.Urgh, the static key trainwreck bites again.
One is not supposed to mix
static_key_true()andSTATIC_KEY_INIT_FALSE.Does this make it go again?
--- arch/x86/include/asm/mmu_context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/include/asm/mmu_context.h +0
-0arch/x86/include/asm/mmu_context.h extern struct static_key rdpmc_always_available; · line 26
Quoted 5 lines
arch/x86/include/asm/mmu_context.h +1
-1@@ -23,7 +23,7 @@ extern struct static_key rdpmc_always_available;static inline void load_mm_cr4 (struct mm_struct *mm) { - if (static_key_true(&rdpmc_always_available) || + if (static_key_false(&rdpmc_always_available) ||In what universe is "
static_key_false" a reasonable name for a function that returns true if a static key is true?
Quoted 8 lines
Quoted 3 lines
Can we rename that function? And could we maybe make static keys type safe? I.e. there would be a type that starts out true and a type that starts out false.
So the '
static_key_false' is really branch is initially false. We had a naming discussion before, but if ppl think its confusing, 'static_key_init_false', or 'static_key_default_false' might be better, or other ideas.... I agree its confusing.
I think the current naming is almost maximally bad. The naming would be less critical if it were typesafe, though.
Quoted 6 lines
In terms of getting the type to match so we don't have these mismatches, I think we could introduce 'struct
static_key_false' and 'structstatic_key_true' with proper initializers. However, 'static_key_slow_inc()/dec()' would also have to add the true/false modifier.
I think that would be okay.
Quoted 8 lines
Or maybe we do:
struct static_key_false { struct static_key key; } random_key;and then the 'static_key_sloc_inc()/dec()' would just take a &random_key.key....
That would be okay, too. Or it could be a macro that has the same effect.
Quoted 4 lines
If we were to change this, I don't think it would be too hard to introduce the new API, convert subsystems over time and then drop the old one.
And we might discover a bug or three while doing it :)
--Andy
Peter Zijlstra
maintainerQuoted 5 lines · Andy Lutomirski
Quoted 2 lines
Quoted 2 lines
In what universe is "
static_key_false" a reasonable name for a function that returns true if a static key is true?I think the current naming is almost maximally bad. The naming would be less critical if it were typesafe, though.
How about something like so on top? It will allow us to slowly migrate existing and new users over to the hopefully saner interface?
--- include/linux/jump_label.h | 67 +++++++++++++++++++++++++++++++++++++++++++++- kernel/sched/core.c | 18 ++++++------- 2 files changed, 74 insertions(+), 11 deletions(-)
include/linux/jump_label.h +66 -1
@@ -213,6 +213,71 @@ static inline bool static_key_enabled(struct static_key *key)return static_key_count (key) >0 ; } -#endif /* _LINUX_JUMP_LABEL_H */ +static inline void static_key_enable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (!count) +static_key_slow_inc (key); +} + +static inline void static_key_disable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (count) +static_key_slow_dec (key); +} + +/* -------------------------------------------------------------------------- */ + +/* +* likely -- default enabled, puts the branch body in-line +*/ + +struct static_likely_key { +struct static_key key; +}; + +#define STATIC_LIKELY_KEY_INIT (struct static_likely_key){ .key = STATIC_KEY_INIT_TRUE, } + +static inline bool static_likely_branch (struct static_likely_key *key) +{ +return static_key_true (&key->key); +} + +/* +* unlikely -- default disabled, puts the branch body out-of-line +*/ + +struct static_unlikely_key { +struct static_key key; +}; + +#define STATIC_UNLIKELY_KEY_INIT (struct static_unlikely_key){ .key = STATIC_KEY_INIT_FALSE, } + +static inline bool static_unlikely_branch (struct static_unlikely_key *key) +{ +return static_key_false (&key->key); +} + +/* +* Advanced usage; refcount, branch is enabled when: count != 0 +*/ + +#define static_branch_inc (_k) static_key_slow_inc(&(_k)->key) +#define static_branch_dec (_k) static_key_slow_dec(&(_k)->key) + +/* +* Normal usage; boolean enable/disable. +*/ + +#define static_branch_enable (_k) static_key_enable(&(_k)->key) +#define static_branch_disable (_k) static_key_disable(&(_k)->key)#endif /* __ASSEMBLY__ */ +#endif /* _LINUX_JUMP_LABEL_H */
kernel/sched/core.c +8 -10
@@ -164,14 +164,12 @@ struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {static void sched_feat_disable (int i) { -if (static_key_enabled (&sched_feat_keys[i])) -static_key_slow_dec (&sched_feat_keys[i]); +static_key_enable (&sched_feat_keys[i]); }static void sched_feat_enable (int i) { -if (!static_key_enabled (&sched_feat_keys[i])) -static_key_slow_inc (&sched_feat_keys[i]); +static_key_disable (&sched_feat_keys[i]); }#else static void sched_feat_disable (int i) { };
@@ -2318,17 +2316,17 @@ void wake_up_new_task(struct task_struct *p)#ifdef CONFIG_PREEMPT_NOTIFIERS -static struct static_key preempt_notifier_key =STATIC_KEY_INIT_FALSE ; +static struct static_unlikely_key preempt_notifier_key =STATIC_UNLIKELY_KEY_INIT ;void preempt_notifier_inc (void ) { -static_key_slow_inc (&preempt_notifier_key); +static_branch_inc (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_inc);void preempt_notifier_dec (void ) { -static_key_slow_dec (&preempt_notifier_key); +static_branch_dec (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_dec);
@@ -2338,7 +2336,7 @@ EXPORT_SYMBOL_GPL(preempt_notifier_dec);*/ void preempt_notifier_register (struct preempt_notifier *notifier) { -if (!static_key_false (&preempt_notifier_key)) +if (!static_unlikely_branch (&preempt_notifier_key))WARN (1 ,"registering preempt_notifier while notifiers disabled\n" );hlist_add_head (¬ifier->link, ¤t->preempt_notifiers);
@@ -2367,7 +2365,7 @@ static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) { -if (static_key_false (&preempt_notifier_key)) +if (static_unlikely_branch (&preempt_notifier_key))__fire_sched_in_preempt_notifiers (curr); }
@@ -2385,7 +2383,7 @@ static __always_inline void fire_sched_out_preempt_notifiers(struct task_struct *curr, struct task_struct *next) { - if (static_key_false(&preempt_notifier_key)) + if (static_unlikely_branch(&preempt_notifier_key)) __fire_sched_out_preempt_notifiers(curr, next); }
Quoted 31 lines · Peter Zijlstra
Quoted 4 lines · Andy Lutomirski
Quoted 2 lines
Quoted 2 lines
In what universe is "
static_key_false" a reasonable name for a function that returns true if a static key is true?I think the current naming is almost maximally bad. The naming would be less critical if it were typesafe, though.
How about something like so on top? It will allow us to slowly migrate existing and new users over to the hopefully saner interface?
--- include/linux/jump_label.h | 67 +++++++++++++++++++++++++++++++++++++++++++++- kernel/sched/core.c | 18 ++++++------- 2 files changed, 74 insertions(+), 11 deletions(-)include/linux/jump_label.h +9
-1@@ -213,6 +213,71 @@ static inline bool static_key_enabled(struct static_key *key)return static_key_count (key) >0 ; } -#endif /* _LINUX_JUMP_LABEL_H */ +static inline void static_key_enable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (!count) +static_key_slow_inc (key); +}
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 234
Quoted 10 lines
include/linux/jump_label.h +10
-0+ +static inline void static_key_disable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (count) +static_key_slow_dec (key); +}
should those be __static_key_enable()/disable() to indicate that we don't that we don't want ppl using these directly. Similarly for other 'internal' functions.
Quoted 30 lines
include/linux/jump_label.h +30
-0+ +/* -------------------------------------------------------------------------- */ + +/* +* likely -- default enabled, puts the branch body in-line +*/ + +struct static_likely_key { +struct static_key key; +}; + +#define STATIC_LIKELY_KEY_INIT (struct static_likely_key){ .key = STATIC_KEY_INIT_TRUE, } + +static inline bool static_likely_branch (struct static_likely_key *key) +{ +return static_key_true (&key->key); +} + +/* +* unlikely -- default disabled, puts the branch body out-of-line +*/ + +struct static_unlikely_key { +struct static_key key; +}; + +#define STATIC_UNLIKELY_KEY_INIT (struct static_unlikely_key){ .key = STATIC_KEY_INIT_FALSE, } + +static inline bool static_unlikely_branch (struct static_unlikely_key *key) +{
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 274
Quoted 10 lines
include/linux/jump_label.h +10
-0+return static_key_false (&key->key); +} + +/* +* Advanced usage; refcount, branch is enabled when: count != 0 +*/ + +#define static_branch_inc (_k) static_key_slow_inc(&(_k)->key) +#define static_branch_dec (_k) static_key_slow_dec(&(_k)->key) +
I think of these as operations on the 'static_key' so I still like 'static_key_inc()/dec()' (removing the 'slow' makes them different still).
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 281
Quoted 7 lines
include/linux/jump_label.h +6
-0+/* +* Normal usage; boolean enable/disable. +*/ + +#define static_branch_enable (_k) static_key_enable(&(_k)->key) +#define static_branch_disable (_k) static_key_disable(&(_k)->key)
Same here maybe: static_key_set_true()/false()?
Thanks,
-Jason
Peter Zijlstra
maintainerQuoted 9 lines · Peter Zijlstra
Quoted 5 lines · Andy Lutomirski
Quoted 2 lines
Quoted 2 lines
In what universe is "
static_key_false" a reasonable name for a function that returns true if a static key is true?I think the current naming is almost maximally bad. The naming would be less critical if it were typesafe, though.
How about something like so on top? It will allow us to slowly migrate existing and new users over to the hopefully saner interface?
Anybody? (Adding more Cc's)
Quoted 151 lines
--- include/linux/jump_label.h | 67 +++++++++++++++++++++++++++++++++++++++++++++- kernel/sched/core.c | 18 ++++++------- 2 files changed, 74 insertions(+), 11 deletions(-)include/linux/jump_label.h +66
-1@@ -213,6 +213,71 @@ static inline bool static_key_enabled(struct static_key *key)return static_key_count (key) >0 ; } -#endif /* _LINUX_JUMP_LABEL_H */ +static inline void static_key_enable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (!count) +static_key_slow_inc (key); +} + +static inline void static_key_disable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (count) +static_key_slow_dec (key); +} + +/* -------------------------------------------------------------------------- */ + +/* +* likely -- default enabled, puts the branch body in-line +*/ + +struct static_likely_key { +struct static_key key; +}; + +#define STATIC_LIKELY_KEY_INIT (struct static_likely_key){ .key = STATIC_KEY_INIT_TRUE, } + +static inline bool static_likely_branch (struct static_likely_key *key) +{ +return static_key_true (&key->key); +} + +/* +* unlikely -- default disabled, puts the branch body out-of-line +*/ + +struct static_unlikely_key { +struct static_key key; +}; + +#define STATIC_UNLIKELY_KEY_INIT (struct static_unlikely_key){ .key = STATIC_KEY_INIT_FALSE, } + +static inline bool static_unlikely_branch (struct static_unlikely_key *key) +{ +return static_key_false (&key->key); +} + +/* +* Advanced usage; refcount, branch is enabled when: count != 0 +*/ + +#define static_branch_inc (_k) static_key_slow_inc(&(_k)->key) +#define static_branch_dec (_k) static_key_slow_dec(&(_k)->key) + +/* +* Normal usage; boolean enable/disable. +*/ + +#define static_branch_enable (_k) static_key_enable(&(_k)->key) +#define static_branch_disable (_k) static_key_disable(&(_k)->key)#endif /* __ASSEMBLY__ */ +#endif /* _LINUX_JUMP_LABEL_H */ kernel/sched/core.c +8
-10@@ -164,14 +164,12 @@ struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {static void sched_feat_disable (int i) { -if (static_key_enabled (&sched_feat_keys[i])) -static_key_slow_dec (&sched_feat_keys[i]); +static_key_enable (&sched_feat_keys[i]); }static void sched_feat_enable (int i) { -if (!static_key_enabled (&sched_feat_keys[i])) -static_key_slow_inc (&sched_feat_keys[i]); +static_key_disable (&sched_feat_keys[i]); }#else static void sched_feat_disable (int i) { };@@ -2318,17 +2316,17 @@ void wake_up_new_task(struct task_struct *p)#ifdef CONFIG_PREEMPT_NOTIFIERS -static struct static_key preempt_notifier_key =STATIC_KEY_INIT_FALSE ; +static struct static_unlikely_key preempt_notifier_key =STATIC_UNLIKELY_KEY_INIT ;void preempt_notifier_inc (void ) { -static_key_slow_inc (&preempt_notifier_key); +static_branch_inc (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_inc);void preempt_notifier_dec (void ) { -static_key_slow_dec (&preempt_notifier_key); +static_branch_dec (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_dec);@@ -2338,7 +2336,7 @@ EXPORT_SYMBOL_GPL(preempt_notifier_dec);*/ void preempt_notifier_register (struct preempt_notifier *notifier) { -if (!static_key_false (&preempt_notifier_key)) +if (!static_unlikely_branch (&preempt_notifier_key))WARN (1 ,"registering preempt_notifier while notifiers disabled\n" );hlist_add_head (¬ifier->link, ¤t->preempt_notifiers);@@ -2367,7 +2365,7 @@ static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) { -if (static_key_false (&preempt_notifier_key)) +if (static_unlikely_branch (&preempt_notifier_key))__fire_sched_in_preempt_notifiers (curr); }@@ -2385,7 +2383,7 @@ static __always_inline void fire_sched_out_preempt_notifiers(struct task_struct *curr, struct task_struct *next) { - if (static_key_false(&preempt_notifier_key)) + if (static_unlikely_branch(&preempt_notifier_key)) __fire_sched_out_preempt_notifiers(curr, next); }
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Peter Zijlstra
Quoted 2 lines · Andy Lutomirski
Quoted 2 lines
Quoted 2 lines
In what universe is "
static_key_false" a reasonable name for a function that returns true if a static key is true?
It might be very well our universe, you just need access to the proper drugs to feel comfortable with it.
Quoted 18 lines
Quoted 18 lines
Quoted 2 lines
I think the current naming is almost maximally bad. The naming would be less critical if it were typesafe, though.
How about something like so on top? It will allow us to slowly migrate existing and new users over to the hopefully saner interface?
include/linux/jump_label.h +11
-1-#endif /* _LINUX_JUMP_LABEL_H */ +static inline void static_key_enable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (!count) +static_key_slow_inc (key); +} + +static inline void static_key_disable (struct static_key *key)
Quoted 8 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 234
Quoted 8 lines
include/linux/jump_label.h +8
-0+{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (count) +static_key_slow_dec (key); +}
The functions above are not part of the interface which should be used in code, right?
To avoid further confusion, can we please move all the existing mess and the new helpers into jump_label_internals.h and just put the new interfaces in jump_label.h?
Quoted 30 lines
Quoted 30 lines
include/linux/jump_label.h +30
-0+/* -------------------------------------------------------------------------- */ + +/* +* likely -- default enabled, puts the branch body in-line +*/ + +struct static_likely_key { +struct static_key key; +}; + +#define STATIC_LIKELY_KEY_INIT (struct static_likely_key){ .key = STATIC_KEY_INIT_TRUE, } + +static inline bool static_likely_branch (struct static_likely_key *key) +{ +return static_key_true (&key->key); +} + +/* +* unlikely -- default disabled, puts the branch body out-of-line +*/ + +struct static_unlikely_key { +struct static_key key; +}; + +#define STATIC_UNLIKELY_KEY_INIT (struct static_unlikely_key){ .key = STATIC_KEY_INIT_FALSE, } + +static inline bool static_unlikely_branch (struct static_unlikely_key *key) +{ +return static_key_false (&key->key);
Quoted 8 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 273
Quoted 8 lines
include/linux/jump_label.h +8
-0+} + +/* +* Advanced usage; refcount, branch is enabled when: count != 0 +*/ + +#define static_branch_inc (_k) static_key_slow_inc(&(_k)->key) +#define static_branch_dec (_k) static_key_slow_dec(&(_k)->key)
Inlines please
Quoted 6 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 280
Quoted 6 lines
include/linux/jump_label.h +6
-0+/* +* Normal usage; boolean enable/disable. +*/ + +#define static_branch_enable (_k) static_key_enable(&(_k)->key) +#define static_branch_disable (_k) static_key_disable(&(_k)->key)
Ditto
All in all much more understandable than the existing horror.
Thanks,
tglx
Peter Zijlstra
maintainerQuoted 17 lines · Thomas Gleixner
Quoted 15 lines · Peter Zijlstra
Quoted 15 lines
include/linux/jump_label.h +14
-1-#endif /* _LINUX_JUMP_LABEL_H */ +static inline void static_key_enable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (!count) +static_key_slow_inc (key); +} + +static inline void static_key_disable (struct static_key *key) +{ +int count =static_key_count (key); +
Quoted 8 lines
Quoted 5 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 234
Quoted 5 lines
include/linux/jump_label.h +5
-0+WARN_ON_ONCE (count <0 || count >1 ); + +if (count) +static_key_slow_dec (key); +}The functions above are not part of the interface which should be used in code, right?
They are in fact. They make it easier to deal with the refcount thing when all you want is boolean states.
Quoted 50 lines
Quoted 30 lines
Quoted 30 lines
include/linux/jump_label.h +30
-0+/* -------------------------------------------------------------------------- */ + +/* +* likely -- default enabled, puts the branch body in-line +*/ + +struct static_likely_key { +struct static_key key; +}; + +#define STATIC_LIKELY_KEY_INIT (struct static_likely_key){ .key = STATIC_KEY_INIT_TRUE, } + +static inline bool static_likely_branch (struct static_likely_key *key) +{ +return static_key_true (&key->key); +} + +/* +* unlikely -- default disabled, puts the branch body out-of-line +*/ + +struct static_unlikely_key { +struct static_key key; +}; + +#define STATIC_UNLIKELY_KEY_INIT (struct static_unlikely_key){ .key = STATIC_KEY_INIT_FALSE, } + +static inline bool static_unlikely_branch (struct static_unlikely_key *key) +{ +return static_key_false (&key->key);Quoted 8 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 273
Quoted 8 lines
include/linux/jump_label.h +8
-0+} + +/* +* Advanced usage; refcount, branch is enabled when: count != 0 +*/ + +#define static_branch_inc (_k) static_key_slow_inc(&(_k)->key) +#define static_branch_dec (_k) static_key_slow_dec(&(_k)->key)Inlines please
Quoted 6 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 280
Quoted 6 lines
include/linux/jump_label.h +6
-0+/* +* Normal usage; boolean enable/disable. +*/ + +#define static_branch_enable (_k) static_key_enable(&(_k)->key) +#define static_branch_disable (_k) static_key_disable(&(_k)->key)Ditto
Quoted 1 line
All in all much more understandable than the existing horror.
They cannot in fact be inlines because we play type tricks. Note how _k can be either struct static_likely_key or struct static_unlikely_key.
Quoted 80 lines · Peter Zijlstra
Quoted 17 lines · Thomas Gleixner
Quoted 15 lines · Peter Zijlstra
Quoted 15 lines
include/linux/jump_label.h +14
-1-#endif /* _LINUX_JUMP_LABEL_H */ +static inline void static_key_enable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (!count) +static_key_slow_inc (key); +} + +static inline void static_key_disable (struct static_key *key) +{ +int count =static_key_count (key); +Quoted 8 lines
Quoted 5 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 234
Quoted 5 lines
+
WARN_ON_ONCE(count < 0 || count > 1);include/linux/jump_label.h +4
-0+ +if (count) +static_key_slow_dec (key); +}The functions above are not part of the interface which should be used in code, right?
They are in fact. They make it easier to deal with the refcount thing when all you want is boolean states.
Quoted 50 lines
Quoted 30 lines
Quoted 30 lines
include/linux/jump_label.h +30
-0+/* -------------------------------------------------------------------------- */ + +/* +* likely -- default enabled, puts the branch body in-line +*/ + +struct static_likely_key { +struct static_key key; +}; + +#define STATIC_LIKELY_KEY_INIT (struct static_likely_key){ .key = STATIC_KEY_INIT_TRUE, } + +static inline bool static_likely_branch (struct static_likely_key *key) +{ +return static_key_true (&key->key); +} + +/* +* unlikely -- default disabled, puts the branch body out-of-line +*/ + +struct static_unlikely_key { +struct static_key key; +}; + +#define STATIC_UNLIKELY_KEY_INIT (struct static_unlikely_key){ .key = STATIC_KEY_INIT_FALSE, } + +static inline bool static_unlikely_branch (struct static_unlikely_key *key) +{ +return static_key_false (&key->key);Quoted 8 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 273
Quoted 8 lines
include/linux/jump_label.h +8
-0+} + +/* +* Advanced usage; refcount, branch is enabled when: count != 0 +*/ + +#define static_branch_inc (_k) static_key_slow_inc(&(_k)->key) +#define static_branch_dec (_k) static_key_slow_dec(&(_k)->key)Inlines please
Quoted 6 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 280
Quoted 6 lines
include/linux/jump_label.h +6
-0+/* +* Normal usage; boolean enable/disable. +*/ + +#define static_branch_enable (_k) static_key_enable(&(_k)->key) +#define static_branch_disable (_k) static_key_disable(&(_k)->key)Ditto
Quoted 6 lines
Quoted 1 line
All in all much more understandable than the existing horror.
They cannot in fact be inlines because we play type tricks. Note how _k can be either struct
static_likely_keyor structstatic_unlikely_key.
To clarify my (mis-)understanding:
There are two degrees of freedom in a static_key. They can start out true or false, and they can be unlikely or likely. Are those two degrees of freedom in fact tied together?
--Andy
Peter Zijlstra
maintainerQuoted 5 lines · Andy Lutomirski
To clarify my (mis-)understanding:
There are two degrees of freedom in a
static_key. They can start out true or false, and they can be unlikely or likely. Are those two degrees of freedom in fact tied together?
Yes, if you start out false, you must be unlikely. If you start out true, you must be likely.
We could maybe try and untangle that if there really is a good use case, but this is the current state.
The whole reason this happened is because 'false' is like:
... <nop> 1: ...
label:
<unlikely code>
jmp 1b
Where the code if out-of-line by default. The enable will rewrite the <nop> with a jmp label.
Of course, if you have code that is on by default, you don't want to pay that out-of-line penalty all the time. So the on by default generates:
... <nop> <likely code> label: ...
Where, if we disable, we replace the nop with jmp label.
Or rather, that all is the intent, GCC doesn't actually honour hot/cold attributes on asm labels very well last time I tried.
Quoted 11 lines · Peter Zijlstra
Quoted 5 lines · Andy Lutomirski
To clarify my (mis-)understanding:
There are two degrees of freedom in a
static_key. They can start out true or false, and they can be unlikely or likely. Are those two degrees of freedom in fact tied together?Yes, if you start out false, you must be unlikely. If you start out true, you must be likely.
We could maybe try and untangle that if there really is a good use case, but this is the current state.
We could potentially allow all combinations but it requires a more complex implementation. Perhaps, by rewriting no-ops to jmps during build-time? Steve Rostedt posted an implementation a while back to do that, but in part it wasn't merged due to its complexity and the fact that it increased the kernel text, which I don't think we ever got to the bottom of. Steve was actually trying to reduce the size of the no-ops by first letting the compiler pick the 'jmp' instruction size (short jumps of only 2-bytes on x86, instead of the hard-coded 5 bytes we currently have). However, we could use the same idea here to allow the mixed branch label and initial variable state.
That said, it seems easy enough to reverse the direction of the branch in an __init or so when we boot, if required.
Quoted 29 lines
The whole reason this happened is because 'false' is like:
... <nop> 1: ...label:
<unlikely code>
jmp 1bWhere the code if out-of-line by default. The enable will rewrite the <nop> with a jmp label.
Of course, if you have code that is on by default, you don't want to pay that out-of-line penalty all the time. So the on by default generates:
... <nop> <likely code> label: ...
Quoted 4 lines
Where, if we disable, we replace the nop with jmp label.
Or rather, that all is the intent, GCC doesn't actually honour hot/cold attributes on asm labels very well last time I tried.
I tried this too not that long ago, and didn't seem to make any difference. Ideally this could allow us to make variations where 'cold' code is moved further out-of-line.
Thanks,
-Jason
Steven Rostedt
reviewerQuoted 13 lines · On Tue, 21 Jul 2015 12:57:08 -0400 Jason Baron <[email protected]> wrote:
Quoted 11 lines · Peter Zijlstra
Quoted 5 lines · Andy Lutomirski
To clarify my (mis-)understanding:
There are two degrees of freedom in a
static_key. They can start out true or false, and they can be unlikely or likely. Are those two degrees of freedom in fact tied together?Yes, if you start out false, you must be unlikely. If you start out true, you must be likely.
We could maybe try and untangle that if there really is a good use case, but this is the current state.
Quoted 6 lines
We could potentially allow all combinations but it requires a more complex implementation. Perhaps, by rewriting no-ops to jmps during build-time? Steve Rostedt posted an implementation a while back to do that, but in part it wasn't merged due to its complexity and the fact that it increased the kernel text, which I don't think we ever got to the bottom of. Steve was actually
I think that happened because we didn't save enough with the nops to compensate for the code that it took to implement that change. Perhaps in the future that will be different as the implementation is a fixed size, while the usage of jump labels increase.
-- Steve
Quoted 9 lines
trying to reduce the size of the no-ops by first letting the compiler pick the 'jmp' instruction size (short jumps of only 2-bytes on x86, instead of the hard-coded 5 bytes we currently have). However, we could use the same idea here to allow the mixed branch label and initial variable state.
That said, it seems easy enough to reverse the direction of the branch in an
__initor so when we boot, if required.
Borislav Petkov
maintainerQuoted 16 lines · Peter Zijlstra
Yes, if you start out false, you must be unlikely. If you start out true, you must be likely.
We could maybe try and untangle that if there really is a good use case, but this is the current state.
The whole reason this happened is because 'false' is like:
... <nop> 1: ...
Quoted 7 lines
label:
<unlikely code>
jmp 1bWhere the code if out-of-line by default. The enable will rewrite the <nop> with a jmp label.
Btw, native_sched_clock() is kinda botched because of that, see below.
I'd want that RDTSC to come first with a NOP preceding it which can become a JMP in case some idiotic CPU can't do RDTSC and needs to use jiffies. Instead, we *unconditionally* jump to RDTSC which is WTF?! We can just as well do a normal unlikely() without the static_key:
.globl native_sched_clock
.type native_sched_clock, @functionnative_sched_clock:
pushq %rbp # movq %rsp, %rbp #, #APP # 21 "./arch/x86/include/asm/jump_label.h" 1 1:.byte 0x0f,0x1f,0x44,0x00,0 .pushsection __jump_table, "aw" .balign 8 .quad 1b, .L122, __use_tsc #, .popsection
# 0 "" 2
#NO_APP
movabsq $-4294667296000000, %rax #, tmp118 popq %rbp # imulq $1000000, jiffies_64(%rip), %rdx #, jiffies_64, D.28443 addq %rdx, %rax # D.28443, D.28443 ret
.L122:
#APP
# 118 "./arch/x86/include/asm/msr.h" 1
rdtsc
# 0 "" 2
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Quoted 22 lines · Borislav Petkov
Quoted 21 lines · Peter Zijlstra
Yes, if you start out false, you must be unlikely. If you start out true, you must be likely.
We could maybe try and untangle that if there really is a good use case, but this is the current state.
The whole reason this happened is because 'false' is like:
... <nop> 1: ...label:
<unlikely code>
jmp 1b
Quoted 8 lines
Quoted 2 lines
Where the code if out-of-line by default. The enable will rewrite the <nop> with a jmp label.
Btw,
native_sched_clock()is kinda botched because of that, see below.I'd want that RDTSC to come first with a NOP preceding it which can become a JMP in case some idiotic CPU can't do RDTSC and needs to use jiffies. Instead, we *unconditionally* jump to RDTSC which is WTF?! We can just as well do a normal
unlikely()without thestatic_key:
hmmm...so this is a case where need to the default the branch to the out-of-line branch at boot. That is, we can't just enable the out-of-line branch at boot time, b/c it might be too late at that point? IE native_sched_clock() gets called very early?
Thanks,
-Jason
Quoted 27 lines
.globl
native_sched_clock
.typenative_sched_clock, @functionnative_sched_clock:pushq %rbp # movq %rsp, %rbp #, #APP # 21 "./arch/x86/include/asm/jump_label.h" 1 1:.byte 0x0f,0x1f,0x44,0x00,0 .pushsection __jump_table, "aw" .balign 8 .quad 1b, .L122, __use_tsc #, .popsection# 0 "" 2
#NO_APPmovabsq $-4294667296000000, %rax #, tmp118 popq %rbp # imulq $1000000, jiffies_64(%rip), %rdx #, jiffies_64, D.28443 addq %rdx, %rax # D.28443, D.28443 ret.L122:
#APP
# 118 "./arch/x86/include/asm/msr.h" 1
rdtsc
# 0 "" 2
Quoted 34 lines · Jason Baron
Quoted 22 lines · Borislav Petkov
Quoted 21 lines · Peter Zijlstra
Yes, if you start out false, you must be unlikely. If you start out true, you must be likely.
We could maybe try and untangle that if there really is a good use case, but this is the current state.
The whole reason this happened is because 'false' is like:
... <nop> 1: ...label:
<unlikely code>
jmp 1bQuoted 8 lines
Quoted 2 lines
Where the code if out-of-line by default. The enable will rewrite the <nop> with a jmp label.
Btw,
native_sched_clock()is kinda botched because of that, see below.I'd want that RDTSC to come first with a NOP preceding it which can become a JMP in case some idiotic CPU can't do RDTSC and needs to use jiffies. Instead, we *unconditionally* jump to RDTSC which is WTF?! We can just as well do a normal
unlikely()without thestatic_key:
Quoted 5 lines
hmmm...so this is a case where need to the default the branch to the out-of-line branch at boot. That is, we can't just enable the out-of-line branch at boot time, b/c it might be too late at that point? IE
native_sched_clock()gets called very early?
Could this be done at link time, or perhaps when compressing the kernel image, instead of at boot time?
--Andy
Quoted 2 lines · Andy Lutomirski
Could this be done at link time, or perhaps when compressing the kernel image, instead of at boot time?
That's only safe to do if the kernel is built for one specific CPU - if it's a generic kernel that boots on multiple hardware designs, it will be wrong for some systems.
In other words - safe to do if you're building it for *your* hardware. Totally unsafe for a distro.
Quoted 11 lines · On Tue, Jul 21, 2015 at 12:00 PM, <[email protected]> wrote:
Quoted 2 lines · Andy Lutomirski
Could this be done at link time, or perhaps when compressing the kernel image, instead of at boot time?
That's only safe to do if the kernel is built for one specific CPU - if it's a generic kernel that boots on multiple hardware designs, it will be wrong for some systems.
In other words - safe to do if you're building it for *your* hardware. Totally unsafe for a distro.
That's not what I meant. We do something in the C code that tells the build step which way the initial state goes. At link time, we make the initial state actually work like that. Then, at run time, we can still switch it again if needed.
--Andy
Quoted 4 lines · Andy Lutomirski
That's not what I meant. We do something in the C code that tells the build step which way the initial state goes. At link time, we make the initial state actually work like that. Then, at run time, we can still switch it again if needed.
OK, that's something different than what I read the first time around.
I'm thinking that a good adjective would be "brittle", in the face of recalcitrant GCC releases. Look at the fun we've had over the years getting things that *should* be fairly intuitive to work correctly (such as "inline").
Having said that, if somebody comes up with something that actually works, I'd be OK with it...
Borislav Petkov
maintainerQuoted 4 lines · Jason Baron
hmmm...so this is a case where need to the default the branch to the out-of-line branch at boot. That is, we can't just enable the out-of-line branch at boot time, b/c it might be too late at that point? IE
native_sched_clock()gets called very early?
Well, even the layout is wrong here. The optimal thing would be to have:
NOP
rdtsc
unlikely:
/* read jiffies */
at build time. And then at boot time, patch in the JMP over the NOP on !use_tsc boxes. And RDTSC works always, no matter how early.
I'm fairly sure we can do that now with alternatives instead of jump labels.
The problem currently is that the 5-byte NOP gets patched in with a JMP so we have an unconditional forwards JMP to the RDTSC.
Now I'd put my money on most arches handling NOPs better then unconditional JMPs and this is a hot path...
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Quoted 16 lines · Borislav Petkov
Quoted 4 lines · Jason Baron
hmmm...so this is a case where need to the default the branch to the out-of-line branch at boot. That is, we can't just enable the out-of-line branch at boot time, b/c it might be too late at that point? IE
native_sched_clock()gets called very early?Well, even the layout is wrong here. The optimal thing would be to have:
NOP
rdtscunlikely:
/* read jiffies */at build time. And then at boot time, patch in the JMP over the NOP on !use_tsc boxes. And RDTSC works always, no matter how early.
Quoted 9 lines
I'm fairly sure we can do that now with alternatives instead of jump labels.
The problem currently is that the 5-byte NOP gets patched in with a JMP so we have an unconditional forwards JMP to the RDTSC.
Now I'd put my money on most arches handling NOPs better then unconditional JMPs and this is a hot path...
Ok,
So we could add all 4 possible initial states, where the branches would be:
static_likely_init_true_branch(struct static_likely_init_true_key *key) static_likely_init_false_branch(struct static_likely_init_false_key *key) static_unlikely_init_false_branch(struct static_unlikely_init_false_key *key) static_unlikely_init_true_branch(struct static_unlikely_init_true_key *key)
So, this last one means 'inline' the false branch, but start the branch as true. Which is, I think what you want here.
So this addresses the use-case of 'initial' branch direction doesn't match the the default branch bias. We could also do this with link time time patching (and potentially reduce the need for 4 different types), but the implementation below doesn't seem too bad :) Its based upon Peter's initial patch. It unfortunately does require some arch specific bits (so we probably need a 'HAVE_ARCH', wrapper until we have support.
Now, the native_sched_clock() starts as:
ffffffff8200bf10 <native_sched_clock>:
ffffffff8200bf10: 55 push %rbp ffffffff8200bf11: 48 89 e5 mov %rsp,%rbp ffffffff8200bf14: eb 30 jmp ffffffff8200bf46 <native_sched_clock+0x36> ffffffff8200bf16: 0f 31 rdtsc
And then the '0x3b' gets patch to a 2-byte no-op
Thanks,
-Jason
arch/x86/include/asm/jump_label.h +14 -0
@@ -30,6 +30,20 @@ l_yes:return true; } +static __always_inline bool arch_static_branch_jump(struct static_key *key) +{ + asm_volatile_goto("1:" + "jmp %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\" \n\t" + _ASM_ALIGN "\n\t" + _ASM_PTR "1b, %l[l_yes], %c0 \n\t" + ".popsection \n\t" + : : "i" (key) : : l_yes); +return false; +l_yes: +return true; +} +#ifdef CONFIG_X86_64 typedef u64 jump_label_t ;#else
arch/x86/kernel/jump_label.c +39 -36
108 lines not on this page · raw message
arch/x86/kernel/tsc.c +3 -3
@@ -38,7 +38,7 @@ static int __read_mostly tsc_unstable;erroneous rdtsc usage on !cpu_has_tsc processors */ static int __read_mostly tsc_disabled =-1 ; -static struct static_key __use_tsc =STATIC_KEY_INIT ; +static struct static_unlikely_init_true_key __use_tsc =STATIC_KEY_UNLIKELY_INIT_TRUE ;int tsc_clocksource_reliable;
@@ -284,7 +284,7 @@ u64 native_sched_clock(void)* very important for it to be as fast as the platform * can achieve it. ) */ -if (!static_key_false (&__use_tsc)) { +if (static_unlikely_init_true_branch (&__use_tsc)) {/* No locking but a rare wrong value is not a big deal: */ return (jiffies_64 -INITIAL_JIFFIES ) * (1000000000 /HZ ); }
@@ -1201,7 +1201,7 @@ void __init tsc_init(void)/* now allow native_sched_clock() to use rdtsc */ tsc_disabled =0 ; -static_key_slow_inc (&__use_tsc); +static_branch_dec (&__use_tsc);if (!no_sched_irq_time)enable_sched_clock_irqtime ();
include/linux/jump_label.h +115 -1
151 lines not on this page · raw message
kernel/jump_label.c +3 -3
@@ -203,7 +203,8 @@ void __init jump_label_init(void)struct static_key *iterk; iterk = (struct static_key *)(unsigned long )iter->key; -arch_jump_label_transform_static (iter,jump_label_type (iterk)); +if (jump_label_type (iterk) ==JUMP_LABEL_DISABLE ) +arch_jump_label_transform_static (iter,JUMP_LABEL_DISABLE );if (iterk == key)continue ;
@@ -318,8 +319,7 @@ static int jump_label_add_module(struct module *mod) jlm->next = key->next; key->next = jlm; -if (jump_label_type (key) ==JUMP_LABEL_ENABLE ) -__jump_label_update (key, iter, iter_stop,JUMP_LABEL_ENABLE ); +__jump_label_update (key, iter, iter_stop,jump_label_type (key)); }return 0 ;
Peter Zijlstra
maintainerQuoted 9 lines · Jason Baron
Ok,
So we could add all 4 possible initial states, where the branches would be:
static_likely_init_true_branch(struct static_likely_init_true_key *key)static_likely_init_false_branch(struct static_likely_init_false_key *key)static_unlikely_init_false_branch(struct static_unlikely_init_false_key *key)static_unlikely_init_true_branch(struct static_unlikely_init_true_key *key)
I'm sorely tempted to go quote cypress hill here...
And I realize part of the problem is that we're wanting to use jump labels before we can patch them. But surely we can do better.
extern bool ____wrong_branch_error(void);
struct static_key_true;
struct static_key_false;
#define static_branch_likely(x) \
({ \
bool branch; \
if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \
branch = !arch_static_branch(&(x)->key); \
else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \
branch = !arch_static_branch_jump(&(x)->key); \
else \
branch = ____wrong_branch_error(); \
branch; \
})#define static_branch_unlikely(x) \
({ \
bool branch; \
if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \
branch = arch_static_branch(&(x)->key); \
else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \
branch = arch_static_branch_jump(&(x)->key); \
else \
branch = ____wrong_branch_error(); \
branch; \
})Can't we make something like that work?
So the immediate problem appears to be the 4 different key inits, which don't seem very supportive of this separation:
+#define STATIC_KEY_LIKEY_INIT_TRUE ((struct static_unlikely_init_true_key) \
+ { .key.enabled = ATOMIC_INIT(1), \
+ .key.entries = (void *)JUMP_LABEL_TYPE_TRUE_BRANCH })
+#define STATIC_KEY_LIKEY_INIT_FALSE ((struct static_unlikely_init_false_key) \
+ { .key.enabled = ATOMIC_INIT(0), \
+ .key.entries = (void *)JUMP_LABEL_TYPE_TRUE_BRANCH })
+#define STATIC_KEY_UNLIKELY_INIT_TRUE ((struct static_unlikely_init_true_key) \
+ { .key.enabled = ATOMIC_INIT(1), \
+ .key.entries = (void *)JUMP_LABEL_TYPE_FALSE_BRANCH })
+#define STATIC_KEY_UNLIKELY_INIT_FALSE ((struct static_unlikely_init_false_key) \
+ { .key.enabled = ATOMIC_INIT(0), \
+ .key.entries = (void *)JUMP_LABEL_TYPE_FALSE_BRANCH })
But I think we can fix that by using a second __jump_table section, then we can augment the LABEL_TYPE_{TRUE,FALSE} thing with the section we find the jump_entry in.
Then we can do:
#define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, } #define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, }
And invert the jump_label_type if we're in the second section.
I think we'll need a second argument to the arch_static_branch*() functions to indicate which section it needs to go in.
static __always_inline bool arch_static_branch(struct static_key *key, bool inv)
{
if (!inv) {
asm_volatile_goto("1:"
".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t"
".pushsection __jump_table, \"aw\" \n\t"
_ASM_ALIGN "\n\t"
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
".popsection \n\t"
: : "i" (key) : : l_yes);
} else {
asm_volatile_goto("1:"
".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t"
".pushsection __jump_table_inv, \"aw\" \n\t"
_ASM_ALIGN "\n\t"
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
".popsection \n\t"
: : "i" (key) : : l_yes);
}
return false;
l_yes:
return true;
}static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv)
{
if (!inv) {
asm_volatile_goto("1:"
"jmp %l[l_yes]\n\t"
".pushsection __jump_table, \"aw\" \n\t"
_ASM_ALIGN "\n\t"
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
".popsection \n\t"
: : "i" (key) : : l_yes);
} else {
asm_volatile_goto("1:"
"jmp %l[l_yes]\n\t"
".pushsection __jump_table_inv, \"aw\" \n\t"
_ASM_ALIGN "\n\t"
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
".popsection \n\t"
: : "i" (key) : : l_yes);
}
return false;
l_yes:
return true;
}And change the branch macros thusly:
#define static_branch_likely(x) \
({ \
bool branch; \
if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \
branch = !arch_static_branch(&(x)->key, false); \
else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \
branch = !arch_static_branch_jump(&(x)->key, true); \
else \
branch = ____wrong_branch_error(); \
branch; \
})#define static_branch_unlikely(x) \
({ \
bool branch; \
if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \
branch = arch_static_branch(&(x)->key, true); \
else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \
branch = arch_static_branch_jump(&(x)->key, false); \
else \
branch = ____wrong_branch_error(); \
branch; \
})And I think it'll all work. Hmm?
Borislav Petkov
maintainerQuoted 6 lines · Peter Zijlstra
Quoted 4 lines
static_likely_init_true_branch(struct static_likely_init_true_key *key)static_likely_init_false_branch(struct static_likely_init_false_key *key)static_unlikely_init_false_branch(struct static_unlikely_init_false_key *key)static_unlikely_init_true_branch(struct static_unlikely_init_true_key *key)I'm sorely tempted to go quote cypress hill here...
Yah, those are at least too long and nuts.
Quoted 32 lines
And I realize part of the problem is that we're wanting to use jump labels before we can patch them. But surely we can do better.
extern bool
____wrong_branch_error(void);struct
static_key_true;
structstatic_key_false;#define
static_branch_likely(x)\({ \ bool branch; \ if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ branch = !arch_static_branch(&(x)->key); \ else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ branch = !arch_static_branch_jump(&(x)->key); \ else \ branch = ____wrong_branch_error(); \ branch; \ })#define
static_branch_unlikely(x)\({ \ bool branch; \ if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ branch = arch_static_branch(&(x)->key); \ else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ branch = arch_static_branch_jump(&(x)->key); \ else \ branch = ____wrong_branch_error(); \ branch; \ })
Quoted 6 lines
Can't we make something like that work?
So the immediate problem appears to be the 4 different key inits, which don't seem very supportive of this separation:
+#define
STATIC_KEY_LIKEY_INIT_TRUE((structstatic_unlikely_init_true_key) \
LIKELY
include/linux/jump_label.h extern void jump_label_apply_nops(struct module *mod);
Quoted 4 lines
include/linux/jump_label.h +3
-0+ { .key.enabled = ATOMIC_INIT(1), \ + .key.entries = (void *)JUMP_LABEL_TYPE_TRUE_BRANCH }) +#define STATIC_KEY_LIKEY_INIT_FALSE ((struct static_unlikely_init_false_key) \
Yuck, those struct names are still too long IMO.
Quoted 12 lines
include/linux/jump_label.h +8
-0+ { .key.enabled = ATOMIC_INIT(0), \ + .key.entries = (void *)JUMP_LABEL_TYPE_TRUE_BRANCH }) +#define STATIC_KEY_UNLIKELY_INIT_TRUE ((struct static_unlikely_init_true_key) \ + { .key.enabled = ATOMIC_INIT(1), \ + .key.entries = (void *)JUMP_LABEL_TYPE_FALSE_BRANCH }) +#define STATIC_KEY_UNLIKELY_INIT_FALSE ((struct static_unlikely_init_false_key) \ + { .key.enabled = ATOMIC_INIT(0), \ + .key.entries = (void *)JUMP_LABEL_TYPE_FALSE_BRANCH })
Quoted 8 lines
But I think we can fix that by using a second
__jump_tablesection, then we can augment the LABEL_TYPE_{TRUE,FALSE} thing with the section we find thejump_entryin.Then we can do:
#define
STATIC_KEY_TRUE_INIT(structstatic_key_true) { .key =STATIC_KEY_INIT_TRUE, } #defineSTATIC_KEY_FALSE_INIT(struct static_key_false){ .key =STATIC_KEY_INIT_FALSE, }
Let's abbreviate that "STATIC_KEY" thing too:
SK_TRUE_INITSK_FALSE_INIT
...
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Quoted 147 lines · Peter Zijlstra
Quoted 9 lines · Jason Baron
Ok,
So we could add all 4 possible initial states, where the branches would be:
static_likely_init_true_branch(struct static_likely_init_true_key *key)static_likely_init_false_branch(struct static_likely_init_false_key *key)static_unlikely_init_false_branch(struct static_unlikely_init_false_key *key)static_unlikely_init_true_branch(struct static_unlikely_init_true_key *key)I'm sorely tempted to go quote cypress hill here...
And I realize part of the problem is that we're wanting to use jump labels before we can patch them. But surely we can do better.
extern bool
____wrong_branch_error(void);struct
static_key_true;
structstatic_key_false;#define
static_branch_likely(x)\({ \ bool branch; \ if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ branch = !arch_static_branch(&(x)->key); \ else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ branch = !arch_static_branch_jump(&(x)->key); \ else \ branch = ____wrong_branch_error(); \ branch; \ })#define
static_branch_unlikely(x)\({ \ bool branch; \ if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ branch = arch_static_branch(&(x)->key); \ else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ branch = arch_static_branch_jump(&(x)->key); \ else \ branch = ____wrong_branch_error(); \ branch; \ })Can't we make something like that work?
So the immediate problem appears to be the 4 different key inits, which don't seem very supportive of this separation:
(quoted diff) +12
-0+#define STATIC_KEY_LIKEY_INIT_TRUE ((struct static_unlikely_init_true_key) \ + { .key.enabled = ATOMIC_INIT(1), \ + .key.entries = (void *)JUMP_LABEL_TYPE_TRUE_BRANCH }) +#define STATIC_KEY_LIKEY_INIT_FALSE ((struct static_unlikely_init_false_key) \ + { .key.enabled = ATOMIC_INIT(0), \ + .key.entries = (void *)JUMP_LABEL_TYPE_TRUE_BRANCH }) +#define STATIC_KEY_UNLIKELY_INIT_TRUE ((struct static_unlikely_init_true_key) \ + { .key.enabled = ATOMIC_INIT(1), \ + .key.entries = (void *)JUMP_LABEL_TYPE_FALSE_BRANCH }) +#define STATIC_KEY_UNLIKELY_INIT_FALSE ((struct static_unlikely_init_false_key) \ + { .key.enabled = ATOMIC_INIT(0), \ + .key.entries = (void *)JUMP_LABEL_TYPE_FALSE_BRANCH })But I think we can fix that by using a second
__jump_tablesection, then we can augment the LABEL_TYPE_{TRUE,FALSE} thing with the section we find thejump_entryin.Then we can do:
#define
STATIC_KEY_TRUE_INIT(structstatic_key_true) { .key =STATIC_KEY_INIT_TRUE, } #defineSTATIC_KEY_FALSE_INIT(struct static_key_false){ .key =STATIC_KEY_INIT_FALSE, }And invert the
jump_label_typeif we're in the second section.I think we'll need a second argument to the arch_static_branch*() functions to indicate which section it needs to go in.
static
__always_inlineboolarch_static_branch(struct static_key *key, bool inv){ if (!inv) { asm_volatile_goto("1:" ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t" ".pushsection __jump_table, \"aw\" \n\t" _ASM_ALIGN "\n\t" _ASM_PTR "1b, %l[l_yes], %c0 \n\t" ".popsection \n\t" : : "i" (key) : : l_yes); } else { asm_volatile_goto("1:" ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t" ".pushsection __jump_table_inv, \"aw\" \n\t" _ASM_ALIGN "\n\t" _ASM_PTR "1b, %l[l_yes], %c0 \n\t" ".popsection \n\t" : : "i" (key) : : l_yes); } return false; l_yes: return true; }static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv){ if (!inv) { asm_volatile_goto("1:" "jmp %l[l_yes]\n\t" ".pushsection __jump_table, \"aw\" \n\t" _ASM_ALIGN "\n\t" _ASM_PTR "1b, %l[l_yes], %c0 \n\t" ".popsection \n\t" : : "i" (key) : : l_yes); } else { asm_volatile_goto("1:" "jmp %l[l_yes]\n\t" ".pushsection __jump_table_inv, \"aw\" \n\t" _ASM_ALIGN "\n\t" _ASM_PTR "1b, %l[l_yes], %c0 \n\t" ".popsection \n\t" : : "i" (key) : : l_yes); } return false; l_yes: return true; }And change the branch macros thusly:
#define
static_branch_likely(x)\({ \ bool branch; \ if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ branch = !arch_static_branch(&(x)->key, false); \ else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ branch = !arch_static_branch_jump(&(x)->key, true); \ else \ branch = ____wrong_branch_error(); \ branch; \ })#define
static_branch_unlikely(x)\
Quoted 11 lines
({ \
bool branch; \
if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \
branch = arch_static_branch(&(x)->key, true); \
else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \
branch = arch_static_branch_jump(&(x)->key, false); \
else \
branch = ____wrong_branch_error(); \
branch; \
})In 'static_branch_unlikely()', I think arch_static_branch() and arch_static_branch_jump() are reversed. Another way to do the 'inv' thing is to simply have the likely() branches all be in one table and the unlikely() in the other. Then, for example for a given _inc() operation we would no-op all the likely() branches and jmp all the unlikely().
Quoted 1 line
And I think it'll all work. Hmm?
Cool. This also gives an extra degree of freedom in that it allows keys to be arbitrarily mixed with the likely/unlikely branch types. I'm not sure that's come up as a use-case, but seems like it would be good. It also implies that the LABEL_TYPE_{TRUE,FALSE}, is no longer associated with the key b/c a key could be used in both and unlikely() or likely() branch. So that would eventually go away, and the 'struct static_key()', I guess could point to its relevant entries in both tables. Although, that means an extra pointer in the 'struct static_key'. It may be simpler to simply add another field to the jump table that specifies if the branch is likely/unlikely, and then we are back to one table? IE arch_static_branch() could add that field to the jump table.
Thanks,
-Jason
Peter Zijlstra
maintainerQuoted 13 lines · Jason Baron
Quoted 1 line
And I think it'll all work. Hmm?
Cool. This also gives an extra degree of freedom in that it allows keys to be arbitrarily mixed with the likely/unlikely branch types. I'm not sure that's come up as a use-case, but seems like it would be good. It also implies that the LABEL_TYPE_{TRUE,FALSE}, is no longer associated with the key b/c a key could be used in both and
unlikely()orlikely()branch. So that would eventually go away, and the 'structstatic_key()', I guess could point to its relevant entries in both tables. Although, that means an extra pointer in the 'structstatic_key'. It may be simpler to simply add another field to the jump table that specifies if the branch is likely/unlikely, and then we are back to one table? IEarch_static_branch()could add that field to the jump table.
Way ahead of you, while implementing the dual section I ran into trouble and found that it would be far easier to indeed stick it in the jump_entry.
However, instead of growing the thing, I've used the LSB of the key field, that's a pointer so it has at least two bits free anyhow.
I've also implemented it for all archs (+- compile failures, I've not gotten that far).
Lemme finish this and I'll post it.
Peter Zijlstra
maintainerQuoted 1 line · Peter Zijlstra
Lemme finish this and I'll post it.
Compile tested on x86_64 only..
Please have a look, I think you said I got some of the logic wrong, I've not double checked that.
I'll go write comments and double check things.
--- arch/arm/include/asm/jump_label.h | 22 +++++++- arch/arm64/include/asm/jump_label.h | 22 +++++++- arch/mips/include/asm/jump_label.h | 23 +++++++- arch/powerpc/include/asm/jump_label.h | 23 +++++++- arch/s390/include/asm/jump_label.h | 23 +++++++- arch/sparc/include/asm/jump_label.h | 38 ++++++++++--- arch/x86/include/asm/jump_label.h | 24 +++++++- include/linux/jump_label.h | 101 +++++++++++++++++++++++++++++++++- kernel/jump_label.c | 89 +++++++++++++++--------------- 9 files changed, 297 insertions(+), 68 deletions(-)
arch/arm/include/asm/jump_label.h +20 -2
@@ -13,14 +13,32 @@#define JUMP_LABEL_NOP "nop"#endif -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("1:\n\t" JUMP_LABEL_NOP "\n\t" ".pushsection __jump_table, \"aw\"\n\t" ".word 1b, %l[l_yes], %c0\n\t" ".popsection\n\t" - : : "i" (key) : : l_yes); + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\n\t" + "b %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + ".word 1b, %l[l_yes], %c0\n\t" + ".popsection\n\t" + : : "i" (kval) : : l_yes);return false; l_yes:
arch/arm64/include/asm/jump_label.h +20 -2
@@ -26,14 +26,32 @@#define JUMP_LABEL_NOP_SIZE AARCH64_INSN_SIZE -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm goto("1: nop\n\t" ".pushsection __jump_table, \"aw\"\n\t" ".align 3\n\t" ".quad 1b, %l[l_yes], %c0\n\t" ".popsection\n\t" - : :"i" (key) : : l_yes); + : :"i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm goto("1: b %l[l_yes]\n\t" +".pushsection __jump_table, \"aw\"\n\t" +".align 3\n\t" +".quad 1b, %l[l_yes], %c0\n\t" +".popsection\n\t" + : :"i" (kval) : : l_yes);return false; l_yes:
arch/mips/include/asm/jump_label.h +21 -2
@@ -26,14 +26,33 @@#define NOP_INSN "nop"#endif -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("1:\t" NOP_INSN "\n\t" "nop\n\t" ".pushsection __jump_table, \"aw\"\n\t" WORD_INSN " 1b, %l[l_yes], %0\n\t" ".popsection\n\t" - : : "i" (key) : : l_yes); + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\tj %l[l_yes]\n\t" + "nop\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + WORD_INSN " 1b, %l[l_yes], %0\n\t" + ".popsection\n\t" + : : "i" (kval) : : l_yes); +return false; l_yes:return true;
arch/powerpc/include/asm/jump_label.h +21 -2
@@ -18,14 +18,33 @@#define JUMP_ENTRY_TYPE stringify_in_c(FTR_ENTRY_LONG)#define JUMP_LABEL_NOP_SIZE 4 -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("1:\n\t" "nop\n\t" ".pushsection __jump_table, \"aw\"\n\t" JUMP_ENTRY_TYPE "1b, %l[l_yes], %c0\n\t" ".popsection \n\t" - : : "i" (key) : : l_yes); + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\n\t" + "b %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + JUMP_ENTRY_TYPE "1b, %l[l_yes], %c0\n\t" + ".popsection \n\t" + : : "i" (kval) : : l_yes); +return false; l_yes:return true;
arch/s390/include/asm/jump_label.h +21 -2
@@ -12,14 +12,33 @@* We use a brcl 0,2 instruction for jump labels at compile time so it * can be easily distinguished from a hotpatch generated instruction. */ -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("0: brcl 0,"__stringify(JUMP_LABEL_NOP_OFFSET)"\n" ".pushsection __jump_table, \"aw\"\n" ".balign 8\n" ".quad 0b, %l[label], %0\n" ".popsection\n" - : : "X" (key) : : label); + : : "X" (kval) : : label); + +return false; +label: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("0: j %l[l_yes]\n" + ".pushsection __jump_table, \"aw\"\n" + ".balign 8\n" + ".quad 0b, %l[label], %0\n" + ".popsection\n" + : : "X" (kval) : : label); +return false; label:return true;
arch/sparc/include/asm/jump_label.h +29 -9
@@ -7,16 +7,36 @@#define JUMP_LABEL_NOP_SIZE 4 -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { - asm_volatile_goto("1:\n\t" - "nop\n\t" - "nop\n\t" - ".pushsection __jump_table, \"aw\"\n\t" - ".align 4\n\t" - ".word 1b, %l[l_yes], %c0\n\t" - ".popsection \n\t" - : : "i" (key) : : l_yes); +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\n\t" + "nop\n\t" + "nop\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + ".align 4\n\t" + ".word 1b, %l[l_yes], %c0\n\t" + ".popsection \n\t" + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\n\t" + "b %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + ".align 4\n\t" + ".word 1b, %l[l_yes], %c0\n\t" + ".popsection \n\t" + : : "i" (kval) : : l_yes); +return false; l_yes:return true;
arch/x86/include/asm/jump_label.h +22 -2
@@ -16,15 +16,35 @@# define STATIC_KEY_INIT_NOP GENERIC_NOP5_ATOMIC#endif -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("1:" ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t" ".pushsection __jump_table, \"aw\" \n\t" _ASM_ALIGN "\n\t" _ASM_PTR "1b, %l[l_yes], %c0 \n\t" ".popsection \n\t" - : : "i" (key) : : l_yes); + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:" + "jmp %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\" \n\t" + _ASM_ALIGN "\n\t" + _ASM_PTR "1b, %l[l_yes], %c0 \n\t" + ".popsection \n\t" + : : "i" (kval) : : l_yes); +return false; l_yes:return true;
include/linux/jump_label.h +100 -1
113 lines not on this page · raw message
kernel/jump_label.c +43 -46
188 lines not on this page · raw message
Quoted 496 lines · Peter Zijlstra
Quoted 1 line · Peter Zijlstra
Lemme finish this and I'll post it.
Compile tested on
x86_64only..Please have a look, I think you said I got some of the logic wrong, I've not double checked that.
I'll go write comments and double check things.
--- arch/arm/include/asm/jump_label.h | 22 +++++++- arch/arm64/include/asm/jump_label.h | 22 +++++++- arch/mips/include/asm/jump_label.h | 23 +++++++- arch/powerpc/include/asm/jump_label.h | 23 +++++++- arch/s390/include/asm/jump_label.h | 23 +++++++- arch/sparc/include/asm/jump_label.h | 38 ++++++++++--- arch/x86/include/asm/jump_label.h | 24 +++++++- include/linux/jump_label.h | 101 +++++++++++++++++++++++++++++++++- kernel/jump_label.c | 89 +++++++++++++++--------------- 9 files changed, 297 insertions(+), 68 deletions(-)arch/arm/include/asm/jump_label.h +20
-2@@ -13,14 +13,32 @@#define JUMP_LABEL_NOP "nop"#endif -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("1:\n\t" JUMP_LABEL_NOP "\n\t" ".pushsection __jump_table, \"aw\"\n\t" ".word 1b, %l[l_yes], %c0\n\t" ".popsection\n\t" - : : "i" (key) : : l_yes); + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\n\t" + "b %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + ".word 1b, %l[l_yes], %c0\n\t" + ".popsection\n\t" + : : "i" (kval) : : l_yes);return false; l_yes:arch/arm64/include/asm/jump_label.h +20
-2@@ -26,14 +26,32 @@#define JUMP_LABEL_NOP_SIZE AARCH64_INSN_SIZE -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm goto("1: nop\n\t" ".pushsection __jump_table, \"aw\"\n\t" ".align 3\n\t" ".quad 1b, %l[l_yes], %c0\n\t" ".popsection\n\t" - : :"i" (key) : : l_yes); + : :"i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm goto("1: b %l[l_yes]\n\t" +".pushsection __jump_table, \"aw\"\n\t" +".align 3\n\t" +".quad 1b, %l[l_yes], %c0\n\t" +".popsection\n\t" + : :"i" (kval) : : l_yes);return false; l_yes:arch/mips/include/asm/jump_label.h +21
-2@@ -26,14 +26,33 @@#define NOP_INSN "nop"#endif -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("1:\t" NOP_INSN "\n\t" "nop\n\t" ".pushsection __jump_table, \"aw\"\n\t" WORD_INSN " 1b, %l[l_yes], %0\n\t" ".popsection\n\t" - : : "i" (key) : : l_yes); + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\tj %l[l_yes]\n\t" + "nop\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + WORD_INSN " 1b, %l[l_yes], %0\n\t" + ".popsection\n\t" + : : "i" (kval) : : l_yes); +return false; l_yes:return true;arch/powerpc/include/asm/jump_label.h +21
-2@@ -18,14 +18,33 @@#define JUMP_ENTRY_TYPE stringify_in_c(FTR_ENTRY_LONG)#define JUMP_LABEL_NOP_SIZE 4 -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("1:\n\t" "nop\n\t" ".pushsection __jump_table, \"aw\"\n\t" JUMP_ENTRY_TYPE "1b, %l[l_yes], %c0\n\t" ".popsection \n\t" - : : "i" (key) : : l_yes); + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\n\t" + "b %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + JUMP_ENTRY_TYPE "1b, %l[l_yes], %c0\n\t" + ".popsection \n\t" + : : "i" (kval) : : l_yes); +return false; l_yes:return true;arch/s390/include/asm/jump_label.h +21
-2@@ -12,14 +12,33 @@* We use a brcl 0,2 instruction for jump labels at compile time so it * can be easily distinguished from a hotpatch generated instruction. */ -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("0: brcl 0,"__stringify(JUMP_LABEL_NOP_OFFSET)"\n" ".pushsection __jump_table, \"aw\"\n" ".balign 8\n" ".quad 0b, %l[label], %0\n" ".popsection\n" - : : "X" (key) : : label); + : : "X" (kval) : : label); + +return false; +label: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("0: j %l[l_yes]\n" + ".pushsection __jump_table, \"aw\"\n" + ".balign 8\n" + ".quad 0b, %l[label], %0\n" + ".popsection\n" + : : "X" (kval) : : label); +return false; label:return true;arch/sparc/include/asm/jump_label.h +29
-9@@ -7,16 +7,36 @@#define JUMP_LABEL_NOP_SIZE 4 -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { - asm_volatile_goto("1:\n\t" - "nop\n\t" - "nop\n\t" - ".pushsection __jump_table, \"aw\"\n\t" - ".align 4\n\t" - ".word 1b, %l[l_yes], %c0\n\t" - ".popsection \n\t" - : : "i" (key) : : l_yes); +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\n\t" + "nop\n\t" + "nop\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + ".align 4\n\t" + ".word 1b, %l[l_yes], %c0\n\t" + ".popsection \n\t" + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:\n\t" + "b %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\"\n\t" + ".align 4\n\t" + ".word 1b, %l[l_yes], %c0\n\t" + ".popsection \n\t" + : : "i" (kval) : : l_yes); +return false; l_yes:return true;arch/x86/include/asm/jump_label.h +22
-2@@ -16,15 +16,35 @@# define STATIC_KEY_INIT_NOP GENERIC_NOP5_ATOMIC#endif -static __always_inline bool arch_static_branch(struct static_key *key) +static __always_inline bool arch_static_branch(struct static_key *key, bool inv) { +unsigned long kval = (unsigned long )key + inv; + asm_volatile_goto("1:" ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t" ".pushsection __jump_table, \"aw\" \n\t" _ASM_ALIGN "\n\t" _ASM_PTR "1b, %l[l_yes], %c0 \n\t" ".popsection \n\t" - : : "i" (key) : : l_yes); + : : "i" (kval) : : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv) +{ +unsigned long kval = (unsigned long )key + inv; + + asm_volatile_goto("1:" + "jmp %l[l_yes]\n\t" + ".pushsection __jump_table, \"aw\" \n\t" + _ASM_ALIGN "\n\t" + _ASM_PTR "1b, %l[l_yes], %c0 \n\t" + ".popsection \n\t" + : : "i" (kval) : : l_yes); +return false; l_yes:return true;include/linux/jump_label.h +100
-1@@ -122,7 +122,7 @@ static inline bool jump_label_get_branch_default(struct static_key *key) static __always_inline bool static_key_false(struct static_key *key) { -return arch_static_branch (key); +return arch_static_branch (key, false); } static __always_inline bool static_key_true(struct static_key *key)@@ -213,6 +213,105 @@ static inline bool static_key_enabled(struct static_key *key)return static_key_count (key) >0 ; } +static inline void static_key_enable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (!count) +static_key_slow_inc (key); +} + +static inline void static_key_disable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (count) +static_key_slow_dec (key); +} + + +/* -------------------------------------------------------------------------- */ + +/* +* Two type wrappers around static_key, such that we can use compile time +* type differentiation to emit the right code. +* +* All the below code is macros in order to play type games. +*/ + +struct static_key_true { +struct static_key key; +}; + +struct static_key_false { +struct static_key key; +}; + +#define STATIC_KEY_TRUE_INIT (struct static_key_true) { .key = STATIC_KEY_INIT_TRUE, } +#define STATIC_KEY_FALSE_INIT (struct static_key_false){ .key = STATIC_KEY_INIT_FALSE, } + +#define DEFINE_STATIC_KEY_TRUE (name) \ + struct static_key_true name = STATIC_KEY_TRUE_INIT + +#define DEFINE_STATIC_KEY_FALSE (name) \ + struct static_key_false name = STATIC_KEY_FALSE_INIT + +#ifdef HAVE_JUMP_LABEL + +/* +* Combine the right initial value (type) with the right branch order and section +* to generate the desired result. +*/ + +#define static_branch_likely (x) \ +({ \ + bool branch; \ + if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ + branch = !arch_static_branch(&(x)->key, false); \ + else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ + branch = !arch_static_branch_jump(&(x)->key, true); \ + else \ + branch = ____wrong_branch_error(); \ + branch; \ +}) + +#define static_branch_unlikely (x) \ +({ \ + bool branch; \ + if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ + branch = arch_static_branch(&(x)->key, true); \ + else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ + branch = arch_static_branch_jump(&(x)->key, false); \ + else \ + branch = ____wrong_branch_error(); \ + branch; \ +}) + +#else /* !HAVE_JUMP_LABEL */ + +#define static_branch_likely (x) static_key_true(&(x)->key) +#define static_branch_unlikely (x) static_key_false(&(x)->key) + +#endif /* HAVE_JUMP_LABEL */ + +/* +* Advanced usage; refcount, branch is enabled when: count != 0 +*/ + +#define static_branch_inc (x) static_key_slow_inc(&(x)->key) +#define static_branch_dec (x) static_key_slow_dec(&(x)->key) + +/* +* Normal usage; boolean enable/disable. +*/ + +#define static_branch_enable (x) static_key_enable(&(x)->key) +#define static_branch_disable (x) static_key_disable(&(x)->key) +#endif /* _LINUX_JUMP_LABEL_H */ #endif /* __ASSEMBLY__ */ kernel/jump_label.c +16
-14@@ -54,7 +54,7 @@ jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)sort (start, size,sizeof (struct jump_entry ), jump_label_cmp,NULL ); } -static void jump_label_update (struct static_key *key,int enable); +static void jump_label_update (struct static_key *key);void static_key_slow_inc (struct static_key *key) {@@ -63,13 +63,8 @@ void static_key_slow_inc(struct static_key *key)return ;jump_label_lock (); -if (atomic_read (&key->enabled) ==0 ) { -if (!jump_label_get_branch_default (key)) -jump_label_update (key,JUMP_LABEL_ENABLE ); -else -jump_label_update (key,JUMP_LABEL_DISABLE ); - } -atomic_inc (&key->enabled); +if (atomic_inc_and_test (&key->enabled)) +jump_label_update (key);jump_label_unlock (); }EXPORT_SYMBOL_GPL (static_key_slow_inc);@@ -87,10 +82,7 @@ static void __static_key_slow_dec(struct static_key *key,atomic_inc (&key->enabled);schedule_delayed_work (work, rate_limit); }else { -if (!jump_label_get_branch_default (key)) -jump_label_update (key,JUMP_LABEL_DISABLE ); -else -jump_label_update (key,JUMP_LABEL_ENABLE ); +jump_label_update (key); }jump_label_unlock (); }@@ -149,7 +141,7 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start,return 0 ; } -/* +/* * Update code which is definitely not currently executing. * Architectures which need heavyweight synchronization to modify * running code can override this to make the non-live update case @@ -158,37 +150,44 @@ static int __jump_label_text_reserved(struct jump_entry *iter_start, void __weak __init_or_module arch_jump_label_transform_static(struct jump_entry *entry,enum jump_label_type type) { -arch_jump_label_transform (entry, type); +arch_jump_label_transform (entry, type); +} + +static struct static_key *static_key_cast (jump_label_t key_addr) +{ +return (struct static_key *)(key_addr & ~1UL ); +} + +static bool static_key_inv (jump_label_t key_addr) +{ +return key_addr &1UL ;
kernel/jump_label.c static int __jump_label_text_reserved(struct jump_entry *iter_start, · line 173
Quoted 10 lines
kernel/jump_label.c +10
-0+} + +static enum jump_label_type jump_label_type (struct jump_entry *entry) +{ +struct static_key *key =static_key_cast (iter->key); +bool true_branch =jump_label_get_branch_default (key); +bool state =static_key_enabled (key); +bool inv =static_key_inv (iter->key); + +return (true_branch ^ state) ^ inv;
iiuc...this allows both the old style keys to co-exist with the new ones. IE state wouldn't be set for the new ones. And inv shouldn't be set for the old ones. Is that correct?
Thanks,
-Jason
Peter Zijlstra
maintainerQuoted 4 lines · Jason Baron
Quoted 4 lines
kernel/jump_label.c +4
-0+static enum jump_label_type jump_label_type (struct jump_entry *entry) +{ +struct static_key *key =static_key_cast (iter->key); +bool true_branch =jump_label_get_branch_default (key);
Quoted 8 lines
kernel/jump_label.c static int __jump_label_text_reserved(struct jump_entry *iter_start, · line 173
Quoted 4 lines
kernel/jump_label.c +4
-0+bool state =static_key_enabled (key); +bool inv =static_key_inv (iter->key); + +return (true_branch ^ state) ^ inv;iiuc...this allows both the old style keys to co-exist with the new ones. IE state wouldn't be set for the new ones. And inv shouldn't be set for the old ones. Is that correct?
@state is the dynamic variable here, the other two are compile time. @true_branch denotes the default (compile time) value, and @inv denotes the (compile time) branch preference.
So @state will still be set for the new ones, but you're right in that @inv will not be set for the 'legacy' stuff.
This one little line needs a big comment.
Peter Zijlstra
maintainerQuoted 14 lines · Peter Zijlstra
Quoted 4 lines · Jason Baron
Quoted 4 lines
kernel/jump_label.c +4
-0+static enum jump_label_type jump_label_type (struct jump_entry *entry) +{ +struct static_key *key =static_key_cast (iter->key); +bool true_branch =jump_label_get_branch_default (key);Quoted 8 lines
kernel/jump_label.c static int __jump_label_text_reserved(struct jump_entry *iter_start, · line 173
Quoted 4 lines
kernel/jump_label.c +4
-0+bool state =static_key_enabled (key); +bool inv =static_key_inv (iter->key); + +return (true_branch ^ state) ^ inv;iiuc...this allows both the old style keys to co-exist with the new ones. IE state wouldn't be set for the new ones. And inv shouldn't be set for the old ones. Is that correct?
Quoted 3 lines
@state is the dynamic variable here, the other two are compile time. @true_branch denotes the default (compile time) value, and @inv denotes the (compile time) branch preference.
Ha!, so that wasn't entirely correct, it turned out @inv means arch_static_branch_jump().
That would let us remove the whole argument to the arch functions.
That said, I generated the logic table for @inv meaning the branch type and then found a logic similar to what you outlined:
/*
* Combine the right initial value (type) with the right branch order
* to generate the desired result.
*
*
* type likely (1) unlikely (0)
* -----------+-----------------------+------------------
* | | * true (1) | ... | ... * | NOP | JMP L * | <br-stmts> | 1: ... * | L: ... | * | | * | | L: <br-stmts> * | | jmp 1b * | | * -----------+-----------------------+------------------ * | | * false (0) | ... | ... * | JMP L | NOP * | <br-stmts> | 1: ... * | L: ... | * | | * | | L: <br-stmts> * | | jmp 1b * | | * -----------+-----------------------+------------------ * * The initial value is encoded in the LSB of static_key::entries, * type: 0 = false, 1 = true. * * The branch type is encoded in the LSB of jump_entry::key, * branch: 0 = unlikely, 1 = likely. * * This gives the following logic table: * * enabled type branch instuction * -----------------------------+----------- * 0 0 0 | NOP * 0 0 1 | JMP * 0 1 0 | NOP * 0 1 1 | JMP * * 1 0 0 | JMP * 1 0 1 | NOP * 1 1 0 | JMP * 1 1 1 | NOP
*
*/
This gives a map: ins = enabled ^ branch, which shows @type to be redundant.
And we can trivially switch over the old static_key_{true,false}() to emit the right branch type.
Whcih would mean we could remove the type encoding entirely, but I'll leave that be for now, maybe it'll come in handy later or whatnot.
Quoted 75 lines · Peter Zijlstra
Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Jason Baron
kernel/jump_label.c static int __jump_label_text_reserved(struct jump_entry *iter_start, · line 173
Quoted 8 lines
kernel/jump_label.c +8
-0+static enum jump_label_type jump_label_type (struct jump_entry *entry) +{ +struct static_key *key =static_key_cast (iter->key); +bool true_branch =jump_label_get_branch_default (key); +bool state =static_key_enabled (key); +bool inv =static_key_inv (iter->key); + +return (true_branch ^ state) ^ inv;iiuc...this allows both the old style keys to co-exist with the new ones. IE state wouldn't be set for the new ones. And inv shouldn't be set for the old ones. Is that correct?
Quoted 3 lines
@state is the dynamic variable here, the other two are compile time. @true_branch denotes the default (compile time) value, and @inv denotes the (compile time) branch preference.
Ha!, so that wasn't entirely correct, it turned out @inv means
arch_static_branch_jump().That would let us remove the whole argument to the arch functions.
That said, I generated the logic table for @inv meaning the branch type and then found a logic similar to what you outlined:
/*
* Combine the right initial value (type) with the right branch order
* to generate the desired result.
*
*
* type likely (1) unlikely (0)
* -----------+-----------------------+------------------* | | * true (1) | ... | ... * | NOP | JMP L * | <br-stmts> | 1: ... * | L: ... | * | | * | | L: <br-stmts> * | | jmp 1b * | | * -----------+-----------------------+------------------ * | | * false (0) | ... | ... * | JMP L | NOP * | <br-stmts> | 1: ... * | L: ... | * | | * | | L: <br-stmts> * | | jmp 1b * | | * -----------+-----------------------+------------------ * * The initial value is encoded in the LSB of static_key::entries, * type: 0 = false, 1 = true. * * The branch type is encoded in the LSB of jump_entry::key, * branch: 0 = unlikely, 1 = likely. * * This gives the following logic table: * * enabled type branch instuction * -----------------------------+----------- * 0 0 0 | NOP * 0 0 1 | JMP * 0 1 0 | NOP * 0 1 1 | JMP * * 1 0 0 | JMP * 1 0 1 | NOP * 1 1 0 | JMP * 1 1 1 | NOP*
*/
Quoted 9 lines
This gives a map: ins = enabled ^ branch, which shows @type to be redundant.
And we can trivially switch over the old static_key_{true,false}() to emit the right branch type.
Whcih would mean we could remove the type encoding entirely, but I'll leave that be for now, maybe it'll come in handy later or whatnot.
I would just remove 'type'. Essentially, before we were storing the 'branch' with the key. However, in this new model the 'branch' is really associated with the branch location, since the same key can be used now with different branches.
Thanks,
-Jason
Peter Zijlstra
maintainerQuoted 26 lines · Jason Baron
Quoted 14 lines
#define
static_branch_likely(x)\({ \ bool branch; \ if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ branch = !arch_static_branch(&(x)->key, false); \ else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ branch = !arch_static_branch_jump(&(x)->key, true); \ else \ branch = ____wrong_branch_error(); \ branch; \ })#define
static_branch_unlikely(x)\Quoted 11 lines
({ \ bool branch; \ if (__builtin_types_compatible_p(typeof(x), struct static_key_true)) \ branch = arch_static_branch(&(x)->key, true); \ else if (__builtin_types_compatible_p(typeof(x), struct static_key_false)) \ branch = arch_static_branch_jump(&(x)->key, false); \ else \ branch = ____wrong_branch_error(); \ branch; \ })
Quoted 2 lines
In '
static_branch_unlikely()', I thinkarch_static_branch()andarch_static_branch_jump()are reversed.
Yes, you're right. But I think I need a nap before touching this stuff again :-)
Steven Rostedt
reviewerQuoted 5 lines · On Thu, 23 Jul 2015 12:42:15 +0200 Peter Zijlstra <[email protected]> wrote:
static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv){ if (!inv) { asm_volatile_goto("1:" "jmp %l[l_yes]\n\t"
And what happens when this gets converted to a two byte jump?
-- Steve
Quoted 20 lines
".pushsection __jump_table, \"aw\" \n\t"
_ASM_ALIGN "\n\t"
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
".popsection \n\t"
: : "i" (key) : : l_yes);
} else {
asm_volatile_goto("1:"
"jmp %l[l_yes]\n\t"
".pushsection __jump_table_inv, \"aw\" \n\t"
_ASM_ALIGN "\n\t"
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
".popsection \n\t"
: : "i" (key) : : l_yes);
}
return false;
l_yes:
return true;
}Peter Zijlstra
maintainerQuoted 11 lines · Steven Rostedt
Quoted 5 lines · On Thu, 23 Jul 2015 12:42:15 +0200 Peter Zijlstra <[email protected]> wrote:
static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv){ if (!inv) { asm_volatile_goto("1:" "jmp %l[l_yes]\n\t"And what happens when this gets converted to a two byte jump?
That would be bad, how can we force it to emit 5 bytes?
Steven Rostedt
reviewerQuoted 10 lines · On Thu, 23 Jul 2015 19:08:11 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 9 lines · Steven Rostedt
Quoted 5 lines · On Thu, 23 Jul 2015 12:42:15 +0200 Peter Zijlstra <[email protected]> wrote:
static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv){ if (!inv) { asm_volatile_goto("1:" "jmp %l[l_yes]\n\t"
Quoted 4 lines
Quoted 2 lines
And what happens when this gets converted to a two byte jump?
That would be bad, how can we force it to emit 5 bytes?
No idea, but I could pull out that old code that converted them :-)
The complexity was in the elf parser that was run at kernel compile time. It was based on the same code that does the work with record-mcount.c to find all the mcount callers and made the sections for them. In fact, it wasn't much different, as record-mcount.c will convert the black listed sections into nops, so they do not bother calling mcount at all. But those sections were not recorded, as they were blacklisted anyway (not whitelisted really, as to be a blacklisted section, it just had to not be in the whitelisted list).
If we got the jmp conversion in, I was going to clean up the code such that both record-mcount.c and the jmp conversions used the same code where applicable.
I would probably still convert every jmp to a nop (2 or 5 byte), and then at boot up convert those back to jmps that are needed.
-- Steve
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Steven Rostedt
On Thu, 23 Jul 2015 12:42:15 +0200
Peter Zijlstra <[email protected]> wrote:
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv){ if (!inv) { asm_volatile_goto("1:" "jmp %l[l_yes]\n\t"And what happens when this gets converted to a two byte jump?
That would be bad, how can we force it to emit 5 bytes?
hmm....I don't think that's an issue, the patching code can detect if its a 2-byte jump - 0xeb, or 5-byte: 0xe9, and do the correct no-op. Same going the other way. See the code I posted a few mails back. In fact, this gets us to the smaller 2-byte no-ops in cases where we are initialized to jump.
Thanks,
-Jason
Steven Rostedt
reviewerQuoted 12 lines · On Thu, 23 Jul 2015 13:33:36 -0400 Jason Baron <[email protected]> wrote:
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Steven Rostedt
Quoted 5 lines · On Thu, 23 Jul 2015 12:42:15 +0200 Peter Zijlstra <[email protected]> wrote:
static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv){ if (!inv) { asm_volatile_goto("1:" "jmp %l[l_yes]\n\t"And what happens when this gets converted to a two byte jump?
Quoted 8 lines
Quoted 1 line
That would be bad, how can we force it to emit 5 bytes?
hmm....I don't think that's an issue, the patching code can detect if its a 2-byte jump -
0xeb, or 5-byte:0xe9, and do the correct no-op. Same going the other way. See the code I posted a few mails back. In fact, this gets us to the smaller 2-byte no-ops in cases where we are initialized to jump.
Ah right, and I already have the code that checks that (from the original plan).
-- Steve
Peter Zijlstra
maintainerQuoted 8 lines · Jason Baron
Quoted 1 line
That would be bad, how can we force it to emit 5 bytes?
hmm....I don't think that's an issue, the patching code can detect if its a 2-byte jump -
0xeb, or 5-byte:0xe9, and do the correct no-op. Same going the other way. See the code I posted a few mails back. In fact, this gets us to the smaller 2-byte no-ops in cases where we are initialized to jump.
Ah yes, I looked right over that.
I think that if we want to go do that, it should be a separate patch though.
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Steven Rostedt
Quoted 5 lines · On Thu, 23 Jul 2015 12:42:15 +0200 Peter Zijlstra <[email protected]> wrote:
static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv){ if (!inv) { asm_volatile_goto("1:" "jmp %l[l_yes]\n\t"
Quoted 4 lines
Quoted 2 lines
And what happens when this gets converted to a two byte jump?
That would be bad, how can we force it to emit 5 bytes?
jmp.d32 on newer toolchains IIRC.
--Andy
Borislav Petkov
maintainerQuoted 1 line · Peter Zijlstra
That would be bad, how can we force it to emit 5 bytes?
.byte 0xe9 like we used to do in static_cpu_has_safe().
Or you can copy the insn sizing from the alternatives macros which I added recently.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Peter Zijlstra
maintainerQuoted 4 lines · Borislav Petkov
Quoted 1 line · Peter Zijlstra
That would be bad, how can we force it to emit 5 bytes?
.byte
0xe9like we used to do instatic_cpu_has_safe().
Like so then?
static __always_inline bool arch_static_branch_jump(struct static_key *key, bool inv)
{
unsigned long kval = (unsigned long)key + inv;
asm_volatile_goto("1:"
".byte 0xe9\n\t .long %l[l_yes]\n\t"
".pushsection __jump_table, \"aw\" \n\t"
_ASM_ALIGN "\n\t"
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
".popsection \n\t"
: : "i" (kval) : : l_yes);
return false;
l_yes:
return true;
}Borislav Petkov
maintainerQuoted 9 lines · Peter Zijlstra
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Peter Zijlstra
That would be bad, how can we force it to emit 5 bytes?
.byte
0xe9like we used to do instatic_cpu_has_safe().Like so then?
static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv)
Quoted 15 lines
{
unsigned long kval = (unsigned long)key + inv;
asm_volatile_goto("1:"
".byte 0xe9\n\t .long %l[l_yes]\n\t"
".pushsection __jump_table, \"aw\" \n\t"
_ASM_ALIGN "\n\t"
_ASM_PTR "1b, %l[l_yes], %c0 \n\t"
".popsection \n\t"
: : "i" (kval) : : l_yes);
return false;
l_yes:
return true;
}Yap.
But, we can do even better and note down what kind of JMP the compiler generated and teach __jump_label_transform() to generate the right one. Maybe this struct jump_entry would get a flags member or so. This way we're optimal.
Methinks...
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Peter Zijlstra
maintainerQuoted 26 lines · Borislav Petkov
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Peter Zijlstra
That would be bad, how can we force it to emit 5 bytes?
.byte
0xe9like we used to do instatic_cpu_has_safe().Like so then?
static
__always_inlineboolarch_static_branch_jump(struct static_key *key, bool inv)Quoted 15 lines
{ unsigned long kval = (unsigned long)key + inv; asm_volatile_goto("1:" ".byte 0xe9\n\t .long %l[l_yes]\n\t" ".pushsection __jump_table, \"aw\" \n\t" _ASM_ALIGN "\n\t" _ASM_PTR "1b, %l[l_yes], %c0 \n\t" ".popsection \n\t" : : "i" (kval) : : l_yes); return false; l_yes: return true; }
Quoted 8 lines
Yap.
But, we can do even better and note down what kind of JMP the compiler generated and teach
__jump_label_transform()to generate the right one. Maybe this structjump_entrywould get a flags member or so. This way we're optimal.Methinks...
Yes, Jason and Steve already have patches to go do that, but I'd really like to keep that separate for now, this thing is big enough as is.
Quoted 8 lines · Borislav Petkov
Quoted 4 lines · Jason Baron
hmmm...so this is a case where need to the default the branch to the out-of-line branch at boot. That is, we can't just enable the out-of-line branch at boot time, b/c it might be too late at that point? IE
native_sched_clock()gets called very early?Well, even the layout is wrong here. The optimal thing would be to have:
Quoted 8 lines
NOP
rdtscunlikely:
/* read jiffies */at build time. And then at boot time, patch in the JMP over the NOP on !use_tsc boxes. And RDTSC works always, no matter how early.
RDTSC doesn't work on 486...
Mikulas
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Thomas Gleixner
Quoted 2 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 273
Quoted 2 lines
include/linux/jump_label.h +2
-0+#define static_branch_inc (_k) static_key_slow_inc(&(_k)->key) +#define static_branch_dec (_k) static_key_slow_dec(&(_k)->key)Inlines please
Quoted 6 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 280
Quoted 6 lines
include/linux/jump_label.h +6
-0+/* +* Normal usage; boolean enable/disable. +*/ + +#define static_branch_enable (_k) static_key_enable(&(_k)->key) +#define static_branch_disable (_k) static_key_disable(&(_k)->key)Ditto
Quoted 4 lines
Quoted 1 line
All in all much more understandable than the existing horror.
They cannot in fact be inlines because we play type tricks. Note how _k can be either struct
static_likely_keyor structstatic_unlikely_key.
Indeed. Care to add a comment?
Thanks,
tglx
Peter Zijlstra
maintainerQuoted 27 lines · Peter Zijlstra
Quoted 17 lines · Thomas Gleixner
Quoted 15 lines · Peter Zijlstra
Quoted 15 lines
include/linux/jump_label.h +14
-1-#endif /* _LINUX_JUMP_LABEL_H */ +static inline void static_key_enable (struct static_key *key) +{ +int count =static_key_count (key); + +WARN_ON_ONCE (count <0 || count >1 ); + +if (!count) +static_key_slow_inc (key); +} + +static inline void static_key_disable (struct static_key *key) +{ +int count =static_key_count (key); +Quoted 8 lines
Quoted 5 lines
include/linux/jump_label.h static inline bool static_key_enabled(struct static_key *key) · line 234
Quoted 5 lines
include/linux/jump_label.h +5
-0+WARN_ON_ONCE (count <0 || count >1 ); + +if (count) +static_key_slow_dec (key); +}The functions above are not part of the interface which should be used in code, right?
Quoted 2 lines
They are in fact. They make it easier to deal with the refcount thing when all you want is boolean states.
That is, things like sched_feat_keys[] which is an array of static keys, the split types doesn't work -- an array can after all have only one type.
In such cases you do have to be very careful to not wreck things.
Peter Zijlstra
maintainerQuoted 4 lines · Jason Baron
So the '
static_key_false' is really branch is initially false. We had a naming discussion before, but if ppl think its confusing, 'static_key_init_false', or 'static_key_default_false' might be better, or other ideas.... I agree its confusing.
Jason, while I have you here on the subject; mind posting that true/fase thing again? That makes more sense than the inc/dec thing for some usage sites.
Quoted 10 lines · Peter Zijlstra
Quoted 4 lines · Jason Baron
So the '
static_key_false' is really branch is initially false. We had a naming discussion before, but if ppl think its confusing, 'static_key_init_false', or 'static_key_default_false' might be better, or other ideas.... I agree its confusing.Jason, while I have you here on the subject; mind posting that true/fase thing again? That makes more sense than the inc/dec thing for some usage sites.
Ok, sounds good. I will try and get to this next week.
Thanks,
-Jason
Borislav Petkov
maintainerQuoted 4 lines · Mikulas Patocka
Hi
I found out that the patch
a66734297f78707ce39d756b656bfae861d53f62breaks the kernel on processors without performance counters, such as AMD K6-3.
Out of curiosity: are you actually using this piece of computer history or you're only booting new kernels for regressions?
:)
Thanks.
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--Quoted 6 lines · Borislav Petkov
Quoted 4 lines · Mikulas Patocka
Hi
I found out that the patch
a66734297f78707ce39d756b656bfae861d53f62breaks the kernel on processors without performance counters, such as AMD K6-3.
Quoted 6 lines
Out of curiosity: are you actually using this piece of computer history or you're only booting new kernels for regressions?
:)
Thanks.
I use it, but mostly for connecting to other machines. I wanted to test some other patch, I compiled a new kernel on K6-3 and found out this crash.
Mikulas