Skip to content

Commit 8630c59

Browse files
committed
Merge tag 'kbuild-v6.16' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild
Pull Kbuild updates from Masahiro Yamada: - Add support for the EXPORT_SYMBOL_GPL_FOR_MODULES() macro, which exports a symbol only to specified modules - Improve ABI handling in gendwarfksyms - Forcibly link lib-y objects to vmlinux even if CONFIG_MODULES=n - Add checkers for redundant or missing <linux/export.h> inclusion - Deprecate the extra-y syntax - Fix a genksyms bug when including enum constants from *.symref files * tag 'kbuild-v6.16' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild: (28 commits) genksyms: Fix enum consts from a reference affecting new values arch: use always-$(KBUILD_BUILTIN) for vmlinux.lds kbuild: set y instead of 1 to KBUILD_{BUILTIN,MODULES} efi/libstub: use 'targets' instead of extra-y in Makefile module: make __mod_device_table__* symbols static scripts/misc-check: check unnecessary #include <linux/export.h> when W=1 scripts/misc-check: check missing #include <linux/export.h> when W=1 scripts/misc-check: add double-quotes to satisfy shellcheck kbuild: move W=1 check for scripts/misc-check to top-level Makefile scripts/tags.sh: allow to use alternative ctags implementation kconfig: introduce menu type enum docs: symbol-namespaces: fix reST warning with literal block kbuild: link lib-y objects to vmlinux forcibly even when CONFIG_MODULES=n tinyconfig: enable CONFIG_LD_DEAD_CODE_DATA_ELIMINATION docs/core-api/symbol-namespaces: drop table of contents and section numbering modpost: check forbidden MODULE_IMPORT_NS("module:") at compile time kbuild: move kbuild syntax processing to scripts/Makefile.build Makefile: remove dependency on archscripts for header installation Documentation/kbuild: Add new gendwarfksyms kABI rules Documentation/kbuild: Drop section numbers ...
2 parents b3154a6 + c50a04f commit 8630c59

File tree

49 files changed

+910
-326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+910
-326
lines changed

Documentation/core-api/symbol-namespaces.rst

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,8 @@ The following document describes how to use Symbol Namespaces to structure the
66
export surface of in-kernel symbols exported through the family of
77
EXPORT_SYMBOL() macros.
88

9-
.. Table of Contents
10-
11-
=== 1 Introduction
12-
=== 2 How to define Symbol Namespaces
13-
--- 2.1 Using the EXPORT_SYMBOL macros
14-
--- 2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
15-
=== 3 How to use Symbols exported in Namespaces
16-
=== 4 Loading Modules that use namespaced Symbols
17-
=== 5 Automatically creating MODULE_IMPORT_NS statements
18-
19-
1. Introduction
20-
===============
9+
Introduction
10+
============
2111

2212
Symbol Namespaces have been introduced as a means to structure the export
2313
surface of the in-kernel API. It allows subsystem maintainers to partition
@@ -28,15 +18,18 @@ kernel. As of today, modules that make use of symbols exported into namespaces,
2818
are required to import the namespace. Otherwise the kernel will, depending on
2919
its configuration, reject loading the module or warn about a missing import.
3020

31-
2. How to define Symbol Namespaces
32-
==================================
21+
Additionally, it is possible to put symbols into a module namespace, strictly
22+
limiting which modules are allowed to use these symbols.
23+
24+
How to define Symbol Namespaces
25+
===============================
3326

3427
Symbols can be exported into namespace using different methods. All of them are
3528
changing the way EXPORT_SYMBOL and friends are instrumented to create ksymtab
3629
entries.
3730

38-
2.1 Using the EXPORT_SYMBOL macros
39-
==================================
31+
Using the EXPORT_SYMBOL macros
32+
------------------------------
4033

4134
In addition to the macros EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(), that allow
4235
exporting of kernel symbols to the kernel symbol table, variants of these are
@@ -54,8 +47,8 @@ refer to ``NULL``. There is no default namespace if none is defined. ``modpost``
5447
and kernel/module/main.c make use the namespace at build time or module load
5548
time, respectively.
5649

