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);
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 /* radix_tree and the future XArray both don't use tagptr_t yet */
39 struct erofs_workgroup *erofs_find_workgroup(
40 struct super_block *sb, pgoff_t index, bool *tag)
42 struct erofs_sb_info *sbi = EROFS_SB(sb);
43 struct erofs_workgroup *grp;
48 grp = radix_tree_lookup(&sbi->workstn_tree, index);
50 *tag = radix_tree_exceptional_entry(grp);
51 grp = (void *)((unsigned long)grp &
52 ~RADIX_TREE_EXCEPTIONAL_ENTRY);
54 if (erofs_workgroup_get(grp, &oldcount)) {
55 /* prefer to relax rcu read side */
60 /* decrease refcount added by erofs_workgroup_put */
61 if (unlikely(oldcount == 1))
62 atomic_long_dec(&erofs_global_shrink_cnt);
63 BUG_ON(index != grp->index);
69 int erofs_register_workgroup(struct super_block *sb,
70 struct erofs_workgroup *grp,
73 struct erofs_sb_info *sbi;
76 /* grp->refcount should not < 1 */
77 BUG_ON(!atomic_read(&grp->refcount));
79 err = radix_tree_preload(GFP_NOFS);
84 erofs_workstn_lock(sbi);
87 grp = (void *)((unsigned long)grp |
88 1UL << RADIX_TREE_EXCEPTIONAL_SHIFT);
90 err = radix_tree_insert(&sbi->workstn_tree,
94 __erofs_workgroup_get(grp);
97 erofs_workstn_unlock(sbi);
98 radix_tree_preload_end();
102 extern void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
104 int erofs_workgroup_put(struct erofs_workgroup *grp)
106 int count = atomic_dec_return(&grp->refcount);
109 atomic_long_inc(&erofs_global_shrink_cnt);
111 atomic_long_dec(&erofs_global_shrink_cnt);
112 erofs_workgroup_free_rcu(grp);
117 unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
118 unsigned long nr_shrink,
121 pgoff_t first_index = 0;
122 void *batch[PAGEVEC_SIZE];
127 erofs_workstn_lock(sbi);
129 found = radix_tree_gang_lookup(&sbi->workstn_tree,
130 batch, first_index, PAGEVEC_SIZE);
132 for (i = 0; i < found; ++i) {
134 struct erofs_workgroup *grp = (void *)
135 ((unsigned long)batch[i] &
136 ~RADIX_TREE_EXCEPTIONAL_ENTRY);
138 first_index = grp->index + 1;
140 cnt = atomic_read(&grp->refcount);
146 #ifndef EROFS_FS_HAS_MANAGED_CACHE
149 if (!erofs_workgroup_try_to_freeze(grp, 1))
153 if (radix_tree_delete(&sbi->workstn_tree,
154 grp->index) != grp) {
155 #ifdef EROFS_FS_HAS_MANAGED_CACHE
157 erofs_workgroup_unfreeze(grp, 1);
162 #ifdef EROFS_FS_HAS_MANAGED_CACHE
163 if (erofs_try_to_free_all_cached_pages(sbi, grp))
166 erofs_workgroup_unfreeze(grp, 1);
168 /* (rarely) grabbed again when freeing */
169 erofs_workgroup_put(grp);
172 if (unlikely(!--nr_shrink))
175 erofs_workstn_unlock(sbi);
184 /* protected by 'erofs_sb_list_lock' */
185 static unsigned int shrinker_run_no;
187 /* protects the mounted 'erofs_sb_list' */
188 static DEFINE_SPINLOCK(erofs_sb_list_lock);
189 static LIST_HEAD(erofs_sb_list);
191 void erofs_register_super(struct super_block *sb)
193 struct erofs_sb_info *sbi = EROFS_SB(sb);
195 mutex_init(&sbi->umount_mutex);
197 spin_lock(&erofs_sb_list_lock);
198 list_add(&sbi->list, &erofs_sb_list);
199 spin_unlock(&erofs_sb_list_lock);
202 void erofs_unregister_super(struct super_block *sb)
204 spin_lock(&erofs_sb_list_lock);
205 list_del(&EROFS_SB(sb)->list);
206 spin_unlock(&erofs_sb_list_lock);
209 unsigned long erofs_shrink_count(struct shrinker *shrink,
210 struct shrink_control *sc)
212 return atomic_long_read(&erofs_global_shrink_cnt);
215 unsigned long erofs_shrink_scan(struct shrinker *shrink,
216 struct shrink_control *sc)
218 struct erofs_sb_info *sbi;
221 unsigned long nr = sc->nr_to_scan;
223 unsigned long freed = 0;
225 spin_lock(&erofs_sb_list_lock);
227 run_no = ++shrinker_run_no;
230 /* Iterate over all mounted superblocks and try to shrink them */
231 p = erofs_sb_list.next;
232 while (p != &erofs_sb_list) {
233 sbi = list_entry(p, struct erofs_sb_info, list);
236 * We move the ones we do to the end of the list, so we stop
237 * when we see one we have already done.
239 if (sbi->shrinker_run_no == run_no)
242 if (!mutex_trylock(&sbi->umount_mutex)) {
247 spin_unlock(&erofs_sb_list_lock);
248 sbi->shrinker_run_no = run_no;
250 #ifdef CONFIG_EROFS_FS_ZIP
251 freed += erofs_shrink_workstation(sbi, nr, false);
254 spin_lock(&erofs_sb_list_lock);
255 /* Get the next list element before we move this one */
259 * Move this one to the end of the list to provide some
262 list_move_tail(&sbi->list, &erofs_sb_list);
263 mutex_unlock(&sbi->umount_mutex);
268 spin_unlock(&erofs_sb_list_lock);