Skip to contentSkip to conversation

RISC-V: Load modules within relative jump range of the kernel text.

PATCH In review Shea Levy 3 participants 9 messages Apr 9, 2018 – Nov 26, 2019

Subsystems (3)
lore
Share & tools
lore

Shea Levy

author
#0 lore raw

PATCH 1/1 RISC-V: Load modules within relative jump range of the kernel text.

---

Note that this patch worked in my old modules patchset and seems to be working now, but my kernel boot locks up on top of riscv-for-linus-4.17-mw0 and I don't know if it's due to this patch or something else that's changed in the mean time.

---
 arch/riscv/include/asm/pgtable.h |  9 +++++++++
 arch/riscv/kernel/module.c       | 11 +++++++++++
 2 files changed, 20 insertions(+)
arch/riscv/include/asm/pgtable.h +9 -0
index 16301966d65b..b08ded13364a 100644
@@ -25,6 +25,7 @@
 #include <asm/page.h>
 #include <asm/tlbflush.h>
 #include <linux/mm_types.h>
+#include <linux/sizes.h>
 
 #ifdef CONFIG_64BIT
 #include <asm/pgtable-64.h>
@@ -425,6 +426,14 @@ static inline void pgtable_cache_init(void)
 #define TASK_SIZE VMALLOC_START
 #endif
 
+/*
+ * The module space lives between the addresses given by TASK_SIZE
+ * and PAGE_OFFSET - it must be within 2G of the kernel text.
+ */
+#define MODULES_SIZE		(SZ_128M)
+#define MODULES_VADDR		(PAGE_OFFSET - MODULES_SIZE)
+#define MODULES_END		(VMALLOC_END)
+
 #include <asm-generic/pgtable.h>
 
 #endif /* !__ASSEMBLY__ */
arch/riscv/kernel/module.c +11 -0
index 5dddba301d0a..1b382c7de095 100644
@@ -16,6 +16,8 @@
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/moduleloader.h>
+#include <linux/vmalloc.h>
+#include <asm/pgtable.h>
 
 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
 {
@@ -382,3 +384,12 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 
 	return 0;
 }
+
+void *module_alloc(unsigned long size)
+{
+	return __vmalloc_node_range(size, 1, MODULES_VADDR,
+				    MODULES_END, GFP_KERNEL,
+				    PAGE_KERNEL_EXEC, 0,
+				    NUMA_NO_NODE,
+				    __builtin_return_address(0));
+}
Signature · 1 line
2.16.2
author
#1 lore raw

Hi Palmer,

Quoted 68 lines · Shea Levy

---

Note that this patch worked in my old modules patchset and seems to be working now, but my kernel boot locks up on top of riscv-for-linus-4.17-mw0 and I don't know if it's due to this patch or something else that's changed in the mean time.

---
 arch/riscv/include/asm/pgtable.h |  9 +++++++++
 arch/riscv/kernel/module.c       | 11 +++++++++++
 2 files changed, 20 insertions(+)
arch/riscv/include/asm/pgtable.h +9 -0
index 16301966d65b..b08ded13364a 100644
@@ -25,6 +25,7 @@
 #include <asm/page.h>
 #include <asm/tlbflush.h>
 #include <linux/mm_types.h>
+#include <linux/sizes.h>
 
 #ifdef CONFIG_64BIT
 #include <asm/pgtable-64.h>
@@ -425,6 +426,14 @@ static inline void pgtable_cache_init(void)
 #define TASK_SIZE VMALLOC_START
 #endif
 
+/*
+ * The module space lives between the addresses given by TASK_SIZE
+ * and PAGE_OFFSET - it must be within 2G of the kernel text.
+ */
+#define MODULES_SIZE		(SZ_128M)
+#define MODULES_VADDR		(PAGE_OFFSET - MODULES_SIZE)
+#define MODULES_END		(VMALLOC_END)
+
 #include <asm-generic/pgtable.h>
 
 #endif /* !__ASSEMBLY__ */
arch/riscv/kernel/module.c +11 -0
index 5dddba301d0a..1b382c7de095 100644
@@ -16,6 +16,8 @@
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/moduleloader.h>
+#include <linux/vmalloc.h>
+#include <asm/pgtable.h>
 
 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
 {
@@ -382,3 +384,12 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 
 	return 0;
 }
+
+void *module_alloc(unsigned long size)
+{
+	return __vmalloc_node_range(size, 1, MODULES_VADDR,
+				    MODULES_END, GFP_KERNEL,
+				    PAGE_KERNEL_EXEC, 0,
+				    NUMA_NO_NODE,
+				    __builtin_return_address(0));
+}
Signature · 1 line
2.16.2

Any thoughts on this?

Thanks,
Shea
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-riscv/attachments/20180422/40ad4641/attachment.sig>

#2 lore raw
Quoted 59 lines · shea at shealevy.com

Hi Palmer,

Quoted 55 lines · Shea Levy

---

Note that this patch worked in my old modules patchset and seems to be working now, but my kernel boot locks up on top of riscv-for-linus-4.17-mw0 and I don't know if it's due to this patch or something else that's changed in the mean time.

---
 arch/riscv/include/asm/pgtable.h |  9 +++++++++
 arch/riscv/kernel/module.c       | 11 +++++++++++
 2 files changed, 20 insertions(+)
arch/riscv/include/asm/pgtable.h +9 -0
index 16301966d65b..b08ded13364a 100644
@@ -25,6 +25,7 @@
 #include <asm/page.h>
 #include <asm/tlbflush.h>
 #include <linux/mm_types.h>
+#include <linux/sizes.h>
 
 #ifdef CONFIG_64BIT
 #include <asm/pgtable-64.h>
@@ -425,6 +426,14 @@ static inline void pgtable_cache_init(void)
 #define TASK_SIZE VMALLOC_START
 #endif
 
+/*
+ * The module space lives between the addresses given by TASK_SIZE
+ * and PAGE_OFFSET - it must be within 2G of the kernel text.
+ */
+#define MODULES_SIZE		(SZ_128M)
+#define MODULES_VADDR		(PAGE_OFFSET - MODULES_SIZE)
+#define MODULES_END		(VMALLOC_END)
+
 #include <asm-generic/pgtable.h>
 
 #endif /* !__ASSEMBLY__ */
arch/riscv/kernel/module.c +2 -0
index 5dddba301d0a..1b382c7de095 100644
@@ -16,6 +16,8 @@
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/moduleloader.h>
+#include <linux/vmalloc.h>
+#include <asm/pgtable.h>
 
 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
 {
@@ -382,3 +384,12 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 
Quoted 15 lines
Quoted 13 lines
arch/riscv/kernel/module.c +9 -0
 	return 0;
 }
+
+void *module_alloc(unsigned long size)
+{
+	return __vmalloc_node_range(size, 1, MODULES_VADDR,
+				    MODULES_END, GFP_KERNEL,
+				    PAGE_KERNEL_EXEC, 0,
+				    NUMA_NO_NODE,
+				    __builtin_return_address(0));
+}
Signature · 1 line
2.16.2

Any thoughts on this?

The concept looks good, but does this actually keep the modules within 2GiB of the text if PAGE_OFFSET is large?

author
#3 lore raw

Hi Palmer,

Quoted 76 lines · Palmer Dabbelt
Quoted 59 lines · shea at shealevy.com

Hi Palmer,

Quoted 55 lines · Shea Levy

---

Note that this patch worked in my old modules patchset and seems to be working now, but my kernel boot locks up on top of riscv-for-linus-4.17-mw0 and I don't know if it's due to this patch or something else that's changed in the mean time.

---
 arch/riscv/include/asm/pgtable.h |  9 +++++++++
 arch/riscv/kernel/module.c       | 11 +++++++++++
 2 files changed, 20 insertions(+)
arch/riscv/include/asm/pgtable.h +9 -0
index 16301966d65b..b08ded13364a 100644
@@ -25,6 +25,7 @@
 #include <asm/page.h>
 #include <asm/tlbflush.h>
 #include <linux/mm_types.h>
+#include <linux/sizes.h>
 
 #ifdef CONFIG_64BIT
 #include <asm/pgtable-64.h>
@@ -425,6 +426,14 @@ static inline void pgtable_cache_init(void)
 #define TASK_SIZE VMALLOC_START
 #endif
 
+/*
+ * The module space lives between the addresses given by TASK_SIZE
+ * and PAGE_OFFSET - it must be within 2G of the kernel text.
+ */
+#define MODULES_SIZE		(SZ_128M)
+#define MODULES_VADDR		(PAGE_OFFSET - MODULES_SIZE)
+#define MODULES_END		(VMALLOC_END)
+
 #include <asm-generic/pgtable.h>
 
 #endif /* !__ASSEMBLY__ */
arch/riscv/kernel/module.c +2 -0
index 5dddba301d0a..1b382c7de095 100644
@@ -16,6 +16,8 @@
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/moduleloader.h>
+#include <linux/vmalloc.h>
+#include <asm/pgtable.h>
 
 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
 {
@@ -382,3 +384,12 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 
Quoted 15 lines
Quoted 13 lines
arch/riscv/kernel/module.c +9 -0
 	return 0;
 }
+
+void *module_alloc(unsigned long size)
+{
+	return __vmalloc_node_range(size, 1, MODULES_VADDR,
+				    MODULES_END, GFP_KERNEL,
+				    PAGE_KERNEL_EXEC, 0,
+				    NUMA_NO_NODE,
+				    __builtin_return_address(0));
+}
Signature · 1 line
2.16.2

Any thoughts on this?

Quoted 2 lines

The concept looks good, but does this actually keep the modules within 2GiB of the text if PAGE_OFFSET is large?

It's been some time since I wrote this, but I thought PAGE_OFFSET was where the kernel text *started*? So unless the text itself is bigger than 2G - 128 M, in which case we're SOL anyway, it seems like this should work. Is there something better we can do, without a large memory model?

Thanks,
Shea
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-riscv/attachments/20180424/a609d868/attachment.sig>

author
#4 lore raw

Hi Palmer,

Quoted 83 lines · Shea Levy

Hi Palmer,

Quoted 76 lines · Palmer Dabbelt
Quoted 59 lines · shea at shealevy.com

Hi Palmer,

Quoted 55 lines · Shea Levy

---

Note that this patch worked in my old modules patchset and seems to be working now, but my kernel boot locks up on top of riscv-for-linus-4.17-mw0 and I don't know if it's due to this patch or something else that's changed in the mean time.

---
 arch/riscv/include/asm/pgtable.h |  9 +++++++++
 arch/riscv/kernel/module.c       | 11 +++++++++++
 2 files changed, 20 insertions(+)
arch/riscv/include/asm/pgtable.h +9 -0
index 16301966d65b..b08ded13364a 100644
@@ -25,6 +25,7 @@
 #include <asm/page.h>
 #include <asm/tlbflush.h>
 #include <linux/mm_types.h>
+#include <linux/sizes.h>
 
 #ifdef CONFIG_64BIT
 #include <asm/pgtable-64.h>
@@ -425,6 +426,14 @@ static inline void pgtable_cache_init(void)
 #define TASK_SIZE VMALLOC_START
 #endif
 
+/*
+ * The module space lives between the addresses given by TASK_SIZE
+ * and PAGE_OFFSET - it must be within 2G of the kernel text.
+ */
+#define MODULES_SIZE		(SZ_128M)
+#define MODULES_VADDR		(PAGE_OFFSET - MODULES_SIZE)
+#define MODULES_END		(VMALLOC_END)
+
 #include <asm-generic/pgtable.h>
 
 #endif /* !__ASSEMBLY__ */
arch/riscv/kernel/module.c +2 -0
index 5dddba301d0a..1b382c7de095 100644
@@ -16,6 +16,8 @@
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/moduleloader.h>
+#include <linux/vmalloc.h>
+#include <asm/pgtable.h>
 
 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
 {
@@ -382,3 +384,12 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 
Quoted 15 lines
Quoted 13 lines
arch/riscv/kernel/module.c +9 -0
 	return 0;
 }
+
+void *module_alloc(unsigned long size)
+{
+	return __vmalloc_node_range(size, 1, MODULES_VADDR,
+				    MODULES_END, GFP_KERNEL,
+				    PAGE_KERNEL_EXEC, 0,
+				    NUMA_NO_NODE,
+				    __builtin_return_address(0));
+}
Signature · 1 line
2.16.2

Any thoughts on this?

Quoted 2 lines

The concept looks good, but does this actually keep the modules within 2GiB of the text if PAGE_OFFSET is large?

Quoted 8 lines

It's been some time since I wrote this, but I thought PAGE_OFFSET was where the kernel text *started*? So unless the text itself is bigger than 2G - 128 M, in which case we're SOL anyway, it seems like this should work. Is there something better we can do, without a large memory model?

Thanks,
Shea

Any further thoughts on this?

Thanks,
Shea
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-riscv/attachments/20180509/1a390a03/attachment.sig>

