[3/3] objtool: Generate ORC data for __pfx code

Message ID bc3344e51f3e87102f1301a0be0f72a7689ea4a4.1681331135.git.jpoimboe@kernel.org
State New
Headers
Series objtool: Generate ORC data for __pfx code |

Commit Message

Josh Poimboeuf April 12, 2023, 8:26 p.m. UTC
  Allow unwinding from prefix code by copying the CFI from the starting
instruction of the corresponding function.  Even when the NOPs are
replaced, they're still stack-invariant instructions so the same ORC
entry can be reused everywhere.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 tools/objtool/check.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
  

Comments

Peter Zijlstra April 13, 2023, 11:23 a.m. UTC | #1
On Wed, Apr 12, 2023 at 01:26:15PM -0700, Josh Poimboeuf wrote:

> @@ -4158,7 +4172,7 @@ static int add_prefix_symbols(struct objtool_file *file)
>  {
>  	struct section *sec;
>  	struct symbol *func;
> -	int ret, warnings = 0;
> +	int warnings = 0;
>  
>  	for_each_sec(file, sec) {
>  		if (!(sec->sh.sh_flags & SHF_EXECINSTR))

Stray hunk that should go in the first patch I suppose.
  
Peter Zijlstra April 13, 2023, 11:24 a.m. UTC | #2
On Wed, Apr 12, 2023 at 01:26:15PM -0700, Josh Poimboeuf wrote:
> Allow unwinding from prefix code by copying the CFI from the starting
> instruction of the corresponding function.  Even when the NOPs are
> replaced, they're still stack-invariant instructions so the same ORC
> entry can be reused everywhere.
> 
> Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> ---
>  tools/objtool/check.c | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index 2f3136145b2e..3f27a0278bf8 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -4123,6 +4123,7 @@ static bool ignore_unreachable_insn(struct objtool_file *file, struct instructio
>  static int add_prefix_symbol(struct objtool_file *file, struct symbol *func)
>  {
>  	struct instruction *insn, *prev;
> +	struct cfi_state *cfi;
>  
>  	insn = find_insn(file, func->sec, func->offset);
>  	if (!insn)
> @@ -4151,6 +4152,19 @@ static int add_prefix_symbol(struct objtool_file *file, struct symbol *func)
>  	if (!prev)
>  		return -1;
>  
> +	if (!insn->cfi) {
> +		/*
> +		 * This can happen if stack validation isn't enabled or the
> +		 * function is annotated with STACK_FRAME_NON_STANDARD.
> +		 */
> +		return 0;
> +	}
> +
> +	/* Propagate insn->cfi to the prefix code */
> +	cfi = cfi_hash_find_or_add(insn->cfi);
> +	for (; prev != insn; prev = next_insn_same_sec(file, prev))
> +		prev->cfi = cfi;
> +
>  	return 0;
>  }

FWIW, this makes the whole thing hard rely on the prefix being single
byte NOPs -- which they are, but perhaps we should assert this?
  
Josh Poimboeuf April 13, 2023, 3:29 p.m. UTC | #3
On Thu, Apr 13, 2023 at 01:24:26PM +0200, Peter Zijlstra wrote:
> > +	if (!insn->cfi) {
> > +		/*
> > +		 * This can happen if stack validation isn't enabled or the
> > +		 * function is annotated with STACK_FRAME_NON_STANDARD.
> > +		 */
> > +		return 0;
> > +	}
> > +
> > +	/* Propagate insn->cfi to the prefix code */
> > +	cfi = cfi_hash_find_or_add(insn->cfi);
> > +	for (; prev != insn; prev = next_insn_same_sec(file, prev))
> > +		prev->cfi = cfi;
> > +
> >  	return 0;
> >  }
> 
> FWIW, this makes the whole thing hard rely on the prefix being single
> byte NOPs -- which they are, but perhaps we should assert this?

Couldn't they be any stack-invariant instructions?
  
Peter Zijlstra April 13, 2023, 7:24 p.m. UTC | #4
On Thu, Apr 13, 2023 at 08:29:33AM -0700, Josh Poimboeuf wrote:
> On Thu, Apr 13, 2023 at 01:24:26PM +0200, Peter Zijlstra wrote:
> > > +	if (!insn->cfi) {
> > > +		/*
> > > +		 * This can happen if stack validation isn't enabled or the
> > > +		 * function is annotated with STACK_FRAME_NON_STANDARD.
> > > +		 */
> > > +		return 0;
> > > +	}
> > > +
> > > +	/* Propagate insn->cfi to the prefix code */
> > > +	cfi = cfi_hash_find_or_add(insn->cfi);
> > > +	for (; prev != insn; prev = next_insn_same_sec(file, prev))
> > > +		prev->cfi = cfi;
> > > +
> > >  	return 0;
> > >  }
> > 
> > FWIW, this makes the whole thing hard rely on the prefix being single
> > byte NOPs -- which they are, but perhaps we should assert this?
> 
> Couldn't they be any stack-invariant instructions?

Hmm, I was thikning that since we don't know the size of the
instructions being written, we need CFI for all offsets. But perhaps,
since we do a left-match on IP, only one entry at the __pfx+0 location
would work?
  
Josh Poimboeuf April 13, 2023, 7:30 p.m. UTC | #5
On Thu, Apr 13, 2023 at 09:24:49PM +0200, Peter Zijlstra wrote:
> On Thu, Apr 13, 2023 at 08:29:33AM -0700, Josh Poimboeuf wrote:
> > On Thu, Apr 13, 2023 at 01:24:26PM +0200, Peter Zijlstra wrote:
> > > > +	if (!insn->cfi) {
> > > > +		/*
> > > > +		 * This can happen if stack validation isn't enabled or the
> > > > +		 * function is annotated with STACK_FRAME_NON_STANDARD.
> > > > +		 */
> > > > +		return 0;
> > > > +	}
> > > > +
> > > > +	/* Propagate insn->cfi to the prefix code */
> > > > +	cfi = cfi_hash_find_or_add(insn->cfi);
> > > > +	for (; prev != insn; prev = next_insn_same_sec(file, prev))
> > > > +		prev->cfi = cfi;
> > > > +
> > > >  	return 0;
> > > >  }
> > > 
> > > FWIW, this makes the whole thing hard rely on the prefix being single
> > > byte NOPs -- which they are, but perhaps we should assert this?
> > 
> > Couldn't they be any stack-invariant instructions?
> 
> Hmm, I was thikning that since we don't know the size of the
> instructions being written, we need CFI for all offsets. But perhaps,
> since we do a left-match on IP, only one entry at the __pfx+0 location
> would work?

