On Fri 22-09-17 11:46:31, Shaohua Li wrote: > From: Shaohua Li <shli@xxxxxx> > > MADV_FREE clears pte dirty bit and then marks the page lazyfree (clear > SwapBacked). There is no lock to prevent the page is added to swap cache > between these two steps by page reclaim. If page reclaim finds such > page, it will simply add the page to swap cache without pageout the page > to swap because the page is marked as clean. Next time, page fault will > read data from the swap slot which doesn't have the original data, so we > have a data corruption. To fix issue, we mark the page dirty and pageout > the page. > > However, we shouldn't dirty all pages which is clean and in swap cache. > swapin page is swap cache and clean too. So we only dirty page which is > added into swap cache in page reclaim, which shouldn't be swapin page. > As Minchan suggested, simply dirty the page in add_to_swap can do the > job. > > Reported-by: Artem Savkov <asavkov@xxxxxxxxxx> > Fix: 802a3a92ad7a(mm: reclaim MADV_FREE pages) > Signed-off-by: Shaohua Li <shli@xxxxxx> > Cc: stable@xxxxxxxxxxxxxxx > Cc: Johannes Weiner <hannes@xxxxxxxxxxx> > Cc: Michal Hocko <mhocko@xxxxxxxx> > Cc: Hillf Danton <hillf.zj@xxxxxxxxxxxxxxx> > Cc: Minchan Kim <minchan@xxxxxxxxxx> > Cc: Hugh Dickins <hughd@xxxxxxxxxx> > Cc: Rik van Riel <riel@xxxxxxxxxx> > Cc: Mel Gorman <mgorman@xxxxxxxxxxxxxxxxxxx> > Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> OK the patch makes sense to me Acked-by: Michal Hocko <mhocko@xxxxxxxx> > --- > mm/swap_state.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/mm/swap_state.c b/mm/swap_state.c > index 71ce2d1..2d64ec4 100644 > --- a/mm/swap_state.c > +++ b/mm/swap_state.c > @@ -231,7 +231,7 @@ int add_to_swap(struct page *page) > * deadlock in the swap out path. > */ > /* > - * Add it to the swap cache. > + * Add it to the swap cache and mark it dirty > */ this comment is simply useless. It doesn't explain why we need it and it is trivial to see what we do there. So I would remove it altogether or make it more useful explaning the MADV_FREE race which would be IMHO much better. > err = add_to_swap_cache(page, entry, > __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN); > @@ -242,6 +242,7 @@ int add_to_swap(struct page *page) > * clear SWAP_HAS_CACHE flag. > */ > goto fail; > + set_page_dirty(page); > > return 1; > > -- > 2.9.5 > -- Michal Hocko SUSE Labs