live-patching.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: madvenka@linux.microsoft.com
To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com,
	ardb@kernel.org, nobuta.keiya@fujitsu.com,
	sjitindarsingh@gmail.com, catalin.marinas@arm.com,
	will@kernel.org, jmorris@namei.org,
	linux-arm-kernel@lists.infradead.org,
	live-patching@vger.kernel.org, linux-kernel@vger.kernel.org,
	madvenka@linux.microsoft.com
Subject: [RFC PATCH v1 5/9] dwarf: Implement DWARF support for modules
Date: Thu,  7 Apr 2022 15:25:14 -0500	[thread overview]
Message-ID: <20220407202518.19780-6-madvenka@linux.microsoft.com> (raw)
In-Reply-To: <20220407202518.19780-1-madvenka@linux.microsoft.com>

From: "Madhavan T. Venkataraman" <madvenka@linux.microsoft.com>

When a module is loaded, allocate and initialize its struct dwarf_info. When
a module is unloaded, free the same.

Add code in dwarf_lookup() to look up a given address in modules, if vmlinux
does not contain the address.

Signed-off-by: Madhavan T. Venkataraman <madvenka@linux.microsoft.com>
---
 include/linux/dwarf.h       | 18 ++++++++++
 include/linux/module.h      |  3 ++
 kernel/dwarf_fp.c           | 71 ++++++++++++++++++++++++++++++++++---
 kernel/module.c             | 31 ++++++++++++++++
 tools/include/linux/dwarf.h | 18 ++++++++++
 5 files changed, 136 insertions(+), 5 deletions(-)

diff --git a/include/linux/dwarf.h b/include/linux/dwarf.h
index 3df15e79003c..aa44a414b0b6 100644
--- a/include/linux/dwarf.h
+++ b/include/linux/dwarf.h
@@ -11,6 +11,7 @@
 #define _LINUX_DWARF_H
 
 #include <linux/types.h>
+#include <linux/module.h>
 
 /*
  * objtool generates two special sections that contain DWARF information that
@@ -54,11 +55,28 @@ struct dwarf_block {
 
 #ifdef CONFIG_DWARF_FP
 extern struct dwarf_rule	*dwarf_lookup(unsigned long pc);
+#ifdef CONFIG_MODULES
+extern void dwarf_module_alloc(struct module *mod,
+			       struct dwarf_rule *rules, size_t rules_size,
+			       unsigned long *pcs, size_t pcs_size);
+extern void dwarf_module_free(struct module *mod);
+#endif
 #else
 static inline struct dwarf_rule *dwarf_lookup(unsigned long pc)
 {
 	return NULL;
 }
+#ifdef CONFIG_MODULES
+static inline void dwarf_module_alloc(struct module *mod,
+					  struct dwarf_rule *rules,
+					  size_t rules_size,
+					  unsigned long *pcs, size_t pcs_size)
+{
+}
+static inline void dwarf_module_free(struct module *mod)
+{
+}
+#endif
 #endif
 
 #endif /* _LINUX_DWARF_H */
diff --git a/include/linux/module.h b/include/linux/module.h
index c9f1200b2312..bd7c69b82808 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -538,6 +538,9 @@ struct module {
 	struct error_injection_entry *ei_funcs;
 	unsigned int num_ei_funcs;
 #endif
+#ifdef CONFIG_DWARF_FP
+	void *dwarf_info;
+#endif
 } ____cacheline_aligned __randomize_layout;
 #ifndef MODULE_ARCH_INIT
 #define MODULE_ARCH_INIT {}
diff --git a/kernel/dwarf_fp.c b/kernel/dwarf_fp.c
index bb14fbe3f3e1..07d647e828cd 100644
--- a/kernel/dwarf_fp.c
+++ b/kernel/dwarf_fp.c
@@ -164,6 +164,44 @@ static struct dwarf_info *dwarf_alloc(struct dwarf_rule *rules, int nrules,
 	return NULL;
 }
 
