blob: 1015b200330b8710e85334a78819e1e697d29274 [file] [log] [blame]
Damien Le Moal3b1a94c2017-06-07 15:55:39 +09001/*
2 * Copyright (C) 2017 Western Digital Corporation or its affiliates.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-zoned.h"
8
9#include <linux/module.h>
10
11#define DM_MSG_PREFIX "zoned reclaim"
12
13struct dmz_reclaim {
14 struct dmz_metadata *metadata;
15 struct dmz_dev *dev;
16
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 Fomichev2c4291b2019-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 */
46#define DMZ_RECLAIM_LOW_UNMAP_RND 30
47
48/*
49 * Percentage of unmapped (free) random zones above which reclaim will
50 * stop if the target is busy.
51 */
52#define DMZ_RECLAIM_HIGH_UNMAP_RND 50
53
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;
61 sector_t wp_block = zone->wp_block;
62 unsigned int nr_blocks;
63 int ret;
64
65 if (wp_block == block)
66 return 0;
67
68 if (wp_block > block)
69 return -EIO;
70
71 /*
72 * Zeroout the space between the write
73 * pointer and the requested position.
74 */
75 nr_blocks = block - wp_block;
76 ret = blkdev_issue_zeroout(zrc->dev->bdev,
77 dmz_start_sect(zmd, zone) + dmz_blk2sect(wp_block),
Damien Le Moal4218a952017-07-24 16:44:37 +090078 dmz_blk2sect(nr_blocks), GFP_NOIO, 0);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090079 if (ret) {
80 dmz_dev_err(zrc->dev,
81 "Align zone %u wp %llu to %llu (wp+%u) blocks failed %d",
82 dmz_id(zmd, zone), (unsigned long long)wp_block,
83 (unsigned long long)block, nr_blocks, ret);
Dmitry Fomichev6b3a5292019-11-06 14:34:35 -080084 dmz_check_bdev(zrc->dev);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090085 return ret;
86 }
87
88 zone->wp_block = block;
89
90 return 0;
91}
92
93/*
94 * dm_kcopyd_copy end notification.
95 */
96static void dmz_reclaim_kcopy_end(int read_err, unsigned long write_err,
97 void *context)
98{
99 struct dmz_reclaim *zrc = context;
100
101 if (read_err || write_err)
102 zrc->kc_err = -EIO;
103 else
104 zrc->kc_err = 0;
105
106 clear_bit_unlock(DMZ_RECLAIM_KCOPY, &zrc->flags);
107 smp_mb__after_atomic();
108 wake_up_bit(&zrc->flags, DMZ_RECLAIM_KCOPY);
109}
110
111/*
112 * Copy valid blocks of src_zone into dst_zone.
113 */
114static int dmz_reclaim_copy(struct dmz_reclaim *zrc,
115 struct dm_zone *src_zone, struct dm_zone *dst_zone)
116{
117 struct dmz_metadata *zmd = zrc->metadata;
118 struct dmz_dev *dev = zrc->dev;
119 struct dm_io_region src, dst;
120 sector_t block = 0, end_block;
121 sector_t nr_blocks;
122 sector_t src_zone_block;
123 sector_t dst_zone_block;
124 unsigned long flags = 0;
125 int ret;
126
127 if (dmz_is_seq(src_zone))
128 end_block = src_zone->wp_block;
129 else
130 end_block = dev->zone_nr_blocks;
131 src_zone_block = dmz_start_block(zmd, src_zone);
132 dst_zone_block = dmz_start_block(zmd, dst_zone);
133
134 if (dmz_is_seq(dst_zone))
135 set_bit(DM_KCOPYD_WRITE_SEQ, &flags);
136
137 while (block < end_block) {
Dmitry Fomichev2c4291b2019-08-10 14:43:11 -0700138 if (dev->flags & DMZ_BDEV_DYING)
139 return -EIO;
140
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900141 /* Get a valid region from the source zone */
142 ret = dmz_first_valid_block(zmd, src_zone, &block);
143 if (ret <= 0)
144 return ret;
145 nr_blocks = ret;
146
147 /*
148 * If we are writing in a sequential zone, we must make sure
149 * that writes are sequential. So Zeroout any eventual hole
150 * between writes.
151 */
152 if (dmz_is_seq(dst_zone)) {
153 ret = dmz_reclaim_align_wp(zrc, dst_zone, block);
154 if (ret)
155 return ret;
156 }
157
158 src.bdev = dev->bdev;
159 src.sector = dmz_blk2sect(src_zone_block + block);
160 src.count = dmz_blk2sect(nr_blocks);
161
162 dst.bdev = dev->bdev;
163 dst.sector = dmz_blk2sect(dst_zone_block + block);
164 dst.count = src.count;
165
166 /* Copy the valid region */
167 set_bit(DMZ_RECLAIM_KCOPY, &zrc->flags);
168 ret = dm_kcopyd_copy(zrc->kc, &src, 1, &dst, flags,
169 dmz_reclaim_kcopy_end, zrc);
170 if (ret)
171 return ret;
172
173 /* Wait for copy to complete */
174 wait_on_bit_io(&zrc->flags, DMZ_RECLAIM_KCOPY,
175 TASK_UNINTERRUPTIBLE);
176 if (zrc->kc_err)
177 return zrc->kc_err;
178
179 block += nr_blocks;
180 if (dmz_is_seq(dst_zone))
181 dst_zone->wp_block = block;
182 }
183
184 return 0;
185}
186
187/*
188 * Move valid blocks of dzone buffer zone into dzone (after its write pointer)
189 * and free the buffer zone.
190 */
191static int dmz_reclaim_buf(struct dmz_reclaim *zrc, struct dm_zone *dzone)
192{
193 struct dm_zone *bzone = dzone->bzone;
194 sector_t chunk_block = dzone->wp_block;
195 struct dmz_metadata *zmd = zrc->metadata;
196 int ret;
197
198 dmz_dev_debug(zrc->dev,
199 "Chunk %u, move buf zone %u (weight %u) to data zone %u (weight %u)",
200 dzone->chunk, dmz_id(zmd, bzone), dmz_weight(bzone),
201 dmz_id(zmd, dzone), dmz_weight(dzone));
202
203 /* Flush data zone into the buffer zone */
204 ret = dmz_reclaim_copy(zrc, bzone, dzone);
205 if (ret < 0)
206 return ret;
207
208 dmz_lock_flush(zmd);
209
210 /* Validate copied blocks */
211 ret = dmz_merge_valid_blocks(zmd, bzone, dzone, chunk_block);
212 if (ret == 0) {
213 /* Free the buffer zone */
214 dmz_invalidate_blocks(zmd, bzone, 0, zrc->dev->zone_nr_blocks);
215 dmz_lock_map(zmd);
216 dmz_unmap_zone(zmd, bzone);
217 dmz_unlock_zone_reclaim(dzone);
218 dmz_free_zone(zmd, bzone);
219 dmz_unlock_map(zmd);
220 }
221
222 dmz_unlock_flush(zmd);
223
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700224 return ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900225}
226
227/*
228 * Merge valid blocks of dzone into its buffer zone and free dzone.
229 */
230static int dmz_reclaim_seq_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
231{
232 unsigned int chunk = dzone->chunk;
233 struct dm_zone *bzone = dzone->bzone;
234 struct dmz_metadata *zmd = zrc->metadata;
235 int ret = 0;
236
237 dmz_dev_debug(zrc->dev,
238 "Chunk %u, move data zone %u (weight %u) to buf zone %u (weight %u)",
239 chunk, dmz_id(zmd, dzone), dmz_weight(dzone),
240 dmz_id(zmd, bzone), dmz_weight(bzone));
241
242 /* Flush data zone into the buffer zone */
243 ret = dmz_reclaim_copy(zrc, dzone, bzone);
244 if (ret < 0)
245 return ret;
246
247 dmz_lock_flush(zmd);
248
249 /* Validate copied blocks */
250 ret = dmz_merge_valid_blocks(zmd, dzone, bzone, 0);
251 if (ret == 0) {
252 /*
253 * Free the data zone and remap the chunk to
254 * the buffer zone.
255 */
256 dmz_invalidate_blocks(zmd, dzone, 0, zrc->dev->zone_nr_blocks);
257 dmz_lock_map(zmd);
258 dmz_unmap_zone(zmd, bzone);
259 dmz_unmap_zone(zmd, dzone);
260 dmz_unlock_zone_reclaim(dzone);
261 dmz_free_zone(zmd, dzone);
262 dmz_map_zone(zmd, bzone, chunk);
263 dmz_unlock_map(zmd);
264 }
265
266 dmz_unlock_flush(zmd);
267
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700268 return ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900269}
270
271/*
272 * Move valid blocks of the random data zone dzone into a free sequential zone.
273 * Once blocks are moved, remap the zone chunk to the sequential zone.
274 */
275static int dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
276{
277 unsigned int chunk = dzone->chunk;
278 struct dm_zone *szone = NULL;
279 struct dmz_metadata *zmd = zrc->metadata;
280 int ret;
281
282 /* Get a free sequential zone */
283 dmz_lock_map(zmd);
284 szone = dmz_alloc_zone(zmd, DMZ_ALLOC_RECLAIM);
285 dmz_unlock_map(zmd);
286 if (!szone)
287 return -ENOSPC;
288
289 dmz_dev_debug(zrc->dev,
290 "Chunk %u, move rnd zone %u (weight %u) to seq zone %u",
291 chunk, dmz_id(zmd, dzone), dmz_weight(dzone),
292 dmz_id(zmd, szone));
293
294 /* Flush the random data zone into the sequential zone */
295 ret = dmz_reclaim_copy(zrc, dzone, szone);
296
297 dmz_lock_flush(zmd);
298
299 if (ret == 0) {
300 /* Validate copied blocks */
301 ret = dmz_copy_valid_blocks(zmd, dzone, szone);
302 }
303 if (ret) {
304 /* Free the sequential zone */
305 dmz_lock_map(zmd);
306 dmz_free_zone(zmd, szone);
307 dmz_unlock_map(zmd);
308 } else {
309 /* Free the data zone and remap the chunk */
310 dmz_invalidate_blocks(zmd, dzone, 0, zrc->dev->zone_nr_blocks);
311 dmz_lock_map(zmd);
312 dmz_unmap_zone(zmd, dzone);
313 dmz_unlock_zone_reclaim(dzone);
314 dmz_free_zone(zmd, dzone);
315 dmz_map_zone(zmd, szone, chunk);
316 dmz_unlock_map(zmd);
317 }
318
319 dmz_unlock_flush(zmd);
320
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700321 return ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900322}
323
324/*
325 * Reclaim an empty zone.
326 */
327static void dmz_reclaim_empty(struct dmz_reclaim *zrc, struct dm_zone *dzone)
328{
329 struct dmz_metadata *zmd = zrc->metadata;
330
331 dmz_lock_flush(zmd);
332 dmz_lock_map(zmd);
333 dmz_unmap_zone(zmd, dzone);
334 dmz_unlock_zone_reclaim(dzone);
335 dmz_free_zone(zmd, dzone);
336 dmz_unlock_map(zmd);
337 dmz_unlock_flush(zmd);
338}
339
340/*
341 * Find a candidate zone for reclaim and process it.
342 */
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700343static int dmz_do_reclaim(struct dmz_reclaim *zrc)
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900344{
345 struct dmz_metadata *zmd = zrc->metadata;
346 struct dm_zone *dzone;
347 struct dm_zone *rzone;
348 unsigned long start;
349 int ret;
350
351 /* Get a data zone */
352 dzone = dmz_get_zone_for_reclaim(zmd);
Hannes Reinecke2296eda2020-05-19 10:14:19 +0200353 if (!dzone)
354 return -EBUSY;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900355
356 start = jiffies;
357
358 if (dmz_is_rnd(dzone)) {
359 if (!dmz_weight(dzone)) {
360 /* Empty zone */
361 dmz_reclaim_empty(zrc, dzone);
362 ret = 0;
363 } else {
364 /*
365 * Reclaim the random data zone by moving its
366 * valid data blocks to a free sequential zone.
367 */
368 ret = dmz_reclaim_rnd_data(zrc, dzone);
369 }
370 rzone = dzone;
371
372 } else {
373 struct dm_zone *bzone = dzone->bzone;
374 sector_t chunk_block = 0;
375
376 ret = dmz_first_valid_block(zmd, bzone, &chunk_block);
377 if (ret < 0)
378 goto out;
379
380 if (ret == 0 || chunk_block >= dzone->wp_block) {
381 /*
382 * The buffer zone is empty or its valid blocks are
383 * after the data zone write pointer.
384 */
385 ret = dmz_reclaim_buf(zrc, dzone);
386 rzone = bzone;
387 } else {
388 /*
389 * Reclaim the data zone by merging it into the
390 * buffer zone so that the buffer zone itself can
391 * be later reclaimed.
392 */
393 ret = dmz_reclaim_seq_data(zrc, dzone);
394 rzone = dzone;
395 }
396 }
397out:
398 if (ret) {
399 dmz_unlock_zone_reclaim(dzone);
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700400 return ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900401 }
402
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700403 ret = dmz_flush_metadata(zrc->metadata);
404 if (ret) {
405 dmz_dev_debug(zrc->dev,
406 "Metadata flush for zone %u failed, err %d\n",
407 dmz_id(zmd, rzone), ret);
408 return ret;
409 }
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900410
411 dmz_dev_debug(zrc->dev, "Reclaimed zone %u in %u ms",
412 dmz_id(zmd, rzone), jiffies_to_msecs(jiffies - start));
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700413 return 0;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900414}
415
416/*
417 * Test if the target device is idle.
418 */
419static inline int dmz_target_idle(struct dmz_reclaim *zrc)
420{
421 return time_is_before_jiffies(zrc->atime + DMZ_IDLE_PERIOD);
422}
423
424/*
425 * Test if reclaim is necessary.
426 */
427static bool dmz_should_reclaim(struct dmz_reclaim *zrc)
428{
429 struct dmz_metadata *zmd = zrc->metadata;
430 unsigned int nr_rnd = dmz_nr_rnd_zones(zmd);
431 unsigned int nr_unmap_rnd = dmz_nr_unmap_rnd_zones(zmd);
432 unsigned int p_unmap_rnd = nr_unmap_rnd * 100 / nr_rnd;
433
434 /* Reclaim when idle */
435 if (dmz_target_idle(zrc) && nr_unmap_rnd < nr_rnd)
436 return true;
437
438 /* If there are still plenty of random zones, do not reclaim */
439 if (p_unmap_rnd >= DMZ_RECLAIM_HIGH_UNMAP_RND)
440 return false;
441
442 /*
443 * If the percentage of unmappped random zones is low,
444 * reclaim even if the target is busy.
445 */
446 return p_unmap_rnd <= DMZ_RECLAIM_LOW_UNMAP_RND;
447}
448
449/*
450 * Reclaim work function.
451 */
452static void dmz_reclaim_work(struct work_struct *work)
453{
454 struct dmz_reclaim *zrc = container_of(work, struct dmz_reclaim, work.work);
455 struct dmz_metadata *zmd = zrc->metadata;
456 unsigned int nr_rnd, nr_unmap_rnd;
457 unsigned int p_unmap_rnd;
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700458 int ret;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900459
Dmitry Fomichev2c4291b2019-08-10 14:43:11 -0700460 if (dmz_bdev_is_dying(zrc->dev))
461 return;
462
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900463 if (!dmz_should_reclaim(zrc)) {
464 mod_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD);
465 return;
466 }
467
468 /*
469 * We need to start reclaiming random zones: set up zone copy
470 * throttling to either go fast if we are very low on random zones
471 * and slower if there are still some free random zones to avoid
472 * as much as possible to negatively impact the user workload.
473 */
474 nr_rnd = dmz_nr_rnd_zones(zmd);
475 nr_unmap_rnd = dmz_nr_unmap_rnd_zones(zmd);
476 p_unmap_rnd = nr_unmap_rnd * 100 / nr_rnd;
477 if (dmz_target_idle(zrc) || p_unmap_rnd < DMZ_RECLAIM_LOW_UNMAP_RND / 2) {
478 /* Idle or very low percentage: go fast */
479 zrc->kc_throttle.throttle = 100;
480 } else {
481 /* Busy but we still have some random zone: throttle */
482 zrc->kc_throttle.throttle = min(75U, 100U - p_unmap_rnd / 2);
483 }
484
485 dmz_dev_debug(zrc->dev,
486 "Reclaim (%u): %s, %u%% free rnd zones (%u/%u)",
487 zrc->kc_throttle.throttle,
488 (dmz_target_idle(zrc) ? "Idle" : "Busy"),
489 p_unmap_rnd, nr_unmap_rnd, nr_rnd);
490
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700491 ret = dmz_do_reclaim(zrc);
Dmitry Fomichev2c4291b2019-08-10 14:43:11 -0700492 if (ret) {
Dmitry Fomichev8de46592019-08-10 14:43:09 -0700493 dmz_dev_debug(zrc->dev, "Reclaim error %d\n", ret);
Dmitry Fomichev6b3a5292019-11-06 14:34:35 -0800494 if (!dmz_check_bdev(zrc->dev))
Dmitry Fomichev2c4291b2019-08-10 14:43:11 -0700495 return;
496 }
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900497
498 dmz_schedule_reclaim(zrc);
499}
500
501/*
502 * Initialize reclaim.
503 */
504int dmz_ctr_reclaim(struct dmz_dev *dev, struct dmz_metadata *zmd,
505 struct dmz_reclaim **reclaim)
506{
507 struct dmz_reclaim *zrc;
508 int ret;
509
510 zrc = kzalloc(sizeof(struct dmz_reclaim), GFP_KERNEL);
511 if (!zrc)
512 return -ENOMEM;
513
514 zrc->dev = dev;
515 zrc->metadata = zmd;
516 zrc->atime = jiffies;
517
518 /* Reclaim kcopyd client */
519 zrc->kc = dm_kcopyd_client_create(&zrc->kc_throttle);
520 if (IS_ERR(zrc->kc)) {
521 ret = PTR_ERR(zrc->kc);
522 zrc->kc = NULL;
523 goto err;
524 }
525
526 /* Reclaim work */
527 INIT_DELAYED_WORK(&zrc->work, dmz_reclaim_work);
528 zrc->wq = alloc_ordered_workqueue("dmz_rwq_%s", WQ_MEM_RECLAIM,
529 dev->name);
530 if (!zrc->wq) {
531 ret = -ENOMEM;
532 goto err;
533 }
534
535 *reclaim = zrc;
536 queue_delayed_work(zrc->wq, &zrc->work, 0);
537
538 return 0;
539err:
540 if (zrc->kc)
541 dm_kcopyd_client_destroy(zrc->kc);
542 kfree(zrc);
543
544 return ret;
545}
546
547/*
548 * Terminate reclaim.
549 */
550void dmz_dtr_reclaim(struct dmz_reclaim *zrc)
551{
552 cancel_delayed_work_sync(&zrc->work);
553 destroy_workqueue(zrc->wq);
554 dm_kcopyd_client_destroy(zrc->kc);
555 kfree(zrc);
556}
557
558/*
559 * Suspend reclaim.
560 */
561void dmz_suspend_reclaim(struct dmz_reclaim *zrc)
562{
563 cancel_delayed_work_sync(&zrc->work);
564}
565
566/*
567 * Resume reclaim.
568 */
569void dmz_resume_reclaim(struct dmz_reclaim *zrc)
570{
571 queue_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD);
572}
573
574/*
575 * BIO accounting.
576 */
577void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc)
578{
579 zrc->atime = jiffies;
580}
581
582/*
583 * Start reclaim if necessary.
584 */
585void dmz_schedule_reclaim(struct dmz_reclaim *zrc)
586{
587 if (dmz_should_reclaim(zrc))
588 mod_delayed_work(zrc->wq, &zrc->work, 0);
589}
590