Right, while in objtool (almost) every insn has insn->cfi, the actual
ORC entries only get created at the boundaries of change.
  

Patch

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 2f3136145b2e..3f27a0278bf8 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4123,6 +4123,7 @@  static bool ignore_unreachable_insn(struct objtool_file *file, struct instructio
 static int add_prefix_symbol(struct objtool_file *file, struct symbol *func)
 {
 	struct instruction *insn, *prev;
+	struct cfi_state *cfi;
 
 	insn = find_insn(file, func->sec, func->offset);
 	if (!insn)
@@ -4151,6 +4152,19 @@  static int add_prefix_symbol(struct objtool_file *file, struct symbol *func)
 	if (!prev)
 		return -1;
 
+	if (!insn->cfi) {
+		/*
+		 * This can happen if stack validation isn't enabled or the
+		 * function is annotated with STACK_FRAME_NON_STANDARD.
+		 */
+		return 0;
+	}
+
+	/* Propagate insn->cfi to the prefix code */
+	cfi = cfi_hash_find_or_add(insn->cfi);
+	for (; prev != insn; prev = next_insn_same_sec(file, prev))
+		prev->cfi = cfi;
+
 	return 0;
 }
 
@@ -4158,7 +4172,7 @@  static int add_prefix_symbols(struct objtool_file *file)
 {
 	struct section *sec;
 	struct symbol *func;
-	int ret, warnings = 0;
+	int warnings = 0;
 
 	for_each_sec(file, sec) {
 		if (!(sec->sh.sh_flags & SHF_EXECINSTR))