Re: [PATCH 3/4] rseq: Make rseq work with protection keys

From: Dave Hansen
Date: Fri Feb 21 2025 - 16:46:06 EST


On 2/21/25 13:36, Mathieu Desnoyers wrote:
>>>
>>
>> Because the rseq return to userspace handler is called on every return
>> to userspace after a task is scheduled back after preemption, I am
>> concerned about the overhead that would be added by a WRPKRU on the
>> fast-path, given that it acts as as barrier against speculation. Issuing
>> WRPKRU only after checking that pkey-0 is not accessible appears to be
>> moving the overhead to a much less common case.
>
> Actually, we should distinguish between two accesses here:
>
> A) loads/stores from/to struct rseq
>
> B) loads from struct rseq_cs (only happens on rseq abort)
>
> (A) is a fast-path executed on return to userspace after a preemption.
> In order to make it fast, we could require that struct rseq is pkey-0
> and typically skip any WRPKRU for this access when pkey-0 is already
> accessible. We can add a check on rseq registration to make sure that
> struct rseq is indeed pkey-0, and reject it with an error if not. This
> should help make the ABI robust and less error-prone.
>
> Now for (B), it's a slow path. When we observe that rseq->rseq_cs is
> not NULL, we can simply override with a permissive pkey to make sure
> the rseq_cs access will work.
>
> Thoughts ?
I think this will be the first ABI which is explicitly pkey-0-only. I
suspect there are a few more of these that are implicit but we just
haven't found them yet.

I wouldn't have any objections about doing this, especially given
sanity checking at rseq registration.