[PATCH v2] rseq: Clear stale yield state on slice grant

From: Shawn Shao

Date: Fri Dec 04 2026 - 22:41:19 EST


rseq_syscall_enter_work() can set the yield result before ptrace or
seccomp decides whether to dispatch the syscall. If entry processing
aborts the call or changes the syscall number, the syscall body does
not consume the result.

The return value is valid only when rseq_slice_yield() follows a
grant observed by user space at the end of the corresponding critical
section. Clear the result on the next grant so an aborted call cannot
provide stale success if that grant is revoked before syscall entry work.

Document this userspace validity rule.

Tested on x86-64 QEMU TCG with
CONFIG_RSEQ_SLICE_EXTENSION=y. The regression returned 1 on the unpatched
kernel and 0 with this patch after a grant was observed and then revoked. A
live-grant control still returned 1.

Link: https://gist.github.com/shaos26/525e502547c0fd673e93067aed7f6d88

Fixes: dd0a04606937 ("rseq: Implement syscall entry work for time slice extensions")
Signed-off-by: Shawn Shao <shawnq1@xxxxxxx>
---
Documentation/userspace-api/rseq.rst | 6 ++++++
include/linux/rseq_entry.h | 2 ++
2 files changed, 8 insertions(+)

Changes in v2:
- Clear stale yield state on the next slice extension grant.
- Document when the rseq_slice_yield() return value is valid.

diff --git a/Documentation/userspace-api/rseq.rst b/Documentation/userspace-api/rseq.rst
index 8549a6c61531c..475fb023f4a7f 100644
--- a/Documentation/userspace-api/rseq.rst
+++ b/Documentation/userspace-api/rseq.rst
@@ -191,6 +191,12 @@ leaving the critical section to relinquish the CPU. The kernel enforces
this by arming a timer to prevent misbehaving userspace from abusing this
mechanism.

+The return value from ``rseq_slice_yield(2)`` is valid only when the syscall
+follows userspace observing the granted bit set at the end of the
+corresponding critical section. A call outside this sequence is invalid; its
+return value must not be used to determine the result of an earlier or
+unrelated time slice extension request.
+
If both the request bit and the granted bit are false when leaving the
critical section, then this indicates that a grant was revoked and no
further action is required by userspace.
diff --git a/include/linux/rseq_entry.h b/include/linux/rseq_entry.h
index 31ce349ed42ce..7806fd8c51bde 100644
--- a/include/linux/rseq_entry.h
+++ b/include/linux/rseq_entry.h
@@ -174,6 +174,8 @@ static __always_inline bool __rseq_grant_slice_extension(bool work_pending)
rseq_stat_inc(rseq_stats.s_granted);

curr->rseq.slice.state.granted = true;
+ /* Clear stale state left by an aborted rseq_slice_yield() syscall. */
+ curr->rseq.slice.yielded = false;
/* Store expiry time for arming the timer on the way out */
curr->rseq.slice.expires = data_race(rseq_slice_ext_nsecs) + ktime_get_mono_fast_ns();
/*