perf/core: Check return value of the perf_event_read() IPI

This change “perf/core: Check return value of the perf_event_read() IPI” in Linux kernel is authored by David Carrillo-Cisneros <davidcc [at]> on Wed Aug 17 13:55:04 2016 -0700.

perf/core: Check return value of the perf_event_read() IPI

The call to smp_call_function_single in perf_event_read() may fail if
an invalid or not online CPU index is passed. Warn user if such bug is
present and return error.

Signed-off-by: David Carrillo-Cisneros <>
Signed-off-by: Peter Zijlstra (Intel) <>
Cc: Alexander Shishkin <>
Cc: Arnaldo Carvalho de Melo <>
Cc: Jiri Olsa <>
Cc: Kan Liang <>
Cc: Linus Torvalds <>
Cc: Paul Turner <>
Cc: Peter Zijlstra <>
Cc: Stephane Eranian <>
Cc: Thomas Gleixner <>
Cc: Vegard Nossum <>
Cc: Vince Weaver <>
Signed-off-by: Ingo Molnar <>

This Linux change may have been applied to various maintained Linux releases and you can find Linux releases including commit 71e7bc2.

There are 7 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 kernel/events/core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index a5fc5c8..5650f53 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3549,9 +3549,10 @@ static int perf_event_read(struct perf_event *event, bool group)
 			.group = group,
 			.ret = 0,
-		smp_call_function_single(event->oncpu,
-					 __perf_event_read, &data, 1);
-		ret = data.ret;
+		ret = smp_call_function_single(event->oncpu, __perf_event_read, &data, 1);
+		/* The event must have been read from an online CPU: */
+		WARN_ON_ONCE(ret);
+		ret = ret ? : data.ret;
 	} else if (event->state == PERF_EVENT_STATE_INACTIVE) {
 		struct perf_event_context *ctx = event->ctx;
 		unsigned long flags;

The commit for this change in Linux stable tree is 71e7bc2 (patch).

Leave a Reply

Your email address will not be published. Required fields are marked *