+#ifdef CONFIG_MODULES
+
+/*
+ * Errors encountered in this function should not be fatal. All it will mean
+ * is that stack traces through the module would be considered unreliable.
+ */
+void dwarf_module_alloc(struct module *mod,
+			struct dwarf_rule *rules, size_t rules_size,
+			unsigned long *pcs, size_t pcs_size)
+{
+	int		nrules, npcs;
+
+	mod->dwarf_info = NULL;
+
+	nrules = rules_size / sizeof(*rules);
+	npcs = pcs_size / sizeof(*pcs);
+	if (!nrules || npcs != nrules)
+		return;
+
+	mod->dwarf_info = dwarf_alloc(rules, nrules, pcs);
+}
+
+void dwarf_module_free(struct module *mod)
+{
+	struct dwarf_info	*info;
+
+	info = mod->dwarf_info;
+	mod->dwarf_info = NULL;
+
+	if (info) {
+		kfree(info->blocks);
+		kfree(info->offsets);
+		kfree(info);
+	}
+}
+
+#endif
+
 static struct dwarf_rule *dwarf_lookup_rule(struct dwarf_info *info,
 					    unsigned long pc)
 {
@@ -212,13 +250,36 @@ static struct dwarf_rule *dwarf_lookup_rule(struct dwarf_info *info,
 	return NULL;
 }
 
+#ifdef CONFIG_MODULES
+
+static struct dwarf_rule *dwarf_module_lookup_rule(unsigned long pc)
+{
+	struct module	*mod;
+
+	mod = __module_address(pc);
+	if (!mod || !mod->dwarf_info)
+		return NULL;
+
+	return dwarf_lookup_rule(mod->dwarf_info, pc);
+}
+
+#else
+
+static struct dwarf_rule *dwarf_module_lookup_rule(unsigned long pc)
+{
+	return NULL;
+}
+
+#endif
+
 struct dwarf_rule *dwarf_lookup(unsigned long pc)
 {
-	/*
-	 * Currently, only looks up vmlinux. Support for modules will be
-	 * added later.
-	 */
-	return dwarf_lookup_rule(vmlinux_dwarf_info, pc);
+	struct dwarf_rule	*rule;
+
+	rule = dwarf_lookup_rule(vmlinux_dwarf_info, pc);
+	if (!rule)
+		rule = dwarf_module_lookup_rule(pc);
+	return rule;
 }
 
 static int __init dwarf_init_feature(void)
diff --git a/kernel/module.c b/kernel/module.c
index 84a9141a5e15..d9b73995b70a 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -59,6 +59,7 @@
 #include <linux/audit.h>
 #include <uapi/linux/module.h>
 #include "module-internal.h"
+#include <linux/dwarf.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/module.h>
@@ -2153,6 +2154,7 @@ void __weak module_arch_freeing_init(struct module *mod)
 }
 
 static void cfi_cleanup(struct module *mod);
+static void module_dwarf_free(struct module *mod);
 
 /* Free a module, remove from lists, etc. */
 static void free_module(struct module *mod)
@@ -2175,6 +2177,9 @@ static void free_module(struct module *mod)
 	/* Arch-specific cleanup. */
 	module_arch_cleanup(mod);
 
+	/* Dwarf cleanup. */
+	module_dwarf_free(mod);
+
 	/* Module unload stuff */
 	module_unload_free(mod);
 
@@ -3946,6 +3951,7 @@ static int unknown_module_param_cb(char *param, char *val, const char *modname,
 }
 
 static void cfi_init(struct module *mod);
+static void module_dwarf_init(struct module *mod, struct load_info *info);
 
 /*
  * Allocate and load the module: note that size of section 0 is always
@@ -4074,6 +4080,8 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	if (err < 0)
 		goto free_modinfo;
 
+	module_dwarf_init(mod, info);
+
 	flush_module_icache(mod);
 
 	/* Setup CFI for the module. */
@@ -4154,6 +4162,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	kfree(mod->args);
  free_arch_cleanup:
 	cfi_cleanup(mod);
+	module_dwarf_free(mod);
 	module_arch_cleanup(mod);
  free_modinfo:
 	free_modinfo(mod);
@@ -4542,6 +4551,28 @@ static void cfi_cleanup(struct module *mod)
 #endif
 }
 
+static void module_dwarf_init(struct module *mod, struct load_info *info)
+{
+	Elf_Shdr *dwarf_rules, *dwarf_pcs;
+
+	dwarf_rules = &info->sechdrs[find_sec(info, ".dwarf_rules")];
+	dwarf_pcs = &info->sechdrs[find_sec(info, ".dwarf_pcs")];
+
+	if (!dwarf_rules || !dwarf_pcs)
+		return;
+
+	dwarf_module_alloc(mod,
+			   (void *) dwarf_rules->sh_addr,
+			   dwarf_rules->sh_size,
+			   (void *) dwarf_pcs->sh_addr,
+			   dwarf_pcs->sh_size);
+}
+
+static void module_dwarf_free(struct module *mod)
+{
+	dwarf_module_free(mod);
+}
+
 /* Maximum number of characters written by module_flags() */
 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
 
