Skip to contentSkip to conversation

arm_pmu: Use NMI for perf interrupt

PATCH New Julien Thierry 2 participants 6 messages Jan 17, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover arm_pmu: Use NMI for perf interrupt

This series is base on the "arm64: provide pseudo NMI with GICv3" patchset [1].

The aim is just to use a Pseudo-NMI for the PMUv3 interrupt.

- Patch 1 and 2 ensure no locks are taken when handling a PMU interrupt
- Patch 3 sets the NMI property for the interrupt.

The series was tested with the perf fuzzer

Here are some results when running:

$ perf record -a hackbench 200 process 1000

Without NMI:

# Overhead  Command          Symbol
# ........  ...............  ...................................
#
    12.07%  hackbench        [k] _raw_spin_unlock_irqrestore
     6.34%  hackbench        [k] el0_svc_naked
     5.53%  hackbench        [k] unix_stream_read_generic
     4.61%  hackbench        [k] __arch_copy_to_user
     3.38%  hackbench        [k] kmem_cache_free
     3.30%  hackbench        [k] refcount_inc_not_zero
     3.22%  hackbench        [.] 0x0000000000005258
     3.11%  hackbench        [k] skb_release_data
     2.92%  hackbench        [k] __lock_text_start
     2.52%  hackbench        [.] 0x00000000000052c8
     2.40%  hackbench        [k] skb_set_owner_w
     2.29%  hackbench        [k] __slab_alloc.isra.24
     2.22%  hackbench        [k] kfree
     2.14%  hackbench        [k] _raw_spin_unlock_irq
     2.03%  hackbench        [k] get_mem_cgroup_from_mm
     2.01%  hackbench        [k] __ll_sc___cmpxchg_double_mb
     1.88%  hackbench        [k] __ll_sc___cmpxchg_case_4
     1.72%  hackbench        [k] sock_alloc_send_pskb
     1.66%  hackbench        [k] __ll_sc___cmpxchg_case_rel_4
     1.62%  hackbench        [k] unix_stream_sendmsg
     1.59%  hackbench        [k] __slab_free
     1.47%  hackbench        [k] _raw_spin_unlock
     1.35%  hackbench        [k] __fget_light
     1.34%  hackbench        [k] __ll_sc___cmpxchg_double
     1.30%  hackbench        [k] memcg_kmem_put_cache
     1.20%  hackbench        [k] __kmalloc_node_track_caller
     1.13%  hackbench        [k] fsnotify
     1.07%  hackbench        [k] kmem_cache_alloc_node
     1.03%  hackbench        [k] vfs_write

With NMI:

# Overhead  Command          Symbol
# ........  ...............  ...................................
#
     8.05%  hackbench	     [k] _raw_spin_unlock_irqrestore
     6.22%  hackbench        [k] el0_svc_naked
     5.42%  hackbench        [k] unix_stream_read_generic
     4.44%  hackbench        [k] __arch_copy_to_user
     3.26%  hackbench        [k] __lock_text_start
     3.20%  hackbench        [k] kmem_cache_free
     3.10%  hackbench        [k] refcount_inc_not_zero
     2.86%  hackbench        [.] 0x0000000000005258
     2.71%  hackbench        [k] __ll_sc___cmpxchg_double_mb
     2.61%  hackbench        [k] skb_release_data
     2.51%  hackbench        [k] _raw_spin_lock_irqsave
     2.45%  hackbench        [k] kfree
     2.24%  hackbench        [.] 0x00000000000052c8
     2.15%  hackbench        [k] skb_set_owner_w
     1.98%  hackbench        [k] get_mem_cgroup_from_mm
     1.97%  hackbench        [k] _raw_spin_unlock
     1.86%  hackbench        [k] __ll_sc___cmpxchg_case_4
     1.65%  hackbench        [k] sock_alloc_send_pskb
     1.62%  hackbench        [k] __ll_sc___cmpxchg_case_rel_4
     1.58%  hackbench        [k] unix_stream_sendmsg
     1.56%  hackbench        [k] __slab_free
     1.31%  hackbench        [k] __kmalloc_node_track_caller
     1.30%  hackbench        [k] memcg_kmem_put_cache
     1.25%  hackbench        [k] __ll_sc___cmpxchg_double
     1.23%  hackbench        [k] __fget_light
     1.15%  hackbench        [k] kmem_cache_alloc_node
     1.08%  hackbench        [k] fsnotify
     1.04%  hackbench        [k] sock_def_readable

