perf/cxlpmu: Misc sashiko raised issues fixes
v2 cover perf/cxlpmu: Misc sashiko raised issues fixes
Teed off of Davidlohr's misc cxlpmu series [1]. I had Claude pick up all the sashiko raised issues and then continued multiple internal review iterations to pick up a number of fixes. I'm no CXL PMU or perf expert, but the fixes look reasonable to me AFAICT.
Please give extra scrutiny to patch 3 and ensure the math is correct.
Since v1:
- Updated 3/9 to address sashiko issue.
- No other changes as sashiko only raised issue with pre-existing.
[1]: https://lore.kernel.org/linux-cxl/[email protected]/
Dave Jiang (9):
perf/cxl: Program the requested event group on configurable counters perf/cxl: Clear stale event fields before reprogramming a counter perf/cxl: Fix the counter overflow delta fixup
perf/cxl: Accept an overflow interrupt on MSI message number 0 perf/cxl: Keep the overflow interrupt pinned to the managed CPU perf/cxl: Unfreeze counters after handling an overflow interrupt perf/cxl: Validate the hardware-reported counter width perf/cxl: Don't use pmu.dev in IRQ and hotplug callbacks after
unregister
perf/cxl: Avoid cpumask_of(-1) when no CPU is assigned
drivers/perf/cxl_pmu.c | 61 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 11 deletions(-)
base-commit: f5098b6bae761e346ebcd9da7f95622c04733cff
Signature · 1 line
2.55.0
PATCH v2 1/9 perf/cxl: Program the requested event group on configurable counters
cxl_pmu_get_event_idx() sets *counter_idx for a configurable counter but never sets *event_idx, so hwc->event_base stays 0 and cxl_pmu_event_start() programs event group 0 regardless of the group the user requested.
Return the matched capability's event_idx.
--- drivers/perf/cxl_pmu.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/perf/cxl_pmu.c +2 -0
@@ -552,6 +552,8 @@ static int cxl_pmu_get_event_idx(struct perf_event *event, int *counter_idx,return -EINVAL ; *counter_idx = i; +if (event_idx) + *event_idx = pmu_ev->event_idx;return 0 ; }
Signature · 1 line
2.55.0
PATCH v2 2/9 perf/cxl: Clear stale event fields before reprogramming a counter
In reply to Dave Jiangcxl_pmu_event_start() ORs the event group id, event mask, edge and invert selections into a configurable counter's config register without clearing those fields first, and cxl_pmu_event_stop() leaves them set. Reusing a counter for another event therefore ORs the new selection onto the old, counting the wrong events with stale edge/invert behaviour.
Clear EVENT_GRP_ID_IDX, EVENTS, EDGE and INVERT before applying the new values.
--- drivers/perf/cxl_pmu.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/perf/cxl_pmu.c +3 -0
@@ -641,6 +641,7 @@ static void cxl_pmu_event_start(struct perf_event *event, int flags) } cfg =readq (base +CXL_PMU_COUNTER_CFG_REG (hwc->idx)); + cfg &= ~(CXL_PMU_COUNTER_CFG_EDGE |CXL_PMU_COUNTER_CFG_INVERT ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_INT_ON_OVRFLW ,1 ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_FREEZE_ON_OVRFLW ,1 ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_ENABLE ,1 );
@@ -651,6 +652,8 @@ static void cxl_pmu_event_start(struct perf_event *event, int flags)/* Fixed purpose counters have next two fields RO */ if (test_bit (hwc->idx, info->conf_counter_bm)) { + cfg &= ~(CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK | +CXL_PMU_COUNTER_CFG_EVENTS_MSK ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK , hwc->event_base); cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EVENTS_MSK,
Signature · 1 line
2.55.0
PATCH v2 3/9 perf/cxl: Fix the counter overflow delta fixup
In reply to Dave JiangCounters are configured with Freeze on Overflow and are never reloaded: on overflow the counter wraps to 0, counts on until the CPMU freezes, and retains that residual (CXL r4.0 8.2.7.2.3). So the masked subtraction in __cxl_pmu_read() only spans the wrap when new_cnt < prev_cnt.
The fixup keys off the delta rather than the operands:
delta = (new_cnt - prev_cnt) & GENMASK_ULL(counter_width - 1, 0); if (overflow && delta < GENMASK_ULL(counter_width - 1, 0)) delta += (1UL << counter_width);
and is wrong both ways. After a mid-period read the subtraction already spans the wrap, but the guard holds for all but one delta value, so a second period is added and the event over-counts. After an earlier overflow read - or after event_start(), which leaves prev_count at 0 - prev_cnt is a small residual, and once the new residual is greater or equal only the difference between the two is counted and the period is lost, which for a residual of 0 means every period after the first. 'perf stat -I' hits the former, plain 'perf stat' the latter.
The shift is also undefined for counter_width == 64, and for >= 32 on 32-bit kernels where 1UL is 32 bits.
Condition the fixup on new_cnt >= prev_cnt, the one case the masked subtraction cannot express, and use mask + 1 for the period: it is 0 for a 64-bit counter, the correct sum mod 2^64, and needs no shift. A userspace read landing between the wrap and the handler clearing the overflow status stays correct because the counter is frozen and cannot advance between the two reads.
---
v2:
- Fix the fixup instead of deleting it. v1 dropped it on the rationale that the masked subtraction already handles the wrap, which only holds for new_cnt < prev_cnt; with freeze on overflow and no reload the overflow path routinely sees new_cnt >= prev_cnt, where v1 silently dropped a full period (sashiko-bot,
https://lore.kernel.org/linux-cxl/[email protected]/).
- Keep the overflow argument and the __cxl_pmu_read()/cxl_pmu_read() split; v1 folded them together once the argument went unused.
- Use mask + 1 rather than 1ULL << counter_width so the 64-bit counter case needs no special casing.
--- drivers/perf/cxl_pmu.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/perf/cxl_pmu.c +12 -6
@@ -690,7 +690,7 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) {struct cxl_pmu_info *info =pmu_to_cxl_pmu_info (event->pmu);struct hw_perf_event *hwc = &event->hw; -u64 new_cnt, prev_cnt, delta; +u64 new_cnt, prev_cnt, delta, mask; do { prev_cnt = local64_read(&hwc->prev_count);
@@ -698,12 +698,18 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) }while (local64_cmpxchg (&hwc->prev_count, prev_cnt, new_cnt) != prev_cnt);/* -* If we know an overflow occur then take that into account. -* Note counter is not reset as that would lose events +* The counter wraps to 0 and is never reloaded, so the masked unsigned +* subtraction only recovers a wrap when new_cnt < prev_cnt. On the +* overflow path new_cnt >= prev_cnt means the full period elapsed +* (prev_cnt is 0 from event_start(), or the residual left by an earlier +* overflow), so add the period back. mask + 1 is 2^counter_width, which +* evaluates to 0 for a 64-bit counter - the correct sum mod 2^64, and +* avoids the undefined 1 << 64. */ - delta = (new_cnt - prev_cnt) &GENMASK_ULL (info->counter_width -1 ,0 ); -if (overflow && delta <GENMASK_ULL (info->counter_width -1 ,0 )) - delta += (1UL << info->counter_width); + mask =GENMASK_ULL (info->counter_width -1 ,0 ); + delta = (new_cnt - prev_cnt) & mask; +if (overflow && new_cnt >= prev_cnt) + delta += mask +1 ;local64_add (delta, &event->count); }
Signature · 1 line
2.55.0
PATCH v2 4/9 perf/cxl: Accept an overflow interrupt on MSI message number 0
In reply to Dave Jiangcxl_pmu_probe() rejects the PMU with -EINVAL when info->irq <= 0, but info->irq holds the MSI/MSI-X message number the device signals overflow on (CXL_PMU_CAP_MSI_N_MSK, a 0-based 4-bit field), with -1 meaning "no interrupt support". Message number 0 is valid and pci_irq_vector() takes a 0-based index, so a compliant device signalling on the first vector is wrongly refused and fails to probe.
Reject only the no-interrupt case (info->irq < 0), matching the sibling CXL mailbox and event IRQ handling.
--- drivers/perf/cxl_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/perf/cxl_pmu.c +1 -1
@@ -876,7 +876,7 @@ static int cxl_pmu_probe(struct device *dev) .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }; -if (info->irq <=0 ) +if (info->irq <0 )return -EINVAL ; rc =pci_irq_vector (pdev, info->irq);
Signature · 1 line
2.55.0
PATCH v2 5/9 perf/cxl: Keep the overflow interrupt pinned to the managed CPU
In reply to Dave JiangThe PMU pins its overflow interrupt to info->on_cpu in the hotplug online/offline callbacks, but requests it with only IRQF_SHARED | IRQF_NO_THREAD. Without IRQF_NOBALANCING, irqbalance or a userspace smp_affinity write can move the interrupt to another CPU. cxl_pmu_irq() then runs cxl_pmu_read() there, doing local64_cmpxchg()/local64_add() on hwc->prev_count and event->count concurrently with the managing CPU; local64_t is only atomic against same-CPU access, so counts get corrupted.
Add IRQF_NOBALANCING so the pinning done in the hotplug callbacks holds, matching other uncore-style PMU drivers.
--- drivers/perf/cxl_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/perf/cxl_pmu.c +2 -1
@@ -888,7 +888,8 @@ static int cxl_pmu_probe(struct device *dev)if (!irq_name)return -ENOMEM ; - rc =devm_request_irq (dev, irq, cxl_pmu_irq,IRQF_SHARED |IRQF_NO_THREAD , + rc =devm_request_irq (dev, irq, cxl_pmu_irq, +IRQF_SHARED |IRQF_NO_THREAD |IRQF_NOBALANCING , irq_name, info);if (rc)return rc;
Signature · 1 line
2.55.0
PATCH v2 6/9 perf/cxl: Unfreeze counters after handling an overflow interrupt
In reply to Dave JiangThe counters are configured with Freeze on Overflow, so when any counter overflows the CPMU freezes every counter in the block (CXL r4.0 §8.2.7.2.1). cxl_pmu_irq() reads the overflowed counters and clears the overflow status, but never writes the CPMU Freeze register to unfreeze, so all counters stay frozen until the next pmu_enable() and events in that window are silently lost.
Unfreeze after clearing the overflow status so counting resumes.
--- drivers/perf/cxl_pmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/perf/cxl_pmu.c +9 -0
@@ -804,6 +804,15 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)writeq (overflowed, base +CXL_PMU_OVERFLOW_REG ); +/* +* Counters are configured to freeze on overflow (Freeze on Overflow), +* which freezes every counter in the CPMU. Once the overflowed counters +* have been read and their status cleared, unfreeze so counting resumes; +* otherwise all counters stay frozen until the next pmu_enable() and +* events are silently lost. +*/ +writeq (0 , base +CXL_PMU_FREEZE_REG ); +return IRQ_HANDLED ; }
Signature · 1 line
2.55.0
PATCH v2 7/9 perf/cxl: Validate the hardware-reported counter width
In reply to Dave Jiangcxl_pmu_parse_caps() takes the Counter Width straight from the CPMU Capability register with no bounds check. The Counter Data register is 64 bits, so a device reporting width 0 (or > 64) is invalid, and the read path's GENMASK_ULL(counter_width - 1, 0) would shift out of range (undefined behavior, e.g. a UBSAN splat) on a value of 0.
Reject a counter width outside 1..64 at probe.
--- drivers/perf/cxl_pmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/perf/cxl_pmu.c +9 -0
@@ -139,6 +139,15 @@ static int cxl_pmu_parse_caps(struct device *dev, struct cxl_pmu_info *info) info->num_counters =FIELD_GET (CXL_PMU_CAP_NUM_COUNTERS_MSK , val) +1 ; info->counter_width =FIELD_GET (CXL_PMU_CAP_COUNTER_WIDTH_MSK , val); +/* +* The Counter Data register is 64 bits wide, so a Counter Width of 0 or +* >64 is invalid. Reject it rather than let GENMASK_ULL(width - 1, 0) in +* the read path shift out of range. +*/ +if (info->counter_width ==0 || info->counter_width >64 ) { +dev_err (dev,"Invalid counter width %d\n" , info->counter_width); +return -ENODEV ; + } info->num_event_capabilities =FIELD_GET (CXL_PMU_CAP_NUM_EVN_CAP_REG_SUP_MSK , val) +1 ; info->filter_hdm =FIELD_GET (CXL_PMU_CAP_FILTERS_SUP_MSK , val) &CXL_PMU_FILTER_HDM ;
Signature · 1 line
2.55.0
PATCH v2 8/9 perf/cxl: Don't use pmu.dev in IRQ and hotplug callbacks after unregister
In reply to Dave JiangOn device removal the devm actions unwind LIFO, so cxl_pmu_perf_unregister() runs first and perf_pmu_unregister() frees info->pmu.dev (device_del() + put_device() -> kfree()). The overflow IRQ (freed last) and the CPU-hotplug instance (removed next) are still live at that point, and both cxl_pmu_irq() and cxl_pmu_offline_cpu() log via dev_dbg()/dev_err() on info->pmu.dev, dereferencing freed memory. The shared IRQ can be entered for a co-function on the same MSI vector, and a CPU can go offline in the window before the hotplug instance is removed.
Log through info->pmu.parent instead, the cxl_pmu device passed to probe, which is devm-managed and outlives every teardown action.
--- drivers/perf/cxl_pmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/perf/cxl_pmu.c +2 -2
@@ -803,7 +803,7 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)struct perf_event *event = info->hw_events[i];if (!event) { -dev_dbg (info->pmu.dev, +dev_dbg (info->pmu.parent,"overflow but on non enabled counter %d\n" , i);continue ; }
@@ -966,7 +966,7 @@ static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) info->on_cpu =-1 ; target =cpumask_any_but (cpu_online_mask, cpu);if (target >= nr_cpu_ids) { -dev_err (info->pmu.dev,"Unable to find a suitable CPU\n" ); +dev_err (info->pmu.parent,"Unable to find a suitable CPU\n" );return 0 ; }
Signature · 1 line
2.55.0
PATCH v2 9/9 perf/cxl: Avoid cpumask_of(-1) when no CPU is assigned
In reply to Dave Jiangcpumask_show() feeds info->on_cpu straight into cpumask_of() for the world-readable cpumask sysfs attribute. on_cpu is -1 before the first hotplug online callback and transiently in cxl_pmu_offline_cpu() before a new target is chosen. cpumask_of(-1) treats the CPU number as unsigned and does out-of-bounds pointer arithmetic in get_cpu_mask(), so a concurrent read of the attribute dereferences a wild pointer and can fault -- a local denial of service.
Read on_cpu once and emit an empty mask when it is negative.
--- drivers/perf/cxl_pmu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/perf/cxl_pmu.c +10 -1
@@ -501,8 +501,17 @@ static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf) {struct cxl_pmu_info *info =dev_get_drvdata (dev); +int cpu =READ_ONCE (info->on_cpu); -return cpumap_print_to_pagebuf (true, buf,cpumask_of (info->on_cpu)); +/* +* on_cpu is -1 before the first online callback and transiently during +* cxl_pmu_offline_cpu(). cpumask_of(-1) computes an out-of-bounds +* pointer, so report an empty mask instead. +*/ +if (cpu <0 ) +return sysfs_emit (buf,"\n" ); + +return cpumap_print_to_pagebuf (true, buf,cpumask_of (cpu)); }static DEVICE_ATTR_RO (cpumask);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 1/9] perf/cxl: Program the requested event group on configurable counters
In reply to Dave JiangThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] Hardware State Pollution / Missing Bitmask Clear on PMU Counter Reuse
- [High] Invalid 64-bit Write on a 32-bit PCIe Register
- [High] Undefined Behavior Due to Out-of-Bounds Shift in Overflow Handling --
--- Patch [1]: [PATCH v2 1/9] perf/cxl: Program the requested event group on configurable counters --- commit 3f34f60eb49c6f10c29064fdd1d2a3cf3e95a287
Author: Dave Jiang <[email protected]> Date: Wed Jul 29 14:55:47 2026 +0000
perf/cxl: Program the requested event group on configurable counters
cxl_pmu_get_event_idx() sets *counter_idx for a configurable counter but never sets *event_idx, so hwc->event_base stays 0 and cxl_pmu_event_start() programs event group 0 regardless of the group the user requested.
Return the matched capability's event_idx.
drivers/perf/cxl_pmu.c static int cxl_pmu_get_event_idx(struct perf_event *event, int *counter_idx, · line 556
Quoted 10 lines
drivers/perf/cxl_pmu.c +2
-0@@ -552,6 +552,8 @@ static int cxl_pmu_get_event_idx(struct perf_event *event, int *counter_idx,return -EINVAL ; *counter_idx = i; +if (event_idx) + *event_idx = pmu_ev->event_idx;
[Severity: High]
This isn't a bug introduced by this patch, but returning the correct event_idx here fully exposes an existing state pollution issue in cxl_pmu_event_start().
When a hardware counter is reused, the configuration is bitwise-ORed:
drivers/perf/cxl_pmu.c:cxl_pmu_event_start() {
...
cfg = readq(base + CXL_PMU_COUNTER_CFG_REG(hwc->idx));
cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_INT_ON_OVRFLW, 1);
...
if (test_bit(hwc->idx, info->conf_counter_bm)) {
cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK,
hwc->event_base);
cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EVENTS_MSK,
cxl_pmu_config_get_mask(event));
}...
}
Because the previous bits are not cleared, does this code accumulate garbage on reused counters (e.g., ORing the new hwc->event_base with the old one)?
[Severity: High]
This is another pre-existing issue, but I noticed a potential hardware access violation in cxl_pmu_event_start() when dealing with HDM filtering:
drivers/perf/cxl_pmu.c:cxl_pmu_event_start() {
...
writeq(cfg, base + CXL_PMU_FILTER_CFG_REG(hwc->idx, 0));
...
}
Since CXL_PMU_FILTER_CFG_REG is a 32-bit register, could sending a 64-bit write with writeq() trigger a PCIe Unsupported Request error or silently overwrite the adjacent Filter 1 register?
[Severity: High]
This is a pre-existing issue, but there appears to be a shift out of bounds in the overflow handling path:
drivers/perf/cxl_pmu.c:__cxl_pmu_read() {
...
if (overflow && delta < GENMASK_ULL(info->counter_width - 1, 0)) delta += (1UL << info->counter_width);
...
}
If the platform supports a counter_width of 64 (or >= 32 on 32-bit architectures), does the use of 1UL rather than 1ULL invoke undefined behavior and silently corrupt the delta calculation?
Quoted 2 lines
drivers/perf/cxl_pmu.c +0
-0return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v2 2/9] perf/cxl: Clear stale event fields before reprogramming a counter
In reply to Dave JiangThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] A 64-bit writeq() is incorrectly used to write a 32-bit filter configuration register, corrupting the adjacent filter register.
- [High] The PMU event read logic incorrectly adds 2^width to the event delta on typical overflows, causing massive overcounting and undefined behavior.
- [Medium] The PMU sysfs format attribute for threshold accepts a 16-bit value, but the accessor silently truncates it to 8 bits. --
--- Patch [2]: [PATCH v2 2/9] perf/cxl: Clear stale event fields before reprogramming a counter --- commit 4bfb25bc15d355490e2f63cedff157f6adb1461a
Author: Dave Jiang <[email protected]>
perf/cxl: Clear stale event fields before reprogramming a counter
This commit fixes an issue where reprogramming a performance counter leaves stale values in the configuration register. It ensures the event group id, event mask, edge, and invert fields are cleared before being reprogrammed. This prevents the counter from accumulating statistics with old selections.
Quoted 4 lines
drivers/perf/cxl_pmu.c +0
-0
[ ... ]
drivers/perf/cxl_pmu.c static void cxl_pmu_event_start(struct perf_event *event, int flags) · line 647
Quoted 8 lines
drivers/perf/cxl_pmu.c +1
-0@@ -641,6 +641,7 @@ static void cxl_pmu_event_start(struct perf_event *event, int flags) } cfg =readq (base +CXL_PMU_COUNTER_CFG_REG (hwc->idx)); + cfg &= ~(CXL_PMU_COUNTER_CFG_EDGE |CXL_PMU_COUNTER_CFG_INVERT ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_INT_ON_OVRFLW ,1 ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_FREEZE_ON_OVRFLW ,1 ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_ENABLE ,1 );
[Severity: High]
This isn't a bug introduced by this patch, but does the filter configuration overwrite the adjacent Filter 1 register?
drivers/perf/cxl_pmu.c:cxl_pmu_event_start()
if (info->filter_hdm) {
if (cxl_pmu_config1_hdm_filter_en(event))
cfg = cxl_pmu_config2_get_hdm_decoder(event);
else
cfg = GENMASK(31, 0); /* No filtering if 0xFFFF_FFFF */
writeq(cfg, base + CXL_PMU_FILTER_CFG_REG(hwc->idx, 0));
}Because CXL_PMU_FILTER_CFG_REG uses a 4-byte stride, the registers are expected to be 32-bit wide. A 64-bit writeq() will write 8 bytes, which on a little-endian system clobbers the adjacent Filter 1 register when writing to Filter 0.
drivers/perf/cxl_pmu.c static void cxl_pmu_event_start(struct perf_event *event, int flags) · line 659
Quoted 9 lines
drivers/perf/cxl_pmu.c +2
-0@@ -651,6 +652,8 @@ static void cxl_pmu_event_start(struct perf_event *event, int flags)/* Fixed purpose counters have next two fields RO */ if (test_bit (hwc->idx, info->conf_counter_bm)) { + cfg &= ~(CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK | +CXL_PMU_COUNTER_CFG_EVENTS_MSK ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK , hwc->event_base); cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EVENTS_MSK,
[Severity: Medium]
This is a pre-existing issue, but could the threshold accessor silently truncate user-provided values?
drivers/perf/cxl_pmu.c:cxl_pmu_config1_get_threshold() static u8 cxl_pmu_config1_get_threshold(struct perf_event *event)
{
return FIELD_GET(CXL_PMU_ATTR_CONFIG1_THRESHOLD_MSK,
event->attr.config1);
}The accessor returns a u8, but the PMU sysfs format attribute for threshold accepts a 16-bit value (config1:0-15). If a user requests an event with a threshold configuration greater than 255, the value gets silently truncated before being programmed into the hardware.
[Severity: High]
This isn't a bug introduced by this patch, but does the event read logic cause massive overcounting when a counter overflows?
drivers/perf/cxl_pmu.c:__cxl_pmu_read()
delta = (new_cnt - prev_cnt) & GENMASK_ULL(info->counter_width - 1, 0); if (overflow && delta < GENMASK_ULL(info->counter_width - 1, 0)) delta += (1UL << info->counter_width);
The calculation (new_cnt - prev_cnt) & mask correctly handles a single wrap-around when new_cnt < prev_cnt due to two's complement arithmetic. Because delta < mask is almost always true during an overflow, the subsequent addition double counts the wrap-around.
Additionally, if info->counter_width is 64, shifting 1UL by 64 will invoke undefined behavior.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v2 7/9] perf/cxl: Validate the hardware-reported counter width
In reply to Dave JiangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Out-of-bounds MMIO write via writeq() overwriting the adjacent 32-bit filter register. --
--- Patch [7]: [PATCH v2 7/9] perf/cxl: Validate the hardware-reported counter width --- commit 5420efbe1c059e0ba51ccd9a185a7ae26d9866db
Author: Dave Jiang <[email protected]>
perf/cxl: Validate the hardware-reported counter width
cxl_pmu_parse_caps() takes the Counter Width straight from the CPMU Capability register with no bounds check. A device reporting width 0 or > 64 is invalid, so reject a counter width outside 1..64 at probe.
drivers/perf/cxl_pmu.c
Quoted 4 lines
drivers/perf/cxl_pmu.c +0
-0
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but I noticed a potential out-of-bounds MMIO write in cxl_pmu_event_start() in this same file:
writeq(cfg, base + CXL_PMU_FILTER_CFG_REG(hwc->idx, 0));
Can writing 64 bits using writeq to a 32-bit MMIO register cause an Unsupported Request (UR) PCIe error on devices that only support 32-bit access to this region?
If the hardware does happen to accept the 64-bit write, will this silently overwrite the adjacent 32-bit filter register with zeroes, corrupting its state?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [PATCH v2 3/9] perf/cxl: Fix the counter overflow delta fixup
In reply to Dave JiangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] A race condition between a lockless PMU read and the PMU overflow IRQ handler causes a full counter period to be erroneously added, massively double-counting the event. --
--- Patch [3]: [PATCH v2 3/9] perf/cxl: Fix the counter overflow delta fixup --- commit 72c8c960c955004fc0b667e90e3698f58a0d0ea6
Author: Dave Jiang <[email protected]>
perf/cxl: Fix the counter overflow delta fixup
This commit updates the CXL PMU counter overflow handling since the counters freeze on overflow and are not reloaded. It modifies the delta fixup logic to trigger when new_cnt is greater than or equal to prev_cnt, adding the full period back to account for the wrap.
Quoted 4 lines
drivers/perf/cxl_pmu.c +0
-0
[ ... ]
Quoted 11 lines
drivers/perf/cxl_pmu.c +5
-2@@ -698,12 +698,18 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) }while (local64_cmpxchg (&hwc->prev_count, prev_cnt, new_cnt) != prev_cnt);/* -* If we know an overflow occur then take that into account. -* Note counter is not reset as that would lose events +* The counter wraps to 0 and is never reloaded, so the masked unsigned +* subtraction only recovers a wrap when new_cnt < prev_cnt. On the +* overflow path new_cnt >= prev_cnt means the full period elapsed +* (prev_cnt is 0 from event_start(), or the residual left by an earlier +* overflow), so add the period back. mask + 1 is 2^counter_width, which
drivers/perf/cxl_pmu.c static void __cxl_pmu_read(struct perf_event *event, bool overflow) · line 712
Quoted 10 lines
drivers/perf/cxl_pmu.c +6
-3+* evaluates to 0 for a 64-bit counter - the correct sum mod 2^64, and +* avoids the undefined 1 << 64. */ - delta = (new_cnt - prev_cnt) &GENMASK_ULL (info->counter_width -1 ,0 ); -if (overflow && delta <GENMASK_ULL (info->counter_width -1 ,0 )) - delta += (1UL << info->counter_width); + mask =GENMASK_ULL (info->counter_width -1 ,0 ); + delta = (new_cnt - prev_cnt) & mask; +if (overflow && new_cnt >= prev_cnt) + delta += mask +1 ;
[Severity: High]
This is a pre-existing issue, but can this lead to massive double-counting if a concurrent lockless read races with the overflow handler?
If a userspace lockless PMU read, such as perf stat -I, happens after the counter wraps and freezes, but before the delayed overflow IRQ handler executes, the lockless reader will correctly compute the wrapped delta and update hwc->prev_count to the frozen residual.
When the IRQ handler in __cxl_pmu_read() subsequently runs, both prev_cnt and new_cnt will read the same frozen residual. The condition new_cnt >= prev_cnt will evaluate to true since they are equal, causing the handler to blindly add a full period (mask + 1) on top of the already accounted wrap.
Could this be addressed by distinguishing between a full period elapsing and a frozen residual being consumed by a concurrent lockless read?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH v2 9/9] perf/cxl: Avoid cpumask_of(-1) when no CPU is assigned
In reply to Dave JiangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but missing validation in cxl_pmu_event_init() leads to a severe out-of-bounds memory access via event->cpu. --
--- Patch [9]: [PATCH v2 9/9] perf/cxl: Avoid cpumask_of(-1) when no CPU is assigned --- commit a8e317fb50ca3d5fbb66719c1a84b3a5f9c785fc
Author: Dave Jiang <[email protected]>
perf/cxl: Avoid cpumask_of(-1) when no CPU is assigned
This commit fixes an out-of-bounds pointer arithmetic issue in cpumask_show() caused by cpumask_of(-1) when info->on_cpu is transiently -1 during CPU hotplug. It reads on_cpu once and emits an empty mask if it is negative.
Quoted 5 lines
drivers/perf/cxl_pmu.c +0
-0@@ -501,8 +501,17 @@ static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf)
drivers/perf/cxl_pmu.c static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, · line 510
Quoted 10 lines
drivers/perf/cxl_pmu.c +6
-1{struct cxl_pmu_info *info =dev_get_drvdata (dev); +int cpu =READ_ONCE (info->on_cpu); -return cpumap_print_to_pagebuf (true, buf,cpumask_of (info->on_cpu)); +/* +* on_cpu is -1 before the first online callback and transiently during +* cxl_pmu_offline_cpu(). cpumask_of(-1) computes an out-of-bounds +* pointer, so report an empty mask instead. +*/
[Severity: High]
This problem wasn't introduced by this patch, but since we are handling the transient -1 state of info->on_cpu here, could the same state cause an out-of-bounds memory access in cxl_pmu_event_init()?
If perf_event_open() is called concurrently while a CPU hotplug offline operation (cxl_pmu_offline_cpu()) is temporarily setting info->on_cpu = -1, the event initialization seems to blindly copy this value:
drivers/perf/cxl_pmu.c:cxl_pmu_event_init() {
...
event->cpu = info->on_cpu;
...
}The core perf subsystem then appears to dereference this -1 CPU index as an array offset:
kernel/events/core.c:find_get_context() {
...
cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu);
ctx = &cpuctx->ctx;
...
}Does passing -1 here cause an out-of-bounds array read that results in a wild pointer to cpuctx? It looks like the kernel then immediately attempts to take a spinlock on this wild pointer, which might lead to memory corruption. Is additional validation needed in cxl_pmu_event_init() to prevent this?
Quoted 6 lines
drivers/perf/cxl_pmu.c +4
-0+if (cpu <0 ) +return sysfs_emit (buf,"\n" ); + +return cpumap_print_to_pagebuf (true, buf,cpumask_of (cpu)); }static DEVICE_ATTR_RO (cpumask);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[email protected]
AI reviewRe: [PATCH v2 5/9] perf/cxl: Keep the overflow interrupt pinned to the managed CPU
In reply to Dave JiangThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Modifying the affinity of a shared IRQ line during CPU hotplug violates local64_t locking rules.
- [High] Race condition during CPU hotplug migration violates local64_t locking rules. --
--- Patch [5]: [PATCH v2 5/9] perf/cxl: Keep the overflow interrupt pinned to the managed CPU --- commit d9c09f0087580638fe392e6b190d53e0657bbc87
Author: Dave Jiang <[email protected]>
perf/cxl: Keep the overflow interrupt pinned to the managed CPU
The PMU driver pins its overflow interrupt to the managing CPU, but requests it with IRQF_SHARED without IRQF_NOBALANCING. This allows irqbalance to move the interrupt, leading to concurrent cross-CPU access of local64_t event counts. This commit adds IRQF_NOBALANCING to ensure the CPU hotplug pinning holds.
drivers/perf/cxl_pmu.c static int cxl_pmu_probe(struct device *dev) · line 893
Quoted 12 lines
drivers/perf/cxl_pmu.c +2
-1@@ -888,7 +888,8 @@ static int cxl_pmu_probe(struct device *dev)if (!irq_name)return -ENOMEM ; - rc =devm_request_irq (dev, irq, cxl_pmu_irq,IRQF_SHARED |IRQF_NO_THREAD , + rc =devm_request_irq (dev, irq, cxl_pmu_irq, +IRQF_SHARED |IRQF_NO_THREAD |IRQF_NOBALANCING , irq_name, info);
[Severity: High]
This isn't a bug introduced by this patch, but there appears to be a pre-existing race condition in the CPU hotplug migration window that violates local64_t locking rules.
In cxl_pmu_offline_cpu(), perf_pmu_migrate_context() moves the PMU events to the target CPU and restarts them:
drivers/perf/cxl_pmu.c:cxl_pmu_offline_cpu() {
...
perf_pmu_migrate_context(&info->pmu, cpu, target);
info->on_cpu = target;
/*
* CPU HP lock is held so we should be guaranteed that this CPU hasn't yet
* gone away.
*/
WARN_ON(irq_set_affinity(info->irq, cpumask_of(target)));
return 0;
}Since perf_pmu_migrate_context() re-enables hardware interrupts, and irq_set_affinity() is called after this migration, an interrupt firing during this window will still be delivered to the old offlining CPU.
Concurrently, user-space reads of the migrated event will execute on the new target CPU.
Could this lead to cross-CPU concurrent accesses to the local64_t event counts, corrupting the data?
[Severity: High]
This is a pre-existing issue, but modifying the affinity of a shared IRQ line during CPU hotplug appears to also violate local64_t locking rules.
Looking at the same cxl_pmu_offline_cpu() snippet above, the driver registers its interrupt with IRQF_SHARED. Because affinity is a per-IRQ property, calling irq_set_affinity() moves the interrupt for all PMU instances sharing this IRQ line.
If another PMU instance is still managed by the old CPU (e.g. its offline callback hasn't run yet), its interrupts will now fire on the new target CPU while user-space reads concurrently execute on the old CPU.
Is there a risk that this cross-CPU access corrupts the local64_t counts?
Quoted 2 lines
drivers/perf/cxl_pmu.c +0
-0if (rc)return rc;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v2 8/9] perf/cxl: Don't use pmu.dev in IRQ and hotplug callbacks after unregister
In reply to Dave JiangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch correctly fixes a UAF in logging statements during teardown but leaves a critical lifecycle race unaddressed. The IRQ is registered via devm_request_irq before cxl_pmu_perf_unregister is registered. On device removal, devm unwinds LIFO, so perf_pmu_unregister runs while the shared IRQ is still live. If the IRQ fires concurrently on another CPU (e.g., due to a co-function changing the shared MSI vector's affinity), cxl_pmu_irq can read a non-NULL event right before it is detached, leading to a NULL pointer dereference in __cxl_pmu_read when it accesses event->pmu. --
--- Patch [8]: [PATCH v2 8/9] perf/cxl: Don't use pmu.dev in IRQ and hotplug callbacks after unregister --- commit 34ab4c1ad03386965fbc73a1d534f56456842698
Author: Dave Jiang <[email protected]>
perf/cxl: Don't use pmu.dev in IRQ and hotplug callbacks after unregister
The patch replaces info->pmu.dev with info->pmu.parent in the IRQ and CPU hotplug callbacks to fix a use-after-free during device removal.
drivers/perf/cxl_pmu.c static irqreturn_t cxl_pmu_irq(int irq, void *data) · line 809
Quoted 13 lines
drivers/perf/cxl_pmu.c +1
-1@@ -803,7 +803,7 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)struct perf_event *event = info->hw_events[i];if (!event) { -dev_dbg (info->pmu.dev, +dev_dbg (info->pmu.parent,"overflow but on non enabled counter %d\n" , i);continue ; }
[Severity: High]
This is a pre-existing issue, but since this patch addresses teardown races, does it leave a critical lifecycle race unaddressed here?
Because the IRQ is registered via devm before cxl_pmu_perf_unregister() is registered, the LIFO unwinding order means perf_pmu_unregister() runs while the shared IRQ is still live.
If a shared MSI interrupt fires concurrently with cxl_pmu_perf_unregister(), cxl_pmu_irq() can read a valid event right before it is detached:
cxl_pmu_irq() {
...
struct perf_event *event = info->hw_events[i];
...
__cxl_pmu_read(event, true);
}Meanwhile on another CPU, the unregister thread detaches the events and synchronously sets event->pmu = NULL. When the IRQ handler calls __cxl_pmu_read(), it dereferences event->pmu:
__cxl_pmu_read() {
struct cxl_pmu_info *info = pmu_to_cxl_pmu_info(event->pmu);
...
}Could this lead to a null pointer dereference and a kernel panic when info->base is subsequently accessed during device unbind or module removal?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Jonathan Cameron
maintainerRe: [PATCH v2 9/9] perf/cxl: Avoid cpumask_of(-1) when no CPU is assigned
In reply to Dave JiangQuoted 8 lines · On Wed, 29 Jul 2026 07:55:55 -0700 Dave Jiang <[email protected]> wrote:
cpumask_show()feedsinfo->on_cpustraight intocpumask_of()for the world-readable cpumask sysfs attribute.on_cpuis -1 before the first hotplug online callback and transiently incxl_pmu_offline_cpu()before a new target is chosen.cpumask_of(-1)treats the CPU number as unsigned and does out-of-bounds pointer arithmetic inget_cpu_mask(), so a concurrent read of the attribute dereferences a wild pointer and can fault -- a local denial of service.
I'm not keen on the solution here.
The transient state is ugly anyway. We can just move setting it to -1 into the dummy code that deals with that well known case of you have CPUs online and code is still running.
The init case looks like a false positive to me. But maybe I'm missing stuff. The perf registration that surfaces the sysfs happens after hotplug handler is added and I believe that synchronously runs it for CPUs that are already up. Given we are running code (and CXL stuff isn't super early) something will be up so it won't remain -1 by the time of use.
Can we just use the generic stuff? Maybe need Robin's stuff to add init / exit per driver calls.
https://lore.kernel.org/linux-arm-kernel/[email protected]/
In general, I'd like Robin to take a quick look at the more generic perf parts of this series given he has clearly been deep in this stuff a lot more recently than me :)
Jonathan
Quoted 34 lines
Read
on_cpuonce and emit an empty mask when it is negative.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +10
-1@@ -501,8 +501,17 @@ static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf) {struct cxl_pmu_info *info =dev_get_drvdata (dev); +int cpu =READ_ONCE (info->on_cpu); -return cpumap_print_to_pagebuf (true, buf,cpumask_of (info->on_cpu)); +/* +* on_cpu is -1 before the first online callback and transiently during +* cxl_pmu_offline_cpu(). cpumask_of(-1) computes an out-of-bounds +* pointer, so report an empty mask instead. +*/ +if (cpu <0 ) +return sysfs_emit (buf,"\n" ); + +return cpumap_print_to_pagebuf (true, buf,cpumask_of (cpu)); }static DEVICE_ATTR_RO (cpumask);
Re: [PATCH v2 8/9] perf/cxl: Don't use pmu.dev in IRQ and hotplug callbacks after unregister
In reply to Dave JiangQuoted 9 lines · On Wed, 29 Jul 2026 07:55:54 -0700 Dave Jiang <[email protected]> wrote:
On device removal the devm actions unwind LIFO, so
cxl_pmu_perf_unregister()runs first andperf_pmu_unregister()freesinfo->pmu.dev(device_del()+put_device()->kfree()). The overflow IRQ (freed last) and the CPU-hotplug instance (removed next) are still live at that point, and bothcxl_pmu_irq()andcxl_pmu_offline_cpu()log via dev_dbg()/dev_err() oninfo->pmu.dev, dereferencing freed memory. The shared IRQ can be entered for a co-function on the same MSI vector, and a CPU can go offline in the window before the hotplug instance is removed.
Quoted 8 lines
Log through
info->pmu.parentinstead, thecxl_pmudevice passed to probe, which is devm-managed and outlives every teardown action.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>
In general logging on subsystem devices is flakey so
Quoted 26 lines
--- drivers/perf/cxl_pmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/perf/cxl_pmu.c +2
-2@@ -803,7 +803,7 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)struct perf_event *event = info->hw_events[i];if (!event) { -dev_dbg (info->pmu.dev, +dev_dbg (info->pmu.parent,"overflow but on non enabled counter %d\n" , i);continue ; }@@ -966,7 +966,7 @@ static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) info->on_cpu =-1 ; target =cpumask_any_but (cpu_online_mask, cpu);if (target >= nr_cpu_ids) { -dev_err (info->pmu.dev,"Unable to find a suitable CPU\n" ); +dev_err (info->pmu.parent,"Unable to find a suitable CPU\n" );return 0 ; }
Re: [PATCH v2 7/9] perf/cxl: Validate the hardware-reported counter width
In reply to Dave JiangQuoted 9 lines · On Wed, 29 Jul 2026 07:55:53 -0700 Dave Jiang <[email protected]> wrote:
cxl_pmu_parse_caps()takes the Counter Width straight from the CPMU Capability register with no bounds check. The Counter Data register is 64 bits, so a device reporting width 0 (or > 64) is invalid, and the read path'sGENMASK_ULL(counter_width - 1, 0)would shift out of range (undefined behavior, e.g. a UBSAN splat) on a value of 0.Reject a counter width outside 1..64 at probe.
Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")
I don't mind hardening this but there has been a lot of push back on 'bugs' that are dependent on broken hardware (part of the AI discussions on the ksummit list).
Anyhow it is easy protection so fair enough - I'm just not sure it is worth the pain of chasing down more complex hardware did the wrong thing 'bugs'
Quoted 28 lines
Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/perf/cxl_pmu.c +9
-0@@ -139,6 +139,15 @@ static int cxl_pmu_parse_caps(struct device *dev, struct cxl_pmu_info *info) info->num_counters =FIELD_GET (CXL_PMU_CAP_NUM_COUNTERS_MSK , val) +1 ; info->counter_width =FIELD_GET (CXL_PMU_CAP_COUNTER_WIDTH_MSK , val); +/* +* The Counter Data register is 64 bits wide, so a Counter Width of 0 or +* >64 is invalid. Reject it rather than let GENMASK_ULL(width - 1, 0) in +* the read path shift out of range. +*/ +if (info->counter_width ==0 || info->counter_width >64 ) { +dev_err (dev,"Invalid counter width %d\n" , info->counter_width); +return -ENODEV ; + } info->num_event_capabilities =FIELD_GET (CXL_PMU_CAP_NUM_EVN_CAP_REG_SUP_MSK , val) +1 ; info->filter_hdm =FIELD_GET (CXL_PMU_CAP_FILTERS_SUP_MSK , val) &CXL_PMU_FILTER_HDM ;
Re: [PATCH v2 6/9] perf/cxl: Unfreeze counters after handling an overflow interrupt
In reply to Dave JiangQuoted 7 lines · On Wed, 29 Jul 2026 07:55:52 -0700 Dave Jiang <[email protected]> wrote:
The counters are configured with Freeze on Overflow, so when any counter overflows the CPMU freezes every counter in the block (CXL r4.0 §8.2.7.2.1).
cxl_pmu_irq()reads the overflowed counters and clears the overflow status, but never writes the CPMU Freeze register to unfreeze, so all counters stay frozen until the nextpmu_enable()and events in that window are silently lost.
Quoted 7 lines
Unfreeze after clearing the overflow status so counting resumes.
Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>
Guess I had an emulation bug. The disadvantage of developing against emulation written by the same person writing the kernel driver.
Quoted 24 lines
--- drivers/perf/cxl_pmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/perf/cxl_pmu.c +9
-0@@ -804,6 +804,15 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)writeq (overflowed, base +CXL_PMU_OVERFLOW_REG ); +/* +* Counters are configured to freeze on overflow (Freeze on Overflow), +* which freezes every counter in the CPMU. Once the overflowed counters +* have been read and their status cleared, unfreeze so counting resumes; +* otherwise all counters stay frozen until the next pmu_enable() and +* events are silently lost. +*/ +writeq (0 , base +CXL_PMU_FREEZE_REG ); +return IRQ_HANDLED ; }
Re: [PATCH v2 5/9] perf/cxl: Keep the overflow interrupt pinned to the managed CPU
In reply to Dave JiangQuoted 9 lines · On Wed, 29 Jul 2026 07:55:51 -0700 Dave Jiang <[email protected]> wrote:
The PMU pins its overflow interrupt to
info->on_cpuin the hotplug online/offline callbacks, but requests it with onlyIRQF_SHARED|IRQF_NO_THREAD. WithoutIRQF_NOBALANCING, irqbalance or a userspacesmp_affinitywrite can move the interrupt to another CPU.cxl_pmu_irq()then runscxl_pmu_read()there, doing local64_cmpxchg()/local64_add() onhwc->prev_countandevent->countconcurrently with the managing CPU;local64_tis only atomic against same-CPU access, so counts get corrupted.
Quoted 8 lines
Add
IRQF_NOBALANCINGso the pinning done in the hotplug callbacks holds, matching other uncore-style PMU drivers.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>
Quoted 18 lines
--- drivers/perf/cxl_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +2
-1@@ -888,7 +888,8 @@ static int cxl_pmu_probe(struct device *dev)if (!irq_name)return -ENOMEM ; - rc =devm_request_irq (dev, irq, cxl_pmu_irq,IRQF_SHARED |IRQF_NO_THREAD , + rc =devm_request_irq (dev, irq, cxl_pmu_irq, +IRQF_SHARED |IRQF_NO_THREAD |IRQF_NOBALANCING , irq_name, info);if (rc)return rc;
Re: [PATCH v2 4/9] perf/cxl: Accept an overflow interrupt on MSI message number 0
In reply to Dave JiangQuoted 7 lines · On Wed, 29 Jul 2026 07:55:50 -0700 Dave Jiang <[email protected]> wrote:
cxl_pmu_probe()rejects the PMU with-EINVALwheninfo->irq<= 0, butinfo->irqholds the MSI/MSI-X message number the device signals overflow on (CXL_PMU_CAP_MSI_N_MSK, a 0-based 4-bit field), with -1 meaning "no interrupt support". Message number 0 is valid andpci_irq_vector()takes a 0-based index, so a compliant device signalling on the first vector is wrongly refused and fails to probe.
Quoted 8 lines
Reject only the no-interrupt case (
info->irq< 0), matching the sibling CXL mailbox and event IRQ handling.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>
Anyone who puts the PMU on vector 0 is presumably sharing one vector for everything - which to me sounds unwise. Still indeed a bug.
Quoted 17 lines
--- drivers/perf/cxl_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/perf/cxl_pmu.c +1
-1@@ -876,7 +876,7 @@ static int cxl_pmu_probe(struct device *dev) .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }; -if (info->irq <=0 ) +if (info->irq <0 )return -EINVAL ; rc =pci_irq_vector (pdev, info->irq);
Re: [PATCH v2 4/9] perf/cxl: Accept an overflow interrupt on MSI message number 0
In reply to Dave JiangQuoted 7 lines · Dave Jiang
cxl_pmu_probe()rejects the PMU with-EINVALwheninfo->irq<= 0, butinfo->irqholds the MSI/MSI-X message number the device signals overflow on (CXL_PMU_CAP_MSI_N_MSK, a 0-based 4-bit field), with -1 meaning "no interrupt support". Message number 0 is valid andpci_irq_vector()takes a 0-based index, so a compliant device signalling on the first vector is wrongly refused and fails to probe.
Quoted 8 lines
Reject only the no-interrupt case (
info->irq< 0), matching the sibling CXL mailbox and event IRQ handling.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>
Re: [PATCH v2 5/9] perf/cxl: Keep the overflow interrupt pinned to the managed CPU
In reply to Dave JiangQuoted 9 lines · Dave Jiang
The PMU pins its overflow interrupt to
info->on_cpuin the hotplug online/offline callbacks, but requests it with onlyIRQF_SHARED|IRQF_NO_THREAD. WithoutIRQF_NOBALANCING, irqbalance or a userspacesmp_affinitywrite can move the interrupt to another CPU.cxl_pmu_irq()then runscxl_pmu_read()there, doing local64_cmpxchg()/local64_add() onhwc->prev_countandevent->countconcurrently with the managing CPU;local64_tis only atomic against same-CPU access, so counts get corrupted.
Quoted 3 lines
Add
IRQF_NOBALANCINGso the pinning done in the hotplug callbacks holds, matching other uncore-style PMU drivers.
Quoted 26 lines
Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>---
drivers/perf/cxl_pmu.c| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +2
-1@@ -888,7 +888,8 @@ static int cxl_pmu_probe(struct device *dev)if (!irq_name)return -ENOMEM ; - rc =devm_request_irq (dev, irq, cxl_pmu_irq,IRQF_SHARED |IRQF_NO_THREAD , + rc =devm_request_irq (dev, irq, cxl_pmu_irq, +IRQF_SHARED |IRQF_NO_THREAD |IRQF_NOBALANCING , irq_name, info);if (rc)return rc;Signature · 1 line
2.55.0
Jonathan Cameron
maintainerRe: [PATCH v2 3/9] perf/cxl: Fix the counter overflow delta fixup
In reply to Dave JiangQuoted 2 lines · On Wed, 29 Jul 2026 07:55:49 -0700 Dave Jiang <[email protected]> wrote:
Counters are configured with Freeze on Overflow and are never reloaded: on overflow the counter wraps to 0, counts on until the CPMU freezes, and
Hi Dave,
Thanks for looking at these.
Why would it count on if it froze? I think the bot is tripping over the fact we don't yet implement free running counters (and the other bug about not unfreezing) for currently the ability to freeze on only some counters (to do periodic sampling for instance).
The CXL CPMU spec is incredibly broad in what is supported, so maybe we want to harden things anyway but I'm not sure the condition described by most of this is real.
Quoted 2 lines
retains that residual (CXL r4.0 8.2.7.2.3). So the masked subtraction in
__cxl_pmu_read()only spans the wrap whennew_cnt<prev_cnt.
It's been a long time so maybe I have how this was meant to work wrong.
There are two paths to __cxl_pmu_read()
1. We have freeze on overflow enabled so any counter that overflows results in an interrupt. At that point all counters are frozen.
We then read only the counter that overflowed (which is 0) and that will update the prev_cnt storage. No chance of hitting the full wrap around seen here.
2. An on demand read (polling) In this case the counter may take any value, but because we have freeze on overflow it can't have wrapped (as otherwise we'd have taken path 1).
So slightly fun question of why we have any wrapping control and I think the answer is because the freeze on overflow isn't very specific in the spec for whether it freezes on max value or 0.
Quoted 16 lines
The fixup keys off the delta rather than the operands:
delta = (new_cnt - prev_cnt) & GENMASK_ULL(counter_width - 1, 0); if (overflow && delta < GENMASK_ULL(counter_width - 1, 0)) delta += (1UL << counter_width);and is wrong both ways. After a mid-period read the subtraction already spans the wrap, but the guard holds for all but one delta value, so a second period is added and the event over-counts. After an earlier overflow read - or after
event_start(), which leavesprev_countat 0 -prev_cntis a small residual, and once the new residual is greater or equal only the difference between the two is counted and the period is lost, which for a residual of 0 means every period after the first. 'perf stat -I' hits the former, plain 'perf stat' the latter.
Quoted 2 lines
The shift is also undefined for
counter_width== 64, and for >= 32 on 32-bit kernels where 1UL is 32 bits.
That bit seems valid.
Quoted 66 lines
Condition the fixup on
new_cnt>=prev_cnt, the one case the masked subtraction cannot express, and use mask + 1 for the period: it is 0 for a 64-bit counter, the correct sum mod 2^64, and needs no shift. A userspace read landing between the wrap and the handler clearing the overflow status stays correct because the counter is frozen and cannot advance between the two reads.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>---
v2:
- Fix the fixup instead of deleting it. v1 dropped it on the rationale that the masked subtraction already handles the wrap, which only holds fornew_cnt<prev_cnt; with freeze on overflow and no reload the overflow path routinely seesnew_cnt>=prev_cnt, where v1 silently dropped a full period (sashiko-bot,
https://lore.kernel.org/linux-cxl/[email protected]/).
- Keep the overflow argument and the __cxl_pmu_read()/cxl_pmu_read() split; v1 folded them together once the argument went unused.
- Use mask + 1 rather than 1ULL <<counter_widthso the 64-bit counter case needs no special casing.--- drivers/perf/cxl_pmu.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)drivers/perf/cxl_pmu.c +12
-6@@ -690,7 +690,7 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) {struct cxl_pmu_info *info =pmu_to_cxl_pmu_info (event->pmu);struct hw_perf_event *hwc = &event->hw; -u64 new_cnt, prev_cnt, delta; +u64 new_cnt, prev_cnt, delta, mask; do { prev_cnt = local64_read(&hwc->prev_count);@@ -698,12 +698,18 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) }while (local64_cmpxchg (&hwc->prev_count, prev_cnt, new_cnt) != prev_cnt);/* -* If we know an overflow occur then take that into account. -* Note counter is not reset as that would lose events +* The counter wraps to 0 and is never reloaded, so the masked unsigned +* subtraction only recovers a wrap when new_cnt < prev_cnt. On the +* overflow path new_cnt >= prev_cnt means the full period elapsed +* (prev_cnt is 0 from event_start(), or the residual left by an earlier +* overflow), so add the period back. mask + 1 is 2^counter_width, which +* evaluates to 0 for a 64-bit counter - the correct sum mod 2^64, and +* avoids the undefined 1 << 64. */ - delta = (new_cnt - prev_cnt) &GENMASK_ULL (info->counter_width -1 ,0 ); -if (overflow && delta <GENMASK_ULL (info->counter_width -1 ,0 )) - delta += (1UL << info->counter_width); + mask =GENMASK_ULL (info->counter_width -1 ,0 ); + delta = (new_cnt - prev_cnt) & mask; +if (overflow && new_cnt >= prev_cnt) + delta += mask +1 ;local64_add (delta, &event->count); }
Jonathan Cameron
maintainerRe: [PATCH v2 2/9] perf/cxl: Clear stale event fields before reprogramming a counter
In reply to Dave JiangQuoted 8 lines · On Wed, 29 Jul 2026 07:55:48 -0700 Dave Jiang <[email protected]> wrote:
cxl_pmu_event_start()ORs the event group id, event mask, edge and invert selections into a configurable counter's config register without clearing those fields first, andcxl_pmu_event_stop()leaves them set. Reusing a counter for another event therefore ORs the new selection onto the old, counting the wrong events with stale edge/invert behaviour.Clear
EVENT_GRP_ID_IDX, EVENTS, EDGE and INVERT before applying the new values.
Maybe use FIELD_MODIFY() instead of mask then fill?
Otherwise LGTM.
Quoted 31 lines
Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 3 +++ 1 file changed, 3 insertions(+)drivers/perf/cxl_pmu.c +3
-0@@ -641,6 +641,7 @@ static void cxl_pmu_event_start(struct perf_event *event, int flags) } cfg =readq (base +CXL_PMU_COUNTER_CFG_REG (hwc->idx)); + cfg &= ~(CXL_PMU_COUNTER_CFG_EDGE |CXL_PMU_COUNTER_CFG_INVERT ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_INT_ON_OVRFLW ,1 ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_FREEZE_ON_OVRFLW ,1 ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_ENABLE ,1 );@@ -651,6 +652,8 @@ static void cxl_pmu_event_start(struct perf_event *event, int flags)/* Fixed purpose counters have next two fields RO */ if (test_bit (hwc->idx, info->conf_counter_bm)) { + cfg &= ~(CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK | +CXL_PMU_COUNTER_CFG_EVENTS_MSK ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK , hwc->event_base); cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EVENTS_MSK,
Re: [PATCH v2 1/9] perf/cxl: Program the requested event group on configurable counters
In reply to Dave JiangQuoted 11 lines · On Wed, 29 Jul 2026 07:55:47 -0700 Dave Jiang <[email protected]> wrote:
cxl_pmu_get_event_idx()sets*counter_idxfor a configurable counter but never sets*event_idx, sohwc->event_basestays 0 andcxl_pmu_event_start()programs event group 0 regardless of the group the user requested.Return the matched capability's
event_idx.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>
One minor thing. Otherwise LGTM
Quoted 10 lines
---
drivers/perf/cxl_pmu.c | 2 ++ 1 file changed, 2 insertions(+)drivers/perf/cxl_pmu.c +0
-0
drivers/perf/cxl_pmu.c static int cxl_pmu_get_event_idx(struct perf_event *event, int *counter_idx, · line 555
Quoted 4 lines
drivers/perf/cxl_pmu.c +1
-0@@ -552,6 +552,8 @@ static int cxl_pmu_get_event_idx(struct perf_event *event, int *counter_idx,return -EINVAL ; *counter_idx = i; +if (event_idx)
Hmm. We never want to get counter_idx and not event_idx. Perhaps add this check to the one for counter_idx at the top of this scope.
Quoted 4 lines
drivers/perf/cxl_pmu.c +1
-0+ *event_idx = pmu_ev->event_idx;return 0 ; }
Re: [PATCH v2 9/9] perf/cxl: Avoid cpumask_of(-1) when no CPU is assigned
In reply to Jonathan CameronQuoted 18 lines · Jonathan Cameron
Quoted 8 lines · On Wed, 29 Jul 2026 07:55:55 -0700 Dave Jiang <[email protected]> wrote:
cpumask_show()feedsinfo->on_cpustraight intocpumask_of()for the world-readable cpumask sysfs attribute.on_cpuis -1 before the first hotplug online callback and transiently incxl_pmu_offline_cpu()before a new target is chosen.cpumask_of(-1)treats the CPU number as unsigned and does out-of-bounds pointer arithmetic inget_cpu_mask(), so a concurrent read of the attribute dereferences a wild pointer and can fault -- a local denial of service.I'm not keen on the solution here.
The transient state is ugly anyway. We can just move setting it to -1 into the dummy code that deals with that well known case of you have CPUs online and code is still running.
Quoted 5 lines
The init case looks like a false positive to me. But maybe I'm missing stuff. The perf registration that surfaces the sysfs happens after hotplug handler is added and I believe that synchronously runs it for CPUs that are already up. Given we are running code (and CXL stuff isn't super early) something will be up so it won't remain -1 by the time of use.
Yup, this is pure nonsensical AI gibberish. The only PMUs which might ever need to take care in this respect are those which can only associate with a fixed subset of CPUs (e.g. arm_dsu_pmu), where that set could potentially all be offlined while still leaving other CPUs capable of calling the driver.
If one _could_ offline the last CPU in the system (which hotplug doesn't allow for obvious reasons), the "local denial of service" would be rather larger than one random sysfs file...
Quoted 3 lines
Can we just use the generic stuff? Maybe need Robin's stuff to add init / exit per driver calls.
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Indeed for arbitrary CXL devices I'm not sure the existing x86 uncore scopes would fit, but feedback on my patch is welcome! ;)
Thanks,
Robin.
Quoted 42 lines
In general, I'd like Robin to take a quick look at the more generic perf parts of this series given he has clearly been deep in this stuff a lot more recently than me :)
Jonathan
Quoted 34 lines
Read
on_cpuonce and emit an empty mask when it is negative.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +10
-1@@ -501,8 +501,17 @@ static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf) {struct cxl_pmu_info *info =dev_get_drvdata (dev); +int cpu =READ_ONCE (info->on_cpu); -return cpumap_print_to_pagebuf (true, buf,cpumask_of (info->on_cpu)); +/* +* on_cpu is -1 before the first online callback and transiently during +* cxl_pmu_offline_cpu(). cpumask_of(-1) computes an out-of-bounds +* pointer, so report an empty mask instead. +*/ +if (cpu <0 ) +return sysfs_emit (buf,"\n" ); + +return cpumap_print_to_pagebuf (true, buf,cpumask_of (cpu)); }static DEVICE_ATTR_RO (cpumask);
Re: [PATCH v2 8/9] perf/cxl: Don't use pmu.dev in IRQ and hotplug callbacks after unregister
In reply to Dave JiangQuoted 26 lines · Dave Jiang
On device removal the devm actions unwind LIFO, so
cxl_pmu_perf_unregister()runs first andperf_pmu_unregister()freesinfo->pmu.dev(device_del()+put_device()->kfree()). The overflow IRQ (freed last) and the CPU-hotplug instance (removed next) are still live at that point, and bothcxl_pmu_irq()andcxl_pmu_offline_cpu()log via dev_dbg()/dev_err() oninfo->pmu.dev, dereferencing freed memory. The shared IRQ can be entered for a co-function on the same MSI vector, and a CPU can go offline in the window before the hotplug instance is removed.Log through
info->pmu.parentinstead, thecxl_pmudevice passed to probe, which is devm-managed and outlives every teardown action.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/perf/cxl_pmu.c +0
-0
drivers/perf/cxl_pmu.c static irqreturn_t cxl_pmu_irq(int irq, void *data) · line 808
Quoted 7 lines
drivers/perf/cxl_pmu.c +1
-1@@ -803,7 +803,7 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)struct perf_event *event = info->hw_events[i];if (!event) { -dev_dbg (info->pmu.dev, +dev_dbg (info->pmu.parent,"overflow but on non enabled counter %d\n" , i);continue ;
This seems dubious - we don't permit sharing the IRQ, and all events must have been stopped and descheduled to allow the PMU to be removed in the first place, so how would an overflow interrupt happen?
drivers/perf/cxl_pmu.c static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) · line 968
Quoted 5 lines
drivers/perf/cxl_pmu.c +0
-0}@@ -966,7 +966,7 @@ static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) info->on_cpu =-1 ; target =cpumask_any_but (cpu_online_mask, cpu);if (target >= nr_cpu_ids) {
And this again is nonsense anyway - if the pointless dead code is bothering people, just delete the whole check.
Thanks,
Robin.
Quoted 5 lines
drivers/perf/cxl_pmu.c +1
-1-dev_err (info->pmu.dev,"Unable to find a suitable CPU\n" ); +dev_err (info->pmu.parent,"Unable to find a suitable CPU\n" );return 0 ; }
Re: [PATCH v2 5/9] perf/cxl: Keep the overflow interrupt pinned to the managed CPU
In reply to Dave JiangQuoted 26 lines · Dave Jiang
The PMU pins its overflow interrupt to
info->on_cpuin the hotplug online/offline callbacks, but requests it with onlyIRQF_SHARED|IRQF_NO_THREAD. WithoutIRQF_NOBALANCING, irqbalance or a userspacesmp_affinitywrite can move the interrupt to another CPU.cxl_pmu_irq()then runscxl_pmu_read()there, doing local64_cmpxchg()/local64_add() onhwc->prev_countandevent->countconcurrently with the managing CPU;local64_tis only atomic against same-CPU access, so counts get corrupted.Add
IRQF_NOBALANCINGso the pinning done in the hotplug callbacks holds, matching other uncore-style PMU drivers.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +0
-0
drivers/perf/cxl_pmu.c static int cxl_pmu_probe(struct device *dev) · line 892
Quoted 6 lines
drivers/perf/cxl_pmu.c +2
-1@@ -888,7 +888,8 @@ static int cxl_pmu_probe(struct device *dev)if (!irq_name)return -ENOMEM ; - rc = devm_request_irq(dev, irq, cxl_pmu_irq, IRQF_SHARED | IRQF_NO_THREAD, + rc = devm_request_irq(dev, irq, cxl_pmu_irq, + IRQF_SHARED | IRQF_NO_THREAD | IRQF_NOBALANCING,
Bah, sorry, now I see I misspoke just now on the other patch - PMUs really _shouldn't_ permit shared IRQs, but this one does :(
Thus it's all well and good to prevent userspace changing affinity, but it doesn't help _all_ that much if other drivers can still legitimately change it behind our backs...
Thanks,
Robin.
Quoted 3 lines
drivers/perf/cxl_pmu.c +0
-0irq_name, info);if (rc)return rc;
Re: [PATCH v2 6/9] perf/cxl: Unfreeze counters after handling an overflow interrupt
In reply to Dave JiangQuoted 24 lines · Dave Jiang
The counters are configured with Freeze on Overflow, so when any counter overflows the CPMU freezes every counter in the block (CXL r4.0 §8.2.7.2.1).
cxl_pmu_irq()reads the overflowed counters and clears the overflow status, but never writes the CPMU Freeze register to unfreeze, so all counters stay frozen until the nextpmu_enable()and events in that window are silently lost.Unfreeze after clearing the overflow status so counting resumes.
Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/perf/cxl_pmu.c +0
-0@@ -804,6 +804,15 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)
drivers/perf/cxl_pmu.c static irqreturn_t cxl_pmu_irq(int irq, void *data) · line 814
Quoted 10 lines
drivers/perf/cxl_pmu.c +8
-0writeq (overflowed, base +CXL_PMU_OVERFLOW_REG ); +/* +* Counters are configured to freeze on overflow (Freeze on Overflow), +* which freezes every counter in the CPMU. Once the overflowed counters +* have been read and their status cleared, unfreeze so counting resumes; +* otherwise all counters stay frozen until the next pmu_enable() and +* events are silently lost. +*/ +writeq (0 , base +CXL_PMU_FREEZE_REG );
Does this unconditionally unfreeze _all_ counters, including any which might have have overflowed since the read of CXL_PMU_OVERFLOW_REG and thus have not been handled yet? Or are we hoping that the "global freeze" behaviour prevents that from being able to happen?
Furthermore, what if an overflow happens to occur just as cxl_pmu_disable() is intentionally freezing all the counters, such that by the time we get here, unfreezing them would be the wrong thing to do?
Thanks,
Robin.
Quoted 4 lines
drivers/perf/cxl_pmu.c +1
-0+return IRQ_HANDLED ; }
Re: [PATCH v2 4/9] perf/cxl: Accept an overflow interrupt on MSI message number 0
In reply to Dave JiangQuoted 6 lines · Dave Jiang
cxl_pmu_probe()rejects the PMU with-EINVALwheninfo->irq<= 0, butinfo->irqholds the MSI/MSI-X message number the device signals overflow on (CXL_PMU_CAP_MSI_N_MSK, a 0-based 4-bit field), with -1 meaning "no interrupt support". Message number 0 is valid andpci_irq_vector()takes a 0-based index, so a compliant device signalling on the first vector is wrongly refused and fails to probe.
If info->irq is a PCI MSI vector index, not the Linux vIRQ number, then surely that means the irq_set_affinity() calls are wrong as well?
Thanks,
Robin.
Quoted 25 lines
Reject only the no-interrupt case (
info->irq< 0), matching the sibling CXL mailbox and event IRQ handling.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/perf/cxl_pmu.c +1
-1@@ -876,7 +876,7 @@ static int cxl_pmu_probe(struct device *dev) .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }; -if (info->irq <=0 ) +if (info->irq <0 )return -EINVAL ; rc =pci_irq_vector (pdev, info->irq);
Re: [PATCH v2 2/9] perf/cxl: Clear stale event fields before reprogramming a counter
In reply to Jonathan CameronQuoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 29 Jul 2026 07:55:48 -0700 Dave Jiang <[email protected]> wrote:
cxl_pmu_event_start()ORs the event group id, event mask, edge and invert selections into a configurable counter's config register without clearing those fields first, andcxl_pmu_event_stop()leaves them set. Reusing a counter for another event therefore ORs the new selection onto the old, counting the wrong events with stale edge/invert behaviour.
Quoted 4 lines
Quoted 2 lines
Clear
EVENT_GRP_ID_IDX, EVENTS, EDGE and INVERT before applying the new values.Maybe use
FIELD_MODIFY()instead of mask then fill?
Will do
DJ
Quoted 35 lines
Otherwise LGTM.
Quoted 31 lines
Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 3 +++ 1 file changed, 3 insertions(+)drivers/perf/cxl_pmu.c +3
-0@@ -641,6 +641,7 @@ static void cxl_pmu_event_start(struct perf_event *event, int flags) } cfg =readq (base +CXL_PMU_COUNTER_CFG_REG (hwc->idx)); + cfg &= ~(CXL_PMU_COUNTER_CFG_EDGE |CXL_PMU_COUNTER_CFG_INVERT ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_INT_ON_OVRFLW ,1 ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_FREEZE_ON_OVRFLW ,1 ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_ENABLE ,1 );@@ -651,6 +652,8 @@ static void cxl_pmu_event_start(struct perf_event *event, int flags)/* Fixed purpose counters have next two fields RO */ if (test_bit (hwc->idx, info->conf_counter_bm)) { + cfg &= ~(CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK | +CXL_PMU_COUNTER_CFG_EVENTS_MSK ); cfg |=FIELD_PREP (CXL_PMU_COUNTER_CFG_EVENT_GRP_ID_IDX_MSK , hwc->event_base); cfg |= FIELD_PREP(CXL_PMU_COUNTER_CFG_EVENTS_MSK,
Re: [PATCH v2 1/9] perf/cxl: Program the requested event group on configurable counters
In reply to Jonathan CameronQuoted 30 lines · Jonathan Cameron
Quoted 11 lines · On Wed, 29 Jul 2026 07:55:47 -0700 Dave Jiang <[email protected]> wrote:
cxl_pmu_get_event_idx()sets*counter_idxfor a configurable counter but never sets*event_idx, sohwc->event_basestays 0 andcxl_pmu_event_start()programs event group 0 regardless of the group the user requested.Return the matched capability's
event_idx.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>One minor thing. Otherwise LGTM
Reviewed-by Jonathan Cameron <[email protected]>Quoted 12 lines
---
drivers/perf/cxl_pmu.c | 2 ++ 1 file changed, 2 insertions(+)drivers/perf/cxl_pmu.c +0
-0@@ -552,6 +552,8 @@ static int cxl_pmu_get_event_idx(struct perf_event *event, int *counter_idx,return -EINVAL ;
Quoted 4 lines
drivers/perf/cxl_pmu.c static int cxl_pmu_get_event_idx(struct perf_event *event, int *counter_idx, · line 555
Quoted 2 lines
drivers/perf/cxl_pmu.c +1
-0*counter_idx = i; +if (event_idx)Hmm. We never want to get
counter_idxand notevent_idx. Perhaps add this check to the one forcounter_idxat the top of this scope.
Ok I'll address that.
Quoted 6 lines
Quoted 5 lines
drivers/perf/cxl_pmu.c +1
-0+ *event_idx = pmu_ev->event_idx;return 0 ; }
Re: [PATCH v2 3/9] perf/cxl: Fix the counter overflow delta fixup
In reply to Jonathan CameronQuoted 31 lines · Jonathan Cameron
Quoted 2 lines · On Wed, 29 Jul 2026 07:55:49 -0700 Dave Jiang <[email protected]> wrote:
Counters are configured with Freeze on Overflow and are never reloaded: on overflow the counter wraps to 0, counts on until the CPMU freezes, and
Hi Dave,
Thanks for looking at these.
Why would it count on if it froze? I think the bot is tripping over the fact we don't yet implement free running counters (and the other bug about not unfreezing) for currently the ability to freeze on only some counters (to do periodic sampling for instance).
The CXL CPMU spec is incredibly broad in what is supported, so maybe we want to harden things anyway but I'm not sure the condition described by most of this is real.
Quoted 2 lines
retains that residual (CXL r4.0 8.2.7.2.3). So the masked subtraction in
__cxl_pmu_read()only spans the wrap whennew_cnt<prev_cnt.It's been a long time so maybe I have how this was meant to work wrong.
There are two paths to
__cxl_pmu_read()1. We have freeze on overflow enabled so any counter that overflows results in an interrupt. At that point all counters are frozen.
We then read only the counter that overflowed (which is 0) and that will update theprev_cntstorage. No chance of hitting the full wrap around seen here.
Quoted 7 lines
2. An on demand read (polling) In this case the counter may take any value, but because we have freeze on overflow it can't have wrapped (as otherwise we'd have taken path 1).
So slightly fun question of why we have any wrapping control and I think the answer is because the freeze on overflow isn't very specific in the spec for whether it freezes on max value or 0.
Vague enough that hardware can freeze on overflow but leave greater than 0 value? Or is it always either max value or 0? If you don't think this patch is needed I can drop.
DJ
Quoted 90 lines
Quoted 18 lines
The fixup keys off the delta rather than the operands:
delta = (new_cnt - prev_cnt) & GENMASK_ULL(counter_width - 1, 0); if (overflow && delta < GENMASK_ULL(counter_width - 1, 0)) delta += (1UL << counter_width);and is wrong both ways. After a mid-period read the subtraction already spans the wrap, but the guard holds for all but one delta value, so a second period is added and the event over-counts. After an earlier overflow read - or after
event_start(), which leavesprev_countat 0 -prev_cntis a small residual, and once the new residual is greater or equal only the difference between the two is counted and the period is lost, which for a residual of 0 means every period after the first. 'perf stat -I' hits the former, plain 'perf stat' the latter.Quoted 2 lines
The shift is also undefined for
counter_width== 64, and for >= 32 on 32-bit kernels where 1UL is 32 bits.That bit seems valid.
Quoted 66 lines
Condition the fixup on
new_cnt>=prev_cnt, the one case the masked subtraction cannot express, and use mask + 1 for the period: it is 0 for a 64-bit counter, the correct sum mod 2^64, and needs no shift. A userspace read landing between the wrap and the handler clearing the overflow status stays correct because the counter is frozen and cannot advance between the two reads.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>---
v2:
- Fix the fixup instead of deleting it. v1 dropped it on the rationale that the masked subtraction already handles the wrap, which only holds fornew_cnt<prev_cnt; with freeze on overflow and no reload the overflow path routinely seesnew_cnt>=prev_cnt, where v1 silently dropped a full period (sashiko-bot,
https://lore.kernel.org/linux-cxl/[email protected]/).
- Keep the overflow argument and the __cxl_pmu_read()/cxl_pmu_read() split; v1 folded them together once the argument went unused.
- Use mask + 1 rather than 1ULL <<counter_widthso the 64-bit counter case needs no special casing.--- drivers/perf/cxl_pmu.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)drivers/perf/cxl_pmu.c +12
-6@@ -690,7 +690,7 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) {struct cxl_pmu_info *info =pmu_to_cxl_pmu_info (event->pmu);struct hw_perf_event *hwc = &event->hw; -u64 new_cnt, prev_cnt, delta; +u64 new_cnt, prev_cnt, delta, mask; do { prev_cnt = local64_read(&hwc->prev_count);@@ -698,12 +698,18 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) }while (local64_cmpxchg (&hwc->prev_count, prev_cnt, new_cnt) != prev_cnt);/* -* If we know an overflow occur then take that into account. -* Note counter is not reset as that would lose events +* The counter wraps to 0 and is never reloaded, so the masked unsigned +* subtraction only recovers a wrap when new_cnt < prev_cnt. On the +* overflow path new_cnt >= prev_cnt means the full period elapsed +* (prev_cnt is 0 from event_start(), or the residual left by an earlier +* overflow), so add the period back. mask + 1 is 2^counter_width, which +* evaluates to 0 for a 64-bit counter - the correct sum mod 2^64, and +* avoids the undefined 1 << 64. */ - delta = (new_cnt - prev_cnt) &GENMASK_ULL (info->counter_width -1 ,0 ); -if (overflow && delta <GENMASK_ULL (info->counter_width -1 ,0 )) - delta += (1UL << info->counter_width); + mask =GENMASK_ULL (info->counter_width -1 ,0 ); + delta = (new_cnt - prev_cnt) & mask; +if (overflow && new_cnt >= prev_cnt) + delta += mask +1 ;local64_add (delta, &event->count); }
Re: [PATCH v2 3/9] perf/cxl: Fix the counter overflow delta fixup
Quoted 38 lines · Dave Jiang
Quoted 35 lines · Jonathan Cameron
Quoted 2 lines · On Wed, 29 Jul 2026 07:55:49 -0700 Dave Jiang <[email protected]> wrote:
Counters are configured with Freeze on Overflow and are never reloaded: on overflow the counter wraps to 0, counts on until the CPMU freezes, and
Hi Dave,
Thanks for looking at these.
Why would it count on if it froze? I think the bot is tripping over the fact we don't yet implement free running counters (and the other bug about not unfreezing) for currently the ability to freeze on only some counters (to do periodic sampling for instance).
The CXL CPMU spec is incredibly broad in what is supported, so maybe we want to harden things anyway but I'm not sure the condition described by most of this is real.
Quoted 2 lines
retains that residual (CXL r4.0 8.2.7.2.3). So the masked subtraction in
__cxl_pmu_read()only spans the wrap whennew_cnt<prev_cnt.It's been a long time so maybe I have how this was meant to work wrong.
There are two paths to
__cxl_pmu_read()1. We have freeze on overflow enabled so any counter that overflows results in an interrupt. At that point all counters are frozen.
We then read only the counter that overflowed (which is 0) and that will update theprev_cntstorage. No chance of hitting the full wrap around seen here.2. An on demand read (polling) In this case the counter may take any value, but because we have freeze on overflow it can't have wrapped (as otherwise we'd have taken path 1).
Quoted 5 lines
Quoted 3 lines
So slightly fun question of why we have any wrapping control and I think the answer is because the freeze on overflow isn't very specific in the spec for whether it freezes on max value or 0.
Vague enough that hardware can freeze on overflow but leave greater than 0 value? Or is it always either max value or 0? If you don't think this patch is needed I can drop.
Actually the spec does defines it to be 0 on overflow (8.2.7.2.3). Although there isn't clear language that indicates with the freeze, if any chance of any additional events being counted before freeze take effect.
Re: [PATCH v2 4/9] perf/cxl: Accept an overflow interrupt on MSI message number 0
In reply to Robin MurphyQuoted 9 lines · Robin Murphy
Quoted 6 lines · Dave Jiang
cxl_pmu_probe()rejects the PMU with-EINVALwheninfo->irq<= 0, butinfo->irqholds the MSI/MSI-X message number the device signals overflow on (CXL_PMU_CAP_MSI_N_MSK, a 0-based 4-bit field), with -1 meaning "no interrupt support". Message number 0 is valid andpci_irq_vector()takes a 0-based index, so a compliant device signalling on the first vector is wrongly refused and fails to probe.If
info->irqis a PCI MSI vector index, not the Linux vIRQ number, then surely that means theirq_set_affinity()calls are wrong as well?
So info->irq reverts back to Linux vIRQ after the irq is requested. It's kind of messy. Maybe we should split it into two fields to remove the confusion? I can create a patch for that before this patch change.
DJ
Quoted 28 lines
Thanks,
Robin.Quoted 23 lines
Reject only the no-interrupt case (
info->irq< 0), matching the sibling CXL mailbox and event IRQ handling.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>---
drivers/perf/cxl_pmu.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)drivers/perf/cxl_pmu.c +1
-0@@ -876,7 +876,7 @@ static int cxl_pmu_probe(struct device *dev) .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }; - if (info->irq <= 0) + if (info->irq < 0) return -EINVAL; rc = pci_irq_vector(pdev, info->irq);
Jonathan Cameron
maintainerRe: [PATCH v2 8/9] perf/cxl: Don't use pmu.dev in IRQ and hotplug callbacks after unregister
In reply to Robin MurphyQuoted 29 lines · On Thu, 30 Jul 2026 12:38:28 +0100 Robin Murphy <[email protected]> wrote:
Quoted 28 lines · Dave Jiang
On device removal the devm actions unwind LIFO, so
cxl_pmu_perf_unregister()runs first andperf_pmu_unregister()freesinfo->pmu.dev(device_del()+put_device()->kfree()). The overflow IRQ (freed last) and the CPU-hotplug instance (removed next) are still live at that point, and bothcxl_pmu_irq()andcxl_pmu_offline_cpu()log via dev_dbg()/dev_err() oninfo->pmu.dev, dereferencing freed memory. The shared IRQ can be entered for a co-function on the same MSI vector, and a CPU can go offline in the window before the hotplug instance is removed.Log through
info->pmu.parentinstead, thecxl_pmudevice passed to probe, which is devm-managed and outlives every teardown action.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/perf/cxl_pmu.c +0
-0@@ -803,7 +803,7 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)struct perf_event *event = info->hw_events[i];
Quoted 9 lines
drivers/perf/cxl_pmu.c static irqreturn_t cxl_pmu_irq(int irq, void *data) · line 808
Quoted 5 lines
drivers/perf/cxl_pmu.c +1
-1if (!event) { -dev_dbg (info->pmu.dev, +dev_dbg (info->pmu.parent,"overflow but on non enabled counter %d\n" , i);continue ;This seems dubious - we don't permit sharing the IRQ, and all events must have been stopped and descheduled to allow the PMU to be removed in the first place, so how would an overflow interrupt happen?
Hmm. Today the driver itself does permit sharing. Which is awkward given need for the interrupts not to get migrated to different CPUs which I guess might happen if we get a race with driver bind and hotplug events. I may well be missing other reasons sharing is bad, but that one seems like enough to rule it out.
Perhaps the fix for now is remove the IRQF_SHARED flag. Clear no one
was using the driver yet with real hardware given some of the fixes in this series so we aren't going to regress anyone.
Hopefully no one actually thinks a device that puts PMUs on shared interrupts is a good idea and no host running CXL runs out and has to force the PCIe stuff to collapse them to a smaller set of vectors.
Quoted 5 lines
drivers/perf/cxl_pmu.c static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) · line 967
Quoted 5 lines
drivers/perf/cxl_pmu.c +0
-0}@@ -966,7 +966,7 @@ static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) info->on_cpu =-1 ;
target = cpumask_any_but(cpu_online_mask, cpu);
Quoted 4 lines
drivers/perf/cxl_pmu.c static int cxl_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) · line 968
Quoted 1 line
drivers/perf/cxl_pmu.c +0
-0if (target >= nr_cpu_ids) {And this again is nonsense anyway - if the pointless dead code is bothering people, just delete the whole check.
True enough. I fear I gut and paste that from somewhere so might be worth a more general scrub for other instances :(
J
Quoted 11 lines
Thanks,
Robin.Quoted 5 lines
drivers/perf/cxl_pmu.c +1
-1-dev_err (info->pmu.dev,"Unable to find a suitable CPU\n" ); +dev_err (info->pmu.parent,"Unable to find a suitable CPU\n" );return 0 ; }
Jonathan Cameron
maintainerRe: [PATCH v2 6/9] perf/cxl: Unfreeze counters after handling an overflow interrupt
In reply to Robin MurphyQuoted 27 lines · On Thu, 30 Jul 2026 13:04:17 +0100 Robin Murphy <[email protected]> wrote:
Quoted 26 lines · Dave Jiang
The counters are configured with Freeze on Overflow, so when any counter overflows the CPMU freezes every counter in the block (CXL r4.0 §8.2.7.2.1).
cxl_pmu_irq()reads the overflowed counters and clears the overflow status, but never writes the CPMU Freeze register to unfreeze, so all counters stay frozen until the nextpmu_enable()and events in that window are silently lost.Unfreeze after clearing the overflow status so counting resumes.
Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/perf/cxl_pmu.c +0
-0@@ -804,6 +804,15 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)writeq (overflowed, base +CXL_PMU_OVERFLOW_REG );
Quoted 14 lines
drivers/perf/cxl_pmu.c static irqreturn_t cxl_pmu_irq(int irq, void *data) · line 814
Quoted 8 lines
drivers/perf/cxl_pmu.c +8
-0+/* +* Counters are configured to freeze on overflow (Freeze on Overflow), +* which freezes every counter in the CPMU. Once the overflowed counters +* have been read and their status cleared, unfreeze so counting resumes; +* otherwise all counters stay frozen until the next pmu_enable() and +* events are silently lost. +*/ +writeq (0 , base +CXL_PMU_FREEZE_REG );Does this unconditionally unfreeze _all_ counters, including any which might have have overflowed since the read of
CXL_PMU_OVERFLOW_REGand thus have not been handled yet? Or are we hoping that the "global freeze" behaviour prevents that from being able to happen?
Yes global freeze should stop world on first one overflowing - if two go together we should see them both in the handler.
Quoted 3 lines
Furthermore, what if an overflow happens to occur just as
cxl_pmu_disable()is intentionally freezing all the counters, such that by the time we get here, unfreezing them would be the wrong thing to do?
Hmm. That may indeed be bad.
Quoted 9 lines
Thanks,
Robin.Quoted 4 lines
drivers/perf/cxl_pmu.c +1
-0+return IRQ_HANDLED ; }
Jonathan Cameron
maintainerRe: [PATCH v2 5/9] perf/cxl: Keep the overflow interrupt pinned to the managed CPU
In reply to Robin MurphyQuoted 37 lines · On Thu, 30 Jul 2026 12:46:24 +0100 Robin Murphy <[email protected]> wrote:
Quoted 26 lines · Dave Jiang
The PMU pins its overflow interrupt to
info->on_cpuin the hotplug online/offline callbacks, but requests it with onlyIRQF_SHARED|IRQF_NO_THREAD. WithoutIRQF_NOBALANCING, irqbalance or a userspacesmp_affinitywrite can move the interrupt to another CPU.cxl_pmu_irq()then runscxl_pmu_read()there, doing local64_cmpxchg()/local64_add() onhwc->prev_countandevent->countconcurrently with the managing CPU;local64_tis only atomic against same-CPU access, so counts get corrupted.Add
IRQF_NOBALANCINGso the pinning done in the hotplug callbacks holds, matching other uncore-style PMU drivers.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +0
-0drivers/perf/cxl_pmu.c static int cxl_pmu_probe(struct device *dev) · line 892
Quoted 6 lines
drivers/perf/cxl_pmu.c +2
-1@@ -888,7 +888,8 @@ static int cxl_pmu_probe(struct device *dev)if (!irq_name)return -ENOMEM ; - rc = devm_request_irq(dev, irq, cxl_pmu_irq, IRQF_SHARED | IRQF_NO_THREAD, + rc = devm_request_irq(dev, irq, cxl_pmu_irq, + IRQF_SHARED | IRQF_NO_THREAD | IRQF_NOBALANCING,Bah, sorry, now I see I misspoke just now on the other patch - PMUs really _shouldn't_ permit shared IRQs, but this one does :(
Quoted 3 lines
Thus it's all well and good to prevent userspace changing affinity, but it doesn't help _all_ that much if other drivers can still legitimately change it behind our backs...
We do in theory know what drivers might do that (and today I don't think any but this one do). In practice though I think two instances of this on same device could foul things up.
As per other suggestion, Dave would you mind just dropping shared support for now? Can revisit if anyone built that insane hardware.
J
Quoted 8 lines
Thanks,
Robin.Quoted 3 lines
drivers/perf/cxl_pmu.c +0
-0irq_name, info);if (rc)return rc;
Jonathan Cameron
maintainerRe: [PATCH v2 4/9] perf/cxl: Accept an overflow interrupt on MSI message number 0
In reply to Robin MurphyQuoted 10 lines · On Thu, 30 Jul 2026 13:18:42 +0100 Robin Murphy <[email protected]> wrote:
Quoted 6 lines · Dave Jiang
cxl_pmu_probe()rejects the PMU with-EINVALwheninfo->irq<= 0, butinfo->irqholds the MSI/MSI-X message number the device signals overflow on (CXL_PMU_CAP_MSI_N_MSK, a 0-based 4-bit field), with -1 meaning "no interrupt support". Message number 0 is valid andpci_irq_vector()takes a 0-based index, so a compliant device signalling on the first vector is wrongly refused and fails to probe.If
info->irqis a PCI MSI vector index, not the Linux vIRQ number, then surely that means theirq_set_affinity()calls are wrong as well?
yup. I really messed this driver up :(
should rename this one and then have irq mean irq.
Quoted 30 lines
Thanks,
Robin.Quoted 25 lines
Reject only the no-interrupt case (
info->irq< 0), matching the sibling CXL mailbox and event IRQ handling.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/perf/cxl_pmu.c +1
-1@@ -876,7 +876,7 @@ static int cxl_pmu_probe(struct device *dev) .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }; -if (info->irq <=0 ) +if (info->irq <0 )return -EINVAL ; rc =pci_irq_vector (pdev, info->irq);
Jonathan Cameron
maintainerRe: [PATCH v2 3/9] perf/cxl: Fix the counter overflow delta fixup
In reply to Dave JiangQuoted 36 lines · On Thu, 30 Jul 2026 09:56:35 -0700 Dave Jiang <[email protected]> wrote:
Quoted 35 lines · Jonathan Cameron
Quoted 2 lines · On Wed, 29 Jul 2026 07:55:49 -0700 Dave Jiang <[email protected]> wrote:
Counters are configured with Freeze on Overflow and are never reloaded: on overflow the counter wraps to 0, counts on until the CPMU freezes, and
Hi Dave,
Thanks for looking at these.
Why would it count on if it froze? I think the bot is tripping over the fact we don't yet implement free running counters (and the other bug about not unfreezing) for currently the ability to freeze on only some counters (to do periodic sampling for instance).
The CXL CPMU spec is incredibly broad in what is supported, so maybe we want to harden things anyway but I'm not sure the condition described by most of this is real.
Quoted 2 lines
retains that residual (CXL r4.0 8.2.7.2.3). So the masked subtraction in
__cxl_pmu_read()only spans the wrap whennew_cnt<prev_cnt.It's been a long time so maybe I have how this was meant to work wrong.
There are two paths to
__cxl_pmu_read()1. We have freeze on overflow enabled so any counter that overflows results in an interrupt. At that point all counters are frozen.
We then read only the counter that overflowed (which is 0) and that will update theprev_cntstorage. No chance of hitting the full wrap around seen here.2. An on demand read (polling) In this case the counter may take any value, but because we have freeze on overflow it can't have wrapped (as otherwise we'd have taken path 1).
Quoted 5 lines
Quoted 3 lines
So slightly fun question of why we have any wrapping control and I think the answer is because the freeze on overflow isn't very specific in the spec for whether it freezes on max value or 0.
Vague enough that hardware can freeze on overflow but leave greater than 0 value? Or is it always either max value or 0? If you don't think this patch is needed I can drop.
Ah. Good point. I think the spec at least comprehends increments of more than 1 for some events so it might not be 0 and this patch would therefore be needed for that corner case.
Quoted 94 lines
DJ
Quoted 90 lines
Quoted 18 lines
The fixup keys off the delta rather than the operands:
delta = (new_cnt - prev_cnt) & GENMASK_ULL(counter_width - 1, 0); if (overflow && delta < GENMASK_ULL(counter_width - 1, 0)) delta += (1UL << counter_width);and is wrong both ways. After a mid-period read the subtraction already spans the wrap, but the guard holds for all but one delta value, so a second period is added and the event over-counts. After an earlier overflow read - or after
event_start(), which leavesprev_countat 0 -prev_cntis a small residual, and once the new residual is greater or equal only the difference between the two is counted and the period is lost, which for a residual of 0 means every period after the first. 'perf stat -I' hits the former, plain 'perf stat' the latter.Quoted 2 lines
The shift is also undefined for
counter_width== 64, and for >= 32 on 32-bit kernels where 1UL is 32 bits.That bit seems valid.
Quoted 66 lines
Condition the fixup on
new_cnt>=prev_cnt, the one case the masked subtraction cannot express, and use mask + 1 for the period: it is 0 for a 64-bit counter, the correct sum mod 2^64, and needs no shift. A userspace read landing between the wrap and the handler clearing the overflow status stays correct because the counter is frozen and cannot advance between the two reads.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>---
v2:
- Fix the fixup instead of deleting it. v1 dropped it on the rationale that the masked subtraction already handles the wrap, which only holds fornew_cnt<prev_cnt; with freeze on overflow and no reload the overflow path routinely seesnew_cnt>=prev_cnt, where v1 silently dropped a full period (sashiko-bot,
https://lore.kernel.org/linux-cxl/[email protected]/).
- Keep the overflow argument and the __cxl_pmu_read()/cxl_pmu_read() split; v1 folded them together once the argument went unused.
- Use mask + 1 rather than 1ULL <<counter_widthso the 64-bit counter case needs no special casing.--- drivers/perf/cxl_pmu.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)drivers/perf/cxl_pmu.c +12
-6@@ -690,7 +690,7 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) {struct cxl_pmu_info *info =pmu_to_cxl_pmu_info (event->pmu);struct hw_perf_event *hwc = &event->hw; -u64 new_cnt, prev_cnt, delta; +u64 new_cnt, prev_cnt, delta, mask; do { prev_cnt = local64_read(&hwc->prev_count);@@ -698,12 +698,18 @@ static void __cxl_pmu_read(struct perf_event *event, bool overflow) }while (local64_cmpxchg (&hwc->prev_count, prev_cnt, new_cnt) != prev_cnt);/* -* If we know an overflow occur then take that into account. -* Note counter is not reset as that would lose events +* The counter wraps to 0 and is never reloaded, so the masked unsigned +* subtraction only recovers a wrap when new_cnt < prev_cnt. On the +* overflow path new_cnt >= prev_cnt means the full period elapsed +* (prev_cnt is 0 from event_start(), or the residual left by an earlier +* overflow), so add the period back. mask + 1 is 2^counter_width, which +* evaluates to 0 for a 64-bit counter - the correct sum mod 2^64, and +* avoids the undefined 1 << 64. */ - delta = (new_cnt - prev_cnt) &GENMASK_ULL (info->counter_width -1 ,0 ); -if (overflow && delta <GENMASK_ULL (info->counter_width -1 ,0 )) - delta += (1UL << info->counter_width); + mask =GENMASK_ULL (info->counter_width -1 ,0 ); + delta = (new_cnt - prev_cnt) & mask; +if (overflow && new_cnt >= prev_cnt) + delta += mask +1 ;local64_add (delta, &event->count); }
Re: [PATCH v2 4/9] perf/cxl: Accept an overflow interrupt on MSI message number 0
In reply to Jonathan CameronQuoted 11 lines · Jonathan Cameron
Quoted 8 lines · On Thu, 30 Jul 2026 13:18:42 +0100 Robin Murphy <[email protected]> wrote:
Quoted 6 lines · Dave Jiang
cxl_pmu_probe()rejects the PMU with-EINVALwheninfo->irq<= 0, butinfo->irqholds the MSI/MSI-X message number the device signals overflow on (CXL_PMU_CAP_MSI_N_MSK, a 0-based 4-bit field), with -1 meaning "no interrupt support". Message number 0 is valid andpci_irq_vector()takes a 0-based index, so a compliant device signalling on the first vector is wrongly refused and fails to probe.
Quoted 6 lines
Quoted 2 lines
If
info->irqis a PCI MSI vector index, not the Linux vIRQ number, then surely that means theirq_set_affinity()calls are wrong as well?yup. I really messed this driver up :(
should rename this one and then have irq mean irq.
I have a patch for v3 :) >
Quoted 32 lines
Quoted 31 lines
Thanks,
Robin.Quoted 25 lines
Reject only the no-interrupt case (
info->irq< 0), matching the sibling CXL mailbox and event IRQ handling.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/perf/cxl_pmu.c +1
-1@@ -876,7 +876,7 @@ static int cxl_pmu_probe(struct device *dev) .capabilities = PERF_PMU_CAP_NO_EXCLUDE, }; -if (info->irq <=0 ) +if (info->irq <0 )return -EINVAL ; rc =pci_irq_vector (pdev, info->irq);
Re: [PATCH v2 5/9] perf/cxl: Keep the overflow interrupt pinned to the managed CPU
In reply to Jonathan CameronQuoted 44 lines · Jonathan Cameron
Quoted 37 lines · On Thu, 30 Jul 2026 12:46:24 +0100 Robin Murphy <[email protected]> wrote:
Quoted 26 lines · Dave Jiang
The PMU pins its overflow interrupt to
info->on_cpuin the hotplug online/offline callbacks, but requests it with onlyIRQF_SHARED|IRQF_NO_THREAD. WithoutIRQF_NOBALANCING, irqbalance or a userspacesmp_affinitywrite can move the interrupt to another CPU.cxl_pmu_irq()then runscxl_pmu_read()there, doing local64_cmpxchg()/local64_add() onhwc->prev_countandevent->countconcurrently with the managing CPU;local64_tis only atomic against same-CPU access, so counts get corrupted.Add
IRQF_NOBALANCINGso the pinning done in the hotplug callbacks holds, matching other uncore-style PMU drivers.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +0
-0drivers/perf/cxl_pmu.c static int cxl_pmu_probe(struct device *dev) · line 892
Quoted 6 lines
drivers/perf/cxl_pmu.c +2
-1@@ -888,7 +888,8 @@ static int cxl_pmu_probe(struct device *dev)if (!irq_name)return -ENOMEM ; - rc = devm_request_irq(dev, irq, cxl_pmu_irq, IRQF_SHARED | IRQF_NO_THREAD, + rc = devm_request_irq(dev, irq, cxl_pmu_irq, + IRQF_SHARED | IRQF_NO_THREAD | IRQF_NOBALANCING,Bah, sorry, now I see I misspoke just now on the other patch - PMUs really _shouldn't_ permit shared IRQs, but this one does :(
Quoted 3 lines
Thus it's all well and good to prevent userspace changing affinity, but it doesn't help _all_ that much if other drivers can still legitimately change it behind our backs...
Quoted 6 lines
We do in theory know what drivers might do that (and today I don't think any but this one do). In practice though I think two instances of this on same device could foul things up.
As per other suggestion, Dave would you mind just dropping shared support for now? Can revisit if anyone built that insane hardware.
Yup I'll do that.
Re: [PATCH v2 5/9] perf/cxl: Keep the overflow interrupt pinned to the managed CPU
In reply to Robin MurphyQuoted 35 lines · Robin Murphy
Quoted 26 lines · Dave Jiang
The PMU pins its overflow interrupt to
info->on_cpuin the hotplug online/offline callbacks, but requests it with onlyIRQF_SHARED|IRQF_NO_THREAD. WithoutIRQF_NOBALANCING, irqbalance or a userspacesmp_affinitywrite can move the interrupt to another CPU.cxl_pmu_irq()then runscxl_pmu_read()there, doing local64_cmpxchg()/local64_add() onhwc->prev_countandevent->countconcurrently with the managing CPU;local64_tis only atomic against same-CPU access, so counts get corrupted.Add
IRQF_NOBALANCINGso the pinning done in the hotplug callbacks holds, matching other uncore-style PMU drivers.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>---
drivers/perf/cxl_pmu.c| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +0
-0drivers/perf/cxl_pmu.c static int cxl_pmu_probe(struct device *dev) · line 892
Quoted 5 lines
drivers/perf/cxl_pmu.c +2
-0@@ -888,7 +888,8 @@ static int cxl_pmu_probe(struct device *dev) if (!irq_name) return -ENOMEM; - rc = devm_request_irq(dev, irq, cxl_pmu_irq, IRQF_SHARED | IRQF_NO_THREAD, + rc = devm_request_irq(dev, irq, cxl_pmu_irq, + IRQF_SHARED | IRQF_NO_THREAD | IRQF_NOBALANCING,Bah, sorry, now I see I misspoke just now on the other patch - PMUs really _shouldn't_ permit shared IRQs, but this one does :(
Quoted 1 line
Thus it's all well and good to prevent userspace changing affinity, but it doesn't help _all_ that much if other drivers can still legitimately change it behind our backs...
I'm also adding NOBALANCING on the CXL side. The interrupts shouldn't be that frequent and are not used for I/O. So it's ok if we block balancing all around if the PMU needs it. And as Jonathan mentioned, dropping SHARED.
Quoted 8 lines
Thanks,
Robin.Quoted 3 lines
drivers/perf/cxl_pmu.c +0
-0irq_name, info); if (rc) return rc;
Re: [PATCH v2 6/9] perf/cxl: Unfreeze counters after handling an overflow interrupt
In reply to Jonathan CameronQuoted 45 lines · Jonathan Cameron
Quoted 27 lines · On Thu, 30 Jul 2026 13:04:17 +0100 Robin Murphy <[email protected]> wrote:
Quoted 26 lines · Dave Jiang
The counters are configured with Freeze on Overflow, so when any counter overflows the CPMU freezes every counter in the block (CXL r4.0 §8.2.7.2.1).
cxl_pmu_irq()reads the overflowed counters and clears the overflow status, but never writes the CPMU Freeze register to unfreeze, so all counters stay frozen until the nextpmu_enable()and events in that window are silently lost.Unfreeze after clearing the overflow status so counting resumes.
Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/perf/cxl_pmu.c +0
-0@@ -804,6 +804,15 @@ static irqreturn_t cxl_pmu_irq(int irq, void *data)writeq (overflowed, base +CXL_PMU_OVERFLOW_REG );Quoted 14 lines
drivers/perf/cxl_pmu.c static irqreturn_t cxl_pmu_irq(int irq, void *data) · line 814
Quoted 8 lines
drivers/perf/cxl_pmu.c +8
-0+/* +* Counters are configured to freeze on overflow (Freeze on Overflow), +* which freezes every counter in the CPMU. Once the overflowed counters +* have been read and their status cleared, unfreeze so counting resumes; +* otherwise all counters stay frozen until the next pmu_enable() and +* events are silently lost. +*/ +writeq (0 , base +CXL_PMU_FREEZE_REG );Does this unconditionally unfreeze _all_ counters, including any which might have have overflowed since the read of
CXL_PMU_OVERFLOW_REGand thus have not been handled yet? Or are we hoping that the "global freeze" behaviour prevents that from being able to happen?
Quoted 8 lines
Yes global freeze should stop world on first one overflowing - if two go together we should see them both in the handler.
Quoted 3 lines
Furthermore, what if an overflow happens to occur just as
cxl_pmu_disable()is intentionally freezing all the counters, such that by the time we get here, unfreezing them would be the wrong thing to do?Hmm. That may indeed be bad.
I can add a bool as a en/disable state in info to guard against that. >
Quoted 10 lines
Quoted 9 lines
Thanks,
Robin.Quoted 4 lines
drivers/perf/cxl_pmu.c +1
-0+return IRQ_HANDLED ; }
Re: [PATCH v2 9/9] perf/cxl: Avoid cpumask_of(-1) when no CPU is assigned
In reply to Jonathan CameronQuoted 24 lines · Jonathan Cameron
Quoted 8 lines · On Wed, 29 Jul 2026 07:55:55 -0700 Dave Jiang <[email protected]> wrote:
cpumask_show()feedsinfo->on_cpustraight intocpumask_of()for the world-readable cpumask sysfs attribute.on_cpuis -1 before the first hotplug online callback and transiently incxl_pmu_offline_cpu()before a new target is chosen.cpumask_of(-1)treats the CPU number as unsigned and does out-of-bounds pointer arithmetic inget_cpu_mask(), so a concurrent read of the attribute dereferences a wild pointer and can fault -- a local denial of service.I'm not keen on the solution here.
The transient state is ugly anyway. We can just move setting it to -1 into the dummy code that deals with that well known case of you have CPUs online and code is still running.
The init case looks like a false positive to me. But maybe I'm missing stuff. The perf registration that surfaces the sysfs happens after hotplug handler is added and I believe that synchronously runs it for CPUs that are already up. Given we are running code (and CXL stuff isn't super early) something will be up so it won't remain -1 by the time of use.
Quoted 3 lines
Can we just use the generic stuff? Maybe need Robin's stuff to add init / exit per driver calls.
https://lore.kernel.org/linux-arm-kernel/[email protected]/
I'll drop this patch for now.
DJ
Quoted 42 lines
In general, I'd like Robin to take a quick look at the more generic perf parts of this series given he has clearly been deep in this stuff a lot more recently than me :)
Jonathan
Quoted 34 lines
Read
on_cpuonce and emit an empty mask when it is negative.Fixes 5d7107c72796 ("perf: CXL Performance Monitoring Unit driver")Reported-by [email protected]Assisted-by Claude:claude-opus-4-8Signed-off-by Dave Jiang <[email protected]>--- drivers/perf/cxl_pmu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)drivers/perf/cxl_pmu.c +10
-1@@ -501,8 +501,17 @@ static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf) {struct cxl_pmu_info *info =dev_get_drvdata (dev); +int cpu =READ_ONCE (info->on_cpu); -return cpumap_print_to_pagebuf (true, buf,cpumask_of (info->on_cpu)); +/* +* on_cpu is -1 before the first online callback and transiently during +* cxl_pmu_offline_cpu(). cpumask_of(-1) computes an out-of-bounds +* pointer, so report an empty mask instead. +*/ +if (cpu <0 ) +return sysfs_emit (buf,"\n" ); + +return cpumap_print_to_pagebuf (true, buf,cpumask_of (cpu)); }static DEVICE_ATTR_RO (cpumask);