Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1fc690b7 authored by Raghavendra Rao Ananta's avatar Raghavendra Rao Ananta Committed by Gerrit - the friendly Code Review server
Browse files

perf: core: Avoid race condition when releasing perf-events



The function, perf_event_release_kernel(), that's used to free the
perf events is dependent on the CPU associated to this event be
online. The function checks at the beginning: if the CPU is offline,
put it in a zombie list and return immediately. Else, proceed and
make a cross-cpu call (from perf_remove_from_context()) to complete
the functionality.

However, there's a potential chance of a race if the CPU went
offline between the initial check and the cross-cpu call. The
cross-cpu call deletes the event from the context's list, but
if the CPU is offline, this deletion doesn't happen. Later
the event is freed irrespective of this failure and the event
still exists in the list. Now, when the list is traversed, it
would try to access the memory which is freed, resulting in a
memory abort.

As a result, before calling perf_event_release_kernel(), capture
the perf's pmus_mutex lock to prevent the CPU from going offline
during the operation.

Change-Id: I20241639ea9a8dc87e5a88cf81e940b3d6cb773c
Signed-off-by: default avatarRaghavendra Rao Ananta <rananta@codeaurora.org>
parent 6154fcdf
Loading
Loading
Loading
Loading
+17 −4
Original line number Diff line number Diff line
@@ -4486,7 +4486,7 @@ static DEFINE_SPINLOCK(zombie_list_lock);
 * object, it will not preserve its functionality. Once the last 'user'
 * gives up the object, we'll destroy the thing.
 */
int perf_event_release_kernel(struct perf_event *event)
static int __perf_event_release_kernel(struct perf_event *event)
{
	struct perf_event_context *ctx = event->ctx;
	struct perf_event *child, *tmp;
@@ -4500,8 +4500,7 @@ int perf_event_release_kernel(struct perf_event *event)
	if (event->state == PERF_EVENT_STATE_ZOMBIE)
		return 0;

	if (event->cpu != -1 && !cpu_online(event->cpu) &&
		event->state == PERF_EVENT_STATE_ACTIVE) {
	if (event->cpu != -1 && per_cpu(is_hotplugging, event->cpu)) {
		event->state = PERF_EVENT_STATE_ZOMBIE;

		spin_lock(&zombie_list_lock);
@@ -4620,6 +4619,17 @@ int perf_event_release_kernel(struct perf_event *event)
	put_event(event); /* Must be the 'last' reference */
	return 0;
}

int perf_event_release_kernel(struct perf_event *event)
{
	int ret;

	mutex_lock(&pmus_lock);
	ret = __perf_event_release_kernel(event);
	mutex_unlock(&pmus_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(perf_event_release_kernel);

/*
@@ -11556,7 +11566,7 @@ static void perf_event_zombie_cleanup(unsigned int cpu)
		 * PMU expects it to be in an active state
		 */
		event->state = PERF_EVENT_STATE_ACTIVE;
		perf_event_release_kernel(event);
		__perf_event_release_kernel(event);

		spin_lock(&zombie_list_lock);
	}
@@ -11571,6 +11581,7 @@ int perf_event_start_swevents(unsigned int cpu)
	struct perf_event *event;
	int idx;

	mutex_lock(&pmus_lock);
	perf_event_zombie_cleanup(cpu);
	perf_deferred_install_in_context(cpu);

@@ -11586,6 +11597,8 @@ int perf_event_start_swevents(unsigned int cpu)
	}
	srcu_read_unlock(&pmus_srcu, idx);
	per_cpu(is_hotplugging, cpu) = false;
	mutex_unlock(&pmus_lock);

	return 0;
}