Skip to content

Commit c09adab

Browse files
mrutland-armwildea01
authored andcommitted
drivers/perf: arm_pmu: split irq request from enable
For historical reasons, we lazily request and free interrupts in the arm pmu driver. This requires us to refcount use of the pmu (by way of counting the active events) in order to request/free interrupts at the correct times, which complicates the driver somewhat. The existing logic is flawed, as it only considers currently online CPUs when requesting, freeing, or managing the affinity of interrupts. Intervening hotplug events can result in erroneous IRQ affinity, online CPUs for which interrupts have not been requested, or offline CPUs whose interrupts are still requested. To fix this, this patch splits the requesting of interrupts from any per-cpu management (i.e. per-cpu enable/disable, and configuration of cpu affinity). We now request all interrupts up-front at probe time (and never free them, since we never unregister PMUs). The management of affinity, and per-cpu enable/disable now happens in our cpu hotplug callback, ensuring it occurs consistently. This means that we must now invoke the CPU hotplug callback at boot time in order to configure IRQs, and since the callback also resets the PMU hardware, we can remove the duplicate reset in the probe path. This rework renders our event refcounting unnecessary, so this is removed. Signed-off-by: Mark Rutland <mark.rutland@arm.com> [will: make armpmu_get_cpu_irq static] Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 7ed98e0 commit c09adab

2 files changed

Lines changed: 50 additions & 107 deletions

File tree

‎drivers/perf/arm_pmu.c‎

Lines changed: 50 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -352,37 +352,6 @@ static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
352352
return ret;
353353
}
354354

