^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2018 HUAWEI, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * https://www.huawei.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Created by Gao Xiang <gaoxiang25@huawei.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/pagevec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) if (!list_empty(pool)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) page = lru_to_page(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) DBG_BUGON(page_ref_count(page) != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) list_del(&page->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) page = alloc_page(gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #ifdef CONFIG_EROFS_FS_ZIP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* global shrink count (for all mounted EROFS instances) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static atomic_long_t erofs_global_shrink_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int erofs_workgroup_get(struct erofs_workgroup *grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) o = erofs_wait_on_workgroup_freezed(grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (o <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (atomic_cmpxchg(&grp->refcount, o, o + 1) != o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* decrease refcount paired by erofs_workgroup_put */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (o == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) atomic_long_dec(&erofs_global_shrink_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) pgoff_t index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct erofs_sb_info *sbi = EROFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct erofs_workgroup *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) grp = xa_load(&sbi->managed_pslots, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (grp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (erofs_workgroup_get(grp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* prefer to relax rcu read side */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) DBG_BUGON(index != grp->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct erofs_workgroup *grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct erofs_sb_info *const sbi = EROFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct erofs_workgroup *pre;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * Bump up a reference count before making this visible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * to others for the XArray in order to avoid potential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * UAF without serialized by xa_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) atomic_inc(&grp->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) xa_lock(&sbi->managed_pslots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pre = __xa_cmpxchg(&sbi->managed_pslots, grp->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) NULL, grp, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (pre) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (xa_is_err(pre)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) pre = ERR_PTR(xa_err(pre));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) } else if (erofs_workgroup_get(pre)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* try to legitimize the current in-tree one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) xa_unlock(&sbi->managed_pslots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) atomic_dec(&grp->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) grp = pre;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) xa_unlock(&sbi->managed_pslots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void __erofs_workgroup_free(struct erofs_workgroup *grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) atomic_long_dec(&erofs_global_shrink_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) erofs_workgroup_free_rcu(grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int erofs_workgroup_put(struct erofs_workgroup *grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int count = atomic_dec_return(&grp->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (count == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) atomic_long_inc(&erofs_global_shrink_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) else if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) __erofs_workgroup_free(grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct erofs_workgroup *grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * If managed cache is on, refcount of workgroups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * themselves could be < 0 (freezed). In other words,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * there is no guarantee that all refcounts > 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (!erofs_workgroup_try_to_freeze(grp, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * Note that all cached pages should be unattached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * before deleted from the XArray. Otherwise some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * cached pages could be still attached to the orphan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * old workgroup when the new one is available in the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (erofs_try_to_free_all_cached_pages(sbi, grp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) erofs_workgroup_unfreeze(grp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * It's impossible to fail after the workgroup is freezed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * however in order to avoid some race conditions, add a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * DBG_BUGON to observe this in advance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) DBG_BUGON(__xa_erase(&sbi->managed_pslots, grp->index) != grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* last refcount should be connected with its managed pslot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) erofs_workgroup_unfreeze(grp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) __erofs_workgroup_free(grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned long nr_shrink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct erofs_workgroup *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned int freed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) unsigned long index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) xa_lock(&sbi->managed_pslots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) xa_for_each(&sbi->managed_pslots, index, grp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* try to shrink each valid workgroup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!erofs_try_to_release_workgroup(sbi, grp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) xa_unlock(&sbi->managed_pslots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ++freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (!--nr_shrink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) xa_lock(&sbi->managed_pslots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) xa_unlock(&sbi->managed_pslots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* protected by 'erofs_sb_list_lock' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static unsigned int shrinker_run_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* protects the mounted 'erofs_sb_list' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static DEFINE_SPINLOCK(erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static LIST_HEAD(erofs_sb_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) void erofs_shrinker_register(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct erofs_sb_info *sbi = EROFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mutex_init(&sbi->umount_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) spin_lock(&erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) list_add(&sbi->list, &erofs_sb_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) spin_unlock(&erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) void erofs_shrinker_unregister(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct erofs_sb_info *const sbi = EROFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) mutex_lock(&sbi->umount_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /* clean up all remaining workgroups in memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) erofs_shrink_workstation(sbi, ~0UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) spin_lock(&erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) list_del(&sbi->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) spin_unlock(&erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) mutex_unlock(&sbi->umount_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static unsigned long erofs_shrink_count(struct shrinker *shrink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct shrink_control *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return atomic_long_read(&erofs_global_shrink_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static unsigned long erofs_shrink_scan(struct shrinker *shrink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct shrink_control *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct erofs_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct list_head *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) unsigned long nr = sc->nr_to_scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) unsigned int run_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) unsigned long freed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) spin_lock(&erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) run_no = ++shrinker_run_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) } while (run_no == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /* Iterate over all mounted superblocks and try to shrink them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) p = erofs_sb_list.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) while (p != &erofs_sb_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) sbi = list_entry(p, struct erofs_sb_info, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * We move the ones we do to the end of the list, so we stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * when we see one we have already done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (sbi->shrinker_run_no == run_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!mutex_trylock(&sbi->umount_mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) p = p->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) spin_unlock(&erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) sbi->shrinker_run_no = run_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) freed += erofs_shrink_workstation(sbi, nr - freed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) spin_lock(&erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* Get the next list element before we move this one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) p = p->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * Move this one to the end of the list to provide some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * fairness.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) list_move_tail(&sbi->list, &erofs_sb_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) mutex_unlock(&sbi->umount_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (freed >= nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) spin_unlock(&erofs_sb_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static struct shrinker erofs_shrinker_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) .scan_objects = erofs_shrink_scan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) .count_objects = erofs_shrink_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) .seeks = DEFAULT_SEEKS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int __init erofs_init_shrinker(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return register_shrinker(&erofs_shrinker_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) void erofs_exit_shrinker(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) unregister_shrinker(&erofs_shrinker_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) #endif /* !CONFIG_EROFS_FS_ZIP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)