Re: [PATCH 7/7] perf/x86/intel/uncore: Implement lazy setup for MSR/MMIO PMU
From: Ian Rogers
Date: Tue May 12 2026 - 20:34:26 EST
On Tue, May 12, 2026 at 4:39 PM Zide Chen <zide.chen@xxxxxxxxx> wrote:
>
> 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 <zide.chen@xxxxxxxxx>
Reviewed-by: Ian Rogers <irogers@xxxxxxxxxx>
Thanks,
Ian
> ---
> arch/x86/events/intel/uncore.c | 72 ++++++----------------------------
> 1 file changed, 12 insertions(+), 60 deletions(-)
>
> diff --git a/arch/x86/events/intel/uncore.c b/arch/x86/events/intel/uncore.c
> index 399f434e1a7d..2aaac0b49bb6 100644
> --- a/arch/x86/events/intel/uncore.c
> +++ b/arch/x86/events/intel/uncore.c
> @@ -1564,8 +1564,11 @@ static void uncore_box_unref(struct intel_uncore_type **types, int die)
> for (i = 0; i < type->num_boxes; i++, pmu++) {
> box = pmu->boxes[die];
> if (box && box->cpu >= 0 &&
> - atomic_dec_return(&box->cpu_refcnt) == 0)
> + atomic_dec_return(&box->cpu_refcnt) == 0) {
> + if (atomic_dec_return(&pmu->die_refcnt) == 0)
> + uncore_pmu_unregister(pmu);
> uncore_box_exit(box);
> + }
> }
> }
> }
> @@ -1659,7 +1662,7 @@ static int uncore_box_ref(struct intel_uncore_type **types,
> box = pmu->boxes[die];
> if (box && box->cpu >= 0 &&
> atomic_inc_return(&box->cpu_refcnt) == 1)
> - uncore_box_init(box);
> + uncore_box_setup(pmu, box);
> }
> }
> return 0;
> @@ -1690,67 +1693,16 @@ 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_cpu_mmio_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);
> - if (ret)
> - goto err;
> + if (!ret)
> + return 0;
>
> - 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;
> + uncore_types_exit(types);
> + types = empty_uncore;
> return ret;
> }
>
> @@ -2052,12 +2004,12 @@ static int __init intel_uncore_init(void)
>
> if (uncore_init->cpu_init) {
> uncore_init->cpu_init();
> - cret = uncore_cpu_init();
> + cret = uncore_cpu_mmio_init(uncore_msr_uncores);
> }
>
> if (uncore_init->mmio_init) {
> uncore_init->mmio_init();
> - mret = uncore_mmio_init();
> + mret = uncore_cpu_mmio_init(uncore_mmio_uncores);
> }
>
> if (cret && pret && mret) {
> --
> 2.54.0
>