Message ID | CAPM=9tw62EZfAm0PbiOPmMrpfR98QMFTWGEQcA34G4ap4xxNkA@mail.gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | [git,pull] drm for 5.19-rc1 | expand |
On Tue, May 24, 2022 at 11:07 PM Dave Airlie <airlied@gmail.com> wrote: > > AMD has started some new GPU support [...] Oh Christ. Which means another set of auto-generated monster headers. Lovely. Linus
The pull request you sent on Wed, 25 May 2022 16:06:58 +1000:
> git://anongit.freedesktop.org/drm/drm tags/drm-next-2022-05-25
has been merged into torvalds/linux.git:
https://git.kernel.org/torvalds/c/2518f226c60d8e04d18ba4295500a5b0b8ac7659
Thank you!
Hi Dave.
On Wed, May 25, 2022 at 4:49 PM Dave Airlie <airlied@gmail.com> wrote:
> drm/amdgpu: add nbio v7_7_0 ip headers
These header files are heavy users of large constants lacking the "U"
suffix e.g.:
#define NB_ADAPTER_ID__SUBSYSTEM_ID_MASK 0xFFFF0000L
Assigning this to unsigned long on 32-bit will trigger a signed integer
overflow, which is technically UB, and causes "error: initializer
element is not constant" warnings with gcc-5 and -std-gnu11, cfr. [1]
While gcc-5 is old, the fact that this is UB will probably start to
bite us one day...
[1] https://lore.kernel.org/r/CAK8P3a0QrihBR_2FQ7uZ5w2JmLjv7czfrrarCMmJOhvNdJ3p9g@mail.gmail.com
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
On Jun 07 2022, Geert Uytterhoeven wrote: > On Wed, May 25, 2022 at 4:49 PM Dave Airlie <airlied@gmail.com> wrote: >> drm/amdgpu: add nbio v7_7_0 ip headers > > These header files are heavy users of large constants lacking the "U" > suffix e.g.: > > #define NB_ADAPTER_ID__SUBSYSTEM_ID_MASK 0xFFFF0000L > > Assigning this to unsigned long on 32-bit will trigger a signed integer > overflow, which is technically UB, and causes "error: initializer > element is not constant" warnings with gcc-5 and -std-gnu11, cfr. [1] That shouldn't happen here, as hexadecimal constants will be of unsigned type if they don't fit in the corresponding signed type.
On Tue, Jun 7, 2022 at 3:23 AM Geert Uytterhoeven <geert@linux-m68k.org> wrote: > > These header files are heavy users of large constants lacking the "U" > suffix e.g.: > > #define NB_ADAPTER_ID__SUBSYSTEM_ID_MASK 0xFFFF0000L As Andreas says, this is not undefined behavior. A hexadecimal integer constant will always get a type that fits the actual value. So on a 32-bit architecture, because 0xFFFF0000 doesn't fit in 'long', it will automatically become 'unsigned long'. Now, a C compiler might still warn about such implicit type conversions, but I'd be a bit surprised if any version of gcc actually would do that, because this behavior for hex constants is *very* traditional, and very common. It's also true that the type of the constant - but not the value - will be different on 32-bit and 64-bit architectures (ie on 64-bit, it will be plain "long" and never extended to "unsigned long", because the hex value obviously fits just fine). I don't see any normal situation where that really matters, since any normal use will have the same result. The case you point to at https://lore.kernel.org/r/CAK8P3a0QrihBR_2FQ7uZ5w2JmLjv7czfrrarCMmJOhvNdJ3p9g@mail.gmail.com is very different, because the constant "1" is always just a plain signed "int". So when you do "(1 << 31)", that is now a signed integer with the top bit set, and so it will have an actual negative value, and that can cause various problems (when right-shifted, or when compared to other values). But hexadecimal constants can be signed types, but they never have negative values. Linus
Hi Linus, On Tue, Jun 7, 2022 at 8:15 PM Linus Torvalds <torvalds@linux-foundation.org> wrote: > On Tue, Jun 7, 2022 at 3:23 AM Geert Uytterhoeven <geert@linux-m68k.org> wrote: > > These header files are heavy users of large constants lacking the "U" > > suffix e.g.: > > > > #define NB_ADAPTER_ID__SUBSYSTEM_ID_MASK 0xFFFF0000L > > As Andreas says, this is not undefined behavior. > > A hexadecimal integer constant will always get a type that fits the > actual value. So on a 32-bit architecture, because 0xFFFF0000 doesn't > fit in 'long', it will automatically become 'unsigned long'. > > Now, a C compiler might still warn about such implicit type > conversions, but I'd be a bit surprised if any version of gcc actually > would do that, because this behavior for hex constants is *very* > traditional, and very common. > > It's also true that the type of the constant - but not the value - > will be different on 32-bit and 64-bit architectures (ie on 64-bit, it > will be plain "long" and never extended to "unsigned long", because > the hex value obviously fits just fine). > > I don't see any normal situation where that really matters, since any > normal use will have the same result. > > The case you point to at > > https://lore.kernel.org/r/CAK8P3a0QrihBR_2FQ7uZ5w2JmLjv7czfrrarCMmJOhvNdJ3p9g@mail.gmail.com > > is very different, because the constant "1" is always just a plain > signed "int". So when you do "(1 << 31)", that is now a signed integer > with the top bit set, and so it will have an actual negative value, > and that can cause various problems (when right-shifted, or when > compared to other values). > > But hexadecimal constants can be signed types, but they never have > negative values. Thank you, I stand corrected. Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds