Re: [PATCH v5 3/6] arch/x86: Implement text_alloc() and text_free()

2020-07-24 Thread Jarkko Sakkinen
On Fri, Jul 24, 2020 at 11:22:58AM +0200, Ingo Molnar wrote:
> 
> * Jarkko Sakkinen  wrote:
> 
> > +void text_free(void *region)
> > +{
> > +   /*
> > +* This memory may be RO, and freeing RO memory in an interrupt is not
> > +* supported by vmalloc.
> > +*/
> > +   lockdep_assert_irqs_enabled();
> > +
> > +   vfree(region);
> 
> Had to dig around a bit to find the source of this restriction. Might 
> make sense to clarify this comment to:
> 
>   /*
>* This memory may be read-only, and freeing VM_FLUSH_RESET_PERMS memory
>* in an interrupt is not supported by vmalloc.
>*/

This definitely clarifies and is a better explanation. I updated the
commit accordingly. Thank you.

> 
> Thanks,
> 
>   Ingo

/Jarkko


Re: [PATCH v5 3/6] arch/x86: Implement text_alloc() and text_free()

2020-07-24 Thread Ingo Molnar


* Jarkko Sakkinen  wrote:

> +void text_free(void *region)
> +{
> + /*
> +  * This memory may be RO, and freeing RO memory in an interrupt is not
> +  * supported by vmalloc.
> +  */
> + lockdep_assert_irqs_enabled();
> +
> + vfree(region);

Had to dig around a bit to find the source of this restriction. Might 
make sense to clarify this comment to:

/*
 * This memory may be read-only, and freeing VM_FLUSH_RESET_PERMS memory
 * in an interrupt is not supported by vmalloc.
 */

Thanks,

Ingo


[PATCH v5 3/6] arch/x86: Implement text_alloc() and text_free()

2020-07-23 Thread Jarkko Sakkinen
Implement text_alloc() and text_free() with with the vmalloc API. These can
be used to allocate pages for trampoline code without relying on the module
loader code.

Cc: linux...@kvack.org
Cc: Masami Hiramatsu 
Cc: Andi Kleen 
Suggested-by: Peter Zijlstra 
Signed-off-by: Jarkko Sakkinen 
---
 arch/x86/Kconfig |  3 +++
 arch/x86/kernel/Makefile |  1 +
 arch/x86/kernel/text_alloc.c | 41 
 3 files changed, 45 insertions(+)
 create mode 100644 arch/x86/kernel/text_alloc.c

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 0dea7fdd7a00..a4ee5d1300f6 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -2035,6 +2035,9 @@ config KEXEC_FILE
 config ARCH_HAS_KEXEC_PURGATORY
def_bool KEXEC_FILE
 
+config ARCH_HAS_TEXT_ALLOC
+   def_bool y
+
 config KEXEC_SIG
bool "Verify kernel signature during kexec_file_load() syscall"
depends on KEXEC_FILE
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index e77261db2391..fa1415424ae3 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -68,6 +68,7 @@ obj-y += tsc.o tsc_msr.o io_delay.o rtc.o
 obj-y  += pci-iommu_table.o
 obj-y  += resource.o
 obj-y  += irqflags.o
+obj-y  += text_alloc.o
 
 obj-y  += process.o
 obj-y  += fpu/
diff --git a/arch/x86/kernel/text_alloc.c b/arch/x86/kernel/text_alloc.c
new file mode 100644
index ..f31c2d82c258
--- /dev/null
+++ b/arch/x86/kernel/text_alloc.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *  Kernel module help for x86.
+ *  Copyright (C) 2001 Rusty Russell.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+void *text_alloc(unsigned long size)
+{
+   void *p;
+
+   if (PAGE_ALIGN(size) > MODULES_LEN)
+   return NULL;
+
+   p = __vmalloc_node_range(size, MODULE_ALIGN, MODULES_VADDR, MODULES_END,
+GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
+__builtin_return_address(0));
+
+   if (p && (kasan_module_alloc(p, size) < 0)) {
+   vfree(p);
+   return NULL;
+   }
+
+   return p;
+}
+
+void text_free(void *region)
+{
+   /*
+* This memory may be RO, and freeing RO memory in an interrupt is not
+* supported by vmalloc.
+*/
+   lockdep_assert_irqs_enabled();
+
+   vfree(region);
+}
-- 
2.25.1



[PATCH v5 3/6] arch/x86: Implement text_alloc() and text_free()

2020-07-23 Thread Jarkko Sakkinen
Implement text_alloc() and text_free() with with the vmalloc API. These can
be used to allocate pages for trampoline code without relying on the module
loader code.

Cc: linux...@kvack.org
Cc: Masami Hiramatsu 
Cc: Andi Kleen 
Suggested-by: Peter Zijlstra 
Signed-off-by: Jarkko Sakkinen 
---
 arch/x86/Kconfig |  3 +++
 arch/x86/kernel/Makefile |  1 +
 arch/x86/kernel/text_alloc.c | 41 
 3 files changed, 45 insertions(+)
 create mode 100644 arch/x86/kernel/text_alloc.c

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 0dea7fdd7a00..a4ee5d1300f6 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -2035,6 +2035,9 @@ config KEXEC_FILE
 config ARCH_HAS_KEXEC_PURGATORY
def_bool KEXEC_FILE
 
+config ARCH_HAS_TEXT_ALLOC
+   def_bool y
+
 config KEXEC_SIG
bool "Verify kernel signature during kexec_file_load() syscall"
depends on KEXEC_FILE
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index e77261db2391..fa1415424ae3 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -68,6 +68,7 @@ obj-y += tsc.o tsc_msr.o io_delay.o rtc.o
 obj-y  += pci-iommu_table.o
 obj-y  += resource.o
 obj-y  += irqflags.o
+obj-y  += text_alloc.o
 
 obj-y  += process.o
 obj-y  += fpu/
diff --git a/arch/x86/kernel/text_alloc.c b/arch/x86/kernel/text_alloc.c
new file mode 100644
index ..f31c2d82c258
--- /dev/null
+++ b/arch/x86/kernel/text_alloc.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *  Kernel module help for x86.
+ *  Copyright (C) 2001 Rusty Russell.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+void *text_alloc(unsigned long size)
+{
+   void *p;
+
+   if (PAGE_ALIGN(size) > MODULES_LEN)
+   return NULL;
+
+   p = __vmalloc_node_range(size, MODULE_ALIGN, MODULES_VADDR, MODULES_END,
+GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
+__builtin_return_address(0));
+
+   if (p && (kasan_module_alloc(p, size) < 0)) {
+   vfree(p);
+   return NULL;
+   }
+
+   return p;
+}
+
+void text_free(void *region)
+{
+   /*
+* This memory may be RO, and freeing RO memory in an interrupt is not
+* supported by vmalloc.
+*/
+   lockdep_assert_irqs_enabled();
+
+   vfree(region);
+}
-- 
2.25.1