Add a sysfs entry /sys/kernel/kexec/crash_cma_ranges to expose all CMA crashkernel ranges.
This allows userspace tools configuring kdump to determine how much memory is reserved for crashkernel. If CMA is used, tools can warn users when attempting to capture user pages with CMA reservation. The new sysfs hold the CMA ranges in below format: cat /sys/kernel/kexec/crash_cma_ranges 100000000-10c7fffff There are already four kexec and kdump sysfs entries under /sys/kernel. Adding more entries there would clutter the directory. To avoid this, the new crash_cma_ranges sysfs entry is placed in a new kexec node under /sys/kernel/. The reason for not including Crash CMA Ranges in /proc/iomem is to avoid conflicts. It has been observed that contiguous memory ranges are sometimes shown as two separate System RAM entries in /proc/iomem. If a CMA range overlaps two System RAM ranges, adding crashk_res to /proc/iomem can create a conflict. Reference [1] describes one such instance on the PowerPC architecture. Link: https://lore.kernel.org/all/[email protected]/ [1] Cc: Aditya Gupta <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Baoquan he <[email protected]> Cc: Dave Young <[email protected]> Cc: Hari Bathini <[email protected]> Cc: Jiri Bohac <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Cc: Mahesh J Salgaonkar <[email protected]> Cc: Pingfan Liu <[email protected]> Cc: Ritesh Harjani (IBM) <[email protected]> Cc: Shivang Upadhyay <[email protected]> Cc: Vivek Goyal <[email protected]> Cc: [email protected] Cc: [email protected] Signed-off-by: Sourabh Jain <[email protected]> --- Changelog: v4 -> v5: https://lore.kernel.org/all/[email protected]/ - Splitted patch from the above patch series. - Code to create kexec node under /sys/kernel is added, eariler it was done in [02/05] of the above patch series. Note: This patch is dependent on the below patch: https://lore.kernel.org/all/[email protected]/ --- .../ABI/testing/sysfs-kernel-kexec-kdump | 10 ++++ kernel/kexec_core.c | 50 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-kernel-kexec-kdump b/Documentation/ABI/testing/sysfs-kernel-kexec-kdump index 96b24565b68e..320ec75a4903 100644 --- a/Documentation/ABI/testing/sysfs-kernel-kexec-kdump +++ b/Documentation/ABI/testing/sysfs-kernel-kexec-kdump @@ -41,3 +41,13 @@ Description: read only is used by the user space utility kexec to support updating the in-kernel kdump image during hotplug operations. User: Kexec tools + +What: /sys/kernel/kexec/crash_cma_ranges +Date: Nov 2025 +Contact: [email protected] +Description: read only + Provides information about the memory ranges reserved from + the Contiguous Memory Allocator (CMA) area that are allocated + to the crash (kdump) kernel. It lists the start and end physical + addresses of CMA regions assigned for crashkernel use. +User: kdump service diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index fa00b239c5d9..51b1e0985eac 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -41,6 +41,7 @@ #include <linux/objtool.h> #include <linux/kmsg_dump.h> #include <linux/dma-map-ops.h> +#include <linux/sysfs.h> #include <asm/page.h> #include <asm/sections.h> @@ -1229,3 +1230,52 @@ int kernel_kexec(void) kexec_unlock(); return error; } + +#ifdef CONFIG_CRASH_RESERVE +static ssize_t crash_cma_ranges_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + + ssize_t len = 0; + int i; + + for (i = 0; i < crashk_cma_cnt; ++i) { + len += sysfs_emit_at(buf, len, "%08llx-%08llx\n", + crashk_cma_ranges[i].start, + crashk_cma_ranges[i].end); + } + return len; +} +static struct kobj_attribute crash_cma_ranges_attr = __ATTR_RO(crash_cma_ranges); + +static struct attribute *kexec_attrs[] = { + &crash_cma_ranges_attr.attr, + NULL +}; + +static struct kobject *kexec_kobj; +ATTRIBUTE_GROUPS(kexec); + +static int __init init_kexec_sysctl(void) +{ + int error; + + kexec_kobj = kobject_create_and_add("kexec", kernel_kobj); + if (!kexec_kobj) { + pr_err("failed to create kexec kobject\n"); + return -ENOMEM; + } + + error = sysfs_create_groups(kexec_kobj, kexec_groups); + if (error) + goto kset_exit; + + return 0; + +kset_exit: + kobject_put(kexec_kobj); + return error; +} + +subsys_initcall(init_kexec_sysctl); +#endif /* CONFIG_CRASH_RESERVE */ -- 2.51.1
