Skip to contentSkip to conversation

perf: Support multiple stacks (v3)

PATCH In review Arun Sharma 2 participants 11 messages Apr 20, 2012 – Jun 6, 2012

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH 1/4 perf, x86: Allow multiple stacks

In reply to Arun Sharma

Without this patch, applications with two different stack regions (eg: native stack vs JIT stack) get truncated callchains even when RBP chaining is present. GDB shows proper stack traces and the frame pointer chaining is intact.

This patch disables the (fp < RSP) check, hoping that other checks in the code save the day for us. In our limited testing, this didn't seem to break anything.

In the long term, we could potentially have userspace advise the kernel on the range of valid stack addresses, so we don't spend a lot of time unwinding from bogus addresses.

Cc Ingo Molnar <[email protected]>
CC Arnaldo Carvalho de Melo <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Tom Zanussi <[email protected]>
---
 arch/x86/kernel/cpu/perf_event.c |    6 ------
 1 files changed, 0 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/perf_event.c +0 -6
index bb8e034..9f98636 100644
@@ -1785,9 +1785,6 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
 		if (bytes != sizeof(frame))
 			break;
 
-		if (fp < compat_ptr(regs->sp))
-			break;
-
 		perf_callchain_store(entry, frame.return_address);
 		fp = compat_ptr(frame.next_frame);
 	}
@@ -1831,9 +1828,6 @@ perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
 		if (bytes != sizeof(frame))
 			break;
 
-		if ((unsigned long)fp < regs->sp)
-			break;
-
 		perf_callchain_store(entry, frame.return_address);
 		fp = frame.next_frame;
 	}
Signature · 1 line
1.7.8.4
#0 lore raw

cover perf: Support multiple stacks (v3)

If userspace has two or more stacks and we happen to take a sample when the stack pointer is pointing to a higher stack, we get truncated callchains. This patch series tries to address that.

Changelog:

v3: add explicit checks to copy_from_user_nmi
v3: add explicit checks in perf_callchain_user*

v2: do this for compat32 as well
v2: improve safety checks and reduce max callchain size

Arun Sharma (4):
perf, x86: Allow multiple stacks
perf: Limit callchains to 127
perf, x86: Check if user fp is valid
x86: Check user address explicitly in copy_from_user_nmi()

 arch/x86/include/asm/uaccess.h   |   13 +++++++------
 arch/x86/kernel/cpu/perf_event.c |   11 +++++++++--
 arch/x86/lib/usercopy.c          |    3 +++
 include/linux/perf_event.h       |    2 +-
 4 files changed, 20 insertions(+), 9 deletions(-)
Signature · 1 line
1.7.8.4
#8 lore raw

PATCH 4/4 x86: Check user address explicitly in copy_from_user_nmi()

Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/x86/lib/usercopy.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/x86/lib/usercopy.c +3 -0
index d6ae30b..2da7914 100644
@@ -21,6 +21,9 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
 	void *map;
 	int ret;
 
