Commit f7b93d42 authored by Ard Biesheuvel's avatar Ard Biesheuvel Committed by Will Deacon

arm64/alternatives: use subsections for replacement sequences

When building very large kernels, the logic that emits replacement
sequences for alternatives fails when relative branches are present
in the code that is emitted into the .altinstr_replacement section
and patched in at the original site and fixed up. The reason is that
the linker will insert veneers if relative branches go out of range,
and due to the relative distance of the .altinstr_replacement from
the .text section where its branch targets usually live, veneers
may be emitted at the end of the .altinstr_replacement section, with
the relative branches in the sequence pointed at the veneers instead
of the actual target.

The alternatives patching logic will attempt to fix up the branch to
point to its original target, which will be the veneer in this case,
but given that the patch site is likely to be far away as well, it
will be out of range and so patching will fail. There are other cases
where these veneers are problematic, e.g., when the target of the
branch is in .text while the patch site is in .init.text, in which
case putting the replacement sequence inside .text may not help either.

So let's use subsections to emit the replacement code as closely as
possible to the patch site, to ensure that veneers are only likely to
be emitted if they are required at the patch site as well, in which
case they will be in range for the replacement sequence both before
and after it is transported to the patch site.

This will prevent alternative sequences in non-init code from being
released from memory after boot, but this is tolerable given that the
entire section is only 512 KB on an allyesconfig build (which weighs in
at 500+ MB for the entire Image). Also, note that modules today carry
the replacement sequences in non-init sections as well, and any of
those that target init code will be emitted into init sections after
this change.

This fixes an early crash when booting an allyesconfig kernel on a
system where any of the alternatives sequences containing relative
branches are activated at boot (e.g., ARM64_HAS_PAN on TX2)
Signed-off-by: default avatarArd Biesheuvel <ardb@kernel.org>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Cc: James Morse <james.morse@arm.com>
Cc: Andre Przywara <andre.przywara@arm.com>
Cc: Dave P Martin <dave.martin@arm.com>
Link: https://lore.kernel.org/r/20200630081921.13443-1-ardb@kernel.orgSigned-off-by: default avatarWill Deacon <will@kernel.org>
parent 108447fd
...@@ -73,11 +73,11 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ...@@ -73,11 +73,11 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
".pushsection .altinstructions,\"a\"\n" \ ".pushsection .altinstructions,\"a\"\n" \
ALTINSTR_ENTRY(feature) \ ALTINSTR_ENTRY(feature) \
".popsection\n" \ ".popsection\n" \
".pushsection .altinstr_replacement, \"a\"\n" \ ".subsection 1\n" \
"663:\n\t" \ "663:\n\t" \
newinstr "\n" \ newinstr "\n" \
"664:\n\t" \ "664:\n\t" \
".popsection\n\t" \ ".previous\n\t" \
".org . - (664b-663b) + (662b-661b)\n\t" \ ".org . - (664b-663b) + (662b-661b)\n\t" \
".org . - (662b-661b) + (664b-663b)\n" \ ".org . - (662b-661b) + (664b-663b)\n" \
".endif\n" ".endif\n"
...@@ -117,9 +117,9 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ...@@ -117,9 +117,9 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
662: .pushsection .altinstructions, "a" 662: .pushsection .altinstructions, "a"
altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f
.popsection .popsection
.pushsection .altinstr_replacement, "ax" .subsection 1
663: \insn2 663: \insn2
664: .popsection 664: .previous
.org . - (664b-663b) + (662b-661b) .org . - (664b-663b) + (662b-661b)
.org . - (662b-661b) + (664b-663b) .org . - (662b-661b) + (664b-663b)
.endif .endif
...@@ -160,7 +160,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ...@@ -160,7 +160,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
.pushsection .altinstructions, "a" .pushsection .altinstructions, "a"
altinstruction_entry 663f, 661f, \cap, 664f-663f, 662f-661f altinstruction_entry 663f, 661f, \cap, 664f-663f, 662f-661f
.popsection .popsection
.pushsection .altinstr_replacement, "ax" .subsection 1
.align 2 /* So GAS knows label 661 is suitably aligned */ .align 2 /* So GAS knows label 661 is suitably aligned */
661: 661:
.endm .endm
...@@ -179,9 +179,9 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ...@@ -179,9 +179,9 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
.macro alternative_else .macro alternative_else
662: 662:
.if .Lasm_alt_mode==0 .if .Lasm_alt_mode==0
.pushsection .altinstr_replacement, "ax" .subsection 1
.else .else
.popsection .previous
.endif .endif
663: 663:
.endm .endm
...@@ -192,7 +192,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ...@@ -192,7 +192,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
.macro alternative_endif .macro alternative_endif
664: 664:
.if .Lasm_alt_mode==0 .if .Lasm_alt_mode==0
.popsection .previous
.endif .endif
.org . - (664b-663b) + (662b-661b) .org . - (664b-663b) + (662b-661b)
.org . - (662b-661b) + (664b-663b) .org . - (662b-661b) + (664b-663b)
......
...@@ -165,9 +165,6 @@ SECTIONS ...@@ -165,9 +165,6 @@ SECTIONS
*(.altinstructions) *(.altinstructions)
__alt_instructions_end = .; __alt_instructions_end = .;
} }
.altinstr_replacement : {
*(.altinstr_replacement)
}
. = ALIGN(SEGMENT_ALIGN); . = ALIGN(SEGMENT_ALIGN);
__inittext_end = .; __inittext_end = .;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment