modpost: refactor error handling and clarify error/fatal difference

We have 3 log functions. fatal() is special because it lets modpost bail
out immediately. The difference between warn() and error() is the only
prefix parts ("WARNING:" vs "ERROR:").

In my understanding, the expected handling of error() is to propagate
the return code of the function to the exit code of modpost, as
check_exports() etc. already does. This is a good manner in general
because we should display as many error messages as possible in a
single run of modpost.

What is annoying about fatal() is that it kills modpost at the first
error. People would need to run Kbuild again and again until they fix
all errors.

But, unfortunately, people tend to do:
"This case should not be allowed. Let's replace warn() with fatal()."

One of the reasons is probably it is tedious to manually hoist the error
code to the main() function.

This commit refactors error() so any single call for it automatically
makes modpost return the error code.

I also added comments in modpost.h for warn(), error(), and fatal().

Please use fatal() only when you have a strong reason to do so.
For example:

  - Memory shortage (i.e. malloc() etc. has failed)
  - The ELF file is broken, and there is no point to continue parsing
  - Something really odd has happened

For general coding errors, please use error().

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Tested-by: Quentin Perret <qperret@google.com>
This commit is contained in:
Masahiro Yamada 2020-12-01 19:34:15 +09:00
Родитель bc72d723ec
Коммит 0fd3fbadd9
2 изменённых файлов: 27 добавлений и 29 удалений

Просмотреть файл

