test_free_pages.c 1003 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/gfp.h>
  9. #include <linux/mm.h>
  10. #include <linux/module.h>
  11. static void test_free_pages(gfp_t gfp)
  12. {
  13. unsigned int i;
  14. for (i = 0; i < 1000 * 1000; i++) {
  15. unsigned long addr = __get_free_pages(gfp, 3);
  16. struct page *page = virt_to_page((void *)addr);
  17. /* Simulate page cache getting a speculative reference */
  18. get_page(page);
  19. free_pages(addr, 3);
  20. put_page(page);
  21. }
  22. }
  23. static int m_in(void)
  24. {
  25. pr_info("Testing with GFP_KERNEL\n");
  26. test_free_pages(GFP_KERNEL);
  27. pr_info("Testing with GFP_KERNEL | __GFP_COMP\n");
  28. test_free_pages(GFP_KERNEL | __GFP_COMP);
  29. pr_info("Test completed\n");
  30. return 0;
  31. }
  32. static void m_ex(void)
  33. {
  34. }
  35. module_init(m_in);
  36. module_exit(m_ex);
  37. MODULE_AUTHOR("Matthew Wilcox <[email protected]>");
  38. MODULE_LICENSE("GPL");