shrinker.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <jaegeuk@kernel.org>
  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 ?: SHRINK_EMPTY;
  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. unsigned int f2fs_donate_files(void)
  108. {
  109. struct f2fs_sb_info *sbi;
  110. struct list_head *p;
  111. unsigned int donate_files = 0;
  112. spin_lock(&f2fs_list_lock);
  113. p = f2fs_list.next;
  114. while (p != &f2fs_list) {
  115. sbi = list_entry(p, struct f2fs_sb_info, s_list);
  116. /* stop f2fs_put_super */
  117. if (!mutex_trylock(&sbi->umount_mutex)) {
  118. p = p->next;
  119. continue;
  120. }
  121. spin_unlock(&f2fs_list_lock);
  122. donate_files += sbi->donate_files;
  123. spin_lock(&f2fs_list_lock);
  124. p = p->next;
  125. mutex_unlock(&sbi->umount_mutex);
  126. }
  127. spin_unlock(&f2fs_list_lock);
  128. return donate_files;
  129. }
  130. static unsigned int do_reclaim_caches(struct f2fs_sb_info *sbi,
  131. unsigned int reclaim_caches_kb)
  132. {
  133. struct inode *inode;
  134. struct f2fs_inode_info *fi;
  135. unsigned int nfiles = sbi->donate_files;
  136. pgoff_t npages = reclaim_caches_kb >> (PAGE_SHIFT - 10);
  137. while (npages && nfiles--) {
  138. pgoff_t len;
  139. spin_lock(&sbi->inode_lock[DONATE_INODE]);
  140. if (list_empty(&sbi->inode_list[DONATE_INODE])) {
  141. spin_unlock(&sbi->inode_lock[DONATE_INODE]);
  142. break;
  143. }
  144. fi = list_first_entry(&sbi->inode_list[DONATE_INODE],
  145. struct f2fs_inode_info, gdonate_list);
  146. list_move_tail(&fi->gdonate_list, &sbi->inode_list[DONATE_INODE]);
  147. inode = igrab(&fi->vfs_inode);
  148. spin_unlock(&sbi->inode_lock[DONATE_INODE]);
  149. if (!inode)
  150. continue;
  151. inode_lock(inode);
  152. if (!is_inode_flag_set(inode, FI_DONATE_FINISHED)) {
  153. len = fi->donate_end - fi->donate_start + 1;
  154. npages = npages < len ? 0 : npages - len;
  155. invalidate_inode_pages2_range(inode->i_mapping,
  156. fi->donate_start, fi->donate_end);
  157. set_inode_flag(inode, FI_DONATE_FINISHED);
  158. }
  159. inode_unlock(inode);
  160. iput(inode);
  161. cond_resched();
  162. }
  163. return npages << (PAGE_SHIFT - 10);
  164. }
  165. void f2fs_reclaim_caches(unsigned int reclaim_caches_kb)
  166. {
  167. struct f2fs_sb_info *sbi;
  168. struct list_head *p;
  169. spin_lock(&f2fs_list_lock);
  170. p = f2fs_list.next;
  171. while (p != &f2fs_list && reclaim_caches_kb) {
  172. sbi = list_entry(p, struct f2fs_sb_info, s_list);
  173. /* stop f2fs_put_super */
  174. if (!mutex_trylock(&sbi->umount_mutex)) {
  175. p = p->next;
  176. continue;
  177. }
  178. spin_unlock(&f2fs_list_lock);
  179. reclaim_caches_kb = do_reclaim_caches(sbi, reclaim_caches_kb);
  180. spin_lock(&f2fs_list_lock);
  181. p = p->next;
  182. mutex_unlock(&sbi->umount_mutex);
  183. }
  184. spin_unlock(&f2fs_list_lock);
  185. }
  186. void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
  187. {
  188. spin_lock(&f2fs_list_lock);
  189. list_add_tail(&sbi->s_list, &f2fs_list);
  190. spin_unlock(&f2fs_list_lock);
  191. }
  192. void f2fs_leave_shrinker(struct f2fs_sb_info *sbi)
  193. {
  194. f2fs_shrink_read_extent_tree(sbi, __count_extent_cache(sbi, EX_READ));
  195. f2fs_shrink_age_extent_tree(sbi,
  196. __count_extent_cache(sbi, EX_BLOCK_AGE));
  197. spin_lock(&f2fs_list_lock);
  198. list_del_init(&sbi->s_list);
  199. spin_unlock(&f2fs_list_lock);
  200. }