57-
2.2 Using the DEFAULT_SYMBOL_NAMESPACE define
58-
=============================================
50+
Using the DEFAULT_SYMBOL_NAMESPACE define
51+
-----------------------------------------
5952

6053
Defining namespaces for all symbols of a subsystem can be very verbose and may
6154
become hard to maintain. Therefore a default define (DEFAULT_SYMBOL_NAMESPACE)
@@ -83,8 +76,24 @@ unit as preprocessor statement. The above example would then read::
8376
within the corresponding compilation unit before the #include for
8477
<linux/export.h>. Typically it's placed before the first #include statement.
8578

86-
3. How to use Symbols exported in Namespaces
87-
============================================
79+
Using the EXPORT_SYMBOL_GPL_FOR_MODULES() macro
80+
-----------------------------------------------
81+
82+
Symbols exported using this macro are put into a module namespace. This
83+
namespace cannot be imported.
84+
85+
The macro takes a comma separated list of module names, allowing only those
86+
modules to access this symbol. Simple tail-globs are supported.
87+
88+
For example::
89+
90+
EXPORT_SYMBOL_GPL_FOR_MODULES(preempt_notifier_inc, "kvm,kvm-*")
91+
92+
will limit usage of this symbol to modules whoes name matches the given
93+
patterns.
94+
95+
How to use Symbols exported in Namespaces
96+
=========================================
8897

8998
In order to use symbols that are exported into namespaces, kernel modules need
9099
to explicitly import these namespaces. Otherwise the kernel might reject to
@@ -106,11 +115,10 @@ inspected with modinfo::
106115

107116

108117
It is advisable to add the MODULE_IMPORT_NS() statement close to other module
109-
metadata definitions like MODULE_AUTHOR() or MODULE_LICENSE(). Refer to section
110-
5. for a way to create missing import statements automatically.
118+
metadata definitions like MODULE_AUTHOR() or MODULE_LICENSE().
111119

112-
4. Loading Modules that use namespaced Symbols
113-
==============================================
120+
Loading Modules that use namespaced Symbols
121+
===========================================
114122

115123
At module loading time (e.g. ``insmod``), the kernel will check each symbol
116124
referenced from the module for its availability and whether the namespace it
@@ -121,8 +129,8 @@ allow loading of modules that don't satisfy this precondition, a configuration
121129
option is available: Setting MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS=y will
122130
enable loading regardless, but will emit a warning.
123131

124-
5. Automatically creating MODULE_IMPORT_NS statements
125-
=====================================================
132+
Automatically creating MODULE_IMPORT_NS statements
133+
==================================================
126134

127135
Missing namespaces imports can easily be detected at build time. In fact,
128136
modpost will emit a warning if a module uses a symbol from a namespace
@@ -154,3 +162,6 @@ in-tree modules::
154162
You can also run nsdeps for external module builds. A typical usage is::
155163

156164
$ make -C <path_to_kernel_src> M=$PWD nsdeps
165+
166+
Note: it will happily generate an import statement for the module namespace;
167+
which will not work and generates build and runtime failures.

Documentation/kbuild/gendwarfksyms.rst

Lines changed: 114 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
DWARF module versioning
33
=======================
44

5-
1. Introduction
6-
===============
5+
Introduction
6+
============
77

88
When CONFIG_MODVERSIONS is enabled, symbol versions for modules
99
are typically calculated from preprocessed source code using the
@@ -14,8 +14,8 @@ selected, **gendwarfksyms** is used instead to calculate symbol versions
1414
from the DWARF debugging information, which contains the necessary
1515
details about the final module ABI.
1616

17-
1.1. Usage
18-
==========
17+
Usage
18+
-----
1919

2020
gendwarfksyms accepts a list of object files on the command line, and a
2121
list of symbol names (one per line) in standard input::
@@ -33,8 +33,8 @@ list of symbol names (one per line) in standard input::
3333
-h, --help Print this message
3434

3535

36-
2. Type information availability
37-
================================
36+
Type information availability
37+
=============================
3838

