Skip to contentSkip to conversation

rseq: x86: implement abort-at-ip extension

RFC PATCH In review Mathieu Desnoyers 6 participants 15 messages Jan 10, 2022 – Jan 12, 2022

Subsystems (5)
x86 Kselftest ABI/API ptrace support +1 more subsystems
lore
Share & tools
lore

Mathieu Desnoyers

author maintainer
#0 lore raw

RFC PATCH v2 1/2 rseq: x86: implement abort-at-ip extension

Allow rseq critical section abort handlers to optionally figure out at which instruction pointer the rseq critical section was aborted.

This allows implementing rseq critical sections with loops containing the commit instruction, for which having the commit as last instruction of the sequence is not possible. This is useful to implement adaptative mutexes aware of preemption in user-space. (see [1])

This also allows implementing rseq critical sections with multiple commit steps, and use the abort-at-ip information to figure out what needs to be undone in the abort handler.

Introduce the RSEQ_FLAG_QUERY_ABORT_AT_IP rseq system call flag. This lets userspace query whether the kernel and architecture supports the abort-at-ip rseq extension.

Only provide this information for rseq critical sections for which the rseq_cs descriptor has the RSEQ_CS_FLAG_ABORT_AT_IP flag set. Abort handlers for critical sections with this flag set need to readjust the stack pointer. The abort-at-ip pointer is populated by the kernel on the top of stack on abort. For x86-32, the abort handler of an abort-at-ip critical section needs to add 4 bytes to the stack pointer. For x86-64, the abort hanler needs to add 136 bytes to the stack
pointer: 8 bytes to skip the abort-at-ip value, and 128 bytes to skip the x86-64 redzone for leaf functions.

[1] https://github.com/compudj/rseq-test/blob/adapt-lock-abort-at-ip/test-rseq-adaptative-lock.c#L80

---
Changes since v1:
- Use top of stack to place abort-at-ip value rather than ecx/rcx register,
- Skip redzone on x86-64.

---
 arch/x86/include/asm/ptrace.h |  5 +++++
 arch/x86/kernel/ptrace.c      | 12 ++++++++++++
 include/uapi/linux/rseq.h     |  4 ++++
 kernel/rseq.c                 | 28 ++++++++++++++++++++++++++++
 4 files changed, 49 insertions(+)
arch/x86/include/asm/ptrace.h +5 -0
index 703663175a5a..c96eb2448110 100644
@@ -387,5 +387,10 @@ extern int do_set_thread_area(struct task_struct *p, int idx,
 # define do_set_thread_area_64(p, s, t)	(0)
 #endif
 
+#ifdef CONFIG_RSEQ
+# define RSEQ_ARCH_HAS_ABORT_AT_IP
+int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip);
+#endif
+
 #endif /* !__ASSEMBLY__ */
 #endif /* _ASM_X86_PTRACE_H */
arch/x86/kernel/ptrace.c +12 -0
index 6d2244c94799..561ed98d12ba 100644
@@ -1368,3 +1368,15 @@ void user_single_step_report(struct pt_regs *regs)
 {
 	send_sigtrap(regs, 0, TRAP_BRKPT);
 }
+
+int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)
+{
+	if (user_64bit_mode(regs)) {
+		/* Need to skip redzone for leaf functions. */
+		regs->sp -= sizeof(u64) + 128;
+		return put_user(ip, (u64 __user *)regs->sp);
+	} else {
+		regs->sp -= sizeof(u32);
+		return put_user(ip, (u32 __user *)regs->sp);
+	}
+}
include/uapi/linux/rseq.h +4 -0
index 9a402fdb60e9..3130232e6d0c 100644
@@ -20,12 +20,14 @@ enum rseq_cpu_id_state {
 
 enum rseq_flags {
 	RSEQ_FLAG_UNREGISTER = (1 << 0),
+	RSEQ_FLAG_QUERY_ABORT_AT_IP = (1 << 1),
 };
 
 enum rseq_cs_flags_bit {
 	RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT	= 0,
 	RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT	= 1,
 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT	= 2,
+	RSEQ_CS_FLAG_ABORT_AT_IP_BIT		= 3,
 };
 
 enum rseq_cs_flags {
@@ -35,6 +37,8 @@ enum rseq_cs_flags {
 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT),
 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE	=
 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT),
+	RSEQ_CS_FLAG_ABORT_AT_IP		=
+		(1U << RSEQ_CS_FLAG_ABORT_AT_IP_BIT),
 };
 
 /*
kernel/rseq.c +28 -0
index 6d45ac3dae7f..fb52f2d11b56 100644
@@ -21,6 +21,13 @@
 #define RSEQ_CS_PREEMPT_MIGRATE_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE | \
 				       RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT)
 
+#ifdef RSEQ_ARCH_HAS_ABORT_AT_IP
+static bool rseq_has_abort_at_ip(void) { return true; }
+#else
+static bool rseq_has_abort_at_ip(void) { return false; }
+static int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip) { return 0; }
+#endif
+
 /*
  *
  * Restartable sequences are a lightweight interface that allows
@@ -79,6 +86,16 @@
  *
  *       [abort_ip]
  *   F1. <failure>
+ *
+ * rseq critical sections defined with the RSEQ_CS_FLAG_ABORT_AT_IP flag
+ * have the following behavior on abort: when the stack grows down: the
+ * stack pointer is decremented to skip the redzone, and decremented of
+ * the pointer size.  The aborted address (abort-at-ip) is stored at
+ * this stack pointer location.  The user-space abort handler needs to
+ * pop the abort-at-ip address from the stack, and add the redzone size
+ * to the stack pointer.
+ *
+ * TODO: describe stack grows up.
  */
 
 static int rseq_update_cpu_id(struct task_struct *t)
@@ -261,6 +278,11 @@ static int rseq_ip_fixup(struct pt_regs *regs)
 	trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
 			    rseq_cs.abort_ip);
 	instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