#5 lore raw
Quoted 94 lines · Shea Levy

Hi Palmer,

Quoted 89 lines · Shea Levy

Hi Palmer,

Quoted 76 lines · Palmer Dabbelt
Quoted 59 lines · On Sun, 22 Apr 2018 05:53:56 PDT (-0700), [email protected] wrote:

Hi Palmer,

Quoted 55 lines · Shea Levy

---

Note that this patch worked in my old modules patchset and seems to be working now, but my kernel boot locks up on top of riscv-for-linus-4.17-mw0 and I don't know if it's due to this patch or something else that's changed in the mean time.

---
 arch/riscv/include/asm/pgtable.h |  9 +++++++++
 arch/riscv/kernel/module.c       | 11 +++++++++++
 2 files changed, 20 insertions(+)
arch/riscv/include/asm/pgtable.h +9 -0
index 16301966d65b..b08ded13364a 100644
@@ -25,6 +25,7 @@
 #include <asm/page.h>
 #include <asm/tlbflush.h>
 #include <linux/mm_types.h>
+#include <linux/sizes.h>
 
 #ifdef CONFIG_64BIT
 #include <asm/pgtable-64.h>
@@ -425,6 +426,14 @@ static inline void pgtable_cache_init(void)
 #define TASK_SIZE VMALLOC_START
 #endif
 
+/*
+ * The module space lives between the addresses given by TASK_SIZE
+ * and PAGE_OFFSET - it must be within 2G of the kernel text.
+ */
+#define MODULES_SIZE              (SZ_128M)
+#define MODULES_VADDR             (PAGE_OFFSET - MODULES_SIZE)
+#define MODULES_END               (VMALLOC_END)
+
 #include <asm-generic/pgtable.h>
 
 #endif /* !__ASSEMBLY__ */
arch/riscv/kernel/module.c +2 -0
index 5dddba301d0a..1b382c7de095 100644
@@ -16,6 +16,8 @@
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/moduleloader.h>
+#include <linux/vmalloc.h>
+#include <asm/pgtable.h>
 
 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
 {
@@ -382,3 +384,12 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 
Quoted 15 lines
Quoted 12 lines
arch/riscv/kernel/module.c +9 -1
   return 0;
 }
+
+void *module_alloc(unsigned long size)
+{
+  return __vmalloc_node_range(size, 1, MODULES_VADDR,
+                              MODULES_END, GFP_KERNEL,
+                              PAGE_KERNEL_EXEC, 0,
+                              NUMA_NO_NODE,
+                              __builtin_return_address(0));
+}
--
Quoted 1 line

2.16.2

Any thoughts on this?

Quoted 2 lines

The concept looks good, but does this actually keep the modules within 2GiB of the text if PAGE_OFFSET is large?

It's been some time since I wrote this, but I thought PAGE_OFFSET was where the kernel text *started*? So unless the text itself is bigger than 2G - 128 M, in which case we're SOL anyway, it seems like this should work. Is there something better we can do, without a large memory model?

Quoted 7 lines
Quoted 2 lines

Thanks,
Shea

Any further thoughts on this?

Thanks,
Shea

Shea,

Waking up the dead (threads)!

I'm hacking on call improvements for the RISC-V BPF JIT. module_alloc() is used under the hood of bpf_jit_binary_alloc(), which in turn is used to allocate the JIT image. The current JIT implementation has to to "load imm64 + jalr" to call kernel syms, since the relative offset is >32b. With your patch, I can use regular jal/auipc+jalr instead. IOW, it would be great if it could be merged. ;-) I'd prefer not having the patch in my BPF JIT series, since that will go through a different tree than Paul's RV one.

Wdyt about brushing of the dust of the patch, and re-send it?

Thanks!
Björn

author
#6 lore raw

Hi Björn,

Unfortunately I'm not sure what more is needed to get this in, and I'm in the middle of a move and won't have easy access to my RISC-V setup for testing. I don't think you can count on me for this one.

Thanks,
Shea

Quoted 116 lines · Björn Töpel
Quoted 94 lines · Shea Levy

Hi Palmer,

Quoted 89 lines · Shea Levy

Hi Palmer,

Quoted 76 lines · Palmer Dabbelt
Quoted 59 lines · On Sun, 22 Apr 2018 05:53:56 PDT (-0700), [email protected] wrote:

Hi Palmer,

Quoted 55 lines · Shea Levy

