2023-03-27 16:03:21

by Josh Poimboeuf

[permalink] [raw]
Subject: [PATCH 0/5] objtool: warning improvements

Add some user-friendliness to the warnings:

- Add OBJTOOL_ARGS="--verbose" option for making it easier to debug
objtool warnings over email

- Remove per-file rate limiting (doesn't make sense for vmlinux.o)

- Add "missing __noreturn" warning

Josh Poimboeuf (5):
objtool: Add '--verbose' option for disassembling affected functions
objtool: Combine '--backtrace' with '--verbose'
objtool: Remove superfluous dead_end_function() check
objtool: Add per-function rate limiting for unreachable warnings
objtool: Add "missing __noreturn" warning

tools/objtool/Documentation/objtool.txt | 12 ++++++
tools/objtool/builtin-check.c | 2 +-
tools/objtool/check.c | 45 ++++++++++++---------
tools/objtool/include/objtool/builtin.h | 2 +-
tools/objtool/include/objtool/elf.h | 1 +
tools/objtool/include/objtool/warn.h | 52 ++++++++++++++++++++++---
6 files changed, 88 insertions(+), 26 deletions(-)

--
2.39.2


2023-03-27 16:03:41

by Josh Poimboeuf

[permalink] [raw]
Subject: [PATCH 2/5] objtool: Combine '--backtrace' with '--verbose'

Get rid of the '--backtrace' option, instead including that
functionality in '--verbose'. This makes it easy to gather all the
information needed for diagnosing objtool warnings.

Signed-off-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/Documentation/objtool.txt | 4 ++--
tools/objtool/builtin-check.c | 1 -
tools/objtool/check.c | 22 ++++++++--------------
tools/objtool/include/objtool/builtin.h | 1 -
tools/objtool/include/objtool/warn.h | 14 ++++++++------
5 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/tools/objtool/Documentation/objtool.txt b/tools/objtool/Documentation/objtool.txt
index 7c1a46af322f..ec6f82fb414c 100644
--- a/tools/objtool/Documentation/objtool.txt
+++ b/tools/objtool/Documentation/objtool.txt
@@ -246,8 +246,8 @@ Objtool warnings

NOTE: When requesting help with an objtool warning, please re-run the
kernel build with `OBJTOOL_ARGS="--verbose" make <whatever>` and send
-the full warning output (including any function disassembly below the
-warning) to the objtool maintainers.
+the full warning output (including any function disassembly or objtool
+backtrace below the warning) to the objtool maintainers.

For asm files, if you're getting an error which doesn't make sense,
first make sure that the affected code follows the above rules.
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index b8de42f6778e..937ba5d78e08 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -84,7 +84,6 @@ static const struct option check_options[] = {
OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),

OPT_GROUP("Options:"),
- OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index cae6ac6ff246..a652b9e5c805 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -3708,8 +3708,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,

ret = validate_branch(file, func, alt->insn, state);
if (ret) {
- if (opts.backtrace)
- BT_FUNC("(alt)", insn);
+ BT_FUNC("(alt)", insn);
return ret;
}
}
@@ -3755,8 +3754,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
ret = validate_branch(file, func,
insn->jump_dest, state);
if (ret) {
- if (opts.backtrace)
- BT_FUNC("(branch)", insn);
+ BT_FUNC("(branch)", insn);
return ret;
}
}
@@ -3855,7 +3853,7 @@ static int validate_unwind_hint(struct objtool_file *file,
{
if (insn->hint && !insn->visited && !insn->ignore) {
int ret = validate_branch(file, insn_func(insn), insn, *state);
- if (ret && opts.backtrace)
+ if (ret)
BT_FUNC("<=== (hint)", insn);
return ret;
}
@@ -3914,8 +3912,7 @@ static int validate_unret(struct objtool_file *file, struct instruction *insn)

ret = validate_unret(file, alt->insn);
if (ret) {
- if (opts.backtrace)
- BT_FUNC("(alt)", insn);
+ BT_FUNC("(alt)", insn);
return ret;
}
}
@@ -3942,10 +3939,8 @@ static int validate_unret(struct objtool_file *file, struct instruction *insn)
}
ret = validate_unret(file, insn->jump_dest);
if (ret) {
- if (opts.backtrace) {
- BT_FUNC("(branch%s)", insn,
- insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
- }
+ BT_FUNC("(branch%s)", insn,
+ insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
return ret;
}

@@ -3967,8 +3962,7 @@ static int validate_unret(struct objtool_file *file, struct instruction *insn)

ret = validate_unret(file, dest);
if (ret) {
- if (opts.backtrace)
- BT_FUNC("(call)", insn);
+ BT_FUNC("(call)", insn);
return ret;
}
/*
@@ -4254,7 +4248,7 @@ static int validate_symbol(struct objtool_file *file, struct section *sec,
state->uaccess = sym->uaccess_safe;

ret = validate_branch(file, insn_func(insn), insn, *state);
- if (ret && opts.backtrace)
+ if (ret)
BT_FUNC("<=== (sym)", insn);
return ret;
}
diff --git a/tools/objtool/include/objtool/builtin.h b/tools/objtool/include/objtool/builtin.h
index fcca6662c8b4..38aef760465e 100644
--- a/tools/objtool/include/objtool/builtin.h
+++ b/tools/objtool/include/objtool/builtin.h
@@ -28,7 +28,6 @@ struct opts {
bool cfi;

/* options: */
- bool backtrace;
bool backup;
bool dryrun;
bool link;
diff --git a/tools/objtool/include/objtool/warn.h b/tools/objtool/include/objtool/warn.h
index b85aa440ee1f..a9ec1ed6a2e3 100644
--- a/tools/objtool/include/objtool/warn.h
+++ b/tools/objtool/include/objtool/warn.h
@@ -91,12 +91,14 @@ static inline void objdump_func(struct section *sec, unsigned long offset)
objdump_func(sec, offset); \
})

