test_free_pages.c 829 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. #include <linux/gfp.h>
  8. #include <linux/mm.h>
  9. #include <linux/module.h>
  10. static void test_free_pages(gfp_t gfp)
  11. {
  12. unsigned int i;
  13. for (i = 0; i < 1000 * 1000; i++) {
  14. unsigned long addr = __get_free_pages(gfp, 3);
  15. struct page *page = virt_to_page(addr);
  16. /* Simulate page cache getting a speculative reference */
  17. get_page(page);
  18. free_pages(addr, 3);
  19. put_page(page);
  20. }
  21. }
  22. static int m_in(void)
  23. {
  24. test_free_pages(GFP_KERNEL);
  25. test_free_pages(GFP_KERNEL | __GFP_COMP);
  26. return 0;
  27. }
  28. static void m_ex(void)
  29. {
  30. }
  31. module_init(m_in);
  32. module_exit(m_ex);
  33. MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>");
  34. MODULE_LICENSE("GPL");