1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
8 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp)
10 struct page *page = *pagepool;
13 DBG_BUGON(page_ref_count(page) != 1);
14 *pagepool = (struct page *)page_private(page);
16 page = alloc_page(gfp);
21 void erofs_release_pages(struct page **pagepool)
24 struct page *page = *pagepool;
26 *pagepool = (struct page *)page_private(page);
31 #ifdef CONFIG_EROFS_FS_ZIP
32 /* global shrink count (for all mounted EROFS instances) */
33 static atomic_long_t erofs_global_shrink_cnt;
35 static bool erofs_workgroup_get(struct erofs_workgroup *grp)
37 if (lockref_get_not_zero(&grp->lockref))
40 spin_lock(&grp->lockref.lock);
41 if (__lockref_is_dead(&grp->lockref)) {
42 spin_unlock(&grp->lockref.lock);
46 if (!grp->lockref.count++)
47 atomic_long_dec(&erofs_global_shrink_cnt);
48 spin_unlock(&grp->lockref.lock);
52 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
55 struct erofs_sb_info *sbi = EROFS_SB(sb);
56 struct erofs_workgroup *grp;
60 grp = xa_load(&sbi->managed_pslots, index);
62 if (!erofs_workgroup_get(grp)) {
63 /* prefer to relax rcu read side */
68 DBG_BUGON(index != grp->index);
74 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
75 struct erofs_workgroup *grp)
77 struct erofs_sb_info *const sbi = EROFS_SB(sb);
78 struct erofs_workgroup *pre;
80 DBG_BUGON(grp->lockref.count < 1);
82 xa_lock(&sbi->managed_pslots);
83 pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index,
87 pre = ERR_PTR(xa_err(pre));
88 } else if (!erofs_workgroup_get(pre)) {
89 /* try to legitimize the current in-tree one */
90 xa_unlock(&sbi->managed_pslots);
96 xa_unlock(&sbi->managed_pslots);
100 static void __erofs_workgroup_free(struct erofs_workgroup *grp)
102 atomic_long_dec(&erofs_global_shrink_cnt);
103 erofs_workgroup_free_rcu(grp);
106 void erofs_workgroup_put(struct erofs_workgroup *grp)
108 if (lockref_put_or_lock(&grp->lockref))
111 DBG_BUGON(__lockref_is_dead(&grp->lockref));
112 if (grp->lockref.count == 1)
113 atomic_long_inc(&erofs_global_shrink_cnt);
114 --grp->lockref.count;
115 spin_unlock(&grp->lockref.lock);
118 static bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
119 struct erofs_workgroup *grp)
123 spin_lock(&grp->lockref.lock);
124 if (grp->lockref.count)
128 * Note that all cached pages should be detached before deleted from
129 * the XArray. Otherwise some cached pages could be still attached to
130 * the orphan old workgroup when the new one is available in the tree.
132 if (erofs_try_to_free_all_cached_pages(sbi, grp))
136 * It's impossible to fail after the workgroup is freezed,
137 * however in order to avoid some race conditions, add a
138 * DBG_BUGON to observe this in advance.
140 DBG_BUGON(__xa_erase(&sbi->managed_pslots, grp->index) != grp);
142 lockref_mark_dead(&grp->lockref);
145 spin_unlock(&grp->lockref.lock);
147 __erofs_workgroup_free(grp);
151 static unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
152 unsigned long nr_shrink)
154 struct erofs_workgroup *grp;
155 unsigned int freed = 0;
158 xa_lock(&sbi->managed_pslots);
159 xa_for_each(&sbi->managed_pslots, index, grp) {
160 /* try to shrink each valid workgroup */
161 if (!erofs_try_to_release_workgroup(sbi, grp))
163 xa_unlock(&sbi->managed_pslots);
168 xa_lock(&sbi->managed_pslots);
170 xa_unlock(&sbi->managed_pslots);
174 /* protected by 'erofs_sb_list_lock' */
175 static unsigned int shrinker_run_no;
177 /* protects the mounted 'erofs_sb_list' */
178 static DEFINE_SPINLOCK(erofs_sb_list_lock);
179 static LIST_HEAD(erofs_sb_list);
181 void erofs_shrinker_register(struct super_block *sb)
183 struct erofs_sb_info *sbi = EROFS_SB(sb);
185 mutex_init(&sbi->umount_mutex);
187 spin_lock(&erofs_sb_list_lock);
188 list_add(&sbi->list, &erofs_sb_list);
189 spin_unlock(&erofs_sb_list_lock);
192 void erofs_shrinker_unregister(struct super_block *sb)
194 struct erofs_sb_info *const sbi = EROFS_SB(sb);
196 mutex_lock(&sbi->umount_mutex);
197 /* clean up all remaining workgroups in memory */
198 erofs_shrink_workstation(sbi, ~0UL);
200 spin_lock(&erofs_sb_list_lock);
201 list_del(&sbi->list);
202 spin_unlock(&erofs_sb_list_lock);
203 mutex_unlock(&sbi->umount_mutex);
206 static unsigned long erofs_shrink_count(struct shrinker *shrink,
207 struct shrink_control *sc)
209 return atomic_long_read(&erofs_global_shrink_cnt);
212 static unsigned long erofs_shrink_scan(struct shrinker *shrink,
213 struct shrink_control *sc)
215 struct erofs_sb_info *sbi;
218 unsigned long nr = sc->nr_to_scan;
220 unsigned long freed = 0;
222 spin_lock(&erofs_sb_list_lock);
224 run_no = ++shrinker_run_no;
225 } while (run_no == 0);
227 /* Iterate over all mounted superblocks and try to shrink them */
228 p = erofs_sb_list.next;
229 while (p != &erofs_sb_list) {
230 sbi = list_entry(p, struct erofs_sb_info, list);
233 * We move the ones we do to the end of the list, so we stop
234 * when we see one we have already done.
236 if (sbi->shrinker_run_no == run_no)
239 if (!mutex_trylock(&sbi->umount_mutex)) {
244 spin_unlock(&erofs_sb_list_lock);
245 sbi->shrinker_run_no = run_no;
247 freed += erofs_shrink_workstation(sbi, nr - freed);
249 spin_lock(&erofs_sb_list_lock);
250 /* Get the next list element before we move this one */
254 * Move this one to the end of the list to provide some
257 list_move_tail(&sbi->list, &erofs_sb_list);
258 mutex_unlock(&sbi->umount_mutex);
263 spin_unlock(&erofs_sb_list_lock);
267 static struct shrinker erofs_shrinker_info = {
268 .scan_objects = erofs_shrink_scan,
269 .count_objects = erofs_shrink_count,
270 .seeks = DEFAULT_SEEKS,
273 int __init erofs_init_shrinker(void)
275 return register_shrinker(&erofs_shrinker_info, "erofs-shrinker");
278 void erofs_exit_shrinker(void)
280 unregister_shrinker(&erofs_shrinker_info);
282 #endif /* !CONFIG_EROFS_FS_ZIP */