locking/rtmutex: Prevent dequeue vs. unlock race
Subsystems (2)
PATCH 1/1 hotplug: Make register and unregister notifier API symmetric
In reply to Jiri SlabyFrom: Michal Hocko <[email protected]>
This patch has been added to the 3.12 stable tree. If you have any objections, please let us know.
===============
commit 777c6e0daebb3fcefbbd6f620410a946b07ef6d0 upstream.
Yu Zhao has noticed that __unregister_cpu_notifier only unregisters its notifiers when HOTPLUG_CPU=y while the registration might succeed even when HOTPLUG_CPU=n if MODULE is enabled. This means that e.g. zswap might keep a stale notifier on the list on the manual clean up during the pool tear down and thus corrupt the list. Resulting in the following
[ 144.964346] BUG: unable to handle kernel paging request at ffff880658a2be78 [ 144.971337] IP: [<ffffffffa290b00b>] raw_notifier_chain_register+0x1b/0x40 <snipped> [ 145.122628] Call Trace: [ 145.125086] [<ffffffffa28e5cf8>] __register_cpu_notifier+0x18/0x20 [ 145.131350] [<ffffffffa2a5dd73>] zswap_pool_create+0x273/0x400 [ 145.137268] [<ffffffffa2a5e0fc>] __zswap_param_set+0x1fc/0x300 [ 145.143188] [<ffffffffa2944c1d>] ? trace_hardirqs_on+0xd/0x10 [ 145.149018] [<ffffffffa2908798>] ? kernel_param_lock+0x28/0x30 [ 145.154940] [<ffffffffa2a3e8cf>] ? __might_fault+0x4f/0xa0 [ 145.160511] [<ffffffffa2a5e237>] zswap_compressor_param_set+0x17/0x20 [ 145.167035] [<ffffffffa2908d3c>] param_attr_store+0x5c/0xb0 [ 145.172694] [<ffffffffa290848d>] module_attr_store+0x1d/0x30 [ 145.178443] [<ffffffffa2b2b41f>] sysfs_kf_write+0x4f/0x70 [ 145.183925] [<ffffffffa2b2a5b9>] kernfs_fop_write+0x149/0x180 [ 145.189761] [<ffffffffa2a99248>] __vfs_write+0x18/0x40 [ 145.194982] [<ffffffffa2a9a412>] vfs_write+0xb2/0x1a0 [ 145.200122] [<ffffffffa2a9a732>] SyS_write+0x52/0xa0 [ 145.205177] [<ffffffffa2ff4d97>] entry_SYSCALL_64_fastpath+0x12/0x17
This can be even triggered manually by changing /sys/module/zswap/parameters/compressor multiple times.
Fix this issue by making unregister APIs symmetric to the register so there are no surprises.
[js] backport to 3.12
--- include/linux/cpu.h | 12 +++--------- kernel/cpu.c | 3 +-- 2 files changed, 4 insertions(+), 11 deletions(-)
include/linux/cpu.h +3 -9
@@ -119,22 +119,16 @@ enum { { .notifier_call = fn, .priority = pri }; \ register_cpu_notifier(&fn##_nb); \ } -#else /* #if defined(CONFIG_HOTPLUG_CPU) || !defined(MODULE) */ -#define cpu_notifier (fn, pri) do { (void)(fn); } while (0) -#endif /* #else #if defined(CONFIG_HOTPLUG_CPU) || !defined(MODULE) */ -#ifdef CONFIG_HOTPLUG_CPU extern int register_cpu_notifier (struct notifier_block *nb);extern void unregister_cpu_notifier (struct notifier_block *nb); -#else -#ifndef MODULE -extern int register_cpu_notifier (struct notifier_block *nb); -#else +#else /* #if defined(CONFIG_HOTPLUG_CPU) || !defined(MODULE) */ +#define cpu_notifier (fn, pri) do { (void)(fn); } while (0) +static inline int register_cpu_notifier (struct notifier_block *nb) {return 0 ; } -#endif static inline void unregister_cpu_notifier (struct notifier_block *nb) {
kernel/cpu.c +1 -2
@@ -182,8 +182,6 @@ static int cpu_notify(unsigned long val, void *v)return __cpu_notify (val, v,-1 ,NULL ); } -#ifdef CONFIG_HOTPLUG_CPU -static void cpu_notify_nofail (unsigned long val,void *v) {BUG_ON (cpu_notify (val, v));
@@ -198,6 +196,7 @@ void __ref unregister_cpu_notifier(struct notifier_block *nb) }EXPORT_SYMBOL (unregister_cpu_notifier); +#ifdef CONFIG_HOTPLUG_CPU /** * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU * @cpu: a CPU id
Signature · 7 lines
2.11.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 locking/rtmutex: Prevent dequeue vs. unlock race
From: Thomas Gleixner <[email protected]>
This patch has been added to the 3.12 stable tree. If you have any objections, please let us know.
===============
commit dbb26055defd03d59f678cb5f2c992abe05b064a upstream.
David reported a futex/rtmutex state corruption. It's caused by the following problem:
CPU0 CPU1 CPU2
l->owner=T1
rt_mutex_lock(l)
lock(l->wait_lock)
l->owner = T1 | HAS_WAITERS;
enqueue(T2)
boost()
unlock(l->wait_lock)
schedule()
rt_mutex_lock(l)
lock(l->wait_lock)
l->owner = T1 | HAS_WAITERS;
enqueue(T3)
boost()
unlock(l->wait_lock)
schedule()
signal(->T2) signal(->T3)
lock(l->wait_lock)
dequeue(T2)
deboost()
unlock(l->wait_lock)
lock(l->wait_lock)
dequeue(T3)
===> wait list is now empty
deboost()
unlock(l->wait_lock)
lock(l->wait_lock)
fixup_rt_mutex_waiters()
if (wait_list_empty(l)) {
owner = l->owner & ~HAS_WAITERS;
l->owner = owner
==> l->owner = T1
}
lock(l->wait_lock)
rt_mutex_unlock(l) fixup_rt_mutex_waiters()
if (wait_list_empty(l)) {
owner = l->owner & ~HAS_WAITERS;
cmpxchg(l->owner, T1, NULL)
===> Success (l->owner = NULL)
l->owner = owner
==> l->owner = T1
}That means the problem is caused by fixup_rt_mutex_waiters() which does the RMW to clear the waiters bit unconditionally when there are no waiters in the rtmutexes rbtree.
This can be fatal: A concurrent unlock can release the rtmutex in the fastpath because the waiters bit is not set. If the cmpxchg() gets in the middle of the RMW operation then the previous owner, which just unlocked the rtmutex is set as the owner again when the write takes place after the successfull cmpxchg().
The solution is rather trivial: verify that the owner member of the rtmutex has the waiters bit set before clearing it. This does not require a cmpxchg() or other atomic operations because the waiters bit can only be set and cleared with the rtmutex wait_lock held. It's also safe against the fast path unlock attempt. The unlock attempt via cmpxchg() will either see the bit set and take the slowpath or see the bit cleared and release it atomically in the fastpath.
It's remarkable that the test program provided by David triggers on ARM64 and MIPS64 really quick, but it refuses to reproduce on x86-64, while the problem exists there as well. That refusal might explain that this got not discovered earlier despite the bug existing from day one of the rtmutex implementation more than 10 years ago.
Thanks to David for meticulously instrumenting the code and providing the information which allowed to decode this subtle problem.
--- kernel/rtmutex.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-)
kernel/rtmutex.c +66 -2
@@ -64,8 +64,72 @@ static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)static void fixup_rt_mutex_waiters (struct rt_mutex *lock) { -if (!rt_mutex_has_waiters (lock)) -clear_rt_mutex_waiters (lock); +unsigned long owner, *p = (unsigned long *) &lock->owner; + +if (rt_mutex_has_waiters (lock)) +return ; + +/* +* The rbtree has no waiters enqueued, now make sure that the +* lock->owner still has the waiters bit set, otherwise the +* following can happen: +* +* CPU 0 CPU 1 CPU2 +* l->owner=T1 +* rt_mutex_lock(l) +* lock(l->lock) +* l->owner = T1 | HAS_WAITERS; +* enqueue(T2) +* boost() +* unlock(l->lock) +* block() +* +* rt_mutex_lock(l) +* lock(l->lock) +* l->owner = T1 | HAS_WAITERS; +* enqueue(T3) +* boost() +* unlock(l->lock) +* block() +* signal(->T2) signal(->T3) +* lock(l->lock) +* dequeue(T2) +* deboost() +* unlock(l->lock) +* lock(l->lock) +* dequeue(T3) +* ==> wait list is empty +* deboost() +* unlock(l->lock) +* lock(l->lock) +* fixup_rt_mutex_waiters() +* if (wait_list_empty(l) { +* l->owner = owner +* owner = l->owner & ~HAS_WAITERS; +* ==> l->owner = T1 +* } +* lock(l->lock) +* rt_mutex_unlock(l) fixup_rt_mutex_waiters() +* if (wait_list_empty(l) { +* owner = l->owner & ~HAS_WAITERS; +* cmpxchg(l->owner, T1, NULL) +* ===> Success (l->owner = NULL) +* +* l->owner = owner +* ==> l->owner = T1 +* } +* +* With the check for the waiter bit in place T3 on CPU2 will not +* overwrite. All tasks fiddling with the waiters bit are +* serialized by l->lock, so nothing else can modify the waiters +* bit. If the bit is set then nothing can change l->owner either +* so the simple RMW is safe. The cmpxchg() will simply fail if it +* happens in the middle of the RMW because the waiters bit is +* still set. +*/ + owner =READ_ONCE (*p); +if (owner &RT_MUTEX_HAS_WAITERS ) +WRITE_ONCE (*p, owner & ~RT_MUTEX_HAS_WAITERS ); }/*
Signature · 1 line
2.11.0
PATCH 1/1 locking/rtmutex: Use READ_ONCE() in rt_mutex_owner()
From: Thomas Gleixner <[email protected]>
This patch has been added to the 3.12 stable tree. If you have any objections, please let us know.
===============
commit 1be5d4fa0af34fb7bafa205aeb59f5c7cc7a089d upstream.
While debugging the rtmutex unlock vs. dequeue race Will suggested to use READ_ONCE() in rt_mutex_owner() as it might race against the cmpxchg_release() in unlock_rt_mutex_safe().
Will: "It's a minor thing which will most likely not matter in practice"
Careful search did not unearth an actual problem in todays code, but it's better to be safe than surprised.
--- kernel/rtmutex_common.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/rtmutex_common.h +3 -2
@@ -96,8 +96,9 @@ task_top_pi_waiter(struct task_struct *p)static inline struct task_struct *rt_mutex_owner (struct rt_mutex *lock) { -return (struct task_struct *) - ((unsigned long )lock->owner & ~RT_MUTEX_OWNER_MASKALL ); +unsigned long owner = (unsigned long )READ_ONCE (lock->owner); + +return (struct task_struct *) (owner & ~RT_MUTEX_OWNER_MASKALL ); }/*
Signature · 1 line
2.11.0
PATCH 1/1 perf/x86: Fix full width counter, counter overflow
In reply to Jiri SlabyFrom: "Peter Zijlstra (Intel)" <[email protected]>
This patch has been added to the 3.12 stable tree. If you have any objections, please let us know.
===============
commit 7f612a7f0bc13a2361a152862435b7941156b6af upstream.
Lukasz reported that perf stat counters overflow handling is broken on KNL/SLM.
Both these parts have full_width_write set, and that does indeed have a problem. In order to deal with counter wrap, we must sample the counter at at least half the counter period (see also the sampling theorem) such that we can unambiguously reconstruct the count.
However commit:
069e0c3c4058 ("perf/x86/intel: Support full width counting")
sets the sampling interval to the full period, not half.
Fixing that exposes another issue, in that we must not sign extend the delta value when we shift it right; the counter cannot have decremented after all.
With both these issues fixed, counter overflow functions correctly again.
--- arch/x86/kernel/cpu/perf_event.c | 2 +- arch/x86/kernel/cpu/perf_event_intel.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.c +1 -1
@@ -64,7 +64,7 @@ u64 x86_perf_event_update(struct perf_event *event)int shift =64 - x86_pmu.cntval_bits;u64 prev_raw_count, new_raw_count;int idx = hwc->idx; -s64 delta; +u64 delta;if (idx ==INTEL_PMC_IDX_FIXED_BTS )return 0 ;
arch/x86/kernel/cpu/perf_event_intel.c +1 -1
@@ -2578,7 +2578,7 @@ __init int intel_pmu_init(void)/* Support full width counters using alternative MSR range */ if (x86_pmu.intel_cap.full_width_write) { - x86_pmu.max_period = x86_pmu.cntval_mask; + x86_pmu.max_period = x86_pmu.cntval_mask >>1 ; x86_pmu.perfctr =MSR_IA32_PMC0 ;pr_cont ("full-width counters, " ); }
Signature · 1 line
2.11.0
PATCH 1/1 can: raw: raw_setsockopt: limit number of can_filter that can be set
In reply to Jiri SlabyFrom: Marc Kleine-Budde <[email protected]>
This patch has been added to the 3.12 stable tree. If you have any objections, please let us know.
===============
commit 332b05ca7a438f857c61a3c21a88489a21532364 upstream.
This patch adds a check to limit the number of can_filters that can be set via setsockopt on CAN_RAW sockets. Otherwise allocations > MAX_ORDER are not prevented resulting in a warning.
Reference: https://lkml.org/lkml/2016/12/2/230
--- include/uapi/linux/can.h | 1 + net/can/raw.c | 3 +++ 2 files changed, 4 insertions(+)
include/uapi/linux/can.h +1 -0
@@ -158,5 +158,6 @@ struct can_filter { };#define CAN_INV_FILTER 0x20000000U/* to be set in can_filter.can_id */ +#define CAN_RAW_FILTER_MAX 512/* maximum number of can_filter set via setsockopt() */ #endif /* CAN_H */
net/can/raw.c +3 -0
@@ -470,6 +470,9 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,if (optlen %sizeof (struct can_filter ) !=0 )return -EINVAL ; +if (optlen >CAN_RAW_FILTER_MAX *sizeof (struct can_filter )) +return -EINVAL ; + count = optlen /sizeof (struct can_filter );if (count >1 ) {
Signature · 1 line
2.11.0
PATCH 1/1 can: peak: fix bad memory access and free sequence
In reply to Jiri SlabyFrom: 추지호 <[email protected]>
This patch has been added to the 3.12 stable tree. If you have any objections, please let us know.
===============
commit b67d0dd7d0dc9e456825447bbeb935d8ef43ea7c upstream.
Fix for bad memory access while disconnecting. netdev is freed before private data free, and dev is accessed after freeing netdev.
This makes a slub problem, and it raise kernel oops with slub debugger config.
--- drivers/net/can/usb/peak_usb/pcan_usb_core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/can/usb/peak_usb/pcan_usb_core.c +4 -2
@@ -824,23 +824,25 @@ lbl_free_candev:static void peak_usb_disconnect (struct usb_interface *intf) {struct peak_usb_device *dev; +struct peak_usb_device *dev_prev_siblings;/* unregister as many netdev devices as siblings */ -for (dev =usb_get_intfdata (intf); dev; dev = dev->prev_siblings) { +for (dev =usb_get_intfdata (intf); dev; dev = dev_prev_siblings) {struct net_device *netdev = dev->netdev;char name[IFNAMSIZ ]; + dev_prev_siblings = dev->prev_siblings; dev->state &= ~PCAN_USB_STATE_CONNECTED ;strncpy (name, netdev->name,IFNAMSIZ );unregister_netdev (netdev); -free_candev (netdev);kfree (dev->cmd_buf); dev->next_siblings =NULL ;if (dev->adapter->dev_free) dev->adapter->dev_free (dev); +free_candev (netdev);dev_info (&intf->dev,"%s removed\n" , name); }
Signature · 1 line
2.11.0
PATCH 1/1 m68k: Fix ndelay() macro
In reply to Jiri SlabyFrom: Boris Brezillon <[email protected]>
This patch has been added to the 3.12 stable tree. If you have any objections, please let us know.
===============
commit 7e251bb21ae08ca2e4fb28cc0981fac2685a8efa upstream.
The current ndelay() macro definition has an extra semi-colon at the end of the line thus leading to a compilation error when ndelay is used in a conditional block without curly braces like this one:
if (cond) ndelay(t); else ...
which, after the preprocessor pass gives:
if (cond) m68k_ndelay(t);; else ...
thus leading to the following gcc error:
error: 'else' without a previous 'if'
Remove this extra semi-colon.
--- arch/m68k/include/asm/delay.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/m68k/include/asm/delay.h +1 -1
@@ -114,6 +114,6 @@ static inline void __udelay(unsigned long usecs)*/ #define HZSCALE (268435456 / (1000000 / HZ)) -#define ndelay (n) __delay(DIV_ROUND_UP((n) * ((((HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6), 1000)); +#define ndelay (n) __delay(DIV_ROUND_UP((n) * ((((HZSCALE) >> 11) * (loops_per_jiffy >> 11)) >> 6), 1000))#endif /* defined(_M68K_DELAY_H) */
Signature · 1 line
2.11.0