Skip to contentSkip to conversation

paravirt/locks: avoid modifying static key before jump_label_init()

PATCH In review Juergen Gross 3 participants 9 messages Oct 23, 2017 – Oct 27, 2017

Subsystems (5)
lore
Share & tools
lore

Juergen Gross

author maintainer
#0 lore raw

PATCH 1/1 paravirt/locks: avoid modifying static key before jump_label_init()

Don't try to set the static virt_spin_lock_key to a value before jump_label_init() has been called, as this will result in a WARN().

Solve the problem by introducing a new lock_init() hook called after jump_label_init() instead of doing the call inside of smp_prepare_boot_cpu().

---
Based on kernel/git/tip/tip.git locking/core

---
 arch/x86/include/asm/qspinlock.h | 5 +----
 arch/x86/kernel/smpboot.c        | 1 -
 include/asm-generic/qspinlock.h  | 6 ++++++
 include/linux/spinlock.h         | 4 ++++
 include/linux/spinlock_up.h      | 4 ++++
 init/main.c                      | 2 ++
 kernel/locking/spinlock.c        | 7 +++++++
 7 files changed, 24 insertions(+), 5 deletions(-)
arch/x86/include/asm/qspinlock.h +1 -4
index 308dfd0714c7..a53ef9ed0dc1 100644
@@ -49,6 +49,7 @@ static inline void queued_spin_unlock(struct qspinlock *lock)
 #ifdef CONFIG_PARAVIRT
 DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key);
 
+#define native_pv_lock_init native_pv_lock_init
 void native_pv_lock_init(void) __init;
 
 #define virt_spin_lock virt_spin_lock
@@ -70,10 +71,6 @@ static inline bool virt_spin_lock(struct qspinlock *lock)
 
 	return true;
 }
-#else
-static inline void native_pv_lock_init(void)
-{
-}
 #endif /* CONFIG_PARAVIRT */
 
 #include <asm-generic/qspinlock.h>
arch/x86/kernel/smpboot.c +0 -1
index 361f91674ce5..55a3121dd479 100644
@@ -1385,7 +1385,6 @@ void __init native_smp_prepare_boot_cpu(void)
 	/* already set me in cpu_online_mask in boot_cpu_init() */
 	cpumask_set_cpu(me, cpu_callout_mask);
 	cpu_set_state_online(me);
-	native_pv_lock_init();
 }
 
 void __init native_smp_cpus_done(unsigned int max_cpus)
include/asm-generic/qspinlock.h +6 -0
index 66260777d644..42784a353401 100644
@@ -111,6 +111,12 @@ static __always_inline bool virt_spin_lock(struct qspinlock *lock)
 }
 #endif
 
+#ifndef native_pv_lock_init
+static __always_inline void native_pv_lock_init(void)
+{
+}
+#endif
+
 /*
  * Remapping spinlock architecture specific functions to the corresponding
  * queued spinlock functions.
include/linux/spinlock.h +4 -0
index 69e079c5ff98..6654ff285e5c 100644
@@ -420,4 +420,8 @@ extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
 #define atomic_dec_and_lock(atomic, lock) \
 		__cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
 
+#ifdef CONFIG_SMP
+void lock_init(void) __init;
+#endif
+
 #endif /* __LINUX_SPINLOCK_H */
include/linux/spinlock_up.h +4 -0
index 612fb530af41..bc4787900ad7 100644
@@ -80,4 +80,8 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock)
 #define arch_read_can_lock(lock)	(((void)(lock), 1))
 #define arch_write_can_lock(lock)	(((void)(lock), 1))
 
+static inline lock_init(void)
+{
+}
+
 #endif /* __LINUX_SPINLOCK_UP_H */
init/main.c +2 -0
index 0ee9c6866ada..e5c9f9bcd311 100644
@@ -88,6 +88,7 @@
 #include <linux/io.h>
 #include <linux/cache.h>
 #include <linux/rodata_test.h>
+#include <linux/spinlock.h>
 
 #include <asm/io.h>
 #include <asm/bugs.h>
@@ -567,6 +568,7 @@ asmlinkage __visible void __init start_kernel(void)
 	sort_main_extable();
 	trap_init();
 	mm_init();
