The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* -Warray-bounds fun again
@ 2022-04-21 14:02 Sven Schnelle
  2022-04-21 16:10 ` Linus Torvalds
  0 siblings, 1 reply; 4+ messages in thread
From: Sven Schnelle @ 2022-04-21 14:02 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kernel, linux-hardening, krebbel, iii, hca, Linus Torvalds

Hi,

while compiling the latest upstream kernel on fedora 36 which
uses gcc-12 by default, i got a lot of -Warray-bounds warnings:

(Note that this is on s390 arch)

In function ‘preempt_count’,
inlined from ‘do_one_initcall’ at init/main.c:1290:14:
./include/asm-generic/rwonce.h:44:26: warning: array subscript 0 is outside array bounds of ‘const volatile int[0]’ [-Warray-bounds]
44 | #define __READ_ONCE(x)  (*(const volatile __unqual_scalar_typeof(x) *)&(x))
   |        ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   /include/asm-generic/rwonce.h:50:9: note: in expansion of macro ‘__READ_ONCE’
50 |         __READ_ONCE(x);
   |         ^~~~~~~~~~~
./arch/s390/include/asm/preempt.h:17:16: note: in expansion of macro ‘READ_ONCE’
17 |         return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
   |                ^~~~~~~~~

This is because S390_lowcore is defined as follows:

#define S390_lowcore (*((struct lowcore *) 0))

Lowcore is a 8K cpu-local memory region on s390 at fixed address 0.

The obvious 'fix' is to use absolute_pointer():

#define S390_lowcore (*((struct lowcore *)absolute_pointer(0)))

That makes the warning go away, but unfortunately the compiler no longer
knows that the memory access is fitting into a load/store with a 12 bit
displacement.

Without absolute_pointer(), reading the preempt count is just a single
instruction: 'l %r11,936'

static inline int preempt_count(void)
{
        return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
 8c4:   58 b0 03 a8             l       %r11,936 <--- load preempt count
 8c8:   b9 04 00 92             lgr     %r9,%r2
        int count = preempt_count();

with absolute pointer(), the compiler no longer optimizes the read to
one instruction and uses an additional base register:

static inline int preempt_count(void)
{
        return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
 8c4:   a7 19 00 00             lghi    %r1,0             <-- use %r1 as base, load with 0
 8c8:   b9 04 00 92             lgr     %r9,%r2
        int count = preempt_count();
        char msgbuf[64];
 8cc:   d7 3f f0 a8 f0 a8       xc      168(64,%r15),168(%r15)
 8d2:   58 b0 13 a8             l       %r11,936(%r1)    <-- and finally add the offset and fetch
        int ret;

The reason for gcc to not optimize that further is likely the asm
statement in RELOC_HIDE (located in include/linux/compiler-gcc.h)

#define RELOC_HIDE(ptr, off)                                    \
({                                                              \
        unsigned long __ptr;                                    \
        __asm__ ("" : "=r"(__ptr) : "0"(ptr));                  \
        (typeof(ptr)) (__ptr + (off));                          \
})


For most of the code this wouldn't be a big problem, but we're storing
information like preempt_count, current thread info, etc in lowcore
because it is the fastest way. I would like to avoid to use additional
instructions/registers just to avoid a warning.

Does anyone have an idea about a different way to make this warning go
away?

Thanks
Sven

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-04-22 13:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-21 14:02 -Warray-bounds fun again Sven Schnelle
2022-04-21 16:10 ` Linus Torvalds
2022-04-22  8:24   ` David Laight
2022-04-22 13:36   ` Sven Schnelle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox