Re: [PATCH glibc 5/9] glibc: Perform rseq(2) registration at C startup and thread creation (v17)

From: Mathieu Desnoyers
Date: Tue Apr 28 2020 - 08:56:33 EST


----- On Apr 28, 2020, at 8:33 AM, Mathieu Desnoyers mathieu.desnoyers@xxxxxxxxxxxx wrote:

> ----- On Apr 28, 2020, at 8:02 AM, Florian Weimer fw@xxxxxxxxxxxxx wrote:
>
[...]
>>
>>> x32 should not be an issue as explained above, so I'm very open to
>>> add this "uptr" for user-space only.
>>
>> Okay, then please use anonymous unions and structs as necessary, to
>> ensure that the uptr field can be reached on all platforms in the same
>> way.
>
> OK, will do!

What I came up with looks like this. User-space can use rseq_cs.uptr.ptr
both on 32-bit and 64-bit to update the pointer:

/* Restartable sequences rseq_cs field.

Contains NULL when no critical section is active for the current
thread, or holds a pointer to the currently active struct rseq_cs.

Updated by user-space, which sets the address of the currently
active rseq_cs at the beginning of assembly instruction sequence
block, and set to NULL by the kernel when it restarts an assembly
instruction sequence block, as well as when the kernel detects that
it is preempting or delivering a signal outside of the range
targeted by the rseq_cs. Also needs to be set to NULL by user-space
before reclaiming memory that contains the targeted struct rseq_cs.

Read and set by the kernel. Set by user-space with single-copy
atomicity semantics. This field should only be updated by the
thread which registered this data structure. Aligned on 64-bit.

User-space may perform the update through the rseq_cs.uptr.ptr
field. The padding needs to be initialized to zero on 32-bit. */
union
{
uint64_t ptr64;
#ifdef __LP64__
uint64_t ptr;
#else
struct
{
# if (defined (__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)) || defined (__BIG_ENDIAN)
uint32_t padding; /* Initialized to zero. */
uint32_t ptr32;
# else /* LITTLE */
uint32_t ptr32;
uint32_t padding; /* Initialized to zero. */
# endif /* ENDIAN */
} ptr;
#endif

#ifndef __KERNEL__
struct
{
# ifdef __LP64__
const struct rseq_cs *ptr;
# else
# if (defined (__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)) || defined (__BIG_ENDIAN)
uint32_t padding; /* Initialized to zero. */
const struct rseq_cs *ptr;
# else /* LITTLE */
const struct rseq_cs *ptr;
uint32_t padding; /* Initialized to zero. */
# endif /* ENDIAN */
# endif
} uptr;
#endif
} rseq_cs;

Thanks,

Mathieu


--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com