Skip to contentSkip to conversation

alloc_tag: avoid execmem_vmap() when !MMU

PATCH Superseded Suren Baghdasaryan 2 participants 5 messages Oct 28, 2024 – Oct 30, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alloc_tag: avoid execmem_vmap() when !MMU

With CONFIG_MMU=n __get_vm_area_node() is not available and memory for the allocation tags cannot be populated as needed. For this case, populate the required memory at initialization time.

---
 include/linux/execmem.h |  2 ++
 lib/alloc_tag.c         | 23 ++++++++++++++++++++++-
 mm/execmem.c            | 32 ++++++++++++++++----------------
 3 files changed, 40 insertions(+), 17 deletions(-)
include/linux/execmem.h +2 -0
index 5a5e2917f870..64130ae19690 100644
@@ -139,6 +139,7 @@ void *execmem_alloc(enum execmem_type type, size_t size);
  */
 void execmem_free(void *ptr);
 
+#ifdef CONFIG_MMU
 /**
  * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
  * @size: size of the virtual mapping in bytes
@@ -148,6 +149,7 @@ void execmem_free(void *ptr);
  * Return: the area descriptor on success or %NULL on failure.
  */
 struct vm_struct *execmem_vmap(size_t size);
+#endif
 
 /**
  * execmem_update_copy - copy an update to executable memory
lib/alloc_tag.c +22 -1
index c1ddac2d29f0..1c74942e6dfd 100644
@@ -268,7 +268,6 @@ void __init alloc_tag_sec_init(void)
 #ifdef CONFIG_MODULES
 
 static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE);
-static struct vm_struct *vm_module_tags;
 /* A dummy object used to indicate an unloaded module */
 static struct module unloaded_mod;
 /* A dummy object used to indicate a module prepended area */
@@ -391,6 +390,9 @@ static bool find_aligned_area(struct ma_state *mas, unsigned long section_size,
 	return false;
 }
 
+#ifdef CONFIG_MMU
+static struct vm_struct *vm_module_tags;
+
 static int vm_module_tags_populate(void)
 {
 	unsigned long phys_size = vm_module_tags->nr_pages << PAGE_SHIFT;
@@ -417,6 +419,13 @@ static int vm_module_tags_populate(void)
 
 	return 0;
 }
+#else
+static int vm_module_tags_populate(void)
+{
+	/* Memory was already allocated */
+	return 0;
+}
+#endif
 
 static void *reserve_module_tags(struct module *mod, unsigned long size,
 				 unsigned int prepend, unsigned long align)
@@ -561,6 +570,7 @@ static void replace_module(struct module *mod, struct module *new_mod)
 
 static int __init alloc_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
 	/* Map space to copy allocation tags */
 	vm_module_tags = execmem_vmap(MODULE_ALLOC_TAG_VMAP_SIZE);
 	if (!vm_module_tags) {
@@ -578,6 +588,13 @@ static int __init alloc_mod_tags_mem(void)
 	}
 
 	module_tags.start_addr = (unsigned long)vm_module_tags->addr;
+#else
+	/* Allocate space to copy allocation tags */
+	module_tags.start_addr = (unsigned long)execmem_alloc(EXECMEM_MODULE_DATA,
+							      MODULE_ALLOC_TAG_VMAP_SIZE);
+	if (!module_tags.start_addr)
+		return -ENOMEM;
+#endif
 	module_tags.end_addr = module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_SIZE;
 	/* Ensure the base is alloc_tag aligned when required for indexing */
 	module_tags.start_addr = alloc_tag_align(module_tags.start_addr);
@@ -587,6 +604,7 @@ static int __init alloc_mod_tags_mem(void)
 
 static void __init free_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
 	int i;
 
 	module_tags.start_addr = 0;
@@ -594,6 +612,9 @@ static void __init free_mod_tags_mem(void)
 		__free_page(vm_module_tags->pages[i]);
 	kfree(vm_module_tags->pages);
 	free_vm_area(vm_module_tags);
+#else
+	execmem_free((void *)module_tags.start_addr);
+#endif
 }
 
 #else /* CONFIG_MODULES */
mm/execmem.c +16 -16
index 5c0f9f2d6f83..317b6a8d35be 100644
@@ -64,6 +64,22 @@ static void *execmem_vmalloc(struct execmem_range *range, size_t size,
 
 	return p;
 }
