Dmitry Fomichev | bae9a0a | 2019-08-02 15:02:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 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; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 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 | */ |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 40 | #define DMZ_IDLE_PERIOD (10UL * HZ) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Percentage of unmapped (free) random zones below which reclaim starts |
| 44 | * even if the target is busy. |
| 45 | */ |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 46 | #define DMZ_RECLAIM_LOW_UNMAP_ZONES 30 |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Percentage of unmapped (free) random zones above which reclaim will |
| 50 | * stop if the target is busy. |
| 51 | */ |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 52 | #define DMZ_RECLAIM_HIGH_UNMAP_ZONES 50 |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 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; |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 61 | struct dmz_dev *dev = dmz_zone_to_dev(zmd, zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 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; |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 77 | ret = blkdev_issue_zeroout(dev->bdev, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 78 | dmz_start_sect(zmd, zone) + dmz_blk2sect(wp_block), |
Damien Le Moal | 4218a95 | 2017-07-24 16:44:37 +0900 | [diff] [blame] | 79 | dmz_blk2sect(nr_blocks), GFP_NOIO, 0); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 80 | if (ret) { |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 81 | dmz_dev_err(dev, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 82 | "Align zone %u wp %llu to %llu (wp+%u) blocks failed %d", |
Hannes Reinecke | b712287 | 2020-05-11 10:24:18 +0200 | [diff] [blame] | 83 | zone->id, (unsigned long long)wp_block, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 84 | (unsigned long long)block, nr_blocks, ret); |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 85 | dmz_check_bdev(dev); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 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; |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 119 | struct dmz_dev *src_dev, *dst_dev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 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 |
Hannes Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 131 | end_block = dmz_zone_nr_blocks(zmd); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 132 | src_zone_block = dmz_start_block(zmd, src_zone); |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 133 | src_dev = dmz_zone_to_dev(zmd, src_zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 134 | dst_zone_block = dmz_start_block(zmd, dst_zone); |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 135 | dst_dev = dmz_zone_to_dev(zmd, dst_zone); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 136 | |
| 137 | if (dmz_is_seq(dst_zone)) |
| 138 | set_bit(DM_KCOPYD_WRITE_SEQ, &flags); |
| 139 | |
| 140 | while (block < end_block) { |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 141 | if (src_dev->flags & DMZ_BDEV_DYING) |
| 142 | return -EIO; |
| 143 | if (dst_dev->flags & DMZ_BDEV_DYING) |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 144 | return -EIO; |
| 145 | |
Hannes Reinecke | a16b7de | 2020-05-19 10:14:23 +0200 | [diff] [blame] | 146 | if (dmz_reclaim_should_terminate(src_zone)) |
| 147 | return -EINTR; |
| 148 | |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 149 | /* 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 Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 166 | src.bdev = src_dev->bdev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 167 | src.sector = dmz_blk2sect(src_zone_block + block); |
| 168 | src.count = dmz_blk2sect(nr_blocks); |
| 169 | |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 170 | dst.bdev = dst_dev->bdev; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 171 | 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 Snitzer | 7209049d | 2018-07-31 17:27:02 -0400 | [diff] [blame] | 176 | dm_kcopyd_copy(zrc->kc, &src, 1, &dst, flags, |
| 177 | dmz_reclaim_kcopy_end, zrc); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 178 | |
| 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 | */ |
| 197 | static 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 Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 204 | 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 208 | |
| 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 Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 220 | dmz_invalidate_blocks(zmd, bzone, 0, dmz_zone_nr_blocks(zmd)); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 221 | 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 Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 230 | return ret; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Merge valid blocks of dzone into its buffer zone and free dzone. |
| 235 | */ |
| 236 | static 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 Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 243 | 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 247 | |
| 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 Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 262 | dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd)); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 263 | 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 Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 274 | return ret; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 275 | } |
| 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 | */ |
| 281 | static 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 Reinecke | c5c7885 | 2020-05-19 10:14:22 +0200 | [diff] [blame] | 287 | int alloc_flags = DMZ_ALLOC_SEQ; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 288 | |
Hannes Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame] | 289 | /* Get a free random or sequential zone */ |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 290 | dmz_lock_map(zmd); |
Hannes Reinecke | c5c7885 | 2020-05-19 10:14:22 +0200 | [diff] [blame] | 291 | again: |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 292 | szone = dmz_alloc_zone(zmd, alloc_flags | DMZ_ALLOC_RECLAIM); |
Hannes Reinecke | c5c7885 | 2020-05-19 10:14:22 +0200 | [diff] [blame] | 293 | if (!szone && alloc_flags == DMZ_ALLOC_SEQ && dmz_nr_cache_zones(zmd)) { |
| 294 | alloc_flags = DMZ_ALLOC_RND; |
| 295 | goto again; |
| 296 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 297 | dmz_unlock_map(zmd); |
| 298 | if (!szone) |
| 299 | return -ENOSPC; |
| 300 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 301 | 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 306 | |
| 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 Reinecke | 3682056 | 2020-05-11 10:24:21 +0200 | [diff] [blame] | 323 | dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd)); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 324 | 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 Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 334 | return ret; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Reclaim an empty zone. |
| 339 | */ |
| 340 | static 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 Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame] | 354 | * Test if the target device is idle. |
| 355 | */ |
| 356 | static 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 362 | * Find a candidate zone for reclaim and process it. |
| 363 | */ |
Dmitry Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 364 | static int dmz_do_reclaim(struct dmz_reclaim *zrc) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 365 | { |
| 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 Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame] | 373 | dzone = dmz_get_zone_for_reclaim(zmd, dmz_target_idle(zrc)); |
Hannes Reinecke | c3ff479 | 2020-06-02 13:09:44 +0200 | [diff] [blame] | 374 | if (!dzone) { |
| 375 | DMDEBUG("(%s): No zone found to reclaim", |
| 376 | dmz_metadata_label(zmd)); |
Hannes Reinecke | 489dc0f | 2020-05-19 10:14:19 +0200 | [diff] [blame] | 377 | return -EBUSY; |
Hannes Reinecke | c3ff479 | 2020-06-02 13:09:44 +0200 | [diff] [blame] | 378 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 379 | |
| 380 | start = jiffies; |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 381 | if (dmz_is_cache(dzone) || dmz_is_rnd(dzone)) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 382 | 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 | } |
| 420 | out: |
| 421 | if (ret) { |
Hannes Reinecke | c3ff479 | 2020-06-02 13:09:44 +0200 | [diff] [blame] | 422 | 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 428 | dmz_unlock_zone_reclaim(dzone); |
Dmitry Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 429 | return ret; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 430 | } |
| 431 | |
Dmitry Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 432 | ret = dmz_flush_metadata(zrc->metadata); |
| 433 | if (ret) { |
Hannes Reinecke | 49de3b7 | 2020-05-14 08:09:29 +0200 | [diff] [blame] | 434 | DMDEBUG("(%s): Metadata flush for zone %u failed, err %d", |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 435 | dmz_metadata_label(zmd), rzone->id, ret); |
Dmitry Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 436 | return ret; |
| 437 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 438 | |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 439 | DMDEBUG("(%s): Reclaimed zone %u in %u ms", |
| 440 | dmz_metadata_label(zmd), |
| 441 | rzone->id, jiffies_to_msecs(jiffies - start)); |
Dmitry Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 442 | return 0; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 443 | } |
| 444 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 445 | static 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 462 | /* |
| 463 | * Test if reclaim is necessary. |
| 464 | */ |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 465 | static bool dmz_should_reclaim(struct dmz_reclaim *zrc, unsigned int p_unmap) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 466 | { |
Hannes Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame] | 467 | 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 472 | /* Reclaim when idle */ |
Hannes Reinecke | 90a9b86 | 2020-05-19 10:14:21 +0200 | [diff] [blame] | 473 | if (dmz_target_idle(zrc) && nr_reclaim) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 474 | return true; |
| 475 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 476 | /* If there are still plenty of cache zones, do not reclaim */ |
| 477 | if (p_unmap >= DMZ_RECLAIM_HIGH_UNMAP_ZONES) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 478 | return false; |
| 479 | |
| 480 | /* |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 481 | * If the percentage of unmapped cache zones is low, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 482 | * reclaim even if the target is busy. |
| 483 | */ |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 484 | return p_unmap <= DMZ_RECLAIM_LOW_UNMAP_ZONES; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Reclaim work function. |
| 489 | */ |
| 490 | static 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 Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 494 | unsigned int p_unmap; |
Dmitry Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 495 | int ret; |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 496 | |
Hannes Reinecke | d0e21ce | 2020-05-11 10:24:23 +0200 | [diff] [blame] | 497 | if (dmz_dev_is_dying(zmd)) |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 498 | return; |
| 499 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 500 | p_unmap = dmz_reclaim_percentage(zrc); |
| 501 | if (!dmz_should_reclaim(zrc, p_unmap)) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 502 | 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 Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 512 | if (dmz_target_idle(zrc) || p_unmap < DMZ_RECLAIM_LOW_UNMAP_ZONES / 2) { |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 513 | /* 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 Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 517 | zrc->kc_throttle.throttle = min(75U, 100U - p_unmap / 2); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 518 | } |
| 519 | |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 520 | DMDEBUG("(%s): Reclaim (%u): %s, %u%% free zones (%u/%u cache %u/%u random)", |
Hannes Reinecke | 2234e73 | 2020-05-11 10:24:22 +0200 | [diff] [blame] | 521 | dmz_metadata_label(zmd), |
| 522 | zrc->kc_throttle.throttle, |
| 523 | (dmz_target_idle(zrc) ? "Idle" : "Busy"), |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 524 | 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 528 | |
Dmitry Fomichev | b234c6d | 2019-08-10 14:43:09 -0700 | [diff] [blame] | 529 | ret = dmz_do_reclaim(zrc); |
Hannes Reinecke | a16b7de | 2020-05-19 10:14:23 +0200 | [diff] [blame] | 530 | if (ret && ret != -EINTR) { |
Hannes Reinecke | d0e21ce | 2020-05-11 10:24:23 +0200 | [diff] [blame] | 531 | if (!dmz_check_dev(zmd)) |
Dmitry Fomichev | 75d66ff | 2019-08-10 14:43:11 -0700 | [diff] [blame] | 532 | return; |
| 533 | } |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 534 | |
| 535 | dmz_schedule_reclaim(zrc); |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Initialize reclaim. |
| 540 | */ |
Hannes Reinecke | 6c805f7 | 2020-05-11 10:24:24 +0200 | [diff] [blame] | 541 | int dmz_ctr_reclaim(struct dmz_metadata *zmd, |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 542 | 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 Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 551 | 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 Reinecke | 2234e73 | 2020-05-11 10:24:22 +0200 | [diff] [blame] | 565 | dmz_metadata_label(zmd)); |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 566 | 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; |
| 575 | err: |
| 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 | */ |
| 586 | void 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 | */ |
| 597 | void dmz_suspend_reclaim(struct dmz_reclaim *zrc) |
| 598 | { |
| 599 | cancel_delayed_work_sync(&zrc->work); |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Resume reclaim. |
| 604 | */ |
| 605 | void 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 | */ |
| 613 | void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc) |
| 614 | { |
| 615 | zrc->atime = jiffies; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Start reclaim if necessary. |
| 620 | */ |
| 621 | void dmz_schedule_reclaim(struct dmz_reclaim *zrc) |
| 622 | { |
Hannes Reinecke | 34f5aff | 2020-05-19 10:14:20 +0200 | [diff] [blame] | 623 | unsigned int p_unmap = dmz_reclaim_percentage(zrc); |
| 624 | |
| 625 | if (dmz_should_reclaim(zrc, p_unmap)) |
Damien Le Moal | 3b1a94c | 2017-06-07 15:55:39 +0900 | [diff] [blame] | 626 | mod_delayed_work(zrc->wq, &zrc->work, 0); |
| 627 | } |