Skip to content

Commit e320d30

Browse files
Matthew Wilcox (Oracle)torvalds
authored andcommitted
mm/page_alloc.c: fix freeing non-compound pages
Here is a very rare race which leaks memory: Page P0 is allocated to the page cache. Page P1 is free. Thread A Thread B Thread C find_get_entry(): xas_load() returns P0 Removes P0 from page cache P0 finds its buddy P1 alloc_pages(GFP_KERNEL, 1) returns P0 P0 has refcount 1 page_cache_get_speculative(P0) P0 has refcount 2 __free_pages(P0) P0 has refcount 1 put_page(P0) P1 is not freed Fix this by freeing all the pages in __free_pages() that won't be freed by the call to put_page(). It's usually not a good idea to split a page, but this is a very unlikely scenario. Fixes: e286781 ("mm: speculative page references") Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Mike Rapoport <rppt@linux.ibm.com> Cc: Nick Piggin <npiggin@gmail.com> Cc: Hugh Dickins <hughd@google.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: https://lkml.kernel.org/r/20200926213919.26642-1-willy@infradead.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a9b576f commit e320d30

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎lib/Kconfig.debug‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,15 @@ config TEST_HMM
23672367

23682368
If unsure, say N.
23692369

2370+
config TEST_FREE_PAGES
2371+
tristate "Test freeing pages"
2372+
help
2373+
Test that a memory leak does not occur due to a race between
2374+
freeing a block of pages and a speculative page reference.
2375+
Loading this module is safe if your kernel has the bug fixed.
2376+
If the bug is not fixed, it will leak gigabytes of memory and
2377+
probably OOM your system.
2378+
23702379
config TEST_FPU
23712380
tristate "Test floating point operations in kernel space"
23722381
depends on X86 && !KCOV_INSTRUMENT_ALL

‎lib/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o
101101
obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o
102102
obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o
103103
obj-$(CONFIG_TEST_HMM) += test_hmm.o
104+
obj-$(CONFIG_TEST_FREE_PAGES) += test_free_pages.o
104105

105106
#
106107
# CFLAGS for compiling floating point code inside the kernel. x86/Makefile turns

‎lib/test_free_pages.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* test_free_pages.c: Check that free_pages() doesn't leak memory
4+
* Copyright (c) 2020 Oracle
5+
* Author: Matthew Wilcox <willy@infradead.org>
6+
*/
7+
8+
#include <linux/gfp.h>
9+
#include <linux/mm.h>
10+
#include <linux/module.h>
11+
12+
static void test_free_pages(gfp_t gfp)
13+
{
14+
unsigned int i;
15+
16+
for (i = 0; i < 1000 * 1000; i++) {
17+
unsigned long addr = __get_free_pages(gfp, 3);
18+
struct page *page = virt_to_page(addr);
19+
20+
/* Simulate page cache getting a speculative reference */
21+
get_page(page);
22+
free_pages(addr, 3);
23+
put_page(page);
24+
}
25+
}
26+
27+
static int m_in(void)
28+
{
29+
test_free_pages(GFP_KERNEL);
30+
test_free_pages(GFP_KERNEL | __GFP_COMP);
31+
32+
return 0;
33+
}
34+
35+
static void m_ex(void)
36+
{
37+
}
38+
39+
module_init(m_in);
40+
module_exit(m_ex);
41+
MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>");
42+
MODULE_LICENSE("GPL");

‎mm/page_alloc.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,6 +4952,9 @@ void __free_pages(struct page *page, unsigned int order)
49524952
{
49534953
if (put_page_testzero(page))
49544954
free_the_page(page, order);
4955+
else if (!PageHead(page))
4956+
while (order-- > 0)
4957+
free_the_page(page + (1 << order), order);
49554958
}
49564959
EXPORT_SYMBOL(__free_pages);
49574960

0 commit comments

Comments
 (0)