1 From: Felix Fietkau <nbd@nbd.name>
2 Subject: mips: replace -mlong-calls with -mno-long-calls to make function calls faster in kernel modules to achieve this, try to
4 lede-commit: 3b3d64743ba2a874df9d70cd19e242205b0a788c
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 arch/mips/Makefile | 5 +
8 arch/mips/include/asm/module.h | 5 +
9 arch/mips/kernel/module.c | 279 ++++++++++++++++++++++++++++++++++++++++-
10 3 files changed, 284 insertions(+), 5 deletions(-)
12 --- a/arch/mips/Makefile
13 +++ b/arch/mips/Makefile
14 @@ -93,8 +93,18 @@ all-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlin
15 cflags-y += -G 0 -mno-abicalls -fno-pic -pipe -mno-branch-likely
16 cflags-y += -msoft-float
17 LDFLAGS_vmlinux += -G 0 -static -n -nostdlib
19 KBUILD_AFLAGS_MODULE += -mlong-calls
20 KBUILD_CFLAGS_MODULE += -mlong-calls
22 + ifdef CONFIG_DYNAMIC_FTRACE
23 + KBUILD_AFLAGS_MODULE += -mlong-calls
24 + KBUILD_CFLAGS_MODULE += -mlong-calls
26 + KBUILD_AFLAGS_MODULE += -mno-long-calls
27 + KBUILD_CFLAGS_MODULE += -mno-long-calls
31 ifeq ($(CONFIG_RELOCATABLE),y)
32 LDFLAGS_vmlinux += --emit-relocs
33 --- a/arch/mips/include/asm/module.h
34 +++ b/arch/mips/include/asm/module.h
35 @@ -11,6 +11,11 @@ struct mod_arch_specific {
36 const struct exception_table_entry *dbe_start;
37 const struct exception_table_entry *dbe_end;
38 struct mips_hi16 *r_mips_hi16_list;
42 + unsigned int phys_plt_offset;
43 + unsigned int virt_plt_offset;
46 typedef uint8_t Elf64_Byte; /* Type for a 8-bit quantity. */
47 --- a/arch/mips/kernel/module.c
48 +++ b/arch/mips/kernel/module.c
49 @@ -44,14 +44,221 @@ struct mips_hi16 {
50 static LIST_HEAD(dbe_list);
51 static DEFINE_SPINLOCK(dbe_lock);
55 + * Get the potential max trampolines size required of the init and
56 + * non-init sections. Only used if we cannot find enough contiguous
57 + * physically mapped memory to put the module into.
60 +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
61 + const char *secstrings, unsigned int symindex, bool is_init)
63 + unsigned long ret = 0;
67 + /* Everything marked ALLOC (this includes the exported symbols) */
68 + for (i = 1; i < hdr->e_shnum; ++i) {
69 + unsigned int info = sechdrs[i].sh_info;
71 + if (sechdrs[i].sh_type != SHT_REL
72 + && sechdrs[i].sh_type != SHT_RELA)
75 + /* Not a valid relocation section? */
76 + if (info >= hdr->e_shnum)
79 + /* Don't bother with non-allocated sections */
80 + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
83 + /* If it's called *.init*, and we're not init, we're
85 + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
89 + syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
90 + if (sechdrs[i].sh_type == SHT_REL) {
91 + Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
92 + unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
94 + for (j = 0; j < size; ++j) {
97 + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
100 + sym = syms + ELF_MIPS_R_SYM(rel[j]);
101 + if (!is_init && sym->st_shndx != SHN_UNDEF)
104 + ret += 4 * sizeof(int);
107 + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
108 + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
110 + for (j = 0; j < size; ++j) {
113 + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
116 + sym = syms + ELF_MIPS_R_SYM(rela[j]);
117 + if (!is_init && sym->st_shndx != SHN_UNDEF)
120 + ret += 4 * sizeof(int);
128 +#ifndef MODULE_START
129 +static void *alloc_phys(unsigned long size)
135 + size = PAGE_ALIGN(size);
136 + order = get_order(size);
138 + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
139 + __GFP_THISNODE, order);
143 + split_page(page, order);
145 + /* mark all pages except for the last one */
146 + for (p = page; p + 1 < page + (size >> PAGE_SHIFT); ++p)
147 + set_bit(PG_owner_priv_1, &p->flags);
149 + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
152 + return page_address(page);
156 +static void free_phys(void *ptr)
161 + page = virt_to_page(ptr);
163 + free = test_and_clear_bit(PG_owner_priv_1, &page->flags);
170 void *module_alloc(unsigned long size)
173 return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
174 GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
175 __builtin_return_address(0));
182 + ptr = alloc_phys(size);
184 + /* If we failed to allocate physically contiguous memory,
185 + * fall back to regular vmalloc. The module loader code will
186 + * create jump tables to handle long jumps */
188 + return vmalloc(size);
194 +static inline bool is_phys_addr(void *ptr)
197 + return (KSEGX((unsigned long)ptr) == CKSEG0);
199 + return (KSEGX(ptr) == KSEG0);
203 +/* Free memory returned from module_alloc */
204 +void module_memfree(void *module_region)
206 + if (is_phys_addr(module_region))
207 + free_phys(module_region);
209 + vfree(module_region);
212 +static void *__module_alloc(int size, bool phys)
217 + ptr = kmalloc(size, GFP_KERNEL);
219 + ptr = vmalloc(size);
223 +static void __module_free(void *ptr)
225 + if (is_phys_addr(ptr))
231 +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
232 + char *secstrings, struct module *mod)
234 + unsigned int symindex = 0;
235 + unsigned int core_size, init_size;
238 + mod->arch.phys_plt_offset = 0;
239 + mod->arch.virt_plt_offset = 0;
240 + mod->arch.phys_plt_tbl = NULL;
241 + mod->arch.virt_plt_tbl = NULL;
243 + if (IS_ENABLED(CONFIG_64BIT))
246 + for (i = 1; i < hdr->e_shnum; i++)
247 + if (sechdrs[i].sh_type == SHT_SYMTAB)
250 + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
251 + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
253 + if ((core_size + init_size) == 0)
256 + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
257 + if (!mod->arch.phys_plt_tbl)
260 + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
261 + if (!mod->arch.virt_plt_tbl) {
262 + __module_free(mod->arch.phys_plt_tbl);
263 + mod->arch.phys_plt_tbl = NULL;
270 int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
272 @@ -65,8 +272,39 @@ static int apply_r_mips_32_rel(struct mo
276 +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
277 + void *start, Elf_Addr v)
279 + unsigned *tramp = start + *plt_offset;
280 + *plt_offset += 4 * sizeof(int);
282 + /* adjust carry for addiu */
283 + if (v & 0x00008000)
286 + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
287 + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
288 + tramp[2] = 0x03200008; /* jr t9 */
289 + tramp[3] = 0x00000000; /* nop */
291 + return (Elf_Addr) tramp;
294 +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
296 + if (is_phys_addr(location))
297 + return add_plt_entry_to(&me->arch.phys_plt_offset,
298 + me->arch.phys_plt_tbl, v);
300 + return add_plt_entry_to(&me->arch.virt_plt_offset,
301 + me->arch.virt_plt_tbl, v);
305 static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
307 + u32 ofs = *location & 0x03ffffff;
310 pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
312 @@ -74,13 +312,17 @@ static int apply_r_mips_26_rel(struct mo
315 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
316 - pr_err("module %s: relocation overflow\n",
319 + v = add_plt_entry(me, location, v + (ofs << 2));
321 + pr_err("module %s: relocation overflow\n",
328 *location = (*location & ~0x03ffffff) |
329 - ((*location + (v >> 2)) & 0x03ffffff);
330 + ((ofs + (v >> 2)) & 0x03ffffff);
334 @@ -349,9 +591,36 @@ int module_finalize(const Elf_Ehdr *hdr,
335 list_add(&me->arch.dbe_list, &dbe_list);
336 spin_unlock_irq(&dbe_lock);
339 + /* Get rid of the fixup trampoline if we're running the module
340 + * from physically mapped address space */
341 + if (me->arch.phys_plt_offset == 0) {
342 + __module_free(me->arch.phys_plt_tbl);
343 + me->arch.phys_plt_tbl = NULL;
345 + if (me->arch.virt_plt_offset == 0) {
346 + __module_free(me->arch.virt_plt_tbl);
347 + me->arch.virt_plt_tbl = NULL;
353 +void module_arch_freeing_init(struct module *mod)
355 + if (mod->state == MODULE_STATE_LIVE)
358 + if (mod->arch.phys_plt_tbl) {
359 + __module_free(mod->arch.phys_plt_tbl);
360 + mod->arch.phys_plt_tbl = NULL;
362 + if (mod->arch.virt_plt_tbl) {
363 + __module_free(mod->arch.virt_plt_tbl);
364 + mod->arch.virt_plt_tbl = NULL;
368 void module_arch_cleanup(struct module *mod)
370 spin_lock_irq(&dbe_lock);