shrinker.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * f2fs shrinker support
  4. * the basic infra was copied from fs/ubifs/shrinker.c
  5. *
  6. * Copyright (c) 2015 Motorola Mobility
  7. * Copyright (c) 2015 Jaegeuk Kim <[email protected]>
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/f2fs_fs.h>
  11. #include "f2fs.h"
  12. #include "node.h"
  13. static LIST_HEAD(f2fs_list);
  14. static DEFINE_SPINLOCK(f2fs_list_lock);
  15. static unsigned int shrinker_run_no;
  16. static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi)
  17. {
  18. return NM_I(sbi)->nat_cnt[RECLAIMABLE_NAT];
  19. }
  20. static unsigned long __count_free_nids(struct f2fs_sb_info *sbi)
  21. {
  22. long count = NM_I(sbi)->nid_cnt[FREE_NID] - MAX_FREE_NIDS;
  23. return count > 0 ? count : 0;
  24. }
  25. static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,
  26. enum extent_type type)
  27. {
  28. struct extent_tree_info *eti = &sbi->extent_tree[type];
  29. return atomic_read(&eti->total_zombie_tree) +
  30. atomic_read(&eti->total_ext_node);
  31. }
  32. unsigned long f2fs_shrink_count(struct shrinker *shrink,
  33. struct shrink_control *sc)
  34. {
  35. struct f2fs_sb_info *sbi;
  36. struct list_head *p;
  37. unsigned long count = 0;
  38. spin_lock(&f2fs_list_lock);
  39. p = f2fs_list.next;
  40. while (p != &f2fs_list) {
  41. sbi = list_entry(p, struct f2fs_sb_info, s_list);
  42. /* stop f2fs_put_super */
  43. if (!mutex_trylock(&sbi->umount_mutex)) {
  44. p = p->next;
  45. continue;
  46. }
  47. spin_unlock(&f2fs_list_lock);
  48. /* count read extent cache entries */
  49. count += __count_extent_cache(sbi, EX_READ);
  50. /* count block age extent cache entries */
  51. count += __count_extent_cache(sbi, EX_BLOCK_AGE);
  52. /* count clean nat cache entries */
  53. count += __count_nat_entries(sbi);
  54. /* count free nids cache entries */
  55. count += __count_free_nids(sbi);
  56. spin_lock(&f2fs_list_lock);
  57. p = p->next;
  58. mutex_unlock(&sbi->umount_mutex);
  59. }
  60. spin_unlock(&f2fs_list_lock);
  61. return count;
  62. }
  63. unsigned long f2fs_shrink_scan(struct shrinker *shrink,
  64. struct shrink_control *sc)
  65. {
  66. unsigned long nr = sc->nr_to_scan;
  67. struct f2fs_sb_info *sbi;
  68. struct list_head *p;
  69. unsigned int run_no;
  70. unsigned long freed = 0;
  71. spin_lock(&f2fs_list_lock);
  72. do {
  73. run_no = ++shrinker_run_no;
  74. } while (run_no == 0);
  75. p = f2fs_list.next;
  76. while (p != &f2fs_list) {
  77. sbi = list_entry(p, struct f2fs_sb_info, s_list);
  78. if (sbi->shrinker_run_no == run_no)
  79. break;
  80. /* stop f2fs_put_super */
  81. if (!mutex_trylock(&sbi->umount_mutex)) {
  82. p = p->next;
  83. continue;
  84. }
  85. spin_unlock(&f2fs_list_lock);
  86. sbi->shrinker_run_no = run_no;
  87. /* shrink extent cache entries */
  88. freed += f2fs_shrink_age_extent_tree(sbi, nr >> 2);
  89. /* shrink read extent cache entries */
  90. freed += f2fs_shrink_read_extent_tree(sbi, nr >> 2);
  91. /* shrink clean nat cache entries */
  92. if (freed < nr)
  93. freed += f2fs_try_to_free_nats(sbi, nr - freed);
  94. /* shrink free nids cache entries */
  95. if (freed < nr)
  96. freed += f2fs_try_to_free_nids(sbi, nr - freed);
  97. spin_lock(&f2fs_list_lock);
  98. p = p->next;
  99. list_move_tail(&sbi->s_list, &f2fs_list);
  100. mutex_unlock(&sbi->umount_mutex);
  101. if (freed >= nr)
  102. break;
  103. }
  104. spin_unlock(&f2fs_list_lock);
  105. return freed;
  106. }
  107. void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
  108. {
  109. spin_lock(&f2fs_list_lock);
  110. list_add_tail(&sbi->s_list, &f2fs_list);
  111. spin_unlock(&f2fs_list_lock);
  112. }
  113. void f2fs_leave_shrinker(struct f2fs_sb_info *sbi)
  114. {
  115. f2fs_shrink_read_extent_tree(sbi, __count_extent_cache(sbi, EX_READ));
  116. f2fs_shrink_age_extent_tree(sbi,
  117. __count_extent_cache(sbi, EX_BLOCK_AGE));
  118. spin_lock(&f2fs_list_lock);
  119. list_del_init(&sbi->s_list);
  120. spin_unlock(&f2fs_list_lock);
  121. }