From nobody Fri Dec 19 20:54:05 2025 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C484D1F893B for ; Wed, 8 Jan 2025 10:15:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736331336; cv=none; b=TQglM3eVF3YCQ01jhsETvCGrIhqt7nDO9zVTjm3w4usoHWhyPcD1ESfjQD0OQ0ycYlUmCs4UFKt/cefVhezDgA8o+/l5FMjMkxsZ3cmT+pW+ahJqAgHa3QI+2VXV6NBTyzSn8TskznhLUYy/wO984Gug6T+eaSHBlDwuD9SNBAQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736331336; c=relaxed/simple; bh=68MVemKVjrWvsDaZ2B9zKpmUOv+r6+1Zk22mAGq2dak=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=im5z73y3D7V+nInhn1Ey40XK9PA33H4ierqGgdIARVnzmpO0QE1UuERxMDwnpCOQQDCDwFcH6NeunnNhl8RoA5bUyAttAf40D3t8KI9PaaDAWz8Jt5Hzc1+3n/BLoMEzHQGov9e14wIPw15y8bqfcpvpWW/knrLtLTC2dQXZi64= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=qJXB2jJg; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="qJXB2jJg" Received: from pps.filterd (m0353725.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 50864qHv007032; Wed, 8 Jan 2025 10:15:22 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=dCDK6BOsU4ZX7A04s KP0HMI7l9HAPh7z4+ZQG7MoYvY=; b=qJXB2jJgoWyHIlPSIE7mvi7NwXU7AQivN GxgK4kkoVurLNpA+8jElojZzrwpaiSwSoDQM+sSNTflkXmcyNos5l5uyRkh0IeV8 li4FIevEEh+V4my1SANgPohqCt+XKrUiZbTOyZiZ1pTahWbbAhgt82YQRnrDgaqN bpwPMNhvdCHVLP3vdfH55VABXyTitvQ5sanN1DNFXPTmwpmXt+mOmMkNhY6FVYtr nTo6VX1eTtFQefZXQwALHQB+/Z8iH6AVZTBEghEgCUngyDqoDJmQ1qhOKpRedM9e U4IFkRNsRZ1XcvKyPxGJeLS/SbSyBb40ZJrA2tjxLcB9TU57ny7WQ== Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4415r54r53-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 08 Jan 2025 10:15:21 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 5086OFF3027963; Wed, 8 Jan 2025 10:15:20 GMT Received: from smtprelay06.fra02v.mail.ibm.com ([9.218.2.230]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 43yhhk7030-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 08 Jan 2025 10:15:20 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay06.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 508AFHEY20840730 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 8 Jan 2025 10:15:17 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3DEDD20065; Wed, 8 Jan 2025 10:15:17 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 776752005A; Wed, 8 Jan 2025 10:15:15 +0000 (GMT) Received: from li-4f5ba44c-27d4-11b2-a85c-a08f5b49eada.in.ibm.com (unknown [9.109.204.94]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 8 Jan 2025 10:15:15 +0000 (GMT) From: Sourabh Jain To: linuxppc-dev@lists.ozlabs.org Cc: Sourabh Jain , Andrew Morton , Baoquan he , Hari Bathini , Madhavan Srinivasan , Michael Ellerman , kexec@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH RESEND v1 5/5] crash: option to let arch decide mem range is usable Date: Wed, 8 Jan 2025 15:44:58 +0530 Message-ID: <20250108101458.406806-6-sourabhjain@linux.ibm.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250108101458.406806-1-sourabhjain@linux.ibm.com> References: <20250108101458.406806-1-sourabhjain@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: DW3et_kc-6OhuUpJsf0nbcpvoXKOZcCr X-Proofpoint-GUID: DW3et_kc-6OhuUpJsf0nbcpvoXKOZcCr X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 suspectscore=0 phishscore=0 clxscore=1015 adultscore=0 bulkscore=0 impostorscore=0 malwarescore=0 mlxscore=0 priorityscore=1501 lowpriorityscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2501080082 Content-Type: text/plain; charset="utf-8" On PowerPC, the memory reserved for the crashkernel can contain components like RTAS, TCE, OPAL, etc., which should be avoided when loading kexec segments into crashkernel memory. Due to these special components, PowerPC has its own set of functions to locate holes in the crashkernel memory for loading kexec segments for kdump. However, for loading kexec segments in the kexec case, PowerPC uses generic functions to locate holes. So, let's use generic functions to locate memory holes for kdump on PowerPC by adding an arch hook to handle such special regions while loading kexec segments, and remove the PowerPC functions to locate holes. Cc: Andrew Morton Cc: Baoquan he Cc: Hari Bathini CC: Madhavan Srinivasan Cc: Michael Ellerman Cc: kexec@lists.infradead.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Sourabh Jain --- arch/powerpc/include/asm/kexec.h | 6 +- arch/powerpc/kexec/file_load_64.c | 259 ++---------------------------- include/linux/kexec.h | 9 ++ kernel/kexec_file.c | 12 ++ 4 files changed, 34 insertions(+), 252 deletions(-) diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/ke= xec.h index 64741558071f..5e4680f9ff35 100644 --- a/arch/powerpc/include/asm/kexec.h +++ b/arch/powerpc/include/asm/kexec.h @@ -95,8 +95,10 @@ int arch_kexec_kernel_image_probe(struct kimage *image, = void *buf, unsigned long int arch_kimage_file_post_load_cleanup(struct kimage *image); #define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_clea= nup =20 -int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf); -#define arch_kexec_locate_mem_hole arch_kexec_locate_mem_hole +int arch_check_excluded_range(struct kimage *image, unsigned long start, + unsigned long end); +#define arch_check_excluded_range arch_check_excluded_range + =20 int load_crashdump_segments_ppc64(struct kimage *image, struct kexec_buf *kbuf); diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_lo= ad_64.c index dc65c1391157..e7ef8b2a2554 100644 --- a/arch/powerpc/kexec/file_load_64.c +++ b/arch/powerpc/kexec/file_load_64.c @@ -49,201 +49,18 @@ const struct kexec_file_ops * const kexec_file_loaders= [] =3D { NULL }; =20 -/** - * __locate_mem_hole_top_down - Looks top down for a large enough memory h= ole - * in the memory regions between buf_min & bu= f_max - * for the buffer. If found, sets kbuf->mem. - * @kbuf: Buffer contents and memory parameters. - * @buf_min: Minimum address for the buffer. - * @buf_max: Maximum address for the buffer. - * - * Returns 0 on success, negative errno on error. - */ -static int __locate_mem_hole_top_down(struct kexec_buf *kbuf, - u64 buf_min, u64 buf_max) -{ - int ret =3D -EADDRNOTAVAIL; - phys_addr_t start, end; - u64 i; - - for_each_mem_range_rev(i, &start, &end) { - /* - * memblock uses [start, end) convention while it is - * [start, end] here. Fix the off-by-one to have the - * same convention. - */ - end -=3D 1; - - if (start > buf_max) - continue; - - /* Memory hole not found */ - if (end < buf_min) - break; - - /* Adjust memory region based on the given range */ - if (start < buf_min) - start =3D buf_min; - if (end > buf_max) - end =3D buf_max; - - start =3D ALIGN(start, kbuf->buf_align); - if (start < end && (end - start + 1) >=3D kbuf->memsz) { - /* Suitable memory range found. Set kbuf->mem */ - kbuf->mem =3D ALIGN_DOWN(end - kbuf->memsz + 1, - kbuf->buf_align); - ret =3D 0; - break; - } - } - - return ret; -} - -/** - * locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a - * suitable buffer with top down approach. - * @kbuf: Buffer contents and memory parameters. - * @buf_min: Minimum address for the buffer. - * @buf_max: Maximum address for the buffer. - * @emem: Exclude memory ranges. - * - * Returns 0 on success, negative errno on error. - */ -static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf, - u64 buf_min, u64 buf_max, - const struct crash_mem *emem) +int arch_check_excluded_range(struct kimage *image, unsigned long start, + unsigned long end) { - int i, ret =3D 0, err =3D -EADDRNOTAVAIL; - u64 start, end, tmin, tmax; - - tmax =3D buf_max; - for (i =3D (emem->nr_ranges - 1); i >=3D 0; i--) { - start =3D emem->ranges[i].start; - end =3D emem->ranges[i].end; - - if (start > tmax) - continue; - - if (end < tmax) { - tmin =3D (end < buf_min ? buf_min : end + 1); - ret =3D __locate_mem_hole_top_down(kbuf, tmin, tmax); - if (!ret) - return 0; - } - - tmax =3D start - 1; - - if (tmax < buf_min) { - ret =3D err; - break; - } - ret =3D 0; - } - - if (!ret) { - tmin =3D buf_min; - ret =3D __locate_mem_hole_top_down(kbuf, tmin, tmax); - } - return ret; -} - -/** - * __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory= hole - * in the memory regions between buf_min & b= uf_max - * for the buffer. If found, sets kbuf->mem. - * @kbuf: Buffer contents and memory parameters. - * @buf_min: Minimum address for the buffer. - * @buf_max: Maximum address for the buffer. - * - * Returns 0 on success, negative errno on error. - */ -static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf, - u64 buf_min, u64 buf_max) -{ - int ret =3D -EADDRNOTAVAIL; - phys_addr_t start, end; - u64 i; - - for_each_mem_range(i, &start, &end) { - /* - * memblock uses [start, end) convention while it is - * [start, end] here. Fix the off-by-one to have the - * same convention. - */ - end -=3D 1; - - if (end < buf_min) - continue; - - /* Memory hole not found */ - if (start > buf_max) - break; - - /* Adjust memory region based on the given range */ - if (start < buf_min) - start =3D buf_min; - if (end > buf_max) - end =3D buf_max; - - start =3D ALIGN(start, kbuf->buf_align); - if (start < end && (end - start + 1) >=3D kbuf->memsz) { - /* Suitable memory range found. Set kbuf->mem */ - kbuf->mem =3D start; - ret =3D 0; - break; - } - } - - return ret; -} - -/** - * locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a - * suitable buffer with bottom up approa= ch. - * @kbuf: Buffer contents and memory parameters. - * @buf_min: Minimum address for the buffer. - * @buf_max: Maximum address for the buffer. - * @emem: Exclude memory ranges. - * - * Returns 0 on success, negative errno on error. - */ -static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf, - u64 buf_min, u64 buf_max, - const struct crash_mem *emem) -{ - int i, ret =3D 0, err =3D -EADDRNOTAVAIL; - u64 start, end, tmin, tmax; - - tmin =3D buf_min; - for (i =3D 0; i < emem->nr_ranges; i++) { - start =3D emem->ranges[i].start; - end =3D emem->ranges[i].end; - - if (end < tmin) - continue; - - if (start > tmin) { - tmax =3D (start > buf_max ? buf_max : start - 1); - ret =3D __locate_mem_hole_bottom_up(kbuf, tmin, tmax); - if (!ret) - return 0; - } - - tmin =3D end + 1; + struct crash_mem *emem; + int i; =20 - if (tmin > buf_max) { - ret =3D err; - break; - } - ret =3D 0; - } + emem =3D image->arch.exclude_ranges; + for (i =3D 0; i < emem->nr_ranges; i++) + if (start < emem->ranges[i].end && end > emem->ranges[i].start) + return 1; =20 - if (!ret) { - tmax =3D buf_max; - ret =3D __locate_mem_hole_bottom_up(kbuf, tmin, tmax); - } - return ret; + return 0; } =20 #ifdef CONFIG_CRASH_DUMP @@ -1004,64 +821,6 @@ int setup_new_fdt_ppc64(const struct kimage *image, v= oid *fdt, struct crash_mem return ret; } =20 -/** - * arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opa= l, - * tce-table, reserved-ranges & such (exclude - * memory ranges) as they can't be used for k= exec - * segment buffer. Sets kbuf->mem when a suit= able - * memory hole is found. - * @kbuf: Buffer contents and memory parameters. - * - * Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_alig= n. - * - * Returns 0 on success, negative errno on error. - */ -int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf) -{ - struct crash_mem **emem; - u64 buf_min, buf_max; - int ret; - - /* Look up the exclude ranges list while locating the memory hole */ - emem =3D &(kbuf->image->arch.exclude_ranges); - if (!(*emem) || ((*emem)->nr_ranges =3D=3D 0)) { - pr_warn("No exclude range list. Using the default locate mem hole method= \n"); - return kexec_locate_mem_hole(kbuf); - } - - buf_min =3D kbuf->buf_min; - buf_max =3D kbuf->buf_max; - /* Segments for kdump kernel should be within crashkernel region */ - if (IS_ENABLED(CONFIG_CRASH_DUMP) && kbuf->image->type =3D=3D KEXEC_TYPE_= CRASH) { - buf_min =3D (buf_min < crashk_res.start ? - crashk_res.start : buf_min); - buf_max =3D (buf_max > crashk_res.end ? - crashk_res.end : buf_max); - } - - if (buf_min > buf_max) { - pr_err("Invalid buffer min and/or max values\n"); - return -EINVAL; - } - - if (kbuf->top_down) - ret =3D locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max, - *emem); - else - ret =3D locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max, - *emem); - - /* Add the buffer allocated to the exclude list for the next lookup */ - if (!ret) { - add_mem_range(emem, kbuf->mem, kbuf->memsz); - sort_memory_ranges(*emem, true); - } else { - pr_err("Failed to locate memory buffer of size %lu\n", - kbuf->memsz); - } - return ret; -} - /** * arch_kexec_kernel_image_probe - Does additional handling needed to setup * kexec segments. diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f0e9f8eda7a3..407f8b0346aa 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -205,6 +205,15 @@ static inline int arch_kimage_file_post_load_cleanup(s= truct kimage *image) } #endif =20 +#ifndef arch_check_excluded_range +static inline int arch_check_excluded_range(struct kimage *image, + unsigned long start, + unsigned long end) +{ + return 0; +} +#endif + #ifdef CONFIG_KEXEC_SIG #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_le= n); diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 3eedb8c226ad..52e1480dbfa1 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -464,6 +464,12 @@ static int locate_mem_hole_top_down(unsigned long star= t, unsigned long end, continue; } =20 + /* Make sure this does not conflict exclude range */ + if (arch_check_excluded_range(image, temp_start, temp_end)) { + temp_start =3D temp_start - PAGE_SIZE; + continue; + } + /* We found a suitable memory range */ break; } while (1); @@ -498,6 +504,12 @@ static int locate_mem_hole_bottom_up(unsigned long sta= rt, unsigned long end, continue; } =20 + /* Make sure this does not conflict exclude range */ + if (arch_check_excluded_range(image, temp_start, temp_end)) { + temp_start =3D temp_start + PAGE_SIZE; + continue; + } + /* We found a suitable memory range */ break; } while (1); --=20 2.47.1