Skip to content

Commit 249b08e

Browse files
Alexey Dobriyantorvalds
authored andcommitted
elf: init pt_regs pointer later
Get "current_pt_regs" pointer right before usage. Space savings on x86_64: add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-180 (-180) Function old new delta load_elf_binary 5806 5626 -180 !!! Looks like the compiler doesn't know that "current_pt_regs" is stable pointer (because it doesn't know ->stack isn't) even though it knows that "current" is stable pointer. So it saves it in the very beginning and then tries to carry it through a lot of code. Here is what happens here: load_elf_binary() ... mov rax,QWORD PTR gs:0x14c00 mov r13,QWORD PTR [rax+0x18] r13 = current->stack call kmem_cache_alloc # first kmalloc [980 bytes later!] # let's spill that sucker because we need a register # for "load_bias" calculations at # # if (interpreter) { # load_bias = ELF_ET_DYN_BASE; # if (current->flags & PF_RANDOMIZE) # load_bias += arch_mmap_rnd(); # elf_flags |= elf_fixed; # } mov QWORD PTR [rsp+0x68],r13 If this is not _the_ root cause it is still eeeeh. After the patch things become much simpler: mov rax, QWORD PTR gs:0x14c00 # current mov rdx, QWORD PTR [rax+0x18] # current->stack movq [rdx+0x3fb8], 0 # fill pt_regs ... call finalize_exec Link: http://lkml.kernel.org/r/20190419200343.GA19788@avx2 Signed-off-by: Alexey Dobriyan <[email protected]> Tested-by: Andrew Morton <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent d8e7cb3 commit 249b08e

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

fs/binfmt_elf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,12 @@ static int load_elf_binary(struct linux_binprm *bprm)
704704
unsigned long start_code, end_code, start_data, end_data;
705705
unsigned long reloc_func_desc __maybe_unused = 0;
706706
int executable_stack = EXSTACK_DEFAULT;
707-
struct pt_regs *regs = current_pt_regs();
708707
struct {
709708
struct elfhdr elf_ex;
710709
struct elfhdr interp_elf_ex;
711710
} *loc;
712711
struct arch_elf_state arch_state = INIT_ARCH_ELF_STATE;
712+
struct pt_regs *regs;
713713

714714
loc = kmalloc(sizeof(*loc), GFP_KERNEL);
715715
if (!loc) {
@@ -1150,6 +1150,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
11501150
MAP_FIXED | MAP_PRIVATE, 0);
11511151
}
11521152

1153+
regs = current_pt_regs();
11531154
#ifdef ELF_PLAT_INIT
11541155
/*
11551156
* The ABI may specify that certain registers be set up in special

0 commit comments

Comments
 (0)