Skip to content

Commit

Permalink
backport: riscv: optimize ELF relocation function in riscv
Browse files Browse the repository at this point in the history
[ upstream commit: 080c432 ]

The patch can optimize the running times of insmod command by modify ELF
relocation function.
In the 5.10 and latest kernel, when install the riscv ELF drivers which
contains multiple symbol table items to be relocated, kernel takes a lot
of time to execute the relocation. For example, we install a 3+MB driver
need 180+s.
We focus on the riscv architecture handle R_RISCV_HI20 and R_RISCV_LO20
type items relocation function in the arch\riscv\kernel\module.c and
find that there are two-loops in the function. If we modify the begin
number in the second for-loops iteration, we could save significant time
for installation. We install the same 3+MB driver could just need 2s.

Signed-off-by: Amma Lee <lixiaoyun@binary-semi.com>
Signed-off-by: Maxim Kochetkov <fido_max@inbox.ru>
Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
Link: https://lore.kernel.org/r/20231214063906.13612-1-fido_max@inbox.ru
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Han Gao <gaohan@iscas.ac.cn>
  • Loading branch information
fidomax authored and RevySR committed Nov 4, 2024
1 parent 54c6dfc commit c66cd51
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions arch/riscv/kernel/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
Elf_Sym *sym;
u32 *location;
unsigned int i, type;
unsigned int j_idx = 0;
Elf_Addr v;
int res;

Expand Down Expand Up @@ -384,9 +385,10 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
v = sym->st_value + rel[i].r_addend;

if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
unsigned int j;
unsigned int j = j_idx;
bool found = false;

for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
do {
unsigned long hi20_loc =
sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[j].r_offset;
Expand Down Expand Up @@ -415,16 +417,26 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
hi20 = (offset + 0x800) & 0xfffff000;
lo12 = offset - hi20;
v = lo12;
found = true;

break;
}
}
if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {

j++;
if (j > sechdrs[relsec].sh_size / sizeof(*rel))
j = 0;

} while (j_idx != j);

if (!found) {
pr_err(
"%s: Can not find HI20 relocation information\n",
me->name);
return -EINVAL;
}

/* Record the previous j-loop end index */
j_idx = j;
}

res = handler(me, location, v);
Expand Down

0 comments on commit c66cd51

Please sign in to comment.