Function _raw_spin_unlock_irqrestore gets a nice offload because perf interrupts are less likely to be triggered right when that function reenables interrupts. Also, interrupt handling code can now be profiled (except for Pseudo-NMI handlers of course).

[1] https://www.spinics.net/lists/arm-kernel/msg629392.html

Cheers,

Julien

-->

Julien Thierry (3):
arm64: perf: Make PMU interrupt NMI safe arm/arm64: kvm: pmu: Make overflow handler NMI safe
arm64: perf: Set PMU interrupt as NMI

 arch/arm64/kernel/perf_event.c | 41 +++++++++--------------------------------
 drivers/perf/arm_pmu.c         |  9 +++++++--
 include/kvm/arm_pmu.h          |  1 +
 include/linux/perf/arm_pmu.h   |  6 ------
 virt/kvm/arm/pmu.c             | 37 +++++++++++++++++++++++++++++++++----
 5 files changed, 50 insertions(+), 44 deletions(-)

--
1.9.1

#1 lore raw

PATCH 1/3 arm64: perf: Make PMU interrupt NMI safe

Before using an NMI, we must make sure the IRQ handler uses no locks and only calls NMI safe functions.

Get rid of PMU lock, saving and restoring the PMU counter selector in the PMU interrupt handler
When PMU interrupt is an NMI do not call irq_work_run and rely on the IRQ work IPI to run the irq_work queue.

Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 arch/arm64/kernel/perf_event.c | 41 +++++++++--------------------------------
 drivers/perf/arm_pmu.c         |  1 -
 include/linux/perf/arm_pmu.h   |  6 ------
 3 files changed, 9 insertions(+), 39 deletions(-)
arch/arm64/kernel/perf_event.c +9 -32
index 3affca3..57fdd87 100644
@@ -609,19 +609,10 @@ static inline u32 armv8pmu_getreset_flags(void)
 
 static void armv8pmu_enable_event(struct perf_event *event)
 {
-	unsigned long flags;
 	struct hw_perf_event *hwc = &event->hw;
-	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
-	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
 	int idx = hwc->idx;
 
 	/*
-	 * Enable counter and interrupt, and set the counter to count
-	 * the event that we're interested in.
-	 */
-	raw_spin_lock_irqsave(&events->pmu_lock, flags);
-
-	/*
 	 * Disable counter
 	 */
 	armv8pmu_disable_counter(idx);
@@ -640,24 +631,14 @@ static void armv8pmu_enable_event(struct perf_event *event)
 	 * Enable counter
 	 */
 	armv8pmu_enable_counter(idx);
-
-	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
 }
 
 static void armv8pmu_disable_event(struct perf_event *event)
 {
-	unsigned long flags;
 	struct hw_perf_event *hwc = &event->hw;
-	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
-	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
 	int idx = hwc->idx;
 
 	/*
-	 * Disable counter and interrupt
-	 */
-	raw_spin_lock_irqsave(&events->pmu_lock, flags);
-
-	/*
 	 * Disable counter
 	 */
 	armv8pmu_disable_counter(idx);
@@ -666,8 +647,6 @@ static void armv8pmu_disable_event(struct perf_event *event)
 	 * Disable interrupt for this counter
 	 */
 	armv8pmu_disable_intens(idx);
-
-	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
 }
 
 static irqreturn_t armv8pmu_handle_irq(int irq_num, void *dev)
@@ -678,6 +657,7 @@ static irqreturn_t armv8pmu_handle_irq(int irq_num, void *dev)
 	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
 	struct pt_regs *regs;
 	int idx;
+	u32 pmselr_save = read_sysreg(pmselr_el0);
 
 	/*
 	 * Get and reset the IRQ flags
@@ -721,37 +701,34 @@ static irqreturn_t armv8pmu_handle_irq(int irq_num, void *dev)
 	}
 
 	/*
+	 * Restore selector registor as it was potentially used in the
+	 * interrupted context.
+	 */
+	write_sysreg(pmselr_save, pmselr_el0);
+
+	/*
 	 * Handle the pending perf events.
 	 *
 	 * Note: this call *must* be run with interrupts disabled. For
 	 * platforms that can have the PMU interrupts raised as an NMI, this
 	 * will not work.
 	 */
