305-mips_module_reloc.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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
  3. lede-commit: 3b3d64743ba2a874df9d70cd19e242205b0a788c
  4. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  5. ---
  6. arch/mips/Makefile | 5 +
  7. arch/mips/include/asm/module.h | 5 +
  8. arch/mips/kernel/module.c | 279 ++++++++++++++++++++++++++++++++++++++++-
  9. 3 files changed, 284 insertions(+), 5 deletions(-)
  10. --- a/arch/mips/Makefile
  11. +++ b/arch/mips/Makefile
  12. @@ -93,8 +93,13 @@ all-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlin
  13. cflags-y += -G 0 -mno-abicalls -fno-pic -pipe -mno-branch-likely
  14. cflags-y += -msoft-float
  15. LDFLAGS_vmlinux += -G 0 -static -n -nostdlib
  16. +ifdef CONFIG_64BIT
  17. KBUILD_AFLAGS_MODULE += -mlong-calls
  18. KBUILD_CFLAGS_MODULE += -mlong-calls
  19. +else
  20. +KBUILD_AFLAGS_MODULE += -mno-long-calls
  21. +KBUILD_CFLAGS_MODULE += -mno-long-calls
  22. +endif
  23. ifeq ($(CONFIG_RELOCATABLE),y)
  24. LDFLAGS_vmlinux += --emit-relocs
  25. --- a/arch/mips/include/asm/module.h
  26. +++ b/arch/mips/include/asm/module.h
  27. @@ -12,6 +12,11 @@ struct mod_arch_specific {
  28. const struct exception_table_entry *dbe_start;
  29. const struct exception_table_entry *dbe_end;
  30. struct mips_hi16 *r_mips_hi16_list;
  31. +
  32. + void *phys_plt_tbl;
  33. + void *virt_plt_tbl;
  34. + unsigned int phys_plt_offset;
  35. + unsigned int virt_plt_offset;
  36. };
  37. typedef uint8_t Elf64_Byte; /* Type for a 8-bit quantity. */
  38. --- a/arch/mips/kernel/module.c
  39. +++ b/arch/mips/kernel/module.c
  40. @@ -44,14 +44,221 @@ struct mips_hi16 {
  41. static LIST_HEAD(dbe_list);
  42. static DEFINE_SPINLOCK(dbe_lock);
  43. -#ifdef MODULE_START
  44. +/*
  45. + * Get the potential max trampolines size required of the init and
  46. + * non-init sections. Only used if we cannot find enough contiguous
  47. + * physically mapped memory to put the module into.
  48. + */
  49. +static unsigned int
  50. +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  51. + const char *secstrings, unsigned int symindex, bool is_init)
  52. +{
  53. + unsigned long ret = 0;
  54. + unsigned int i, j;
  55. + Elf_Sym *syms;
  56. +
  57. + /* Everything marked ALLOC (this includes the exported symbols) */
  58. + for (i = 1; i < hdr->e_shnum; ++i) {
  59. + unsigned int info = sechdrs[i].sh_info;
  60. +
  61. + if (sechdrs[i].sh_type != SHT_REL
  62. + && sechdrs[i].sh_type != SHT_RELA)
  63. + continue;
  64. +
  65. + /* Not a valid relocation section? */
  66. + if (info >= hdr->e_shnum)
  67. + continue;
  68. +
  69. + /* Don't bother with non-allocated sections */
  70. + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  71. + continue;
  72. +
  73. + /* If it's called *.init*, and we're not init, we're
  74. + not interested */
  75. + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
  76. + != is_init)
  77. + continue;
  78. +
  79. + syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
  80. + if (sechdrs[i].sh_type == SHT_REL) {
  81. + Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
  82. + unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
  83. +
  84. + for (j = 0; j < size; ++j) {
  85. + Elf_Sym *sym;
  86. +
  87. + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
  88. + continue;
  89. +
  90. + sym = syms + ELF_MIPS_R_SYM(rel[j]);
  91. + if (!is_init && sym->st_shndx != SHN_UNDEF)
  92. + continue;
  93. +
  94. + ret += 4 * sizeof(int);
  95. + }
  96. + } else {
  97. + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
  98. + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
  99. +
  100. + for (j = 0; j < size; ++j) {
  101. + Elf_Sym *sym;
  102. +
  103. + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
  104. + continue;
  105. +
  106. + sym = syms + ELF_MIPS_R_SYM(rela[j]);
  107. + if (!is_init && sym->st_shndx != SHN_UNDEF)
  108. + continue;
  109. +
  110. + ret += 4 * sizeof(int);
  111. + }
  112. + }
  113. + }
  114. +
  115. + return ret;
  116. +}
  117. +
  118. +#ifndef MODULE_START
  119. +static void *alloc_phys(unsigned long size)
  120. +{
  121. + unsigned order;
  122. + struct page *page;
  123. + struct page *p;
  124. +
  125. + size = PAGE_ALIGN(size);
  126. + order = get_order(size);
  127. +
  128. + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
  129. + __GFP_THISNODE, order);
  130. + if (!page)
  131. + return NULL;
  132. +
  133. + split_page(page, order);
  134. +
  135. + /* mark all pages except for the last one */
  136. + for (p = page; p + 1 < page + (size >> PAGE_SHIFT); ++p)
  137. + set_bit(PG_owner_priv_1, &p->flags);
  138. +
  139. + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
  140. + __free_page(p);
  141. +
  142. + return page_address(page);
  143. +}
  144. +#endif
  145. +
  146. +static void free_phys(void *ptr)
  147. +{
  148. + struct page *page;
  149. + bool free;
  150. +
  151. + page = virt_to_page(ptr);
  152. + do {
  153. + free = test_and_clear_bit(PG_owner_priv_1, &page->flags);
  154. + __free_page(page);
  155. + page++;
  156. + } while (free);
  157. +}
  158. +
  159. +
  160. void *module_alloc(unsigned long size)
  161. {
  162. +#ifdef MODULE_START
  163. return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
  164. GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
  165. __builtin_return_address(0));
  166. +#else
  167. + void *ptr;
  168. +
  169. + if (size == 0)
  170. + return NULL;
  171. +
  172. + ptr = alloc_phys(size);
  173. +
  174. + /* If we failed to allocate physically contiguous memory,
  175. + * fall back to regular vmalloc. The module loader code will
  176. + * create jump tables to handle long jumps */
  177. + if (!ptr)
  178. + return vmalloc(size);
  179. +
  180. + return ptr;
  181. +#endif
  182. }
  183. +
  184. +static inline bool is_phys_addr(void *ptr)
  185. +{
  186. +#ifdef CONFIG_64BIT
  187. + return (KSEGX((unsigned long)ptr) == CKSEG0);
  188. +#else
  189. + return (KSEGX(ptr) == KSEG0);
  190. #endif
  191. +}
  192. +
  193. +/* Free memory returned from module_alloc */
  194. +void module_memfree(void *module_region)
  195. +{
  196. + if (is_phys_addr(module_region))
  197. + free_phys(module_region);
  198. + else
  199. + vfree(module_region);
  200. +}
  201. +
  202. +static void *__module_alloc(int size, bool phys)
  203. +{
  204. + void *ptr;
  205. +
  206. + if (phys)
  207. + ptr = kmalloc(size, GFP_KERNEL);
  208. + else
  209. + ptr = vmalloc(size);
  210. + return ptr;
  211. +}
  212. +
  213. +static void __module_free(void *ptr)
  214. +{
  215. + if (is_phys_addr(ptr))
  216. + kfree(ptr);
  217. + else
  218. + vfree(ptr);
  219. +}
  220. +
  221. +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  222. + char *secstrings, struct module *mod)
  223. +{
  224. + unsigned int symindex = 0;
  225. + unsigned int core_size, init_size;
  226. + int i;
  227. +
  228. + mod->arch.phys_plt_offset = 0;
  229. + mod->arch.virt_plt_offset = 0;
  230. + mod->arch.phys_plt_tbl = NULL;
  231. + mod->arch.virt_plt_tbl = NULL;
  232. +
  233. + if (IS_ENABLED(CONFIG_64BIT))
  234. + return 0;
  235. +
  236. + for (i = 1; i < hdr->e_shnum; i++)
  237. + if (sechdrs[i].sh_type == SHT_SYMTAB)
  238. + symindex = i;
  239. +
  240. + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
  241. + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
  242. +
  243. + if ((core_size + init_size) == 0)
  244. + return 0;
  245. +
  246. + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
  247. + if (!mod->arch.phys_plt_tbl)
  248. + return -ENOMEM;
  249. +
  250. + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
  251. + if (!mod->arch.virt_plt_tbl) {
  252. + __module_free(mod->arch.phys_plt_tbl);
  253. + mod->arch.phys_plt_tbl = NULL;
  254. + return -ENOMEM;
  255. + }
  256. +
  257. + return 0;
  258. +}
  259. static int apply_r_mips_none(struct module *me, u32 *location,
  260. u32 base, Elf_Addr v, bool rela)
  261. @@ -67,9 +274,40 @@ static int apply_r_mips_32(struct module
  262. return 0;
  263. }
  264. +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
  265. + void *start, Elf_Addr v)
  266. +{
  267. + unsigned *tramp = start + *plt_offset;
  268. + *plt_offset += 4 * sizeof(int);
  269. +
  270. + /* adjust carry for addiu */
  271. + if (v & 0x00008000)
  272. + v += 0x10000;
  273. +
  274. + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
  275. + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
  276. + tramp[2] = 0x03200008; /* jr t9 */
  277. + tramp[3] = 0x00000000; /* nop */
  278. +
  279. + return (Elf_Addr) tramp;
  280. +}
  281. +
  282. +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
  283. +{
  284. + if (is_phys_addr(location))
  285. + return add_plt_entry_to(&me->arch.phys_plt_offset,
  286. + me->arch.phys_plt_tbl, v);
  287. + else
  288. + return add_plt_entry_to(&me->arch.virt_plt_offset,
  289. + me->arch.virt_plt_tbl, v);
  290. +
  291. +}
  292. +
  293. static int apply_r_mips_26(struct module *me, u32 *location,
  294. u32 base, Elf_Addr v, bool rela)
  295. {
  296. + u32 ofs = base & 0x03ffffff;
  297. +
  298. if (v % 4) {
  299. pr_err("module %s: dangerous R_MIPS_26 relocation\n",
  300. me->name);
  301. @@ -77,13 +315,17 @@ static int apply_r_mips_26(struct module
  302. }
  303. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  304. - pr_err("module %s: relocation overflow\n",
  305. - me->name);
  306. - return -ENOEXEC;
  307. + v = add_plt_entry(me, location, v + (ofs << 2));
  308. + if (!v) {
  309. + pr_err("module %s: relocation overflow\n",
  310. + me->name);
  311. + return -ENOEXEC;
  312. + }
  313. + ofs = 0;
  314. }
  315. *location = (*location & ~0x03ffffff) |
  316. - ((base + (v >> 2)) & 0x03ffffff);
  317. + ((ofs + (v >> 2)) & 0x03ffffff);
  318. return 0;
  319. }
  320. @@ -459,9 +701,36 @@ int module_finalize(const Elf_Ehdr *hdr,
  321. list_add(&me->arch.dbe_list, &dbe_list);
  322. spin_unlock_irq(&dbe_lock);
  323. }
  324. +
  325. + /* Get rid of the fixup trampoline if we're running the module
  326. + * from physically mapped address space */
  327. + if (me->arch.phys_plt_offset == 0) {
  328. + __module_free(me->arch.phys_plt_tbl);
  329. + me->arch.phys_plt_tbl = NULL;
  330. + }
  331. + if (me->arch.virt_plt_offset == 0) {
  332. + __module_free(me->arch.virt_plt_tbl);
  333. + me->arch.virt_plt_tbl = NULL;
  334. + }
  335. +
  336. return 0;
  337. }
  338. +void module_arch_freeing_init(struct module *mod)
  339. +{
  340. + if (mod->state == MODULE_STATE_LIVE)
  341. + return;
  342. +
  343. + if (mod->arch.phys_plt_tbl) {
  344. + __module_free(mod->arch.phys_plt_tbl);
  345. + mod->arch.phys_plt_tbl = NULL;
  346. + }
  347. + if (mod->arch.virt_plt_tbl) {
  348. + __module_free(mod->arch.virt_plt_tbl);
  349. + mod->arch.virt_plt_tbl = NULL;
  350. + }
  351. +}
  352. +
  353. void module_arch_cleanup(struct module *mod)
  354. {
  355. spin_lock_irq(&dbe_lock);