Re: [PATCHv2] mm, page_vma_mapped: Drop faulty pointer arithmetics in check_pte()

From: Linus Torvalds
Date: Fri Jan 19 2018 - 13:02:08 EST


On Fri, Jan 19, 2018 at 4:49 AM, Kirill A. Shutemov
<kirill.shutemov@xxxxxxxxxxxxxxx> wrote:
>
> + if (pfn < page_to_pfn(pvmw->page))
> + return false;
> +
> + /* THP can be referenced by any subpage */
> + if (pfn - page_to_pfn(pvmw->page) >= hpage_nr_pages(pvmw->page))
> + return false;
> +

Is gcc actually clever enough to merge these? The "page_to_pfn()"
logic can be pretty expensive (exactly for the sparsemem case, but
per-node DISCOTIGMEM has some complexity too.

So I'd prefer to make that explicit, perhaps by having a helper
function that does this something like

static inline bool pfn_in_hpage(unsigned long pfn, struct page *hpage)
{
unsigned long hpage_pfn = page_to_pfn(hpage);

return pfn >= hpage_pfn && pfn - hpage_pfn < hpage_nr_pages(hpage);
}

and then just use

return pfn_in_hpage(pfn, pvmw->page);

in that caller. Hmm? Wouldn't that be more legible, and avoid the
repeated pvmw->page and page_to_pfn() cases?

Even if maybe gcc can do the CSE and turn it all into the same thing
in the end..

Linus