Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp304396imm; Tue, 19 Jun 2018 21:45:08 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKL/oJl3kCf1louIzGfovr/5tv/61Ujw08GpRoZT3KsfasIC+BIx9YGoWYwUW0I0UuGg1d+ X-Received: by 2002:a63:5f0b:: with SMTP id t11-v6mr17156146pgb.79.1529469908095; Tue, 19 Jun 2018 21:45:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529469908; cv=none; d=google.com; s=arc-20160816; b=WV0LWdUgsXeKDtn1hcLIdcD4NjjeP5ViSfQv/KnEBbNQzz0ABbjf4ppIwpAwfsuuXN DkuO1yM1Y2gytIhqUPxfgUZMLWxa83EevO8Hvi6UQZO+qCj9j6NBEALRROv/1lbgudUu AKtfhkojcZMqoAH07UHVsvK1IMZyGiD0AxFIxsumKJRovFoxHV2d8W0+lCXOtBjaKlKF aWBItSc6ZcubsCMPL1yP8wk5ksnkGGPMvZptQLBbGxizTyUGOnHTB2iWIFPWy7oe2Igl mTRzySET2Irriyhu8/JjBaEbzMVHE8o56MQlb0TIZPXw+aPKDxOnprXCc2s+at63cvmj j9gQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=lj1IIzR1NND1YVyjhwcm6i6yNxGh0ELYIYI0eJwd758=; b=ECBYo6gmt6k5TK73ZnLabl1kbe5pYwbUamEWq4Y9/syH/axubTTuftD2RyX7HRms77 dVuTlF2dDB50xc81iCA0rQFw5jJVMvqvOgCcrqRl8jOO818EkhaPS84Ok63Fn/pRWXov d2XWfuRLHxntTlPC3xex5wYrUaH0u3pW9UZvASCprFRddxioD/7ZlE1yexoYJsV4HZYo xZW2axvaM2byNc9qah+2PDN8eXuKn4+djqtkGvsvysCrz7Brg60/xKzpAYYuT3K2kS2u jPkPs2Y0nuQycFQswJ6QaXTn56zdRFotKEJqtrXE3AqqXgcJHmRYXkUn6yQ1EhhDUD/a w9DQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=nfDhzB51; dkim=fail header.i=@chromium.org header.s=google header.b=lyZVGw1N; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n1-v6si1415692pld.188.2018.06.19.21.44.53; Tue, 19 Jun 2018 21:45:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=nfDhzB51; dkim=fail header.i=@chromium.org header.s=google header.b=lyZVGw1N; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932071AbeFTEn0 (ORCPT + 99 others); Wed, 20 Jun 2018 00:43:26 -0400 Received: from mail-yw0-f193.google.com ([209.85.161.193]:37304 "EHLO mail-yw0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751422AbeFTEnW (ORCPT ); Wed, 20 Jun 2018 00:43:22 -0400 Received: by mail-yw0-f193.google.com with SMTP id j134-v6so713579ywg.4 for ; Tue, 19 Jun 2018 21:43:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=lj1IIzR1NND1YVyjhwcm6i6yNxGh0ELYIYI0eJwd758=; b=nfDhzB51JGomowjMHvYoy8aPVHEAforyF9meTRdsfzWvQiOur6NJVMXXtPJ6O6PFwo Ylr7yYw5AmI7Ym8yM/D9RlDP6UyhoF/IQts8UH4Tg26bNKa+kAtn0exW6sDvdfEPFm9l Fon+q227vlCblf2U+ewQN6Y3cXL1P7ORLnYkYqX6Ds+L84gIZs5AjEH4XuTxQgFs16Sz 3j3xGbMsRj5dhp6CgQTRolwMPmPuFrtRWZH7WxKfKakMeSqSfdx6jYEBXTyBKlHZpSCE TEAGyapOjma1hsiH1Iz+64/ZlQjHLAyezyrPBUErjwC2wsm0thNISrhM2fNfoJ7oi8wc 8chQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=lj1IIzR1NND1YVyjhwcm6i6yNxGh0ELYIYI0eJwd758=; b=lyZVGw1N6Z90Vi5C+6N5+Q3dEwRALUBfhEAQM41ojaqCbIxxwkXipnSQwcJWLNl04I /6NqEtwfZu3n4DrnVDaQGyYIXC7rYFQAaIfh+ToWcovFwcoCVWTy5VyCORA7HsMSshEL DvGOhTLQChmeGIOm/AuyTx3ajmTe0Ba9EWxKE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=lj1IIzR1NND1YVyjhwcm6i6yNxGh0ELYIYI0eJwd758=; b=ChBk/U5EFYmIP40x9ZV2pxwEDBKx2QCfS+GKfIKNpzd35hjPS5emv6XnrvTNZQkzCY /EAmNupBeSjBOAXMpslT45eVnyXBfHGagXyHz2w4zHzbzXeHQ3lrfhmUg82GOxojvDt0 FCMW8R9/3qbtgQZ9+HvWSB12MJsscYVIN0bPDimqRI0Vj1HI99Q3V3bag/fUbIXXdE58 ryy3NQBz9TJdET+BmC3kgAEcWBgVUoI5XOaY/uc7ItKw3ZiU8qdKTZzY1FwzOxU0vnPz Q+ZQ1ozp4E3at0nqRKATxKSa64wjyMThpBXAMRsDbxD4jiTswhzR+oX8qyp2xVeqmawR XCHg== X-Gm-Message-State: APt69E04e8snSUGlk8JOxzDWcpL2+QcJ/HWyP82zJCce6hdr3NdIhWvk v2JrZpJIARGe2zL+faUCvfQTJCvxieqJdzk/qdwqFg== X-Received: by 2002:a81:4a05:: with SMTP id x5-v6mr9783739ywa.116.1529469801916; Tue, 19 Jun 2018 21:43:21 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:d6c5:0:0:0:0:0 with HTTP; Tue, 19 Jun 2018 21:43:20 -0700 (PDT) In-Reply-To: <1525706596-13601-1-git-send-email-s.mesoraca16@gmail.com> References: <1525706596-13601-1-git-send-email-s.mesoraca16@gmail.com> From: Kees Cook Date: Tue, 19 Jun 2018 21:43:20 -0700 X-Google-Sender-Auth: HGyiSGuOyaXd7UqjzM_gNBEsmwg Message-ID: Subject: Re: [PATCH v2] net: dsa: drop some VLAs in switch.c To: Salvatore Mesoraca Cc: Andrew Lunn , LKML , Kernel Hardening , Network Development , "David S. Miller" , Florian Fainelli , Vivien Didelot , David Laight Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 7, 2018 at 8:23 AM, Salvatore Mesoraca wrote: > We avoid 2 VLAs by using a pre-allocated field in dsa_switch. > We also try to avoid dynamic allocation whenever possible. > > Link: http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com > Link: http://lkml.kernel.org/r/20180505185145.GB32630@lunn.ch > > Signed-off-by: Salvatore Mesoraca Friendly ping. What's needed to take this into the tree? It looks like all the issues in v1 were addressed here. Thanks! -Kees > --- > include/net/dsa.h | 3 +++ > net/dsa/dsa2.c | 14 ++++++++++++++ > net/dsa/switch.c | 22 ++++++++++------------ > 3 files changed, 27 insertions(+), 12 deletions(-) > > diff --git a/include/net/dsa.h b/include/net/dsa.h > index 60fb4ec..576791d 100644 > --- a/include/net/dsa.h > +++ b/include/net/dsa.h > @@ -256,6 +256,9 @@ struct dsa_switch { > /* Number of switch port queues */ > unsigned int num_tx_queues; > > + unsigned long *bitmap; > + unsigned long _bitmap; > + > /* Dynamically allocated ports, keep last */ > size_t num_ports; > struct dsa_port ports[]; > diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c > index adf50fb..cebf35f0 100644 > --- a/net/dsa/dsa2.c > +++ b/net/dsa/dsa2.c > @@ -748,6 +748,20 @@ struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n) > if (!ds) > return NULL; > > + /* We avoid allocating memory outside dsa_switch > + * if it is not needed. > + */ > + if (n <= sizeof(ds->_bitmap) * 8) { > + ds->bitmap = &ds->_bitmap; > + } else { > + ds->bitmap = devm_kzalloc(dev, > + BITS_TO_LONGS(n) * > + sizeof(unsigned long), > + GFP_KERNEL); > + if (unlikely(!ds->bitmap)) > + return NULL; > + } > + > ds->dev = dev; > ds->num_ports = n; > > diff --git a/net/dsa/switch.c b/net/dsa/switch.c > index b935117..142b294 100644 > --- a/net/dsa/switch.c > +++ b/net/dsa/switch.c > @@ -136,21 +136,20 @@ static int dsa_switch_mdb_add(struct dsa_switch *ds, > { > const struct switchdev_obj_port_mdb *mdb = info->mdb; > struct switchdev_trans *trans = info->trans; > - DECLARE_BITMAP(group, ds->num_ports); > int port; > > /* Build a mask of Multicast group members */ > - bitmap_zero(group, ds->num_ports); > + bitmap_zero(ds->bitmap, ds->num_ports); > if (ds->index == info->sw_index) > - set_bit(info->port, group); > + set_bit(info->port, ds->bitmap); > for (port = 0; port < ds->num_ports; port++) > if (dsa_is_dsa_port(ds, port)) > - set_bit(port, group); > + set_bit(port, ds->bitmap); > > if (switchdev_trans_ph_prepare(trans)) > - return dsa_switch_mdb_prepare_bitmap(ds, mdb, group); > + return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap); > > - dsa_switch_mdb_add_bitmap(ds, mdb, group); > + dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap); > > return 0; > } > @@ -204,21 +203,20 @@ static int dsa_switch_vlan_add(struct dsa_switch *ds, > { > const struct switchdev_obj_port_vlan *vlan = info->vlan; > struct switchdev_trans *trans = info->trans; > - DECLARE_BITMAP(members, ds->num_ports); > int port; > > /* Build a mask of VLAN members */ > - bitmap_zero(members, ds->num_ports); > + bitmap_zero(ds->bitmap, ds->num_ports); > if (ds->index == info->sw_index) > - set_bit(info->port, members); > + set_bit(info->port, ds->bitmap); > for (port = 0; port < ds->num_ports; port++) > if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) > - set_bit(port, members); > + set_bit(port, ds->bitmap); > > if (switchdev_trans_ph_prepare(trans)) > - return dsa_switch_vlan_prepare_bitmap(ds, vlan, members); > + return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap); > > - dsa_switch_vlan_add_bitmap(ds, vlan, members); > + dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap); > > return 0; > } > -- > 1.9.1 > -- Kees Cook Pixel Security