x86/events/amd/iommu: Fix cpumask of IOMMU events
Subsystems (4)
Vasant Hegde
maintainerPATCH 1/1 x86/events/amd/iommu: Fix cpumask of IOMMU events
IOMMU performance counters are accessed via MMIO space. Currently IOMMU PMUs uses a single global cpumask (iommu_cpumask) shared across all AMD IOMMU PMU instances. This prevents collecting per-socket numbers (ex: perf stat --per-socket).
Fix this by adjusting cpumask based on IOMMUs NUMA node.
--- arch/x86/events/amd/iommu.c | 29 +++++++++++++++++++++++------ drivers/iommu/amd/init.c | 7 +++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 31 insertions(+), 6 deletions(-)
arch/x86/events/amd/iommu.c +23 -6
@@ -10,6 +10,7 @@#define pr_fmt (fmt) "perf/amd_iommu: " fmt +#include <linux/device.h> #include <linux/perf_event.h> #include <linux/init.h> #include <linux/cpumask.h>
@@ -43,6 +44,7 @@ struct perf_amd_iommu {u8 max_counters;u64 cntr_assign_mask;raw_spinlock_t lock; +cpumask_t cpumask; };static LIST_HEAD (perf_amd_iommu_list);
@@ -131,13 +133,15 @@ static struct amd_iommu_event_desc amd_iommu_v2_event_descs[] = {/*--------------------------------------------- * sysfs cpumask attributes *---------------------------------------------*/ -static cpumask_t iommu_cpumask; -static ssize_t _iommu_cpumask_show (struct device *dev,struct device_attribute *attr,char *buf) { -return cpumap_print_to_pagebuf (true, buf, &iommu_cpumask); +struct pmu *pmu =dev_get_drvdata (dev); +struct perf_amd_iommu *perf_iommu = + container_of(pmu, struct perf_amd_iommu, pmu); + +return cpumap_print_to_pagebuf (true, buf, &perf_iommu->cpumask); } static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);
@@ -420,7 +424,8 @@ static const struct pmu iommu_pmu __initconst = {static __init int init_one_iommu (unsigned int idx) {struct perf_amd_iommu *perf_iommu; -int ret; +struct device *dev; +int node, cpu, ret; perf_iommu = kzalloc_obj(struct perf_amd_iommu);if (!perf_iommu)
@@ -440,6 +445,20 @@ static __init int init_one_iommu(unsigned int idx)return -EINVAL ; } + dev =amd_iommu_idx_to_dev (idx); + node = dev ?dev_to_node (dev) :NUMA_NO_NODE ; +if (node !=NUMA_NO_NODE ) + cpu =cpumask_any_and (cpumask_of_node (node), cpu_online_mask); +else + cpu =cpumask_any (cpu_online_mask); + +if (cpu >= nr_cpu_ids) { +pr_warn ("Failed to find online CPU for IOMMU %d.\n" , idx); +kfree (perf_iommu); +return -ENODEV ; + } +cpumask_set_cpu (cpu, &perf_iommu->cpumask); +snprintf (perf_iommu->name,IOMMU_NAME_SIZE ,"amd_iommu_%u" , idx); ret =perf_pmu_register (&perf_iommu->pmu, perf_iommu->name,-1 );
@@ -483,8 +502,6 @@ static __init int amd_iommu_pc_init(void)return -ENODEV ; } -/* Init cpumask attributes to only core 0 */ -cpumask_set_cpu (0 , &iommu_cpumask);return 0 ; }
drivers/iommu/amd/init.c +7 -0
@@ -3939,6 +3939,13 @@ struct amd_iommu *get_amd_iommu(unsigned int idx)return NULL ; } +struct device *amd_iommu_idx_to_dev (unsigned int idx) +{ +struct amd_iommu *iommu =get_amd_iommu (idx); + +return iommu ? &iommu->dev->dev :NULL ; +} +/**************************************************************************** * * IOMMU EFR Performance Counter support functionality. This code allows
include/linux/amd-iommu.h +1 -0
@@ -67,6 +67,7 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn,int amd_iommu_pc_get_reg (struct amd_iommu *iommu,u8 bank,u8 cntr,u8 fxn,u64 *value);struct amd_iommu *get_amd_iommu (unsigned int idx); +struct device *amd_iommu_idx_to_dev (unsigned int idx);#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_disable (void );
Signature · 1 line
2.31.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] PMU initialization fails for IOMMUs located on NUMA nodes without online CPUs. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] PMU initialization fails for IOMMUs located on NUMA nodes without online CPUs. --
commit ef1363d0a37870d7cf1f1095cd3ebf42cc5a8bdc
Author: Vasant Hegde <[email protected]>
x86/events/amd/iommu: Fix cpumask of IOMMU events
This commit changes the IOMMU PMU cpumask from a single global mask to a per-IOMMU mask based on the NUMA node of the IOMMU, allowing per-socket performance counter collection.
Quoted 3 lines
arch/x86/events/amd/iommu.c +0
-0
[ ... ]
arch/x86/events/amd/iommu.c static __init int init_one_iommu(unsigned int idx) · line 451
Quoted 8 lines
arch/x86/events/amd/iommu.c +4
-0@@ -440,6 +445,20 @@ static __init int init_one_iommu(unsigned int idx)return -EINVAL ; } + dev =amd_iommu_idx_to_dev (idx); + node = dev ?dev_to_node (dev) :NUMA_NO_NODE ; +if (node !=NUMA_NO_NODE ) + cpu =cpumask_any_and (cpumask_of_node (node), cpu_online_mask);
If the IOMMU resides on a CPU-less NUMA node (like a PCIe expansion switch or a memory-only CXL node), or if all CPUs on that node are currently offline, will cpumask_any_and() return a value greater than or equal to nr_cpu_ids?
arch/x86/events/amd/iommu.c static __init int init_one_iommu(unsigned int idx) · line 459
Quoted 8 lines
arch/x86/events/amd/iommu.c +8
-0+else + cpu =cpumask_any (cpu_online_mask); + +if (cpu >= nr_cpu_ids) { +pr_warn ("Failed to find online CPU for IOMMU %d.\n" , idx); +kfree (perf_iommu); +return -ENODEV ; + }
Could this cause PMU initialization to fail for IOMMUs on CPU-less nodes?
Since AMD IOMMU performance counters are accessed via MMIO space, they can be driven by any CPU in the system. Would it be better to fall back to cpumask_any(cpu_online_mask) instead of returning -ENODEV when a local CPU is not available?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Chun-Tse Shao
Tested-byThanks,
CT
Quoted 51 lines · On Sun, May 17, 2026 at 5:12 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] PMU initialization fails for IOMMUs located on NUMA nodes without online CPUs. --commit
ef1363d0a37870d7cf1f1095cd3ebf42cc5a8bdc
Author: Vasant Hegde <[email protected]>x86/events/amd/iommu: Fix cpumask of IOMMU events
This commit changes the IOMMU PMU cpumask from a single global mask to a per-IOMMU mask based on the NUMA node of the IOMMU, allowing per-socket performance counter collection.
Quoted 3 lines
arch/x86/events/amd/iommu.c +0
-0[ ... ]
arch/x86/events/amd/iommu.c static __init int init_one_iommu(unsigned int idx) · line 451
Quoted 8 lines
arch/x86/events/amd/iommu.c +4
-0@@ -440,6 +445,20 @@ static __init int init_one_iommu(unsigned int idx)return -EINVAL ; } + dev =amd_iommu_idx_to_dev (idx); + node = dev ?dev_to_node (dev) :NUMA_NO_NODE ; +if (node !=NUMA_NO_NODE ) + cpu =cpumask_any_and (cpumask_of_node (node), cpu_online_mask);If the IOMMU resides on a CPU-less NUMA node (like a PCIe expansion switch or a memory-only CXL node), or if all CPUs on that node are currently offline, will
cpumask_any_and()return a value greater than or equal tonr_cpu_ids?arch/x86/events/amd/iommu.c static __init int init_one_iommu(unsigned int idx) · line 459
Quoted 8 lines
arch/x86/events/amd/iommu.c +8
-0+else + cpu =cpumask_any (cpu_online_mask); + +if (cpu >= nr_cpu_ids) { +pr_warn ("Failed to find online CPU for IOMMU %d.\n" , idx); +kfree (perf_iommu); +return -ENODEV ; + }Could this cause PMU initialization to fail for IOMMUs on CPU-less nodes?
Since AMD IOMMU performance counters are accessed via MMIO space, they can be driven by any CPU in the system. Would it be better to fall back to
cpumask_any(cpu_online_mask)instead of returning-ENODEVwhen a local CPU is not available?--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Vasant Hegde
maintainerQuoted 5 lines · On 5/17/2026 5:42 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] PMU initialization fails for IOMMUs located on NUMA nodes without online CPUs. --
Thanks for the review.
Quoted 17 lines
commit
ef1363d0a37870d7cf1f1095cd3ebf42cc5a8bdc
Author: Vasant Hegde <[email protected]>x86/events/amd/iommu: Fix cpumask of IOMMU events
This commit changes the IOMMU PMU cpumask from a single global mask to a per-IOMMU mask based on the NUMA node of the IOMMU, allowing per-socket performance counter collection.
Quoted 3 lines
arch/x86/events/amd/iommu.c +0
-0[ ... ]
Quoted 4 lines
arch/x86/events/amd/iommu.c +0
-0@@ -440,6 +445,20 @@ static __init int init_one_iommu(unsigned int idx)return -EINVAL ; }
Quoted 9 lines
arch/x86/events/amd/iommu.c static __init int init_one_iommu(unsigned int idx) · line 451
Quoted 4 lines
arch/x86/events/amd/iommu.c +4
-0+ dev =amd_iommu_idx_to_dev (idx); + node = dev ?dev_to_node (dev) :NUMA_NO_NODE ; +if (node !=NUMA_NO_NODE ) + cpu =cpumask_any_and (cpumask_of_node (node), cpu_online_mask);If the IOMMU resides on a CPU-less NUMA node (like a PCIe expansion switch or a memory-only CXL node), or if all CPUs on that node are currently offline, will
cpumask_any_and()return a value greater than or equal tonr_cpu_ids?
If its CPU-less then put it back to CPU 0? Any suggestions ?
-Vasant
Quoted 8 lines · Vasant Hegde
IOMMU performance counters are accessed via MMIO space. Currently IOMMU PMUs uses a single global cpumask (
iommu_cpumask) shared across all AMD IOMMU PMU instances. This prevents collecting per-socket numbers (ex: perf stat --per-socket).Fix this by adjusting cpumask based on IOMMUs NUMA node.
Signed-off-by Vasant Hegde <[email protected]>
Quoted 118 lines
--- arch/x86/events/amd/iommu.c | 29 +++++++++++++++++++++++------ drivers/iommu/amd/init.c | 7 +++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 31 insertions(+), 6 deletions(-)arch/x86/events/amd/iommu.c +23
-6@@ -10,6 +10,7 @@#define pr_fmt (fmt) "perf/amd_iommu: " fmt +#include <linux/device.h> #include <linux/perf_event.h> #include <linux/init.h> #include <linux/cpumask.h> @@ -43,6 +44,7 @@ struct perf_amd_iommu {u8 max_counters;u64 cntr_assign_mask;raw_spinlock_t lock; +cpumask_t cpumask; };static LIST_HEAD (perf_amd_iommu_list);@@ -131,13 +133,15 @@ static struct amd_iommu_event_desc amd_iommu_v2_event_descs[] = {/*--------------------------------------------- * sysfs cpumask attributes *---------------------------------------------*/ -static cpumask_t iommu_cpumask; -static ssize_t _iommu_cpumask_show (struct device *dev,struct device_attribute *attr,char *buf) { -return cpumap_print_to_pagebuf (true, buf, &iommu_cpumask); +struct pmu *pmu =dev_get_drvdata (dev); +struct perf_amd_iommu *perf_iommu = + container_of(pmu, struct perf_amd_iommu, pmu); + +return cpumap_print_to_pagebuf (true, buf, &perf_iommu->cpumask); } static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);@@ -420,7 +424,8 @@ static const struct pmu iommu_pmu __initconst = {static __init int init_one_iommu (unsigned int idx) {struct perf_amd_iommu *perf_iommu; -int ret; +struct device *dev; +int node, cpu, ret; perf_iommu = kzalloc_obj(struct perf_amd_iommu);if (!perf_iommu)@@ -440,6 +445,20 @@ static __init int init_one_iommu(unsigned int idx)return -EINVAL ; } + dev =amd_iommu_idx_to_dev (idx); + node = dev ?dev_to_node (dev) :NUMA_NO_NODE ; +if (node !=NUMA_NO_NODE ) + cpu =cpumask_any_and (cpumask_of_node (node), cpu_online_mask); +else + cpu =cpumask_any (cpu_online_mask); + +if (cpu >= nr_cpu_ids) { +pr_warn ("Failed to find online CPU for IOMMU %d.\n" , idx); +kfree (perf_iommu); +return -ENODEV ; + } +cpumask_set_cpu (cpu, &perf_iommu->cpumask); +snprintf (perf_iommu->name,IOMMU_NAME_SIZE ,"amd_iommu_%u" , idx); ret =perf_pmu_register (&perf_iommu->pmu, perf_iommu->name,-1 );@@ -483,8 +502,6 @@ static __init int amd_iommu_pc_init(void)return -ENODEV ; } -/* Init cpumask attributes to only core 0 */ -cpumask_set_cpu (0 , &iommu_cpumask);return 0 ; }drivers/iommu/amd/init.c +7
-0@@ -3939,6 +3939,13 @@ struct amd_iommu *get_amd_iommu(unsigned int idx)return NULL ; } +struct device *amd_iommu_idx_to_dev (unsigned int idx) +{ +struct amd_iommu *iommu =get_amd_iommu (idx); + +return iommu ? &iommu->dev->dev :NULL ; +} +/**************************************************************************** * * IOMMU EFR Performance Counter support functionality. This code allows include/linux/amd-iommu.h +1
-0@@ -67,6 +67,7 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn,int amd_iommu_pc_get_reg (struct amd_iommu *iommu,u8 bank,u8 cntr,u8 fxn,u64 *value);struct amd_iommu *get_amd_iommu (unsigned int idx); +struct device *amd_iommu_idx_to_dev (unsigned int idx);#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_disable (void );Signature · 1 line
2.31.1
Quoted 8 lines · Vasant Hegde
IOMMU performance counters are accessed via MMIO space. Currently IOMMU PMUs uses a single global cpumask (
iommu_cpumask) shared across all AMD IOMMU PMU instances. This prevents collecting per-socket numbers (ex: perf stat --per-socket).Fix this by adjusting cpumask based on IOMMUs NUMA node.
Signed-off-by Vasant Hegde <[email protected]>
Quoted 115 lines
--- arch/x86/events/amd/iommu.c | 29 +++++++++++++++++++++++------ drivers/iommu/amd/init.c | 7 +++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 31 insertions(+), 6 deletions(-)arch/x86/events/amd/iommu.c +23
-6@@ -10,6 +10,7 @@#define pr_fmt (fmt) "perf/amd_iommu: " fmt +#include <linux/device.h> #include <linux/perf_event.h> #include <linux/init.h> #include <linux/cpumask.h> @@ -43,6 +44,7 @@ struct perf_amd_iommu {u8 max_counters;u64 cntr_assign_mask;raw_spinlock_t lock; +cpumask_t cpumask; };static LIST_HEAD (perf_amd_iommu_list);@@ -131,13 +133,15 @@ static struct amd_iommu_event_desc amd_iommu_v2_event_descs[] = {/*--------------------------------------------- * sysfs cpumask attributes *---------------------------------------------*/ -static cpumask_t iommu_cpumask; -static ssize_t _iommu_cpumask_show (struct device *dev,struct device_attribute *attr,char *buf) { -return cpumap_print_to_pagebuf (true, buf, &iommu_cpumask); +struct pmu *pmu =dev_get_drvdata (dev); +struct perf_amd_iommu *perf_iommu = + container_of(pmu, struct perf_amd_iommu, pmu); + +return cpumap_print_to_pagebuf (true, buf, &perf_iommu->cpumask); } static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);@@ -420,7 +424,8 @@ static const struct pmu iommu_pmu __initconst = {static __init int init_one_iommu (unsigned int idx) {struct perf_amd_iommu *perf_iommu; -int ret; +struct device *dev; +int node, cpu, ret; perf_iommu = kzalloc_obj(struct perf_amd_iommu);if (!perf_iommu)@@ -440,6 +445,20 @@ static __init int init_one_iommu(unsigned int idx)return -EINVAL ; } + dev =amd_iommu_idx_to_dev (idx); + node = dev ?dev_to_node (dev) :NUMA_NO_NODE ; +if (node !=NUMA_NO_NODE ) + cpu =cpumask_any_and (cpumask_of_node (node), cpu_online_mask); +else + cpu =cpumask_any (cpu_online_mask); + +if (cpu >= nr_cpu_ids) { +pr_warn ("Failed to find online CPU for IOMMU %d.\n" , idx); +kfree (perf_iommu); +return -ENODEV ; + } +cpumask_set_cpu (cpu, &perf_iommu->cpumask); +snprintf (perf_iommu->name,IOMMU_NAME_SIZE ,"amd_iommu_%u" , idx); ret =perf_pmu_register (&perf_iommu->pmu, perf_iommu->name,-1 );@@ -483,8 +502,6 @@ static __init int amd_iommu_pc_init(void)return -ENODEV ; } -/* Init cpumask attributes to only core 0 */ -cpumask_set_cpu (0 , &iommu_cpumask);return 0 ; }drivers/iommu/amd/init.c +7
-0@@ -3939,6 +3939,13 @@ struct amd_iommu *get_amd_iommu(unsigned int idx)return NULL ; } +struct device *amd_iommu_idx_to_dev (unsigned int idx) +{ +struct amd_iommu *iommu =get_amd_iommu (idx); + +return iommu ? &iommu->dev->dev :NULL ; +} +/**************************************************************************** * * IOMMU EFR Performance Counter support functionality. This code allows include/linux/amd-iommu.h +1
-0@@ -67,6 +67,7 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn,int amd_iommu_pc_get_reg (struct amd_iommu *iommu,u8 bank,u8 cntr,u8 fxn,u64 *value);struct amd_iommu *get_amd_iommu (unsigned int idx); +struct device *amd_iommu_idx_to_dev (unsigned int idx);#ifdef CONFIG_KVM_AMD_SEV int amd_iommu_snp_disable (void );
Vasant Hegde
maintainerHi,
Quoted 6 lines · Vasant Hegde
IOMMU performance counters are accessed via MMIO space. Currently IOMMU PMUs uses a single global cpumask (
iommu_cpumask) shared across all AMD IOMMU PMU instances. This prevents collecting per-socket numbers (ex: perf stat --per-socket).Fix this by adjusting cpumask based on IOMMUs NUMA node.
Can you please review/pick this patch?
-Vasant
Quoted 5 lines · Vasant Hegde
IOMMU performance counters are accessed via MMIO space. Currently IOMMU PMUs uses a single global cpumask (
iommu_cpumask) shared across all AMD IOMMU PMU instances. This prevents collecting per-socket numbers (ex: perf stat --per-socket).
Quoted 8 lines
Fix this by adjusting cpumask based on IOMMUs NUMA node.
Signed-off-by Vasant Hegde <[email protected]>--- arch/x86/events/amd/iommu.c | 29 +++++++++++++++++++++++------ drivers/iommu/amd/init.c | 7 +++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 31 insertions(+), 6 deletions(-)
Ping for attention.
Thanks,
CT
Quoted 19 lines · Jörg Rödel
Quoted 5 lines · Vasant Hegde
IOMMU performance counters are accessed via MMIO space. Currently IOMMU PMUs uses a single global cpumask (
iommu_cpumask) shared across all AMD IOMMU PMU instances. This prevents collecting per-socket numbers (ex: perf stat --per-socket).Quoted 8 lines
Fix this by adjusting cpumask based on IOMMUs NUMA node.
Signed-off-by Vasant Hegde <[email protected]>--- arch/x86/events/amd/iommu.c | 29 +++++++++++++++++++++++------ drivers/iommu/amd/init.c | 7 +++++++ include/linux/amd-iommu.h | 1 + 3 files changed, 31 insertions(+), 6 deletions(-)Acked-by Joerg Roedel <[email protected]>