From patchwork Mon May 7 15:23:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salvatore Mesoraca X-Patchwork-Id: 10384359 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 623D760159 for ; Mon, 7 May 2018 15:23:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5E53F24603 for ; Mon, 7 May 2018 15:23:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 52C862886B; Mon, 7 May 2018 15:23:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 7CC4924603 for ; Mon, 7 May 2018 15:23:46 +0000 (UTC) Received: (qmail 12226 invoked by uid 550); 7 May 2018 15:23:43 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 12188 invoked from network); 7 May 2018 15:23:42 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Z00bKkbsS5ItJDut2FnPxNX2T5V0SKTWeMhI01fG63Y=; b=FuudKQefVI1d8cda6Kz1hP6fbG4JFrCbRk/1NplOjPQ7/0rRiw4Q3oRtJa96OK05WG BM/bfrbayZ3VTtqu9QyDQdeLUZTMTCZfJWKGk5Q0Uv1mkvsNsUhs/COokpizMl7NqVqM U2ZUQ9In/eFZ2NFGZqOtYRPSlaewYp+atVEEaFbRaXerMkYdBHV/66+AE39Un3LTyqC4 wRJ7hpmYsmXLgNwlyo97CL/VOBeJ9w47oX6uLPBA5gsHRrvTzXp+uXK1PhKb8k7ZTSUN gDibihwM79L29YYoPkLh0gv9f3xHMrySq02J+CPbWigCf5F5dPlV9mpziyQRS0P6LScc 6hhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Z00bKkbsS5ItJDut2FnPxNX2T5V0SKTWeMhI01fG63Y=; b=YSesVExSwflMSN9kfjU5iCgms34FQgKX+lkgW3SciSS+mihB9DzO2R4WfzQoxqglFu N6vstrMDkdXSE7I/JPRm8k6TW72RZVc8q4RGexrGRjHKOuyVq3F+d+kY/WxUsAAQMWQO aRpTxZ+jzEkVuIvYkzz9tEv5JNMpa6lAhzuVy4brGSwGQiHWQeDU72rz7W3Qq2Ai0DGN iVOOOci5XnypdyoJW3AEDxfJdNCbzLuLMNPRS+QNKQa6yl1HMcWKlTMOP25TawusEF3Q VNuZswfZFjpqYSIfLvGC8yjSExSaoa9VRJuRcWJn7tW5UH+VHKdCPJm8W8WgSBUVM7+g ynJQ== X-Gm-Message-State: ALKqPwfGwgD/0PeAnSuKJcu0dDTUdyUOui7JXZQB9J4Rm4xIFce9dnsS ilCFUqVk7z8Vn4dy7XW+m1E= X-Google-Smtp-Source: AB8JxZqzTTh9aFDyLddPwiHSejYfSflhBnUrgufZNAV+aL+pY8ZO8065aEcK5rotifgvqSmrFi3LTQ== X-Received: by 2002:a1c:b4c3:: with SMTP id d186-v6mr1021421wmf.4.1525706611292; Mon, 07 May 2018 08:23:31 -0700 (PDT) From: Salvatore Mesoraca To: Andrew Lunn Cc: linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, netdev@vger.kernel.org, "David S. Miller" , Florian Fainelli , Kees Cook , Salvatore Mesoraca , Vivien Didelot , David Laight Subject: [PATCH v2] net: dsa: drop some VLAs in switch.c Date: Mon, 7 May 2018 17:23:16 +0200 Message-Id: <1525706596-13601-1-git-send-email-s.mesoraca16@gmail.com> X-Mailer: git-send-email 1.9.1 X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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; }