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 @@ -12,6 +12,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,224 @@ 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);
190 + kmemleak_alloc(ptr, size, 1, GFP_KERNEL);
195 +static inline bool is_phys_addr(void *ptr)
198 + return (KSEGX((unsigned long)ptr) == CKSEG0);
200 + return (KSEGX(ptr) == KSEG0);
204 +/* Free memory returned from module_alloc */
205 +void module_memfree(void *module_region)
207 + if (is_phys_addr(module_region)) {
208 + kmemleak_free(module_region);
209 + free_phys(module_region);
211 + vfree(module_region);
215 +static void *__module_alloc(int size, bool phys)
220 + ptr = kmalloc(size, GFP_KERNEL);
222 + ptr = vmalloc(size);
226 +static void __module_free(void *ptr)
228 + if (is_phys_addr(ptr))
234 +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
235 + char *secstrings, struct module *mod)
237 + unsigned int symindex = 0;
238 + unsigned int core_size, init_size;
241 + mod->arch.phys_plt_offset = 0;
242 + mod->arch.virt_plt_offset = 0;
243 + mod->arch.phys_plt_tbl = NULL;
244 + mod->arch.virt_plt_tbl = NULL;
246 + if (IS_ENABLED(CONFIG_64BIT))
249 + for (i = 1; i < hdr->e_shnum; i++)
250 + if (sechdrs[i].sh_type == SHT_SYMTAB)
253 + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
254 + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
256 + if ((core_size + init_size) == 0)
259 + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
260 + if (!mod->arch.phys_plt_tbl)
263 + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
264 + if (!mod->arch.virt_plt_tbl) {
265 + __module_free(mod->arch.phys_plt_tbl);
266 + mod->arch.phys_plt_tbl = NULL;
273 static int apply_r_mips_none(struct module *me, u32 *location,
274 u32 base, Elf_Addr v, bool rela)
275 @@ -67,9 +277,40 @@ static int apply_r_mips_32(struct module
279 +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
280 + void *start, Elf_Addr v)
282 + unsigned *tramp = start + *plt_offset;
283 + *plt_offset += 4 * sizeof(int);
285 + /* adjust carry for addiu */
286 + if (v & 0x00008000)
289 + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
290 + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
291 + tramp[2] = 0x03200008; /* jr t9 */
292 + tramp[3] = 0x00000000; /* nop */
294 + return (Elf_Addr) tramp;
297 +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
299 + if (is_phys_addr(location))
300 + return add_plt_entry_to(&me->arch.phys_plt_offset,
301 + me->arch.phys_plt_tbl, v);
303 + return add_plt_entry_to(&me->arch.virt_plt_offset,
304 + me->arch.virt_plt_tbl, v);
308 static int apply_r_mips_26(struct module *me, u32 *location,
309 u32 base, Elf_Addr v, bool rela)
311 + u32 ofs = base & 0x03ffffff;
314 pr_err("module %s: dangerous R_MIPS_26 relocation\n",
316 @@ -77,13 +318,17 @@ static int apply_r_mips_26(struct module
319 if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
320 - pr_err("module %s: relocation overflow\n",
323 + v = add_plt_entry(me, location, v + (ofs << 2));
325 + pr_err("module %s: relocation overflow\n",
332 *location = (*location & ~0x03ffffff) |
333 - ((base + (v >> 2)) & 0x03ffffff);
334 + ((ofs + (v >> 2)) & 0x03ffffff);
338 @@ -459,9 +704,36 @@ int module_finalize(const Elf_Ehdr *hdr,
339 list_add(&me->arch.dbe_list, &dbe_list);
340 spin_unlock_irq(&dbe_lock);
343 + /* Get rid of the fixup trampoline if we're running the module
344 + * from physically mapped address space */
345 + if (me->arch.phys_plt_offset == 0) {
346 + __module_free(me->arch.phys_plt_tbl);
347 + me->arch.phys_plt_tbl = NULL;
349 + if (me->arch.virt_plt_offset == 0) {
350 + __module_free(me->arch.virt_plt_tbl);
351 + me->arch.virt_plt_tbl = NULL;
357 +void module_arch_freeing_init(struct module *mod)
359 + if (mod->state == MODULE_STATE_LIVE)
362 + if (mod->arch.phys_plt_tbl) {
363 + __module_free(mod->arch.phys_plt_tbl);
364 + mod->arch.phys_plt_tbl = NULL;
366 + if (mod->arch.virt_plt_tbl) {
367 + __module_free(mod->arch.virt_plt_tbl);
368 + mod->arch.virt_plt_tbl = NULL;
372 void module_arch_cleanup(struct module *mod)
374 spin_lock_irq(&dbe_lock);