355-
static void
356-
armpmu_release_hardware(struct arm_pmu *armpmu)
357-
{
358-
armpmu->free_irq(armpmu);
359-
}
360-
361-
static int
362-
armpmu_reserve_hardware(struct arm_pmu *armpmu)
363-
{
364-
int err = armpmu->request_irq(armpmu, armpmu_dispatch_irq);
365-
if (err) {
366-
armpmu_release_hardware(armpmu);
367-
return err;
368-
}
369-
370-
return 0;
371-
}
372-
373-
static void
374-
hw_perf_event_destroy(struct perf_event *event)
375-
{
376-
struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
377-
atomic_t *active_events = &armpmu->active_events;
378-
struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
379-
380-
if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
381-
armpmu_release_hardware(armpmu);
382-
mutex_unlock(pmu_reserve_mutex);
383-
}
384-
}
385-
386355
static int
387356
event_requires_mode_exclusion(struct perf_event_attr *attr)
388357
{
@@ -455,8 +424,6 @@ __hw_perf_event_init(struct perf_event *event)
455424
static int armpmu_event_init(struct perf_event *event)
456425
{
457426
struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
458-
int err = 0;
459-
atomic_t *active_events = &armpmu->active_events;
460427

461428
/*
462429
* Reject CPU-affine events for CPUs that are of a different class to
@@ -476,26 +443,7 @@ static int armpmu_event_init(struct perf_event *event)
476443
if (armpmu->map_event(event) == -ENOENT)
477444
return -ENOENT;
478445

479-
event->destroy = hw_perf_event_destroy;
480-
481-
if (!atomic_inc_not_zero(active_events)) {
482-
mutex_lock(&armpmu->reserve_mutex);
483-
if (atomic_read(active_events) == 0)
484-
err = armpmu_reserve_hardware(armpmu);
485-
486-
if (!err)
487-
atomic_inc(active_events);
488-
mutex_unlock(&armpmu->reserve_mutex);
489-
}
490-
491-
if (err)
492-
return err;
493-
494-
err = __hw_perf_event_init(event);
495-
if (err)
496-
hw_perf_event_destroy(event);
497-
498-
return err;
446+
return __hw_perf_event_init(event);
499447
}
500448

501449
static void armpmu_enable(struct pmu *pmu)
@@ -555,9 +503,6 @@ static struct attribute_group armpmu_common_attr_group = {
555503

556504
static void armpmu_init(struct arm_pmu *armpmu)
557505
{
558-
atomic_set(&armpmu->active_events, 0);
559-
mutex_init(&armpmu->reserve_mutex);
560-
561506
armpmu->pmu = (struct pmu) {
562507
.pmu_enable = armpmu_enable,
563508
.pmu_disable = armpmu_disable,
@@ -601,21 +546,7 @@ int perf_num_counters(void)
601546
}
602547
EXPORT_SYMBOL_GPL(perf_num_counters);
603548

604-
static void cpu_pmu_enable_percpu_irq(void *data)
605-
{
606-
int irq = *(int *)data;
607-
608-
enable_percpu_irq(irq, IRQ_TYPE_NONE);
609-
}
610-
611-
static void cpu_pmu_disable_percpu_irq(void *data)
612-
{
613-
int irq = *(int *)data;
614-
615-
disable_percpu_irq(irq);
616-
}
617-
618-
static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
549+
static void cpu_pmu_free_irqs(struct arm_pmu *cpu_pmu)
619550
{
620551
int cpu;
621552
struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
@@ -626,10 +557,7 @@ static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
626557
continue;
627558

628559
if (irq_is_percpu(irq)) {
629-
on_each_cpu_mask(&cpu_pmu->supported_cpus,
630-
cpu_pmu_disable_percpu_irq, &irq, 1);
631560
free_percpu_irq(irq, &hw_events->percpu_pmu);
632-
633561
break;
634562
}
635563

@@ -640,7 +568,7 @@ static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
640568
}
641569
}
642570

643-
static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
571+
static int cpu_pmu_request_irqs(struct arm_pmu *cpu_pmu, irq_handler_t handler)
644572
{
645573
int cpu, err;
646574
struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
@@ -656,25 +584,9 @@ static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
656584
if (err) {
657585
pr_err("unable to request IRQ%d for ARM PMU counters\n",
658586
irq);
659-
return err;
660587
}
661588

662-
on_each_cpu_mask(&cpu_pmu->supported_cpus,
663-
cpu_pmu_enable_percpu_irq, &irq, 1);
664-
665-
break;
666-
}
667-
668-
/*
669-
* If we have a single PMU interrupt that we can't shift,
670-
* assume that we're running on a uniprocessor machine and
671-
* continue. Otherwise, continue without this interrupt.
672-
*/
673-
if (irq_set_affinity(irq, cpumask_of(cpu)) &&
674-
num_possible_cpus() > 1) {
675-
pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
676-
irq, cpu);
677-
continue;
589+
return err;
678590
}
679591

680592
err = request_irq(irq, handler,
@@ -692,6 +604,12 @@ static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
692604
return 0;
693605
}
694606

607+
static int armpmu_get_cpu_irq(struct arm_pmu *pmu, int cpu)
608+
{
609+
struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
610+
return per_cpu(hw_events->irq, cpu);
611+
}
612+
695613
/*
696614
* PMU hardware loses all context when a CPU goes offline.
697615
* When a CPU is hotplugged back in, since some hardware registers are
@@ -701,11 +619,42 @@ static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
701619
static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
702620
{
703621
struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
622+
int irq;
704623

705624
if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
706625
return 0;
707626
if (pmu->reset)
708627
pmu->reset(pmu);
628+
629+
irq = armpmu_get_cpu_irq(pmu, cpu);
630+
if (irq) {
631+
if (irq_is_percpu(irq)) {
632+
enable_percpu_irq(irq, IRQ_TYPE_NONE);
633+
return 0;
634+
}
635+
636+
if (irq_force_affinity(irq, cpumask_of(cpu)) &&
637+
num_possible_cpus() > 1) {
638+
pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
639+
irq, cpu);
640+
}
641+
}
642+
643+
return 0;
644+
}
645+
646+
static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)
647+
{
648+
struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
649+
int irq;
650+
651+
if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
652+
return 0;
653+
654+
irq = armpmu_get_cpu_irq(pmu, cpu);
655+
if (irq && irq_is_percpu(irq))
656+
disable_percpu_irq(irq);
657+
709658
return 0;
710659
}
711660

@@ -811,23 +760,19 @@ static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
811760
{
812761
int err;
813762

814-
err = cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
815-
&cpu_pmu->node);
763+
err = cpu_pmu_request_irqs(cpu_pmu, armpmu_dispatch_irq);
764+
if (err)
765+
goto out;
766+
767+
err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_STARTING,
768+
&cpu_pmu->node);
816769
if (err)
817770
goto out;
818771

819772
err = cpu_pm_pmu_register(cpu_pmu);
820773
if (err)
821774
goto out_unregister;
822775

823-
cpu_pmu->request_irq = cpu_pmu_request_irq;
824-
cpu_pmu->free_irq = cpu_pmu_free_irq;
825-
826-
/* Ensure the PMU has sane values out of reset. */
827-
if (cpu_pmu->reset)
828-
on_each_cpu_mask(&cpu_pmu->supported_cpus, cpu_pmu->reset,
829-
cpu_pmu, 1);
830-
831776
/*
832777
* This is a CPU PMU potentially in a heterogeneous configuration (e.g.
833778
* big.LITTLE). This is not an uncore PMU, and we have taken ctx
@@ -842,6 +787,7 @@ static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
842787
cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
843788
&cpu_pmu->node);
844789
out:
790+
cpu_pmu_free_irqs(cpu_pmu);
845791
return err;
846792
}
847793

@@ -1122,7 +1068,8 @@ static int arm_pmu_hp_init(void)
11221068

11231069
ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_STARTING,
11241070
"perf/arm/pmu:starting",
1125-
arm_perf_starting_cpu, NULL);
1071+
arm_perf_starting_cpu,
1072+
arm_perf_teardown_cpu);
11261073
if (ret)
11271074
pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
11281075
ret);

‎include/linux/perf/arm_pmu.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,8 @@ struct arm_pmu {
105105
void (*start)(struct arm_pmu *);
106106
void (*stop)(struct arm_pmu *);
107107
void (*reset)(void *);
108-
int (*request_irq)(struct arm_pmu *, irq_handler_t handler);
109-
void (*free_irq)(struct arm_pmu *);
110108
int (*map_event)(struct perf_event *event);
111109
int num_events;
112-
atomic_t active_events;
113-
struct mutex reserve_mutex;
114110
u64 max_period;
115111
bool secure_access; /* 32-bit ARM only */
116112
#define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40

0 commit comments

Comments
 (0)