Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address

From: Jarkko Sakkinen
Date: Tue Sep 06 2022 - 06:27:05 EST


On Tue, Aug 09, 2022 at 06:55:43PM +0200, Borislav Petkov wrote:
> On Mon, Jun 20, 2022 at 11:03:43PM +0000, Ashish Kalra wrote:
> > + pfn = pte_pfn(*pte);
> > +
> > + /* If its large page then calculte the fault pfn */
> > + if (level > PG_LEVEL_4K) {
> > + unsigned long mask;
> > +
> > + mask = pages_per_hpage(level) - pages_per_hpage(level - 1);
> > + pfn |= (address >> PAGE_SHIFT) & mask;
>
> Oh boy, this is unnecessarily complicated. Isn't this
>
> pfn |= pud_index(address);
>
> or
> pfn |= pmd_index(address);

I played with this a bit and ended up with

pfn = pte_pfn(*pte) | PFN_DOWN(address & page_level_mask(level - 1));

Unless I got something terribly wrong, this should do the
same (see the attached patch) as the existing calculations.

BR, Jarkko