linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xiaomeng Tong <xiam0nd.tong@gmail.com>
To: torvalds@linux-foundation.org
Cc: akpm@linux-foundation.org, alsa-devel@alsa-project.org,
	amd-gfx@lists.freedesktop.org, andriy.shevchenko@linux.intel.com,
	arnd@arndb.de, bcm-kernel-feedback-list@broadcom.com,
	bjohannesmeyer@gmail.com, c.giuffrida@vu.nl,
	christian.koenig@amd.com, christophe.jaillet@wanadoo.fr,
	dan.carpenter@oracle.com, dmaengine@vger.kernel.org,
	drbd-dev@lists.linbit.com, dri-devel@lists.freedesktop.org,
	gustavo@embeddedor.com, h.j.bos@vu.nl,
	intel-gfx@lists.freedesktop.org,
	intel-wired-lan@lists.osuosl.org, jakobkoschel@gmail.com,
	jgg@ziepe.ca, keescook@chromium.org,
	kgdb-bugreport@lists.sourceforge.net, kvm@vger.kernel.org,
	linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-aspeed@lists.ozlabs.org, linux-block@vger.kernel.org,
	linux-cifs@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net,
	linux-fsdevel@vger.kernel.org, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
	linux-mediatek@lists.infradead.org, linux-pm@vger.kernel.org,
	linux-rdma@vger.kernel.org, linux-scsi@vger.kernel.org,
	linux-sgx@vger.kernel.org, linux-staging@lists.linux.dev,
	linux-tegra@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-wireless@vger.kernel.org,
	linux1394-devel@lists.sourceforge.net, linux@rasmusvillemoes.dk,
	linuxppc-dev@lists.ozlabs.org, nathan@kernel.org,
	netdev@vger.kernel.org, nouveau@lists.freedesktop.org,
	rppt@kernel.org, samba-technical@lists.samba.org,
	tglx@linutronix.de, tipc-discussion@lists.sourceforge.net,
	v9fs-developer@lists.sourceforge.net
Subject: Re: [PATCH 2/6] treewide: remove using list iterator after loop body as a ptr
Date: Wed,  2 Mar 2022 17:31:06 +0800	[thread overview]
Message-ID: <20220302093106.8402-1-xiam0nd.tong@gmail.com> (raw)
In-Reply-To: <CAHk-=whLK11HyvpUtEftOjc3Gup2V77KpAQ2fycj3uai=qceHw@mail.gmail.com>

On Mon, 28 Feb 2022 16:41:04 -0800, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> But basically to _me_, the important part is that the end result is
> maintainable longer-term.

I couldn't agree more. And because of that, I stick with the following
approach because it's maintainable longer-term than "type(pos) pos" one:
 Implements a new macro for each list_for_each_entry* with _inside suffix.
  #define list_for_each_entry_inside(pos, type, head, member)

I have posted a patch series here to demonstrate this approach:
https://lore.kernel.org/lkml/20220301075839.4156-3-xiam0nd.tong@gmail.com/

Although we need replace all the use of list_for_each_entry* (15000+)
with list_for_each_entry*_inside, the work can be done gradually rather
than all at once. We can incrementally replace these callers until
all these in the kernel are completely updated with *_inside* one. At
that time, we can just remove the implements of origin macros and rename
the *_inside* macro back to the origin name just in one single patch.

And the "type(pos) pos" approach need teach developers to "not initialize
the iterator variable, otherwise the use-after-loop will not be reported by
compiler", which is unreasonable and impossible for all developers. 

And it will mess up the following code logic and no warnning reported by
compiler, even without initializing "ext" at the beginning:
void foo(struct mem_extent *arg) {
  struct mem_extent *ext;  // used both for iterator and normal ptr
  ...
  ext = arg;  // this assignment can alse be done in another bar() func
  ...
  list_for_each_entry(ext, head, member) {
    if (found(ext))
       break;
  }
  ...
  // use ext after the loop
  ret = ext;
}
If the loop hit the break, the last "ret" will be the found ext iterator.
However, if the "type(pos) pos" approach applied, the last "ret" will be
"arg" which is not the intention of the developers, because the "ext" is
two different variables inside and outside the loop.

