shrinker.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Artem Bityutskiy (Битюцкий Артём)
  8. * Adrian Hunter
  9. */
  10. /*
  11. * This file implements UBIFS shrinker which evicts clean znodes from the TNC
  12. * tree when Linux VM needs more RAM.
  13. *
  14. * We do not implement any LRU lists to find oldest znodes to free because it
  15. * would add additional overhead to the file system fast paths. So the shrinker
  16. * just walks the TNC tree when searching for znodes to free.
  17. *
  18. * If the root of a TNC sub-tree is clean and old enough, then the children are
  19. * also clean and old enough. So the shrinker walks the TNC in level order and
  20. * dumps entire sub-trees.
  21. *
  22. * The age of znodes is just the time-stamp when they were last looked at.
  23. * The current shrinker first tries to evict old znodes, then young ones.
  24. *
  25. * Since the shrinker is global, it has to protect against races with FS
  26. * un-mounts, which is done by the 'ubifs_infos_lock' and 'c->umount_mutex'.
  27. */
  28. #include "ubifs.h"
  29. /* List of all UBIFS file-system instances */
  30. LIST_HEAD(ubifs_infos);
  31. /*
  32. * We number each shrinker run and record the number on the ubifs_info structure
  33. * so that we can easily work out which ubifs_info structures have already been
  34. * done by the current run.
  35. */
  36. static unsigned int shrinker_run_no;
  37. /* Protects 'ubifs_infos' list */
  38. DEFINE_SPINLOCK(ubifs_infos_lock);
  39. /* Global clean znode counter (for all mounted UBIFS instances) */
  40. atomic_long_t ubifs_clean_zn_cnt;
  41. /**
  42. * shrink_tnc - shrink TNC tree.
  43. * @c: UBIFS file-system description object
  44. * @nr: number of znodes to free
  45. * @age: the age of znodes to free
  46. * @contention: if any contention, this is set to %1
  47. *
  48. * This function traverses TNC tree and frees clean znodes. It does not free
  49. * clean znodes which younger then @age. Returns number of freed znodes.
  50. */
  51. static int shrink_tnc(struct ubifs_info *c, int nr, int age, int *contention)
  52. {
  53. int total_freed = 0;
  54. struct ubifs_znode *znode, *zprev;
  55. time64_t time = ktime_get_seconds();
  56. ubifs_assert(c, mutex_is_locked(&c->umount_mutex));
  57. ubifs_assert(c, mutex_is_locked(&c->tnc_mutex));
  58. if (!c->zroot.znode || atomic_long_read(&c->clean_zn_cnt) == 0)
  59. return 0;
  60. /*
  61. * Traverse the TNC tree in levelorder manner, so that it is possible
  62. * to destroy large sub-trees. Indeed, if a znode is old, then all its
  63. * children are older or of the same age.
  64. *
  65. * Note, we are holding 'c->tnc_mutex', so we do not have to lock the
  66. * 'c->space_lock' when _reading_ 'c->clean_zn_cnt', because it is
  67. * changed only when the 'c->tnc_mutex' is held.
  68. */
  69. zprev = NULL;
  70. znode = ubifs_tnc_levelorder_next(c, c->zroot.znode, NULL);
  71. while (znode && total_freed < nr &&
  72. atomic_long_read(&c->clean_zn_cnt) > 0) {
  73. int freed;
  74. /*
  75. * If the znode is clean, but it is in the 'c->cnext' list, this
  76. * means that this znode has just been written to flash as a
  77. * part of commit and was marked clean. They will be removed
  78. * from the list at end commit. We cannot change the list,
  79. * because it is not protected by any mutex (design decision to
  80. * make commit really independent and parallel to main I/O). So
  81. * we just skip these znodes.
  82. *
  83. * Note, the 'clean_zn_cnt' counters are not updated until
  84. * after the commit, so the UBIFS shrinker does not report
  85. * the znodes which are in the 'c->cnext' list as freeable.
  86. *
  87. * Also note, if the root of a sub-tree is not in 'c->cnext',
  88. * then the whole sub-tree is not in 'c->cnext' as well, so it
  89. * is safe to dump whole sub-tree.
  90. */
  91. if (znode->cnext) {
  92. /*
  93. * Very soon these znodes will be removed from the list
  94. * and become freeable.
  95. */
  96. *contention = 1;
  97. } else if (!ubifs_zn_dirty(znode) &&
  98. abs(time - znode->time) >= age) {
  99. if (znode->parent)
  100. znode->parent->zbranch[znode->iip].znode = NULL;
  101. else
  102. c->zroot.znode = NULL;
  103. freed = ubifs_destroy_tnc_subtree(c, znode);
  104. atomic_long_sub(freed, &ubifs_clean_zn_cnt);
  105. atomic_long_sub(freed, &c->clean_zn_cnt);
  106. total_freed += freed;
  107. znode = zprev;
  108. }
  109. if (unlikely(!c->zroot.znode))
  110. break;
  111. zprev = znode;
  112. znode = ubifs_tnc_levelorder_next(c, c->zroot.znode, znode);
  113. cond_resched();
  114. }
  115. return total_freed;
  116. }
  117. /**
  118. * shrink_tnc_trees - shrink UBIFS TNC trees.
  119. * @nr: number of znodes to free
  120. * @age: the age of znodes to free
  121. * @contention: if any contention, this is set to %1
  122. *
  123. * This function walks the list of mounted UBIFS file-systems and frees clean
  124. * znodes which are older than @age, until at least @nr znodes are freed.
  125. * Returns the number of freed znodes.
  126. */
  127. static int shrink_tnc_trees(int nr, int age, int *contention)
  128. {
  129. struct ubifs_info *c;
  130. struct list_head *p;
  131. unsigned int run_no;
  132. int freed = 0;
  133. spin_lock(&ubifs_infos_lock);
  134. do {
  135. run_no = ++shrinker_run_no;
  136. } while (run_no == 0);
  137. /* Iterate over all mounted UBIFS file-systems and try to shrink them */
  138. p = ubifs_infos.next;
  139. while (p != &ubifs_infos) {
  140. c = list_entry(p, struct ubifs_info, infos_list);
  141. /*
  142. * We move the ones we do to the end of the list, so we stop
  143. * when we see one we have already done.
  144. */
  145. if (c->shrinker_run_no == run_no)
  146. break;
  147. if (!mutex_trylock(&c->umount_mutex)) {
  148. /* Some un-mount is in progress, try next FS */
  149. *contention = 1;
  150. p = p->next;
  151. continue;
  152. }
  153. /*
  154. * We're holding 'c->umount_mutex', so the file-system won't go
  155. * away.
  156. */
  157. if (!mutex_trylock(&c->tnc_mutex)) {
  158. mutex_unlock(&c->umount_mutex);
  159. *contention = 1;
  160. p = p->next;
  161. continue;
  162. }
  163. spin_unlock(&ubifs_infos_lock);
  164. /*
  165. * OK, now we have TNC locked, the file-system cannot go away -
  166. * it is safe to reap the cache.
  167. */
  168. c->shrinker_run_no = run_no;
  169. freed += shrink_tnc(c, nr, age, contention);
  170. mutex_unlock(&c->tnc_mutex);
  171. spin_lock(&ubifs_infos_lock);
  172. /* Get the next list element before we move this one */
  173. p = p->next;
  174. /*
  175. * Move this one to the end of the list to provide some
  176. * fairness.
  177. */
  178. list_move_tail(&c->infos_list, &ubifs_infos);
  179. mutex_unlock(&c->umount_mutex);
  180. if (freed >= nr)
  181. break;
  182. }
  183. spin_unlock(&ubifs_infos_lock);
  184. return freed;
  185. }
  186. /**
  187. * kick_a_thread - kick a background thread to start commit.
  188. *
  189. * This function kicks a background thread to start background commit. Returns
  190. * %-1 if a thread was kicked or there is another reason to assume the memory
  191. * will soon be freed or become freeable. If there are no dirty znodes, returns
  192. * %0.
  193. */
  194. static int kick_a_thread(void)
  195. {
  196. int i;
  197. struct ubifs_info *c;
  198. /*
  199. * Iterate over all mounted UBIFS file-systems and find out if there is
  200. * already an ongoing commit operation there. If no, then iterate for
  201. * the second time and initiate background commit.
  202. */
  203. spin_lock(&ubifs_infos_lock);
  204. for (i = 0; i < 2; i++) {
  205. list_for_each_entry(c, &ubifs_infos, infos_list) {
  206. long dirty_zn_cnt;
  207. if (!mutex_trylock(&c->umount_mutex)) {
  208. /*
  209. * Some un-mount is in progress, it will
  210. * certainly free memory, so just return.
  211. */
  212. spin_unlock(&ubifs_infos_lock);
  213. return -1;
  214. }
  215. dirty_zn_cnt = atomic_long_read(&c->dirty_zn_cnt);
  216. if (!dirty_zn_cnt || c->cmt_state == COMMIT_BROKEN ||
  217. c->ro_mount || c->ro_error) {
  218. mutex_unlock(&c->umount_mutex);
  219. continue;
  220. }
  221. if (c->cmt_state != COMMIT_RESTING) {
  222. spin_unlock(&ubifs_infos_lock);
  223. mutex_unlock(&c->umount_mutex);
  224. return -1;
  225. }
  226. if (i == 1) {
  227. list_move_tail(&c->infos_list, &ubifs_infos);
  228. spin_unlock(&ubifs_infos_lock);
  229. ubifs_request_bg_commit(c);
  230. mutex_unlock(&c->umount_mutex);
  231. return -1;
  232. }
  233. mutex_unlock(&c->umount_mutex);
  234. }
  235. }
  236. spin_unlock(&ubifs_infos_lock);
  237. return 0;
  238. }
  239. unsigned long ubifs_shrink_count(struct shrinker *shrink,
  240. struct shrink_control *sc)
  241. {
  242. long clean_zn_cnt = atomic_long_read(&ubifs_clean_zn_cnt);
  243. /*
  244. * Due to the way UBIFS updates the clean znode counter it may
  245. * temporarily be negative.
  246. */
  247. return clean_zn_cnt >= 0 ? clean_zn_cnt : 1;
  248. }
  249. unsigned long ubifs_shrink_scan(struct shrinker *shrink,
  250. struct shrink_control *sc)
  251. {
  252. unsigned long nr = sc->nr_to_scan;
  253. int contention = 0;
  254. unsigned long freed;
  255. long clean_zn_cnt = atomic_long_read(&ubifs_clean_zn_cnt);
  256. if (!clean_zn_cnt) {
  257. /*
  258. * No clean znodes, nothing to reap. All we can do in this case
  259. * is to kick background threads to start commit, which will
  260. * probably make clean znodes which, in turn, will be freeable.
  261. * And we return -1 which means will make VM call us again
  262. * later.
  263. */
  264. dbg_tnc("no clean znodes, kick a thread");
  265. return kick_a_thread();
  266. }
  267. freed = shrink_tnc_trees(nr, OLD_ZNODE_AGE, &contention);
  268. if (freed >= nr)
  269. goto out;
  270. dbg_tnc("not enough old znodes, try to free young ones");
  271. freed += shrink_tnc_trees(nr - freed, YOUNG_ZNODE_AGE, &contention);
  272. if (freed >= nr)
  273. goto out;
  274. dbg_tnc("not enough young znodes, free all");
  275. freed += shrink_tnc_trees(nr - freed, 0, &contention);
  276. if (!freed && contention) {
  277. dbg_tnc("freed nothing, but contention");
  278. return SHRINK_STOP;
  279. }
  280. out:
  281. dbg_tnc("%lu znodes were freed, requested %lu", freed, nr);
  282. return freed;
  283. }