cxl, mm: Keep device-reported poison out of the page allocator
Subsystems (4)
cover cxl, mm: Keep device-reported poison out of the page allocator
A CXL memory device keeps its own poison list: the DPAs it knows to be bad. The list lives on the device, so it survives a host reboot, a power cycle, and moving the device to another host.
Linux reads that list only when user space writes trigger_poison_list. Nothing reads it when a region is assembled, so with a device that already has poison:
1. the RAM region is assembled and dax/kmem onlines it;
2. every frame enters the buddy allocator, including the ones the
device would have named had anyone asked;
3. a task is handed one and reads it: machine check, memory_failure(), SIGBUS.
This series reads the poison list at region probe, before the range is onlined, and keeps the frames it covers out of the allocator. Nothing is unmapped or signalled, and no runtime path changes: the frames never enter the allocator, so this is not forwarding latent poison to memory_failure().
Dependency
==========
This is built on Breno Leitao's hwpoison series [1], which keeps frames poisoned before a kexec out of the next kernel's allocator. The approach here, withholding frames as they are onlined rather than taking them back out later, follows [1]. [1] hooks __free_pages_core(), splits a block around its bad frames, and marks them with hwpoison_boot_page(). Patch 4 adds a second source to that hook.
The two cover different cases. [1] records frames in a bitmap spanning the RAM the EFI memory map describes. Memory hot-added after boot sits outside it, and CXL region memory is hot-added after boot. [1] also carries its record across kexec only, while the device's list survives reboot and migration.
Applies on next-20260908 plus [1] v5. The num_poisoned_pages_inc() change agreed for v6 [2] does not touch these patches. Patches 1, 2 and 5 touch only drivers/cxl; patches 3 and 4 touch mm.
Design
======
Which records are withheld. A corrected error never reaches the poison list, so every record is uncorrectable. Only records with source Internal, a failure of the device's own media, are withheld. External poison was written by the host into healthy media and a rewrite recovers it; Injected poison comes from the Inject Poison test command. Withholding either would make a recoverable state permanent.
No host-side storage. The device is the source of truth and is re-read on every probe. When a location is repaired (sPPR, sparing, hPPR) the device drops it from the list, and the next probe onlines the frame again. A record persisted by the host could not learn about the repair.
Volatile ranges. CXL r3.2 8.2.9.8.4.1:
If the device does not support poison list for volatile ranges and any location in the requested list maps to volatile, the device shall return Invalid Physical Address.
A device that returns it, or that has no Get Poison List at all, is onlined as today with nothing withheld. This is also the reason for the RAM -EFAULT tolerance poison_by_decoder() has carried, without a comment, since f0832a586396 ("cxl/region: Provide region info to the cxl_poison trace event").
Incomplete lists. If the device reports overflow, the host stops at max_errors with more records pending, or a media scan is in progress, region probe fails rather than onlining on a partial list.
Translation. Each record is mapped to the pages it covers through cxl_dpa_to_hpa(), including the extended linear cache alias. PFNs are collected in an xarray, which removes duplicates and keeps them sorted, then coalesced into ranges and registered once per region. The ranges are unregistered when the region is torn down.
Out of scope: regions with normalized addressing, and PMEM regions converted to system RAM through device-dax.
Patches
=======
1. Snapshot the full Get Poison List across continuations into a caller-owned buffer, keeping overflow and scan-in-progress state. Also bound the record count by the returned payload, and reject an empty payload with More set.
2. Factor the per-decoder query out of poison_by_decoder().
3. Add a registry of PFN ranges known bad before online. Readers walk it under RCU, since __free_pages_core() runs concurrently across nodes during deferred init.
4. Consult the registry from [1]'s free_poisoned_block() and __free_pages_core().
5. At RAM-region probe, read each decoder's list, translate, and register before the DAX region is created.
Testing
=======
QEMU 9.1.0, x86 q35, one 512 MiB volatile cxl-type3 device, 1-way region at 0x190000000.
QMP cxl-inject-poison records poison as Internal. The kernel's debugfs inject_poison goes through the mailbox and records it as Injected, so both cases can be tested without a mock.
Three 64-byte records were set up before the region was created:
DPA Injected by Source Result after create-region --- ----------- ------ ---------------------------- 0x100000 QMP Internal withheld (PFN 0x190100) 0x200000 debugfs (mailbox) Injected not withheld, source filtered 0x300000 QMP, then cleared - not withheld, no longer listed
create-region logged "withholding 1 frame(s) reported poisoned by the device", and HardwareCorrupted rose to 4 kB: one page, for the one Internal record.
To reproduce:
-------------------------
1. T1 (Terminal 1): boot the guest
vng --disable-kvm -v -r ./arch/x86/boot/bzImage \
--disable-microvm --memory 4G --cpus 4 \
--append "memhp_default_state=online_movable" \
--qemu-opts="-machine q35,cxl=on \
-object memory-backend-ram,id=vmem0,share=on,size=512M \
-device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
-device cxl-rp,port=0,bus=cxl.1,id=rp0,chassis=0,slot=2 \
-device cxl-type3,bus=rp0,volatile-memdev=vmem0,id=cxl-vmem0 \
-M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G \
-qmp unix:/tmp/qmp.sock,server=on,wait=off"2. T1(Terminal 1), in the guest as root: set up and check
# mount -t debugfs none /sys/kernel/debug 2>/dev/null # mount -t tracefs none /sys/kernel/tracing 2>/dev/null # echo 1 > /sys/kernel/tracing/events/cxl/cxl_poison/enable # echo 'file drivers/cxl/core/region.c +p' > /sys/kernel/debug/dynamic_debug/control
# cxl list -M -D -u
[
{
"memdevs":[
{
"memdev":"mem0",
"ram_size":"512.00 MiB (536.87 MB)",
"serial":"0",
"host":"0000:0d:00.0",
"firmware_version":"BWFW VERSION 00",
"poison_injectable":true
}
]
},
{
"root decoders":[
{
"decoder":"decoder0.0",
"resource":"0x190000000",
"size":"4.00 GiB (4.29 GB)",
"interleave_ways":1,
"max_available_extent":"4.00 GiB (4.29 GB)",
"pmem_capable":true,
"volatile_capable":true,
"accelmem_capable":true,
"qos_class":0,
"nr_targets":1
}
] }
]
3. T2, on the host: inject two Internal records over QMP(Qemu Machine Protocol)
$qmp() { printf '{"execute":"qmp_capabilities"}\n%s\n' "$1" | socat - UNIX-CONNECT:/tmp/qmp.sock; }
$qmp '{"execute":"cxl-inject-poison","arguments":{"path":"/machine/peripheral/cxl-vmem0","start":1048576,"length":64}}' # DPA 1 MB
$qmp '{"execute":"cxl-inject-poison","arguments":{"path":"/machine/peripheral/cxl-vmem0","start":3145728,"length":64}}' # DPA 3 MB
4. T1: inject an Injected record, and clear one
# ls /sys/kernel/debug/cxl/mem0/ clear_poison dpamem inject_poison
# echo 0x200000 > /sys/kernel/debug/cxl/mem0/inject_poison # echo 0x300000 > /sys/kernel/debug/cxl/mem0/clear_poison
5. T1: check the device’s list before creating a region
# echo > /sys/kernel/tracing/trace
# echo 1 > /sys/bus/cxl/devices/mem0/trigger_poison_list
# cat /sys/kernel/tracing/trace
# tracer: nop
#
# entries-in-buffer/entries-written: 2/2 #P:4
#
# _-----=> irqs-off/BH-disabled
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / _-=> migrate-disable
# |||| / delay
# TASK-PID CPU# ||||| TIMESTAMP FUNCTION
# | | | ||||| | |
bash-194 [001] ..... 511.644231: cxl_poison: memdev=mem0 host=0000:0d:00.0 serial=0 trace_type=List region= region_uuid=00000000-0000-0000-0000-000000000000 hpa=0xffffffffffffffff hpa_alias0=0xffffffffffffffff dpa=0x200000 dpa_length=0x40 source=Injected flags= overflow_time=0
bash-194 [001] ..... 511.644343: cxl_poison: memdev=mem0 host=0000:0d:00.0 serial=0 trace_type=List region= region_uuid=00000000-0000-0000-0000-000000000000 hpa=0xffffffffffffffff hpa_alias0=0xffffffffffffffff dpa=0x100000 dpa_length=0x40 source=Internal flags= overflow_time=0
# cat /proc/iomem
190000000-28fffffff : CXL Window 06. T1: create the region
# cxl create-region -m -t ram -d decoder0.0 -w 1 mem0
cxl region0: Bypassing cpu_cache_invalidate_memregion() for testing!
cxl_region region0: withholding 1 frame(s) reported poisoned by the device --------------------> Withholding 1 frame
{
"region":"region0",
"resource":"0x190000000",
"size":"512.00 MiB (536.87 MB)",
"type":"ram",
"interleave_ways":1,
"interleave_granularity":256,
"decode_state":"commit",
"locked":false,
"mappings":[
{
"position":0,
"memdev":"mem0",
"decoder":"decoder2.0"
}
],
"qos_class_mismatch":true
}
cxl region: cmd_create_region: created 1 region
Fallback order for Node 0: 0
Built 1 zonelists, mobility grouping on. Total pages: 1007394
Policy zone: Normal# cat /proc/iomem
190000000-28fffffff : CXL Window 0
190000000-1afffffff : region0
190000000-1afffffff : dax0.0
190000000-1afffffff : System RAM (kmem)512 MB is 0x20000000, so the region ends at 0x1afffffff
./tools/mm/page-types -a 0x190000+0x20000 -b hwpoison -l
offset len flags
190100 1 ___________________X_______________________
flags page-count MB symbolic-flags long-symbolic-flags
0x0000000000080000 1 0 ___________________X_______________________ hwpoison
total 1 07. T1: check exactly one frame is out of the allocator
# grep HardwareCorrupted /proc/meminfo HardwareCorrupted: 4 kB
Not yet covered: interleaved regions, extended linear cache, poison list overflow, hot-remove of a block holding a withheld frame, and real hardware. I intend to cover these before a non-RFC posting.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
[2] https://lore.kernel.org/linux-cxl/[email protected]/
Shaikh Kamaluddin (5):
cxl/mbox: Add Get Poison List snapshot support cxl/region: Factor decoder poison-list retrieval mm/memory-failure: Add a pre-online poison registry mm/page_alloc: Keep pre-online poison out of the buddy allocator cxl/region: Register device poison before exposing RAM
drivers/cxl/core/mbox.c | 137 ++++++++++++- drivers/cxl/core/region.c | 363 ++++++++++++++++++++++++++++++++- drivers/cxl/cxlmem.h | 25 +++ include/linux/memory-failure.h | 92 +++++++++ mm/memory-failure.c | 175 ++++++++++++++++ mm/page_alloc.c | 8 +- 6 files changed, 787 insertions(+), 13 deletions(-)
base-commit: a9d7ced84989ec05be09b4b8428759ef60450a0f
prerequisite-patch-id: fe9d44deb8ccc48c0311bc4131bdf733eb353b34
prerequisite-patch-id: 4cabe6adb37cd8c218bd43c97970bc65f313b671
prerequisite-patch-id: 52ad24bceedb9c03df167f10573166a1d97ba97d
prerequisite-patch-id: 44bfbc8aa3c4b19593f7b0d466fbc0cb99b99f76
prerequisite-patch-id: 2d344a5322ff07c7895a59d2425540a30e4e0630
prerequisite-patch-id: e776f306dcbdda3f6972187ad1e43a62c2754890
prerequisite-patch-id: 3ef887599e31ef973d6d9ca15d0462d55f5f0409
prerequisite-patch-id: a23fe88ab4529b6a6669a991e15cf1631311391c
prerequisite-patch-id: 111c69880ed0e148cdda285896c006e8d1fc98a8
Signature · 1 line
2.43.0
RFC PATCH 1/5 cxl/mbox: Add Get Poison List snapshot support
cxl_mem_get_poison() uses a device-owned output buffer for each Get Poison List response. It traces the records before issuing the next command, which overwrites that buffer when the device sets the More Media Error Records flag. A caller therefore cannot inspect the complete set of collected records after the function returns.
Add a caller-owned snapshot and copy each validated mailbox response into it. Preserve device overflow and scan-in-progress status, and indicate when the host stops collection before retrieving all available records.
Keep cxl_mem_get_poison() as the existing trace-only wrapper so its current callers, including trigger_poison_list, retain their behavior.
--- drivers/cxl/core/mbox.c | 137 ++++++++++++++++++++++++++++++++++++++-- drivers/cxl/cxlmem.h | 25 ++++++++ 2 files changed, 156 insertions(+), 6 deletions(-)
drivers/cxl/core/mbox.c +131 -6
@@ -1376,14 +1376,91 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds) }EXPORT_SYMBOL_NS_GPL (cxl_set_timestamp,"CXL" ); -int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len, -struct cxl_region *cxlr) +int cxl_poison_snapshot_init (struct cxl_memdev *cxlmd, +struct cxl_poison_snapshot *snapshot) +{ +struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlmd->cxlds); +u32 capacity = mds->poison.max_errors; + +if (!snapshot) +return -EINVAL ; +memset (snapshot,0 ,sizeof (*snapshot)); + +if (!test_bit (CXL_POISON_ENABLED_LIST , mds->poison.enabled_cmds)) +return -EOPNOTSUPP ; + +if (!capacity) +return -EPROTO ; + + snapshot->records =kvmalloc_array (mds->poison.max_errors, +sizeof (*snapshot->records), +GFP_KERNEL ); +if (!snapshot->records) +return -ENOMEM ; + + snapshot->capacity = capacity; + +return 0 ; +} + +void cxl_poison_snapshot_destroy (struct cxl_poison_snapshot *snapshot) +{ +if (!snapshot) +return ; + +kvfree (snapshot->records); +memset (snapshot,0 ,sizeof (*snapshot)); +} + +static int cxl_poison_snapshot_append (struct cxl_poison_snapshot *snapshot, +struct cxl_poison_record *records, +u32 count) +{ +u32 available; + +/* +* Existing cxl_mem_get_poison() callers request tracing only and +* pass no snapshot. +*/ + +if (!snapshot) +return 0 ; + +if (!snapshot->records) +return -EINVAL ; + +if (snapshot->nr_records > snapshot->capacity) +return -EOVERFLOW ; + +if (!count) +return 0 ; + +if (!records) +return -EINVAL ; + + available = snapshot->capacity - snapshot->nr_records; +if (count > available) { + snapshot->truncated = true; +return -ENOSPC ; + } + +memcpy (&snapshot->records[snapshot->nr_records], records, + count *sizeof (*records)); + snapshot->nr_records += count; + +return 0 ; +} + +static int cxl_mem_get_poison_common (struct cxl_memdev *cxlmd,u64 offset, +u64 len,struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot) {struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlmd->cxlds);struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox;struct cxl_mbox_poison_out *po;struct cxl_mbox_poison_in pi; -int nr_records =0 ; +u32 nr_records =0 ; +u32 count;int rc;ACQUIRE (mutex_intr, lock)(&mds->poison.mutex);
@@ -1408,29 +1485,77 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,if (rc)break ; -if (!le16_to_cpu (po->count)) { + count =le16_to_cpu (po->count); +if (!count) {dev_dbg (&cxlmd->dev,"Poison empty payload!\n" ); + +if (po->flags &CXL_POISON_FLAG_MORE ) { +if (snapshot) + snapshot->truncated = true; + rc = -EPROTO ; + } + +break ; + } + +if (struct_size (po, record, count) > mbox_cmd.size_out) { +dev_err (&cxlmd->dev, +"invalid poison record count: %u\n" , count); + rc = -EPROTO ;break ; } -for (int i =0 ; i <le16_to_cpu (po->count); i++) +for (u32 i =0 ; i < count; i++)trace_cxl_poison (cxlmd, cxlr, &po->record[i], po->flags, po->overflow_ts,CXL_POISON_TRACE_LIST ); +if (snapshot && (po->flags &CXL_POISON_FLAG_OVERFLOW )) { + snapshot->device_flags |=CXL_POISON_FLAG_OVERFLOW ; + snapshot->overflow_ts =le64_to_cpu (po->overflow_ts); + } + +if (snapshot && (po->flags &CXL_POISON_FLAG_SCANNING )) + snapshot->device_flags |=CXL_POISON_FLAG_SCANNING ; + + rc =cxl_poison_snapshot_append (snapshot, po->record, count); +if (rc) +break ; +/* Protect against an uncleared _FLAG_MORE */ - nr_records = nr_records +le16_to_cpu (po->count); + nr_records += count;if (nr_records >= mds->poison.max_errors) {dev_dbg (&cxlmd->dev,"Max Error Records reached: %d\n" , nr_records); +if (snapshot && (po->flags &CXL_POISON_FLAG_MORE )) + snapshot->truncated = true; +break ; } }while (po->flags &CXL_POISON_FLAG_MORE );return rc; } + +int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len, +struct cxl_region *cxlr) +{ +return cxl_mem_get_poison_common (cxlmd, offset, len, cxlr,NULL ); +}EXPORT_SYMBOL_NS_GPL (cxl_mem_get_poison,"CXL" ); +int cxl_mem_get_poison_snapshot (struct cxl_memdev *cxlmd,u64 offset,u64 len, +struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot) +{ +if (!snapshot || !snapshot->records || !snapshot->capacity) +return -EINVAL ; + +if (snapshot->nr_records > snapshot->capacity) +return -EOVERFLOW ; + +return cxl_mem_get_poison_common (cxlmd, offset, len, cxlr, snapshot); +}static void free_poison_buf (void *buf) {kvfree (buf);
drivers/cxl/cxlmem.h +25 -0
@@ -812,6 +812,31 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds);int cxl_poison_state_init (struct cxl_memdev_state *mds);int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len,struct cxl_region *cxlr); + +/** +* struct cxl_poison_snapshot - Aggregate Get Poison List result +* @records: Copied Media Error Records from all mailbox responses +* @nr_records: Number of valid records stored in @records +* @capacity: Maximum number of records that @records can hold +* @overflow_ts: Timestamp associated with a reported device overflow +* @device_flags: Persistent device status observed during collection +* @truncated: Host stopped before collecting all available records +*/ +struct cxl_poison_snapshot { +struct cxl_poison_record *records; +u32 nr_records; +u32 capacity; +u64 overflow_ts; +u8 device_flags; +bool truncated; +}; + +int cxl_poison_snapshot_init (struct cxl_memdev *cxlmd, +struct cxl_poison_snapshot *snapshot); +void cxl_poison_snapshot_destroy (struct cxl_poison_snapshot *snapshot); +int cxl_mem_get_poison_snapshot (struct cxl_memdev *cxlmd,u64 offset,u64 len, +struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot);int cxl_trigger_poison_list (struct cxl_memdev *cxlmd);int cxl_inject_poison (struct cxl_memdev *cxlmd,u64 dpa);int cxl_clear_poison (struct cxl_memdev *cxlmd,u64 dpa);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [RFC PATCH 1/5] cxl/mbox: Add Get Poison List snapshot support
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] cxl: cxl_poison_snapshot_append returns -ENOSPC, bypassing truncation handler --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] cxl: cxl_poison_snapshot_append returns -ENOSPC, bypassing truncation handler --
commit 14df359a915714a1d8f08f27fcdf1ccf13b88763
Author: Shaikh Kamaluddin <[email protected]>
cxl/mbox: Add Get Poison List snapshot support
The commit adds snapshot support to copy Get Poison List records from a device into a caller-owned buffer. It allows callers to inspect the complete set of collected records while preserving existing trace-only behavior for current callers.
Quoted 4 lines
drivers/cxl/core/mbox.c +0
-0
[ ... ]
Quoted 4 lines
drivers/cxl/core/mbox.c +4
-0+static int cxl_poison_snapshot_append (struct cxl_poison_snapshot *snapshot, +struct cxl_poison_record *records, +u32 count) +{
[ ... ]
drivers/cxl/core/mbox.c int cxl_set_timestamp(struct cxl_memdev_state *mds) · line 1445
Quoted 5 lines
drivers/cxl/core/mbox.c +5
-0+ available = snapshot->capacity - snapshot->nr_records; +if (count > available) { + snapshot->truncated = true; +return -ENOSPC ; + }
[Severity: High]
Does returning -ENOSPC here cause the caller to prematurely abort?
In drivers/cxl/core/mbox.c, cxl_poison_snapshot_append(), when the device returns more poison records than snapshot->capacity, returning -ENOSPC instead of clamping the count and returning success prevents the loop in cxl_mem_get_poison_common() from completing its normal termination logic.
Quoted 12 lines
drivers/cxl/core/mbox.c +11
-0+ +memcpy (&snapshot->records[snapshot->nr_records], records, + count *sizeof (*records)); + snapshot->nr_records += count; + +return 0 ; +} + +static int cxl_mem_get_poison_common (struct cxl_memdev *cxlmd,u64 offset, +u64 len,struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot) {
[ ... ]
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1523
Quoted 3 lines
drivers/cxl/core/mbox.c +3
-0+ rc =cxl_poison_snapshot_append (snapshot, po->record, count); +if (rc) +break ;
[Severity: High]
Does breaking early on -ENOSPC deprive the caller of the expected truncation state?
In drivers/cxl/core/mbox.c, cxl_mem_get_poison_common(), if cxl_poison_snapshot_append() returns -ENOSPC, this propagates up and breaks the loop before the internal termination logic can be reached. The generic error cascades up to the caller, bypassing the expected truncation handler designed to handle this exact scenario.
Quoted 4 lines
drivers/cxl/core/mbox.c +3
-1+ +/* Protect against an uncleared _FLAG_MORE */ - nr_records = nr_records +le16_to_cpu (po->count); + nr_records += count;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jonathan Cameron
maintainerRe: [RFC PATCH 1/5] cxl/mbox: Add Get Poison List snapshot support
In reply to Shaikh KamaluddinQuoted 27 lines · On Sat, 10 Oct 2026 21:50:09 +0530 Shaikh Kamaluddin…
cxl_mem_get_poison()uses a device-owned output buffer for each Get Poison List response. It traces the records before issuing the next command, which overwrites that buffer when the device sets the More Media Error Records flag. A caller therefore cannot inspect the complete set of collected records after the function returns.Add a caller-owned snapshot and copy each validated mailbox response into it. Preserve device overflow and scan-in-progress status, and indicate when the host stops collection before retrieving all available records.
Keep
cxl_mem_get_poison()as the existing trace-only wrapper so its current callers, includingtrigger_poison_list, retain their behavior.Signed-off-by Shaikh Kamaluddin <[email protected]>--- drivers/cxl/core/mbox.c | 137 ++++++++++++++++++++++++++++++++++++++-- drivers/cxl/cxlmem.h | 25 ++++++++ 2 files changed, 156 insertions(+), 6 deletions(-)drivers/cxl/core/mbox.c +0
-0@@ -1376,14 +1376,91 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds) }EXPORT_SYMBOL_NS_GPL (cxl_set_timestamp,"CXL" );
drivers/cxl/core/mbox.c int cxl_set_timestamp(struct cxl_memdev_state *mds) · line 1385
Quoted 10 lines
drivers/cxl/core/mbox.c +7
-2-int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len, -struct cxl_region *cxlr) +int cxl_poison_snapshot_init (struct cxl_memdev *cxlmd, +struct cxl_poison_snapshot *snapshot) +{ +struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlmd->cxlds); +u32 capacity = mds->poison.max_errors; + +if (!snapshot)
That seems like a buggy use of the interface, so not sure we need that defence.
drivers/cxl/core/mbox.c int cxl_set_timestamp(struct cxl_memdev_state *mds) · line 1397
Quoted 12 lines
drivers/cxl/core/mbox.c +12
-0+return -EINVAL ; +memset (snapshot,0 ,sizeof (*snapshot)); + +if (!test_bit (CXL_POISON_ENABLED_LIST , mds->poison.enabled_cmds)) +return -EOPNOTSUPP ; + +if (!capacity) +return -EPROTO ; + + snapshot->records =kvmalloc_array (mds->poison.max_errors, +sizeof (*snapshot->records), +GFP_KERNEL );
kvmalloc_objs()?
Quoted 72 lines
drivers/cxl/core/mbox.c +61
-1+if (!snapshot->records) +return -ENOMEM ; + + snapshot->capacity = capacity; + +return 0 ; +} + +void cxl_poison_snapshot_destroy (struct cxl_poison_snapshot *snapshot) +{ +if (!snapshot) +return ; + +kvfree (snapshot->records); +memset (snapshot,0 ,sizeof (*snapshot)); +} + +static int cxl_poison_snapshot_append (struct cxl_poison_snapshot *snapshot, +struct cxl_poison_record *records, +u32 count) +{ +u32 available; + +/* +* Existing cxl_mem_get_poison() callers request tracing only and +* pass no snapshot. +*/ + +if (!snapshot) +return 0 ; + +if (!snapshot->records) +return -EINVAL ; + +if (snapshot->nr_records > snapshot->capacity) +return -EOVERFLOW ; + +if (!count) +return 0 ; + +if (!records) +return -EINVAL ; + + available = snapshot->capacity - snapshot->nr_records; +if (count > available) { + snapshot->truncated = true; +return -ENOSPC ; + } + +memcpy (&snapshot->records[snapshot->nr_records], records, + count *sizeof (*records)); + snapshot->nr_records += count; + +return 0 ; +} + +static int cxl_mem_get_poison_common (struct cxl_memdev *cxlmd,u64 offset, +u64 len,struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot) {struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlmd->cxlds);struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox;struct cxl_mbox_poison_out *po;struct cxl_mbox_poison_in pi; -int nr_records =0 ; +u32 nr_records =0 ; +u32 count;int rc;ACQUIRE (mutex_intr, lock)(&mds->poison.mutex);@@ -1408,29 +1485,77 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,if (rc)
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1494
Quoted 10 lines
drivers/cxl/core/mbox.c +6
-1break ; -if (!le16_to_cpu (po->count)) { + count =le16_to_cpu (po->count); +if (!count) {dev_dbg (&cxlmd->dev,"Poison empty payload!\n" ); + +if (po->flags &CXL_POISON_FLAG_MORE ) { +if (snapshot) + snapshot->truncated = true;
Count of zero and yet more? How would that happen? This smells like a hardware bug. Maybe we defend against it, but that's a separate question from the rest of this patch.
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1503
Quoted 9 lines
drivers/cxl/core/mbox.c +9
-0+ rc = -EPROTO ; + } + +break ; + } + +if (struct_size (po, record, count) > mbox_cmd.size_out) { +dev_err (&cxlmd->dev, +"invalid poison record count: %u\n" , count);
Also looks like broken hardware. I don't necessarily mind that sort of defense, but not in a patch doing anything else.
Quoted 4 lines
drivers/cxl/core/mbox.c +1
-0+ rc = -EPROTO ;break ; }
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1516
Quoted 10 lines
drivers/cxl/core/mbox.c +5
-1-for (int i =0 ; i <le16_to_cpu (po->count); i++) +for (u32 i =0 ; i < count; i++)trace_cxl_poison (cxlmd, cxlr, &po->record[i], po->flags, po->overflow_ts,CXL_POISON_TRACE_LIST ); +if (snapshot && (po->flags &CXL_POISON_FLAG_OVERFLOW )) { + snapshot->device_flags |=CXL_POISON_FLAG_OVERFLOW ; + snapshot->overflow_ts =le64_to_cpu (po->overflow_ts); + }
I'm curious. What use is the overflow_ts in your scheme? Lets keep things minimal unless there is a reason to add data to the snapshot.
Quoted 33 lines
drivers/cxl/core/mbox.c +20
-1+ +if (snapshot && (po->flags &CXL_POISON_FLAG_SCANNING )) + snapshot->device_flags |=CXL_POISON_FLAG_SCANNING ; + + rc =cxl_poison_snapshot_append (snapshot, po->record, count); +if (rc) +break ; +/* Protect against an uncleared _FLAG_MORE */ - nr_records = nr_records +le16_to_cpu (po->count); + nr_records += count;if (nr_records >= mds->poison.max_errors) {dev_dbg (&cxlmd->dev,"Max Error Records reached: %d\n" , nr_records); +if (snapshot && (po->flags &CXL_POISON_FLAG_MORE )) + snapshot->truncated = true; +break ; } }while (po->flags &CXL_POISON_FLAG_MORE );return rc; } + +int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len, +struct cxl_region *cxlr) +{ +return cxl_mem_get_poison_common (cxlmd, offset, len, cxlr,NULL ); +}EXPORT_SYMBOL_NS_GPL (cxl_mem_get_poison,"CXL" ); +int cxl_mem_get_poison_snapshot(struct cxl_memdev *cxlmd, u64 offset, u64 len, + struct cxl_region *cxlr,
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1558
Quoted 10 lines
drivers/cxl/core/mbox.c +10
-0+ struct cxl_poison_snapshot *snapshot) +{ +if (!snapshot || !snapshot->records || !snapshot->capacity) +return -EINVAL ; + +if (snapshot->nr_records > snapshot->capacity) +return -EOVERFLOW ; + +return cxl_mem_get_poison_common (cxlmd, offset, len, cxlr, snapshot); +}
Blank line here
Quoted 13 lines
drivers/cxl/core/mbox.c +0
-0static void free_poison_buf (void *buf) {kvfree (buf);drivers/cxl/cxlmem.h +2
-0@@ -812,6 +812,31 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds);int cxl_poison_state_init (struct cxl_memdev_state *mds);int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len,struct cxl_region *cxlr); + +/**
drivers/cxl/cxlmem.h int cxl_set_timestamp(struct cxl_memdev_state *mds); · line 826
Quoted 10 lines
drivers/cxl/cxlmem.h +10
-0+* struct cxl_poison_snapshot - Aggregate Get Poison List result +* @records: Copied Media Error Records from all mailbox responses +* @nr_records: Number of valid records stored in @records +* @capacity: Maximum number of records that @records can hold +* @overflow_ts: Timestamp associated with a reported device overflow +* @device_flags: Persistent device status observed during collection +* @truncated: Host stopped before collecting all available records +*/ +struct cxl_poison_snapshot { +struct cxl_poison_record *records;
__counted_by_ptr() seems likely to apply here.
drivers/cxl/cxlmem.h int cxl_set_timestamp(struct cxl_memdev_state *mds); · line 829
Quoted 3 lines
drivers/cxl/cxlmem.h +3
-0+u32 nr_records; +u32 capacity; +u64 overflow_ts;
As above. I'd like to ensure this only stashes what we actually need.
Quoted 13 lines
drivers/cxl/cxlmem.h +10
-0+u8 device_flags; +bool truncated; +}; + +int cxl_poison_snapshot_init (struct cxl_memdev *cxlmd, +struct cxl_poison_snapshot *snapshot); +void cxl_poison_snapshot_destroy (struct cxl_poison_snapshot *snapshot); +int cxl_mem_get_poison_snapshot (struct cxl_memdev *cxlmd,u64 offset,u64 len, +struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot);int cxl_trigger_poison_list (struct cxl_memdev *cxlmd);int cxl_inject_poison (struct cxl_memdev *cxlmd,u64 dpa);int cxl_clear_poison (struct cxl_memdev *cxlmd,u64 dpa);
Re: [RFC PATCH 1/5] cxl/mbox: Add Get Poison List snapshot support
Quoted 31 lines · Jonathan Cameron
Quoted 28 lines · On Sat, 10 Oct 2026 21:50:09 +0530 Shaikh Kamaluddin…
cxl_mem_get_poison()uses a device-owned output buffer for each Get Poison List response. It traces the records before issuing the next command, which overwrites that buffer when the device sets the More Media Error Records flag. A caller therefore cannot inspect the complete set of collected records after the function returns.Add a caller-owned snapshot and copy each validated mailbox response into it. Preserve device overflow and scan-in-progress status, and indicate when the host stops collection before retrieving all available records.
Keep
cxl_mem_get_poison()as the existing trace-only wrapper so its current callers, includingtrigger_poison_list, retain their behavior.Signed-off-by Shaikh Kamaluddin <[email protected]>--- drivers/cxl/core/mbox.c | 137 ++++++++++++++++++++++++++++++++++++++-- drivers/cxl/cxlmem.h | 25 ++++++++ 2 files changed, 156 insertions(+), 6 deletions(-)drivers/cxl/core/mbox.c +0
-0@@ -1376,14 +1376,91 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds) }EXPORT_SYMBOL_NS_GPL (cxl_set_timestamp,"CXL" );
Quoted 8 lines
drivers/cxl/core/mbox.c int cxl_set_timestamp(struct cxl_memdev_state *mds) · line 1384
Quoted 8 lines
drivers/cxl/core/mbox.c +6
-2-int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len, -struct cxl_region *cxlr) +int cxl_poison_snapshot_init (struct cxl_memdev *cxlmd, +struct cxl_poison_snapshot *snapshot) +{ +struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlmd->cxlds); +u32 capacity = mds->poison.max_errors; +
Hi Jonathan,
Thanks for review, and sorry for the late reply.
Quoted 5 lines
drivers/cxl/core/mbox.c int cxl_set_timestamp(struct cxl_memdev_state *mds) · line 1385
Quoted 1 line
drivers/cxl/core/mbox.c +1
-0+if (!snapshot)That seems like a buggy use of the interface, so not sure we need that defence.
Agreed, I will drop it.
Quoted 6 lines
Quoted 6 lines
drivers/cxl/core/mbox.c +6
-0+return -EINVAL ; +memset (snapshot,0 ,sizeof (*snapshot)); + +if (!test_bit (CXL_POISON_ENABLED_LIST , mds->poison.enabled_cmds)) +return -EOPNOTSUPP ; +
Quoted 9 lines
drivers/cxl/core/mbox.c int cxl_set_timestamp(struct cxl_memdev_state *mds) · line 1397
Quoted 6 lines
drivers/cxl/core/mbox.c +6
-0+if (!capacity) +return -EPROTO ; + + snapshot->records =kvmalloc_array (mds->poison.max_errors, +sizeof (*snapshot->records), +GFP_KERNEL );
kvmalloc_objs()?
Yes, will switch.
Quoted 74 lines
Quoted 74 lines
drivers/cxl/core/mbox.c +61
-1+if (!snapshot->records) +return -ENOMEM ; + + snapshot->capacity = capacity; + +return 0 ; +} + +void cxl_poison_snapshot_destroy (struct cxl_poison_snapshot *snapshot) +{ +if (!snapshot) +return ; + +kvfree (snapshot->records); +memset (snapshot,0 ,sizeof (*snapshot)); +} + +static int cxl_poison_snapshot_append (struct cxl_poison_snapshot *snapshot, +struct cxl_poison_record *records, +u32 count) +{ +u32 available; + +/* +* Existing cxl_mem_get_poison() callers request tracing only and +* pass no snapshot. +*/ + +if (!snapshot) +return 0 ; + +if (!snapshot->records) +return -EINVAL ; + +if (snapshot->nr_records > snapshot->capacity) +return -EOVERFLOW ; + +if (!count) +return 0 ; + +if (!records) +return -EINVAL ; + + available = snapshot->capacity - snapshot->nr_records; +if (count > available) { + snapshot->truncated = true; +return -ENOSPC ; + } + +memcpy (&snapshot->records[snapshot->nr_records], records, + count *sizeof (*records)); + snapshot->nr_records += count; + +return 0 ; +} + +static int cxl_mem_get_poison_common (struct cxl_memdev *cxlmd,u64 offset, +u64 len,struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot) {struct cxl_memdev_state *mds =to_cxl_memdev_state (cxlmd->cxlds);struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox;struct cxl_mbox_poison_out *po;struct cxl_mbox_poison_in pi; -int nr_records =0 ; +u32 nr_records =0 ; +u32 count;int rc;ACQUIRE (mutex_intr, lock)(&mds->poison.mutex);@@ -1408,29 +1485,77 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,if (rc)break ;
Quoted 13 lines
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1494
Quoted 8 lines
drivers/cxl/core/mbox.c +6
-1-if (!le16_to_cpu (po->count)) { + count =le16_to_cpu (po->count); +if (!count) {dev_dbg (&cxlmd->dev,"Poison empty payload!\n" ); + +if (po->flags &CXL_POISON_FLAG_MORE ) { +if (snapshot) + snapshot->truncated = true;Count of zero and yet more? How would that happen? This smells like a hardware bug. Maybe we defend against it, but that's a separate question from the rest of this patch.
Agreed, it would be a device bug. I would still like to handle it: the current code breaks out and returns 0 on an empty payload even with More set, so the snapshot's caller can't tell the list was cut short, and region probe would online the memory on the partial list.
I will take it out this series and send it as a separate patch, so this only adds the snapshot.
Quoted 4 lines
Quoted 4 lines
drivers/cxl/core/mbox.c +4
-0+ rc = -EPROTO ; + } + +break ;
Quoted 9 lines
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1503
Quoted 5 lines
drivers/cxl/core/mbox.c +5
-0+ } + +if (struct_size (po, record, count) > mbox_cmd.size_out) { +dev_err (&cxlmd->dev, +"invalid poison record count: %u\n" , count);Also looks like broken hardware. I don't necessarily mind that sort of defense, but not in a patch doing anything else.
Same, it guards against a count larger than the returned payload, I will move it into that patch as well.
Quoted 10 lines
Quoted 10 lines
drivers/cxl/core/mbox.c +2
-1+ rc = -EPROTO ;break ; } -for (int i =0 ; i <le16_to_cpu (po->count); i++) +for (u32 i =0 ; i < count; i++)trace_cxl_poison (cxlmd, cxlr, &po->record[i], po->flags, po->overflow_ts,CXL_POISON_TRACE_LIST );
Quoted 8 lines
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1516
Quoted 4 lines
drivers/cxl/core/mbox.c +4
-0+if (snapshot && (po->flags &CXL_POISON_FLAG_OVERFLOW )) { + snapshot->device_flags |=CXL_POISON_FLAG_OVERFLOW ; + snapshot->overflow_ts =le64_to_cpu (po->overflow_ts); + }I'm curious. What use is the
overflow_tsin your scheme? Lets keep things minimal unless there is a reason to add data to the snapshot.
None, as it turns out. Only the overflow flag is used, to fail the probe when the list is incomplete. I will drop overflow_ts.
Quoted 31 lines
Quoted 31 lines
drivers/cxl/core/mbox.c +18
-1+ +if (snapshot && (po->flags &CXL_POISON_FLAG_SCANNING )) + snapshot->device_flags |=CXL_POISON_FLAG_SCANNING ; + + rc =cxl_poison_snapshot_append (snapshot, po->record, count); +if (rc) +break ; +/* Protect against an uncleared _FLAG_MORE */ - nr_records = nr_records +le16_to_cpu (po->count); + nr_records += count;if (nr_records >= mds->poison.max_errors) {dev_dbg (&cxlmd->dev,"Max Error Records reached: %d\n" , nr_records); +if (snapshot && (po->flags &CXL_POISON_FLAG_MORE )) + snapshot->truncated = true; +break ; } }while (po->flags &CXL_POISON_FLAG_MORE );return rc; } + +int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len, +struct cxl_region *cxlr) +{ +return cxl_mem_get_poison_common (cxlmd, offset, len, cxlr,NULL ); +}EXPORT_SYMBOL_NS_GPL (cxl_mem_get_poison,"CXL" );
Quoted 15 lines
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1558
Quoted 12 lines
drivers/cxl/core/mbox.c +12
-0+int cxl_mem_get_poison_snapshot (struct cxl_memdev *cxlmd,u64 offset,u64 len, +struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot) +{ +if (!snapshot || !snapshot->records || !snapshot->capacity) +return -EINVAL ; + +if (snapshot->nr_records > snapshot->capacity) +return -EOVERFLOW ; + +return cxl_mem_get_poison_common (cxlmd, offset, len, cxlr, snapshot); +}Blank line here
Will fix.
Quoted 17 lines
Quoted 17 lines
drivers/cxl/core/mbox.c +0
-0static void free_poison_buf (void *buf) {kvfree (buf);drivers/cxl/cxlmem.h +6
-0@@ -812,6 +812,31 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds);int cxl_poison_state_init (struct cxl_memdev_state *mds);int cxl_mem_get_poison (struct cxl_memdev *cxlmd,u64 offset,u64 len,struct cxl_region *cxlr); + +/** +* struct cxl_poison_snapshot - Aggregate Get Poison List result +* @records: Copied Media Error Records from all mailbox responses +* @nr_records: Number of valid records stored in @records +* @capacity: Maximum number of records that @records can hold
Quoted 9 lines
drivers/cxl/cxlmem.h int cxl_set_timestamp(struct cxl_memdev_state *mds); · line 826
Quoted 6 lines
drivers/cxl/cxlmem.h +6
-0+* @overflow_ts: Timestamp associated with a reported device overflow +* @device_flags: Persistent device status observed during collection +* @truncated: Host stopped before collecting all available records +*/ +struct cxl_poison_snapshot { +struct cxl_poison_record *records;
__counted_by_ptr()seems likely to apply here.
Yes. I will use __counted_by_ptr(capacity), since capacity is allocated size and nr_records only says how much of it is filled.
Quoted 6 lines
drivers/cxl/cxlmem.h int cxl_set_timestamp(struct cxl_memdev_state *mds); · line 829
Quoted 3 lines
drivers/cxl/cxlmem.h +3
-0+u32 nr_records; +u32 capacity; +u64 overflow_ts;As above. I'd like to ensure this only stashes what we actually need.
Will drop, as above.
Thanks,
Shaikh Kamal
Quoted 14 lines
Quoted 13 lines
drivers/cxl/cxlmem.h +10
-0+u8 device_flags; +bool truncated; +}; + +int cxl_poison_snapshot_init (struct cxl_memdev *cxlmd, +struct cxl_poison_snapshot *snapshot); +void cxl_poison_snapshot_destroy (struct cxl_poison_snapshot *snapshot); +int cxl_mem_get_poison_snapshot (struct cxl_memdev *cxlmd,u64 offset,u64 len, +struct cxl_region *cxlr, +struct cxl_poison_snapshot *snapshot);int cxl_trigger_poison_list (struct cxl_memdev *cxlmd);int cxl_inject_poison (struct cxl_memdev *cxlmd,u64 dpa);int cxl_clear_poison (struct cxl_memdev *cxlmd,u64 dpa);
RFC PATCH 2/5 cxl/region: Factor decoder poison-list retrieval
In reply to Shaikh Kamaluddinpoison_by_decoder() performs two different operations: it queries an unmapped DPA gap preceding an endpoint decoder, then queries the DPA resource assigned to that decoder and its region.
Factor the second operation into cxl_get_poison_by_decoder(). Give the helper an optional snapshot so a later region-probe caller can retain records from exactly one mapped decoder. Keep the existing endpoint walk, skipped-DPA queries, partition-tail queries, -EFAULT handling, and trace behavior unchanged.
--- drivers/cxl/core/region.c | 41 ++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-)
drivers/cxl/core/region.c +36 -5
@@ -2940,6 +2940,40 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,return rc; } +enum cxl_poison_query_policy { +CXL_POISON_QUERY_DIAGNOSTIC , +CXL_POISON_QUERY_PREONLINE , +}; + +static int cxl_get_poison_by_decoder (struct cxl_endpoint_decoder *cxled, +struct cxl_poison_snapshot *snapshot, +enum cxl_poison_query_policy policy) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_dev_state *cxlds = cxlmd->cxlds; +enum cxl_partition_mode mode; +u64 offset, length; +int rc; + +if (!cxled->dpa_res) +return 0 ; + + mode = cxlds->part[cxled->part].mode; + offset = cxled->dpa_res->start; + length =resource_size (cxled->dpa_res); +if (snapshot) + rc =cxl_mem_get_poison_snapshot (cxlmd, offset, length, + cxled->cxld.region, snapshot); +else + rc =cxl_mem_get_poison (cxlmd, offset, length, + cxled->cxld.region); + +if (rc == -EFAULT && mode ==CXL_PARTMODE_RAM && + policy ==CXL_POISON_QUERY_DIAGNOSTIC ) + rc =0 ; +return rc; +} +static int poison_by_decoder (struct device *dev,void *arg) {struct cxl_poison_context *ctx = arg;
@@ -2971,11 +3005,8 @@ static int poison_by_decoder(struct device *dev, void *arg)return rc; } - offset = cxled->dpa_res->start; - length = cxled->dpa_res->end - offset +1 ; - rc =cxl_mem_get_poison (cxlmd, offset, length, cxled->cxld.region); -if (rc == -EFAULT && mode ==CXL_PARTMODE_RAM ) - rc =0 ; + rc =cxl_get_poison_by_decoder (cxled,NULL , +CXL_POISON_QUERY_DIAGNOSTIC );if (rc)return rc;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [RFC PATCH 2/5] cxl/region: Factor decoder poison-list retrieval
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] cxl/region: Out-of-bounds read on cxlds->part when cxled->part is -1 --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] cxl/region: Out-of-bounds read on cxlds->part when cxled->part is -1 --
commit 15ab3ed0729f2bd36a81eed918520197e4a18f3a
Author: Shaikh Kamaluddin <[email protected]>
cxl/region: Factor decoder poison-list retrieval
poison_by_decoder() performs two different operations: it queries an unmapped DPA gap preceding an endpoint decoder, then queries the DPA resource assigned to that decoder and its region.
Quoted 17 lines
drivers/cxl/core/region.c +9
-0@@ -2940,6 +2940,40 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,return rc; } +enum cxl_poison_query_policy { +CXL_POISON_QUERY_DIAGNOSTIC , +CXL_POISON_QUERY_PREONLINE , +}; + +static int cxl_get_poison_by_decoder (struct cxl_endpoint_decoder *cxled, +struct cxl_poison_snapshot *snapshot, +enum cxl_poison_query_policy policy) +{
drivers/cxl/core/region.c static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd, · line 2961
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_dev_state *cxlds = cxlmd->cxlds; +enum cxl_partition_mode mode; +u64 offset, length; +int rc; + +if (!cxled->dpa_res) +return 0 ; + + mode = cxlds->part[cxled->part].mode;
[Severity: Medium]
Does this code read out of bounds when cxled->part is -1?
If a CXL device's hardware decoder configuration reserves a DPA range that does not map to any valid partition, __cxl_dpa_reserve() gracefully warns but returns success, leaving cxled->part = -1:
drivers/cxl/core/hdm.c:__cxl_dpa_reserve() {
if (cxled->part < 0)
for (int i = 0; i < cxlds->nr_partitions; i++)
if (resource_contains(&cxlds->part[i].res, res)) {
cxled->part = i;
break;
}
if (cxled->part < 0)
dev_warn(dev, "decoder%d.%d: %pr does not map any partition\n",
port->id, cxled->cxld.id, res);
port->hdm_end++;
get_device(&cxled->cxld.dev);
return 0;
}Later, when iterating over decoders, it skips the !cxled->dpa_res check and unconditionally executes mode = cxlds->part[cxled->part].mode.
Since part array is embedded inside struct cxl_dev_state right after dpa_res, part[-1] reads the tail padding or end of dpa_res within the same memory allocation. This uses garbage data for the mode variable, which can improperly mask -EFAULT errors if it randomly matches CXL_PARTMODE_RAM.
Note that the pre-existing caller logic in poison_by_decoder() also retains this out-of-bounds array access before invoking this new helper.
Quoted 15 lines
drivers/cxl/core/region.c +15
-0+ offset = cxled->dpa_res->start; + length =resource_size (cxled->dpa_res); +if (snapshot) + rc =cxl_mem_get_poison_snapshot (cxlmd, offset, length, + cxled->cxld.region, snapshot); +else + rc =cxl_mem_get_poison (cxlmd, offset, length, + cxled->cxld.region); + +if (rc == -EFAULT && mode ==CXL_PARTMODE_RAM && + policy ==CXL_POISON_QUERY_DIAGNOSTIC ) + rc =0 ; +return rc; +} +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Jonathan Cameron
maintainerRe: [RFC PATCH 2/5] cxl/region: Factor decoder poison-list retrieval
In reply to Shaikh KamaluddinQuoted 20 lines · On Sat, 10 Oct 2026 21:50:10 +0530 Shaikh Kamaluddin…
poison_by_decoder()performs two different operations: it queries an unmapped DPA gap preceding an endpoint decoder, then queries the DPA resource assigned to that decoder and its region.Factor the second operation into
cxl_get_poison_by_decoder(). Give the helper an optional snapshot so a later region-probe caller can retain records from exactly one mapped decoder. Keep the existing endpoint walk, skipped-DPA queries, partition-tail queries,-EFAULThandling, and trace behavior unchanged.Signed-off-by Shaikh Kamaluddin <[email protected]>--- drivers/cxl/core/region.c | 41 ++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-)drivers/cxl/core/region.c +0
-0
drivers/cxl/core/region.c static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd, · line 2945
Quoted 6 lines
drivers/cxl/core/region.c +3
-0@@ -2940,6 +2940,40 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,return rc; } +enum cxl_poison_query_policy { +CXL_POISON_QUERY_DIAGNOSTIC , +CXL_POISON_QUERY_PREONLINE ,
For now, perhaps focus on flags defining what you want done, not when the query is happening.
Quoted 17 lines
drivers/cxl/core/region.c +17
-0+}; + +static int cxl_get_poison_by_decoder (struct cxl_endpoint_decoder *cxled, +struct cxl_poison_snapshot *snapshot, +enum cxl_poison_query_policy policy) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_dev_state *cxlds = cxlmd->cxlds; +enum cxl_partition_mode mode; +u64 offset, length; +int rc; + +if (!cxled->dpa_res) +return 0 ; + + mode = cxlds->part[cxled->part].mode; + offset = cxled->dpa_res->start;
drivers/cxl/core/region.c static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd, · line 2972
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+ length =resource_size (cxled->dpa_res); +if (snapshot) + rc =cxl_mem_get_poison_snapshot (cxlmd, offset, length, + cxled->cxld.region, snapshot); +else + rc =cxl_mem_get_poison (cxlmd, offset, length, + cxled->cxld.region); + +if (rc == -EFAULT && mode ==CXL_PARTMODE_RAM && + policy ==CXL_POISON_QUERY_DIAGNOSTIC )
This is becoming odd enough I'd add some comments saying why we are eating the error return.
Quoted 21 lines
drivers/cxl/core/region.c +6
-5+ rc =0 ; +return rc; +} +static int poison_by_decoder (struct device *dev,void *arg) {struct cxl_poison_context *ctx = arg;@@ -2971,11 +3005,8 @@ static int poison_by_decoder(struct device *dev, void *arg)return rc; } - offset = cxled->dpa_res->start; - length = cxled->dpa_res->end - offset +1 ; - rc =cxl_mem_get_poison (cxlmd, offset, length, cxled->cxld.region); -if (rc == -EFAULT && mode ==CXL_PARTMODE_RAM ) - rc =0 ; + rc =cxl_get_poison_by_decoder (cxled,NULL , +CXL_POISON_QUERY_DIAGNOSTIC );if (rc)return rc;
Re: [RFC PATCH 2/5] cxl/region: Factor decoder poison-list retrieval
Quoted 18 lines · Jonathan Cameron
Quoted 15 lines · On Sat, 10 Oct 2026 21:50:10 +0530 Shaikh Kamaluddin…
poison_by_decoder()performs two different operations: it queries an unmapped DPA gap preceding an endpoint decoder, then queries the DPA resource assigned to that decoder and its region.Factor the second operation into
cxl_get_poison_by_decoder(). Give the helper an optional snapshot so a later region-probe caller can retain records from exactly one mapped decoder. Keep the existing endpoint walk, skipped-DPA queries, partition-tail queries,-EFAULThandling, and trace behavior unchanged.Signed-off-by Shaikh Kamaluddin <[email protected]>--- drivers/cxl/core/region.c | 41 ++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-)
Quoted 8 lines
drivers/cxl/core/region.c static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd, · line 2942
Quoted 8 lines
drivers/cxl/core/region.c +0
-0@@ -2940,6 +2940,40 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,return rc; }
Hi Jonathan,
Quoted 7 lines
drivers/cxl/core/region.c static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd, · line 2945
Quoted 3 lines
drivers/cxl/core/region.c +3
-0+enum cxl_poison_query_policy { +CXL_POISON_QUERY_DIAGNOSTIC , +CXL_POISON_QUERY_PREONLINE ,For now, perhaps focus on flags defining what you want done, not when the query is happening.
Make sense. I will drop the enum. The snapshot argument already tells the helper what to do: NULL for trace path, a snapshot for region probe. The helpler will return the error unchanged and each caller will handle -EFAULT itself, as poison_by_decoder() does today.
Quoted 22 lines
Quoted 22 lines
drivers/cxl/core/region.c +22
-0+}; + +static int cxl_get_poison_by_decoder (struct cxl_endpoint_decoder *cxled, +struct cxl_poison_snapshot *snapshot, +enum cxl_poison_query_policy policy) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_dev_state *cxlds = cxlmd->cxlds; +enum cxl_partition_mode mode; +u64 offset, length; +int rc; + +if (!cxled->dpa_res) +return 0 ; + + mode = cxlds->part[cxled->part].mode; + offset = cxled->dpa_res->start; + length =resource_size (cxled->dpa_res); +if (snapshot) + rc =cxl_mem_get_poison_snapshot (cxlmd, offset, length, + cxled->cxld.region, snapshot); +else
Quoted 9 lines
drivers/cxl/core/region.c static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd, · line 2972
Quoted 5 lines
drivers/cxl/core/region.c +5
-0+ rc =cxl_mem_get_poison (cxlmd, offset, length, + cxled->cxld.region); + +if (rc == -EFAULT && mode ==CXL_PARTMODE_RAM && + policy ==CXL_POISON_QUERY_DIAGNOSTIC )This is becoming odd enough I'd add some comments saying why we are eating the error return.
Will add one. A device that keeps no poison list for volatile ranges returns Invalid Physical address for them (CXL r3.2 8.2.9.8.4.1) which comes back as -EFAULT. The trace path treats that as "no records" instead of failing the whole walk.
Quoted 22 lines
Quoted 21 lines
drivers/cxl/core/region.c +6
-5+ rc =0 ; +return rc; +} +static int poison_by_decoder (struct device *dev,void *arg) {struct cxl_poison_context *ctx = arg;@@ -2971,11 +3005,8 @@ static int poison_by_decoder(struct device *dev, void *arg)return rc; } - offset = cxled->dpa_res->start; - length = cxled->dpa_res->end - offset +1 ; - rc =cxl_mem_get_poison (cxlmd, offset, length, cxled->cxld.region); -if (rc == -EFAULT && mode ==CXL_PARTMODE_RAM ) - rc =0 ; + rc =cxl_get_poison_by_decoder (cxled,NULL , +CXL_POISON_QUERY_DIAGNOSTIC );if (rc)return rc;
RFC PATCH 3/5 mm/memory-failure: Add a pre-online poison registry
In reply to Shaikh KamaluddinA frame can be known bad before its backing memory is onlined. A CXL Type-3 device retains poison as DPA-based Media Error Records and can report those records through GET_POISON_LIST after a reboot, kexec, or device hotplug.
CXL poison can be retrieved while a region is being prepared, before dax/kmem calls add_memory_driver_managed() for the range. At that point there is no initialized struct page on which memory_failure() can operate. The poison information must therefore be retained until the corresponding pages pass through memory initialization.
Add a registry of sorted, nonoverlapping poisoned PFN ranges. Each registration represents one owner and its independently managed memory. For example, a CXL region registers the poisoned PFN ranges translated from its device poison records and receives an opaque handle for that range set. A second CXL region receives a separate handle for its own ranges.
The owner uses its handle to remove exactly its registration after the associated memory can no longer enter the allocator. Removing one CXL region's registration does not affect poison ranges registered by another region.
Keep each registered range set immutable and protect the registration list with RCU. This keeps allocator-side lookups lockless while allowing a registration to be removed safely after an RCU grace period. A later patch will consult the registry before initialized pages are released to the buddy allocator.
--- include/linux/memory-failure.h | 92 +++++++++++++++++ mm/memory-failure.c | 175 +++++++++++++++++++++++++++++++++ 2 files changed, 267 insertions(+)
include/linux/memory-failure.h +92 -0
@@ -11,9 +11,73 @@ struct pfn_address_space { unsigned long pfn, pgoff_t *pgoff); }; +struct preonline_hwpoison ; + +/** +* struct preonline_hwpoison_range - Pre-online poisoned PFN range +* @start_pfn: First PFN in the range +* @nr_pages: Number of consecutive PFNs in the range +*/ +struct preonline_hwpoison_range { +unsigned long start_pfn; +unsigned long nr_pages; +}; +#ifdef CONFIG_MEMORY_FAILURE int register_pfn_address_space (struct pfn_address_space *pfn_space);void unregister_pfn_address_space (struct pfn_address_space *pfn_space); + +/** +* preonline_hwpoison_register - Register poison before memory is onlined +* @ranges: Sorted, nonoverlapping PFN ranges in ascending start PFN order. +* Adjacent ranges are accepted, but callers should coalesce them. +* The array is copied and need not outlive this call. +* @nr_ranges: Number of entries in @ranges. Must be nonzero. +* @handle: Registration handle returned on success. Set to NULL on failure. +* +* Register PFN ranges that must be withheld when their backing memory is +* initialized. Pass the returned handle to +* preonline_hwpoison_unregister() only after the memory can no longer enter +* the allocator. +* +* Return: 0 on success, -EINVAL if @handle is NULL or @ranges is empty, +* unsorted, overlapping, or contains a zero-length entry, -EOVERFLOW on PFN +* wraparound, and -ENOMEM on allocation failure. +*/ +int preonline_hwpoison_register (const struct preonline_hwpoison_range *ranges, +unsigned int nr_ranges, +struct preonline_hwpoison **handle); + +/** +* preonline_hwpoison_unregister - Remove a pre-online poison registration +* @handle: Handle returned by preonline_hwpoison_register(), or NULL +* +* Remove @handle from future pre-online poison lookups. The caller must first +* ensure that the associated memory can no longer be initialized or handed +* to the page allocator. +* +* Pages already marked as hwpoison remain marked. +*/ +void preonline_hwpoison_unregister (struct preonline_hwpoison *handle); + +/** +* preonline_hwpoison_intersects - Does a PFN range cover a known-bad frame? +* @start_pfn: First PFN of the range to test +* @nr_pages: Length of the range in frames +* +* Return: %true if any registered range overlaps [@start_pfn, @start_pfn + +* @nr_pages), %false otherwise. +*/ +bool preonline_hwpoison_intersects (unsigned long start_pfn, +unsigned long nr_pages); + +/** +* preonline_hwpoison_contains - Is a single frame known bad? +* @pfn: Frame to test +* +* Return: %true if @pfn falls in a registered range. +*/ +bool preonline_hwpoison_contains (unsigned long pfn);#else static inline int register_pfn_address_space (struct pfn_address_space *pfn_space) {
@@ -23,6 +87,34 @@ static inline int register_pfn_address_space(struct pfn_address_space *pfn_spacestatic inline void unregister_pfn_address_space (struct pfn_address_space *pfn_space) { } + +static inline int +preonline_hwpoison_register (const struct preonline_hwpoison_range *ranges, +unsigned int nr_ranges, +struct preonline_hwpoison **handle) +{ +if (handle) + *handle =NULL ; + +return -EOPNOTSUPP ; +} + +static inline void +preonline_hwpoison_unregister (struct preonline_hwpoison *handle) +{ +} + +static inline bool preonline_hwpoison_intersects (unsigned long start_pfn, +unsigned long nr_pages) +{ +return false; +} + +static inline bool preonline_hwpoison_contains (unsigned long pfn) +{ +return false; +} +#endif /* CONFIG_MEMORY_FAILURE */ #endif /* _LINUX_MEMORY_FAILURE_H */
mm/memory-failure.c +175 -0
181 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [RFC PATCH 3/5] mm/memory-failure: Add a pre-online poison registry
Quoted 23 lines · Shaikh Kamaluddin
A frame can be known bad before its backing memory is onlined. A CXL Type-3 device retains poison as DPA-based Media Error Records and can report those records through
GET_POISON_LISTafter a reboot, kexec, or device hotplug.CXL poison can be retrieved while a region is being prepared, before dax/kmem calls
add_memory_driver_managed()for the range. At that point there is no initialized struct page on whichmemory_failure()can operate. The poison information must therefore be retained until the corresponding pages pass through memory initialization.Add a registry of sorted, nonoverlapping poisoned PFN ranges. Each registration represents one owner and its independently managed memory. For example, a CXL region registers the poisoned PFN ranges translated from its device poison records and receives an opaque handle for that range set. A second CXL region receives a separate handle for its own ranges.
The owner uses its handle to remove exactly its registration after the associated memory can no longer enter the allocator. Removing one CXL region's registration does not affect poison ranges registered by another region.
Quoted 8 lines
Keep each registered range set immutable and protect the registration list with RCU. This keeps allocator-side lookups lockless while allowing a registration to be removed safely after an RCU grace period. A later patch will consult the registry before initialized pages are released to the buddy allocator.
Signed-off-by Shaikh Kamaluddin <[email protected]>---
Oh no, another source of poison information. That doesn't sound like a good design.
Signature · 3 lines
Cheers, David
RFC PATCH 4/5 mm/page_alloc: Keep pre-online poison out of the buddy allocator
In reply to Shaikh KamaluddinThe inherited-poison path checks initialized blocks before releasing them to the buddy allocator. CXL device poison discovered before hot-added memory is onlined is recorded separately in the pre-online poison registry.
Extend __free_pages_core() to send a block to free_poisoned_block() when it intersects either poison source. In free_poisoned_block(), withhold a page when its PFN is registered as pre-online poison or its physical address is present in the inherited-poison table.
Pages identified by either source continue through hwpoison_boot_page(), while clean pages are released through accept_and_free_block().
--- mm/page_alloc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
mm/page_alloc.c +6 -2
@@ -33,6 +33,7 @@#include <linux/cpuset.h> #include <linux/folio_batch.h> #include <linux/memory_hotplug.h> +#include <linux/memory-failure.h> #include <linux/nodemask.h> #include <linux/vmstat.h> #include <linux/fault-inject.h>
@@ -1601,12 +1602,14 @@ static void __meminit free_poisoned_block(struct page *page, unsigned int order, enum meminit_context context) {unsigned long i, nr_pages =1UL << order; +unsigned long pfn =page_to_pfn (page);for (i =0 ; i < nr_pages; i++) {struct page *p = page + i;phys_addr_t phys =page_to_phys (p); -if (range_contains_poisoned_memory (phys,PAGE_SIZE )) { +if (range_contains_poisoned_memory (phys,PAGE_SIZE ) || +preonline_hwpoison_contains (pfn + i)) {hwpoison_boot_page (p, context);continue ; }
@@ -1652,7 +1655,8 @@ void __meminit __free_pages_core(struct page *page, unsigned int order,/* First: a block parked by __free_unaccepted() never returns here. */ if (range_contains_poisoned_memory (page_to_phys (page), -PAGE_SIZE << order)) { +PAGE_SIZE << order) || +preonline_hwpoison_intersects (page_to_pfn (page), nr_pages)) {free_poisoned_block (page, order, context);return ; }
Signature · 1 line
2.43.0
Re: [RFC PATCH 4/5] mm/page_alloc: Keep pre-online poison out of the buddy allocator
Quoted 60 lines · Shaikh Kamaluddin
The inherited-poison path checks initialized blocks before releasing them to the buddy allocator. CXL device poison discovered before hot-added memory is onlined is recorded separately in the pre-online poison registry.
Extend
__free_pages_core()to send a block tofree_poisoned_block()when it intersects either poison source. Infree_poisoned_block(), withhold a page when its PFN is registered as pre-online poison or its physical address is present in the inherited-poison table.Pages identified by either source continue through
hwpoison_boot_page(), while clean pages are released throughaccept_and_free_block().Signed-off-by Shaikh Kamaluddin <[email protected]>--- mm/page_alloc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)mm/page_alloc.c +6
-2@@ -33,6 +33,7 @@#include <linux/cpuset.h> #include <linux/folio_batch.h> #include <linux/memory_hotplug.h> +#include <linux/memory-failure.h> #include <linux/nodemask.h> #include <linux/vmstat.h> #include <linux/fault-inject.h> @@ -1601,12 +1602,14 @@ static void __meminit free_poisoned_block(struct page *page, unsigned int order, enum meminit_context context) {unsigned long i, nr_pages =1UL << order; +unsigned long pfn =page_to_pfn (page);for (i =0 ; i < nr_pages; i++) {struct page *p = page + i;phys_addr_t phys =page_to_phys (p); -if (range_contains_poisoned_memory (phys,PAGE_SIZE )) { +if (range_contains_poisoned_memory (phys,PAGE_SIZE ) || +preonline_hwpoison_contains (pfn + i)) {hwpoison_boot_page (p, context);continue ; }@@ -1652,7 +1655,8 @@ void __meminit __free_pages_core(struct page *page, unsigned int order,/* First: a block parked by __free_unaccepted() never returns here. */ if (range_contains_poisoned_memory (page_to_phys (page), -PAGE_SIZE << order)) { +PAGE_SIZE << order) || +preonline_hwpoison_intersects (page_to_pfn (page), nr_pages)) {free_poisoned_block (page, order, context);return ; }Signature · 1 line
2.43.0
Hi Shaikh,
I think the idea of discovering device poison first is good, but the boundary should be more strictly designed.
Now if I don't get it wrong, if we turn on memory_hotplug.memmap_on_memory, we can have part of CXL memory to do the bookkeeping for itself, without using the existing RAM.
What if that bookkeeping part is poisoned ? the check ignores this situation right ?
What about draw a clear line between normal RAM and hot-add CXL memory, have the latter gone through the thorough check and do the rest you want ?
Best regards,
Richard Cheng.
Re: [RFC PATCH 4/5] mm/page_alloc: Keep pre-online poison out of the buddy allocator
Quoted 64 lines · Richard Cheng
Quoted 60 lines · Shaikh Kamaluddin
The inherited-poison path checks initialized blocks before releasing them to the buddy allocator. CXL device poison discovered before hot-added memory is onlined is recorded separately in the pre-online poison registry.
Extend
__free_pages_core()to send a block tofree_poisoned_block()when it intersects either poison source. Infree_poisoned_block(), withhold a page when its PFN is registered as pre-online poison or its physical address is present in the inherited-poison table.Pages identified by either source continue through
hwpoison_boot_page(), while clean pages are released throughaccept_and_free_block().Signed-off-by Shaikh Kamaluddin <[email protected]>--- mm/page_alloc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)mm/page_alloc.c +6
-2@@ -33,6 +33,7 @@#include <linux/cpuset.h> #include <linux/folio_batch.h> #include <linux/memory_hotplug.h> +#include <linux/memory-failure.h> #include <linux/nodemask.h> #include <linux/vmstat.h> #include <linux/fault-inject.h> @@ -1601,12 +1602,14 @@ static void __meminit free_poisoned_block(struct page *page, unsigned int order, enum meminit_context context) {unsigned long i, nr_pages =1UL << order; +unsigned long pfn =page_to_pfn (page);for (i =0 ; i < nr_pages; i++) {struct page *p = page + i;phys_addr_t phys =page_to_phys (p); -if (range_contains_poisoned_memory (phys,PAGE_SIZE )) { +if (range_contains_poisoned_memory (phys,PAGE_SIZE ) || +preonline_hwpoison_contains (pfn + i)) {hwpoison_boot_page (p, context);continue ; }@@ -1652,7 +1655,8 @@ void __meminit __free_pages_core(struct page *page, unsigned int order,/* First: a block parked by __free_unaccepted() never returns here. */ if (range_contains_poisoned_memory (page_to_phys (page), -PAGE_SIZE << order)) { +PAGE_SIZE << order) || +preonline_hwpoison_intersects (page_to_pfn (page), nr_pages)) {free_poisoned_block (page, order, context);return ; }Signature · 1 line
2.43.0Hi Shaikh,
Quoted 3 lines
I think the idea of discovering device poison first is good, but the boundary should be more strictly designed.
Thanks for the review, Richard.
Quoted 6 lines
Now if I don't get it wrong, if we turn on memory_hotplug.memmap_on_memory, we can have part of CXL memory to do the bookkeeping for itself, without using the existing RAM.
What if that bookkeeping part is poisoned ? the check ignores this situation right ?
Yes, you are right. With memmap_on_memory, each 128 MiB block's struct pages are placed in its first 2 MiB (0x200 pages) by vmemmap_populate() when the memory is added, and memory_block_online() then onlines only start_pfn + 0x200 onwards. Those vmemmap pages never reach __free_pages_core(), so the registry is never consulted for them.
I reproduced it in QEMU 9.1 (TCG) on next-20260908 + [1] v5 + this RFC, with two Internal poison records in the first block: DPA 0x100000 (PFN 0x190100, inside the vmemmap area 0x190000-0x1901ff) and DPA 0x400000 (PFN 0x190400, outside it). Both are registered, but only 0x190400 is withheld.
Steps:
1. Config: CONFIG_SPARSEMEM_VMEMMAP=y, CONFIG_MHP_MEMMAP_ON_MEMORY=y.
2. Boot with memmap_on_memory, without auto-online:
vng --disable-kvm --run ./arch/x86/boot/bzImage \
--disable-microvm --memory 4G --cpus 4 \
--append "memory_hotplug.memmap_on_memory=1" \
--qemu-opts="-machine q35,cxl=on \
-object memory-backend-ram,id=vmem0,share=on,size=512M \
-device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
-device cxl-rp,port=0,bus=cxl.1,id=rp0,chassis=0,slot=2 \
-device cxl-type3,bus=rp0,volatile-memdev=vmem0,id=cxl-vmem0 \
-M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G \
-qmp unix:/tmp/qmp.sock,server=on,wait=off"# cat /sys/module/memory_hotplug/parameters/memmap_on_memory Y
3. Inject Internal poison over QMP (host):
{ "execute": "cxl-inject-poison",
"arguments": { "path": "/machine/peripheral/cxl-vmem0",
"start": 1048576, "length": 64 } }
{ "execute": "cxl-inject-poison",
"arguments": { "path": "/machine/peripheral/cxl-vmem0",
"start": 4194304, "length": 64 } }4. Create the region with kmem not yet bound, then enable memmap_on_memory on the dax device:
# grep MemTotal /proc/meminfo MemTotal: 3999996 kB # echo 0 > /sys/bus/dax/drivers_autoprobe # cxl create-region -m -t ram -d decoder0.0 -w 1 mem0 cxl_region region0: withholding 2 frame(s) reported poisoned by the device # echo 1 > /sys/bus/dax/devices/dax0.0/memmap_on_memory
5. Add and online the memory:
# daxctl reconfigure-device --mode=system-ram dax0.0 # lsmem 0x0000000190000000-0x00000001afffffff 512M online yes 50-53
6. Result:
# grep -E 'MemTotal|HardwareCorrupted' /proc/meminfo MemTotal: 4516092 kB HardwareCorrupted: 4 kB # tools/mm/page-types -a 0x190000+0x20000 -b hwpoison -l offset len flags 190400 1 ___________________X_______________________
MemTotal grows by 504 MiB rather than 512 MiB (4 blocks x 2 MiB of memmap), so memmap_on_memory was in effect. Only 0x190400 is hwpoisoned; 0x190100 holds struct pages.
Quoted 16 lines
What about draw a clear line between normal RAM and hot-add CXL memory, have the latter gone through the thorough check and do the rest you want ?
Best regards,
Richard Cheng.
Re: [RFC PATCH 4/5] mm/page_alloc: Keep pre-online poison out of the buddy allocator
In reply to Richard ChengQuoted 71 lines · Richard Cheng
Quoted 60 lines · Shaikh Kamaluddin
The inherited-poison path checks initialized blocks before releasing them to the buddy allocator. CXL device poison discovered before hot-added memory is onlined is recorded separately in the pre-online poison registry.
Extend
__free_pages_core()to send a block tofree_poisoned_block()when it intersects either poison source. Infree_poisoned_block(), withhold a page when its PFN is registered as pre-online poison or its physical address is present in the inherited-poison table.Pages identified by either source continue through
hwpoison_boot_page(), while clean pages are released throughaccept_and_free_block().Signed-off-by Shaikh Kamaluddin <[email protected]>--- mm/page_alloc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)mm/page_alloc.c +6
-2@@ -33,6 +33,7 @@#include <linux/cpuset.h> #include <linux/folio_batch.h> #include <linux/memory_hotplug.h> +#include <linux/memory-failure.h> #include <linux/nodemask.h> #include <linux/vmstat.h> #include <linux/fault-inject.h> @@ -1601,12 +1602,14 @@ static void __meminit free_poisoned_block(struct page *page, unsigned int order, enum meminit_context context) {unsigned long i, nr_pages =1UL << order; +unsigned long pfn =page_to_pfn (page);for (i =0 ; i < nr_pages; i++) {struct page *p = page + i;phys_addr_t phys =page_to_phys (p); -if (range_contains_poisoned_memory (phys,PAGE_SIZE )) { +if (range_contains_poisoned_memory (phys,PAGE_SIZE ) || +preonline_hwpoison_contains (pfn + i)) {hwpoison_boot_page (p, context);continue ; }@@ -1652,7 +1655,8 @@ void __meminit __free_pages_core(struct page *page, unsigned int order,/* First: a block parked by __free_unaccepted() never returns here. */ if (range_contains_poisoned_memory (page_to_phys (page), -PAGE_SIZE << order)) { +PAGE_SIZE << order) || +preonline_hwpoison_intersects (page_to_pfn (page), nr_pages)) {free_poisoned_block (page, order, context);return ; }Signature · 1 line
2.43.0Hi Shaikh,
I think the idea of discovering device poison first is good, but the boundary should be more strictly designed.
Now if I don't get it wrong, if we turn on memory_hotplug.memmap_on_memory, we can have part of CXL memory to do the bookkeeping for itself, without using the existing RAM.
Quoted 6 lines
What if that bookkeeping part is poisoned ? the check ignores this situation right ?
What about draw a clear line between normal RAM and hot-add CXL memory, have the latter gone through the thorough check and do the rest you want ?
Hi Richard,
In my earlier reply I focused on reproducing the issue you raised. This is the follow-up on your last question: I traced where 0x190100 ends up, and it also shows where I think the line should be drawn.
1. Layout of the first block
----------------------------
x86-64, 4 KiB pages, 128 MiB memory blocks, sizeof(struct page) = 64 (pahole -C page : "size: 64").
block PFNs 0x190000000 >> 12 = 0x190000, 0x8000 pages
-> PFN 0x190000 - 0x197fff (128 MiB)
memmap size 0x8000 * 64 = 0x200000 bytes (2 MiB)
memmap frames PFN_UP(0x200000) = (0x200000 + 0xfff) >> 12 = 0x200
pageblock = 0x200 pages (order 9), already aligned,
so no padding: altmap->free = 0x200
(memory_block_memmap_on_memory_pages())The pageblock rounding is from 2d1f649c7c08 ("mm/memory_hotplug:
support memmap_on_memory when memmap is not aligned to pageblocks"):
online/offline need pageblock-aligned ranges, so the memmap area is padded to a whole pageblock and online_pages() starts on a pageblock boundary. Here 0x200 already is one pageblock, so free equals the memmap size; on 64K-page ppc64 with 256 MiB blocks it would be 32 frames for a 4-frame memmap.
altmap .base_pfn = 0x190000, .free = 0x200
(create_altmaps_and_memory_blocks())
-> memmap stored in PFN 0x190000 - 0x1901ffWith CONFIG_SPARSEMEM_VMEMMAP, pfn_to_page(pfn) = vmemmap_base + pfn, so
struct page VA of pfn = vmemmap_base + pfn * 64 pfn of a struct page = (VA - vmemmap_base) / 64
The block's struct pages are one 2 MiB array, 64 bytes per page, stored in PFN order in its first 0x200 frames:
offset: 0x0 0x40 ... 0xfc0 0x1000 ... 0x100000
entry: 0x190000 0x190001 ... 0x19003f 0x190040 ... 0x194000
|<-- frame 0x190000 (64 entries) ->| |<- frame 0x190100Frame 0x190100 starts at offset 0x100 * 4096 = 0x100000, i.e. entry 0x100000 / 64 = 0x4000. So the registered frame holds the struct pages of PFN 0x194000 - 0x19403f.
2. Trace
--------
kprobes with stacktrace triggers:
p:poison/vmemmap vmemmap_populate start=$arg1:x64 altmap=$arg4:x64 vbase=@vmemmap_base:x64
p:poison/memmap_init mhp_init_memmap_on_memory pfn=$arg1:x64 nr=$arg2:x64
p:poison/online online_pages pfn=$arg1:x64 nr=$arg2:x64
p:poison/hwpoison hwpoison_boot_page page=$arg1:x64
13 events, each with a stack, none lost. vmemmap_base was 0xffc4f4aa80000000.
Add (daxctl binds kmem; one __add_memory_driver_managed() for the whole range, one vmemmap_populate() per block):
725.066915 vmemmap_populate start=0xffc4f4aa86400000 altmap=0xff4a93014354df80 725.084123 vmemmap_populate start=0xffc4f4aa86600000 altmap=0xff4a9301434b8080 725.086550 vmemmap_populate start=0xffc4f4aa86800000 altmap=0xff4a9301421dafc0 725.090221 vmemmap_populate start=0xffc4f4aa86a00000 altmap=0xff4a93014340bd40
start = pfn_to_page(block start), from __populate_section_memmap():
(0x86400000 - 0x80000000) / 64 = 0x190000
(0x86600000 - 0x80000000) / 64 = 0x198000
(0x86800000 - 0x80000000) / 64 = 0x1a0000
(0x86a00000 - 0x80000000) / 64 = 0x1a8000 Consecutive starts are 0x200000 apart: one block's memmap.
altmap is a pointer to the per-block struct vmem_altmap (kmemdup()ed, hence four different direct-map addresses). Non-NULL means the memmap is allocated from the block: for the first block, PFN 0x190000 - 0x1901ff, including 0x190100.
Online (daxctl writes memoryN/state; memory_block_online() inlined into memory_block_change_state()):
725.214535 mhp_init_memmap_on_memory pfn=0x190000 nr=0x200
725.231165 online_pages pfn=0x190200 nr=0x7e00
725.658068 hwpoison_boot_page page=0xffc4f4aa86410000
(0x86410000 - 0x80000000) / 64 = 0x190400
stack: online_pages -> __free_pages_core -> hwpoison_boot_page The other three blocks show the same memmap_init/online pair (pfn base and base + 0x200) and no hwpoison event.
The two online events are memory_block_online() (drivers/base/memory.c) splitting the block at altmap->free:
unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
/* 0x190000 */
unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
/* 0x8000 */
unsigned long nr_vmemmap_pages = 0;
...
if (mem->altmap)
nr_vmemmap_pages = mem->altmap->free; /* 0x200 */
...
if (nr_vmemmap_pages) {
ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages,
zone);
/* 0x190000, 0x200 */
...
}
ret = online_pages(start_pfn + nr_vmemmap_pages,
nr_pages - nr_vmemmap_pages, zone, mem->group);
/* 0x190200, 0x7e00 */
0x1901FF 0x190200
block 0x190000 |---------------------|------------------------------------|0x197fff
|<----- altmap->free = 0x200 ->|<---- 0x8000 - 0x200 = 0x7e00 ----->| part 1 mhp_init_memmap_on_memory() (0x190100 here), Its range [0x190000 ~ 0x1901FF]
part 2 online_pages() (0x190400 here), its range [0x190200 ~ 0x197FFF]
The trace values pfn=0x190000 nr=0x200 and pfn=0x190200 nr=0x7e00 are these two expressions evaluated. mhp_init_memmap_on_memory() only moves part 1 into the zone and marks it PageVmemmapSelfHosted; it never calls online_page_callback or __free_pages_core(), since those frames hold live struct pages. Only part 2 goes through online_pages_range() -> generic_online_page() -> __free_pages_core().
3. Why 0x190100 is missed and 0x190400 is not
---------------------------------------------
PFN 0x190000 +--------------------------------+
| memmap, altmap->free = 0x200 | vmemmap_populate() at add,
0x190100 | X registered, NOT withheld | mhp_init_memmap_on_memory()
| | at online: never freed
0x1901ff +--------------------------------+
0x190200 +--------------------------------+
| online_pages(), 0x7e00 pages | [online_pages_range]
0x190400 | X registered, withheld | [generic_online_page]
| | __free_pages_core() <- RFC
0x197fff +--------------------------------+ hwpoison_boot_page()0x190100 < base + altmap->free. It is taken for struct pages in __add_pages() (725.07s), before any online code runs (725.21s), and at online it is excluded from the range online_pages() frees. It never reaches __free_pages_core(), where the RFC checks.
0x190400 >= base + altmap->free. It is in the freed range, reaches __free_pages_core(), matches the registry and is withheld.
No check at online can catch the first case: the decision is made when the altmap is chosen.
The full reproduction (memmap_on_memory on , every command, and the complete stacks) is at the end .
4. A line between normal RAM and hot-added CXL memory
-----------------------------------------------------
The two already reach the allocator by different paths; the RFC put its check at the one point they share:
boot RAM hot-added CXL (dax/kmem)
-------- ------------------------
sparse_init() [create_altmaps_and_memory_blocks] <----- A
memmap from memblock arch_add_memory()
memmap: altmap or regular
memblock_free_pages() online_pages()
deferred_free_pages() [generic_online_page] <----- B
| |
| |
'----- __free_pages_core() -------' <- RFC v1 check (shared)For v2 I plan to move everything for hot-added memory to the right-hand side, in mm/memory_hotplug.c, and leave __free_pages_core() and boot RAM alone.
A. Add time, in create_altmaps_and_memory_blocks(), before
arch_add_memory(). If a block's [base_pfn, base_pfn + free) range intersects a registered range, add that block without an altmap. Sketch:
if (preonline_hwpoison_intersects(mhp_altmap.base_pfn,
mhp_altmap.free)) {
params.altmap = NULL;
} else {
params.altmap = kmemdup(&mhp_altmap, sizeof(mhp_altmap),
GFP_KERNEL);
if (!params.altmap) {
ret = -ENOMEM;
goto out;
}
} That block's memmap then comes from regular memory, memory_block_online() sees no altmap and onlines the whole block, and the bad frame is an ordinary page that reaches B. Other blocks keep memmap_on_memory. The check covers the whole altmap->free range, including the pageblock padding added in "force" mode by 2d1f649c7c08, since all of it is kept out of online_pages(). This is the check that is missing today.
A also sits right where 2d1f649c7c08 decides how many frames each block gives up (memory_block_memmap_on_memory_pages() feeding mhp_altmap.free), so the range it checks is exactly the range memory_block_online() later excludes.
B. Online time, in generic_online_page(). Free the good pages and keep
the registered ones out of the buddy:
void generic_online_page(struct page *page, unsigned int order)
{
unsigned long pfn = page_to_pfn(page);
unsigned long i, nr = 1UL << order;
if (!preonline_hwpoison_intersects(pfn, nr)) {
__free_pages_core(page, order, MEMINIT_HOTPLUG);
return;
}
for (i = 0; i < nr; i++) {
if (preonline_hwpoison_contains(pfn + i))
hwpoison_hotplug_page(page + i);
else
__free_pages_core(page + i, 0,
MEMINIT_HOTPLUG);
}
} Withheld pages go to a new helper in mm/memory-failure.c, which leaves a page in the state Breno's v6 [1] gives an inherited frame:
void hwpoison_hotplug_page(struct page *page)
{
unsigned long pfn = page_to_pfn(page);
if (PageHWPoison(page))
return;
/* __free_pages_core() would have cleared it. */
__ClearPageOffline(page);
SetPageHWPoison(page);
/* Device-reported media error: unpoison must not free it. */
hw_memory_failure = true;
/* offline_pages() subtracts it again. */
adjust_managed_page_count(page, 1);
/* The memory block exists before online: count both. */
num_poisoned_pages_inc(pfn);
} memory_block_online() then keeps refusing to re-online the block (-EHWPOISON), and removing it subtracts the count again.
The case tested in the RFC cover letter (memmap_on_memory off, the Internal record at 0x190100 withheld) goes through B alone: there is no altmap, so A never comes into play, and the poison list is still read at region probe exactly as in patch 5.
With A in place every registered frame of hot-added memory reaches B, so the __free_pages_core() hook in patch 4 is no longer needed (Breno's v6 has dropped that hook anyway). Patch 4 goes away: boot RAM does no registry lookups, and the series no longer depends on Breno's and applies to cxl/next on its own.
Do A and B match what you had in mind? If you see a problem with either, especially dropping the altmap per block in A, I would rather hear it before I start on v2.
One more question for B. Breno's v6 has hwpoison_boot_page() for a frame inherited across kexec:
void __init hwpoison_boot_page(struct page *page)
{
SetPageHWPoison(page);
atomic_long_inc(&num_poisoned_pages);
hw_memory_failure = true;
adjust_managed_page_count(page, 1);
}My hotplug helper is almost the same. It also clears PageOffline, and it uses num_poisoned_pages_inc(pfn) because the memory block is already there at that point. I can't call Breno's helper from hotplug since it is __init.
For v2 I'd like to keep the two helpers separate, so this series does not depend on Breno's. Once both are merged, the common part can be pulled out into one function:
static void __meminit hwpoison_withheld_page(struct page *page)
{
SetPageHWPoison(page);
hw_memory_failure = true;
adjust_managed_page_count(page, 1);
}Is that okay with you, or would you rather have one shared helper from the start?
Thanks,
Shaikh Kamaluddin
[1] https://lore.kernel.org/all/[email protected]/
Reproducing the hotplug events:
---------------------------------
Step 1 (T1): boot the guest
From the top of your kernel tree:
vng --disable-kvm --run ./arch/x86/boot/bzImage \
--disable-microvm --memory 4G --cpus 4 \
--append "memory_hotplug.memmap_on_memory=1 tsc=unstable" \
--qemu-opts="-machine q35,cxl=on \
-object memory-backend-ram,id=vmem0,share=on,size=512M \
-device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
-device cxl-rp,port=0,bus=cxl.1,id=rp0,chassis=0,slot=2 \
-device cxl-type3,bus=rp0,volatile-memdev=vmem0,id=cxl-vmem0 \
-M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G \
-qmp unix:/tmp/qmp.sock,server=on,wait=off"Step 2 (T1, guest): basic setup and checks
----------------------------------------
# mount -t debugfs none /sys/kernel/debug 2>/dev/null # mount -t tracefs none /sys/kernel/tracing 2>/dev/null # mount -t tmpfs none /mnt
cat /sys/module/memory_hotplug/parameters/memmap_on_memory # Y
#cxl list -M # mem0 present
# grep -E 'MemTotal|HardwareCorrupted' /proc/meminfo # note MemTotal
Step 3 (T2, host): inject two Internal records
-------------------------------------------------
Do this after every boot, because QEMU loses the poison list when it restarts:
# qmp() { printf '{"execute":"qmp_capabilities"}\n%s\n' "$1" | socat - UNIX-CONNECT:/tmp/qmp.sock; }
# qmp '{"execute":"cxl-inject-poison","arguments":{"path":"/machine/peripheral/cxl-vmem0","start":1048576,"length":64}}' # DPA 1 MiB -> PFN 0x190100, inside vmemmap
# qmp '{"execute":"cxl-inject-poison","arguments":{"path":"/machine/peripheral/cxl-vmem0","start":4194304,"length":64}}' # DPA 4 MiB -> PFN 0x190400, outsideStep 4 (T1): create the region without letting kmem bind
---------------------------------------------------------
# echo 0 > /sys/bus/dax/drivers_autoprobe # cxl create-region -m -t ram -d decoder0.0 -w 1 mem0 # dmesg | grep withhold # "withholding 2 frame(s)"
# echo 1 > /sys/bus/dax/devices/dax0.0/memmap_on_memory # cat /sys/bus/dax/devices/dax0.0/memmap_on_memory # 1 #lsmem # blocks 50-53 must NOT be listed
Step 5 (T1): add the four probes with stack traces
---------------------------------------------------
# cd /sys/kernel/tracing # echo nop > current_tracer # echo > kprobe_events
# echo 'p:poison/vmemmap vmemmap_populate start=$arg1:x64 altmap=$arg4:x64' >> kprobe_events # echo 'p:poison/memmap_init mhp_init_memmap_on_memory pfn=$arg1:x64 nr=$arg2:x64' >> kprobe_events # echo 'p:poison/online online_pages pfn=$arg1:x64 nr=$arg2:x64' >> kprobe_events # echo 'p:poison/hwpoison hwpoison_boot_page page=$arg1:x64' >> kprobe_events # cat kprobe_events
#for e in vmemmap memmap_init online hwpoison; do
echo stacktrace > events/poison/$e/trigger
done
Step 6 (T1): run it
---------------------
# echo 1 > events/poison/enable # echo > trace # daxctl reconfigure-device --mode=system-ram dax0.0 # echo 0 > events/poison/enable
Step 7 (T1): read the call chains
-----------------------------------
cat trace > /mnt/hotplug_stacks.txt
cat /mnt/hotplug_stacks.txt
# tracer: nop
#
# entries-in-buffer/entries-written: 26/26 #P:4
#
# _-----=> irqs-off/BH-disabled
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / _-=> migrate-disable
# |||| / delay
# TASK-PID CPU# ||||| TIMESTAMP FUNCTION
# | | | ||||| | |
daxctl-224 [003] ..... 725.066915: vmemmap: (vmemmap_populate+0x4/0x60) start=0xffc4f4aa86400000 altmap=0xff4a93014354df80
daxctl-224 [003] ..... 725.069220: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> vmemmap_populate
=> __populate_section_memmap
=> sparse_add_section
=> __add_pages
=> add_pages
=> __add_memory_resource
=> __add_memory_driver_managed
=> dax_kmem_do_hotplug
=> dev_dax_kmem_probe
=> dax_bus_probe
=> really_probe
=> __driver_probe_device
=> driver_probe_device
=> __driver_attach
=> bus_for_each_dev
=> do_id_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 725.084123: vmemmap: (vmemmap_populate+0x4/0x60) start=0xffc4f4aa86600000 altmap=0xff4a9301434b8080
daxctl-224 [003] ..... 725.084240: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> vmemmap_populate
=> __populate_section_memmap
=> sparse_add_section
=> __add_pages
=> add_pages
=> __add_memory_resource
=> __add_memory_driver_managed
=> dax_kmem_do_hotplug
=> dev_dax_kmem_probe
=> dax_bus_probe
=> really_probe
=> __driver_probe_device
=> driver_probe_device
=> __driver_attach
=> bus_for_each_dev
=> do_id_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 725.086550: vmemmap: (vmemmap_populate+0x4/0x60) start=0xffc4f4aa86800000 altmap=0xff4a9301421dafc0
daxctl-224 [003] ..... 725.087099: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> vmemmap_populate
=> __populate_section_memmap
=> sparse_add_section
=> __add_pages
=> add_pages
=> __add_memory_resource
=> __add_memory_driver_managed
=> dax_kmem_do_hotplug
=> dev_dax_kmem_probe
=> dax_bus_probe
=> really_probe
=> __driver_probe_device
=> driver_probe_device
=> __driver_attach
=> bus_for_each_dev
=> do_id_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 725.090221: vmemmap: (vmemmap_populate+0x4/0x60) start=0xffc4f4aa86a00000 altmap=0xff4a93014340bd40
daxctl-224 [003] ..... 725.090409: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> vmemmap_populate
=> __populate_section_memmap
=> sparse_add_section
=> __add_pages
=> add_pages
=> __add_memory_resource
=> __add_memory_driver_managed
=> dax_kmem_do_hotplug
=> dev_dax_kmem_probe
=> dax_bus_probe
=> really_probe
=> __driver_probe_device
=> driver_probe_device
=> __driver_attach
=> bus_for_each_dev
=> do_id_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 725.214535: memmap_init: (mhp_init_memmap_on_memory+0x4/0xa0) pfn=0x190000 nr=0x200
daxctl-224 [003] ..... 725.214996: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> mhp_init_memmap_on_memory
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 725.231165: online: (online_pages+0x4/0x330) pfn=0x190200 nr=0x7e00
daxctl-224 [003] ..... 725.231245: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> online_pages
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 725.658068: hwpoison: (hwpoison_boot_page+0x4/0x70) page=0xffc4f4aa86410000
daxctl-224 [003] ..... 725.658306: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> hwpoison_boot_page
=> __free_pages_core
=> online_pages
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 725.888289: memmap_init: (mhp_init_memmap_on_memory+0x4/0xa0) pfn=0x1a8000 nr=0x200
daxctl-224 [003] ..... 725.888421: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> mhp_init_memmap_on_memory
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 725.924992: online: (online_pages+0x4/0x330) pfn=0x1a8200 nr=0x7e00
daxctl-224 [003] ..... 725.925075: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> online_pages
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 727.248819: memmap_init: (mhp_init_memmap_on_memory+0x4/0xa0) pfn=0x198000 nr=0x200
daxctl-224 [003] ..... 727.248893: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> mhp_init_memmap_on_memory
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [003] ..... 727.262077: online: (online_pages+0x4/0x330) pfn=0x198200 nr=0x7e00
daxctl-224 [003] ..... 727.262127: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> online_pages
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [000] ..... 728.465063: memmap_init: (mhp_init_memmap_on_memory+0x4/0xa0) pfn=0x1a0000 nr=0x200
daxctl-224 [000] ..... 728.465203: <stack trace>
=> kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> mhp_init_memmap_on_memory
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
daxctl-224 [000] ..... 728.480563: online: (online_pages+0x4/0x330) pfn=0x1a0200 nr=0x7e00
daxctl-224 [000] ..... 728.480690: <stack trace> => kprobe_trace_func
=> kprobe_dispatcher
=> kprobe_ftrace_handler
=> 0xffffffffc024e0da
=> online_pages
=> memory_block_change_state
=> memory_subsys_online
=> device_online
=> state_store
=> kernfs_fop_write_iter
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
Quoted 12 lines
Best regards,
Richard Cheng.
Re: [RFC PATCH 4/5] mm/page_alloc: Keep pre-online poison out of the buddy allocator
In reply to Shaikh KamaluddinQuoted 48 lines · Shaikh Kamaluddin
The inherited-poison path checks initialized blocks before releasing them to the buddy allocator. CXL device poison discovered before hot-added memory is onlined is recorded separately in the pre-online poison registry.
Extend
__free_pages_core()to send a block tofree_poisoned_block()when it intersects either poison source. Infree_poisoned_block(), withhold a page when its PFN is registered as pre-online poison or its physical address is present in the inherited-poison table.Pages identified by either source continue through
hwpoison_boot_page(), while clean pages are released throughaccept_and_free_block().Signed-off-by Shaikh Kamaluddin <[email protected]>--- mm/page_alloc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)mm/page_alloc.c +4
-1@@ -33,6 +33,7 @@#include <linux/cpuset.h> #include <linux/folio_batch.h> #include <linux/memory_hotplug.h> +#include <linux/memory-failure.h> #include <linux/nodemask.h> #include <linux/vmstat.h> #include <linux/fault-inject.h> @@ -1601,12 +1602,14 @@ static void __meminit free_poisoned_block(struct page *page, unsigned int order, enum meminit_context context) {unsigned long i, nr_pages =1UL << order; +unsigned long pfn =page_to_pfn (page);for (i =0 ; i < nr_pages; i++) {struct page *p = page + i;phys_addr_t phys =page_to_phys (p); -if (range_contains_poisoned_memory (phys,PAGE_SIZE )) { +if (range_contains_poisoned_memory (phys,PAGE_SIZE ) || +preonline_hwpoison_contains (pfn + i)) {hwpoison_boot_page (p, context);continue ; }
mm/page_alloc.c void __meminit __free_pages_core(struct page *page, unsigned int order, · line 1659
Quoted 6 lines
mm/page_alloc.c +2
-1@@ -1652,7 +1655,8 @@ void __meminit __free_pages_core(struct page *page, unsigned int order,/* First: a block parked by __free_unaccepted() never returns here. */ if (range_contains_poisoned_memory (page_to_phys (page), -PAGE_SIZE << order)) { +PAGE_SIZE << order) || +preonline_hwpoison_intersects (page_to_pfn (page), nr_pages)) {
That looks rather hacky as well.
I really don't want us to have various source of poison information that the page allocator + core-mm has to mess with. It's just very bad design.
I made that clear in the last kexec attempt to do something similar.
This likely belongs into memory onlining, which could online only the relevant, un-poisoned pageblocks.
Not sure how it would best retrieve that information, but we should definitely not add more shaky hwpoison sources to random pieces of core kernel code.
Signature · 3 lines
Cheers, David
RFC PATCH 5/5 cxl/region: Register device poison before exposing RAM
In reply to Shaikh KamaluddinThe trigger_poison_list interface may retrieve poison after CXL memory is already online, which is too late to prevent known-bad frames from entering the page allocator.
During RAM-region probe, retrieve a poison-list snapshot for each participating endpoint decoder. Validate each record against the decoder's assigned DPA range, translate its poisoned DPA units to HPA and PFN, and collect the affected PFNs in an xarray. Coalesce consecutive PFNs and register the resulting ranges with the pre-online hwpoison registry before creating the DAX region.
When Get Poison List is supported, abort region probe if the list is incomplete, malformed, or cannot be translated. If volatile poison-list retrieval is unsupported, preserve the existing behavior and warn that poison cannot be withheld before memory onlining.
--- drivers/cxl/core/region.c | 322 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+)
drivers/cxl/core/region.c +322 -0
@@ -14,6 +14,7 @@#include <linux/string_choices.h> #include <cxlmem.h> #include <cxl.h> +#include <linux/memory-failure.h> #include "core.h" #include "mce.h"
@@ -3038,6 +3039,323 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port)return rc; } +/* +* Device-reported poison is gathered at RAM-region probe, before the range +* reaches dax/kmem, and registered with the pre-online registry so the +* frames it covers are withheld when the memory is onlined. +*/ +static int cxl_poison_store_pfn (struct xarray *pfns,u64 hpa) +{ +return xa_err (xa_store (pfns,PHYS_PFN (hpa),xa_mk_value (1 ), +GFP_KERNEL )); +} + +static int cxl_poison_store_hpa (struct cxl_region *cxlr,struct xarray *pfns, +u64 hpa) +{ +struct cxl_region_params *p = &cxlr->params; +int rc; + +/* Outside this region: not ours to withhold. */ +if (!cxl_resource_contains_addr (p->res, hpa)) +return -ERANGE ; + + rc =cxl_poison_store_pfn (pfns, hpa); +if (rc || !p->cache_size) +return rc; + +/* +* An extended linear cache makes the same DPA visible a second time +* at hpa - cache_size. Match the translation used by the +* cxl_general_media and cxl_dram trace events. +*/ +if (WARN_ON_ONCE (hpa < p->res->start + p->cache_size)) +return -ERANGE ; + +return cxl_poison_store_pfn (pfns, hpa - p->cache_size); +} + +static int cxl_poison_record_to_pfns (struct cxl_region *cxlr, +struct cxl_memdev *cxlmd, +const struct cxl_poison_record *record, +struct xarray *pfns) +{ +struct cxl_region_params *p = &cxlr->params; +u64 raw =le64_to_cpu (record->address); +u64 units =le32_to_cpu (record->length); +u64 dpa = raw &CXL_POISON_START_MASK ; +u8 source = raw &CXL_POISON_SOURCE_MASK ; +u64 length, end, step, cur; + +if (!units || +check_mul_overflow (units, (u64 )CXL_POISON_LEN_MULT , &length) || +check_add_overflow (dpa, length, &end)) +return -EPROTO ; + +/* +* A corrected error never reaches the poison list, so every record +* is uncorrectable. Only a failure of the device's own media is +* withheld: External poison was written by the host into healthy +* media and a rewrite or Clear Poison recovers it, and Injected +* poison is a test artifact. Withholding either would make a +* recoverable state permanent. +*/ +if (source !=CXL_POISON_SOURCE_INTERNAL ) { +dev_dbg (&cxlr->dev,"%s: dpa %#llx source %u not withheld\n" , +dev_name (&cxlmd->dev), dpa, source); +return 0 ; + } + +/* +* Translate once per unit of HPA contiguity rather than per 64 +* bytes. Consecutive DPAs within an interleave granule map to +* consecutive HPAs, and a granule no larger than a page lies within +* one page, so stepping by min(granule, PAGE_SIZE) from an aligned +* start reaches every page the record covers. +*/ + step =clamp_t (u64, p->interleave_granularity,CXL_POISON_LEN_MULT , +PAGE_SIZE ); + +for (cur =ALIGN_DOWN (dpa, step); cur < end; cur += step) { +u64 hpa =cxl_dpa_to_hpa (cxlr, cxlmd,max (cur, dpa)); +int rc; + +/* Not mapped by this region; nothing here to withhold. */ +if (hpa ==ULLONG_MAX ) +continue ; + + rc =cxl_poison_store_hpa (cxlr, pfns, hpa); +if (rc) +return rc; + } + +return 0 ; +} + +static int cxl_region_collect_decoder_poison (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +struct xarray *pfns) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_poison_snapshot snapshot; +int rc; + + rc =cxl_poison_snapshot_init (cxlmd, &snapshot); +if (rc == -EOPNOTSUPP ) { +dev_info (&cxlr->dev, +"%s: no Get Poison List support, nothing withheld\n" , +dev_name (&cxlmd->dev)); +return 0 ; + } +if (rc) +return rc; + + rc =cxl_get_poison_by_decoder (cxled, &snapshot, +CXL_POISON_QUERY_PREONLINE ); +if (rc == -EFAULT ) { +/* +* The device keeps no poison list for volatile ranges, which +* CXL r3.2 8.2.9.8.4.1 permits. Online the region as today. +*/ +dev_info (&cxlr->dev, +"%s: no volatile poison list, nothing withheld\n" , +dev_name (&cxlmd->dev)); + rc =0 ; + goto out; + } +if (rc) + goto out; + +/* +* Withholding from a partial list would online the region on the +* belief that every bad frame had been named. +*/ +if (snapshot.truncated || + (snapshot.device_flags &CXL_POISON_FLAG_OVERFLOW )) { +dev_err (&cxlr->dev,"%s: poison list incomplete\n" , +dev_name (&cxlmd->dev)); + rc = -EOVERFLOW ; + goto out; + } +if (snapshot.device_flags &CXL_POISON_FLAG_SCANNING ) { +dev_err (&cxlr->dev,"%s: media scan in progress\n" , +dev_name (&cxlmd->dev)); + rc = -EBUSY ; + goto out; + } + +for (u32 i =0 ; i < snapshot.nr_records; i++) { + rc =cxl_poison_record_to_pfns (cxlr, cxlmd, + &snapshot.records[i], pfns); +if (rc) +break ; + } +out: +cxl_poison_snapshot_destroy (&snapshot); +return rc; +} + +static unsigned int cxl_count_poison_ranges (struct xarray *pfns) +{ +unsigned long index, previous =0 ; +unsigned int nr =0 ; +void *entry; +bool first = true; + +xa_for_each (pfns, index, entry) { +if (first || index != previous +1 ) + nr++; + first = false; + previous = index; + } +return nr; +} + +/* +* The xarray holds each PFN once and xa_for_each() walks them in ascending +* order, so runs of consecutive indices are exactly the sorted, +* nonoverlapping ranges preonline_hwpoison_register() requires. +*/ +static int cxl_pfns_to_ranges (struct xarray *pfns, +struct preonline_hwpoison_range **ranges_out, +unsigned int *nr_ranges_out) +{ +struct preonline_hwpoison_range *ranges; +unsigned long index, previous =0 , run_start =0 ; +unsigned int nr_ranges, nr =0 ; +void *entry; +bool first = true; + + nr_ranges =cxl_count_poison_ranges (pfns); +if (!nr_ranges) { + *ranges_out =NULL ; + *nr_ranges_out =0 ; +return 0 ; + } + + ranges =kvmalloc_array (nr_ranges,sizeof (*ranges),GFP_KERNEL ); +if (!ranges) +return -ENOMEM ; + +xa_for_each (pfns, index, entry) { +if (first) { + run_start = index; + previous = index; + first = false; +continue ; + } +if (index == previous +1 ) { + previous = index; +continue ; + } + ranges[nr++] = (struct preonline_hwpoison_range ) { + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + run_start = index; + previous = index; + } + ranges[nr++] = (struct preonline_hwpoison_range ){ + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + +WARN_ON_ONCE (nr != nr_ranges); + *ranges_out = ranges; + *nr_ranges_out = nr; +return 0 ; +} + +static void cxl_unregister_device_poison (void *data) +{ +preonline_hwpoison_unregister (data); +} + +static int cxl_region_register_device_poison (struct cxl_region *cxlr) +{ +struct cxl_region_params *p = &cxlr->params; +struct preonline_hwpoison_range *ranges =NULL ; +struct preonline_hwpoison *handle; +unsigned int nr_ranges =0 ; +unsigned long nr_frames =0 ; +struct xarray pfns; +int rc =0 ; + +lockdep_assert_held (&cxl_rwsem.region); +lockdep_assert_held (&cxl_rwsem.dpa); + +/* The region may have been decommitted while the lock was dropped. */ +if (p->state <CXL_CONFIG_COMMIT ) +return -ENXIO ; + +if (test_bit (CXL_REGION_F_NORMALIZED_ADDRESSING , &cxlr->flags)) { +dev_info (&cxlr->dev, +"normalized addressing, poison not translated\n" ); +return 0 ; + } + +xa_init (&pfns); + +for (int i =0 ; i < p->nr_targets; i++) { +struct cxl_endpoint_decoder *cxled = p->targets[i]; + +if (!cxled->dpa_res || !resource_size (cxled->dpa_res)) +continue ; + rc =cxl_region_collect_decoder_poison (cxlr, cxled, &pfns); +if (rc) + goto out; + } + +if (xa_empty (&pfns)) + goto out; + + rc =cxl_pfns_to_ranges (&pfns, &ranges, &nr_ranges); +if (rc) + goto out; + + rc =preonline_hwpoison_register (ranges, nr_ranges, &handle); +if (rc == -EOPNOTSUPP ) { +/* No poison tracking in this kernel; nothing to withhold. */ + rc =0 ; + goto out; + } +if (rc) + goto out; + +for (unsigned int i =0 ; i < nr_ranges; i++) + nr_frames += ranges[i].nr_pages; +dev_warn (&cxlr->dev, +"withholding %lu frame(s) reported poisoned by the device\n" , + nr_frames); + + rc =devm_add_action_or_reset (&cxlr->dev, cxl_unregister_device_poison, + handle); +out: +kvfree (ranges); +xa_destroy (&pfns); +return rc; +} + +static int cxl_region_scan_device_poison (struct cxl_region *cxlr) +{ +int rc; + +/* Nothing would consume the result. */ +if (!IS_ENABLED (CONFIG_MEMORY_FAILURE )) +return 0 ; + +ACQUIRE (rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); + rc =ACQUIRE_ERR (rwsem_read_intr, ®ion_rwsem); +if (rc) +return rc; +ACQUIRE (rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); + rc =ACQUIRE_ERR (rwsem_read_intr, &dpa_rwsem); +if (rc) +return rc; + +return cxl_region_register_device_poison (cxlr); +} +struct cxl_dpa_to_region_context {struct cxl_region *cxlr;u64 dpa;
@@ -4262,6 +4580,10 @@ static int cxl_region_probe(struct device *dev) p->res->start, p->res->end, cxlr, is_system_ram) > 0)return 0 ; + rc =cxl_region_scan_device_poison (cxlr); +if (rc) +return rc; +return devm_cxl_add_dax_region (cxlr);default : dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [RFC PATCH 5/5] cxl/region: Register device poison before exposing RAM
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] cxl: probe abort due to strict -ERANGE return in cxl_poison_store_hpa() - [High] cxl: infinite…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] cxl: probe abort due to strict -ERANGE return in cxl_poison_store_hpa()
- [High] cxl: infinite loop in cxl_poison_record_to_pfns() due to integer overflow --
commit 9d79e4bd40f84f47cf37cc9109545f12829756e5
Author: Shaikh Kamaluddin <[email protected]>
cxl/region: Register device poison before exposing RAM
This commit retrieves device-reported poison lists during CXL RAM region probe and registers them with the pre-online hwpoison registry to withhold bad frames from being onlined.
Quoted 4 lines
drivers/cxl/core/region.c +0
-0
[ ... ]
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3061
Quoted 9 lines
drivers/cxl/core/region.c +9
-0+static int cxl_poison_store_hpa (struct cxl_region *cxlr,struct xarray *pfns, +u64 hpa) +{ +struct cxl_region_params *p = &cxlr->params; +int rc; + +/* Outside this region: not ours to withhold. */ +if (!cxl_resource_contains_addr (p->res, hpa)) +return -ERANGE ;
[Severity: High]
Does this error handling cause the entire region probe to abort when a poison record falls outside the expected range?
The comment notes "not ours to withhold", which suggests this is an expected condition that should simply be skipped. However, returning -ERANGE here causes the caller cxl_poison_record_to_pfns() to return the error immediately, which bubbles up through cxl_region_collect_decoder_poison() and aborts the entire region probe.
Should this return 0 instead to avoid failing the region probe when devices return poison records that span outside the queried DPA range?
Quoted 4 lines
drivers/cxl/core/region.c +4
-0+ + rc =cxl_poison_store_pfn (pfns, hpa); +if (rc || !p->cache_size) +return rc;
[ ... ]
Quoted 5 lines
drivers/cxl/core/region.c +5
-0+static int cxl_poison_record_to_pfns (struct cxl_region *cxlr, +struct cxl_memdev *cxlmd, +const struct cxl_poison_record *record, +struct xarray *pfns) +{
[ ... ]
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3119
Quoted 4 lines
drivers/cxl/core/region.c +4
-0+ step =clamp_t (u64, p->interleave_granularity,CXL_POISON_LEN_MULT , +PAGE_SIZE ); + +for (cur =ALIGN_DOWN (dpa, step); cur < end; cur += step) {
[Severity: High]
Can this loop iterate infinitely due to an integer wrap-around?
If an untrusted device provides a dpa near U64_MAX and a small length, the check_add_overflow() validation at the top of this function will pass. When cur is initialized to ALIGN_DOWN(dpa, step), the cur += step increment could overflow and wrap around to 0. Since 0 is less than end, the loop condition remains true forever, potentially causing a lockup while holding the cxl_rwsem locks.
Quoted 11 lines
drivers/cxl/core/region.c +11
-0+u64 hpa =cxl_dpa_to_hpa (cxlr, cxlmd,max (cur, dpa)); +int rc; + +/* Not mapped by this region; nothing here to withhold. */ +if (hpa ==ULLONG_MAX ) +continue ; + + rc =cxl_poison_store_hpa (cxlr, pfns, hpa); +if (rc) +return rc; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [RFC PATCH 5/5] cxl/region: Register device poison before exposing RAM
In reply to Shaikh KamaluddinQuoted 318 lines · Shaikh Kamaluddin
The
trigger_poison_listinterface may retrieve poison after CXL memory is already online, which is too late to prevent known-bad frames from entering the page allocator.During RAM-region probe, retrieve a poison-list snapshot for each participating endpoint decoder. Validate each record against the decoder's assigned DPA range, translate its poisoned DPA units to HPA and PFN, and collect the affected PFNs in an xarray. Coalesce consecutive PFNs and register the resulting ranges with the pre-online hwpoison registry before creating the DAX region.
When Get Poison List is supported, abort region probe if the list is incomplete, malformed, or cannot be translated. If volatile poison-list retrieval is unsupported, preserve the existing behavior and warn that poison cannot be withheld before memory onlining.
Signed-off-by Shaikh Kamaluddin <[email protected]>--- drivers/cxl/core/region.c | 322 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+)drivers/cxl/core/region.c +282
-0@@ -14,6 +14,7 @@#include <linux/string_choices.h> #include <cxlmem.h> #include <cxl.h> +#include <linux/memory-failure.h> #include "core.h" #include "mce.h" @@ -3038,6 +3039,323 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port)return rc; } +/* +* Device-reported poison is gathered at RAM-region probe, before the range +* reaches dax/kmem, and registered with the pre-online registry so the +* frames it covers are withheld when the memory is onlined. +*/ +static int cxl_poison_store_pfn (struct xarray *pfns,u64 hpa) +{ +return xa_err (xa_store (pfns,PHYS_PFN (hpa),xa_mk_value (1 ), +GFP_KERNEL )); +} + +static int cxl_poison_store_hpa (struct cxl_region *cxlr,struct xarray *pfns, +u64 hpa) +{ +struct cxl_region_params *p = &cxlr->params; +int rc; + +/* Outside this region: not ours to withhold. */ +if (!cxl_resource_contains_addr (p->res, hpa)) +return -ERANGE ; + + rc =cxl_poison_store_pfn (pfns, hpa); +if (rc || !p->cache_size) +return rc; + +/* +* An extended linear cache makes the same DPA visible a second time +* at hpa - cache_size. Match the translation used by the +* cxl_general_media and cxl_dram trace events. +*/ +if (WARN_ON_ONCE (hpa < p->res->start + p->cache_size)) +return -ERANGE ; + +return cxl_poison_store_pfn (pfns, hpa - p->cache_size); +} + +static int cxl_poison_record_to_pfns (struct cxl_region *cxlr, +struct cxl_memdev *cxlmd, +const struct cxl_poison_record *record, +struct xarray *pfns) +{ +struct cxl_region_params *p = &cxlr->params; +u64 raw =le64_to_cpu (record->address); +u64 units =le32_to_cpu (record->length); +u64 dpa = raw &CXL_POISON_START_MASK ; +u8 source = raw &CXL_POISON_SOURCE_MASK ; +u64 length, end, step, cur; + +if (!units || +check_mul_overflow (units, (u64 )CXL_POISON_LEN_MULT , &length) || +check_add_overflow (dpa, length, &end)) +return -EPROTO ; + +/* +* A corrected error never reaches the poison list, so every record +* is uncorrectable. Only a failure of the device's own media is +* withheld: External poison was written by the host into healthy +* media and a rewrite or Clear Poison recovers it, and Injected +* poison is a test artifact. Withholding either would make a +* recoverable state permanent. +*/ +if (source !=CXL_POISON_SOURCE_INTERNAL ) { +dev_dbg (&cxlr->dev,"%s: dpa %#llx source %u not withheld\n" , +dev_name (&cxlmd->dev), dpa, source); +return 0 ; + } + +/* +* Translate once per unit of HPA contiguity rather than per 64 +* bytes. Consecutive DPAs within an interleave granule map to +* consecutive HPAs, and a granule no larger than a page lies within +* one page, so stepping by min(granule, PAGE_SIZE) from an aligned +* start reaches every page the record covers. +*/ + step =clamp_t (u64, p->interleave_granularity,CXL_POISON_LEN_MULT , +PAGE_SIZE ); + +for (cur =ALIGN_DOWN (dpa, step); cur < end; cur += step) { +u64 hpa =cxl_dpa_to_hpa (cxlr, cxlmd,max (cur, dpa)); +int rc; + +/* Not mapped by this region; nothing here to withhold. */ +if (hpa ==ULLONG_MAX ) +continue ; + + rc =cxl_poison_store_hpa (cxlr, pfns, hpa); +if (rc) +return rc; + } + +return 0 ; +} + +static int cxl_region_collect_decoder_poison (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +struct xarray *pfns) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_poison_snapshot snapshot; +int rc; + + rc =cxl_poison_snapshot_init (cxlmd, &snapshot); +if (rc == -EOPNOTSUPP ) { +dev_info (&cxlr->dev, +"%s: no Get Poison List support, nothing withheld\n" , +dev_name (&cxlmd->dev)); +return 0 ; + } +if (rc) +return rc; + + rc =cxl_get_poison_by_decoder (cxled, &snapshot, +CXL_POISON_QUERY_PREONLINE ); +if (rc == -EFAULT ) { +/* +* The device keeps no poison list for volatile ranges, which +* CXL r3.2 8.2.9.8.4.1 permits. Online the region as today. +*/ +dev_info (&cxlr->dev, +"%s: no volatile poison list, nothing withheld\n" , +dev_name (&cxlmd->dev)); + rc =0 ; + goto out; + } +if (rc) + goto out; + +/* +* Withholding from a partial list would online the region on the +* belief that every bad frame had been named. +*/ +if (snapshot.truncated || + (snapshot.device_flags &CXL_POISON_FLAG_OVERFLOW )) { +dev_err (&cxlr->dev,"%s: poison list incomplete\n" , +dev_name (&cxlmd->dev)); + rc = -EOVERFLOW ; + goto out; + } +if (snapshot.device_flags &CXL_POISON_FLAG_SCANNING ) { +dev_err (&cxlr->dev,"%s: media scan in progress\n" , +dev_name (&cxlmd->dev)); + rc = -EBUSY ; + goto out; + } + +for (u32 i =0 ; i < snapshot.nr_records; i++) { + rc =cxl_poison_record_to_pfns (cxlr, cxlmd, + &snapshot.records[i], pfns); +if (rc) +break ; + } +out: +cxl_poison_snapshot_destroy (&snapshot); +return rc; +} + +static unsigned int cxl_count_poison_ranges (struct xarray *pfns) +{ +unsigned long index, previous =0 ; +unsigned int nr =0 ; +void *entry; +bool first = true; + +xa_for_each (pfns, index, entry) { +if (first || index != previous +1 ) + nr++; + first = false; + previous = index; + } +return nr; +} + +/* +* The xarray holds each PFN once and xa_for_each() walks them in ascending +* order, so runs of consecutive indices are exactly the sorted, +* nonoverlapping ranges preonline_hwpoison_register() requires. +*/ +static int cxl_pfns_to_ranges (struct xarray *pfns, +struct preonline_hwpoison_range **ranges_out, +unsigned int *nr_ranges_out) +{ +struct preonline_hwpoison_range *ranges; +unsigned long index, previous =0 , run_start =0 ; +unsigned int nr_ranges, nr =0 ; +void *entry; +bool first = true; + + nr_ranges =cxl_count_poison_ranges (pfns); +if (!nr_ranges) { + *ranges_out =NULL ; + *nr_ranges_out =0 ; +return 0 ; + } + + ranges =kvmalloc_array (nr_ranges,sizeof (*ranges),GFP_KERNEL ); +if (!ranges) +return -ENOMEM ; + +xa_for_each (pfns, index, entry) { +if (first) { + run_start = index; + previous = index; + first = false; +continue ; + } +if (index == previous +1 ) { + previous = index; +continue ; + } + ranges[nr++] = (struct preonline_hwpoison_range ) { + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + run_start = index; + previous = index; + } + ranges[nr++] = (struct preonline_hwpoison_range ){ + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + +WARN_ON_ONCE (nr != nr_ranges); + *ranges_out = ranges; + *nr_ranges_out = nr; +return 0 ; +} + +static void cxl_unregister_device_poison (void *data) +{ +preonline_hwpoison_unregister (data); +} + +static int cxl_region_register_device_poison (struct cxl_region *cxlr) +{ +struct cxl_region_params *p = &cxlr->params; +struct preonline_hwpoison_range *ranges =NULL ; +struct preonline_hwpoison *handle; +unsigned int nr_ranges =0 ; +unsigned long nr_frames =0 ; +struct xarray pfns; +int rc =0 ; + +lockdep_assert_held (&cxl_rwsem.region); +lockdep_assert_held (&cxl_rwsem.dpa); + +/* The region may have been decommitted while the lock was dropped. */ +if (p->state <CXL_CONFIG_COMMIT ) +return -ENXIO ; + +if (test_bit (CXL_REGION_F_NORMALIZED_ADDRESSING , &cxlr->flags)) { +dev_info (&cxlr->dev, +"normalized addressing, poison not translated\n" ); +return 0 ; + } + +xa_init (&pfns); + +for (int i =0 ; i < p->nr_targets; i++) { +struct cxl_endpoint_decoder *cxled = p->targets[i]; + +if (!cxled->dpa_res || !resource_size (cxled->dpa_res)) +continue ; + rc =cxl_region_collect_decoder_poison (cxlr, cxled, &pfns); +if (rc) + goto out; + } + +if (xa_empty (&pfns)) + goto out; + + rc =cxl_pfns_to_ranges (&pfns, &ranges, &nr_ranges); +if (rc) + goto out; + + rc =preonline_hwpoison_register (ranges, nr_ranges, &handle); +if (rc == -EOPNOTSUPP ) { +/* No poison tracking in this kernel; nothing to withhold. */ + rc =0 ; + goto out; + } +if (rc)
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3332
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+ goto out; + +for (unsigned int i =0 ; i < nr_ranges; i++) + nr_frames += ranges[i].nr_pages; +dev_warn (&cxlr->dev, +"withholding %lu frame(s) reported poisoned by the device\n" , + nr_frames); + + rc =devm_add_action_or_reset (&cxlr->dev, cxl_unregister_device_poison, + handle);
Hi Shaikh,
I think you are trying to propose the architecture as the following ?
- CXL region driver collects the device's poison info and owns the registration
- MM use the registration to keep poisoned pages out of the page allocator when memory is onlined
- DAX/kmem adds and removes the corresponding system RAM
The registration belongs to the driver, but the memory that needs it can outlive the driver binding.
Unbinding the driver doesn't necessarily remove the memory. If some mem blocks are still online, removal of the range can fail.
When driver cleanup removes the registration, MM loses poison info of those memory blocks as well.
I don't have a good idea how we should re-design the ownership here, maybe we can discuss about it.
Best regards,
Richard Cheng.
Quoted 43 lines
drivers/cxl/core/region.c +30
-0+out: +kvfree (ranges); +xa_destroy (&pfns); +return rc; +} + +static int cxl_region_scan_device_poison (struct cxl_region *cxlr) +{ +int rc; + +/* Nothing would consume the result. */ +if (!IS_ENABLED (CONFIG_MEMORY_FAILURE )) +return 0 ; + +ACQUIRE (rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); + rc =ACQUIRE_ERR (rwsem_read_intr, ®ion_rwsem); +if (rc) +return rc; +ACQUIRE (rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); + rc =ACQUIRE_ERR (rwsem_read_intr, &dpa_rwsem); +if (rc) +return rc; + +return cxl_region_register_device_poison (cxlr); +} +struct cxl_dpa_to_region_context {struct cxl_region *cxlr;u64 dpa;@@ -4262,6 +4580,10 @@ static int cxl_region_probe(struct device *dev) p->res->start, p->res->end, cxlr, is_system_ram) > 0)return 0 ; + rc =cxl_region_scan_device_poison (cxlr); +if (rc) +return rc; +return devm_cxl_add_dax_region (cxlr);default : dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",Signature · 1 line
2.43.0
Dave Jiang
maintainerRe: [RFC PATCH 5/5] cxl/region: Register device poison before exposing RAM
Quoted 341 lines · Richard Cheng
Quoted 318 lines · Shaikh Kamaluddin
The
trigger_poison_listinterface may retrieve poison after CXL memory is already online, which is too late to prevent known-bad frames from entering the page allocator.During RAM-region probe, retrieve a poison-list snapshot for each participating endpoint decoder. Validate each record against the decoder's assigned DPA range, translate its poisoned DPA units to HPA and PFN, and collect the affected PFNs in an xarray. Coalesce consecutive PFNs and register the resulting ranges with the pre-online hwpoison registry before creating the DAX region.
When Get Poison List is supported, abort region probe if the list is incomplete, malformed, or cannot be translated. If volatile poison-list retrieval is unsupported, preserve the existing behavior and warn that poison cannot be withheld before memory onlining.
Signed-off-by Shaikh Kamaluddin <[email protected]>--- drivers/cxl/core/region.c | 322 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+)drivers/cxl/core/region.c +282
-0@@ -14,6 +14,7 @@#include <linux/string_choices.h> #include <cxlmem.h> #include <cxl.h> +#include <linux/memory-failure.h> #include "core.h" #include "mce.h" @@ -3038,6 +3039,323 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port)return rc; } +/* +* Device-reported poison is gathered at RAM-region probe, before the range +* reaches dax/kmem, and registered with the pre-online registry so the +* frames it covers are withheld when the memory is onlined. +*/ +static int cxl_poison_store_pfn (struct xarray *pfns,u64 hpa) +{ +return xa_err (xa_store (pfns,PHYS_PFN (hpa),xa_mk_value (1 ), +GFP_KERNEL )); +} + +static int cxl_poison_store_hpa (struct cxl_region *cxlr,struct xarray *pfns, +u64 hpa) +{ +struct cxl_region_params *p = &cxlr->params; +int rc; + +/* Outside this region: not ours to withhold. */ +if (!cxl_resource_contains_addr (p->res, hpa)) +return -ERANGE ; + + rc =cxl_poison_store_pfn (pfns, hpa); +if (rc || !p->cache_size) +return rc; + +/* +* An extended linear cache makes the same DPA visible a second time +* at hpa - cache_size. Match the translation used by the +* cxl_general_media and cxl_dram trace events. +*/ +if (WARN_ON_ONCE (hpa < p->res->start + p->cache_size)) +return -ERANGE ; + +return cxl_poison_store_pfn (pfns, hpa - p->cache_size); +} + +static int cxl_poison_record_to_pfns (struct cxl_region *cxlr, +struct cxl_memdev *cxlmd, +const struct cxl_poison_record *record, +struct xarray *pfns) +{ +struct cxl_region_params *p = &cxlr->params; +u64 raw =le64_to_cpu (record->address); +u64 units =le32_to_cpu (record->length); +u64 dpa = raw &CXL_POISON_START_MASK ; +u8 source = raw &CXL_POISON_SOURCE_MASK ; +u64 length, end, step, cur; + +if (!units || +check_mul_overflow (units, (u64 )CXL_POISON_LEN_MULT , &length) || +check_add_overflow (dpa, length, &end)) +return -EPROTO ; + +/* +* A corrected error never reaches the poison list, so every record +* is uncorrectable. Only a failure of the device's own media is +* withheld: External poison was written by the host into healthy +* media and a rewrite or Clear Poison recovers it, and Injected +* poison is a test artifact. Withholding either would make a +* recoverable state permanent. +*/ +if (source !=CXL_POISON_SOURCE_INTERNAL ) { +dev_dbg (&cxlr->dev,"%s: dpa %#llx source %u not withheld\n" , +dev_name (&cxlmd->dev), dpa, source); +return 0 ; + } + +/* +* Translate once per unit of HPA contiguity rather than per 64 +* bytes. Consecutive DPAs within an interleave granule map to +* consecutive HPAs, and a granule no larger than a page lies within +* one page, so stepping by min(granule, PAGE_SIZE) from an aligned +* start reaches every page the record covers. +*/ + step =clamp_t (u64, p->interleave_granularity,CXL_POISON_LEN_MULT , +PAGE_SIZE ); + +for (cur =ALIGN_DOWN (dpa, step); cur < end; cur += step) { +u64 hpa =cxl_dpa_to_hpa (cxlr, cxlmd,max (cur, dpa)); +int rc; + +/* Not mapped by this region; nothing here to withhold. */ +if (hpa ==ULLONG_MAX ) +continue ; + + rc =cxl_poison_store_hpa (cxlr, pfns, hpa); +if (rc) +return rc; + } + +return 0 ; +} + +static int cxl_region_collect_decoder_poison (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +struct xarray *pfns) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_poison_snapshot snapshot; +int rc; + + rc =cxl_poison_snapshot_init (cxlmd, &snapshot); +if (rc == -EOPNOTSUPP ) { +dev_info (&cxlr->dev, +"%s: no Get Poison List support, nothing withheld\n" , +dev_name (&cxlmd->dev)); +return 0 ; + } +if (rc) +return rc; + + rc =cxl_get_poison_by_decoder (cxled, &snapshot, +CXL_POISON_QUERY_PREONLINE ); +if (rc == -EFAULT ) { +/* +* The device keeps no poison list for volatile ranges, which +* CXL r3.2 8.2.9.8.4.1 permits. Online the region as today. +*/ +dev_info (&cxlr->dev, +"%s: no volatile poison list, nothing withheld\n" , +dev_name (&cxlmd->dev)); + rc =0 ; + goto out; + } +if (rc) + goto out; + +/* +* Withholding from a partial list would online the region on the +* belief that every bad frame had been named. +*/ +if (snapshot.truncated || + (snapshot.device_flags &CXL_POISON_FLAG_OVERFLOW )) { +dev_err (&cxlr->dev,"%s: poison list incomplete\n" , +dev_name (&cxlmd->dev)); + rc = -EOVERFLOW ; + goto out; + } +if (snapshot.device_flags &CXL_POISON_FLAG_SCANNING ) { +dev_err (&cxlr->dev,"%s: media scan in progress\n" , +dev_name (&cxlmd->dev)); + rc = -EBUSY ; + goto out; + } + +for (u32 i =0 ; i < snapshot.nr_records; i++) { + rc =cxl_poison_record_to_pfns (cxlr, cxlmd, + &snapshot.records[i], pfns); +if (rc) +break ; + } +out: +cxl_poison_snapshot_destroy (&snapshot); +return rc; +} + +static unsigned int cxl_count_poison_ranges (struct xarray *pfns) +{ +unsigned long index, previous =0 ; +unsigned int nr =0 ; +void *entry; +bool first = true; + +xa_for_each (pfns, index, entry) { +if (first || index != previous +1 ) + nr++; + first = false; + previous = index; + } +return nr; +} + +/* +* The xarray holds each PFN once and xa_for_each() walks them in ascending +* order, so runs of consecutive indices are exactly the sorted, +* nonoverlapping ranges preonline_hwpoison_register() requires. +*/ +static int cxl_pfns_to_ranges (struct xarray *pfns, +struct preonline_hwpoison_range **ranges_out, +unsigned int *nr_ranges_out) +{ +struct preonline_hwpoison_range *ranges; +unsigned long index, previous =0 , run_start =0 ; +unsigned int nr_ranges, nr =0 ; +void *entry; +bool first = true; + + nr_ranges =cxl_count_poison_ranges (pfns); +if (!nr_ranges) { + *ranges_out =NULL ; + *nr_ranges_out =0 ; +return 0 ; + } + + ranges =kvmalloc_array (nr_ranges,sizeof (*ranges),GFP_KERNEL ); +if (!ranges) +return -ENOMEM ; + +xa_for_each (pfns, index, entry) { +if (first) { + run_start = index; + previous = index; + first = false; +continue ; + } +if (index == previous +1 ) { + previous = index; +continue ; + } + ranges[nr++] = (struct preonline_hwpoison_range ) { + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + run_start = index; + previous = index; + } + ranges[nr++] = (struct preonline_hwpoison_range ){ + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + +WARN_ON_ONCE (nr != nr_ranges); + *ranges_out = ranges; + *nr_ranges_out = nr; +return 0 ; +} + +static void cxl_unregister_device_poison (void *data) +{ +preonline_hwpoison_unregister (data); +} + +static int cxl_region_register_device_poison (struct cxl_region *cxlr) +{ +struct cxl_region_params *p = &cxlr->params; +struct preonline_hwpoison_range *ranges =NULL ; +struct preonline_hwpoison *handle; +unsigned int nr_ranges =0 ; +unsigned long nr_frames =0 ; +struct xarray pfns; +int rc =0 ; + +lockdep_assert_held (&cxl_rwsem.region); +lockdep_assert_held (&cxl_rwsem.dpa); + +/* The region may have been decommitted while the lock was dropped. */ +if (p->state <CXL_CONFIG_COMMIT ) +return -ENXIO ; + +if (test_bit (CXL_REGION_F_NORMALIZED_ADDRESSING , &cxlr->flags)) { +dev_info (&cxlr->dev, +"normalized addressing, poison not translated\n" ); +return 0 ; + } + +xa_init (&pfns); + +for (int i =0 ; i < p->nr_targets; i++) { +struct cxl_endpoint_decoder *cxled = p->targets[i]; + +if (!cxled->dpa_res || !resource_size (cxled->dpa_res)) +continue ; + rc =cxl_region_collect_decoder_poison (cxlr, cxled, &pfns); +if (rc) + goto out; + } + +if (xa_empty (&pfns)) + goto out; + + rc =cxl_pfns_to_ranges (&pfns, &ranges, &nr_ranges); +if (rc) + goto out; + + rc =preonline_hwpoison_register (ranges, nr_ranges, &handle); +if (rc == -EOPNOTSUPP ) { +/* No poison tracking in this kernel; nothing to withhold. */ + rc =0 ; + goto out; + } +if (rc)drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3332
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+ goto out; + +for (unsigned int i =0 ; i < nr_ranges; i++) + nr_frames += ranges[i].nr_pages; +dev_warn (&cxlr->dev, +"withholding %lu frame(s) reported poisoned by the device\n" , + nr_frames); + + rc =devm_add_action_or_reset (&cxlr->dev, cxl_unregister_device_poison, + handle);Hi Shaikh,
I think you are trying to propose the architecture as the following ?
- CXL region driver collects the device's poison info and owns the registration
- MM use the registration to keep poisoned pages out of the page allocator when memory is onlined
- DAX/kmem adds and removes the corresponding system RAMThe registration belongs to the driver, but the memory that needs it can outlive the driver binding.
Quoted 7 lines
Unbinding the driver doesn't necessarily remove the memory. If some mem blocks are still online, removal of the range can fail.
When driver cleanup removes the registration, MM loses poison info of those memory blocks as well.I don't have a good idea how we should re-design the ownership here, maybe we can discuss about it.
Maybe MM should own the registry and keyed by physical range. Then the lifetime of the poison would match the region being put into MM? The CXL driver would only need to add entries.
Quoted 4 lines
Best regards,
Richard Cheng.
Re: [RFC PATCH 5/5] cxl/region: Register device poison before exposing RAM
In reply to Richard ChengQuoted 332 lines · Richard Cheng
Quoted 318 lines · Shaikh Kamaluddin
The
trigger_poison_listinterface may retrieve poison after CXL memory is already online, which is too late to prevent known-bad frames from entering the page allocator.During RAM-region probe, retrieve a poison-list snapshot for each participating endpoint decoder. Validate each record against the decoder's assigned DPA range, translate its poisoned DPA units to HPA and PFN, and collect the affected PFNs in an xarray. Coalesce consecutive PFNs and register the resulting ranges with the pre-online hwpoison registry before creating the DAX region.
When Get Poison List is supported, abort region probe if the list is incomplete, malformed, or cannot be translated. If volatile poison-list retrieval is unsupported, preserve the existing behavior and warn that poison cannot be withheld before memory onlining.
Signed-off-by Shaikh Kamaluddin <[email protected]>--- drivers/cxl/core/region.c | 322 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+)drivers/cxl/core/region.c +282
-0@@ -14,6 +14,7 @@#include <linux/string_choices.h> #include <cxlmem.h> #include <cxl.h> +#include <linux/memory-failure.h> #include "core.h" #include "mce.h" @@ -3038,6 +3039,323 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port)return rc; } +/* +* Device-reported poison is gathered at RAM-region probe, before the range +* reaches dax/kmem, and registered with the pre-online registry so the +* frames it covers are withheld when the memory is onlined. +*/ +static int cxl_poison_store_pfn (struct xarray *pfns,u64 hpa) +{ +return xa_err (xa_store (pfns,PHYS_PFN (hpa),xa_mk_value (1 ), +GFP_KERNEL )); +} + +static int cxl_poison_store_hpa (struct cxl_region *cxlr,struct xarray *pfns, +u64 hpa) +{ +struct cxl_region_params *p = &cxlr->params; +int rc; + +/* Outside this region: not ours to withhold. */ +if (!cxl_resource_contains_addr (p->res, hpa)) +return -ERANGE ; + + rc =cxl_poison_store_pfn (pfns, hpa); +if (rc || !p->cache_size) +return rc; + +/* +* An extended linear cache makes the same DPA visible a second time +* at hpa - cache_size. Match the translation used by the +* cxl_general_media and cxl_dram trace events. +*/ +if (WARN_ON_ONCE (hpa < p->res->start + p->cache_size)) +return -ERANGE ; + +return cxl_poison_store_pfn (pfns, hpa - p->cache_size); +} + +static int cxl_poison_record_to_pfns (struct cxl_region *cxlr, +struct cxl_memdev *cxlmd, +const struct cxl_poison_record *record, +struct xarray *pfns) +{ +struct cxl_region_params *p = &cxlr->params; +u64 raw =le64_to_cpu (record->address); +u64 units =le32_to_cpu (record->length); +u64 dpa = raw &CXL_POISON_START_MASK ; +u8 source = raw &CXL_POISON_SOURCE_MASK ; +u64 length, end, step, cur; + +if (!units || +check_mul_overflow (units, (u64 )CXL_POISON_LEN_MULT , &length) || +check_add_overflow (dpa, length, &end)) +return -EPROTO ; + +/* +* A corrected error never reaches the poison list, so every record +* is uncorrectable. Only a failure of the device's own media is +* withheld: External poison was written by the host into healthy +* media and a rewrite or Clear Poison recovers it, and Injected +* poison is a test artifact. Withholding either would make a +* recoverable state permanent. +*/ +if (source !=CXL_POISON_SOURCE_INTERNAL ) { +dev_dbg (&cxlr->dev,"%s: dpa %#llx source %u not withheld\n" , +dev_name (&cxlmd->dev), dpa, source); +return 0 ; + } + +/* +* Translate once per unit of HPA contiguity rather than per 64 +* bytes. Consecutive DPAs within an interleave granule map to +* consecutive HPAs, and a granule no larger than a page lies within +* one page, so stepping by min(granule, PAGE_SIZE) from an aligned +* start reaches every page the record covers. +*/ + step =clamp_t (u64, p->interleave_granularity,CXL_POISON_LEN_MULT , +PAGE_SIZE ); + +for (cur =ALIGN_DOWN (dpa, step); cur < end; cur += step) { +u64 hpa =cxl_dpa_to_hpa (cxlr, cxlmd,max (cur, dpa)); +int rc; + +/* Not mapped by this region; nothing here to withhold. */ +if (hpa ==ULLONG_MAX ) +continue ; + + rc =cxl_poison_store_hpa (cxlr, pfns, hpa); +if (rc) +return rc; + } + +return 0 ; +} + +static int cxl_region_collect_decoder_poison (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +struct xarray *pfns) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_poison_snapshot snapshot; +int rc; + + rc =cxl_poison_snapshot_init (cxlmd, &snapshot); +if (rc == -EOPNOTSUPP ) { +dev_info (&cxlr->dev, +"%s: no Get Poison List support, nothing withheld\n" , +dev_name (&cxlmd->dev)); +return 0 ; + } +if (rc) +return rc; + + rc =cxl_get_poison_by_decoder (cxled, &snapshot, +CXL_POISON_QUERY_PREONLINE ); +if (rc == -EFAULT ) { +/* +* The device keeps no poison list for volatile ranges, which +* CXL r3.2 8.2.9.8.4.1 permits. Online the region as today. +*/ +dev_info (&cxlr->dev, +"%s: no volatile poison list, nothing withheld\n" , +dev_name (&cxlmd->dev)); + rc =0 ; + goto out; + } +if (rc) + goto out; + +/* +* Withholding from a partial list would online the region on the +* belief that every bad frame had been named. +*/ +if (snapshot.truncated || + (snapshot.device_flags &CXL_POISON_FLAG_OVERFLOW )) { +dev_err (&cxlr->dev,"%s: poison list incomplete\n" , +dev_name (&cxlmd->dev)); + rc = -EOVERFLOW ; + goto out; + } +if (snapshot.device_flags &CXL_POISON_FLAG_SCANNING ) { +dev_err (&cxlr->dev,"%s: media scan in progress\n" , +dev_name (&cxlmd->dev)); + rc = -EBUSY ; + goto out; + } + +for (u32 i =0 ; i < snapshot.nr_records; i++) { + rc =cxl_poison_record_to_pfns (cxlr, cxlmd, + &snapshot.records[i], pfns); +if (rc) +break ; + } +out: +cxl_poison_snapshot_destroy (&snapshot); +return rc; +} + +static unsigned int cxl_count_poison_ranges (struct xarray *pfns) +{ +unsigned long index, previous =0 ; +unsigned int nr =0 ; +void *entry; +bool first = true; + +xa_for_each (pfns, index, entry) { +if (first || index != previous +1 ) + nr++; + first = false; + previous = index; + } +return nr; +} + +/* +* The xarray holds each PFN once and xa_for_each() walks them in ascending +* order, so runs of consecutive indices are exactly the sorted, +* nonoverlapping ranges preonline_hwpoison_register() requires. +*/ +static int cxl_pfns_to_ranges (struct xarray *pfns, +struct preonline_hwpoison_range **ranges_out, +unsigned int *nr_ranges_out) +{ +struct preonline_hwpoison_range *ranges; +unsigned long index, previous =0 , run_start =0 ; +unsigned int nr_ranges, nr =0 ; +void *entry; +bool first = true; + + nr_ranges =cxl_count_poison_ranges (pfns); +if (!nr_ranges) { + *ranges_out =NULL ; + *nr_ranges_out =0 ; +return 0 ; + } + + ranges =kvmalloc_array (nr_ranges,sizeof (*ranges),GFP_KERNEL ); +if (!ranges) +return -ENOMEM ; + +xa_for_each (pfns, index, entry) { +if (first) { + run_start = index; + previous = index; + first = false; +continue ; + } +if (index == previous +1 ) { + previous = index; +continue ; + } + ranges[nr++] = (struct preonline_hwpoison_range ) { + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + run_start = index; + previous = index; + } + ranges[nr++] = (struct preonline_hwpoison_range ){ + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + +WARN_ON_ONCE (nr != nr_ranges); + *ranges_out = ranges; + *nr_ranges_out = nr; +return 0 ; +} + +static void cxl_unregister_device_poison (void *data) +{ +preonline_hwpoison_unregister (data); +} + +static int cxl_region_register_device_poison (struct cxl_region *cxlr) +{ +struct cxl_region_params *p = &cxlr->params; +struct preonline_hwpoison_range *ranges =NULL ; +struct preonline_hwpoison *handle; +unsigned int nr_ranges =0 ; +unsigned long nr_frames =0 ; +struct xarray pfns; +int rc =0 ; + +lockdep_assert_held (&cxl_rwsem.region); +lockdep_assert_held (&cxl_rwsem.dpa); + +/* The region may have been decommitted while the lock was dropped. */ +if (p->state <CXL_CONFIG_COMMIT ) +return -ENXIO ; + +if (test_bit (CXL_REGION_F_NORMALIZED_ADDRESSING , &cxlr->flags)) { +dev_info (&cxlr->dev, +"normalized addressing, poison not translated\n" ); +return 0 ; + } + +xa_init (&pfns); + +for (int i =0 ; i < p->nr_targets; i++) { +struct cxl_endpoint_decoder *cxled = p->targets[i]; + +if (!cxled->dpa_res || !resource_size (cxled->dpa_res)) +continue ; + rc =cxl_region_collect_decoder_poison (cxlr, cxled, &pfns); +if (rc) + goto out; + } + +if (xa_empty (&pfns)) + goto out; + + rc =cxl_pfns_to_ranges (&pfns, &ranges, &nr_ranges); +if (rc) + goto out; + + rc =preonline_hwpoison_register (ranges, nr_ranges, &handle); +if (rc == -EOPNOTSUPP ) { +/* No poison tracking in this kernel; nothing to withhold. */ + rc =0 ; + goto out; + } +if (rc)drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3332
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+ goto out; + +for (unsigned int i =0 ; i < nr_ranges; i++) + nr_frames += ranges[i].nr_pages; +dev_warn (&cxlr->dev, +"withholding %lu frame(s) reported poisoned by the device\n" , + nr_frames); + + rc =devm_add_action_or_reset (&cxlr->dev, cxl_unregister_device_poison, + handle);Hi Shaikh,
Quoted 6 lines
I think you are trying to propose the architecture as the following ?
- CXL region driver collects the device's poison info and owns the registration
- MM use the registration to keep poisoned pages out of the page allocator when memory is onlined
- DAX/kmem adds and removes the corresponding system RAM
Yes, that's how the RFC works today. If we go with Dave's suggestion below, only the first point changes: CXL only provides the entries, and MM owns them.
Quoted 8 lines
The registration belongs to the driver, but the memory that needs it can outlive the driver binding.
Unbinding the driver doesn't necessarily remove the memory. If some mem blocks are still online, removal of the range can fail.
When driver cleanup removes the registration, MM loses poison info of those memory blocks as well.
You're right. If any block is still online, dev_dax_kmem_remove() can't remove the range and leaves all its blocks in place, but the devm action still drops the entries. Blocks that were online are fine, their bad frames are already marked. The problem is a block that was offline: it can be onlined later, and its bad frame then goes to the buddy.
Quoted 3 lines
I don't have a good idea how we should re-design the ownership here, maybe we can discuss about it.
Hand the poison info to MM when the memory is added. The driver's registration only has to live until add_memory_driver_managed(). At that point MM copies the registered ranges inside the added range and keeps them with the memory blocks (or the memory group dax/kmem already registers), and frees them when the memory is removed. After that the driver binding doesn't matter. Hook A already makes its decision at add time, so both hooks would read MM's copy.
Does that sound right? If so I'll do it this way in v2.
Quoted 49 lines
Best regards,
Richard Cheng.Quoted 43 lines
drivers/cxl/core/region.c +30
-0+out: +kvfree (ranges); +xa_destroy (&pfns); +return rc; +} + +static int cxl_region_scan_device_poison (struct cxl_region *cxlr) +{ +int rc; + +/* Nothing would consume the result. */ +if (!IS_ENABLED (CONFIG_MEMORY_FAILURE )) +return 0 ; + +ACQUIRE (rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); + rc =ACQUIRE_ERR (rwsem_read_intr, ®ion_rwsem); +if (rc) +return rc; +ACQUIRE (rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); + rc =ACQUIRE_ERR (rwsem_read_intr, &dpa_rwsem); +if (rc) +return rc; + +return cxl_region_register_device_poison (cxlr); +} +struct cxl_dpa_to_region_context {struct cxl_region *cxlr;u64 dpa;@@ -4262,6 +4580,10 @@ static int cxl_region_probe(struct device *dev) p->res->start, p->res->end, cxlr, is_system_ram) > 0)return 0 ; + rc =cxl_region_scan_device_poison (cxlr); +if (rc) +return rc; +return devm_cxl_add_dax_region (cxlr);default : dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [RFC PATCH 5/5] cxl/region: Register device poison before exposing RAM
In reply to Shaikh KamaluddinQuoted 11 lines · On Sat, 10 Oct 2026 21:50:13 +0530 Shaikh Kamaluddin…
The
trigger_poison_listinterface may retrieve poison after CXL memory is already online, which is too late to prevent known-bad frames from entering the page allocator.During RAM-region probe, retrieve a poison-list snapshot for each participating endpoint decoder. Validate each record against the decoder's assigned DPA range, translate its poisoned DPA units to HPA and PFN, and collect the affected PFNs in an xarray. Coalesce consecutive PFNs and register the resulting ranges with the pre-online hwpoison registry before creating the DAX region.
Quoted 6 lines
When Get Poison List is supported, abort region probe if the list is incomplete, malformed, or cannot be translated. If volatile poison-list retrieval is unsupported, preserve the existing behavior and warn that poison cannot be withheld before memory onlining.
Signed-off-by Shaikh Kamaluddin <[email protected]>
A few comments inline. I wouldn't rush to revise this though as I think the whole concept needs more input from others.
Jonathan
Quoted 19 lines
--- drivers/cxl/core/region.c | 322 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+)drivers/cxl/core/region.c +1
-0@@ -14,6 +14,7 @@#include <linux/string_choices.h> #include <cxlmem.h> #include <cxl.h> +#include <linux/memory-failure.h> #include "core.h" #include "mce.h" @@ -3038,6 +3039,323 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port)return rc; }
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3050
Quoted 10 lines
drivers/cxl/core/region.c +9
-0+/* +* Device-reported poison is gathered at RAM-region probe, before the range +* reaches dax/kmem, and registered with the pre-online registry so the +* frames it covers are withheld when the memory is onlined. +*/ +static int cxl_poison_store_pfn (struct xarray *pfns,u64 hpa) +{ +return xa_err (xa_store (pfns,PHYS_PFN (hpa),xa_mk_value (1 ), +GFP_KERNEL ));
Fix alignment of the line above. Right now it isn't visually clear which function call the parameter is for.
Quoted 16 lines
drivers/cxl/core/region.c +15
-0+} +static int cxl_poison_record_to_pfns (struct cxl_region *cxlr, +struct cxl_memdev *cxlmd, +const struct cxl_poison_record *record, +struct xarray *pfns) +{ +struct cxl_region_params *p = &cxlr->params; +u64 raw =le64_to_cpu (record->address); +u64 units =le32_to_cpu (record->length); +u64 dpa = raw &CXL_POISON_START_MASK ; +u8 source = raw &CXL_POISON_SOURCE_MASK ; +u64 length, end, step, cur; + + if (!units || + check_mul_overflow(units, (u64)CXL_POISON_LEN_MULT, &length) ||
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3101
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+ check_add_overflow(dpa, length, &end)) +return -EPROTO ; + +/* +* A corrected error never reaches the poison list, so every record +* is uncorrectable. Only a failure of the device's own media is +* withheld: External poison was written by the host into healthy +* media and a rewrite or Clear Poison recovers it, and Injected +* poison is a test artifact. Withholding either would make a +* recoverable state permanent.
This reflects back to the comment I made in the cover letter. Device discovered poison can be ephemeral. I'm also not sure for volatile where any poison record would come from if you are doing this for newly powered up devices - they might scan whilst initializing but might not as that is very very slow to do. Typically poison is found much later by patrol scrub (or synchronous read). This may be more interesting for persistent memory where the persistence is used, but you aren't addressing that as far as I can tell.
Quoted 47 lines
drivers/cxl/core/region.c +47
-0+*/ +if (source !=CXL_POISON_SOURCE_INTERNAL ) { +dev_dbg (&cxlr->dev,"%s: dpa %#llx source %u not withheld\n" , +dev_name (&cxlmd->dev), dpa, source); +return 0 ; + } + +/* +* Translate once per unit of HPA contiguity rather than per 64 +* bytes. Consecutive DPAs within an interleave granule map to +* consecutive HPAs, and a granule no larger than a page lies within +* one page, so stepping by min(granule, PAGE_SIZE) from an aligned +* start reaches every page the record covers. +*/ + step =clamp_t (u64, p->interleave_granularity,CXL_POISON_LEN_MULT , +PAGE_SIZE ); + +for (cur =ALIGN_DOWN (dpa, step); cur < end; cur += step) { +u64 hpa =cxl_dpa_to_hpa (cxlr, cxlmd,max (cur, dpa)); +int rc; + +/* Not mapped by this region; nothing here to withhold. */ +if (hpa ==ULLONG_MAX ) +continue ; + + rc =cxl_poison_store_hpa (cxlr, pfns, hpa); +if (rc) +return rc; + } + +return 0 ; +} + +static int cxl_region_collect_decoder_poison (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +struct xarray *pfns) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_poison_snapshot snapshot; +int rc; + + rc =cxl_poison_snapshot_init (cxlmd, &snapshot); +if (rc == -EOPNOTSUPP ) { +dev_info (&cxlr->dev, +"%s: no Get Poison List support, nothing withheld\n" , +dev_name (&cxlmd->dev)); +return 0 ;
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3158
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+ } +if (rc) +return rc; + + rc =cxl_get_poison_by_decoder (cxled, &snapshot, +CXL_POISON_QUERY_PREONLINE ); +if (rc == -EFAULT ) { +/* +* The device keeps no poison list for volatile ranges, which +* CXL r3.2 8.2.9.8.4.1 permits. Online the region as today.
Use latest available spec for references (they don't make old ones easy to download!)
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3162
Quoted 4 lines
drivers/cxl/core/region.c +4
-0+*/ +dev_info (&cxlr->dev, +"%s: no volatile poison list, nothing withheld\n" , +dev_name (&cxlmd->dev));
too noisy.
Quoted 8 lines
drivers/cxl/core/region.c +8
-0+ rc =0 ; + goto out; + } +if (rc) + goto out; + +/* +* Withholding from a partial list would online the region on the
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3180
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+* belief that every bad frame had been named. +*/ +if (snapshot.truncated || + (snapshot.device_flags &CXL_POISON_FLAG_OVERFLOW )) { +dev_err (&cxlr->dev,"%s: poison list incomplete\n" , +dev_name (&cxlmd->dev)); + rc = -EOVERFLOW ; + goto out; + } +if (snapshot.device_flags &CXL_POISON_FLAG_SCANNING ) {
That one seems wrong. We don't prevent memory from coming online because there might be poison to be found.
Quoted 46 lines
drivers/cxl/core/region.c +46
-0+dev_err (&cxlr->dev,"%s: media scan in progress\n" , +dev_name (&cxlmd->dev)); + rc = -EBUSY ; + goto out; + } + +for (u32 i =0 ; i < snapshot.nr_records; i++) { + rc =cxl_poison_record_to_pfns (cxlr, cxlmd, + &snapshot.records[i], pfns); +if (rc) +break ; + } +out: +cxl_poison_snapshot_destroy (&snapshot); +return rc; +} + +static unsigned int cxl_count_poison_ranges (struct xarray *pfns) +{ +unsigned long index, previous =0 ; +unsigned int nr =0 ; +void *entry; +bool first = true; + +xa_for_each (pfns, index, entry) { +if (first || index != previous +1 ) + nr++; + first = false; + previous = index; + } +return nr; +} + +/* +* The xarray holds each PFN once and xa_for_each() walks them in ascending +* order, so runs of consecutive indices are exactly the sorted, +* nonoverlapping ranges preonline_hwpoison_register() requires. +*/ +static int cxl_pfns_to_ranges (struct xarray *pfns, +struct preonline_hwpoison_range **ranges_out, +unsigned int *nr_ranges_out) +{ +struct preonline_hwpoison_range *ranges; +unsigned long index, previous =0 , run_start =0 ; +unsigned int nr_ranges, nr =0 ; +void *entry;
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3236
Quoted 10 lines
drivers/cxl/core/region.c +10
-0+bool first = true; + + nr_ranges =cxl_count_poison_ranges (pfns); +if (!nr_ranges) { + *ranges_out =NULL ; + *nr_ranges_out =0 ; +return 0 ; + } + + ranges =kvmalloc_array (nr_ranges,sizeof (*ranges),GFP_KERNEL );
kvmalloc_objs()
Quoted 101 lines
drivers/cxl/core/region.c +101
-0+if (!ranges) +return -ENOMEM ; + +xa_for_each (pfns, index, entry) { +if (first) { + run_start = index; + previous = index; + first = false; +continue ; + } +if (index == previous +1 ) { + previous = index; +continue ; + } + ranges[nr++] = (struct preonline_hwpoison_range ) { + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + run_start = index; + previous = index; + } + ranges[nr++] = (struct preonline_hwpoison_range ){ + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + +WARN_ON_ONCE (nr != nr_ranges); + *ranges_out = ranges; + *nr_ranges_out = nr; +return 0 ; +} + +static void cxl_unregister_device_poison (void *data) +{ +preonline_hwpoison_unregister (data); +} + +static int cxl_region_register_device_poison (struct cxl_region *cxlr) +{ +struct cxl_region_params *p = &cxlr->params; +struct preonline_hwpoison_range *ranges =NULL ; +struct preonline_hwpoison *handle; +unsigned int nr_ranges =0 ; +unsigned long nr_frames =0 ; +struct xarray pfns; +int rc =0 ; + +lockdep_assert_held (&cxl_rwsem.region); +lockdep_assert_held (&cxl_rwsem.dpa); + +/* The region may have been decommitted while the lock was dropped. */ +if (p->state <CXL_CONFIG_COMMIT ) +return -ENXIO ; + +if (test_bit (CXL_REGION_F_NORMALIZED_ADDRESSING , &cxlr->flags)) { +dev_info (&cxlr->dev, +"normalized addressing, poison not translated\n" ); +return 0 ; + } + +xa_init (&pfns); + +for (int i =0 ; i < p->nr_targets; i++) { +struct cxl_endpoint_decoder *cxled = p->targets[i]; + +if (!cxled->dpa_res || !resource_size (cxled->dpa_res)) +continue ; + rc =cxl_region_collect_decoder_poison (cxlr, cxled, &pfns); +if (rc) + goto out; + } + +if (xa_empty (&pfns)) + goto out; + + rc =cxl_pfns_to_ranges (&pfns, &ranges, &nr_ranges); +if (rc) + goto out; + + rc =preonline_hwpoison_register (ranges, nr_ranges, &handle); +if (rc == -EOPNOTSUPP ) { +/* No poison tracking in this kernel; nothing to withhold. */ + rc =0 ; + goto out; + } +if (rc) + goto out; + +for (unsigned int i =0 ; i < nr_ranges; i++) + nr_frames += ranges[i].nr_pages; +dev_warn (&cxlr->dev, +"withholding %lu frame(s) reported poisoned by the device\n" , + nr_frames); + + rc =devm_add_action_or_reset (&cxlr->dev, cxl_unregister_device_poison, + handle); +out: +kvfree (ranges); +xa_destroy (&pfns); +return rc; +}
Re: [RFC PATCH 5/5] cxl/region: Register device poison before exposing RAM
Quoted 14 lines · Jonathan Cameron
Quoted 11 lines · On Sat, 10 Oct 2026 21:50:13 +0530 Shaikh Kamaluddin…
The
trigger_poison_listinterface may retrieve poison after CXL memory is already online, which is too late to prevent known-bad frames from entering the page allocator.During RAM-region probe, retrieve a poison-list snapshot for each participating endpoint decoder. Validate each record against the decoder's assigned DPA range, translate its poisoned DPA units to HPA and PFN, and collect the affected PFNs in an xarray. Coalesce consecutive PFNs and register the resulting ranges with the pre-online hwpoison registry before creating the DAX region.
Quoted 9 lines
Quoted 6 lines
When Get Poison List is supported, abort region probe if the list is incomplete, malformed, or cannot be translated. If volatile poison-list retrieval is unsupported, preserve the existing behavior and warn that poison cannot be withheld before memory onlining.
Signed-off-by Shaikh Kamaluddin <[email protected]>A few comments inline. I wouldn't rush to revise this though as I think the whole concept needs more input from others.
Hi Jonathan,
Agreed. There are open comments on the mm side too (3/5 and 4/5), so I will get the design settled before sending v2.
Quoted 22 lines
Jonathan
Quoted 20 lines
--- drivers/cxl/core/region.c | 322 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+)drivers/cxl/core/region.c +1
-0@@ -14,6 +14,7 @@#include <linux/string_choices.h> #include <cxlmem.h> #include <cxl.h> +#include <linux/memory-failure.h> #include "core.h" #include "mce.h" @@ -3038,6 +3039,323 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port)return rc; }
Quoted 13 lines
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3050
Quoted 9 lines
drivers/cxl/core/region.c +9
-0+/* +* Device-reported poison is gathered at RAM-region probe, before the range +* reaches dax/kmem, and registered with the pre-online registry so the +* frames it covers are withheld when the memory is onlined. +*/ +static int cxl_poison_store_pfn (struct xarray *pfns,u64 hpa) +{ +return xa_err (xa_store (pfns,PHYS_PFN (hpa),xa_mk_value (1 ), +GFP_KERNEL ));Fix alignment of the line above. Right now it isn't visually clear which function call the parameter is for.
Will fix.
Quoted 21 lines
Quoted 21 lines
drivers/cxl/core/region.c +20
-0+} +static int cxl_poison_record_to_pfns (struct cxl_region *cxlr, +struct cxl_memdev *cxlmd, +const struct cxl_poison_record *record, +struct xarray *pfns) +{ +struct cxl_region_params *p = &cxlr->params; +u64 raw =le64_to_cpu (record->address); +u64 units =le32_to_cpu (record->length); +u64 dpa = raw &CXL_POISON_START_MASK ; +u8 source = raw &CXL_POISON_SOURCE_MASK ; +u64 length, end, step, cur; + +if (!units || +check_mul_overflow (units, (u64 )CXL_POISON_LEN_MULT , &length) || +check_add_overflow (dpa, length, &end)) +return -EPROTO ; + +/* +* A corrected error never reaches the poison list, so every record
Quoted 8 lines
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3101
Quoted 5 lines
drivers/cxl/core/region.c +5
-0+* is uncorrectable. Only a failure of the device's own media is +* withheld: External poison was written by the host into healthy +* media and a rewrite or Clear Poison recovers it, and Injected +* poison is a test artifact. Withholding either would make a +* recoverable state permanent. This reflects back to the comment I made in the cover letter. Device discovered poison can be ephemeral.
Agreed. If the error was only temporary, the next write would fix it. But nothing ever writes a withheld frame, so it stays on the list and we keep withholding it forever.
Quoted 5 lines
I'm also not sure for volatile
where any poison record would come from if you are doing this for newly powered up devices - they might scan whilst initializing but might not as that is very very slow to do. Typically poison is found much later by patrol scrub (or synchronous read).
A real media fault is still there after a power cycle, which is why PPR exists, at boot or at runtime. But on a volatile device the poison list may not remember it: until patrol scrub or a read finds it again, the list can be empty. So this mainly helps when the device stays powered while the host restarts, like kexec or a driver re-bind, or with a device that keeps its error records across a power cycle.
Quoted 4 lines
This may be more interesting for
persistent memory where the persistence is used, but you aren't addressing that as far as I can tell.
Agreed, that is the stronger case. Today the CXL driver doesn't feed the poison list into the nvdimm bad ranges at all, so pmem and devdax on CXL don't see known-bad locations either. cxl_pmem_ctl() also returns -ENOTTY for ND_CMD_CLEAR_ERROR, so a bad block couldn't be cleared by a write even if it were listed.
I will look at both as a separate series: bad ranges from the poison list at pmem region probe, plus clear-error support. kmem could then use the same list when a pmem range becomes system RAM.
Quoted 52 lines
Quoted 52 lines
drivers/cxl/core/region.c +52
-0+*/ +if (source !=CXL_POISON_SOURCE_INTERNAL ) { +dev_dbg (&cxlr->dev,"%s: dpa %#llx source %u not withheld\n" , +dev_name (&cxlmd->dev), dpa, source); +return 0 ; + } + +/* +* Translate once per unit of HPA contiguity rather than per 64 +* bytes. Consecutive DPAs within an interleave granule map to +* consecutive HPAs, and a granule no larger than a page lies within +* one page, so stepping by min(granule, PAGE_SIZE) from an aligned +* start reaches every page the record covers. +*/ + step =clamp_t (u64, p->interleave_granularity,CXL_POISON_LEN_MULT , +PAGE_SIZE ); + +for (cur =ALIGN_DOWN (dpa, step); cur < end; cur += step) { +u64 hpa =cxl_dpa_to_hpa (cxlr, cxlmd,max (cur, dpa)); +int rc; + +/* Not mapped by this region; nothing here to withhold. */ +if (hpa ==ULLONG_MAX ) +continue ; + + rc =cxl_poison_store_hpa (cxlr, pfns, hpa); +if (rc) +return rc; + } + +return 0 ; +} + +static int cxl_region_collect_decoder_poison (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +struct xarray *pfns) +{ +struct cxl_memdev *cxlmd =cxled_to_memdev (cxled); +struct cxl_poison_snapshot snapshot; +int rc; + + rc =cxl_poison_snapshot_init (cxlmd, &snapshot); +if (rc == -EOPNOTSUPP ) { +dev_info (&cxlr->dev, +"%s: no Get Poison List support, nothing withheld\n" , +dev_name (&cxlmd->dev)); +return 0 ; + } +if (rc) +return rc; + + rc = cxl_get_poison_by_decoder(cxled, &snapshot,
Quoted 9 lines
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3158
Quoted 5 lines
drivers/cxl/core/region.c +5
-0+ CXL_POISON_QUERY_PREONLINE); +if (rc == -EFAULT ) { +/* +* The device keeps no poison list for volatile ranges, which +* CXL r3.2 8.2.9.8.4.1 permits. Online the region as today. Use latest available spec for references (they don't make old ones easy to download!)
Will update the latest references.
Quoted 6 lines
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3162
Quoted 4 lines
drivers/cxl/core/region.c +4
-0+*/ +dev_info (&cxlr->dev, +"%s: no volatile poison list, nothing withheld\n" , +dev_name (&cxlmd->dev));too noisy.
Will drop it.
Quoted 13 lines
Quoted 13 lines
drivers/cxl/core/region.c +13
-0+ rc =0 ; + goto out; + } +if (rc) + goto out; + +/* +* Withholding from a partial list would online the region on the +* belief that every bad frame had been named. +*/ +if (snapshot.truncated || + (snapshot.device_flags &CXL_POISON_FLAG_OVERFLOW )) { + dev_err(&cxlr->dev, "%s: poison list incomplete\n",
Quoted 8 lines
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3180
Quoted 5 lines
drivers/cxl/core/region.c +5
-0+ dev_name(&cxlmd->dev)); + rc = -EOVERFLOW ; + goto out; + } +if (snapshot.device_flags &CXL_POISON_FLAG_SCANNING ) {That one seems wrong. We don't prevent memory from coming online because there might be poison to be found.
Agreed, I will drop that check. Anything the scan finds later is handled like any other poison found at runtime.
Quoted 51 lines
Quoted 51 lines
drivers/cxl/core/region.c +50
-0+dev_err (&cxlr->dev,"%s: media scan in progress\n" , +dev_name (&cxlmd->dev)); + rc = -EBUSY ; + goto out; + } + +for (u32 i =0 ; i < snapshot.nr_records; i++) { + rc =cxl_poison_record_to_pfns (cxlr, cxlmd, + &snapshot.records[i], pfns); +if (rc) +break ; + } +out: +cxl_poison_snapshot_destroy (&snapshot); +return rc; +} + +static unsigned int cxl_count_poison_ranges (struct xarray *pfns) +{ +unsigned long index, previous =0 ; +unsigned int nr =0 ; +void *entry; +bool first = true; + +xa_for_each (pfns, index, entry) { +if (first || index != previous +1 ) + nr++; + first = false; + previous = index; + } +return nr; +} + +/* +* The xarray holds each PFN once and xa_for_each() walks them in ascending +* order, so runs of consecutive indices are exactly the sorted, +* nonoverlapping ranges preonline_hwpoison_register() requires. +*/ +static int cxl_pfns_to_ranges (struct xarray *pfns, +struct preonline_hwpoison_range **ranges_out, +unsigned int *nr_ranges_out) +{ +struct preonline_hwpoison_range *ranges; +unsigned long index, previous =0 , run_start =0 ; +unsigned int nr_ranges, nr =0 ; +void *entry; +bool first = true; + + nr_ranges =cxl_count_poison_ranges (pfns); +if (!nr_ranges) {
Quoted 9 lines
drivers/cxl/core/region.c int cxl_get_poison_by_endpoint(struct cxl_port *port) · line 3236
Quoted 6 lines
drivers/cxl/core/region.c +6
-0+ *ranges_out =NULL ; + *nr_ranges_out =0 ; +return 0 ; + } + + ranges =kvmalloc_array (nr_ranges,sizeof (*ranges),GFP_KERNEL );
kvmalloc_objs()
Will switch.
Thanks,
Shaikh Kamal
Quoted 103 lines
Quoted 101 lines
drivers/cxl/core/region.c +101
-0+if (!ranges) +return -ENOMEM ; + +xa_for_each (pfns, index, entry) { +if (first) { + run_start = index; + previous = index; + first = false; +continue ; + } +if (index == previous +1 ) { + previous = index; +continue ; + } + ranges[nr++] = (struct preonline_hwpoison_range ) { + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + run_start = index; + previous = index; + } + ranges[nr++] = (struct preonline_hwpoison_range ){ + .start_pfn = run_start, + .nr_pages = previous - run_start +1 , + }; + +WARN_ON_ONCE (nr != nr_ranges); + *ranges_out = ranges; + *nr_ranges_out = nr; +return 0 ; +} + +static void cxl_unregister_device_poison (void *data) +{ +preonline_hwpoison_unregister (data); +} + +static int cxl_region_register_device_poison (struct cxl_region *cxlr) +{ +struct cxl_region_params *p = &cxlr->params; +struct preonline_hwpoison_range *ranges =NULL ; +struct preonline_hwpoison *handle; +unsigned int nr_ranges =0 ; +unsigned long nr_frames =0 ; +struct xarray pfns; +int rc =0 ; + +lockdep_assert_held (&cxl_rwsem.region); +lockdep_assert_held (&cxl_rwsem.dpa); + +/* The region may have been decommitted while the lock was dropped. */ +if (p->state <CXL_CONFIG_COMMIT ) +return -ENXIO ; + +if (test_bit (CXL_REGION_F_NORMALIZED_ADDRESSING , &cxlr->flags)) { +dev_info (&cxlr->dev, +"normalized addressing, poison not translated\n" ); +return 0 ; + } + +xa_init (&pfns); + +for (int i =0 ; i < p->nr_targets; i++) { +struct cxl_endpoint_decoder *cxled = p->targets[i]; + +if (!cxled->dpa_res || !resource_size (cxled->dpa_res)) +continue ; + rc =cxl_region_collect_decoder_poison (cxlr, cxled, &pfns); +if (rc) + goto out; + } + +if (xa_empty (&pfns)) + goto out; + + rc =cxl_pfns_to_ranges (&pfns, &ranges, &nr_ranges); +if (rc) + goto out; + + rc =preonline_hwpoison_register (ranges, nr_ranges, &handle); +if (rc == -EOPNOTSUPP ) { +/* No poison tracking in this kernel; nothing to withhold. */ + rc =0 ; + goto out; + } +if (rc) + goto out; + +for (unsigned int i =0 ; i < nr_ranges; i++) + nr_frames += ranges[i].nr_pages; +dev_warn (&cxlr->dev, +"withholding %lu frame(s) reported poisoned by the device\n" , + nr_frames); + + rc =devm_add_action_or_reset (&cxlr->dev, cxl_unregister_device_poison, + handle); +out: +kvfree (ranges); +xa_destroy (&pfns); +return rc; +}
Jonathan Cameron
maintainerQuoted 3 lines · On Sat, 10 Oct 2026 21:50:08 +0530 Shaikh Kamaluddin…
A CXL memory device keeps its own poison list: the DPAs it knows to be bad. The list lives on the device, so it survives a host reboot, a power cycle, and moving the device to another host.
That is a bit too general + needs backing with specific spec references. I'm not sure for instance if there are any guarantees around poison lists for volatile as some errors detected by local ECC will be intermittent anyway.
I may have forgotten it but I don't recall the CXL spec talking at all about any long term tracking of errors in volatile memory across power cycles. There are interfaces to test memory to detect stuck bits but I don't think there is any requirement to run them at device boot (and we haven't enabled them in linux yet as on demand things).
Quoted 27 lines
Linux reads that list only when user space writes
trigger_poison_list. Nothing reads it when a region is assembled, so with a device that already has poison:1. the RAM region is assembled and dax/kmem onlines it;
2. every frame enters the buddy allocator, including the ones thedevice would have named had anyone asked;3. a task is handed one and reads it: machine check,
memory_failure(), SIGBUS.This series reads the poison list at region probe, before the range is onlined, and keeps the frames it covers out of the allocator. Nothing is unmapped or signalled, and no runtime path changes: the frames never enter the allocator, so this is not forwarding latent poison to
memory_failure().Dependency
==========This is built on Breno Leitao's hwpoison series [1], which keeps frames poisoned before a kexec out of the next kernel's allocator. The approach here, withholding frames as they are onlined rather than taking them back out later, follows [1]. [1] hooks
__free_pages_core(), splits a block around its bad frames, and marks them withhwpoison_boot_page(). Patch 4 adds a second source to that hook.
Quoted 5 lines
The two cover different cases. [1] records frames in a bitmap spanning the RAM the EFI memory map describes. Memory hot-added after boot sits outside it, and CXL region memory is hot-added after boot. [1] also carries its record across kexec only, while the device's list survives reboot and migration.
The CXL region - may be hot added after boot. Whilst I like that model, on many existing systems the CXL subsystem is just identifying what was already there.
Quoted 8 lines
Applies on next-20260908 plus [1] v5. The
num_poisoned_pages_inc()change agreed for v6 [2] does not touch these patches. Patches 1, 2 and 5 touch onlydrivers/cxl; patches 3 and 4 touch mm.Design
======
Quoted 6 lines
Which records are withheld. A corrected error never reaches the poison list, so every record is uncorrectable. Only records with source Internal, a failure of the device's own media, are withheld. External poison was written by the host into healthy media and a rewrite recovers it; Injected poison comes from the Inject Poison test command. Withholding either would make a recoverable state permanent.
At best a detection of an ECC error in local media is a hint that it might not be cleared by a suitable full cache line write.
Quoted 3 lines
No host-side storage. The device is the source of truth and is re-read on every probe. When a location is repaired (sPPR, sparing, hPPR) the
It might well be repaired just fine by a host write assuming the CPU arch supports clearing poison that way. Ideally we'd poke it some to find out whether it is recoverable.
Quoted 15 lines
device drops it from the list, and the next probe onlines the frame again. A record persisted by the host could not learn about the repair.
Volatile ranges. CXL r3.2 8.2.9.8.4.1:
If the device does not support poison list for volatile ranges and any location in the requested list maps to volatile, the device shall return Invalid Physical Address.
A device that returns it, or that has no Get Poison List at all, is onlined as today with nothing withheld. This is also the reason for the RAM
-EFAULTtolerancepoison_by_decoder()has carried, without a comment, sincef0832a586396("cxl/region: Provide region info to thecxl_poisontrace event").
Quoted 3 lines
Incomplete lists. If the device reports overflow, the host stops at
max_errorswith more records pending, or a media scan is in progress, region probe fails rather than onlining on a partial list.
This bit needs shout more about as you just stopped region probe on regions that previously probed (but would potentially return poison on some memory). I'd put a Note: Will stop regions probing if poison list has overflowed. right at the top of this cover letter.
I'm not necessarily against this. I need to think a bit more on the impacts.
Certainly an interesting patch set.
Quoted 2 lines
prerequisite-patch-id:
a23fe88ab4529b6a6669a991e15cf1631311391c
prerequisite-patch-id:111c69880ed0e148cdda285896c006e8d1fc98a8
Alison Schofield
maintainerOn Sat, Oct 10, 2026 at 09:50:08PM +0530, Shaikh Kamaluddin wrote:
snip
Quoted 4 lines
Applies on next-20260908 plus [1] v5. The
num_poisoned_pages_inc()change agreed for v6 [2] does not touch these patches. Patches 1, 2 and 5 touch onlydrivers/cxl; patches 3 and 4 touch mm.
Hi Shaikh,
Can you share a public branch with all this code in one place?
-- Alison
snip
Quoted 15 lines
base-commit:
a9d7ced84989ec05be09b4b8428759ef60450a0f
prerequisite-patch-id:fe9d44deb8ccc48c0311bc4131bdf733eb353b34
prerequisite-patch-id:4cabe6adb37cd8c218bd43c97970bc65f313b671
prerequisite-patch-id:52ad24bceedb9c03df167f10573166a1d97ba97d
prerequisite-patch-id:44bfbc8aa3c4b19593f7b0d466fbc0cb99b99f76
prerequisite-patch-id:2d344a5322ff07c7895a59d2425540a30e4e0630
prerequisite-patch-id:e776f306dcbdda3f6972187ad1e43a62c2754890
prerequisite-patch-id:3ef887599e31ef973d6d9ca15d0462d55f5f0409
prerequisite-patch-id:a23fe88ab4529b6a6669a991e15cf1631311391c
prerequisite-patch-id:111c69880ed0e148cdda285896c006e8d1fc98a8Signature · 1 line
2.43.0
Quoted 9 lines · Alison Schofield
On Sat, Oct 10, 2026 at 09:50:08PM +0530, Shaikh Kamaluddin wrote:
snip
Quoted 4 lines
Applies on next-20260908 plus [1] v5. The
num_poisoned_pages_inc()change agreed for v6 [2] does not touch these patches. Patches 1, 2 and 5 touch onlydrivers/cxl; patches 3 and 4 touch mm.
Quoted 22 lines
Hi Shaikh,
Can you share a public branch with all this code in one place?
-- Alison
snip
Quoted 15 lines
base-commit:
a9d7ced84989ec05be09b4b8428759ef60450a0f
prerequisite-patch-id:fe9d44deb8ccc48c0311bc4131bdf733eb353b34
prerequisite-patch-id:4cabe6adb37cd8c218bd43c97970bc65f313b671
prerequisite-patch-id:52ad24bceedb9c03df167f10573166a1d97ba97d
prerequisite-patch-id:44bfbc8aa3c4b19593f7b0d466fbc0cb99b99f76
prerequisite-patch-id:2d344a5322ff07c7895a59d2425540a30e4e0630
prerequisite-patch-id:e776f306dcbdda3f6972187ad1e43a62c2754890
prerequisite-patch-id:3ef887599e31ef973d6d9ca15d0462d55f5f0409
prerequisite-patch-id:a23fe88ab4529b6a6669a991e15cf1631311391c
prerequisite-patch-id:111c69880ed0e148cdda285896c006e8d1fc98a8Signature · 1 line
2.43.0
Hi Alison,
Here's a branch with everything in one place:
https://github.com/shaikhkamal/linux/tree/cxl-poison-rfc-Breno-v5
(top: 9e63fdb "cxl/region: Register device poison before exposing RAM")
It's next-20260908 + Breno's hwpoison-kho v5 + this 5-patch RFC, matching what was posted.
Thanks,
Shaikh