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