-#define BT_FUNC(format, insn, ...) \
-({ \
- struct instruction *_insn = (insn); \
- char *_str = offstr(_insn->sec, _insn->offset); \
- WARN(" %s: " format, _str, ##__VA_ARGS__); \
- free(_str); \
+#define BT_FUNC(format, insn, ...) \
+({ \
+ if (opts.verbose) { \
+ struct instruction *_insn = (insn); \
+ char *_str = offstr(_insn->sec, _insn->offset); \
+ WARN(" %s: " format, _str, ##__VA_ARGS__); \
+ free(_str); \
+ } \
})

#define WARN_ELF(format, ...) \
--
2.39.2

2023-03-27 16:03:50

by Josh Poimboeuf

[permalink] [raw]
Subject: [PATCH 5/5] objtool: Add "missing __noreturn" warning

Most "unreachable instruction" warnings these days seem to actually be
the result of a missing __noreturn annotation. Add an explicit check
for that.

Signed-off-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/Documentation/objtool.txt | 7 +++++++
tools/objtool/check.c | 16 ++++++++++++++--
2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/Documentation/objtool.txt b/tools/objtool/Documentation/objtool.txt
index ec6f82fb414c..e04d16490c3b 100644
--- a/tools/objtool/Documentation/objtool.txt
+++ b/tools/objtool/Documentation/objtool.txt
@@ -423,6 +423,13 @@ the objtool maintainers.
names and does not use module_init() / module_exit() macros to create
them.

+13. file.o: warning: func(): missing __noreturn
+
+ Objtool has detected that the function doesn't return, but is missing
+ the __noreturn annotation. NOTE: In addition to adding the
+ __noreturn annotation, the function name also needs to be added to
+ 'global_noreturns' in tools/objtool/check.c.
+

If the error doesn't seem to make sense, it could be a bug in objtool.
Feel free to ask the objtool maintainer for help.
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 67a684225702..1ed3024af2b1 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4548,7 +4548,8 @@ static int validate_sls(struct objtool_file *file)

static int validate_reachable_instructions(struct objtool_file *file)
{
- struct instruction *insn;
+ struct instruction *insn, *prev_insn;
+ struct symbol *call_dest;

if (file->ignore_unreachables)
return 0;
@@ -4561,8 +4562,19 @@ static int validate_reachable_instructions(struct objtool_file *file)
continue;
insn->sym->warned = 1;

+ prev_insn = prev_insn_same_sec(file, insn);
+ if (prev_insn)
+ call_dest = insn_call_dest(prev_insn);
+ if (prev_insn && prev_insn->dead_end && call_dest) {
+ if (call_dest->warned)
+ continue;
+ call_dest->warned = 1;
+
+ WARN("%s(): missing __noreturn", call_dest->name);
+ continue;
+ }
+
WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
- return 1;
}

return 0;
--
2.39.2

2023-03-27 16:04:00

by Josh Poimboeuf

[permalink] [raw]
Subject: [PATCH 1/5] objtool: Add '--verbose' option for disassembling affected functions

When a warning is associated with a function, add an option to
disassemble that function.

This makes it easier for reporters to submit the information needed to
diagnose objtool warnings.

Signed-off-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/Documentation/objtool.txt | 5 ++++
tools/objtool/builtin-check.c | 1 +
tools/objtool/include/objtool/builtin.h | 1 +
tools/objtool/include/objtool/warn.h | 38 +++++++++++++++++++++++++
4 files changed, 45 insertions(+)

diff --git a/tools/objtool/Documentation/objtool.txt b/tools/objtool/Documentation/objtool.txt
index 8e53fc6735ef..7c1a46af322f 100644
--- a/tools/objtool/Documentation/objtool.txt
+++ b/tools/objtool/Documentation/objtool.txt
@@ -244,6 +244,11 @@ To achieve the validation, objtool enforces the following rules:
Objtool warnings
----------------

+NOTE: When requesting help with an objtool warning, please re-run the
+kernel build with `OBJTOOL_ARGS="--verbose" make <whatever>` and send
+the full warning output (including any function disassembly below the
+warning) to the objtool maintainers.
+
For asm files, if you're getting an error which doesn't make sense,
first make sure that the affected code follows the above rules.

diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 7c175198d09f..b8de42f6778e 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -93,6 +93,7 @@ static const struct option check_options[] = {
OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
+ OPT_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"),

OPT_END(),
};
diff --git a/tools/objtool/include/objtool/builtin.h b/tools/objtool/include/objtool/builtin.h
index 2a108e648b7a..fcca6662c8b4 100644
--- a/tools/objtool/include/objtool/builtin.h
+++ b/tools/objtool/include/objtool/builtin.h
@@ -37,6 +37,7 @@ struct opts {
bool no_unreachable;
bool sec_address;
bool stats;
+ bool verbose;
};

extern struct opts opts;
diff --git a/tools/objtool/include/objtool/warn.h b/tools/objtool/include/objtool/warn.h
index a3e79ae75f2e..b85aa440ee1f 100644
--- a/tools/objtool/include/objtool/warn.h
+++ b/tools/objtool/include/objtool/warn.h
@@ -11,6 +11,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <unistd.h>
#include <objtool/builtin.h>
#include <objtool/elf.h>

@@ -41,6 +42,41 @@ static inline char *offstr(struct section *sec, unsigned long offset)
return str;
}

+static inline void objdump_func(struct section *sec, unsigned long offset)
+{
+ struct symbol *sym = find_func_containing(sec, offset);
+ const char *script_relative = "scripts/objdump-func";
+ bool is_text = (sec->sh.sh_flags & SHF_EXECINSTR);
+ char *cmd, *srctree, *script;
+
+ if (is_text)
+ sym = find_func_containing(sec, offset);
+ if (!sym)
+ sym = find_symbol_containing(sec, offset);
+ if (!sym)
+ return;
+
+ srctree = getenv("abs_srctree");
+ if (!srctree)
+ return;
+
+ script = malloc(strlen(srctree) + strlen(script_relative) + 2);
+ if (!script)
+ return;
+
+ sprintf(script, "%s/%s", srctree, script_relative);
+
+ if (access(script, X_OK))
+ return;
+
+ cmd = malloc(strlen(script) + strlen(objname) + strlen(sym->name) + 10);
+ if (!cmd)
+ return;
+
+ sprintf(cmd, "%s %s %s 1>&2", script, objname, sym->name);
+ system(cmd);
+}
+
#define WARN(format, ...) \
fprintf(stderr, \
"%s: warning: objtool: " format "\n", \
@@ -51,6 +87,8 @@ static inline char *offstr(struct section *sec, unsigned long offset)
char *_str = offstr(sec, offset); \
WARN("%s: " format, _str, ##__VA_ARGS__); \
free(_str); \
+ if (opts.verbose) \
+ objdump_func(sec, offset); \
})

#define BT_FUNC(format, insn, ...) \
--
2.39.2

2023-03-28 08:09:06

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 2/5] objtool: Combine '--backtrace' with '--verbose'

On Mon, Mar 27, 2023 at 09:00:45AM -0700, Josh Poimboeuf wrote:
> Get rid of the '--backtrace' option, instead including that
> functionality in '--verbose'. This makes it easy to gather all the
> information needed for diagnosing objtool warnings.

Hurmm.. can't we have verbose imply backtrace but keep the separate
option? I'm not sure if I always want the objdump thing -- esp with
multiple warnings on vmlinux that's going to be really slow -- better to
dump the whole of vmlinux.o once at the end.


2023-03-28 08:47:59

by Miroslav Benes

[permalink] [raw]
Subject: Re: [PATCH 1/5] objtool: Add '--verbose' option for disassembling affected functions

> +static inline void objdump_func(struct section *sec, unsigned long offset)
> +{
> + struct symbol *sym = find_func_containing(sec, offset);

Unnecessary assignment?

> + const char *script_relative = "scripts/objdump-func";
> + bool is_text = (sec->sh.sh_flags & SHF_EXECINSTR);
> + char *cmd, *srctree, *script;
> +
> + if (is_text)
> + sym = find_func_containing(sec, offset);
> + if (!sym)
> + sym = find_symbol_containing(sec, offset);
> + if (!sym)
> + return;

Miroslav

2023-03-28 09:13:07

by Miroslav Benes

[permalink] [raw]
Subject: Re: [PATCH 5/5] objtool: Add "missing __noreturn" warning

> WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
> - return 1;

I knew I should have read the whole set first...

Miroslav

2023-03-28 20:22:37

by Josh Poimboeuf

[permalink] [raw]
Subject: Re: [PATCH 2/5] objtool: Combine '--backtrace' with '--verbose'

On Tue, Mar 28, 2023 at 10:07:43AM +0200, Peter Zijlstra wrote:
> On Mon, Mar 27, 2023 at 09:00:45AM -0700, Josh Poimboeuf wrote:
> > Get rid of the '--backtrace' option, instead including that
> > functionality in '--verbose'. This makes it easy to gather all the
> > information needed for diagnosing objtool warnings.
>
> Hurmm.. can't we have verbose imply backtrace but keep the separate
> option? I'm not sure if I always want the objdump thing -- esp with
> multiple warnings on vmlinux that's going to be really slow -- better to
> dump the whole of vmlinux.o once at the end.

That's a good point, vmlinux would be unbearable for multiple warnings.

We could accumulate a list of affected functions and then supply that to
objdump-func.sh at the end and dump them all at the same time.

objdump-func.sh would need to be changed to look for multiple funcs at
once.

If I do that, do you still want the separate backtrace option?

--
Josh

2023-03-29 07:29:46

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 2/5] objtool: Combine '--backtrace' with '--verbose'

On Tue, Mar 28, 2023 at 01:19:21PM -0700, Josh Poimboeuf wrote:
> On Tue, Mar 28, 2023 at 10:07:43AM +0200, Peter Zijlstra wrote:
> > On Mon, Mar 27, 2023 at 09:00:45AM -0700, Josh Poimboeuf wrote:
> > > Get rid of the '--backtrace' option, instead including that
> > > functionality in '--verbose'. This makes it easy to gather all the
> > > information needed for diagnosing objtool warnings.
> >
> > Hurmm.. can't we have verbose imply backtrace but keep the separate
> > option? I'm not sure if I always want the objdump thing -- esp with
> > multiple warnings on vmlinux that's going to be really slow -- better to
> > dump the whole of vmlinux.o once at the end.
>
> That's a good point, vmlinux would be unbearable for multiple warnings.
>
> We could accumulate a list of affected functions and then supply that to
> objdump-func.sh at the end and dump them all at the same time.
>
> objdump-func.sh would need to be changed to look for multiple funcs at
> once.
>
> If I do that, do you still want the separate backtrace option?

For now, lets keep the orthogonal options -- we can always remove it
later if we find they go unused. This automagic stuff is going to take a
bit of getting used to I recon ;-)

2023-03-29 16:38:00

by Josh Poimboeuf

[permalink] [raw]
Subject: Re: [PATCH 5/5] objtool: Add "missing __noreturn" warning

On Tue, Mar 28, 2023 at 11:10:48AM +0200, Miroslav Benes wrote:
> > WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
> > - return 1;
>
> I knew I should have read the whole set first...

Oops, guess that is in the wrong patch.

--
Josh