+	lock_init();
 
 	ftrace_init();
 
kernel/locking/spinlock.c +7 -0
index 4b082b5cac9e..f086e444c2ac 100644
@@ -397,3 +397,10 @@ notrace int in_lock_functions(unsigned long addr)
 	&& addr < (unsigned long)__lock_text_end;
 }
 EXPORT_SYMBOL(in_lock_functions);
+
+void __init lock_init(void)
+{
+#ifdef CONFIG_QUEUED_SPINLOCKS
+	native_pv_lock_init();
+#endif
+}
Signature · 1 line
2.12.3
#1 lore raw

Hi Juergen,

Quoted 10 lines · Juergen Gross

Don't try to set the static virt_spin_lock_key to a value before jump_label_init() has been called, as this will result in a WARN().

Solve the problem by introducing a new lock_init() hook called after jump_label_init() instead of doing the call inside of smp_prepare_boot_cpu().

---
Based on kernel/git/tip/tip.git locking/core

I also found that WARN() in tip tree.

IMO, adding a hook in start_kernel() is not elegant. It will affect other arches and increase the complexity of the system.

I like your original method.
So, I try to fix it by moving the native_pv_lock_init() from
native_smp_prepare_boot_cpu() to native_smp_prepare_cpus().

I hope it's useful to you.

Thanks,
dou.

==================<

arch/x86/kernel/smpboot.c +2 -1
index aed1460..6b1335a 100644
@@ -1323,6 +1323,8 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)
        pr_info("CPU0: ");
        print_cpu_info(&cpu_data(0));
 
+       native_pv_lock_init();
+
        uv_system_init();
 
        set_mtrr_aps_delayed_init();
@@ -1350,7 +1352,6 @@ void __init native_smp_prepare_boot_cpu(void)
        /* already set me in cpu_online_mask in boot_cpu_init() */
        cpumask_set_cpu(me, cpu_callout_mask);
        cpu_set_state_online(me);
-       native_pv_lock_init();
 }
 
 void __init native_smp_cpus_done(unsigned int max_cpus)
arch/x86/xen/smp_pv.c +2 -0
index 5147140..570b2bc 100644
@@ -236,6 +236,8 @@ static void __init xen_pv_smp_prepare_cpus(unsigned int max_cpus)
                xen_raw_printk(m);
                panic(m);
        }
+       native_pv_lock_init();
+
        xen_init_lock_cpu(0);
 
        smp_store_boot_cpu_info();
Quoted 124 lines
---
 arch/x86/include/asm/qspinlock.h | 5 +----
 arch/x86/kernel/smpboot.c        | 1 -
 include/asm-generic/qspinlock.h  | 6 ++++++
 include/linux/spinlock.h         | 4 ++++
 include/linux/spinlock_up.h      | 4 ++++
 init/main.c                      | 2 ++
 kernel/locking/spinlock.c        | 7 +++++++
 7 files changed, 24 insertions(+), 5 deletions(-)
arch/x86/include/asm/qspinlock.h +1 -4
index 308dfd0714c7..a53ef9ed0dc1 100644
@@ -49,6 +49,7 @@ static inline void queued_spin_unlock(struct qspinlock *lock)
 #ifdef CONFIG_PARAVIRT
 DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key);
 
+#define native_pv_lock_init native_pv_lock_init
 void native_pv_lock_init(void) __init;
 
 #define virt_spin_lock virt_spin_lock
@@ -70,10 +71,6 @@ static inline bool virt_spin_lock(struct qspinlock *lock)
 
 	return true;
 }
-#else
-static inline void native_pv_lock_init(void)
-{
-}
 #endif /* CONFIG_PARAVIRT */
 
 #include <asm-generic/qspinlock.h>
arch/x86/kernel/smpboot.c +0 -1
index 361f91674ce5..55a3121dd479 100644
@@ -1385,7 +1385,6 @@ void __init native_smp_prepare_boot_cpu(void)
 	/* already set me in cpu_online_mask in boot_cpu_init() */
 	cpumask_set_cpu(me, cpu_callout_mask);
 	cpu_set_state_online(me);
