[PATCH 3/7] perf: Add a helper to stop running events

From: Alexander Shishkin
Date: Thu Dec 03 2015 - 05:33:20 EST


This patch adds a helper function that stops running events without
changing their state. The use case at the moment is stopping active AUX
events while their ring buffer's AUX area is getting unmapped. Since we
know that a new AUX transaction can't be started once ring buffer's
aux_mmap_count drops to zero, the only thing we need to do is stop the
active transactions.

This does a cross-cpu call that will only look at active events that are
running on their cpus.

Signed-off-by: Alexander Shishkin <alexander.shishkin@xxxxxxxxxxxxxxx>
---
kernel/events/core.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 0d3296f600..66f835a2df 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2314,6 +2314,50 @@ void perf_event_enable(struct perf_event *event)
}
EXPORT_SYMBOL_GPL(perf_event_enable);

+static int __perf_event_stop(void *info)
+{
+ int ret = -EINVAL;
+ struct perf_event *event = info;
+ struct perf_event_context *ctx = event->ctx;
+ struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
+
+ raw_spin_lock(&ctx->lock);
+ /* scheduler had a chance to do its thing */
+ if (ctx->task && cpuctx->task_ctx != ctx)
+ goto unlock;
+
+ ret = 0;
+ if (event->state < PERF_EVENT_STATE_ACTIVE)
+ goto unlock;
+
+ perf_pmu_disable(event->pmu);
+ event->pmu->stop(event, PERF_EF_UPDATE);
+ perf_pmu_enable(event->pmu);
+
+unlock:
+ raw_spin_unlock(&ctx->lock);
+
+ return ret;
+}
+
+/*
+ * Stop an event without touching its state;
+ * useful if you know that it won't get restarted when you let go of
+ * the context lock, such as aux path in perf_mmap_close().
+ */
+static void perf_event_stop(struct perf_event *event)
+{
+ struct perf_event_context *ctx;
+
+ ctx = perf_event_ctx_lock(event);
+
+ if (remote_call_or_ctx_lock(event, __perf_event_stop, event,
+ PERF_EVENT_STATE_NONE))
+ raw_spin_unlock_irq(&event->ctx->lock);
+
+ perf_event_ctx_unlock(event, ctx);
+}
+
static int _perf_event_refresh(struct perf_event *event, int refresh)
{
/*
--
2.6.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/