+
+struct vm_struct *execmem_vmap(size_t size)
+{
+	struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
+	struct vm_struct *area;
+
+	area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+				  range->start, range->end, NUMA_NO_NODE,
+				  GFP_KERNEL, __builtin_return_address(0));
+	if (!area && range->fallback_start)
+		area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+					  range->fallback_start, range->fallback_end,
+					  NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
+
+	return area;
+}
 #else
 static void *execmem_vmalloc(struct execmem_range *range, size_t size,
 			     pgprot_t pgprot, unsigned long vm_flags)
@@ -368,22 +384,6 @@ void execmem_free(void *ptr)
 		vfree(ptr);
 }
 
-struct vm_struct *execmem_vmap(size_t size)
-{
-	struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
-	struct vm_struct *area;
-
-	area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-				  range->start, range->end, NUMA_NO_NODE,
-				  GFP_KERNEL, __builtin_return_address(0));
-	if (!area && range->fallback_start)
-		area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-					  range->fallback_start, range->fallback_end,
-					  NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
-
-	return area;
-}
-
 void *execmem_update_copy(void *dst, const void *src, size_t size)
 {
 	return text_poke_copy(dst, src, size);

base-commit: 92409df3e00a17d6fd5bb27732fa186749725ed5

Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
maintainer
#1 lore raw

Hi Suren,

Quoted 3 lines · Suren Baghdasaryan

With CONFIG_MMU=n __get_vm_area_node() is not available and memory for the allocation tags cannot be populated as needed. For this case, populate the required memory at initialization time.

Do we care that much to have alloc_tags on !MMU configs?

Your patch is great, but it would have been a lot easier to make MEM_ALLOC_PROFILING depend on MMU.

Quoted 166 lines
---
 include/linux/execmem.h |  2 ++
 lib/alloc_tag.c         | 23 ++++++++++++++++++++++-
 mm/execmem.c            | 32 ++++++++++++++++----------------
 3 files changed, 40 insertions(+), 17 deletions(-)
include/linux/execmem.h +2 -0
index 5a5e2917f870..64130ae19690 100644
@@ -139,6 +139,7 @@ void *execmem_alloc(enum execmem_type type, size_t size);
  */
 void execmem_free(void *ptr);
 
+#ifdef CONFIG_MMU
 /**
  * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
  * @size: size of the virtual mapping in bytes
@@ -148,6 +149,7 @@ void execmem_free(void *ptr);
  * Return: the area descriptor on success or %NULL on failure.
  */
 struct vm_struct *execmem_vmap(size_t size);
+#endif
 
 /**
  * execmem_update_copy - copy an update to executable memory
lib/alloc_tag.c +22 -1
index c1ddac2d29f0..1c74942e6dfd 100644
@@ -268,7 +268,6 @@ void __init alloc_tag_sec_init(void)
 #ifdef CONFIG_MODULES
 
 static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE);
-static struct vm_struct *vm_module_tags;
 /* A dummy object used to indicate an unloaded module */
 static struct module unloaded_mod;
 /* A dummy object used to indicate a module prepended area */
@@ -391,6 +390,9 @@ static bool find_aligned_area(struct ma_state *mas, unsigned long section_size,
 	return false;
 }
 
+#ifdef CONFIG_MMU
+static struct vm_struct *vm_module_tags;
+
 static int vm_module_tags_populate(void)
 {
 	unsigned long phys_size = vm_module_tags->nr_pages << PAGE_SHIFT;
@@ -417,6 +419,13 @@ static int vm_module_tags_populate(void)
 
 	return 0;
 }
+#else
+static int vm_module_tags_populate(void)
+{
+	/* Memory was already allocated */
+	return 0;
+}
+#endif
 
 static void *reserve_module_tags(struct module *mod, unsigned long size,
 				 unsigned int prepend, unsigned long align)
@@ -561,6 +570,7 @@ static void replace_module(struct module *mod, struct module *new_mod)
 
 static int __init alloc_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
 	/* Map space to copy allocation tags */
 	vm_module_tags = execmem_vmap(MODULE_ALLOC_TAG_VMAP_SIZE);
 	if (!vm_module_tags) {
@@ -578,6 +588,13 @@ static int __init alloc_mod_tags_mem(void)
 	}
 
 	module_tags.start_addr = (unsigned long)vm_module_tags->addr;
+#else
+	/* Allocate space to copy allocation tags */
+	module_tags.start_addr = (unsigned long)execmem_alloc(EXECMEM_MODULE_DATA,
+							      MODULE_ALLOC_TAG_VMAP_SIZE);
+	if (!module_tags.start_addr)
+		return -ENOMEM;
+#endif
 	module_tags.end_addr = module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_SIZE;
 	/* Ensure the base is alloc_tag aligned when required for indexing */
 	module_tags.start_addr = alloc_tag_align(module_tags.start_addr);
@@ -587,6 +604,7 @@ static int __init alloc_mod_tags_mem(void)
 
 static void __init free_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
 	int i;
 
 	module_tags.start_addr = 0;
@@ -594,6 +612,9 @@ static void __init free_mod_tags_mem(void)
 		__free_page(vm_module_tags->pages[i]);
 	kfree(vm_module_tags->pages);
 	free_vm_area(vm_module_tags);
+#else
+	execmem_free((void *)module_tags.start_addr);
+#endif
 }
 
 #else /* CONFIG_MODULES */
