blob: ec9a1d780dc143c0bd8ab3d6b42b8532f022fe48 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiangb29e64d2018-07-26 20:21:59 +08002/*
Gao Xiangb29e64d2018-07-26 20:21:59 +08003 * Copyright (C) 2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiangb29e64d2018-07-26 20:21:59 +08005 */
Gao Xiangb29e64d2018-07-26 20:21:59 +08006#include "internal.h"
Gao Xiang3883a792018-07-26 20:22:06 +08007#include <linux/pagevec.h>
Gao Xiangb29e64d2018-07-26 20:21:59 +08008
Gao Xiangeaa91722021-10-22 17:01:20 +08009struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp)
Gao Xiangb29e64d2018-07-26 20:21:59 +080010{
Gao Xiangeaa91722021-10-22 17:01:20 +080011 struct page *page = *pagepool;
Gao Xiangb29e64d2018-07-26 20:21:59 +080012
Gao Xiangeaa91722021-10-22 17:01:20 +080013 if (page) {
Gao Xiangb25a1512019-07-31 23:57:43 +080014 DBG_BUGON(page_ref_count(page) != 1);
Gao Xiangeaa91722021-10-22 17:01:20 +080015 *pagepool = (struct page *)page_private(page);
Gao Xiangb29e64d2018-07-26 20:21:59 +080016 } else {
Gao Xiang5ddcee12019-11-21 21:59:54 +080017 page = alloc_page(gfp);
Gao Xiangb29e64d2018-07-26 20:21:59 +080018 }
19 return page;
20}
21
Gao Xiangeaa91722021-10-22 17:01:20 +080022void erofs_release_pages(struct page **pagepool)
23{
24 while (*pagepool) {
25 struct page *page = *pagepool;
26
27 *pagepool = (struct page *)page_private(page);
28 put_page(page);
29 }
30}
31
Gao Xiang22fe04a2019-07-31 23:57:39 +080032#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiange7e9a302018-07-26 20:22:05 +080033/* global shrink count (for all mounted EROFS instances) */
34static atomic_long_t erofs_global_shrink_cnt;
35
Gao Xiang4501ca32019-01-16 21:10:10 +080036static int erofs_workgroup_get(struct erofs_workgroup *grp)
Gao Xiangd60eff42019-01-16 16:59:53 +080037{
38 int o;
39
40repeat:
41 o = erofs_wait_on_workgroup_freezed(grp);
Gao Xiang8d8a09b2019-08-30 00:38:27 +080042 if (o <= 0)
Gao Xiangd60eff42019-01-16 16:59:53 +080043 return -1;
44
Gao Xiang8d8a09b2019-08-30 00:38:27 +080045 if (atomic_cmpxchg(&grp->refcount, o, o + 1) != o)
Gao Xiangd60eff42019-01-16 16:59:53 +080046 goto repeat;
47
Gao Xiang4501ca32019-01-16 21:10:10 +080048 /* decrease refcount paired by erofs_workgroup_put */
Gao Xiang8d8a09b2019-08-30 00:38:27 +080049 if (o == 1)
Gao Xiang4501ca32019-01-16 21:10:10 +080050 atomic_long_dec(&erofs_global_shrink_cnt);
Gao Xiangd60eff42019-01-16 16:59:53 +080051 return 0;
52}
Gao Xiange7e9a302018-07-26 20:22:05 +080053
Gao Xiang4501ca32019-01-16 21:10:10 +080054struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
Vladimir Zapolskiy997626d2020-01-02 14:01:16 +020055 pgoff_t index)
Gao Xiange7e9a302018-07-26 20:22:05 +080056{
57 struct erofs_sb_info *sbi = EROFS_SB(sb);
58 struct erofs_workgroup *grp;
Gao Xiange7e9a302018-07-26 20:22:05 +080059
60repeat:
61 rcu_read_lock();
Gao Xiang64094a02020-02-20 10:46:42 +080062 grp = xa_load(&sbi->managed_pslots, index);
Bhanusree Pola561fb352019-03-22 10:38:16 +080063 if (grp) {
Gao Xiang4501ca32019-01-16 21:10:10 +080064 if (erofs_workgroup_get(grp)) {
Gao Xiange7e9a302018-07-26 20:22:05 +080065 /* prefer to relax rcu read side */
66 rcu_read_unlock();
67 goto repeat;
68 }
69
Gao Xiangb8e076a2018-12-11 15:17:50 +080070 DBG_BUGON(index != grp->index);
Gao Xiange7e9a302018-07-26 20:22:05 +080071 }
72 rcu_read_unlock();
73 return grp;
74}
75
Gao Xiang64094a02020-02-20 10:46:42 +080076struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
77 struct erofs_workgroup *grp)
Gao Xiange7e9a302018-07-26 20:22:05 +080078{
Gao Xiang64094a02020-02-20 10:46:42 +080079 struct erofs_sb_info *const sbi = EROFS_SB(sb);
80 struct erofs_workgroup *pre;
Gao Xiange7e9a302018-07-26 20:22:05 +080081
Gao Xiang51232df2018-11-23 01:16:00 +080082 /*
Gao Xiang64094a02020-02-20 10:46:42 +080083 * 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.
Gao Xiang51232df2018-11-23 01:16:00 +080086 */
Gao Xiang64094a02020-02-20 10:46:42 +080087 atomic_inc(&grp->refcount);
Gao Xiange7e9a302018-07-26 20:22:05 +080088
Gao Xiang64094a02020-02-20 10:46:42 +080089repeat:
90 xa_lock(&sbi->managed_pslots);
91 pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index,
92 NULL, grp, GFP_NOFS);
93 if (pre) {
94 if (xa_is_err(pre)) {
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);
99 cond_resched();
100 goto repeat;
101 }
102 atomic_dec(&grp->refcount);
103 grp = pre;
104 }
105 xa_unlock(&sbi->managed_pslots);
106 return grp;
Gao Xiange7e9a302018-07-26 20:22:05 +0800107}
108
Gao Xiang51232df2018-11-23 01:16:00 +0800109static void __erofs_workgroup_free(struct erofs_workgroup *grp)
110{
111 atomic_long_dec(&erofs_global_shrink_cnt);
112 erofs_workgroup_free_rcu(grp);
113}
114
Gao Xiang3883a792018-07-26 20:22:06 +0800115int erofs_workgroup_put(struct erofs_workgroup *grp)
116{
117 int count = atomic_dec_return(&grp->refcount);
118
119 if (count == 1)
120 atomic_long_inc(&erofs_global_shrink_cnt);
Gao Xiang51232df2018-11-23 01:16:00 +0800121 else if (!count)
122 __erofs_workgroup_free(grp);
Gao Xiang3883a792018-07-26 20:22:06 +0800123 return count;
124}
125
Jeremy Sowden0a64d622019-01-08 11:31:47 +0000126static bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
Gao Xiangbda17a42019-10-08 20:56:13 +0800127 struct erofs_workgroup *grp)
Gao Xiang51232df2018-11-23 01:16:00 +0800128{
129 /*
Gao Xiang2bb90cc2019-07-31 23:57:50 +0800130 * 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.
Gao Xiang51232df2018-11-23 01:16:00 +0800133 */
134 if (!erofs_workgroup_try_to_freeze(grp, 1))
135 return false;
136
137 /*
Gao Xiang2bb90cc2019-07-31 23:57:50 +0800138 * Note that all cached pages should be unattached
Gao Xiang64094a02020-02-20 10:46:42 +0800139 * before deleted from the XArray. Otherwise some
Gao Xiang2bb90cc2019-07-31 23:57:50 +0800140 * cached pages could be still attached to the orphan
141 * old workgroup when the new one is available in the tree.
Gao Xiang51232df2018-11-23 01:16:00 +0800142 */
143 if (erofs_try_to_free_all_cached_pages(sbi, grp)) {
144 erofs_workgroup_unfreeze(grp, 1);
145 return false;
146 }
147
148 /*
Gao Xiang2bb90cc2019-07-31 23:57:50 +0800149 * It's impossible to fail after the workgroup is freezed,
Gao Xiang51232df2018-11-23 01:16:00 +0800150 * however in order to avoid some race conditions, add a
151 * DBG_BUGON to observe this in advance.
152 */
Huang Jianan57bbeac2021-11-18 21:58:44 +0800153 DBG_BUGON(__xa_erase(&sbi->managed_pslots, grp->index) != grp);
Gao Xiang51232df2018-11-23 01:16:00 +0800154
Gao Xiangee4bf862020-07-30 02:02:35 +0800155 /* last refcount should be connected with its managed pslot. */
156 erofs_workgroup_unfreeze(grp, 0);
157 __erofs_workgroup_free(grp);
Gao Xiang51232df2018-11-23 01:16:00 +0800158 return true;
159}
160
Gao Xiang22fe04a2019-07-31 23:57:39 +0800161static unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
Gao Xiangbda17a42019-10-08 20:56:13 +0800162 unsigned long nr_shrink)
Gao Xiange7e9a302018-07-26 20:22:05 +0800163{
Gao Xiang64094a02020-02-20 10:46:42 +0800164 struct erofs_workgroup *grp;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200165 unsigned int freed = 0;
Gao Xiang64094a02020-02-20 10:46:42 +0800166 unsigned long index;
Gao Xiang3883a792018-07-26 20:22:06 +0800167
Huang Jianan57bbeac2021-11-18 21:58:44 +0800168 xa_lock(&sbi->managed_pslots);
Gao Xiang64094a02020-02-20 10:46:42 +0800169 xa_for_each(&sbi->managed_pslots, index, grp) {
Gao Xiang51232df2018-11-23 01:16:00 +0800170 /* try to shrink each valid workgroup */
Gao Xiangbda17a42019-10-08 20:56:13 +0800171 if (!erofs_try_to_release_workgroup(sbi, grp))
Gao Xiang3883a792018-07-26 20:22:06 +0800172 continue;
Huang Jianan57bbeac2021-11-18 21:58:44 +0800173 xa_unlock(&sbi->managed_pslots);
Gao Xiang3883a792018-07-26 20:22:06 +0800174
Gao Xiang3883a792018-07-26 20:22:06 +0800175 ++freed;
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800176 if (!--nr_shrink)
Huang Jianan57bbeac2021-11-18 21:58:44 +0800177 return freed;
178 xa_lock(&sbi->managed_pslots);
Gao Xiang3883a792018-07-26 20:22:06 +0800179 }
Huang Jianan57bbeac2021-11-18 21:58:44 +0800180 xa_unlock(&sbi->managed_pslots);
Gao Xiang3883a792018-07-26 20:22:06 +0800181 return freed;
Gao Xiange7e9a302018-07-26 20:22:05 +0800182}
183
Gao Xianga1581312018-07-26 20:22:04 +0800184/* protected by 'erofs_sb_list_lock' */
185static unsigned int shrinker_run_no;
186
187/* protects the mounted 'erofs_sb_list' */
188static DEFINE_SPINLOCK(erofs_sb_list_lock);
Gao Xiang2497ee42018-07-26 20:22:03 +0800189static LIST_HEAD(erofs_sb_list);
190
Gao Xiang22fe04a2019-07-31 23:57:39 +0800191void erofs_shrinker_register(struct super_block *sb)
Gao Xiang2497ee42018-07-26 20:22:03 +0800192{
Gao Xianga1581312018-07-26 20:22:04 +0800193 struct erofs_sb_info *sbi = EROFS_SB(sb);
194
195 mutex_init(&sbi->umount_mutex);
196
197 spin_lock(&erofs_sb_list_lock);
198 list_add(&sbi->list, &erofs_sb_list);
199 spin_unlock(&erofs_sb_list_lock);
Gao Xiang2497ee42018-07-26 20:22:03 +0800200}
201
Gao Xiang22fe04a2019-07-31 23:57:39 +0800202void erofs_shrinker_unregister(struct super_block *sb)
Gao Xiang2497ee42018-07-26 20:22:03 +0800203{
Gao Xiang22fe04a2019-07-31 23:57:39 +0800204 struct erofs_sb_info *const sbi = EROFS_SB(sb);
205
206 mutex_lock(&sbi->umount_mutex);
Gao Xiangbda17a42019-10-08 20:56:13 +0800207 /* clean up all remaining workgroups in memory */
208 erofs_shrink_workstation(sbi, ~0UL);
Gao Xiang22fe04a2019-07-31 23:57:39 +0800209
Gao Xianga1581312018-07-26 20:22:04 +0800210 spin_lock(&erofs_sb_list_lock);
Gao Xiang22fe04a2019-07-31 23:57:39 +0800211 list_del(&sbi->list);
Gao Xianga1581312018-07-26 20:22:04 +0800212 spin_unlock(&erofs_sb_list_lock);
Gao Xiang22fe04a2019-07-31 23:57:39 +0800213 mutex_unlock(&sbi->umount_mutex);
Gao Xianga1581312018-07-26 20:22:04 +0800214}
215
Gao Xiangd55bc7b2019-01-16 16:59:55 +0800216static unsigned long erofs_shrink_count(struct shrinker *shrink,
217 struct shrink_control *sc)
Gao Xianga1581312018-07-26 20:22:04 +0800218{
219 return atomic_long_read(&erofs_global_shrink_cnt);
220}
221
Gao Xiangd55bc7b2019-01-16 16:59:55 +0800222static unsigned long erofs_shrink_scan(struct shrinker *shrink,
223 struct shrink_control *sc)
Gao Xianga1581312018-07-26 20:22:04 +0800224{
225 struct erofs_sb_info *sbi;
226 struct list_head *p;
227
228 unsigned long nr = sc->nr_to_scan;
229 unsigned int run_no;
230 unsigned long freed = 0;
231
232 spin_lock(&erofs_sb_list_lock);
Gao Xiang2bb90cc2019-07-31 23:57:50 +0800233 do {
Gao Xianga1581312018-07-26 20:22:04 +0800234 run_no = ++shrinker_run_no;
Gao Xiang2bb90cc2019-07-31 23:57:50 +0800235 } while (run_no == 0);
Gao Xianga1581312018-07-26 20:22:04 +0800236
237 /* Iterate over all mounted superblocks and try to shrink them */
238 p = erofs_sb_list.next;
239 while (p != &erofs_sb_list) {
240 sbi = list_entry(p, struct erofs_sb_info, list);
241
242 /*
243 * We move the ones we do to the end of the list, so we stop
244 * when we see one we have already done.
245 */
246 if (sbi->shrinker_run_no == run_no)
247 break;
248
249 if (!mutex_trylock(&sbi->umount_mutex)) {
250 p = p->next;
251 continue;
252 }
253
254 spin_unlock(&erofs_sb_list_lock);
255 sbi->shrinker_run_no = run_no;
256
Gao Xiang9d5a09c2020-02-26 16:10:06 +0800257 freed += erofs_shrink_workstation(sbi, nr - freed);
Gao Xianga1581312018-07-26 20:22:04 +0800258
259 spin_lock(&erofs_sb_list_lock);
260 /* Get the next list element before we move this one */
261 p = p->next;
262
263 /*
264 * Move this one to the end of the list to provide some
265 * fairness.
266 */
267 list_move_tail(&sbi->list, &erofs_sb_list);
268 mutex_unlock(&sbi->umount_mutex);
269
270 if (freed >= nr)
271 break;
272 }
273 spin_unlock(&erofs_sb_list_lock);
274 return freed;
Gao Xiang2497ee42018-07-26 20:22:03 +0800275}
276
Gao Xiang22fe04a2019-07-31 23:57:39 +0800277static struct shrinker erofs_shrinker_info = {
Gao Xiangd55bc7b2019-01-16 16:59:55 +0800278 .scan_objects = erofs_shrink_scan,
279 .count_objects = erofs_shrink_count,
280 .seeks = DEFAULT_SEEKS,
281};
282
Gao Xiang22fe04a2019-07-31 23:57:39 +0800283int __init erofs_init_shrinker(void)
284{
285 return register_shrinker(&erofs_shrinker_info);
286}
287
288void erofs_exit_shrinker(void)
289{
290 unregister_shrinker(&erofs_shrinker_info);
291}
292#endif /* !CONFIG_EROFS_FS_ZIP */