diff --git a/tools/include/linux/dwarf.h b/tools/include/linux/dwarf.h
index 3df15e79003c..aa44a414b0b6 100644
--- a/tools/include/linux/dwarf.h
+++ b/tools/include/linux/dwarf.h
@@ -11,6 +11,7 @@
 #define _LINUX_DWARF_H
 
 #include <linux/types.h>
+#include <linux/module.h>
 
 /*
  * objtool generates two special sections that contain DWARF information that
@@ -54,11 +55,28 @@ struct dwarf_block {
 
 #ifdef CONFIG_DWARF_FP
 extern struct dwarf_rule	*dwarf_lookup(unsigned long pc);
+#ifdef CONFIG_MODULES
+extern void dwarf_module_alloc(struct module *mod,
+			       struct dwarf_rule *rules, size_t rules_size,
+			       unsigned long *pcs, size_t pcs_size);
+extern void dwarf_module_free(struct module *mod);
+#endif
 #else
 static inline struct dwarf_rule *dwarf_lookup(unsigned long pc)
 {
 	return NULL;
 }
+#ifdef CONFIG_MODULES
+static inline void dwarf_module_alloc(struct module *mod,
+					  struct dwarf_rule *rules,
+					  size_t rules_size,
+					  unsigned long *pcs, size_t pcs_size)
+{
+}
+static inline void dwarf_module_free(struct module *mod)
+{
+}
+#endif
 #endif
 
 #endif /* _LINUX_DWARF_H */
