linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Jakub Kicinski <kuba@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Bart Van Assche <bvanassche@acm.org>,
	linux-mm@kvack.org,  Ivan Shapovalov <intelfx@intelfx.name>,
	Vlastimil Babka <vbabka@suse.cz>,
	 David Laight <david.laight@aculab.com>,
	Nathan Chancellor <nathan@kernel.org>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	David Rientjes <rientjes@google.com>,
	 David Hildenbrand <david@redhat.com>,
	Kaiyang Zhao <kaiyang2@cs.cmu.edu>,
	 Joel Granados <joel.granados@kernel.org>,
	Sourav Panda <souravpanda@google.com>,
	 Johannes Weiner <hannes@cmpxchg.org>,
	llvm@lists.linux.dev
Subject: Re: [PATCH] mm: Fix clang W=1 compiler warnings
Date: Fri, 7 Feb 2025 19:11:08 -0800	[thread overview]
Message-ID: <CAHk-=wjMux0w49bTdSbC3DOoc9FRctDrRvaqFUS4KFTmkbtKWg@mail.gmail.com> (raw)
In-Reply-To: <20250207173813.5081ba76@kernel.org>

On Fri, 7 Feb 2025 at 17:38, Jakub Kicinski <kuba@kernel.org> wrote:
>
> ../include/linux/vmstat.h:504:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

Ok.

So my gut feel is that this warning is simply bogus. We have
situations where we very intentionally use enumerations instead of a
list of #define constants.

That NR_VM_ZONE_STAT_ITEMS case really *does* look like it's a great
example of how code that doesn't care about the actual numbers, and
just wants the compiler to generate a sequence for us is supposed to
look.

Because sometimes we do enums just because it's a nice way to get
automatically incrementing values when we don't care exactly what the
values are.

And sometimes we do it for actual namespace reasons - see for example
commit 1a251f52cfdc ("minmax: make generic MIN() and MAX() macros
available everywhere") where in order to avoid clashes with the
"MIN()" macro, I made a couple of drivers that had a 'MIN' value (much
too generic a name, but it was what it was) use an enum instead.

See

   git show 1a251f52cfdc drivers/hwmon/adt7475.c

for details.

Now, at least in that case I don't think there was any arithmetic with
those values, so clang-19 wouldn't complain about it either, but I
mention that commit as a case of "it's actually very valid to use an
enum for actual real honest-to-goodness integer value enumeration".

Which really makes me feel like the new clang warning is seriously misguided.

In sparse I actually wanted to have integer types that don't silently
cast to other integer types. So sparse knows the concept of "bitwise"
and "nocast" attributes.  The "nocast" thing doesn't work well in
practice, but "bitwise" was a huge success and is how a lot of our
endianness problems were solved.

So the *concept* of an enum - or any other integer type - that "stays
in the type" is good, but I think it's a major mistake to think they
have to do so without any sane escape, and to only limit it to enums.

That said, we may not have *many* of those enum cases in the kernel
(and we do tend to have a history of using long series of '#define'
sequences to do these constants), so maybe the warning is acceptable
if it's a case of "this is literally the *only* one in the kernel".

But not having clang-19, I can't see if this is a case of "this is so
rare that let's just avoid it", or "this is the case that causes the
most noise for every file build, but there are lots of other cases of
this".

                  Linus


  parent reply	other threads:[~2025-02-08  3:11 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-31 19:12 Bart Van Assche
2025-02-03 13:32 ` Vlastimil Babka
2025-02-08  0:49 ` Jakub Kicinski
2025-02-08  1:01   ` Linus Torvalds
2025-02-08  1:38     ` Jakub Kicinski
2025-02-08  2:18       ` Nathan Chancellor
2025-02-08  3:11       ` Linus Torvalds [this message]
2025-02-08  3:33         ` Nathan Chancellor
2025-02-08  3:49           ` Linus Torvalds
2025-02-08  4:24             ` Linus Torvalds
2025-02-10 18:33               ` Jakub Kicinski
2025-02-08  2:55     ` Bart Van Assche
2025-02-08  3:22       ` Linus Torvalds
2025-02-08  3:30         ` Bart Van Assche
2025-02-08 10:28 ` Matthew Wilcox
2025-02-11 14:34   ` Vlastimil Babka
2025-02-11 18:48     ` Bart Van Assche
2025-02-11 19:25       ` Matthew Wilcox

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='CAHk-=wjMux0w49bTdSbC3DOoc9FRctDrRvaqFUS4KFTmkbtKWg@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=bvanassche@acm.org \
    --cc=david.laight@aculab.com \
    --cc=david@redhat.com \
    --cc=hannes@cmpxchg.org \
    --cc=intelfx@intelfx.name \
    --cc=joel.granados@kernel.org \
    --cc=kaiyang2@cs.cmu.edu \
    --cc=kuba@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=llvm@lists.linux.dev \
    --cc=nathan@kernel.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=rientjes@google.com \
    --cc=souravpanda@google.com \
    --cc=vbabka@suse.cz \
    /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