From mboxrd@z Thu Jan 1 00:00:00 1970 From: Liu Yu Subject: [PATCH] reduce the spinlock conflict during massive connect Date: Mon, 6 Nov 2017 10:28:21 +0800 Message-ID: <9b38c346-0035-4c12-21e7-dbde9f961c8a@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=gbk Content-Transfer-Encoding: 7bit Cc: "\"David" "S." "Miller\"" ";Alexey" Kuznetsov ";Hideaki" YOSHIFUJI To: netdev@vger.kernel.org Return-path: Received: from mail-pf0-f194.google.com ([209.85.192.194]:56132 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750857AbdKFC2Z (ORCPT ); Sun, 5 Nov 2017 21:28:25 -0500 Received: by mail-pf0-f194.google.com with SMTP id 17so6611422pfn.12 for ; Sun, 05 Nov 2017 18:28:24 -0800 (PST) In-Reply-To: Sender: netdev-owner@vger.kernel.org List-ID: From: Liu Yu When a mount of processes connect to the same port at the same address simultaneously, they are likely getting the same bhash and therefore conflict with each other. The more the cpu number, the worse in this case. Use spin_trylock instead for this scene, which seems doesn't matter for common case. Signed-off-by: Liu Yu --- net/ipv4/inet_hashtables.c | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-) diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index e7d15fb..cc11ec7 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -581,13 +581,17 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row, other_parity_scan: port = low + offset; for (i = 0; i < remaining; i += 2, port += 2) { + int ret; + if (unlikely(port >= high)) port -= remaining; if (inet_is_local_reserved_port(net, port)) continue; head = &hinfo->bhash[inet_bhashfn(net, port, hinfo->bhash_size)]; - spin_lock_bh(&head->lock); + ret = spin_trylock(&head->lock); + if (unlikely(!ret)) + continue; /* Does not bother with rcv_saddr checks, because * the established check is already unique enough. -- 1.7.1