Skip to contentSkip to conversation

mm/cma: Make kmemleak ignore CMA regions

PATCH In review Thierry Reding 3 participants 3 messages Oct 21, 2014 – Oct 23, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/cma: Make kmemleak ignore CMA regions

From: Thierry Reding <[email protected]>

kmemleak will add allocations as objects to a pool. The memory allocated for each object in this pool is periodically searched for pointers to other allocated objects. This only works for memory that is mapped into the kernel's virtual address space, which happens not to be the case for most CMA regions.

Furthermore, CMA regions are typically used to store data transferred to or from a device and therefore don't contain pointers to other objects.

---
Note: I'm not sure this is really the right fix. But without this, the kernel crashes on the first execution of the scan_gray_list() because it tries to access highmem. Perhaps a more appropriate fix would be to reject any object that can't map to a kernel virtual address?

---
 mm/cma.c | 1 +
 1 file changed, 1 insertion(+)
mm/cma.c +1 -0
index 963bc4add9af..349f9266f6d3 100644
@@ -280,6 +280,7 @@ int __init cma_declare_contiguous(phys_addr_t base,
 			ret = -ENOMEM;
 			goto err;
 		} else {
+			kmemleak_ignore(phys_to_virt(addr));
 			base = addr;
 		}
 	}
Signature · 7 lines
2.1.2

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#1 lore raw
Quoted 11 lines · On Tue, 21 Oct 2014 14:14:56 +0200 Thierry Reding <[email protected]>…

From: Thierry Reding <[email protected]>

kmemleak will add allocations as objects to a pool. The memory allocated for each object in this pool is periodically searched for pointers to other allocated objects. This only works for memory that is mapped into the kernel's virtual address space, which happens not to be the case for most CMA regions.

Furthermore, CMA regions are typically used to store data transferred to or from a device and therefore don't contain pointers to other objects.

Quoted 6 lines

---
Note: I'm not sure this is really the right fix. But without this, the kernel crashes on the first execution of the scan_gray_list() because it tries to access highmem. Perhaps a more appropriate fix would be to reject any object that can't map to a kernel virtual address?

Let's cc Catalin.

mm/cma.c int __init cma_declare_contiguous(phys_addr_t base, · line 286

Quoted 10 lines
mm/cma.c +1 -0
@@ -280,6 +280,7 @@ int __init cma_declare_contiguous(phys_addr_t base,
 			ret = -ENOMEM;
 			goto err;
 		} else {
+			kmemleak_ignore(phys_to_virt(addr));
 			base = addr;
 		}
 	}

And let's tell our poor readers why we did stuff. Something like this.

mm/cma.c renamed from mm/cma.c~mm-cma-make-kmemleak-ignore-cma-regions-fix +4 -0
@@ -280,6 +280,10 @@ int __init cma_declare_contiguous(phys_a
 			ret = -ENOMEM;
 			goto err;
 		} else {
+			/*
+			 * kmemleak writes metadata to the tracked objects, but
+			 * this address isn't mapped and accessible.
+			 */
 			kmemleak_ignore(phys_to_virt(addr));
 			base = addr;
 		}

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 22 lines · Andrew Morton
Quoted 11 lines · On Tue, 21 Oct 2014 14:14:56 +0200 Thierry Reding <[email protected]>…

From: Thierry Reding <[email protected]>

kmemleak will add allocations as objects to a pool. The memory allocated for each object in this pool is periodically searched for pointers to other allocated objects. This only works for memory that is mapped into the kernel's virtual address space, which happens not to be the case for most CMA regions.

Furthermore, CMA regions are typically used to store data transferred to or from a device and therefore don't contain pointers to other objects.

Quoted 6 lines

---
Note: I'm not sure this is really the right fix. But without this, the kernel crashes on the first execution of the scan_gray_list() because it tries to access highmem. Perhaps a more appropriate fix would be to reject any object that can't map to a kernel virtual address?

Let's cc Catalin.

Quoted 10 lines

mm/cma.c int __init cma_declare_contiguous(phys_addr_t base, · line 286

Quoted 10 lines
mm/cma.c +1 -0
@@ -280,6 +280,7 @@ int __init cma_declare_contiguous(phys_addr_t base,
                      ret = -ENOMEM;
                      goto err;
              } else {
+                     kmemleak_ignore(phys_to_virt(addr));
                      base = addr;
              }
      }

I wonder whether using __va() for the argument of kmemleak_alloc() in memblock_alloc_range_nid() is always correct. Is memblock.current_limit guaranteed to be in lowmem? If not, I think we need some logic not to call kmemleak_alloc() for all memblock allocations (and avoid the need to ignore them later).

Quoted 5 lines

And let's tell our poor readers why we did stuff. Something like this.

mm/cma.c int __init cma_declare_contiguous(phys_a · line 289

Quoted 10 lines
mm/cma.c renamed from mm/cma.c~mm-cma-make-kmemleak-ignore-cma-regions-fix +4 -0
@@ -280,6 +280,10 @@ int __init cma_declare_contiguous(phys_a
                        ret = -ENOMEM;
                        goto err;
                } else {
+                       /*
+                        * kmemleak writes metadata to the tracked objects, but
+                        * this address isn't mapped and accessible.
+                        */
                        kmemleak_ignore(phys_to_virt(addr));
                        base = addr;
                }

The reason is different, as per Therry's patch description. Kmemleak does not write metadata to the tracked objects but reads them during memory scanning. So maybe something like "kmemleak scans/reads tracked objects for pointers to other objects but this address isn't mapped and accessible."

A better API to use here would have been kmemleak_no_scan(), however, I don't think we care about such CMA pointers anyway since they seem to be tracked by physical address which kmemleak doesn't store.

Signature · 7 lines
Catalin

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>