linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Thompson <daniel.thompson@linaro.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: "Xiaomeng Tong" <xiam0nd.tong@gmail.com>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Jakob Koschel" <jakobkoschel@gmail.com>,
	"Jann Horn" <jannh@google.com>,
	"Kees Cook" <keescook@chromium.org>,
	"Linux Kbuild mailing list" <linux-kbuild@vger.kernel.org>,
	"Linux Kernel Mailing List" <linux-kernel@vger.kernel.org>,
	Linux-MM <linux-mm@kvack.org>, Netdev <netdev@vger.kernel.org>,
	"Barnabás Pőcze" <pobrn@protonmail.com>
Subject: Re: [PATCH 2/6] list: add new MACROs to make iterator invisiable outside the loop
Date: Wed, 16 Mar 2022 15:45:44 +0000	[thread overview]
Message-ID: <20220316154544.bfewwi7zseyyja47@maple.lan> (raw)
In-Reply-To: <CAHk-=wi58pvQhMX2sRt7nKqwHAFAmn27MrJg3XbeJgio6ONgdA@mail.gmail.com>

On Fri, Mar 11, 2022 at 10:41:06AM -0800, Linus Torvalds wrote:
> On Fri, Mar 11, 2022 at 6:27 AM Daniel Thompson
> <daniel.thompson@linaro.org> wrote:
> >
> > It is possible simply to use spelling to help uncover errors in
> > list_traverse()?
> 
> I'd love to, and thought that would be a lovely idea, but in another
> thread ("") Barnabás Pőcze pointed out that we actually have a fair
> number of cases where the list member entries are embedded in internal
> structures and have a '.' in them:
> 
>   https://lore.kernel.org/all/wKlkWvCGvBrBjshT6gHT23JY9kWImhFPmTKfZWtN5Bkv_OtIFHTy7thr5SAEL6sYDthMDth-rvFETX-gCZPPCb9t2bO1zilj0Q-OTTSbe00=@protonmail.com/
> 
> which means that you can't actually append the target_member name
> except in the simplest cases, because it wouldn't result in one single
> identifier.
> 
> Otherwise it would be a lovely idea.

When I prototyped this I did actually include a backdoor to cover
situations like this but I ended up (incorrectly at appears) editing it
out for simplicity.

Basically the union is free so we can have more than one type * member:

#define list_traversal_head(type, name, target_member) \
       union { \
               struct list_head name; \
               type *name##_traversal_type; \
               type *name##_traversal_mismatch_##target_member; \
       }

This allows that the single structure cases to be checked whilst nested
structures (and array which I noticed also crop up) have a trap door such
as list_traverse_unchecked().

I did a quick grep to estimate how many nested/array cases there are and
came up with around 2.5% (roughly ~200 in ~8500, counting only the single
line users of list_for_each_entry() ).

As you say, lovely idea but having to use special API 2.5% of the time
seems a bit on the high side.

BTW, a complete aside, but whilst I was looking for trouble I also
spotted code where the list head is an array which means we are not able
to lookup the travesral type correctly:
list_for_each_entry(modes[i], &connector->modes, head)
However I found only one instance of this so it
much more acceptable rate of special cases than the 2.5% above.


> > > [this bit used to quote the definition of LIST_HEAD() ;-) ]
> > For architectures without HAVE_LD_DEAD_CODE_DATA_ELIMINATION then the
> > "obvious" extension of list_traversal_head() ends up occupying bss
> > space. Even replacing the pointer with a zero length array is still
> > provoking gcc-11 (arm64) to allocate a byte from bss (often with a lot
> > of padding added).
> 
> I think compilers give objects at least one byte of space, so that two
> different objects get different addresses, and don't compare equal.
> 
> That said, I'm not seeing your issue. list_traversal_head() is a
> union, and always has that 'struct list_head' in it, and that's the
> biggest part of the union.

Perhaps its a bit overblown for the safe of a few kilobytes (even if
there were two traversal types members) but I was wondering if there is
any cunning trick for LIST_HEAD() since we cannot have an anonymous
union outside a struct. In short, is this the best we can do for
LIST_TRAVERSE_HEAD():

#define LIST_TRAVERSE_HEAD(type, name, target_member) \
	type * name##_traversal_type; \
	struct list_head name = LIST_HEAD_INIT(name)


#define STATIC_LIST_TRAVERSE_HEAD(type, name, target_member) \
	static type * name##_traversal_type; \
	static list_head name = LIST_HEAD_INIT(name)


Daniel.


  reply	other threads:[~2022-03-16 15:45 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-01  7:58 [PATCH 0/6] list_for_each_entry*: " Xiaomeng Tong
2022-03-01  7:58 ` [PATCH 1/6] Kbuild: compile kernel with gnu11 std Xiaomeng Tong
2022-03-01 17:59   ` kernel test robot
2022-03-01 20:16     ` Linus Torvalds
2022-03-01 20:54       ` Arnd Bergmann
2022-03-01 21:04         ` Linus Torvalds
2022-03-01 21:15           ` Linus Torvalds
2022-03-01 21:43             ` Xiaomeng Tong
2022-03-01  7:58 ` [PATCH 2/6] list: add new MACROs to make iterator invisiable outside the loop Xiaomeng Tong
2022-03-02  2:52   ` kernel test robot
2022-03-02 13:02   ` James Bottomley
2022-03-03  3:31     ` Xiaomeng Tong
2022-03-06 14:33       ` James Bottomley
2022-03-03 20:02   ` Linus Torvalds
2022-03-04  2:51     ` Xiaomeng Tong
2022-03-05 21:09       ` Linus Torvalds
2022-03-06  0:35         ` Linus Torvalds
2022-03-06 12:19           ` Jakob Koschel
2022-03-06 18:57             ` Linus Torvalds
2022-03-06 14:06           ` Xiaomeng Tong
2022-03-10 23:54           ` [PATCH 2/6] list: add new MACROs to make iterator invisiable Michał Mirosław
2022-03-11  0:46             ` Linus Torvalds
2022-03-12 10:24               ` Michał Mirosław
2022-03-12 21:43                 ` Linus Torvalds
2022-03-11  7:15           ` [RFC PATCH] list: test: Add a test for list_traverse David Gow
2022-03-11 14:27           ` [PATCH 2/6] list: add new MACROs to make iterator invisiable outside the loop Daniel Thompson
2022-03-11 18:41             ` Linus Torvalds
2022-03-16 15:45               ` Daniel Thompson [this message]
2022-03-01  7:58 ` [PATCH 3/6] kernel: remove iterator use " Xiaomeng Tong
2022-03-01 10:41   ` Greg KH
2022-03-01 11:34     ` Xiaomeng Tong
2022-03-01 11:48       ` Xiaomeng Tong
2022-03-01  7:58 ` [PATCH 4/6] mm: " Xiaomeng Tong
2022-03-01 12:19   ` Xiaomeng Tong
2022-03-01  7:58 ` [PATCH 5/6] net/core: " Xiaomeng Tong
2022-03-01 12:23   ` Xiaomeng Tong
2022-03-01  7:58 ` [PATCH 6/6] drivers/dma: " Xiaomeng Tong
2022-03-01 12:25   ` Xiaomeng Tong

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=20220316154544.bfewwi7zseyyja47@maple.lan \
    --to=daniel.thompson@linaro.org \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=jakobkoschel@gmail.com \
    --cc=jannh@google.com \
    --cc=keescook@chromium.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=netdev@vger.kernel.org \
    --cc=pobrn@protonmail.com \
    --cc=torvalds@linux-foundation.org \
    --cc=xiam0nd.tong@gmail.com \
    /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