Skip to content

Commit 3106ecb

Browse files
lxindavem330
authored andcommitted
sctp: not disable bh in the whole sctp_get_port_local()
With disabling bh in the whole sctp_get_port_local(), when snum == 0 and too many ports have been used, the do-while loop will take the cpu for a long time and cause cpu stuck: [ ] watchdog: BUG: soft lockup - CPU#11 stuck for 22s! [ ] RIP: 0010:native_queued_spin_lock_slowpath+0x4de/0x940 [ ] Call Trace: [ ] _raw_spin_lock+0xc1/0xd0 [ ] sctp_get_port_local+0x527/0x650 [sctp] [ ] sctp_do_bind+0x208/0x5e0 [sctp] [ ] sctp_autobind+0x165/0x1e0 [sctp] [ ] sctp_connect_new_asoc+0x355/0x480 [sctp] [ ] __sctp_connect+0x360/0xb10 [sctp] There's no need to disable bh in the whole function of sctp_get_port_local. So fix this cpu stuck by removing local_bh_disable() called at the beginning, and using spin_lock_bh() instead. The same thing was actually done for inet_csk_get_port() in Commit ea8add2 ("tcp/dccp: better use of ephemeral ports in bind()"). Thanks to Marcelo for pointing the buggy code out. v1->v2: - use cond_resched() to yield cpu to other tasks if needed, as Eric noticed. Fixes: 1da177e ("Linux-2.6.12-rc2") Reported-by: Ying Xu <yinxu@redhat.com> Signed-off-by: Xin Long <lucien.xin@gmail.com> Acked-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1838d6c commit 3106ecb

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

‎net/sctp/socket.c‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8060,8 +8060,6 @@ static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
80608060

80618061
pr_debug("%s: begins, snum:%d\n", __func__, snum);
80628062

8063-
local_bh_disable();
8064-
80658063
if (snum == 0) {
80668064
/* Search for an available port. */
80678065
int low, high, remaining, index;
@@ -8079,20 +8077,21 @@ static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
80798077
continue;
80808078
index = sctp_phashfn(net, rover);
80818079
head = &sctp_port_hashtable[index];
8082-
spin_lock(&head->lock);
8080+
spin_lock_bh(&head->lock);
80838081
sctp_for_each_hentry(pp, &head->chain)
80848082
if ((pp->port == rover) &&
80858083
net_eq(net, pp->net))
80868084
goto next;
80878085
break;
80888086
next:
8089-
spin_unlock(&head->lock);
8087+
spin_unlock_bh(&head->lock);
8088+
cond_resched();
80908089
} while (--remaining > 0);
80918090

80928091
/* Exhausted local port range during search? */
80938092
ret = 1;
80948093
if (remaining <= 0)
8095-
goto fail;
8094+
return ret;
80968095

80978096
/* OK, here is the one we will use. HEAD (the port
80988097
* hash table list entry) is non-NULL and we hold it's
@@ -8107,7 +8106,7 @@ static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
81078106
* port iterator, pp being NULL.
81088107
*/
81098108
head = &sctp_port_hashtable[sctp_phashfn(net, snum)];
8110-
spin_lock(&head->lock);
8109+
spin_lock_bh(&head->lock);
81118110
sctp_for_each_hentry(pp, &head->chain) {
81128111
if ((pp->port == snum) && net_eq(pp->net, net))
81138112
goto pp_found;
@@ -8207,10 +8206,7 @@ static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
82078206
ret = 0;
82088207

82098208
fail_unlock:
8210-
spin_unlock(&head->lock);
8211-
8212-
fail:
8213-
local_bh_enable();
8209+
spin_unlock_bh(&head->lock);
82148210
return ret;
82158211
}
82168212

0 commit comments

Comments
 (0)