From patchwork Fri Feb 3 08:52:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?57yYIOmZtg==?= X-Patchwork-Id: 13127190 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0333C636D6 for ; Fri, 3 Feb 2023 08:53:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232471AbjBCIxb (ORCPT ); Fri, 3 Feb 2023 03:53:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231542AbjBCIx3 (ORCPT ); Fri, 3 Feb 2023 03:53:29 -0500 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01olkn2025.outbound.protection.outlook.com [40.92.107.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E018A2C66A; Fri, 3 Feb 2023 00:53:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VOmdN+r+SWk9lAjJ98rk7s4W1zntm89V4RbiiB+GT0HeV5E0+Q1B0F/YawApurc6a1N91UEsw2FW2ceuY45w3pigR2Y3Rv7LinXnjltUD4LfA/lygqmWOpdyTMG+69iYoHPK8uPKC1UfrQ2oQB26lr9cbuLC2mY3XnEUGnrzd3be/p4CdtjPuViKjokbRm9hhDQg7gfuRTZwszozTA5aXeU93Hqwu69+YfdIPAdDPdDwdpS25D/txllYqzenOhAemZGs5+1w5c7jFExe2a7CpfcyAb9cODK4t2zjuQBYvDrsJHWFaimEcEQdxxjJUgYc28bhxLce8ScBcOtX8dH8Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=KtqbQdLv493zXb9EZS6kdPrDzcdP21q6asQPwvUEUOo=; b=DqxGROjDttct7MCrb65BSFUHJ4t7eJ5BvbUYQ0iwbq0e1cycGqRdqTUJdEd8CK2pR6WEb0GIXA+jsxUxKrMWeCkOwMnFmzw2B66OCvDzNWAdJmrGVAGBUc25Cca8mFwt0WZ2sUEULG/q/yOWqYm5swklFeUuabZy6LCVMm61r05BmQdoLdx5eMQS2e4epJpguD+l5AJaOlo9vA24xIyPsZ119LJI03SIEm1XzKAjV/XUG/yb74bowgI2qwB/wogrRhBPBQV91d8ReEnNK2J5wujkSN7UBEFUMMRqBaLaN3Gh96ORbi4ESzF4FWdmz/adxe2XoRHyDn7TpmcWWOPZuw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KtqbQdLv493zXb9EZS6kdPrDzcdP21q6asQPwvUEUOo=; b=UsdR7HwpPE7Nk4zXmaRfvCm3mXxa0HCyeGMd5+foPLIp4AQwrr6bM6XRSVjGQajz8t7IOIyQONkXOZQjDhpVQvWVdLLjvfweSgfUihndmoI0PJ6TF3Rl/L9I0i9E5JjTeHqz+K3OqsaXITrusfGZhVlTFohC03VR7CLBpG2UXH97Isqbw41ZAnjoAIt8vBgreyMvY0DlyMANLIWp31sWavTaUUCHTJLcN/u+ItGmnlNYOEOD+/5VQqihzbC7qroEExzsqORL1TpPX5iOUQKZb9h1nUNIkdi7ROOhbzjgUR/ygFoudvPWggHxojB9ifZVwT2livWl918SFoeH1Ubl3g== Received: from OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:19b::11) by OS3P286MB1919.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:171::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.36; Fri, 3 Feb 2023 08:53:24 +0000 Received: from OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM ([fe80::9a1d:12f5:126c:9068]) by OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM ([fe80::9a1d:12f5:126c:9068%8]) with mapi id 15.20.6064.022; Fri, 3 Feb 2023 08:53:23 +0000 From: Eddy Tao To: netdev@vger.kernel.org Cc: Eddy Tao , Pravin B Shelar , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , dev@openvswitch.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v5 1/1] net:openvswitch:reduce cpu_used_mask memory Date: Fri, 3 Feb 2023 16:52:56 +0800 Message-ID: X-Mailer: git-send-email 2.27.0 X-TMN: [hg8SPq3bRRjXY7Nnh6hE4zaGMS1yovz/] X-ClientProxiedBy: SG2P153CA0002.APCP153.PROD.OUTLOOK.COM (2603:1096::12) To OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:19b::11) X-Microsoft-Original-Message-ID: <20230203085257.254240-1-taoyuan_eddy@hotmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: OS3P286MB2295:EE_|OS3P286MB1919:EE_ X-MS-Office365-Filtering-Correlation-Id: 0fb3c58e-bd35-4fae-4b23-08db05c41b82 X-MS-Exchange-SLBlob-MailProps: Op6Ru+d2ciGZxW9SRnKHFeRtQv7uON2YAiNRMPEO9TWlGk+3SWraKATLMuVVUBM54pIykylzJYogXOPdlQ6xlrVI4+cPzxvjZ68UFmuU30tJexYliVvP1hjAj6nd+H9e1BaXj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lrc+dsMJZ/OTO5c3ANQvM+BV1Bizukz5eQ6nBfRtHPMobkeYUk5jlgEpAwe1moThTTBZrl7kS50n5sdJ08WUyK36NuG6ykTJaggqDUyuEEAln0xHReQSLBF5g/pbnhSkz9LyaF67IJG83fLq6aOwMALAiflT2QrZ/cz855AwMJ7GR6JAZZtmsOcwoXLHhAAha2QhSzdmjLfs59i02tECIbaWWsu9TRAw+uVTXqo3D359mXtodjaaRJFWcKeqRnnP8lHOniF5KhIuNuX4hpLQcfNWWQZCrS3QA01mGYqeesfemo9Pe1mg6s4UnRG+XjPHeecsI5E3UPxOYUISr778Mp/0/BqNmFt8stbzAGJgP6HFByb0N/eCnTsQlrSV39RROA1GfCRB0BcuQ9pDCBrbhn6/kLaI9pNf4WElcAy5mPsq8aivM08oz8Au2DhgoPgsAFBE0H57a9XrEa9ayQBd1gEhKBqLd0H63F8JyTENKIwR1wR5lI+MRtYgkF2qFQAG3ie0lhGJJuVltstI= X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-05f45.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 0fb3c58e-bd35-4fae-4b23-08db05c41b82 X-MS-Exchange-CrossTenant-AuthSource: OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Feb 2023 08:53:23.9590 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: OS3P286MB1919 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Use actual CPU number instead of hardcoded value to decide the size of 'cpu_used_mask' in 'struct sw_flow'. Below is the reason. 'struct cpumask cpu_used_mask' is embedded in struct sw_flow. Its size is hardcoded to CONFIG_NR_CPUS bits, which can be 8192 by default, it costs memory and slows down ovs_flow_alloc To address this, redefine cpu_used_mask to pointer append cpumask_size() bytes after 'stat' to hold cpumask cpumask APIs like cpumask_next and cpumask_set_cpu never access bits beyond cpu count, cpumask_size() bytes of memory is enough Signed-off-by: Eddy Tao --- net/openvswitch/flow.c | 9 ++++++--- net/openvswitch/flow.h | 2 +- net/openvswitch/flow_table.c | 8 +++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/net/openvswitch/flow.c b/net/openvswitch/flow.c index e20d1a973417..a56483eda015 100644 --- a/net/openvswitch/flow.c +++ b/net/openvswitch/flow.c @@ -107,7 +107,8 @@ void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags, rcu_assign_pointer(flow->stats[cpu], new_stats); - cpumask_set_cpu(cpu, &flow->cpu_used_mask); + cpumask_set_cpu(cpu, + flow->cpu_used_mask); goto unlock; } } @@ -135,7 +136,8 @@ void ovs_flow_stats_get(const struct sw_flow *flow, memset(ovs_stats, 0, sizeof(*ovs_stats)); /* We open code this to make sure cpu 0 is always considered */ - for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask)) { + for (cpu = 0; cpu < nr_cpu_ids; + cpu = cpumask_next(cpu, flow->cpu_used_mask)) { struct sw_flow_stats *stats = rcu_dereference_ovsl(flow->stats[cpu]); if (stats) { @@ -159,7 +161,8 @@ void ovs_flow_stats_clear(struct sw_flow *flow) int cpu; /* We open code this to make sure cpu 0 is always considered */ - for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask)) { + for (cpu = 0; cpu < nr_cpu_ids; + cpu = cpumask_next(cpu, flow->cpu_used_mask)) { struct sw_flow_stats *stats = ovsl_dereference(flow->stats[cpu]); if (stats) { diff --git a/net/openvswitch/flow.h b/net/openvswitch/flow.h index 073ab73ffeaa..b5711aff6e76 100644 --- a/net/openvswitch/flow.h +++ b/net/openvswitch/flow.h @@ -229,7 +229,7 @@ struct sw_flow { */ struct sw_flow_key key; struct sw_flow_id id; - struct cpumask cpu_used_mask; + struct cpumask *cpu_used_mask; struct sw_flow_mask *mask; struct sw_flow_actions __rcu *sf_acts; struct sw_flow_stats __rcu *stats[]; /* One for each CPU. First one diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index 0a0e4c283f02..dc6a174c3194 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -87,11 +87,12 @@ struct sw_flow *ovs_flow_alloc(void) if (!stats) goto err; + flow->cpu_used_mask = (struct cpumask *)&flow->stats[nr_cpu_ids]; spin_lock_init(&stats->lock); RCU_INIT_POINTER(flow->stats[0], stats); - cpumask_set_cpu(0, &flow->cpu_used_mask); + cpumask_set_cpu(0, flow->cpu_used_mask); return flow; err: @@ -115,7 +116,7 @@ static void flow_free(struct sw_flow *flow) flow->sf_acts); /* We open code this to make sure cpu 0 is always considered */ for (cpu = 0; cpu < nr_cpu_ids; - cpu = cpumask_next(cpu, &flow->cpu_used_mask)) { + cpu = cpumask_next(cpu, flow->cpu_used_mask)) { if (flow->stats[cpu]) kmem_cache_free(flow_stats_cache, (struct sw_flow_stats __force *)flow->stats[cpu]); @@ -1196,7 +1197,8 @@ int ovs_flow_init(void) flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) + (nr_cpu_ids - * sizeof(struct sw_flow_stats *)), + * sizeof(struct sw_flow_stats *)) + + cpumask_size(), 0, 0, NULL); if (flow_cache == NULL) return -ENOMEM;