The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Sven Schnelle <svens@linux.ibm.com>
To: Kees Cook <keescook@chromium.org>
Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org,
	krebbel@linux.ibm.com, iii@linux.ibm.com, hca@linux.ibm.com,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: -Warray-bounds fun again
Date: Thu, 21 Apr 2022 16:02:07 +0200	[thread overview]
Message-ID: <yt9dzgkelelc.fsf@linux.ibm.com> (raw)

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

             reply	other threads:[~2022-04-21 14:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-21 14:02 Sven Schnelle [this message]
2022-04-21 16:10 ` -Warray-bounds fun again Linus Torvalds
2022-04-22  8:24   ` David Laight
2022-04-22 13:36   ` Sven Schnelle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=yt9dzgkelelc.fsf@linux.ibm.com \
    --to=svens@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=iii@linux.ibm.com \
    --cc=keescook@chromium.org \
    --cc=krebbel@linux.ibm.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox