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);
28 BUG_ON(page->mapping != NULL);
33 /* global shrink count (for all mounted EROFS instances) */
34 static atomic_long_t erofs_global_shrink_cnt;
36 #ifdef CONFIG_EROFS_FS_ZIP
38 struct erofs_workgroup *erofs_find_workgroup(
39 struct super_block *sb, pgoff_t index, bool *tag)
41 struct erofs_sb_info *sbi = EROFS_SB(sb);
42 struct erofs_workgroup *grp;
47 grp = radix_tree_lookup(&sbi->workstn_tree, index);
49 *tag = xa_pointer_tag(grp);
50 grp = xa_untag_pointer(grp);
52 if (erofs_workgroup_get(grp, &oldcount)) {
53 /* prefer to relax rcu read side */
58 /* decrease refcount added by erofs_workgroup_put */
59 if (unlikely(oldcount == 1))
60 atomic_long_dec(&erofs_global_shrink_cnt);
61 BUG_ON(index != grp->index);
67 int erofs_register_workgroup(struct super_block *sb,
68 struct erofs_workgroup *grp,
71 struct erofs_sb_info *sbi;
74 /* grp->refcount should not < 1 */
75 BUG_ON(!atomic_read(&grp->refcount));
77 err = radix_tree_preload(GFP_NOFS);
82 erofs_workstn_lock(sbi);
84 grp = xa_tag_pointer(grp, tag);
86 err = radix_tree_insert(&sbi->workstn_tree,
90 __erofs_workgroup_get(grp);
93 erofs_workstn_unlock(sbi);
94 radix_tree_preload_end();
98 extern void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
100 int erofs_workgroup_put(struct erofs_workgroup *grp)
102 int count = atomic_dec_return(&grp->refcount);
105 atomic_long_inc(&erofs_global_shrink_cnt);
107 atomic_long_dec(&erofs_global_shrink_cnt);
108 erofs_workgroup_free_rcu(grp);
113 unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
114 unsigned long nr_shrink,
117 pgoff_t first_index = 0;
118 void *batch[PAGEVEC_SIZE];
119 unsigned int freed = 0;
123 erofs_workstn_lock(sbi);
125 found = radix_tree_gang_lookup(&sbi->workstn_tree,
126 batch, first_index, PAGEVEC_SIZE);
128 for (i = 0; i < found; ++i) {
130 struct erofs_workgroup *grp = xa_untag_pointer(batch[i]);
132 first_index = grp->index + 1;
134 cnt = atomic_read(&grp->refcount);
140 #ifndef EROFS_FS_HAS_MANAGED_CACHE
143 if (!erofs_workgroup_try_to_freeze(grp, 1))
147 if (xa_untag_pointer(radix_tree_delete(&sbi->workstn_tree,
148 grp->index)) != grp) {
149 #ifdef EROFS_FS_HAS_MANAGED_CACHE
151 erofs_workgroup_unfreeze(grp, 1);
156 #ifdef EROFS_FS_HAS_MANAGED_CACHE
157 if (erofs_try_to_free_all_cached_pages(sbi, grp))
160 erofs_workgroup_unfreeze(grp, 1);
162 /* (rarely) grabbed again when freeing */
163 erofs_workgroup_put(grp);
166 if (unlikely(!--nr_shrink))
169 erofs_workstn_unlock(sbi);
178 /* protected by 'erofs_sb_list_lock' */
179 static unsigned int shrinker_run_no;
181 /* protects the mounted 'erofs_sb_list' */
182 static DEFINE_SPINLOCK(erofs_sb_list_lock);
183 static LIST_HEAD(erofs_sb_list);
185 void erofs_register_super(struct super_block *sb)
187 struct erofs_sb_info *sbi = EROFS_SB(sb);
189 mutex_init(&sbi->umount_mutex);
191 spin_lock(&erofs_sb_list_lock);
192 list_add(&sbi->list, &erofs_sb_list);
193 spin_unlock(&erofs_sb_list_lock);
196 void erofs_unregister_super(struct super_block *sb)
198 spin_lock(&erofs_sb_list_lock);
199 list_del(&EROFS_SB(sb)->list);
200 spin_unlock(&erofs_sb_list_lock);
203 unsigned long erofs_shrink_count(struct shrinker *shrink,
204 struct shrink_control *sc)
206 return atomic_long_read(&erofs_global_shrink_cnt);
209 unsigned long erofs_shrink_scan(struct shrinker *shrink,
210 struct shrink_control *sc)
212 struct erofs_sb_info *sbi;
215 unsigned long nr = sc->nr_to_scan;
217 unsigned long freed = 0;
219 spin_lock(&erofs_sb_list_lock);
221 run_no = ++shrinker_run_no;
224 /* Iterate over all mounted superblocks and try to shrink them */
225 p = erofs_sb_list.next;
226 while (p != &erofs_sb_list) {
227 sbi = list_entry(p, struct erofs_sb_info, list);
230 * We move the ones we do to the end of the list, so we stop
231 * when we see one we have already done.
233 if (sbi->shrinker_run_no == run_no)
236 if (!mutex_trylock(&sbi->umount_mutex)) {
241 spin_unlock(&erofs_sb_list_lock);
242 sbi->shrinker_run_no = run_no;
244 #ifdef CONFIG_EROFS_FS_ZIP
245 freed += erofs_shrink_workstation(sbi, nr, false);
248 spin_lock(&erofs_sb_list_lock);
249 /* Get the next list element before we move this one */
253 * Move this one to the end of the list to provide some
256 list_move_tail(&sbi->list, &erofs_sb_list);
257 mutex_unlock(&sbi->umount_mutex);
262 spin_unlock(&erofs_sb_list_lock);