Skip to contentSkip to conversation

core kernel fixes

GIT PULL Ingo Molnar 3 participants 5 messages Nov 5, 2017

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

Linus,

Please pull the latest core-urgent-for-linus git tree from:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git core-urgent-for-linus

# HEAD: ec1e1b6109171d1890a437481c35b2b56d2327b8 objtool: Prevent GCC from merging annotate_unreachable(), take 2

Please note that this pull request is RFC due to the top commit:

ec1e1b610917: objtool: Prevent GCC from merging annotate_unreachable(), take 2

... which is admittedly somewhat of an ad-hoc workaround for something the compiler should have done - if there's another solution we can try that.

The other changes:

- futex race fixes

- objtool build warning fix

- two watchdog fixes: a crash fix (revert) and a /proc/sys/kernel/watchdog_thresh handling bug fix.

Thanks,

Ingo

------------------>
Don Zickus (1):
watchdog/hardlockup/perf: Use atomics to track in-use cpu counter

Josh Poimboeuf (2):
objtool: Resync objtool's instruction decoder source code copy with the kernel's latest version
objtool: Prevent GCC from merging annotate_unreachable(), take 2

Peter Zijlstra (1):
futex: Fix more put_pi_state() vs. exit_pi_state_list() races

Thomas Gleixner (1):
watchdog/harclockup/perf: Revert a33d44843d45 ("watchdog/hardlockup/perf: Simplify deferred event destroy")

 include/linux/compiler.h                          |  4 ++--
 kernel/futex.c                                    | 23 ++++++++++++++++++++---
 kernel/watchdog_hld.c                             | 15 ++++++++++-----
 tools/objtool/arch/x86/insn/gen-insn-attr-x86.awk |  1 +
 4 files changed, 33 insertions(+), 10 deletions(-)
include/linux/compiler.h +2 -2
index fd8697aa4f73..202710420d6d 100644
@@ -191,13 +191,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	asm("%c0:\n\t"							\
 	    ".pushsection .discard.reachable\n\t"			\
 	    ".long %c0b - .\n\t"					\
-	    ".popsection\n\t" : : "i" (__LINE__));			\
+	    ".popsection\n\t" : : "i" (__COUNTER__));			\
 })
 #define annotate_unreachable() ({					\
 	asm("%c0:\n\t"							\
 	    ".pushsection .discard.unreachable\n\t"			\
 	    ".long %c0b - .\n\t"					\
-	    ".popsection\n\t" : : "i" (__LINE__));			\
+	    ".popsection\n\t" : : "i" (__COUNTER__));			\
 })
 #define ASM_UNREACHABLE							\
 	"999:\n\t"							\
