Skip to content

Commit 1d08277

Browse files
mettiJessica Yu
authored andcommitted
modpost: add support for generating namespace dependencies
This patch adds an option to modpost to generate a <module>.ns_deps file per module, containing the namespace dependencies for that module. E.g. if the linked module my-module.ko would depend on the symbol myfunc.MY_NS in the namespace MY_NS, the my-module.ns_deps file created by modpost would contain the entry MY_NS to express the namespace dependency of my-module imposed by using the symbol myfunc. These files can subsequently be used by static analysis tools (like coccinelle scripts) to address issues with missing namespace imports. A later patch of this series will introduce such a script 'nsdeps' and a corresponding make target to automatically add missing MODULE_IMPORT_NS() definitions to the module's sources. For that it uses the information provided in the generated .ns_deps files. Co-developed-by: Martijn Coenen <maco@android.com> Signed-off-by: Martijn Coenen <maco@android.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Matthias Maennich <maennich@google.com> Signed-off-by: Jessica Yu <jeyu@kernel.org>
1 parent 8e2adc6 commit 1d08277

4 files changed

Lines changed: 53 additions & 6 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*.lzo
3333
*.mod
3434
*.mod.c
35+
*.ns_deps
3536
*.o
3637
*.o.*
3738
*.order

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ clean: $(clean-dirs)
16691669
-o -name '*.ko.*' \
16701670
-o -name '*.dtb' -o -name '*.dtb.S' -o -name '*.dt.yaml' \
16711671
-o -name '*.dwo' -o -name '*.lst' \
1672-
-o -name '*.su' -o -name '*.mod' \
1672+
-o -name '*.su' -o -name '*.mod' -o -name '*.ns_deps' \
16731673
-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
16741674
-o -name '*.lex.c' -o -name '*.tab.[ch]' \
16751675
-o -name '*.asn1.[ch]' \

‎scripts/mod/modpost.c‎

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ static int sec_mismatch_count = 0;
3838
static int sec_mismatch_fatal = 0;
3939
/* ignore missing files */
4040
static int ignore_missing_files;
41+
/* write namespace dependencies */
42+
static int write_namespace_deps;
4143

4244
enum export {
4345
export_plain, export_unused, export_gpl,
@@ -2176,10 +2178,15 @@ static int check_exports(struct module *mod)
21762178
else
21772179
basename = mod->name;
21782180

2179-
if (exp->namespace &&
2180-
!module_imports_namespace(mod, exp->namespace)) {
2181-
warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2182-
basename, exp->name, exp->namespace);
2181+
if (exp->namespace) {
2182+
add_namespace(&mod->required_namespaces,
2183+
exp->namespace);
2184+
2185+
if (!write_namespace_deps &&
2186+
!module_imports_namespace(mod, exp->namespace)) {
2187+
warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2188+
basename, exp->name, exp->namespace);
2189+
}
21832190
}
21842191

21852192
if (!mod->gpl_compatible)
@@ -2484,6 +2491,31 @@ static void write_dump(const char *fname)
24842491
free(buf.p);
24852492
}
24862493

2494+
static void write_namespace_deps_files(void)
2495+
{
2496+
struct module *mod;
2497+
struct namespace_list *ns;
2498+
struct buffer ns_deps_buf = {};
2499+
2500+
for (mod = modules; mod; mod = mod->next) {
2501+
char fname[PATH_MAX];
2502+
2503+
if (mod->skip)
2504+
continue;
2505+
2506+
ns_deps_buf.pos = 0;
2507+
2508+
for (ns = mod->required_namespaces; ns; ns = ns->next)
2509+
buf_printf(&ns_deps_buf, "%s\n", ns->namespace);
2510+
2511+
if (ns_deps_buf.pos == 0)
2512+
continue;
2513+
2514+
sprintf(fname, "%s.ns_deps", mod->name);
2515+
write_if_changed(&ns_deps_buf, fname);
2516+
}
2517+
}
2518+
24872519
struct ext_sym_list {
24882520
struct ext_sym_list *next;
24892521
const char *file;
@@ -2500,7 +2532,7 @@ int main(int argc, char **argv)
25002532
struct ext_sym_list *extsym_iter;
25012533
struct ext_sym_list *extsym_start = NULL;
25022534

2503-
while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
2535+
while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awEd")) != -1) {
25042536
switch (opt) {
25052537
case 'i':
25062538
kernel_read = optarg;
@@ -2541,6 +2573,9 @@ int main(int argc, char **argv)
25412573
case 'E':
25422574
sec_mismatch_fatal = 1;
25432575
break;
2576+
case 'd':
2577+
write_namespace_deps = 1;
2578+
break;
25442579
default:
25452580
exit(1);
25462581
}
@@ -2575,6 +2610,9 @@ int main(int argc, char **argv)
25752610

25762611
err |= check_modname_len(mod);
25772612
err |= check_exports(mod);
2613+
if (write_namespace_deps)
2614+
continue;
2615+
25782616
add_header(&buf, mod);
25792617
add_intree_flag(&buf, !external_module);
25802618
add_retpoline(&buf);
@@ -2587,6 +2625,12 @@ int main(int argc, char **argv)
25872625
sprintf(fname, "%s.mod.c", mod->name);
25882626
write_if_changed(&buf, fname);
25892627
}
2628+
2629+
if (write_namespace_deps) {
2630+
write_namespace_deps_files();
2631+
return 0;
2632+
}
2633+
25902634
if (dump_write)
25912635
write_dump(dump_write);
25922636
if (sec_mismatch_count && sec_mismatch_fatal)

‎scripts/mod/modpost.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ struct module {
126126
struct buffer dev_table_buf;
127127
char srcversion[25];
128128
int is_dot_o;
129+
// Required namespace dependencies
130+
struct namespace_list *required_namespaces;
129131
// Actual imported namespaces
130132
struct namespace_list *imported_namespaces;
131133
};

0 commit comments

Comments
 (0)