-	irq_work_run();
+	if (!in_nmi())
+		irq_work_run();
 
 	return IRQ_HANDLED;
 }
 
 static void armv8pmu_start(struct arm_pmu *cpu_pmu)
 {
-	unsigned long flags;
-	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
-
-	raw_spin_lock_irqsave(&events->pmu_lock, flags);
 	/* Enable all counters */
 	armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
-	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
 }
 
 static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
 {
-	unsigned long flags;
-	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
-
-	raw_spin_lock_irqsave(&events->pmu_lock, flags);
 	/* Disable all counters */
 	armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
-	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
 }
 
 static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
drivers/perf/arm_pmu.c +0 -1
index 7bc5eee..40ad36f 100644
@@ -845,7 +845,6 @@ struct arm_pmu *armpmu_alloc(void)
 		struct pmu_hw_events *events;
 
 		events = per_cpu_ptr(pmu->hw_events, cpu);
-		raw_spin_lock_init(&events->pmu_lock);
 		events->percpu_pmu = pmu;
 	}
 
include/linux/perf/arm_pmu.h +0 -6
index af0f44ef..29efdab 100644
@@ -69,12 +69,6 @@ struct pmu_hw_events {
 	DECLARE_BITMAP(used_mask, ARMPMU_MAX_HWEVENTS);
 
 	/*
-	 * Hardware lock to serialize accesses to PMU registers. Needed for the
-	 * read/modify/write sequences.
-	 */
-	raw_spinlock_t		pmu_lock;
-
-	/*
 	 * When using percpu IRQs, we need a percpu dev_id. Place it here as we
 	 * already have to allocate this struct per cpu.
 	 */

--
1.9.1

maintainer
#2 lore raw

PATCH 1/3 arm64: perf: Make PMU interrupt NMI safe

Quoted 5 lines · Julien Thierry

Before using an NMI, we must make sure the IRQ handler uses no locks and only calls NMI safe functions.

Get rid of PMU lock, saving and restoring the PMU counter selector in the PMU interrupt handler

You fail to explain why its safe to do so. Presumably that lock was there for a reason, why can you remove it and expect things to keep working?

#3 lore raw

PATCH 1/3 arm64: perf: Make PMU interrupt NMI safe

Quoted 11 lines · Peter Zijlstra
Quoted 5 lines · Julien Thierry

Before using an NMI, we must make sure the IRQ handler uses no locks and only calls NMI safe functions.

Get rid of PMU lock, saving and restoring the PMU counter selector in the PMU interrupt handler

You fail to explain why its safe to do so. Presumably that lock was there for a reason, why can you remove it and expect things to keep working?

Right, this was poorly worded. The lock was there to prevent the PMU interrupt modifying the counter selector register while the interrupted context was using it.

So the lock was not simply removed, but replaced with the saving and restoring the selector register value in the interrupt handler.

I'll reword that when I submit a new iteration of that series.

Thanks,

Signature · 1 line
Julien Thierry
#4 lore raw

PATCH 2/3 arm/arm64: kvm: pmu: Make overflow handler NMI safe

In reply to Julien Thierry

When using an NMI for the PMU interrupt, taking any lock migh cause a deadlock. The current PMU overflow handler in KVM takes takes locks when trying to wake up a vcpu.

When overflow handler is called by an NMI, defer the vcpu waking in an irq_work queue.

---
 include/kvm/arm_pmu.h |  1 +
 virt/kvm/arm/pmu.c    | 37 +++++++++++++++++++++++++++++++++----
 2 files changed, 34 insertions(+), 4 deletions(-)
include/kvm/arm_pmu.h +1 -0
index f87fe20..6a7c9dd 100644
@@ -37,6 +37,7 @@ struct kvm_pmu {
 	bool ready;
 	bool created;
 	bool irq_level;
+	struct irq_work overflow_work;
 };
 
 #define kvm_arm_pmu_v3_ready(v)		((v)->arch.pmu.ready)
virt/kvm/arm/pmu.c +33 -4
index 8a9c423..c5cdeea 100644
@@ -273,15 +273,37 @@ void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
 	kvm_pmu_update_state(vcpu);
 }
 