+	if (rseq_cs.flags & RSEQ_CS_FLAG_ABORT_AT_IP) {
+		ret = rseq_abort_at_ip(regs, ip);
+		if (ret)
+			return ret;
+	}
 	return 0;
 }
 
@@ -330,6 +352,12 @@ SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
 {
 	int ret;
 
+	if (flags & RSEQ_FLAG_QUERY_ABORT_AT_IP) {
+		if (flags & ~RSEQ_FLAG_QUERY_ABORT_AT_IP)
+			return -EINVAL;
+		return rseq_has_abort_at_ip() ? 0 : -EINVAL;
+	}
+
 	if (flags & RSEQ_FLAG_UNREGISTER) {
 		if (flags & ~RSEQ_FLAG_UNREGISTER)
 			return -EINVAL;
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

RFC PATCH v2 2/2 selftests: rseq: test abort-at-ip extension on x86

---
 tools/testing/selftests/rseq/.gitignore       |   1 +
 tools/testing/selftests/rseq/Makefile         |   3 +-
 tools/testing/selftests/rseq/rseq.c           |  21 ++
 tools/testing/selftests/rseq/rseq.h           |  12 +
 .../selftests/rseq/rseq_ext_abort_at_ip.c     | 285 ++++++++++++++++++
 5 files changed, 321 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/rseq/rseq_ext_abort_at_ip.c
tools/testing/selftests/rseq/.gitignore +1 -0
index 5910888ebfe1..472152afab77 100644
@@ -5,3 +5,4 @@ basic_rseq_op_test
 param_test
 param_test_benchmark
 param_test_compare_twice
+rseq_ext_abort_at_ip
tools/testing/selftests/rseq/Makefile +2 -1
index 2af9d39a9716..dae0dba7552b 100644
@@ -13,7 +13,8 @@ LDLIBS += -lpthread
 OVERRIDE_TARGETS = 1
 
 TEST_GEN_PROGS = basic_test basic_percpu_ops_test param_test \
-		param_test_benchmark param_test_compare_twice
+		param_test_benchmark param_test_compare_twice \
+		rseq_ext_abort_at_ip
 
 TEST_GEN_PROGS_EXTENDED = librseq.so
 
tools/testing/selftests/rseq/rseq.c +21 -0
index 7159eb777fd3..3e012f581ddb 100644
@@ -73,6 +73,27 @@ static int sys_rseq(volatile struct rseq *rseq_abi, uint32_t rseq_len,
 	return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig);
 }
 
+/*
+ * Return 0 if extension is available, negative error otherwise.
+ */
+int rseq_query_extension(enum rseq_extension ext)
+{
+	int ret;
+	unsigned int flags;
+
+	switch (ext) {
+	case RSEQ_EXT_ABORT_AT_IP:
+		flags = RSEQ_FLAG_QUERY_ABORT_AT_IP;
+		break;
+	default:
+		return -EINVAL;
+	}
+	ret = sys_rseq(NULL, 0, flags, 0);
+	if (!ret)
+		return 0;
+	return -errno;
+}
+
 int rseq_register_current_thread(void)
 {
 	int rc, ret = 0;
tools/testing/selftests/rseq/rseq.h +12 -0
index 3f63eb362b92..34c80402c078 100644
@@ -67,6 +67,12 @@ extern int __rseq_handled;
 		abort();		\
 	} while (0)
 
+enum rseq_extension {
+	RSEQ_EXT_ABORT_AT_IP = 0,
+};
+
+#define ASM_RSEQ_CS_FLAG_ABORT_AT_IP	(1U << 3)
+
 #if defined(__x86_64__) || defined(__i386__)
 #include <rseq-x86.h>
 #elif defined(__ARMEL__)
@@ -162,4 +168,10 @@ static inline void rseq_prepare_unload(void)
 	rseq_clear_rseq_cs();
 }
 
+/*
+ * Query whether rseq extension is available. Return 0 if available, negative
+ * error value otherwise.
+ */
+int rseq_query_extension(enum rseq_extension ext);
+
 #endif  /* RSEQ_H_ */
tools/testing/selftests/rseq/rseq_ext_abort_at_ip.c new file +285 -0

285 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw
In reply to Mathieu Desnoyers
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Mathieu,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on tip/x86/core tip/master linus/master v5.16 next-20220110] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Mathieu-Desnoyers/rseq-x86-implement-abort-at-ip-extension/20220111-011941
base:   https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
config: i386-randconfig-a011-20210930 (https://download.01.org/0day-ci/archive/20220111/202201111332.CweiXUcC-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/8731a339925d9270b03a51b7c1b54e718dded248
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Mathieu-Desnoyers/rseq-x86-implement-abort-at-ip-extension/20220111-011941
        git checkout 8731a339925d9270b03a51b7c1b54e718dded248
        # save the config file to linux build tree
        mkdir build_dir
        make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash arch/x86/kernel/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> arch/x86/kernel/ptrace.c:1372:5: warning: no previous prototype for 'rseq_abort_at_ip' [-Wmissing-prototypes]

1372 | int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)
| ^~~~~~~~~~~~~~~~

vim +/rseq_abort_at_ip +1372 arch/x86/kernel/ptrace.c

1371

> 1372	int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw
In reply to Mathieu Desnoyers
Quoted 65 lines · Mathieu Desnoyers

Allow rseq critical section abort handlers to optionally figure out at which instruction pointer the rseq critical section was aborted.

This allows implementing rseq critical sections with loops containing the commit instruction, for which having the commit as last instruction of the sequence is not possible. This is useful to implement adaptative mutexes aware of preemption in user-space. (see [1])

This also allows implementing rseq critical sections with multiple commit steps, and use the abort-at-ip information to figure out what needs to be undone in the abort handler.

Introduce the RSEQ_FLAG_QUERY_ABORT_AT_IP rseq system call flag. This lets userspace query whether the kernel and architecture supports the abort-at-ip rseq extension.

