* [PATCH v3] mm/memmap: Prevent double scanning of memmap by kmemleak
@ 2025-01-06 2:11 Guo Weikang
2025-01-06 9:46 ` Catalin Marinas
0 siblings, 1 reply; 2+ messages in thread
From: Guo Weikang @ 2025-01-06 2:11 UTC (permalink / raw)
To: Catalin Marinas, Mike Rapoport, Andrew Morton
Cc: Guo Weikang, linux-mm, linux-kernel
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.
Link: https://lore.kernel.org/lkml/CAOm6qn=FVeTpH54wGDFMHuCOeYtvoTx30ktnv9-w3Nh8RMofEA@mail.gmail.com/
Signed-off-by: Guo Weikang <guoweikang.kernel@gmail.com>
---
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/202501021601.jub4p3EM-lkp@intel.com/
v2 -> v3: Rebase on latest mm-everything and modified some minor comments.
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Links: https://lore.kernel.org/lkml/20250103110150.1352226-1-guoweikang.kernel@gmail.com/
---
include/linux/memblock.h | 4 ++++
mm/mm_init.c | 8 ++++++--
mm/sparse-vmemmap.c | 5 +++--
3 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index dee628350cd1..e79eb6ac516f 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -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 */
diff --git a/mm/mm_init.c b/mm/mm_init.c
index 24b68b425afb..2630cc30147e 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1585,13 +1585,17 @@ void __init *memmap_alloc(phys_addr_t size, phys_addr_t align,
{
void *ptr;
+ /*
+ * Kmemleak will explicitly scan mem_map by traversing all valid
+ * `struct *page`,so memblock does not need to be added to the scan list.
+ */
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)
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index cec67c5f37d8..3287ebadd167 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -31,6 +31,8 @@
#include <asm/dma.h>
#include <asm/pgalloc.h>
+#include "internal.h"
+
/*
* Allocate a block of memory to be used to back the virtual memory map
* or to back the page tables that are used to create the mapping.
@@ -42,8 +44,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)
--
2.25.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v3] mm/memmap: Prevent double scanning of memmap by kmemleak
2025-01-06 2:11 [PATCH v3] mm/memmap: Prevent double scanning of memmap by kmemleak Guo Weikang
@ 2025-01-06 9:46 ` Catalin Marinas
0 siblings, 0 replies; 2+ messages in thread
From: Catalin Marinas @ 2025-01-06 9:46 UTC (permalink / raw)
To: Guo Weikang; +Cc: Mike Rapoport, Andrew Morton, linux-mm, linux-kernel
On Mon, Jan 06, 2025 at 10:11:25AM +0800, Guo Weikang wrote:
> 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.
>
> Link: https://lore.kernel.org/lkml/CAOm6qn=FVeTpH54wGDFMHuCOeYtvoTx30ktnv9-w3Nh8RMofEA@mail.gmail.com/
>
> Signed-off-by: Guo Weikang <guoweikang.kernel@gmail.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-01-06 9:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-06 2:11 [PATCH v3] mm/memmap: Prevent double scanning of memmap by kmemleak Guo Weikang
2025-01-06 9:46 ` Catalin Marinas
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox