Skip to contentSkip to conversation

alloc_tag-load-module-tags-into-separate-contiguous-memory.patch removed from -mm tree

merged mm-stable Andrew Morton 1 participant 1 message Nov 6, 2024

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: alloc_tag: load module tags into separate contiguous memory
has been removed from the -mm tree. Its filename was
alloc_tag-load-module-tags-into-separate-contiguous-memory.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Suren Baghdasaryan <[email protected]>
Subject: alloc_tag: load module tags into separate contiguous memory
Date: Wed, 23 Oct 2024 10:07:56 -0700

When a module gets unloaded there is a possibility that some of the allocations it made are still used and therefore the allocation tags corresponding to these allocations are still referenced. As such, the memory for these tags can't be freed. This is currently handled as an abnormal situation and module's data section is not being unloaded. To handle this situation without keeping module's data in memory, allow codetags with longer lifespan than the module to be loaded into their own separate memory. The in-use memory areas and gaps after module unloading in this separate memory are tracked using maple trees. Allocation tags arrange their separate memory so that it is virtually contiguous and that will allow simple allocation tag indexing later on in this patchset. The size of this virtually contiguous memory is set to store up to 100000 allocation tags.

[[email protected]: fix empty codetag module section handling]
[[email protected]: update comment, per Dan]
Cc Ard Biesheuvel <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Borislav Petkov (AMD) <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Daniel Gomez <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc David Rientjes <[email protected]>
Cc Dennis Zhou <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Hubbard <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Kalesh Singh <[email protected]>
Cc Kees Cook <[email protected]>
Cc Kent Overstreet <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Luis Chamberlain <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Rapoport (Microsoft) <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Sourav Panda <[email protected]>
Cc Steven Rostedt (Google) <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Uladzislau Rezki (Sony) <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Xiongwei Song <[email protected]>
Cc Yu Zhao <[email protected]>
Cc Dan Carpenter <[email protected]>

---

 include/asm-generic/codetag.lds.h |   19 ++
 include/linux/alloc_tag.h         |   13 +
 include/linux/codetag.h           |   37 +++-
 kernel/module/main.c              |   84 ++++++---
 lib/alloc_tag.c                   |  249 +++++++++++++++++++++++++---
 lib/codetag.c                     |  100 ++++++++++-
 scripts/module.lds.S              |    5 
 7 files changed, 445 insertions(+), 62 deletions(-)
include/asm-generic/codetag.lds.h renamed from include/asm-generic/codetag.lds.h~alloc_tag-load-module-tags-into-separate-contiguous-memory +19 -0
@@ -11,4 +11,23 @@
 #define CODETAG_SECTIONS()		\
 	SECTION_WITH_BOUNDARIES(alloc_tags)
 
+/*
+ * Module codetags which aren't used after module unload, therefore have the
+ * same lifespan as the module and can be safely unloaded with the module.
+ */
+#define MOD_CODETAG_SECTIONS()
+
+#define MOD_SEPARATE_CODETAG_SECTION(_name)	\
+	.codetag.##_name : {			\
+		SECTION_WITH_BOUNDARIES(_name)	\
+	}
+
+/*
+ * For codetags which might be used after module unload, therefore might stay
+ * longer in memory. Each such codetag type has its own section so that we can
+ * unload them individually once unused.
+ */
+#define MOD_SEPARATE_CODETAG_SECTIONS()		\
+	MOD_SEPARATE_CODETAG_SECTION(alloc_tags)
+
 #endif /* __ASM_GENERIC_CODETAG_LDS_H */
include/linux/alloc_tag.h renamed from include/linux/alloc_tag.h~alloc_tag-load-module-tags-into-separate-contiguous-memory +11 -2
@@ -30,6 +30,13 @@ struct alloc_tag {
 	struct alloc_tag_counters __percpu	*counters;
 } __aligned(8);
 
+struct alloc_tag_module_section {
+	unsigned long start_addr;
+	unsigned long end_addr;
+	/* used size */
+	unsigned long size;
+};
+
 #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
 
 #define CODETAG_EMPTY	((void *)1)
