2024-04-16 09:33:13

by Naresh Kamboju

[permalink] [raw]
Subject: powerpc: io-defs.h:43:1: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic]

The Powerpc clang builds failed due to following warnings / errors on the
Linux next-20240416 tag.

Powerpc:
- tqm8xx_defconfig + clang-17 - Failed
- allnoconfig + clang-17 - Failed
- tinyconfig + clang-17 - Failed

Reported-by: Linux Kernel Functional Testing <[email protected]>

Build log:
--------
In file included from arch/powerpc/kernel/ptrace/ptrace.c:19:
In file included from include/linux/syscalls.h:93:
In file included from include/trace/syscall.h:7:
In file included from include/linux/trace_events.h:9:
In file included from include/linux/hardirq.h:11:
In file included from arch/powerpc/include/asm/hardirq.h:6:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:14:
In file included from arch/powerpc/include/asm/io.h:672:
arch/powerpc/include/asm/io-defs.h:43:1: error: performing pointer
arithmetic on a null pointer has undefined behavior
[-Werror,-Wnull-pointer-arithmetic]
43 | DEF_PCI_AC_NORET(insb, (unsigned long p, void *b, unsigned long c),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44 | (p, b, c), pio, p)
| ~~~~~~~~~~~~~~~~~~

Links:
- https://qa-reports.linaro.org/lkft/linux-next-master/build/next-20240416/testrun/23495171/suite/build/test/clang-17-tqm8xx_defconfig/details/
- https://storage.tuxsuite.com/public/linaro/lkft/builds/2fAvI3mKJ0dTHcazPrLl2zNF9JO/

--
Linaro LKFT
https://lkft.linaro.org


2024-04-16 11:02:14

by Arnd Bergmann

[permalink] [raw]
Subject: Re: powerpc: io-defs.h:43:1: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic]

On Tue, Apr 16, 2024, at 11:32, Naresh Kamboju wrote:
> The Powerpc clang builds failed due to following warnings / errors on the
> Linux next-20240416 tag.
>
> Powerpc:
> - tqm8xx_defconfig + clang-17 - Failed
> - allnoconfig + clang-17 - Failed
> - tinyconfig + clang-17 - Failed
>
> Reported-by: Linux Kernel Functional Testing <[email protected]>

I'm not sure why this showed up now, but there is a series from
in progress that will avoid this in the future, as the same
issue is present on a couple of other architectures.

The broken definitions are in the !CONFIG_PCI path of

#ifndef CONFIG_PCI
#define _IO_BASE 0
#define _ISA_MEM_BASE 0
#define PCI_DRAM_OFFSET 0
#elif defined(CONFIG_PPC32)
#define _IO_BASE isa_io_base
#define _ISA_MEM_BASE isa_mem_base
#define PCI_DRAM_OFFSET pci_dram_offset
#else
#define _IO_BASE pci_io_base
#define _ISA_MEM_BASE isa_mem_base
#define PCI_DRAM_OFFSET 0
#endif

Once the series is merged, the !PCI case can disable
CONFIG_HAS_IOPORT and move all references to it into #ifdef
sections, something like the (incomplete) patch below.

It looks like regardless of this, powerpc can also just set
_IO_BASE to ISA_IO_BASE unconditionally, but I could be missing
something there.

Arnd

---
diff --git a/arch/powerpc/include/asm/io.h b/arch/powerpc/include/asm/io.h
index 08c550ed49be..29e002b9316c 100644
--- a/arch/powerpc/include/asm/io.h
+++ b/arch/powerpc/include/asm/io.h
@@ -36,11 +36,8 @@ extern struct pci_dev *isa_bridge_pcidev;
* bases. Most of this file only uses _IO_BASE though which we
* define properly based on the platform
*/
-#ifndef CONFIG_PCI
-#define _IO_BASE 0
-#define _ISA_MEM_BASE 0
-#define PCI_DRAM_OFFSET 0
-#elif defined(CONFIG_PPC32)
+#ifdef CONFIG_HAS_IOPORT
+#ifdef CONFIG_PPC32
#define _IO_BASE isa_io_base
#define _ISA_MEM_BASE isa_mem_base
#define PCI_DRAM_OFFSET pci_dram_offset
@@ -486,8 +483,7 @@ static inline u64 __raw_rm_readq(volatile void __iomem *paddr)
* to port it over
*/

