Eric Biggers
0f49b0519f
mm/madvise.c: fix freeing of locked page with MADV_FREE
...
commit 263630e8d176d87308481ebdcd78ef9426739c6b upstream.
If madvise(..., MADV_FREE) split a transparent hugepage, it called
put_page() before unlock_page().
This was wrong because put_page() can free the page, e.g. if a
concurrent madvise(..., MADV_DONTNEED) has removed it from the memory
mapping. put_page() then rightfully complained about freeing a locked
page.
Fix this by moving the unlock_page() before put_page().
This bug was found by syzkaller, which encountered the following splat:
BUG: Bad page state in process syzkaller412798 pfn:1bd800
page:ffffea0006f60000 count:0 mapcount:0 mapping: (null) index:0x20a00
flags: 0x200000000040019(locked|uptodate|dirty|swapbacked)
raw: 0200000000040019 0000000000000000 0000000000020a00 00000000ffffffff
raw: ffffea0006f60020 ffffea0006f60020 0000000000000000 0000000000000000
page dumped because: PAGE_FLAGS_CHECK_AT_FREE flag(s) set
bad because of flags: 0x1(locked)
Modules linked in:
CPU: 1 PID: 3037 Comm: syzkaller412798 Not tainted 4.13.0-rc5+ #35
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:16 [inline]
dump_stack+0x194/0x257 lib/dump_stack.c:52
bad_page+0x230/0x2b0 mm/page_alloc.c:565
free_pages_check_bad+0x1f0/0x2e0 mm/page_alloc.c:943
free_pages_check mm/page_alloc.c:952 [inline]
free_pages_prepare mm/page_alloc.c:1043 [inline]
free_pcp_prepare mm/page_alloc.c:1068 [inline]
free_hot_cold_page+0x8cf/0x12b0 mm/page_alloc.c:2584
__put_single_page mm/swap.c:79 [inline]
__put_page+0xfb/0x160 mm/swap.c:113
put_page include/linux/mm.h:814 [inline]
madvise_free_pte_range+0x137a/0x1ec0 mm/madvise.c:371
walk_pmd_range mm/pagewalk.c:50 [inline]
walk_pud_range mm/pagewalk.c:108 [inline]
walk_p4d_range mm/pagewalk.c:134 [inline]
walk_pgd_range mm/pagewalk.c:160 [inline]
__walk_page_range+0xc3a/0x1450 mm/pagewalk.c:249
walk_page_range+0x200/0x470 mm/pagewalk.c:326
madvise_free_page_range.isra.9+0x17d/0x230 mm/madvise.c:444
madvise_free_single_vma+0x353/0x580 mm/madvise.c:471
madvise_dontneed_free mm/madvise.c:555 [inline]
madvise_vma mm/madvise.c:664 [inline]
SYSC_madvise mm/madvise.c:832 [inline]
SyS_madvise+0x7d3/0x13c0 mm/madvise.c:760
entry_SYSCALL_64_fastpath+0x1f/0xbe
Here is a C reproducer:
#define _GNU_SOURCE
#include <pthread.h>
#include <sys/mman.h>
#include <unistd.h>
#define MADV_FREE 8
#define PAGE_SIZE 4096
static void *mapping;
static const size_t mapping_size = 0x1000000;
static void *madvise_thrproc(void *arg)
{
madvise(mapping, mapping_size, (long)arg);
}
int main(void)
{
pthread_t t[2];
for (;;) {
mapping = mmap(NULL, mapping_size, PROT_WRITE,
MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
munmap(mapping + mapping_size / 2, PAGE_SIZE);
pthread_create(&t[0], 0, madvise_thrproc, (void*)MADV_DONTNEED);
pthread_create(&t[1], 0, madvise_thrproc, (void*)MADV_FREE);
pthread_join(t[0], NULL);
pthread_join(t[1], NULL);
munmap(mapping, mapping_size);
}
}
Note: to see the splat, CONFIG_TRANSPARENT_HUGEPAGE=y and
CONFIG_DEBUG_VM=y are needed.
Google Bug Id: 64696096
Link: http://lkml.kernel.org/r/20170823205235.132061-1-ebiggers3@gmail.com
Fixes: 854e9ed09ded ("mm: support madvise(MADV_FREE)")
Signed-off-by: Eric Biggers <ebiggers@google.com>
Acked-by: David Rientjes <rientjes@google.com>
Acked-by: Minchan Kim <minchan@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-08-30 10:21:47 +02:00
..
2017-06-17 06:41:51 +02:00
2017-02-26 11:10:52 +01:00
2016-07-26 16:19:19 -07:00
2016-10-11 15:06:33 -07:00
2016-01-27 09:09:57 -05:00
2015-07-17 16:39:54 -07:00
2016-11-11 08:12:37 -08:00
2015-08-14 15:56:32 -07:00
2017-01-12 11:39:32 +01:00
2016-03-17 15:09:34 -07:00
2016-10-07 18:46:29 -07:00
2016-03-17 15:09:34 -07:00
2015-11-05 19:34:48 -08:00
2016-06-09 14:23:11 -07:00
2016-03-15 16:55:16 -07:00
2017-03-12 06:41:43 +01:00
2016-10-19 08:11:24 -07:00
2016-07-26 16:19:19 -07:00
2017-06-24 07:11:18 +02:00
2016-05-19 19:12:14 -07:00
2017-07-21 07:42:21 +02:00
2016-05-20 17:58:30 -07:00
2017-04-08 09:30:35 +02:00
2015-09-10 13:29:01 -07:00
2017-01-06 10:40:13 +01:00
2017-08-11 08:49:29 -07:00
2015-02-10 14:30:31 -08:00
2016-10-27 16:23:01 -07:00
2016-09-13 02:35:27 +02:00
2017-01-12 11:39:32 +01:00
2016-03-17 15:09:34 -07:00
2016-03-17 15:09:34 -07:00
2016-11-11 08:12:37 -08:00
2017-06-07 12:07:49 +02:00
2017-07-21 07:42:21 +02:00
2016-05-22 17:21:27 -07:00
2017-08-30 10:21:47 +02:00
2016-10-12 10:23:41 -07:00
2017-08-24 17:12:19 -07:00
2017-04-21 09:31:23 +02:00
2017-02-09 08:08:28 +01:00
2017-06-24 07:11:16 +02:00
2017-08-24 17:12:19 -07:00
2017-08-24 17:12:20 -07:00
2016-07-28 16:07:41 -07:00
2015-09-08 15:35:28 -07:00
2017-08-24 17:12:21 -07:00
2016-10-07 18:46:28 -07:00
2017-06-07 12:07:49 +02:00
2016-03-17 15:09:34 -07:00
2017-07-21 07:42:22 +02:00
2016-04-28 11:44:19 +02:00
2016-03-17 15:09:34 -07:00
2016-05-19 19:12:14 -07:00
2017-08-11 08:49:29 -07:00
2017-08-11 08:49:29 -07:00
2015-11-05 19:34:48 -08:00
2017-08-24 17:12:19 -07:00
2017-01-06 10:40:13 +01:00
2016-10-07 18:46:29 -07:00
2017-08-24 17:12:19 -07:00
2015-11-05 19:34:48 -08:00
2016-10-07 18:46:27 -07:00
2016-07-28 16:07:41 -07:00
2016-10-07 18:46:29 -07:00
2016-10-07 18:46:29 -07:00
2016-10-07 18:46:27 -07:00
2016-06-03 15:06:22 -07:00
2016-10-07 18:46:28 -07:00
2016-01-15 17:56:32 -08:00
2016-03-17 15:09:34 -07:00
2017-03-26 13:05:58 +02:00
2016-03-17 15:09:34 -07:00
2016-10-18 14:13:37 -07:00
2016-03-17 15:09:34 -07:00
2016-08-26 17:39:35 -07:00
2017-08-11 08:49:29 -07:00
2017-08-30 10:21:46 +02:00
2017-03-22 12:43:38 +01:00
2017-03-22 12:43:38 +01:00
2017-03-22 12:43:38 +01:00
2017-03-22 12:43:38 +01:00
2017-06-07 12:07:49 +02:00
2016-08-02 17:31:41 -04:00
2016-08-02 17:31:41 -04:00
2017-07-05 14:40:17 +02:00
2016-10-07 18:46:28 -07:00
2016-10-07 18:46:28 -07:00
2017-01-19 20:17:59 +01:00
2017-06-14 15:06:00 +02:00
2016-09-20 16:07:39 -07:00
2016-04-04 10:41:08 -07:00
2016-10-22 09:39:10 -07:00
2016-10-07 18:46:27 -07:00
2017-07-05 14:40:28 +02:00
2017-03-12 06:41:43 +01:00
2017-03-12 06:41:44 +01:00
2016-10-07 18:46:30 -07:00
2017-04-08 09:30:36 +02:00
2016-06-03 16:02:55 -07:00
2016-01-15 11:40:52 -08:00
2015-11-06 17:50:42 -08:00
2017-04-21 09:31:19 +02:00
2017-02-09 08:08:27 +01:00