-- 
2.25.1


  parent reply	other threads:[~2022-04-07 20:38 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <95691cae4f4504f33d0fc9075541b1e7deefe96f>
2022-01-17 14:55 ` [PATCH v13 00/11] arm64: Reorganize the unwinder and implement stack trace reliability checks madvenka
2022-01-17 14:55   ` [PATCH v13 01/11] arm64: Remove NULL task check from unwind_frame() madvenka
2022-01-17 14:55   ` [PATCH v13 02/11] arm64: Rename unwinder functions madvenka
2022-01-17 14:56   ` [PATCH v13 03/11] arm64: Rename stackframe to unwind_state madvenka
2022-01-17 14:56   ` [PATCH v13 04/11] arm64: Split unwind_init() madvenka
2022-02-02 18:44     ` Mark Brown
2022-02-03  0:26       ` Madhavan T. Venkataraman
2022-02-03  0:39         ` Madhavan T. Venkataraman
2022-02-03 11:29           ` Mark Brown
2022-02-15 13:07     ` Mark Rutland
2022-02-15 18:04       ` Madhavan T. Venkataraman
2022-01-17 14:56   ` [PATCH v13 05/11] arm64: Copy the task argument to unwind_state madvenka
2022-02-02 18:45     ` Mark Brown
2022-02-15 13:22     ` Mark Rutland
2022-02-22 16:53       ` Madhavan T. Venkataraman
2022-01-17 14:56   ` [PATCH v13 06/11] arm64: Use stack_trace_consume_fn and rename args to unwind() madvenka
2022-02-02 18:46     ` Mark Brown
2022-02-03  0:34       ` Madhavan T. Venkataraman
2022-02-03 11:30         ` Mark Brown
2022-02-03 14:45           ` Madhavan T. Venkataraman
2022-02-15 13:39     ` Mark Rutland
2022-02-15 18:12       ` Madhavan T. Venkataraman
2022-03-07 16:51       ` Madhavan T. Venkataraman
2022-03-07 17:01         ` Mark Brown
2022-03-08 22:00           ` Madhavan T. Venkataraman
2022-03-09 11:47             ` Mark Brown
2022-03-09 15:34               ` Madhavan T. Venkataraman
2022-03-10  8:33               ` Miroslav Benes
2022-03-10 12:36                 ` Madhavan T. Venkataraman
2022-03-16  3:43               ` Josh Poimboeuf
2022-04-08 14:44         ` Mark Rutland
2022-04-08 17:58           ` Mark Rutland
2022-04-10 17:42             ` Madhavan T. Venkataraman
2022-04-10 17:33           ` Madhavan T. Venkataraman
2022-04-10 17:45           ` Madhavan T. Venkataraman
2022-01-17 14:56   ` [PATCH v13 07/11] arm64: Make the unwind loop in unwind() similar to other architectures madvenka
2022-01-17 14:56   ` [PATCH v13 08/11] arm64: Introduce stack trace reliability checks in the unwinder madvenka
2022-01-17 14:56   ` [PATCH v13 09/11] arm64: Create a list of SYM_CODE functions, check return PC against list madvenka
2022-01-17 14:56   ` [PATCH v13 10/11] arm64: Introduce arch_stack_walk_reliable() madvenka
2022-01-17 14:56   ` [PATCH v13 11/11] arm64: Select HAVE_RELIABLE_STACKTRACE madvenka
2022-01-25  5:21     ` nobuta.keiya
2022-01-25 13:43       ` Madhavan T. Venkataraman
2022-01-26 10:20         ` nobuta.keiya
2022-01-26 17:14           ` Madhavan T. Venkataraman
2022-01-27  1:13             ` nobuta.keiya
2022-01-26 17:16       ` Mark Brown
2022-04-07 20:25 ` [RFC PATCH v1 0/9] arm64: livepatch: Use DWARF Call Frame Information for frame pointer validation madvenka
2022-04-07 20:25   ` [RFC PATCH v1 1/9] objtool: Parse DWARF Call Frame Information in object files madvenka
2022-04-07 20:25   ` [RFC PATCH v1 2/9] objtool: Generate DWARF rules and place them in a special section madvenka
2022-04-07 20:25   ` [RFC PATCH v1 3/9] dwarf: Build the kernel with DWARF information madvenka
2022-04-07 20:25   ` [RFC PATCH v1 4/9] dwarf: Implement DWARF rule processing in the kernel madvenka
2022-04-07 20:25   ` madvenka [this message]
2022-04-07 20:25   ` [RFC PATCH v1 6/9] arm64: unwinder: Add a reliability check in the unwinder based on DWARF CFI madvenka
2022-04-07 20:25   ` [RFC PATCH v1 7/9] arm64: dwarf: Implement unwind hints madvenka
2022-04-07 20:25   ` [RFC PATCH v1 8/9] dwarf: Miscellaneous changes required for enabling livepatch madvenka
2022-04-07 20:25   ` [RFC PATCH v1 9/9] dwarf: Enable livepatch for ARM64 madvenka
2022-04-08  0:21   ` [RFC PATCH v1 0/9] arm64: livepatch: Use DWARF Call Frame Information for frame pointer validation Josh Poimboeuf
2022-04-08 11:41     ` Peter Zijlstra
2022-04-11 17:26       ` Madhavan T. Venkataraman
2022-04-11 17:18     ` Madhavan T. Venkataraman
2022-04-12  8:32       ` Chen Zhongjin
2022-04-16  0:56         ` Josh Poimboeuf
2022-04-18 12:28           ` Chen Zhongjin
2022-04-18 16:11             ` Josh Poimboeuf
2022-04-18 18:38               ` Madhavan T. Venkataraman
     [not found]       ` <844b3ede-eddb-cbe6-80e0-3529e2da2eb6@huawei.com>
2022-04-12 17:27         ` Madhavan T. Venkataraman
2022-04-16  1:07       ` Josh Poimboeuf
2022-04-14 14:11     ` Madhavan T. Venkataraman
2022-04-08 10:55   ` Peter Zijlstra
2022-04-08 11:54     ` Peter Zijlstra
2022-04-08 14:34       ` Josh Poimboeuf
2022-04-10 17:47     ` Madhavan T. Venkataraman
2022-04-11 16:34       ` Josh Poimboeuf
2022-04-08 12:06   ` Peter Zijlstra
2022-04-11 17:35     ` Madhavan T. Venkataraman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220407202518.19780-6-madvenka@linux.microsoft.com \
    --to=madvenka@linux.microsoft.com \
    --cc=ardb@kernel.org \
    --cc=broonie@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=jmorris@namei.org \
    --cc=jpoimboe@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=nobuta.keiya@fujitsu.com \
    --cc=sjitindarsingh@gmail.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).