sctp: Prevent soft lockup when sctp_accept() is called during a timeout event
Subsystems (2)
PATCH 3/5 x86/mm: Add barriers and document switch_mm()-vs-flush synchronization
In reply to Chas WilliamsFrom: Andy Lutomirski <[email protected]>
commit 71b3c126e61177eb693423f2e18a1914205b165e upstream.
When switch_mm() activates a new PGD, it also sets a bit that tells other CPUs that the PGD is in use so that TLB flush IPIs will be sent. In order for that to work correctly, the bit needs to be visible prior to loading the PGD and therefore starting to fill the local TLB.
Document all the barriers that make this work correctly and add a couple that were missing.
[ luis: backported to 3.16:
- dropped N/A comment in flush_tlb_mm_range()
- adjusted context ]
--- arch/x86/include/asm/mmu_context.h | 32 +++++++++++++++++++++++++++++++- arch/x86/mm/tlb.c | 25 ++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-)
arch/x86/include/asm/mmu_context.h +31 -1
@@ -42,7 +42,32 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,#endif cpumask_set_cpu (cpu,mm_cpumask (next)); -/* Re-load page tables */ +/* +* Re-load page tables. +* +* This logic has an ordering constraint: +* +* CPU 0: Write to a PTE for 'next' +* CPU 0: load bit 1 in mm_cpumask. if nonzero, send IPI. +* CPU 1: set bit 1 in next's mm_cpumask +* CPU 1: load from the PTE that CPU 0 writes (implicit) +* +* We need to prevent an outcome in which CPU 1 observes +* the new PTE value and CPU 0 observes bit 1 clear in +* mm_cpumask. (If that occurs, then the IPI will never +* be sent, and CPU 0's TLB will contain a stale entry.) +* +* The bad outcome can occur if either CPU's load is +* reordered before that CPU's store, so both CPUs much +* execute full barriers to prevent this from happening. +* +* Thus, switch_mm needs a full barrier between the +* store to mm_cpumask and any operation that could load +* from next->pgd. This barrier synchronizes with +* remote TLB flushers. Fortunately, load_cr3 is +* serializing and thus acts as a full barrier. +* +*/ load_cr3 (next->pgd);/* Stop flush ipis for the previous mm */
@@ -65,10 +90,15 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,* schedule, protecting us from simultaneous changes. */ cpumask_set_cpu (cpu,mm_cpumask (next)); +/* * We were in lazy tlb mode and leave_mm disabled * tlb flush IPI delivery. We must reload CR3 * to make sure to use no freed page tables. +* +* As above, this is a barrier that forces +* TLB repopulation to be ordered after the +* store to mm_cpumask. */ load_cr3 (next->pgd);load_LDT_nolock (&next->context);
arch/x86/mm/tlb.c +22 -3
@@ -152,7 +152,10 @@ void flush_tlb_current_task(void)preempt_disable ();count_vm_tlb_event (NR_TLB_LOCAL_FLUSH_ALL ); + +/* This is an implicit full barrier that synchronizes with switch_mm. */ local_flush_tlb (); +if (cpumask_any_but (mm_cpumask (mm),smp_processor_id ()) < nr_cpu_ids)flush_tlb_others (mm_cpumask (mm), mm,0UL ,TLB_FLUSH_ALL );preempt_enable ();
@@ -166,11 +169,19 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,unsigned long nr_base_pages;preempt_disable (); -if (current->active_mm != mm) +if (current->active_mm != mm) { +/* Synchronize with switch_mm. */ +smp_mb (); + goto flush_all; + }if (!current->mm) {leave_mm (smp_processor_id ()); + +/* Synchronize with switch_mm. */ +smp_mb (); + goto flush_all; }
@@ -222,10 +233,18 @@ void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)preempt_disable ();if (current->active_mm == mm) { -if (current->mm) +if (current->mm) { +/* +* Implicit full barrier (INVLPG) that synchronizes +* with switch_mm. +*/ __flush_tlb_one (start); -else + }else {leave_mm (smp_processor_id ()); + +/* Synchronize with switch_mm. */ +smp_mb (); + } }if (cpumask_any_but (mm_cpumask (mm),smp_processor_id ()) < nr_cpu_ids)
Signature · 7 lines
2.5.5 -- 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/5 sctp: Prevent soft lockup when sctp_accept() is called during a timeout event
From: Karl Heiss <[email protected]>
commit 635682a14427d241bab7bbdeebb48a7d7b91638e upstream.
A case can occur when sctp_accept() is called by the user during a heartbeat timeout event after the 4-way handshake. Since sctp_assoc_migrate() changes both assoc->base.sk and assoc->ep, the bh_sock_lock in sctp_generate_heartbeat_event() will be taken with the listening socket but released with the new association socket. The result is a deadlock on any future attempts to take the listening socket lock.
Note that this race can occur with other SCTP timeouts that take the bh_lock_sock() in the event sctp_accept() is called.
BUG: soft lockup - CPU#9 stuck for 67s! [swapper:0] ... RIP: 0010:[<ffffffff8152d48e>] [<ffffffff8152d48e>] _spin_lock+0x1e/0x30 RSP: 0018:ffff880028323b20 EFLAGS: 00000206 RAX: 0000000000000002 RBX: ffff880028323b20 RCX: 0000000000000000 RDX: 0000000000000000 RSI: ffff880028323be0 RDI: ffff8804632c4b48 RBP: ffffffff8100bb93 R08: 0000000000000000 R09: 0000000000000000 R10: ffff880610662280 R11: 0000000000000100 R12: ffff880028323aa0 R13: ffff8804383c3880 R14: ffff880028323a90 R15: ffffffff81534225 FS: 0000000000000000(0000) GS:ffff880028320000(0000) knlGS:0000000000000000 CS: 0010 DS: 0018 ES: 0018 CR0: 000000008005003b CR2: 00000000006df528 CR3: 0000000001a85000 CR4: 00000000000006e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400 Process swapper (pid: 0, threadinfo ffff880616b70000, task ffff880616b6cab0) Stack: ffff880028323c40 ffffffffa01c2582 ffff880614cfb020 0000000000000000 <d> 0100000000000000 00000014383a6c44 ffff8804383c3880 ffff880614e93c00 <d> ffff880614e93c00 0000000000000000 ffff8804632c4b00 ffff8804383c38b8 Call Trace: <IRQ> [<ffffffffa01c2582>] ? sctp_rcv+0x492/0xa10 [sctp] [<ffffffff8148c559>] ? nf_iterate+0x69/0xb0 [<ffffffff814974a0>] ? ip_local_deliver_finish+0x0/0x2d0 [<ffffffff8148c716>] ? nf_hook_slow+0x76/0x120 [<ffffffff814974a0>] ? ip_local_deliver_finish+0x0/0x2d0 [<ffffffff8149757d>] ? ip_local_deliver_finish+0xdd/0x2d0 [<ffffffff81497808>] ? ip_local_deliver+0x98/0xa0 [<ffffffff81496ccd>] ? ip_rcv_finish+0x12d/0x440 [<ffffffff81497255>] ? ip_rcv+0x275/0x350 [<ffffffff8145cfeb>] ? __netif_receive_skb+0x4ab/0x750 ...
With lockdep debugging:
=====================================
[ BUG: bad unlock balance detected! ]
-------------------------------------
CslRx/12087 is trying to release lock (slock-AF_INET) at:
[<ffffffffa01bcae0>] sctp_generate_timeout_event+0x40/0xe0 [sctp] but there are no more locks to release!
other info that might help us debug this:
2 locks held by CslRx/12087:
#0: (&asoc->timers[i]){+.-...}, at: [<ffffffff8108ce1f>] run_timer_softirq+0x16f/0x3e0 #1: (slock-AF_INET){+.-...}, at: [<ffffffffa01bcac3>] sctp_generate_timeout_event+0x23/0xe0 [sctp]
Ensure the socket taken is also the same one that is released by saving a copy of the socket before entering the timeout event critical section.
--- net/sctp/sm_sideeffect.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)
net/sctp/sm_sideeffect.c +23 -19
@@ -244,12 +244,13 @@ void sctp_generate_t3_rtx_event(unsigned long peer)int error;struct sctp_transport *transport = (struct sctp_transport *) peer;struct sctp_association *asoc = transport->asoc; -struct net *net =sock_net (asoc->base.sk); +struct sock *sk = asoc->base.sk; +struct net *net =sock_net (sk);/* Check whether a task is in the sock. */ -bh_lock_sock (asoc->base.sk); -if (sock_owned_by_user (asoc->base.sk)) { +bh_lock_sock (sk); +if (sock_owned_by_user (sk)) {pr_debug ("%s: sock is busy\n" , __func__);/* Try again later. */
@@ -272,10 +273,10 @@ void sctp_generate_t3_rtx_event(unsigned long peer) transport, GFP_ATOMIC);if (error) - asoc->base.sk->sk_err = -error; + sk->sk_err = -error; out_unlock: -bh_unlock_sock (asoc->base.sk); +bh_unlock_sock (sk);sctp_transport_put (transport); }
@@ -285,11 +286,12 @@ out_unlock:static void sctp_generate_timeout_event (struct sctp_association *asoc,sctp_event_timeout_t timeout_type) { -struct net *net =sock_net (asoc->base.sk); +struct sock *sk = asoc->base.sk; +struct net *net =sock_net (sk);int error =0 ; -bh_lock_sock (asoc->base.sk); -if (sock_owned_by_user (asoc->base.sk)) { +bh_lock_sock (sk); +if (sock_owned_by_user (sk)) {pr_debug ("%s: sock is busy: timer %d\n" , __func__, timeout_type);
@@ -312,10 +314,10 @@ static void sctp_generate_timeout_event(struct sctp_association *asoc, (void *)timeout_type, GFP_ATOMIC);if (error) - asoc->base.sk->sk_err = -error; + sk->sk_err = -error; out_unlock: -bh_unlock_sock (asoc->base.sk); +bh_unlock_sock (sk);sctp_association_put (asoc); }
@@ -365,10 +367,11 @@ void sctp_generate_heartbeat_event(unsigned long data)int error =0 ;struct sctp_transport *transport = (struct sctp_transport *) data;struct sctp_association *asoc = transport->asoc; -struct net *net =sock_net (asoc->base.sk); +struct sock *sk = asoc->base.sk; +struct net *net =sock_net (sk); -bh_lock_sock (asoc->base.sk); -if (sock_owned_by_user (asoc->base.sk)) { +bh_lock_sock (sk); +if (sock_owned_by_user (sk)) {pr_debug ("%s: sock is busy\n" , __func__);/* Try again later. */
@@ -389,10 +392,10 @@ void sctp_generate_heartbeat_event(unsigned long data) transport, GFP_ATOMIC);if (error) - asoc->base.sk->sk_err = -error; + sk->sk_err = -error; out_unlock: -bh_unlock_sock (asoc->base.sk); +bh_unlock_sock (sk);sctp_transport_put (transport); }
@@ -403,10 +406,11 @@ void sctp_generate_proto_unreach_event(unsigned long data) {struct sctp_transport *transport = (struct sctp_transport *) data;struct sctp_association *asoc = transport->asoc; -struct net *net =sock_net (asoc->base.sk); +struct sock *sk = asoc->base.sk; +struct net *net =sock_net (sk); -bh_lock_sock (asoc->base.sk); -if (sock_owned_by_user (asoc->base.sk)) { +bh_lock_sock (sk); +if (sock_owned_by_user (sk)) {pr_debug ("%s: sock is busy\n" , __func__);/* Try again later. */
@@ -427,7 +431,7 @@ void sctp_generate_proto_unreach_event(unsigned long data) asoc->state, asoc->ep, asoc, transport, GFP_ATOMIC); out_unlock: -bh_unlock_sock (asoc->base.sk); +bh_unlock_sock (sk);sctp_association_put (asoc); }
Signature · 1 line
2.5.5
PATCH 2/5 USB: fix invalid memory access in hub_activate()
From: Alan Stern <[email protected]>
commit e50293ef9775c5f1cf3fcc093037dd6a8c5684ea upstream.
Commit 8520f38099cc ("USB: change hub initialization sleeps to delayed_work") changed the hub_activate() routine to make part of it run in a workqueue. However, the commit failed to take a reference to the usb_hub structure or to lock the hub interface while doing so. As a result, if a hub is plugged in and quickly unplugged before the work routine can run, the routine will try to access memory that has been deallocated. Or, if the hub is unplugged while the routine is running, the memory may be deallocated while it is in active use.
This patch fixes the problem by taking a reference to the usb_hub at the start of hub_activate() and releasing it at the end (when the work is finished), and by locking the hub interface while the work routine is running. It also adds a check at the start of the routine to see if the hub has already been disconnected, in which nothing should be done.
[ luis: backported to 3.16:
- Added forward declaration of hub_release() which mainline had with commit
32a6958998c5 ("usb: hub: convert khubd into workqueue") ]--- drivers/usb/core/hub.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
drivers/usb/core/hub.c +20 -3
@@ -106,6 +106,7 @@ EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);#define HUB_DEBOUNCE_STEP 25#define HUB_DEBOUNCE_STABLE 100 +static void hub_release (struct kref *kref);static int usb_reset_and_verify_device (struct usb_device *udev);static inline char *portspeed (struct usb_hub *hub,int portstatus)
@@ -1023,10 +1024,20 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)unsigned delay;/* Continue a partial initialization */ -if (type ==HUB_INIT2 ) - goto init2; -if (type ==HUB_INIT3 ) +if (type ==HUB_INIT2 || type ==HUB_INIT3 ) { +device_lock (hub->intfdev); + +/* Was the hub disconnected while we were waiting? */ +if (hub->disconnected) { +device_unlock (hub->intfdev); +kref_put (&hub->kref, hub_release); +return ; + } +if (type ==HUB_INIT2 ) + goto init2; goto init3; + } +kref_get (&hub->kref);/* The superspeed hub except for root hub has to use Hub Depth * value as an offset into the route string to locate the bits
@@ -1220,6 +1231,7 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)PREPARE_DELAYED_WORK (&hub->init_work, hub_init_func3);schedule_delayed_work (&hub->init_work,msecs_to_jiffies (delay)); +device_unlock (hub->intfdev);return ;/* Continues at init3: below */ }else {msleep (delay);
@@ -1240,6 +1252,11 @@ static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)/* Allow autosuspend if it was suppressed */ if (type <=HUB_INIT3 )usb_autopm_put_interface_async (to_usb_interface (hub->intfdev)); + +if (type ==HUB_INIT2 || type ==HUB_INIT3 ) +device_unlock (hub->intfdev); + +kref_put (&hub->kref, hub_release); }/* Implement the continuations for the delays above */
Signature · 1 line
2.5.5
PATCH 4/5 x86/mm: Improve switch_mm() barrier comments
In reply to Chas WilliamsFrom: Andy Lutomirski <[email protected]>
commit 4eaffdd5a5fe6ff9f95e1ab4de1ac904d5e0fa8b upstream.
My previous comments were still a bit confusing and there was a typo. Fix it up.
--- arch/x86/include/asm/mmu_context.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
arch/x86/include/asm/mmu_context.h +8 -7
@@ -58,14 +58,16 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,* be sent, and CPU 0's TLB will contain a stale entry.) * * The bad outcome can occur if either CPU's load is -* reordered before that CPU's store, so both CPUs much +* reordered before that CPU's store, so both CPUs must * execute full barriers to prevent this from happening. * * Thus, switch_mm needs a full barrier between the * store to mm_cpumask and any operation that could load -* from next->pgd. This barrier synchronizes with -* remote TLB flushers. Fortunately, load_cr3 is -* serializing and thus acts as a full barrier. +* from next->pgd. TLB fills are special and can happen +* due to instruction fetches or for no reason at all, +* and neither LOCK nor MFENCE orders them. +* Fortunately, load_cr3() is serializing and gives the +* ordering guarantee we need. * */ load_cr3 (next->pgd);
@@ -96,9 +98,8 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,* tlb flush IPI delivery. We must reload CR3 * to make sure to use no freed page tables. * -* As above, this is a barrier that forces -* TLB repopulation to be ordered after the -* store to mm_cpumask. +* As above, load_cr3() is serializing and orders TLB +* fills with respect to the mm_cpumask write. */ load_cr3 (next->pgd);load_LDT_nolock (&next->context);
Signature · 1 line
2.5.5
PATCH 5/5 udp: properly support MSG_PEEK with truncated buffers
In reply to Chas WilliamsFrom: Eric Dumazet <[email protected]>
commit 197c949e7798fbf28cfadc69d9ca0c2abbf93191 upstream.
Backport of this upstream commit into stable kernels :89c22d8c3b27 ("net: Fix skb csum races when peeking") exposed a bug in udp stack vs MSG_PEEK support, when user provides a buffer smaller than skb payload.
In this case, skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr),
msg->msg_iov);
returns -EFAULT.
This bug does not happen in upstream kernels since Al Viro did a great job to replace this into :skb_copy_and_csum_datagram_msg(skb, sizeof(struct udphdr), msg); This variant is safe vs short buffers.
For the time being, instead reverting Herbert Xu patch and add back skb->ip_summed invalid changes, simply store the result of udp_lib_checksum_complete() so that we avoid computing the checksum a second time, and avoid the problematic skb_copy_and_csum_datagram_iovec() call.
This patch can be applied on recent kernels as it avoids a double checksumming, then backported to stable kernels as a bug fix.
--- net/ipv4/udp.c | 6 ++++-- net/ipv6/udp.c | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-)
net/ipv4/udp.c +4 -2
@@ -1233,6 +1233,7 @@ int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,int peeked, off =0 ;int err;int is_udplite =IS_UDPLITE (sk); +bool checksum_valid = false;bool slow;if (flags &MSG_ERRQUEUE )
@@ -1258,11 +1259,12 @@ try_again:*/ if (copied < ulen ||UDP_SKB_CB (skb)->partial_cov) { -if (udp_lib_checksum_complete (skb)) + checksum_valid = !udp_lib_checksum_complete (skb); +if (!checksum_valid) goto csum_copy_err; } -if (skb_csum_unnecessary (skb)) +if (checksum_valid ||skb_csum_unnecessary (skb)) err =skb_copy_datagram_iovec (skb,sizeof (struct udphdr ), msg->msg_iov, copied);else {
net/ipv6/udp.c +4 -2
@@ -389,6 +389,7 @@ int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,int peeked, off =0 ;int err;int is_udplite =IS_UDPLITE (sk); +bool checksum_valid = false;int is_udp4;bool slow;
@@ -420,11 +421,12 @@ try_again:*/ if (copied < ulen ||UDP_SKB_CB (skb)->partial_cov) { -if (udp_lib_checksum_complete (skb)) + checksum_valid = !udp_lib_checksum_complete (skb); +if (!checksum_valid) goto csum_copy_err; } -if (skb_csum_unnecessary (skb)) +if (checksum_valid ||skb_csum_unnecessary (skb)) err =skb_copy_datagram_iovec (skb,sizeof (struct udphdr ), msg->msg_iov, copied);else {
Signature · 1 line
2.5.5