From patchwork Wed Feb 1 09:21:42 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: 13123991 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 8CFF2C05027 for ; Wed, 1 Feb 2023 09:22:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231245AbjBAJWZ (ORCPT ); Wed, 1 Feb 2023 04:22:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229770AbjBAJWY (ORCPT ); Wed, 1 Feb 2023 04:22:24 -0500 Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01olkn2046.outbound.protection.outlook.com [40.92.52.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 046BC3B64B; Wed, 1 Feb 2023 01:22:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YlJlzrQNJJ5iUA7+bY9XHVdr2nhcHYmL8DQLupi/hVesUa7jj94OZzhx5/ekmYRiXA9uou6ih4lkDispe8OOt5yKxU21OGuNCi9uPxmUk79wfntiSv1kBxHeIeSaPzdvZCKTfFt4sdnGnndtvyCKPefOcVCgIkfx8//K9AZA6GaHr49TrMEiEMydOsc2TlY9Y9pKC14vGGknMa67yizJiAOHQpglS17akAzvbuIQqJsuW4AsEYxTQxTPnDjfZUK1l8ir0ocHBlA2mmh7VjMIHsY57w9o7aZwkwQ1KAqMjKriufOHTUse36qwSCYCZGdZAPEUaBj2y/6YgitRXGxA/w== 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=oYi9vaWN1R0sh5LTc2JbjoBekKKGZCXg5+8t1+6VzkyiGA6ZgK8XZcBJ529GLpKeJIFmxgT8Rroxj0AEYcCOPhXoawMljaB58y9MM5LavMeZdUxtBnKSqyeAhChEs0ggJ+BE4AyfrxL5+V3WkHu9ZF8azj/mM6vauAs29jKlAaoK7KpAiofhmFvRkqLFEHX/WTCKyJUp1k5C3/g1+8frwDZ4bKhvmK5wRzS9WK/jmrBiY5hOAXSkRDo7qJzrTQ68hbQqPgijZG9Mtt6Cn8RrSKoBio9/nRevnDeIWH7Gz6+BEqA5Shk39LdmTfaPER3OJRdDXfN+eBjCnUDIHKL6Rw== 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=MTky5UxClZdOv/rcqVKHUrB3OOoPvJtCerdIYe8sj1R/a5fg2BQlq2lfgeVctuPNsY/3KqJiuyC5wfXgyxrVxdC2bQmxz1wKw3kxqOAu1Lh8B8iCNrDu1AZo9HY7B2P1ttzMHBFcDl91Nus0qpBYOrFxExeZZStg1aUQVe28TxA7ZGeg3f/+8vM462sB8PfxjjFAh9ITEAfdwURBW/1TAhlv0d1wWUckB3/cHG7f8K3okqZlnhVyJgc7ThO/77Y4Q3ZGZtbOPGGrvqsxOZVmAWYIV1IBg8OIoB4sI1sawZbAuYZHeB+y2htubkL0e3MLscZLtVm+P18hVv7D1e5HDg== Received: from OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:19b::11) by TYVP286MB3168.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:2ad::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.36; Wed, 1 Feb 2023 09:22:19 +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 09:22:19 +0000 From: taoyuan_eddy@hotmail.com To: linux-kernel@vger.kernel.org Cc: eddytaoyuan , Pravin B Shelar , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, dev@openvswitch.org Subject: [PATCH] net: openvswitch: reduce cpu_used_mask memory consumption Date: Wed, 1 Feb 2023 17:21:42 +0800 Message-ID: X-Mailer: git-send-email 2.27.0 X-TMN: [R/iPOsx/Qlz3d2tj+2OIEx5sSk81ZjPzzVjqdvHv03RLCxaW8EzqpkKtbxMaBoW3] X-ClientProxiedBy: PS2PR03CA0009.apcprd03.prod.outlook.com (2603:1096:300:5b::21) To OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:19b::11) X-Microsoft-Original-Message-ID: <20230201092143.621334-1-taoyuan_eddy@hotmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: OS3P286MB2295:EE_|TYVP286MB3168:EE_ X-MS-Office365-Filtering-Correlation-Id: c3aea883-6109-4f93-6b39-08db0435d160 X-MS-Exchange-SLBlob-MailProps: q3cIJLDN2c+w+CziC9pPzN+leFQPe4aiwxsgVG8JqV+LCOdr9O29Epb4hqw5MDHW6wP1LJvE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gby+XYZ4hK5vId/uMS/cJnsCZWLQzwx79sOpMG/IescaLwqW++VCUBoVSlJEsY8nfZbHNwhPFQDk8LpnNGXyAtZL+HjYdBESC9sPoDDKRD6MeZQ8kUFsODD4QGLd9pC04priI2YxV4ZhuctpMzayFljUvvfelwc1Gd0o4yPlPDxoxS9kH55R8+yXnmqtobe6d4m2d0M7wUm08h6yOwRU/FjAR0CNDlDkhXKcoRlBUPHmmwr9kTLbflA5zVr7ocOn7zD8F3J2PA0LjddlxTbx16qUFmkJD1JfSFGLDSmb0HwulE0fymORlyMwB+OBroKMn8BY8qrW1zl3YDWyTCiH+1J+tASmxY7Q1B2PdvsJoz/9jSuKp9DW6NvfJTHpy61DPoWTUwbFIyhFHktQv11PlSaWgMqF3vNgCj830zc6BS8J03C5k/dT8L2riwsRefufXVnenAcDHZL1OF+RmAMul8R6oK35oWYydVQ1tQWOyKGRM40b/+6OLp7JypxV+3e2m7bJJQMSlC8l/hgCMtJNQmjrzIDidlj1ydFh8AXA/OerfJUuOBz2UlOPMJ4Oujb+JgKvWKHf31jIG+Dl0SX+YNzI0y7KKQ== X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-05f45.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: c3aea883-6109-4f93-6b39-08db0435d160 X-MS-Exchange-CrossTenant-AuthSource: OS3P286MB2295.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Feb 2023 09:22:19.8872 (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: TYVP286MB3168 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;