mm/execmem.c +16 -16
index 5c0f9f2d6f83..317b6a8d35be 100644
@@ -64,6 +64,22 @@ static void *execmem_vmalloc(struct execmem_range *range, size_t size,
 
 	return p;
 }
+
+struct vm_struct *execmem_vmap(size_t size)
+{
+	struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
+	struct vm_struct *area;
+
+	area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+				  range->start, range->end, NUMA_NO_NODE,
+				  GFP_KERNEL, __builtin_return_address(0));
+	if (!area && range->fallback_start)
+		area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+					  range->fallback_start, range->fallback_end,
+					  NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
+
+	return area;
+}
 #else
 static void *execmem_vmalloc(struct execmem_range *range, size_t size,
 			     pgprot_t pgprot, unsigned long vm_flags)
@@ -368,22 +384,6 @@ void execmem_free(void *ptr)
 		vfree(ptr);
 }
 
-struct vm_struct *execmem_vmap(size_t size)
-{
-	struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
-	struct vm_struct *area;
-
-	area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-				  range->start, range->end, NUMA_NO_NODE,
-				  GFP_KERNEL, __builtin_return_address(0));
-	if (!area && range->fallback_start)
-		area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-					  range->fallback_start, range->fallback_end,
-					  NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
-
-	return area;
-}
-
 void *execmem_update_copy(void *dst, const void *src, size_t size)
 {
 	return text_poke_copy(dst, src, size);

base-commit: 92409df3e00a17d6fd5bb27732fa186749725ed5

Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
Signature · 2 lines
Sincerely yours,
Mike.
#2 lore raw
Quoted 4 lines · Mike Rapoport

Hi Suren,

On Mon, Oct 28, 2024 at 01:29:35PM -0700, Suren Baghdasaryan wrote:

Quoted 8 lines
Quoted 3 lines

With CONFIG_MMU=n __get_vm_area_node() is not available and memory for the allocation tags cannot be populated as needed. For this case, populate the required memory at initialization time.

Do we care that much to have alloc_tags on !MMU configs?

Your patch is great, but it would have been a lot easier to make MEM_ALLOC_PROFILING depend on MMU.

I didn't want to prevent !MMU users from using allocation profiling but I'm not aware of any current !MMU users. Therefore I have no objections to making MEM_ALLOC_PROFILING dependent on MMU if we indeed don't care about !MMU && MEM_ALLOC_PROFILING configuration. I'll be posting v5 tomorrow with all these fixes, so please let me know which way you prefer. Thanks!

Quoted 171 lines
Quoted 163 lines
---
 include/linux/execmem.h |  2 ++
 lib/alloc_tag.c         | 23 ++++++++++++++++++++++-
 mm/execmem.c            | 32 ++++++++++++++++----------------
 3 files changed, 40 insertions(+), 17 deletions(-)
include/linux/execmem.h +2 -0
index 5a5e2917f870..64130ae19690 100644
@@ -139,6 +139,7 @@ void *execmem_alloc(enum execmem_type type, size_t size);
  */
 void execmem_free(void *ptr);
 
+#ifdef CONFIG_MMU
 /**
  * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
  * @size: size of the virtual mapping in bytes
@@ -148,6 +149,7 @@ void execmem_free(void *ptr);
  * Return: the area descriptor on success or %NULL on failure.
  */
 struct vm_struct *execmem_vmap(size_t size);
+#endif
 
 /**
  * execmem_update_copy - copy an update to executable memory
lib/alloc_tag.c +22 -1
index c1ddac2d29f0..1c74942e6dfd 100644
@@ -268,7 +268,6 @@ void __init alloc_tag_sec_init(void)
 #ifdef CONFIG_MODULES
 
 static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE);
-static struct vm_struct *vm_module_tags;
 /* A dummy object used to indicate an unloaded module */
 static struct module unloaded_mod;
 /* A dummy object used to indicate a module prepended area */
@@ -391,6 +390,9 @@ static bool find_aligned_area(struct ma_state *mas, unsigned long section_size,
      return false;
 }
 
+#ifdef CONFIG_MMU
+static struct vm_struct *vm_module_tags;
+
 static int vm_module_tags_populate(void)
 {
      unsigned long phys_size = vm_module_tags->nr_pages << PAGE_SHIFT;
@@ -417,6 +419,13 @@ static int vm_module_tags_populate(void)
 
      return 0;
 }
+#else
+static int vm_module_tags_populate(void)
+{
+     /* Memory was already allocated */
+     return 0;
+}
+#endif
 
 static void *reserve_module_tags(struct module *mod, unsigned long size,
                               unsigned int prepend, unsigned long align)
@@ -561,6 +570,7 @@ static void replace_module(struct module *mod, struct module *new_mod)
 
 static int __init alloc_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
      /* Map space to copy allocation tags */
      vm_module_tags = execmem_vmap(MODULE_ALLOC_TAG_VMAP_SIZE);
      if (!vm_module_tags) {
@@ -578,6 +588,13 @@ static int __init alloc_mod_tags_mem(void)
      }
 
      module_tags.start_addr = (unsigned long)vm_module_tags->addr;
+#else
+     /* Allocate space to copy allocation tags */
+     module_tags.start_addr = (unsigned long)execmem_alloc(EXECMEM_MODULE_DATA,
+                                                           MODULE_ALLOC_TAG_VMAP_SIZE);
+     if (!module_tags.start_addr)
+             return -ENOMEM;
+#endif
      module_tags.end_addr = module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_SIZE;
      /* Ensure the base is alloc_tag aligned when required for indexing */
      module_tags.start_addr = alloc_tag_align(module_tags.start_addr);
@@ -587,6 +604,7 @@ static int __init alloc_mod_tags_mem(void)
 
 static void __init free_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
      int i;
 
      module_tags.start_addr = 0;
@@ -594,6 +612,9 @@ static void __init free_mod_tags_mem(void)
              __free_page(vm_module_tags->pages[i]);
      kfree(vm_module_tags->pages);
      free_vm_area(vm_module_tags);
+#else
+     execmem_free((void *)module_tags.start_addr);
+#endif
 }
 
 #else /* CONFIG_MODULES */