Thus, my idea is *better a finger off than always aching*, let's choose
the "list_for_each_entry_inside(pos, type, head, member)" approach.

> 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.

we can pass a shorter type name to list_for_each_entry_inside, thus no
need to split it over two lines. Actually it is not a big problem.
+ #define t struct sram_bank_info
- list_for_each_entry(pos, head, member) {
+ list_for_each_entry_inside(pos, t, head, member) {

I put the type at the second argument not the first to avoid messing up
the pattern match in some coccinelle scripts.

>  (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

sometimes developers can be confused by the reported warnning:
"used without having been initialized", and can not figure out immediately
that "oh, now i am using another different variable but with the same name
of the loop iterator variable", which has changed the programming habits
of developers.

>  (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

It will lead to a wrong/NULL pointer dereference if the pointer is used
anywhere else, depend on which value is used to initialized with.

Best regard,
--
Xiaomeng Tong

  parent reply	other threads:[~2022-03-02  9:31 UTC|newest]

Thread overview: 85+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-28 11:08 [PATCH 0/6] Remove usage of list iterator past the loop body Jakob Koschel
2022-02-28 11:08 ` [PATCH 1/6] drivers: usb: remove " Jakob Koschel
2022-02-28 11:24   ` Dan Carpenter
2022-02-28 12:03     ` Jakob Koschel
2022-02-28 13:18       ` Dan Carpenter
2022-02-28 18:20     ` Joe Perches
2022-03-01  5:52       ` Dan Carpenter
2022-02-28 11:08 ` [PATCH 2/6] treewide: remove using list iterator after loop body as a ptr Jakob Koschel
2022-02-28 11:20   ` Greg KH
2022-02-28 12:06     ` Jakob Koschel
2022-03-01 17:37       ` Greg KH
2022-02-28 12:19   ` Christian König
2022-02-28 19:56     ` Linus Torvalds
2022-02-28 20:03       ` Linus Torvalds
2022-02-28 20:10         ` Linus Torvalds
2022-02-28 20:14           ` Linus Torvalds
2022-02-28 20:53             ` Segher Boessenkool
2022-02-28 20:16           ` Matthew Wilcox
2022-02-28 20:27             ` Johannes Berg
2022-02-28 20:41               ` Linus Torvalds
2022-02-28 20:37             ` Linus Torvalds
2022-02-28 23:26               ` Matthew Wilcox
2022-03-01  0:45                 ` Linus Torvalds
2022-03-01  0:57                   ` Linus Torvalds
2022-03-01 18:14                   ` Kees Cook
2022-03-01 18:47                     ` Linus Torvalds
2022-03-01 19:01                     ` Matthew Wilcox
2022-03-01  3:03             ` David Laight
2022-02-28 21:47           ` Jakob Koschel
2022-03-01  0:41             ` Linus Torvalds
2022-03-01  6:32               ` Jakub Kicinski
2022-03-01 11:28               ` Jakob Koschel
2022-03-01 17:36                 ` Greg KH
2022-03-01 17:40                   ` Jakob Koschel
2022-03-01 17:58                     ` Greg KH
2022-03-01 18:21                 ` Kees Cook
2022-03-02  9:31               ` Xiaomeng Tong [this message]
2022-03-02 14:04                 ` David Laight
2022-03-03  2:27                   ` Xiaomeng Tong
2022-03-03  4:58                     ` David Laight
2022-03-03  7:26                       ` Xiaomeng Tong
2022-03-03  9:30                         ` David Laight
2022-03-03 12:37                           ` Xiaomeng Tong
2022-03-03 12:18                         ` [Kgdb-bugreport] " Daniel Thompson
2022-03-04  6:59                           ` Xiaomeng Tong
2022-03-03  7:32                       ` Jakob Koschel
2022-03-03  8:30                         ` Xiaomeng Tong
2022-03-03  8:38                           ` Xiaomeng Tong
2022-02-28 20:07       ` Christian König
2022-02-28 20:42         ` James Bottomley
2022-02-28 20:56           ` Christian König
2022-02-28 21:13             ` James Bottomley
2022-03-01  7:03               ` Christian König
2022-02-28 22:05             ` Jakob Koschel
2022-02-28 21:18           ` Jeffrey Walton
2022-02-28 21:59           ` Mike Rapoport
2022-02-28 22:28             ` James Bottomley
2022-02-28 22:50               ` Barnabás Pőcze
2022-03-01  0:30               ` Segher Boessenkool
2022-03-01  0:54                 ` Linus Torvalds
2022-03-01 19:06               ` Linus Torvalds
2022-03-01 19:42                 ` Linus Torvalds
2022-03-01 22:58                 ` David Laight
2022-03-01 23:03                   ` Linus Torvalds
2022-03-01 23:19                     ` David Laight
2022-03-01 23:55                       ` Linus Torvalds
2022-03-02  9:29                         ` Rasmus Villemoes
2022-03-02 20:07                           ` Kees Cook
2022-03-02 20:18                             ` Linus Torvalds
2022-03-02 20:59                               ` Kees Cook
2022-03-03  8:37                             ` Dan Carpenter
2022-03-03 10:56                           ` Dan Carpenter
2022-03-01  2:15       ` David Laight
2022-02-28 13:13   ` Dan Carpenter
2022-02-28 11:08 ` [PATCH 3/6] treewide: fix incorrect use to determine if list is empty Jakob Koschel
2022-02-28 11:38   ` Dan Carpenter
2022-02-28 11:08 ` [PATCH 4/6] drivers: remove unnecessary use of list iterator variable Jakob Koschel
2022-02-28 11:08 ` [PATCH 5/6] treewide: remove dereference of list iterator after loop body Jakob Koschel
2022-02-28 11:08 ` [PATCH 6/6] treewide: remove check of list iterator against head past the " Jakob Koschel
2022-02-28 13:12   ` Dan Carpenter
2022-03-01 20:36   ` Linus Torvalds
2022-03-02 17:14   ` [Intel-gfx] " Tvrtko Ursulin
2022-03-07 15:00 ` [PATCH 0/6] Remove usage of list iterator " Dan Carpenter
2022-03-07 15:26   ` David Laight
2022-03-07 19:15     ` Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220302093106.8402-1-xiam0nd.tong@gmail.com \
    --to=xiam0nd.tong@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alsa-devel@alsa-project.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=bjohannesmeyer@gmail.com \
    --cc=c.giuffrida@vu.nl \
    --cc=christian.koenig@amd.com \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=dan.carpenter@oracle.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=drbd-dev@lists.linbit.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gustavo@embeddedor.com \
    --cc=h.j.bos@vu.nl \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-wired-lan@lists.osuosl.org \
    --cc=jakobkoschel@gmail.com \
    --cc=jgg@ziepe.ca \
    --cc=keescook@chromium.org \
    --cc=kgdb-bugreport@lists.sourceforge.net \
    --cc=kvm@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-aspeed@lists.ozlabs.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=linux-tegra@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=linux1394-devel@lists.sourceforge.net \
    --cc=linux@rasmusvillemoes.dk \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=nathan@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=nouveau@lists.freedesktop.org \
    --cc=rppt@kernel.org \
    --cc=samba-technical@lists.samba.org \
    --cc=tglx@linutronix.de \
    --cc=tipc-discussion@lists.sourceforge.net \
    --cc=torvalds@linux-foundation.org \
    --cc=v9fs-developer@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).