-#ifdef CONFIG_PPC32
-
+#if defined(CONFIG_PPC32) && defined(CONFIG_HAS_IOPORT)
#define __do_in_asm(name, op) \
static inline unsigned int name(unsigned int port) \
{ \
@@ -534,7 +530,7 @@ __do_out_asm(_rec_outb, "stbx")
__do_out_asm(_rec_outw, "sthbrx")
__do_out_asm(_rec_outl, "stwbrx")

-#endif /* CONFIG_PPC32 */
+#endif /* CONFIG_PPC32 && CONFIG_HAS_IOPORT */

/* The "__do_*" operations below provide the actual "base" implementation
* for each of the defined accessors. Some of them use the out_* functions
@@ -577,6 +573,7 @@ __do_out_asm(_rec_outl, "stwbrx")
#define __do_readq_be(addr) in_be64(PCI_FIX_ADDR(addr))
#endif /* !defined(CONFIG_EEH) */

+#ifdef CONFIG_HAS_IOPORT
#ifdef CONFIG_PPC32
#define __do_outb(val, port) _rec_outb(val, port)
#define __do_outw(val, port) _rec_outw(val, port)
@@ -592,6 +589,7 @@ __do_out_asm(_rec_outl, "stwbrx")
#define __do_inw(port) readw((PCI_IO_ADDR)_IO_BASE + port);
#define __do_inl(port) readl((PCI_IO_ADDR)_IO_BASE + port);
#endif /* !CONFIG_PPC32 */
+#endif

#ifdef CONFIG_EEH
#define __do_readsb(a, b, n) eeh_readsb(PCI_FIX_ADDR(a), (b), (n))
@@ -606,12 +604,14 @@ __do_out_asm(_rec_outl, "stwbrx")
#define __do_writesw(a, b, n) _outsw(PCI_FIX_ADDR(a),(b),(n))
#define __do_writesl(a, b, n) _outsl(PCI_FIX_ADDR(a),(b),(n))

+#ifdef CONFIG_HAS_IOPORT
#define __do_insb(p, b, n) readsb((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
#define __do_insw(p, b, n) readsw((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
#define __do_insl(p, b, n) readsl((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
#define __do_outsb(p, b, n) writesb((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
#define __do_outsw(p, b, n) writesw((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
#define __do_outsl(p, b, n) writesl((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
+#endif

#define __do_memset_io(addr, c, n) \
_memset_io(PCI_FIX_ADDR(addr), c, n)
@@ -689,6 +689,8 @@ static inline void name at \
#define writesb writesb
#define writesw writesw
#define writesl writesl
+
+#ifdef CONFIG_HAS_IOPORT
#define inb inb
#define inw inw
#define inl inl
@@ -701,6 +703,8 @@ static inline void name at \
#define outsb outsb
#define outsw outsw
#define outsl outsl
+#endif
+
#ifdef __powerpc64__
#define readq readq
#define writeq writeq

2024-04-16 11:10:11

by David Laight

[permalink] [raw]
Subject: RE: powerpc: io-defs.h:43:1: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic]

From: Segher Boessenkool
> Sent: 16 April 2024 11:38
>
> On Tue, Apr 16, 2024 at 03:02:52PM +0530, Naresh Kamboju wrote:
> > In file included from arch/powerpc/include/asm/io.h:672:
> > arch/powerpc/include/asm/io-defs.h:43:1: error: performing pointer
> > arithmetic on a null pointer has undefined behavior
> > [-Werror,-Wnull-pointer-arithmetic]
>
> It is not UB, but just undefined: the program is meaningless.
>
> It is not a null pointer but even a null pointer constant here. It
> matters in places, including here.
>
> It would help if the warnings were more correct :-(

Isn't it only a problem because the NULL pointer isn't required to
be the all-zero bit pattern?

So when do we get a warning from using memset() on a structure
that contains pointers? Since it is equally buggy.

Has anyone ever seen a system where NULL wasn't 'all zeros'?
I've used a system where the 'native' invalid pointer was 'all ones',
but even there the C code used 'all zeros'.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


2024-04-16 11:42:28

by Segher Boessenkool

[permalink] [raw]
Subject: Re: powerpc: io-defs.h:43:1: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic]

On Tue, Apr 16, 2024 at 03:02:52PM +0530, Naresh Kamboju wrote:
> In file included from arch/powerpc/include/asm/io.h:672:
> arch/powerpc/include/asm/io-defs.h:43:1: error: performing pointer
> arithmetic on a null pointer has undefined behavior
> [-Werror,-Wnull-pointer-arithmetic]

It is not UB, but just undefined: the program is meaningless.

It is not a null pointer but even a null pointer constant here. It
matters in places, including here.

It would help if the warnings were more correct :-(


Segher

2024-04-16 11:56:35

by Arnd Bergmann

[permalink] [raw]
Subject: Re: powerpc: io-defs.h:43:1: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic]

On Tue, Apr 16, 2024, at 13:01, Arnd Bergmann wrote:
> On Tue, Apr 16, 2024, at 11:32, Naresh Kamboju wrote:
>> The Powerpc clang builds failed due to following warnings / errors on the
>> Linux next-20240416 tag.
>>
>> Powerpc:
>> - tqm8xx_defconfig + clang-17 - Failed
>> - allnoconfig + clang-17 - Failed
>> - tinyconfig + clang-17 - Failed
>>
>> Reported-by: Linux Kernel Functional Testing <[email protected]>
>
> I'm not sure why this showed up now, but there is a series from
> in progress that will avoid this in the future, as the same
> issue is present on a couple of other architectures.
>

I see now, it was introduced by my patch to turn on -Wextra
by default. I had tested that patch on all architectures
with allmodconfig and defconfig, but I did not test any
powerpc configs with PCI disabled.

Arnd

2024-04-16 12:42:38

by Dan Carpenter

[permalink] [raw]
Subject: Re: powerpc: io-defs.h:43:1: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic]

On Tue, Apr 16, 2024 at 01:55:57PM +0200, Arnd Bergmann wrote:
> On Tue, Apr 16, 2024, at 13:01, Arnd Bergmann wrote:
> > On Tue, Apr 16, 2024, at 11:32, Naresh Kamboju wrote:
> >> The Powerpc clang builds failed due to following warnings / errors on the
> >> Linux next-20240416 tag.
> >>
> >> Powerpc:
> >> - tqm8xx_defconfig + clang-17 - Failed
> >> - allnoconfig + clang-17 - Failed
> >> - tinyconfig + clang-17 - Failed
> >>
> >> Reported-by: Linux Kernel Functional Testing <[email protected]>
> >
> > I'm not sure why this showed up now, but there is a series from
> > in progress that will avoid this in the future, as the same
> > issue is present on a couple of other architectures.
> >
>
> I see now, it was introduced by my patch to turn on -Wextra
> by default. I had tested that patch on all architectures
> with allmodconfig and defconfig, but I did not test any
> powerpc configs with PCI disabled.

I think this warning is clang specific as well... (Maybe clang was
included in all architectures but I'm not sure).

regards,
dan carpenter


2024-04-16 12:45:01

by Arnd Bergmann

[permalink] [raw]
Subject: Re: powerpc: io-defs.h:43:1: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic]

On Tue, Apr 16, 2024, at 14:42, Dan Carpenter wrote:
> On Tue, Apr 16, 2024 at 01:55:57PM +0200, Arnd Bergmann wrote:
>> On Tue, Apr 16, 2024, at 13:01, Arnd Bergmann wrote:
>> > On Tue, Apr 16, 2024, at 11:32, Naresh Kamboju wrote:
>> >> The Powerpc clang builds failed due to following warnings / errors on the
>> >> Linux next-20240416 tag.
>> >>
>> >> Powerpc:
>> >> - tqm8xx_defconfig + clang-17 - Failed
>> >> - allnoconfig + clang-17 - Failed
>> >> - tinyconfig + clang-17 - Failed
>> >>
>> >> Reported-by: Linux Kernel Functional Testing <[email protected]>
>> >
>> > I'm not sure why this showed up now, but there is a series from
>> > in progress that will avoid this in the future, as the same
>> > issue is present on a couple of other architectures.
>> >
>>
>> I see now, it was introduced by my patch to turn on -Wextra
>> by default. I had tested that patch on all architectures
>> with allmodconfig and defconfig, but I did not test any
>> powerpc configs with PCI disabled.
>
> I think this warning is clang specific as well... (Maybe clang was
> included in all architectures but I'm not sure).

Yes, I did test with both gcc and clang where supported.

Arnd