Skip to contentSkip to conversation

mm/memmap: Prevent double scanning of memmap by kmemleak

PATCH Superseded Guo Weikang 2 participants 3 messages Jan 3, 2025 – Jan 6, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/memmap: Prevent double scanning of memmap by kmemleak

kmemleak explicitly scans the mem_map through the valid struct page objects. However, memmap_alloc() was also adding this memory to the gray object list, causing it to be scanned twice. Removes memmap_alloc() from the scan list and adds a comment to clarify the behavior.

---
v1 -> v2: Fix CI: Recover __earlyonly_bootmem_alloc that is marked with __ref to prevent init section mismatch WARNING.
Links: https://lore.kernel.org/oe-kbuild-all/[email protected]/

---
 include/linux/memblock.h | 4 ++++
 mm/mm_init.c             | 8 ++++++--
 mm/sparse-vmemmap.c      | 5 ++---
 3 files changed, 12 insertions(+), 5 deletions(-)
include/linux/memblock.h +4 -0
index dee628350cd1..e79eb6ac516f 100644
@@ -378,6 +378,10 @@ static inline int memblock_get_region_node(const struct memblock_region *r)
 /* Flags for memblock allocation APIs */
 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
+/*
+ *  MEMBLOCK_ALLOC_NOLEAKTRACE avoids kmemleak tracing. It implies
+ *  MEMBLOCK_ALLOC_ACCESSIBLE
+ */
 #define MEMBLOCK_ALLOC_NOLEAKTRACE	1
 
 /* We are using top down, so it is safe to use 0 here */
mm/mm_init.c +6 -2
index 24b68b425afb..71b58f5f2492 100644
@@ -1580,6 +1580,10 @@ static void __init free_area_init_core(struct pglist_data *pgdat)
 	}
 }
 
+/*
+ * Kmemleak will explicitly scan mem_map by traversing all valid `struct *page`,
+ * so memblock does not need to be added to the scan list.
+ */
 void __init *memmap_alloc(phys_addr_t size, phys_addr_t align,
 			  phys_addr_t min_addr, int nid, bool exact_nid)
 {
@@ -1587,11 +1591,11 @@ void __init *memmap_alloc(phys_addr_t size, phys_addr_t align,
 
 	if (exact_nid)
 		ptr = memblock_alloc_exact_nid_raw(size, align, min_addr,
-						   MEMBLOCK_ALLOC_ACCESSIBLE,
+						   MEMBLOCK_ALLOC_NOLEAKTRACE,
 						   nid);
 	else
 		ptr = memblock_alloc_try_nid_raw(size, align, min_addr,
-						 MEMBLOCK_ALLOC_ACCESSIBLE,
+						 MEMBLOCK_ALLOC_NOLEAKTRACE,
 						 nid);
 
 	if (ptr && size > 0)
mm/sparse-vmemmap.c +2 -3
index cec67c5f37d8..903a5422907b 100644
@@ -27,7 +27,7 @@
 #include <linux/spinlock.h>
 #include <linux/vmalloc.h>
 #include <linux/sched.h>
-
+#include "internal.h"
 #include <asm/dma.h>
 #include <asm/pgalloc.h>
 
@@ -42,8 +42,7 @@ static void * __ref __earlyonly_bootmem_alloc(int node,
 				unsigned long align,
 				unsigned long goal)
 {
-	return memblock_alloc_try_nid_raw(size, align, goal,
-					       MEMBLOCK_ALLOC_ACCESSIBLE, node);
+	return memmap_alloc(size, align, goal, node, false);
 }
 
 void * __meminit vmemmap_alloc_block(unsigned long size, int node)
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 lines · Guo Weikang
mm/mm_init.c +0 -0
index 24b68b425afb..71b58f5f2492 100644

mm/mm_init.c static void __init free_area_init_core(struct pglist_data *pgdat) · line 1589

Quoted 10 lines
mm/mm_init.c +4 -0
@@ -1580,6 +1580,10 @@ static void __init free_area_init_core(struct pglist_data *pgdat)
 	}
 }
 
