From patchwork Wed Feb 1 13:24:39 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: 13124273 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 D9E8CC05027 for ; Wed, 1 Feb 2023 13:25:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232300AbjBANZK (ORCPT ); Wed, 1 Feb 2023 08:25:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232292AbjBANZJ (ORCPT ); Wed, 1 Feb 2023 08:25:09 -0500 Received: from APC01-SG2-obe.outbound.protection.outlook.com (mail-sgaapc01olkn2075.outbound.protection.outlook.com [40.92.53.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39DC835A3; Wed, 1 Feb 2023 05:25:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g+ihNt6InrXocKZz+7iLUPhxqH++1QmN4up6PxlSuyYpmEvkf3XQwZB4LDxNicvMWpcaagWG+rDXu7W9aYyVhlbL+xBoLsJ3CDmvomvZ8YTAVFrtTol5/Vh4BRagxbiwNZm+jm9RDfzT8L2LQtkYGIwe9uhxhW9r2ffv3+Az8r5GzYBfTO3Avei8Z+UznuzRL8K3S7Gi2SNeeWkHsnSWIERyJSjlifSdsYsdV1UcKy12WIuDIN+WpjZH508XHJpFmppyZZGPCKfS8tB4RqbyaFjswnYbByfh8W1gyb1vEFrxeUtLqCv4Wp36N/n7OMUTLLrSrcCmAfJcE83kPp0t1A== 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=5dqeX7eeMtwcIhbTKCp+MHOTinnUmHuAu7G2bJktK7s=; b=W57K21kwbS7MTKF9JlQUi/RlwUYsFt3/JE0Ai+rhE+T1KP264cOKtYxY6CtrpT8dx3FfqXDpUBZ+VR7G7tS4+M+Md7ApHqntvoSc+VPPxMn7F0ld4o2Dq/tFpM8kAwdV0oOtHRC5j4acLSKe3S4ec1L33r6kpcfFCyE18/3CT8rUkBsfuVEn/CV3AGZg/L4CvNkUE/+1XxuJfFQWNqAZOLIbRhnaAmhpFlM68EOsqKAW0gy4BgOhbTZui+sQV4XlfTMif0GIcH1d0jKQeT477K0jSvp5zB/5INA5K1lA0yDaxnE/ttazhMPVmDzBBepEoYYbAhoPKe8+wUJFcaBo0Q== 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=5dqeX7eeMtwcIhbTKCp+MHOTinnUmHuAu7G2bJktK7s=; b=bUWqrHfcqX81EiKuJ79zxtH/TRcrJem6CBDwUnViVionDzjgJeHxlTco4IK/+REN+hu2HYgQBcwWUILyoQutdikjQFydBRYPJwJ7uZmYsgfHEqUDEyB3Mkp1mhJDwEOy/aNEzp65Q8aquJmzsIkuO4+a9GlThkugL7UeFMIFcSFUi9qTC8+YGV/Hblnx17Li9Wvr0NLV8yiyRjHw0novt1fX0DC8SbHHNt9Lng3EXFTG5ZYzrE5amO+kmIwZ87xU4K7XSSxYCeZKKZpGPsPIaqodlKLDr5WwWJsZ1vg7mUO9MRJZr3/Use6AP4lOoXcspW0DFH5hMBscfuuSpljJmA== Received: from OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:19b::11) by OSZP286MB2240.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:18d::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.25; Wed, 1 Feb 2023 13:25:04 +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; Wed, 1 Feb 2023 13:25:04 +0000 From: taoyuan_eddy@hotmail.com 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 v2 1/1] net: openvswitch: reduce cpu_used_mask memory Date: Wed, 1 Feb 2023 21:24:39 +0800 Message-ID: X-Mailer: git-send-email 2.27.0 X-TMN: [sUPTXEReOI2Zv76tiMz6U9PNd/i0p1P0vOthcSmdaXGQgc/ubcmrAvaCcH7uEjn5] X-ClientProxiedBy: SI2PR01CA0052.apcprd01.prod.exchangelabs.com (2603:1096:4:193::11) To OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:19b::11) X-Microsoft-Original-Message-ID: <20230201132439.709236-1-taoyuan_eddy@hotmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: OS3P286MB2295:EE_|OSZP286MB2240:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d607598-99fc-4a9f-fa7e-08db0457ba8e X-MS-Exchange-SLBlob-MailProps: q3cIJLDN2c+w+CziC9pPzGUhn8kCPPeB/KSK5v8pj7EaaxuGM6Oo1nIOTV8oCtv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1qYQRYitFTcgFoBshtd/FqPDQHPMqE+Ou8fv1mYmF5lxAGMpHqIs7NgHrW8Rai3rvJM2pvtwrbwGR5gklPiQq8kTRwIMcet5+x4gn+bDBvV62bVoY/vx5gUYwe1+UacX2ymFs9SdvzU7BdXcAvvPTb8pjrq7VvRit81jONE6wQueRRfPytpgJ8sdd6DWJo+GmQi4rTicGwQLh++W/Asu7chMn5jmGNR8HXzQZCUbKsRgqij93CJxucFHB/jjcQOPw59rSLMyhU9s5EKgqUhQlGFDPSFb5OOVruIMwJiOkr++G2pTR2UVu9AS3Sqw+zcMA6G+8pFAM6o5YPpBVMTvepH1+39yiqDlnR5r9YDMjuHwioMoYvkxPUXbOCCs46hcG8NiPa7in4Oo0IiRuhhpnWK/ZGrUmtoMc4nTDywf51BHwyQ4TAzGJfcRhMNQVrmcIruWkPzsKqIuPHntubIe+EelsGEFAn0NC2qu5ZGDN9GxHvgEt1bFiuOglOs9KD7h7zzKzFbw7Eb6LZrJfrQkkDrErSOqpf3G4Xj0XFVpZ1jSfYpq7b80fwucJzHqZbuB/tBPjFsAEuuRSx5lnbMNe4VAGFzM8svkZWWruxYAwsNA== X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-05f45.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 9d607598-99fc-4a9f-fa7e-08db0457ba8e X-MS-Exchange-CrossTenant-AuthSource: OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Feb 2023 13:25:04.4780 (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: OSZP286MB2240 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Eddy Tao 'struct cpumask cpu_used_mask' is embedded in struct sw_flow. However, its size is hardcoded to CONFIG_NR_CPUS bits, which can be 8192 by default, it costs memory and slows down ovs_flow_alloc. This fix uses actual CPU number instead Signed-off-by: Eddy Tao --- net/openvswitch/flow.c | 6 +++--- net/openvswitch/flow.h | 2 +- net/openvswitch/flow_table.c | 24 +++++++++++++++++++++--- 3 files changed, 25 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..63c95c9a814d 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,26 @@ 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 embedding 'struct cpumask' in 'struct sw_flow' + * consumes memory unnecessarily large. Cpumask is an bitmap + * of CONFIG_NR_CPUS bits, which is hardcoded in .config + * and default value can be 8192, in this case is 1024 bytes. + * It drops ovs_flow_alloc performance and cost memory. + * 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 memory + * for struct cpumask. + * + * cpumask APIs like cpumask_next and cpumask_set_cpu are defined + * to never access bits beyond cpu count, as such 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;