+	if (__range_not_ok(from, n, TASK_SIZE) == 0)
+		return len;
+
 	do {
 		ret = __get_user_pages_fast(addr, 1, 0, &page);
 		if (!ret)
Signature · 1 line
1.7.8.4
#6 lore raw

PATCH 3/4 perf, x86: Check if user fp is valid

In reply to Arun Sharma
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/x86/include/asm/uaccess.h   |   13 +++++++------
 arch/x86/kernel/cpu/perf_event.c |   12 ++++++++++++
 2 files changed, 19 insertions(+), 6 deletions(-)
arch/x86/include/asm/uaccess.h +7 -6
index e054459..997d59b 100644
@@ -32,9 +32,9 @@
 
 #define segment_eq(a, b)	((a).seg == (b).seg)
 
-#define __addr_ok(addr)					\
-	((unsigned long __force)(addr) <		\
-	 (current_thread_info()->addr_limit.seg))
+#define __cur_addr_limit	(current_thread_info()->addr_limit.seg)
+#define __addr_ok(addr) 	\
+	((unsigned long __force)(addr) < __cur_addr_limit)
 
 /*
  * Test whether a block of memory is a valid user space address.
@@ -46,14 +46,14 @@
  * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
  */
 
-#define __range_not_ok(addr, size)					\
+#define __range_not_ok(addr, size, limit)				\
 ({									\
 	unsigned long flag, roksum;					\
 	__chk_user_ptr(addr);						\
 	asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0"		\
 	    : "=&r" (flag), "=r" (roksum)				\
 	    : "1" (addr), "g" ((long)(size)),				\
-	      "rm" (current_thread_info()->addr_limit.seg));		\
+	      "rm" (limit));						\
 	flag;								\
 })
 
@@ -76,7 +76,8 @@
  * checks that the pointer is in the user space range - after calling
  * this function, memory access functions may still return -EFAULT.
  */
-#define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
+#define access_ok(type, addr, size) \
+	(likely(__range_not_ok(addr, size, __cur_addr_limit) == 0))
 
 /*
  * The exception table consists of pairs of addresses: the first is the
arch/x86/kernel/cpu/perf_event.c +12 -0
index 9f98636..3e4be2c 100644
@@ -1761,6 +1761,12 @@ perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
 	dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
 }
 
+static inline int
+valid_user_frame(const void __user *fp, unsigned long size)
+{
+	return (__range_not_ok(fp, size, TASK_SIZE) == 0);
+}
+
 #ifdef CONFIG_COMPAT
 
 #include <asm/compat.h>
@@ -1785,6 +1791,9 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
 		if (bytes != sizeof(frame))
 			break;
 
+		if (!valid_user_frame(fp, sizeof(frame)))
+			break;
+
 		perf_callchain_store(entry, frame.return_address);
 		fp = compat_ptr(frame.next_frame);
 	}
@@ -1828,6 +1837,9 @@ perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
 		if (bytes != sizeof(frame))
 			break;
 
+		if (!valid_user_frame(fp, sizeof(frame)))
+			break;
+
 		perf_callchain_store(entry, frame.return_address);
 		fp = frame.next_frame;
 	}
Signature · 1 line
1.7.8.4
#4 lore raw

PATCH 2/4 perf: Limit callchains to 127

In reply to Arun Sharma

Stack depth of 255 seems excessive, given that copy_from_user_nmi() could be slow.

Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 include/linux/perf_event.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
include/linux/perf_event.h +1 -1
index ddbb6a9..d1e71c1 100644
@@ -609,7 +609,7 @@ struct perf_guest_info_callbacks {
 #include <linux/sysfs.h>
 #include <asm/local.h>
 
-#define PERF_MAX_STACK_DEPTH		255
+#define PERF_MAX_STACK_DEPTH		127
 
 struct perf_callchain_entry {
 	__u64				nr;
Signature · 1 line
1.7.8.4
#2 lore raw

Re: [PATCH 1/4] perf, x86: Allow multiple stacks

In reply to Arun Sharma
Quoted 5 lines · Arun Sharma

Without this patch, applications with two different stack regions (eg: native stack vs JIT stack) get truncated callchains even when RBP chaining is present. GDB shows proper stack traces and the frame pointer chaining is intact.

Quoted 7 lines

This patch disables the (fp< RSP) check, hoping that other checks in the code save the day for us. In our limited testing, this didn't seem to break anything.

In the long term, we could potentially have userspace advise the kernel on the range of valid stack addresses, so we don't spend a lot of time unwinding from bogus addresses.

Ingo/Peter: Did you get a chance to look at this patch series (Support multiple stacks v3)?

-Arun

maintainer
#9 lore raw

Re: [PATCH 4/4] x86: Check user address explicitly in copy_from_user_nmi()

In reply to Arun Sharma
Quoted 14 lines · Arun Sharma
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/x86/lib/usercopy.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
arch/x86/lib/usercopy.c +0 -0
index d6ae30b..2da7914 100644

arch/x86/lib/usercopy.c copy_from_user_nmi(void *to, const void __user *from, unsigned long n) · line 29

Quoted 9 lines
arch/x86/lib/usercopy.c +3 -0
@@ -21,6 +21,9 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
 	void *map;
 	int ret;
 
+	if (__range_not_ok(from, n, TASK_SIZE) == 0)
+		return len;
+
 	do {
 		ret = __get_user_pages_fast(addr, 1, 0, &page);
 		if (!ret)

Needed the below to make it compile.. folded that.

---

arch/x86/lib/usercopy.c +1 -0
@@ -8,6 +8,7 @@
 #include <linux/module.h>
 
 #include <asm/word-at-a-time.h>
+#include <linux/sched.h>
 
 /*
  * best effort, GUP based copy_from_user() that is NMI-safe
#3 lore raw

[tip:perf/core] perf/x86: Allow multiple stacks

In reply to Arun Sharma
Commit-ID:  302fa4b58ac754a6da13f4f5546f710fecc3b945
Gitweb:     http://git.kernel.org/tip/302fa4b58ac754a6da13f4f5546f710fecc3b945
Author:     Arun Sharma <[email protected]>
AuthorDate: Fri, 20 Apr 2012 15:41:33 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 6 Jun 2012 17:07:58 +0200

perf/x86: Allow multiple stacks

Without this patch, applications with two different stack regions (eg: native stack vs JIT stack) get truncated callchains even when RBP chaining is present. GDB shows proper stack traces and the frame pointer chaining is intact.

This patch disables the (fp < RSP) check, hoping that other checks in the code save the day for us. In our limited testing, this didn't seem to break anything.

In the long term, we could potentially have userspace advise the kernel on the range of valid stack addresses, so we don't spend a lot of time unwinding from bogus addresses.

---
 arch/x86/kernel/cpu/perf_event.c |    6 ------
 1 files changed, 0 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/perf_event.c +0 -6
index cb60838..e78bc25 100644
@@ -1781,9 +1781,6 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
 		if (bytes != sizeof(frame))
 			break;
 
-		if (fp < compat_ptr(regs->sp))
-			break;
-
 		perf_callchain_store(entry, frame.return_address);
 		fp = compat_ptr(frame.next_frame);
 	}
@@ -1827,9 +1824,6 @@ perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
 		if (bytes != sizeof(frame))
 			break;
 
-		if ((unsigned long)fp < regs->sp)
-			break;
-
 		perf_callchain_store(entry, frame.return_address);
 		fp = frame.next_frame;
 	}
#5 lore raw

[tip:perf/core] perf: Limit callchains to 127

In reply to Arun Sharma
Commit-ID:  0b0d9cf6ec7bab91977da2d71c09157f110f7c2e
Gitweb:     http://git.kernel.org/tip/0b0d9cf6ec7bab91977da2d71c09157f110f7c2e
Author:     Arun Sharma <[email protected]>
AuthorDate: Fri, 20 Apr 2012 15:41:34 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 6 Jun 2012 17:08:00 +0200

perf: Limit callchains to 127

Stack depth of 255 seems excessive, given that copy_from_user_nmi() could be slow.

---
 include/linux/perf_event.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
include/linux/perf_event.h +1 -1
index 1817d40..45db49f 100644
@@ -555,7 +555,7 @@ enum perf_event_type {
 	PERF_RECORD_MAX,			/* non-ABI */
 };
 
-#define PERF_MAX_STACK_DEPTH		255
+#define PERF_MAX_STACK_DEPTH		127
 
 enum perf_callchain_context {
 	PERF_CONTEXT_HV			= (__u64)-32,
#7 lore raw

[tip:perf/core] perf/x86: Check if user fp is valid

In reply to Arun Sharma
Commit-ID:  bc6ca7b342d5ae15c3ba3081fd40271b8039fb25
Gitweb:     http://git.kernel.org/tip/bc6ca7b342d5ae15c3ba3081fd40271b8039fb25
Author:     Arun Sharma <[email protected]>
AuthorDate: Fri, 20 Apr 2012 15:41:35 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 6 Jun 2012 17:08:01 +0200

perf/x86: Check if user fp is valid

---
 arch/x86/include/asm/uaccess.h   |   12 ++++++------
 arch/x86/kernel/cpu/perf_event.c |   12 ++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)
arch/x86/include/asm/uaccess.h +6 -6
index 04cd688..e1f3a17 100644
@@ -33,9 +33,8 @@
 #define segment_eq(a, b)	((a).seg == (b).seg)
 
 #define user_addr_max() (current_thread_info()->addr_limit.seg)
-#define __addr_ok(addr)					\
-	((unsigned long __force)(addr) <		\
-	 (current_thread_info()->addr_limit.seg))
+#define __addr_ok(addr) 	\
+	((unsigned long __force)(addr) < user_addr_max())
 
 /*
  * Test whether a block of memory is a valid user space address.
@@ -47,14 +46,14 @@
  * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
  */
 
-#define __range_not_ok(addr, size)					\
+#define __range_not_ok(addr, size, limit)				\
 ({									\
 	unsigned long flag, roksum;					\
 	__chk_user_ptr(addr);						\
 	asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0"		\
 	    : "=&r" (flag), "=r" (roksum)				\
 	    : "1" (addr), "g" ((long)(size)),				\
-	      "rm" (current_thread_info()->addr_limit.seg));		\
+	      "rm" (limit));						\
 	flag;								\
 })
 
@@ -77,7 +76,8 @@
  * checks that the pointer is in the user space range - after calling
  * this function, memory access functions may still return -EFAULT.
  */
-#define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
+#define access_ok(type, addr, size) \
+	(likely(__range_not_ok(addr, size, user_addr_max()) == 0))
 
 /*
  * The exception table consists of pairs of addresses relative to the
arch/x86/kernel/cpu/perf_event.c +12 -0
index e78bc25..c4706cf 100644
@@ -1757,6 +1757,12 @@ perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
 	dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
 }
 
+static inline int
+valid_user_frame(const void __user *fp, unsigned long size)
+{
+	return (__range_not_ok(fp, size, TASK_SIZE) == 0);
+}
+
 #ifdef CONFIG_COMPAT
 
 #include <asm/compat.h>
@@ -1781,6 +1787,9 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
 		if (bytes != sizeof(frame))
 			break;
 
+		if (!valid_user_frame(fp, sizeof(frame)))
+			break;
+
 		perf_callchain_store(entry, frame.return_address);
 		fp = compat_ptr(frame.next_frame);
 	}
@@ -1824,6 +1833,9 @@ perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
 		if (bytes != sizeof(frame))
 			break;
 
+		if (!valid_user_frame(fp, sizeof(frame)))
+			break;
+
 		perf_callchain_store(entry, frame.return_address);
 		fp = frame.next_frame;
 	}
#10 lore raw

[tip:perf/core] perf/x86: Check user address explicitly in copy_from_user_nmi()

In reply to Arun Sharma
Commit-ID:  db0dc75d6403b6663c0eab4c6ccb672eb9b2ed72
Gitweb:     http://git.kernel.org/tip/db0dc75d6403b6663c0eab4c6ccb672eb9b2ed72
Author:     Arun Sharma <[email protected]>
AuthorDate: Fri, 20 Apr 2012 15:41:36 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 6 Jun 2012 17:08:04 +0200

perf/x86: Check user address explicitly in copy_from_user_nmi()

---
 arch/x86/lib/usercopy.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
arch/x86/lib/usercopy.c +4 -0
index f61ee67..677b1ed 100644
@@ -8,6 +8,7 @@
 #include <linux/module.h>
 
 #include <asm/word-at-a-time.h>
+#include <linux/sched.h>
 
 /*
  * best effort, GUP based copy_from_user() that is NMI-safe
@@ -21,6 +22,9 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
 	void *map;
 	int ret;
 
+	if (__range_not_ok(from, n, TASK_SIZE) == 0)
+		return len;
+
 	do {
 		ret = __get_user_pages_fast(addr, 1, 0, &page);
 		if (!ret)