From patchwork Tue Jan 31 13:58:22 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: 13123033 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 827FBC38142 for ; Tue, 31 Jan 2023 14:00:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232419AbjAaOAC (ORCPT ); Tue, 31 Jan 2023 09:00:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229590AbjAaOAA (ORCPT ); Tue, 31 Jan 2023 09:00:00 -0500 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01olkn2091.outbound.protection.outlook.com [40.92.107.91]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 777775FE7; Tue, 31 Jan 2023 05:59:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EoFdaADBYH9y4upGBGbV7YbMRyZ4he6KgmZoj1IFvfwa/mLMktmueBAisJmSh4BagGV5WE2SCcRE5X9445DAU7NtN15rI/4mf5FYCvo/34ptofGPlqROkPDkQQHZXHmvU5oUN7YT21miAXibYPnagBmtGKBOmx3ngxsW7ZKAhPNuEPce8QVFAiQgRV6acn8SerW21Kfjh1Ai2LP7gVrlprqqAkpRcPvaVtt1kFOnUJLXS/e3PMf7dQlT/vkQEkFZqEqoEA89UbjxxNANCWKjE04NSn4qV2rGfLlC4bRQRwT/v/kLzNcK04mik9AjSoUgINiEbTqySoHyPOTRG2SyJw== 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=jZ6rQ0LFqCsRAwpjPQN3PskuqAMJihfP438gksUBkpA=; b=YRvTYdGOu4rrHw/XGtVX+Bwyuk5X0denBxMRwvY9FuIAU9bkEEW+jHwHQju/Nn0kftJSec+PNsVaWcW4J/JFan/Z73LFXc0g5uiH9LVh3blsoGz4XQhdlq7DkzgqxiDucB6wdsz52DB1aGZp5rlTqdMwbV0zHbvJ2MTzsXPECK7Yq46kWDAgZr7dJrMgDZvmk4YslPYbeMWzj/TBJOPdQ+mm5dREoAhbrfj9HKDtStj/YjUwjPsW3x3io1Nd8ZZyXiOBDuOpCyX++Yyf4QTNIAw7csj1hvLu2z5smggjoCAaJ8ja8+AavgX9eFiWq/zs/b+B7e1eexnCTKO1kY93/w== 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=jZ6rQ0LFqCsRAwpjPQN3PskuqAMJihfP438gksUBkpA=; b=mECtNeqqHrjlH115Iz1aVKahZ2kwWXFPs5NBVsRdpafAMnS6TOeYch7YBN8+jxuFjKAO/m4udHBmJMGBPX09rk6Ec2FYsKTdebJ9sWSwdO26hSGaPhnfd6y57AjgiC/E0t3hsrpzPSZl1jbEzNh3quO84vSj6h9atsQGR67XzXNI/WiPmNqkEwqa2g5b3bYDSvnuhLSXIzwoYBuuaQrs+5XsNimoQ6Twy0hqh3F5c/qvJ5u1dvm/UJggWMB0uY0j/jcPvxSbVRly/Kr4eZJgOWbqEBW4LBXJ5CEONfJT4yyw+iQpRuYMUHF8xV2b6hQ8Bm9wUBPyriyKw/6sBRFL/Q== Received: from OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:19b::11) by OSZP286MB2269.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:18a::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.38; Tue, 31 Jan 2023 13:59:56 +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; Tue, 31 Jan 2023 13:59:56 +0000 From: taoyuan_eddy@hotmail.com To: dev@openvswitch.org Cc: eddytaoyuan , Pravin B Shelar , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] net: openvswitch: reduce cpu_used_mask memory consumption Date: Tue, 31 Jan 2023 21:58:22 +0800 Message-ID: X-Mailer: git-send-email 2.27.0 X-TMN: [K6qdMsHffJ9eaoHYt73mUuTZb1s8awWLBRRiDU8qkbleIigINddMNE3QQ6M7Z0Ku] X-ClientProxiedBy: SI2P153CA0011.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::14) To OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:19b::11) X-Microsoft-Original-Message-ID: <20230131135823.390249-1-taoyuan_eddy@hotmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: OS3P286MB2295:EE_|OSZP286MB2269:EE_ X-MS-Office365-Filtering-Correlation-Id: a7bb523c-e1ec-4443-cb0d-08db03936f26 X-MS-Exchange-SLBlob-MailProps: q6Zzr5Fg03Ht49wMEz40i2N/NnYAXb+epVakThJqCTMDF64i4WfcTmd9HHRWqC/slNGnnvaNizRJ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0ahhUM9grFgr+0iC0+nJBuLFdweSP3TaF8upRwPxS5LKZXwdrVoSr9u7xBARmhD7rdxY+ofzh9N3Cw+dVZjqUNtu4r8k2m4oj5U75L9LZTyPZ+p9sGW5sk/2wNWDjAL5qudB6wJ6VzQDbLFqmfWFeJ/zt8nufHoPli+Um3t3wY56YwX0qoQm27EQBOgslqn6m7Cgpdoka4GMCmReUCWZgADHcHPqmbroQMeAFd+chzbE0KaVQORtySun6+a2aqKgimDZJtoeb5wughL79jQJ8yzNDlDYmfFLoz9YK3eTKjOWb3sFDU+qNcIewRcVS8dCluIvtb13COKuG5mhZjfHY7zxZC3XdOAlAbQ7vSEiMm2hKq+1m693ZViLCkCrrkVjhp24CCCeSm3FZk/977NxlhkdbeiuAEgFzGMDAyUtcVyZxbhcJ4Oosv6eIwYGS+N0UXrvF6MsF1ZJRvXnr/SdHkmuw+43FGBYBU+/yS+3io7OqNClipfXC3h+W9BixNJcdSdGxOPP54gIqTl2tnI7b92P9ofdt4XIE+xo7k4iXZiUHbClEBYmJyPYHfnBNmMi06kXxYqw== X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-05f45.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: a7bb523c-e1ec-4443-cb0d-08db03936f26 X-MS-Exchange-CrossTenant-AuthSource: OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jan 2023 13:59:56.6695 (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: OSZP286MB2269 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: eddytaoyuan struct cpumask cpu_used_mask is directly embedded in struct sw_flow however, its size is hardcoded to CONFIG_NR_CPUS bits, which can be as large as 8192 by default, it cost memory and slows down ovs_flow_alloc, this fix used actual CPU number instead Signed-off-by: eddytaoyuan --- net/openvswitch/flow.c | 6 +++--- net/openvswitch/flow.h | 2 +- net/openvswitch/flow_table.c | 25 ++++++++++++++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/net/openvswitch/flow.c b/net/openvswitch/flow.c index e20d1a973417..06345cd8c777 100644 --- a/net/openvswitch/flow.c +++ b/net/openvswitch/flow.c @@ -107,7 +107,7 @@ 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 +135,7 @@ 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 +159,7 @@ 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..c0fdff73272f 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]); @@ -1194,9 +1195,27 @@ int ovs_flow_init(void) BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); + /* + * Simply including 'struct cpumask' in 'struct sw_flow' + * consumes memory unnecessarily large. + * The reason is that compilation option CONFIG_NR_CPUS(default 8192) + * decides the value of NR_CPUS, which in turn decides size of + * 'struct cpumask', which means 1024 bytes are needed for the cpumask + * It affects ovs_flow_alloc performance as well as memory footprint. + * We should use actual CPU count instead of hardcoded value. + * + * To address this, 'cpu_used_mask' is redefined to pointer + * and append a cpumask_size() after 'stat' to hold the actual memory + * of struct cpumask + * + * cpumask APIs like cpumask_next and cpumask_set_cpu have been defined + * to never access bits beyond cpu count by design, thus above change is + * safe even though the actual memory provided is smaller than + * sizeof(struct cpumask) + */ 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;