Re: [PATCH -next] mm/filemap: fix a data race in filemap_fault()

From: Matthew Wilcox
Date: Mon Feb 10 2020 - 12:25:17 EST


On Mon, Feb 10, 2020 at 12:00:29PM -0500, Qian Cai wrote:
> @@ -2622,7 +2622,7 @@ void filemap_map_pages(struct vm_fault *vmf,
> if (page->index >= max_idx)
> goto unlock;
>
> - if (file->f_ra.mmap_miss > 0)
> + if (data_race(file->f_ra.mmap_miss > 0))
> file->f_ra.mmap_miss--;

How is this safe? Two threads can each see 1, and then both decrement the
in-memory copy, causing it to end up at -1.