Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp751831pxp; Sat, 5 Mar 2022 18:09:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJzWDkIOz1H0yHsC0whtnA9QOU698DnXi2Hp1pQrhtGxogMS0aec06HJ1ygZpDq6DArNw+NR X-Received: by 2002:a05:6a00:134c:b0:4c7:9893:3452 with SMTP id k12-20020a056a00134c00b004c798933452mr6304374pfu.37.1646532590239; Sat, 05 Mar 2022 18:09:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646532590; cv=none; d=google.com; s=arc-20160816; b=I7C+5FokeP1+K1lRcCDHPBtL4Wh7wM9ltmOTjYJn+MsEXiP6H4WaO4kP91GfxgN2k0 lS3bTu3u2OuJznbbG93dnQ9qZ7IpJQEbGZSQmxJxz4uSZOR0GW5QwqJMsfpVe0UF2Rni o30stRPWq+aVBBqKu39CbKXjziJ/9gNWvG69/kG2ZQv1+7jvHkGqrQebwoisvksXcZVL wIebTdSRixS1+ZSeh/uV3XMNx0iFD5pVTJWo68qjtWNRDsyrNz/iJzORWurB02EQ9Kvp 5EniVqijwN7u9yQFvdJQajB3nNS7lQgoS9AyPoLpj0UIUL7hqtOL4gPSHkGlMKXLd7xX vsGA== 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=s2/4A0u52052NhlLyv5JICwZ+hKXNcyjgvPejm7yB/I=; b=urYSBWRThaV7OpsnUAHe/+Y3lTQsWrDuNtCARc7APYqRjzxlLzMFKu/FU3QFiHq9hZ cSeWotxwRkg2x1+u+utnlfMRclBXq0noDsFaTL8Vx39o+L9M8rRcHIsiOLNagwPwouLf xJl3YfD1Y3FHRcwdFe8frHojOA/6WuqhXtYvbp8/gjeFkbpSL66tmLzz7CIaobhW2f/N oNfrm2d2QIo8PWKYxMddbE7R0rGFUqb/rWr1pfMa7ixdp5CooGZk+pa934LgTVkN6BaG 2Gdp6KCfiKUA/6EJ/p8NJzo5hmnGv00flvnqRCqI63la9eb3e1F9D9/i5bsSJo0d04sP v2Ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=g8gQIPzf; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bf5-20020a656d05000000b003726bf50270si9243726pgb.598.2022.03.05.18.09.10; Sat, 05 Mar 2022 18:09:50 -0800 (PST) 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=@linux-foundation.org header.s=google header.b=g8gQIPzf; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232616AbiCEVKQ (ORCPT + 99 others); Sat, 5 Mar 2022 16:10:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232601AbiCEVKO (ORCPT ); Sat, 5 Mar 2022 16:10:14 -0500 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDEE96A07F for ; Sat, 5 Mar 2022 13:09:22 -0800 (PST) Received: by mail-lf1-x129.google.com with SMTP id w7so6281320lfd.6 for ; Sat, 05 Mar 2022 13:09:22 -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=s2/4A0u52052NhlLyv5JICwZ+hKXNcyjgvPejm7yB/I=; b=g8gQIPzfZ2LG3gjucY5ZIcN2ya279MiioMJ7uCceoo9vkrRICzK31sZf9y8ntZ/OSZ zhNbSxIWHh/6mLmeJ3TGYLeWaMuJcyFXU4X67ktDHsEMcoPGvxH3OT6tsXf+Mv+yzcoc 1mE2TNZgL4GXuqrwWSr6hHya2FaGxdYrQzaZU= 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=s2/4A0u52052NhlLyv5JICwZ+hKXNcyjgvPejm7yB/I=; b=SkSDUaxylMj9F53LyN9Zp80uI7q/fs5jbtXlZfqLy3EdXYQSdrZiSI20KZZZ+33m+w +Not7M/+wR9ykDwJatHVLRInAP7vH+FMxuF5lU/WrLdDvCcC8tLyosXpXiG4f0Vdnyhs r0olmlOOyoiw1PXaUE1luYEapO/BnAKfRHiyzlIISIo/IVPyjHP46LBoyKiGv1ZZ3p6D WXchcBN2vYnoQMqB0IDDIJlStwS3HHGGXkW6SxxeGszBZHpDtJAlDHUxjcq6A3Wzdo2h m6/d/SGctWjvFkaShr3/MqTjrHt3L9GdFfCWWx2NKwCap+4w5ARazHG2F/jtgK1sQ7Zy kbOw== X-Gm-Message-State: AOAM530Fie4NssdRX1wKgEfF9ae00vuHcPX8I3wvvoC/UzHcU3++ywDl Agu3FrNqxN7fzl+Iecuy2rtvNFrh0i8VHtoqe/Q= X-Received: by 2002:a05:6512:1398:b0:445:bcef:e4fd with SMTP id p24-20020a056512139800b00445bcefe4fdmr3244876lfa.398.1646514560940; Sat, 05 Mar 2022 13:09:20 -0800 (PST) Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com. [209.85.208.174]) by smtp.gmail.com with ESMTPSA id s10-20020a19ad4a000000b0044826a25a2esm392550lfd.292.2022.03.05.13.09.17 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 05 Mar 2022 13:09:19 -0800 (PST) Received: by mail-lj1-f174.google.com with SMTP id u7so15329707ljk.13 for ; Sat, 05 Mar 2022 13:09:17 -0800 (PST) X-Received: by 2002:a2e:80c6:0:b0:246:3334:9778 with SMTP id r6-20020a2e80c6000000b0024633349778mr2967024ljg.443.1646514557473; Sat, 05 Mar 2022 13:09:17 -0800 (PST) MIME-Version: 1.0 References: <20220304025109.15501-1-xiam0nd.tong@gmail.com> In-Reply-To: <20220304025109.15501-1-xiam0nd.tong@gmail.com> From: Linus Torvalds Date: Sat, 5 Mar 2022 13:09:01 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/6] list: add new MACROs to make iterator invisiable outside the loop To: Xiaomeng Tong Cc: Arnd Bergmann , Greg Kroah-Hartman , Jakob Koschel , Jann Horn , Kees Cook , Linux Kbuild mailing list , Linux Kernel Mailing List , Linux-MM , Netdev 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-kernel@vger.kernel.org On Thu, Mar 3, 2022 at 6:51 PM Xiaomeng Tong wrote: > > > - it means that the already *good* cases are the ones that are > > penalized by having to change > > Yes, but it also kills potential risks that one day somebody mistakely > uses iterator after the loop in this already *good* cases, as it removed > the original declare of pos and any use-after-loop will be catched by > compiler. The thing is, I think we already have a solution to that case. I think it's the bad "entry used outside" that we need to care about doing well. > 3. restore all name back to list_for_each_entry after everything is done: > (minus 2 chars) You are ignoring the big elephant in the room - counting the small things, but not counting the BIG thing. That type name argument is long. Right now we avoid it by pre-declaring it, and that's in many ways the natural thing to do in C (you don't declare types in the place that uses them, you declare the types in the variable declarations above the code). Now, I'd love for the list head entry itself to "declare the type", and solve it that way. That would in many ways be the optimal situation, in that when a structure has that struct list_head xyz; entry, it would be lovely to declare *there* what the list entry type is - and have 'list_for_each_entry()' just pick it up that way. It would be doable in theory - with some preprocessor trickery all the 'struct list_head' things *could* be made to be unnamed unions of the list head, and the actual type it points to, ie something like #define declare_list_head(type,type) union { struct list_head x; type *x##_list_type; } and then (to pick one particular example), we could make the "struct task_struct" entry for children be - struct list_head children; + declare_list_head(struct task_struct, children); and now when you use list_for_each_entry(p, &father->children, sibling) { you could actually pick out the type with some really ugly preprocessor crud, by doing 'typeof(*head##_list_type)' to get the type of the thing we iterate over. So we *could* embed the type that a list head points to with tricks like that. The it would actually be type-safe, and not need a declaration of the type anywhere. And it would be kind of nice to document "this is a list head pointer to this kind of type". And yes, it would be even better if we could also encode the member name that contains the list entries somehow (ie in this case the 'sibling' list entry of the task struct) so that you'd really document the full chain. But even my twisted mind cannot come up with any tricks to do *that*. But the above would be quite a *major* change. And the above kind of preprocessor trickery and encoding a secondary type as a union entry that isn't actually used for anythign else may be too ugly to live anyway. Linus