kernel/futex.c +20 -3
index 0d638f008bb1..76ed5921117a 100644
@@ -903,11 +903,27 @@ void exit_pi_state_list(struct task_struct *curr)
 	 */
 	raw_spin_lock_irq(&curr->pi_lock);
 	while (!list_empty(head)) {
-
 		next = head->next;
 		pi_state = list_entry(next, struct futex_pi_state, list);
 		key = pi_state->key;
 		hb = hash_futex(&key);
+
+		/*
+		 * We can race against put_pi_state() removing itself from the
+		 * list (a waiter going away). put_pi_state() will first
+		 * decrement the reference count and then modify the list, so
+		 * its possible to see the list entry but fail this reference
+		 * acquire.
+		 *
+		 * In that case; drop the locks to let put_pi_state() make
+		 * progress and retry the loop.
+		 */
+		if (!atomic_inc_not_zero(&pi_state->refcount)) {
+			raw_spin_unlock_irq(&curr->pi_lock);
+			cpu_relax();
+			raw_spin_lock_irq(&curr->pi_lock);
+			continue;
+		}
 		raw_spin_unlock_irq(&curr->pi_lock);
 
 		spin_lock(&hb->lock);
@@ -918,8 +934,10 @@ void exit_pi_state_list(struct task_struct *curr)
 		 * task still owns the PI-state:
 		 */
 		if (head->next != next) {
+			/* retain curr->pi_lock for the loop invariant */
 			raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
 			spin_unlock(&hb->lock);
+			put_pi_state(pi_state);
 			continue;
 		}
 
@@ -927,9 +945,8 @@ void exit_pi_state_list(struct task_struct *curr)
 		WARN_ON(list_empty(&pi_state->list));
 		list_del_init(&pi_state->list);
 		pi_state->owner = NULL;
-		raw_spin_unlock(&curr->pi_lock);
 
-		get_pi_state(pi_state);
+		raw_spin_unlock(&curr->pi_lock);
 		raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
 		spin_unlock(&hb->lock);
 
kernel/watchdog_hld.c +10 -5
index 4583feb66393..e449a23e9d59 100644
@@ -13,6 +13,7 @@
 #define pr_fmt(fmt) "NMI watchdog: " fmt
 
 #include <linux/nmi.h>
+#include <linux/atomic.h>
 #include <linux/module.h>
 #include <linux/sched/debug.h>
 
@@ -22,10 +23,11 @@
 static DEFINE_PER_CPU(bool, hard_watchdog_warn);
 static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
+static DEFINE_PER_CPU(struct perf_event *, dead_event);
 static struct cpumask dead_events_mask;
 
 static unsigned long hardlockup_allcpu_dumped;
-static unsigned int watchdog_cpus;
+static atomic_t watchdog_cpus = ATOMIC_INIT(0);
 
 void arch_touch_nmi_watchdog(void)
 {
@@ -189,7 +191,8 @@ void hardlockup_detector_perf_enable(void)
 	if (hardlockup_detector_event_create())
 		return;
 
-	if (!watchdog_cpus++)
+	/* use original value for check */
+	if (!atomic_fetch_inc(&watchdog_cpus))
 		pr_info("Enabled. Permanently consumes one hw-PMU counter.\n");
 
 	perf_event_enable(this_cpu_read(watchdog_ev));
@@ -204,8 +207,10 @@ void hardlockup_detector_perf_disable(void)
 
 	if (event) {
 		perf_event_disable(event);
+		this_cpu_write(watchdog_ev, NULL);
+		this_cpu_write(dead_event, event);
 		cpumask_set_cpu(smp_processor_id(), &dead_events_mask);
-		watchdog_cpus--;
+		atomic_dec(&watchdog_cpus);
 	}
 }
 
@@ -219,7 +224,7 @@ void hardlockup_detector_perf_cleanup(void)
 	int cpu;
 
 	for_each_cpu(cpu, &dead_events_mask) {
-		struct perf_event *event = per_cpu(watchdog_ev, cpu);
+		struct perf_event *event = per_cpu(dead_event, cpu);
 
 		/*
 		 * Required because for_each_cpu() reports  unconditionally
@@ -227,7 +232,7 @@ void hardlockup_detector_perf_cleanup(void)
 		 */
 		if (event)
 			perf_event_release_kernel(event);
-		per_cpu(watchdog_ev, cpu) = NULL;
+		per_cpu(dead_event, cpu) = NULL;
 	}
 	cpumask_clear(&dead_events_mask);
 }
tools/objtool/arch/x86/insn/gen-insn-attr-x86.awk +1 -0
index a3d2c62fd805..b02a36b2c14f 100644
@@ -1,4 +1,5 @@
 #!/bin/awk -f
+# SPDX-License-Identifier: GPL-2.0
 # gen-insn-attr-x86.awk: Instruction attribute table generator
 # Written by Masami Hiramatsu <[email protected]>
 #
#1 lore raw
Quoted 7 lines · Ingo Molnar

Please note that this pull request is RFC due to the top commit:

ec1e1b610917: objtool: Prevent GCC from merging annotate_unreachable(), take 2

... which is admittedly somewhat of an ad-hoc workaround for something the compiler should have done - if there's another solution we can try that.

So I'm certainly ok with that workaround since apparently "asm volatile" doesn't do it.

