Re: [PATCH 08/10] mm/hugetlb: Make walk_hugetlb_range() safe to pmd unshare

From: Peter Xu
Date: Tue Dec 06 2022 - 11:46:18 EST


On Mon, Dec 05, 2022 at 03:52:51PM -0800, John Hubbard wrote:
> On 12/5/22 15:33, Mike Kravetz wrote:
> > On 11/29/22 14:35, Peter Xu wrote:
> > > Since walk_hugetlb_range() walks the pgtable, it needs the vma lock
> > > to make sure the pgtable page will not be freed concurrently.
> > >
> > > Signed-off-by: Peter Xu <peterx@xxxxxxxxxx>
> > > ---
> > > mm/pagewalk.c | 2 ++
> > > 1 file changed, 2 insertions(+)
> > >
> > > diff --git a/mm/pagewalk.c b/mm/pagewalk.c
> > > index 7f1c9b274906..d98564a7be57 100644
> > > --- a/mm/pagewalk.c
> > > +++ b/mm/pagewalk.c
> > > @@ -302,6 +302,7 @@ static int walk_hugetlb_range(unsigned long addr, unsigned long end,
> > > const struct mm_walk_ops *ops = walk->ops;
> > > int err = 0;
> > > + hugetlb_vma_lock_read(vma);
> > > do {
> > > next = hugetlb_entry_end(h, addr, end);
> > > pte = huge_pte_offset(walk->mm, addr & hmask, sz);
> >
> > For each found pte, we will be calling mm_walk_ops->hugetlb_entry() with
> > the vma_lock held. I looked into the various hugetlb_entry routines, and
> > I am not sure about hmm_vma_walk_hugetlb_entry. It seems like it could
> > possibly call hmm_vma_fault -> handle_mm_fault -> hugetlb_fault. If this
> > can happen, then we may have an issue as hugetlb_fault will also need to
> > acquire the vma_lock in read mode.

Thanks for spotting that, Mike.

I used to notice that path special but that's when I was still using RCU
locks who doesn't have the issue. Then I overlooked this one when
switchover.

> >
> > I do not know the hmm code well enough to know if this may be an actual
> > issue?
>
> Oh, this sounds like a serious concern. If we add a new lock, and hold it
> during callbacks that also need to take it, that's not going to work out,
> right?
>
> And yes, hmm_range_fault() and related things do a good job of revealing
> this kind of deadlock. :)

I've got a fixup attached. John, since this got your attention please also
have a look too in case there's further issues.

Thanks,

--
Peter Xu