Re: [PATCH 05/12] powerpc: add pte_free_defer() for pgtables sharing page

Jason Gunthorpe
2023-06-02T14:20:22Z

On Mon, May 29, 2023 at 03:02:02PM +0100, Matthew Wilcox wrote:
> On Sun, May 28, 2023 at 11:20:21PM -0700, Hugh Dickins wrote:
> > +void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable)
> > +{
> > + struct page *page;
> > +
> > + page = virt_to_page(pgtable);
> > + call_rcu(&page->rcu_head, pte_free_now);
> > +}
>
> This can't be safe (on ppc). IIRC you might have up to 16x4k page
> tables sharing one 64kB page. So if you have two page tables from the
> same page being defer-freed simultaneously, you'll reuse the rcu_head
> and I cannot imagine things go well from that point.
>
> I have no idea how to solve this problem.

Maybe power and s390 should allocate a side structure, sort of a
pre-memdesc thing to store enough extra data?

If we can get enough bytes then something like this would let a single
rcu head be shared to manage the free bits.

struct 64k_page {
u8 free_pages;
u8 pending_rcu_free_pages;
struct rcu_head head;
}

free_sub_page(sub_id)
if (atomic_fetch_or(1 << sub_id, &64k_page->pending_rcu_free_pages))
call_rcu(&64k_page->head)

rcu_func()
64k_page->free_pages |= atomic_xchg(0, &64k_page->pending_rcu_free_pages)

if (64k_pages->free_pages == all_ones)
free_pgea(64k_page);

Jason