mm/execmem.c +16 -16
index 5c0f9f2d6f83..317b6a8d35be 100644
@@ -64,6 +64,22 @@ static void *execmem_vmalloc(struct execmem_range *range, size_t size,
 
      return p;
 }
+
+struct vm_struct *execmem_vmap(size_t size)
+{
+     struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
+     struct vm_struct *area;
+
+     area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+                               range->start, range->end, NUMA_NO_NODE,
+                               GFP_KERNEL, __builtin_return_address(0));
+     if (!area && range->fallback_start)
+             area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+                                       range->fallback_start, range->fallback_end,
+                                       NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
+
+     return area;
+}
 #else
 static void *execmem_vmalloc(struct execmem_range *range, size_t size,
                           pgprot_t pgprot, unsigned long vm_flags)
@@ -368,22 +384,6 @@ void execmem_free(void *ptr)
              vfree(ptr);
 }
 
-struct vm_struct *execmem_vmap(size_t size)
-{
-     struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
-     struct vm_struct *area;
-
-     area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-                               range->start, range->end, NUMA_NO_NODE,
-                               GFP_KERNEL, __builtin_return_address(0));
-     if (!area && range->fallback_start)
-             area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-                                       range->fallback_start, range->fallback_end,
-                                       NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
-
-     return area;
-}
-
 void *execmem_update_copy(void *dst, const void *src, size_t size)
 {
      return text_poke_copy(dst, src, size);
Quoted 4 lines

base-commit: 92409df3e00a17d6fd5bb27732fa186749725ed5 --
2.47.0.163.g1226f6d8fa-goog

--
Sincerely yours,
Mike.

maintainer
#3 lore raw
Quoted 14 lines · Suren Baghdasaryan
Quoted 4 lines · Mike Rapoport

Hi Suren,

On Mon, Oct 28, 2024 at 01:29:35PM -0700, Suren Baghdasaryan wrote:

Quoted 8 lines
Quoted 3 lines

With CONFIG_MMU=n __get_vm_area_node() is not available and memory for the allocation tags cannot be populated as needed. For this case, populate the required memory at initialization time.

Do we care that much to have alloc_tags on !MMU configs?

Your patch is great, but it would have been a lot easier to make MEM_ALLOC_PROFILING depend on MMU.

Quoted 6 lines

I didn't want to prevent !MMU users from using allocation profiling but I'm not aware of any current !MMU users. Therefore I have no objections to making MEM_ALLOC_PROFILING dependent on MMU if we indeed don't care about !MMU && MEM_ALLOC_PROFILING configuration. I'll be posting v5 tomorrow with all these fixes, so please let me know which way you prefer. Thanks!

I think if nommu users would want that feature, we can add the ifdefery later

Quoted 170 lines
Quoted 170 lines
Quoted 162 lines
---
 include/linux/execmem.h |  2 ++
 lib/alloc_tag.c         | 23 ++++++++++++++++++++++-
 mm/execmem.c            | 32 ++++++++++++++++----------------
 3 files changed, 40 insertions(+), 17 deletions(-)
include/linux/execmem.h +2 -0
index 5a5e2917f870..64130ae19690 100644
@@ -139,6 +139,7 @@ void *execmem_alloc(enum execmem_type type, size_t size);
  */
 void execmem_free(void *ptr);
 
+#ifdef CONFIG_MMU
 /**
  * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
  * @size: size of the virtual mapping in bytes
@@ -148,6 +149,7 @@ void execmem_free(void *ptr);
  * Return: the area descriptor on success or %NULL on failure.
  */
 struct vm_struct *execmem_vmap(size_t size);
+#endif
 
 /**
  * execmem_update_copy - copy an update to executable memory
lib/alloc_tag.c +22 -1
index c1ddac2d29f0..1c74942e6dfd 100644
@@ -268,7 +268,6 @@ void __init alloc_tag_sec_init(void)
 #ifdef CONFIG_MODULES
 
 static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE);
-static struct vm_struct *vm_module_tags;
 /* A dummy object used to indicate an unloaded module */
 static struct module unloaded_mod;
 /* A dummy object used to indicate a module prepended area */
@@ -391,6 +390,9 @@ static bool find_aligned_area(struct ma_state *mas, unsigned long section_size,
      return false;
 }
 
+#ifdef CONFIG_MMU
+static struct vm_struct *vm_module_tags;
+
 static int vm_module_tags_populate(void)
 {
      unsigned long phys_size = vm_module_tags->nr_pages << PAGE_SHIFT;
@@ -417,6 +419,13 @@ static int vm_module_tags_populate(void)
 
      return 0;
 }
+#else
+static int vm_module_tags_populate(void)
+{
+     /* Memory was already allocated */
+     return 0;
+}
+#endif
 
 static void *reserve_module_tags(struct module *mod, unsigned long size,
                               unsigned int prepend, unsigned long align)
@@ -561,6 +570,7 @@ static void replace_module(struct module *mod, struct module *new_mod)
 
 static int __init alloc_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
      /* Map space to copy allocation tags */
      vm_module_tags = execmem_vmap(MODULE_ALLOC_TAG_VMAP_SIZE);
      if (!vm_module_tags) {
@@ -578,6 +588,13 @@ static int __init alloc_mod_tags_mem(void)
      }
 
      module_tags.start_addr = (unsigned long)vm_module_tags->addr;
+#else
+     /* Allocate space to copy allocation tags */
+     module_tags.start_addr = (unsigned long)execmem_alloc(EXECMEM_MODULE_DATA,
+                                                           MODULE_ALLOC_TAG_VMAP_SIZE);
+     if (!module_tags.start_addr)
+             return -ENOMEM;
+#endif
      module_tags.end_addr = module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_SIZE;
      /* Ensure the base is alloc_tag aligned when required for indexing */
      module_tags.start_addr = alloc_tag_align(module_tags.start_addr);
@@ -587,6 +604,7 @@ static int __init alloc_mod_tags_mem(void)
 
 static void __init free_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
      int i;
 
      module_tags.start_addr = 0;
@@ -594,6 +612,9 @@ static void __init free_mod_tags_mem(void)
              __free_page(vm_module_tags->pages[i]);
      kfree(vm_module_tags->pages);
      free_vm_area(vm_module_tags);
+#else
+     execmem_free((void *)module_tags.start_addr);
+#endif
 }
 
 #else /* CONFIG_MODULES */