-	native_pv_lock_init();
 }
 
 void __init native_smp_cpus_done(unsigned int max_cpus)
include/asm-generic/qspinlock.h +6 -0
index 66260777d644..42784a353401 100644
@@ -111,6 +111,12 @@ static __always_inline bool virt_spin_lock(struct qspinlock *lock)
 }
 #endif
 
+#ifndef native_pv_lock_init
+static __always_inline void native_pv_lock_init(void)
+{
+}
+#endif
+
 /*
  * Remapping spinlock architecture specific functions to the corresponding
  * queued spinlock functions.
include/linux/spinlock.h +4 -0
index 69e079c5ff98..6654ff285e5c 100644
@@ -420,4 +420,8 @@ extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
 #define atomic_dec_and_lock(atomic, lock) \
 		__cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
 
+#ifdef CONFIG_SMP
+void lock_init(void) __init;
+#endif
+
 #endif /* __LINUX_SPINLOCK_H */
include/linux/spinlock_up.h +4 -0
index 612fb530af41..bc4787900ad7 100644
@@ -80,4 +80,8 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock)
 #define arch_read_can_lock(lock)	(((void)(lock), 1))
 #define arch_write_can_lock(lock)	(((void)(lock), 1))
 
+static inline lock_init(void)
+{
+}
+
 #endif /* __LINUX_SPINLOCK_UP_H */
init/main.c +2 -0
index 0ee9c6866ada..e5c9f9bcd311 100644
@@ -88,6 +88,7 @@
 #include <linux/io.h>
 #include <linux/cache.h>
 #include <linux/rodata_test.h>
+#include <linux/spinlock.h>
 
 #include <asm/io.h>
 #include <asm/bugs.h>
@@ -567,6 +568,7 @@ asmlinkage __visible void __init start_kernel(void)
 	sort_main_extable();
 	trap_init();
 	mm_init();
+	lock_init();
 
 	ftrace_init();
 
kernel/locking/spinlock.c +7 -0
index 4b082b5cac9e..f086e444c2ac 100644
@@ -397,3 +397,10 @@ notrace int in_lock_functions(unsigned long addr)
 	&& addr < (unsigned long)__lock_text_end;
 }
 EXPORT_SYMBOL(in_lock_functions);
+
+void __init lock_init(void)
+{
+#ifdef CONFIG_QUEUED_SPINLOCKS
+	native_pv_lock_init();
+#endif
+}
author maintainer
#2 lore raw
Quoted 14 lines · Dou Liyang

Hi Juergen,

Quoted 10 lines · Juergen Gross

Don't try to set the static virt_spin_lock_key to a value before jump_label_init() has been called, as this will result in a WARN().

Solve the problem by introducing a new lock_init() hook called after jump_label_init() instead of doing the call inside of smp_prepare_boot_cpu().

---
Based on kernel/git/tip/tip.git locking/core

Quoted 8 lines

I also found that WARN() in tip tree.

IMO, adding a hook in start_kernel() is not elegant. It will affect other arches and increase the complexity of the system.

I like your original method.
So, I try to fix it by moving the native_pv_lock_init() from native_smp_prepare_boot_cpu() to native_smp_prepare_cpus().

Hmm, this might work, but the Xen case has to be modified (same for my more general solution), as xen_init_spinlocks() is still modifying the static key too early. And we can't move xen_init_spinlocks() to smp_prepare_cpus() as this would be too late for the alternatives patching.

So let me extend your patch a little bit to cover Xen, too.

Quoted 1 line

I hope it's useful to you.

It really is, thanks.

Juergen

#3 lore raw

Hi Juergen,

[...]

Quoted 10 lines
Quoted 3 lines

I like your original method.
So, I try to fix it by moving the native_pv_lock_init() from
native_smp_prepare_boot_cpu() to native_smp_prepare_cpus().

Hmm, this might work, but the Xen case has to be modified (same for my more general solution), as xen_init_spinlocks() is still modifying the static key too early. And we can't move xen_init_spinlocks() to smp_prepare_cpus() as this would be too late for the alternatives patching.

Yes, Right.

Quoted 2 lines

So let me extend your patch a little bit to cover Xen, too.

