Re: [PATCH v5 7/9] drivers/base/memory: count inherited poisoned frames into the block
From: David Hildenbrand (Arm)
Date: Thu Sep 24 2026 - 07:29:06 EST
On 9/22/26 14:34, Breno Leitao wrote:
> Hello David,
>
> On Tue, Sep 22, 2026 at 01:33:51PM +0200, David Hildenbrand (Arm) wrote:
>> On 9/21/26 16:31, Breno Leitao wrote:
>>>
>>> The EFI table is only written when there is a memory failure. That is
>>> the only thing that writes to it:
>>>
>>> action_result() -> efi_hwpoison_record_pfn() -> set_bit()
>>>
>>> You can see it on patch "mm/memory-failure: efi: record
>>> hardware-poisoned frames into the poisoned-memory table"
>>>
>>> Then, when the kernel kexecs into a second kernel, the EFI config table
>>> is queried and the pages are poisoned from it at boot, as they are
>>> getting into the buddy allocator, in __free_pages_core().
>>
>> I am not sure that is really the right place. That means we only poison free
>> memory.
>
> Right, and that is a real gap. A frame only reaches __free_pages_core()
> if it is handed to the buddy allocator, so anything this kernel already
> took from memblock -- the memmap itself, early page tables, percpu --
> can be sitting on a recorded frame with nobody noticing.
>
>> Shouldn't we poison as soon as we initialize the memmap, and check
>> whether any memblock allocations ended up on that poisoned memory and bail out?
>
Thanks for the history on this.
>
> That is close to what I had until v2, where the bitmap was walked at EFI
> parse time and every set unit was memblock_reserve()d:
>
> for (off = 0; off < bitmap_size; off += PAGE_SIZE) {
> ...
> for_each_set_bit(bit, map, nbits) {
> u64 unit = off * BITS_PER_BYTE + bit;
>
> memblock_reserve(phys_base + unit * unit_size, unit_size);
> nr_units++;
> }
> ...
> }
Yes, that what I have in mind.
>
> Set the full v2 patch at
> https://lore.kernel.org/all/20260821-hwpoison-kho-v2-6-5743791e48e6@xxxxxxxxxx/
>
> Kiryl raised some concerns, such as x86 does not keep memblock past
> boot,
The memmap contains that information. If it's a scalability problem with
thousands of hwpoisoned 2M ranges, don't try to keep kernels running on utterly
broken systems, seriously.
> and kexec picks placement from iomem_resource via
> walk_system_ram_res(), which memblock_reserve() does not touch.
Where we could scan the memmap. If we find scalability problems with that they
should be addressed there.
> So the
> frames stay out of buddy, but the next image can still be placed on
> them, and since nothing sets PG_hwpoison none of the existing hwpoison
> users see them either.
Memblock knows which ranges were poisoned. It shouldn't hand these out, neither
to memblock users or the buddy.
And is it knows that these ranges are poisoned, it should be marking them as
hwpoisoned when we initialize the memmap.
Kiryl probably remembered the unaccepted-memory hammer and thought of hwpoison
of being just another nail. It's not. hwpoison is a proper kernel infrastructure.
>
> Most of the discussion happened here:
> https://lore.kernel.org/all/aog_uH5fTsJzXjjx@thinkstation/
>
> v3 then poisoned the frames from mm_core_init(), right after
> memblock_free_all(), taking them back off buddy.
They should have never been exposed to the buddy.
> That breaks with
> CONFIG_DEFERRED_STRUCT_PAGE_INIT, where the struct pages above
> first_deferred_pfn do not exist yet at that point, and Kiryl's other
> point was that the shape was wrong regardless: flag the frame on its
> first add to buddy, not after buddy is up.
The flags should be set earlier and pages never even handed to the buddy.
Handing out poisoned pages from one allocator to hide them from another
allocator is stupid.
>
> See the discussion at:
> https://lore.kernel.org/all/apGbDOnPm5VXP5ez@thinkstation/
>
> Which is how it ended up in __free_pages_core().
>>>> From that point on, the memmap should be our reliable source of information.
>
> Sure, is this approach better?
>
> - the bitmap is read as the struct pages are initialised, and the only
> thing it does there is SetPageHWPoison();
>
> - __free_pages_core() keeps the hook but tests PageHWPoison() instead
> of the table, so a recorded frame is still held back before it can
> enter buddy;
>
> - nothing else queries the table.
Why should we give pages we know are poisoned to the buddy? And why should we
let memblock hand out poisoned ranges?
I'm sorry, it's all hacky.
I see why we want a bitmap from kernel A to kernel B, but once the kernel is up
we have established infrastrcture to identify poisoned pages.
Just look at the mess in the patch here I replied to. Pairing bitmap scans with
memmap scans ... hacky.
>
> One caveat on "exactly once": if that means one pass at one point in
> boot, it is what v3[1] did -- hwpoison_init_boot() from mm_core_init(),
> right after memblock_free_all() -- and it does not work. With
> CONFIG_DEFERRED_STRUCT_PAGE_INIT the struct pages above
> first_deferred_pfn are not there yet at that point.
>
> Link: https://lore.kernel.org/all/20260826-hwpoison-kho-v3-5-6f79c4b605bc@xxxxxxxxxx/
>
> So I _think_ we want to do it once per frame, as that frame's struct
> page comes up. __init_single_page() is the single function they all pass
> through. Is __init_single_page() the right place to query the bitmap and
> mark is as poisoned?
That's what I originally had in mind, but I am not sure if it's the right time
to query it.
Assume we tell memblock about the poisoned ranges early and it would not hand
them out.
memblock_free_pages() would never get called in the first place.
All pages would look like they are simply allocated by memblock.
We'd have to traverse the poisoned ranges in memblock once to set the hwpoisoned
bits.
>
>> For this code here, just remember globally whether we hwpoisoned any page. If
>> so, just walk the memmap. If not (the 99.9999% of all systems, no need to scan
>> anything).
>
> Yes, and the counter already exists: num_poisoned_pages and we can query
> it. I am also planning to add something similar in the EFI table, like
> an ->empty field, which will be cleared when a bit is set, avoiding
> doing bitmap walk on the happy path (not a single poisoned page).
>
> Anyway, range_hwpoison() short-circuits on it today,
>
> if (!size || !atomic_long_read(&num_poisoned_pages))
> return poison;
>
> so memblk_nr_poison_init() can gate on the same thing and walk the
> memmap, with no EFI call left in drivers/base/memory.c at all. That
> disposes of the misleading name there too, which was where this
> started.
>
> Now, the half I am less sure about is checking whether a memblock
> allocation landed on a recorded frame. Poisoning at memmap init cannot
> prevent that -- the memmap is itself a memblock allocation, so the
> struct pages do not exist while the early allocations are being made --
> it can only detect it afterwards, as a pass over memblock.reserved
> before memblock_free_all().
>
> The alternative is to bring v2's memblock_reserve() back purely to keep
> the early allocator off those frames, and still poison at memmap init
> so the flag is there for everyone else. That gets both properties, at
> the cost of frames that are Reserved and HWPoison and never in buddy,
> which I have not thought through for unpoison.
Thanks for the history on this. I tried to summarize my thoughts above. I might
be wrong.
--
Cheers,
David