Skip to content

Commit f7b93d4

Browse files
ardbiesheuvelwilldeacon
authored andcommitted
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: Ard Biesheuvel <[email protected]> Cc: Suzuki K Poulose <[email protected]> Cc: James Morse <[email protected]> Cc: Andre Przywara <[email protected]> Cc: Dave P Martin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent 108447f commit f7b93d4

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

arch/arm64/include/asm/alternative.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
7373
".pushsection .altinstructions,\"a\"\n" \
7474
ALTINSTR_ENTRY(feature) \
7575
".popsection\n" \
76-
".pushsection .altinstr_replacement, \"a\"\n" \
76+
".subsection 1\n" \
7777
"663:\n\t" \
7878
newinstr "\n" \
7979
"664:\n\t" \
80-
".popsection\n\t" \
80+
".previous\n\t" \
8181
".org . - (664b-663b) + (662b-661b)\n\t" \
8282
".org . - (662b-661b) + (664b-663b)\n" \
8383
".endif\n"
@@ -117,9 +117,9 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
117117
662: .pushsection .altinstructions, "a"
118118
altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f
119119
.popsection
120-
.pushsection .altinstr_replacement, "ax"
120+
.subsection 1
121121
663: \insn2
122-
664: .popsection
122+
664: .previous
123123
.org . - (664b-663b) + (662b-661b)
124124
.org . - (662b-661b) + (664b-663b)
125125
.endif
@@ -160,7 +160,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
160160
.pushsection .altinstructions, "a"
161161
altinstruction_entry 663f, 661f, \cap, 664f-663f, 662f-661f
162162
.popsection
163-
.pushsection .altinstr_replacement, "ax"
163+
.subsection 1
164164
.align 2 /* So GAS knows label 661 is suitably aligned */
165165
661:
166166
.endm
@@ -179,9 +179,9 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
179179
.macro alternative_else
180180
662:
181181
.if .Lasm_alt_mode==0
182-
.pushsection .altinstr_replacement, "ax"
182+
.subsection 1
183183
.else
184-
.popsection
184+
.previous
185185
.endif
186186
663:
187187
.endm
@@ -192,7 +192,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
192192
.macro alternative_endif
193193
664:
194194
.if .Lasm_alt_mode==0
195-
.popsection
195+
.previous
196196
.endif
197197
.org . - (664b-663b) + (662b-661b)
198198
.org . - (662b-661b) + (664b-663b)

arch/arm64/kernel/vmlinux.lds.S

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ SECTIONS
165165
*(.altinstructions)
166166
__alt_instructions_end = .;
167167
}
168-
.altinstr_replacement : {
169-
*(.altinstr_replacement)
170-
}
171168

172169
. = ALIGN(SEGMENT_ALIGN);
173170
__inittext_end = .;

0 commit comments

Comments
 (0)