dm zoned: separate random and cache zones
[linux-2.6-microblaze.git] / drivers / md / dm-zoned-reclaim.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
14 struct dmz_reclaim {
15         struct dmz_metadata     *metadata;
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  */
33 enum {
34         DMZ_RECLAIM_KCOPY,
35 };
36
37 /*
38  * Number of seconds of target BIO inactivity to consider the target idle.
39  */
40 #define DMZ_IDLE_PERIOD                 (10UL * HZ)
41
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_ZONES     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_ZONES    50
53
54 /*
55  * Align a sequential zone write pointer to chunk_block.
56  */
57 static 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         struct dmz_dev *dev = dmz_zone_to_dev(zmd, zone);
62         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;
77         ret = blkdev_issue_zeroout(dev->bdev,
78                                    dmz_start_sect(zmd, zone) + dmz_blk2sect(wp_block),
79                                    dmz_blk2sect(nr_blocks), GFP_NOIO, 0);
80         if (ret) {
81                 dmz_dev_err(dev,
82                             "Align zone %u wp %llu to %llu (wp+%u) blocks failed %d",
83                             zone->id, (unsigned long long)wp_block,
84                             (unsigned long long)block, nr_blocks, ret);
85                 dmz_check_bdev(dev);
86                 return ret;
87         }
88
89         zone->wp_block = block;
90
91         return 0;
92 }
93
94 /*
95  * dm_kcopyd_copy end notification.
96  */
97 static 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  */
115 static 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;
119         struct dmz_dev *src_dev, *dst_dev;
120         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
131                 end_block = dmz_zone_nr_blocks(zmd);
132         src_zone_block = dmz_start_block(zmd, src_zone);
133         src_dev = dmz_zone_to_dev(zmd, src_zone);
134         dst_zone_block = dmz_start_block(zmd, dst_zone);
135         dst_dev = dmz_zone_to_dev(zmd, dst_zone);
136
137         if (dmz_is_seq(dst_zone))
138                 set_bit(DM_KCOPYD_WRITE_SEQ, &flags);
139
140         while (block < end_block) {
141                 if (src_dev->flags & DMZ_BDEV_DYING)
142                         return -EIO;
143                 if (dst_dev->flags & DMZ_BDEV_DYING)
144                         return -EIO;
145
146                 /* Get a valid region from the source zone */
147                 ret = dmz_first_valid_block(zmd, src_zone, &block);
148                 if (ret <= 0)
149                         return ret;
150                 nr_blocks = ret;
151
152                 /*
153                  * If we are writing in a sequential zone, we must make sure
154                  * that writes are sequential. So Zeroout any eventual hole
155                  * between writes.
156                  */
157                 if (dmz_is_seq(dst_zone)) {
158                         ret = dmz_reclaim_align_wp(zrc, dst_zone, block);
159                         if (ret)
160                                 return ret;
161                 }
162
163                 src.bdev = src_dev->bdev;
164                 src.sector = dmz_blk2sect(src_zone_block + block);
165                 src.count = dmz_blk2sect(nr_blocks);
166
167                 dst.bdev = dst_dev->bdev;
168                 dst.sector = dmz_blk2sect(dst_zone_block + block);
169                 dst.count = src.count;
170
171                 /* Copy the valid region */
172                 set_bit(DMZ_RECLAIM_KCOPY, &zrc->flags);
173                 dm_kcopyd_copy(zrc->kc, &src, 1, &dst, flags,
174                                dmz_reclaim_kcopy_end, zrc);
175
176                 /* Wait for copy to complete */
177                 wait_on_bit_io(&zrc->flags, DMZ_RECLAIM_KCOPY,
178                                TASK_UNINTERRUPTIBLE);
179                 if (zrc->kc_err)
180                         return zrc->kc_err;
181
182                 block += nr_blocks;
183                 if (dmz_is_seq(dst_zone))
184                         dst_zone->wp_block = block;
185         }
186
187         return 0;
188 }
189
190 /*
191  * Move valid blocks of dzone buffer zone into dzone (after its write pointer)
192  * and free the buffer zone.
193  */
194 static int dmz_reclaim_buf(struct dmz_reclaim *zrc, struct dm_zone *dzone)
195 {
196         struct dm_zone *bzone = dzone->bzone;
197         sector_t chunk_block = dzone->wp_block;
198         struct dmz_metadata *zmd = zrc->metadata;
199         int ret;
200
201         DMDEBUG("(%s): Chunk %u, move buf zone %u (weight %u) to data zone %u (weight %u)",
202                 dmz_metadata_label(zmd),
203                 dzone->chunk, bzone->id, dmz_weight(bzone),
204                 dzone->id, dmz_weight(dzone));
205
206         /* Flush data zone into the buffer zone */
207         ret = dmz_reclaim_copy(zrc, bzone, dzone);
208         if (ret < 0)
209                 return ret;
210
211         dmz_lock_flush(zmd);
212
213         /* Validate copied blocks */
214         ret = dmz_merge_valid_blocks(zmd, bzone, dzone, chunk_block);
215         if (ret == 0) {
216                 /* Free the buffer zone */
217                 dmz_invalidate_blocks(zmd, bzone, 0, dmz_zone_nr_blocks(zmd));
218                 dmz_lock_map(zmd);
219                 dmz_unmap_zone(zmd, bzone);
220                 dmz_unlock_zone_reclaim(dzone);
221                 dmz_free_zone(zmd, bzone);
222                 dmz_unlock_map(zmd);
223         }
224
225         dmz_unlock_flush(zmd);
226
227         return ret;
228 }
229
230 /*
231  * Merge valid blocks of dzone into its buffer zone and free dzone.
232  */
233 static int dmz_reclaim_seq_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
234 {
235         unsigned int chunk = dzone->chunk;
236         struct dm_zone *bzone = dzone->bzone;
237         struct dmz_metadata *zmd = zrc->metadata;
238         int ret = 0;
239
240         DMDEBUG("(%s): Chunk %u, move data zone %u (weight %u) to buf zone %u (weight %u)",
241                 dmz_metadata_label(zmd),
242                 chunk, dzone->id, dmz_weight(dzone),
243                 bzone->id, dmz_weight(bzone));
244
245         /* Flush data zone into the buffer zone */
246         ret = dmz_reclaim_copy(zrc, dzone, bzone);
247         if (ret < 0)
248                 return ret;
249
250         dmz_lock_flush(zmd);
251
252         /* Validate copied blocks */
253         ret = dmz_merge_valid_blocks(zmd, dzone, bzone, 0);
254         if (ret == 0) {
255                 /*
256                  * Free the data zone and remap the chunk to
257                  * the buffer zone.
258                  */
259                 dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd));
260                 dmz_lock_map(zmd);
261                 dmz_unmap_zone(zmd, bzone);
262                 dmz_unmap_zone(zmd, dzone);
263                 dmz_unlock_zone_reclaim(dzone);
264                 dmz_free_zone(zmd, dzone);
265                 dmz_map_zone(zmd, bzone, chunk);
266                 dmz_unlock_map(zmd);
267         }
268
269         dmz_unlock_flush(zmd);
270
271         return ret;
272 }
273
274 /*
275  * Move valid blocks of the random data zone dzone into a free sequential zone.
276  * Once blocks are moved, remap the zone chunk to the sequential zone.
277  */
278 static int dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
279 {
280         unsigned int chunk = dzone->chunk;
281         struct dm_zone *szone = NULL;
282         struct dmz_metadata *zmd = zrc->metadata;
283         int ret;
284         int alloc_flags = dmz_nr_cache_zones(zmd) ?
285                 DMZ_ALLOC_RND : DMZ_ALLOC_SEQ;
286
287         /* Get a free sequential zone */
288         dmz_lock_map(zmd);
289         szone = dmz_alloc_zone(zmd, alloc_flags | DMZ_ALLOC_RECLAIM);
290         dmz_unlock_map(zmd);
291         if (!szone)
292                 return -ENOSPC;
293
294         DMDEBUG("(%s): Chunk %u, move %s zone %u (weight %u) to %s zone %u",
295                 dmz_metadata_label(zmd), chunk,
296                 dmz_is_cache(dzone) ? "cache" : "rnd",
297                 dzone->id, dmz_weight(dzone),
298                 dmz_is_rnd(szone) ? "rnd" : "seq", szone->id);
299
300         /* Flush the random data zone into the sequential zone */
301         ret = dmz_reclaim_copy(zrc, dzone, szone);
302
303         dmz_lock_flush(zmd);
304
305         if (ret == 0) {
306                 /* Validate copied blocks */
307                 ret = dmz_copy_valid_blocks(zmd, dzone, szone);
308         }
309         if (ret) {
310                 /* Free the sequential zone */
311                 dmz_lock_map(zmd);
312                 dmz_free_zone(zmd, szone);
313                 dmz_unlock_map(zmd);
314         } else {
315                 /* Free the data zone and remap the chunk */
316                 dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd));
317                 dmz_lock_map(zmd);
318                 dmz_unmap_zone(zmd, dzone);
319                 dmz_unlock_zone_reclaim(dzone);
320                 dmz_free_zone(zmd, dzone);
321                 dmz_map_zone(zmd, szone, chunk);
322                 dmz_unlock_map(zmd);
323         }
324
325         dmz_unlock_flush(zmd);
326
327         return ret;
328 }
329
330 /*
331  * Reclaim an empty zone.
332  */
333 static void dmz_reclaim_empty(struct dmz_reclaim *zrc, struct dm_zone *dzone)
334 {
335         struct dmz_metadata *zmd = zrc->metadata;
336
337         dmz_lock_flush(zmd);
338         dmz_lock_map(zmd);
339         dmz_unmap_zone(zmd, dzone);
340         dmz_unlock_zone_reclaim(dzone);
341         dmz_free_zone(zmd, dzone);
342         dmz_unlock_map(zmd);
343         dmz_unlock_flush(zmd);
344 }
345
346 /*
347  * Find a candidate zone for reclaim and process it.
348  */
349 static int dmz_do_reclaim(struct dmz_reclaim *zrc)
350 {
351         struct dmz_metadata *zmd = zrc->metadata;
352         struct dm_zone *dzone;
353         struct dm_zone *rzone;
354         unsigned long start;
355         int ret;
356
357         /* Get a data zone */
358         dzone = dmz_get_zone_for_reclaim(zmd);
359         if (!dzone)
360                 return -EBUSY;
361
362         start = jiffies;
363         if (dmz_is_cache(dzone) || dmz_is_rnd(dzone)) {
364                 if (!dmz_weight(dzone)) {
365                         /* Empty zone */
366                         dmz_reclaim_empty(zrc, dzone);
367                         ret = 0;
368                 } else {
369                         /*
370                          * Reclaim the random data zone by moving its
371                          * valid data blocks to a free sequential zone.
372                          */
373                         ret = dmz_reclaim_rnd_data(zrc, dzone);
374                 }
375                 rzone = dzone;
376
377         } else {
378                 struct dm_zone *bzone = dzone->bzone;
379                 sector_t chunk_block = 0;
380
381                 ret = dmz_first_valid_block(zmd, bzone, &chunk_block);
382                 if (ret < 0)
383                         goto out;
384
385                 if (ret == 0 || chunk_block >= dzone->wp_block) {
386                         /*
387                          * The buffer zone is empty or its valid blocks are
388                          * after the data zone write pointer.
389                          */
390                         ret = dmz_reclaim_buf(zrc, dzone);
391                         rzone = bzone;
392                 } else {
393                         /*
394                          * Reclaim the data zone by merging it into the
395                          * buffer zone so that the buffer zone itself can
396                          * be later reclaimed.
397                          */
398                         ret = dmz_reclaim_seq_data(zrc, dzone);
399                         rzone = dzone;
400                 }
401         }
402 out:
403         if (ret) {
404                 dmz_unlock_zone_reclaim(dzone);
405                 return ret;
406         }
407
408         ret = dmz_flush_metadata(zrc->metadata);
409         if (ret) {
410                 DMDEBUG("(%s): Metadata flush for zone %u failed, err %d",
411                         dmz_metadata_label(zmd), rzone->id, ret);
412                 return ret;
413         }
414
415         DMDEBUG("(%s): Reclaimed zone %u in %u ms",
416                 dmz_metadata_label(zmd),
417                 rzone->id, jiffies_to_msecs(jiffies - start));
418         return 0;
419 }
420
421 /*
422  * Test if the target device is idle.
423  */
424 static inline int dmz_target_idle(struct dmz_reclaim *zrc)
425 {
426         return time_is_before_jiffies(zrc->atime + DMZ_IDLE_PERIOD);
427 }
428
429 static unsigned int dmz_reclaim_percentage(struct dmz_reclaim *zrc)
430 {
431         struct dmz_metadata *zmd = zrc->metadata;
432         unsigned int nr_cache = dmz_nr_cache_zones(zmd);
433         unsigned int nr_rnd = dmz_nr_rnd_zones(zmd);
434         unsigned int nr_unmap, nr_zones;
435
436         if (nr_cache) {
437                 nr_zones = nr_cache;
438                 nr_unmap = dmz_nr_unmap_cache_zones(zmd);
439         } else {
440                 nr_zones = nr_rnd;
441                 nr_unmap = dmz_nr_unmap_rnd_zones(zmd);
442         }
443         return nr_unmap * 100 / nr_zones;
444 }
445
446 /*
447  * Test if reclaim is necessary.
448  */
449 static bool dmz_should_reclaim(struct dmz_reclaim *zrc, unsigned int p_unmap)
450 {
451         /* Reclaim when idle */
452         if (dmz_target_idle(zrc) && p_unmap < 100)
453                 return true;
454
455         /* If there are still plenty of cache zones, do not reclaim */
456         if (p_unmap >= DMZ_RECLAIM_HIGH_UNMAP_ZONES)
457                 return false;
458
459         /*
460          * If the percentage of unmapped cache zones is low,
461          * reclaim even if the target is busy.
462          */
463         return p_unmap <= DMZ_RECLAIM_LOW_UNMAP_ZONES;
464 }
465
466 /*
467  * Reclaim work function.
468  */
469 static void dmz_reclaim_work(struct work_struct *work)
470 {
471         struct dmz_reclaim *zrc = container_of(work, struct dmz_reclaim, work.work);
472         struct dmz_metadata *zmd = zrc->metadata;
473         unsigned int p_unmap;
474         int ret;
475
476         if (dmz_dev_is_dying(zmd))
477                 return;
478
479         p_unmap = dmz_reclaim_percentage(zrc);
480         if (!dmz_should_reclaim(zrc, p_unmap)) {
481                 mod_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD);
482                 return;
483         }
484
485         /*
486          * We need to start reclaiming random zones: set up zone copy
487          * throttling to either go fast if we are very low on random zones
488          * and slower if there are still some free random zones to avoid
489          * as much as possible to negatively impact the user workload.
490          */
491         if (dmz_target_idle(zrc) || p_unmap < DMZ_RECLAIM_LOW_UNMAP_ZONES / 2) {
492                 /* Idle or very low percentage: go fast */
493                 zrc->kc_throttle.throttle = 100;
494         } else {
495                 /* Busy but we still have some random zone: throttle */
496                 zrc->kc_throttle.throttle = min(75U, 100U - p_unmap / 2);
497         }
498
499         DMDEBUG("(%s): Reclaim (%u): %s, %u%% free zones (%u/%u cache %u/%u random)",
500                 dmz_metadata_label(zmd),
501                 zrc->kc_throttle.throttle,
502                 (dmz_target_idle(zrc) ? "Idle" : "Busy"),
503                 p_unmap, dmz_nr_unmap_cache_zones(zmd),
504                 dmz_nr_cache_zones(zmd),
505                 dmz_nr_unmap_rnd_zones(zmd),
506                 dmz_nr_rnd_zones(zmd));
507
508         ret = dmz_do_reclaim(zrc);
509         if (ret) {
510                 DMDEBUG("(%s): Reclaim error %d",
511                         dmz_metadata_label(zmd), ret);
512                 if (!dmz_check_dev(zmd))
513                         return;
514         }
515
516         dmz_schedule_reclaim(zrc);
517 }
518
519 /*
520  * Initialize reclaim.
521  */
522 int dmz_ctr_reclaim(struct dmz_metadata *zmd,
523                     struct dmz_reclaim **reclaim)
524 {
525         struct dmz_reclaim *zrc;
526         int ret;
527
528         zrc = kzalloc(sizeof(struct dmz_reclaim), GFP_KERNEL);
529         if (!zrc)
530                 return -ENOMEM;
531
532         zrc->metadata = zmd;
533         zrc->atime = jiffies;
534
535         /* Reclaim kcopyd client */
536         zrc->kc = dm_kcopyd_client_create(&zrc->kc_throttle);
537         if (IS_ERR(zrc->kc)) {
538                 ret = PTR_ERR(zrc->kc);
539                 zrc->kc = NULL;
540                 goto err;
541         }
542
543         /* Reclaim work */
544         INIT_DELAYED_WORK(&zrc->work, dmz_reclaim_work);
545         zrc->wq = alloc_ordered_workqueue("dmz_rwq_%s", WQ_MEM_RECLAIM,
546                                           dmz_metadata_label(zmd));
547         if (!zrc->wq) {
548                 ret = -ENOMEM;
549                 goto err;
550         }
551
552         *reclaim = zrc;
553         queue_delayed_work(zrc->wq, &zrc->work, 0);
554
555         return 0;
556 err:
557         if (zrc->kc)
558                 dm_kcopyd_client_destroy(zrc->kc);
559         kfree(zrc);
560
561         return ret;
562 }
563
564 /*
565  * Terminate reclaim.
566  */
567 void dmz_dtr_reclaim(struct dmz_reclaim *zrc)
568 {
569         cancel_delayed_work_sync(&zrc->work);
570         destroy_workqueue(zrc->wq);
571         dm_kcopyd_client_destroy(zrc->kc);
572         kfree(zrc);
573 }
574
575 /*
576  * Suspend reclaim.
577  */
578 void dmz_suspend_reclaim(struct dmz_reclaim *zrc)
579 {
580         cancel_delayed_work_sync(&zrc->work);
581 }
582
583 /*
584  * Resume reclaim.
585  */
586 void dmz_resume_reclaim(struct dmz_reclaim *zrc)
587 {
588         queue_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD);
589 }
590
591 /*
592  * BIO accounting.
593  */
594 void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc)
595 {
596         zrc->atime = jiffies;
597 }
598
599 /*
600  * Start reclaim if necessary.
601  */
602 void dmz_schedule_reclaim(struct dmz_reclaim *zrc)
603 {
604         unsigned int p_unmap = dmz_reclaim_percentage(zrc);
605
606         if (dmz_should_reclaim(zrc, p_unmap))
607                 mod_delayed_work(zrc->wq, &zrc->work, 0);
608 }
609