@ -40,6 +40,8 @@ static int ignore_missing_files;
/* If set to 1, only warn (instead of error) about missing ns imports */ /* If set to 1, only warn (instead of error) about missing ns imports */
static int allow_missing_ns_imports; static int allow_missing_ns_imports;
static bool error_occurred;
enum export { enum export {
export_plain, export_unused, export_gpl, export_plain, export_unused, export_gpl,
export_unused_gpl, export_gpl_future, export_unknown export_unused_gpl, export_gpl_future, export_unknown
@ -78,6 +80,8 @@ modpost_log(enum loglevel loglevel, const char *fmt, ...)
if (loglevel == LOG_FATAL) if (loglevel == LOG_FATAL)
exit(1); exit(1);
if (loglevel == LOG_ERROR)
error_occurred = true;
} }
static inline bool strends(const char *str, const char *postfix) static inline bool strends(const char *str, const char *postfix)
@ -2174,22 +2178,18 @@ static void check_for_unused(enum export exp, const char *m, const char *s)
} }
} }
static int check_exports(struct module *mod) static void check_exports(struct module *mod)
{ {
struct symbol *s, *exp; struct symbol *s, *exp;
int err = 0;
for (s = mod->unres; s; s = s->next) { for (s = mod->unres; s; s = s->next) {
const char *basename; const char *basename;
exp = find_symbol(s->name); exp = find_symbol(s->name);
if (!exp || exp->module == mod) { if (!exp || exp->module == mod) {
if (have_vmlinux && !s->weak) { if (have_vmlinux && !s->weak)
modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR, modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
"\"%s\" [%s.ko] undefined!\n", "\"%s\" [%s.ko] undefined!\n",
s->name, mod->name); s->name, mod->name);
if (!warn_unresolved)
err = 1;
}
continue; continue;
} }
basename = strrchr(mod->name, '/'); basename = strrchr(mod->name, '/');
@ -2203,8 +2203,6 @@ static int check_exports(struct module *mod)
modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR, modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
"module %s uses symbol %s from namespace %s, but does not import it.\n", "module %s uses symbol %s from namespace %s, but does not import it.\n",
basename, exp->name, exp->namespace); basename, exp->name, exp->namespace);
if (!allow_missing_ns_imports)
err = 1;
add_namespace(&mod->missing_namespaces, exp->namespace); add_namespace(&mod->missing_namespaces, exp->namespace);
} }
@ -2212,11 +2210,9 @@ static int check_exports(struct module *mod)
check_for_gpl_usage(exp->export, basename, exp->name); check_for_gpl_usage(exp->export, basename, exp->name);
check_for_unused(exp->export, basename, exp->name); check_for_unused(exp->export, basename, exp->name);
} }
return err;
} }
static int check_modname_len(struct module *mod) static void check_modname_len(struct module *mod)
{ {
const char *mod_name; const char *mod_name;
@ -2225,12 +2221,8 @@ static int check_modname_len(struct module *mod)
mod_name = mod->name; mod_name = mod->name;
else else
mod_name++; mod_name++;
if (strlen(mod_name) >= MODULE_NAME_LEN) { if (strlen(mod_name) >= MODULE_NAME_LEN)
error("module name is too long [%s.ko]\n", mod->name); error("module name is too long [%s.ko]\n", mod->name);
return 1;
}
return 0;
} }
/** /**
@ -2289,10 +2281,9 @@ static void add_staging_flag(struct buffer *b, const char *name)
/** /**
* Record CRCs for unresolved symbols * Record CRCs for unresolved symbols
**/ **/
static int add_versions(struct buffer *b, struct module *mod) static void add_versions(struct buffer *b, struct module *mod)
{ {
struct symbol *s, *exp; struct symbol *s, *exp;
int err = 0;
for (s = mod->unres; s; s = s->next) { for (s = mod->unres; s; s = s->next) {
exp = find_symbol(s->name); exp = find_symbol(s->name);
@ -2304,7 +2295,7 @@ static int add_versions(struct buffer *b, struct module *mod)
} }
if (!modversions) if (!modversions)
return err; return;
buf_printf(b, "\n"); buf_printf(b, "\n");
buf_printf(b, "static const struct modversion_info ____versions[]\n"); buf_printf(b, "static const struct modversion_info ____versions[]\n");
@ -2321,7 +2312,6 @@ static int add_versions(struct buffer *b, struct module *mod)
if (strlen(s->name) >= MODULE_NAME_LEN) { if (strlen(s->name) >= MODULE_NAME_LEN) {
error("too long symbol \"%s\" [%s.ko]\n", error("too long symbol \"%s\" [%s.ko]\n",
s->name, mod->name); s->name, mod->name);
err = 1;
break; break;
} }
buf_printf(b, "\t{ %#8x, \"%s\" },\n", buf_printf(b, "\t{ %#8x, \"%s\" },\n",
@ -2329,8 +2319,6 @@ static int add_versions(struct buffer *b, struct module *mod)
} }
buf_printf(b, "};\n"); buf_printf(b, "};\n");
return err;
} }
static void add_depends(struct buffer *b, struct module *mod) static void add_depends(struct buffer *b, struct module *mod)
@ -2554,7 +2542,6 @@ int main(int argc, char **argv)
char *missing_namespace_deps = NULL; char *missing_namespace_deps = NULL;
char *dump_write = NULL, *files_source = NULL; char *dump_write = NULL, *files_source = NULL;
int opt; int opt;
int err;
int n; int n;
struct dump_list *dump_read_start = NULL; struct dump_list *dump_read_start = NULL;
struct dump_list **dump_read_iter = &dump_read_start; struct dump_list **dump_read_iter = &dump_read_start;
@ -2624,8 +2611,6 @@ int main(int argc, char **argv)
if (!have_vmlinux) if (!have_vmlinux)
warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n"); warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
err = 0;
for (mod = modules; mod; mod = mod->next) { for (mod = modules; mod; mod = mod->next) {
char fname[PATH_MAX]; char fname[PATH_MAX];
@ -2634,14 +2619,14 @@ int main(int argc, char **argv)
buf.pos = 0; buf.pos = 0;
err |= check_modname_len(mod); check_modname_len(mod);
err |= check_exports(mod); check_exports(mod);
add_header(&buf, mod); add_header(&buf, mod);
add_intree_flag(&buf, !external_module); add_intree_flag(&buf, !external_module);
add_retpoline(&buf); add_retpoline(&buf);
add_staging_flag(&buf, mod->name); add_staging_flag(&buf, mod->name);
err |= add_versions(&buf, mod); add_versions(&buf, mod);
add_depends(&buf, mod); add_depends(&buf, mod);
add_moddevtable(&buf, mod); add_moddevtable(&buf, mod);
add_srcversion(&buf, mod); add_srcversion(&buf, mod);
@ -2671,5 +2656,5 @@ int main(int argc, char **argv)
free(buf.p); free(buf.p);
return err; return error_occurred ? 1 : 0;
} }

Просмотреть файл

@ -201,6 +201,19 @@ enum loglevel {
void modpost_log(enum loglevel loglevel, const char *fmt, ...); void modpost_log(enum loglevel loglevel, const char *fmt, ...);
/*
* warn - show the given message, then let modpost continue running, still
* allowing modpost to exit successfully. This should be used when
* we still allow to generate vmlinux and modules.
*
* error - show the given message, then let modpost continue running, but fail
* in the end. This should be used when we should stop building vmlinux
* or modules, but we can continue running modpost to catch as many
* issues as possible.
*
* fatal - show the given message, and bail out immediately. This should be
* used when there is no point to continue running modpost.
*/
#define warn(fmt, args...) modpost_log(LOG_WARN, fmt, ##args) #define warn(fmt, args...) modpost_log(LOG_WARN, fmt, ##args)
#define error(fmt, args...) modpost_log(LOG_ERROR, fmt, ##args) #define error(fmt, args...) modpost_log(LOG_ERROR, fmt, ##args)
#define fatal(fmt, args...) modpost_log(LOG_FATAL, fmt, ##args) #define fatal(fmt, args...) modpost_log(LOG_FATAL, fmt, ##args)