350-v5.18-MIPS-pgalloc-fix-memory-leak-caused-by-pgd_free.patch 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. From e852442da56f43795cb6255d90b9fd0c84b209bb Mon Sep 17 00:00:00 2001
  2. From: Yaliang Wang <[email protected]>
  3. Date: Thu, 10 Mar 2022 19:31:16 +0800
  4. Subject: [PATCH] MIPS: pgalloc: fix memory leak caused by pgd_free()
  5. pgd page is freed by generic implementation pgd_free() since commit
  6. f9cb654cb550 ("asm-generic: pgalloc: provide generic pgd_free()"),
  7. however, there are scenarios that the system uses more than one page as
  8. the pgd table, in such cases the generic implementation pgd_free() won't
  9. be applicable anymore. For example, when PAGE_SIZE_4KB is enabled and
  10. MIPS_VA_BITS_48 is not enabled in a 64bit system, the macro "PGD_ORDER"
  11. will be set as "1", which will cause allocating two pages as the pgd
  12. table. Well, at the same time, the generic implementation pgd_free()
  13. just free one pgd page, which will result in the memory leak.
  14. The memory leak can be easily detected by executing shell command:
  15. "while true; do ls > /dev/null; grep MemFree /proc/meminfo; done"
  16. Fixes: f9cb654cb550 ("asm-generic: pgalloc: provide generic pgd_free()")
  17. Signed-off-by: Yaliang Wang <[email protected]>
  18. Signed-off-by: Thomas Bogendoerfer <[email protected]>
  19. (cherry picked from commit 2bc5bab9a763d520937e4f3fe8df51c6a1eceb97)
  20. ---
  21. arch/mips/include/asm/pgalloc.h | 6 ++++++
  22. 1 file changed, 6 insertions(+)
  23. --- a/arch/mips/include/asm/pgalloc.h
  24. +++ b/arch/mips/include/asm/pgalloc.h
  25. @@ -15,6 +15,7 @@
  26. #define __HAVE_ARCH_PMD_ALLOC_ONE
  27. #define __HAVE_ARCH_PUD_ALLOC_ONE
  28. +#define __HAVE_ARCH_PGD_FREE
  29. #include <asm-generic/pgalloc.h>
  30. static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
  31. @@ -49,6 +50,11 @@ static inline void pud_populate(struct m
  32. extern void pgd_init(unsigned long page);
  33. extern pgd_t *pgd_alloc(struct mm_struct *mm);
  34. +static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  35. +{
  36. + free_pages((unsigned long)pgd, PGD_ORDER);
  37. +}
  38. +
  39. #define __pte_free_tlb(tlb,pte,address) \
  40. do { \
  41. pgtable_pte_page_dtor(pte); \