linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christophe de Dinechin <dinechin@redhat.com>
To: trivial@kernel.org
Cc: Ben Segall <bsegall@google.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@redhat.com>, Mel Gorman <mgorman@suse.de>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Daniel Bristot de Oliveira <bristot@redhat.com>,
	Jason Wang <jasowang@redhat.com>,
	virtualization@lists.linux-foundation.org,
	linux-kernel@vger.kernel.org,
	Zhen Lei <thunder.leizhen@huawei.com>,
	Christophe de Dinechin <dinechin@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>
Subject: [PATCH 1/3] sched/headers: Fix compilation error with GCC 12
Date: Thu, 14 Apr 2022 17:08:53 +0200	[thread overview]
Message-ID: <20220414150855.2407137-2-dinechin@redhat.com> (raw)
In-Reply-To: <20220414150855.2407137-1-dinechin@redhat.com>

With gcc version 12.0.1 20220401 (Red Hat 12.0.1-0) (GCC), the following
errors are reported in sched.h when building after `make defconfig`:

|   CC      kernel/sched/core.o
| In file included from kernel/sched/core.c:81:
| kernel/sched/core.c: In function ‘set_rq_online.part.0’:
| kernel/sched/sched.h:2197:52: error: array subscript -1 is outside
|  array bounds of ‘struct sched_class[44343134792571037]’
|  [-Werror=array-bounds]
|  2197 | #define sched_class_lowest  (__begin_sched_classes - 1)
|       |                                                    ^
| kernel/sched/sched.h:2200:41: note: in definition of macro
|  ‘for_class_range’
|  2200 |         for (class = (_from); class != (_to); class--)
|       |                                         ^~~
| kernel/sched/sched.h:2203:53: note: in expansion of macro
|  ‘sched_class_lowest’
|  2203 |for_class_range(class, sched_class_highest, sched_class_lowest)
|       |                                            ^~~~~~~~~~~~~~~~~~
| kernel/sched/core.c:9115:17: note: in expansion of macro
|  ‘for_each_class’
|  9115 |                 for_each_class(class) {
|       |                 ^~~~~~~~~~~~~~
| kernel/sched/sched.h:2193:27: note: at offset -208 into object
|   ‘__begin_sched_classes’ of size [0, 9223372036854775807]
|  2193 | extern struct sched_class __begin_sched_classes[];
|       |                           ^~~~~~~~~~~~~~~~~~~~~
| kernel/sched/core.c: In function ‘set_rq_offline.part.0’:
| kernel/sched/sched.h:2197:52: error: array subscript -1 is outside
|   array bounds of ‘struct sched_class[44343134792571037]’
|   [-Werror=array-bounds]
|  2197 | #define sched_class_lowest  (__begin_sched_classes - 1)
|       |                                                    ^
| kernel/sched/sched.h:2200:41: note: in definition of macro
|  ‘for_class_range’
|  2200 |         for (class = (_from); class != (_to); class--)
|       |                                         ^~~
| kernel/sched/sched.h:2203:53: note: in expansion of macro
|  ‘sched_class_lowest’
|  2203 |for_class_range(class, sched_class_highest, sched_class_lowest)
|       |                                            ^~~~~~~~~~~~~~~~~~
| kernel/sched/core.c:9127:17: note: in expansion of macro
|  ‘for_each_class’
|  9127 |                 for_each_class(class) {
|       |                 ^~~~~~~~~~~~~~
| kernel/sched/sched.h:2193:27: note: at offset -208 into object
|  ‘__begin_sched_classes’ of size [0, 9223372036854775807]
|  2193 | extern struct sched_class __begin_sched_classes[];
|       |                           ^~~~~~~~~~~~~~~~~~~~~
| In function ‘__pick_next_task’,
|     inlined from ‘pick_next_task’ at kernel/sched/core.c:6204:9,
|     inlined from ‘__schedule’ at kernel/sched/core.c:6352:9:
| kernel/sched/sched.h:2197:52: error: array subscript -1 is outside
|  array bounds of ‘struct sched_class[44343134792571037]’
|  [-Werror=array-bounds]
|  2197 | #define sched_class_lowest  (__begin_sched_classes - 1)
|       |                                                    ^
| kernel/sched/sched.h:2200:41: note: in definition of macro
|  ‘for_class_range’
|  2200 |         for (class = (_from); class != (_to); class--)
|       |                                         ^~~
| kernel/sched/sched.h:2203:53: note: in expansion of macro
|  ‘sched_class_lowest’
|  2203 |for_class_range(class, sched_class_highest, sched_class_lowest)
|       |                                            ^~~~~~~~~~~~~~~~~~
| kernel/sched/core.c:5711:9: note: in expansion of macro
|  ‘for_each_class’
|  5711 |         for_each_class(class) {
|       |         ^~~~~~~~~~~~~~
| kernel/sched/sched.h: In function ‘__schedule’:
| kernel/sched/sched.h:2193:27: note: at offset -208 into object
|  ‘__begin_sched_classes’ of size [0, 9223372036854775807]
|  2193 | extern struct sched_class __begin_sched_classes[];
|       |                           ^~~~~~~~~~~~~~~~~~~~~
| cc1: all warnings being treated as errors

Rewrite the definitions of sched_class_highest and for_class_range to
avoid this error as follows:

1/ The sched_class_highest is rewritten to be relative to
  __begin_sched_classes, so that GCC sees it as being part of the
  __begin_sched_classes array and not a distinct __end_sched_classes
  array.

2/ The for_class_range macro is modified to replace the comparison with
  an out-of-bound pointer __begin_sched_classes - 1 with an equivalent,
  but in-bounds comparison.

In that specific case, I believe that the GCC analysis is correct and
potentially valuable for other arrays, so it makes sense to keep it
enabled.

Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
---
 kernel/sched/sched.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 8dccb34eb190..6350fbc7418d 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2193,11 +2193,18 @@ const struct sched_class name##_sched_class \
 extern struct sched_class __begin_sched_classes[];
 extern struct sched_class __end_sched_classes[];
 
-#define sched_class_highest (__end_sched_classes - 1)
+/*
+ * sched_class_highests is really __end_sched_classes - 1, but written in a way
+ * that makes it clear that it is within __begin_sched_classes[] and not outside
+ * of __end_sched_classes[].
+ */
+#define sched_class_highest (__begin_sched_classes + \
+			     (__end_sched_classes - __begin_sched_classes - 1))
 #define sched_class_lowest  (__begin_sched_classes - 1)
 
+/* The + 1 below places the pointers within the range of their array */
 #define for_class_range(class, _from, _to) \
-	for (class = (_from); class != (_to); class--)
+	for (class = (_from); class + 1 != (_to) + 1; class--)
 
 #define for_each_class(class) \
 	for_class_range(class, sched_class_highest, sched_class_lowest)
-- 
2.35.1


  reply	other threads:[~2022-04-14 15:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-14 15:08 [PATCH 0/3] trivial: Fix several compilation errors/warnings with GCC12 Christophe de Dinechin
2022-04-14 15:08 ` Christophe de Dinechin [this message]
2022-04-14 15:21   ` [PATCH 1/3] sched/headers: Fix compilation error with GCC 12 Peter Zijlstra
2022-04-14 20:30     ` Andrew Morton
2022-04-17 15:52       ` Peter Zijlstra
2022-04-20 18:45         ` Kees Cook
2022-04-21  7:32           ` Peter Zijlstra
2022-04-25 14:23       ` Christophe de Dinechin
2022-04-17 13:27     ` David Laight
2022-04-25 14:07     ` Christophe de Dinechin
2022-05-19 11:16       ` Peter Zijlstra
2022-04-14 15:08 ` [PATCH 2/3] nodemask.h: Fix compilation error with GCC12 Christophe de Dinechin
2022-04-14 15:23   ` Peter Zijlstra
2022-04-14 15:08 ` [PATCH 3/3] virtio-pci: Use cpumask_available to fix compilation error Christophe de Dinechin
2022-04-15  8:48   ` Michael S. Tsirkin
2022-04-28  9:48     ` Christophe Marie Francois Dupont de Dinechin
2022-04-28 11:06       ` Michael S. Tsirkin
2022-05-15 21:24 ` [PATCH 0/3] trivial: Fix several compilation errors/warnings with GCC12 Davidlohr Bueso

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=20220414150855.2407137-2-dinechin@redhat.com \
    --to=dinechin@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=bristot@redhat.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jasowang@redhat.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=thunder.leizhen@huawei.com \
    --cc=trivial@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=virtualization@lists.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;
as well as URLs for NNTP newsgroup(s).