Re: [RFCv2 05/10] x86/mm: Provide untagged_addr() helper

Peter Zijlstra
2022-05-12T14:24:21Z

On Thu, May 12, 2022 at 03:06:38PM +0200, Thomas Gleixner wrote:

> #define untagged_addr(addr) ({ \
> u64 __addr = (__force u64)(addr); \
> \
> __addr &= current->thread.lam_untag_mask; \
> (__force __typeof__(addr))__addr; \
> })
>
> No conditionals, fast _and_ correct. Setting this untag mask up once
> when LAM is enabled is not rocket science.

But that goes wrong if someone ever wants to untag a kernel address and
not use the result for access_ok().

I'd feel better about something like:

s64 __addr = (addr);
s64 __sign = __addr;

__sign >>= 63;
__sign &= lam_untag_mask;
__addr &= lam_untag_mask;
__addr |= __sign;

__addr;

Which simply extends bit 63 downwards -- although possibly there's an
easier way to do that, this is pretty gross.