1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
7 #include <linux/pagevec.h>
9 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp)
11 struct page *page = *pagepool;
14 DBG_BUGON(page_ref_count(page) != 1);
15 *pagepool = (struct page *)page_private(page);
17 page = alloc_page(gfp);
22 void erofs_release_pages(struct page **pagepool)
25 struct page *page = *pagepool;
27 *pagepool = (struct page *)page_private(page);
32 #ifdef CONFIG_EROFS_FS_ZIP
33 /* global shrink count (for all mounted EROFS instances) */
34 static atomic_long_t erofs_global_shrink_cnt;
36 static int erofs_workgroup_get(struct erofs_workgroup *grp)
41 o = erofs_wait_on_workgroup_freezed(grp);
45 if (atomic_cmpxchg(&grp->refcount, o, o + 1) != o)
48 /* decrease refcount paired by erofs_workgroup_put */
50 atomic_long_dec(&erofs_global_shrink_cnt);
54 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
57 struct erofs_sb_info *sbi = EROFS_SB(sb);
58 struct erofs_workgroup *grp;
62 grp = xa_load(&sbi->managed_pslots, index);
64 if (erofs_workgroup_get(grp)) {
65 /* prefer to relax rcu read side */
70 DBG_BUGON(index != grp->index);
76 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
77 struct erofs_workgroup *grp)
79 struct erofs_sb_info *const sbi = EROFS_SB(sb);
80 struct erofs_workgroup *pre;
83 * Bump up a reference count before making this visible
84 * to others for the XArray in order to avoid potential
85 * UAF without serialized by xa_lock.
87 atomic_inc(&grp->refcount);
90 xa_lock(&sbi->managed_pslots);
91 pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index,
95 pre = ERR_PTR(xa_err(pre));
96 } else if (erofs_workgroup_get(pre)) {
97 /* try to legitimize the current in-tree one */
98 xa_unlock(&sbi->managed_pslots);
102 atomic_dec(&grp->refcount);
105 xa_unlock(&sbi->managed_pslots);
109 static void __erofs_workgroup_free(struct erofs_workgroup *grp)
111 atomic_long_dec(&erofs_global_shrink_cnt);
112 erofs_workgroup_free_rcu(grp);
115 int erofs_workgroup_put(struct erofs_workgroup *grp)
117 int count = atomic_dec_return(&grp->refcount);
120 atomic_long_inc(&erofs_global_shrink_cnt);
122 __erofs_workgroup_free(grp);
126 static bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
127 struct erofs_workgroup *grp)
130 * If managed cache is on, refcount of workgroups
131 * themselves could be < 0 (freezed). In other words,
132 * there is no guarantee that all refcounts > 0.
134 if (!erofs_workgroup_try_to_freeze(grp, 1))
138 * Note that all cached pages should be unattached
139 * before deleted from the XArray. Otherwise some
140 * cached pages could be still attached to the orphan
141 * old workgroup when the new one is available in the tree.
143 if (erofs_try_to_free_all_cached_pages(sbi, grp)) {
144 erofs_workgroup_unfreeze(grp, 1);
149 * It's impossible to fail after the workgroup is freezed,
150 * however in order to avoid some race conditions, add a
151 * DBG_BUGON to observe this in advance.
153 DBG_BUGON(xa_erase(&sbi->managed_pslots, grp->index) != grp);
155 /* last refcount should be connected with its managed pslot. */
156 erofs_workgroup_unfreeze(grp, 0);
157 __erofs_workgroup_free(grp);
161 static unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
162 unsigned long nr_shrink)
164 struct erofs_workgroup *grp;
165 unsigned int freed = 0;
168 xa_for_each(&sbi->managed_pslots, index, grp) {
169 /* try to shrink each valid workgroup */
170 if (!erofs_try_to_release_workgroup(sbi, grp))
180 /* protected by 'erofs_sb_list_lock' */
181 static unsigned int shrinker_run_no;
183 /* protects the mounted 'erofs_sb_list' */
184 static DEFINE_SPINLOCK(erofs_sb_list_lock);
185 static LIST_HEAD(erofs_sb_list);
187 void erofs_shrinker_register(struct super_block *sb)
189 struct erofs_sb_info *sbi = EROFS_SB(sb);
191 mutex_init(&sbi->umount_mutex);
193 spin_lock(&erofs_sb_list_lock);
194 list_add(&sbi->list, &erofs_sb_list);
195 spin_unlock(&erofs_sb_list_lock);
198 void erofs_shrinker_unregister(struct super_block *sb)
200 struct erofs_sb_info *const sbi = EROFS_SB(sb);
202 mutex_lock(&sbi->umount_mutex);
203 /* clean up all remaining workgroups in memory */
204 erofs_shrink_workstation(sbi, ~0UL);
206 spin_lock(&erofs_sb_list_lock);
207 list_del(&sbi->list);
208 spin_unlock(&erofs_sb_list_lock);
209 mutex_unlock(&sbi->umount_mutex);
212 static unsigned long erofs_shrink_count(struct shrinker *shrink,
213 struct shrink_control *sc)
215 return atomic_long_read(&erofs_global_shrink_cnt);
218 static unsigned long erofs_shrink_scan(struct shrinker *shrink,
219 struct shrink_control *sc)
221 struct erofs_sb_info *sbi;
224 unsigned long nr = sc->nr_to_scan;
226 unsigned long freed = 0;
228 spin_lock(&erofs_sb_list_lock);
230 run_no = ++shrinker_run_no;
231 } while (run_no == 0);
233 /* Iterate over all mounted superblocks and try to shrink them */
234 p = erofs_sb_list.next;
235 while (p != &erofs_sb_list) {
236 sbi = list_entry(p, struct erofs_sb_info, list);
239 * We move the ones we do to the end of the list, so we stop
240 * when we see one we have already done.
242 if (sbi->shrinker_run_no == run_no)
245 if (!mutex_trylock(&sbi->umount_mutex)) {
250 spin_unlock(&erofs_sb_list_lock);
251 sbi->shrinker_run_no = run_no;
253 freed += erofs_shrink_workstation(sbi, nr - freed);
255 spin_lock(&erofs_sb_list_lock);
256 /* Get the next list element before we move this one */
260 * Move this one to the end of the list to provide some
263 list_move_tail(&sbi->list, &erofs_sb_list);
264 mutex_unlock(&sbi->umount_mutex);
269 spin_unlock(&erofs_sb_list_lock);
273 static struct shrinker erofs_shrinker_info = {
274 .scan_objects = erofs_shrink_scan,
275 .count_objects = erofs_shrink_count,
276 .seeks = DEFAULT_SEEKS,
279 int __init erofs_init_shrinker(void)
281 return register_shrinker(&erofs_shrinker_info);
284 void erofs_exit_shrinker(void)
286 unregister_shrinker(&erofs_shrinker_info);
288 #endif /* !CONFIG_EROFS_FS_ZIP */