Only provide this information for rseq critical sections for which the rseq_cs descriptor has the RSEQ_CS_FLAG_ABORT_AT_IP flag set. Abort handlers for critical sections with this flag set need to readjust the stack pointer. The abort-at-ip pointer is populated by the kernel on the top of stack on abort. For x86-32, the abort handler of an abort-at-ip critical section needs to add 4 bytes to the stack pointer. For x86-64, the abort hanler needs to add 136 bytes to the stack
pointer: 8 bytes to skip the abort-at-ip value, and 128 bytes to skip the x86-64 redzone for leaf functions.

[1] https://github.com/compudj/rseq-test/blob/adapt-lock-abort-at-ip/test-rseq-adaptative-lock.c#L80

---
Changes since v1:
- Use top of stack to place abort-at-ip value rather than ecx/rcx register,
- Skip redzone on x86-64.

---
 arch/x86/include/asm/ptrace.h |  5 +++++
 arch/x86/kernel/ptrace.c      | 12 ++++++++++++
 include/uapi/linux/rseq.h     |  4 ++++
 kernel/rseq.c                 | 28 ++++++++++++++++++++++++++++
 4 files changed, 49 insertions(+)
arch/x86/include/asm/ptrace.h +5 -0
index 703663175a5a..c96eb2448110 100644
@@ -387,5 +387,10 @@ extern int do_set_thread_area(struct task_struct *p, int idx,
 # define do_set_thread_area_64(p, s, t)	(0)
 #endif
 
+#ifdef CONFIG_RSEQ
+# define RSEQ_ARCH_HAS_ABORT_AT_IP
+int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip);
+#endif
+
 #endif /* !__ASSEMBLY__ */
 #endif /* _ASM_X86_PTRACE_H */
arch/x86/kernel/ptrace.c +1 -0
index 6d2244c94799..561ed98d12ba 100644
@@ -1368,3 +1368,15 @@ void user_single_step_report(struct pt_regs *regs)
 {
 	send_sigtrap(regs, 0, TRAP_BRKPT);
 }
+

arch/x86/kernel/ptrace.c void user_single_step_report(struct pt_regs *regs) · line 1381

Quoted 10 lines
arch/x86/kernel/ptrace.c +10 -0
+int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)
+{
+	if (user_64bit_mode(regs)) {
+		/* Need to skip redzone for leaf functions. */
+		regs->sp -= sizeof(u64) + 128;
+		return put_user(ip, (u64 __user *)regs->sp);
+	} else {
+		regs->sp -= sizeof(u32);
+		return put_user(ip, (u32 __user *)regs->sp);
+	}

I think it would be really helpful if you added the full explanation for sizeof(u64) + 128 or -sizeof(u32) into this codepath or provide constants. For folks not familiar with stuff like this it'll look like magic numbers. :)

Quoted 51 lines
arch/x86/kernel/ptrace.c +1 -0
+}
include/uapi/linux/rseq.h +4 -0
index 9a402fdb60e9..3130232e6d0c 100644
@@ -20,12 +20,14 @@ enum rseq_cpu_id_state {
 
 enum rseq_flags {
 	RSEQ_FLAG_UNREGISTER = (1 << 0),
+	RSEQ_FLAG_QUERY_ABORT_AT_IP = (1 << 1),
 };
 
 enum rseq_cs_flags_bit {
 	RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT	= 0,
 	RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT	= 1,
 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT	= 2,
+	RSEQ_CS_FLAG_ABORT_AT_IP_BIT		= 3,
 };
 
 enum rseq_cs_flags {
@@ -35,6 +37,8 @@ enum rseq_cs_flags {
 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT),
 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE	=
 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT),
+	RSEQ_CS_FLAG_ABORT_AT_IP		=
+		(1U << RSEQ_CS_FLAG_ABORT_AT_IP_BIT),
 };
 
 /*
kernel/rseq.c +7 -0
index 6d45ac3dae7f..fb52f2d11b56 100644
@@ -21,6 +21,13 @@
 #define RSEQ_CS_PREEMPT_MIGRATE_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE | \
 				       RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT)
 
+#ifdef RSEQ_ARCH_HAS_ABORT_AT_IP
+static bool rseq_has_abort_at_ip(void) { return true; }
+#else
+static bool rseq_has_abort_at_ip(void) { return false; }
+static int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip) { return 0; }
+#endif
+
 /*
  *
  * Restartable sequences are a lightweight interface that allows
@@ -79,6 +86,16 @@
  *
  *       [abort_ip]
  *   F1. <failure>

kernel/rseq.c line 98

Quoted 10 lines
kernel/rseq.c +10 -0
+ *
+ * rseq critical sections defined with the RSEQ_CS_FLAG_ABORT_AT_IP flag
+ * have the following behavior on abort: when the stack grows down: the
+ * stack pointer is decremented to skip the redzone, and decremented of
+ * the pointer size.  The aborted address (abort-at-ip) is stored at
+ * this stack pointer location.  The user-space abort handler needs to
+ * pop the abort-at-ip address from the stack, and add the redzone size
+ * to the stack pointer.
+ *
+ * TODO: describe stack grows up.

Is this intentional or did you forget? :)

author maintainer
#4 lore raw
Quoted 75 lines · ----- On Jan 11, 2022, at 6:05 AM, Christian Brauner [email protected] wrote:
Quoted 74 lines · Mathieu Desnoyers

Allow rseq critical section abort handlers to optionally figure out at which instruction pointer the rseq critical section was aborted.

This allows implementing rseq critical sections with loops containing the commit instruction, for which having the commit as last instruction of the sequence is not possible. This is useful to implement adaptative mutexes aware of preemption in user-space. (see [1])

This also allows implementing rseq critical sections with multiple commit steps, and use the abort-at-ip information to figure out what needs to be undone in the abort handler.

Introduce the RSEQ_FLAG_QUERY_ABORT_AT_IP rseq system call flag. This lets userspace query whether the kernel and architecture supports the abort-at-ip rseq extension.

Only provide this information for rseq critical sections for which the rseq_cs descriptor has the RSEQ_CS_FLAG_ABORT_AT_IP flag set. Abort handlers for critical sections with this flag set need to readjust the stack pointer. The abort-at-ip pointer is populated by the kernel on the top of stack on abort. For x86-32, the abort handler of an abort-at-ip critical section needs to add 4 bytes to the stack pointer. For x86-64, the abort hanler needs to add 136 bytes to the stack
pointer: 8 bytes to skip the abort-at-ip value, and 128 bytes to skip the x86-64 redzone for leaf functions.

[1]
https://github.com/compudj/rseq-test/blob/adapt-lock-abort-at-ip/test-rseq-adaptative-lock.c#L80

---
Changes since v1:
- Use top of stack to place abort-at-ip value rather than ecx/rcx register,
- Skip redzone on x86-64.

---
 arch/x86/include/asm/ptrace.h |  5 +++++
 arch/x86/kernel/ptrace.c      | 12 ++++++++++++
 include/uapi/linux/rseq.h     |  4 ++++
 kernel/rseq.c                 | 28 ++++++++++++++++++++++++++++
 4 files changed, 49 insertions(+)
arch/x86/include/asm/ptrace.h +5 -0
index 703663175a5a..c96eb2448110 100644
@@ -387,5 +387,10 @@ extern int do_set_thread_area(struct task_struct *p, int
 idx,
 # define do_set_thread_area_64(p, s, t)	(0)
 #endif
 
+#ifdef CONFIG_RSEQ
+# define RSEQ_ARCH_HAS_ABORT_AT_IP
+int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip);
+#endif
+
 #endif /* !__ASSEMBLY__ */

