Skip to contentSkip to conversation

objtool validation of static branches

PATCH Superseded Peter Zijlstra 4 participants 21 messages Jan 15, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peter Zijlstra

author maintainer
#0 lore raw

cover objtool validation of static branches

Because dwmw2 is a wee bit paranoid and GCC has a history of being slightly retarded at times; here are a few patches that validate it will in fact generate sensible code for static branches.

With this I hope to take away dwmw2's concerns for hard relying on asm-goto.

With that, we can stop sprinkling LFENCE all over the place with the IBRS/IBPB patches.

author maintainer
#1 lore raw

PATCH 1/4 objtool: Implement base jump_assert support

Implement a jump_label assertion that asserts that the code location is indeed only reachable through a static_branch. Because if GCC is absolutely retaded it could generate code like:

	xor rax,rax
	NOP/JMP 1f
	mov $1, rax
1:
	test rax,rax
	jz 2f
	<do-code>

2:

instead of the sensible:

	NOP/JMP 1f
	<do-code>

1:

This implements objtool infrastructure for ensuring the code ends up sane, since we'll rely on that for correctness and security.

We tag the instructions after the static branch with br_static=true; that is the instruction after the NOP and the instruction at the JMP+disp site.

Then, when we read the .discard.jump_assert section, we assert that each entry points to an instruction that has br_static set.

With this we can assert that the code emitted for the if statement ends up at the static jump location and nothing untowards happened.

Cc Josh Poimboeuf <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
---
 tools/objtool/check.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++--
 tools/objtool/check.h |    1 
 2 files changed, 69 insertions(+), 2 deletions(-)