+/*
+ * Kmemleak will explicitly scan mem_map by traversing all valid `struct *page`,
+ * so memblock does not need to be added to the scan list.
+ */
 void __init *memmap_alloc(phys_addr_t size, phys_addr_t align,
 			  phys_addr_t min_addr, int nid, bool exact_nid)
 {

Nitpick: normally I'd place the comment in the code, before the 'if' statement. We keep the comments above functions for a description of the function.

Quoted 19 lines
mm/mm_init.c +2 -2
@@ -1587,11 +1591,11 @@ void __init *memmap_alloc(phys_addr_t size, phys_addr_t align,
 
 	if (exact_nid)
 		ptr = memblock_alloc_exact_nid_raw(size, align, min_addr,
-						   MEMBLOCK_ALLOC_ACCESSIBLE,
+						   MEMBLOCK_ALLOC_NOLEAKTRACE,
 						   nid);
 	else
 		ptr = memblock_alloc_try_nid_raw(size, align, min_addr,
-						 MEMBLOCK_ALLOC_ACCESSIBLE,
+						 MEMBLOCK_ALLOC_NOLEAKTRACE,
 						 nid);
 
 	if (ptr && size > 0)
mm/sparse-vmemmap.c +0 -0
index cec67c5f37d8..903a5422907b 100644

mm/sparse-vmemmap.c line 32

Quoted 7 lines
mm/sparse-vmemmap.c +1 -1
@@ -27,7 +27,7 @@
 #include <linux/spinlock.h>
 #include <linux/vmalloc.h>
 #include <linux/sched.h>
-
+#include "internal.h"
 #include <asm/dma.h>
 #include <asm/pgalloc.h>

Another nit: if the original authors of this file preferred the includes to be in blocks, I'd keep the internal.h include as a separate block after the asm/ includes.

Apart from these minor things, the patch looks fine.

Thanks.

Signature · 1 line
Catalin
#2 lore raw

Thansk, Catalin

Catalin Marinas <[email protected]> wrote on Friday, 3 January 2025 at 22:28

Quoted 10 lines · Guo Weikang
Quoted 8 lines
mm/mm_init.c +0 -0
index 24b68b425afb..71b58f5f2492 100644
@@ -1580,6 +1580,10 @@ static void __init free_area_init_core(struct pglist_data *pgdat)
      }
 }
 
Quoted 12 lines

mm/mm_init.c static void __init free_area_init_core(struct pglist_data *pgdat) · line 1589

Quoted 7 lines
mm/mm_init.c +4 -0
+/*
+ * Kmemleak will explicitly scan mem_map by traversing all valid `struct *page`,
+ * so memblock does not need to be added to the scan list.
+ */
 void __init *memmap_alloc(phys_addr_t size, phys_addr_t align,
                        phys_addr_t min_addr, int nid, bool exact_nid)
 {

Nitpick: normally I'd place the comment in the code, before the 'if' statement. We keep the comments above functions for a description of the function.

Thank you for the suggestion! I agree that placing the comment before the 'if' statement is
a better approach. I will update the patch accordingly.

Quoted 22 lines
Quoted 22 lines
mm/mm_init.c +2 -2
@@ -1587,11 +1591,11 @@ void __init *memmap_alloc(phys_addr_t size, phys_addr_t align,
 
      if (exact_nid)
              ptr = memblock_alloc_exact_nid_raw(size, align, min_addr,
-                                                MEMBLOCK_ALLOC_ACCESSIBLE,
+                                                MEMBLOCK_ALLOC_NOLEAKTRACE,
                                                 nid);
      else
              ptr = memblock_alloc_try_nid_raw(size, align, min_addr,
-                                              MEMBLOCK_ALLOC_ACCESSIBLE,
+                                              MEMBLOCK_ALLOC_NOLEAKTRACE,
                                               nid);
 
      if (ptr && size > 0)
mm/sparse-vmemmap.c +0 -0
index cec67c5f37d8..903a5422907b 100644
@@ -27,7 +27,7 @@
 #include <linux/spinlock.h>
 #include <linux/vmalloc.h>
 #include <linux/sched.h>
Quoted 9 lines

mm/sparse-vmemmap.c line 32

Quoted 4 lines
mm/sparse-vmemmap.c +1 -1
-
+#include "internal.h"
 #include <asm/dma.h>
 #include <asm/pgalloc.h>

Another nit: if the original authors of this file preferred the includes to be in blocks, I'd keep the internal.h include as a separate block after the asm/ includes.

Here is the order I actually refer to mm/sparse.c, but after looking at other files, I think your suggestion is better.

Quoted 2 lines

Apart from these minor things, the patch looks fine.

Tanks!

Quoted 6 lines

Thanks.

--
Catalin

---
Guo