3939
While symbols are typically exported in the same translation unit (TU)
4040
where they're defined, it's also perfectly fine for a TU to export
@@ -56,8 +56,8 @@ type for calculating symbol versions even if the symbol is defined
5656
elsewhere. The name of the symbol pointer is expected to start with
5757
`__gendwarfksyms_ptr_`, followed by the name of the exported symbol.
5858

59-
3. Symtypes output format
60-
=========================
59+
Symtypes output format
60+
======================
6161

6262
Similarly to genksyms, gendwarfksyms supports writing a symtypes
6363
file for each processed object that contain types for exported
@@ -85,8 +85,8 @@ produces C-style type strings, gendwarfksyms uses the same simple parsed
8585
DWARF format produced by **--dump-dies**, but with type references
8686
instead of fully expanded strings.
8787

88-
4. Maintaining a stable kABI
89-
============================
88+
Maintaining a stable kABI
89+
=========================
9090

9191
Distribution maintainers often need the ability to make ABI compatible
9292
changes to kernel data structures due to LTS updates or backports. Using
@@ -104,8 +104,8 @@ for source code annotation. Note that as these features are only used to
104104
transform the inputs for symbol versioning, the user is responsible for
105105
ensuring that their changes actually won't break the ABI.
106106

107-
4.1. kABI rules
108-
===============
107+
kABI rules
108+
----------
109109

110110
kABI rules allow distributions to fine-tune certain parts
111111
of gendwarfksyms output and thus control how symbol
@@ -125,22 +125,25 @@ the rules. The fields are as follows:
125125
qualified name of the DWARF Debugging Information Entry (DIE).
126126
- `value`: Provides rule-specific data.
127127

128-
The following helper macro, for example, can be used to specify rules
128+
The following helper macros, for example, can be used to specify rules
129129
in the source code::
130130

