Re: [PATCH 0/3] KVM: x86/mmu: Add macro for hugepage GFN mask

From: Paolo Bonzini
Date: Thu Oct 29 2020 - 03:24:11 EST


On 28/10/20 16:29, Sean Christopherson wrote:
> The naming and usage also aligns with the kernel, which defines PAGE, PMD and
> PUD masks, and has near identical usage patterns.
>
> #define PAGE_SIZE (_AC(1,UL) << PAGE_SHIFT)
> #define PAGE_MASK (~(PAGE_SIZE-1))
>
> #define PMD_PAGE_SIZE (_AC(1, UL) << PMD_SHIFT)
> #define PMD_PAGE_MASK (~(PMD_PAGE_SIZE-1))
>
> #define PUD_PAGE_SIZE (_AC(1, UL) << PUD_SHIFT)
> #define PUD_PAGE_MASK (~(PUD_PAGE_SIZE-1))

Well, PAGE_MASK is also one of my pet peeves for Linux. At least I am
consistent. :)

>> and of course if you're debugging it you have to
>> look closer and check if it's really "x & -y" or "x & ~y", but at least
>> in normal cursory code reading that's how it works for me.
>
> IMO, "x & -y" has a higher barrier to entry, especially when the kernel's page
> masks uses "x & ~(y - 1))". But, my opinion is definitely colored by my
> inability to read two's-complement on the fly.

Fair enough. What about having instead

#define KVM_HPAGE_GFN_BASE(gfn, level) \
(x & ~(KVM_PAGES_PER_HPAGE(gfn) - 1))
#define KVM_HPAGE_GFN_INDEX(gfn, level) \
(x & (KVM_PAGES_PER_HPAGE(gfn) - 1))

?

Paolo