Skip to content

Commit d9e84fb

Browse files
solbjorntsbogend
authored andcommitted
MIPS: relocatable: optimize the relocation process
For now, vmlinux relocation functions for relocatable kernel are implemented as an array of handlers of a particular type. Convert that array into a single switch-case function to: - remove unused arguments; - change the return type of simple handlers to void; - remove the array and don't use any data at all; - avoid using indirect calls; - allow the compiler to inline and greatly optimize the relocation function[s]; and also mark do_relocations() and show_kernel_relocation() static as they aren't used anywhere else. The result on MIPS32 R2 with GCC 10.2 -O2 is: scripts/bloat-o-meter -c arch/mips/kernel/__relocate.o arch/mips/kernel/relocate.o add/remove: 0/6 grow/shrink: 1/0 up/down: 356/-640 (-284) Function old new delta relocate_kernel 852 1208 +356 apply_r_mips_32_rel 20 - -20 apply_r_mips_hi16_rel 40 - -40 apply_r_mips_64_rel 44 - -44 apply_r_mips_26_rel 144 - -144 show_kernel_relocation 164 - -164 do_relocations 228 - -228 Total: Before=1780, After=1496, chg -15.96% add/remove: 0/1 grow/shrink: 0/0 up/down: 0/-76 (-76) Data old new delta reloc_handlers_rel 76 - -76 Total: Before=92, After=16, chg -82.61% add/remove: 0/0 grow/shrink: 0/0 up/down: 0/0 (0) RO Data old new delta Total: Before=0, After=0, chg +0.00% All functions were collapsed into the main one, relocate_kernel(). Signed-off-by: Alexander Lobakin <alobakin@pm.me> Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
1 parent 049a68e commit d9e84fb

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

arch/mips/kernel/relocate.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,14 @@ static void __init sync_icache(void *kbase, unsigned long kernel_length)
7070
__sync();
7171
}
7272

73-
static int __init apply_r_mips_64_rel(u32 *loc_orig, u32 *loc_new, long offset)
73+
static void __init apply_r_mips_64_rel(u32 *loc_new, long offset)
7474
{
7575
*(u64 *)loc_new += offset;
76-
77-
return 0;
7876
}
7977

80-
static int __init apply_r_mips_32_rel(u32 *loc_orig, u32 *loc_new, long offset)
78+
static void __init apply_r_mips_32_rel(u32 *loc_new, long offset)
8179
{
8280
*loc_new += offset;
83-
84-
return 0;
8581
}
8682

8783
static int __init apply_r_mips_26_rel(u32 *loc_orig, u32 *loc_new, long offset)
@@ -114,25 +110,42 @@ static int __init apply_r_mips_26_rel(u32 *loc_orig, u32 *loc_new, long offset)
114110
}
115111

116112

117-
static int __init apply_r_mips_hi16_rel(u32 *loc_orig, u32 *loc_new, long offset)
113+
static void __init apply_r_mips_hi16_rel(u32 *loc_orig, u32 *loc_new,
114+
long offset)
118115
{
119116
unsigned long insn = *loc_orig;
120117
unsigned long target = (insn & 0xffff) << 16; /* high 16bits of target */
121118

122119
target += offset;
123120

124121
*loc_new = (insn & ~0xffff) | ((target >> 16) & 0xffff);
125-
return 0;
126122
}
127123

128-
static int (*reloc_handlers_rel[]) (u32 *, u32 *, long) __initdata = {
129-
[R_MIPS_64] = apply_r_mips_64_rel,
130-
[R_MIPS_32] = apply_r_mips_32_rel,
131-
[R_MIPS_26] = apply_r_mips_26_rel,
132-
[R_MIPS_HI16] = apply_r_mips_hi16_rel,
133-
};
124+
static int __init reloc_handler(u32 type, u32 *loc_orig, u32 *loc_new,
125+
long offset)
126+
{
127+
switch (type) {
128+
case R_MIPS_64:
129+
apply_r_mips_64_rel(loc_new, offset);
130+
break;
131+
case R_MIPS_32:
132+
apply_r_mips_32_rel(loc_new, offset);
133+
break;
134+
case R_MIPS_26:
135+
return apply_r_mips_26_rel(loc_orig, loc_new, offset);
136+
case R_MIPS_HI16:
137+
apply_r_mips_hi16_rel(loc_orig, loc_new, offset);
138+
break;
139+
default:
140+
pr_err("Unhandled relocation type %d at 0x%pK\n", type,
141+
loc_orig);
142+
return -ENOEXEC;
143+
}
134144

135-
int __init do_relocations(void *kbase_old, void *kbase_new, long offset)
145+
return 0;
146+
}
147+
148+
static int __init do_relocations(void *kbase_old, void *kbase_new, long offset)
136149
{
137150
u32 *r;
138151
u32 *loc_orig;
@@ -149,14 +162,7 @@ int __init do_relocations(void *kbase_old, void *kbase_new, long offset)
149162
loc_orig = kbase_old + ((*r & 0x00ffffff) << 2);
150163
loc_new = RELOCATED(loc_orig);
151164

152-
if (reloc_handlers_rel[type] == NULL) {
153-
/* Unsupported relocation */
154-
pr_err("Unhandled relocation type %d at 0x%pK\n",
155-
type, loc_orig);
156-
return -ENOEXEC;
157-
}
158-
159-
res = reloc_handlers_rel[type](loc_orig, loc_new, offset);
165+
res = reloc_handler(type, loc_orig, loc_new, offset);
160166
if (res)
161167
return res;
162168
}
@@ -412,7 +418,7 @@ void *__init relocate_kernel(void)
412418
/*
413419
* Show relocation information on panic.
414420
*/
415-
void show_kernel_relocation(const char *level)
421+
static void show_kernel_relocation(const char *level)
416422
{
417423
unsigned long offset;
418424

0 commit comments

Comments
 (0)