Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm/mremap.c: fix extent calculation

When `next < old_addr`, `next - old_addr` arithmetic underflows causing
`extent` to be incorrect.

Make `extent` the smaller of `next - old_addr` or `old_end - old_addr`.

Link: https://lkml.kernel.org/r/20201219170433.2418867-1-kaleshsingh@google.com
Fixes: c49dd34018026 ("mm: speedup mremap on 1GB or larger regions")
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Reported-by: Guenter Roeck <linux@roeck-us.net>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Lokesh Gidra <lokeshgidra@google.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Kalesh Singh and committed by
Linus Torvalds
e05986ee dc2da7b4

+3 -1
+3 -1
mm/mremap.c
··· 358 358 359 359 next = (old_addr + size) & mask; 360 360 /* even if next overflowed, extent below will be ok */ 361 - extent = (next > old_end) ? old_end - old_addr : next - old_addr; 361 + extent = next - old_addr; 362 + if (extent > old_end - old_addr) 363 + extent = old_end - old_addr; 362 364 next = (new_addr + size) & mask; 363 365 if (extent > next - new_addr) 364 366 extent = next - new_addr;