kernel-hacking-2024-linux-s.../drivers
Isaac J. Manjarres ce4d9a1ea3 of: reserved_mem: Have kmemleak ignore dynamically allocated reserved mem
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>
2023-02-09 15:56:51 -08:00
..
accel
accessibility
acpi Merge branches 'acpi-resource' and 'acpi-video' 2023-01-13 11:11:05 +01:00
amba
android
ata ata: pata_cs5535: Don't build on UML 2023-01-14 07:38:48 +09:00
atm
auxdisplay
base
bcma
block xen: branch for v6.2-rc4 2023-01-12 17:02:20 -06:00
bluetooth
bus
cdrom
char xen: branch for v6.2-rc4 2023-01-12 17:02:20 -06:00
clk
clocksource
comedi
connector
counter
cpufreq cpufreq: amd-pstate: fix kernel hang issue while amd-pstate unregistering 2023-01-10 20:31:08 +01:00
cpuidle
crypto MTD changes: 2023-01-12 05:56:06 -06:00
cxl
dax
dca
devfreq
dio
dma
dma-buf
edac EDAC/highbank: Fix memory leak in highbank_mc_probe() 2023-01-03 17:03:57 +01:00
eisa
extcon
firewire
firmware kernel hardening fixes for v6.2-rc4 2023-01-14 10:04:00 -06:00
fpga
fsi
gnss
gpio
gpu drm fixes for 6.2-rc4 2023-01-13 07:18:59 -06:00
greybus
hid
hsi
hte
hv
hwmon
hwspinlock
hwtracing
i2c
i3c
idle
iio
infiniband
input xen: branch for v6.2-rc4 2023-01-12 17:02:20 -06:00
interconnect
iommu iommu/mediatek-v1: Fix an error handling path in mtk_iommu_v1_probe() 2023-01-13 13:46:32 +01:00
ipack
irqchip
isdn
leds
macintosh
mailbox
mcb
md block: handle bio_split_to_limits() NULL return 2023-01-04 09:05:23 -07:00
media
memory
memstick
message
mfd
misc
mmc
most
mtd
mux
net Including fixes from rxrpc. 2023-01-12 18:20:44 -06:00
nfc nfc: pn533: Wait for out_urb's completion in pn533_usb_send_frame() 2023-01-09 07:34:13 +00:00
ntb
nubus
nvdimm
nvme block-6.2-2023-01-13 2023-01-13 17:41:19 -06:00
nvmem
of of: reserved_mem: Have kmemleak ignore dynamically allocated reserved mem 2023-02-09 15:56:51 -08:00
opp
parisc
parport
pci pci-v6.2-fixes-1 2023-01-13 17:32:22 -06:00
pcmcia
peci
perf
phy
pinctrl
platform platform/x86: thinkpad_acpi: Fix profile mode display in AMT mode 2023-01-13 11:40:30 +01:00
pnp
power
powercap
pps
ps3
ptp
pwm
rapidio
ras
regulator regulator: qcom-rpmh: PM8550 ldo11 regulator is an nldo 2023-01-03 15:54:38 +00:00
remoteproc
reset
rpmsg
rtc
s390 block-2023-01-06 2023-01-06 13:12:42 -08:00
sbus
scsi SCSI fixes on 20230114 2023-01-14 07:57:25 -06:00
sh
siox
slimbus
soc
soundwire
spi spi: Merge rename of spi-cs-setup-ns DT property 2023-01-11 14:15:22 +00:00
spmi
ssb
staging
target
tc
tee
thermal
thunderbolt
tty xen: branch for v6.2-rc4 2023-01-12 17:02:20 -06:00
ufs
uio
usb xen: branch for v6.2-rc4 2023-01-12 17:02:20 -06:00
vdpa
vfio
vhost
video xen: branch for v6.2-rc4 2023-01-12 17:02:20 -06:00
virt
virtio
vlynq
w1
watchdog
xen xen: branch for v6.2-rc4 2023-01-12 17:02:20 -06:00
zorro
Kconfig
Makefile