WSL2-Linux-Kernel/drivers/of
Isaac J. Manjarres 9197daee9e of: reserved_mem: Have kmemleak ignore dynamically allocated reserved mem
commit ce4d9a1ea3 upstream.

Patch series "Fix kmemleak crashes when scanning CMA regions", v2.

When trying to boot a device with an ARM64 kernel with the following
config options enabled:

CONFIG_DEBUG_PAGEALLOC=y
CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT=y
CONFIG_DEBUG_KMEMLEAK=y

a crash is encountered when kmemleak starts to scan the list of gray
or allocated objects that it maintains. Upon closer inspection, it was
observed that these page-faults always occurred when kmemleak attempted
to scan a CMA region.

At the moment, kmemleak is made aware of CMA regions that are specified
through the devicetree to be dynamically allocated within a range of
addresses. However, kmemleak should not need to scan CMA regions or any
reserved memory region, as those regions can be used for DMA transfers
between drivers and peripherals, and thus wouldn't contain anything
useful for kmemleak.

Additionally, since CMA regions are unmapped from the kernel's address
space when they are freed to the buddy allocator at boot when
CONFIG_DEBUG_PAGEALLOC is enabled, kmemleak shouldn't attempt to access
those memory regions, as that will trigger a crash. Thus, kmemleak
should ignore all dynamically allocated reserved memory regions.


This patch (of 1):

Currently, kmemleak ignores dynamically allocated reserved memory regions
that don't have a kernel mapping.  However, regions that do retain a
kernel mapping (e.g.  CMA regions) do get scanned by kmemleak.

This is not ideal for two reasons:

1  kmemleak works by scanning memory regions for pointers to allocated
   objects to determine if those objects have been leaked or not.
   However, reserved memory regions can be used between drivers and
   peripherals for DMA transfers, and thus, would not contain pointers to
   allocated objects, making it unnecessary for kmemleak to scan these
   reserved memory regions.

2  When CONFIG_DEBUG_PAGEALLOC is enabled, along with kmemleak, the
   CMA reserved memory regions are unmapped from the kernel's address
   space when they are freed to buddy at boot.  These CMA reserved regions
   are still tracked by kmemleak, however, and when kmemleak attempts to
   scan them, a crash will happen, as accessing the CMA region will result
   in a page-fault, since the regions are unmapped.

Thus, use kmemleak_ignore_phys() for all dynamically allocated reserved
memory regions, instead of those that do not have a kernel mapping
associated with them.

Link: https://lkml.kernel.org/r/20230208232001.2052777-1-isaacmanjarres@google.com
Link: https://lkml.kernel.org/r/20230208232001.2052777-2-isaacmanjarres@google.com
Fixes: a7259df767 ("memblock: make memblock_find_in_range method private")
Signed-off-by: Isaac J. Manjarres <isaacmanjarres@google.com>
Acked-by: Mike Rapoport (IBM) <rppt@kernel.org>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Cc: Frank Rowand <frowand.list@gmail.com>
Cc: Kirill A. Shutemov <kirill.shtuemov@linux.intel.com>
Cc: Nick Kossifidis <mick@ics.forth.gr>
Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Cc: Rob Herring <robh@kernel.org>
Cc: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
Cc: Saravana Kannan <saravanak@google.com>
Cc: <stable@vger.kernel.org>	[5.15+]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-02-22 12:57:07 +01:00
..
unittest-data of: unittest: Statically apply overlays using fdtoverlay 2021-03-23 15:27:51 -06:00
Kconfig of: net: move of_net under net/ 2022-03-08 19:12:41 +01:00
Makefile of: net: move of_net under net/ 2022-03-08 19:12:41 +01:00
address.c of/address: Return an error when no valid dma-ranges are found 2023-02-14 19:17:57 +01:00
base.c of: base: Improve argument length mismatch error 2022-01-27 11:05:19 +01:00
device.c of: device: Fix missing of_node_put() in of_dma_set_restricted_buffer 2022-08-17 14:23:51 +02:00
dynamic.c of: Add missing 'Return' section in kerneldoc comments 2021-03-27 12:59:03 -06:00
fdt.c of: fdt: fix off-by-one error in unflatten_dt_nodes() 2022-09-23 14:15:46 +02:00
fdt_address.c
irq.c of: Add missing 'Return' section in kerneldoc comments 2021-03-27 12:59:03 -06:00
kexec.c of/kexec: Fix reading 32-bit "linux,initrd-{start,end}" values 2023-01-12 11:58:53 +01:00
kobj.c of: Don't allow __of_attached_node_sysfs() without CONFIG_SYSFS 2021-08-23 13:52:09 -05:00
of_numa.c
of_private.h of: Move of_dma_set_restricted_buffer() into device.c 2021-08-23 06:32:02 -04:00
of_reserved_mem.c of: reserved_mem: Have kmemleak ignore dynamically allocated reserved mem 2023-02-22 12:57:07 +01:00
overlay.c of: overlay: fix null pointer dereferencing in find_dup_cset_node_entry() and find_dup_cset_prop() 2022-12-31 13:14:19 +01:00
pdt.c
platform.c iommu: Drop unnecessary of_iommu.h includes 2021-06-08 14:15:46 +02:00
property.c of: property: decrement node refcount in of_fwnode_get_reference_args() 2022-12-08 11:28:39 +01:00
resolver.c
unittest.c of: unittest: 64 bit dma address test requires arch support 2022-01-27 11:04:12 +01:00