* [PATCH v4 2/3] risc-v: Use generic io.h helpers for nommu
2021-04-30 11:16 [PATCH v4 0/3] asm-generic/io.h: Silence -Wnull-pointer-arithmetic warning on PCI_IOBASE Niklas Schnelle
2021-04-30 11:16 ` [PATCH v4 1/3] sparc: explicitly set PCI_IOBASE to 0 Niklas Schnelle
@ 2021-04-30 11:16 ` Niklas Schnelle
2021-04-30 11:16 ` [PATCH v4 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE Niklas Schnelle
2021-05-03 16:07 ` [PATCH v4 0/3] asm-generic/io.h: Silence -Wnull-pointer-arithmetic warning on PCI_IOBASE Arnd Bergmann
3 siblings, 0 replies; 7+ messages in thread
From: Niklas Schnelle @ 2021-04-30 11:16 UTC (permalink / raw)
To: Arnd Bergmann, Vineet Gupta, David S. Miller
Cc: Nathan Chancellor, Nick Desaulniers, clang-built-linux,
linux-arch, linux-kernel, linux-s390, linux-snps-arc, sparclinux
From: Niklas Schnelle <niklas@komani.de>
Without MMU support PCI_IOBASE is left undefined because PCI_IO_END is
VMEMMAP_START. Nevertheless the in*()/out*() helper macros are left
defined with uses of PCI_IOBASE. At the moment this only compiles
because asm-generic/io.h defines PCI_IOBASE as 0 if it is undefined and
so at macro expansion the macro is defined. This is suprising at the
least and leads to compilation errors when asm-generic/io.h is changed
to leave PCI_IOBASE undefined. Instead only define the in*()/out*()
helper macros with MMU support and fall back to the asm-generic/io.h
helper stubs otherwise.
Signed-off-by: Niklas Schnelle <niklas@komani.de>
---
arch/riscv/include/asm/io.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/arch/riscv/include/asm/io.h b/arch/riscv/include/asm/io.h
index c025a746a148..31a8b98c0f13 100644
--- a/arch/riscv/include/asm/io.h
+++ b/arch/riscv/include/asm/io.h
@@ -23,12 +23,12 @@
#include <asm/mmio.h>
/*
- * I/O port access constants.
+ * I/O port access constants. Without MMU support leave PCI_IOBASE undefined
+ * and fall back to generic stubs for I/O access routines.
*/
#ifdef CONFIG_MMU
#define IO_SPACE_LIMIT (PCI_IO_SIZE - 1)
#define PCI_IOBASE ((void __iomem *)PCI_IO_START)
-#endif /* CONFIG_MMU */
/*
* Emulation routines for the port-mapped IO space used by some PCI drivers.
@@ -145,6 +145,7 @@ __io_writes_outs(writes, u64, q, __io_bw(), __io_aw())
__io_writes_outs(outs, u64, q, __io_pbr(), __io_paw())
#define outsq(addr, buffer, count) __outsq((void __iomem *)addr, buffer, count)
#endif
+#endif /* CONFIG_MMU */
#include <asm-generic/io.h>
--
2.31.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE
2021-04-30 11:16 [PATCH v4 0/3] asm-generic/io.h: Silence -Wnull-pointer-arithmetic warning on PCI_IOBASE Niklas Schnelle
2021-04-30 11:16 ` [PATCH v4 1/3] sparc: explicitly set PCI_IOBASE to 0 Niklas Schnelle
2021-04-30 11:16 ` [PATCH v4 2/3] risc-v: Use generic io.h helpers for nommu Niklas Schnelle
@ 2021-04-30 11:16 ` Niklas Schnelle
2021-05-03 16:07 ` [PATCH v4 0/3] asm-generic/io.h: Silence -Wnull-pointer-arithmetic warning on PCI_IOBASE Arnd Bergmann
3 siblings, 0 replies; 7+ messages in thread
From: Niklas Schnelle @ 2021-04-30 11:16 UTC (permalink / raw)
To: Arnd Bergmann, Vineet Gupta, David S. Miller
Cc: Nathan Chancellor, Nick Desaulniers, clang-built-linux,
linux-arch, linux-kernel, linux-s390, linux-snps-arc, sparclinux
When PCI_IOBASE is not defined, it is set to 0 such that it is ignored
in calls to the readX/writeX primitives. This triggers clang's
-Wnull-pointer-arithmetic warning and will result in illegal accesses on
platforms that do not support I/O ports.
Make things explicit and silence the warning by letting inb() and
friends fail with WARN_ONCE() and a 0xff... return in case PCI_IOBASE is
not defined.
Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
---
include/asm-generic/io.h | 65 +++++++++++++++++++++++++++++++++++++---
1 file changed, 61 insertions(+), 4 deletions(-)
diff --git a/include/asm-generic/io.h b/include/asm-generic/io.h
index c6af40ce03be..6207fb4b9a7d 100644
--- a/include/asm-generic/io.h
+++ b/include/asm-generic/io.h
@@ -8,6 +8,7 @@
#define __ASM_GENERIC_IO_H
#include <asm/page.h> /* I/O is all done through memory accesses */
+#include <linux/bug.h>
#include <linux/string.h> /* for memset() and memcpy() */
#include <linux/types.h>
@@ -440,10 +441,6 @@ static inline void writesq(volatile void __iomem *addr, const void *buffer,
#endif
#endif /* CONFIG_64BIT */
-#ifndef PCI_IOBASE
-#define PCI_IOBASE ((void __iomem *)0)
-#endif
-
#ifndef IO_SPACE_LIMIT
#define IO_SPACE_LIMIT 0xffff
#endif
@@ -458,12 +455,17 @@ static inline void writesq(volatile void __iomem *addr, const void *buffer,
#define _inb _inb
static inline u8 _inb(unsigned long addr)
{
+#ifdef PCI_IOBASE
u8 val;
__io_pbr();
val = __raw_readb(PCI_IOBASE + addr);
__io_par(val);
return val;
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+ return ~0;
+#endif
}
#endif
@@ -471,12 +473,17 @@ static inline u8 _inb(unsigned long addr)
#define _inw _inw
static inline u16 _inw(unsigned long addr)
{
+#ifdef PCI_IOBASE
u16 val;
__io_pbr();
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
__io_par(val);
return val;
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+ return ~0;
+#endif
}
#endif
@@ -484,12 +491,17 @@ static inline u16 _inw(unsigned long addr)
#define _inl _inl
static inline u32 _inl(unsigned long addr)
{
+#ifdef PCI_IOBASE
u32 val;
__io_pbr();
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
__io_par(val);
return val;
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+ return ~0;
+#endif
}
#endif
@@ -497,9 +509,13 @@ static inline u32 _inl(unsigned long addr)
#define _outb _outb
static inline void _outb(u8 value, unsigned long addr)
{
+#ifdef PCI_IOBASE
__io_pbw();
__raw_writeb(value, PCI_IOBASE + addr);
__io_paw();
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -507,9 +523,13 @@ static inline void _outb(u8 value, unsigned long addr)
#define _outw _outw
static inline void _outw(u16 value, unsigned long addr)
{
+#ifdef PCI_IOBASE
__io_pbw();
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
__io_paw();
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -517,9 +537,13 @@ static inline void _outw(u16 value, unsigned long addr)
#define _outl _outl
static inline void _outl(u32 value, unsigned long addr)
{
+#ifdef PCI_IOBASE
__io_pbw();
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
__io_paw();
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -606,7 +630,11 @@ static inline void outl_p(u32 value, unsigned long addr)
#define insb insb
static inline void insb(unsigned long addr, void *buffer, unsigned int count)
{
+#ifdef PCI_IOBASE
readsb(PCI_IOBASE + addr, buffer, count);
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -614,7 +642,11 @@ static inline void insb(unsigned long addr, void *buffer, unsigned int count)
#define insw insw
static inline void insw(unsigned long addr, void *buffer, unsigned int count)
{
+#ifdef PCI_IOBASE
readsw(PCI_IOBASE + addr, buffer, count);
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -622,7 +654,11 @@ static inline void insw(unsigned long addr, void *buffer, unsigned int count)
#define insl insl
static inline void insl(unsigned long addr, void *buffer, unsigned int count)
{
+#ifdef PCI_IOBASE
readsl(PCI_IOBASE + addr, buffer, count);
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -631,7 +667,11 @@ static inline void insl(unsigned long addr, void *buffer, unsigned int count)
static inline void outsb(unsigned long addr, const void *buffer,
unsigned int count)
{
+#ifdef PCI_IOBASE
writesb(PCI_IOBASE + addr, buffer, count);
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -640,7 +680,11 @@ static inline void outsb(unsigned long addr, const void *buffer,
static inline void outsw(unsigned long addr, const void *buffer,
unsigned int count)
{
+#ifdef PCI_IOBASE
writesw(PCI_IOBASE + addr, buffer, count);
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -649,7 +693,11 @@ static inline void outsw(unsigned long addr, const void *buffer,
static inline void outsl(unsigned long addr, const void *buffer,
unsigned int count)
{
+#ifdef PCI_IOBASE
writesl(PCI_IOBASE + addr, buffer, count);
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
@@ -1001,18 +1049,27 @@ static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
#define ioport_map ioport_map
static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
{
+#ifdef PCI_IOBASE
port &= IO_SPACE_LIMIT;
return (port > MMIO_UPPER_LIMIT) ? NULL : PCI_IOBASE + port;
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+ return NULL;
+#endif
}
#define __pci_ioport_unmap __pci_ioport_unmap
static inline void __pci_ioport_unmap(void __iomem *p)
{
+#ifdef PCI_IOBASE
uintptr_t start = (uintptr_t) PCI_IOBASE;
uintptr_t addr = (uintptr_t) p;
if (addr >= start && addr < start + IO_SPACE_LIMIT)
return;
iounmap(p);
+#else
+ WARN_ONCE(1, "No I/O port support\n");
+#endif
}
#endif
--
2.31.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v4 0/3] asm-generic/io.h: Silence -Wnull-pointer-arithmetic warning on PCI_IOBASE
2021-04-30 11:16 [PATCH v4 0/3] asm-generic/io.h: Silence -Wnull-pointer-arithmetic warning on PCI_IOBASE Niklas Schnelle
` (2 preceding siblings ...)
2021-04-30 11:16 ` [PATCH v4 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE Niklas Schnelle
@ 2021-05-03 16:07 ` Arnd Bergmann
2021-05-04 7:39 ` Niklas Schnelle
3 siblings, 1 reply; 7+ messages in thread
From: Arnd Bergmann @ 2021-05-03 16:07 UTC (permalink / raw)
To: Niklas Schnelle
Cc: Vineet Gupta, David S. Miller, Nathan Chancellor,
Nick Desaulniers, clang-built-linux, linux-arch,
Linux Kernel Mailing List, linux-s390,
open list:SYNOPSYS ARC ARCHITECTURE, sparclinux
On Fri, Apr 30, 2021 at 1:16 PM Niklas Schnelle <schnelle@linux.ibm.com> wrote:
>
> From: Niklas Schnelle <niklas@komani.de>
>
> This is version 4 of my attempt to get rid of a clang
> -Wnull-pointer-arithmetic warning for the use of PCI_IOBASE in
> asm-generic/io.h. This was originally found on s390 but should apply to
> all platforms leaving PCI_IOBASE undefined while making use of the inb()
> and friends helpers from asm-generic/io.h.
>
> This applies cleanly and was compile tested on top of v5.12 for the
> previously broken ARC, nds32, h8300 and risc-v architecture
>
> I did boot test this only on x86_64 and s390x the former implements
> inb() itself while the latter would emit a WARN_ONCE() but no drivers
> use inb().
This looks all fine to me, but with the merge window open right now, I
can't add it into linux-next yet, and it wouldn't qualify as a bugfix for 5.13.
Please resend them to me after -rc1 is out so I can merge it for
5.14 through the asm-generic tree.
Please add two small changes to the changelog texts:
- for patch 3, please include a 'Link: tag' to the lore archive of the
previous discussion, that should cover any questions that people
may have
- for the risc-v patch, I would suggest explaining that this fixes
an existing runtime bug, not just a compiler error:
| This is already broken, as accessing a fixed I/O port number of
| an ISA device on NOMMU RISC-V would turn into a NULL pointer
| dereference.
Feel free to either copy this, or use your own explanation.
Arnd
^ permalink raw reply [flat|nested] 7+ messages in thread