blob: fd4d47dfcea16f2adc3aad6fc2bebd54e985d01d [file] [log] [blame]
Dmitry Fomichevbae9a0a2019-08-02 15:02:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Damien Le Moal3b1a94c2017-06-07 15:55:39 +09002/*
3 * Copyright (C) 2017 Western Digital Corporation or its affiliates.
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm-zoned.h"
9
10#include <linux/module.h>
11
12#define DM_MSG_PREFIX "zoned reclaim"
13
14struct dmz_reclaim {
15 struct dmz_metadata *metadata;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090016
17 struct delayed_work work;
18 struct workqueue_struct *wq;
19
20 struct dm_kcopyd_client *kc;
21 struct dm_kcopyd_throttle kc_throttle;
22 int kc_err;
23
24 unsigned long flags;
25
26 /* Last target access time */
27 unsigned long atime;
28};
29
30/*
31 * Reclaim state flags.
32 */
33enum {
34 DMZ_RECLAIM_KCOPY,
35};
36
37/*
38 * Number of seconds of target BIO inactivity to consider the target idle.
39 */
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -070040#define DMZ_IDLE_PERIOD (10UL * HZ)
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090041
42/*
43 * Percentage of unmapped (free) random zones below which reclaim starts
44 * even if the target is busy.
45 */
Hannes Reinecke34f5aff2020-05-19 10:14:20 +020046#define DMZ_RECLAIM_LOW_UNMAP_ZONES 30
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090047
48/*
49 * Percentage of unmapped (free) random zones above which reclaim will
50 * stop if the target is busy.
51 */
Hannes Reinecke34f5aff2020-05-19 10:14:20 +020052#define DMZ_RECLAIM_HIGH_UNMAP_ZONES 50
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090053
54/*
55 * Align a sequential zone write pointer to chunk_block.
56 */
57static int dmz_reclaim_align_wp(struct dmz_reclaim *zrc, struct dm_zone *zone,
58 sector_t block)
59{
60 struct dmz_metadata *zmd = zrc->metadata;
Hannes Reinecke6c805f72020-05-11 10:24:24 +020061 struct dmz_dev *dev = dmz_zone_to_dev(zmd, zone);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090062 sector_t wp_block = zone->wp_block;
63 unsigned int nr_blocks;
64 int ret;
65
66 if (wp_block == block)
67 return 0;
68
69 if (wp_block > block)
70 return -EIO;
71
72 /*
73 * Zeroout the space between the write
74 * pointer and the requested position.
75 */
76 nr_blocks = block - wp_block;
Hannes Reinecke6c805f72020-05-11 10:24:24 +020077 ret = blkdev_issue_zeroout(dev->bdev,
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090078 dmz_start_sect(zmd, zone) + dmz_blk2sect(wp_block),
Damien Le Moal4218a952017-07-24 16:44:37 +090079 dmz_blk2sect(nr_blocks), GFP_NOIO, 0);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090080 if (ret) {
Hannes Reinecke6c805f72020-05-11 10:24:24 +020081 dmz_dev_err(dev,
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090082 "Align zone %u wp %llu to %llu (wp+%u) blocks failed %d",
Hannes Reineckeb7122872020-05-11 10:24:18 +020083 zone->id, (unsigned long long)wp_block,
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090084 (unsigned long long)block, nr_blocks, ret);
Hannes Reinecke6c805f72020-05-11 10:24:24 +020085 dmz_check_bdev(dev);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090086 return ret;
87 }
88
89 zone->wp_block = block;
90
91 return 0;
92}
93
94/*
95 * dm_kcopyd_copy end notification.
96 */
97static void dmz_reclaim_kcopy_end(int read_err, unsigned long write_err,
98 void *context)
99{
100 struct dmz_reclaim *zrc = context;
101
102 if (read_err || write_err)
103 zrc->kc_err = -EIO;
104 else
105 zrc->kc_err = 0;
106
107 clear_bit_unlock(DMZ_RECLAIM_KCOPY, &zrc->flags);
108 smp_mb__after_atomic();
109 wake_up_bit(&zrc->flags, DMZ_RECLAIM_KCOPY);
110}
111
112/*
113 * Copy valid blocks of src_zone into dst_zone.
114 */
115static int dmz_reclaim_copy(struct dmz_reclaim *zrc,
116 struct dm_zone *src_zone, struct dm_zone *dst_zone)
117{
118 struct dmz_metadata *zmd = zrc->metadata;
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200119 struct dmz_dev *src_dev, *dst_dev;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900120 struct dm_io_region src, dst;
121 sector_t block = 0, end_block;
122 sector_t nr_blocks;
123 sector_t src_zone_block;
124 sector_t dst_zone_block;
125 unsigned long flags = 0;
126 int ret;
127
128 if (dmz_is_seq(src_zone))
129 end_block = src_zone->wp_block;
130 else
Hannes Reinecke36820562020-05-11 10:24:21 +0200131 end_block = dmz_zone_nr_blocks(zmd);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900132 src_zone_block = dmz_start_block(zmd, src_zone);
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200133 src_dev = dmz_zone_to_dev(zmd, src_zone);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900134 dst_zone_block = dmz_start_block(zmd, dst_zone);
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200135 dst_dev = dmz_zone_to_dev(zmd, dst_zone);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900136
137 if (dmz_is_seq(dst_zone))
138 set_bit(DM_KCOPYD_WRITE_SEQ, &flags);
139
140 while (block < end_block) {
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200141 if (src_dev->flags & DMZ_BDEV_DYING)
142 return -EIO;
143 if (dst_dev->flags & DMZ_BDEV_DYING)
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -0700144 return -EIO;
145
Hannes Reineckea16b7de2020-05-19 10:14:23 +0200146 if (dmz_reclaim_should_terminate(src_zone))
147 return -EINTR;
148
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900149 /* Get a valid region from the source zone */
150 ret = dmz_first_valid_block(zmd, src_zone, &block);
151 if (ret <= 0)
152 return ret;
153 nr_blocks = ret;
154
155 /*
156 * If we are writing in a sequential zone, we must make sure
157 * that writes are sequential. So Zeroout any eventual hole
158 * between writes.
159 */
160 if (dmz_is_seq(dst_zone)) {
161 ret = dmz_reclaim_align_wp(zrc, dst_zone, block);
162 if (ret)
163 return ret;
164 }
165
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200166 src.bdev = src_dev->bdev;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900167 src.sector = dmz_blk2sect(src_zone_block + block);
168 src.count = dmz_blk2sect(nr_blocks);
169
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200170 dst.bdev = dst_dev->bdev;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900171 dst.sector = dmz_blk2sect(dst_zone_block + block);
172 dst.count = src.count;
173
174 /* Copy the valid region */
175 set_bit(DMZ_RECLAIM_KCOPY, &zrc->flags);
Mike Snitzer7209049d2018-07-31 17:27:02 -0400176 dm_kcopyd_copy(zrc->kc, &src, 1, &dst, flags,
177 dmz_reclaim_kcopy_end, zrc);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900178
179 /* Wait for copy to complete */
180 wait_on_bit_io(&zrc->flags, DMZ_RECLAIM_KCOPY,
181 TASK_UNINTERRUPTIBLE);
182 if (zrc->kc_err)
183 return zrc->kc_err;
184
185 block += nr_blocks;
186 if (dmz_is_seq(dst_zone))
187 dst_zone->wp_block = block;
188 }
189
190 return 0;
191}
192
193/*
194 * Move valid blocks of dzone buffer zone into dzone (after its write pointer)
195 * and free the buffer zone.
196 */
197static int dmz_reclaim_buf(struct dmz_reclaim *zrc, struct dm_zone *dzone)
198{
199 struct dm_zone *bzone = dzone->bzone;
200 sector_t chunk_block = dzone->wp_block;
201 struct dmz_metadata *zmd = zrc->metadata;
202 int ret;
203
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200204 DMDEBUG("(%s): Chunk %u, move buf zone %u (weight %u) to data zone %u (weight %u)",
205 dmz_metadata_label(zmd),
206 dzone->chunk, bzone->id, dmz_weight(bzone),
207 dzone->id, dmz_weight(dzone));
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900208
209 /* Flush data zone into the buffer zone */
210 ret = dmz_reclaim_copy(zrc, bzone, dzone);
211 if (ret < 0)
212 return ret;
213
214 dmz_lock_flush(zmd);
215
216 /* Validate copied blocks */
217 ret = dmz_merge_valid_blocks(zmd, bzone, dzone, chunk_block);
218 if (ret == 0) {
219 /* Free the buffer zone */
Hannes Reinecke36820562020-05-11 10:24:21 +0200220 dmz_invalidate_blocks(zmd, bzone, 0, dmz_zone_nr_blocks(zmd));
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900221 dmz_lock_map(zmd);
222 dmz_unmap_zone(zmd, bzone);
223 dmz_unlock_zone_reclaim(dzone);
224 dmz_free_zone(zmd, bzone);
225 dmz_unlock_map(zmd);
226 }
227
228 dmz_unlock_flush(zmd);
229
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700230 return ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900231}
232
233/*
234 * Merge valid blocks of dzone into its buffer zone and free dzone.
235 */
236static int dmz_reclaim_seq_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
237{
238 unsigned int chunk = dzone->chunk;
239 struct dm_zone *bzone = dzone->bzone;
240 struct dmz_metadata *zmd = zrc->metadata;
241 int ret = 0;
242
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200243 DMDEBUG("(%s): Chunk %u, move data zone %u (weight %u) to buf zone %u (weight %u)",
244 dmz_metadata_label(zmd),
245 chunk, dzone->id, dmz_weight(dzone),
246 bzone->id, dmz_weight(bzone));
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900247
248 /* Flush data zone into the buffer zone */
249 ret = dmz_reclaim_copy(zrc, dzone, bzone);
250 if (ret < 0)
251 return ret;
252
253 dmz_lock_flush(zmd);
254
255 /* Validate copied blocks */
256 ret = dmz_merge_valid_blocks(zmd, dzone, bzone, 0);
257 if (ret == 0) {
258 /*
259 * Free the data zone and remap the chunk to
260 * the buffer zone.
261 */
Hannes Reinecke36820562020-05-11 10:24:21 +0200262 dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd));
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900263 dmz_lock_map(zmd);
264 dmz_unmap_zone(zmd, bzone);
265 dmz_unmap_zone(zmd, dzone);
266 dmz_unlock_zone_reclaim(dzone);
267 dmz_free_zone(zmd, dzone);
268 dmz_map_zone(zmd, bzone, chunk);
269 dmz_unlock_map(zmd);
270 }
271
272 dmz_unlock_flush(zmd);
273
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700274 return ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900275}
276
277/*
278 * Move valid blocks of the random data zone dzone into a free sequential zone.
279 * Once blocks are moved, remap the zone chunk to the sequential zone.
280 */
281static int dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
282{
283 unsigned int chunk = dzone->chunk;
284 struct dm_zone *szone = NULL;
285 struct dmz_metadata *zmd = zrc->metadata;
286 int ret;
Hannes Reineckec5c78852020-05-19 10:14:22 +0200287 int alloc_flags = DMZ_ALLOC_SEQ;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900288
Hannes Reinecke90a9b862020-05-19 10:14:21 +0200289 /* Get a free random or sequential zone */
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900290 dmz_lock_map(zmd);
Hannes Reineckec5c78852020-05-19 10:14:22 +0200291again:
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200292 szone = dmz_alloc_zone(zmd, alloc_flags | DMZ_ALLOC_RECLAIM);
Hannes Reineckec5c78852020-05-19 10:14:22 +0200293 if (!szone && alloc_flags == DMZ_ALLOC_SEQ && dmz_nr_cache_zones(zmd)) {
294 alloc_flags = DMZ_ALLOC_RND;
295 goto again;
296 }
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900297 dmz_unlock_map(zmd);
298 if (!szone)
299 return -ENOSPC;
300
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200301 DMDEBUG("(%s): Chunk %u, move %s zone %u (weight %u) to %s zone %u",
302 dmz_metadata_label(zmd), chunk,
303 dmz_is_cache(dzone) ? "cache" : "rnd",
304 dzone->id, dmz_weight(dzone),
305 dmz_is_rnd(szone) ? "rnd" : "seq", szone->id);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900306
307 /* Flush the random data zone into the sequential zone */
308 ret = dmz_reclaim_copy(zrc, dzone, szone);
309
310 dmz_lock_flush(zmd);
311
312 if (ret == 0) {
313 /* Validate copied blocks */
314 ret = dmz_copy_valid_blocks(zmd, dzone, szone);
315 }
316 if (ret) {
317 /* Free the sequential zone */
318 dmz_lock_map(zmd);
319 dmz_free_zone(zmd, szone);
320 dmz_unlock_map(zmd);
321 } else {
322 /* Free the data zone and remap the chunk */
Hannes Reinecke36820562020-05-11 10:24:21 +0200323 dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd));
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900324 dmz_lock_map(zmd);
325 dmz_unmap_zone(zmd, dzone);
326 dmz_unlock_zone_reclaim(dzone);
327 dmz_free_zone(zmd, dzone);
328 dmz_map_zone(zmd, szone, chunk);
329 dmz_unlock_map(zmd);
330 }
331
332 dmz_unlock_flush(zmd);
333
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700334 return ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900335}
336
337/*
338 * Reclaim an empty zone.
339 */
340static void dmz_reclaim_empty(struct dmz_reclaim *zrc, struct dm_zone *dzone)
341{
342 struct dmz_metadata *zmd = zrc->metadata;
343
344 dmz_lock_flush(zmd);
345 dmz_lock_map(zmd);
346 dmz_unmap_zone(zmd, dzone);
347 dmz_unlock_zone_reclaim(dzone);
348 dmz_free_zone(zmd, dzone);
349 dmz_unlock_map(zmd);
350 dmz_unlock_flush(zmd);
351}
352
353/*
Hannes Reinecke90a9b862020-05-19 10:14:21 +0200354 * Test if the target device is idle.
355 */
356static inline int dmz_target_idle(struct dmz_reclaim *zrc)
357{
358 return time_is_before_jiffies(zrc->atime + DMZ_IDLE_PERIOD);
359}
360
361/*
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900362 * Find a candidate zone for reclaim and process it.
363 */
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700364static int dmz_do_reclaim(struct dmz_reclaim *zrc)
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900365{
366 struct dmz_metadata *zmd = zrc->metadata;
367 struct dm_zone *dzone;
368 struct dm_zone *rzone;
369 unsigned long start;
370 int ret;
371
372 /* Get a data zone */
Hannes Reinecke90a9b862020-05-19 10:14:21 +0200373 dzone = dmz_get_zone_for_reclaim(zmd, dmz_target_idle(zrc));
Hannes Reineckec3ff4792020-06-02 13:09:44 +0200374 if (!dzone) {
375 DMDEBUG("(%s): No zone found to reclaim",
376 dmz_metadata_label(zmd));
Hannes Reinecke489dc0f2020-05-19 10:14:19 +0200377 return -EBUSY;
Hannes Reineckec3ff4792020-06-02 13:09:44 +0200378 }
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900379
380 start = jiffies;
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200381 if (dmz_is_cache(dzone) || dmz_is_rnd(dzone)) {
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900382 if (!dmz_weight(dzone)) {
383 /* Empty zone */
384 dmz_reclaim_empty(zrc, dzone);
385 ret = 0;
386 } else {
387 /*
388 * Reclaim the random data zone by moving its
389 * valid data blocks to a free sequential zone.
390 */
391 ret = dmz_reclaim_rnd_data(zrc, dzone);
392 }
393 rzone = dzone;
394
395 } else {
396 struct dm_zone *bzone = dzone->bzone;
397 sector_t chunk_block = 0;
398
399 ret = dmz_first_valid_block(zmd, bzone, &chunk_block);
400 if (ret < 0)
401 goto out;
402
403 if (ret == 0 || chunk_block >= dzone->wp_block) {
404 /*
405 * The buffer zone is empty or its valid blocks are
406 * after the data zone write pointer.
407 */
408 ret = dmz_reclaim_buf(zrc, dzone);
409 rzone = bzone;
410 } else {
411 /*
412 * Reclaim the data zone by merging it into the
413 * buffer zone so that the buffer zone itself can
414 * be later reclaimed.
415 */
416 ret = dmz_reclaim_seq_data(zrc, dzone);
417 rzone = dzone;
418 }
419 }
420out:
421 if (ret) {
Hannes Reineckec3ff4792020-06-02 13:09:44 +0200422 if (ret == -EINTR)
423 DMDEBUG("(%s): reclaim zone %u interrupted",
424 dmz_metadata_label(zmd), rzone->id);
425 else
426 DMDEBUG("(%s): Failed to reclaim zone %u, err %d",
427 dmz_metadata_label(zmd), rzone->id, ret);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900428 dmz_unlock_zone_reclaim(dzone);
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700429 return ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900430 }
431
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700432 ret = dmz_flush_metadata(zrc->metadata);
433 if (ret) {
Hannes Reinecke49de3b72020-05-14 08:09:29 +0200434 DMDEBUG("(%s): Metadata flush for zone %u failed, err %d",
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200435 dmz_metadata_label(zmd), rzone->id, ret);
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700436 return ret;
437 }
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900438
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200439 DMDEBUG("(%s): Reclaimed zone %u in %u ms",
440 dmz_metadata_label(zmd),
441 rzone->id, jiffies_to_msecs(jiffies - start));
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700442 return 0;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900443}
444
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200445static unsigned int dmz_reclaim_percentage(struct dmz_reclaim *zrc)
446{
447 struct dmz_metadata *zmd = zrc->metadata;
448 unsigned int nr_cache = dmz_nr_cache_zones(zmd);
449 unsigned int nr_rnd = dmz_nr_rnd_zones(zmd);
450 unsigned int nr_unmap, nr_zones;
451
452 if (nr_cache) {
453 nr_zones = nr_cache;
454 nr_unmap = dmz_nr_unmap_cache_zones(zmd);
455 } else {
456 nr_zones = nr_rnd;
457 nr_unmap = dmz_nr_unmap_rnd_zones(zmd);
458 }
459 return nr_unmap * 100 / nr_zones;
460}
461
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900462/*
463 * Test if reclaim is necessary.
464 */
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200465static bool dmz_should_reclaim(struct dmz_reclaim *zrc, unsigned int p_unmap)
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900466{
Hannes Reinecke90a9b862020-05-19 10:14:21 +0200467 unsigned int nr_reclaim = dmz_nr_rnd_zones(zrc->metadata);
468
469 if (dmz_nr_cache_zones(zrc->metadata))
470 nr_reclaim += dmz_nr_cache_zones(zrc->metadata);
471
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900472 /* Reclaim when idle */
Hannes Reinecke90a9b862020-05-19 10:14:21 +0200473 if (dmz_target_idle(zrc) && nr_reclaim)
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900474 return true;
475
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200476 /* If there are still plenty of cache zones, do not reclaim */
477 if (p_unmap >= DMZ_RECLAIM_HIGH_UNMAP_ZONES)
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900478 return false;
479
480 /*
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200481 * If the percentage of unmapped cache zones is low,
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900482 * reclaim even if the target is busy.
483 */
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200484 return p_unmap <= DMZ_RECLAIM_LOW_UNMAP_ZONES;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900485}
486
487/*
488 * Reclaim work function.
489 */
490static void dmz_reclaim_work(struct work_struct *work)
491{
492 struct dmz_reclaim *zrc = container_of(work, struct dmz_reclaim, work.work);
493 struct dmz_metadata *zmd = zrc->metadata;
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200494 unsigned int p_unmap;
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700495 int ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900496
Hannes Reinecked0e21ce2020-05-11 10:24:23 +0200497 if (dmz_dev_is_dying(zmd))
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -0700498 return;
499
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200500 p_unmap = dmz_reclaim_percentage(zrc);
501 if (!dmz_should_reclaim(zrc, p_unmap)) {
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900502 mod_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD);
503 return;
504 }
505
506 /*
507 * We need to start reclaiming random zones: set up zone copy
508 * throttling to either go fast if we are very low on random zones
509 * and slower if there are still some free random zones to avoid
510 * as much as possible to negatively impact the user workload.
511 */
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200512 if (dmz_target_idle(zrc) || p_unmap < DMZ_RECLAIM_LOW_UNMAP_ZONES / 2) {
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900513 /* Idle or very low percentage: go fast */
514 zrc->kc_throttle.throttle = 100;
515 } else {
516 /* Busy but we still have some random zone: throttle */
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200517 zrc->kc_throttle.throttle = min(75U, 100U - p_unmap / 2);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900518 }
519
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200520 DMDEBUG("(%s): Reclaim (%u): %s, %u%% free zones (%u/%u cache %u/%u random)",
Hannes Reinecke2234e732020-05-11 10:24:22 +0200521 dmz_metadata_label(zmd),
522 zrc->kc_throttle.throttle,
523 (dmz_target_idle(zrc) ? "Idle" : "Busy"),
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200524 p_unmap, dmz_nr_unmap_cache_zones(zmd),
525 dmz_nr_cache_zones(zmd),
526 dmz_nr_unmap_rnd_zones(zmd),
527 dmz_nr_rnd_zones(zmd));
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900528
Dmitry Fomichevb234c6d2019-08-10 14:43:09 -0700529 ret = dmz_do_reclaim(zrc);
Hannes Reineckea16b7de2020-05-19 10:14:23 +0200530 if (ret && ret != -EINTR) {
Hannes Reinecked0e21ce2020-05-11 10:24:23 +0200531 if (!dmz_check_dev(zmd))
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -0700532 return;
533 }
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900534
535 dmz_schedule_reclaim(zrc);
536}
537
538/*
539 * Initialize reclaim.
540 */
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200541int dmz_ctr_reclaim(struct dmz_metadata *zmd,
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900542 struct dmz_reclaim **reclaim)
543{
544 struct dmz_reclaim *zrc;
545 int ret;
546
547 zrc = kzalloc(sizeof(struct dmz_reclaim), GFP_KERNEL);
548 if (!zrc)
549 return -ENOMEM;
550
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900551 zrc->metadata = zmd;
552 zrc->atime = jiffies;
553
554 /* Reclaim kcopyd client */
555 zrc->kc = dm_kcopyd_client_create(&zrc->kc_throttle);
556 if (IS_ERR(zrc->kc)) {
557 ret = PTR_ERR(zrc->kc);
558 zrc->kc = NULL;
559 goto err;
560 }
561
562 /* Reclaim work */
563 INIT_DELAYED_WORK(&zrc->work, dmz_reclaim_work);
564 zrc->wq = alloc_ordered_workqueue("dmz_rwq_%s", WQ_MEM_RECLAIM,
Hannes Reinecke2234e732020-05-11 10:24:22 +0200565 dmz_metadata_label(zmd));
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900566 if (!zrc->wq) {
567 ret = -ENOMEM;
568 goto err;
569 }
570
571 *reclaim = zrc;
572 queue_delayed_work(zrc->wq, &zrc->work, 0);
573
574 return 0;
575err:
576 if (zrc->kc)
577 dm_kcopyd_client_destroy(zrc->kc);
578 kfree(zrc);
579
580 return ret;
581}
582
583/*
584 * Terminate reclaim.
585 */
586void dmz_dtr_reclaim(struct dmz_reclaim *zrc)
587{
588 cancel_delayed_work_sync(&zrc->work);
589 destroy_workqueue(zrc->wq);
590 dm_kcopyd_client_destroy(zrc->kc);
591 kfree(zrc);
592}
593
594/*
595 * Suspend reclaim.
596 */
597void dmz_suspend_reclaim(struct dmz_reclaim *zrc)
598{
599 cancel_delayed_work_sync(&zrc->work);
600}
601
602/*
603 * Resume reclaim.
604 */
605void dmz_resume_reclaim(struct dmz_reclaim *zrc)
606{
607 queue_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD);
608}
609
610/*
611 * BIO accounting.
612 */
613void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc)
614{
615 zrc->atime = jiffies;
616}
617
618/*
619 * Start reclaim if necessary.
620 */
621void dmz_schedule_reclaim(struct dmz_reclaim *zrc)
622{
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200623 unsigned int p_unmap = dmz_reclaim_percentage(zrc);
624
625 if (dmz_should_reclaim(zrc, p_unmap))
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900626 mod_delayed_work(zrc->wq, &zrc->work, 0);
627}