On 3/29/22 18:04, David Hildenbrand wrote: > Whenever GUP currently ends up taking a R/O pin on an anonymous page that > might be shared -- mapped R/O and !PageAnonExclusive() -- any write fault > on the page table entry will end up replacing the mapped anonymous page > due to COW, resulting in the GUP pin no longer being consistent with the > page actually mapped into the page table. > > The possible ways to deal with this situation are: > (1) Ignore and pin -- what we do right now. > (2) Fail to pin -- which would be rather surprising to callers and > could break user space. > (3) Trigger unsharing and pin the now exclusive page -- reliable R/O > pins. > > Let's implement 3) because it provides the clearest semantics and > allows for checking in unpin_user_pages() and friends for possible BUGs: > when trying to unpin a page that's no longer exclusive, clearly > something went very wrong and might result in memory corruptions that > might be hard to debug. So we better have a nice way to spot such > issues. > > This change implies that whenever user space *wrote* to a private > mapping (IOW, we have an anonymous page mapped), that GUP pins will > always remain consistent: reliable R/O GUP pins of anonymous pages. > > As a side note, this commit fixes the COW security issue for hugetlb with > FOLL_PIN as documented in: > https://lore.kernel.org/r/3ae33b08-d9ef-f846-56fb-645e3b9b4c66@xxxxxxxxxx > The vmsplice reproducer still applies, because vmsplice uses FOLL_GET > instead of FOLL_PIN. > > Note that follow_huge_pmd() doesn't apply because we cannot end up in > there with FOLL_PIN. > > This commit is heavily based on prototype patches by Andrea. > > Co-developed-by: Andrea Arcangeli <aarcange@xxxxxxxxxx> > Signed-off-by: Andrea Arcangeli <aarcange@xxxxxxxxxx> > Signed-off-by: David Hildenbrand <david@xxxxxxxxxx> Acked-by: Vlastimil Babka <vbabka@xxxxxxx>