@@ -54,6 +61,8 @@ static inline void set_codetag_empty(uni
 
 #ifdef CONFIG_MEM_ALLOC_PROFILING
 
+#define ALLOC_TAG_SECTION_NAME	"alloc_tags"
+
 struct codetag_bytes {
 	struct codetag *ct;
 	s64 bytes;
@@ -76,7 +85,7 @@ DECLARE_PER_CPU(struct alloc_tag_counter
 
 #define DEFINE_ALLOC_TAG(_alloc_tag)						\
 	static struct alloc_tag _alloc_tag __used __aligned(8)			\
-	__section("alloc_tags") = {						\
+	__section(ALLOC_TAG_SECTION_NAME) = {					\
 		.ct = CODE_TAG_INIT,						\
 		.counters = &_shared_alloc_tag };
 
@@ -85,7 +94,7 @@ DECLARE_PER_CPU(struct alloc_tag_counter
 #define DEFINE_ALLOC_TAG(_alloc_tag)						\
 	static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr);	\
 	static struct alloc_tag _alloc_tag __used __aligned(8)			\
-	__section("alloc_tags") = {						\
+	__section(ALLOC_TAG_SECTION_NAME) = {					\
 		.ct = CODE_TAG_INIT,						\
 		.counters = &_alloc_tag_cntr };
 
include/linux/codetag.h renamed from include/linux/codetag.h~alloc_tag-load-module-tags-into-separate-contiguous-memory +32 -5
@@ -35,8 +35,15 @@ struct codetag_type_desc {
 	size_t tag_size;
 	void (*module_load)(struct codetag_type *cttype,
 			    struct codetag_module *cmod);
-	bool (*module_unload)(struct codetag_type *cttype,
+	void (*module_unload)(struct codetag_type *cttype,
 			      struct codetag_module *cmod);
+#ifdef CONFIG_MODULES
+	void (*module_replaced)(struct module *mod, struct module *new_mod);
+	bool (*needs_section_mem)(struct module *mod, unsigned long size);
+	void *(*alloc_section_mem)(struct module *mod, unsigned long size,
+				   unsigned int prepend, unsigned long align);
+	void (*free_section_mem)(struct module *mod, bool used);
+#endif
 };
 
 struct codetag_iterator {
@@ -71,11 +78,31 @@ struct codetag_type *
 codetag_register_type(const struct codetag_type_desc *desc);
 
 #if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES)
+
+bool codetag_needs_module_section(struct module *mod, const char *name,
+				  unsigned long size);
+void *codetag_alloc_module_section(struct module *mod, const char *name,
+				   unsigned long size, unsigned int prepend,
+				   unsigned long align);
+void codetag_free_module_sections(struct module *mod);
+void codetag_module_replaced(struct module *mod, struct module *new_mod);
 void codetag_load_module(struct module *mod);
-bool codetag_unload_module(struct module *mod);
-#else
+void codetag_unload_module(struct module *mod);
+
+#else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
+
+static inline bool
+codetag_needs_module_section(struct module *mod, const char *name,
+			     unsigned long size) { return false; }
+static inline void *
+codetag_alloc_module_section(struct module *mod, const char *name,
+			     unsigned long size, unsigned int prepend,
+			     unsigned long align) { return NULL; }
+static inline void codetag_free_module_sections(struct module *mod) {}
+static inline void codetag_module_replaced(struct module *mod, struct module *new_mod) {}
 static inline void codetag_load_module(struct module *mod) {}
-static inline bool codetag_unload_module(struct module *mod) { return true; }
-#endif
+static inline void codetag_unload_module(struct module *mod) {}
+
+#endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */
 
 #endif /* _LINUX_CODETAG_H */
kernel/module/main.c renamed from kernel/module/main.c~alloc_tag-load-module-tags-into-separate-contiguous-memory +58 -26

175 lines not on this page · raw message

lib/alloc_tag.c renamed from lib/alloc_tag.c~alloc_tag-load-module-tags-into-separate-contiguous-memory +229 -20

285 lines not on this page · raw message

lib/codetag.c renamed from lib/codetag.c~alloc_tag-load-module-tags-into-separate-contiguous-memory +93 -7

130 lines not on this page · raw message

scripts/module.lds.S renamed from scripts/module.lds.S~alloc_tag-load-module-tags-into-separate-contiguous-memory +3 -2
@@ -50,7 +50,7 @@ SECTIONS {
 	.data : {
 		*(.data .data.[0-9a-zA-Z_]*)
 		*(.data..L*)
-		CODETAG_SECTIONS()
+		MOD_CODETAG_SECTIONS()
 	}
 
 	.rodata : {
@@ -59,9 +59,10 @@ SECTIONS {
 	}
 #else
 	.data : {
-		CODETAG_SECTIONS()
+		MOD_CODETAG_SECTIONS()
 	}
 #endif
+	MOD_SEPARATE_CODETAG_SECTIONS()
 }
 
 /* bring in arch-specific sections */

_

Patches currently in -mm which might be from [email protected] are

mm-convert-mm_lock_seq-to-a-proper-seqcount.patch
mm-introduce-mmap_lock_speculation_beginend.patch