* [PATCH] mm: kmemleak: Keep full physical address for OBJECT_PHYS objects
@ 2026-12-03 11:54 Stefan Wiehler
2026-12-04 11:56 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Stefan Wiehler @ 2026-12-03 11:54 UTC (permalink / raw)
To: Catalin Marinas, Andrew Morton; +Cc: linux-mm, Stefan Wiehler
On 32-bit kernels with CONFIG_ARM_LPAE, kmemleak_alloc_phys() and the
other *_phys() helpers pass a phys_addr_t, but kmemleak stored the
address in an unsigned long. Memory above 4GB therefore lost its upper
bits. At scan time the lowmem check then compared the truncated page
frame against min_low_pfn/max_low_pfn, painted the object black, and
skipped scanning it.
On our KeyStone II setup, the unflattened device tree lives in a
memblock above 4GB. Every pointer that only exists in that tree, such as
kobject names, property names and fwnode links created during
of_core_init(), was therefore reported as an unreferenced object.
Store the object pointer as u64 and carry it through the object tree,
lookup, create, delete, paint and scan paths. Virtual addresses are
still untagged and cast to unsigned long where they are dereferenced
or printed.
Signed-off-by: Stefan Wiehler <stefan.wiehler@nokia.com>
---
mm/kmemleak.c | 118 ++++++++++++++++++++++++++++++--------------------
1 file changed, 70 insertions(+), 48 deletions(-)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 20c0eb41c873..2c466dd26f23 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -143,7 +143,8 @@ struct kmemleak_object {
/* object usage count; object freed when use_count == 0 */
atomic_t use_count;
unsigned int del_state; /* deletion state */
- unsigned long pointer;
+ /* virtual or physical address of the block */
+ u64 pointer;
size_t size;
/* pass surplus references to this pointer */
unsigned long excess_ref;
@@ -305,14 +306,14 @@ static void warn_or_seq_hex_dump(struct seq_file *seq, int prefix_type,
static void hex_dump_object(struct seq_file *seq,
struct kmemleak_object *object)
{
- const u8 *ptr = (const u8 *)object->pointer;
+ const u8 *ptr = (const u8 *)(unsigned long)object->pointer;
size_t len;
if (WARN_ON_ONCE(object->flags & OBJECT_PHYS))
return;
if (object->flags & OBJECT_PERCPU)
- ptr = (const u8 *)this_cpu_ptr((void __percpu *)object->pointer);
+ ptr = (const u8 *)this_cpu_ptr((void __percpu *)(unsigned long)object->pointer);
/* limit the number of lines to HEX_MAX_LINES */
len = min_t(size_t, object->size, HEX_MAX_LINES * HEX_ROW_SIZE);
@@ -382,9 +383,9 @@ static void __print_unreferenced(struct seq_file *seq,
unsigned int nr_entries;
nr_entries = stack_depot_fetch(object->trace_handle, &entries);
- warn_or_seq_printf(seq, "unreferenced object%s 0x%08lx (size %zu):\n",
+ warn_or_seq_printf(seq, "unreferenced object%s 0x%08llx (size %zu):\n",
__object_type_str(object),
- object->pointer, object->size);
+ (unsigned long long)object->pointer, object->size);
warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu\n",
object->comm, object->pid, object->jiffies);
if (hex_dump)
@@ -410,8 +411,9 @@ static void print_unreferenced(struct seq_file *seq,
*/
static void dump_object_info(struct kmemleak_object *object)
{
- pr_notice("Object%s 0x%08lx (size %zu):\n",
- __object_type_str(object), object->pointer, object->size);
+ pr_notice("Object%s 0x%08llx (size %zu):\n",
+ __object_type_str(object),
+ (unsigned long long)object->pointer, object->size);
pr_notice(" comm \"%s\", pid %d, jiffies %lu\n",
object->comm, object->pid, object->jiffies);
pr_notice(" min_count = %d\n", object->min_count);
@@ -432,24 +434,37 @@ static struct rb_root *object_tree(unsigned long objflags)
return &object_tree_root;
}
+/*
+ * Address used as the object-tree key. Physical addresses are kept whole so
+ * a 32-bit kernel with 64-bit phys_addr_t does not drop the upper bits.
+ * Virtual addresses are untagged.
+ */
+static u64 object_tree_addr(u64 ptr, unsigned int objflags)
+{
+ if (objflags & OBJECT_PHYS)
+ return ptr;
+
+ return (unsigned long)kasan_reset_tag((void *)(unsigned long)ptr);
+}
+
/*
* Look-up a memory block metadata (kmemleak_object) in the object search
* tree based on a pointer value. If alias is 0, only values pointing to the
* beginning of the memory block are allowed. The kmemleak_lock must be held
* when calling this function.
*/
-static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias,
+static struct kmemleak_object *__lookup_object(u64 ptr, int alias,
unsigned int objflags)
{
struct rb_node *rb = object_tree(objflags)->rb_node;
- unsigned long untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr);
+ u64 untagged_ptr = object_tree_addr(ptr, objflags);
while (rb) {
struct kmemleak_object *object;
- unsigned long untagged_objp;
+ u64 untagged_objp;
object = rb_entry(rb, struct kmemleak_object, rb_node);
- untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
+ untagged_objp = object_tree_addr(object->pointer, objflags);
if (untagged_ptr < untagged_objp)
rb = object->rb_node.rb_left;
@@ -463,8 +478,8 @@ static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias,
* This is done to avoid deadlock.
*/
printk_deferred_enter();
- kmemleak_warn("Found object by alias at 0x%08lx\n",
- ptr);
+ kmemleak_warn("Found object by alias at 0x%08llx\n",
+ (unsigned long long)ptr);
dump_object_info(object);
printk_deferred_exit();
break;
@@ -592,7 +607,7 @@ static void put_object(struct kmemleak_object *object)
/*
* Look up an object in the object search tree and increase its use_count.
*/
-static struct kmemleak_object *__find_and_get_object(unsigned long ptr, int alias,
+static struct kmemleak_object *__find_and_get_object(u64 ptr, int alias,
unsigned int objflags)
{
unsigned long flags;
@@ -629,8 +644,7 @@ static void __remove_object(struct kmemleak_object *object)
object->del_state |= DELSTATE_REMOVED;
}
-static struct kmemleak_object *__find_and_remove_object(unsigned long ptr,
- int alias,
+static struct kmemleak_object *__find_and_remove_object(u64 ptr, int alias,
unsigned int objflags)
{
struct kmemleak_object *object;
@@ -647,7 +661,7 @@ static struct kmemleak_object *__find_and_remove_object(unsigned long ptr,
* tree root and object_list. The returned object's use_count should be at
* least 1, as initially set by create_object().
*/
-static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int alias,
+static struct kmemleak_object *find_and_remove_object(u64 ptr, int alias,
unsigned int objflags)
{
unsigned long flags;
@@ -725,40 +739,45 @@ static struct kmemleak_object *__alloc_object(gfp_t gfp)
return object;
}
-static int __link_object(struct kmemleak_object *object, unsigned long ptr,
+static int __link_object(struct kmemleak_object *object, u64 ptr,
size_t size, int min_count, unsigned int objflags)
{
struct kmemleak_object *parent;
struct rb_node **link, *rb_parent;
- unsigned long untagged_ptr;
- unsigned long untagged_objp;
+ u64 untagged_ptr;
+ u64 untagged_objp;
object->flags = OBJECT_ALLOCATED | objflags;
object->pointer = ptr;
- object->size = kfence_ksize((void *)ptr) ?: size;
+ object->size = (objflags & OBJECT_PHYS) ? size :
+ (kfence_ksize((void *)(unsigned long)ptr) ?: size);
object->min_count = min_count;
object->jiffies = jiffies;
- untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr);
+ untagged_ptr = object_tree_addr(ptr, objflags);
/*
* Only update min_addr and max_addr with object storing virtual
* address. And update min_percpu_addr max_percpu_addr for per-CPU
* objects.
*/
if (objflags & OBJECT_PERCPU) {
- min_percpu_addr = min(min_percpu_addr, untagged_ptr);
- max_percpu_addr = max(max_percpu_addr, untagged_ptr + size);
+ unsigned long addr = untagged_ptr;
+
+ min_percpu_addr = min(min_percpu_addr, addr);
+ max_percpu_addr = max(max_percpu_addr, addr + size);
} else if (!(objflags & OBJECT_PHYS)) {
- min_addr = min(min_addr, untagged_ptr);
- max_addr = max(max_addr, untagged_ptr + size);
+ unsigned long addr = untagged_ptr;
+
+ min_addr = min(min_addr, addr);
+ max_addr = max(max_addr, addr + size);
}
link = &object_tree(objflags)->rb_node;
rb_parent = NULL;
while (*link) {
rb_parent = *link;
parent = rb_entry(rb_parent, struct kmemleak_object, rb_node);
- untagged_objp = (unsigned long)kasan_reset_tag((void *)parent->pointer);
+ untagged_objp = object_tree_addr(parent->pointer, objflags);
if (untagged_ptr + size <= untagged_objp)
link = &parent->rb_node.rb_left;
else if (untagged_objp + parent->size <= untagged_ptr)
@@ -769,8 +788,8 @@ static int __link_object(struct kmemleak_object *object, unsigned long ptr,
* This is done to avoid deadlock.
*/
printk_deferred_enter();
- kmemleak_stop("Cannot insert 0x%lx into the object search tree (overlaps existing)\n",
- ptr);
+ kmemleak_stop("Cannot insert 0x%llx into the object search tree "
+ "(overlaps existing)\n", (unsigned long long)ptr);
/*
* No need for parent->lock here since "parent" cannot
* be freed while the kmemleak_lock is held.
@@ -791,7 +810,7 @@ static int __link_object(struct kmemleak_object *object, unsigned long ptr,
* Create the metadata (struct kmemleak_object) corresponding to an allocated
* memory block and add it to the object_list and object tree.
*/
-static void __create_object(unsigned long ptr, size_t size,
+static void __create_object(u64 ptr, size_t size,
int min_count, gfp_t gfp, unsigned int objflags)
{
struct kmemleak_object *object;
@@ -817,7 +836,7 @@ static void create_object(unsigned long ptr, size_t size,
}
/* Create kmemleak object which allocated with physical address. */
-static void create_object_phys(unsigned long ptr, size_t size,
+static void create_object_phys(phys_addr_t ptr, size_t size,
int min_count, gfp_t gfp)
{
__create_object(ptr, size, min_count, gfp, OBJECT_PHYS);
@@ -854,7 +873,7 @@ static void __delete_object(struct kmemleak_object *object)
* Look up the metadata (struct kmemleak_object) corresponding to ptr and
* delete it.
*/
-static void delete_object_full(unsigned long ptr, unsigned int objflags)
+static void delete_object_full(u64 ptr, unsigned int objflags)
{
struct kmemleak_object *object;
@@ -873,11 +892,11 @@ static void delete_object_full(unsigned long ptr, unsigned int objflags)
* delete it. If the memory block is partially freed, the function may create
* additional metadata for the remaining parts of the block.
*/
-static void delete_object_part(unsigned long ptr, size_t size,
- unsigned int objflags)
+static void delete_object_part(u64 ptr, size_t size, unsigned int objflags)
{
struct kmemleak_object *object, *object_l, *object_r;
- unsigned long start, end, flags;
+ unsigned long flags;
+ u64 start, end;
object_l = __alloc_object(GFP_KERNEL);
if (!object_l)
@@ -914,8 +933,8 @@ static void delete_object_part(unsigned long ptr, size_t size,
__delete_object(object);
} else {
#ifdef DEBUG
- kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
- ptr, size);
+ kmemleak_warn("Partially freeing unknown object at 0x%08llx (size %zu)\n",
+ (unsigned long long)ptr, size);
#endif
}
@@ -942,7 +961,7 @@ static void paint_it(struct kmemleak_object *object, int color)
raw_spin_unlock_irqrestore(&object->lock, flags);
}
-static void paint_ptr(unsigned long ptr, int color, unsigned int objflags)
+static void paint_ptr(u64 ptr, int color, unsigned int objflags)
{
struct kmemleak_object *object;
@@ -970,7 +989,7 @@ static void make_gray_object(unsigned long ptr)
* Mark the object as black-colored so that it is ignored from scans and
* reporting.
*/
-static void make_black_object(unsigned long ptr, unsigned int objflags)
+static void make_black_object(u64 ptr, unsigned int objflags)
{
paint_ptr(ptr, KMEMLEAK_BLACK, objflags);
}
@@ -1017,7 +1036,7 @@ static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
}
untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr);
- untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
+ untagged_objp = (unsigned long)kasan_reset_tag((void *)(unsigned long)object->pointer);
if (scan_area_cache)
area = kmem_cache_alloc_noprof(scan_area_cache,
@@ -1368,7 +1387,7 @@ void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp)
* Create object with OBJECT_PHYS flag and
* assume min_count 0.
*/
- create_object_phys((unsigned long)phys, size, 0, gfp);
+ create_object_phys(phys, size, 0, gfp);
}
EXPORT_SYMBOL(kmemleak_alloc_phys);
@@ -1384,7 +1403,7 @@ void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size)
pr_debug("%s(0x%px)\n", __func__, &phys);
if (kmemleak_enabled)
- delete_object_part((unsigned long)phys, size, OBJECT_PHYS);
+ delete_object_part(phys, size, OBJECT_PHYS);
}
EXPORT_SYMBOL(kmemleak_free_part_phys);
@@ -1398,7 +1417,7 @@ void __ref kmemleak_ignore_phys(phys_addr_t phys)
pr_debug("%s(0x%px)\n", __func__, &phys);
if (kmemleak_enabled)
- make_black_object((unsigned long)phys, OBJECT_PHYS);
+ make_black_object(phys, OBJECT_PHYS);
}
EXPORT_SYMBOL(kmemleak_ignore_phys);
@@ -1419,13 +1438,15 @@ static bool update_checksum(struct kmemleak_object *object)
object->checksum = 0;
for_each_possible_cpu(cpu) {
- void *ptr = per_cpu_ptr((void __percpu *)object->pointer, cpu);
+ void *ptr = per_cpu_ptr((void __percpu *)(unsigned long)object->pointer,
+ cpu);
object->checksum = crc32(object->checksum,
kasan_reset_tag((void *)ptr), object->size);
}
} else {
- object->checksum = crc32(0, kasan_reset_tag((void *)object->pointer), object->size);
+ object->checksum = crc32(0, kasan_reset_tag((void *)(unsigned long)object->pointer),
+ object->size);
}
kasan_enable_current();
kcsan_enable_current();
@@ -1618,7 +1639,8 @@ static void scan_object(struct kmemleak_object *object)
unsigned int cpu;
for_each_possible_cpu(cpu) {
- void *start = per_cpu_ptr((void __percpu *)object->pointer, cpu);
+ void *start = per_cpu_ptr((void __percpu *)(unsigned long)object->pointer,
+ cpu);
void *end = start + object->size;
scan_block(start, end, object);
@@ -1633,7 +1655,7 @@ static void scan_object(struct kmemleak_object *object)
object->flags & OBJECT_FULL_SCAN) {
void *start = object->flags & OBJECT_PHYS ?
__va((phys_addr_t)object->pointer) :
- (void *)object->pointer;
+ (void *)(unsigned long)object->pointer;
void *end = start + object->size;
void *next;
@@ -1936,7 +1958,7 @@ static int __kmemleak_scan(bool full)
/* ignore objects outside lowmem (paint them black) */
if ((object->flags & OBJECT_PHYS) &&
!(object->flags & OBJECT_NO_SCAN)) {
- unsigned long phys = object->pointer;
+ u64 phys = object->pointer;
if (PHYS_PFN(phys) < min_low_pfn ||
PHYS_PFN(phys + object->size) > max_low_pfn)
--
2.42.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] mm: kmemleak: Keep full physical address for OBJECT_PHYS objects
2026-12-03 11:54 [PATCH] mm: kmemleak: Keep full physical address for OBJECT_PHYS objects Stefan Wiehler
@ 2026-12-04 11:56 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 11:56 UTC (permalink / raw)
To: Stefan Wiehler, Catalin Marinas, Andrew Morton; +Cc: linux-mm
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: kmemleak: dump_str_object_info() truncates >4GB physical
addresses
- [Low] mm: kmemleak: missing Fixes trailer for commit 0c24e061196c
- [Low] mm: kmemleak u64 pointer adds struct padding and 32-bit lookup
overhead
--
commit 42bc9a7add9577059db2423f9b06da1b825a2889
Author: Stefan Wiehler <stefan.wiehler@nokia.com>
> mm: kmemleak: Keep full physical address for OBJECT_PHYS objects
>
> On 32-bit kernels with CONFIG_ARM_LPAE, kmemleak_alloc_phys() and the
> other *_phys() helpers pass a phys_addr_t, but kmemleak stored the
> address in an unsigned long. Memory above 4GB therefore lost its upper
> bits. At scan time the lowmem check then compared the truncated page
> frame against min_low_pfn/max_low_pfn, painted the object black, and
> skipped scanning it.
>
> On our KeyStone II setup, the unflattened device tree lives in a
> memblock above 4GB. Every pointer that only exists in that tree, such as
> kobject names, property names and fwnode links created during
> of_core_init(), was therefore reported as an unreferenced object.
>
> Store the object pointer as u64 and carry it through the object tree,
> lookup, create, delete, paint and scan paths. Virtual addresses are
> still untagged and cast to unsigned long where they are dereferenced
> or printed.
>
> Signed-off-by: Stefan Wiehler <stefan.wiehler@nokia.com>
[Severity: Low]
This fixes a bug introduced when physical addresses began being stored
directly in struct kmemleak_object.pointer. Should this include:
Fixes: 0c24e061196c ("mm: kmemleak: add rbtree and store physical address for objects allocated with PA")
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index d84c1e2c30478..428e2679dbcef 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -144,7 +144,8 @@ struct kmemleak_object {
> /* object usage count; object freed when use_count == 0 */
> atomic_t use_count;
> unsigned int del_state; /* deletion state */
> - unsigned long pointer;
> + /* virtual or physical address of the block */
> + u64 pointer;
> size_t size;
> /* pass surplus references to this pointer */
> unsigned long excess_ref;
[Severity: Low]
Could this use phys_addr_t instead of u64, and place size before pointer?
On 32-bit SMP architectures with 8-byte u64 alignment (such as ARM EABI),
putting u64 pointer before size adds 8 bytes of padding, growing struct
kmemleak_object from 116 to 128 bytes (+192 KB for the default mem_pool)
and pushing size into a second cacheline away from rb_node and pointer.
Using phys_addr_t avoids both the growth and 64-bit arithmetic in
__lookup_object() and __link_object() on 32-bit !CONFIG_PHYS_ADDR_T_64BIT
builds, while moving size before pointer eliminates the padding and
cacheline split on 32-bit CONFIG_PHYS_ADDR_T_64BIT builds too.
[ ... ]
> @@ -593,7 +608,7 @@ static void put_object(struct kmemleak_object *object)
> /*
> * Look up an object in the object search tree and increase its use_count.
> */
> -static struct kmemleak_object *__find_and_get_object(unsigned long ptr, int alias,
> +static struct kmemleak_object *__find_and_get_object(u64 ptr, int alias,
> unsigned int objflags)
> {
> unsigned long flags;
[Severity: Medium]
Should dump_str_object_info() and __dump_str_object_info() also be updated
for 64-bit physical addresses?
dump_str_object_info() still parses the debugfs dump= input into an
unsigned long with kstrtoul() (and prints unknown addresses with 0x%08lx),
so on 32-bit LPAE kernels looking up an OBJECT_PHYS object above 4GB
returns -EINVAL.
When widening dump_str_object_info() to kstrtou64() and
__dump_str_object_info() to u64, the virtual and OBJECT_PERCPU lookups
should be skipped when addr > ULONG_MAX; otherwise object_tree_addr()
truncates the upper bits and can match an unrelated 32-bit object.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115429.2383861-1-stefan.wiehler@nokia.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 11:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 11:54 [PATCH] mm: kmemleak: Keep full physical address for OBJECT_PHYS objects Stefan Wiehler
2026-12-04 11:56 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox