Skip to contentSkip to conversation

perf: Force USER_DS when recording user stack data.

PATCH In review Yabin Cui 3 participants 3 messages Aug 23, 2018 – Sep 10, 2018

Subsystems (2)
lore
Share & tools
lore

Yabin Cui

author
#0 lore raw

PATCH 1/1 perf: Force USER_DS when recording user stack data.

Perf can record user stack data in response to a synchronous request, such as a tracepoint firing. If this happens under set_fs(KERNEL_DS), then we end up reading user stack data using __copy_from_user_inatomic() under set_fs(KERNEL_DS). I think this conflicts with the intention of using set_fs(KERNEL_DS). And it is explicitly forbidden by hardware on ARM64 when both CONFIG_ARM64_UAO and CONFIG_ARM64_PAN are used.

So fix this by forcing USER_DS when recording user stack data.

---
 kernel/events/core.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/events/core.c +4 -0
index 2a62b96600ad..9bc047421e75 100644
@@ -5948,6 +5948,7 @@ perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
 		unsigned long sp;
 		unsigned int rem;
 		u64 dyn_size;
+		mm_segment_t fs;
 
 		/*
 		 * We dump:
@@ -5965,7 +5966,10 @@ perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
 
 		/* Data. */
 		sp = perf_user_stack_pointer(regs);
+		fs = get_fs();
+		set_fs(USER_DS);
 		rem = __output_copy_user(handle, (void *) sp, dump_size);
+		set_fs(fs);
 		dyn_size = dump_size - rem;
 
 		perf_output_skip(handle, rem);
Signature · 1 line
2.19.0.rc0.228.g281dcd1b4d0-goog
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Yabin Cui

Perf can record user stack data in response to a synchronous request, such as a tracepoint firing. If this happens under set_fs(KERNEL_DS), then we end up reading user stack data using __copy_from_user_inatomic() under set_fs(KERNEL_DS). I think this conflicts with the intention of using set_fs(KERNEL_DS). And it is explicitly forbidden by hardware on ARM64 when both CONFIG_ARM64_UAO and CONFIG_ARM64_PAN are used.

So fix this by forcing USER_DS when recording user stack data.

Ingo, I think this wants a stable tag too; seems to be a corrolary of:

88b0193d9418 ("perf/callchain: Force USER_DS when invoking perf_callchain_user()")

Quoted 30 lines
---
 kernel/events/core.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/events/core.c +4 -0
index 2a62b96600ad..9bc047421e75 100644
@@ -5948,6 +5948,7 @@ perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
 		unsigned long sp;
 		unsigned int rem;
 		u64 dyn_size;
+		mm_segment_t fs;
 
 		/*
 		 * We dump:
@@ -5965,7 +5966,10 @@ perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
 
 		/* Data. */
 		sp = perf_user_stack_pointer(regs);
+		fs = get_fs();
+		set_fs(USER_DS);
 		rem = __output_copy_user(handle, (void *) sp, dump_size);
+		set_fs(fs);
 		dyn_size = dump_size - rem;
 
 		perf_output_skip(handle, rem);
Signature · 1 line
2.19.0.rc0.228.g281dcd1b4d0-goog
#2 lore raw

[tip:perf/urgent] perf/core: Force USER_DS when recording user stack data

In reply to Yabin Cui
perf/core: Force USER_DS when recording user stack data
Commit-ID:  02e184476eff848273826c1d6617bb37e5bcc7ad
Gitweb:     https://git.kernel.org/tip/02e184476eff848273826c1d6617bb37e5bcc7ad
Author:     Yabin Cui <[email protected]>
AuthorDate: Thu, 23 Aug 2018 15:59:35 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 10 Sep 2018 14:01:46 +0200

perf/core: Force USER_DS when recording user stack data

Perf can record user stack data in response to a synchronous request, such as a tracepoint firing. If this happens under set_fs(KERNEL_DS), then we end up reading user stack data using __copy_from_user_inatomic() under set_fs(KERNEL_DS). I think this conflicts with the intention of using set_fs(KERNEL_DS). And it is explicitly forbidden by hardware on ARM64 when both CONFIG_ARM64_UAO and CONFIG_ARM64_PAN are used.

So fix this by forcing USER_DS when recording user stack data.

Cc Alexander Shishkin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Fixes 88b0193d9418 ("perf/callchain: Force USER_DS when invoking perf_callchain_user()")
---
 kernel/events/core.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/events/core.c +4 -0
index abaed4f8bb7f..c80549bf82c6 100644
@@ -5943,6 +5943,7 @@ perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
 		unsigned long sp;
 		unsigned int rem;
 		u64 dyn_size;
+		mm_segment_t fs;
 
 		/*
 		 * We dump:
@@ -5960,7 +5961,10 @@ perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
 
 		/* Data. */
 		sp = perf_user_stack_pointer(regs);
+		fs = get_fs();
+		set_fs(USER_DS);
 		rem = __output_copy_user(handle, (void *) sp, dump_size);
+		set_fs(fs);
 		dyn_size = dump_size - rem;
 
 		perf_output_skip(handle, rem);