Skip to content

Commit

Permalink
module: use aliases to find module on find_module_all()
Browse files Browse the repository at this point in the history
Modules can have a series of aliases, but we don't currently use
them to check if a module is already loaded. Part of this is because
load_module() will stick to checking for already loaded modules using
the actual module name, not an alias. Its however desriable to also
check for aliases on find_module_all() for existing callers and future
callers. The curent gain to using aliases on find_module_all() will
simply be to be able to support unloading modules using the alias using
the delete_module() syscall.

You can debug this with dynamic debug:

GRUB_CMDLINE_LINUX_DEFAULT="dyndbg=\"func module_process_aliases +p; func module_name_match +p; \" "

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
  • Loading branch information
mcgrof authored and intel-lab-lkp committed Mar 11, 2023
1 parent 4cb0953 commit d920294
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
17 changes: 17 additions & 0 deletions kernel/module/aliases.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ int module_process_aliases(struct module *mod, struct load_info *info)

return -ENOMEM;
}

bool module_name_match_aliases(struct module *mod, const char *name, size_t len)
{
unsigned int i;
const char *alias;

for (i=0; i < mod->num_aliases; i++) {
alias = mod->aliases[i];
if (strlen(alias) == len && !memcmp(alias, name, len)) {
pr_debug("module %s alias matched: alias[%u] = %s\n",
mod->name, i, alias);
return true;
}
}

return false;
}
5 changes: 5 additions & 0 deletions kernel/module/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ static inline int same_magic(const char *amagic, const char *bmagic, bool has_cr
#ifdef CONFIG_MODULE_KERNEL_ALIAS
void free_mod_aliases(struct module *mod);
int module_process_aliases(struct module *mod, struct load_info *info);
bool module_name_match_aliases(struct module *mod, const char *name, size_t len);
#else
static void free_mod_aliases(struct module *mod)
{
Expand All @@ -314,4 +315,8 @@ static int module_process_aliases(struct module *mod, struct load_info *info)
{
return 0;
}
static bool module_name_match_aliases(struct module *mod, const char *name, size_t len)
{
return false;
}
#endif /* CONFIG_MODULE_KERNEL_ALIAS */
24 changes: 23 additions & 1 deletion kernel/module/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,28 @@ bool find_symbol(struct find_symbol_arg *fsa)
return false;
}

static bool module_name_match(struct module *mod, const char *name, size_t len)
{
unsigned int i;
const char *alias;

if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
return true;

return module_name_match_aliases(mod, name, len);

for (i=0; i < mod->num_aliases; i++) {
alias = mod->aliases[i];
if (strlen(alias) == len && !memcmp(alias, name, len)) {
pr_debug("module %s alias matched: alias[%u] = %s\n",
mod->name, i, alias);
return true;
}
}

return false;
}

/*
* Search for module by name: must hold module_mutex (or preempt disabled
* for read-only access).
Expand All @@ -353,7 +375,7 @@ struct module *find_module_all(const char *name, size_t len,
lockdep_is_held(&module_mutex)) {
if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
continue;
if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
if (module_name_match(mod, name, len))
return mod;
}
return NULL;
Expand Down

0 comments on commit d920294

Please sign in to comment.