utils.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/drivers/staging/erofs/utils.c
  4. *
  5. * Copyright (C) 2018 HUAWEI, Inc.
  6. * http://www.huawei.com/
  7. * Created by Gao Xiang <gaoxiang25@huawei.com>
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of the Linux
  11. * distribution for more details.
  12. */
  13. #include "internal.h"
  14. #include <linux/pagevec.h>
  15. struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp)
  16. {
  17. struct page *page;
  18. if (!list_empty(pool)) {
  19. page = lru_to_page(pool);
  20. list_del(&page->lru);
  21. } else {
  22. page = alloc_pages(gfp | __GFP_NOFAIL, 0);
  23. }
  24. return page;
  25. }
  26. /* global shrink count (for all mounted EROFS instances) */
  27. static atomic_long_t erofs_global_shrink_cnt;
  28. #ifdef CONFIG_EROFS_FS_ZIP
  29. /* radix_tree and the future XArray both don't use tagptr_t yet */
  30. struct erofs_workgroup *erofs_find_workgroup(
  31. struct super_block *sb, pgoff_t index, bool *tag)
  32. {
  33. struct erofs_sb_info *sbi = EROFS_SB(sb);
  34. struct erofs_workgroup *grp;
  35. int oldcount;
  36. repeat:
  37. rcu_read_lock();
  38. grp = radix_tree_lookup(&sbi->workstn_tree, index);
  39. if (grp != NULL) {
  40. *tag = radix_tree_exceptional_entry(grp);
  41. grp = (void *)((unsigned long)grp &
  42. ~RADIX_TREE_EXCEPTIONAL_ENTRY);
  43. if (erofs_workgroup_get(grp, &oldcount)) {
  44. /* prefer to relax rcu read side */
  45. rcu_read_unlock();
  46. goto repeat;
  47. }
  48. /* decrease refcount added by erofs_workgroup_put */
  49. if (unlikely(oldcount == 1))
  50. atomic_long_dec(&erofs_global_shrink_cnt);
  51. DBG_BUGON(index != grp->index);
  52. }
  53. rcu_read_unlock();
  54. return grp;
  55. }
  56. int erofs_register_workgroup(struct super_block *sb,
  57. struct erofs_workgroup *grp,
  58. bool tag)
  59. {
  60. struct erofs_sb_info *sbi;
  61. int err;
  62. /* grp shouldn't be broken or used before */
  63. if (unlikely(atomic_read(&grp->refcount) != 1)) {
  64. DBG_BUGON(1);
  65. return -EINVAL;
  66. }
  67. err = radix_tree_preload(GFP_NOFS);
  68. if (err)
  69. return err;
  70. sbi = EROFS_SB(sb);
  71. erofs_workstn_lock(sbi);
  72. if (tag)
  73. grp = (void *)((unsigned long)grp |
  74. 1UL << RADIX_TREE_EXCEPTIONAL_SHIFT);
  75. /*
  76. * Bump up reference count before making this workgroup
  77. * visible to other users in order to avoid potential UAF
  78. * without serialized by erofs_workstn_lock.
  79. */
  80. __erofs_workgroup_get(grp);
  81. err = radix_tree_insert(&sbi->workstn_tree,
  82. grp->index, grp);
  83. if (unlikely(err))
  84. /*
  85. * it's safe to decrease since the workgroup isn't visible
  86. * and refcount >= 2 (cannot be freezed).
  87. */
  88. __erofs_workgroup_put(grp);
  89. erofs_workstn_unlock(sbi);
  90. radix_tree_preload_end();
  91. return err;
  92. }
  93. extern void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
  94. static void __erofs_workgroup_free(struct erofs_workgroup *grp)
  95. {
  96. atomic_long_dec(&erofs_global_shrink_cnt);
  97. erofs_workgroup_free_rcu(grp);
  98. }
  99. int erofs_workgroup_put(struct erofs_workgroup *grp)
  100. {
  101. int count = atomic_dec_return(&grp->refcount);
  102. if (count == 1)
  103. atomic_long_inc(&erofs_global_shrink_cnt);
  104. else if (!count)
  105. __erofs_workgroup_free(grp);
  106. return count;
  107. }
  108. #ifdef EROFS_FS_HAS_MANAGED_CACHE
  109. /* for cache-managed case, customized reclaim paths exist */
  110. static void erofs_workgroup_unfreeze_final(struct erofs_workgroup *grp)
  111. {
  112. erofs_workgroup_unfreeze(grp, 0);
  113. __erofs_workgroup_free(grp);
  114. }
  115. bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
  116. struct erofs_workgroup *grp,
  117. bool cleanup)
  118. {
  119. void *entry;
  120. /*
  121. * for managed cache enabled, the refcount of workgroups
  122. * themselves could be < 0 (freezed). So there is no guarantee
  123. * that all refcount > 0 if managed cache is enabled.
  124. */
  125. if (!erofs_workgroup_try_to_freeze(grp, 1))
  126. return false;
  127. /*
  128. * note that all cached pages should be unlinked
  129. * before delete it from the radix tree.
  130. * Otherwise some cached pages of an orphan old workgroup
  131. * could be still linked after the new one is available.
  132. */
  133. if (erofs_try_to_free_all_cached_pages(sbi, grp)) {
  134. erofs_workgroup_unfreeze(grp, 1);
  135. return false;
  136. }
  137. /*
  138. * it is impossible to fail after the workgroup is freezed,
  139. * however in order to avoid some race conditions, add a
  140. * DBG_BUGON to observe this in advance.
  141. */
  142. entry = radix_tree_delete(&sbi->workstn_tree, grp->index);
  143. DBG_BUGON((void *)((unsigned long)entry &
  144. ~RADIX_TREE_EXCEPTIONAL_ENTRY) != grp);
  145. /*
  146. * if managed cache is enable, the last refcount
  147. * should indicate the related workstation.
  148. */
  149. erofs_workgroup_unfreeze_final(grp);
  150. return true;
  151. }
  152. #else
  153. /* for nocache case, no customized reclaim path at all */
  154. bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
  155. struct erofs_workgroup *grp,
  156. bool cleanup)
  157. {
  158. int cnt = atomic_read(&grp->refcount);
  159. void *entry;
  160. DBG_BUGON(cnt <= 0);
  161. DBG_BUGON(cleanup && cnt != 1);
  162. if (cnt > 1)
  163. return false;
  164. entry = radix_tree_delete(&sbi->workstn_tree, grp->index);
  165. DBG_BUGON((void *)((unsigned long)entry &
  166. ~RADIX_TREE_EXCEPTIONAL_ENTRY) != grp);
  167. /* (rarely) could be grabbed again when freeing */
  168. erofs_workgroup_put(grp);
  169. return true;
  170. }
  171. #endif
  172. unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
  173. unsigned long nr_shrink,
  174. bool cleanup)
  175. {
  176. pgoff_t first_index = 0;
  177. void *batch[PAGEVEC_SIZE];
  178. unsigned freed = 0;
  179. int i, found;
  180. repeat:
  181. erofs_workstn_lock(sbi);
  182. found = radix_tree_gang_lookup(&sbi->workstn_tree,
  183. batch, first_index, PAGEVEC_SIZE);
  184. for (i = 0; i < found; ++i) {
  185. struct erofs_workgroup *grp = (void *)
  186. ((unsigned long)batch[i] &
  187. ~RADIX_TREE_EXCEPTIONAL_ENTRY);
  188. first_index = grp->index + 1;
  189. /* try to shrink each valid workgroup */
  190. if (!erofs_try_to_release_workgroup(sbi, grp, cleanup))
  191. continue;
  192. ++freed;
  193. if (unlikely(!--nr_shrink))
  194. break;
  195. }
  196. erofs_workstn_unlock(sbi);
  197. if (i && nr_shrink)
  198. goto repeat;
  199. return freed;
  200. }
  201. #endif
  202. /* protected by 'erofs_sb_list_lock' */
  203. static unsigned int shrinker_run_no;
  204. /* protects the mounted 'erofs_sb_list' */
  205. static DEFINE_SPINLOCK(erofs_sb_list_lock);
  206. static LIST_HEAD(erofs_sb_list);
  207. void erofs_register_super(struct super_block *sb)
  208. {
  209. struct erofs_sb_info *sbi = EROFS_SB(sb);
  210. mutex_init(&sbi->umount_mutex);
  211. spin_lock(&erofs_sb_list_lock);
  212. list_add(&sbi->list, &erofs_sb_list);
  213. spin_unlock(&erofs_sb_list_lock);
  214. }
  215. void erofs_unregister_super(struct super_block *sb)
  216. {
  217. spin_lock(&erofs_sb_list_lock);
  218. list_del(&EROFS_SB(sb)->list);
  219. spin_unlock(&erofs_sb_list_lock);
  220. }
  221. unsigned long erofs_shrink_count(struct shrinker *shrink,
  222. struct shrink_control *sc)
  223. {
  224. return atomic_long_read(&erofs_global_shrink_cnt);
  225. }
  226. unsigned long erofs_shrink_scan(struct shrinker *shrink,
  227. struct shrink_control *sc)
  228. {
  229. struct erofs_sb_info *sbi;
  230. struct list_head *p;
  231. unsigned long nr = sc->nr_to_scan;
  232. unsigned int run_no;
  233. unsigned long freed = 0;
  234. spin_lock(&erofs_sb_list_lock);
  235. do
  236. run_no = ++shrinker_run_no;
  237. while (run_no == 0);
  238. /* Iterate over all mounted superblocks and try to shrink them */
  239. p = erofs_sb_list.next;
  240. while (p != &erofs_sb_list) {
  241. sbi = list_entry(p, struct erofs_sb_info, list);
  242. /*
  243. * We move the ones we do to the end of the list, so we stop
  244. * when we see one we have already done.
  245. */
  246. if (sbi->shrinker_run_no == run_no)
  247. break;
  248. if (!mutex_trylock(&sbi->umount_mutex)) {
  249. p = p->next;
  250. continue;
  251. }
  252. spin_unlock(&erofs_sb_list_lock);
  253. sbi->shrinker_run_no = run_no;
  254. #ifdef CONFIG_EROFS_FS_ZIP
  255. freed += erofs_shrink_workstation(sbi, nr, false);
  256. #endif
  257. spin_lock(&erofs_sb_list_lock);
  258. /* Get the next list element before we move this one */
  259. p = p->next;
  260. /*
  261. * Move this one to the end of the list to provide some
  262. * fairness.
  263. */
  264. list_move_tail(&sbi->list, &erofs_sb_list);
  265. mutex_unlock(&sbi->umount_mutex);
  266. if (freed >= nr)
  267. break;
  268. }
  269. spin_unlock(&erofs_sb_list_lock);
  270. return freed;
  271. }