[Linux-kernel-mentees] [PATCH] perf_event: Annotate rb pointer with __rcu

Amol Grover frextrite at gmail.com
Sun Feb 9 10:25:31 UTC 2020


perf_event::rb is used in RCU context, tell sparse
about it aswell to eliminate false-positives.

Fixes the following instances of sparse error:
incompatible types in comparison expression (different address spaces)
kernel/events/core.c:5596:9
kernel/events/core.c:5302:22
kernel/events/core.c:5438:14
kernel/events/core.c:5471:14
kernel/events/core.c:5528:14
kernel/events/core.c:5614:14
kernel/events/core.c:5627:14
kernel/events/core.c:7182:13
kernel/events/ring_buffer.c:169:14
kernel/events/ring_buffer.c:169:14
kernel/events/ring_buffer.c:169:14

This introduces the following 2 new sparse errors:
kernel/events/core.c:7212:9
kernel/events/core.c:5749:31

which are fixed by using RCU primitives, rcu_dereference()
and rcu_access_pointer() on perf_event::rb.

Signed-off-by: Amol Grover <frextrite at gmail.com>
---
 include/linux/perf_event.h | 2 +-
 kernel/events/core.c       | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 6d4c22aee384..1691107d2800 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -694,7 +694,7 @@ struct perf_event {
 	struct mutex			mmap_mutex;
 	atomic_t			mmap_count;
 
-	struct ring_buffer		*rb;
+	struct ring_buffer __rcu	*rb;
 	struct list_head		rb_entry;
 	unsigned long			rcu_batches;
 	int				rcu_pending;
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 2173c23c25b4..7b9411d21165 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5746,7 +5746,7 @@ static void perf_mmap_close(struct vm_area_struct *vma)
 		 * still restart the iteration to make sure we're not now
 		 * iterating the wrong list.
 		 */
-		if (event->rb == rb)
+		if (rcu_access_pointer(event->rb) == rb)
 			ring_buffer_attach(event, NULL);
 
 		mutex_unlock(&event->mmap_mutex);
@@ -7209,7 +7209,8 @@ static void perf_pmu_output_stop(struct perf_event *event)
 
 restart:
 	rcu_read_lock();
-	list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
+	list_for_each_entry_rcu(iter, &rcu_dereference(event->rb)->event_list,
+				rb_entry) {
 		/*
 		 * For per-CPU events, we need to make sure that neither they
 		 * nor their children are running; for cpu==-1 events it's
-- 
2.24.1



More information about the Linux-kernel-mentees mailing list