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 45CFBC05027 for ; Thu, 2 Feb 2023 11:07:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231364AbjBBLHb (ORCPT ); Thu, 2 Feb 2023 06:07:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229761AbjBBLH3 (ORCPT ); Thu, 2 Feb 2023 06:07:29 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5A8B6B009 for ; Thu, 2 Feb 2023 03:06:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675335949; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9M4NDpJqrMaRwiYT/VeraTjAir6oKB3t+zIsWAMAXf8=; b=dpR3HoAcTyrYwWJNs4ZoX75HEjQuSbeNJ01qQwHuT/ICJce2VTirDcxAoL0lSbBwj1Cz07 IRITpoil6RzfcQWPAXSH16yIkinyGuZKDoWEz+GT4VvPHiNA0g+SnqwXkt+U36+q6n9b0b y+8edX8TfMxNsHvxxAltwo2h7ni/3wc= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-663-EPs_h2FtOuu5M2lKcTJGfQ-1; Thu, 02 Feb 2023 06:05:47 -0500 X-MC-Unique: EPs_h2FtOuu5M2lKcTJGfQ-1 Received: by mail-ej1-f69.google.com with SMTP id sa8-20020a170906eda800b0087875c99e6bso1301433ejb.22 for ; Thu, 02 Feb 2023 03:05:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9M4NDpJqrMaRwiYT/VeraTjAir6oKB3t+zIsWAMAXf8=; b=IIVdhcI3Mh+39o7uLjD/T12dJETNkEIOzrsNt243qrvV9jetK6Fi8nLY40nUQ98na0 Z/8GhLRF8xqa36yeNxXttOeJUbITJmYT79hzUz6hGzWgeIb0v7Gb7SC3FSD8hL1Dr7Lo IXGN+SohyeQGd5nGjBnOM58qsIlrQJzUwWRVFtq6Gs8jeIOU9PDEFMCV6nePk/u4gBg8 krq7SUzDQ/EzEu6gu7E/DHCPfHj4IS9X0yD1g422GacwiQcLJ6siMig7S3cH4ZGBadTo YSSxY91kX88Ubw5DjZcwfoynuHT9O/wnJQvUsn80xXZjhDMZhBVKpiX2B8+iDCKmhfu6 gZ2g== X-Gm-Message-State: AO0yUKXmTm9iR3WGJwjcRMsnRivgnD8N33LlH/btzJWioSgE16hVKFoq upJt+hc48aOw5+HvAy/Sq78QopObLCqHUxUy/qgxBeDRSYxvvfWd2RhVVjQ3BIGTrYmu31fBRCM /16BNs/4Y7w+kHtvUZQIXOlz3 X-Received: by 2002:a17:906:c0c3:b0:883:3c7d:944e with SMTP id bn3-20020a170906c0c300b008833c7d944emr5437055ejb.9.1675335946681; Thu, 02 Feb 2023 03:05:46 -0800 (PST) X-Google-Smtp-Source: AK7set+fc8EWwrGtQ1phRFqugSVTLUZhZ3tA5/uyYMtzGXASHL4KhTuAfYpcQFuLTIxGgBC/ci0TuQ== X-Received: by 2002:a17:906:c0c3:b0:883:3c7d:944e with SMTP id bn3-20020a170906c0c300b008833c7d944emr5437035ejb.9.1675335946424; Thu, 02 Feb 2023 03:05:46 -0800 (PST) Received: from [10.39.192.164] (5920ab7b.static.cust.trined.nl. [89.32.171.123]) by smtp.gmail.com with ESMTPSA id er12-20020a056402448c00b0049668426aa6sm10924515edb.24.2023.02.02.03.05.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Feb 2023 03:05:45 -0800 (PST) From: Eelco Chaudron To: Eddy Tao Cc: netdev@vger.kernel.org, dev@openvswitch.org, linux-kernel@vger.kernel.org, Eric Dumazet , Jakub Kicinski , Paolo Abeni , "David S. Miller" Subject: Re: [ovs-dev] [PATCH net-next v3 1/1] net:openvswitch:reduce cpu_used_mask memory Date: Thu, 02 Feb 2023 12:05:44 +0100 X-Mailer: MailMate (1.14r5939) Message-ID: <561547E5-D4C2-4FD6-9B25-100719D4D379@redhat.com> In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2 Feb 2023, at 11:32, Eddy Tao wrote: > 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 > as well as the iteration of bits in cpu_used_mask when handling > netlink message from ofproto I=E2=80=99m trying to understand how this will decrease memory usage. The= size of the flow_cache stayed the same (actually it=E2=80=99s large due = to the extra pointer). Also do not understand why the iteration is less, as the mask is initiali= zed the same. Cheers, Eelco > 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 | 8 +++++--- > net/openvswitch/flow.h | 2 +- > net/openvswitch/flow_table.c | 8 +++++--- > 3 files changed, 11 insertions(+), 7 deletions(-) > > diff --git a/net/openvswitch/flow.c b/net/openvswitch/flow.c > index e20d1a973417..0109a5f86f6a 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,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 =3D 0; cpu < nr_cpu_ids; cpu =3D cpumask_next(cpu, &flow->cp= u_used_mask)) { > + for (cpu =3D 0; cpu < nr_cpu_ids; > + cpu =3D cpumask_next(cpu, flow->cpu_used_mask)) { > struct sw_flow_stats *stats =3D rcu_dereference_ovsl(flow->stats[cpu= ]); > > if (stats) { > @@ -159,7 +160,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 =3D 0; cpu < nr_cpu_ids; cpu =3D cpumask_next(cpu, &flow->cp= u_used_mask)) { > + for (cpu =3D 0; cpu < nr_cpu_ids; > + cpu =3D cpumask_next(cpu, flow->cpu_used_mask)) { > struct sw_flow_stats *stats =3D 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 =3D (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 =3D 0; cpu < nr_cpu_ids; > - cpu =3D cpumask_next(cpu, &flow->cpu_used_mask)) { > + cpu =3D 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 =3D 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 =3D=3D NULL) > return -ENOMEM; > -- = > 2.27.0 > > _______________________________________________ > dev mailing list > dev@openvswitch.org > https://mail.openvswitch.org/mailman/listinfo/ovs-dev