BACKPORT: mremap: properly flush TLB before releasing the page

commit eb66ae030829605d61fbef1909ce310e29f78821 upstream.

Jann Horn points out that our TLB flushing was subtly wrong for the
mremap() case.  What makes mremap() special is that we don't follow the
usual "add page to list of pages to be freed, then flush tlb, and then
free pages".  No, mremap() obviously just _moves_ the page from one page
table location to another.

That matters, because mremap() thus doesn't directly control the
lifetime of the moved page with a freelist: instead, the lifetime of the
page is controlled by the page table locking, that serializes access to
the entry.

As a result, we need to flush the TLB not just before releasing the lock
for the source location (to avoid any concurrent accesses to the entry),
but also before we release the destination page table lock (to avoid the
TLB being flushed after somebody else has already done something to that

This also makes the whole "need_flush" logic unnecessary, since we now
always end up flushing the TLB for every valid entry.

Reported-and-tested-by: Jann Horn <>
Acked-by: Will Deacon <>
Tested-by: Ingo Molnar <>
Acked-by: Peter Zijlstra (Intel) <>
Signed-off-by: Linus Torvalds <>
Signed-off-by: Greg Kroah-Hartman <>

(cherry picked from commit 541500abfe9eb30a89ff0a6eb42a21521996d68d)

TEST=lakitu-release tryjob

Change-Id: I669e272e097327ad7b07c35e8607a17baf21dec9
Tested-by: Daniel Wang <>
Reviewed-by: Guenter Roeck <>
Commit-Queue: Daniel Wang <>
3 files changed