perf/x86/intel/uncore: PMU setup robustness fixes
Subsystems (2)
v2 cover perf/x86/intel/uncore: PMU setup robustness fixes
This series fixes correctness issues in Intel uncore PMU setup:
- If all init_box() on a PMU fails, the PMU sysfs node may still exist, while perf events read zeros and silently report wrong data.
- If init_box() fails on only some dies, perf may return partial non-zero counts, which is harder to diagnose.
- CPU hotplug ref/unref ordering bugs can skip init_box() when the first CPU in a die comes online, and can call box_exit() prematurely when the second-to-last CPU goes offline.
- PCI PMU cleanup on setup failure has activeboxes leaks and potential NULL pointer dereference in error paths.
To address this, the series introduces a PMU broken state to track setup failures and switches MSR/MMIO PMUs to lazy registration, matching existing PCI behavior.
To avoid merge conflicts, this series should be applied after:
https://lore.kernel.org/lkml/[email protected]/
(textual conflict, no logical dependency)
V2 changes:
- Add new patch 1 to fix PCI PMU cleanup issues (Sashiko)
- Keep pmu->activeboxes naming and semantics to avoid potential refcnt leaks in the uncore_pci_remove() path. To accomplish this, make the PMU broken flag sticky and decrement pmu->activeboxes on active box only.
- Update commit messages and changelogs according.
V1: https://lore.kernel.org/lkml/[email protected]/
Sashiko's review: https://sashiko.dev/#/patchset/[email protected]
Zide Chen (8):
perf/x86/intel/uncore: Fix PCI PMU cleanup on setup failure perf/x86/intel/uncore: Fix refcnt and other cleanups perf/x86/intel/uncore: Let init_box() callback report failures perf/x86/intel/uncore: Keep PCI PMUs working when MMIO/MSR setup fails perf/x86/intel/uncore: Factor out box setup code perf/x86/intel/uncore: Introduce PMU flags and broken state perf/x86/intel/uncore: Fix uncore_box ref/unref ordering on CPU
hotplug
perf/x86/intel/uncore: Implement lazy setup for MSR/MMIO PMU
arch/x86/events/intel/uncore.c | 222 +++++++++++------------ arch/x86/events/intel/uncore.h | 39 +++- arch/x86/events/intel/uncore_discovery.c | 21 ++- arch/x86/events/intel/uncore_discovery.h | 6 +- arch/x86/events/intel/uncore_nhmex.c | 3 +- arch/x86/events/intel/uncore_snb.c | 82 ++++++--- arch/x86/events/intel/uncore_snbep.c | 77 +++++--- 7 files changed, 254 insertions(+), 196 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 1/8 perf/x86/intel/uncore: Fix PCI PMU cleanup on setup failure
When uncore_pci_pmu_register() fails, pmu->boxes[die] is set to NULL before returning. In the uncore_pci_remove() path, this causes uncore_pci_pmu_unregister() to be skipped entirely, leaking pmu->activeboxes. In the uncore_bus_notify() path, uncore_pci_pmu_unregister() may still be called and must exit early when pmu->boxes[die] is NULL to avoid a NULL pointer dereference, and to ensure activeboxes is only decremented for a previously active box.
Additionally, since pci_get_drvdata() returns NULL on registration failure, uncore_pci_remove() can no longer treat NULL drvdata as an indicator of an auxiliary PCI device. Remove the associated WARN_ON_ONCE().
---
V2: new patch.
--- arch/x86/events/intel/uncore.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/events/intel/uncore.c +4 -1
@@ -1183,6 +1183,7 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,/* First active box registers the pmu */ ret =uncore_pmu_register (pmu);if (ret) { +atomic_dec (&pmu->activeboxes); pmu->boxes[die] =NULL ;uncore_box_exit (box);kfree (box);
@@ -1248,6 +1249,9 @@ static void uncore_pci_pmu_unregister(struct intel_uncore_pmu *pmu, int die) {struct intel_uncore_box *box = pmu->boxes[die]; +if (!box) +return ; + pmu->boxes[die] =NULL ;if (atomic_dec_return (&pmu->activeboxes) ==0 )uncore_pmu_unregister (pmu);
@@ -1272,7 +1276,6 @@ static void uncore_pci_remove(struct pci_dev *pdev)break ; } } -WARN_ON_ONCE (i >=UNCORE_EXTRA_PCI_DEV_MAX );return ; }
Signature · 1 line
2.54.0
Re: [PATCH V2 1/8] perf/x86/intel/uncore: Fix PCI PMU cleanup on setup failure
Quoted 44 lines · Zide Chen
When
uncore_pci_pmu_register()fails, pmu->boxes[die] is set to NULL before returning. In theuncore_pci_remove()path, this causesuncore_pci_pmu_unregister()to be skipped entirely, leakingpmu->activeboxes. In theuncore_bus_notify()path,uncore_pci_pmu_unregister()may still be called and must exit early when pmu->boxes[die] is NULL to avoid a NULL pointer dereference, and to ensure activeboxes is only decremented for a previously active box.Additionally, since
pci_get_drvdata()returns NULL on registration failure,uncore_pci_remove()can no longer treat NULL drvdata as an indicator of an auxiliary PCI device. Remove the associatedWARN_ON_ONCE().Signed-off-by Zide Chen <[email protected]>---
V2: new patch.--- arch/x86/events/intel/uncore.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/x86/events/intel/uncore.c +4
-0@@ -1183,6 +1183,7 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,/* First active box registers the pmu */ ret =uncore_pmu_register (pmu);if (ret) { +atomic_dec (&pmu->activeboxes); pmu->boxes[die] =NULL ;uncore_box_exit (box);kfree (box);@@ -1248,6 +1249,9 @@ static void uncore_pci_pmu_unregister(struct intel_uncore_pmu *pmu, int die) {struct intel_uncore_box *box = pmu->boxes[die]; +if (!box) +return ; + pmu->boxes[die] =NULL ;if (atomic_dec_return (&pmu->activeboxes) ==0 )uncore_pmu_unregister (pmu);
arch/x86/events/intel/uncore.c static void uncore_pci_remove(struct pci_dev *pdev) · line 1281
Quoted 7 lines
arch/x86/events/intel/uncore.c +0
-1@@ -1272,7 +1276,6 @@ static void uncore_pci_remove(struct pci_dev *pdev)break ; } } -WARN_ON_ONCE (i >=UNCORE_EXTRA_PCI_DEV_MAX );return ; }
PATCH v2 2/8 perf/x86/intel/uncore: Fix refcnt and other cleanups
In reply to Zide ChenFix typo UNCORE_BOX_FLAG_INITIATED to UNCORE_BOX_FLAG_INITIALIZED.
Rename the 'id' parameter in uncore_box_{ref,unref}() to 'die' to reflect its actual meaning and be consistent with other functions.
Remove the incorrect atomic_inc(&box->refcnt) from uncore_pci_pmu_register(): PCI boxes are not tracked by refcnt, and this call incorrectly increments it on a per-die basis.
---
v2:
- Don't rename pmu->activeboxes and keep its semantics because in uncore_pci_remove() path, uncore_pci_pmu_unregister() won't be called for non-active boxes.
- Since pmu->activeboxes keeps it's name, don't need to rename box->refcnt to box->cpu_refcnt.
--- arch/x86/events/intel/uncore.c | 11 +++++------ arch/x86/events/intel/uncore.h | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-)
arch/x86/events/intel/uncore.c +5 -6
@@ -1170,7 +1170,6 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,if (!box)return -ENOMEM ; -atomic_inc (&box->refcnt); box->dieid = die; box->pci_dev = pdev; box->pmu = pmu;
@@ -1518,7 +1517,7 @@ static void uncore_change_context(struct intel_uncore_type **uncores,uncore_change_type_ctx (*uncores, old_cpu, new_cpu); } -static void uncore_box_unref (struct intel_uncore_type **types,int id) +static void uncore_box_unref (struct intel_uncore_type **types,int die) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;
@@ -1529,7 +1528,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id) type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 )uncore_box_exit (box); }
@@ -1604,14 +1603,14 @@ static int allocate_boxes(struct intel_uncore_type **types, }static int uncore_box_ref (struct intel_uncore_type **types, -int id,unsigned int cpu) +int die,unsigned int cpu) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box;int i, ret; - ret =allocate_boxes (types, id, cpu); + ret =allocate_boxes (types, die, cpu);if (ret)return ret;
@@ -1619,7 +1618,7 @@ static int uncore_box_ref(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 )uncore_box_init (box); }
arch/x86/events/intel/uncore.h +3 -3
@@ -185,7 +185,7 @@ struct intel_uncore_box {#define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70#define CFL_UNC_CBO_7_PER_CTR0 0xf76 -#define UNCORE_BOX_FLAG_INITIATED 0 +#define UNCORE_BOX_FLAG_INITIALIZED 0/* event config registers are 8-byte apart */ #define UNCORE_BOX_FLAG_CTL_OFFS8 1/* CFL 8th CBOX has different MSR space */
@@ -559,7 +559,7 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,static inline void uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->init_box) box->pmu->type->ops->init_box (box); }
@@ -567,7 +567,7 @@ static inline void uncore_box_init(struct intel_uncore_box *box)static inline void uncore_box_exit (struct intel_uncore_box *box) { -if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->exit_box) box->pmu->type->ops->exit_box (box); }
Signature · 1 line
2.54.0
Re: [PATCH V2 2/8] perf/x86/intel/uncore: Fix refcnt and other cleanups
Quoted 27 lines · Zide Chen
Fix typo
UNCORE_BOX_FLAG_INITIATEDtoUNCORE_BOX_FLAG_INITIALIZED.Rename the 'id' parameter in uncore_box_{ref,unref}() to 'die' to reflect its actual meaning and be consistent with other functions.
Remove the incorrect
atomic_inc(&box->refcnt)fromuncore_pci_pmu_register(): PCI boxes are not tracked by refcnt, and this call incorrectly increments it on a per-die basis.Signed-off-by Zide Chen <[email protected]>---
v2:
- Don't renamepmu->activeboxesand keep its semantics because inuncore_pci_remove()path,uncore_pci_pmu_unregister()won't be called for non-active boxes.
- Sincepmu->activeboxeskeeps it's name, don't need to renamebox->refcnttobox->cpu_refcnt.--- arch/x86/events/intel/uncore.c | 11 +++++------ arch/x86/events/intel/uncore.h | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-)arch/x86/events/intel/uncore.c +0
-0
arch/x86/events/intel/uncore.c static int uncore_pci_pmu_register(struct pci_dev *pdev, · line 1173
Quoted 4 lines
arch/x86/events/intel/uncore.c +0
-1@@ -1170,7 +1170,6 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,if (!box)return -ENOMEM ; -atomic_inc (&box->refcnt);
I'm not sure if we should remove this. The uncore_box_ref()/uncore_box_unref() are only called for MSR or MMIO type uncore PMUs. For the uncore PMUs of PCI type, the box->refcnt is only increased here. All the 3 kinds of uncore PMUs should keep consistent behavior on the refcnt.
Could we keep this and just decrease the refcnt in uncore_pci_pmu_unregister()? Thanks.
Quoted 78 lines
arch/x86/events/intel/uncore.c +5
-5box->dieid = die; box->pci_dev = pdev; box->pmu = pmu;@@ -1518,7 +1517,7 @@ static void uncore_change_context(struct intel_uncore_type **uncores,uncore_change_type_ctx (*uncores, old_cpu, new_cpu); } -static void uncore_box_unref (struct intel_uncore_type **types,int id) +static void uncore_box_unref (struct intel_uncore_type **types,int die) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;@@ -1529,7 +1528,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id) type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 )uncore_box_exit (box); }@@ -1604,14 +1603,14 @@ static int allocate_boxes(struct intel_uncore_type **types, }static int uncore_box_ref (struct intel_uncore_type **types, -int id,unsigned int cpu) +int die,unsigned int cpu) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box;int i, ret; - ret =allocate_boxes (types, id, cpu); + ret =allocate_boxes (types, die, cpu);if (ret)return ret;@@ -1619,7 +1618,7 @@ static int uncore_box_ref(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 )uncore_box_init (box); }arch/x86/events/intel/uncore.h +3
-3@@ -185,7 +185,7 @@ struct intel_uncore_box {#define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70#define CFL_UNC_CBO_7_PER_CTR0 0xf76 -#define UNCORE_BOX_FLAG_INITIATED 0 +#define UNCORE_BOX_FLAG_INITIALIZED 0/* event config registers are 8-byte apart */ #define UNCORE_BOX_FLAG_CTL_OFFS8 1/* CFL 8th CBOX has different MSR space */ @@ -559,7 +559,7 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,static inline void uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->init_box) box->pmu->type->ops->init_box (box); }@@ -567,7 +567,7 @@ static inline void uncore_box_init(struct intel_uncore_box *box)static inline void uncore_box_exit (struct intel_uncore_box *box) { -if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->exit_box) box->pmu->type->ops->exit_box (box); }
Re: [PATCH V2 2/8] perf/x86/intel/uncore: Fix refcnt and other cleanups
Quoted 32 lines · Dapeng
Quoted 30 lines · Zide Chen
Fix typo
UNCORE_BOX_FLAG_INITIATEDtoUNCORE_BOX_FLAG_INITIALIZED.Rename the 'id' parameter in uncore_box_{ref,unref}() to 'die' to reflect its actual meaning and be consistent with other functions.
Remove the incorrect
atomic_inc(&box->refcnt)fromuncore_pci_pmu_register(): PCI boxes are not tracked by refcnt, and this call incorrectly increments it on a per-die basis.Signed-off-by Zide Chen <[email protected]>---
v2:
- Don't renamepmu->activeboxesand keep its semantics because inuncore_pci_remove()path,uncore_pci_pmu_unregister()won't be called for non-active boxes.
- Sincepmu->activeboxeskeeps it's name, don't need to renamebox->refcnttobox->cpu_refcnt.--- arch/x86/events/intel/uncore.c | 11 +++++------ arch/x86/events/intel/uncore.h | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-)arch/x86/events/intel/uncore.c +0
-0@@ -1170,7 +1170,6 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,if (!box)return -ENOMEM ;
Quoted 7 lines
arch/x86/events/intel/uncore.c static int uncore_pci_pmu_register(struct pci_dev *pdev, · line 1173
Quoted 1 line
arch/x86/events/intel/uncore.c +0
-1-atomic_inc (&box->refcnt);I'm not sure if we should remove this. The uncore_box_ref()/uncore_box_unref() are only called for MSR or MMIO type uncore PMUs. For the uncore PMUs of PCI type, the
box->refcntis only increased here. All the 3 kinds of uncore PMUs should keep consistent behavior on the refcnt.
box->refcnt tracks how many CPUs are active within this die. Here it is incorrectly incremented on a per-die basis. Additionally, during the PCI uncore enumeration or teardown process, there is no per-CPU context, so box->refcnt is useless in PCI uncore.
PCI uncore only needs pmu->activeboxes.
Quoted 82 lines
Could we keep this and just decrease the refcnt in
uncore_pci_pmu_unregister()? Thanks.Quoted 78 lines
arch/x86/events/intel/uncore.c +5
-5box->dieid = die; box->pci_dev = pdev; box->pmu = pmu;@@ -1518,7 +1517,7 @@ static void uncore_change_context(struct intel_uncore_type **uncores,uncore_change_type_ctx (*uncores, old_cpu, new_cpu); } -static void uncore_box_unref (struct intel_uncore_type **types,int id) +static void uncore_box_unref (struct intel_uncore_type **types,int die) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;@@ -1529,7 +1528,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id) type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 )uncore_box_exit (box); }@@ -1604,14 +1603,14 @@ static int allocate_boxes(struct intel_uncore_type **types, }static int uncore_box_ref (struct intel_uncore_type **types, -int id,unsigned int cpu) +int die,unsigned int cpu) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box;int i, ret; - ret =allocate_boxes (types, id, cpu); + ret =allocate_boxes (types, die, cpu);if (ret)return ret;@@ -1619,7 +1618,7 @@ static int uncore_box_ref(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 )uncore_box_init (box); }arch/x86/events/intel/uncore.h +3
-3@@ -185,7 +185,7 @@ struct intel_uncore_box {#define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70#define CFL_UNC_CBO_7_PER_CTR0 0xf76 -#define UNCORE_BOX_FLAG_INITIATED 0 +#define UNCORE_BOX_FLAG_INITIALIZED 0/* event config registers are 8-byte apart */ #define UNCORE_BOX_FLAG_CTL_OFFS8 1/* CFL 8th CBOX has different MSR space */ @@ -559,7 +559,7 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,static inline void uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->init_box) box->pmu->type->ops->init_box (box); }@@ -567,7 +567,7 @@ static inline void uncore_box_init(struct intel_uncore_box *box)static inline void uncore_box_exit (struct intel_uncore_box *box) { -if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->exit_box) box->pmu->type->ops->exit_box (box); }
Re: [PATCH V2 2/8] perf/x86/intel/uncore: Fix refcnt and other cleanups
Quoted 39 lines · Zide
Quoted 31 lines · Dapeng
Quoted 30 lines · Zide Chen
Fix typo
UNCORE_BOX_FLAG_INITIATEDtoUNCORE_BOX_FLAG_INITIALIZED.Rename the 'id' parameter in uncore_box_{ref,unref}() to 'die' to reflect its actual meaning and be consistent with other functions.
Remove the incorrect
atomic_inc(&box->refcnt)fromuncore_pci_pmu_register(): PCI boxes are not tracked by refcnt, and this call incorrectly increments it on a per-die basis.Signed-off-by Zide Chen <[email protected]>---
v2:
- Don't renamepmu->activeboxesand keep its semantics because inuncore_pci_remove()path,uncore_pci_pmu_unregister()won't be called for non-active boxes.
- Sincepmu->activeboxeskeeps it's name, don't need to renamebox->refcnttobox->cpu_refcnt.--- arch/x86/events/intel/uncore.c | 11 +++++------ arch/x86/events/intel/uncore.h | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-)arch/x86/events/intel/uncore.c +0
-0@@ -1170,7 +1170,6 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,if (!box)return -ENOMEM ;Quoted 6 lines
arch/x86/events/intel/uncore.c static int uncore_pci_pmu_register(struct pci_dev *pdev, · line 1173
Quoted 1 line
arch/x86/events/intel/uncore.c +0
-1-atomic_inc (&box->refcnt);I'm not sure if we should remove this. The uncore_box_ref()/uncore_box_unref() are only called for MSR or MMIO type uncore PMUs. For the uncore PMUs of PCI type, the
box->refcntis only increased here. All the 3 kinds of uncore PMUs should keep consistent behavior on the refcnt.
Quoted 6 lines
box->refcnttracks how many CPUs are active within this die. Here it is incorrectly incremented on a per-die basis. Additionally, during the PCI uncore enumeration or teardown process, there is no per-CPU context, sobox->refcntis useless in PCI uncore.PCI uncore only needs
pmu->activeboxes.
Per my understanding, the main aim of refcnt is to prevent the box structure is freed unexpectedly, it doesn't directly couple with CPUs. It just records how many users are using the the box regardless the users are CPU or something else. For the uncore PMUs of MSR and MMIO type, CPUs can be seen as the users, while for the uncore PMUs of PCI, I suppose the dies (actually any cpu on the die) can be seen the users.
If we delete this refcnt reference, then the box structure is under no protection and the box structure could be freed by some accidents.
Quoted 84 lines
Quoted 83 lines
Could we keep this and just decrease the refcnt in
uncore_pci_pmu_unregister()? Thanks.Quoted 78 lines
arch/x86/events/intel/uncore.c +5
-5box->dieid = die; box->pci_dev = pdev; box->pmu = pmu;@@ -1518,7 +1517,7 @@ static void uncore_change_context(struct intel_uncore_type **uncores,uncore_change_type_ctx (*uncores, old_cpu, new_cpu); } -static void uncore_box_unref (struct intel_uncore_type **types,int id) +static void uncore_box_unref (struct intel_uncore_type **types,int die) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;@@ -1529,7 +1528,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id) type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 )uncore_box_exit (box); }@@ -1604,14 +1603,14 @@ static int allocate_boxes(struct intel_uncore_type **types, }static int uncore_box_ref (struct intel_uncore_type **types, -int id,unsigned int cpu) +int die,unsigned int cpu) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box;int i, ret; - ret =allocate_boxes (types, id, cpu); + ret =allocate_boxes (types, die, cpu);if (ret)return ret;@@ -1619,7 +1618,7 @@ static int uncore_box_ref(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 )uncore_box_init (box); }arch/x86/events/intel/uncore.h +3
-3@@ -185,7 +185,7 @@ struct intel_uncore_box {#define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70#define CFL_UNC_CBO_7_PER_CTR0 0xf76 -#define UNCORE_BOX_FLAG_INITIATED 0 +#define UNCORE_BOX_FLAG_INITIALIZED 0/* event config registers are 8-byte apart */ #define UNCORE_BOX_FLAG_CTL_OFFS8 1/* CFL 8th CBOX has different MSR space */ @@ -559,7 +559,7 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,static inline void uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->init_box) box->pmu->type->ops->init_box (box); }@@ -567,7 +567,7 @@ static inline void uncore_box_init(struct intel_uncore_box *box)static inline void uncore_box_exit (struct intel_uncore_box *box) { -if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->exit_box) box->pmu->type->ops->exit_box (box); }
Re: [PATCH V2 2/8] perf/x86/intel/uncore: Fix refcnt and other cleanups
Quoted 46 lines · Dapeng
Quoted 44 lines · Zide
Quoted 31 lines · Dapeng
Quoted 30 lines · Zide Chen
Fix typo
UNCORE_BOX_FLAG_INITIATEDtoUNCORE_BOX_FLAG_INITIALIZED.Rename the 'id' parameter in uncore_box_{ref,unref}() to 'die' to reflect its actual meaning and be consistent with other functions.
Remove the incorrect
atomic_inc(&box->refcnt)fromuncore_pci_pmu_register(): PCI boxes are not tracked by refcnt, and this call incorrectly increments it on a per-die basis.Signed-off-by Zide Chen <[email protected]>---
v2:
- Don't renamepmu->activeboxesand keep its semantics because inuncore_pci_remove()path,uncore_pci_pmu_unregister()won't be called for non-active boxes.
- Sincepmu->activeboxeskeeps it's name, don't need to renamebox->refcnttobox->cpu_refcnt.--- arch/x86/events/intel/uncore.c | 11 +++++------ arch/x86/events/intel/uncore.h | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-)arch/x86/events/intel/uncore.c +0
-0@@ -1170,7 +1170,6 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,if (!box)return -ENOMEM ;Quoted 6 lines
arch/x86/events/intel/uncore.c static int uncore_pci_pmu_register(struct pci_dev *pdev, · line 1173
Quoted 1 line
arch/x86/events/intel/uncore.c +0
-1-atomic_inc (&box->refcnt);I'm not sure if we should remove this. The uncore_box_ref()/uncore_box_unref() are only called for MSR or MMIO type uncore PMUs. For the uncore PMUs of PCI type, the
box->refcntis only increased here. All the 3 kinds of uncore PMUs should keep consistent behavior on the refcnt.
box->refcnttracks how many CPUs are active within this die. Here it is incorrectly incremented on a per-die basis. Additionally, during the PCI uncore enumeration or teardown process, there is no per-CPU context, sobox->refcntis useless in PCI uncore.
Quoted 8 lines
Quoted 1 line
PCI uncore only needs
pmu->activeboxes.Per my understanding, the main aim of refcnt is to prevent the box structure is freed unexpectedly, it doesn't directly couple with CPUs. It just records how many users are using the the box regardless the users are CPU or something else. For the uncore PMUs of MSR and MMIO type, CPUs can be seen as the users, while for the uncore PMUs of PCI, I suppose the dies (actually any cpu on the die) can be seen the users.
No, box->refcnt is a refcnt for in-die CPUs, while pmu->activeboxes is a per-die count. They are clear and distinct. That's also why I tried to rename them to box->cpu_refcnt and pmu->die_refcnt in v1.
If this line is not deleted, box->refcnt for PCI boxes is always <= 1, which is confusing and useless.
Quoted 2 lines
If we delete this refcnt reference, then the box structure is under no protection and the box structure could be freed by some accidents.
box->refcnt is not checked on any PCI boxes; Because there is one pmu->box[die] per die, it utilizes box->refcnt for MSR/MMIO boxes only:
- box_ref(): first in-die CPU online — allocate and initialize the box.
- box_unref(): last in-die CPU offline — tear down the box.
Uncore PCI PMU enumeration and hot plug/unplug do not involve any CPU online/offline events, which is why box->refcnt is not used.
Quoted 85 lines
Quoted 85 lines
Quoted 84 lines
Could we keep this and just decrease the refcnt in
uncore_pci_pmu_unregister()? Thanks.Quoted 78 lines
arch/x86/events/intel/uncore.c +5
-5box->dieid = die; box->pci_dev = pdev; box->pmu = pmu;@@ -1518,7 +1517,7 @@ static void uncore_change_context(struct intel_uncore_type **uncores,uncore_change_type_ctx (*uncores, old_cpu, new_cpu); } -static void uncore_box_unref (struct intel_uncore_type **types,int id) +static void uncore_box_unref (struct intel_uncore_type **types,int die) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;@@ -1529,7 +1528,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id) type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 )uncore_box_exit (box); }@@ -1604,14 +1603,14 @@ static int allocate_boxes(struct intel_uncore_type **types, }static int uncore_box_ref (struct intel_uncore_type **types, -int id,unsigned int cpu) +int die,unsigned int cpu) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box;int i, ret; - ret =allocate_boxes (types, id, cpu); + ret =allocate_boxes (types, die, cpu);if (ret)return ret;@@ -1619,7 +1618,7 @@ static int uncore_box_ref(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 )uncore_box_init (box); }arch/x86/events/intel/uncore.h +3
-3@@ -185,7 +185,7 @@ struct intel_uncore_box {#define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70#define CFL_UNC_CBO_7_PER_CTR0 0xf76 -#define UNCORE_BOX_FLAG_INITIATED 0 +#define UNCORE_BOX_FLAG_INITIALIZED 0/* event config registers are 8-byte apart */ #define UNCORE_BOX_FLAG_CTL_OFFS8 1/* CFL 8th CBOX has different MSR space */ @@ -559,7 +559,7 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,static inline void uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->init_box) box->pmu->type->ops->init_box (box); }@@ -567,7 +567,7 @@ static inline void uncore_box_init(struct intel_uncore_box *box)static inline void uncore_box_exit (struct intel_uncore_box *box) { -if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->exit_box) box->pmu->type->ops->exit_box (box); }
Re: [PATCH V2 2/8] perf/x86/intel/uncore: Fix refcnt and other cleanups
Quoted 62 lines · Zide
Quoted 44 lines · Dapeng
Quoted 43 lines · Zide
Quoted 31 lines · Dapeng
Quoted 30 lines · Zide Chen
Fix typo
UNCORE_BOX_FLAG_INITIATEDtoUNCORE_BOX_FLAG_INITIALIZED.Rename the 'id' parameter in uncore_box_{ref,unref}() to 'die' to reflect its actual meaning and be consistent with other functions.
Remove the incorrect
atomic_inc(&box->refcnt)fromuncore_pci_pmu_register(): PCI boxes are not tracked by refcnt, and this call incorrectly increments it on a per-die basis.Signed-off-by Zide Chen <[email protected]>---
v2:
- Don't renamepmu->activeboxesand keep its semantics because inuncore_pci_remove()path,uncore_pci_pmu_unregister()won't be called for non-active boxes.
- Sincepmu->activeboxeskeeps it's name, don't need to renamebox->refcnttobox->cpu_refcnt.--- arch/x86/events/intel/uncore.c | 11 +++++------ arch/x86/events/intel/uncore.h | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-)arch/x86/events/intel/uncore.c +0
-0@@ -1170,7 +1170,6 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,if (!box)return -ENOMEM ;Quoted 6 lines
arch/x86/events/intel/uncore.c static int uncore_pci_pmu_register(struct pci_dev *pdev, · line 1173
Quoted 1 line
arch/x86/events/intel/uncore.c +0
-1-atomic_inc (&box->refcnt);I'm not sure if we should remove this. The uncore_box_ref()/uncore_box_unref() are only called for MSR or MMIO type uncore PMUs. For the uncore PMUs of PCI type, the
box->refcntis only increased here. All the 3 kinds of uncore PMUs should keep consistent behavior on the refcnt.
box->refcnttracks how many CPUs are active within this die. Here it is incorrectly incremented on a per-die basis. Additionally, during the PCI uncore enumeration or teardown process, there is no per-CPU context, sobox->refcntis useless in PCI uncore.Quoted 7 lines
Quoted 1 line
PCI uncore only needs
pmu->activeboxes.Per my understanding, the main aim of refcnt is to prevent the box structure is freed unexpectedly, it doesn't directly couple with CPUs. It just records how many users are using the the box regardless the users are CPU or something else. For the uncore PMUs of MSR and MMIO type, CPUs can be seen as the users, while for the uncore PMUs of PCI, I suppose the dies (actually any cpu on the die) can be seen the users.
No,
box->refcntis a refcnt for in-die CPUs, whilepmu->activeboxesis a per-die count. They are clear and distinct. That's also why I tried to rename them tobox->cpu_refcntandpmu->die_refcntin v1.If this line is not deleted,
box->refcntfor PCI boxes is always <= 1, which is confusing and useless.Quoted 2 lines
If we delete this refcnt reference, then the box structure is under no protection and the box structure could be freed by some accidents.
Quoted 8 lines
box->refcntis not checked on any PCI boxes; Because there is one pmu->box[die] per die, it utilizesbox->refcntfor MSR/MMIO boxes only:-
box_ref(): first in-die CPU online — allocate and initialize the box.
-box_unref(): last in-die CPU offline — tear down the box.Uncore PCI PMU enumeration and hot plug/unplug do not involve any CPU online/offline events, which is why
box->refcntis not used.
No, the refcnt is still useful even for pci type's uncore PMUs, especially from the software's perspective. refcnt is the essential way to prevent the unexpected free of the referenced structure. Although currently there would be only one user for pci type's uncore PMUs and refcnt would be 1 in most time, we can't ensure the code would always right in any time and no code accidentally free the box. We'd better keep this refcnt mechanism to avoid this situation. Thanks.
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Could we keep this and just decrease the refcnt in
uncore_pci_pmu_unregister()? Thanks.Quoted 78 lines
arch/x86/events/intel/uncore.c +5
-5box->dieid = die; box->pci_dev = pdev; box->pmu = pmu;@@ -1518,7 +1517,7 @@ static void uncore_change_context(struct intel_uncore_type **uncores,uncore_change_type_ctx (*uncores, old_cpu, new_cpu); } -static void uncore_box_unref (struct intel_uncore_type **types,int id) +static void uncore_box_unref (struct intel_uncore_type **types,int die) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;@@ -1529,7 +1528,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id) type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 )uncore_box_exit (box); }@@ -1604,14 +1603,14 @@ static int allocate_boxes(struct intel_uncore_type **types, }static int uncore_box_ref (struct intel_uncore_type **types, -int id,unsigned int cpu) +int die,unsigned int cpu) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box;int i, ret; - ret =allocate_boxes (types, id, cpu); + ret =allocate_boxes (types, die, cpu);if (ret)return ret;@@ -1619,7 +1618,7 @@ static int uncore_box_ref(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 )uncore_box_init (box); }arch/x86/events/intel/uncore.h +3
-3@@ -185,7 +185,7 @@ struct intel_uncore_box {#define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70#define CFL_UNC_CBO_7_PER_CTR0 0xf76 -#define UNCORE_BOX_FLAG_INITIATED 0 +#define UNCORE_BOX_FLAG_INITIALIZED 0/* event config registers are 8-byte apart */ #define UNCORE_BOX_FLAG_CTL_OFFS8 1/* CFL 8th CBOX has different MSR space */ @@ -559,7 +559,7 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,static inline void uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->init_box) box->pmu->type->ops->init_box (box); }@@ -567,7 +567,7 @@ static inline void uncore_box_init(struct intel_uncore_box *box)static inline void uncore_box_exit (struct intel_uncore_box *box) { -if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->exit_box) box->pmu->type->ops->exit_box (box); }
Re: [PATCH V2 2/8] perf/x86/intel/uncore: Fix refcnt and other cleanups
Quoted 73 lines · Dapeng
Quoted 62 lines · Zide
Quoted 44 lines · Dapeng
Quoted 43 lines · Zide
Quoted 31 lines · Dapeng
Quoted 30 lines · Zide Chen
Fix typo
UNCORE_BOX_FLAG_INITIATEDtoUNCORE_BOX_FLAG_INITIALIZED.Rename the 'id' parameter in uncore_box_{ref,unref}() to 'die' to reflect its actual meaning and be consistent with other functions.
Remove the incorrect
atomic_inc(&box->refcnt)fromuncore_pci_pmu_register(): PCI boxes are not tracked by refcnt, and this call incorrectly increments it on a per-die basis.Signed-off-by Zide Chen <[email protected]>---
v2:
- Don't renamepmu->activeboxesand keep its semantics because inuncore_pci_remove()path,uncore_pci_pmu_unregister()won't be called for non-active boxes.
- Sincepmu->activeboxeskeeps it's name, don't need to renamebox->refcnttobox->cpu_refcnt.--- arch/x86/events/intel/uncore.c | 11 +++++------ arch/x86/events/intel/uncore.h | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-)arch/x86/events/intel/uncore.c +0
-0@@ -1170,7 +1170,6 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,if (!box)return -ENOMEM ;Quoted 6 lines
arch/x86/events/intel/uncore.c static int uncore_pci_pmu_register(struct pci_dev *pdev, · line 1173
Quoted 1 line
arch/x86/events/intel/uncore.c +0
-1-atomic_inc (&box->refcnt);I'm not sure if we should remove this. The uncore_box_ref()/uncore_box_unref() are only called for MSR or MMIO type uncore PMUs. For the uncore PMUs of PCI type, the
box->refcntis only increased here. All the 3 kinds of uncore PMUs should keep consistent behavior on the refcnt.
box->refcnttracks how many CPUs are active within this die. Here it is incorrectly incremented on a per-die basis. Additionally, during the PCI uncore enumeration or teardown process, there is no per-CPU context, sobox->refcntis useless in PCI uncore.Quoted 7 lines
Quoted 1 line
PCI uncore only needs
pmu->activeboxes.Per my understanding, the main aim of refcnt is to prevent the box structure is freed unexpectedly, it doesn't directly couple with CPUs. It just records how many users are using the the box regardless the users are CPU or something else. For the uncore PMUs of MSR and MMIO type, CPUs can be seen as the users, while for the uncore PMUs of PCI, I suppose the dies (actually any cpu on the die) can be seen the users.
No,
box->refcntis a refcnt for in-die CPUs, whilepmu->activeboxesis a per-die count. They are clear and distinct. That's also why I tried to rename them tobox->cpu_refcntandpmu->die_refcntin v1.If this line is not deleted,
box->refcntfor PCI boxes is always <= 1, which is confusing and useless.Quoted 2 lines
If we delete this refcnt reference, then the box structure is under no protection and the box structure could be freed by some accidents.
Quoted 8 lines
box->refcntis not checked on any PCI boxes; Because there is one pmu->box[die] per die, it utilizesbox->refcntfor MSR/MMIO boxes only:-
box_ref(): first in-die CPU online — allocate and initialize the box.
-box_unref(): last in-die CPU offline — tear down the box.Uncore PCI PMU enumeration and hot plug/unplug do not involve any CPU online/offline events, which is why
box->refcntis not used.
Quoted 7 lines
No, the refcnt is still useful even for pci type's uncore PMUs, especially from the software's perspective. refcnt is the essential way to prevent the unexpected free of the referenced structure. Although currently there would be only one user for pci type's uncore PMUs and refcnt would be 1 in most time, we can't ensure the code would always right in any time and no code accidentally free the box. We'd better keep this refcnt mechanism to avoid this situation. Thanks.
pmu->activeboxes is the refcnt used by PCI PMUs, whose online/offline events operate on a per-die basis.
box->refcnt is the refcnt used by MSR/MMIO PMUs, where online/offline events are per-CPU based.
In this particular case (uncore_pci_pmu_register(), prior to the upcoming refactor), incrementing box->refcnt also increments pmu->activeboxes. This makes box->refcnt completely redundant for PCI PMUs and effectively a misuse of the per-cpu field for a per-die operation.
If I’m missing any scenarios where PCI PMU boxes still require additional protection from box->refcnt, please help point them out.
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Could we keep this and just decrease the refcnt in
uncore_pci_pmu_unregister()? Thanks.Quoted 78 lines
arch/x86/events/intel/uncore.c +5
-5box->dieid = die; box->pci_dev = pdev; box->pmu = pmu;@@ -1518,7 +1517,7 @@ static void uncore_change_context(struct intel_uncore_type **uncores,uncore_change_type_ctx (*uncores, old_cpu, new_cpu); } -static void uncore_box_unref (struct intel_uncore_type **types,int id) +static void uncore_box_unref (struct intel_uncore_type **types,int die) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;@@ -1529,7 +1528,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id) type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 )uncore_box_exit (box); }@@ -1604,14 +1603,14 @@ static int allocate_boxes(struct intel_uncore_type **types, }static int uncore_box_ref (struct intel_uncore_type **types, -int id,unsigned int cpu) +int die,unsigned int cpu) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box;int i, ret; - ret =allocate_boxes (types, id, cpu); + ret =allocate_boxes (types, die, cpu);if (ret)return ret;@@ -1619,7 +1618,7 @@ static int uncore_box_ref(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 )uncore_box_init (box); }arch/x86/events/intel/uncore.h +3
-3@@ -185,7 +185,7 @@ struct intel_uncore_box {#define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70#define CFL_UNC_CBO_7_PER_CTR0 0xf76 -#define UNCORE_BOX_FLAG_INITIATED 0 +#define UNCORE_BOX_FLAG_INITIALIZED 0/* event config registers are 8-byte apart */ #define UNCORE_BOX_FLAG_CTL_OFFS8 1/* CFL 8th CBOX has different MSR space */ @@ -559,7 +559,7 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,static inline void uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->init_box) box->pmu->type->ops->init_box (box); }@@ -567,7 +567,7 @@ static inline void uncore_box_init(struct intel_uncore_box *box)static inline void uncore_box_exit (struct intel_uncore_box *box) { -if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->exit_box) box->pmu->type->ops->exit_box (box); }
Re: [PATCH V2 2/8] perf/x86/intel/uncore: Fix refcnt and other cleanups
Quoted 85 lines · Zide
Quoted 70 lines · Dapeng
Quoted 61 lines · Zide
Quoted 44 lines · Dapeng
Quoted 43 lines · Zide
Quoted 31 lines · Dapeng
Quoted 30 lines · Zide Chen
Fix typo
UNCORE_BOX_FLAG_INITIATEDtoUNCORE_BOX_FLAG_INITIALIZED.Rename the 'id' parameter in uncore_box_{ref,unref}() to 'die' to reflect its actual meaning and be consistent with other functions.
Remove the incorrect
atomic_inc(&box->refcnt)fromuncore_pci_pmu_register(): PCI boxes are not tracked by refcnt, and this call incorrectly increments it on a per-die basis.Signed-off-by Zide Chen <[email protected]>---
v2:
- Don't renamepmu->activeboxesand keep its semantics because inuncore_pci_remove()path,uncore_pci_pmu_unregister()won't be called for non-active boxes.
- Sincepmu->activeboxeskeeps it's name, don't need to renamebox->refcnttobox->cpu_refcnt.--- arch/x86/events/intel/uncore.c | 11 +++++------ arch/x86/events/intel/uncore.h | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-)arch/x86/events/intel/uncore.c +0
-0@@ -1170,7 +1170,6 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,if (!box)return -ENOMEM ;Quoted 6 lines
arch/x86/events/intel/uncore.c static int uncore_pci_pmu_register(struct pci_dev *pdev, · line 1173
Quoted 1 line
arch/x86/events/intel/uncore.c +0
-1-atomic_inc (&box->refcnt);I'm not sure if we should remove this. The uncore_box_ref()/uncore_box_unref() are only called for MSR or MMIO type uncore PMUs. For the uncore PMUs of PCI type, the
box->refcntis only increased here. All the 3 kinds of uncore PMUs should keep consistent behavior on the refcnt.
box->refcnttracks how many CPUs are active within this die. Here it is incorrectly incremented on a per-die basis. Additionally, during the PCI uncore enumeration or teardown process, there is no per-CPU context, sobox->refcntis useless in PCI uncore.Quoted 7 lines
Quoted 1 line
PCI uncore only needs
pmu->activeboxes.Per my understanding, the main aim of refcnt is to prevent the box structure is freed unexpectedly, it doesn't directly couple with CPUs. It just records how many users are using the the box regardless the users are CPU or something else. For the uncore PMUs of MSR and MMIO type, CPUs can be seen as the users, while for the uncore PMUs of PCI, I suppose the dies (actually any cpu on the die) can be seen the users.
No,
box->refcntis a refcnt for in-die CPUs, whilepmu->activeboxesis a per-die count. They are clear and distinct. That's also why I tried to rename them tobox->cpu_refcntandpmu->die_refcntin v1.If this line is not deleted,
box->refcntfor PCI boxes is always <= 1, which is confusing and useless.Quoted 2 lines
If we delete this refcnt reference, then the box structure is under no protection and the box structure could be freed by some accidents.
Quoted 8 lines
box->refcntis not checked on any PCI boxes; Because there is one pmu->box[die] per die, it utilizesbox->refcntfor MSR/MMIO boxes only:-
box_ref(): first in-die CPU online — allocate and initialize the box.
-box_unref(): last in-die CPU offline — tear down the box.Uncore PCI PMU enumeration and hot plug/unplug do not involve any CPU online/offline events, which is why
box->refcntis not used.Quoted 7 lines
No, the refcnt is still useful even for pci type's uncore PMUs, especially from the software's perspective. refcnt is the essential way to prevent the unexpected free of the referenced structure. Although currently there would be only one user for pci type's uncore PMUs and refcnt would be 1 in most time, we can't ensure the code would always right in any time and no code accidentally free the box. We'd better keep this refcnt mechanism to avoid this situation. Thanks.
pmu->activeboxesis the refcnt used by PCI PMUs, whose online/offline events operate on a per-die basis.
box->refcntis the refcnt used by MSR/MMIO PMUs, where online/offline events are per-CPU based.
Quoted 7 lines
In this particular case (
uncore_pci_pmu_register(), prior to the upcoming refactor), incrementingbox->refcntalso incrementspmu->activeboxes. This makesbox->refcntcompletely redundant for PCI PMUs and effectively a misuse of the per-cpu field for a per-die operation.If I’m missing any scenarios where PCI PMU boxes still require additional protection from
box->refcnt, please help point them out.
IMO, activeboxes looks more like the reference count of PMU structure instead of the box structure. Box->refcnt is actually the reference count of box structure. I know the refcnt could not matter too much for PCI uncore PMUs since it has only one user right now, but we'd better keep the identical refcnt behavior on all the MSR/MMIO/PCI uncore PMUs, this makes the code clean and robust.
Thanks.
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Quoted 83 lines
Could we keep this and just decrease the refcnt in
uncore_pci_pmu_unregister()? Thanks.Quoted 78 lines
arch/x86/events/intel/uncore.c +5
-5box->dieid = die; box->pci_dev = pdev; box->pmu = pmu;@@ -1518,7 +1517,7 @@ static void uncore_change_context(struct intel_uncore_type **uncores,uncore_change_type_ctx (*uncores, old_cpu, new_cpu); } -static void uncore_box_unref (struct intel_uncore_type **types,int id) +static void uncore_box_unref (struct intel_uncore_type **types,int die) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;@@ -1529,7 +1528,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id) type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 )uncore_box_exit (box); }@@ -1604,14 +1603,14 @@ static int allocate_boxes(struct intel_uncore_type **types, }static int uncore_box_ref (struct intel_uncore_type **types, -int id,unsigned int cpu) +int die,unsigned int cpu) {struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box;int i, ret; - ret =allocate_boxes (types, id, cpu); + ret =allocate_boxes (types, die, cpu);if (ret)return ret;@@ -1619,7 +1618,7 @@ static int uncore_box_ref(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { - box = pmu->boxes[id]; + box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 )uncore_box_init (box); }arch/x86/events/intel/uncore.h +3
-3@@ -185,7 +185,7 @@ struct intel_uncore_box {#define CFL_UNC_CBO_7_PERFEVTSEL0 0xf70#define CFL_UNC_CBO_7_PER_CTR0 0xf76 -#define UNCORE_BOX_FLAG_INITIATED 0 +#define UNCORE_BOX_FLAG_INITIALIZED 0/* event config registers are 8-byte apart */ #define UNCORE_BOX_FLAG_CTL_OFFS8 1/* CFL 8th CBOX has different MSR space */ @@ -559,7 +559,7 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,static inline void uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->init_box) box->pmu->type->ops->init_box (box); }@@ -567,7 +567,7 @@ static inline void uncore_box_init(struct intel_uncore_box *box)static inline void uncore_box_exit (struct intel_uncore_box *box) { -if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIATED , &box->flags)) { +if (test_and_clear_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) {if (box->pmu->type->ops->exit_box) box->pmu->type->ops->exit_box (box); }
PATCH v2 3/8 perf/x86/intel/uncore: Let init_box() callback report failures
In reply to Zide ChenThe init_box() callback currently returns void, so initialization failures are silently ignored and the box is still marked initialized.
Change the callback to return int so platform code can report errors back to the common uncore layer.
Update uncore_box_init() to set the initialized flag only when init_box() succeeds. Because box->refcnt guarantees that at most one CPU calls uncore_box_init() for a given box at a time, plain __set_bit() is safe for the initialized flag without atomic overhead.
Convert all init_box() implementations to return 0 on success or a negative error code on failure. This is a prerequisite for propagating initialization errors to the caller so they can be handled properly.
--- arch/x86/events/intel/uncore.h | 16 +++-- arch/x86/events/intel/uncore_discovery.c | 21 ++++--- arch/x86/events/intel/uncore_discovery.h | 6 +- arch/x86/events/intel/uncore_nhmex.c | 3 +- arch/x86/events/intel/uncore_snb.c | 80 +++++++++++++++--------- arch/x86/events/intel/uncore_snbep.c | 77 ++++++++++++++--------- 6 files changed, 126 insertions(+), 77 deletions(-)
arch/x86/events/intel/uncore.h +11 -5
@@ -129,7 +129,7 @@ struct intel_uncore_type {#define events_group attr_groups[2]struct intel_uncore_ops { -void (*init_box)(struct intel_uncore_box *); +int (*init_box)(struct intel_uncore_box *);void (*exit_box)(struct intel_uncore_box *);void (*disable_box)(struct intel_uncore_box *);void (*enable_box)(struct intel_uncore_box *);
@@ -557,12 +557,18 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,return box->pmu->type->ops->read_counter (box, event); } -static inline void uncore_box_init (struct intel_uncore_box *box) +static inline int uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) { -if (box->pmu->type->ops->init_box) - box->pmu->type->ops->init_box (box); +int ret =0 ; + +if (!test_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags) && + box->pmu->type->ops->init_box) { + ret = box->pmu->type->ops->init_box (box); +if (!ret) +__set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags); } + +return ret; }static inline void uncore_box_exit (struct intel_uncore_box *box)
arch/x86/events/intel/uncore_discovery.c +12 -9
@@ -489,14 +489,15 @@ static u64 intel_generic_uncore_box_ctl(struct intel_uncore_box *box)return unit->addr; } -void intel_generic_uncore_msr_init_box (struct intel_uncore_box *box) +int intel_generic_uncore_msr_init_box (struct intel_uncore_box *box) {u64 box_ctl =intel_generic_uncore_box_ctl (box);if (!box_ctl) -return ; +return -ENODEV ;wrmsrq (box_ctl,GENERIC_PMON_BOX_CTL_INT ); +return 0 ; }void intel_generic_uncore_msr_disable_box (struct intel_uncore_box *box)
@@ -578,15 +579,16 @@ static inline int intel_pci_uncore_box_ctl(struct intel_uncore_box *box)return UNCORE_DISCOVERY_PCI_BOX_CTRL (intel_generic_uncore_box_ctl (box)); } -void intel_generic_uncore_pci_init_box (struct intel_uncore_box *box) +int intel_generic_uncore_pci_init_box (struct intel_uncore_box *box) {int box_ctl =intel_pci_uncore_box_ctl (box);if (!box_ctl) -return ; +return -ENODEV ;__set_bit (UNCORE_BOX_FLAG_CTL_OFFS8 , &box->flags); -pci_write_config_dword (box->pci_dev, box_ctl,GENERIC_PMON_BOX_CTL_INT ); +return pci_write_config_dword (box->pci_dev, box_ctl, +GENERIC_PMON_BOX_CTL_INT ); }void intel_generic_uncore_pci_disable_box (struct intel_uncore_box *box)
@@ -648,7 +650,7 @@ static struct intel_uncore_ops generic_uncore_pci_ops = {#define UNCORE_GENERIC_MMIO_SIZE 0x4000 -void intel_generic_uncore_mmio_init_box (struct intel_uncore_box *box) +int intel_generic_uncore_mmio_init_box (struct intel_uncore_box *box) {static struct intel_uncore_discovery_unit *unit;struct intel_uncore_type *type = box->pmu->type;
@@ -658,13 +660,13 @@ void intel_generic_uncore_mmio_init_box(struct intel_uncore_box *box)if (!unit) {pr_warn ("Uncore type %d id %d: Cannot find box control address.\n" , type->type_id, box->pmu->pmu_idx); -return ; +return -ENODEV ; }if (!unit->addr) {pr_warn ("Uncore type %d box %d: Invalid box control address.\n" , type->type_id, unit->id); -return ; +return -ENODEV ; } addr = unit->addr;
@@ -672,10 +674,11 @@ void intel_generic_uncore_mmio_init_box(struct intel_uncore_box *box)if (!box->io_addr) {pr_warn ("Uncore type %d box %d: ioremap error for 0x%llx.\n" , type->type_id, unit->id, (unsigned long long )addr); -return ; +return -ENOMEM ; }writel (GENERIC_PMON_BOX_CTL_INT , box->io_addr); +return 0 ; }void intel_generic_uncore_mmio_disable_box (struct intel_uncore_box *box)
arch/x86/events/intel/uncore_discovery.h +3 -3
@@ -148,11 +148,11 @@ void intel_uncore_generic_uncore_cpu_init(void);int intel_uncore_generic_uncore_pci_init (void );void intel_uncore_generic_uncore_mmio_init (void ); -void intel_generic_uncore_msr_init_box (struct intel_uncore_box *box); +int intel_generic_uncore_msr_init_box (struct intel_uncore_box *box);void intel_generic_uncore_msr_disable_box (struct intel_uncore_box *box);void intel_generic_uncore_msr_enable_box (struct intel_uncore_box *box); -void intel_generic_uncore_mmio_init_box (struct intel_uncore_box *box); +int intel_generic_uncore_mmio_init_box (struct intel_uncore_box *box);void intel_generic_uncore_mmio_disable_box (struct intel_uncore_box *box);void intel_generic_uncore_mmio_enable_box (struct intel_uncore_box *box); void intel_generic_uncore_mmio_disable_event(struct intel_uncore_box *box,
@@ -160,7 +160,7 @@ void intel_generic_uncore_mmio_disable_event(struct intel_uncore_box *box,void intel_generic_uncore_mmio_enable_event (struct intel_uncore_box *box,struct perf_event *event); -void intel_generic_uncore_pci_init_box (struct intel_uncore_box *box); +int intel_generic_uncore_pci_init_box (struct intel_uncore_box *box);void intel_generic_uncore_pci_disable_box (struct intel_uncore_box *box);void intel_generic_uncore_pci_enable_box (struct intel_uncore_box *box); void intel_generic_uncore_pci_disable_event(struct intel_uncore_box *box,
arch/x86/events/intel/uncore_nhmex.c +2 -1
@@ -199,9 +199,10 @@ DEFINE_UNCORE_FORMAT_ATTR(counter, counter, "config:6-7");DEFINE_UNCORE_FORMAT_ATTR (match, match,"config1:0-63" );DEFINE_UNCORE_FORMAT_ATTR (mask, mask,"config2:0-63" ); -static void nhmex_uncore_msr_init_box (struct intel_uncore_box *box) +static int nhmex_uncore_msr_init_box (struct intel_uncore_box *box) {wrmsrq (NHMEX_U_MSR_PMON_GLOBAL_CTL ,NHMEX_U_PMON_GLOBAL_EN_ALL ); +return 0 ; }static void nhmex_uncore_msr_exit_box (struct intel_uncore_box *box)
arch/x86/events/intel/uncore_snb.c +51 -29
229 lines not on this page · raw message
arch/x86/events/intel/uncore_snbep.c +47 -30
224 lines not on this page · raw message
Signature · 1 line
2.54.0
Re: [PATCH V2 3/8] perf/x86/intel/uncore: Let init_box() callback report failures
Quoted 676 lines · Zide Chen
The
init_box()callback currently returns void, so initialization failures are silently ignored and the box is still marked initialized.Change the callback to return int so platform code can report errors back to the common uncore layer.
Update
uncore_box_init()to set the initialized flag only wheninit_box()succeeds. Becausebox->refcntguarantees that at most one CPU callsuncore_box_init()for a given box at a time, plain__set_bit()is safe for the initialized flag without atomic overhead.Convert all
init_box()implementations to return 0 on success or a negative error code on failure. This is a prerequisite for propagating initialization errors to the caller so they can be handled properly.Reviewed-by Ian Rogers <[email protected]>Signed-off-by Zide Chen <[email protected]>--- arch/x86/events/intel/uncore.h | 16 +++-- arch/x86/events/intel/uncore_discovery.c | 21 ++++--- arch/x86/events/intel/uncore_discovery.h | 6 +- arch/x86/events/intel/uncore_nhmex.c | 3 +- arch/x86/events/intel/uncore_snb.c | 80 +++++++++++++++--------- arch/x86/events/intel/uncore_snbep.c | 77 ++++++++++++++--------- 6 files changed, 126 insertions(+), 77 deletions(-)arch/x86/events/intel/uncore.h +11
-5@@ -129,7 +129,7 @@ struct intel_uncore_type {#define events_group attr_groups[2]struct intel_uncore_ops { -void (*init_box)(struct intel_uncore_box *); +int (*init_box)(struct intel_uncore_box *);void (*exit_box)(struct intel_uncore_box *);void (*disable_box)(struct intel_uncore_box *);void (*enable_box)(struct intel_uncore_box *);@@ -557,12 +557,18 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,return box->pmu->type->ops->read_counter (box, event); } -static inline void uncore_box_init (struct intel_uncore_box *box) +static inline int uncore_box_init (struct intel_uncore_box *box) { -if (!test_and_set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)) { -if (box->pmu->type->ops->init_box) - box->pmu->type->ops->init_box (box); +int ret =0 ; + +if (!test_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags) && + box->pmu->type->ops->init_box) { + ret = box->pmu->type->ops->init_box (box); +if (!ret) +__set_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags); } + +return ret; }static inline void uncore_box_exit (struct intel_uncore_box *box)arch/x86/events/intel/uncore_discovery.c +12
-9@@ -489,14 +489,15 @@ static u64 intel_generic_uncore_box_ctl(struct intel_uncore_box *box)return unit->addr; } -void intel_generic_uncore_msr_init_box (struct intel_uncore_box *box) +int intel_generic_uncore_msr_init_box (struct intel_uncore_box *box) {u64 box_ctl =intel_generic_uncore_box_ctl (box);if (!box_ctl) -return ; +return -ENODEV ;wrmsrq (box_ctl,GENERIC_PMON_BOX_CTL_INT ); +return 0 ; }void intel_generic_uncore_msr_disable_box (struct intel_uncore_box *box)@@ -578,15 +579,16 @@ static inline int intel_pci_uncore_box_ctl(struct intel_uncore_box *box)return UNCORE_DISCOVERY_PCI_BOX_CTRL (intel_generic_uncore_box_ctl (box)); } -void intel_generic_uncore_pci_init_box (struct intel_uncore_box *box) +int intel_generic_uncore_pci_init_box (struct intel_uncore_box *box) {int box_ctl =intel_pci_uncore_box_ctl (box);if (!box_ctl) -return ; +return -ENODEV ;__set_bit (UNCORE_BOX_FLAG_CTL_OFFS8 , &box->flags); -pci_write_config_dword (box->pci_dev, box_ctl,GENERIC_PMON_BOX_CTL_INT ); +return pci_write_config_dword (box->pci_dev, box_ctl, +GENERIC_PMON_BOX_CTL_INT ); }void intel_generic_uncore_pci_disable_box (struct intel_uncore_box *box)@@ -648,7 +650,7 @@ static struct intel_uncore_ops generic_uncore_pci_ops = {#define UNCORE_GENERIC_MMIO_SIZE 0x4000 -void intel_generic_uncore_mmio_init_box (struct intel_uncore_box *box) +int intel_generic_uncore_mmio_init_box (struct intel_uncore_box *box) {static struct intel_uncore_discovery_unit *unit;struct intel_uncore_type *type = box->pmu->type;@@ -658,13 +660,13 @@ void intel_generic_uncore_mmio_init_box(struct intel_uncore_box *box)if (!unit) {pr_warn ("Uncore type %d id %d: Cannot find box control address.\n" , type->type_id, box->pmu->pmu_idx); -return ; +return -ENODEV ; }if (!unit->addr) {pr_warn ("Uncore type %d box %d: Invalid box control address.\n" , type->type_id, unit->id); -return ; +return -ENODEV ; } addr = unit->addr;@@ -672,10 +674,11 @@ void intel_generic_uncore_mmio_init_box(struct intel_uncore_box *box)if (!box->io_addr) {pr_warn ("Uncore type %d box %d: ioremap error for 0x%llx.\n" , type->type_id, unit->id, (unsigned long long )addr); -return ; +return -ENOMEM ; }writel (GENERIC_PMON_BOX_CTL_INT , box->io_addr); +return 0 ; }void intel_generic_uncore_mmio_disable_box (struct intel_uncore_box *box)arch/x86/events/intel/uncore_discovery.h +3
-3@@ -148,11 +148,11 @@ void intel_uncore_generic_uncore_cpu_init(void);int intel_uncore_generic_uncore_pci_init (void );void intel_uncore_generic_uncore_mmio_init (void ); -void intel_generic_uncore_msr_init_box (struct intel_uncore_box *box); +int intel_generic_uncore_msr_init_box (struct intel_uncore_box *box);void intel_generic_uncore_msr_disable_box (struct intel_uncore_box *box);void intel_generic_uncore_msr_enable_box (struct intel_uncore_box *box); -void intel_generic_uncore_mmio_init_box (struct intel_uncore_box *box); +int intel_generic_uncore_mmio_init_box (struct intel_uncore_box *box);void intel_generic_uncore_mmio_disable_box (struct intel_uncore_box *box);void intel_generic_uncore_mmio_enable_box (struct intel_uncore_box *box); void intel_generic_uncore_mmio_disable_event(struct intel_uncore_box *box,@@ -160,7 +160,7 @@ void intel_generic_uncore_mmio_disable_event(struct intel_uncore_box *box,void intel_generic_uncore_mmio_enable_event (struct intel_uncore_box *box,struct perf_event *event); -void intel_generic_uncore_pci_init_box (struct intel_uncore_box *box); +int intel_generic_uncore_pci_init_box (struct intel_uncore_box *box);void intel_generic_uncore_pci_disable_box (struct intel_uncore_box *box);void intel_generic_uncore_pci_enable_box (struct intel_uncore_box *box); void intel_generic_uncore_pci_disable_event(struct intel_uncore_box *box,arch/x86/events/intel/uncore_nhmex.c +2
-1@@ -199,9 +199,10 @@ DEFINE_UNCORE_FORMAT_ATTR(counter, counter, "config:6-7");DEFINE_UNCORE_FORMAT_ATTR (match, match,"config1:0-63" );DEFINE_UNCORE_FORMAT_ATTR (mask, mask,"config2:0-63" ); -static void nhmex_uncore_msr_init_box (struct intel_uncore_box *box) +static int nhmex_uncore_msr_init_box (struct intel_uncore_box *box) {wrmsrq (NHMEX_U_MSR_PMON_GLOBAL_CTL ,NHMEX_U_PMON_GLOBAL_EN_ALL ); +return 0 ; }static void nhmex_uncore_msr_exit_box (struct intel_uncore_box *box)arch/x86/events/intel/uncore_snb.c +51
-29@@ -295,12 +295,14 @@ static void snb_uncore_msr_disable_event(struct intel_uncore_box *box, struct pewrmsrq (event->hw.config_base,0 ); } -static void snb_uncore_msr_init_box (struct intel_uncore_box *box) +static int snb_uncore_msr_init_box (struct intel_uncore_box *box) {if (box->pmu->pmu_idx ==0 ) {wrmsrq (SNB_UNC_PERF_GLOBAL_CTL ,SNB_UNC_GLOBAL_CTL_EN |SNB_UNC_GLOBAL_CTL_CORE_ALL ); } + +return 0 ; }static void snb_uncore_msr_enable_box (struct intel_uncore_box *box)@@ -394,7 +396,7 @@ void snb_uncore_cpu_init(void) snb_uncore_cbox.num_boxes =topology_num_cores_per_package (); } -static void skl_uncore_msr_init_box (struct intel_uncore_box *box) +static int skl_uncore_msr_init_box (struct intel_uncore_box *box) {if (box->pmu->pmu_idx ==0 ) { wrmsrq(SKL_UNC_PERF_GLOBAL_CTL,@@ -404,6 +406,8 @@ static void skl_uncore_msr_init_box(struct intel_uncore_box *box)/* The 8th CBOX has different MSR space */ if (box->pmu->pmu_idx ==7 )__set_bit (UNCORE_BOX_FLAG_CFL8_CBOX_MSR_OFFS , &box->flags); + +return 0 ; }static void skl_uncore_msr_enable_box (struct intel_uncore_box *box)@@ -547,10 +551,12 @@ static struct intel_uncore_type *tgl_msr_uncores[] = { NULL, }; -static void rkl_uncore_msr_init_box (struct intel_uncore_box *box) +static int rkl_uncore_msr_init_box (struct intel_uncore_box *box) {if (box->pmu->pmu_idx ==0 )wrmsrq (SKL_UNC_PERF_GLOBAL_CTL ,SNB_UNC_GLOBAL_CTL_EN ); + +return 0 ; }void tgl_uncore_cpu_init (void )@@ -707,9 +713,10 @@ static struct intel_uncore_type mtl_uncore_hac_cbox = { .format_group = &adl_uncore_format_group, }; -static void mtl_uncore_msr_init_box (struct intel_uncore_box *box) +static int mtl_uncore_msr_init_box (struct intel_uncore_box *box) {wrmsrq (uncore_msr_box_ctl (box),SNB_UNC_GLOBAL_CTL_EN ); +return 0 ; }static struct intel_uncore_ops mtl_uncore_msr_ops = {@@ -773,10 +780,12 @@ static struct intel_uncore_type *lnl_msr_uncores[] = {#define LNL_UNC_MSR_GLOBAL_CTL 0x240e -static void lnl_uncore_msr_init_box (struct intel_uncore_box *box) +static int lnl_uncore_msr_init_box (struct intel_uncore_box *box) {if (box->pmu->pmu_idx ==0 )wrmsrq (LNL_UNC_MSR_GLOBAL_CTL ,SNB_UNC_GLOBAL_CTL_EN ); + +return 0 ; }static struct intel_uncore_ops lnl_uncore_msr_ops = {@@ -874,7 +883,7 @@ static const struct attribute_group snb_uncore_imc_format_group = { .attrs = snb_uncore_imc_formats_attr, }; -static void snb_uncore_imc_init_box (struct intel_uncore_box *box) +static int snb_uncore_imc_init_box (struct intel_uncore_box *box) {struct intel_uncore_type *type = box->pmu->type;struct pci_dev *pdev = box->pci_dev;@@ -893,10 +902,13 @@ static void snb_uncore_imc_init_box(struct intel_uncore_box *box) addr &= ~(PAGE_SIZE -1 ); box->io_addr =ioremap (addr, type->mmio_map_size); -if (!box->io_addr) +if (!box->io_addr) {pr_warn ("perf uncore: Failed to ioremap for %s.\n" , type->name); +return -ENOMEM ; + } box->hrtimer_duration =UNCORE_SNB_IMC_HRTIMER_INTERVAL ; +return 0 ; }static void snb_uncore_imc_enable_box (struct intel_uncore_box *box)@@ -1532,7 +1544,7 @@ static struct pci_dev *tgl_uncore_get_mc_dev(void)#define TGL_UNCORE_MMIO_IMC_MEM_OFFSET 0x10000#define TGL_UNCORE_PCI_IMC_MAP_SIZE 0xe000 -static void +static int uncore_get_box_mmio_addr (struct intel_uncore_box *box,unsigned int base_offset,int bar_offset,int step)@@ -1541,19 +1553,20 @@ uncore_get_box_mmio_addr(struct intel_uncore_box *box,struct intel_uncore_pmu *pmu = box->pmu;struct intel_uncore_type *type = pmu->type;resource_size_t addr; +int ret =0 ;u32 bar;if (!pdev) {pr_warn ("perf uncore: Cannot find matched IMC device.\n" ); -return ; +return -ENODEV ; }pci_read_config_dword (pdev, bar_offset, &bar);if (!(bar &BIT (0 ))) {pr_warn ("perf uncore: BAR 0x%x is disabled. Failed to map %s counters.\n" , bar_offset, type->name); -pci_dev_put (pdev); -return ; + ret = -ENODEV ; + goto out; } bar &= ~BIT (0 ); addr = (resource_size_t)(bar + step * pmu->pmu_idx);@@ -1565,23 +1578,26 @@ uncore_get_box_mmio_addr(struct intel_uncore_box *box, addr += base_offset; box->io_addr =ioremap (addr, type->mmio_map_size); -if (!box->io_addr) +if (!box->io_addr) { + ret = -ENOMEM ;pr_warn ("perf uncore: Failed to ioremap for %s.\n" , type->name); - + } +out:pci_dev_put (pdev); +return ret; } -static void __uncore_imc_init_box (struct intel_uncore_box *box, +static int __uncore_imc_init_box (struct intel_uncore_box *box,unsigned int base_offset) { -uncore_get_box_mmio_addr (box, base_offset, +return uncore_get_box_mmio_addr (box, base_offset,SNB_UNCORE_PCI_IMC_BAR_OFFSET ,TGL_UNCORE_MMIO_IMC_MEM_OFFSET ); } -static void tgl_uncore_imc_freerunning_init_box (struct intel_uncore_box *box) +static int tgl_uncore_imc_freerunning_init_box (struct intel_uncore_box *box) { -__uncore_imc_init_box (box,0 ); +return __uncore_imc_init_box (box,0 ); }static struct intel_uncore_ops tgl_uncore_imc_freerunning_ops = {@@ -1648,13 +1664,15 @@ void tgl_uncore_mmio_init(void)#define ADL_UNCORE_IMC_CTL_INT (ADL_UNCORE_IMC_CTL_RST_CTRL | \ ADL_UNCORE_IMC_CTL_RST_CTRS) -static void adl_uncore_imc_init_box (struct intel_uncore_box *box) +static int adl_uncore_imc_init_box (struct intel_uncore_box *box) { -__uncore_imc_init_box (box,ADL_UNCORE_IMC_BASE ); +int ret =__uncore_imc_init_box (box,ADL_UNCORE_IMC_BASE );/* The global control in MC1 can control both MCs. */ -if (box->io_addr && (box->pmu->pmu_idx ==1 )) +if (!ret && (box->pmu->pmu_idx ==1 ))writel (ADL_UNCORE_IMC_CTL_INT , box->io_addr +ADL_UNCORE_IMC_GLOBAL_CTL ); + +return ret; }static void adl_uncore_mmio_disable_box (struct intel_uncore_box *box)@@ -1731,9 +1749,9 @@ static struct freerunning_counters adl_uncore_imc_freerunning[] = { [ADL_MMIO_UNCORE_IMC_DATA_WRITE] = { 0xA0, 0x0, 0x0, 1, 64 }, }; -static void adl_uncore_imc_freerunning_init_box (struct intel_uncore_box *box) +static int adl_uncore_imc_freerunning_init_box (struct intel_uncore_box *box) { -__uncore_imc_init_box (box,ADL_UNCORE_IMC_FREERUNNING_BASE ); +return __uncore_imc_init_box (box,ADL_UNCORE_IMC_FREERUNNING_BASE ); }static struct intel_uncore_ops adl_uncore_imc_freerunning_ops = {@@ -1803,9 +1821,9 @@ static const struct attribute_group lnl_uncore_format_group = { .attrs = lnl_uncore_formats_attr, }; -static void lnl_uncore_hbo_init_box (struct intel_uncore_box *box) +static int lnl_uncore_hbo_init_box (struct intel_uncore_box *box) { -uncore_get_box_mmio_addr (box,LNL_UNCORE_HBO_BASE , +return uncore_get_box_mmio_addr (box,LNL_UNCORE_HBO_BASE ,LNL_UNCORE_PCI_SAFBAR_OFFSET ,LNL_UNCORE_HBO_OFFSET ); }@@ -1829,14 +1847,16 @@ static struct intel_uncore_type lnl_uncore_hbo = { .format_group = &lnl_uncore_format_group, }; -static void lnl_uncore_sncu_init_box (struct intel_uncore_box *box) +static int lnl_uncore_sncu_init_box (struct intel_uncore_box *box) { -uncore_get_box_mmio_addr (box,LNL_UNCORE_SNCU_BASE , +int ret =uncore_get_box_mmio_addr (box,LNL_UNCORE_SNCU_BASE ,LNL_UNCORE_PCI_SAFBAR_OFFSET ,0 ); -if (box->io_addr) +if (!ret)writel (ADL_UNCORE_IMC_CTL_INT , box->io_addr +LNL_UNCORE_GLOBAL_CTL ); + +return ret; }static struct intel_uncore_ops lnl_uncore_sncu_ops = {@@ -1887,13 +1907,15 @@ static struct intel_uncore_type ptl_uncore_imc = { .mmio_map_size = 0xf00, }; -static void ptl_uncore_sncu_init_box (struct intel_uncore_box *box) +static int ptl_uncore_sncu_init_box (struct intel_uncore_box *box) { -intel_generic_uncore_mmio_init_box (box); +int ret =intel_generic_uncore_mmio_init_box (box);/* Clear the global freeze bit */ if (box->io_addr)writel (0 , box->io_addr +PTL_UNCORE_GLOBAL_CTL_OFFSET ); + +return ret; }static struct intel_uncore_ops ptl_uncore_sncu_ops = {arch/x86/events/intel/uncore_snbep.c +47
-30@@ -627,12 +627,12 @@ static u64 snbep_uncore_pci_read_counter(struct intel_uncore_box *box, struct pereturn count; } -static void snbep_uncore_pci_init_box (struct intel_uncore_box *box) +static int snbep_uncore_pci_init_box (struct intel_uncore_box *box) {struct pci_dev *pdev = box->pci_dev;int box_ctl =uncore_pci_box_ctl (box); -pci_write_config_dword (pdev, box_ctl,SNBEP_PMON_BOX_CTL_INT ); +return pci_write_config_dword (pdev, box_ctl,SNBEP_PMON_BOX_CTL_INT ); }static void snbep_uncore_msr_disable_box (struct intel_uncore_box *box)@@ -680,12 +680,14 @@ static void snbep_uncore_msr_disable_event(struct intel_uncore_box *box,wrmsrq (hwc->config_base, hwc->config); } -static void snbep_uncore_msr_init_box (struct intel_uncore_box *box) +static int snbep_uncore_msr_init_box (struct intel_uncore_box *box) {unsigned msr =uncore_msr_box_ctl (box);if (msr)wrmsrq (msr,SNBEP_PMON_BOX_CTL_INT ); + +return 0 ; }static struct attribute *snbep_uncore_formats_attr[] = {@@ -1507,18 +1509,21 @@ int snbep_uncore_pci_init(void)/* end of Sandy Bridge-EP uncore support */ /* IvyTown uncore support */ -static void ivbep_uncore_msr_init_box (struct intel_uncore_box *box) +static int ivbep_uncore_msr_init_box (struct intel_uncore_box *box) {unsigned msr =uncore_msr_box_ctl (box);if (msr)wrmsrq (msr,IVBEP_PMON_BOX_CTL_INT ); + +return 0 ; } -static void ivbep_uncore_pci_init_box (struct intel_uncore_box *box) +static int ivbep_uncore_pci_init_box (struct intel_uncore_box *box) {struct pci_dev *pdev = box->pci_dev; -pci_write_config_dword (pdev,SNBEP_PCI_PMON_BOX_CTL ,IVBEP_PMON_BOX_CTL_INT ); +return pci_write_config_dword (pdev,SNBEP_PCI_PMON_BOX_CTL , +IVBEP_PMON_BOX_CTL_INT ); }#define IVBEP_UNCORE_MSR_OPS_COMMON_INIT () \@@ -2784,7 +2789,7 @@ static struct intel_uncore_type hswep_uncore_cbox = {/* * Write SBOX Initialization register bit by bit to avoid spurious #GPs */ -static void hswep_uncore_sbox_msr_init_box (struct intel_uncore_box *box) +static int hswep_uncore_sbox_msr_init_box (struct intel_uncore_box *box) {unsigned msr =uncore_msr_box_ctl (box);@@ -2798,6 +2803,8 @@ static void hswep_uncore_sbox_msr_init_box(struct intel_uncore_box *box)wrmsrq (msr, flags); } } + +return 0 ; }static struct intel_uncore_ops hswep_uncore_sbox_msr_ops = {@@ -4162,12 +4169,13 @@ static const struct attribute_group skx_upi_uncore_format_group = { .attrs = skx_upi_uncore_formats_attr, }; -static void skx_upi_uncore_pci_init_box (struct intel_uncore_box *box) +static int skx_upi_uncore_pci_init_box (struct intel_uncore_box *box) {struct pci_dev *pdev = box->pci_dev;__set_bit (UNCORE_BOX_FLAG_CTL_OFFS8 , &box->flags); -pci_write_config_dword (pdev,SKX_UPI_PCI_PMON_BOX_CTL ,IVBEP_PMON_BOX_CTL_INT ); +return pci_write_config_dword (pdev,SKX_UPI_PCI_PMON_BOX_CTL , +IVBEP_PMON_BOX_CTL_INT ); }static struct intel_uncore_ops skx_upi_uncore_pci_ops = {@@ -4323,12 +4331,13 @@ static struct intel_uncore_type skx_uncore_upi = { .cleanup_mapping = skx_upi_cleanup_mapping, }; -static void skx_m2m_uncore_pci_init_box (struct intel_uncore_box *box) +static int skx_m2m_uncore_pci_init_box (struct intel_uncore_box *box) {struct pci_dev *pdev = box->pci_dev;__set_bit (UNCORE_BOX_FLAG_CTL_OFFS8 , &box->flags); -pci_write_config_dword (pdev,SKX_M2M_PCI_PMON_BOX_CTL ,IVBEP_PMON_BOX_CTL_INT ); +return pci_write_config_dword (pdev,SKX_M2M_PCI_PMON_BOX_CTL , +IVBEP_PMON_BOX_CTL_INT ); }static struct intel_uncore_ops skx_m2m_uncore_pci_ops = {@@ -4831,13 +4840,13 @@ void snr_uncore_cpu_init(void) uncore_msr_uncores = snr_msr_uncores; } -static void snr_m2m_uncore_pci_init_box (struct intel_uncore_box *box) +static int snr_m2m_uncore_pci_init_box (struct intel_uncore_box *box) {struct pci_dev *pdev = box->pci_dev;int box_ctl =uncore_pci_box_ctl (box);__set_bit (UNCORE_BOX_FLAG_CTL_OFFS8 , &box->flags); -pci_write_config_dword (pdev, box_ctl,IVBEP_PMON_BOX_CTL_INT ); +return pci_write_config_dword (pdev, box_ctl,IVBEP_PMON_BOX_CTL_INT ); }static struct intel_uncore_ops snr_m2m_uncore_pci_ops = {@@ -5010,17 +5019,22 @@ static int snr_uncore_mmio_map(struct intel_uncore_box *box,return 0 ; } -static void __snr_uncore_mmio_init_box (struct intel_uncore_box *box, +static int __snr_uncore_mmio_init_box (struct intel_uncore_box *box,unsigned int box_ctl,int mem_offset,unsigned int device) { -if (!snr_uncore_mmio_map (box, box_ctl, mem_offset, device)) +int ret; + + ret =snr_uncore_mmio_map (box, box_ctl, mem_offset, device); +if (!ret)writel (IVBEP_PMON_BOX_CTL_INT , box->io_addr); + +return ret; } -static void snr_uncore_mmio_init_box (struct intel_uncore_box *box) +static int snr_uncore_mmio_init_box (struct intel_uncore_box *box) { -__snr_uncore_mmio_init_box (box,uncore_mmio_box_ctl (box), +return __snr_uncore_mmio_init_box (box,uncore_mmio_box_ctl (box),SNR_IMC_MMIO_MEM0_OFFSET ,SNR_MC_DEVICE_ID ); }@@ -5637,14 +5651,14 @@ int icx_uncore_pci_init(void)return 0 ; } -static void icx_uncore_imc_init_box (struct intel_uncore_box *box) +static int icx_uncore_imc_init_box (struct intel_uncore_box *box) {unsigned int box_ctl = box->pmu->type->box_ctl + box->pmu->type->mmio_offset * (box->pmu->pmu_idx %ICX_NUMBER_IMC_CHN );int mem_offset = (box->pmu->pmu_idx /ICX_NUMBER_IMC_CHN ) *ICX_IMC_MEM_STRIDE +SNR_IMC_MMIO_MEM0_OFFSET ; -__snr_uncore_mmio_init_box (box, box_ctl, mem_offset, +return __snr_uncore_mmio_init_box (box, box_ctl, mem_offset,SNR_MC_DEVICE_ID ); }@@ -5701,12 +5715,12 @@ static struct uncore_event_desc icx_uncore_imc_freerunning_events[] = { { /* end: all zeroes */ }, }; -static void icx_uncore_imc_freerunning_init_box (struct intel_uncore_box *box) +static int icx_uncore_imc_freerunning_init_box (struct intel_uncore_box *box) {int mem_offset = box->pmu->pmu_idx *ICX_IMC_MEM_STRIDE +SNR_IMC_MMIO_MEM0_OFFSET ; -snr_uncore_mmio_map (box,uncore_mmio_box_ctl (box), +return snr_uncore_mmio_map (box,uncore_mmio_box_ctl (box), mem_offset,SNR_MC_DEVICE_ID ); }@@ -6003,10 +6017,10 @@ static struct intel_uncore_type spr_uncore_mdf = { .name = "mdf", }; -static void spr_uncore_mmio_offs8_init_box (struct intel_uncore_box *box) +static int spr_uncore_mmio_offs8_init_box (struct intel_uncore_box *box) {__set_bit (UNCORE_BOX_FLAG_CTL_OFFS8 , &box->flags); -intel_generic_uncore_mmio_init_box (box); +return intel_generic_uncore_mmio_init_box (box); }static struct intel_uncore_ops spr_uncore_mmio_offs8_ops = {@@ -6187,12 +6201,11 @@ static struct uncore_event_desc spr_uncore_imc_freerunning_events[] = {#define SPR_MC_DEVICE_ID 0x3251 -static void spr_uncore_imc_freerunning_init_box (struct intel_uncore_box *box) +static int spr_uncore_imc_freerunning_init_box (struct intel_uncore_box *box) {int mem_offset = box->pmu->pmu_idx *ICX_IMC_MEM_STRIDE +SNR_IMC_MMIO_MEM0_OFFSET ; - -snr_uncore_mmio_map (box,uncore_mmio_box_ctl (box), - mem_offset,SPR_MC_DEVICE_ID ); +return snr_uncore_mmio_map (box,uncore_mmio_box_ctl (box), + mem_offset,SPR_MC_DEVICE_ID ); }static struct intel_uncore_ops spr_uncore_imc_freerunning_ops = {@@ -6881,20 +6894,24 @@ static unsigned int dmr_iio_freerunning_box_offsets[] = {0x0 ,0x8000 ,0x18000 ,0x20000 }; -static void dmr_uncore_freerunning_init_box (struct intel_uncore_box *box) +static int dmr_uncore_freerunning_init_box (struct intel_uncore_box *box) {struct intel_uncore_type *type = box->pmu->type;u64 mmio_base;if (box->pmu->pmu_idx >= type->num_boxes) -return ; +return -ENODEV ; mmio_base =DMR_IMH1_HIOP_MMIO_BASE ; mmio_base += dmr_iio_freerunning_box_offsets[box->pmu->pmu_idx]; box->io_addr =ioremap (mmio_base, type->mmio_map_size); -if (!box->io_addr) +if (!box->io_addr) {pr_warn ("perf uncore: Failed to ioremap for %s.\n" , type->name); +return -ENOMEM ; + } + +return 0 ; } static struct intel_uncore_ops dmr_uncore_freerunning_ops = {
PATCH v2 4/8 perf/x86/intel/uncore: Keep PCI PMUs working when MMIO/MSR setup fails
In reply to Zide Chenuncore_event_cpu_online() returns -ENOMEM early when both the MSR and MMIO box allocations fail. This also aborts PCI uncore setup, even though PCI PMUs are independent of the MSR/MMIO paths.
Remove the early return so PCI uncore setup always runs regardless of whether MSR or MMIO box allocation succeeds.
--- arch/x86/events/intel/uncore.c | 2 -- 1 file changed, 2 deletions(-)
arch/x86/events/intel/uncore.c +0 -2
@@ -1633,8 +1633,6 @@ static int uncore_event_cpu_online(unsigned int cpu) die =topology_logical_die_id (cpu); msr_ret =uncore_box_ref (uncore_msr_uncores, die, cpu); mmio_ret =uncore_box_ref (uncore_mmio_uncores, die, cpu); -if (msr_ret && mmio_ret) -return -ENOMEM ;/* * Check if there is an online cpu in the package
Signature · 1 line
2.54.0
Re: [PATCH V2 4/8] perf/x86/intel/uncore: Keep PCI PMUs working when MMIO/MSR setup fails
Quoted 27 lines · Zide Chen
uncore_event_cpu_online()returns-ENOMEMearly when both the MSR and MMIO box allocations fail. This also aborts PCI uncore setup, even though PCI PMUs are independent of the MSR/MMIO paths.Remove the early return so PCI uncore setup always runs regardless of whether MSR or MMIO box allocation succeeds.
Fixes 3da04b8a00dd ("perf/x86/intel/uncore: Support MMIO type uncore blocks")Reviewed-by Ian Rogers <[email protected]>Signed-off-by Zide Chen <[email protected]>--- arch/x86/events/intel/uncore.c | 2 -- 1 file changed, 2 deletions(-)arch/x86/events/intel/uncore.c +0
-2@@ -1633,8 +1633,6 @@ static int uncore_event_cpu_online(unsigned int cpu) die =topology_logical_die_id (cpu); msr_ret =uncore_box_ref (uncore_msr_uncores, die, cpu); mmio_ret =uncore_box_ref (uncore_mmio_uncores, die, cpu); -if (msr_ret && mmio_ret) -return -ENOMEM ;/* * Check if there is an online cpu in the package
PATCH v2 5/8 perf/x86/intel/uncore: Factor out box setup code
In reply to Zide ChenThe PCI uncore PMU path already implements a lazy registration model:
the PMU is registered when the first active box appears and unregistered when the last active box is removed.
Factor this registration management into a shared helper, so the same code can be reused by the MSR and MMIO paths in later changes.
No functional change intended.
--- arch/x86/events/intel/uncore.c | 40 ++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-)
arch/x86/events/intel/uncore.c +28 -12
@@ -1148,6 +1148,29 @@ uncore_pci_find_dev_pmu(struct pci_dev *pdev, const struct pci_device_id *ids)return pmu; } +static int uncore_box_setup (struct intel_uncore_pmu *pmu, +struct intel_uncore_box *box) +{ +int ret; + +uncore_box_init (box); + +/* First active box registers the pmu. */ +if (atomic_inc_return (&pmu->activeboxes) >1 ) +return 0 ; + + ret =uncore_pmu_register (pmu); +if (ret) { +atomic_dec (&pmu->activeboxes); + goto err; + } + +return 0 ; +err: +uncore_box_exit (box); +return ret; +} +/* * Register the PMU for a PCI device * @pdev: The PCI device.
@@ -1173,20 +1196,13 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev, box->dieid = die; box->pci_dev = pdev; box->pmu = pmu; -uncore_box_init (box); - pmu->boxes[die] = box; -if (atomic_inc_return (&pmu->activeboxes) >1 ) -return 0 ; - -/* First active box registers the pmu */ - ret =uncore_pmu_register (pmu); -if (ret) { -atomic_dec (&pmu->activeboxes); - pmu->boxes[die] =NULL ; -uncore_box_exit (box); + ret =uncore_box_setup (pmu, box); +if (!ret) + pmu->boxes[die] = box; +else kfree (box); - } +return ret; }
Signature · 1 line
2.54.0
Re: [PATCH V2 5/8] perf/x86/intel/uncore: Factor out box setup code
Quoted 75 lines · Zide Chen
The PCI uncore PMU path already implements a lazy registration model:
the PMU is registered when the first active box appears and unregistered when the last active box is removed.Factor this registration management into a shared helper, so the same code can be reused by the MSR and MMIO paths in later changes.
No functional change intended.
Reviewed-by Ian Rogers <[email protected]>Signed-off-by Zide Chen <[email protected]>--- arch/x86/events/intel/uncore.c | 40 ++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-)arch/x86/events/intel/uncore.c +28
-12@@ -1148,6 +1148,29 @@ uncore_pci_find_dev_pmu(struct pci_dev *pdev, const struct pci_device_id *ids)return pmu; } +static int uncore_box_setup (struct intel_uncore_pmu *pmu, +struct intel_uncore_box *box) +{ +int ret; + +uncore_box_init (box); + +/* First active box registers the pmu. */ +if (atomic_inc_return (&pmu->activeboxes) >1 ) +return 0 ; + + ret =uncore_pmu_register (pmu); +if (ret) { +atomic_dec (&pmu->activeboxes); + goto err; + } + +return 0 ; +err: +uncore_box_exit (box); +return ret; +} +/* * Register the PMU for a PCI device * @pdev: The PCI device. @@ -1173,20 +1196,13 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev, box->dieid = die; box->pci_dev = pdev; box->pmu = pmu; -uncore_box_init (box); - pmu->boxes[die] = box; -if (atomic_inc_return (&pmu->activeboxes) >1 ) -return 0 ; - -/* First active box registers the pmu */ - ret =uncore_pmu_register (pmu); -if (ret) { -atomic_dec (&pmu->activeboxes); - pmu->boxes[die] =NULL ; -uncore_box_exit (box); + ret =uncore_box_setup (pmu, box); +if (!ret) + pmu->boxes[die] = box; +else kfree (box); - } +return ret; }
PATCH v2 6/8 perf/x86/intel/uncore: Introduce PMU flags and broken state
In reply to Zide ChenReplace the boolean 'registered' field in intel_uncore_pmu with an unsigned long 'flags' field, and add a PMU_BROKEN flag to track box setup failures. Broken flag is sticky, means it won't be cleared unless module reload or system reboot.
Broken PMUs are skipped in the CPU hotplug and box allocation paths.
When any box fails to initialize, the PMU is marked broken. Broken PMUs reject new event assignments and skip future box setup attempts. If the PMU was already registered, it remains so to avoid disrupting in-flight events on other boxes.
---
v2:
- Make the broken flag sticky by clear_bit() in uncore_pmu_unregister() other than zeroing out pmu->flag.
- In uncore_change_type_ctx(), don't stop CPU migration on broken PMU for in-flight events. (Sashiko).
- Use box->cpu == -1 to identify inactive boxes that don't need migration, no need to check uncore_die_has_box(), which is incomplete.
--- arch/x86/events/intel/uncore.c | 44 ++++++++++++++++++++++-------- arch/x86/events/intel/uncore.h | 13 ++++++++- arch/x86/events/intel/uncore_snb.c | 2 +- 3 files changed, 46 insertions(+), 13 deletions(-)
arch/x86/events/intel/uncore.c +33 -11
@@ -757,7 +757,7 @@ static int uncore_pmu_event_init(struct perf_event *event) pmu =uncore_event_to_pmu (event);/* no device found for this pmu */ -if (!pmu->registered) +if (!uncore_pmu_available (pmu))return -ENOENT ;/* Sampling not supported yet */
@@ -953,16 +953,18 @@ static int uncore_pmu_register(struct intel_uncore_pmu *pmu) ret =perf_pmu_register (&pmu->pmu, pmu->name,-1 );if (!ret) - pmu->registered = true; +uncore_pmu_set_registered (pmu);return ret; }static void uncore_pmu_unregister (struct intel_uncore_pmu *pmu) { -if (!pmu->registered) +if (!uncore_pmu_registered (pmu))return ;perf_pmu_unregister (&pmu->pmu); - pmu->registered = false; + +/* Keep PMU_BROKEN_BIT stick. */ +uncore_pmu_clear_registered (pmu); }static void uncore_free_boxes (struct intel_uncore_pmu *pmu)
@@ -1153,7 +1155,12 @@ static int uncore_box_setup(struct intel_uncore_pmu *pmu, {int ret; -uncore_box_init (box); +if (uncore_pmu_broken (pmu)) +return -ENODEV ; + + ret =uncore_box_init (box); +if (ret) + goto err;/* First active box registers the pmu. */ if (atomic_inc_return (&pmu->activeboxes) >1 )
@@ -1167,6 +1174,16 @@ static int uncore_box_setup(struct intel_uncore_pmu *pmu,return 0 ; err: +/* +* On failure on any box, mark the per-package PMU as broken regardless +* of whether it was registered or not. +* +* Don't decrement refcnt to avoid other in-die CPUs from trying to set +* up the PMU box again. +* +* Don't kfree box; MSR and MMIO boxes are freed at module exit only. +*/ +uncore_pmu_set_broken (pmu);uncore_box_exit (box);return ret; }
@@ -1190,8 +1207,10 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,return -EINVAL ; box =uncore_alloc_box (type,NUMA_NO_NODE ); -if (!box) +if (!box) { +uncore_pmu_set_broken (pmu);return -ENOMEM ; + } box->dieid = die; box->pci_dev = pdev;
@@ -1504,7 +1523,8 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,if (old_cpu <0 ) {WARN_ON_ONCE (box->cpu !=-1 ); -if (uncore_die_has_box (type, die, pmu->pmu_idx)) { +if (uncore_die_has_box (type, die, pmu->pmu_idx) && + !uncore_pmu_broken (pmu)) { box->cpu = new_cpu;cpumask_set_cpu (new_cpu, &pmu->cpu_mask); }
@@ -1512,12 +1532,14 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu, }WARN_ON_ONCE (box->cpu !=-1 && box->cpu != old_cpu); - box->cpu =-1 ;cpumask_clear_cpu (old_cpu, &pmu->cpu_mask); -if (new_cpu <0 ) +if (new_cpu <0 ) { + box->cpu =-1 ;continue ; + } -if (!uncore_die_has_box (type, die, pmu->pmu_idx)) +/* non-active box doesn't need migration. */ +if (box->cpu ==-1 )continue ;uncore_pmu_cancel_hrtimer (box);perf_pmu_migrate_context (&pmu->pmu, old_cpu, new_cpu);
@@ -1593,7 +1615,7 @@ static int allocate_boxes(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { -if (pmu->boxes[die]) +if (pmu->boxes[die] ||uncore_pmu_broken (pmu))continue ; box =uncore_alloc_box (type,cpu_to_node (cpu));if (!box)
arch/x86/events/intel/uncore.h +12 -1
@@ -146,13 +146,24 @@ struct intel_uncore_pmu {struct pmu pmu;char name[UNCORE_PMU_NAME_LEN ];int pmu_idx; -bool registered; +unsigned long flags;atomic_t activeboxes;cpumask_t cpu_mask;struct intel_uncore_type *type;struct intel_uncore_box **boxes; }; +#define PMU_REGISTERED_BIT 0 +#define PMU_BROKEN_BIT 1 + +#define uncore_pmu_registered (pmu) test_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +#define uncore_pmu_broken (pmu) test_bit(PMU_BROKEN_BIT, &(pmu)->flags) +#define uncore_pmu_available (pmu) (uncore_pmu_registered(pmu) && \ + !uncore_pmu_broken(pmu)) +#define uncore_pmu_set_registered (pmu) set_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +#define uncore_pmu_set_broken (pmu) set_bit(PMU_BROKEN_BIT, &(pmu)->flags) +#define uncore_pmu_clear_registered (pmu) clear_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +struct intel_uncore_extra_reg {raw_spinlock_t lock;u64 config, config1, config2;
arch/x86/events/intel/uncore_snb.c +1 -1
@@ -940,7 +940,7 @@ static int snb_uncore_imc_event_init(struct perf_event *event) pmu =uncore_event_to_pmu (event);/* no device found for this pmu */ -if (!pmu->registered) +if (!uncore_pmu_available (pmu))return -ENOENT ;/* Sampling not supported yet */
Signature · 1 line
2.54.0
Re: [PATCH V2 6/8] perf/x86/intel/uncore: Introduce PMU flags and broken state
Quoted 49 lines · Zide Chen
Replace the boolean 'registered' field in
intel_uncore_pmuwith an unsigned long 'flags' field, and add aPMU_BROKENflag to track box setup failures. Broken flag is sticky, means it won't be cleared unless module reload or system reboot.Broken PMUs are skipped in the CPU hotplug and box allocation paths.
When any box fails to initialize, the PMU is marked broken. Broken PMUs reject new event assignments and skip future box setup attempts. If the PMU was already registered, it remains so to avoid disrupting in-flight events on other boxes.
Signed-off-by Zide Chen <[email protected]>---
v2:
- Make the broken flag sticky byclear_bit()inuncore_pmu_unregister()other than zeroing outpmu->flag.
- Inuncore_change_type_ctx(), don't stop CPU migration on broken PMU for in-flight events. (Sashiko).
- Usebox->cpu== -1 to identify inactive boxes that don't need migration, no need to checkuncore_die_has_box(), which is incomplete.--- arch/x86/events/intel/uncore.c | 44 ++++++++++++++++++++++-------- arch/x86/events/intel/uncore.h | 13 ++++++++- arch/x86/events/intel/uncore_snb.c | 2 +- 3 files changed, 46 insertions(+), 13 deletions(-)arch/x86/events/intel/uncore.c +2
-2@@ -757,7 +757,7 @@ static int uncore_pmu_event_init(struct perf_event *event) pmu =uncore_event_to_pmu (event);/* no device found for this pmu */ -if (!pmu->registered) +if (!uncore_pmu_available (pmu))return -ENOENT ;/* Sampling not supported yet */ @@ -953,16 +953,18 @@ static int uncore_pmu_register(struct intel_uncore_pmu *pmu) ret =perf_pmu_register (&pmu->pmu, pmu->name,-1 );if (!ret) - pmu->registered = true; +uncore_pmu_set_registered (pmu);return ret; }
arch/x86/events/intel/uncore.c static int uncore_pmu_register(struct intel_uncore_pmu *pmu) · line 967
Quoted 10 lines
arch/x86/events/intel/uncore.c +4
-2static void uncore_pmu_unregister (struct intel_uncore_pmu *pmu) { -if (!pmu->registered) +if (!uncore_pmu_registered (pmu))return ;perf_pmu_unregister (&pmu->pmu); - pmu->registered = false; + +/* Keep PMU_BROKEN_BIT stick. */ +uncore_pmu_clear_registered (pmu);
Why to set PMU_BROKEN_BIT to a stick bit and doesn't clear it when unregistering the PMU? Per my understanding, there is no active boxes when uncore_pmu_unregister() is called, seem all flags can be cleared. When the new CPU or PCI device are hot plug-in again, it may be a new device or the old issue which leads to the PMU can't be initialized has been fixed, the uncore PMU can work correctly, the PMU_BROKEN_BIT should be set any more.
Quoted 75 lines
arch/x86/events/intel/uncore.c +26
-6}static void uncore_free_boxes (struct intel_uncore_pmu *pmu)@@ -1153,7 +1155,12 @@ static int uncore_box_setup(struct intel_uncore_pmu *pmu, {int ret; -uncore_box_init (box); +if (uncore_pmu_broken (pmu)) +return -ENODEV ; + + ret =uncore_box_init (box); +if (ret) + goto err;/* First active box registers the pmu. */ if (atomic_inc_return (&pmu->activeboxes) >1 )@@ -1167,6 +1174,16 @@ static int uncore_box_setup(struct intel_uncore_pmu *pmu,return 0 ; err: +/* +* On failure on any box, mark the per-package PMU as broken regardless +* of whether it was registered or not. +* +* Don't decrement refcnt to avoid other in-die CPUs from trying to set +* up the PMU box again. +* +* Don't kfree box; MSR and MMIO boxes are freed at module exit only. +*/ +uncore_pmu_set_broken (pmu);uncore_box_exit (box);return ret; }@@ -1190,8 +1207,10 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,return -EINVAL ; box =uncore_alloc_box (type,NUMA_NO_NODE ); -if (!box) +if (!box) { +uncore_pmu_set_broken (pmu);return -ENOMEM ; + } box->dieid = die; box->pci_dev = pdev;@@ -1504,7 +1523,8 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,if (old_cpu <0 ) {WARN_ON_ONCE (box->cpu !=-1 ); -if (uncore_die_has_box (type, die, pmu->pmu_idx)) { +if (uncore_die_has_box (type, die, pmu->pmu_idx) && + !uncore_pmu_broken (pmu)) { box->cpu = new_cpu;cpumask_set_cpu (new_cpu, &pmu->cpu_mask); }@@ -1512,12 +1532,14 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu, }WARN_ON_ONCE (box->cpu !=-1 && box->cpu != old_cpu); - box->cpu =-1 ;cpumask_clear_cpu (old_cpu, &pmu->cpu_mask); -if (new_cpu <0 ) +if (new_cpu <0 ) { + box->cpu =-1 ;continue ; + } -if (!uncore_die_has_box (type, die, pmu->pmu_idx)) +/* non-active box doesn't need migration. */ +if (box->cpu ==-1 )continue ;uncore_pmu_cancel_hrtimer (box);perf_pmu_migrate_context (&pmu->pmu, old_cpu, new_cpu);
arch/x86/events/intel/uncore.c static int allocate_boxes(struct intel_uncore_type **types, · line 1617
Quoted 3 lines
arch/x86/events/intel/uncore.c +0
-0@@ -1593,7 +1615,7 @@ static int allocate_boxes(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) {
IIUC, the "type->num_boxes" actually indicates the number of PMU or the number of box classes. Since currently there could be multiple boxes for each kind of PMUs on the multiple dies device, the name "num_boxes" become inaccurate and much misleading. could we change it to a more accurate name, like "num_pmus" or "num_box_classes"?
Quoted 48 lines
arch/x86/events/intel/uncore.c +1
-1-if (pmu->boxes[die]) +if (pmu->boxes[die] ||uncore_pmu_broken (pmu))continue ; box =uncore_alloc_box (type,cpu_to_node (cpu));if (!box)arch/x86/events/intel/uncore.h +12
-1@@ -146,13 +146,24 @@ struct intel_uncore_pmu {struct pmu pmu;char name[UNCORE_PMU_NAME_LEN ];int pmu_idx; -bool registered; +unsigned long flags;atomic_t activeboxes;cpumask_t cpu_mask;struct intel_uncore_type *type;struct intel_uncore_box **boxes; }; +#define PMU_REGISTERED_BIT 0 +#define PMU_BROKEN_BIT 1 + +#define uncore_pmu_registered (pmu) test_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +#define uncore_pmu_broken (pmu) test_bit(PMU_BROKEN_BIT, &(pmu)->flags) +#define uncore_pmu_available (pmu) (uncore_pmu_registered(pmu) && \ + !uncore_pmu_broken(pmu)) +#define uncore_pmu_set_registered (pmu) set_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +#define uncore_pmu_set_broken (pmu) set_bit(PMU_BROKEN_BIT, &(pmu)->flags) +#define uncore_pmu_clear_registered (pmu) clear_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +struct intel_uncore_extra_reg {raw_spinlock_t lock;u64 config, config1, config2;arch/x86/events/intel/uncore_snb.c +1
-1@@ -940,7 +940,7 @@ static int snb_uncore_imc_event_init(struct perf_event *event) pmu =uncore_event_to_pmu (event);/* no device found for this pmu */ -if (!pmu->registered) +if (!uncore_pmu_available (pmu))return -ENOENT ;/* Sampling not supported yet */
Re: [PATCH V2 6/8] perf/x86/intel/uncore: Introduce PMU flags and broken state
Quoted 60 lines · Dapeng
Quoted 58 lines · Zide Chen
Replace the boolean 'registered' field in
intel_uncore_pmuwith an unsigned long 'flags' field, and add aPMU_BROKENflag to track box setup failures. Broken flag is sticky, means it won't be cleared unless module reload or system reboot.Broken PMUs are skipped in the CPU hotplug and box allocation paths.
When any box fails to initialize, the PMU is marked broken. Broken PMUs reject new event assignments and skip future box setup attempts. If the PMU was already registered, it remains so to avoid disrupting in-flight events on other boxes.
Signed-off-by Zide Chen <[email protected]>---
v2:
- Make the broken flag sticky byclear_bit()inuncore_pmu_unregister()other than zeroing outpmu->flag.
- Inuncore_change_type_ctx(), don't stop CPU migration on broken PMU for in-flight events. (Sashiko).
- Usebox->cpu== -1 to identify inactive boxes that don't need migration, no need to checkuncore_die_has_box(), which is incomplete.--- arch/x86/events/intel/uncore.c | 44 ++++++++++++++++++++++-------- arch/x86/events/intel/uncore.h | 13 ++++++++- arch/x86/events/intel/uncore_snb.c | 2 +- 3 files changed, 46 insertions(+), 13 deletions(-)arch/x86/events/intel/uncore.c +5
-4@@ -757,7 +757,7 @@ static int uncore_pmu_event_init(struct perf_event *event) pmu =uncore_event_to_pmu (event);/* no device found for this pmu */ -if (!pmu->registered) +if (!uncore_pmu_available (pmu))return -ENOENT ;/* Sampling not supported yet */ @@ -953,16 +953,18 @@ static int uncore_pmu_register(struct intel_uncore_pmu *pmu) ret =perf_pmu_register (&pmu->pmu, pmu->name,-1 );if (!ret) - pmu->registered = true; +uncore_pmu_set_registered (pmu);return ret; }static void uncore_pmu_unregister (struct intel_uncore_pmu *pmu) { -if (!pmu->registered) +if (!uncore_pmu_registered (pmu))return ;perf_pmu_unregister (&pmu->pmu); - pmu->registered = false; + +/* Keep PMU_BROKEN_BIT stick. */
Quoted 8 lines
arch/x86/events/intel/uncore.c static int uncore_pmu_register(struct intel_uncore_pmu *pmu) · line 967
Quoted 1 line
arch/x86/events/intel/uncore.c +1
-0+uncore_pmu_clear_registered (pmu);Why to set
PMU_BROKEN_BITto a stick bit and doesn't clear it when unregistering the PMU? Per my understanding, there is no active boxes whenuncore_pmu_unregister()is called, seem all flags can be cleared. When the new CPU or PCI device are hot plug-in again, it may be a new device or the old issue which leads to the PMU can't be initialized has been fixed, the uncore PMU can work correctly, thePMU_BROKEN_BITshould be set any more.
Woops, a typo here: stick->sticky.
Let's examine a theoretical flow for a particular PMU:
PCI dev on die0 online: activeboxes=1, PMU registered
PCI dev on die1 online: init_box failed, pmu->activeboxes=1, set broken PCI dev on die0 offline: activeboxes=0, PMU unregistered
If the broken bit is cleared in uncore_pmu_unregister():
PCI dev on die0 or die2 online: activeboxes=1, PMU registered
This is wrong, because the box for die1 is not working.
In reality, box_allocate() or init_box() failures may not happen at all, and when they do, the system may be experiencing severe out-of-memory or other catastrophic conditions. Keeping the broken flag sticky should be fine.
One alternative to avoid making it sticky is to use pmu->activeboxes as pmu->die_refcnt to track online dies, not necessarily active boxes, as v1 does.
The problem with this approach is that it could cause a pmu->die_refcnt leak in the uncore_pci_remove() path: if init_box() fails but pmu->die_refcnt has already been incremented, there is no chance to call uncore_pci_pmu_unregister() because pci_get_drvdata() returns NULL.
Quoted 68 lines
Quoted 68 lines
arch/x86/events/intel/uncore.c +24
-5}static void uncore_free_boxes (struct intel_uncore_pmu *pmu)@@ -1153,7 +1155,12 @@ static int uncore_box_setup(struct intel_uncore_pmu *pmu, {int ret; -uncore_box_init (box); +if (uncore_pmu_broken (pmu)) +return -ENODEV ; + + ret =uncore_box_init (box); +if (ret) + goto err;/* First active box registers the pmu. */ if (atomic_inc_return (&pmu->activeboxes) >1 )@@ -1167,6 +1174,16 @@ static int uncore_box_setup(struct intel_uncore_pmu *pmu,return 0 ; err: +/* +* On failure on any box, mark the per-package PMU as broken regardless +* of whether it was registered or not. +* +* Don't decrement refcnt to avoid other in-die CPUs from trying to set +* up the PMU box again. +* +* Don't kfree box; MSR and MMIO boxes are freed at module exit only. +*/ +uncore_pmu_set_broken (pmu);uncore_box_exit (box);return ret; }@@ -1190,8 +1207,10 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,return -EINVAL ; box =uncore_alloc_box (type,NUMA_NO_NODE ); -if (!box) +if (!box) { +uncore_pmu_set_broken (pmu);return -ENOMEM ; + } box->dieid = die; box->pci_dev = pdev;@@ -1504,7 +1523,8 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,if (old_cpu <0 ) {WARN_ON_ONCE (box->cpu !=-1 ); -if (uncore_die_has_box (type, die, pmu->pmu_idx)) { +if (uncore_die_has_box (type, die, pmu->pmu_idx) && + !uncore_pmu_broken (pmu)) { box->cpu = new_cpu;cpumask_set_cpu (new_cpu, &pmu->cpu_mask); }@@ -1512,12 +1532,14 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu, }WARN_ON_ONCE (box->cpu !=-1 && box->cpu != old_cpu); - box->cpu =-1 ;cpumask_clear_cpu (old_cpu, &pmu->cpu_mask); -if (new_cpu <0 ) +if (new_cpu <0 ) { + box->cpu =-1 ;continue ; + }
Quoted 16 lines
arch/x86/events/intel/uncore.c static int allocate_boxes(struct intel_uncore_type **types, · line 1617
Quoted 10 lines
arch/x86/events/intel/uncore.c +2
-1-if (!uncore_die_has_box (type, die, pmu->pmu_idx)) +/* non-active box doesn't need migration. */ +if (box->cpu ==-1 )continue ;uncore_pmu_cancel_hrtimer (box);perf_pmu_migrate_context (&pmu->pmu, old_cpu, new_cpu);@@ -1593,7 +1615,7 @@ static int allocate_boxes(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) {IIUC, the "
type->num_boxes" actually indicates the number of PMU or the number of box classes. Since currently there could be multiple boxes for each kind of PMUs on the multiple dies device, the name "num_boxes" become inaccurate and much misleading. could we change it to a more accurate name, like "num_pmus" or "num_box_classes"?
Yes, agree. Will do more clean up in the future.
Quoted 49 lines
Quoted 49 lines
arch/x86/events/intel/uncore.c +1
-1-if (pmu->boxes[die]) +if (pmu->boxes[die] ||uncore_pmu_broken (pmu))continue ; box =uncore_alloc_box (type,cpu_to_node (cpu));if (!box)arch/x86/events/intel/uncore.h +12
-1@@ -146,13 +146,24 @@ struct intel_uncore_pmu {struct pmu pmu;char name[UNCORE_PMU_NAME_LEN ];int pmu_idx; -bool registered; +unsigned long flags;atomic_t activeboxes;cpumask_t cpu_mask;struct intel_uncore_type *type;struct intel_uncore_box **boxes; }; +#define PMU_REGISTERED_BIT 0 +#define PMU_BROKEN_BIT 1 + +#define uncore_pmu_registered (pmu) test_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +#define uncore_pmu_broken (pmu) test_bit(PMU_BROKEN_BIT, &(pmu)->flags) +#define uncore_pmu_available (pmu) (uncore_pmu_registered(pmu) && \ + !uncore_pmu_broken(pmu)) +#define uncore_pmu_set_registered (pmu) set_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +#define uncore_pmu_set_broken (pmu) set_bit(PMU_BROKEN_BIT, &(pmu)->flags) +#define uncore_pmu_clear_registered (pmu) clear_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +struct intel_uncore_extra_reg {raw_spinlock_t lock;u64 config, config1, config2;arch/x86/events/intel/uncore_snb.c +1
-1@@ -940,7 +940,7 @@ static int snb_uncore_imc_event_init(struct perf_event *event) pmu =uncore_event_to_pmu (event);/* no device found for this pmu */ -if (!pmu->registered) +if (!uncore_pmu_available (pmu))return -ENOENT ;/* Sampling not supported yet */
Re: [PATCH V2 6/8] perf/x86/intel/uncore: Introduce PMU flags and broken state
Quoted 70 lines · Zide
Quoted 50 lines · Dapeng
Quoted 49 lines · Zide Chen
Replace the boolean 'registered' field in
intel_uncore_pmuwith an unsigned long 'flags' field, and add aPMU_BROKENflag to track box setup failures. Broken flag is sticky, means it won't be cleared unless module reload or system reboot.Broken PMUs are skipped in the CPU hotplug and box allocation paths.
When any box fails to initialize, the PMU is marked broken. Broken PMUs reject new event assignments and skip future box setup attempts. If the PMU was already registered, it remains so to avoid disrupting in-flight events on other boxes.
Signed-off-by Zide Chen <[email protected]>---
v2:
- Make the broken flag sticky byclear_bit()inuncore_pmu_unregister()other than zeroing outpmu->flag.
- Inuncore_change_type_ctx(), don't stop CPU migration on broken PMU for in-flight events. (Sashiko).
- Usebox->cpu== -1 to identify inactive boxes that don't need migration, no need to checkuncore_die_has_box(), which is incomplete.--- arch/x86/events/intel/uncore.c | 44 ++++++++++++++++++++++-------- arch/x86/events/intel/uncore.h | 13 ++++++++- arch/x86/events/intel/uncore_snb.c | 2 +- 3 files changed, 46 insertions(+), 13 deletions(-)arch/x86/events/intel/uncore.c +2
-2@@ -757,7 +757,7 @@ static int uncore_pmu_event_init(struct perf_event *event) pmu =uncore_event_to_pmu (event);/* no device found for this pmu */ -if (!pmu->registered) +if (!uncore_pmu_available (pmu))return -ENOENT ;/* Sampling not supported yet */ @@ -953,16 +953,18 @@ static int uncore_pmu_register(struct intel_uncore_pmu *pmu) ret =perf_pmu_register (&pmu->pmu, pmu->name,-1 );if (!ret) - pmu->registered = true; +uncore_pmu_set_registered (pmu);return ret; }Quoted 16 lines
arch/x86/events/intel/uncore.c static int uncore_pmu_register(struct intel_uncore_pmu *pmu) · line 967
Quoted 10 lines
arch/x86/events/intel/uncore.c +4
-2static void uncore_pmu_unregister (struct intel_uncore_pmu *pmu) { -if (!pmu->registered) +if (!uncore_pmu_registered (pmu))return ;perf_pmu_unregister (&pmu->pmu); - pmu->registered = false; + +/* Keep PMU_BROKEN_BIT stick. */ +uncore_pmu_clear_registered (pmu);Why to set
PMU_BROKEN_BITto a stick bit and doesn't clear it when unregistering the PMU? Per my understanding, there is no active boxes whenuncore_pmu_unregister()is called, seem all flags can be cleared. When the new CPU or PCI device are hot plug-in again, it may be a new device or the old issue which leads to the PMU can't be initialized has been fixed, the uncore PMU can work correctly, thePMU_BROKEN_BITshould be set any more.Woops, a typo here:
stick->sticky.
Quoted 8 lines
Let's examine a theoretical flow for a particular PMU:
PCI dev on die0 online: activeboxes=1, PMU registered
PCI dev on die1 online:init_boxfailed, pmu->activeboxes=1, set broken PCI dev on die0 offline: activeboxes=0, PMU unregisteredIf the broken bit is cleared in
uncore_pmu_unregister():
PCI dev on die0 or die2 online: activeboxes=1, PMU registered
I see.
Quoted 100 lines
This is wrong, because the box for die1 is not working.
In reality,
box_allocate()orinit_box()failures may not happen at all, and when they do, the system may be experiencing severe out-of-memory or other catastrophic conditions. Keeping the broken flag sticky should be fine.One alternative to avoid making it sticky is to use
pmu->activeboxesaspmu->die_refcntto track online dies, not necessarily active boxes, as v1 does.The problem with this approach is that it could cause a
pmu->die_refcntleak in theuncore_pci_remove()path: ifinit_box()fails butpmu->die_refcnthas already been incremented, there is no chance to calluncore_pci_pmu_unregister()becausepci_get_drvdata()returns NULL.Quoted 68 lines
Quoted 68 lines
arch/x86/events/intel/uncore.c +24
-5}static void uncore_free_boxes (struct intel_uncore_pmu *pmu)@@ -1153,7 +1155,12 @@ static int uncore_box_setup(struct intel_uncore_pmu *pmu, {int ret; -uncore_box_init (box); +if (uncore_pmu_broken (pmu)) +return -ENODEV ; + + ret =uncore_box_init (box); +if (ret) + goto err;/* First active box registers the pmu. */ if (atomic_inc_return (&pmu->activeboxes) >1 )@@ -1167,6 +1174,16 @@ static int uncore_box_setup(struct intel_uncore_pmu *pmu,return 0 ; err: +/* +* On failure on any box, mark the per-package PMU as broken regardless +* of whether it was registered or not. +* +* Don't decrement refcnt to avoid other in-die CPUs from trying to set +* up the PMU box again. +* +* Don't kfree box; MSR and MMIO boxes are freed at module exit only. +*/ +uncore_pmu_set_broken (pmu);uncore_box_exit (box);return ret; }@@ -1190,8 +1207,10 @@ static int uncore_pci_pmu_register(struct pci_dev *pdev,return -EINVAL ; box =uncore_alloc_box (type,NUMA_NO_NODE ); -if (!box) +if (!box) { +uncore_pmu_set_broken (pmu);return -ENOMEM ; + } box->dieid = die; box->pci_dev = pdev;@@ -1504,7 +1523,8 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,if (old_cpu <0 ) {WARN_ON_ONCE (box->cpu !=-1 ); -if (uncore_die_has_box (type, die, pmu->pmu_idx)) { +if (uncore_die_has_box (type, die, pmu->pmu_idx) && + !uncore_pmu_broken (pmu)) { box->cpu = new_cpu;cpumask_set_cpu (new_cpu, &pmu->cpu_mask); }@@ -1512,12 +1532,14 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu, }WARN_ON_ONCE (box->cpu !=-1 && box->cpu != old_cpu); - box->cpu =-1 ;cpumask_clear_cpu (old_cpu, &pmu->cpu_mask); -if (new_cpu <0 ) +if (new_cpu <0 ) { + box->cpu =-1 ;continue ; + }Quoted 15 lines
arch/x86/events/intel/uncore.c static int allocate_boxes(struct intel_uncore_type **types, · line 1617
Quoted 10 lines
arch/x86/events/intel/uncore.c +2
-1-if (!uncore_die_has_box (type, die, pmu->pmu_idx)) +/* non-active box doesn't need migration. */ +if (box->cpu ==-1 )continue ;uncore_pmu_cancel_hrtimer (box);perf_pmu_migrate_context (&pmu->pmu, old_cpu, new_cpu);@@ -1593,7 +1615,7 @@ static int allocate_boxes(struct intel_uncore_type **types, type = *types; pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) {IIUC, the "
type->num_boxes" actually indicates the number of PMU or the number of box classes. Since currently there could be multiple boxes for each kind of PMUs on the multiple dies device, the name "num_boxes" become inaccurate and much misleading. could we change it to a more accurate name, like "num_pmus" or "num_box_classes"?
Quoted 1 line
Yes, agree. Will do more clean up in the future.
Thanks.
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
arch/x86/events/intel/uncore.c +1
-1-if (pmu->boxes[die]) +if (pmu->boxes[die] ||uncore_pmu_broken (pmu))continue ; box =uncore_alloc_box (type,cpu_to_node (cpu));if (!box)arch/x86/events/intel/uncore.h +12
-1@@ -146,13 +146,24 @@ struct intel_uncore_pmu {struct pmu pmu;char name[UNCORE_PMU_NAME_LEN ];int pmu_idx; -bool registered; +unsigned long flags;atomic_t activeboxes;cpumask_t cpu_mask;struct intel_uncore_type *type;struct intel_uncore_box **boxes; }; +#define PMU_REGISTERED_BIT 0 +#define PMU_BROKEN_BIT 1 + +#define uncore_pmu_registered (pmu) test_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +#define uncore_pmu_broken (pmu) test_bit(PMU_BROKEN_BIT, &(pmu)->flags) +#define uncore_pmu_available (pmu) (uncore_pmu_registered(pmu) && \ + !uncore_pmu_broken(pmu)) +#define uncore_pmu_set_registered (pmu) set_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +#define uncore_pmu_set_broken (pmu) set_bit(PMU_BROKEN_BIT, &(pmu)->flags) +#define uncore_pmu_clear_registered (pmu) clear_bit(PMU_REGISTERED_BIT, &(pmu)->flags) +struct intel_uncore_extra_reg {raw_spinlock_t lock;u64 config, config1, config2;arch/x86/events/intel/uncore_snb.c +1
-1@@ -940,7 +940,7 @@ static int snb_uncore_imc_event_init(struct perf_event *event) pmu =uncore_event_to_pmu (event);/* no device found for this pmu */ -if (!pmu->registered) +if (!uncore_pmu_available (pmu))return -ENOENT ;/* Sampling not supported yet */
PATCH v2 7/8 perf/x86/intel/uncore: Fix uncore_box ref/unref ordering on CPU hotplug
In reply to Zide ChenIn uncore_event_cpu_online(), uncore_box_ref() was called before uncore_change_context(). uncore_box_ref() gates on box->cpu >= 0, but box->cpu is still -1 at that point because uncore_change_context() has not run yet. As a result, the box is never initialized on the first CPU to come online in a die, leaving it permanently uninitialized in the single-CPU-per-die case.
Thus, box->refcnt is one count below the true value, and in the CPU offline path, the box will be torn down on the second-to-last CPU.
In uncore_event_cpu_offline(), uncore_box_unref() was called after uncore_change_context(), so box->cpu is already -1 when the collector CPU goes offline, which prevents it from tearing down the box.
Fix by swapping the call order in both paths so that uncore_box_{ref,unref}() runs at the point where box->cpu reflects the correct context.
--- arch/x86/events/intel/uncore.c | 50 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 27 deletions(-)
arch/x86/events/intel/uncore.c +23 -27
@@ -1577,9 +1577,15 @@ static int uncore_event_cpu_offline(unsigned int cpu) {int die, target; +/* Clear the references */ + die =topology_logical_die_id (cpu); +uncore_box_unref (uncore_msr_uncores, die); +uncore_box_unref (uncore_mmio_uncores, die); +/* Check if exiting cpu is used for collecting uncore events */ if (!cpumask_test_and_clear_cpu (cpu, &uncore_cpu_mask)) - goto unref; +return 0 ; +/* Find a new cpu to collect uncore events */ target =cpumask_any_but (topology_die_cpumask (cpu), cpu);
@@ -1592,16 +1598,10 @@ static int uncore_event_cpu_offline(unsigned int cpu)uncore_change_context (uncore_msr_uncores, cpu, target);uncore_change_context (uncore_mmio_uncores, cpu, target);uncore_change_context (uncore_pci_uncores, cpu, target); - -unref: -/* Clear the references */ - die =topology_logical_die_id (cpu); -uncore_box_unref (uncore_msr_uncores, die); -uncore_box_unref (uncore_mmio_uncores, die);return 0 ; } -static int allocate_boxes (struct intel_uncore_type **types, +static void allocate_boxes (struct intel_uncore_type **types,unsigned int die,unsigned int cpu) {struct intel_uncore_box *box, *tmp;
@@ -1618,8 +1618,10 @@ static int allocate_boxes(struct intel_uncore_type **types,if (pmu->boxes[die] ||uncore_pmu_broken (pmu))continue ; box =uncore_alloc_box (type,cpu_to_node (cpu)); -if (!box) +if (!box) { +uncore_pmu_set_broken (pmu); goto cleanup; + } box->pmu = pmu; box->dieid = die;list_add (&box->active_list, &allocated);
@@ -1630,14 +1632,13 @@ static int allocate_boxes(struct intel_uncore_type **types,list_del_init (&box->active_list); box->pmu->boxes[die] = box; } -return 0 ; +return ; cleanup:list_for_each_entry_safe (box, tmp, &allocated, active_list) {list_del_init (&box->active_list);kfree (box); } -return -ENOMEM ; } static int uncore_box_ref(struct intel_uncore_type **types,
@@ -1646,11 +1647,7 @@ static int uncore_box_ref(struct intel_uncore_type **types,struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box; -int i, ret; - - ret =allocate_boxes (types, die, cpu); -if (ret) -return ret; +int i;for (; *types; types++) { type = *types;
@@ -1666,27 +1663,26 @@ static int uncore_box_ref(struct intel_uncore_type **types,static int uncore_event_cpu_online (unsigned int cpu) { -int die, target, msr_ret, mmio_ret; +int die, target; die =topology_logical_die_id (cpu); - msr_ret =uncore_box_ref (uncore_msr_uncores, die, cpu); - mmio_ret =uncore_box_ref (uncore_mmio_uncores, die, cpu); +allocate_boxes (uncore_msr_uncores, die, cpu); +allocate_boxes (uncore_mmio_uncores, die, cpu);/* * Check if there is an online cpu in the package * which collects uncore events already. */ target =cpumask_any_and (&uncore_cpu_mask,topology_die_cpumask (cpu)); -if (target < nr_cpu_ids) -return 0 ; - -cpumask_set_cpu (cpu, &uncore_cpu_mask); - -if (!msr_ret) +if (target >= nr_cpu_ids) { +cpumask_set_cpu (cpu, &uncore_cpu_mask);uncore_change_context (uncore_msr_uncores,-1 , cpu); -if (!mmio_ret)uncore_change_context (uncore_mmio_uncores,-1 , cpu); -uncore_change_context (uncore_pci_uncores,-1 , cpu); +uncore_change_context (uncore_pci_uncores,-1 , cpu); + } + +uncore_box_ref (uncore_msr_uncores, die, cpu); +uncore_box_ref (uncore_mmio_uncores, die, cpu);return 0 ; }
Signature · 1 line
2.54.0
Re: [PATCH V2 7/8] perf/x86/intel/uncore: Fix uncore_box ref/unref ordering on CPU hotplug
Quoted 107 lines · Zide Chen
In
uncore_event_cpu_online(),uncore_box_ref()was called beforeuncore_change_context().uncore_box_ref()gates onbox->cpu>= 0, butbox->cpuis still -1 at that point becauseuncore_change_context()has not run yet. As a result, the box is never initialized on the first CPU to come online in a die, leaving it permanently uninitialized in the single-CPU-per-die case.Thus,
box->refcntis one count below the true value, and in the CPU offline path, the box will be torn down on the second-to-last CPU.In
uncore_event_cpu_offline(),uncore_box_unref()was called afteruncore_change_context(), sobox->cpuis already -1 when the collector CPU goes offline, which prevents it from tearing down the box.Fix by swapping the call order in both paths so that uncore_box_{ref,unref}() runs at the point where
box->cpureflects the correct context.Fixes c74443d92f68 ("perf/x86/uncore: Support per PMU cpumask")Reviewed-by Ian Rogers <[email protected]>Signed-off-by Zide Chen <[email protected]>--- arch/x86/events/intel/uncore.c | 50 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 27 deletions(-)arch/x86/events/intel/uncore.c +13
-16@@ -1577,9 +1577,15 @@ static int uncore_event_cpu_offline(unsigned int cpu) {int die, target; +/* Clear the references */ + die =topology_logical_die_id (cpu); +uncore_box_unref (uncore_msr_uncores, die); +uncore_box_unref (uncore_mmio_uncores, die); +/* Check if exiting cpu is used for collecting uncore events */ if (!cpumask_test_and_clear_cpu (cpu, &uncore_cpu_mask)) - goto unref; +return 0 ; +/* Find a new cpu to collect uncore events */ target =cpumask_any_but (topology_die_cpumask (cpu), cpu);@@ -1592,16 +1598,10 @@ static int uncore_event_cpu_offline(unsigned int cpu)uncore_change_context (uncore_msr_uncores, cpu, target);uncore_change_context (uncore_mmio_uncores, cpu, target);uncore_change_context (uncore_pci_uncores, cpu, target); - -unref: -/* Clear the references */ - die =topology_logical_die_id (cpu); -uncore_box_unref (uncore_msr_uncores, die); -uncore_box_unref (uncore_mmio_uncores, die);return 0 ; } -static int allocate_boxes (struct intel_uncore_type **types, +static void allocate_boxes (struct intel_uncore_type **types,unsigned int die,unsigned int cpu) {struct intel_uncore_box *box, *tmp;@@ -1618,8 +1618,10 @@ static int allocate_boxes(struct intel_uncore_type **types,if (pmu->boxes[die] ||uncore_pmu_broken (pmu))continue ; box =uncore_alloc_box (type,cpu_to_node (cpu)); -if (!box) +if (!box) { +uncore_pmu_set_broken (pmu); goto cleanup; + } box->pmu = pmu; box->dieid = die;list_add (&box->active_list, &allocated);@@ -1630,14 +1632,13 @@ static int allocate_boxes(struct intel_uncore_type **types,list_del_init (&box->active_list); box->pmu->boxes[die] = box; } -return 0 ; +return ; cleanup:list_for_each_entry_safe (box, tmp, &allocated, active_list) {list_del_init (&box->active_list);kfree (box); } -return -ENOMEM ; } static int uncore_box_ref(struct intel_uncore_type **types,@@ -1646,11 +1647,7 @@ static int uncore_box_ref(struct intel_uncore_type **types,struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box; -int i, ret; - - ret =allocate_boxes (types, die, cpu); -if (ret) -return ret; +int i;for (; *types; types++) { type = *types;@@ -1666,27 +1663,26 @@ static int uncore_box_ref(struct intel_uncore_type **types,
arch/x86/events/intel/uncore.c static int uncore_box_ref(struct intel_uncore_type **types, · line 1670
Quoted 10 lines
arch/x86/events/intel/uncore.c +3
-3static int uncore_event_cpu_online (unsigned int cpu) { -int die, target, msr_ret, mmio_ret; +int die, target; die =topology_logical_die_id (cpu); - msr_ret =uncore_box_ref (uncore_msr_uncores, die, cpu); - mmio_ret =uncore_box_ref (uncore_mmio_uncores, die, cpu); +allocate_boxes (uncore_msr_uncores, die, cpu); +allocate_boxes (uncore_mmio_uncores, die, cpu);
allocate_boxes() are moved to uncore_event_cpu_online() from uncore_box_ref(). It's a significant and good change since PCI uncore PMUs doesn't call allocate_boxes(), but the commit message doesn't mention this. We'd better extract this change to a separate patch which would make the changes clearer. Thanks.
Others look good to me.
Quoted 26 lines
arch/x86/events/intel/uncore.c +7
-8/* * Check if there is an online cpu in the package * which collects uncore events already. */ target =cpumask_any_and (&uncore_cpu_mask,topology_die_cpumask (cpu)); -if (target < nr_cpu_ids) -return 0 ; - -cpumask_set_cpu (cpu, &uncore_cpu_mask); - -if (!msr_ret) +if (target >= nr_cpu_ids) { +cpumask_set_cpu (cpu, &uncore_cpu_mask);uncore_change_context (uncore_msr_uncores,-1 , cpu); -if (!mmio_ret)uncore_change_context (uncore_mmio_uncores,-1 , cpu); -uncore_change_context (uncore_pci_uncores,-1 , cpu); +uncore_change_context (uncore_pci_uncores,-1 , cpu); + } + +uncore_box_ref (uncore_msr_uncores, die, cpu); +uncore_box_ref (uncore_mmio_uncores, die, cpu);return 0 ; }
Re: [PATCH V2 7/8] perf/x86/intel/uncore: Fix uncore_box ref/unref ordering on CPU hotplug
Quoted 114 lines · Dapeng
Quoted 112 lines · Zide Chen
In
uncore_event_cpu_online(),uncore_box_ref()was called beforeuncore_change_context().uncore_box_ref()gates onbox->cpu>= 0, butbox->cpuis still -1 at that point becauseuncore_change_context()has not run yet. As a result, the box is never initialized on the first CPU to come online in a die, leaving it permanently uninitialized in the single-CPU-per-die case.Thus,
box->refcntis one count below the true value, and in the CPU offline path, the box will be torn down on the second-to-last CPU.In
uncore_event_cpu_offline(),uncore_box_unref()was called afteruncore_change_context(), sobox->cpuis already -1 when the collector CPU goes offline, which prevents it from tearing down the box.Fix by swapping the call order in both paths so that uncore_box_{ref,unref}() runs at the point where
box->cpureflects the correct context.Fixes c74443d92f68 ("perf/x86/uncore: Support per PMU cpumask")Reviewed-by Ian Rogers <[email protected]>Signed-off-by Zide Chen <[email protected]>--- arch/x86/events/intel/uncore.c | 50 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 27 deletions(-)arch/x86/events/intel/uncore.c +14
-17@@ -1577,9 +1577,15 @@ static int uncore_event_cpu_offline(unsigned int cpu) {int die, target; +/* Clear the references */ + die =topology_logical_die_id (cpu); +uncore_box_unref (uncore_msr_uncores, die); +uncore_box_unref (uncore_mmio_uncores, die); +/* Check if exiting cpu is used for collecting uncore events */ if (!cpumask_test_and_clear_cpu (cpu, &uncore_cpu_mask)) - goto unref; +return 0 ; +/* Find a new cpu to collect uncore events */ target =cpumask_any_but (topology_die_cpumask (cpu), cpu);@@ -1592,16 +1598,10 @@ static int uncore_event_cpu_offline(unsigned int cpu)uncore_change_context (uncore_msr_uncores, cpu, target);uncore_change_context (uncore_mmio_uncores, cpu, target);uncore_change_context (uncore_pci_uncores, cpu, target); - -unref: -/* Clear the references */ - die =topology_logical_die_id (cpu); -uncore_box_unref (uncore_msr_uncores, die); -uncore_box_unref (uncore_mmio_uncores, die);return 0 ; } -static int allocate_boxes (struct intel_uncore_type **types, +static void allocate_boxes (struct intel_uncore_type **types,unsigned int die,unsigned int cpu) {struct intel_uncore_box *box, *tmp;@@ -1618,8 +1618,10 @@ static int allocate_boxes(struct intel_uncore_type **types,if (pmu->boxes[die] ||uncore_pmu_broken (pmu))continue ; box =uncore_alloc_box (type,cpu_to_node (cpu)); -if (!box) +if (!box) { +uncore_pmu_set_broken (pmu); goto cleanup; + } box->pmu = pmu; box->dieid = die;list_add (&box->active_list, &allocated);@@ -1630,14 +1632,13 @@ static int allocate_boxes(struct intel_uncore_type **types,list_del_init (&box->active_list); box->pmu->boxes[die] = box; } -return 0 ; +return ; cleanup:list_for_each_entry_safe (box, tmp, &allocated, active_list) {list_del_init (&box->active_list);kfree (box); } -return -ENOMEM ; } static int uncore_box_ref(struct intel_uncore_type **types,@@ -1646,11 +1647,7 @@ static int uncore_box_ref(struct intel_uncore_type **types,struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box; -int i, ret; - - ret =allocate_boxes (types, die, cpu); -if (ret) -return ret; +int i;for (; *types; types++) { type = *types;@@ -1666,27 +1663,26 @@ static int uncore_box_ref(struct intel_uncore_type **types,static int uncore_event_cpu_online (unsigned int cpu) { -int die, target, msr_ret, mmio_ret; +int die, target;
Quoted 11 lines
arch/x86/events/intel/uncore.c static int uncore_box_ref(struct intel_uncore_type **types, · line 1670
Quoted 5 lines
arch/x86/events/intel/uncore.c +2
-2die =topology_logical_die_id (cpu); - msr_ret =uncore_box_ref (uncore_msr_uncores, die, cpu); - mmio_ret =uncore_box_ref (uncore_mmio_uncores, die, cpu); +allocate_boxes (uncore_msr_uncores, die, cpu); +allocate_boxes (uncore_mmio_uncores, die, cpu);
allocate_boxes()are moved touncore_event_cpu_online()fromuncore_box_ref(). It's a significant and good change since PCI uncore PMUs doesn't callallocate_boxes(), but the commit message doesn't mention this. We'd better extract this change to a separate patch which would make the changes clearer. Thanks.
All the functions involved in this patch are not called in PCI PMUs, and the call graph is not complicated with only one or two callers for each of them.
Splitting it into two patches may be overkill; additionally, it may lose the big picture and make it harder to understand the overall flow.
Quoted 29 lines
Others look good to me.
Quoted 26 lines
arch/x86/events/intel/uncore.c +7
-8/* * Check if there is an online cpu in the package * which collects uncore events already. */ target =cpumask_any_and (&uncore_cpu_mask,topology_die_cpumask (cpu)); -if (target < nr_cpu_ids) -return 0 ; - -cpumask_set_cpu (cpu, &uncore_cpu_mask); - -if (!msr_ret) +if (target >= nr_cpu_ids) { +cpumask_set_cpu (cpu, &uncore_cpu_mask);uncore_change_context (uncore_msr_uncores,-1 , cpu); -if (!mmio_ret)uncore_change_context (uncore_mmio_uncores,-1 , cpu); -uncore_change_context (uncore_pci_uncores,-1 , cpu); +uncore_change_context (uncore_pci_uncores,-1 , cpu); + } + +uncore_box_ref (uncore_msr_uncores, die, cpu); +uncore_box_ref (uncore_mmio_uncores, die, cpu);return 0 ; }
Re: [PATCH V2 7/8] perf/x86/intel/uncore: Fix uncore_box ref/unref ordering on CPU hotplug
Quoted 125 lines · Zide
Quoted 113 lines · Dapeng
Quoted 112 lines · Zide Chen
In
uncore_event_cpu_online(),uncore_box_ref()was called beforeuncore_change_context().uncore_box_ref()gates onbox->cpu>= 0, butbox->cpuis still -1 at that point becauseuncore_change_context()has not run yet. As a result, the box is never initialized on the first CPU to come online in a die, leaving it permanently uninitialized in the single-CPU-per-die case.Thus,
box->refcntis one count below the true value, and in the CPU offline path, the box will be torn down on the second-to-last CPU.In
uncore_event_cpu_offline(),uncore_box_unref()was called afteruncore_change_context(), sobox->cpuis already -1 when the collector CPU goes offline, which prevents it from tearing down the box.Fix by swapping the call order in both paths so that uncore_box_{ref,unref}() runs at the point where
box->cpureflects the correct context.Fixes c74443d92f68 ("perf/x86/uncore: Support per PMU cpumask")Reviewed-by Ian Rogers <[email protected]>Signed-off-by Zide Chen <[email protected]>--- arch/x86/events/intel/uncore.c | 50 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 27 deletions(-)arch/x86/events/intel/uncore.c +14
-17@@ -1577,9 +1577,15 @@ static int uncore_event_cpu_offline(unsigned int cpu) {int die, target; +/* Clear the references */ + die =topology_logical_die_id (cpu); +uncore_box_unref (uncore_msr_uncores, die); +uncore_box_unref (uncore_mmio_uncores, die); +/* Check if exiting cpu is used for collecting uncore events */ if (!cpumask_test_and_clear_cpu (cpu, &uncore_cpu_mask)) - goto unref; +return 0 ; +/* Find a new cpu to collect uncore events */ target =cpumask_any_but (topology_die_cpumask (cpu), cpu);@@ -1592,16 +1598,10 @@ static int uncore_event_cpu_offline(unsigned int cpu)uncore_change_context (uncore_msr_uncores, cpu, target);uncore_change_context (uncore_mmio_uncores, cpu, target);uncore_change_context (uncore_pci_uncores, cpu, target); - -unref: -/* Clear the references */ - die =topology_logical_die_id (cpu); -uncore_box_unref (uncore_msr_uncores, die); -uncore_box_unref (uncore_mmio_uncores, die);return 0 ; } -static int allocate_boxes (struct intel_uncore_type **types, +static void allocate_boxes (struct intel_uncore_type **types,unsigned int die,unsigned int cpu) {struct intel_uncore_box *box, *tmp;@@ -1618,8 +1618,10 @@ static int allocate_boxes(struct intel_uncore_type **types,if (pmu->boxes[die] ||uncore_pmu_broken (pmu))continue ; box =uncore_alloc_box (type,cpu_to_node (cpu)); -if (!box) +if (!box) { +uncore_pmu_set_broken (pmu); goto cleanup; + } box->pmu = pmu; box->dieid = die;list_add (&box->active_list, &allocated);@@ -1630,14 +1632,13 @@ static int allocate_boxes(struct intel_uncore_type **types,list_del_init (&box->active_list); box->pmu->boxes[die] = box; } -return 0 ; +return ; cleanup:list_for_each_entry_safe (box, tmp, &allocated, active_list) {list_del_init (&box->active_list);kfree (box); } -return -ENOMEM ; } static int uncore_box_ref(struct intel_uncore_type **types,@@ -1646,11 +1647,7 @@ static int uncore_box_ref(struct intel_uncore_type **types,struct intel_uncore_type *type;struct intel_uncore_pmu *pmu;struct intel_uncore_box *box; -int i, ret; - - ret =allocate_boxes (types, die, cpu); -if (ret) -return ret; +int i;for (; *types; types++) { type = *types;@@ -1666,27 +1663,26 @@ static int uncore_box_ref(struct intel_uncore_type **types,static int uncore_event_cpu_online (unsigned int cpu) { -int die, target, msr_ret, mmio_ret; +int die, target;Quoted 10 lines
arch/x86/events/intel/uncore.c static int uncore_box_ref(struct intel_uncore_type **types, · line 1670
Quoted 5 lines
arch/x86/events/intel/uncore.c +2
-2die =topology_logical_die_id (cpu); - msr_ret =uncore_box_ref (uncore_msr_uncores, die, cpu); - mmio_ret =uncore_box_ref (uncore_mmio_uncores, die, cpu); +allocate_boxes (uncore_msr_uncores, die, cpu); +allocate_boxes (uncore_mmio_uncores, die, cpu);
allocate_boxes()are moved touncore_event_cpu_online()fromuncore_box_ref(). It's a significant and good change since PCI uncore PMUs doesn't callallocate_boxes(), but the commit message doesn't mention this. We'd better extract this change to a separate patch which would make the changes clearer. Thanks.
Quoted 6 lines
All the functions involved in this patch are not called in PCI PMUs, and the call graph is not complicated with only one or two callers for each of them.
Splitting it into two patches may be overkill; additionally, it may lose the big picture and make it harder to understand the overall flow.
Ok, if we still prefer keep them in same patch, please mention the movement of allocate_boxes() in the change log. Thanks.
Quoted 29 lines
Quoted 29 lines
Others look good to me.
Quoted 26 lines
arch/x86/events/intel/uncore.c +7
-8/* * Check if there is an online cpu in the package * which collects uncore events already. */ target =cpumask_any_and (&uncore_cpu_mask,topology_die_cpumask (cpu)); -if (target < nr_cpu_ids) -return 0 ; - -cpumask_set_cpu (cpu, &uncore_cpu_mask); - -if (!msr_ret) +if (target >= nr_cpu_ids) { +cpumask_set_cpu (cpu, &uncore_cpu_mask);uncore_change_context (uncore_msr_uncores,-1 , cpu); -if (!mmio_ret)uncore_change_context (uncore_mmio_uncores,-1 , cpu); -uncore_change_context (uncore_pci_uncores,-1 , cpu); +uncore_change_context (uncore_pci_uncores,-1 , cpu); + } + +uncore_box_ref (uncore_msr_uncores, die, cpu); +uncore_box_ref (uncore_mmio_uncores, die, cpu);return 0 ; }
PATCH v2 8/8 perf/x86/intel/uncore: Implement lazy setup for MSR/MMIO PMU
In reply to Zide ChenMSR and MMIO uncore PMUs are currently registered at module init time and appear in sysfs even when no PMU boxes are functional.
Apply the same lazy registration model used by PCI uncore PMUs: the PMU is registered when the first box is successfully initialized, and unregistered when the last box exits. If a box fails to initialize on a subsequent die, the PMU is marked broken but remains registered to avoid disrupting any in-flight perf events.
Box allocation and free remain at module init/exit time to avoid repeated kfree/alloc cycles across CPU offline/online events.
---
V2:
- uncore_box_unref(): Only decrement pmu->activeboxes for active boxes (those without init_box callback or successfully initialized) to prevent underflow when initialization fails.
- Set uncore_{msr,mmio}_uncores to empty_uncore when uncore_pmu_types_init() fails.
- Rename uncore_cpu_mmio_init() to uncore_pmu_types_init() (Dapeng).
--- arch/x86/events/intel/uncore.c | 78 +++++++--------------------------- arch/x86/events/intel/uncore.h | 6 +++ 2 files changed, 22 insertions(+), 62 deletions(-)
arch/x86/events/intel/uncore.c +16 -62
@@ -1567,8 +1567,13 @@ static void uncore_box_unref(struct intel_uncore_type **types, int die) pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { box = pmu->boxes[die]; -if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 ) +if (box && box->cpu >=0 && +atomic_dec_return (&box->refcnt) ==0 ) { +if (uncore_box_active (box) && +atomic_dec_return (&pmu->activeboxes) ==0 ) +uncore_pmu_unregister (pmu);uncore_box_exit (box); + } } } }
@@ -1655,7 +1660,7 @@ static int uncore_box_ref(struct intel_uncore_type **types,for (i =0 ; i < type->num_boxes; i++, pmu++) { box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 ) -uncore_box_init (box); +uncore_box_setup (pmu, box); } }return 0 ;
@@ -1686,67 +1691,12 @@ static int uncore_event_cpu_online(unsigned int cpu)return 0 ; } -static int __init type_pmu_register (struct intel_uncore_type *type) +static int __init uncore_pmu_types_init (struct intel_uncore_type **types) { -int i, ret; - -for (i =0 ; i < type->num_boxes; i++) { - ret =uncore_pmu_register (&type->pmus[i]); -if (ret) -return ret; - } -return 0 ; -} - -static int __init uncore_msr_pmus_register (void ) -{ -struct intel_uncore_type **types = uncore_msr_uncores; -int ret; - -for (; *types; types++) { - ret =type_pmu_register (*types); -if (ret) -return ret; - } -return 0 ; -} - -static int __init uncore_cpu_init (void ) -{ -int ret; - - ret =uncore_types_init (uncore_msr_uncores); -if (ret) - goto err; - - ret =uncore_msr_pmus_register (); -if (ret) - goto err; -return 0 ; -err: -uncore_types_exit (uncore_msr_uncores); - uncore_msr_uncores = empty_uncore; -return ret; -} - -static int __init uncore_mmio_init (void ) -{ -struct intel_uncore_type **types = uncore_mmio_uncores; -int ret; - - ret =uncore_types_init (types); +int ret =uncore_types_init (types);if (ret) - goto err; +uncore_types_exit (types); -for (; *types; types++) { - ret =type_pmu_register (*types); -if (ret) - goto err; - } -return 0 ; -err: -uncore_types_exit (uncore_mmio_uncores); - uncore_mmio_uncores = empty_uncore;return ret; }
@@ -2047,12 +1997,16 @@ static int __init intel_uncore_init(void)if (uncore_init->cpu_init) { uncore_init->cpu_init (); - cret =uncore_cpu_init (); + cret =uncore_pmu_types_init (uncore_msr_uncores); +if (cret) + uncore_msr_uncores = empty_uncore; }if (uncore_init->mmio_init) { uncore_init->mmio_init (); - mret =uncore_mmio_init (); + mret =uncore_pmu_types_init (uncore_mmio_uncores); +if (mret) + uncore_mmio_uncores = empty_uncore; }if (cret && pret && mret) {
arch/x86/events/intel/uncore.h +6 -0
@@ -568,6 +568,12 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,return box->pmu->type->ops->read_counter (box, event); } +static inline bool uncore_box_active (struct intel_uncore_box *box) +{ +return (!box->pmu->type->ops->init_box || +test_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)); +} +static inline int uncore_box_init (struct intel_uncore_box *box) {int ret =0 ;
Signature · 1 line
2.54.0
Re: [PATCH V2 8/8] perf/x86/intel/uncore: Implement lazy setup for MSR/MMIO PMU
Quoted 161 lines · Zide Chen
MSR and MMIO uncore PMUs are currently registered at module init time and appear in sysfs even when no PMU boxes are functional.
Apply the same lazy registration model used by PCI uncore PMUs: the PMU is registered when the first box is successfully initialized, and unregistered when the last box exits. If a box fails to initialize on a subsequent die, the PMU is marked broken but remains registered to avoid disrupting any in-flight perf events.
Box allocation and free remain at module init/exit time to avoid repeated kfree/alloc cycles across CPU offline/online events.
Signed-off-by Zide Chen <[email protected]>---
V2:
-uncore_box_unref(): Only decrementpmu->activeboxesfor active boxes (those withoutinit_boxcallback or successfully initialized) to prevent underflow when initialization fails.
- Set uncore_{msr,mmio}_uncores toempty_uncorewhenuncore_pmu_types_init()fails.
- Renameuncore_cpu_mmio_init()touncore_pmu_types_init()(Dapeng).--- arch/x86/events/intel/uncore.c | 78 +++++++--------------------------- arch/x86/events/intel/uncore.h | 6 +++ 2 files changed, 22 insertions(+), 62 deletions(-)arch/x86/events/intel/uncore.c +16
-62@@ -1567,8 +1567,13 @@ static void uncore_box_unref(struct intel_uncore_type **types, int die) pmu = type->pmus;for (i =0 ; i < type->num_boxes; i++, pmu++) { box = pmu->boxes[die]; -if (box && box->cpu >=0 &&atomic_dec_return (&box->refcnt) ==0 ) +if (box && box->cpu >=0 && +atomic_dec_return (&box->refcnt) ==0 ) { +if (uncore_box_active (box) && +atomic_dec_return (&pmu->activeboxes) ==0 ) +uncore_pmu_unregister (pmu);uncore_box_exit (box); + } } } }@@ -1655,7 +1660,7 @@ static int uncore_box_ref(struct intel_uncore_type **types,for (i =0 ; i < type->num_boxes; i++, pmu++) { box = pmu->boxes[die];if (box && box->cpu >=0 &&atomic_inc_return (&box->refcnt) ==1 ) -uncore_box_init (box); +uncore_box_setup (pmu, box); } }return 0 ;@@ -1686,67 +1691,12 @@ static int uncore_event_cpu_online(unsigned int cpu)return 0 ; } -static int __init type_pmu_register (struct intel_uncore_type *type) +static int __init uncore_pmu_types_init (struct intel_uncore_type **types) { -int i, ret; - -for (i =0 ; i < type->num_boxes; i++) { - ret =uncore_pmu_register (&type->pmus[i]); -if (ret) -return ret; - } -return 0 ; -} - -static int __init uncore_msr_pmus_register (void ) -{ -struct intel_uncore_type **types = uncore_msr_uncores; -int ret; - -for (; *types; types++) { - ret =type_pmu_register (*types); -if (ret) -return ret; - } -return 0 ; -} - -static int __init uncore_cpu_init (void ) -{ -int ret; - - ret =uncore_types_init (uncore_msr_uncores); -if (ret) - goto err; - - ret =uncore_msr_pmus_register (); -if (ret) - goto err; -return 0 ; -err: -uncore_types_exit (uncore_msr_uncores); - uncore_msr_uncores = empty_uncore; -return ret; -} - -static int __init uncore_mmio_init (void ) -{ -struct intel_uncore_type **types = uncore_mmio_uncores; -int ret; - - ret =uncore_types_init (types); +int ret =uncore_types_init (types);if (ret) - goto err; +uncore_types_exit (types); -for (; *types; types++) { - ret =type_pmu_register (*types); -if (ret) - goto err; - } -return 0 ; -err: -uncore_types_exit (uncore_mmio_uncores); - uncore_mmio_uncores = empty_uncore;return ret; }@@ -2047,12 +1997,16 @@ static int __init intel_uncore_init(void)if (uncore_init->cpu_init) { uncore_init->cpu_init (); - cret =uncore_cpu_init (); + cret =uncore_pmu_types_init (uncore_msr_uncores); +if (cret) + uncore_msr_uncores = empty_uncore; }if (uncore_init->mmio_init) { uncore_init->mmio_init (); - mret =uncore_mmio_init (); + mret =uncore_pmu_types_init (uncore_mmio_uncores); +if (mret) + uncore_mmio_uncores = empty_uncore; }if (cret && pret && mret) {arch/x86/events/intel/uncore.h +6
-0@@ -568,6 +568,12 @@ static inline u64 uncore_read_counter(struct intel_uncore_box *box,return box->pmu->type->ops->read_counter (box, event); } +static inline bool uncore_box_active (struct intel_uncore_box *box) +{ +return (!box->pmu->type->ops->init_box || +test_bit (UNCORE_BOX_FLAG_INITIALIZED , &box->flags)); +} +static inline int uncore_box_init (struct intel_uncore_box *box) {int ret =0 ;