Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp56356pxb; Tue, 12 Apr 2022 16:41:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTuQM0fIwb+/nevXsHUCccg62p3Zigopybrps4WNUxwZ6WUNQH+NrgDyI+KW94uWVqZWjo X-Received: by 2002:a17:90a:31cf:b0:1c9:f9b8:68c7 with SMTP id j15-20020a17090a31cf00b001c9f9b868c7mr7714454pjf.34.1649806876342; Tue, 12 Apr 2022 16:41:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649806876; cv=none; d=google.com; s=arc-20160816; b=qZAZGh9sEodJSD6YY10rFUJgLpWdeAjr9RzT5bquorDPYQUSEvG+yg75julvBLpPjJ CF1NATXhGPY4hHBqejfLHTRmF2KYlQL49BMEpLdC5+jMpDdoBkR+c6DPUWsmy2ZBrEmp Cnnc1Tarlgdg/eIcB5GAhfqb7eUEpP50HwSXvz3jnweoAPIocg7E/ZMZfB7HGhyEICov NH/E00xy1GSxQbXsOLnafIu1bBCHeOg+/esadOxGyxRfaNSCiECHMMvBTJsUubQO39jk EyswJqykYEgc5N2HtsuUim728x/Aed6r60wVZpKCHDFdVnwwDr+sMAYruBefxZkS6c/y W/PA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Vf75TiqrDs9Xy+FgQuO5VDyKrFCIlVjIyCetakykwl0=; b=N8Uk+aQhZPa3QYmqRKY0yp/ULlUtrVtCvyHq64rdUYQIjxpmnYc/FISBlvpXFgp7Yq P4+wfxY1jzAI9qUYULavENcQ9K4Cte7tC5OPVcvouFKSC3szcFpRC0E30QqvmP3aaEh0 FKUDfXHI1wXgsbIrHlH/y7FQy1zFUfXcxE1HiGvkLXhohdnC835PoToQ/uduUD1FCcB/ ugDvKMc7jstB+yQxt6JaNWJOV9gNPSgofz8hf8DPQnqzD6Vw+YYdIrtgXRgcVgk2Z1C5 5BOO82ZmwT2uheatlMaM54nVxVgiVKZRSlYv62LgaPoPP5FxLv3EShJlXrlop4naI4yT HR+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=cCtRTwf6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h18-20020a056a001a5200b004fa3a8e0066si13152412pfv.285.2022.04.12.16.41.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 16:41:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=cCtRTwf6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E4CB01C42DD; Tue, 12 Apr 2022 14:32:54 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353869AbiDLMve (ORCPT + 99 others); Tue, 12 Apr 2022 08:51:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355279AbiDLMsJ (ORCPT ); Tue, 12 Apr 2022 08:48:09 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECC8926C8; Tue, 12 Apr 2022 05:16:31 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id v4so8599434edl.7; Tue, 12 Apr 2022 05:16:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Vf75TiqrDs9Xy+FgQuO5VDyKrFCIlVjIyCetakykwl0=; b=cCtRTwf6J9/01EnztyQz7m140uZQxQAof1HI0y11qryZ2fVorK4lB65ioLV1c0nrsl 1h7MbY6VrAgCGgExfsMJ+Oucm7qXGeoQcY14GDDr26ZpyFafCfZoW1yPkG7pzauJTpYK qt48CXccLoIw4c0ptJ1BddtsNLNSv0o3DWP+Ck4kKcPt8FlT+NokS2owlHiBwjwkNsSZ RJxdUKhnddVNngK60N5gdpHkjJeAa5EKM9uV8EP9edxlslkHs9ylQqAZF35pKDFlOUZi fNUHt8KI4LoCDQKrGZX/4Evr0L5B9uFtEEGyqpzITNz2o5ce82I9sB/uSxltmar2MoZ9 sUdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Vf75TiqrDs9Xy+FgQuO5VDyKrFCIlVjIyCetakykwl0=; b=nPgAUT1mLyKFK5m5wnpTCpYiIoOhx6R7rQkUUx94HYqPfcKPevpWVNHwKuRasut9hm w0h0ce6S9moZWgyjQEPRiww0dYJ7BGic5ZdTHiBCb/IM+V56p21mkapYjc+qJsZXppEd GBGIR7Ut7kQmwzFnPsuf1dnmqwkVoEa84CWQ/2KnFo12OKa2pm72CCYhXq8zLDmL5qy4 jVNYW9ZApq50Aw30HMbkwY70Su+8FZ/wvRAvVCMKoFkCAoETuWG6X9mY6GCsqTBi9Wle 5obhgMu80OgLAw494QFvgLEBDNfEe/jcum8xk1sz7Ygp8JNAcHa+kPTNiAvY406HpJnK DwkA== X-Gm-Message-State: AOAM5339SZfnD8LzsHvEXj17EXx5xHo8CYX+TV6k/dDPvHIcIIjF8aTQ dMEfSdpg0VSbCGZYofmA5Bo= X-Received: by 2002:a50:d90f:0:b0:418:8a5a:14b2 with SMTP id t15-20020a50d90f000000b004188a5a14b2mr37765432edj.241.1649765790424; Tue, 12 Apr 2022 05:16:30 -0700 (PDT) Received: from localhost.localdomain (i130160.upc-i.chello.nl. [62.195.130.160]) by smtp.googlemail.com with ESMTPSA id b5-20020a17090630c500b006e8044fa76bsm8827900ejb.143.2022.04.12.05.16.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 05:16:30 -0700 (PDT) From: Jakob Koschel To: "David S. Miller" Cc: Jakub Kicinski , Paolo Abeni , Andrew Lunn , Vivien Didelot , Florian Fainelli , Vladimir Oltean , Lars Povlsen , Steen Hegelund , UNGLinuxDriver@microchip.com, Ariel Elior , Manish Chopra , Edward Cree , Martin Habets , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Jiri Pirko , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Casper Andersson , Bjarni Jonasson , Jakob Koschel , Christophe JAILLET , Michael Walle , Jason Gunthorpe , Colin Ian King , Arnd Bergmann , Eric Dumazet , Di Zhu , Xu Wang , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, bpf@vger.kernel.org, Mike Rapoport , "Brian Johannesmeyer" , Cristiano Giuffrida , "Bos, H.J." , Vladimir Oltean Subject: [PATCH net-next v3 06/18] net: dsa: mv88e6xxx: refactor mv88e6xxx_port_vlan() Date: Tue, 12 Apr 2022 14:15:45 +0200 Message-Id: <20220412121557.3553555-7-jakobkoschel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412121557.3553555-1-jakobkoschel@gmail.com> References: <20220412121557.3553555-1-jakobkoschel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Vladimir Oltean To avoid bugs and speculative execution exploits due to type-confused pointers at the end of a list_for_each_entry() loop, one measure is to restrict code to not use the iterator variable outside the loop block. In the case of mv88e6xxx_port_vlan(), this isn't a problem, as we never let the loops exit through "natural causes" anyway, by using a "found" variable and then using the last "dp" iterator prior to the break, which is a safe thing to do. Nonetheless, with the expected new syntax, this pattern will no longer be possible. Profit off of the occasion and break the two port finding methods into smaller sub-functions. Somehow, returning a copy of the iterator pointer is still accepted. This change makes it redundant to have a "bool found", since the "dp" from mv88e6xxx_port_vlan() now holds NULL if we haven't found what we were looking for. Signed-off-by: Vladimir Oltean Signed-off-by: Jakob Koschel --- drivers/net/dsa/mv88e6xxx/chip.c | 54 ++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c index b3aa0e5bc842..1f35e89053e6 100644 --- a/drivers/net/dsa/mv88e6xxx/chip.c +++ b/drivers/net/dsa/mv88e6xxx/chip.c @@ -1378,42 +1378,50 @@ static int mv88e6xxx_set_mac_eee(struct dsa_switch *ds, int port, return 0; } +static struct dsa_port *mv88e6xxx_find_port(struct dsa_switch_tree *dst, + int sw_index, int port) +{ + struct dsa_port *dp; + + list_for_each_entry(dp, &dst->ports, list) + if (dp->ds->index == sw_index && dp->index == port) + return dp; + + return NULL; +} + +static struct dsa_port * +mv88e6xxx_find_port_by_bridge_num(struct dsa_switch_tree *dst, + unsigned int bridge_num) +{ + struct dsa_port *dp; + + list_for_each_entry(dp, &dst->ports, list) + if (dsa_port_bridge_num_get(dp) == bridge_num) + return dp; + + return NULL; +} + /* Mask of the local ports allowed to receive frames from a given fabric port */ static u16 mv88e6xxx_port_vlan(struct mv88e6xxx_chip *chip, int dev, int port) { struct dsa_switch *ds = chip->ds; struct dsa_switch_tree *dst = ds->dst; struct dsa_port *dp, *other_dp; - bool found = false; u16 pvlan; - /* dev is a physical switch */ if (dev <= dst->last_switch) { - list_for_each_entry(dp, &dst->ports, list) { - if (dp->ds->index == dev && dp->index == port) { - /* dp might be a DSA link or a user port, so it - * might or might not have a bridge. - * Use the "found" variable for both cases. - */ - found = true; - break; - } - } - /* dev is a virtual bridge */ + /* dev is a physical switch */ + dp = mv88e6xxx_find_port(dst, dev, port); } else { - list_for_each_entry(dp, &dst->ports, list) { - unsigned int bridge_num = dsa_port_bridge_num_get(dp); - - if (bridge_num + dst->last_switch != dev) - continue; - - found = true; - break; - } + /* dev is a virtual bridge */ + dp = mv88e6xxx_find_port_by_bridge_num(dst, + dev - dst->last_switch); } /* Prevent frames from unknown switch or virtual bridge */ - if (!found) + if (!dp) return 0; /* Frames from DSA links and CPU ports can egress any local port */ -- 2.25.1