V3 version of the lazytime patches
[ext4-patch-queue.git] / limit-number-of-scanned-extents-in-status-tree-shrinker
blob773ba09e5ee114ee4d05e7eca6f0a95dc244889d
1 ext4: limit number of scanned extents in status tree shrinker
3 From: Jan Kara <jack@suse.cz>
5 Currently we scan extent status trees of inodes until we reclaim nr_to_scan
6 extents. This can however require a lot of scanning when there are lots
7 of delayed extents (as those cannot be reclaimed).
9 Change shrinker to work as shrinkers are supposed to and *scan* only
10 nr_to_scan extents regardless of how many extents did we actually
11 reclaim. We however need to be careful and avoid scanning each status
12 tree from the beginning - that could lead to a situation where we would
13 not be able to reclaim anything at all when first nr_to_scan extents in
14 the tree are always unreclaimable. We remember with each inode offset
15 where we stopped scanning and continue from there when we next come
16 across the inode.
18 Note that we also need to update places calling __es_shrink() manually
19 to pass reasonable nr_to_scan to have a chance of reclaiming anything and
20 not just 1.
22 Signed-off-by: Jan Kara <jack@suse.cz>
23 ---
24  fs/ext4/ext4.h           |  5 ++-
25  fs/ext4/extents_status.c | 91 +++++++++++++++++++++++++++++++-----------------
26  fs/ext4/super.c          |  1 +
27  3 files changed, 64 insertions(+), 33 deletions(-)
29 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
30 index 653d262e2a34..1515486c671b 100644
31 --- a/fs/ext4/ext4.h
32 +++ b/fs/ext4/ext4.h
33 @@ -890,6 +890,9 @@ struct ext4_inode_info {
34         struct list_head i_es_list;
35         unsigned int i_es_all_nr;       /* protected by i_es_lock */
36         unsigned int i_es_shk_nr;       /* protected by i_es_lock */
37 +       ext4_lblk_t i_es_shrink_lblk;   /* Offset where we start searching for
38 +                                          extents to shrink. Protected by
39 +                                          i_es_lock  */
41         /* ialloc */
42         ext4_group_t    i_last_alloc_group;
43 @@ -1330,7 +1333,7 @@ struct ext4_sb_info {
45         /* Reclaim extents from extent status tree */
46         struct shrinker s_es_shrinker;
47 -       struct list_head s_es_list;
48 +       struct list_head s_es_list;     /* List of inodes with reclaimable extents */
49         long s_es_nr_inode;
50         struct ext4_es_stats s_es_stats;
51         struct mb_cache *s_mb_cache;
52 diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
53 index de2d9d8bf22f..8f2aac4006d2 100644
54 --- a/fs/ext4/extents_status.c
55 +++ b/fs/ext4/extents_status.c
56 @@ -147,8 +147,7 @@ static struct kmem_cache *ext4_es_cachep;
57  static int __es_insert_extent(struct inode *inode, struct extent_status *newes);
58  static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
59                               ext4_lblk_t end);
60 -static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
61 -                                      int nr_to_scan);
62 +static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
63  static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
64                        struct ext4_inode_info *locked_ei);
66 @@ -716,7 +715,7 @@ int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
67  retry:
68         err = __es_insert_extent(inode, &newes);
69         if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
70 -                                         1, EXT4_I(inode)))
71 +                                         128, EXT4_I(inode)))
72                 goto retry;
73         if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
74                 err = 0;
75 @@ -874,7 +873,7 @@ retry:
76                                 es->es_len = orig_es.es_len;
77                                 if ((err == -ENOMEM) &&
78                                     __es_shrink(EXT4_SB(inode->i_sb),
79 -                                                       1, EXT4_I(inode)))
80 +                                                       128, EXT4_I(inode)))
81                                         goto retry;
82                                 goto out;
83                         }
84 @@ -976,8 +975,6 @@ retry:
85         spin_lock(&sbi->s_es_lock);
86         nr_to_walk = sbi->s_es_nr_inode;
87         while (nr_to_walk-- > 0) {
88 -               int shrunk;
90                 if (list_empty(&sbi->s_es_list)) {
91                         spin_unlock(&sbi->s_es_lock);
92                         goto out;
93 @@ -985,7 +982,7 @@ retry:
94                 ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
95                                       i_es_list);
96                 /* Move the inode to the tail */
97 -               list_move(&ei->i_es_list, sbi->s_es_list.prev);
98 +               list_move_tail(&ei->i_es_list, &sbi->s_es_list);
100                 /*
101                  * Normally we try hard to avoid shrinking precached inodes,
102 @@ -1007,13 +1004,10 @@ retry:
103                  */
104                 spin_unlock(&sbi->s_es_lock);
106 -               shrunk = __es_try_to_reclaim_extents(ei, nr_to_scan);
107 +               nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
108                 write_unlock(&ei->i_es_lock);
110 -               nr_shrunk += shrunk;
111 -               nr_to_scan -= shrunk;
113 -               if (nr_to_scan == 0)
114 +               if (nr_to_scan <= 0)
115                         goto out;
116                 spin_lock(&sbi->s_es_lock);
117         }
118 @@ -1029,7 +1023,7 @@ retry:
119         }
121         if (locked_ei && nr_shrunk == 0)
122 -               nr_shrunk = __es_try_to_reclaim_extents(locked_ei, nr_to_scan);
123 +               nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
125  out:
126         scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
127 @@ -1224,27 +1218,33 @@ void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
128         unregister_shrinker(&sbi->s_es_shrinker);
131 -static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
132 -                                      int nr_to_scan)
134 + * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
135 + * most *nr_to_scan extents, update *nr_to_scan accordingly.
136 + *
137 + * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
138 + * Increment *nr_shrunk by the number of reclaimed extents. Also update
139 + * ei->i_es_shrink_lblk to where we should continue scanning.
140 + */
141 +static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
142 +                                int *nr_to_scan, int *nr_shrunk)
144         struct inode *inode = &ei->vfs_inode;
145         struct ext4_es_tree *tree = &ei->i_es_tree;
146 -       struct rb_node *node;
147         struct extent_status *es;
148 -       unsigned long nr_shrunk = 0;
149 -       static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
150 -                                     DEFAULT_RATELIMIT_BURST);
152 -       if (ei->i_es_shk_nr == 0)
153 -               return 0;
154 +       struct rb_node *node;
156 -       if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
157 -           __ratelimit(&_rs))
158 -               ext4_warning(inode->i_sb, "forced shrink of precached extents");
159 +       es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
160 +       if (!es)
161 +               goto out_wrap;
162 +       node = &es->rb_node;
163 +       while (*nr_to_scan > 0) {
164 +               if (es->es_lblk > end) {
165 +                       ei->i_es_shrink_lblk = end + 1;
166 +                       return 0;
167 +               }
169 -       node = rb_first(&tree->root);
170 -       while (node != NULL) {
171 -               es = rb_entry(node, struct extent_status, rb_node);
172 +               (*nr_to_scan)--;
173                 node = rb_next(&es->rb_node);
174                 /*
175                  * We can't reclaim delayed extent from status tree because
176 @@ -1253,11 +1253,38 @@ static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
177                 if (!ext4_es_is_delayed(es)) {
178                         rb_erase(&es->rb_node, &tree->root);
179                         ext4_es_free_extent(inode, es);
180 -                       nr_shrunk++;
181 -                       if (--nr_to_scan == 0)
182 -                               break;
183 +                       (*nr_shrunk)++;
184                 }
185 +               if (!node)
186 +                       goto out_wrap;
187 +               es = rb_entry(node, struct extent_status, rb_node);
188         }
189 -       tree->cache_es = NULL;
190 +       ei->i_es_shrink_lblk = es->es_lblk;
191 +       return 1;
192 +out_wrap:
193 +       ei->i_es_shrink_lblk = 0;
194 +       return 0;
197 +static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
199 +       struct inode *inode = &ei->vfs_inode;
200 +       int nr_shrunk = 0;
201 +       ext4_lblk_t start = ei->i_es_shrink_lblk;
202 +       static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
203 +                                     DEFAULT_RATELIMIT_BURST);
205 +       if (ei->i_es_shk_nr == 0)
206 +               return 0;
208 +       if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
209 +           __ratelimit(&_rs))
210 +               ext4_warning(inode->i_sb, "forced shrink of precached extents");
212 +       if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
213 +           start != 0)
214 +               es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
216 +       ei->i_es_tree.cache_es = NULL;
217         return nr_shrunk;
219 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
220 index f108d84e7da2..b53c243a142b 100644
221 --- a/fs/ext4/super.c
222 +++ b/fs/ext4/super.c
223 @@ -883,6 +883,7 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
224         INIT_LIST_HEAD(&ei->i_es_list);
225         ei->i_es_all_nr = 0;
226         ei->i_es_shk_nr = 0;
227 +       ei->i_es_shrink_lblk = 0;
228         ei->i_reserved_data_blocks = 0;
229         ei->i_reserved_meta_blocks = 0;
230         ei->i_allocated_meta_blocks = 0;
231 -- 
232 1.8.1.4