That said, I think that if that asm needs to not be merged, it should _also_ be marked as "volatile" - since that's the documented bit for "not moved significantly". Of course, then because apparently that isn't enough, the __COUNTER__ games are ok, but might really mention an explicit comment in the code as to why they exist. Because right now they look just odd and nonsensical.

Linus

#2 lore raw
Quoted 12 lines · Linus Torvalds
Quoted 7 lines · Ingo Molnar

Please note that this pull request is RFC due to the top commit:

ec1e1b610917: objtool: Prevent GCC from merging annotate_unreachable(), take 2

... which is admittedly somewhat of an ad-hoc workaround for something the compiler should have done - if there's another solution we can try that.

So I'm certainly ok with that workaround since apparently "asm volatile" doesn't do it.

Quoted 6 lines

That said, I think that if that asm needs to not be merged, it should _also_ be marked as "volatile" - since that's the documented bit for "not moved significantly". Of course, then because apparently that isn't enough, the __COUNTER__ games are ok, but might really mention an explicit comment in the code as to why they exist. Because right now they look just odd and nonsensical.

The GCC manual says:

"asm statements that have no output operands, including asm goto
statements, are implicitly volatile."

Since these macros have input operands, but no output operands, I assume they're already implicitly volatile. But we can certainly make it explicit. And yes, a comment would be good.

Something like so?

include/linux/compiler.h +13 -8
index 3672353a0acd..188ed9f65517 100644
@@ -88,17 +88,22 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 
 /* Unreachable code */
 #ifdef CONFIG_STACK_VALIDATION
+/*
+ * These macros help objtool understand GCC code flow for unreachable code.
+ * The __COUNTER__ based labels are a hack to make each instance of the macros
+ * unique, to convince GCC not to merge duplicate inline asm statements.
+ */
 #define annotate_reachable() ({						\
-	asm("%c0:\n\t"							\
-	    ".pushsection .discard.reachable\n\t"			\
-	    ".long %c0b - .\n\t"					\
-	    ".popsection\n\t" : : "i" (__COUNTER__));			\
+	asm volatile("%c0:\n\t"						\
+		     ".pushsection .discard.reachable\n\t"		\
+		     ".long %c0b - .\n\t"				\
+		     ".popsection\n\t" : : "i" (__COUNTER__));		\
 })
 #define annotate_unreachable() ({					\
-	asm("%c0:\n\t"							\
-	    ".pushsection .discard.unreachable\n\t"			\
-	    ".long %c0b - .\n\t"					\
-	    ".popsection\n\t" : : "i" (__COUNTER__));			\
+	asm volatile("%c0:\n\t"						\
+		     ".pushsection .discard.unreachable\n\t"		\
+		     ".long %c0b - .\n\t"				\
+		     ".popsection\n\t" : : "i" (__COUNTER__));		\
 })
 #define ASM_UNREACHABLE							\
 	"999:\n\t"							\
#3 lore raw
Quoted 5 lines · Josh Poimboeuf

The GCC manual says:

"asm statements that have no output operands, including asm goto
statements, are implicitly volatile."

Hmm. Fair enough.

And the manual does say that it can merge and duplicate those asms (and suggests using "%=" to generate a unique number, but I guess "%c0" with __COUNTER__ is equivalent).

I think the gcc manual has changed. I'm pretty certain it used to say that "volatile" asms would not be "moved significantly". They've silently changed semantics before too, oh well.

Linus

#4 lore raw
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Josh Poimboeuf

The GCC manual says:

"asm statements that have no output operands, including asm goto
statements, are implicitly volatile."

Hmm. Fair enough.

Quoted 7 lines

And the manual does say that it can merge and duplicate those asms (and suggests using "%=" to generate a unique number, but I guess "%c0" with __COUNTER__ is equivalent).

I think the gcc manual has changed. I'm pretty certain it used to say that "volatile" asms would not be "moved significantly". They've silently changed semantics before too, oh well.

I had tried the '%=' thing before, because that was exactly what I needed. But alas, it's not supported by the older GCCs.

Signature · 1 line
Josh