tools/objtool/check.c +68 -2
@@ -687,8 +687,17 @@ static int handle_jump_alt(struct objtoo
 			   struct instruction *orig_insn,
 			   struct instruction **new_insn)
 {
-	if (orig_insn->type == INSN_NOP)
+	struct instruction *next_insn = list_next_entry(orig_insn, list);
+
+	if (orig_insn->type == INSN_NOP) {
+		/*
+		 * If orig_insn is a NOP, then new_insn is the branch target
+		 * for when it would've been a JMP.
+		 */
+		next_insn->br_static = true;
+		(*new_insn)->br_static = true;
 		return 0;
+	}
 
 	if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
 		WARN_FUNC("unsupported instruction at jump label",
@@ -696,7 +705,16 @@ static int handle_jump_alt(struct objtoo
 		return -1;
 	}
 
-	*new_insn = list_next_entry(orig_insn, list);
+	/*
+	 * Otherwise, orig_insn is a JMP and it will have orig_insn->jump_dest.
+	 * In this case we'll effectively NOP the alt by pointing new_insn at
+	 * next_insn.
+	 */
+	orig_insn->jump_dest->br_static = true;
+	next_insn->br_static = true;
+
+	*new_insn = next_insn;
+
 	return 0;
 }
 
@@ -1067,6 +1085,50 @@ static int read_unwind_hints(struct objt
 	return 0;
 }
 
+static int read_jump_assertions(struct objtool_file *file)
+{
+	struct section *sec, *relasec;
+	struct instruction *insn;
+	struct rela *rela;
+	int i;
+
+	sec = find_section_by_name(file->elf, ".discard.jump_assert");
+	if (!sec)
+		return 0;
+
+	relasec = sec->rela;
+	if (!relasec) {
+		WARN("missing .rela.discard.jump_assert section");
+		return -1;
+	}
+
+	if (sec->len % sizeof(unsigned long)) {
+		WARN("jump_assert size mismatch: %d %ld", sec->len, sizeof(unsigned long));
+		return -1;
+	}
+
+	for (i = 0; i < sec->len / sizeof(unsigned long); i++) {
+		rela = find_rela_by_dest(sec, i * sizeof(unsigned long));
+		if (!rela) {
+			WARN("can't find rela for jump_assert[%d]", i);
+			return -1;
+		}
+
+		insn = find_insn(file, rela->sym->sec, rela->addend);
+		if (!insn) {
+			WARN("can't find insn for jump_assert[%d]", i);
+			return -1;
+		}
+
+		if (!insn->br_static) {
+			WARN_FUNC("static assert FAIL", insn->sec, insn->offset);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 static int decode_sections(struct objtool_file *file)
 {
 	int ret;
@@ -1105,6 +1167,10 @@ static int decode_sections(struct objtoo
 	if (ret)
 		return ret;
 
+	ret = read_jump_assertions(file);
+	if (ret)
+		return ret;
+
 	return 0;
 }
 
tools/objtool/check.h +1 -0
@@ -45,6 +45,7 @@ struct instruction {
 	unsigned char type;
 	unsigned long immediate;
 	bool alt_group, visited, dead_end, ignore, hint, save, restore, ignore_alts;
+	bool br_static;
 	struct symbol *call_dest;
 	struct instruction *jump_dest;
 	struct list_head alts;
#2 lore raw

Re: [PATCH 1/4] objtool: Implement base jump_assert support

Big props to you for braving the bowels of the objtool code.

Quoted 1 line · Peter Zijlstra

+static int read_jump_assertions(struct objtool_file *file)

This does more than just _read_ the assertions. Can you call it something like assert_static_jumps() or do_static_jump_assertions() and then call it from the main check() function?

Quoted 60 lines
tools/objtool/check.c +47 -0
+{
+	struct section *sec, *relasec;
+	struct instruction *insn;
+	struct rela *rela;
+	int i;
+
+	sec = find_section_by_name(file->elf, ".discard.jump_assert");
+	if (!sec)
+		return 0;
+
+	relasec = sec->rela;
+	if (!relasec) {
+		WARN("missing .rela.discard.jump_assert section");
+		return -1;
+	}
+
+	if (sec->len % sizeof(unsigned long)) {
+		WARN("jump_assert size mismatch: %d %ld", sec->len, sizeof(unsigned long));
+		return -1;
+	}
+
+	for (i = 0; i < sec->len / sizeof(unsigned long); i++) {
+		rela = find_rela_by_dest(sec, i * sizeof(unsigned long));
+		if (!rela) {
+			WARN("can't find rela for jump_assert[%d]", i);
+			return -1;
+		}
+
+		insn = find_insn(file, rela->sym->sec, rela->addend);
+		if (!insn) {
+			WARN("can't find insn for jump_assert[%d]", i);
+			return -1;
+		}
+
+		if (!insn->br_static) {
+			WARN_FUNC("static assert FAIL", insn->sec, insn->offset);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 static int decode_sections(struct objtool_file *file)
 {
 	int ret;
@@ -1105,6 +1167,10 @@ static int decode_sections(struct objtoo
 	if (ret)
 		return ret;
 
+	ret = read_jump_assertions(file);
+	if (ret)
+		return ret;
+
 	return 0;
 }
 

tools/objtool/check.h struct instruction { · line 48

Quoted 4 lines
tools/objtool/check.h +1 -0
@@ -45,6 +45,7 @@ struct instruction {
 	unsigned char type;
 	unsigned long immediate;
 	bool alt_group, visited, dead_end, ignore, hint, save, restore, ignore_alts;
+	bool br_static;

s/br_static/static_jump_dest/?

Also, fellow objtool expert, you forgot the patch to the MAINTAINERS file ;-)

Signature · 1 line
Josh
author maintainer
#3 lore raw

Re: [PATCH 1/4] objtool: Implement base jump_assert support

Quoted 8 lines · Josh Poimboeuf

Big props to you for braving the bowels of the objtool code.

Quoted 1 line · Peter Zijlstra

+static int read_jump_assertions(struct objtool_file *file)

This does more than just _read_ the assertions. Can you call it something like assert_static_jumps() or do_static_jump_assertions() and then call it from the main check() function?

Sure.

Quoted 9 lines

tools/objtool/check.h struct instruction { · line 48

Quoted 7 lines
tools/objtool/check.h +1 -0
@@ -45,6 +45,7 @@ struct instruction {
 	unsigned char type;
 	unsigned long immediate;
 	bool alt_group, visited, dead_end, ignore, hint, save, restore, ignore_alts;
+	bool br_static;

s/br_static/static_jump_dest/?

fair enough.

Quoted 2 lines

Also, fellow objtool expert, you forgot the patch to the MAINTAINERS file ;-)

Well, uhm.. I'll think about it ;-)

author maintainer
#4 lore raw

PATCH 2/4 objtool: Implement jump_assert for _static_cpu_has()

In reply to Peter Zijlstra

Unlike the jump_label bits, static_cpu_has is implemented with alternatives. Sadly it doesn't readily distinguish itself from any other alternatives.

Use a heuristic to guess at it :/

But like jump_labels, make static_cpu_has set br_static on the instructions after the static branch such that we can assert on it.

Cc Josh Poimboeuf <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
---
 tools/objtool/check.c   |   21 +++++++++++++++++++++
 tools/objtool/special.c |   27 ++++++++++++++++++++++++++-
 tools/objtool/special.h |    1 +
 3 files changed, 48 insertions(+), 1 deletion(-)
tools/objtool/check.c +21 -0
@@ -636,6 +636,12 @@ static int handle_group_alt(struct objto
 	fake_jump->ignore = true;
 
 	if (!special_alt->new_len) {
+		/*
+		 * The NOP case for _static_cpu_has()
+		 */
+		if (special_alt->static_feat)
+			fake_jump->jump_dest->br_static = true;
+
 		*new_insn = fake_jump;
 		return 0;
 	}
@@ -664,6 +670,21 @@ static int handle_group_alt(struct objto
 				  insn->sec, insn->offset);
 			return -1;
 		}
+
+		if (special_alt->static_feat) {
+			if (insn->type != INSN_JUMP_UNCONDITIONAL) {
+				WARN_FUNC("not an unconditional jump in _static_cpu_has()",
+					  insn->sec, insn->offset);
+			}
+			if (insn->jump_dest == fake_jump) {
+				WARN_FUNC("jump inside alternative for _static_cpu_has()",
+					  insn->sec, insn->offset);
+			}
+			/*
+			 * The JMP+disp case for _static_cpu_has()
+			 */
+			insn->jump_dest->br_static = true;
+		}
 	}
 
 	if (!last_new_insn) {
tools/objtool/special.c +26 -1
@@ -41,6 +41,7 @@
 #define ALT_ORIG_LEN_OFFSET	10
 #define ALT_NEW_LEN_OFFSET	11
 
+#define X86_FEATURE_ALWAYS (3*32+21)
 #define X86_FEATURE_POPCNT (4*32+23)
 
 struct special_entry {
@@ -110,6 +111,9 @@ static int get_alt_entry(struct elf *elf
 		 */
 		if (feature == X86_FEATURE_POPCNT)
 			alt->skip_orig = true;
+
+		if (feature == X86_FEATURE_ALWAYS)
+			alt->static_feat = true;
 	}
 
 	orig_rela = find_rela_by_dest(sec, offset + entry->orig);
@@ -155,7 +159,7 @@ int special_get_alts(struct elf *elf, st
 	struct special_entry *entry;
 	struct section *sec;
 	unsigned int nr_entries;
-	struct special_alt *alt;
+	struct special_alt *alt, *last = NULL;
 	int idx, ret;
 
 	INIT_LIST_HEAD(alts);
@@ -186,6 +190,27 @@ int special_get_alts(struct elf *elf, st
 				return ret;
 
 			list_add_tail(&alt->list, alts);
+
+			if (last) {
+				/*
+				 * If we have two consecutive alternatives for
+				 * the same location, for which the first has
+				 * FEATURE_ALWAYS set and the second has no
+				 * replacement, then assume this is
+				 * _static_cpu_has() and will have to set
+				 * br_static to make jump_assert work.
+				 */
+				if (alt->orig_sec == last->orig_sec &&
+				    alt->orig_off == last->orig_off &&
+				    alt->orig_len == last->orig_len &&
+				    !alt->new_len && !alt->new_sec && !alt->new_off &&
+				    last->static_feat)
+					alt->static_feat = true;
+				else
+					last->static_feat = false;
+			}
+
+			last = alt;
 		}
 	}
 
tools/objtool/special.h +1 -0
@@ -27,6 +27,7 @@ struct special_alt {
 	bool group;
 	bool skip_orig;
 	bool jump_or_nop;
+	bool static_feat;
 
 	struct section *orig_sec;
 	unsigned long orig_off;
#5 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

Quoted 8 lines · Peter Zijlstra

Unlike the jump_label bits, static_cpu_has is implemented with alternatives. Sadly it doesn't readily distinguish itself from any other alternatives.

Use a heuristic to guess at it :/

But like jump_labels, make static_cpu_has set br_static on the instructions after the static branch such that we can assert on it.

This seems a bit heavy handed and fragile, though maybe it is the best way. Still I wonder if there's a better way to do it.

Some quick ideas:

a) Somehow use __jump_table in the _static_cpu_has() macro?

b) Add another special annotation to tell objtool where _static_cpu_has() locations are?

May need to ruminate on this one a bit...

Signature · 1 line
Josh
author maintainer
#6 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

Quoted 10 lines · Josh Poimboeuf
Quoted 8 lines · Peter Zijlstra

Unlike the jump_label bits, static_cpu_has is implemented with alternatives. Sadly it doesn't readily distinguish itself from any other alternatives.

Use a heuristic to guess at it :/

But like jump_labels, make static_cpu_has set br_static on the instructions after the static branch such that we can assert on it.

Quoted 6 lines

This seems a bit heavy handed and fragile, though maybe it is the best way. Still I wonder if there's a better way to do it.

Some quick ideas:

a) Somehow use __jump_table in the _static_cpu_has() macro?

Can do, but adds permanent overhead for the fake table entries, also the alternative in _static_cpu_has is slightly more complex, but it would work I think.

Quoted 2 lines

b) Add another special annotation to tell objtool where _static_cpu_has() locations are?

Almost did that, but I figured I'd give this a try first. But yes I agree it is somewhat ugly.

#7 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

Quoted 5 lines · Peter Zijlstra
Quoted 2 lines

b) Add another special annotation to tell objtool where _static_cpu_has() locations are?

Almost did that, but I figured I'd give this a try first. But yes I agree it is somewhat ugly.

I'm thinking this is the way to go. Create an ANNOTATE_STATIC_JUMP macro, which is used in both _static_cpu_has() and arch_static_branch[_jump](). Then it's very generalized and all the special-casing for both alternatives and jump tables will go away.

Signature · 1 line
Josh
maintainer
#8 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

Right,

I've been putting away extending struct alt_instr for a long time now, trying to be conservative about it but I guess this might be the right time to change that. How about:

struct alt_instr {

        s32 instr_offset;       /* original instruction */
        s32 repl_offset;        /* offset to replacement instruction */
        u16 cpuid;              /* cpuid bit set for replacement */
        u8  instrlen;           /* length of original instruction */
        u8  replacementlen;     /* length of new instruction */
        u8  padlen;             /* length of build-time padding */

u64 flags; /* alternative flags, see <some enum> */
} __packed;

This way we have 64 settings. So we could do:

...
.flags = ALT_FLAGS_STATIC_CPU_HAS,

or something like that and then we can do additional processing/matching for the alternatives.

Or, we can do

struct alt_instr {

        s32 instr_offset;       /* original instruction */
        s32 repl_offset;        /* offset to replacement instruction */
        u16 cpuid;              /* cpuid bit set for replacement */
        u8  instrlen;           /* length of original instruction */
        u8  replacementlen;     /* length of new instruction */
        u8  padlen;             /* length of build-time padding */
	u8 type;		/* types */
} __packed;

and have 256 types but that would be limiting as we won't be able to set more than one.

Hmmm?

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#9 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

Quoted 36 lines · Borislav Petkov

Right,

I've been putting away extending struct alt_instr for a long time now, trying to be conservative about it but I guess this might be the right time to change that. How about:

struct alt_instr {

        s32 instr_offset;       /* original instruction */
        s32 repl_offset;        /* offset to replacement instruction */
        u16 cpuid;              /* cpuid bit set for replacement */
        u8  instrlen;           /* length of original instruction */
        u8  replacementlen;     /* length of new instruction */
        u8  padlen;             /* length of build-time padding */

u64 flags; /* alternative flags, see <some enum> */
} __packed;

This way we have 64 settings. So we could do:

...
.flags = ALT_FLAGS_STATIC_CPU_HAS,

or something like that and then we can do additional processing/matching for the alternatives.

Or, we can do

struct alt_instr {

        s32 instr_offset;       /* original instruction */
        s32 repl_offset;        /* offset to replacement instruction */
        u16 cpuid;              /* cpuid bit set for replacement */
        u8  instrlen;           /* length of original instruction */
        u8  replacementlen;     /* length of new instruction */
        u8  padlen;             /* length of build-time padding */
	u8 type;		/* types */
} __packed;
Quoted 4 lines

and have 256 types but that would be limiting as we won't be able to set more than one.

Hmmm?

That might be a good idea, but here we also need to annotate jump labels. So unless you want to make alternatives broad enough to encompass jump labels, I don't think it solves this particular problem.

Signature · 1 line
Josh
#10 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

Quoted 37 lines · Josh Poimboeuf
Quoted 36 lines · Borislav Petkov

Right,

I've been putting away extending struct alt_instr for a long time now, trying to be conservative about it but I guess this might be the right time to change that. How about:

struct alt_instr {

        s32 instr_offset;       /* original instruction */
        s32 repl_offset;        /* offset to replacement instruction */
        u16 cpuid;              /* cpuid bit set for replacement */
        u8  instrlen;           /* length of original instruction */
        u8  replacementlen;     /* length of new instruction */
        u8  padlen;             /* length of build-time padding */

u64 flags; /* alternative flags, see <some enum> */
} __packed;

This way we have 64 settings. So we could do:

...
.flags = ALT_FLAGS_STATIC_CPU_HAS,

or something like that and then we can do additional processing/matching for the alternatives.

Or, we can do

struct alt_instr {

        s32 instr_offset;       /* original instruction */
        s32 repl_offset;        /* offset to replacement instruction */
        u16 cpuid;              /* cpuid bit set for replacement */
        u8  instrlen;           /* length of original instruction */
        u8  replacementlen;     /* length of new instruction */
        u8  padlen;             /* length of build-time padding */
	u8 type;		/* types */
} __packed;
Quoted 8 lines
Quoted 4 lines

and have 256 types but that would be limiting as we won't be able to set more than one.

Hmmm?

That might be a good idea, but here we also need to annotate jump labels. So unless you want to make alternatives broad enough to encompass jump labels, I don't think it solves this particular problem.

Well, to clarify, it would solve _some_ of the problem. Maybe even most of the problem. We'd still need to special-case jump labels in objtool (like in 1/4), but that's probably not a big deal.

So, contradicting my previous answer here... yes, it would help.

Signature · 1 line
Josh
maintainer
#11 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

Quoted 5 lines · Josh Poimboeuf

Well, to clarify, it would solve _some_ of the problem. Maybe even most of the problem. We'd still need to special-case jump labels in objtool (like in 1/4), but that's probably not a big deal.

So, contradicting my previous answer here... yes, it would help.

Yeah, so PeterZ was asking on IRC today how to detect static_cpu_has() and even if we do some magic matching, nothing beats an explicit flag.

And I'm proposing this because the flags thing has come up in the past already. So it's not like it won't find more users. We can do all kinds of fancy alternatives processing when we tag the call sites properly. :-)

Now lemme look at the rest of the patchset.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
author maintainer
#12 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

In reply to Josh Poimboeuf
Quoted 5 lines · Josh Poimboeuf

Well, to clarify, it would solve _some_ of the problem. Maybe even most of the problem. We'd still need to special-case jump labels in objtool (like in 1/4), but that's probably not a big deal.

So, contradicting my previous answer here... yes, it would help.

OK, saves me from having to argue otherwise. My argument would've been that jump labels are fully described and don't need further annotation.

Yes, Boris' proposal would certainly help with the alternative stuff.

#13 lore raw

Re: [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has()

Quoted 11 lines · Peter Zijlstra
Quoted 5 lines · Josh Poimboeuf

Well, to clarify, it would solve _some_ of the problem. Maybe even most of the problem. We'd still need to special-case jump labels in objtool (like in 1/4), but that's probably not a big deal.

So, contradicting my previous answer here... yes, it would help.

OK, saves me from having to argue otherwise. My argument would've been that jump labels are fully described and don't need further annotation.

Yes, Boris' proposal would certainly help with the alternative stuff.

Fair enough. So either annotating _static_cpu_has() or adding an alt_instr flag works for me.

Signature · 1 line
Josh
author maintainer
#14 lore raw

PATCH 3/4 x86/jump_label: Implement arch_static_assert()

In reply to Peter Zijlstra

Implement the static (branch) assertion. It simply emits the address of the next instruction into a special section which objtool will read and validate against either __jump_table or .altinstructions.

Use like:

	if (static_branch_likely(_key)) {
		arch_static_assert();
		/* do stuff */
	}

Or

	if (static_cpu_has(_feat)) {
		arch_static_assert();
		/* do stuff */
	}
Cc Josh Poimboeuf <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
---
 arch/x86/include/asm/jump_label.h |    9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/include/asm/jump_label.h +9 -0
@@ -62,6 +62,15 @@ static __always_inline bool arch_static_
 	return true;
 }
 
+static __always_inline void arch_static_assert(void)
+{
+	asm volatile ("1:\n\t"
+		      ".pushsection .discard.jump_assert, \"aw\" \n\t"
+		      _ASM_ALIGN  "\n\t"
+		      _ASM_PTR "1b \n\t"
+		      ".popsection \n\t");
+}
+
 #ifdef CONFIG_X86_64
 typedef u64 jump_label_t;
 #else
#15 lore raw

Re: [PATCH 3/4] x86/jump_label: Implement arch_static_assert()

Quoted 31 lines · Peter Zijlstra

Implement the static (branch) assertion. It simply emits the address of the next instruction into a special section which objtool will read and validate against either __jump_table or .altinstructions.

Use like:

	if (static_branch_likely(_key)) {
		arch_static_assert();
		/* do stuff */
	}

Or

	if (static_cpu_has(_feat)) {
		arch_static_assert();
		/* do stuff */
	}
Cc Josh Poimboeuf <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
---
 arch/x86/include/asm/jump_label.h |    9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/include/asm/jump_label.h +0 -0
@@ -62,6 +62,15 @@ static __always_inline bool arch_static_
 	return true;
 }

arch/x86/include/asm/jump_label.h static __always_inline bool arch_static_ · line 73

Quoted 10 lines
arch/x86/include/asm/jump_label.h +9 -0
 
+static __always_inline void arch_static_assert(void)
+{
+	asm volatile ("1:\n\t"
+		      ".pushsection .discard.jump_assert, \"aw\" \n\t"
+		      _ASM_ALIGN  "\n\t"
+		      _ASM_PTR "1b \n\t"
+		      ".popsection \n\t");
+}
+

This needs a nice comment about what exactly it asserts.

And also, people without objtool enabled (i.e., no ORC or livepatch) won't see the assertion. Do we care about those people? :-)

Signature · 1 line
Josh
#16 lore raw

Re: [PATCH 3/4] x86/jump_label: Implement arch_static_assert()

Quoted 2 lines · Josh Poimboeuf

And also, people without objtool enabled (i.e., no ORC or livepatch) won't see the assertion.  Do we care about those people? :-)

I think that's OK. Peter is right that this *would* be a GCC regression. Someone who *does* have objtool enabled would see it, and we'd be aware of it. That's enough to assuage my paranoia.

author maintainer
#17 lore raw

Re: [PATCH 3/4] x86/jump_label: Implement arch_static_assert()

In reply to Josh Poimboeuf
Quoted 14 lines · Josh Poimboeuf

arch/x86/include/asm/jump_label.h static __always_inline bool arch_static_ · line 73

Quoted 9 lines · Peter Zijlstra
arch/x86/include/asm/jump_label.h +9 -0
+static __always_inline void arch_static_assert(void)
+{
+	asm volatile ("1:\n\t"
+		      ".pushsection .discard.jump_assert, \"aw\" \n\t"
+		      _ASM_ALIGN  "\n\t"
+		      _ASM_PTR "1b \n\t"
+		      ".popsection \n\t");
+}
+

This needs a nice comment about what exactly it asserts.

Right, I'll go write one, if you don't see anything horrendous in the objtool code ofcourse :-)

Quoted 2 lines

And also, people without objtool enabled (i.e., no ORC or livepatch) won't see the assertion. Do we care about those people? :-)

Nah, we should get plenty of build coverage with the others I think.

#18 lore raw

Re: [PATCH 3/4] x86/jump_label: Implement arch_static_assert()

In reply to Peter Zijlstra
Quoted 29 lines · Peter Zijlstra

Implement the static (branch) assertion. It simply emits the address of the next instruction into a special section which objtool will read and validate against either __jump_table or .altinstructions.

Use like:

	if (static_branch_likely(_key)) {
		arch_static_assert();
		/* do stuff */
	}

Or

	if (static_cpu_has(_feat)) {
		arch_static_assert();
		/* do stuff */
	}
Cc Josh Poimboeuf <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
---
 arch/x86/include/asm/jump_label.h |    9 +++++++++
 1 file changed, 9 insertions(+)

arch/x86/include/asm/jump_label.h static __always_inline bool arch_static_ · line 68

Quoted 7 lines
arch/x86/include/asm/jump_label.h +4 -0
@@ -62,6 +62,15 @@ static __always_inline bool arch_static_
 	return true;
 }
 
+static __always_inline void arch_static_assert(void)
+{
+	asm volatile ("1:\n\t"
+		      ".pushsection .discard.jump_assert, \"aw\" \n\t"

The "aw" flags aren't needed, the section is neither allocatable nor writable.

Signature · 1 line
Josh
author maintainer
#19 lore raw

PATCH 4/4 x86: Reindent _static_cpu_has

In reply to Peter Zijlstra

Because its daft..

Cc Josh Poimboeuf <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
---
 arch/x86/include/asm/cpufeature.h |   78 +++++++++++++++++++-------------------
 1 file changed, 39 insertions(+), 39 deletions(-)
arch/x86/include/asm/cpufeature.h +39 -39
@@ -145,45 +145,45 @@ extern void clear_cpu_cap(struct cpuinfo
  */
 static __always_inline __pure bool _static_cpu_has(u16 bit)
 {
-		asm_volatile_goto("1: jmp 6f\n"
-			 "2:\n"
-			 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
-			         "((5f-4f) - (2b-1b)),0x90\n"
-			 "3:\n"
-			 ".section .altinstructions,\"a\"\n"
-			 " .long 1b - .\n"		/* src offset */
-			 " .long 4f - .\n"		/* repl offset */
-			 " .word %P1\n"			/* always replace */
-			 " .byte 3b - 1b\n"		/* src len */
-			 " .byte 5f - 4f\n"		/* repl len */
-			 " .byte 3b - 2b\n"		/* pad len */
-			 ".previous\n"
-			 ".section .altinstr_replacement,\"ax\"\n"
-			 "4: jmp %l[t_no]\n"
-			 "5:\n"
-			 ".previous\n"
-			 ".section .altinstructions,\"a\"\n"
-			 " .long 1b - .\n"		/* src offset */
-			 " .long 0\n"			/* no replacement */
-			 " .word %P0\n"			/* feature bit */
-			 " .byte 3b - 1b\n"		/* src len */
-			 " .byte 0\n"			/* repl len */
-			 " .byte 0\n"			/* pad len */
-			 ".previous\n"
-			 ".section .altinstr_aux,\"ax\"\n"
-			 "6:\n"
-			 " testb %[bitnum],%[cap_byte]\n"
-			 " jnz %l[t_yes]\n"
-			 " jmp %l[t_no]\n"
-			 ".previous\n"
-			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS),
-			     [bitnum] "i" (1 << (bit & 7)),
-			     [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
-			 : : t_yes, t_no);
-	t_yes:
-		return true;
-	t_no:
-		return false;
+	asm_volatile_goto("1: jmp 6f\n"
+		 "2:\n"
+		 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
+			 "((5f-4f) - (2b-1b)),0x90\n"
+		 "3:\n"
+		 ".section .altinstructions,\"a\"\n"
+		 " .long 1b - .\n"		/* src offset */
+		 " .long 4f - .\n"		/* repl offset */
+		 " .word %P1\n"			/* always replace */
+		 " .byte 3b - 1b\n"		/* src len */
+		 " .byte 5f - 4f\n"		/* repl len */
+		 " .byte 3b - 2b\n"		/* pad len */
+		 ".previous\n"
+		 ".section .altinstr_replacement,\"ax\"\n"
+		 "4: jmp %l[t_no]\n"
+		 "5:\n"
+		 ".previous\n"
+		 ".section .altinstructions,\"a\"\n"
+		 " .long 1b - .\n"		/* src offset */
+		 " .long 0\n"			/* no replacement */
+		 " .word %P0\n"			/* feature bit */
+		 " .byte 3b - 1b\n"		/* src len */
+		 " .byte 0\n"			/* repl len */
+		 " .byte 0\n"			/* pad len */
+		 ".previous\n"
+		 ".section .altinstr_aux,\"ax\"\n"
+		 "6:\n"
+		 " testb %[bitnum],%[cap_byte]\n"
+		 " jnz %l[t_yes]\n"
+		 " jmp %l[t_no]\n"
+		 ".previous\n"
+		 : : "i" (bit), "i" (X86_FEATURE_ALWAYS),
+		     [bitnum] "i" (1 << (bit & 7)),
+		     [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
+		 : : t_yes, t_no);
+t_yes:
+	return true;
+t_no:
+	return false;
 }
 
 #define static_cpu_has(bit)					\
#20 lore raw

Re: [PATCH 4/4] x86: Reindent _static_cpu_has

Quoted 84 lines · Peter Zijlstra

Because its daft..

Cc Josh Poimboeuf <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
---
 arch/x86/include/asm/cpufeature.h |   78 +++++++++++++++++++-------------------
 1 file changed, 39 insertions(+), 39 deletions(-)
arch/x86/include/asm/cpufeature.h +29 -39
@@ -145,45 +145,45 @@ extern void clear_cpu_cap(struct cpuinfo
  */
 static __always_inline __pure bool _static_cpu_has(u16 bit)
 {
-		asm_volatile_goto("1: jmp 6f\n"
-			 "2:\n"
-			 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
-			         "((5f-4f) - (2b-1b)),0x90\n"
-			 "3:\n"
-			 ".section .altinstructions,\"a\"\n"
-			 " .long 1b - .\n"		/* src offset */
-			 " .long 4f - .\n"		/* repl offset */
-			 " .word %P1\n"			/* always replace */
-			 " .byte 3b - 1b\n"		/* src len */
-			 " .byte 5f - 4f\n"		/* repl len */
-			 " .byte 3b - 2b\n"		/* pad len */
-			 ".previous\n"
-			 ".section .altinstr_replacement,\"ax\"\n"
-			 "4: jmp %l[t_no]\n"
-			 "5:\n"
-			 ".previous\n"
-			 ".section .altinstructions,\"a\"\n"
-			 " .long 1b - .\n"		/* src offset */
-			 " .long 0\n"			/* no replacement */
-			 " .word %P0\n"			/* feature bit */
-			 " .byte 3b - 1b\n"		/* src len */
-			 " .byte 0\n"			/* repl len */
-			 " .byte 0\n"			/* pad len */
-			 ".previous\n"
-			 ".section .altinstr_aux,\"ax\"\n"
-			 "6:\n"
-			 " testb %[bitnum],%[cap_byte]\n"
-			 " jnz %l[t_yes]\n"
-			 " jmp %l[t_no]\n"
-			 ".previous\n"
-			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS),
-			     [bitnum] "i" (1 << (bit & 7)),
-			     [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
-			 : : t_yes, t_no);
-	t_yes:
-		return true;
-	t_no:
-		return false;
+	asm_volatile_goto("1: jmp 6f\n"
+		 "2:\n"
+		 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
+			 "((5f-4f) - (2b-1b)),0x90\n"
+		 "3:\n"
+		 ".section .altinstructions,\"a\"\n"
+		 " .long 1b - .\n"		/* src offset */
+		 " .long 4f - .\n"		/* repl offset */
+		 " .word %P1\n"			/* always replace */
+		 " .byte 3b - 1b\n"		/* src len */
+		 " .byte 5f - 4f\n"		/* repl len */
+		 " .byte 3b - 2b\n"		/* pad len */
+		 ".previous\n"
+		 ".section .altinstr_replacement,\"ax\"\n"
+		 "4: jmp %l[t_no]\n"
+		 "5:\n"
+		 ".previous\n"
+		 ".section .altinstructions,\"a\"\n"
+		 " .long 1b - .\n"		/* src offset */
+		 " .long 0\n"			/* no replacement */
+		 " .word %P0\n"			/* feature bit */
+		 " .byte 3b - 1b\n"		/* src len */
+		 " .byte 0\n"			/* repl len */
+		 " .byte 0\n"			/* pad len */
+		 ".previous\n"
+		 ".section .altinstr_aux,\"ax\"\n"
+		 "6:\n"
+		 " testb %[bitnum],%[cap_byte]\n"
+		 " jnz %l[t_yes]\n"

arch/x86/include/asm/cpufeature.h extern void clear_cpu_cap(struct cpuinfo · line 186

Quoted 10 lines
arch/x86/include/asm/cpufeature.h +10 -0
+		 " jmp %l[t_no]\n"
+		 ".previous\n"
+		 : : "i" (bit), "i" (X86_FEATURE_ALWAYS),
+		     [bitnum] "i" (1 << (bit & 7)),
+		     [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
+		 : : t_yes, t_no);
+t_yes:
+	return true;
+t_no:
+	return false;

While you're at it, might as well convert the feature bit constraints to have names like %P[name] instead of %P0 and %P1?

Signature · 1 line
Josh