+static inline struct kvm_vcpu *kvm_pmu_to_vcpu(struct kvm_pmu *pmu)
+{
+	struct kvm_vcpu_arch *vcpu_arch;
+
+	vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu);
+	return container_of(vcpu_arch, struct kvm_vcpu, arch);
+}
+
 static inline struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc)
 {
 	struct kvm_pmu *pmu;
-	struct kvm_vcpu_arch *vcpu_arch;
 
 	pmc -= pmc->idx;
 	pmu = container_of(pmc, struct kvm_pmu, pmc[0]);
-	vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu);
-	return container_of(vcpu_arch, struct kvm_vcpu, arch);
+	return kvm_pmu_to_vcpu(pmu);
+}
+
+/**
+ * When perf interrupt is an NMI, we cannot safely notify the vcpu corresponding
+ * to the even.
+ * This is why we need a callback to do it once outside of the NMI context.
+ */
+static void kvm_pmu_perf_overflow_notify_vcpu(struct irq_work *work)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_pmu *pmu;
+
+	pmu = container_of(work, struct kvm_pmu, overflow_work);
+	vcpu = kvm_pmu_to_vcpu(pmu);
+
+	kvm_vcpu_kick(vcpu);
 }
 
 /**
@@ -299,7 +321,11 @@ static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
 
 	if (kvm_pmu_overflow_status(vcpu)) {
 		kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
-		kvm_vcpu_kick(vcpu);
+
+		if (!in_nmi())
+			kvm_vcpu_kick(vcpu);
+		else
+			irq_work_queue(&vcpu->arch.pmu.overflow_work);
 	}
 }
 
@@ -501,6 +527,9 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
 			return ret;
 	}
 
+	init_irq_work(&vcpu->arch.pmu.overflow_work,
+		      kvm_pmu_perf_overflow_notify_vcpu);
+
 	vcpu->arch.pmu.created = true;
 	return 0;
 }

--
1.9.1

#5 lore raw

PATCH 3/3 arm64: perf: Set PMU interrupt as NMI

In reply to Julien Thierry

If pseudo-NMIs are supported by the kernel, treat PMU interrupt as such.

Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
---
 drivers/perf/arm_pmu.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/perf/arm_pmu.c +7 -1
index 40ad36f..c18d425 100644
@@ -545,6 +545,7 @@ void armpmu_free_irq(struct arm_pmu *armpmu, int cpu)
 		return;
 	}
 
+	irq_set_irqchip_state(irq, IRQCHIP_STATE_NMI, false);
 	free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
 }
 
@@ -597,6 +598,7 @@ int armpmu_request_irq(struct arm_pmu *armpmu, int cpu)
 				    IRQF_NO_THREAD;
 		}
 
+		irq_set_irqchip_state(irq, IRQCHIP_STATE_NMI, true);
 		err = request_irq(irq, handler, irq_flags, "arm-pmu",
 				  per_cpu_ptr(&hw_events->percpu_pmu, cpu));
 	}
@@ -609,6 +611,7 @@ int armpmu_request_irq(struct arm_pmu *armpmu, int cpu)
 
 err_out:
 	pr_err("unable to request IRQ%d for ARM PMU counters\n", irq);
+	irq_set_irqchip_state(irq, IRQCHIP_STATE_NMI, false);
 	return err;
 }
 
@@ -650,6 +653,7 @@ static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
 	irq = armpmu_get_cpu_irq(pmu, cpu);
 	if (irq) {
 		if (irq_is_percpu_devid(irq)) {
+			irq_set_irqchip_state(irq, IRQCHIP_STATE_NMI, true);
 			enable_percpu_irq(irq, IRQ_TYPE_NONE);
 			return 0;
 		}
@@ -667,8 +671,10 @@ static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)
 		return 0;
 
 	irq = armpmu_get_cpu_irq(pmu, cpu);
-	if (irq && irq_is_percpu_devid(irq))
+	if (irq && irq_is_percpu_devid(irq)) {
 		disable_percpu_irq(irq);
+		irq_set_irqchip_state(irq, IRQCHIP_STATE_NMI, false);
+	}
 
 	return 0;
 }

--
1.9.1