Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3600771pxb; Mon, 24 Jan 2022 13:14:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJwXAOfgVfr09MPJI9xHhkoOa1UwBqazFC8OZYxwbiGE/YHqGzvc2YfJ0tznEQA6//6QmNBz X-Received: by 2002:a17:902:9887:b0:14a:199:bc51 with SMTP id s7-20020a170902988700b0014a0199bc51mr16013622plp.39.1643058848707; Mon, 24 Jan 2022 13:14:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643058848; cv=none; d=google.com; s=arc-20160816; b=ZWzUi/nVGOh/zQIfnsgnEV90cZN+xyKwMPow2ecavIhNSlFhVMM7rZObI48P/oISxF rtqnZBIAwxxW40t9SMtG9aCBseMiD9tbb7o2bZc+w8EZ1ljKejI9T3jQE9Sn3H0gzepa g4wN34v+MBMuv0UrYpWdaUWFrdOzQU3w5ih2GS4ldSF+Haykz91TrzOgV27ihX1L7w+D KcliLdfKSb2yz4e6zwdlS9jtBgfIiWLtcFHBt3Uekwjpn71UdkGTZQcHRsR2Tx8PF0Hx YYU7oPrN0C+wuHhrFOLAJsdaVA8cXJECDpkWofQf7JKQrTgSP5dki2ctcQuAFv3v2V5e fhhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pwbNSQMtKJOkN3ppgaf+xZFlYWNnKBygbZ6nBR41Zrs=; b=e0xzuRvCtOJEe5hxTs3ViermbzeN17T3vEUUWFrtQGJp0r73UuWIrGUUn61smAM0A5 jq2sT/D/zTD5XTBt2boF3QHfIlqGNSkvLIaRi4oyPhAzy5MC1dOQ4HxifQQc73eh91Ln idh4fNHlHEvQjdj8Aw7E+uz2L5DqHtvYBCMxLqXbW+PuVZSDWaT/MFiGJiyu7Hi9jaZJ ksf9vQ/SK0vWvGthuuJ7e1+4F3jRqAzeL7nVJfidCsLXhirL2K8q5NrLxb5iLv3kV77h TqOsHtMM+MunK4Dv4lASDxIUVJiUPOrf6npx16ULfTk3mZ6OVUXrSdxNXRmOY68gU63F s5aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="r62/BZaF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q9si16414317plx.119.2022.01.24.13.13.55; Mon, 24 Jan 2022 13:14:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="r62/BZaF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1389006AbiAXUkW (ORCPT + 99 others); Mon, 24 Jan 2022 15:40:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351141AbiAXUT0 (ORCPT ); Mon, 24 Jan 2022 15:19:26 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF446C0612F5; Mon, 24 Jan 2022 11:38:59 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 75DE8B81229; Mon, 24 Jan 2022 19:38:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9970DC340E5; Mon, 24 Jan 2022 19:38:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643053137; bh=v3qNmppBobj181G6eIp2mj/h+JGFuzf+50jMrh4mUIc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r62/BZaFwG/AOLCJgFhSFU688fQP0OhmUd3y9YwJGRsLgarMFv5KHWljaU/Ew5MuQ aEQh5DVsRPdrL+7RoGiDbi3rXBT790osk+2oQZhozEfBt4XTsTaksFGwktgeG+wqHO T/TQUrOxkyzfNHOvt/6MtS3gSYnftzwwRaC2L5p0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , David Ahern , Jakub Kicinski Subject: [PATCH 5.4 289/320] ipv4: avoid quadratic behavior in netns dismantle Date: Mon, 24 Jan 2022 19:44:33 +0100 Message-Id: <20220124184003.787440229@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183953.750177707@linuxfoundation.org> References: <20220124183953.750177707@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet commit d07418afea8f1d9896aaf9dc5ae47ac4f45b220c upstream. net/ipv4/fib_semantics.c uses an hash table of 256 slots, keyed by device ifindexes: fib_info_devhash[DEVINDEX_HASHSIZE] Problem is that with network namespaces, devices tend to use the same ifindex. lo device for instance has a fixed ifindex of one, for all network namespaces. This means that hosts with thousands of netns spend a lot of time looking at some hash buckets with thousands of elements, notably at netns dismantle. Simply add a per netns perturbation (net_hash_mix()) to spread elements more uniformely. Also change fib_devindex_hashfn() to use more entropy. Fixes: aa79e66eee5d ("net: Make ifindex generation per-net namespace") Signed-off-by: Eric Dumazet Reviewed-by: David Ahern Signed-off-by: Jakub Kicinski Signed-off-by: Greg Kroah-Hartman --- net/ipv4/fib_semantics.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) --- a/net/ipv4/fib_semantics.c +++ b/net/ipv4/fib_semantics.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -318,11 +319,15 @@ static inline int nh_comp(struct fib_inf static inline unsigned int fib_devindex_hashfn(unsigned int val) { - unsigned int mask = DEVINDEX_HASHSIZE - 1; + return hash_32(val, DEVINDEX_HASHBITS); +} + +static struct hlist_head * +fib_info_devhash_bucket(const struct net_device *dev) +{ + u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex; - return (val ^ - (val >> DEVINDEX_HASHBITS) ^ - (val >> (DEVINDEX_HASHBITS * 2))) & mask; + return &fib_info_devhash[fib_devindex_hashfn(val)]; } static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope, @@ -432,12 +437,11 @@ int ip_fib_check_default(__be32 gw, stru { struct hlist_head *head; struct fib_nh *nh; - unsigned int hash; spin_lock(&fib_info_lock); - hash = fib_devindex_hashfn(dev->ifindex); - head = &fib_info_devhash[hash]; + head = fib_info_devhash_bucket(dev); + hlist_for_each_entry(nh, head, nh_hash) { if (nh->fib_nh_dev == dev && nh->fib_nh_gw4 == gw && @@ -1594,12 +1598,10 @@ link_it: } else { change_nexthops(fi) { struct hlist_head *head; - unsigned int hash; if (!nexthop_nh->fib_nh_dev) continue; - hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex); - head = &fib_info_devhash[hash]; + head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev); hlist_add_head(&nexthop_nh->nh_hash, head); } endfor_nexthops(fi) } @@ -1940,8 +1942,7 @@ void fib_nhc_update_mtu(struct fib_nh_co void fib_sync_mtu(struct net_device *dev, u32 orig_mtu) { - unsigned int hash = fib_devindex_hashfn(dev->ifindex); - struct hlist_head *head = &fib_info_devhash[hash]; + struct hlist_head *head = fib_info_devhash_bucket(dev); struct fib_nh *nh; hlist_for_each_entry(nh, head, nh_hash) { @@ -1960,12 +1961,11 @@ void fib_sync_mtu(struct net_device *dev */ int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force) { - int ret = 0; - int scope = RT_SCOPE_NOWHERE; + struct hlist_head *head = fib_info_devhash_bucket(dev); struct fib_info *prev_fi = NULL; - unsigned int hash = fib_devindex_hashfn(dev->ifindex); - struct hlist_head *head = &fib_info_devhash[hash]; + int scope = RT_SCOPE_NOWHERE; struct fib_nh *nh; + int ret = 0; if (force) scope = -1; @@ -2110,7 +2110,6 @@ out: int fib_sync_up(struct net_device *dev, unsigned char nh_flags) { struct fib_info *prev_fi; - unsigned int hash; struct hlist_head *head; struct fib_nh *nh; int ret; @@ -2126,8 +2125,7 @@ int fib_sync_up(struct net_device *dev, } prev_fi = NULL; - hash = fib_devindex_hashfn(dev->ifindex); - head = &fib_info_devhash[hash]; + head = fib_info_devhash_bucket(dev); ret = 0; hlist_for_each_entry(nh, head, nh_hash) {