From mboxrd@z Thu Jan 1 00:00:00 1970 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 2AA9E442FB9 for ; Wed, 29 Jul 2026 09:33:24 +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=1785317613; cv=none; b=aNgktaNCsLd3CgRt0GfM7dxYNiAfvtmveJ6ep5sluGjXijAEHIEn11/Tvm+tH4QmH5dEamzjjEKR+ES8gb4f5Eiu6+y+SeB/wMpZoxD8Sw3vcvkAld5CjAakB3WgxtAamJQ+XQMWV+0CkpdDTD20grVrkKz1VjQNYUP+otnbeE0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785317613; c=relaxed/simple; bh=qXCLGfDbP+v41t4/ufsLweHEcjqH+Li7xw+Gg03p5+w=; h=Subject:To:CC:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=CjP63X1iRoxtDFdYGVk35hskrl4wuWUxJzgHftTIcBcPsUCqyMtVogtxeq7dfOtyKBCxn/IqY05Y0k0fLZL9T6j7xpQp7Efdp4jQO9izPIFejwqfGxyHQkoC52SycB8/tePpQy57v+NFD2dVCAOBFjBwwqP/CPOTXFGklNJznUI= 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=0cbv10gE; 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="0cbv10gE" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=l28RUUIO0sTYXTUgslp8RhU5Xau1YXhJe+DpH+cC4Js=; b=0cbv10gEwX5uu9GIcx/ZyCSFDsAMaWqXcnMDdrzJ6IyLWtJojWvfk8o1RVjTRNKzAwe3xAGcb HchCKLYBh9BmJo2N85ZSseW5l5z3Y/n/vzf3322ZrFh4b+N/3rS65VcHVJTLTWLty1vMUH84yjK 1I7foOTOqYlmbZv6hROzRnE= Received: from mail.maildlp.com (unknown [172.19.163.127]) by canpmsgout07.his.huawei.com (SkyGuard) with ESMTPS id 4h96Mr4BNtzLlXX; Wed, 29 Jul 2026 17:23:52 +0800 (CST) Received: from dggemv712-chm.china.huawei.com (unknown [10.1.198.32]) by mail.maildlp.com (Postfix) with ESMTPS id 9365B40572; Wed, 29 Jul 2026 17:33:19 +0800 (CST) Received: from kwepemq500010.china.huawei.com (7.202.194.235) by dggemv712-chm.china.huawei.com (10.1.198.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 29 Jul 2026 17:33:19 +0800 Received: from [10.173.124.160] (10.173.124.160) by kwepemq500010.china.huawei.com (7.202.194.235) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 29 Jul 2026 17:33:18 +0800 Subject: Re: [PATCH] kexec: keep the next kernel off hardware-poisoned pages To: Breno Leitao CC: , , , , , , Kiryl Shutsemau , Andrew Morton , David Hildenbrand , "Lorenzo Stoakes" , "Liam R. Howlett" , "Vlastimil Babka" , Mike Rapoport , "Suren Baghdasaryan" , Michal Hocko , Baoquan He , Pasha Tatashin , "Pratyush Yadav" , Naoya Horiguchi References: <20260728-kexec_posioned-v1-1-160c81d180fe@debian.org> From: Miaohe Lin Message-ID: <16c18549-c9a1-ba55-91b8-52d9368ebbc0@huawei.com> Date: Wed, 29 Jul 2026 17:33:17 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <20260728-kexec_posioned-v1-1-160c81d180fe@debian.org> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-ClientProxiedBy: kwepems500002.china.huawei.com (7.221.188.17) To kwepemq500010.china.huawei.com (7.202.194.235) On 2026/7/29 0:22, Breno Leitao wrote: > Memory failures (such as unrecoverable ECCs errors) are getting more and > more common. The kernel knows how to handle it while running, marking it > as poisoned (and SIGBUS user tasks). > > Poisoned memory is removed from the buddy allocator, but, not from > other places. A current problem is that kexec will load new kernel > on top of a bad/poisoned memory, which is undesirable. > > If the next kernel's image, initrd or purgatory lands on poisoned frame, > the relocation copy writes to the bad memory and the machine checks > during the kexec. > > Skip hardware-poisoned frames when placing segments: check them in the > kexec_file hole finder so it lays the next kernel down on good memory, > and reject a poisoned destination in sanity_check_segment_list() for > the kexec_load path, which cannot relocate. > > Suggested-by: Kiryl Shutsemau > Signed-off-by: Breno Leitao > --- > include/linux/mm.h | 6 ++++++ > kernel/kexec_core.c | 10 ++++++++++ > kernel/kexec_file.c | 14 ++++++++++++++ > mm/memory-failure.c | 18 ++++++++++++++++++ > 4 files changed, 48 insertions(+) > > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 7fabe6c66b4b7..a89108bcc3f90 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -5192,6 +5192,7 @@ extern const struct attribute_group memory_failure_attr_group; > extern void memory_failure_queue(unsigned long pfn, int flags); > void num_poisoned_pages_inc(unsigned long pfn); > void num_poisoned_pages_sub(unsigned long pfn, long i); > +bool range_contains_hwpoison(phys_addr_t start, unsigned long size); > #else > static inline void memory_failure_queue(unsigned long pfn, int flags) > { > @@ -5204,6 +5205,11 @@ static inline void num_poisoned_pages_inc(unsigned long pfn) > static inline void num_poisoned_pages_sub(unsigned long pfn, long i) > { > } > + > +static inline bool range_contains_hwpoison(phys_addr_t start, unsigned long size) > +{ > + return false; > +} > #endif > > #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_MEMORY_HOTPLUG) > diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c > index dc770b9a6d053..18793f925835f 100644 > --- a/kernel/kexec_core.c > +++ b/kernel/kexec_core.c > @@ -212,6 +212,16 @@ int sanity_check_segment_list(struct kimage *image) > } > #endif > > + /* > + * Reject destinations that land on hardware-poisoned memory: the > + * relocation copy would machine-check on the bad frame. > + */ > + for (i = 0; i < nr_segments; i++) { > + if (range_contains_hwpoison(image->segment[i].mem, > + image->segment[i].memsz)) > + return -EADDRNOTAVAIL; > + } > + > /* > * The destination addresses are searched from system RAM rather than > * being allocated from the buddy allocator, so they are not guaranteed > diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c > index 59fb9d71e9d86..cd77350f12f85 100644 > --- a/kernel/kexec_file.c > +++ b/kernel/kexec_file.c > @@ -504,6 +504,13 @@ static int locate_mem_hole_top_down(unsigned long start, unsigned long end, > continue; > } > > + /* Avoid placing the next kernel on hardware-poisoned memory */ > + if (range_contains_hwpoison(temp_start, > + temp_end - temp_start + 1)) { > + temp_start = temp_start - PAGE_SIZE; > + continue; > + } > + > /* We found a suitable memory range */ > break; > } while (1); > @@ -546,6 +553,13 @@ static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end, > continue; > } > > + /* Avoid placing the next kernel on hardware-poisoned memory */ > + if (range_contains_hwpoison(temp_start, > + temp_end - temp_start + 1)) { > + temp_start = temp_start + PAGE_SIZE; > + continue; > + } > + > /* We found a suitable memory range */ > break; > } while (1); > diff --git a/mm/memory-failure.c b/mm/memory-failure.c > index a8b03e2920ba8..032bb23db7566 100644 > --- a/mm/memory-failure.c > +++ b/mm/memory-failure.c > @@ -96,6 +96,24 @@ void num_poisoned_pages_sub(unsigned long pfn, long i) > memblk_nr_poison_sub(pfn, i); > } > > +/* > + * Check if any page in [start, start + size) has hardware-poisoned segments > + */ > +bool range_contains_hwpoison(phys_addr_t start, unsigned long size) > +{ > + unsigned long pfn, end_pfn; > + > + if (!size || !atomic_long_read(&num_poisoned_pages)) > + return false; > + > + end_pfn = PHYS_PFN(start + size - 1); > + for (pfn = PHYS_PFN(start); pfn <= end_pfn; pfn++) { > + if (pfn_valid(pfn) && PageHWPoison(pfn_to_page(pfn))) Should we use pfn_to_online_page here? I doubt we might meet the same problem in [1] if pfn_to_page is used here. [1]: https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=d613f53c83ec47089c4e25859d5e8e0359f6f8da Thanks. .