---

Note that this patch worked in my old modules patchset and seems to be working now, but my kernel boot locks up on top of riscv-for-linus-4.17-mw0 and I don't know if it's due to this patch or something else that's changed in the mean time.

---
 arch/riscv/include/asm/pgtable.h |  9 +++++++++
 arch/riscv/kernel/module.c       | 11 +++++++++++
 2 files changed, 20 insertions(+)
arch/riscv/include/asm/pgtable.h +9 -0
index 16301966d65b..b08ded13364a 100644
@@ -25,6 +25,7 @@
 #include <asm/page.h>
 #include <asm/tlbflush.h>
 #include <linux/mm_types.h>
+#include <linux/sizes.h>
 
 #ifdef CONFIG_64BIT
 #include <asm/pgtable-64.h>
@@ -425,6 +426,14 @@ static inline void pgtable_cache_init(void)
 #define TASK_SIZE VMALLOC_START
 #endif
 
+/*
+ * The module space lives between the addresses given by TASK_SIZE
+ * and PAGE_OFFSET - it must be within 2G of the kernel text.
+ */
+#define MODULES_SIZE              (SZ_128M)
+#define MODULES_VADDR             (PAGE_OFFSET - MODULES_SIZE)
+#define MODULES_END               (VMALLOC_END)
+
 #include <asm-generic/pgtable.h>
 
 #endif /* !__ASSEMBLY__ */
arch/riscv/kernel/module.c +2 -0
index 5dddba301d0a..1b382c7de095 100644
@@ -16,6 +16,8 @@
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/moduleloader.h>
+#include <linux/vmalloc.h>
+#include <asm/pgtable.h>
 
 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
 {
@@ -382,3 +384,12 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 
Quoted 15 lines
Quoted 12 lines
arch/riscv/kernel/module.c +9 -1
   return 0;
 }
+
+void *module_alloc(unsigned long size)
+{
+  return __vmalloc_node_range(size, 1, MODULES_VADDR,
+                              MODULES_END, GFP_KERNEL,
+                              PAGE_KERNEL_EXEC, 0,
+                              NUMA_NO_NODE,
+                              __builtin_return_address(0));
+}
--
Quoted 1 line

2.16.2

Any thoughts on this?

Quoted 2 lines

The concept looks good, but does this actually keep the modules within 2GiB of the text if PAGE_OFFSET is large?

It's been some time since I wrote this, but I thought PAGE_OFFSET was where the kernel text *started*? So unless the text itself is bigger than 2G - 128 M, in which case we're SOL anyway, it seems like this should work. Is there something better we can do, without a large memory model?

Quoted 7 lines
Quoted 2 lines

Thanks,
Shea

Any further thoughts on this?

Thanks,
Shea

Shea,

Waking up the dead (threads)!

I'm hacking on call improvements for the RISC-V BPF JIT. module_alloc() is used under the hood of bpf_jit_binary_alloc(), which in turn is used to allocate the JIT image. The current JIT implementation has to to "load imm64 + jalr" to call kernel syms, since the relative offset is >32b. With your patch, I can use regular jal/auipc+jalr instead. IOW, it would be great if it could be merged. ;-) I'd prefer not having the patch in my BPF JIT series, since that will go through a different tree than Paul's RV one.

Quoted 5 lines

Wdyt about brushing of the dust of the patch, and re-send it?

Thanks!
Björn

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#7 lore raw
Quoted 7 lines · Shea Levy

Hi Björn,

Unfortunately I'm not sure what more is needed to get this in, and I'm in the middle of a move and won't have easy access to my RISC-V setup for testing. I don't think you can count on me for this one.

Thanks for getting back quickly! No worries, I'll pick it up!

Cheers,
Björn

#8 lore raw
Quoted 12 lines · Björn Töpel
Quoted 7 lines · Shea Levy

Hi Björn,

Unfortunately I'm not sure what more is needed to get this in, and I'm in the middle of a move and won't have easy access to my RISC-V setup for testing. I don't think you can count on me for this one.

Thanks for getting back quickly! No worries, I'll pick it up!

I just pulled in your patch in my series [1] (it's not done for submission yet, but passes all tests); Just to get the idea. Reading up on the thread, it looks like we can share some more between the archs (mips).

Thanks,
Björn

[1] https://github.com/bjoto/linux/tree/rv64-bpf-jit-bcc

Quoted 3 lines

Cheers,
Björn