xref: /linux/lib/test_free_pages.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1e320d301SMatthew Wilcox (Oracle) // SPDX-License-Identifier: GPL-2.0+
2e320d301SMatthew Wilcox (Oracle) /*
3e320d301SMatthew Wilcox (Oracle)  * test_free_pages.c: Check that free_pages() doesn't leak memory
4e320d301SMatthew Wilcox (Oracle)  * Copyright (c) 2020 Oracle
5e320d301SMatthew Wilcox (Oracle)  * Author: Matthew Wilcox <willy@infradead.org>
6e320d301SMatthew Wilcox (Oracle)  */
7e320d301SMatthew Wilcox (Oracle) 
80ae446e4SGeert Uytterhoeven #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
90ae446e4SGeert Uytterhoeven 
10e320d301SMatthew Wilcox (Oracle) #include <linux/gfp.h>
11e320d301SMatthew Wilcox (Oracle) #include <linux/mm.h>
12e320d301SMatthew Wilcox (Oracle) #include <linux/module.h>
13e320d301SMatthew Wilcox (Oracle) 
test_free_pages(gfp_t gfp)14e320d301SMatthew Wilcox (Oracle) static void test_free_pages(gfp_t gfp)
15e320d301SMatthew Wilcox (Oracle) {
16e320d301SMatthew Wilcox (Oracle) 	unsigned int i;
17e320d301SMatthew Wilcox (Oracle) 
18e320d301SMatthew Wilcox (Oracle) 	for (i = 0; i < 1000 * 1000; i++) {
19e320d301SMatthew Wilcox (Oracle) 		unsigned long addr = __get_free_pages(gfp, 3);
20b3c56f8fSLinus Walleij 		struct page *page = virt_to_page((void *)addr);
21e320d301SMatthew Wilcox (Oracle) 
22e320d301SMatthew Wilcox (Oracle) 		/* Simulate page cache getting a speculative reference */
23e320d301SMatthew Wilcox (Oracle) 		get_page(page);
24e320d301SMatthew Wilcox (Oracle) 		free_pages(addr, 3);
25e320d301SMatthew Wilcox (Oracle) 		put_page(page);
26e320d301SMatthew Wilcox (Oracle) 	}
27e320d301SMatthew Wilcox (Oracle) }
28e320d301SMatthew Wilcox (Oracle) 
m_in(void)29e320d301SMatthew Wilcox (Oracle) static int m_in(void)
30e320d301SMatthew Wilcox (Oracle) {
310ae446e4SGeert Uytterhoeven 	pr_info("Testing with GFP_KERNEL\n");
32e320d301SMatthew Wilcox (Oracle) 	test_free_pages(GFP_KERNEL);
330ae446e4SGeert Uytterhoeven 	pr_info("Testing with GFP_KERNEL | __GFP_COMP\n");
34e320d301SMatthew Wilcox (Oracle) 	test_free_pages(GFP_KERNEL | __GFP_COMP);
350ae446e4SGeert Uytterhoeven 	pr_info("Test completed\n");
36e320d301SMatthew Wilcox (Oracle) 
37e320d301SMatthew Wilcox (Oracle) 	return 0;
38e320d301SMatthew Wilcox (Oracle) }
39e320d301SMatthew Wilcox (Oracle) 
m_ex(void)40e320d301SMatthew Wilcox (Oracle) static void m_ex(void)
41e320d301SMatthew Wilcox (Oracle) {
42e320d301SMatthew Wilcox (Oracle) }
43e320d301SMatthew Wilcox (Oracle) 
44e320d301SMatthew Wilcox (Oracle) module_init(m_in);
45e320d301SMatthew Wilcox (Oracle) module_exit(m_ex);
46e320d301SMatthew Wilcox (Oracle) MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>");
47*30347491SJeff Johnson MODULE_DESCRIPTION("Check that free_pages() doesn't leak memory");
48e320d301SMatthew Wilcox (Oracle) MODULE_LICENSE("GPL");
49