mm/execmem.c +16 -16
index 5c0f9f2d6f83..317b6a8d35be 100644
@@ -64,6 +64,22 @@ static void *execmem_vmalloc(struct execmem_range *range, size_t size,
 
      return p;
 }
+
+struct vm_struct *execmem_vmap(size_t size)
+{
+     struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
+     struct vm_struct *area;
+
+     area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+                               range->start, range->end, NUMA_NO_NODE,
+                               GFP_KERNEL, __builtin_return_address(0));
+     if (!area && range->fallback_start)
+             area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+                                       range->fallback_start, range->fallback_end,
+                                       NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
+
+     return area;
+}
 #else
 static void *execmem_vmalloc(struct execmem_range *range, size_t size,
                           pgprot_t pgprot, unsigned long vm_flags)
@@ -368,22 +384,6 @@ void execmem_free(void *ptr)
              vfree(ptr);
 }
 
-struct vm_struct *execmem_vmap(size_t size)
-{
-     struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
-     struct vm_struct *area;
-
-     area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-                               range->start, range->end, NUMA_NO_NODE,
-                               GFP_KERNEL, __builtin_return_address(0));
-     if (!area && range->fallback_start)
-             area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-                                       range->fallback_start, range->fallback_end,
-                                       NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
-
-     return area;
-}
-
 void *execmem_update_copy(void *dst, const void *src, size_t size)
 {
      return text_poke_copy(dst, src, size);
Quoted 4 lines

base-commit: 92409df3e00a17d6fd5bb27732fa186749725ed5 --
2.47.0.163.g1226f6d8fa-goog

--
Sincerely yours,
Mike.

Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw
Quoted 23 lines · Mike Rapoport
Quoted 14 lines · Suren Baghdasaryan
Quoted 4 lines · Mike Rapoport

Hi Suren,

On Mon, Oct 28, 2024 at 01:29:35PM -0700, Suren Baghdasaryan wrote:

Quoted 8 lines
Quoted 3 lines

With CONFIG_MMU=n __get_vm_area_node() is not available and memory for the allocation tags cannot be populated as needed. For this case, populate the required memory at initialization time.

Do we care that much to have alloc_tags on !MMU configs?

Your patch is great, but it would have been a lot easier to make MEM_ALLOC_PROFILING depend on MMU.

Quoted 6 lines

I didn't want to prevent !MMU users from using allocation profiling but I'm not aware of any current !MMU users. Therefore I have no objections to making MEM_ALLOC_PROFILING dependent on MMU if we indeed don't care about !MMU && MEM_ALLOC_PROFILING configuration. I'll be posting v5 tomorrow with all these fixes, so please let me know which way you prefer. Thanks!

Quoted 2 lines

I think if nommu users would want that feature, we can add the ifdefery later

Sounds good. I'll add the dependency and simplify this fix in my next posting. Thanks!

Quoted 175 lines
Quoted 163 lines
Quoted 163 lines
Quoted 163 lines
---
 include/linux/execmem.h |  2 ++
 lib/alloc_tag.c         | 23 ++++++++++++++++++++++-
 mm/execmem.c            | 32 ++++++++++++++++----------------
 3 files changed, 40 insertions(+), 17 deletions(-)
include/linux/execmem.h +2 -0
index 5a5e2917f870..64130ae19690 100644
@@ -139,6 +139,7 @@ void *execmem_alloc(enum execmem_type type, size_t size);
  */
 void execmem_free(void *ptr);
 
+#ifdef CONFIG_MMU
 /**
  * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
  * @size: size of the virtual mapping in bytes
@@ -148,6 +149,7 @@ void execmem_free(void *ptr);
  * Return: the area descriptor on success or %NULL on failure.
  */
 struct vm_struct *execmem_vmap(size_t size);
+#endif
 
 /**
  * execmem_update_copy - copy an update to executable memory
lib/alloc_tag.c +22 -1
index c1ddac2d29f0..1c74942e6dfd 100644
@@ -268,7 +268,6 @@ void __init alloc_tag_sec_init(void)
 #ifdef CONFIG_MODULES
 
 static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE);
-static struct vm_struct *vm_module_tags;
 /* A dummy object used to indicate an unloaded module */
 static struct module unloaded_mod;
 /* A dummy object used to indicate a module prepended area */
@@ -391,6 +390,9 @@ static bool find_aligned_area(struct ma_state *mas, unsigned long section_size,
      return false;
 }
 
+#ifdef CONFIG_MMU
+static struct vm_struct *vm_module_tags;
+
 static int vm_module_tags_populate(void)
 {
      unsigned long phys_size = vm_module_tags->nr_pages << PAGE_SHIFT;
@@ -417,6 +419,13 @@ static int vm_module_tags_populate(void)
 
      return 0;
 }
+#else
+static int vm_module_tags_populate(void)
+{
+     /* Memory was already allocated */
+     return 0;
+}
+#endif
 
 static void *reserve_module_tags(struct module *mod, unsigned long size,
                               unsigned int prepend, unsigned long align)
@@ -561,6 +570,7 @@ static void replace_module(struct module *mod, struct module *new_mod)
 
 static int __init alloc_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
      /* Map space to copy allocation tags */
      vm_module_tags = execmem_vmap(MODULE_ALLOC_TAG_VMAP_SIZE);
      if (!vm_module_tags) {
@@ -578,6 +588,13 @@ static int __init alloc_mod_tags_mem(void)
      }
 
      module_tags.start_addr = (unsigned long)vm_module_tags->addr;
+#else
+     /* Allocate space to copy allocation tags */
+     module_tags.start_addr = (unsigned long)execmem_alloc(EXECMEM_MODULE_DATA,
+                                                           MODULE_ALLOC_TAG_VMAP_SIZE);
+     if (!module_tags.start_addr)
+             return -ENOMEM;
+#endif
      module_tags.end_addr = module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_SIZE;
      /* Ensure the base is alloc_tag aligned when required for indexing */
      module_tags.start_addr = alloc_tag_align(module_tags.start_addr);
@@ -587,6 +604,7 @@ static int __init alloc_mod_tags_mem(void)
 
 static void __init free_mod_tags_mem(void)
 {
+#ifdef CONFIG_MMU
      int i;
 
      module_tags.start_addr = 0;
@@ -594,6 +612,9 @@ static void __init free_mod_tags_mem(void)
              __free_page(vm_module_tags->pages[i]);
      kfree(vm_module_tags->pages);
      free_vm_area(vm_module_tags);
+#else
+     execmem_free((void *)module_tags.start_addr);
+#endif
 }
 
 #else /* CONFIG_MODULES */
mm/execmem.c +16 -16
index 5c0f9f2d6f83..317b6a8d35be 100644
@@ -64,6 +64,22 @@ static void *execmem_vmalloc(struct execmem_range *range, size_t size,
 
      return p;
 }
+
+struct vm_struct *execmem_vmap(size_t size)
+{
+     struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
+     struct vm_struct *area;
+
+     area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+                               range->start, range->end, NUMA_NO_NODE,
+                               GFP_KERNEL, __builtin_return_address(0));
+     if (!area && range->fallback_start)
+             area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
+                                       range->fallback_start, range->fallback_end,
+                                       NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
+
+     return area;
+}
 #else
 static void *execmem_vmalloc(struct execmem_range *range, size_t size,
                           pgprot_t pgprot, unsigned long vm_flags)
@@ -368,22 +384,6 @@ void execmem_free(void *ptr)
              vfree(ptr);
 }
 
-struct vm_struct *execmem_vmap(size_t size)
-{
-     struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
-     struct vm_struct *area;
-
-     area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-                               range->start, range->end, NUMA_NO_NODE,
-                               GFP_KERNEL, __builtin_return_address(0));
-     if (!area && range->fallback_start)
-             area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
-                                       range->fallback_start, range->fallback_end,
-                                       NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
-
-     return area;
-}
-
 void *execmem_update_copy(void *dst, const void *src, size_t size)
 {
      return text_poke_copy(dst, src, size);
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

base-commit: 92409df3e00a17d6fd5bb27732fa186749725ed5 --
2.47.0.163.g1226f6d8fa-goog

--
Sincerely yours,
Mike.

--
Sincerely yours,
Mike.