Re: [PATCH v6 15/18] x86/kasan: Handle UD1 for inline KASAN reports

From: Peter Zijlstra

Date: Tue Nov 18 2025 - 15:36:03 EST


On Mon, Nov 17, 2025 at 09:47:20AM +0000, Maciej Wieczór-Retman wrote:

> >> +void kasan_inline_handler(struct pt_regs *regs)
> >> +{
> >> + int metadata = regs->cx;
> >> + u64 addr = regs->di;
> >> + u64 pc = regs->ip;
> >> + bool recover = metadata & KASAN_ECX_RECOVER;
> >> + bool write = metadata & KASAN_ECX_WRITE;
> >> + size_t size = KASAN_ECX_SIZE(metadata);
> >> +
> >> + if (user_mode(regs))
> >> + return;
> >> +
> >> + if (!kasan_report((void *)addr, size, write, pc))
> >> + return;
> >> +
> >> + kasan_die_unless_recover(recover, "Oops - KASAN", regs, metadata, die);
> >> +}
> >
> >I'm confused. Going by the ARM64 code, the meta-data is constant per
> >site -- it is encoded in the break immediate.
> >
> >And I suggested you do the same on x86 by using the single byte
> >displacement instruction encoding.
> >
> > ud1 0xFF(%ecx), %ecx
> >
> >Also, we don't have to use a fixed register for the address, you can do:
> >
> > ud1 0xFF(%ecx), %reg
> >
> >and have %reg tell us what register the address is in.
> >
> >Then you can recover the meta-data from the displacement immediate and
> >the address from whatever register is denoted.
> >
> >This avoids the 'callsite' from having to clobber cx and move the address
> >into di.
> >
> >What you have here will work, and I don't suppose we care about code
> >density with KASAN much, but it could've been so much better :/
>
> Thanks for checking the patch out, maybe I got too focused on just
> getting clang to work. You're right, I'll try using the displacement
> encoding.
>
> I was attempting a few different encodings because clang was fussy about
> putting data where I wanted it. The one in the patch worked fine and I
> thought it'd be consistent with the form that UBSan uses. But yeah, I'll
> work on it more.
>
> I'll also go and rebase my series onto your WARN() hackery one since
> there are a lot of changes to traps.c.

Thanks!