Skip to contentSkip to conversation

arch/x86: kprobes: Remove MODULES dependency

PATCH Superseded Jarkko Sakkinen 1 participant 4 messages Jul 24, 2020

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v5 cover arch/x86: kprobes: Remove MODULES dependency

Remove MODULES dependency by migrating from module_alloc() to the new text_alloc() API. Essentially these changes provide preliminaries for allowing to compile a static kernel with a proper tracing support.

The same API can be used later on in other sites that allocate space for trampolines, and trivially scaled to other arch's. An arch can inform with CONFIG_ARCH_HAS_TEXT_ALLOC that it's providing implementation for text_alloc().

Cc Andi Kleen <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>

v4:
* Squash lock_modules() patches into one.
* Remove fallback versions of text_alloc() and text_free(). Instead, use ARCH_HAS_TEXT_ALLOC at site when required.
* Use lockdep_assert_irqs_enabled() in text_free() instead of WARN_ON(in_interrupt()).

v3:
* Make text_alloc() API disjoint.
* Remove all the possible extra clutter not absolutely required and split into more logical pieces.

Jarkko Sakkinen (6):
kprobes: Remove dependency to the module_mutex
vmalloc: Add text_alloc() and text_free() arch/x86: Implement text_alloc() and text_free() arch/x86: kprobes: Use text_alloc() and text_free()
kprobes: Use text_alloc() and text_free()
kprobes: Remove CONFIG_MODULES dependency

 arch/Kconfig                   |  2 +-
 arch/x86/Kconfig               |  3 ++
 arch/x86/kernel/Makefile       |  1 +
 arch/x86/kernel/kprobes/core.c |  4 +--
 arch/x86/kernel/text_alloc.c   | 41 +++++++++++++++++++++++
 include/linux/module.h         | 32 ++++++++++++++----
 include/linux/vmalloc.h        | 17 ++++++++++
 kernel/kprobes.c               | 61 +++++++++++++++++++++++-----------
 kernel/trace/trace_kprobe.c    | 20 ++++++++---
 9 files changed, 147 insertions(+), 34 deletions(-)
 create mode 100644 arch/x86/kernel/text_alloc.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v5 1/6 kprobes: Remove dependency to the module_mutex

Add lock_modules() and unlock_modules() wrappers for acquiring module_mutex in order to remove the compile time dependency to it.

---
 include/linux/module.h      | 18 ++++++++++++++++++
 kernel/kprobes.c            |  4 ++--
 kernel/trace/trace_kprobe.c |  4 ++--
 3 files changed, 22 insertions(+), 4 deletions(-)
include/linux/module.h +18 -0
index 2e6670860d27..8850b9692b8f 100644
@@ -705,6 +705,16 @@ static inline bool is_livepatch_module(struct module *mod)
 bool is_module_sig_enforced(void);
 void set_module_sig_enforced(void);
 
+static inline void lock_modules(void)
+{
+	mutex_lock(&module_mutex);
+}
+
+static inline void unlock_modules(void)
+{
+	mutex_unlock(&module_mutex);
+}
+
 #else /* !CONFIG_MODULES... */
 
 static inline struct module *__module_address(unsigned long addr)
@@ -852,6 +862,14 @@ void *dereference_module_function_descriptor(struct module *mod, void *ptr)
 	return ptr;
 }
 
+static inline void lock_modules(void)
+{
+}
+
+static inline void unlock_modules(void)
+{
+}
+
 #endif /* CONFIG_MODULES */
 
 #ifdef CONFIG_SYSFS
kernel/kprobes.c +2 -2
index 2e97febeef77..4e46d96d4e16 100644
@@ -564,7 +564,7 @@ static void kprobe_optimizer(struct work_struct *work)
 	cpus_read_lock();
 	mutex_lock(&text_mutex);
 	/* Lock modules while optimizing kprobes */
-	mutex_lock(&module_mutex);
+	lock_modules();
 
 	/*
 	 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
@@ -589,7 +589,7 @@ static void kprobe_optimizer(struct work_struct *work)
 	/* Step 4: Free cleaned kprobes after quiesence period */
 	do_free_cleaned_kprobes();
 
-	mutex_unlock(&module_mutex);
+	unlock_modules();
 	mutex_unlock(&text_mutex);
 	cpus_read_unlock();
 
kernel/trace/trace_kprobe.c +2 -2
index aefb6065b508..710ec6a6aa8f 100644
@@ -122,9 +122,9 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
 	if (!p)
 		return true;
 	*p = '\0';
-	mutex_lock(&module_mutex);
+	lock_modules();
 	ret = !!find_module(tk->symbol);
-	mutex_unlock(&module_mutex);
+	unlock_modules();
 	*p = ':';
 
 	return ret;
Signature · 1 line
2.25.1
#2 lore raw

PATCH v5 2/6 vmalloc: Add text_alloc() and text_free()

In reply to Jarkko Sakkinen

Introduce functions for allocating memory for dynamic trampolines, such as kprobes. An arch can promote the availability of these functions with CONFIG_ARCH_HAS_TEXT_ALLOC.

---
 include/linux/vmalloc.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/linux/vmalloc.h +17 -0
index 0221f852a7e1..6c151a0ac02a 100644
@@ -249,4 +249,21 @@ pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
 int register_vmap_purge_notifier(struct notifier_block *nb);
 int unregister_vmap_purge_notifier(struct notifier_block *nb);
 
+/*
+ * These functions can be optionally implemented by an arch in order to
+ * provide specialized functions for allocating trampoline code.
+ */
+
+#ifdef CONFIG_ARCH_HAS_TEXT_ALLOC
+/*
+ * Allocate memory to be used for trampoline code.
+ */
+void *text_alloc(unsigned long size);
+
+/*
+ * Free memory returned from text_alloc().
+ */
+void text_free(void *region);
+#endif /* CONFIG_ARCH_HAS_TEXT_ALLOC */
+
 #endif /* _LINUX_VMALLOC_H */
Signature · 1 line
2.25.1
#3 lore raw

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

In reply to 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.

---
 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
arch/x86/Kconfig +3 -0
index 0dea7fdd7a00..a4ee5d1300f6 100644
@@ -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
arch/x86/kernel/Makefile +1 -0
index e77261db2391..fa1415424ae3 100644
@@ -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/
arch/x86/kernel/text_alloc.c new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.25.1