Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3316849pxm; Mon, 28 Feb 2022 17:16:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJzvOyMOXP+vxpvXaVwiI+XCcyooFFJTixMA+hHXogsehkfhS/SHlamTgM2omt9MTR5weSqv X-Received: by 2002:a17:902:ef52:b0:151:84fe:bccb with SMTP id e18-20020a170902ef5200b0015184febccbmr645plx.9.1646097399607; Mon, 28 Feb 2022 17:16:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646097399; cv=none; d=google.com; s=arc-20160816; b=fGbcKQh734rE7fJAWYrqGD5N90oKvwkWCis2j9DDj5CFtmLbEeEdF4CViDlDCQrRHC 7hVGuYNGa87dnyjTZhe4TdQmqc/FzKYqtcz5diSVKSzj8N3cd8BsnhNEp2OX9Z+bWegT Y0m5F3xZF4LeUhzdDMBJt4bTAOZxoCCAxbR17mAt7uwD4Is8lMboXI4/bVUJdA8nfMvi WqOzhpGDx5cUJ0RXnJtwxNOb2SaaBsV4owXnKi+sr0jCikeIO2BJmpe3o49TUklxEYN/ cSCmTuqT1ZIqRlk4Q5b/LESLXype6Ep59zrj6Q1fJ5sHtQB5bxeUYoyDro6SuRhgrFw9 LvBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=cUh7YOptqnvj33mIS3WykOaibv0ctq8lQu0USbAcExo=; b=mUm8AAhP9Nz6ZD1DxUthQpVN0UWUrvDAWhCyKQBkR5vaf7KHuR1FyqwAwypVM5MBSP iN8RyQFsOkkX7ruLpU8y+50R1svoRok75CqQVtXTg5QB27L+Kpgp7hp8wArCMlzmkm7A LnkSEtW5Cxp1hViTJMTc4T+S8aXk4USUNXcg8C9FQo1xGre8Gcw3bD4N5YjMaZkPcd5K 8zClol/FO8kN2tZAM6ejAwAtFzg0nkZoLzV2U9SYh4UBKmb+Lfhv0e1XXrt33bpwES6m qWafDvKW7v3f4gdxzZA713SZ4rTNkHmKP5BFLbywIiFOw3KJdWeeblo7T9nWe6YgzEoL pKPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=gduPBw6T; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 2-20020a630502000000b003739e883d9asi10566365pgf.876.2022.02.28.17.16.28; Mon, 28 Feb 2022 17:16:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-wireless-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=@linux-foundation.org header.s=google header.b=gduPBw6T; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231853AbiCAAmX (ORCPT + 72 others); Mon, 28 Feb 2022 19:42:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231851AbiCAAmR (ORCPT ); Mon, 28 Feb 2022 19:42:17 -0500 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BA34E5C for ; Mon, 28 Feb 2022 16:41:36 -0800 (PST) Received: by mail-ej1-x635.google.com with SMTP id a8so28242772ejc.8 for ; Mon, 28 Feb 2022 16:41:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cUh7YOptqnvj33mIS3WykOaibv0ctq8lQu0USbAcExo=; b=gduPBw6Te4zeyjZyaNuQQxqABiMq6fHp34ckrObT/FmzNLnDhHL/6KerjIgP5i0R8H Vm0a8HwZ8W8PnRRZRxnIsSc7yENLFtSGfKUqeeqvjpp8888PKIgF8NBwqM5k9ZzIrQCZ iYuicfiGesL4zlgkSDywOQeUUhyrP2kMozUBc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=cUh7YOptqnvj33mIS3WykOaibv0ctq8lQu0USbAcExo=; b=2IHQ9NFUlTNeJX0kl2UsB8WL7mA1XALa02C25rEXj5KGgHBONmL42Da+7LUWONLHif r3/3vXJSZL+BfTfHXd/3ijwlgyJ4WCvzCst/fdMx5dm9UfxN6/ttJA0+MKoqaCIo6G6y djqvOk0Wybo4ECkloW0u/nLiPm/7AxAFCVXbv6h/EDQWiF4Uv3Op7m0TOCjXC+Hq7odl YjPJL0g1FchdAVN6GxpDBjlGxsgSsv6zyBZB4+oRUwS03iKVjMm5aF8Z9hVI5SDpeHfb VqmiQz03ypYUorX6lPWcQSmFTU6bazFilj5oO/4ybNK74M/Bav2MM4F0NBaJn7/qeNdz he6Q== X-Gm-Message-State: AOAM530j4SE8Ayb2LQ1AgcYNTbaXG7JnW15+C2YXK8I4hr2GbSIXlNPr KChXmD9hBoheyC9KEEwOoHsOqcA6RrBJiFU+/e0= X-Received: by 2002:a17:906:194f:b0:6ce:3670:92b with SMTP id b15-20020a170906194f00b006ce3670092bmr16579477eje.737.1646095294269; Mon, 28 Feb 2022 16:41:34 -0800 (PST) Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com. [209.85.218.42]) by smtp.gmail.com with ESMTPSA id t2-20020aa7d702000000b0040f6617cc10sm6693251edq.89.2022.02.28.16.41.31 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Feb 2022 16:41:32 -0800 (PST) Received: by mail-ej1-f42.google.com with SMTP id a8so28242615ejc.8 for ; Mon, 28 Feb 2022 16:41:31 -0800 (PST) X-Received: by 2002:ac2:4d91:0:b0:443:127b:558a with SMTP id g17-20020ac24d91000000b00443127b558amr14552706lfe.542.1646095280878; Mon, 28 Feb 2022 16:41:20 -0800 (PST) MIME-Version: 1.0 References: <20220228110822.491923-1-jakobkoschel@gmail.com> <20220228110822.491923-3-jakobkoschel@gmail.com> <2e4e95d6-f6c9-a188-e1cd-b1eae465562a@amd.com> In-Reply-To: From: Linus Torvalds Date: Mon, 28 Feb 2022 16:41:04 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/6] treewide: remove using list iterator after loop body as a ptr To: Jakob Koschel Cc: =?UTF-8?Q?Christian_K=C3=B6nig?= , alsa-devel@alsa-project.org, linux-aspeed@lists.ozlabs.org, "Gustavo A. R. Silva" , linux-iio@vger.kernel.org, nouveau@lists.freedesktop.org, Rasmus Villemoes , dri-devel , Cristiano Giuffrida , amd-gfx list , samba-technical@lists.samba.org, linux1394-devel@lists.sourceforge.net, drbd-dev@lists.linbit.com, linux-arch , CIFS , KVM list , linux-scsi , linux-rdma , linux-staging@lists.linux.dev, "Bos, H.J." , Jason Gunthorpe , intel-wired-lan@lists.osuosl.org, kgdb-bugreport@lists.sourceforge.net, bcm-kernel-feedback-list@broadcom.com, Dan Carpenter , Linux Media Mailing List , Kees Cook , Arnd Bergman , Linux PM , intel-gfx , Brian Johannesmeyer , Nathan Chancellor , linux-fsdevel , Christophe JAILLET , v9fs-developer@lists.sourceforge.net, linux-tegra , Thomas Gleixner , Andy Shevchenko , Linux ARM , linux-sgx@vger.kernel.org, linux-block , Netdev , linux-usb@vger.kernel.org, linux-wireless , Linux Kernel Mailing List , Linux F2FS Dev Mailing List , tipc-discussion@lists.sourceforge.net, Linux Crypto Mailing List , dma , linux-mediatek@lists.infradead.org, Andrew Morton , linuxppc-dev , Mike Rapoport Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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-wireless@vger.kernel.org On Mon, Feb 28, 2022 at 1:47 PM Jakob Koschel wrote: > > The goal of this is to get compiler warnings right? This would indeed be great. Yes, so I don't mind having a one-time patch that has been gathered using some automated checker tool, but I don't think that works from a long-term maintenance perspective. So if we have the basic rule being "don't use the loop iterator after the loop has finished, because it can cause all kinds of subtle issues", then in _addition_ to fixing the existing code paths that have this issue, I really would want to (a) get a compiler warning for future cases and (b) make it not actually _work_ for future cases. Because otherwise it will just happen again. > Changing the list_for_each_entry() macro first will break all of those cases > (e.g. the ones using 'list_entry_is_head()). So I have no problems with breaking cases that we basically already have a patch for due to your automated tool. There were certainly more than a handful, but it didn't look _too_ bad to just make the rule be "don't use the iterator after the loop". Of course, that's just based on that patch of yours. Maybe there are a ton of other cases that your patch didn't change, because they didn't match your trigger case, so I may just be overly optimistic here. But basically to _me_, the important part is that the end result is maintainable longer-term. I'm more than happy to have a one-time patch to fix a lot of dubious cases if we can then have clean rules going forward. > I assumed it is better to fix those cases first and then have a simple > coccinelle script changing the macro + moving the iterator into the scope > of the macro. So that had been another plan of mine, until I actually looked at changing the macro. In the one case I looked at, it was ugly beyond belief. It turns out that just syntactically, it's really nice to give the type of the iterator from outside the way we do now. Yeah, it may be a bit odd, and maybe it's partly because I'm so used to the "list_for_each_list_entry()" syntax, but moving the type into the loop construct really made it nasty - either one very complex line, or having to split it over two lines which was even worse. Maybe the place I looked at just happened to have a long typename, but it's basically always going to be a struct, so it's never a _simple_ type. And it just looked very odd adn unnatural to have the type as one of the "arguments" to that list_for_each_entry() macro. So yes, initially my idea had been to just move the iterator entirely inside the macro. But specifying the type got so ugly that I think that typeof (pos) pos trick inside the macro really ends up giving us the best of all worlds: (a) let's us keep the existing syntax and code for all the nice cases that did everything inside the loop anyway (b) gives us a nice warning for any normal use-after-loop case (unless you explicitly initialized it like that sgx_mmu_notifier_release() function did for no good reason (c) also guarantees that even if you don't get a warning, non-converted (or newly written) bad code won't actually _work_ so you end up getting the new rules without any ambiguity or mistaken > With this you are no longer able to set the 'outer' pos within the list > iterator loop body or am I missing something? Correct. Any assignment inside the loop will be entirely just to the local loop case. So any "break;" out of the loop will have to set another variable - like your updated patch did. > I fail to see how this will make most of the changes in this > patch obsolete (if that was the intention). I hope my explanation above clarifies my thinking: I do not dislike your patch, and in fact your patch is indeed required to make the new semantics work. What I disliked was always the maintainability of your patch - making the rules be something that isn't actually visible in the source code, and letting the old semantics still work as well as they ever did, and having to basically run some verification pass to find bad users. (I also disliked your original patch that mixed up the "CPU speculation type safety" with the actual non-speculative problems, but that was another issue). Linus