Re: [PATCH] mm: gup: fix the fast GUP race against THP collapse

From: Jason Gunthorpe
Date: Tue Sep 06 2022 - 10:23:31 EST


On Mon, Sep 05, 2022 at 09:59:47AM +0200, David Hildenbrand wrote:

> > That should be READ_ONCE() for the *pmdp and *ptep reads. Because this
> > whole lockless house of cards may fall apart if we try reading the
> > page table values without READ_ONCE().
>
> I came to the conclusion that the implicit memory barrier when grabbing a
> reference on the page is sufficient such that we don't need READ_ONCE here.

READ_ONCE is not about barriers or ordering, you still need the
acquire inside the atomic to make the algorithm work.

READ_ONCE primarily is a marker that the data being read is unstable
and that the compiler must avoid all instability when reading it. eg
in this case the compiler could insanely double read the value, even
though the 'if' requires only a single read. This would result in
corrupt calculation.

> If we still intend to change that code, we should fixup all GUP-fast
> functions in a similar way.

This is correct, IMHO we have been slowly modernizing the mm approach
to the memory model to include things like this. While it would be
nice to do everything I think small bits are welcomed as the are
discovered.

Jason