Yes!

How about moving the check of xen_pvspin into native_pv_lock_init() like below?

Thanks,
dou.

------------------------->8

arch/x86/kernel/paravirt.c +1 -1
index 041096b..b5f3ecb 100644
@@ -119,7 +119,7 @@ DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
 
 void __init native_pv_lock_init(void)
 {
-       if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
+       if (!static_cpu_has(X86_FEATURE_HYPERVISOR) || !xen_pvspin)
                static_branch_disable(&virt_spin_lock_key);
 }
 
arch/x86/kernel/smpboot.c +2 -1
index aed1460..6b1335a 100644
@@ -1323,6 +1323,8 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)
        pr_info("CPU0: ");
        print_cpu_info(&cpu_data(0));
 
+       native_pv_lock_init();
+
        uv_system_init();
 
        set_mtrr_aps_delayed_init();
@@ -1350,7 +1352,6 @@ void __init native_smp_prepare_boot_cpu(void)
        /* already set me in cpu_online_mask in boot_cpu_init() */
        cpumask_set_cpu(me, cpu_callout_mask);
        cpu_set_state_online(me);
-       native_pv_lock_init();
 }
 
 void __init native_smp_cpus_done(unsigned int max_cpus)
arch/x86/xen/smp_pv.c +2 -0
index 5147140..570b2bc 100644
@@ -236,6 +236,8 @@ static void __init xen_pv_smp_prepare_cpus(unsigned int max_cpus)
                xen_raw_printk(m);
                panic(m);
        }
+       native_pv_lock_init();
+
        xen_init_lock_cpu(0);
 
        smp_store_boot_cpu_info();
arch/x86/xen/spinlock.c +0 -1
index e8ab80a..8e0ec79 100644
@@ -130,7 +130,6 @@ void __init xen_init_spinlocks(void)
 
        if (!xen_pvspin) {
                printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
-               static_branch_disable(&virt_spin_lock_key);
                return;
        }
        printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
Quoted 9 lines
Quoted 1 line

I hope it's useful to you.

It really is, thanks.

Juergen

author maintainer
#4 lore raw
Quoted 16 lines · Dou Liyang

Hi Juergen,

[...]

Quoted 10 lines
Quoted 3 lines

I like your original method.
So, I try to fix it by moving the native_pv_lock_init() from native_smp_prepare_boot_cpu() to native_smp_prepare_cpus().

Hmm, this might work, but the Xen case has to be modified (same for my more general solution), as xen_init_spinlocks() is still modifying the static key too early. And we can't move xen_init_spinlocks() to smp_prepare_cpus() as this would be too late for the alternatives patching.

Yes, Right.

Quoted 7 lines
Quoted 2 lines

So let me extend your patch a little bit to cover Xen, too.

Yes!

How about moving the check of xen_pvspin into native_pv_lock_init() like below?

This would leak xen_pvspin to non-Xen code. I'd rather do the static_branch_disable() in xen_init_lock_cpu() if cpu == 0.

Juergen

Quoted 80 lines

Thanks,
    dou.

------------------------->8

arch/x86/kernel/paravirt.c +1 -1
index 041096b..b5f3ecb 100644
@@ -119,7 +119,7 @@ DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
 
  void __init native_pv_lock_init(void)
  {
-       if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
+       if (!static_cpu_has(X86_FEATURE_HYPERVISOR) || !xen_pvspin)
                 static_branch_disable(&virt_spin_lock_key);
  }
 
arch/x86/kernel/smpboot.c +2 -0
index aed1460..6b1335a 100644
@@ -1323,6 +1323,8 @@ void __init native_smp_prepare_cpus(unsigned int
 max_cpus)
         pr_info("CPU0: ");
         print_cpu_info(&cpu_data(0));
 
+       native_pv_lock_init();
+
         uv_system_init();
 

        set_mtrr_aps_delayed_init();

arch/x86/kernel/smpboot.c +0 -1
@@ -1350,7 +1352,6 @@ void __init native_smp_prepare_boot_cpu(void)
         /* already set me in cpu_online_mask in boot_cpu_init() */
         cpumask_set_cpu(me, cpu_callout_mask);
         cpu_set_state_online(me);
