Received: by 2002:a05:6a10:87d6:0:0:0:0 with SMTP id g22csp988358pxr; Mon, 11 Apr 2022 12:05:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEDPATK3uomFTgLqnhg42a8OsjVyTLNLVitcqCWU8gaJmkU/hrHrP1FCQyfWMBtlbqGJKi X-Received: by 2002:a17:902:8c81:b0:156:7fee:643b with SMTP id t1-20020a1709028c8100b001567fee643bmr33811501plo.59.1649703921876; Mon, 11 Apr 2022 12:05:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649703921; cv=none; d=google.com; s=arc-20160816; b=i5VWJt9iO3b8GvCNSgbV5OvYXLfbQyegWPilJpE/sLpfC6fUdFrDhjYPUjHrQmueQg Gc5thjrBV6AEe44nK/y7Jwa/P2T2Y5HcEI2DN1jdKFeslyXpmzqcqX1fxF9tSTPqtTI2 gpZP06mVG1T6T/4PEWVo+Vlv8BUP7wPf1zfDyvBGPrcMW/B6YDj9zNqlEeVQJsOeAob5 TLKvtRP5ozyp3W3xZ9R2WE7mcHcVZLl/kDvpx2og//MTQgtK8S8MogPjrDEBVSuippAB o2aZfCOWuAw+ejUy0rY9h1wHmxTrDL1AGe9nhEW8l1QWKJKUyuFqQPQ7Rw6wUlD4w6ql +KYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=6cdVUS6XiscYrUgxUxtAL2ssNdCpR5fNQ/PXRqx8HXE=; b=ILb/iPp7AZ6T3x7p+h40cNd4Zj5f4xDN1C92Hg7X8JOwjeik7obJkI9aLGFX4pb0bO E8KK/8bcDvekNjnMo64o8yHmhNToOsMHXsyWm9nOmgYjV0tnHrN5L4C95aVl9IeXFseu 2M/LuZME2rliG1gSScm7BegWPtKOoG/65q/ppHqlp3lgAbdDuHh5muVd3J9SgGHjscus 76T6cl3hyYpgWcdiMW24iaYaIhWZsFHGJVF/5bXfsZ9N+s/teul3C9/CUNID6CbUuN6b 1avR1hyQVT1ukOW+HSU4k2Yt0HPuW1YzlATYrw53z9o7bhoCFOWF6nkl4O1yrVR5x2md tywQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=E3oamHlV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b1-20020a056a0002c100b004fa3a8dffbcsi9168609pft.115.2022.04.11.12.05.06; Mon, 11 Apr 2022 12:05:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=E3oamHlV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239999AbiDJS0x (ORCPT + 99 others); Sun, 10 Apr 2022 14:26:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230458AbiDJS0x (ORCPT ); Sun, 10 Apr 2022 14:26:53 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53CC04FC6C; Sun, 10 Apr 2022 11:24:41 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id v15so5698364edb.12; Sun, 10 Apr 2022 11:24:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=6cdVUS6XiscYrUgxUxtAL2ssNdCpR5fNQ/PXRqx8HXE=; b=E3oamHlVUO9sG+tUlCb7w/lxckY+1bMJ8Oj/JMRsdthREWTAo/EU1ssZbsEddX/CMW XF+LLnOvzqbQ09/kqpDMUFnXICqi/jhgYhhv5gxrC5MMdDeloz3+bT7nNdHFZfSEL2q0 XjhyXlJ2wmJ+nWvvMT0izK3719NdGolLrqWsrZQa3UBq1r9OQ40xF0MyV4NqmK82mhIJ Z5+8pR6KZwFLv6NnK3nKzBo3rK6QovFRSGe9xxfzi7Cygm/LyX76sqMrbY0iZzA1pxUb HTenU1x8AjD/thUEi/iR5ReeV/7u+ZMAdb3HHejyOJt71iosXVU5vKr6vibgesiixUDv Ojhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=6cdVUS6XiscYrUgxUxtAL2ssNdCpR5fNQ/PXRqx8HXE=; b=oZ70vUMHT1f2R7ZkKTsK/pRe0ZP070Wo3lnf7IMacIGvDui3Qg3WI5w4LLDA+PKw0J ZJE93qQ3ip7ELF5x62uTV3wWumnlfqyheUbFBYxWUrx0CY6TdAeCTalcoYkhW7wkbj1z W96Cq67u7QaASq/Y/QdXMaF9pocve+5HZe4Eyt4ggZxS4A1uM8U54Il2o7QovM58kmBt BR0SvG0myREqnMKveFBHXkWzjdWyryab9GNGMQiWbLOivDhGCV32OEMtuMZJriHm8EmG aRByH5CqKO7WOF5fUpoc+qp5TGsqsMymbNlAoNhzy3ulnEWDQFvOq+nMzNv1e6TEw3Yf W3Ww== X-Gm-Message-State: AOAM531uTlVcXMsZd8j8xnQLHzLUc9QHOhUqBcPguwNO+l05xJbpKq5e RIQd87DoQ7h7iZeL7Rb04EXqU0lcMxcNtVTC X-Received: by 2002:aa7:db94:0:b0:410:f0e8:c39e with SMTP id u20-20020aa7db94000000b00410f0e8c39emr29340972edt.14.1649615079652; Sun, 10 Apr 2022 11:24:39 -0700 (PDT) Received: from smtpclient.apple (i130160.upc-i.chello.nl. [62.195.130.160]) by smtp.gmail.com with ESMTPSA id s4-20020a170906bc4400b006e893908c4csm356693ejv.60.2022.04.10.11.24.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Apr 2022 11:24:39 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.80.82.1.1\)) Subject: Re: [PATCH net-next 02/15] net: dsa: sja1105: Remove usage of iterator for list_add() after loop From: Jakob Koschel In-Reply-To: <935062D0-C657-4C79-A0BE-70141D052EC0@gmail.com> Date: Sun, 10 Apr 2022 20:24:37 +0200 Cc: "David S. Miller" , Jakub Kicinski , Paolo Abeni , Andrew Lunn , Vivien Didelot , Florian Fainelli , Lars Povlsen , Steen Hegelund , UNGLinuxDriver@microchip.com, Ariel Elior , Manish Chopra , Edward Cree , Martin Habets , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Jiri Pirko , Casper Andersson , Bjarni Jonasson , Colin Ian King , Michael Walle , Christophe JAILLET , Arnd Bergmann , Eric Dumazet , Di Zhu , Xu Wang , Netdev , LKML , Linux ARM , linuxppc-dev , Mike Rapoport , Brian Johannesmeyer , Cristiano Giuffrida , "Bos, H.J." Content-Transfer-Encoding: quoted-printable Message-Id: References: <20220407102900.3086255-1-jakobkoschel@gmail.com> <20220407102900.3086255-3-jakobkoschel@gmail.com> <20220408114120.tvf2lxvhfqbnrlml@skbuf> <20220410110508.em3r7z62ufqcbrfm@skbuf> <935062D0-C657-4C79-A0BE-70141D052EC0@gmail.com> To: Vladimir Oltean X-Mailer: Apple Mail (2.3696.80.82.1.1) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 > On 10. Apr 2022, at 14:39, Jakob Koschel = wrote: >=20 >=20 >=20 >> On 10. Apr 2022, at 13:05, Vladimir Oltean wrote: >>=20 >> On Sun, Apr 10, 2022 at 12:51:56PM +0200, Jakob Koschel wrote: >>> I've just looked at this again in a bit more detail while = integrating it into the patch series. >>>=20 >>> I realized that this just shifts the 'problem' to using the 'pos' = iterator variable after the loop. >>> If the scope of the list iterator would be lowered to the list = traversal loop it would also make sense >>> to also do it for list_for_each(). >>=20 >> Yes, but list_for_each() was never formulated as being problematic in >> the same way as list_for_each_entry(), was it? I guess I'm starting = to >> not understand what is the true purpose of the changes. >=20 > Sorry for having caused the confusion. Let me elaborate a bit to give = more context. >=20 > There are two main benefits of this entire effort. >=20 > 1) fix the architectural bugs and avoid any missuse of the list = iterator after the loop > by construction. This only concerns the list_for_each_entry_*() macros = and your change > will allow lowering the scope for all of those. It might be debatable = that it would be > more consistent to lower the scope for list_for_each() as well, but it = wouldn't be > strictly necessary. >=20 > 2) fix *possible* speculative bugs. In our project, Kasper [1], we = were able to show > that this can be an issue for the list traversal macros (and this is = how the entire > effort started). > The reason is that the processor might run an additional loop = iteration in speculative > execution with the iterator variable computed based on the head = element. This can > (and we have verified this) happen if the CPU incorrectly=20 > assumes !list_entry_is_head(pos, head, member). >=20 > If this happens, all memory accesses based on the iterator variable = *potentially* open > the chance for spectre [2] gadgets. The proposed mitigation was = setting the iterator variable > to NULL when the terminating condition is reached (in speculative safe = way). Then, > the additional speculative list iteration would still execute but = won't access any > potential secret data. >=20 > And this would also be required for list_for_each() since combined = with the list_entry() > within the loop it basically is semantically identical to = list_for_each_entry() > for the additional speculative iteration. >=20 > Now, I have no strong opinion on going all the way and since 2) is not = the main motivation > for this I'm also fine with sticking to your proposed solution, but it = would mean that implementing > a "speculative safe" list_for_each() will be more difficult in the = future since it is using > the iterator of list_for_each() past the loop. >=20 > I hope this explains the background a bit better. >=20 >>=20 >>> What do you think about doing it this way: >>>=20 >>> diff --git a/drivers/net/dsa/sja1105/sja1105_vl.c = b/drivers/net/dsa/sja1105/sja1105_vl.c >>> index b7e95d60a6e4..f5b0502c1098 100644 >>> --- a/drivers/net/dsa/sja1105/sja1105_vl.c >>> +++ b/drivers/net/dsa/sja1105/sja1105_vl.c >>> @@ -28,6 +28,7 @@ static int sja1105_insert_gate_entry(struct = sja1105_gating_config *gating_cfg, >>> list_add(&e->list, &gating_cfg->entries); >>> } else { >>> struct sja1105_gate_entry *p; >>> + struct list_head *pos =3D NULL; >>>=20 >>> list_for_each_entry(p, &gating_cfg->entries, list) { >>> if (p->interval =3D=3D e->interval) { >>> @@ -37,10 +38,14 @@ static int sja1105_insert_gate_entry(struct = sja1105_gating_config *gating_cfg, >>> goto err; >>> } >>>=20 >>> - if (e->interval < p->interval) >>> + if (e->interval < p->interval) { >>> + pos =3D &p->list; >>> break; >>> + } >>> } >>> - list_add(&e->list, p->list.prev); >>> + if (!pos) >>> + pos =3D &gating_cfg->entries; >>> + list_add(&e->list, pos->prev); >>> } >>>=20 >>> gating_cfg->num_entries++; >>> -- >>>=20 >>>>=20 >>>> Thanks for the suggestion. >>>>=20 >>>>> } >>>>>=20 >>>>> gating_cfg->num_entries++; >>>>> -----------------------------[ cut here = ]----------------------------- >>>>=20 >>>> [1] = https://lore.kernel.org/linux-kernel/20220407102900.3086255-12-jakobkosche= l@gmail.com/ >>>>=20 >>>> Jakob >>>=20 >>> Thanks, >>> Jakob >=20 > Thanks, > Jakob >=20 > [1] https://www.vusec.net/projects/kasper/ > [2] https://spectreattack.com/spectre.pdf Btw, I just realized that the if (!pos) is not necessary. This should = simply do it: diff --git a/drivers/net/dsa/sja1105/sja1105_vl.c = b/drivers/net/dsa/sja1105/sja1105_vl.c index b7e95d60a6e4..2d59e75a9e3d 100644 --- a/drivers/net/dsa/sja1105/sja1105_vl.c +++ b/drivers/net/dsa/sja1105/sja1105_vl.c @@ -28,6 +28,7 @@ static int sja1105_insert_gate_entry(struct = sja1105_gating_config *gating_cfg, list_add(&e->list, &gating_cfg->entries); } else { + struct list_head *pos =3D &gating_cfg->entries; struct sja1105_gate_entry *p; =20 list_for_each_entry(p, &gating_cfg->entries, list) { if (p->interval =3D=3D e->interval) { @@ -37,10 +38,12 @@ static int sja1105_insert_gate_entry(struct = sja1105_gating_config *gating_cfg, goto err; } =20 - if (e->interval < p->interval) + if (e->interval < p->interval) { + pos =3D &p->list; break; + } } - list_add(&e->list, p->list.prev); + list_add(&e->list, pos->prev); } =20 gating_cfg->num_entries++; --=20 2.25.1 Thanks, Jakob