#endif /* _ASM_X86_PTRACE_H */

arch/x86/kernel/ptrace.c +8 -0
index 6d2244c94799..561ed98d12ba 100644
@@ -1368,3 +1368,15 @@ void user_single_step_report(struct pt_regs *regs)
 {
 	send_sigtrap(regs, 0, TRAP_BRKPT);
 }
+
+int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)
+{
+	if (user_64bit_mode(regs)) {
+		/* Need to skip redzone for leaf functions. */
+		regs->sp -= sizeof(u64) + 128;
+		return put_user(ip, (u64 __user *)regs->sp);
+	} else {
Quoted 8 lines

arch/x86/kernel/ptrace.c void user_single_step_report(struct pt_regs *regs) · line 1381

Quoted 3 lines
arch/x86/kernel/ptrace.c +3 -0
+		regs->sp -= sizeof(u32);
+		return put_user(ip, (u32 __user *)regs->sp);
+	}

I think it would be really helpful if you added the full explanation for sizeof(u64) + 128 or -sizeof(u32) into this codepath or provide constants. For folks not familiar with stuff like this it'll look like magic numbers. :)

Good point, here is the planned update:

int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)

{
        if (user_64bit_mode(regs)) {
                /*
                 * rseq abort-at-ip x86-64 ABI: stack pointer is decremented to
                 * skip the redzone (128 bytes on x86-64), and decremented of
                 * the pointer size (8 bytes).  The aborted address (abort-at-ip)
                 * is stored at this updated stack pointer location (top of stack).
                 *
                 * Skipping the redzone is needed to make sure not to corrupt
                 * stack data when the rseq critical section is within a leaf
                 * function.
                 */
                regs->sp -= sizeof(u64) + 128;
                return put_user(ip, (u64 __user *)regs->sp);
        } else {
                /*
                 * rseq abort-at-ip x86-32 ABI: stack pointer is decremented of
                 * the pointer size (4 bytes).  The aborted address (abort-at-ip)
                 * is stored at this updated stack pointer location (top of stack).
                 */
                regs->sp -= sizeof(u32);
                return put_user(ip, (u32 __user *)regs->sp);
        }
}
Quoted 57 lines
Quoted 57 lines
arch/x86/kernel/ptrace.c +1 -0
+}
include/uapi/linux/rseq.h +4 -0
index 9a402fdb60e9..3130232e6d0c 100644
@@ -20,12 +20,14 @@ enum rseq_cpu_id_state {
 
 enum rseq_flags {
 	RSEQ_FLAG_UNREGISTER = (1 << 0),
+	RSEQ_FLAG_QUERY_ABORT_AT_IP = (1 << 1),
 };
 
 enum rseq_cs_flags_bit {
 	RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT	= 0,
 	RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT	= 1,
 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT	= 2,
+	RSEQ_CS_FLAG_ABORT_AT_IP_BIT		= 3,
 };
 
 enum rseq_cs_flags {
@@ -35,6 +37,8 @@ enum rseq_cs_flags {
 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT),
 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE	=
 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT),
+	RSEQ_CS_FLAG_ABORT_AT_IP		=
+		(1U << RSEQ_CS_FLAG_ABORT_AT_IP_BIT),
 };
 
 /*
kernel/rseq.c +7 -0
index 6d45ac3dae7f..fb52f2d11b56 100644
@@ -21,6 +21,13 @@
 #define RSEQ_CS_PREEMPT_MIGRATE_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE | \
 				       RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT)
 
+#ifdef RSEQ_ARCH_HAS_ABORT_AT_IP
+static bool rseq_has_abort_at_ip(void) { return true; }
+#else
+static bool rseq_has_abort_at_ip(void) { return false; }
+static int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip) { return 0;
 }
+#endif
+
 /*
  *

* Restartable sequences are a lightweight interface that allows

kernel/rseq.c +4 -0
@@ -79,6 +86,16 @@
  *
  *       [abort_ip]
  *   F1. <failure>
+ *
+ * rseq critical sections defined with the RSEQ_CS_FLAG_ABORT_AT_IP flag
+ * have the following behavior on abort: when the stack grows down: the
+ * stack pointer is decremented to skip the redzone, and decremented of
Quoted 8 lines

kernel/rseq.c line 98

Quoted 6 lines
kernel/rseq.c +6 -0
+ * the pointer size.  The aborted address (abort-at-ip) is stored at
+ * this stack pointer location.  The user-space abort handler needs to
+ * pop the abort-at-ip address from the stack, and add the redzone size
+ * to the stack pointer.
+ *
+ * TODO: describe stack grows up.

Is this intentional or did you forget? :)

Since I did not implement abort-at-ip on stack-grows-up architectures, I felt it would be too early to describe the algorithm. I can simply remove the TODO altogether and we'll take care of it when we get there ? If I had to try to wordsmith it, it would look like e.g.:

* [...] When the stack grows up: the
* stack pointer is incremented to skip the redzone, and incremented of
* the pointer size. The aborted address (abort-at-ip) is stored immediately
* under this stack pointer location. The user-space abort handler needs to
* pop the abort-at-ip address from the stack, and subtract the redzone size
* from the stack pointer.

[ Please let me know if I got somehow confused in my understanding of stack grows up architectures. ]

I'm also unsure whether any of the stack grows up architecture have redzones ? From a quick grep for redzone in Linux arch/, only openrisc, powerpc64 and x86-64 appear to have redzones.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#5 lore raw
Quoted 86 lines · Mathieu Desnoyers
Quoted 75 lines · ----- On Jan 11, 2022, at 6:05 AM, Christian Brauner [email protected] wrote:
Quoted 74 lines · Mathieu Desnoyers

Allow rseq critical section abort handlers to optionally figure out at which instruction pointer the rseq critical section was aborted.

This allows implementing rseq critical sections with loops containing the commit instruction, for which having the commit as last instruction of the sequence is not possible. This is useful to implement adaptative mutexes aware of preemption in user-space. (see [1])

This also allows implementing rseq critical sections with multiple commit steps, and use the abort-at-ip information to figure out what needs to be undone in the abort handler.

Introduce the RSEQ_FLAG_QUERY_ABORT_AT_IP rseq system call flag. This lets userspace query whether the kernel and architecture supports the abort-at-ip rseq extension.

Only provide this information for rseq critical sections for which the rseq_cs descriptor has the RSEQ_CS_FLAG_ABORT_AT_IP flag set. Abort handlers for critical sections with this flag set need to readjust the stack pointer. The abort-at-ip pointer is populated by the kernel on the top of stack on abort. For x86-32, the abort handler of an abort-at-ip critical section needs to add 4 bytes to the stack pointer. For x86-64, the abort hanler needs to add 136 bytes to the stack
pointer: 8 bytes to skip the abort-at-ip value, and 128 bytes to skip the x86-64 redzone for leaf functions.

[1]
https://github.com/compudj/rseq-test/blob/adapt-lock-abort-at-ip/test-rseq-adaptative-lock.c#L80

---
Changes since v1:
- Use top of stack to place abort-at-ip value rather than ecx/rcx register,
- Skip redzone on x86-64.

---
 arch/x86/include/asm/ptrace.h |  5 +++++
 arch/x86/kernel/ptrace.c      | 12 ++++++++++++
 include/uapi/linux/rseq.h     |  4 ++++
 kernel/rseq.c                 | 28 ++++++++++++++++++++++++++++
 4 files changed, 49 insertions(+)
arch/x86/include/asm/ptrace.h +5 -0
index 703663175a5a..c96eb2448110 100644
@@ -387,5 +387,10 @@ extern int do_set_thread_area(struct task_struct *p, int
 idx,
 # define do_set_thread_area_64(p, s, t)	(0)
 #endif
 
+#ifdef CONFIG_RSEQ
+# define RSEQ_ARCH_HAS_ABORT_AT_IP
+int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip);
+#endif
+
 #endif /* !__ASSEMBLY__ */

