From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout02.his.huawei.com (canpmsgout02.his.huawei.com [113.46.200.217]) (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 8F6DA4570FA; Fri, 18 Sep 2026 10:04:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.217 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725861; cv=none; b=S44I8EjllcVwcskDiJKCJFYkjdI4ip7FkLTyObzx0jNG9iHH2o2E0jaq9aKf95HtrsyidAMAyjijWjTbAF4hLvPrBjYxIZqKYJWv7r7sGxALbst7W93H+GDvBDwKtjZcV9yRIOhIo2DeHsJLUHm0ifEBJMELcmM9TPInASQt230= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725861; c=relaxed/simple; bh=OMbzw4uT78uE0UCyzaGrvjYdVsESPkacJ328Z3Pvr3E=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GrkLcikC09IUCgnTgdhDdYGEYzBYITvOobWvP3i8BuHiCMOGIi+nw9TdsTBmyHTrxnzXb9cdMWrX08H0836wu/mqjzv5EPYVVlDqaceJ9aUPUNQkH+ADPPjPhdFeVTG+4Xq3axApIp2e1yEvPPP2KtdItFIDIR9UelFt5v+cdfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=Ez/wOSF7; arc=none smtp.client-ip=113.46.200.217 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="Ez/wOSF7" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=gzeLYP4W/HVtauBH6zrP+s/J9Q0YG9Qv0eMgDswYLO0=; b=Ez/wOSF7kN0QxhLrYOxwsAq0t/qVSH7zO1LO6a7rzHbsnPODXfipqqsojEw0qCoGxc7ny7coD r5CTzt7mwcrfYR0Ns1b2jGOyjEoC1z/DtVergIB5XHUbsALHHtqEUbsn6WEYdxjWUtLytZbUcoZ lEboIM0h4256GoXqxjGf9KM= Received: from mail.maildlp.com (unknown [172.19.162.197]) by canpmsgout02.his.huawei.com (SkyGuard) with ESMTPS id 4hmSbq689nzcb4c; Fri, 18 Sep 2026 17:52:55 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id E5DD54057D; Fri, 18 Sep 2026 18:04:10 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:09 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 01/17] kexec: Fix CMA segment address translation with non-zero text_offset Date: Fri, 18 Sep 2026 18:04:26 +0800 Message-ID: <20260918100442.3841135-2-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" kimage_load_cma_segment() and kimage_map_segment() translate a CMA segment to a kernel virtual address with page_address(cma), ignoring segment->mem. But arm64's image_load() adds text_offset to segment->mem, so the kernel payload is copied to the wrong offset while image->start points past it, and kexec jumps into the middle of the kernel. kimage_map_segment() has the same problem for any CMA segment whose mem was moved. Add kimage_cma_vaddr() to translate a boot physical address inside a CMA segment to its virtual address, and use it in both places. Cc: Andrew Morton Cc: Baoquan He Cc: Mike Rapoport Cc: Pasha Tatashin Cc: Pratyush Yadav Cc: Pingfan Liu Cc: Justinien Bouron Cc: Sourabh Jain Cc: stable@vger.kernel.org Fixes: 07d24902977e ("kexec: enable CMA based contiguous allocation") Signed-off-by: Jinjie Ruan --- kernel/kexec_core.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index dc770b9a6d05..7f7cb77f0caa 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -738,11 +738,23 @@ static struct page *kimage_alloc_page(struct kimage *= image, return page; } =20 +/* + * Translate a boot physical address inside a CMA segment to a kernel + * virtual address. Architecture loaders may move segment->mem away from + * the CMA base (arm64 adds text_offset), so the offset must be preserved. + */ +static void *kimage_cma_vaddr(struct page *cma, unsigned long mem) +{ + unsigned long cma_base =3D page_to_boot_pfn(cma) << PAGE_SHIFT; + + return page_address(cma) + (mem - cma_base); +} + static int kimage_load_cma_segment(struct kimage *image, int idx) { struct kexec_segment *segment =3D &image->segment[idx]; struct page *cma =3D image->segment_cma[idx]; - char *ptr =3D page_address(cma); + char *ptr =3D kimage_cma_vaddr(cma, segment->mem); size_t ubytes, mbytes; int result =3D 0; unsigned char __user *buf =3D NULL; @@ -965,7 +977,7 @@ void *kimage_map_segment(struct kimage *image, int idx) =20 cma =3D image->segment_cma[idx]; if (cma) - return page_address(cma); + return kimage_cma_vaddr(cma, image->segment[idx].mem); =20 addr =3D image->segment[idx].mem; size =3D image->segment[idx].memsz; --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout05.his.huawei.com (canpmsgout05.his.huawei.com [113.46.200.220]) (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 9079B483BEC; Fri, 18 Sep 2026 10:04:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.220 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725861; cv=none; b=uy7kf73NT/Bgya7mNhTObLfeqpjEK6eWrUOI8G7I/CR43Ah5bSJbWSrNTIcuvAHUBPXW0WlOUTUlJAv6Pz6BDoIiKW0h94Ic/FSkcYr9iU0XjbHnp0+jEwvRd/ESDpq6XYRIfI8+oHpx0tD5emPee2jwSH9jIUQBNqO7L00+0ak= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725861; c=relaxed/simple; bh=jMLaafSPE8s83Dax3RdGcMRjHwthgNzgViogh5E9GpA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=pUykUzkD9njYLiyWe+O4DKpPt59Itw1nZcMiWUmSXQem/PzsuWCKLZQyaY3tNzugAInoXMj8mmFaMtM/iNfXjwkdbFvvOkw7APLRAEkdSWJzXIwrlfV2A9tlkZefHUvuuJfMZu/QAdNGdzC14KF/Js/XHMW0g1RIwfdubCCssZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=h1jxo3g6; arc=none smtp.client-ip=113.46.200.220 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="h1jxo3g6" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=g746orfjBM5FKI/4ioFM0MPbnRDGHMAm7NJngqq6WSA=; b=h1jxo3g6i+Np9G/ygFulWombg/WWMVz/nTSO7gjsBS9mUvrsf8A1hYwT/QK9YNw/PYEjbGJJP flmsFMVVXB+OPm2NGt/WJCIxlXEKLJyUYJ29/n9rb9hEiJCkb2OYQAVDWCoW/izsogrfAUkYoKj HDssVLSCyfy0uWkUl/kaLnw= Received: from mail.maildlp.com (unknown [172.19.162.144]) by canpmsgout05.his.huawei.com (SkyGuard) with ESMTPS id 4hmSc12nmtz12LHn; Fri, 18 Sep 2026 17:53:05 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 6093540538; Fri, 18 Sep 2026 18:04:12 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:10 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 02/17] kexec: Record allocated CMA pages to fix release size mismatch Date: Fri, 18 Sep 2026 18:04:27 +0800 Message-ID: <20260918100442.3841135-3-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" The CMA pages allocated for a kexec segment are released using the segment's memsz to calculate the number of pages. However, some architecture loaders modify the segment's memsz after allocation (e.g. arm64 subtracts text_offset), causing the release function to free fewer pages than were originally allocated, leaking the remaining CMA pages. Add a per-segment `segment_cma_pages` array to store the number of pages actually allocated from CMA. Populate it during kexec_add_buffer() using the aligned memsz, and use it in kimage_free_cma() to accurately release all allocated pages. This avoids relying on the potentially modified segment->memsz and prevents silent CMA memory leaks. Cc: Andrew Morton Cc: Baoquan He Cc: Mike Rapoport Cc: Pasha Tatashin Cc: Pratyush Yadav Cc: Brian Mak Cc: Pingfan Liu Cc: Sourabh Jain Cc: Justinien Bouron Cc: Li Chen Cc: stable@vger.kernel.org Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40= huawei.com Fixes: 07d24902977e ("kexec: enable CMA based contiguous allocation") Signed-off-by: Jinjie Ruan --- include/linux/kexec.h | 1 + kernel/kexec_core.c | 7 ++++--- kernel/kexec_file.c | 13 +++++++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 0af8ae4fdd08..6b1df80524bf 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -349,6 +349,7 @@ struct kimage { unsigned long nr_segments; struct kexec_segment segment[KEXEC_SEGMENT_MAX]; struct page *segment_cma[KEXEC_SEGMENT_MAX]; + unsigned long segment_cma_pages[KEXEC_SEGMENT_MAX]; =20 struct list_head control_pages; struct list_head dest_pages; diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 7f7cb77f0caa..5493591a9e4b 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -560,14 +560,15 @@ static void kimage_free_cma(struct kimage *image) =20 for (i =3D 0; i < image->nr_segments; i++) { struct page *cma =3D image->segment_cma[i]; - u32 nr_pages =3D image->segment[i].memsz >> PAGE_SHIFT; + unsigned long nr_pages =3D image->segment_cma_pages[i]; =20 if (!cma) continue; =20 - arch_kexec_pre_free_pages(page_address(cma), nr_pages); - dma_release_from_contiguous(NULL, cma, nr_pages); + arch_kexec_pre_free_pages(page_address(cma), (unsigned int)nr_pages); + dma_release_from_contiguous(NULL, cma, (int)nr_pages); image->segment_cma[i] =3D NULL; + image->segment_cma_pages[i] =3D 0; } =20 } diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 59fb9d71e9d8..67d6df9824e6 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -670,7 +670,7 @@ static int kexec_walk_resources(struct kexec_buf *kbuf, =20 static int kexec_alloc_contig(struct kexec_buf *kbuf) { - size_t nr_pages =3D kbuf->memsz >> PAGE_SHIFT; + size_t nr_pages =3D PFN_DOWN(kbuf->memsz); unsigned long mem; struct page *p; =20 @@ -756,14 +756,16 @@ int kexec_locate_mem_hole(struct kexec_buf *kbuf) */ int kexec_add_buffer(struct kexec_buf *kbuf) { + unsigned long nr_segments =3D kbuf->image->nr_segments; struct kexec_segment *ksegment; + size_t nr_cma_pages =3D 0; int ret; =20 /* Currently adding segment this way is allowed only in file mode */ if (!kbuf->image->file_mode) return -EINVAL; =20 - if (kbuf->image->nr_segments >=3D KEXEC_SEGMENT_MAX) + if (nr_segments >=3D KEXEC_SEGMENT_MAX) return -EINVAL; =20 /* @@ -789,12 +791,15 @@ int kexec_add_buffer(struct kexec_buf *kbuf) return ret; =20 /* Found a suitable memory range */ - ksegment =3D &kbuf->image->segment[kbuf->image->nr_segments]; + ksegment =3D &kbuf->image->segment[nr_segments]; ksegment->kbuf =3D kbuf->buffer; ksegment->bufsz =3D kbuf->bufsz; ksegment->mem =3D kbuf->mem; ksegment->memsz =3D kbuf->memsz; - kbuf->image->segment_cma[kbuf->image->nr_segments] =3D kbuf->cma; + kbuf->image->segment_cma[nr_segments] =3D kbuf->cma; + if (kbuf->cma) + nr_cma_pages =3D PFN_DOWN(kbuf->memsz); + kbuf->image->segment_cma_pages[nr_segments] =3D nr_cma_pages; kbuf->image->nr_segments++; return 0; } --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout04.his.huawei.com (canpmsgout04.his.huawei.com [113.46.200.219]) (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 92F43485CEC; Fri, 18 Sep 2026 10:04:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.219 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725862; cv=none; b=AQHr74kam3xPcN+HegVPYOVMizTxyHmecXHG+VFVcqyxUC7+OOtPhIWvfB1zMglBjFYd9YObMiqJEEuL98aQeATtNC+0bYBOXNeL+tx/MGo6LT9NZh0OPWAA3B/Y9pw4nN38AoEDm8x4ai41bcq7u9c6iE8V+ehQyCR2gO2HFiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725862; c=relaxed/simple; bh=ntluBHrwYzwN8mq/Qnbdl2zgjNZNzbbvpZ5IEpeRQIM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uY1tM9b3PhHgAyLu74/3jb+aAz6hdvMUkAFkIJFQIcAhrJkYE8pK/s6UiEL+3vXo0W8xa3d6KkspPiyx4aYBbD69GIt/JM67Qr9niMDvg78t8nLMd1mEH7YyUGnxIozT7vOtSM8ih+R7RcmDs+SPAmyeClOwWoSGnxR48dTxF/0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=M3xDCzOS; arc=none smtp.client-ip=113.46.200.219 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="M3xDCzOS" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=2Eq1XJNYhRFTE/SJtlUDPWTtcQMeeDeygEezx5WcQDg=; b=M3xDCzOSH1ex1aIXQYlZy5bUMNl8IOExQZttwkBTy/1chlRHO/kzqULiUCzyJpa1xmha57Pt9 6lPA8D6mDEfjpfxI7U4GV7kTxHty4Tg4b4+dOqX5QVt6cyuL1vky1HDVwNZXgBujl8bGaIfFt0t WgzQI/6r2eltMXLNXd6LJ7c= Received: from mail.maildlp.com (unknown [172.19.163.0]) by canpmsgout04.his.huawei.com (SkyGuard) with ESMTPS id 4hmSc83Hvbz1prKW; Fri, 18 Sep 2026 17:53:12 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id B2E5B40537; Fri, 18 Sep 2026 18:04:13 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:12 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 03/17] kexec: Extract kexec_free_segment_cma() from kimage_free_cma() Date: Fri, 18 Sep 2026 18:04:28 +0800 Message-ID: <20260918100442.3841135-4-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" kimage_free_cma() relies on image->nr_segments to iterate over segments. When an architecture loader (e.g., arm64) truncates nr_segments on a mid-way failure, CMA pages allocated beyond the new boundary become unreachable, causing silent memory leaks. Extract the per-segment freeing logic into the exported helper kexec_free_segment_cma(), so that architecture loaders can release individual segments before nr_segments is truncated. Refactor kimage_free_cma() to loop over the new helper, preserving existing behavior. Cc: Andrew Morton Cc: Baoquan He Cc: Mike Rapoport Cc: Pasha Tatashin Cc: Pratyush Yadav Signed-off-by: Jinjie Ruan --- include/linux/kexec.h | 2 ++ kernel/kexec_core.c | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 6b1df80524bf..72258d813301 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -533,6 +533,7 @@ extern bool kexec_file_dbg_print; =20 extern void *kimage_map_segment(struct kimage *image, int idx); extern void kimage_unmap_segment(void *buffer); +void kexec_free_segment_cma(struct kimage *image, unsigned long idx); #else /* !CONFIG_KEXEC_CORE */ struct pt_regs; struct task_struct; @@ -544,6 +545,7 @@ static inline int kexec_crash_loaded(void) { return 0; } static inline void *kimage_map_segment(struct kimage *image, int idx) { return NULL; } static inline void kimage_unmap_segment(void *buffer) { } +static inline void kexec_free_segment_cma(struct kimage *image, unsigned l= ong idx) { } #define kexec_in_progress false #endif /* CONFIG_KEXEC_CORE */ =20 diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 5493591a9e4b..31c8228318a7 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -554,23 +554,26 @@ static void kimage_free_entry(kimage_entry_t entry) kimage_free_pages(page); } =20 -static void kimage_free_cma(struct kimage *image) +void kexec_free_segment_cma(struct kimage *image, unsigned long idx) { - unsigned long i; + unsigned long nr_pages =3D image->segment_cma_pages[idx]; + struct page *cma =3D image->segment_cma[idx]; =20 - for (i =3D 0; i < image->nr_segments; i++) { - struct page *cma =3D image->segment_cma[i]; - unsigned long nr_pages =3D image->segment_cma_pages[i]; + if (!cma) + return; =20 - if (!cma) - continue; + arch_kexec_pre_free_pages(page_address(cma), (unsigned int)nr_pages); + dma_release_from_contiguous(NULL, cma, (int)nr_pages); + image->segment_cma[idx] =3D NULL; + image->segment_cma_pages[idx] =3D 0; +} =20 - arch_kexec_pre_free_pages(page_address(cma), (unsigned int)nr_pages); - dma_release_from_contiguous(NULL, cma, (int)nr_pages); - image->segment_cma[i] =3D NULL; - image->segment_cma_pages[i] =3D 0; - } +static void kimage_free_cma(struct kimage *image) +{ + unsigned long i; =20 + for (i =3D 0; i < image->nr_segments; i++) + kexec_free_segment_cma(image, i); } =20 void kimage_free(struct kimage *image) --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout03.his.huawei.com (canpmsgout03.his.huawei.com [113.46.200.218]) (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 4D82047D925; Fri, 18 Sep 2026 10:04:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725862; cv=none; b=X0qtEvXOHKaqCf5XsbdgeO9skPobHLU8HQT9TBHqj8W31/BOktfM0NbrrvWlPoHz91U5XMMBel43KFPIcJDvmj4wrYa6GzX/c5lWiF4W/EIWVlsmpG5gGl5eReVbw50/+Ewxd/1JZk4zyyrvMP2cEX2gaOxoz2bS91X9SrAFWEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725862; c=relaxed/simple; bh=dwgncQqVyzPFNessPOGKN2UeVG9vVedPe+OsTFQgmXU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XSxv6/Tm+MBRXRJnbPNkz8GLFbzPZE8Eh8/42rscPNO3XcF2Kj/1I7/lD7J9BnZTER+gXyEdajG+vggt6dZMHQTBjqi7bhHxjKTU+wqsUpqroIdk/A7EAtut6YTHjryU/rskTz+R9sUUT+8m8bZM881Sy6hTnGk4HLcWLadfZak= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=CtxVcq19; arc=none smtp.client-ip=113.46.200.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="CtxVcq19" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=igqtNYMEL8oKIpnu2DrMNvEYzwwJSEovzlP+FnYUOCA=; b=CtxVcq19s4/+nlwp4cUNXfDx1nZiyIWFU4uWCub+//6KtP7pZWS9arcnPlF2LC/ogoKXyeVeP TCuE1W141iovkLTXAPEBXvujBx6CaVYA0od9WENgqjGwUbcGu+QPm1w6sqo05U8KePvOSjI314O 40wUQWbWiVj05nmSEjphxXc= Received: from mail.maildlp.com (unknown [172.19.162.140]) by canpmsgout03.his.huawei.com (SkyGuard) with ESMTPS id 4hmSbG0MSrzpSyG; Fri, 18 Sep 2026 17:52:26 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 26E1A20333; Fri, 18 Sep 2026 18:04:15 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:13 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 04/17] arm64: kexec_file: Fix CMA page leaks in segment placement retry loops Date: Fri, 18 Sep 2026 18:04:29 +0800 Message-ID: <20260918100442.3841135-5-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" During kexec image placement retry loops, any midway failure causes the loader to truncate `image->nr_segments` back to its initial state to purge the failed segments. However, this truncation introduces a memory leak. The CMA pages allocated via kexec_add_buffer() during the failed attempt are tracked in the `image->segment_cma` array. Because the subsequent cleanup paths only iterate up to the truncated `nr_segments` boundary, these allocated CMA pages outside the new boundary are permanently leaked. Fix this by explicitly releasing the associated CMA buffers in the failure paths before `image->nr_segments` is reduced. Cc: Catalin Marinas Cc: Will Deacon Cc: Breno Leitao Cc: Pratyush Yadav Cc: Andrew Morton Cc: Yeoreum Yun Cc: Baoquan He Cc: stable@vger.kernel.org Fixes: 07d24902977e4 ("kexec: enable CMA based contiguous allocation") Signed-off-by: Jinjie Ruan --- arch/arm64/kernel/kexec_image.c | 1 + arch/arm64/kernel/machine_kexec_file.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_imag= e.c index b70f4df15a1a..ffcb7f9075e6 100644 --- a/arch/arm64/kernel/kexec_image.c +++ b/arch/arm64/kernel/kexec_image.c @@ -107,6 +107,7 @@ static void *image_load(struct kimage *image, * We couldn't find space for the other segments; erase the * kernel segment and try the next available hole. */ + kexec_free_segment_cma(image, kernel_segment_number); image->nr_segments -=3D 1; kbuf.buf_min =3D kernel_segment->mem + kernel_segment->memsz; kbuf.mem =3D KEXEC_BUF_MEM_UNKNOWN; diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/mac= hine_kexec_file.c index 854d872dfd0f..e48f29167b38 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -179,7 +179,10 @@ int load_other_segments(struct kimage *image, return 0; =20 out_err: - image->nr_segments =3D orig_segments; + while (image->nr_segments > orig_segments) { + kexec_free_segment_cma(image, image->nr_segments - 1); + image->nr_segments--; + } kvfree(dtb); return ret; } --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout01.his.huawei.com (canpmsgout01.his.huawei.com [113.46.200.216]) (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 9AFDD49A3A7; Fri, 18 Sep 2026 10:04:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.216 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725869; cv=none; b=d9CQzyDnEtI+Px2dTuxDCUMSPbg1DKJpDNOT4CmRluPZqcgiD9s4l68D9qxhjPpBfTHU6JZuBYOgdx9uIxmw1Sx+55ZPhA4M93SnPvwUuBn+421dxhbGf+R6jlNVF7iRAWep250/suW8sI80oKOtches8prjkJ6GGGwOEFnvj14= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725869; c=relaxed/simple; bh=yiGxQ7QIQdwQ3p4Fr0ygdVNI7N08QTdXriOuGM/62Gk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=P//VuA0fJ7kblwX60gxTfYLjA+k+5TMdsg0ja9RavcsAzZATz5B6xq2t+UfxD42+myZB5qsZCAL0mLKJmILQbwaD+oZki2wGhRjA08rKyVTkKQXPzFwIUKzAbmRdUy+sViO/lFIY50SEWIAsYxvw0Bx4Y0x6j4cccwd1Gmi1hcs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=cXqqzSbx; arc=none smtp.client-ip=113.46.200.216 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="cXqqzSbx" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=RK4VHRefALxjC8rB7K7w3VAwG6W6KlTC58IWMIYwUgI=; b=cXqqzSbxz3lvEuOS9efT6kFVnZWykzECj9tiFIF8xUH5zySw0l0wtrrPJavS5/6c0ugtHU44w TQx608nHzqqoqjifoGQQm3bSkj4dVZdXvljaZRoir8tVoNs8JWQj9Dkf8ZWXUzNH3gtlFlTVTbj f2PJ9QOpTcwTuI1J8MwWSoI= Received: from mail.maildlp.com (unknown [172.19.162.197]) by canpmsgout01.his.huawei.com (SkyGuard) with ESMTPS id 4hmSbN1YwLz1T4MQ; Fri, 18 Sep 2026 17:52:32 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 8D05940591; Fri, 18 Sep 2026 18:04:16 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:14 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 05/17] arm64: kexec_file: Fix elf_headers memory leak in retry loop Date: Fri, 18 Sep 2026 18:04:30 +0800 Message-ID: <20260918100442.3841135-6-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" If load_other_segments() fails after image->elf_headers is assigned, the memory lifecycle is safely managed by the global kimage object and will be freed in arch_kimage_file_post_load_cleanup(). However, during a retry loop in image_load(), a subsequent iteration will allocate a new buffer and overwrite image->elf_headers. This permanently leaks the stale memory from the previous iteration before the global cleanup can track it. Fix this by explicitly freeing the stale `image->elf_headers` buffer before assigning the newly allocated headers. Cc: Catalin Marinas Cc: Will Deacon Cc: Thomas Huth Cc: Breno Leitao Cc: Andrew Morton Cc: Yeoreum Yun Cc: Baoquan He Cc: stable@vger.kernel.org Fixes: 108aa503657e ("arm64: kexec_file: try more regions if loading segmen= ts fails") Signed-off-by: Jinjie Ruan --- arch/arm64/kernel/machine_kexec_file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/mac= hine_kexec_file.c index e48f29167b38..2f750e5f4fcc 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -112,6 +112,10 @@ int load_other_segments(struct kimage *image, vfree(headers); goto out_err; } + + if (unlikely(image->elf_headers)) + vfree(image->elf_headers); + image->elf_headers =3D headers; image->elf_load_addr =3D kbuf.mem; image->elf_headers_sz =3D headers_sz; --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout07.his.huawei.com (canpmsgout07.his.huawei.com [113.46.200.222]) (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 80DCD47ECD0; Fri, 18 Sep 2026 10:04:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.222 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725866; cv=none; b=Zaxzg6kZJLhwhwyMuJnk/zgE0lhAxRq/qoj/0BUQw4SXGAnvBnmT7BW1p9lTOssclosKTCyuBHbc9dlsxVaHUTwAfwf1mckxkdx5FQBgsuIc3978y4oZN1HLtDlV1UVW/a2h3iq3+dTK3ogEe1Easf9hHkN+4S5n4D/31RBvkwI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725866; c=relaxed/simple; bh=pqwmROV2ldLwW0tL4b4mcAsvlFNFi2XdYsiPH9Ty7ls=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fa4JArnpsikxpI1OStvL2Oh+RLKFxZv2YMRsozg+9REnLyExDYJNVjcKMQb9tBUVQ9Y9j6tRNCuIdrZqKe9q51GGmJk/X1TwWXyTEM5ErfRY2DlNAbta0O+giEEljnsVGc08NtLfkfHNHUHAUWBdTFZEkdmzY5wcATu4b7F/mco= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=xq1YbTvD; arc=none smtp.client-ip=113.46.200.222 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="xq1YbTvD" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=C/N27OffVwB9HknmerMZN+vGpNQOngX4+yhYYxhlpgs=; b=xq1YbTvD3XNNnXqFEYm+rNDxG6EdsqcQH0HTMN1s5TayGIm1XkNSyQvmkbCWJmLw2RC3Hg9MW 03OTT0taTlXRmwul+dn+StNpv/it+wBQ3leQZDkbShT3eaE7XAglReIYtuKqLTKOKW4s7jvAaSh i66n7enwbsFGUXQhFmGkLKQ= Received: from mail.maildlp.com (unknown [172.19.163.214]) by canpmsgout07.his.huawei.com (SkyGuard) with ESMTPS id 4hmScG0wm4zLlTm; Fri, 18 Sep 2026 17:53:18 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 0BD9C4057C; Fri, 18 Sep 2026 18:04:18 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:16 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 06/17] LoongArch: kexec_file: Fix CMA page leaks in segment placement retry loops Date: Fri, 18 Sep 2026 18:04:31 +0800 Message-ID: <20260918100442.3841135-7-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" During kexec image placement retry loops, any midway failure causes the loader to truncate `image->nr_segments` back to its initial state to purge the failed segments. However, this truncation introduces a memory leak. The CMA pages allocated via kexec_add_buffer() during the failed attempt are tracked in the `image->segment_cma` array. Because the subsequent cleanup paths only iterate up to the truncated `nr_segments` boundary, these allocated CMA pages outside the new boundary are permanently leaked. Fix this by explicitly releasing the associated CMA buffers in the failure paths before `image->nr_segments` is reduced. Cc: Huacai Chen Cc: WANG Xuerui Cc: Youling Tang Cc: "Mike Rapoport (Microsoft)" Cc: Sourabh Jain Cc: Kees Cook Cc: stable@vger.kernel.org Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40= huawei.com Fixes: 55d990f0084c ("LoongArch: Add EFI binary support for kexec_file") Signed-off-by: Jinjie Ruan --- arch/loongarch/kernel/kexec_efi.c | 1 + arch/loongarch/kernel/machine_kexec_file.c | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/loongarch/kernel/kexec_efi.c b/arch/loongarch/kernel/kexe= c_efi.c index 5ee78ebb1546..15fd797ff3de 100644 --- a/arch/loongarch/kernel/kexec_efi.c +++ b/arch/loongarch/kernel/kexec_efi.c @@ -86,6 +86,7 @@ static void *efi_kexec_load(struct kimage *image, * We couldn't find space for the other segments; erase the * kernel segment and try the next available hole. */ + kexec_free_segment_cma(image, kernel_segment_number); image->nr_segments -=3D 1; kbuf.buf_min =3D kernel_segment->mem + kernel_segment->memsz; kbuf.mem =3D KEXEC_BUF_MEM_UNKNOWN; diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/ke= rnel/machine_kexec_file.c index 5412aa9f3568..62a5be102065 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -217,7 +217,11 @@ int load_other_segments(struct kimage *image, return 0; =20 out_err: - image->nr_segments =3D orig_segments; + while (image->nr_segments > orig_segments) { + kexec_free_segment_cma(image, image->nr_segments - 1); + image->nr_segments--; + } + kfree(modified_cmdline); return ret; } --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout09.his.huawei.com (canpmsgout09.his.huawei.com [113.46.200.224]) (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 E294048FF6F; Fri, 18 Sep 2026 10:04:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.224 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725874; cv=none; b=S2wY9mpMhVM4m/ty9qoMk4Pr8bjBvHyt5LfZ698bRI8OTiMC2wuEUU/RtBsa5Kk+cSWgRwz/rM49bZybOiA2SFfIqW1KDqwa/+gntJQYDllc5J0pH8stQQcIxc2qc7mIryQIcd8Ik/kaYBeGyJizz90p3Q/r0sUOe+C10IWreKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725874; c=relaxed/simple; bh=LUUN6hor9kB1vGW9ORLacT7hasUnGUVa0oXa/BtcYiM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dnTX0Daf0k+XmIUPFngeUEWksXfPmhHO2Qxw9wW5yukdmfiHWXlN9xmVrVZEHiWFaIHGHRU4Q2OZXbLzvNn2tkhPfQgVZpXJ2dukgzzbIMpIfrYtcveN131XEP97L3YDT8R9/UbZSKmZn1rThq22rMniZK/OliCPi/Hqc/JQvMQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=WJsU5ZLP; arc=none smtp.client-ip=113.46.200.224 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="WJsU5ZLP" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=up4/oktyfAdON3km3wdZWwjp1/r+vQu8s3QiiQB06UU=; b=WJsU5ZLPqqnObamEl+eDb1OQLn8fzTm2lS+jqZvsjkBOcZoPNvOCq137VskBlGKfkXIRHLxz0 r3TIwLJgnYKX+hhXcyhYsMrcfZw4l3uKg5x5sTenzOpFogQlp8Dx4ywI3SARgUR1LDQ5r9Wjz1F GJzxQD5oYk6LTh2w3OIGcw8= Received: from mail.maildlp.com (unknown [172.19.163.214]) by canpmsgout09.his.huawei.com (SkyGuard) with ESMTPS id 4hmScJ1Tbcz1cyV9; Fri, 18 Sep 2026 17:53:20 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 7569B4057C; Fri, 18 Sep 2026 18:04:19 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:17 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 07/17] LoongArch: kexec_file: Fix elf_headers memory leak in retry loop Date: Fri, 18 Sep 2026 18:04:32 +0800 Message-ID: <20260918100442.3841135-8-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" If load_other_segments() fails after image->elf_headers is assigned, the memory lifecycle is safely managed by the global kimage object and will be freed in arch_kimage_file_post_load_cleanup(). However, during a retry loop in efi_kexec_load(), a subsequent iteration will allocate a new buffer and overwrite image->elf_headers. This permanently leaks the stale memory from the previous iteration before the global cleanup can track it. Fix this by explicitly freeing the stale `image->elf_headers` buffer before assigning the newly allocated headers. Cc: Huacai Chen Cc: WANG Xuerui Cc: Youling Tang Cc: "Mike Rapoport (Microsoft)" Cc: Sourabh Jain Cc: Kees Cook Cc: stable@vger.kernel.org Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40= huawei.com Fixes: 55d990f0084c ("LoongArch: Add EFI binary support for kexec_file") Signed-off-by: Jinjie Ruan --- arch/loongarch/kernel/machine_kexec_file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/ke= rnel/machine_kexec_file.c index 62a5be102065..3beb6977ecc6 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -166,6 +166,10 @@ int load_other_segments(struct kimage *image, vfree(headers); goto out_err; } + + if (unlikely(image->elf_headers)) + vfree(image->elf_headers); + image->elf_headers =3D headers; image->elf_load_addr =3D kbuf.mem; image->elf_headers_sz =3D headers_sz; --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout07.his.huawei.com (canpmsgout07.his.huawei.com [113.46.200.222]) (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 07B7D42CB0D; Fri, 18 Sep 2026 10:04:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.222 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725866; cv=none; b=nqiyudFPY2yd2OXI59SuAY74iA5s+A1ZTD4TQuodNSW7DQRrGR3IXqLbY9Obbw4g8WMJPddSO/WQke+SCyCxCzAlbg1sfHztftuD7j5Pn+g/N6h1rPMPhUQ3cyfz3+DEvrN5zyMY//ARy5WS9UoxPPkbzdD1as2ibOb3Ty65yPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725866; c=relaxed/simple; bh=Ygfamq+zVCgcwFeC3h6mtl26Diwqy0m0fQ1dYnRCot0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZkCP7UvKEVXU85ftIvPLMBSKfODBLTxTrkFcejCUvY5G2kG2CR0UevgjYreA279hFtnPnmrWMLAEFvCIkvXgxJNy5RSHi54y3A6PLhNAkeKB+sFyqDhVi5OEkZE/h4stBMByr1FkHd8hhSK5u47M8vV4401Fx6ZP9fRZ2w0ATTQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=h20MzYgr; arc=none smtp.client-ip=113.46.200.222 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="h20MzYgr" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=20ZJiQbk77XpGyt2zLlFzhTV8L6cgd0AT+Mg72ZNP+o=; b=h20MzYgrtfLEEV4IsF22m8M0aeU+9fIDiLpJWraCVTgEvlTBvhYu0irkyY7dZ8KlR25j3eFDX uDHiuEi34Vyc2FyCL6c39JSfjX+M3Cz00j/vkUz1PTc66H//7kIYPJQEJrTEE3IZKzXvZCqcFXg laCNtc/NvKopW2lnm4CTtD4= Received: from mail.maildlp.com (unknown [172.19.162.92]) by canpmsgout07.his.huawei.com (SkyGuard) with ESMTPS id 4hmScK00bdzLlX1; Fri, 18 Sep 2026 17:53:20 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id DC0BB40586; Fri, 18 Sep 2026 18:04:20 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:19 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 08/17] LoongArch: kexec_file: Fix a modified_cmdline leak Date: Fri, 18 Sep 2026 18:04:33 +0800 Message-ID: <20260918100442.3841135-9-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" load_other_segments() allocates modified_cmdline and stores it in image->arch.cmdline_ptr. machine_kexec_prepare() then copies it to KEXEC_CMDLINE_ADDR and overwrites the pointer, so the heap buffer is leaked on every successful kexec_file_load(). Free the buffer after the copy. Cc: Huacai Chen Cc: WANG Xuerui Cc: Kexin Liu Cc: Youling Tang Cc: Qiang Ma Cc: Tianyang Zhang Cc: George Guo Cc: stable@vger.kernel.org Fixes: d162feec6b6e ("LoongArch: Add preparatory infrastructure for kexec_f= ile") Signed-off-by: Jinjie Ruan --- arch/loongarch/kernel/machine_kexec.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/loongarch/kernel/machine_kexec.c b/arch/loongarch/kernel/= machine_kexec.c index 1883cae93bc3..19ccc91b6f2a 100644 --- a/arch/loongarch/kernel/machine_kexec.c +++ b/arch/loongarch/kernel/machine_kexec.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include =20 @@ -56,6 +57,7 @@ int machine_kexec_prepare(struct kimage *kimage) */ memcpy((void *)KEXEC_CMDLINE_ADDR, (void *)kimage->arch.cmdline_ptr, strlen((char *)kimage->arch.cmdline_ptr) + 1); + kfree((void *)kimage->arch.cmdline_ptr); kimage->arch.cmdline_ptr =3D (unsigned long)KEXEC_CMDLINE_ADDR; } else { /* Find the command line */ --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout12.his.huawei.com (canpmsgout12.his.huawei.com [113.46.200.227]) (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 80EC1491588; Fri, 18 Sep 2026 10:04:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.227 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725874; cv=none; b=tqGRK1LOXaBCLbGMiSk26zPY+5iclKlltGX2oGvYhhUHEyt+YcTgNLADnTTNPfs0RrLK3MK1M7zplsCZ7l2Srggbc65BavJkdv3oylT2EvdEaxGzWGEe3ldgikX6p5zA0MueBv/KFE8jOjeIUamVPvkpPap9B5l+Nz1gaf01XSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725874; c=relaxed/simple; bh=wjR21x1VZ0jBGYVQvbCGyafKMjtbiu05vUZUySil/VY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aeN0b0SBgPsKgLEhyYHlZ6CgK1eUcUu9tMDV2DBQQzgaksAhAeBADjzpYZYUlA20ved/H3uHsJ6re6UcjxoWtIB8d1hmAOjpEyOyo1hGnaAt3u2AXZPgBKvD59mIJRtlI7VPmF0i3fB7/vao5HOuc2L491PTM7xuThvUjZt/sXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=wVs/Hs9G; arc=none smtp.client-ip=113.46.200.227 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="wVs/Hs9G" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=gl9dOVAVG56ScS9MwUOL4momh5UtqrFy6pN1ShFcuZM=; b=wVs/Hs9Gq6VG/cWAMpWvzJmy/oIAMsdmFRrHLaMN8dhT/208NNm+Jy57C8JdLMxP37J0PUlpB GvTnqbaNF3Y1buLGfFQc0tAbMgmC9XUKdLppSMLKPgLCRodGAQm9QZNfIEVZADGVfpSUfrMTgAo 355Yjh2Bx4bZrE0WgEv0YQ0= Received: from mail.maildlp.com (unknown [172.19.163.163]) by canpmsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4hmScG2rCdznTWH; Fri, 18 Sep 2026 17:53:18 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 492124048B; Fri, 18 Sep 2026 18:04:22 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:20 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 09/17] x86/crash: Fix massive out-of-bounds write on 32-bit Highmem Date: Fri, 18 Sep 2026 18:04:34 +0800 Message-ID: <20260918100442.3841135-10-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" On 32-bit x86 systems with HIGHMEM, kmap_local_page() only maps a single 4KB page. However, the elfcorehdr segment can span several pages (up to hundreds of kilobytes). The original code blindly copies 'elfsz' bytes at once via memcpy_flushcache(), overwriting adjacent fixmap entries or critical virtual addresses. Fix this by copying the new elfcorehdr page by page. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: "Mike Rapoport (Microsoft)" Cc: Vishal Verma Cc: Baoquan He Cc: Chao Gao Cc: Sean Christopherson Cc: Eric DeVolder Cc: Hari Bathini Cc: Andrew Morton Cc: Sourabh Jain Cc: stable@vger.kernel.org Fixes: ea53ad9cf73b ("x86/crash: add x86 crash hotplug support") Link: https://sashiko.dev/#/patchset/20260907125404.922123-1-ruanjinjie%40h= uawei.com Signed-off-by: Jinjie Ruan --- arch/x86/kernel/crash.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index e681ec9cf1dc..3c9f4fbbe7ff 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -447,9 +447,10 @@ unsigned int arch_crash_get_elfcorehdr_size(void) */ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) { - void *elfbuf =3D NULL, *old_elfcorehdr; unsigned long mem, memsz; unsigned long elfsz =3D 0; + void *elfbuf =3D NULL; + unsigned long done; =20 /* * As crash_prepare_elf64_headers() has already described all @@ -484,21 +485,20 @@ void arch_crash_handle_hotplug_event(struct kimage *i= mage, void *arg) =20 /* * Copy new elfcorehdr over the old elfcorehdr at destination. - */ - old_elfcorehdr =3D kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); - if (!old_elfcorehdr) { - pr_err("mapping elfcorehdr segment failed\n"); - goto out; - } - - /* - * Temporarily invalidate the crash image while the - * elfcorehdr is updated. + * The segment is physically contiguous but can span several pages. + * On 32-bit Highmem architectures, kmap_local_page() maps only a + * single page at a time, so copy page by page. */ xchg(&kexec_crash_image, NULL); - memcpy_flushcache(old_elfcorehdr, elfbuf, elfsz); + for (done =3D 0; done < elfsz; ) { + size_t chunk =3D min_t(size_t, PAGE_SIZE, elfsz - done); + void *dst =3D kmap_local_page(pfn_to_page((mem + done) >> PAGE_SHIFT)); + + memcpy_flushcache(dst, elfbuf + done, chunk); + kunmap_local(dst); + done +=3D chunk; + } xchg(&kexec_crash_image, image); - kunmap_local(old_elfcorehdr); pr_debug("updated elfcorehdr\n"); =20 out: --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout09.his.huawei.com (canpmsgout09.his.huawei.com [113.46.200.224]) (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 BCFF44A7CAC; Fri, 18 Sep 2026 10:04:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.224 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725876; cv=none; b=AY+MofYRcBVBEqNhwAoM8XJWfAuXjfyuaLcP6cNaFLcMZUhGte8iqRztZTcokt1OFqVWz6W2GyvHFPFTYwboH4LldpkeWmvzbsnR6WG4iORsbiwMC81AUnGWB/S/2z2rFrXHpIwK82DnER+ACxTiHqk81JdM98DKA5q+sj1dy40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725876; c=relaxed/simple; bh=2KdupsWceweyWYND6ijvaYhreMt2HBNEZIG8MHJnBs8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tSGmhLjieGW9bTOv2acbqYXwBkzbuTMDEfnxXg+38cVGmPhodORPYtNSzzUBa7i3Oa3mz9oQJnXXvW0Ha0U/1189PBlEpWpfMDe5/yyBfGQlxXkXv9pcMLGsAwB0dL+dQtivJ8aAZQ6qpUDFnJL+3RR6ErL/8PkuPLa0zjsHNIM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=QDOtzo0Y; arc=none smtp.client-ip=113.46.200.224 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="QDOtzo0Y" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=+SZRrezQ0aGrf6aoi2mjbpyD9DzDa0MzzJwmMo2zVh0=; b=QDOtzo0YbEPraF8hBNN1eIHHylUMG/0f8u5ka7yGUNTwM1OnOYxP9zWXeAlrcrPh3LHLBghb2 nIQRJeX/fS4RhMOLtRND7crtI/jS6NinldAxrfL97sRs7OyUEmKcrvK/a+f42xeMlDcgBE0logz Y70h41sj0wu9iQS94ph0bQ8= Received: from mail.maildlp.com (unknown [172.19.162.92]) by canpmsgout09.his.huawei.com (SkyGuard) with ESMTPS id 4hmScN3GQlz1cyVL; Fri, 18 Sep 2026 17:53:24 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id AD84440586; Fri, 18 Sep 2026 18:04:23 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:22 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 10/17] crash: Extract crash_get_memory_ranges() helper Date: Fri, 18 Sep 2026 18:04:35 +0800 Message-ID: <20260918100442.3841135-11-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" Factor out the crash memory range collection logic from crash_prepare_headers() into a separate function. This allows the memory hotplug path to obtain and modify the range list (e.g. remove offlined memory) before generating the elfcorehdr. Cc: Andrew Morton Cc: Baoquan He Cc: Mike Rapoport Cc: Pasha Tatashin Cc: Pratyush Yadav Cc: Dave Young Signed-off-by: Jinjie Ruan --- include/linux/crash_core.h | 1 + kernel/crash_core.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h index bc087124cd78..b1c816e98143 100644 --- a/include/linux/crash_core.h +++ b/include/linux/crash_core.h @@ -62,6 +62,7 @@ extern int crash_prepare_elf64_headers(struct crash_mem *= mem, int need_kernel_ma extern int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long *sz, unsigned long *nr_mem_ranges); extern int crash_exclude_core_ranges(struct crash_mem **cmem); +int crash_get_memory_ranges(struct crash_mem **mem_ranges); =20 struct kimage; struct kexec_segment; diff --git a/kernel/crash_core.c b/kernel/crash_core.c index 2b36aa9fade0..406b68d2adfd 100644 --- a/kernel/crash_core.c +++ b/kernel/crash_core.c @@ -317,8 +317,7 @@ int crash_exclude_core_ranges(struct crash_mem **cmem) return 0; } =20 -int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long = *sz, - unsigned long *nr_mem_ranges) +int crash_get_memory_ranges(struct crash_mem **mem_ranges) { unsigned int max_nr_ranges; struct crash_mem *cmem; @@ -344,13 +343,30 @@ int crash_prepare_headers(int need_kernel_map, void *= *addr, unsigned long *sz, if (ret) goto out; =20 + *mem_ranges =3D cmem; + return 0; + +out: + kvfree(cmem); + return ret; +} + +int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long = *sz, + unsigned long *nr_mem_ranges) +{ + struct crash_mem *cmem =3D NULL; + int ret; + + ret =3D crash_get_memory_ranges(&cmem); + if (ret) + return ret; + /* Return the computed number of memory ranges, for hotplug usage */ if (nr_mem_ranges) *nr_mem_ranges =3D cmem->nr_ranges; =20 ret =3D crash_prepare_elf64_headers(cmem, need_kernel_map, addr, sz); =20 -out: kvfree(cmem); return ret; } --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout05.his.huawei.com (canpmsgout05.his.huawei.com [113.46.200.220]) (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 B587248C8C7; Fri, 18 Sep 2026 10:04:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.220 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725874; cv=none; b=DQ69SkkB5tsJuijp/Xi0a562e7Q7f4Wt71/IVZK2K9hh0pgOQuusmmb/E9+stvSVKMH7+SFE4TySUax6HdaZGZ1F1a+wBP0l3fHvj8jVrvaIYg/4nTCchp7iCp3V5stBYk6CVoPplRTo5Nl87rqxdluUOR5MPlxez2HkdPq5a2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725874; c=relaxed/simple; bh=Buvn155pBJjURrSDDuwnfKSngOV/uoq0pUJUZ4yGNTw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JAkhHfb81e8bGZ+OFZH2KpChVSVYIUcAQWNXqbvFwADGF76dH9WrxsnzPDR2oKsJrEx3O6mr88cUWLVfGuoVa/KmyamR+Jo2WvoE5CcKJlTz6VawUQJguI+hAWJY5eisV8DWAEa1bqt7JUwVVPa7QDBKADq0otcCxWLZYuAKIo8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=zvyDPHtM; arc=none smtp.client-ip=113.46.200.220 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="zvyDPHtM" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=QWRwGA8IIWGqEmgXFHVo35ysA7ES/LPOJVNWp6AUBps=; b=zvyDPHtMXrIlKFMijCNSGV93POHhDpPEWLDf6NcDHeY4LQlngXcrut7XT/+zJayfwCPKA+CjB rIjWb7CcBTXVBn1eLuOcyP5UPmoRPnG64cslN+lcdwQdBUwDiDN+pGdNJR2HL+wQjqIhN5D0E/5 lx7RD09w72DQURnUsPZ6Wmk= Received: from mail.maildlp.com (unknown [172.19.162.144]) by canpmsgout05.his.huawei.com (SkyGuard) with ESMTPS id 4hmScG1kRsz12LHj; Fri, 18 Sep 2026 17:53:18 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 36D4240572; Fri, 18 Sep 2026 18:04:25 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:23 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 11/17] crash: Normalize the kexec_load elfcorehdr at load time Date: Fri, 18 Sep 2026 18:04:36 +0800 Message-ID: <20260918100442.3841135-12-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" For kexec_load() the kernel does not build the elfcorehdr, so it is only rewritten by the first crash hotplug event. CPU hotplug events do not change the elfcorehdr, but they may run without device_hotplug_lock (e.g. CPU offlining during suspend), so they cannot perform that rewrite without racing with memory hotplug. Normalize the elfcorehdr once when the crash image is installed, while device_hotplug_lock can still be taken safely, and let the hotplug paths skip CPU events entirely. The elfcorehdr segment is located by scanning the segments for the ELF magic. Factor that scan out into crash_find_elfcorehdr() and reuse it from crash_handle_hotplug_event(). Architectures that do not need the rewrite (e.g. powerpc) treat KEXEC_CRASH_HP_NONE as a no-op. Signed-off-by: Jinjie Ruan --- arch/powerpc/kexec/crash.c | 1 + arch/x86/kernel/crash.c | 5 ++-- include/linux/crash_core.h | 1 + kernel/crash_core.c | 58 +++++++++++++++++++++++++++----------- kernel/kexec.c | 4 +++ 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/arch/powerpc/kexec/crash.c b/arch/powerpc/kexec/crash.c index 775895f31037..8ede33740fc0 100644 --- a/arch/powerpc/kexec/crash.c +++ b/arch/powerpc/kexec/crash.c @@ -638,6 +638,7 @@ void arch_crash_handle_hotplug_event(struct kimage *ima= ge, void *arg) struct memory_notify *mn; =20 switch (image->hp_action) { + case KEXEC_CRASH_HP_NONE: case KEXEC_CRASH_HP_REMOVE_CPU: return; =20 diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index 3c9f4fbbe7ff..f34fa8dba028 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -457,9 +457,8 @@ void arch_crash_handle_hotplug_event(struct kimage *ima= ge, void *arg) * possible CPUs, there is no need to update the elfcorehdr * for additional CPU changes. */ - if ((image->file_mode || image->elfcorehdr_updated) && - ((image->hp_action =3D=3D KEXEC_CRASH_HP_ADD_CPU) || - (image->hp_action =3D=3D KEXEC_CRASH_HP_REMOVE_CPU))) + if (image->hp_action =3D=3D KEXEC_CRASH_HP_ADD_CPU || + image->hp_action =3D=3D KEXEC_CRASH_HP_REMOVE_CPU) return; =20 /* diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h index b1c816e98143..a740757dff35 100644 --- a/include/linux/crash_core.h +++ b/include/linux/crash_core.h @@ -39,6 +39,7 @@ static inline void arch_crash_handle_hotplug_event(struct= kimage *image, void *a #endif =20 int crash_check_hotplug_support(void); +void crash_hotplug_prepare_elfcorehdr(struct kimage *image); =20 #ifndef arch_crash_hotplug_support static inline int arch_crash_hotplug_support(struct kimage *image, unsigne= d long kexec_flags) diff --git a/kernel/crash_core.c b/kernel/crash_core.c index 406b68d2adfd..47a4579eb97e 100644 --- a/kernel/crash_core.c +++ b/kernel/crash_core.c @@ -647,6 +647,28 @@ int crash_check_hotplug_support(void) return rc; } =20 +static void crash_find_elfcorehdr(struct kimage *image) +{ + unsigned char *ptr; + unsigned long mem; + unsigned int n; + + if (image->elfcorehdr_index >=3D 0) + return; + + for (n =3D 0; n < image->nr_segments; n++) { + mem =3D image->segment[n].mem; + ptr =3D kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); + if (!ptr) + continue; + + /* The segment containing elfcorehdr */ + if (memcmp(ptr, ELFMAG, SELFMAG) =3D=3D 0) + image->elfcorehdr_index =3D (int)n; + kunmap_local(ptr); + } +} + /* * To accurately reflect hot un/plug changes of CPU and Memory resources * (including onling and offlining of those resources), the relevant @@ -702,22 +724,7 @@ static void crash_handle_hotplug_event(unsigned int hp= _action, unsigned int cpu, * is allocated. Find the segment containing the elfcorehdr, * if not already found. */ - if (image->elfcorehdr_index < 0) { - unsigned long mem; - unsigned char *ptr; - unsigned int n; - - for (n =3D 0; n < image->nr_segments; n++) { - mem =3D image->segment[n].mem; - ptr =3D kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); - if (ptr) { - /* The segment containing elfcorehdr */ - if (memcmp(ptr, ELFMAG, SELFMAG) =3D=3D 0) - image->elfcorehdr_index =3D (int)n; - kunmap_local(ptr); - } - } - } + crash_find_elfcorehdr(image); =20 if (image->elfcorehdr_index < 0) { pr_err("unable to locate elfcorehdr segment"); @@ -747,6 +754,25 @@ static void crash_handle_hotplug_event(unsigned int hp= _action, unsigned int cpu, crash_hotplug_unlock(); } =20 +void crash_hotplug_prepare_elfcorehdr(struct kimage *image) +{ + if (!image || !image->hotplug_support || image->file_mode) + return; + + crash_find_elfcorehdr(image); + if (image->elfcorehdr_index < 0) + return; + + /* + * kexec_load() images are not normalized at load, so do it here while + * the lock is still free to take. hp_action is KEXEC_CRASH_HP_NONE, + * which the arch handler treats as "just rebuild the elfcorehdr". + */ + lock_device_hotplug(); + arch_crash_handle_hotplug_event(image, NULL); + unlock_device_hotplug(); +} + static int crash_memhp_notifier(struct notifier_block *nb, unsigned long v= al, void *arg) { switch (val) { diff --git a/kernel/kexec.c b/kernel/kexec.c index 90756dc6339b..ea9ba00bc786 100644 --- a/kernel/kexec.c +++ b/kernel/kexec.c @@ -166,6 +166,10 @@ static int do_kexec_load(unsigned long entry, unsigned= long nr_segments, /* Install the new kernel and uninstall the old */ image =3D xchg(dest_image, image); =20 +#ifdef CONFIG_CRASH_HOTPLUG + if ((flags & KEXEC_ON_CRASH) && kexec_crash_image) + crash_hotplug_prepare_elfcorehdr(kexec_crash_image); +#endif out: #ifdef CONFIG_CRASH_DUMP if ((flags & KEXEC_ON_CRASH) && kexec_crash_image) --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout08.his.huawei.com (canpmsgout08.his.huawei.com [113.46.200.223]) (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 A63574B2034; Fri, 18 Sep 2026 10:04:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.223 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725879; cv=none; b=K8LmkntIXmBr3f70yxsOvWHFjwJfTTb3Rp4D0u6gGHSR7tiIrgbDXkvVVMGWCrID7LdOSJ9OqWVp4lJsYMkRibPxm86W++e0iZmCI2gLeVGARTqkyI91kQjGZMGmw0hYlJcG9iCbVz28ezBXPxk82tAllXUFcbkaWiEWGbOb2bo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725879; c=relaxed/simple; bh=XuyhEpvC2GZP/6d5+qorTJgENNMFVxGeTFipF7TW/js=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tlDoX5LaFcGupLklZlcBhId3vHH5XSzr8mCWKoT6kH5d1YcmXDwaAzMUeYYU/SOQce/NaIuOc7iNJEESiRR2lmaSM0sudLIocmSutrjlYELHCJY08O8JwETqCljKflW1E6qb8muwy+m55i73q9WliOSvQjfScVW4cGmdqem8p1I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=ecL8AZSB; arc=none smtp.client-ip=113.46.200.223 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="ecL8AZSB" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=fcjawUwcF+Ipqrmkc8OrDyT490KlVJ99NxRKRbRdpXA=; b=ecL8AZSBtNZyNEDhb7BkDVSF7YYpJKkdn+Gr4NAiy2DW2kneIO5+g5PmiFfrGKL5IsFrau7oz vJR7SAcg2qXV6KQen8h3kFQFaWecc7ZPX+Uxz1BuGMnZIWUhkXy3p4JcCeGOa99CANsaYD4jLJE M1oAwIx3bIJ0dBf+uImYDDI= Received: from mail.maildlp.com (unknown [172.19.163.214]) by canpmsgout08.his.huawei.com (SkyGuard) with ESMTPS id 4hmScQ3fhCzmVXM; Fri, 18 Sep 2026 17:53:26 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 948334057C; Fri, 18 Sep 2026 18:04:26 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:24 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 12/17] crash: Fix TOCTOU race in crash memory range collection Date: Fri, 18 Sep 2026 18:04:37 +0800 Message-ID: <20260918100442.3841135-13-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" The crash kernel ELF core header construction counts system memory ranges via `arch_get_system_nr_ranges()`, allocates the crash_mem buffer, and then populates it via `arch_crash_populate_cmem()`. This sequence has a time-of-check-to-time-of-use (TOCTOU) race with memory hotplug: a concurrent hotplug event between the count and populate steps can increase the number of ranges beyond the allocated capacity, causing an out-of-bounds write. If the event triggers memblock_double_array(), the memblock array can be freed and reallocated during iteration, leading to a use-after-free. Protect the entire range collection with device_hotplug_lock. Since the hotplug notification path already holds that lock, add a lockless helper, crash_get_memory_ranges_nolock(), for use there. The regular crash_get_memory_ranges() acquires the lock and calls the helper. Cc: stable@vger.kernel.org Cc: Andrew Morton Cc: Baoquan He Cc: Mike Rapoport Cc: Pasha Tatashin Cc: Pratyush Yadav Cc: Dave Young Cc: AKASHI Takahiro Cc: Will Deacon Cc: James Morse Cc: Palmer Dabbelt Cc: Youling Tang Cc: Huacai Chen Fixes: 8d5f894a3108 ("x86: kexec_file: lift CRASH_MAX_RANGES limit on crash= _mem buffer") Fixes: 3751e728cef2 ("arm64: kexec_file: add crash dump support") Fixes: 8acea455fafa ("RISC-V: Support for kexec_file on panic") Fixes: 1bcca8620a91 ("LoongArch: Add crash dump support for kexec_file") Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40= huawei.com Signed-off-by: Jinjie Ruan --- arch/x86/kernel/crash.c | 9 ++++++++- include/linux/crash_core.h | 2 +- kernel/crash_core.c | 28 +++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index f34fa8dba028..7fa139725514 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -447,6 +447,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void) */ void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) { + struct crash_mem *cmem =3D NULL; unsigned long mem, memsz; unsigned long elfsz =3D 0; void *elfbuf =3D NULL; @@ -461,11 +462,16 @@ void arch_crash_handle_hotplug_event(struct kimage *i= mage, void *arg) image->hp_action =3D=3D KEXEC_CRASH_HP_REMOVE_CPU) return; =20 + if (crash_get_memory_ranges_nolock(&cmem)) { + pr_err("Failed to get crash mem range\n"); + goto out; + } + /* * Create the new elfcorehdr reflecting the changes to CPU and/or * memory resources. */ - if (crash_prepare_headers(IS_ENABLED(CONFIG_X86_64), &elfbuf, &elfsz, NUL= L)) { + if (crash_prepare_elf64_headers(cmem, IS_ENABLED(CONFIG_X86_64), &elfbuf,= &elfsz)) { pr_err("unable to create new elfcorehdr"); goto out; } @@ -501,6 +507,7 @@ void arch_crash_handle_hotplug_event(struct kimage *ima= ge, void *arg) pr_debug("updated elfcorehdr\n"); =20 out: + kvfree(cmem); vfree(elfbuf); } #endif diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h index a740757dff35..1296a9b29974 100644 --- a/include/linux/crash_core.h +++ b/include/linux/crash_core.h @@ -63,7 +63,7 @@ extern int crash_prepare_elf64_headers(struct crash_mem *= mem, int need_kernel_ma extern int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long *sz, unsigned long *nr_mem_ranges); extern int crash_exclude_core_ranges(struct crash_mem **cmem); -int crash_get_memory_ranges(struct crash_mem **mem_ranges); +int crash_get_memory_ranges_nolock(struct crash_mem **mem_ranges); =20 struct kimage; struct kexec_segment; diff --git a/kernel/crash_core.c b/kernel/crash_core.c index 47a4579eb97e..760ca9822a13 100644 --- a/kernel/crash_core.c +++ b/kernel/crash_core.c @@ -7,6 +7,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 #include +#include #include #include #include @@ -317,7 +318,21 @@ int crash_exclude_core_ranges(struct crash_mem **cmem) return 0; } =20 -int crash_get_memory_ranges(struct crash_mem **mem_ranges) +/** + * crash_get_memory_ranges_nolock - Collect crash kernel memory ranges + * @mem_ranges: Output parameter for the allocated crash_mem structure + * + * Gathers the system memory ranges to be included in the crash kernel's + * ELF core header, excluding the crashkernel reserved region and other + * architecture-specific areas. + * + * Context: Caller must hold device_hotplug_lock. + * + * Return: 0 on success, in which case *@mem_ranges points to a newly + * allocated struct crash_mem that the caller must free with kvfree(). + * Returns a negative error code on failure. + */ +int crash_get_memory_ranges_nolock(struct crash_mem **mem_ranges) { unsigned int max_nr_ranges; struct crash_mem *cmem; @@ -351,6 +366,17 @@ int crash_get_memory_ranges(struct crash_mem **mem_ran= ges) return ret; } =20 +static int crash_get_memory_ranges(struct crash_mem **mem_ranges) +{ + int ret; + + lock_device_hotplug(); + ret =3D crash_get_memory_ranges_nolock(mem_ranges); + unlock_device_hotplug(); + + return ret; +} + int crash_prepare_headers(int need_kernel_map, void **addr, unsigned long = *sz, unsigned long *nr_mem_ranges) { --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout08.his.huawei.com (canpmsgout08.his.huawei.com [113.46.200.223]) (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 9819146A61A; Fri, 18 Sep 2026 10:04:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.223 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725876; cv=none; b=N9uzQxXup6MeeAqP6DTjDVFatPmqKedHJ8SrLFkicOk7uwDYc0rbZFZZvR71xvHsXRpqDxZGwTYSLReWUbn2CYg/Ksy76gpuJvJx268M7K2YITDyESB+H9jnY6+sfmwgeND8fGVv9qG8RdFaS7KN1ozhOEZ5CPTs3r2WgXOkUyU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725876; c=relaxed/simple; bh=p/IWpq+C7u+MF6XwAB7/2JQrgRcXiQhePJ2btwJFrYI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WvRERY+UAQauYhAwMSRFwgr+vZHXG6hnmXKW3ivtSS8F771N3aELaA+IZlh6vVnEg0xX1YeObcYv3kEw+6CjvIIUWEn1dIHZjrtlbNb0+TG70DTtP+P3u25XqcCNZpcSsAijw107Kld/TL0eaLSeocHZ5Sbz6j0XGBpMDJGbi4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=BckyjycA; arc=none smtp.client-ip=113.46.200.223 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="BckyjycA" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=zufRRvpwVHCucp4/a8wU/krIeaWea9Chc2jByLlFL4c=; b=BckyjycAl3ciFIMoUA4aZPGw9VUPen7PpwqZaxbivk06HsP2G2e1o1OYRpVjCGxZ5ScmXWiON VLeKRZcC4u9YJAKQArJ+X4FgbKFWIz3sdq8RcX+POy8ZO6Pxq07qezk90rBm7yrQxrk4UOcJCME uqEZBVLJJhWv39vUktkr7hk= Received: from mail.maildlp.com (unknown [172.19.163.214]) by canpmsgout08.his.huawei.com (SkyGuard) with ESMTPS id 4hmScS01CXzmVWY; Fri, 18 Sep 2026 17:53:27 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 17A674057C; Fri, 18 Sep 2026 18:04:28 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:26 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 13/17] crash: Assert device_hotplug_lock in crash_get_memory_ranges_nolock() Date: Fri, 18 Sep 2026 18:04:38 +0800 Message-ID: <20260918100442.3841135-14-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" crash_get_memory_ranges_nolock() walks memblock, which memory hotplug modifies under device_hotplug_lock. A caller that does not hold the lock can race with memblock_double_array() and iterate a freed regions array. Add device_hotplug_lock_assert_held() and call it at the top of the function so that such callers are caught by lockdep. Signed-off-by: Jinjie Ruan --- drivers/base/core.c | 5 +++++ include/linux/device.h | 1 + kernel/crash_core.c | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 4c0c373998a1..c04adc457a96 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -2420,6 +2420,11 @@ void unlock_device_hotplug(void) mutex_unlock(&device_hotplug_lock); } =20 +void device_hotplug_lock_assert_held(void) +{ + lockdep_assert_held(&device_hotplug_lock); +} + int lock_device_hotplug_sysfs(void) { if (mutex_trylock(&device_hotplug_lock)) diff --git a/include/linux/device.h b/include/linux/device.h index aee79fd6b32b..9cca8c8bd372 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1261,6 +1261,7 @@ do { \ void lock_device_hotplug(void); void unlock_device_hotplug(void); int lock_device_hotplug_sysfs(void); +void device_hotplug_lock_assert_held(void); int device_offline(struct device *dev); int device_online(struct device *dev); =20 diff --git a/kernel/crash_core.c b/kernel/crash_core.c index 760ca9822a13..852a5f51d472 100644 --- a/kernel/crash_core.c +++ b/kernel/crash_core.c @@ -338,6 +338,13 @@ int crash_get_memory_ranges_nolock(struct crash_mem **= mem_ranges) struct crash_mem *cmem; int ret; =20 + /* + * Callers must serialize against memory hotplug by holding + * device_hotplug_lock, otherwise the memblock iteration below can + * race with memblock_double_array() and read freed memory. + */ + device_hotplug_lock_assert_held(); + max_nr_ranges =3D arch_get_system_nr_ranges(); if (!max_nr_ranges) return -ENOMEM; --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout01.his.huawei.com (canpmsgout01.his.huawei.com [113.46.200.216]) (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 257FF4B5CBD; Fri, 18 Sep 2026 10:04:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.216 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725881; cv=none; b=GzjwcHQ2sngZQg1BXzbJdJkYMZbmw/RPNA8on3LJ+oucGdghw+jMwGeIot7+IEDqLsI4gYlED8X069L+9sm3d+9WiPK7hhTNpuS7c4+f2shOJKcZZV+jMqfKj+D4pz3cgXshZNBqHDaksc1SZDaX2ujQQ5Rt6m8vzU88PBP/6wg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725881; c=relaxed/simple; bh=sNTZcqukE9loz6LzRqMiF7FOoMVTuNbCNm71ywyhIco=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XQ2usIDuQwPTYlnNcr0C9Dlxm8QyIv8lp3Hleo6ymyOgcJr0xEOTjlDOKkXEOjUYCczTuoSVwOym2lTbJ4PzW4oN0tMwVYZhnvUKLxjsFnFOwq/VoYGOJnCkw4QC5bnN9DSFeu45n0e9VxmdBB6MjPLtmiVZ31VkXVvvkLRdHOc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=0wcIVz/N; arc=none smtp.client-ip=113.46.200.216 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="0wcIVz/N" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=PnxxiylnyHipl/A8SV3AAIGnQGPzs5xWXDe5kWQdoK8=; b=0wcIVz/NKsz3gLpf8y14MTzeo91L7xA17SSIcnva1ijvtAHPfvMAiGbgY84jOLMdOSDODlTqp FE9GxBfXkkJdMRm4pBCXfVlwvRpVN7aHNLNfNcYCiXQH1Z7phnSffRJ7Nq8BfOP/+sVIY+6zxF9 0VjE7YG+aZCdI1BcLH46bJU= Received: from mail.maildlp.com (unknown [172.19.163.104]) by canpmsgout01.his.huawei.com (SkyGuard) with ESMTPS id 4hmSbd0lhFz1T4JM; Fri, 18 Sep 2026 17:52:45 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 70DAC4057F; Fri, 18 Sep 2026 18:04:29 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:27 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 14/17] elf: Introduce elf64_phdr_size() helper Date: Fri, 18 Sep 2026 18:04:39 +0800 Message-ID: <20260918100442.3841135-15-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" Add a common helper to compute the total size of an ELF64 header (Ehdr + program headers) from the number of program headers. Replace open-coded calculations in powerpc, x86, vmcore, and crash_core. On ppc64, struct elfhdr maps to elf64_hdr, so the powerpc change is a pure cleanup. No functional change intended. Cc: Madhavan Srinivasan Cc: Michael Ellerman Cc: Nicholas Piggin Cc: "Christophe Leroy (CS GROUP)" Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Andrew Morton Cc: Baoquan He Cc: Mike Rapoport Cc: Pasha Tatashin Cc: Pratyush Yadav Cc: Dave Young Cc: Kees Cook Cc: Sourabh Jain Signed-off-by: Jinjie Ruan --- arch/powerpc/kexec/crash.c | 2 +- arch/powerpc/platforms/powernv/opal-core.c | 3 +-- arch/x86/kernel/crash.c | 3 +-- fs/proc/vmcore.c | 6 ++---- include/linux/elf.h | 4 ++++ kernel/crash_core.c | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/arch/powerpc/kexec/crash.c b/arch/powerpc/kexec/crash.c index 8ede33740fc0..0cdc64da35b8 100644 --- a/arch/powerpc/kexec/crash.c +++ b/arch/powerpc/kexec/crash.c @@ -478,7 +478,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void) if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) phdr_cnt +=3D CONFIG_CRASH_MAX_MEMORY_RANGES; =20 - return sizeof(struct elfhdr) + (phdr_cnt * sizeof(Elf64_Phdr)); + return elf64_phdr_size(phdr_cnt); } =20 /** diff --git a/arch/powerpc/platforms/powernv/opal-core.c b/arch/powerpc/plat= forms/powernv/opal-core.c index 32662d30d70f..fc0aad61504b 100644 --- a/arch/powerpc/platforms/powernv/opal-core.c +++ b/arch/powerpc/platforms/powernv/opal-core.c @@ -309,8 +309,7 @@ static int __init create_opalcore(void) char *bufp; =20 /* Get size of header & CPU notes for OPAL core */ - hdr_size =3D (sizeof(Elf64_Ehdr) + - ((oc_conf->ptload_cnt + 1) * sizeof(Elf64_Phdr))); + hdr_size =3D elf64_phdr_size(oc_conf->ptload_cnt + 1); cpu_notes_size =3D ((oc_conf->num_cpus * (CRASH_CORE_NOTE_HEAD_BYTES + CRASH_CORE_NOTE_NAME_BYTES + CRASH_CORE_NOTE_DESC_BYTES)) + diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index 7fa139725514..6711f8bf1db3 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -374,8 +374,7 @@ int crash_load_segments(struct kimage *image) pnum +=3D 2 + CONFIG_NR_CPUS_DEFAULT; =20 if (pnum < (unsigned long)PN_XNUM) { - kbuf.memsz =3D pnum * sizeof(Elf64_Phdr); - kbuf.memsz +=3D sizeof(Elf64_Ehdr); + kbuf.memsz =3D elf64_phdr_size(pnum); =20 image->elfcorehdr_index =3D image->nr_segments; =20 diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c index 44d15436439f..ff324969d798 100644 --- a/fs/proc/vmcore.c +++ b/fs/proc/vmcore.c @@ -1238,8 +1238,7 @@ static int __init parse_crash_elf64_headers(void) } =20 /* Read in all elf headers. */ - elfcorebuf_sz_orig =3D sizeof(Elf64_Ehdr) + - ehdr.e_phnum * sizeof(Elf64_Phdr); + elfcorebuf_sz_orig =3D elf64_phdr_size(ehdr.e_phnum); elfcorebuf_sz =3D elfcorebuf_sz_orig; elfcorebuf =3D (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(elfcorebuf_sz_orig)); @@ -1605,8 +1604,7 @@ static int vmcore_add_device_ram_elf64(struct list_he= ad *list, size_t count) } =20 /* elfcorebuf_sz must always cover full pages. */ - new_size =3D sizeof(Elf64_Ehdr) + - (ehdr->e_phnum + count) * sizeof(Elf64_Phdr); + new_size =3D elf64_phdr_size(ehdr->e_phnum + count); new_size =3D roundup(new_size, PAGE_SIZE); =20 /* diff --git a/include/linux/elf.h b/include/linux/elf.h index 5c402788da19..400f58a13d92 100644 --- a/include/linux/elf.h +++ b/include/linux/elf.h @@ -109,4 +109,8 @@ static inline int arch_elf_adjust_prot(int prot, } #endif =20 +static inline unsigned long elf64_phdr_size(unsigned long phdr_cnt) +{ + return phdr_cnt * sizeof(Elf64_Phdr) + sizeof(Elf64_Ehdr); +} #endif /* _LINUX_ELF_H */ diff --git a/kernel/crash_core.c b/kernel/crash_core.c index 852a5f51d472..8e30ae107a7c 100644 --- a/kernel/crash_core.c +++ b/kernel/crash_core.c @@ -193,7 +193,7 @@ int crash_prepare_elf64_headers(struct crash_mem *mem, = int need_kernel_map, */ =20 nr_phdr++; - elf_sz =3D sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); + elf_sz =3D elf64_phdr_size(nr_phdr); elf_sz =3D ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); =20 buf =3D vzalloc(elf_sz); --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout02.his.huawei.com (canpmsgout02.his.huawei.com [113.46.200.217]) (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 D826D48F03A; Fri, 18 Sep 2026 10:04:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.217 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725883; cv=none; b=MoyQ4YdPmjmaO9ma+VLwpdEhVLfJCgEDtUGGg24F/stRaJ+s04eeYGbb6aws0uALIh3j/XcKB2kDaLQtsS2+jAqVByR+e3FtCmNb/IAIOAwD2uU7yA4JIYoWEhq2aYRjyxkqTXFem9+vj8EPko2yWYWEHOrYAeIn6ykWeYDXUR8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725883; c=relaxed/simple; bh=xwhK9qSWMeXCgM2EnrFRfLyMTOkniMvySOOy8G0C6aw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JiNxGtk5vA1CFjN70p+TCa7sxWvFm4QrH3o/65ddy4OZVu8nM7d/JG+opousjqcY6LvWTS8YPxwSm+EN6JwsEzT7lEOsNrQFbfH8D91MFSOi2APw6HYF8rikVFp4rwqrL5WWvMH9fKdthomfTwrGrlfNHOfwZp+sJIczgaAAAAk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=C2eZF9Mr; arc=none smtp.client-ip=113.46.200.217 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="C2eZF9Mr" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=xY6MkLtAML0NhdwFhEh6HEFrtMWS+BaF8Jv6SYbylCI=; b=C2eZF9MriTz09Fyd6sSJRN6eb3mHxLBMVzS2eueaGbQwfruJTStfHmzacHg2EphpPC7IEogVN vKy7HEOXjk8HJKqpXYqJT2TzYh11n9eTIQIYH9HAE08ba7TuLwPVwJITDWlXJ6ajgI17Ju7r/zg o/XgABw9N1BRe9NbOBaj9iM= Received: from mail.maildlp.com (unknown [172.19.162.140]) by canpmsgout02.his.huawei.com (SkyGuard) with ESMTPS id 4hmScC5GyQzcb4j; Fri, 18 Sep 2026 17:53:15 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id CF0FE20333; Fri, 18 Sep 2026 18:04:30 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:29 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 15/17] crash: Introduce crash_extra_elfcorehdr_size() helper Date: Fri, 18 Sep 2026 18:04:40 +0800 Message-ID: <20260918100442.3841135-16-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" Extract the elfcorehdr extra space calculation from powerpc into a generic helper crash_extra_elfcorehdr_size() for use by other architectures like arm64. Strengthen the original powerpc check: instead of only checking the loose CONFIG_CRASH_MAX_MEMORY_RANGES, the new helper enforces a strict compile-time BUILD_BUG_ON() to guarantee that the absolute maximum theoretical number of ELF Program Headers will never exceed the ELF physical limit of PN_XNUM. This ensures absolute safety across all architectures with zero runtime overhead. The helper also provides a zero-size stub when crash memory hotplug is disabled. Cc: Madhavan Srinivasan Cc: Michael Ellerman Cc: Nicholas Piggin Cc: "Christophe Leroy (CS GROUP)" Cc: Andrew Morton Cc: Baoquan He Cc: Mike Rapoport Cc: Pasha Tatashin Cc: Pratyush Yadav Cc: Dave Young Cc: Sourabh Jain Signed-off-by: Jinjie Ruan --- arch/powerpc/kexec/file_load_64.c | 15 +-------------- include/linux/crash_core.h | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_lo= ad_64.c index bd80c5fb1b1f..d3512c85ea83 100644 --- a/arch/powerpc/kexec/file_load_64.c +++ b/arch/powerpc/kexec/file_load_64.c @@ -374,19 +374,6 @@ static int load_backup_segment(struct kimage *image, s= truct kexec_buf *kbuf) return 0; } =20 -static unsigned int kdump_extra_elfcorehdr_size(struct crash_mem *cmem) -{ -#if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG) - if (CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM) - pr_warn("Number of Phdrs %u exceeds max\n", CONFIG_CRASH_MAX_MEMORY_RANG= ES); - else if (cmem->nr_ranges >=3D CONFIG_CRASH_MAX_MEMORY_RANGES) - pr_warn("Configured crash mem ranges may not be enough\n"); - else - return (CONFIG_CRASH_MAX_MEMORY_RANGES - cmem->nr_ranges) * sizeof(Elf64= _Phdr); -#endif - return 0; -} - /** * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfc= orehdr * segment needed to load kdump kernel. @@ -424,7 +411,7 @@ static int load_elfcorehdr_segment(struct kimage *image= , struct kexec_buf *kbuf) * Account for extra space required to accommodate additional memory * ranges in elfcorehdr due to memory hotplug events. */ - kbuf->memsz =3D headers_sz + kdump_extra_elfcorehdr_size(cmem); + kbuf->memsz =3D headers_sz + crash_extra_elfcorehdr_size(cmem->nr_ranges); kbuf->top_down =3D false; =20 ret =3D kexec_add_buffer(kbuf); diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h index 1296a9b29974..aee2449f343b 100644 --- a/include/linux/crash_core.h +++ b/include/linux/crash_core.h @@ -107,4 +107,24 @@ ssize_t dm_crypt_keys_read(char *buf, size_t count, u6= 4 *ppos); static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0= ; } #endif =20 +#if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG) +static inline unsigned int crash_extra_elfcorehdr_size(unsigned int nr_mem= _ranges) +{ + BUILD_BUG_ON((2 + CONFIG_NR_CPUS + CONFIG_CRASH_MAX_MEMORY_RANGES) >=3D + (unsigned int)PN_XNUM); + + if (nr_mem_ranges >=3D CONFIG_CRASH_MAX_MEMORY_RANGES) { + pr_warn_once("Configured crash mem ranges may not be enough\n"); + return 0; + } + + return (CONFIG_CRASH_MAX_MEMORY_RANGES - nr_mem_ranges) * sizeof(Elf64_Ph= dr); +} +#else +static inline unsigned int crash_extra_elfcorehdr_size(unsigned int nr_mem= _ranges) +{ + return 0; +} +#endif + #endif /* LINUX_CRASH_CORE_H */ --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout11.his.huawei.com (canpmsgout11.his.huawei.com [113.46.200.226]) (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 6ECF547987C; Fri, 18 Sep 2026 10:04:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.226 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725879; cv=none; b=RoYvhTCMiwie5xYUC7f3pt4zrDXgwURlqNn8p5jrMEzV7iTG611rbSdgElDWQcG01+cd5ZoluwRtKENxwR7vYTSC0kUMg4mg52mQtre33n2iTnvI63w8CwN5ZHy7ayTfFDT9N8omcQeBDrJVRC5VCaeuJUDRiqse9iM1DaU/mHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725879; c=relaxed/simple; bh=kp+6I708DzV79AoDBZFUXSGVAlkZ158GWQXxLRdCt18=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=C0vnZNbpUfp/UhHb8uWcUA3u62Z52t7phmk21fYImRCy9jUC3Zh1C7+G4uy8gyVPX3r2+PxsYveNEVUVMOYdzu38dh0Xn26q+xfWC/LhtZX+7EdPsJu4ZTJh/rqAtrq77MKVGrFL9OJASF+ejOreF1qK3hCcwbWkEONBdfcDr20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=PHmoGMGZ; arc=none smtp.client-ip=113.46.200.226 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="PHmoGMGZ" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=9eBN1xy3LsTspM2cXc9iihQZgxEMtvQyI2J8OiqZLKg=; b=PHmoGMGZCpSK83cFfL1l+GZjf/pY9I+6IoZqg3WJ4VLG4xW6NHlRLdZlpXZs7q+S1hU6FgzSG rPNsboboV+FxPnNuPRJvY6Ww7D51z6C62ISbmu/FflpvRh5Xiy1KVbEeTFJW1U4tML5IiSjG8eN PcED3zgeHCZOJt4Rn7qsZuQ= Received: from mail.maildlp.com (unknown [172.19.163.163]) by canpmsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4hmScS4nqFzKm9b; Fri, 18 Sep 2026 17:53:28 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 3E6074048B; Fri, 18 Sep 2026 18:04:32 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:30 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 16/17] arm64: kexec_file: Simplify load_other_segments() Date: Fri, 18 Sep 2026 18:04:41 +0800 Message-ID: <20260918100442.3841135-17-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" Use `kbuf` fields directly in crash_prepare_headers() to eliminate the local variables "headers" and "headers_sz".. Advance the assignment to image->elf_headers before calling kexec_add_buffer(). If kexec_add_buffer() fails, the explicit vfree() in the error path can be removed, as the global infrastructure in arch_kimage_file_post_load_cleanup() will handle the cleanup. Cc: Catalin Marinas Cc: Will Deacon Cc: Baoquan He Cc: Breno Leitao Signed-off-by: Jinjie Ruan --- arch/arm64/kernel/machine_kexec_file.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/mac= hine_kexec_file.c index 2f750e5f4fcc..3d907f8ee594 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -89,36 +89,30 @@ int load_other_segments(struct kimage *image, kbuf.buf_min =3D kernel_load_addr + kernel_size; =20 #ifdef CONFIG_CRASH_DUMP - /* load elf core header */ - void *headers; - unsigned long headers_sz; if (image->type =3D=3D KEXEC_TYPE_CRASH) { - ret =3D crash_prepare_headers(true, &headers, &headers_sz, NULL); + ret =3D crash_prepare_headers(true, &kbuf.buffer, &kbuf.bufsz, NULL); if (ret) { pr_err("Preparing elf core header failed\n"); goto out_err; } =20 - kbuf.buffer =3D headers; - kbuf.bufsz =3D headers_sz; + if (unlikely(image->elf_headers)) + vfree(image->elf_headers); + + image->elf_headers =3D kbuf.buffer; + image->elf_headers_sz =3D kbuf.bufsz; + kbuf.mem =3D KEXEC_BUF_MEM_UNKNOWN; - kbuf.memsz =3D headers_sz; + kbuf.memsz =3D kbuf.bufsz; kbuf.buf_align =3D SZ_64K; /* largest supported page size */ kbuf.buf_max =3D ULONG_MAX; kbuf.top_down =3D true; =20 ret =3D kexec_add_buffer(&kbuf); - if (ret) { - vfree(headers); + if (ret) goto out_err; - } - - if (unlikely(image->elf_headers)) - vfree(image->elf_headers); =20 - image->elf_headers =3D headers; image->elf_load_addr =3D kbuf.mem; - image->elf_headers_sz =3D headers_sz; =20 kexec_dprintk("Loaded elf core header at 0x%lx bufsz=3D0x%lx memsz=3D0x%= lx\n", image->elf_load_addr, kbuf.bufsz, kbuf.memsz); --=20 2.34.1 From nobody Fri Sep 25 00:41:25 2026 Received: from canpmsgout12.his.huawei.com (canpmsgout12.his.huawei.com [113.46.200.227]) (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 77E874B44BA; Fri, 18 Sep 2026 10:04:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.227 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725880; cv=none; b=PWt2hdqEYXqLfir5yJz6YS1+X7KtTyDge0JVMQGxyxxDZJ8FYv3dKqmg0hN6zNJNcwwGjXk8g9iC0kiqrxgQfwWDBR8CNivdJAFYDF+D8sYW2dZPxToUmuijp0PnRJ5duWwWpTS67MUFtd+GXLn11F4KDPwKsPsSqm8VAnDX9xo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789725880; c=relaxed/simple; bh=roxIGFoZo07iu+KOlAtHr+oPF0CZ7ZTn0Mkd4VGFUHw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AOQT6LtgY9B+bOKiC95U37trLtN2+IdcMPxXNCZZr8lRkLv5H6lhZoc3R3RyvJWPa82j79SyXVXM8K4bYguYjjIjTxKDCt4+m2m0xNd+YIgrz3UaA3WhzWJ62Vfc7FzJIDJIN5mkoeYhJWIwrXU/kd1/5MsfUFs0GTdBqCILjxU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=dTuhtcwS; arc=none smtp.client-ip=113.46.200.227 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="dTuhtcwS" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=GBoAe3J5L5xeTOESkZrKV2F9E4gAR8UGD40P6q3mXxY=; b=dTuhtcwSh/o5shWC0RoXx2lcvY/zYvyJR2Kz9hjSNHzX9HE953qpbG9U7PH1h/b0Pzl5cdz9y u/4FTCFoKvtgjAfRGJYMjTPsm3gD2Yas+qmy/I2kYMKOsOPq1PF47iCv/rkfr8ZNIN6qj6UZOvJ h4w+ZCHTuPAhooLAh+ate2o= Received: from mail.maildlp.com (unknown [172.19.163.15]) by canpmsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4hmScT5pwPznTVk; Fri, 18 Sep 2026 17:53:29 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id ACEB140586; Fri, 18 Sep 2026 18:04:33 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 18 Sep 2026 18:04:32 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 17/17] arm64: crash: Add crash hotplug support Date: Fri, 18 Sep 2026 18:04:42 +0800 Message-ID: <20260918100442.3841135-18-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260918100442.3841135-1-ruanjinjie@huawei.com> References: <20260918100442.3841135-1-ruanjinjie@huawei.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-ClientProxiedBy: kwepems100001.china.huawei.com (7.221.188.238) To kwepemk200008.china.huawei.com (7.202.194.74) Content-Type: text/plain; charset="utf-8" When CPU or memory hotplug events occur, the elfcorehdr in the kdump image becomes stale, potentially leading to incomplete crash dumps. Currently, userspace udev rules reload the entire kdump image upon such events, which is inefficient and leaves kdump inactive for a long time. Commit 247262756121 ("crash: add generic infrastructure for crash hotplug support") introduced a kernel mechanism to update only the elfcorehdr. This patch enables that support for arm64. On arm64, only memory hotplug events require elfcorehdr updates: - Physical CPU hotplug is not supported. - For ACPI based vCPU hotplug [1], the elfcorehdr is built using for_each_possible_cpu(), so no update is needed. The patch: - Adds CONFIG_ARCH_SUPPORTS_CRASH_HOTPLUG (default y). - Implements following arch functions to handle memory hotplug: 1. arch_crash_hotplug_support() 2. arch_crash_get_elfcorehdr_size() 3. arch_crash_handle_hotplug_event() - Moves arch_get_system_nr_ranges() and arch_crash_populate_cmem() from machine_kexec_file.c to crash.c for crash hotplug reuse. Follows the approach of x86 commit ea53ad9cf73b ("x86/crash: add x86 crash hotplug support") and powerpc commit b741092d5976 ("powerpc/crash: add crash CPU hotplug support"). Cc: Catalin Marinas Cc: Will Deacon Cc: Baoquan He Cc: "Mike Rapoport (Microsoft)" Cc: Andrew Morton Cc: Breno Leitao Cc: Sourabh Jain Cc: Mark Rutland Cc: Ard Biesheuvel Cc: Thomas Huth [1]: https://lore.kernel.org/all/20240529133446.28446-1-Jonathan.Cameron@hu= awei.com/ Signed-off-by: Jinjie Ruan --- arch/arm64/Kconfig | 3 + arch/arm64/include/asm/kexec.h | 11 ++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/crash.c | 171 +++++++++++++++++++++++++ arch/arm64/kernel/machine_kexec_file.c | 42 ++---- 5 files changed, 196 insertions(+), 33 deletions(-) create mode 100644 arch/arm64/kernel/crash.c diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index b5a51b0ef944..96de2660b1cb 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -1720,6 +1720,9 @@ config ARCH_DEFAULT_CRASH_DUMP config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION def_bool CRASH_RESERVE =20 +config ARCH_SUPPORTS_CRASH_HOTPLUG + def_bool y + config TRANS_TABLE def_bool y depends on HIBERNATION || KEXEC_CORE diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index 892e5bebda95..f165c094b32e 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -130,6 +130,17 @@ extern int load_other_segments(struct kimage *image, char *cmdline); #endif =20 +#ifdef CONFIG_CRASH_HOTPLUG +void arch_crash_handle_hotplug_event(struct kimage *image, void *arg); +#define arch_crash_handle_hotplug_event arch_crash_handle_hotplug_event + +int arch_crash_hotplug_support(struct kimage *image, unsigned long kexec_f= lags); +#define arch_crash_hotplug_support arch_crash_hotplug_support + +unsigned int arch_crash_get_elfcorehdr_size(void); +#define crash_get_elfcorehdr_size arch_crash_get_elfcorehdr_size +#endif + #endif /* __ASSEMBLER__ */ =20 #endif diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index d2690c3ec528..9bbac452994c 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -64,7 +64,7 @@ obj-$(CONFIG_KEXEC_CORE) +=3D machine_kexec.o relocate_k= ernel.o \ obj-$(CONFIG_KEXEC_FILE) +=3D machine_kexec_file.o kexec_image.o obj-$(CONFIG_ARM64_RELOC_TEST) +=3D arm64-reloc-test.o arm64-reloc-test-y :=3D reloc_test_core.o reloc_test_syms.o -obj-$(CONFIG_CRASH_DUMP) +=3D crash_dump.o +obj-$(CONFIG_CRASH_DUMP) +=3D crash_dump.o crash.o obj-$(CONFIG_VMCORE_INFO) +=3D vmcore_info.o obj-$(CONFIG_ARM_SDE_INTERFACE) +=3D sdei.o obj-$(CONFIG_ARM64_PTR_AUTH) +=3D pointer_auth.o diff --git a/arch/arm64/kernel/crash.c b/arch/arm64/kernel/crash.c new file mode 100644 index 000000000000..08dc9b952498 --- /dev/null +++ b/arch/arm64/kernel/crash.c @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Architecture specific functions for kexec based crash dumps. + */ + +#define pr_fmt(fmt) "crash hp: " fmt + +#include +#include +#include +#include +#include +#include + +#include + +#if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_HOTPLUG) +unsigned int arch_get_system_nr_ranges(void) +{ + unsigned int nr_ranges =3D 2 + crashk_cma_cnt; /* for exclusion of crashk= ernel region */ + phys_addr_t start, end; + u64 i; + + for_each_mem_range(i, &start, &end) + nr_ranges++; + + /* Memory hot-unplug may cause range split. So add an extra slot here. */ + if (IS_ENABLED(CONFIG_CRASH_HOTPLUG)) + nr_ranges++; + + return nr_ranges; +} + +int arch_crash_populate_cmem(struct crash_mem *cmem) +{ + phys_addr_t start, end; + u64 i; + + for_each_mem_range(i, &start, &end) { + cmem->ranges[cmem->nr_ranges].start =3D start; + cmem->ranges[cmem->nr_ranges].end =3D end - 1; + cmem->nr_ranges++; + } + + return 0; +} +#endif + +#ifdef CONFIG_CRASH_HOTPLUG +int arch_crash_hotplug_support(struct kimage *image, unsigned long kexec_f= lags) +{ +#ifdef CONFIG_KEXEC_FILE + if (image->file_mode) + return 1; +#endif + /* + * For kexec_load syscall, crash hotplug support requires + * KEXEC_CRASH_HOTPLUG_SUPPORT flag to be passed by userspace. + */ + return kexec_flags & KEXEC_CRASH_HOTPLUG_SUPPORT; +} + +unsigned int arch_crash_get_elfcorehdr_size(void) +{ + unsigned long phdr_cnt; + + /* A program header for possible CPUs, vmcoreinfo and kernel_map */ + phdr_cnt =3D 2 + num_possible_cpus(); + if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) + phdr_cnt +=3D CONFIG_CRASH_MAX_MEMORY_RANGES; + + return elf64_phdr_size(phdr_cnt); +} + +/** + * update_crash_elfcorehdr() - Recreate the elfcorehdr and replace it with= old + * elfcorehdr in the kexec segment array. + * @image: the active struct kimage + * @mn: struct memory_notify data handler + */ +static void update_crash_elfcorehdr(struct kimage *image, struct memory_no= tify *mn) +{ + void *elfbuf =3D NULL, *old_elfcorehdr; + unsigned long mem, memsz, elfsz =3D 0; + struct crash_mem *cmem =3D NULL; + u64 start, end; + int ret; + + ret =3D crash_get_memory_ranges_nolock(&cmem); + if (ret) { + pr_err("Failed to get crash memory ranges.\n"); + goto out; + } + + /* + * The hot unplugged memory is part of crash memory ranges, + * remove it here. + */ + if (image->hp_action =3D=3D KEXEC_CRASH_HP_REMOVE_MEMORY) { + start =3D PFN_PHYS(mn->start_pfn); + end =3D start + PFN_PHYS(mn->nr_pages) - 1; + + ret =3D crash_exclude_mem_range(cmem, start, end); + if (ret) { + pr_err("Failed to remove hot-unplugged memory from crash memory ranges.= \n"); + goto out; + } + } + + /* + * Create the new elfcorehdr reflecting the changes to CPU and/or + * memory resources. + */ + ret =3D crash_prepare_elf64_headers(cmem, true, &elfbuf, &elfsz); + if (ret) { + pr_err("Failed to create new elfcorehdr"); + goto out; + } + + /* + * Obtain address and size of the elfcorehdr segment, and + * check it against the new elfcorehdr buffer. + */ + mem =3D image->segment[image->elfcorehdr_index].mem; + memsz =3D image->segment[image->elfcorehdr_index].memsz; + if (elfsz > memsz) { + pr_err("update elfcorehdr elfsz %lu > memsz %lu", + elfsz, memsz); + goto out; + } + + /* Copy new elfcorehdr over the old elfcorehdr at destination. */ + old_elfcorehdr =3D phys_to_virt(mem); + + /* + * Temporarily invalidate the crash image while the + * elfcorehdr is updated. + */ + xchg(&kexec_crash_image, NULL); + memcpy(old_elfcorehdr, elfbuf, elfsz); + dcache_clean_inval_poc((unsigned long)old_elfcorehdr, + (unsigned long)(old_elfcorehdr + elfsz)); + xchg(&kexec_crash_image, image); + pr_debug("updated elfcorehdr\n"); + +out: + kvfree(cmem); + vfree(elfbuf); +} + +/** + * arch_crash_handle_hotplug_event() - Handle hotplug elfcorehdr changes + * @image: a pointer to kexec_crash_image + * @arg: struct memory_notify handler for memory hotplug case and + * NULL for CPU hotplug case. + * + * Update the kdump image based on the type of hotplug event: + * - CPU add and remove: No action is needed. + * - Memory add/remove: Update the elfcorehdr to reflect the current memor= y layout. + * + * Prepare the new elfcorehdr and replace the existing elfcorehdr. + */ +void arch_crash_handle_hotplug_event(struct kimage *image, void *arg) +{ + if (image->hp_action =3D=3D KEXEC_CRASH_HP_ADD_CPU || + image->hp_action =3D=3D KEXEC_CRASH_HP_REMOVE_CPU) + return; + + update_crash_elfcorehdr(image, (struct memory_notify *)arg); +} +#endif /* CONFIG_CRASH_HOTPLUG */ diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/mac= hine_kexec_file.c index 3d907f8ee594..c0d0442fad9b 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -39,34 +38,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *im= age) return kexec_image_post_load_cleanup_default(image); } =20 -#ifdef CONFIG_CRASH_DUMP -unsigned int arch_get_system_nr_ranges(void) -{ - unsigned int nr_ranges =3D 2 + crashk_cma_cnt; /* for exclusion of crashk= ernel region */ - phys_addr_t start, end; - u64 i; - - for_each_mem_range(i, &start, &end) - nr_ranges++; - - return nr_ranges; -} - -int arch_crash_populate_cmem(struct crash_mem *cmem) -{ - phys_addr_t start, end; - u64 i; - - for_each_mem_range(i, &start, &end) { - cmem->ranges[cmem->nr_ranges].start =3D start; - cmem->ranges[cmem->nr_ranges].end =3D end - 1; - cmem->nr_ranges++; - } - - return 0; -} -#endif - /* * Tries to add the initrd and DTB to the image. If it is not possible to = find * valid locations, this function will undo changes to the image and retur= n non @@ -89,8 +60,9 @@ int load_other_segments(struct kimage *image, kbuf.buf_min =3D kernel_load_addr + kernel_size; =20 #ifdef CONFIG_CRASH_DUMP + unsigned long nr_ranges =3D 0; if (image->type =3D=3D KEXEC_TYPE_CRASH) { - ret =3D crash_prepare_headers(true, &kbuf.buffer, &kbuf.bufsz, NULL); + ret =3D crash_prepare_headers(true, &kbuf.buffer, &kbuf.bufsz, &nr_range= s); if (ret) { pr_err("Preparing elf core header failed\n"); goto out_err; @@ -100,10 +72,13 @@ int load_other_segments(struct kimage *image, vfree(image->elf_headers); =20 image->elf_headers =3D kbuf.buffer; - image->elf_headers_sz =3D kbuf.bufsz; =20 kbuf.mem =3D KEXEC_BUF_MEM_UNKNOWN; - kbuf.memsz =3D kbuf.bufsz; + kbuf.memsz =3D kbuf.bufsz + crash_extra_elfcorehdr_size(nr_ranges); + image->elf_headers_sz =3D kbuf.memsz; +#ifdef CONFIG_CRASH_HOTPLUG + image->elfcorehdr_index =3D image->nr_segments; +#endif kbuf.buf_align =3D SZ_64K; /* largest supported page size */ kbuf.buf_max =3D ULONG_MAX; kbuf.top_down =3D true; @@ -177,6 +152,9 @@ int load_other_segments(struct kimage *image, return 0; =20 out_err: +#ifdef CONFIG_CRASH_HOTPLUG + image->elfcorehdr_index =3D -1; +#endif while (image->nr_segments > orig_segments) { kexec_free_segment_cma(image, image->nr_segments - 1); image->nr_segments--; --=20 2.34.1