1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/staging/erofs/utils.c
5 * Copyright (C) 2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
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.
15 #include <linux/pagevec.h>
17 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp)
21 if (!list_empty(pool)) {
22 page = lru_to_page(pool);
25 page = alloc_pages(gfp | __GFP_NOFAIL, 0);
30 /* global shrink count (for all mounted EROFS instances) */
31 static atomic_long_t erofs_global_shrink_cnt;
33 #ifdef CONFIG_EROFS_FS_ZIP
35 /* radix_tree and the future XArray both don't use tagptr_t yet */
36 struct erofs_workgroup *erofs_find_workgroup(
37 struct super_block *sb, pgoff_t index, bool *tag)
39 struct erofs_sb_info *sbi = EROFS_SB(sb);
40 struct erofs_workgroup *grp;
45 grp = radix_tree_lookup(&sbi->workstn_tree, index);
47 *tag = radix_tree_exceptional_entry(grp);
48 grp = (void *)((unsigned long)grp &
49 ~RADIX_TREE_EXCEPTIONAL_ENTRY);
51 if (erofs_workgroup_get(grp, &oldcount)) {
52 /* prefer to relax rcu read side */
57 /* decrease refcount added by erofs_workgroup_put */
58 if (unlikely(oldcount == 1))
59 atomic_long_dec(&erofs_global_shrink_cnt);
60 DBG_BUGON(index != grp->index);
66 int erofs_register_workgroup(struct super_block *sb,
67 struct erofs_workgroup *grp,
70 struct erofs_sb_info *sbi;
73 /* grp shouldn't be broken or used before */
74 if (unlikely(atomic_read(&grp->refcount) != 1)) {
79 err = radix_tree_preload(GFP_NOFS);
84 erofs_workstn_lock(sbi);
87 grp = (void *)((unsigned long)grp |
88 1UL << RADIX_TREE_EXCEPTIONAL_SHIFT);
91 * Bump up reference count before making this workgroup
92 * visible to other users in order to avoid potential UAF
93 * without serialized by erofs_workstn_lock.
95 __erofs_workgroup_get(grp);
97 err = radix_tree_insert(&sbi->workstn_tree,
101 * it's safe to decrease since the workgroup isn't visible
102 * and refcount >= 2 (cannot be freezed).
104 __erofs_workgroup_put(grp);
106 erofs_workstn_unlock(sbi);
107 radix_tree_preload_end();
111 extern void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
113 static void __erofs_workgroup_free(struct erofs_workgroup *grp)
115 atomic_long_dec(&erofs_global_shrink_cnt);
116 erofs_workgroup_free_rcu(grp);
119 int erofs_workgroup_put(struct erofs_workgroup *grp)
121 int count = atomic_dec_return(&grp->refcount);
124 atomic_long_inc(&erofs_global_shrink_cnt);
126 __erofs_workgroup_free(grp);
130 #ifdef EROFS_FS_HAS_MANAGED_CACHE
131 /* for cache-managed case, customized reclaim paths exist */
132 static void erofs_workgroup_unfreeze_final(struct erofs_workgroup *grp)
134 erofs_workgroup_unfreeze(grp, 0);
135 __erofs_workgroup_free(grp);
138 bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
139 struct erofs_workgroup *grp,
145 * for managed cache enabled, the refcount of workgroups
146 * themselves could be < 0 (freezed). So there is no guarantee
147 * that all refcount > 0 if managed cache is enabled.
149 if (!erofs_workgroup_try_to_freeze(grp, 1))
153 * note that all cached pages should be unlinked
154 * before delete it from the radix tree.
155 * Otherwise some cached pages of an orphan old workgroup
156 * could be still linked after the new one is available.
158 if (erofs_try_to_free_all_cached_pages(sbi, grp)) {
159 erofs_workgroup_unfreeze(grp, 1);
164 * it is impossible to fail after the workgroup is freezed,
165 * however in order to avoid some race conditions, add a
166 * DBG_BUGON to observe this in advance.
168 entry = radix_tree_delete(&sbi->workstn_tree, grp->index);
169 DBG_BUGON((void *)((unsigned long)entry &
170 ~RADIX_TREE_EXCEPTIONAL_ENTRY) != grp);
173 * if managed cache is enable, the last refcount
174 * should indicate the related workstation.
176 erofs_workgroup_unfreeze_final(grp);
181 /* for nocache case, no customized reclaim path at all */
182 bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
183 struct erofs_workgroup *grp,
186 int cnt = atomic_read(&grp->refcount);
190 DBG_BUGON(cleanup && cnt != 1);
195 entry = radix_tree_delete(&sbi->workstn_tree, grp->index);
196 DBG_BUGON((void *)((unsigned long)entry &
197 ~RADIX_TREE_EXCEPTIONAL_ENTRY) != grp);
199 /* (rarely) could be grabbed again when freeing */
200 erofs_workgroup_put(grp);
206 unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
207 unsigned long nr_shrink,
210 pgoff_t first_index = 0;
211 void *batch[PAGEVEC_SIZE];
216 erofs_workstn_lock(sbi);
218 found = radix_tree_gang_lookup(&sbi->workstn_tree,
219 batch, first_index, PAGEVEC_SIZE);
221 for (i = 0; i < found; ++i) {
222 struct erofs_workgroup *grp = (void *)
223 ((unsigned long)batch[i] &
224 ~RADIX_TREE_EXCEPTIONAL_ENTRY);
226 first_index = grp->index + 1;
228 /* try to shrink each valid workgroup */
229 if (!erofs_try_to_release_workgroup(sbi, grp, cleanup))
233 if (unlikely(!--nr_shrink))
236 erofs_workstn_unlock(sbi);
245 /* protected by 'erofs_sb_list_lock' */
246 static unsigned int shrinker_run_no;
248 /* protects the mounted 'erofs_sb_list' */
249 static DEFINE_SPINLOCK(erofs_sb_list_lock);
250 static LIST_HEAD(erofs_sb_list);
252 void erofs_register_super(struct super_block *sb)
254 struct erofs_sb_info *sbi = EROFS_SB(sb);
256 mutex_init(&sbi->umount_mutex);
258 spin_lock(&erofs_sb_list_lock);
259 list_add(&sbi->list, &erofs_sb_list);
260 spin_unlock(&erofs_sb_list_lock);
263 void erofs_unregister_super(struct super_block *sb)
265 spin_lock(&erofs_sb_list_lock);
266 list_del(&EROFS_SB(sb)->list);
267 spin_unlock(&erofs_sb_list_lock);
270 unsigned long erofs_shrink_count(struct shrinker *shrink,
271 struct shrink_control *sc)
273 return atomic_long_read(&erofs_global_shrink_cnt);
276 unsigned long erofs_shrink_scan(struct shrinker *shrink,
277 struct shrink_control *sc)
279 struct erofs_sb_info *sbi;
282 unsigned long nr = sc->nr_to_scan;
284 unsigned long freed = 0;
286 spin_lock(&erofs_sb_list_lock);
288 run_no = ++shrinker_run_no;
291 /* Iterate over all mounted superblocks and try to shrink them */
292 p = erofs_sb_list.next;
293 while (p != &erofs_sb_list) {
294 sbi = list_entry(p, struct erofs_sb_info, list);
297 * We move the ones we do to the end of the list, so we stop
298 * when we see one we have already done.
300 if (sbi->shrinker_run_no == run_no)
303 if (!mutex_trylock(&sbi->umount_mutex)) {
308 spin_unlock(&erofs_sb_list_lock);
309 sbi->shrinker_run_no = run_no;
311 #ifdef CONFIG_EROFS_FS_ZIP
312 freed += erofs_shrink_workstation(sbi, nr - freed, false);
315 spin_lock(&erofs_sb_list_lock);
316 /* Get the next list element before we move this one */
320 * Move this one to the end of the list to provide some
323 list_move_tail(&sbi->list, &erofs_sb_list);
324 mutex_unlock(&sbi->umount_mutex);
329 spin_unlock(&erofs_sb_list_lock);