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/
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
34 #define __erofs_workgroup_get(grp) atomic_inc(&(grp)->refcount)
35 #define __erofs_workgroup_put(grp) atomic_dec(&(grp)->refcount)
37 static int erofs_workgroup_get(struct erofs_workgroup *grp)
42 o = erofs_wait_on_workgroup_freezed(grp);
46 if (unlikely(atomic_cmpxchg(&grp->refcount, o, o + 1) != o))
49 /* decrease refcount paired by erofs_workgroup_put */
51 atomic_long_dec(&erofs_global_shrink_cnt);
55 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
56 pgoff_t index, bool *tag)
58 struct erofs_sb_info *sbi = EROFS_SB(sb);
59 struct erofs_workgroup *grp;
63 grp = radix_tree_lookup(&sbi->workstn_tree, index);
65 *tag = xa_pointer_tag(grp);
66 grp = xa_untag_pointer(grp);
68 if (erofs_workgroup_get(grp)) {
69 /* prefer to relax rcu read side */
74 DBG_BUGON(index != grp->index);
80 int erofs_register_workgroup(struct super_block *sb,
81 struct erofs_workgroup *grp,
84 struct erofs_sb_info *sbi;
87 /* grp shouldn't be broken or used before */
88 if (unlikely(atomic_read(&grp->refcount) != 1)) {
93 err = radix_tree_preload(GFP_NOFS);
98 erofs_workstn_lock(sbi);
100 grp = xa_tag_pointer(grp, tag);
103 * Bump up reference count before making this workgroup
104 * visible to other users in order to avoid potential UAF
105 * without serialized by erofs_workstn_lock.
107 __erofs_workgroup_get(grp);
109 err = radix_tree_insert(&sbi->workstn_tree,
113 * it's safe to decrease since the workgroup isn't visible
114 * and refcount >= 2 (cannot be freezed).
116 __erofs_workgroup_put(grp);
118 erofs_workstn_unlock(sbi);
119 radix_tree_preload_end();
123 static void __erofs_workgroup_free(struct erofs_workgroup *grp)
125 atomic_long_dec(&erofs_global_shrink_cnt);
126 erofs_workgroup_free_rcu(grp);
129 int erofs_workgroup_put(struct erofs_workgroup *grp)
131 int count = atomic_dec_return(&grp->refcount);
134 atomic_long_inc(&erofs_global_shrink_cnt);
136 __erofs_workgroup_free(grp);
140 #ifdef EROFS_FS_HAS_MANAGED_CACHE
141 /* for cache-managed case, customized reclaim paths exist */
142 static void erofs_workgroup_unfreeze_final(struct erofs_workgroup *grp)
144 erofs_workgroup_unfreeze(grp, 0);
145 __erofs_workgroup_free(grp);
148 static bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
149 struct erofs_workgroup *grp,
153 * for managed cache enabled, the refcount of workgroups
154 * themselves could be < 0 (freezed). So there is no guarantee
155 * that all refcount > 0 if managed cache is enabled.
157 if (!erofs_workgroup_try_to_freeze(grp, 1))
161 * note that all cached pages should be unlinked
162 * before delete it from the radix tree.
163 * Otherwise some cached pages of an orphan old workgroup
164 * could be still linked after the new one is available.
166 if (erofs_try_to_free_all_cached_pages(sbi, grp)) {
167 erofs_workgroup_unfreeze(grp, 1);
172 * it is impossible to fail after the workgroup is freezed,
173 * however in order to avoid some race conditions, add a
174 * DBG_BUGON to observe this in advance.
176 DBG_BUGON(xa_untag_pointer(radix_tree_delete(&sbi->workstn_tree,
177 grp->index)) != grp);
180 * if managed cache is enable, the last refcount
181 * should indicate the related workstation.
183 erofs_workgroup_unfreeze_final(grp);
188 /* for nocache case, no customized reclaim path at all */
189 static bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
190 struct erofs_workgroup *grp,
193 int cnt = atomic_read(&grp->refcount);
196 DBG_BUGON(cleanup && cnt != 1);
201 DBG_BUGON(xa_untag_pointer(radix_tree_delete(&sbi->workstn_tree,
202 grp->index)) != grp);
204 /* (rarely) could be grabbed again when freeing */
205 erofs_workgroup_put(grp);
211 unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
212 unsigned long nr_shrink,
215 pgoff_t first_index = 0;
216 void *batch[PAGEVEC_SIZE];
217 unsigned int freed = 0;
221 erofs_workstn_lock(sbi);
223 found = radix_tree_gang_lookup(&sbi->workstn_tree,
224 batch, first_index, PAGEVEC_SIZE);
226 for (i = 0; i < found; ++i) {
227 struct erofs_workgroup *grp = xa_untag_pointer(batch[i]);
229 first_index = grp->index + 1;
231 /* try to shrink each valid workgroup */
232 if (!erofs_try_to_release_workgroup(sbi, grp, cleanup))
236 if (unlikely(!--nr_shrink))
239 erofs_workstn_unlock(sbi);
248 /* protected by 'erofs_sb_list_lock' */
249 static unsigned int shrinker_run_no;
251 /* protects the mounted 'erofs_sb_list' */
252 static DEFINE_SPINLOCK(erofs_sb_list_lock);
253 static LIST_HEAD(erofs_sb_list);
255 void erofs_register_super(struct super_block *sb)
257 struct erofs_sb_info *sbi = EROFS_SB(sb);
259 mutex_init(&sbi->umount_mutex);
261 spin_lock(&erofs_sb_list_lock);
262 list_add(&sbi->list, &erofs_sb_list);
263 spin_unlock(&erofs_sb_list_lock);
266 void erofs_unregister_super(struct super_block *sb)
268 spin_lock(&erofs_sb_list_lock);
269 list_del(&EROFS_SB(sb)->list);
270 spin_unlock(&erofs_sb_list_lock);
273 static unsigned long erofs_shrink_count(struct shrinker *shrink,
274 struct shrink_control *sc)
276 return atomic_long_read(&erofs_global_shrink_cnt);
279 static unsigned long erofs_shrink_scan(struct shrinker *shrink,
280 struct shrink_control *sc)
282 struct erofs_sb_info *sbi;
285 unsigned long nr = sc->nr_to_scan;
287 unsigned long freed = 0;
289 spin_lock(&erofs_sb_list_lock);
291 run_no = ++shrinker_run_no;
294 /* Iterate over all mounted superblocks and try to shrink them */
295 p = erofs_sb_list.next;
296 while (p != &erofs_sb_list) {
297 sbi = list_entry(p, struct erofs_sb_info, list);
300 * We move the ones we do to the end of the list, so we stop
301 * when we see one we have already done.
303 if (sbi->shrinker_run_no == run_no)
306 if (!mutex_trylock(&sbi->umount_mutex)) {
311 spin_unlock(&erofs_sb_list_lock);
312 sbi->shrinker_run_no = run_no;
314 #ifdef CONFIG_EROFS_FS_ZIP
315 freed += erofs_shrink_workstation(sbi, nr, false);
318 spin_lock(&erofs_sb_list_lock);
319 /* Get the next list element before we move this one */
323 * Move this one to the end of the list to provide some
326 list_move_tail(&sbi->list, &erofs_sb_list);
327 mutex_unlock(&sbi->umount_mutex);
332 spin_unlock(&erofs_sb_list_lock);
336 struct shrinker erofs_shrinker_info = {
337 .scan_objects = erofs_shrink_scan,
338 .count_objects = erofs_shrink_count,
339 .seeks = DEFAULT_SEEKS,