#endif /* _ASM_X86_PTRACE_H */

arch/x86/kernel/ptrace.c +8 -0
index 6d2244c94799..561ed98d12ba 100644
@@ -1368,3 +1368,15 @@ void user_single_step_report(struct pt_regs *regs)
 {
 	send_sigtrap(regs, 0, TRAP_BRKPT);
 }
+
+int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)
+{
+	if (user_64bit_mode(regs)) {
+		/* Need to skip redzone for leaf functions. */
+		regs->sp -= sizeof(u64) + 128;
+		return put_user(ip, (u64 __user *)regs->sp);
+	} else {
Quoted 8 lines

arch/x86/kernel/ptrace.c void user_single_step_report(struct pt_regs *regs) · line 1381

Quoted 3 lines
arch/x86/kernel/ptrace.c +3 -0
+		regs->sp -= sizeof(u32);
+		return put_user(ip, (u32 __user *)regs->sp);
+	}

I think it would be really helpful if you added the full explanation for sizeof(u64) + 128 or -sizeof(u32) into this codepath or provide constants. For folks not familiar with stuff like this it'll look like magic numbers. :)

Quoted 1 line

Good point, here is the planned update:

That's great, thanks!

Quoted 107 lines

int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)

{
        if (user_64bit_mode(regs)) {
                /*
                 * rseq abort-at-ip x86-64 ABI: stack pointer is decremented to
                 * skip the redzone (128 bytes on x86-64), and decremented of
                 * the pointer size (8 bytes).  The aborted address (abort-at-ip)
                 * is stored at this updated stack pointer location (top of stack).
                 *
                 * Skipping the redzone is needed to make sure not to corrupt
                 * stack data when the rseq critical section is within a leaf
                 * function.
                 */
                regs->sp -= sizeof(u64) + 128;
                return put_user(ip, (u64 __user *)regs->sp);
        } else {
                /*
                 * rseq abort-at-ip x86-32 ABI: stack pointer is decremented of
                 * the pointer size (4 bytes).  The aborted address (abort-at-ip)
                 * is stored at this updated stack pointer location (top of stack).
                 */
                regs->sp -= sizeof(u32);
                return put_user(ip, (u32 __user *)regs->sp);
        }
}
Quoted 57 lines
Quoted 57 lines
arch/x86/kernel/ptrace.c +1 -0
+}
include/uapi/linux/rseq.h +4 -0
index 9a402fdb60e9..3130232e6d0c 100644
@@ -20,12 +20,14 @@ enum rseq_cpu_id_state {
 
 enum rseq_flags {
 	RSEQ_FLAG_UNREGISTER = (1 << 0),
+	RSEQ_FLAG_QUERY_ABORT_AT_IP = (1 << 1),
 };
 
 enum rseq_cs_flags_bit {
 	RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT	= 0,
 	RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT	= 1,
 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT	= 2,
+	RSEQ_CS_FLAG_ABORT_AT_IP_BIT		= 3,
 };
 
 enum rseq_cs_flags {
@@ -35,6 +37,8 @@ enum rseq_cs_flags {
 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT),
 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE	=
 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT),
+	RSEQ_CS_FLAG_ABORT_AT_IP		=
+		(1U << RSEQ_CS_FLAG_ABORT_AT_IP_BIT),
 };
 
 /*
kernel/rseq.c +7 -0
index 6d45ac3dae7f..fb52f2d11b56 100644
@@ -21,6 +21,13 @@
 #define RSEQ_CS_PREEMPT_MIGRATE_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE | \
 				       RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT)
 
+#ifdef RSEQ_ARCH_HAS_ABORT_AT_IP
+static bool rseq_has_abort_at_ip(void) { return true; }
+#else
+static bool rseq_has_abort_at_ip(void) { return false; }
+static int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip) { return 0;
 }
+#endif
+
 /*
  *

* Restartable sequences are a lightweight interface that allows

kernel/rseq.c +4 -0
@@ -79,6 +86,16 @@
  *
  *       [abort_ip]
  *   F1. <failure>
+ *
+ * rseq critical sections defined with the RSEQ_CS_FLAG_ABORT_AT_IP flag
+ * have the following behavior on abort: when the stack grows down: the
+ * stack pointer is decremented to skip the redzone, and decremented of
Quoted 8 lines

kernel/rseq.c line 98

Quoted 6 lines
kernel/rseq.c +6 -0
+ * the pointer size.  The aborted address (abort-at-ip) is stored at
+ * this stack pointer location.  The user-space abort handler needs to
+ * pop the abort-at-ip address from the stack, and add the redzone size
+ * to the stack pointer.
+ *
+ * TODO: describe stack grows up.

Is this intentional or did you forget? :)

Since I did not implement abort-at-ip on stack-grows-up architectures, I felt it would be too early to describe the algorithm. I can simply remove the TODO altogether and we'll take care of it when we get there ? If I had to try to wordsmith it, it would look like e.g.:

* [...] When the stack grows up: the
* stack pointer is incremented to skip the redzone, and incremented of
* the pointer size. The aborted address (abort-at-ip) is stored immediately
* under this stack pointer location. The user-space abort handler needs to
* pop the abort-at-ip address from the stack, and subtract the redzone size
* from the stack pointer.

Quoted 4 lines

[ Please let me know if I got somehow confused in my understanding of stack grows up architectures. ]

I'm also unsure whether any of the stack grows up architecture have redzones ?

I don't think so? From when I last touched that piece of arch code when massaging copy_thread() I only remember parisc as having an upwards growing stack.

Quoted 2 lines

From a quick grep for redzone in Linux arch/, only openrisc, powerpc64 and x86-64 appear to have redzones.

author maintainer
#6 lore raw
Quoted 1 line · ----- On Jan 12, 2022, at 3:46 AM, Christian Brauner…

On Tue, Jan 11, 2022 at 12:43:05PM -0500, Mathieu Desnoyers wrote:

[...]

Quoted 29 lines
Quoted 29 lines
Quoted 11 lines
Quoted 10 lines

+ *
+ * rseq critical sections defined with the RSEQ_CS_FLAG_ABORT_AT_IP flag
+ * have the following behavior on abort: when the stack grows down: the
+ * stack pointer is decremented to skip the redzone, and decremented of
+ * the pointer size. The aborted address (abort-at-ip) is stored at
+ * this stack pointer location. The user-space abort handler needs to
+ * pop the abort-at-ip address from the stack, and add the redzone size
+ * to the stack pointer.
+ *
+ * TODO: describe stack grows up.

Quoted 1 line

Is this intentional or did you forget? :)

Since I did not implement abort-at-ip on stack-grows-up architectures, I felt it would be too early to describe the algorithm. I can simply remove the TODO altogether and we'll take care of it when we get there ? If I had to try to wordsmith it, it would look like e.g.:

* [...] When the stack grows up: the
* stack pointer is incremented to skip the redzone, and incremented of
* the pointer size. The aborted address (abort-at-ip) is stored immediately
* under this stack pointer location. The user-space abort handler needs to
* pop the abort-at-ip address from the stack, and subtract the redzone size
* from the stack pointer.

[ Please let me know if I got somehow confused in my understanding of stack grows
up architectures. ]

Quoted 8 lines
Quoted 1 line

I'm also unsure whether any of the stack grows up architecture have redzones ?

I don't think so? From when I last touched that piece of arch code when massaging copy_thread() I only remember parisc as having an upwards growing stack.

Quoted 2 lines

From a quick grep for redzone in Linux arch/, only openrisc, powerpc64 and x86-64 appear to have redzones.

I figured it was kind of silly to special-case arch-agnostic comments for stack grows up/down, how about the following instead ?

* rseq critical sections defined with the RSEQ_CS_FLAG_ABORT_AT_IP flag
* have the following behavior on abort: the stack pointer is adjusted to
* skip over the redzone [*], and the aborted address (abort-at-ip) is pushed
* at this stack pointer location. The user-space abort handler needs to pop
* the abort-at-ip address from the stack, and adjust the stack pointer to skip
* back over the redzone.
*
* [*] The openrisc, powerpc64 and x86-64 architectures define a "redzone" as a
* stack area beyond the stack pointer which can be used by the compiler
* to store local variables in leaf functions.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#7 lore raw
Quoted 45 lines · Mathieu Desnoyers
Quoted 1 line · ----- On Jan 12, 2022, at 3:46 AM, Christian Brauner…

On Tue, Jan 11, 2022 at 12:43:05PM -0500, Mathieu Desnoyers wrote:

[...]

Quoted 29 lines
Quoted 29 lines
Quoted 11 lines
Quoted 10 lines

+ *
+ * rseq critical sections defined with the RSEQ_CS_FLAG_ABORT_AT_IP flag
+ * have the following behavior on abort: when the stack grows down: the
+ * stack pointer is decremented to skip the redzone, and decremented of
+ * the pointer size. The aborted address (abort-at-ip) is stored at
+ * this stack pointer location. The user-space abort handler needs to
+ * pop the abort-at-ip address from the stack, and add the redzone size
+ * to the stack pointer.
+ *
+ * TODO: describe stack grows up.

Quoted 1 line

Is this intentional or did you forget? :)

Since I did not implement abort-at-ip on stack-grows-up architectures, I felt it would be too early to describe the algorithm. I can simply remove the TODO altogether and we'll take care of it when we get there ? If I had to try to wordsmith it, it would look like e.g.:

* [...] When the stack grows up: the
* stack pointer is incremented to skip the redzone, and incremented of
* the pointer size. The aborted address (abort-at-ip) is stored immediately
* under this stack pointer location. The user-space abort handler needs to
* pop the abort-at-ip address from the stack, and subtract the redzone size
* from the stack pointer.

[ Please let me know if I got somehow confused in my understanding of stack grows
up architectures. ]

Quoted 8 lines
Quoted 1 line

I'm also unsure whether any of the stack grows up architecture have redzones ?

I don't think so? From when I last touched that piece of arch code when massaging copy_thread() I only remember parisc as having an upwards growing stack.

Quoted 2 lines

From a quick grep for redzone in Linux arch/, only openrisc, powerpc64 and x86-64 appear to have redzones.

I figured it was kind of silly to special-case arch-agnostic comments for stack grows up/down, how about the following instead ?

Quoted 10 lines

* rseq critical sections defined with the RSEQ_CS_FLAG_ABORT_AT_IP flag
* have the following behavior on abort: the stack pointer is adjusted to
* skip over the redzone [*], and the aborted address (abort-at-ip) is pushed
* at this stack pointer location. The user-space abort handler needs to pop
* the abort-at-ip address from the stack, and adjust the stack pointer to skip
* back over the redzone.
*
* [*] The openrisc, powerpc64 and x86-64 architectures define a "redzone" as a
* stack area beyond the stack pointer which can be used by the compiler
* to store local variables in leaf functions.

Sounds good to me.

#8 lore raw
In reply to Mathieu Desnoyers
Quoted 3 lines

* [*] The openrisc, powerpc64 and x86-64 architectures define a "redzone" as a
* stack area beyond the stack pointer which can be used by the compiler
* to store local variables in leaf functions.

I wonder if that is really worth the trouble it causes! By the time a function is spilling values to stack the cost of a %sp update is almost certainly noise.

Someone clearly thought it was a 'good idea (tm)'.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author maintainer
#9 lore raw
Quoted 9 lines · ----- On Jan 12, 2022, at 9:58 AM, David Laight [email protected] wrote:
Quoted 3 lines

* [*] The openrisc, powerpc64 and x86-64 architectures define a "redzone" as a
* stack area beyond the stack pointer which can be used by the compiler
* to store local variables in leaf functions.

I wonder if that is really worth the trouble it causes! By the time a function is spilling values to stack the cost of a %sp update is almost certainly noise.

Someone clearly thought it was a 'good idea (tm)'.

I must admit that I've been surprised to learn about these redzones. Thanks for pointing them out to me, it was clearly a blind spot. I suspect it would be useful to introduce per-architecture KERNEL_REDZONE, USER_REDZONE and COMPAT_USER_REDZONE with a asm-generic version defining them to 0, with proper documentation. It would make it clearer to kernel developers working on stuff similar to signal handler delivery that they need to consider these carefully.

Mathieu

Quoted 7 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#10 lore raw

From: Mathieu Desnoyers

Quoted 12 lines

Sent: 12 January 2022 15:06

Quoted 8 lines · ----- On Jan 12, 2022, at 9:58 AM, David Laight [email protected] wrote:
Quoted 3 lines

* [*] The openrisc, powerpc64 and x86-64 architectures define a "redzone" as a
* stack area beyond the stack pointer which can be used by the compiler
* to store local variables in leaf functions.

I wonder if that is really worth the trouble it causes! By the time a function is spilling values to stack the cost of a %sp update is almost certainly noise.

Quoted 8 lines
Quoted 1 line

Someone clearly thought it was a 'good idea (tm)'.

I must admit that I've been surprised to learn about these redzones. Thanks for pointing them out to me, it was clearly a blind spot. I suspect it would be useful to introduce per-architecture KERNEL_REDZONE, USER_REDZONE and COMPAT_USER_REDZONE with a asm-generic version defining them to 0, with proper documentation. It would make it clearer to kernel developers working on stuff similar to signal handler delivery that they need to consider these carefully.

They can never be used in kernel - any ISR would overwrite them.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author maintainer
#11 lore raw
Quoted 25 lines · ----- On Jan 12, 2022, at 10:15 AM, David Laight [email protected] wrote:

From: Mathieu Desnoyers

Quoted 14 lines

Sent: 12 January 2022 15:06

Quoted 9 lines · ----- On Jan 12, 2022, at 9:58 AM, David Laight [email protected] wrote:
Quoted 3 lines

* [*] The openrisc, powerpc64 and x86-64 architectures define a "redzone" as a
* stack area beyond the stack pointer which can be used by the compiler
* to store local variables in leaf functions.

I wonder if that is really worth the trouble it causes! By the time a function is spilling values to stack the cost of a %sp update is almost certainly noise.

Someone clearly thought it was a 'good idea (tm)'.

Quoted 9 lines

I must admit that I've been surprised to learn about these redzones. Thanks for pointing them out to me, it was clearly a blind spot. I suspect it would be useful
to introduce per-architecture KERNEL_REDZONE, USER_REDZONE and COMPAT_USER_REDZONE
with a asm-generic version defining them to 0, with proper documentation. It would
make it clearer to kernel developers working on stuff similar to signal handler delivery that they need to consider these carefully.

Quoted 1 line

They can never be used in kernel - any ISR would overwrite them.

arch/powerpc/include/asm/ptrace.h define those for ppc64:

113:#define USER_REDZONE_SIZE 512
114:#define KERNEL_REDZONE_SIZE 288

and then uses the kernel redzone size for:

#define STACK_INT_FRAME_SIZE (sizeof(struct pt_regs) + \
STACK_FRAME_OVERHEAD + KERNEL_REDZONE_SIZE)

which AFAIU should ensure that ISR don't overwrite the redzone within the kernel.

Thanks,

Mathieu

Quoted 7 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
maintainer
#12 lore raw
In reply to David Laight
Quoted 15 lines · David Laight

From: Mathieu Desnoyers

Quoted 14 lines

Sent: 12 January 2022 15:06

Quoted 9 lines · ----- On Jan 12, 2022, at 9:58 AM, David Laight [email protected] wrote:
Quoted 3 lines

* [*] The openrisc, powerpc64 and x86-64 architectures define a "redzone" as a
* stack area beyond the stack pointer which can be used by the compiler
* to store local variables in leaf functions.

I wonder if that is really worth the trouble it causes! By the time a function is spilling values to stack the cost of a %sp update is almost certainly noise.

Someone clearly thought it was a 'good idea (tm)'.

Quoted 8 lines
Quoted 6 lines

I must admit that I've been surprised to learn about these redzones. Thanks for pointing them out to me, it was clearly a blind spot. I suspect it would be useful to introduce per-architecture KERNEL_REDZONE, USER_REDZONE and COMPAT_USER_REDZONE with a asm-generic version defining them to 0, with proper documentation. It would make it clearer to kernel developers working on stuff similar to signal handler delivery that they need to consider these carefully.

They can never be used in kernel - any ISR would overwrite them.

That depends on how the architecture does exceptions; also consider:

https://www.intel.com/content/www/us/en/develop/download/flexible-return-and-event-delivery-specification.html

#13 lore raw

From: Peter Zijlstra

Quoted 25 lines

Sent: 12 January 2022 15:34

Quoted 22 lines · David Laight

From: Mathieu Desnoyers

Quoted 20 lines

Sent: 12 January 2022 15:06

Quoted 9 lines · ----- On Jan 12, 2022, at 9:58 AM, David Laight [email protected] wrote:
Quoted 3 lines

* [*] The openrisc, powerpc64 and x86-64 architectures define a "redzone" as a
* stack area beyond the stack pointer which can be used by the compiler
* to store local variables in leaf functions.

I wonder if that is really worth the trouble it causes! By the time a function is spilling values to stack the cost of a %sp update is almost certainly noise.

Someone clearly thought it was a 'good idea (tm)'.

I must admit that I've been surprised to learn about these redzones. Thanks for pointing them out to me, it was clearly a blind spot. I suspect it would be useful to introduce per-architecture KERNEL_REDZONE, USER_REDZONE and COMPAT_USER_REDZONE with a asm-generic version defining them to 0, with proper documentation. It would make it clearer to kernel developers working on stuff similar to signal handler delivery that they need to consider these carefully.

Quoted 3 lines
Quoted 1 line

They can never be used in kernel - any ISR would overwrite them.

That depends on how the architecture does exceptions;

True, many newer ones don't actually write anything to the stack. Makes the cpu simpler.

Quoted 4 lines

also consider:

https://www.intel.com/content/www/us/en/develop/download/flexible-return-and-event-delivery-
specification.html

That contains the snippet:
The SWAPGS instruction supports efficient updates of the GS base address.

Which is just so horribly not true...
Even FRED is always doing a GS swap - so you can easily lose the kernel GS value.

I remember fixing all the 'in kernel' faults in the netbsd x86-64 return to user path. Entirely horrid...

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#14 lore raw
In reply to Mathieu Desnoyers
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Mathieu,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on tip/x86/core tip/master linus/master v5.16 next-20220112] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Mathieu-Desnoyers/rseq-x86-implement-abort-at-ip-extension/20220111-011941
base:   https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
config: i386-randconfig-a015 (https://download.01.org/0day-ci/archive/20220113/202201130222.lBbn1hO4-lkp(a)intel.com/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 244dd2913a43a200f5a6544d424cdc37b771028b)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/8731a339925d9270b03a51b7c1b54e718dded248
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Mathieu-Desnoyers/rseq-x86-implement-abort-at-ip-extension/20220111-011941 git checkout 8731a339925d9270b03a51b7c1b54e718dded248
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash arch/x86/kernel/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> arch/x86/kernel/ptrace.c:1372:5: warning: no previous prototype for function 'rseq_abort_at_ip' [-Wmissing-prototypes]
   int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)
       ^

arch/x86/kernel/ptrace.c:1372:1: note: declare 'static' if the function is not intended to be used outside of this translation unit int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)
^
static
1 warning generated.

vim +/rseq_abort_at_ip +1372 arch/x86/kernel/ptrace.c

1371

> 1372	int rseq_abort_at_ip(struct pt_regs *regs, unsigned long ip)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org