-       native_pv_lock_init();
  }
 
  void __init native_smp_cpus_done(unsigned int max_cpus)
arch/x86/xen/smp_pv.c +2 -0
index 5147140..570b2bc 100644
@@ -236,6 +236,8 @@ static void __init xen_pv_smp_prepare_cpus(unsigned
 int max_cpus)
                 xen_raw_printk(m);
                 panic(m);
         }
+       native_pv_lock_init();
+
         xen_init_lock_cpu(0);
 

        smp_store_boot_cpu_info();

arch/x86/xen/spinlock.c +0 -1
index e8ab80a..8e0ec79 100644
@@ -130,7 +130,6 @@ void __init xen_init_spinlocks(void)
 
         if (!xen_pvspin) {
                 printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
-               static_branch_disable(&virt_spin_lock_key);
                 return;
         }
         printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
Quoted 11 lines
Quoted 3 lines

I hope it's useful to you.

It really is, thanks.

Juergen

#5 lore raw

Hi Juergen,

Quoted 5 lines
Quoted 3 lines

How about moving the check of xen_pvspin into native_pv_lock_init() like below?

This would leak xen_pvspin to non-Xen code. I'd rather do the

Oops, yes, I made the mistake. Please ignore it.

Quoted 2 lines

static_branch_disable() in xen_init_lock_cpu() if cpu == 0.

Yes. I will test it. :-)

Thanks,
dou.

#6 lore raw
In reply to Juergen Gross
Quoted 10 lines · Juergen Gross

Don't try to set the static virt_spin_lock_key to a value before jump_label_init() has been called, as this will result in a WARN().

Solve the problem by introducing a new lock_init() hook called after jump_label_init() instead of doing the call inside of smp_prepare_boot_cpu().

---
Based on kernel/git/tip/tip.git locking/core

Just a quick ping: what's the conclusion of the discussion, do we want this patch as-is?

Also, it's unclear from the changelog under what circumstances this bug was observed and what symptoms there are and how severe the bug - can the warning trigger on vanilla v4.14?

Thanks,

Ingo

author maintainer
#7 lore raw
Quoted 10 lines · Ingo Molnar
Quoted 7 lines · Juergen Gross

Don't try to set the static virt_spin_lock_key to a value before jump_label_init() has been called, as this will result in a WARN().

Solve the problem by introducing a new lock_init() hook called after jump_label_init() instead of doing the call inside of smp_prepare_boot_cpu().

Quoted 6 lines
Quoted 3 lines

---
Based on kernel/git/tip/tip.git locking/core

Just a quick ping: what's the conclusion of the discussion, do we want this patch as-is?

Dou Liyang (CC-ed) suggested another alternative he wanted to test. This would be much less intrusive. I can send a patch based on his idea in case he doesn't react in time: he basically suggested moving the call of native_pv_lock_init() to native_smp_prepare_cpus() - this will need another small adaption in Xen, but this is really simple.

Quoted 3 lines

Also, it's unclear from the changelog under what circumstances this bug was observed and what symptoms there are and how severe the bug - can the warning trigger on vanilla v4.14?

No, the patch introducing the bug is in the tip tree only.

Juergen

#8 lore raw

Hi Juergen

Quoted 15 lines · Juergen Gross
Quoted 14 lines · Ingo Molnar
Quoted 10 lines · Juergen Gross

Don't try to set the static virt_spin_lock_key to a value before jump_label_init() has been called, as this will result in a WARN().

Solve the problem by introducing a new lock_init() hook called after jump_label_init() instead of doing the call inside of smp_prepare_boot_cpu().

---
Based on kernel/git/tip/tip.git locking/core

Quoted 6 lines
Quoted 2 lines

Just a quick ping: what's the conclusion of the discussion, do we want this patch as-is?

Dou Liyang (CC-ed) suggested another alternative he wanted to test. This would be much less intrusive. I can send a patch based on his idea in case he doesn't react in time: he basically suggested moving the call of

I am sorry I misunderstood your comments, I thought you were going to send a new version, So I said i will test it. ;-)

Now, I understood clearly. I will do it and send a patch for this bug.

Thanks,
dou