131-
#define __KABI_RULE(hint, target, value) \
132-
static const char __PASTE(__gendwarfksyms_rule_, \
131+
#define ___KABI_RULE(hint, target, value) \
132+
static const char __PASTE(__gendwarfksyms_rule_, \
133133
__COUNTER__)[] __used __aligned(1) \
134134
__section(".discard.gendwarfksyms.kabi_rules") = \
135-
"1\0" #hint "\0" #target "\0" #value
135+
"1\0" #hint "\0" target "\0" value
136+
137+
#define __KABI_RULE(hint, target, value) \
138+
___KABI_RULE(hint, #target, #value)
136139

137140

138141
Currently, only the rules discussed in this section are supported, but
139142
the format is extensible enough to allow further rules to be added as
140143
need arises.
141144

142-
4.1.1. Managing definition visibility
143-
=====================================
145+
Managing definition visibility
146+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144147

145148
A declaration can change into a full definition when additional includes
146149
are pulled into the translation unit. This changes the versions of any
@@ -168,8 +171,8 @@ Example usage::
168171

169172
KABI_DECLONLY(s);
170173

171-
4.1.2. Adding enumerators
172-
=========================
174+
Adding enumerators
175+
~~~~~~~~~~~~~~~~~~
173176

174177
For enums, all enumerators and their values are included in calculating
175178
symbol versions, which becomes a problem if we later need to add more
@@ -223,8 +226,89 @@ Example usage::
223226
KABI_ENUMERATOR_IGNORE(e, C);
224227
KABI_ENUMERATOR_VALUE(e, LAST, 2);
225228

226-
4.3. Adding structure members
227-
=============================
229+
Managing structure size changes
230+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231+
232+
A data structure can be partially opaque to modules if its allocation is
233+
handled by the core kernel, and modules only need to access some of its
234+
members. In this situation, it's possible to append new members to the
235+
structure without breaking the ABI, as long as the layout for the original
236+
members remains unchanged.
237+
238+
To append new members, we can hide them from symbol versioning as
239+
described in section :ref:`Hiding members <hiding_members>`, but we can't
240+
hide the increase in structure size. The `byte_size` rule allows us to
241+
override the structure size used for symbol versioning.
242+
243+
The rule fields are expected to be as follows:
244+
245+
- `type`: "byte_size"
246+
- `target`: The fully qualified name of the target data structure
247+
(as shown in **--dump-dies** output).
248+
- `value`: A positive decimal number indicating the structure size
249+
in bytes.
250+
251+
Using the `__KABI_RULE` macro, this rule can be defined as::
252+
253+
#define KABI_BYTE_SIZE(fqn, value) \
254+
__KABI_RULE(byte_size, fqn, value)
255+
256+
Example usage::
257+
258+
struct s {
259+
/* Unchanged original members */
260+
unsigned long a;
261+
void *p;
262+
263+
/* Appended new members */
264+
KABI_IGNORE(0, unsigned long n);
265+
};
266+
267+
KABI_BYTE_SIZE(s, 16);
268+
269+
Overriding type strings
270+
~~~~~~~~~~~~~~~~~~~~~~~
271+
272+
In rare situations where distributions must make significant changes to
273+
otherwise opaque data structures that have inadvertently been included
274+
in the published ABI, keeping symbol versions stable using the more
275+
targeted kABI rules can become tedious. The `type_string` rule allows us
276+
to override the full type string for a type or a symbol, and even add
277+
types for versioning that no longer exist in the kernel.
278+
279+
The rule fields are expected to be as follows:
280+
281+
- `type`: "type_string"
282+
- `target`: The fully qualified name of the target data structure
283+
(as shown in **--dump-dies** output) or symbol.
284+
- `value`: A valid type string (as shown in **--symtypes**) output)
285+
to use instead of the real type.
286+
287+
Using the `__KABI_RULE` macro, this rule can be defined as::
288+
289+
#define KABI_TYPE_STRING(type, str) \
290+
___KABI_RULE("type_string", type, str)
291+
292+
Example usage::
293+
294+
/* Override type for a structure */
295+
KABI_TYPE_STRING("s#s",
296+
"structure_type s { "
297+
"member base_type int byte_size(4) "
298+
"encoding(5) n "
299+
"data_member_location(0) "
300+
"} byte_size(8)");
301+
302+
/* Override type for a symbol */
303+
KABI_TYPE_STRING("my_symbol", "variable s#s");
304+
305+
The `type_string` rule should be used only as a last resort if maintaining
306+
a stable symbol versions cannot be reasonably achieved using other
307+
means. Overriding a type string increases the risk of actual ABI breakages
308+
going unnoticed as it hides all changes to the type.
309+
310+
Adding structure members
311+
------------------------
228312

229313
Perhaps the most common ABI compatible change is adding a member to a
230314
kernel data structure. When changes to a structure are anticipated,
@@ -237,8 +321,8 @@ natural method. This section describes gendwarfksyms support for using
237321
reserved space in data structures and hiding members that don't change
238322
the ABI when calculating symbol versions.
239323

240-
4.3.1. Reserving space and replacing members
241-
============================================
324+
Reserving space and replacing members
325+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
242326

243327
Space is typically reserved for later use by appending integer types, or
244328
arrays, to the end of the data structure, but any type can be used. Each
@@ -276,8 +360,10 @@ The examples include `KABI_(RESERVE|USE|REPLACE)*` macros that help
276360
simplify the process and also ensure the replacement member is correctly
277361
aligned and its size won't exceed the reserved space.
278362

279-
4.3.2. Hiding members
280-
=====================
363+
.. _hiding_members:
364+
365+
Hiding members
366+
~~~~~~~~~~~~~~
281367

282368
Predicting which structures will require changes during the support
283369
timeframe isn't always possible, in which case one might have to resort
@@ -305,4 +391,5 @@ member to a union where one of the fields has a name starting with
305391
unsigned long b;
306392
};
307393

308-
With **--stable**, both versions produce the same symbol version.
394+
With **--stable**, both versions produce the same symbol version. The
395+
examples include a `KABI_IGNORE` macro to simplify the code.

Documentation/kbuild/makefiles.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ Example::
291291
# arch/x86/kernel/Makefile
292292
extra-y += vmlinux.lds
293293

294+
extra-y is now deprecated because this is equivalent to:
295+
296+
always-$(KBUILD_BUILTIN) += vmlinux.lds
297+
294298
$(extra-y) should only contain targets needed for vmlinux.
295299

296300
Kbuild skips extra-y when vmlinux is apparently not a final goal.

0 commit comments

Comments
 (0)