[PATCH v2 0/8] linker-section array fix and clean ups

From: Johan Hovold
Date: Mon Nov 23 2020 - 05:25:18 EST


We rely on the linker to create arrays for a number of things including
kernel parameters and device-tree-match entries.

The stride of these linker-section arrays obviously needs to match the
expectations of the code accessing them or bad things will happen.

One thing to watch out for is that gcc is known to increase the
alignment of larger objects with static extent as an optimisation (on
x86), but this can be suppressed by using the aligned attribute when
declaring entries.

We've been relying on this behaviour for 16 years for kernel parameters
(and other structures) and it indeed hasn't changed since the
introduction of the aligned attribute in gcc 3.1 (see align_variable()
in [1]).

Occasionally this gcc optimisation do cause problems which have instead
been worked around in various creative ways including using indirection
through an array of pointers. This was originally done for tracepoints
[2] after a number of failed attempts to create properly aligned arrays,
and the approach was later reused for module-version attributes [3] and
earlycon entries.

This series reverts the latter two workarounds in favour of the one-line
fix of aligning the entries according to the requirement of the type.

In principle, there shouldn't be anything preventing us from doing the
same for tracepoints.

The key observation here is that the arrays should be constructed using
the alignment of the type in question (as given by __alignof__()) rather
than some specific alignment such as sizeof(void *). This allows the
structures to be stored efficiently, but more importantly prevents
breakage on architectures like m68k where pointers are 2-byte aligned
should the size or alignment of the type change (e.g. so that the size
is no longer divisible by four).

As a preventive measure in case the kernel-parameter structures are ever
amended (or the code pattern is reused elsewhere), the final patches
switches the parameter declarations to also use type alignment.

The series has been tested using gcc 4.9 and 9.3 on x86_32 and
x86_64 and using gcc 7.2 on arm; and has been compile-tested and
verified using gcc 4.9 and 10.1 on aarch64, sparc and m68k.

Note that the patches are mostly independent and can be merged through
different subsystem trees. I decided to post them as a series to provide
a common background and have a single thread for any general discussion.

Rob and Greg, can you take patches 1 and 2 through your trees,
respectively?

Jessica, you said you could take the module and params patches through
your tree?

Who picks up the init.h one? Linus?

Johan

[1] https://github.com/gcc-mirror/gcc/blob/master/gcc/varasm.c
[2] https://lore.kernel.org/lkml/20110126222622.GA10794@Krystal/
[3] https://lore.kernel.org/lkml/1297123347-2170-1-git-send-email-dtor@xxxxxxxxxx/


Changes in v2
- amend commit messages of patches 1, 2, 4, 5 and 7 slightly in order
to make it more clear that the gcc optimisation is suppressed by
specifying alignment when declaring variables

v1
- https://lore.kernel.org/r/20201103175711.10731-1-johan@xxxxxxxxxx


Johan Hovold (8):
of: fix linker-section match-table corruption
earlycon: simplify earlycon-table implementation
module: drop version-attribute alignment
module: simplify version-attribute handling
init: use type alignment for kernel parameters
params: drop redundant "unused" attributes
params: use type alignment for kernel parameters
params: clean up module-param macros

drivers/of/fdt.c | 7 ++-----
drivers/tty/serial/earlycon.c | 6 ++----
include/linux/init.h | 2 +-
include/linux/module.h | 28 ++++++++++++++--------------
include/linux/moduleparam.h | 12 ++++++------
include/linux/of.h | 1 +
include/linux/serial_core.h | 20 +++++++-------------
kernel/params.c | 10 ++++------
8 files changed, 37 insertions(+), 49 deletions(-)

--
2.26.2