Skip to content

Commit b2e3e65

Browse files
Mathieu DesnoyersLinus Torvalds
authored andcommitted
Linux Kernel Markers: create modpost file
This adds some new magic in the MODPOST phase for CONFIG_MARKERS. Analogous to the Module.symvers file, the build will now write a Module.markers file when CONFIG_MARKERS=y is set. This file lists the name, defining module, and format string of each marker, separated by \t characters. This simple text file can be used by offline build procedures for instrumentation code, analogous to how System.map and Module.symvers can be useful to have for kernels other than the one you are running right now. The strings are made easy to extract by having the __trace_mark macro define the name and format together in a single array called __mstrtab_* in the __markers_strings section. This is straightforward and reliable as long as the marker structs are always defined by this macro. It is an unreasonable amount of hairy work to extract the string pointers from the __markers section structs, which entails handling a relocation type for every machine under the sun. Mathieu : - Ran through checkpatch.pl Signed-off-by: Roland McGrath <roland@redhat.com> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> Cc: David Smith <dsmith@redhat.com> Cc: Sam Ravnborg <sam@ravnborg.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fb40bd7 commit b2e3e65

4 files changed

Lines changed: 180 additions & 7 deletions

File tree

‎include/linux/marker.h‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,12 @@ struct marker {
6161
*/
6262
#define __trace_mark(name, call_private, format, args...) \
6363
do { \
64-
static const char __mstrtab_name_##name[] \
64+
static const char __mstrtab_##name[] \
6565
__attribute__((section("__markers_strings"))) \
66-
= #name; \
67-
static const char __mstrtab_format_##name[] \
68-
__attribute__((section("__markers_strings"))) \
69-
= format; \
66+
= #name "\0" format; \
7067
static struct marker __mark_##name \
7168
__attribute__((section("__markers"), aligned(8))) = \
72-
{ __mstrtab_name_##name, __mstrtab_format_##name, \
69+
{ __mstrtab_##name, &__mstrtab_##name[sizeof(#name)], \
7370
0, 0, marker_probe_cb, \
7471
{ __mark_empty_function, NULL}, NULL }; \
7572
__mark_check_format(format, ## args); \

‎scripts/Makefile.modpost‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# 2) modpost is then used to
1414
# 3) create one <module>.mod.c file pr. module
1515
# 4) create one Module.symvers file with CRC for all exported symbols
16+
# 4a) [CONFIG_MARKERS] create one Module.markers file listing defined markers
1617
# 5) compile all <module>.mod.c files
1718
# 6) final link of the module to a <module.ko> file
1819

@@ -45,6 +46,10 @@ include scripts/Makefile.lib
4546

4647
kernelsymfile := $(objtree)/Module.symvers
4748
modulesymfile := $(firstword $(KBUILD_EXTMOD))/Module.symvers
49+
kernelmarkersfile := $(objtree)/Module.markers
50+
modulemarkersfile := $(firstword $(KBUILD_EXTMOD))/Module.markers
51+
52+
markersfile = $(if $(KBUILD_EXTMOD),$(modulemarkersfile),$(kernelmarkersfile))
4853

4954
# Step 1), find all modules listed in $(MODVERDIR)/
5055
__modules := $(sort $(shell grep -h '\.ko' /dev/null $(wildcard $(MODVERDIR)/*.mod)))
@@ -63,6 +68,8 @@ modpost = scripts/mod/modpost \
6368
$(if $(KBUILD_EXTMOD),-I $(modulesymfile)) \
6469
$(if $(KBUILD_EXTMOD),-o $(modulesymfile)) \
6570
$(if $(CONFIG_DEBUG_SECTION_MISMATCH),,-S) \
71+
$(if $(CONFIG_MARKERS),-K $(kernelmarkersfile)) \
72+
$(if $(CONFIG_MARKERS),-M $(markersfile)) \
6673
$(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w)
6774

6875
quiet_cmd_modpost = MODPOST $(words $(filter-out vmlinux FORCE, $^)) modules
@@ -82,6 +89,10 @@ vmlinux.o: FORCE
8289
$(symverfile): __modpost ;
8390
$(modules:.ko=.mod.c): __modpost ;
8491

92+
ifdef CONFIG_MARKERS
93+
$(markersfile): __modpost ;
94+
endif
95+
8596

8697
# Step 5), compile all *.mod.c files
8798

‎scripts/mod/modpost.c‎

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* Usage: modpost vmlinux module1.o module2.o ...
1212
*/
1313

14+
#define _GNU_SOURCE
15+
#include <stdio.h>
1416
#include <ctype.h>
1517
#include "modpost.h"
1618
#include "../../include/linux/license.h"
@@ -435,6 +437,8 @@ static int parse_elf(struct elf_info *info, const char *filename)
435437
info->export_unused_gpl_sec = i;
436438
else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
437439
info->export_gpl_future_sec = i;
440+
else if (strcmp(secname, "__markers_strings") == 0)
441+
info->markers_strings_sec = i;
438442

439443
if (sechdrs[i].sh_type != SHT_SYMTAB)
440444
continue;
@@ -1470,6 +1474,62 @@ static void check_sec_ref(struct module *mod, const char *modname,
14701474
}
14711475
}
14721476

1477+
static void get_markers(struct elf_info *info, struct module *mod)
1478+
{
1479+
const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec];
1480+
const char *strings = (const char *) info->hdr + sh->sh_offset;
1481+
const Elf_Sym *sym, *first_sym, *last_sym;
1482+
size_t n;
1483+
1484+
if (!info->markers_strings_sec)
1485+
return;
1486+
1487+
/*
1488+
* First count the strings. We look for all the symbols defined
1489+
* in the __markers_strings section named __mstrtab_*. For
1490+
* these local names, the compiler puts a random .NNN suffix on,
1491+
* so the names don't correspond exactly.
1492+
*/
1493+
first_sym = last_sym = NULL;
1494+
n = 0;
1495+
for (sym = info->symtab_start; sym < info->symtab_stop; sym++)
1496+
if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1497+
sym->st_shndx == info->markers_strings_sec &&
1498+
!strncmp(info->strtab + sym->st_name,
1499+
"__mstrtab_", sizeof "__mstrtab_" - 1)) {
1500+
if (first_sym == NULL)
1501+
first_sym = sym;
1502+
last_sym = sym;
1503+
++n;
1504+
}
1505+
1506+
if (n == 0)
1507+
return;
1508+
1509+
/*
1510+
* Now collect each name and format into a line for the output.
1511+
* Lines look like:
1512+
* marker_name vmlinux marker %s format %d
1513+
* The format string after the second \t can use whitespace.
1514+
*/
1515+
mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n));
1516+
mod->nmarkers = n;
1517+
1518+
n = 0;
1519+
for (sym = first_sym; sym <= last_sym; sym++)
1520+
if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1521+
sym->st_shndx == info->markers_strings_sec &&
1522+
!strncmp(info->strtab + sym->st_name,
1523+
"__mstrtab_", sizeof "__mstrtab_" - 1)) {
1524+
const char *name = strings + sym->st_value;
1525+
const char *fmt = strchr(name, '\0') + 1;
1526+
char *line = NULL;
1527+
asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1528+
NOFAIL(line);
1529+
mod->markers[n++] = line;
1530+
}
1531+
}
1532+
14731533
static void read_symbols(char *modname)
14741534
{
14751535
const char *symname;
@@ -1521,6 +1581,8 @@ static void read_symbols(char *modname)
15211581
get_src_version(modname, mod->srcversion,
15221582
sizeof(mod->srcversion)-1);
15231583

1584+
get_markers(&info, mod);
1585+
15241586
parse_elf_finish(&info);
15251587

15261588
/* Our trick to get versioning for struct_module - it's
@@ -1867,16 +1929,104 @@ static void write_dump(const char *fname)
18671929
write_if_changed(&buf, fname);
18681930
}
18691931

1932+
static void add_marker(struct module *mod, const char *name, const char *fmt)
1933+
{
1934+
char *line = NULL;
1935+
asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1936+
NOFAIL(line);
1937+
1938+
mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) *
1939+
sizeof mod->markers[0])));
1940+
mod->markers[mod->nmarkers++] = line;
1941+
}
1942+
1943+
static void read_markers(const char *fname)
1944+
{
1945+
unsigned long size, pos = 0;
1946+
void *file = grab_file(fname, &size);
1947+
char *line;
1948+
1949+
if (!file) /* No old markers, silently ignore */
1950+
return;
1951+
1952+
while ((line = get_next_line(&pos, file, size))) {
1953+
char *marker, *modname, *fmt;
1954+
struct module *mod;
1955+
1956+
marker = line;
1957+
modname = strchr(marker, '\t');
1958+
if (!modname)
1959+
goto fail;
1960+
*modname++ = '\0';
1961+
fmt = strchr(modname, '\t');
1962+
if (!fmt)
1963+
goto fail;
1964+
*fmt++ = '\0';
1965+
if (*marker == '\0' || *modname == '\0')
1966+
goto fail;
1967+
1968+
mod = find_module(modname);
1969+
if (!mod) {
1970+
if (is_vmlinux(modname))
1971+
have_vmlinux = 1;
1972+
mod = new_module(NOFAIL(strdup(modname)));
1973+
mod->skip = 1;
1974+
}
1975+
1976+
add_marker(mod, marker, fmt);
1977+
}
1978+
return;
1979+
fail:
1980+
fatal("parse error in markers list file\n");
1981+
}
1982+
1983+
static int compare_strings(const void *a, const void *b)
1984+
{
1985+
return strcmp(*(const char **) a, *(const char **) b);
1986+
}
1987+
1988+
static void write_markers(const char *fname)
1989+
{
1990+
struct buffer buf = { };
1991+
struct module *mod;
1992+
size_t i;
1993+
1994+
for (mod = modules; mod; mod = mod->next)
1995+
if ((!external_module || !mod->skip) && mod->markers != NULL) {
1996+
/*
1997+
* Sort the strings so we can skip duplicates when
1998+
* we write them out.
1999+
*/
2000+
qsort(mod->markers, mod->nmarkers,
2001+
sizeof mod->markers[0], &compare_strings);
2002+
for (i = 0; i < mod->nmarkers; ++i) {
2003+
char *line = mod->markers[i];
2004+
buf_write(&buf, line, strlen(line));
2005+
while (i + 1 < mod->nmarkers &&
2006+
!strcmp(mod->markers[i],
2007+
mod->markers[i + 1]))
2008+
free(mod->markers[i++]);
2009+
free(mod->markers[i]);
2010+
}
2011+
free(mod->markers);
2012+
mod->markers = NULL;
2013+
}
2014+
2015+
write_if_changed(&buf, fname);
2016+
}
2017+
18702018
int main(int argc, char **argv)
18712019
{
18722020
struct module *mod;
18732021
struct buffer buf = { };
18742022
char *kernel_read = NULL, *module_read = NULL;
18752023
char *dump_write = NULL;
2024+
char *markers_read = NULL;
2025+
char *markers_write = NULL;
18762026
int opt;
18772027
int err;
18782028

1879-
while ((opt = getopt(argc, argv, "i:I:msSo:aw")) != -1) {
2029+
while ((opt = getopt(argc, argv, "i:I:msSo:awM:K:")) != -1) {
18802030
switch (opt) {
18812031
case 'i':
18822032
kernel_read = optarg;
@@ -1903,6 +2053,12 @@ int main(int argc, char **argv)
19032053
case 'w':
19042054
warn_unresolved = 1;
19052055
break;
2056+
case 'M':
2057+
markers_write = optarg;
2058+
break;
2059+
case 'K':
2060+
markers_read = optarg;
2061+
break;
19062062
default:
19072063
exit(1);
19082064
}
@@ -1950,5 +2106,11 @@ int main(int argc, char **argv)
19502106
"'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
19512107
sec_mismatch_count);
19522108

2109+
if (markers_read)
2110+
read_markers(markers_read);
2111+
2112+
if (markers_write)
2113+
write_markers(markers_write);
2114+
19532115
return err;
19542116
}

‎scripts/mod/modpost.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ struct module {
112112
int has_init;
113113
int has_cleanup;
114114
struct buffer dev_table_buf;
115+
char **markers;
116+
size_t nmarkers;
115117
char srcversion[25];
116118
};
117119

@@ -126,6 +128,7 @@ struct elf_info {
126128
Elf_Section export_gpl_sec;
127129
Elf_Section export_unused_gpl_sec;
128130
Elf_Section export_gpl_future_sec;
131+
Elf_Section markers_strings_sec;
129132
const char *strtab;
130133
char *modinfo;
131134
unsigned int modinfo_len;

0 commit comments

Comments
 (0)