2021-05-10 08:55:14

by Niklas Schnelle

[permalink] [raw]
Subject: [PATCH v5 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE

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.

Link: https://lore.kernel.org/lkml/[email protected]/
Signed-off-by: Niklas Schnelle <[email protected]>
---
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 e93375c710b9..7b523683c241 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

@@ -1020,18 +1068,27 @@ static inline void __iomem *ioremap_np(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.25.1


2021-05-10 13:16:00

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v5 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE

Hi Niklas,

I love your patch! Perhaps something to improve:

[auto build test WARNING on soc/for-next]
[also build test WARNING on asm-generic/master v5.13-rc1 next-20210510]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Niklas-Schnelle/asm-generic-io-h-Silence-Wnull-pointer-arithmetic-warning-on-PCI_IOBASE/20210510-165435
base: https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
config: h8300-randconfig-r035-20210510 (attached as .config)
compiler: h8300-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/fa75946a8988026bee808b5840438a3908f0a65b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Niklas-Schnelle/asm-generic-io-h-Silence-Wnull-pointer-arithmetic-warning-on-PCI_IOBASE/20210510-165435
git checkout fa75946a8988026bee808b5840438a3908f0a65b
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross W=1 ARCH=h8300

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

In file included from include/linux/kernel.h:10,
from drivers/media/rc/nuvoton-cir.c:25:
include/linux/scatterlist.h: In function 'sg_set_buf':
include/asm-generic/page.h:89:50: warning: ordered comparison of pointer with null pointer [-Wextra]
89 | #define virt_addr_valid(kaddr) (((void *)(kaddr) >= (void *)PAGE_OFFSET) && \
| ^~
include/linux/compiler.h:78:42: note: in definition of macro 'unlikely'
78 | # define unlikely(x) __builtin_expect(!!(x), 0)
| ^
include/linux/scatterlist.h:137:2: note: in expansion of macro 'BUG_ON'
137 | BUG_ON(!virt_addr_valid(buf));
| ^~~~~~
include/linux/scatterlist.h:137:10: note: in expansion of macro 'virt_addr_valid'
137 | BUG_ON(!virt_addr_valid(buf));
| ^~~~~~~~~~~~~~~
drivers/media/rc/nuvoton-cir.c: In function 'nvt_get_rx_ir_data':
>> drivers/media/rc/nuvoton-cir.c:761:15: warning: iteration 32 invokes undefined behavior [-Waggressive-loop-optimizations]
761 | nvt->buf[i] = nvt_cir_reg_read(nvt, CIR_SRXFIFO);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/media/rc/nuvoton-cir.c:760:2: note: within this loop
760 | for (i = 0; i < fifocount; i++)
| ^~~


vim +761 drivers/media/rc/nuvoton-cir.c

fbdc781c6ba974 drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-08 747
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 748 /* copy data from hardware rx fifo into driver buffer */
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 749 static void nvt_get_rx_ir_data(struct nvt_dev *nvt)
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 750 {
6db0168821fa4e drivers/media/rc/nuvoton-cir.c Heiner Kallweit 2016-08-02 751 u8 fifocount;
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 752 int i;
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 753
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 754 /* Get count of how many bytes to read from RX FIFO */
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 755 fifocount = nvt_cir_reg_read(nvt, CIR_RXFCONT);
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 756
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 757 nvt_dbg("attempting to fetch %u bytes from hw rx fifo", fifocount);
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 758
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 759 /* Read fifocount bytes from CIR Sample RX FIFO register */
6db0168821fa4e drivers/media/rc/nuvoton-cir.c Heiner Kallweit 2016-08-02 760 for (i = 0; i < fifocount; i++)
6db0168821fa4e drivers/media/rc/nuvoton-cir.c Heiner Kallweit 2016-08-02 @761 nvt->buf[i] = nvt_cir_reg_read(nvt, CIR_SRXFIFO);
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 762
bacf8351f23cfd drivers/media/rc/nuvoton-cir.c Heiner Kallweit 2016-08-02 763 nvt->pkts = fifocount;
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 764 nvt_dbg("%s: pkts now %d", __func__, nvt->pkts);
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 765
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 766 nvt_process_rx_ir_data(nvt);
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 767 }
6d2f5c27880c2c drivers/media/IR/nuvoton-cir.c Jarod Wilson 2010-10-07 768

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (5.30 kB)
.config.gz (25.91 kB)
Download all attachments

2021-05-10 13:50:31

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v5 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE

Hi Niklas,

I love your patch! Perhaps something to improve:

[auto build test WARNING on soc/for-next]
[also build test WARNING on asm-generic/master v5.13-rc1 next-20210510]
[cannot apply to sparc-next/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Niklas-Schnelle/asm-generic-io-h-Silence-Wnull-pointer-arithmetic-warning-on-PCI_IOBASE/20210510-165435
base: https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
config: openrisc-randconfig-r025-20210510 (attached as .config)
compiler: or1k-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/fa75946a8988026bee808b5840438a3908f0a65b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Niklas-Schnelle/asm-generic-io-h-Silence-Wnull-pointer-arithmetic-warning-on-PCI_IOBASE/20210510-165435
git checkout fa75946a8988026bee808b5840438a3908f0a65b
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross W=1 ARCH=openrisc

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

In file included from include/linux/kernel.h:10,
from include/linux/list.h:9,
from include/linux/module.h:12,
from drivers/net/arcnet/com20020.c:31:
drivers/net/arcnet/com20020.c: In function 'com20020_reset':
>> include/linux/compiler.h:70:32: warning: 'inbyte' is used uninitialized in this function [-Wuninitialized]
70 | (__if_trace.miss_hit[1]++,1) : \
| ^
drivers/net/arcnet/com20020.c:286:9: note: 'inbyte' was declared here
286 | u_char inbyte;
| ^~~~~~


vim +/inbyte +70 include/linux/compiler.h

a15fd609ad53a6 Linus Torvalds 2019-03-20 59
a15fd609ad53a6 Linus Torvalds 2019-03-20 60 #define __trace_if_value(cond) ({ \
2bcd521a684cc9 Steven Rostedt 2008-11-21 61 static struct ftrace_branch_data \
e04462fb82f8dd Miguel Ojeda 2018-09-03 62 __aligned(4) \
33def8498fdde1 Joe Perches 2020-10-21 63 __section("_ftrace_branch") \
a15fd609ad53a6 Linus Torvalds 2019-03-20 64 __if_trace = { \
2bcd521a684cc9 Steven Rostedt 2008-11-21 65 .func = __func__, \
2bcd521a684cc9 Steven Rostedt 2008-11-21 66 .file = __FILE__, \
2bcd521a684cc9 Steven Rostedt 2008-11-21 67 .line = __LINE__, \
2bcd521a684cc9 Steven Rostedt 2008-11-21 68 }; \
a15fd609ad53a6 Linus Torvalds 2019-03-20 69 (cond) ? \
a15fd609ad53a6 Linus Torvalds 2019-03-20 @70 (__if_trace.miss_hit[1]++,1) : \
a15fd609ad53a6 Linus Torvalds 2019-03-20 71 (__if_trace.miss_hit[0]++,0); \
a15fd609ad53a6 Linus Torvalds 2019-03-20 72 })
a15fd609ad53a6 Linus Torvalds 2019-03-20 73

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (3.33 kB)
.config.gz (24.93 kB)
Download all attachments

2021-05-10 13:51:55

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH v5 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE

On Mon, May 10, 2021 at 3:30 PM kernel test robot <[email protected]> wrote:
> All warnings (new ones prefixed by >>):
>
> In file included from include/linux/kernel.h:10,
> from include/linux/list.h:9,
> from include/linux/module.h:12,
> from drivers/net/arcnet/com20020.c:31:
> drivers/net/arcnet/com20020.c: In function 'com20020_reset':
> >> include/linux/compiler.h:70:32: warning: 'inbyte' is used uninitialized in this function [-Wuninitialized]
> 70 | (__if_trace.miss_hit[1]++,1) : \
> | ^
> drivers/net/arcnet/com20020.c:286:9: note: 'inbyte' was declared here
> 286 | u_char inbyte;
> | ^~~~~~

This looks like a real problem with the patch: the insb()/insw()/insl() helpers
should memset(buffer, 0xff, size) to avoid using random stack data.

Arnd

2021-05-10 13:57:21

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH v5 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE

On Mon, May 10, 2021 at 3:08 PM kernel test robot <[email protected]> wrote:
>
> In file included from include/linux/kernel.h:10,
> from drivers/media/rc/nuvoton-cir.c:25:
> include/linux/scatterlist.h: In function 'sg_set_buf':
> include/asm-generic/page.h:89:50: warning: ordered comparison of pointer with null pointer [-Wextra]
> 89 | #define virt_addr_valid(kaddr) (((void *)(kaddr) >= (void *)PAGE_OFFSET) && \
> | ^~
> include/linux/compiler.h:78:42: note: in definition of macro 'unlikely'
> 78 | # define unlikely(x) __builtin_expect(!!(x), 0)
> | ^
> include/linux/scatterlist.h:137:2: note: in expansion of macro 'BUG_ON'
> 137 | BUG_ON(!virt_addr_valid(buf));
> | ^~~~~~
> include/linux/scatterlist.h:137:10: note: in expansion of macro 'virt_addr_valid'
> 137 | BUG_ON(!virt_addr_valid(buf));
> | ^~~~~~~~~~~~~~~
> drivers/media/rc/nuvoton-cir.c: In function 'nvt_get_rx_ir_data':
> >> drivers/media/rc/nuvoton-cir.c:761:15: warning: iteration 32 invokes undefined behavior [-Waggressive-loop-optimizations]
> 761 | nvt->buf[i] = nvt_cir_reg_read(nvt, CIR_SRXFIFO);

I think you can ignore this one, it's a preexisting issue with this
driver that gets uncovered by your patch: if "fifocount" is read from
a broken device as 0xff, the loop causes a buffer overflow.

The code is already unreachable because the interrupt handler
will have aborted already, so the compiler's dead code elimination
should have shut up that warning, but adding a range check
before the loop would address this as well.

As far as I can tell, this warning only shows up when building with
"make W=1".

Arnd

2021-05-10 14:13:53

by Niklas Schnelle

[permalink] [raw]
Subject: Re: [PATCH v5 3/3] asm-generic/io.h: warn in inb() and friends with undefined PCI_IOBASE

On Mon, 2021-05-10 at 15:43 +0200, Arnd Bergmann wrote:
> On Mon, May 10, 2021 at 3:30 PM kernel test robot <[email protected]> wrote:
> > All warnings (new ones prefixed by >>):
> >
> > In file included from include/linux/kernel.h:10,
> > from include/linux/list.h:9,
> > from include/linux/module.h:12,
> > from drivers/net/arcnet/com20020.c:31:
> > drivers/net/arcnet/com20020.c: In function 'com20020_reset':
> > > > include/linux/compiler.h:70:32: warning: 'inbyte' is used uninitialized in this function [-Wuninitialized]
> > 70 | (__if_trace.miss_hit[1]++,1) : \
> > | ^
> > drivers/net/arcnet/com20020.c:286:9: note: 'inbyte' was declared here
> > 286 | u_char inbyte;
> > | ^~~~~~
>
> This looks like a real problem with the patch: the insb()/insw()/insl() helpers
> should memset(buffer, 0xff, size) to avoid using random stack data.
>
> Arnd


Yes I agree, will send a v6 shortly. Thanks.