dm-zoned-reclaim.c 14.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (C) 2017 Western Digital Corporation or its affiliates.
 *
 * This file is released under the GPL.
 */

#include "dm-zoned.h"

#include <linux/module.h>

#define	DM_MSG_PREFIX		"zoned reclaim"

struct dmz_reclaim {
	struct dmz_metadata     *metadata;

	struct delayed_work	work;
	struct workqueue_struct *wq;

	struct dm_kcopyd_client	*kc;
	struct dm_kcopyd_throttle kc_throttle;
	int			kc_err;

H
Hannes Reinecke 已提交
24 25
	int			dev_idx;

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	unsigned long		flags;

	/* Last target access time */
	unsigned long		atime;
};

/*
 * Reclaim state flags.
 */
enum {
	DMZ_RECLAIM_KCOPY,
};

/*
 * Number of seconds of target BIO inactivity to consider the target idle.
 */
42
#define DMZ_IDLE_PERIOD			(10UL * HZ)
43 44 45 46 47

/*
 * Percentage of unmapped (free) random zones below which reclaim starts
 * even if the target is busy.
 */
48
#define DMZ_RECLAIM_LOW_UNMAP_ZONES	30
49 50 51 52 53

/*
 * Percentage of unmapped (free) random zones above which reclaim will
 * stop if the target is busy.
 */
54
#define DMZ_RECLAIM_HIGH_UNMAP_ZONES	50
55 56 57 58 59 60 61 62

/*
 * Align a sequential zone write pointer to chunk_block.
 */
static int dmz_reclaim_align_wp(struct dmz_reclaim *zrc, struct dm_zone *zone,
				sector_t block)
{
	struct dmz_metadata *zmd = zrc->metadata;
63
	struct dmz_dev *dev = zone->dev;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	sector_t wp_block = zone->wp_block;
	unsigned int nr_blocks;
	int ret;

	if (wp_block == block)
		return 0;

	if (wp_block > block)
		return -EIO;

	/*
	 * Zeroout the space between the write
	 * pointer and the requested position.
	 */
	nr_blocks = block - wp_block;
79
	ret = blkdev_issue_zeroout(dev->bdev,
80
				   dmz_start_sect(zmd, zone) + dmz_blk2sect(wp_block),
81
				   dmz_blk2sect(nr_blocks), GFP_NOIO, 0);
82
	if (ret) {
83
		dmz_dev_err(dev,
84
			    "Align zone %u wp %llu to %llu (wp+%u) blocks failed %d",
85
			    zone->id, (unsigned long long)wp_block,
86
			    (unsigned long long)block, nr_blocks, ret);
87
		dmz_check_bdev(dev);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		return ret;
	}

	zone->wp_block = block;

	return 0;
}

/*
 * dm_kcopyd_copy end notification.
 */
static void dmz_reclaim_kcopy_end(int read_err, unsigned long write_err,
				  void *context)
{
	struct dmz_reclaim *zrc = context;

	if (read_err || write_err)
		zrc->kc_err = -EIO;
	else
		zrc->kc_err = 0;

	clear_bit_unlock(DMZ_RECLAIM_KCOPY, &zrc->flags);
	smp_mb__after_atomic();
	wake_up_bit(&zrc->flags, DMZ_RECLAIM_KCOPY);
}

/*
 * Copy valid blocks of src_zone into dst_zone.
 */
static int dmz_reclaim_copy(struct dmz_reclaim *zrc,
			    struct dm_zone *src_zone, struct dm_zone *dst_zone)
{
	struct dmz_metadata *zmd = zrc->metadata;
	struct dm_io_region src, dst;
	sector_t block = 0, end_block;
	sector_t nr_blocks;
	sector_t src_zone_block;
	sector_t dst_zone_block;
	unsigned long flags = 0;
	int ret;

	if (dmz_is_seq(src_zone))
		end_block = src_zone->wp_block;
	else
132
		end_block = dmz_zone_nr_blocks(zmd);
133 134 135 136 137 138 139
	src_zone_block = dmz_start_block(zmd, src_zone);
	dst_zone_block = dmz_start_block(zmd, dst_zone);

	if (dmz_is_seq(dst_zone))
		set_bit(DM_KCOPYD_WRITE_SEQ, &flags);

	while (block < end_block) {
140
		if (src_zone->dev->flags & DMZ_BDEV_DYING)
141
			return -EIO;
142
		if (dst_zone->dev->flags & DMZ_BDEV_DYING)
143 144
			return -EIO;

145 146 147
		if (dmz_reclaim_should_terminate(src_zone))
			return -EINTR;

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		/* Get a valid region from the source zone */
		ret = dmz_first_valid_block(zmd, src_zone, &block);
		if (ret <= 0)
			return ret;
		nr_blocks = ret;

		/*
		 * If we are writing in a sequential zone, we must make sure
		 * that writes are sequential. So Zeroout any eventual hole
		 * between writes.
		 */
		if (dmz_is_seq(dst_zone)) {
			ret = dmz_reclaim_align_wp(zrc, dst_zone, block);
			if (ret)
				return ret;
		}

165
		src.bdev = src_zone->dev->bdev;
166 167 168
		src.sector = dmz_blk2sect(src_zone_block + block);
		src.count = dmz_blk2sect(nr_blocks);

169
		dst.bdev = dst_zone->dev->bdev;
170 171 172 173 174
		dst.sector = dmz_blk2sect(dst_zone_block + block);
		dst.count = src.count;

		/* Copy the valid region */
		set_bit(DMZ_RECLAIM_KCOPY, &zrc->flags);
175 176
		dm_kcopyd_copy(zrc->kc, &src, 1, &dst, flags,
			       dmz_reclaim_kcopy_end, zrc);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

		/* Wait for copy to complete */
		wait_on_bit_io(&zrc->flags, DMZ_RECLAIM_KCOPY,
			       TASK_UNINTERRUPTIBLE);
		if (zrc->kc_err)
			return zrc->kc_err;

		block += nr_blocks;
		if (dmz_is_seq(dst_zone))
			dst_zone->wp_block = block;
	}

	return 0;
}

/*
 * Move valid blocks of dzone buffer zone into dzone (after its write pointer)
 * and free the buffer zone.
 */
static int dmz_reclaim_buf(struct dmz_reclaim *zrc, struct dm_zone *dzone)
{
	struct dm_zone *bzone = dzone->bzone;
	sector_t chunk_block = dzone->wp_block;
	struct dmz_metadata *zmd = zrc->metadata;
	int ret;

H
Hannes Reinecke 已提交
203 204
	DMDEBUG("(%s/%u): Chunk %u, move buf zone %u (weight %u) to data zone %u (weight %u)",
		dmz_metadata_label(zmd), zrc->dev_idx,
205 206
		dzone->chunk, bzone->id, dmz_weight(bzone),
		dzone->id, dmz_weight(dzone));
207 208 209 210 211 212 213 214 215 216 217 218

	/* Flush data zone into the buffer zone */
	ret = dmz_reclaim_copy(zrc, bzone, dzone);
	if (ret < 0)
		return ret;

	dmz_lock_flush(zmd);

	/* Validate copied blocks */
	ret = dmz_merge_valid_blocks(zmd, bzone, dzone, chunk_block);
	if (ret == 0) {
		/* Free the buffer zone */
219
		dmz_invalidate_blocks(zmd, bzone, 0, dmz_zone_nr_blocks(zmd));
220 221 222 223 224 225 226 227 228
		dmz_lock_map(zmd);
		dmz_unmap_zone(zmd, bzone);
		dmz_unlock_zone_reclaim(dzone);
		dmz_free_zone(zmd, bzone);
		dmz_unlock_map(zmd);
	}

	dmz_unlock_flush(zmd);

229
	return ret;
230 231 232 233 234 235 236 237 238 239 240 241
}

/*
 * Merge valid blocks of dzone into its buffer zone and free dzone.
 */
static int dmz_reclaim_seq_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
{
	unsigned int chunk = dzone->chunk;
	struct dm_zone *bzone = dzone->bzone;
	struct dmz_metadata *zmd = zrc->metadata;
	int ret = 0;

H
Hannes Reinecke 已提交
242 243
	DMDEBUG("(%s/%u): Chunk %u, move data zone %u (weight %u) to buf zone %u (weight %u)",
		dmz_metadata_label(zmd), zrc->dev_idx,
244 245
		chunk, dzone->id, dmz_weight(dzone),
		bzone->id, dmz_weight(bzone));
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

	/* Flush data zone into the buffer zone */
	ret = dmz_reclaim_copy(zrc, dzone, bzone);
	if (ret < 0)
		return ret;

	dmz_lock_flush(zmd);

	/* Validate copied blocks */
	ret = dmz_merge_valid_blocks(zmd, dzone, bzone, 0);
	if (ret == 0) {
		/*
		 * Free the data zone and remap the chunk to
		 * the buffer zone.
		 */
261
		dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd));
262 263 264 265 266 267 268 269 270 271 272
		dmz_lock_map(zmd);
		dmz_unmap_zone(zmd, bzone);
		dmz_unmap_zone(zmd, dzone);
		dmz_unlock_zone_reclaim(dzone);
		dmz_free_zone(zmd, dzone);
		dmz_map_zone(zmd, bzone, chunk);
		dmz_unlock_map(zmd);
	}

	dmz_unlock_flush(zmd);

273
	return ret;
274 275 276 277 278 279 280 281 282 283 284 285
}

/*
 * Move valid blocks of the random data zone dzone into a free sequential zone.
 * Once blocks are moved, remap the zone chunk to the sequential zone.
 */
static int dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
{
	unsigned int chunk = dzone->chunk;
	struct dm_zone *szone = NULL;
	struct dmz_metadata *zmd = zrc->metadata;
	int ret;
286
	int alloc_flags = DMZ_ALLOC_SEQ;
287

288
	/* Get a free random or sequential zone */
289
	dmz_lock_map(zmd);
290
again:
291 292
	szone = dmz_alloc_zone(zmd, zrc->dev_idx,
			       alloc_flags | DMZ_ALLOC_RECLAIM);
293 294 295 296
	if (!szone && alloc_flags == DMZ_ALLOC_SEQ && dmz_nr_cache_zones(zmd)) {
		alloc_flags = DMZ_ALLOC_RND;
		goto again;
	}
297 298 299 300
	dmz_unlock_map(zmd);
	if (!szone)
		return -ENOSPC;

H
Hannes Reinecke 已提交
301 302
	DMDEBUG("(%s/%u): Chunk %u, move %s zone %u (weight %u) to %s zone %u",
		dmz_metadata_label(zmd), zrc->dev_idx, chunk,
303 304 305
		dmz_is_cache(dzone) ? "cache" : "rnd",
		dzone->id, dmz_weight(dzone),
		dmz_is_rnd(szone) ? "rnd" : "seq", szone->id);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

	/* Flush the random data zone into the sequential zone */
	ret = dmz_reclaim_copy(zrc, dzone, szone);

	dmz_lock_flush(zmd);

	if (ret == 0) {
		/* Validate copied blocks */
		ret = dmz_copy_valid_blocks(zmd, dzone, szone);
	}
	if (ret) {
		/* Free the sequential zone */
		dmz_lock_map(zmd);
		dmz_free_zone(zmd, szone);
		dmz_unlock_map(zmd);
	} else {
		/* Free the data zone and remap the chunk */
323
		dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd));
324 325 326 327 328 329 330 331 332 333
		dmz_lock_map(zmd);
		dmz_unmap_zone(zmd, dzone);
		dmz_unlock_zone_reclaim(dzone);
		dmz_free_zone(zmd, dzone);
		dmz_map_zone(zmd, szone, chunk);
		dmz_unlock_map(zmd);
	}

	dmz_unlock_flush(zmd);

334
	return ret;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
}

/*
 * Reclaim an empty zone.
 */
static void dmz_reclaim_empty(struct dmz_reclaim *zrc, struct dm_zone *dzone)
{
	struct dmz_metadata *zmd = zrc->metadata;

	dmz_lock_flush(zmd);
	dmz_lock_map(zmd);
	dmz_unmap_zone(zmd, dzone);
	dmz_unlock_zone_reclaim(dzone);
	dmz_free_zone(zmd, dzone);
	dmz_unlock_map(zmd);
	dmz_unlock_flush(zmd);
}

353 354 355 356 357 358 359 360
/*
 * Test if the target device is idle.
 */
static inline int dmz_target_idle(struct dmz_reclaim *zrc)
{
	return time_is_before_jiffies(zrc->atime + DMZ_IDLE_PERIOD);
}

361 362 363
/*
 * Find a candidate zone for reclaim and process it.
 */
364
static int dmz_do_reclaim(struct dmz_reclaim *zrc)
365 366 367 368 369 370 371 372
{
	struct dmz_metadata *zmd = zrc->metadata;
	struct dm_zone *dzone;
	struct dm_zone *rzone;
	unsigned long start;
	int ret;

	/* Get a data zone */
373 374
	dzone = dmz_get_zone_for_reclaim(zmd, zrc->dev_idx,
					 dmz_target_idle(zrc));
375
	if (!dzone) {
H
Hannes Reinecke 已提交
376 377
		DMDEBUG("(%s/%u): No zone found to reclaim",
			dmz_metadata_label(zmd), zrc->dev_idx);
378
		return -EBUSY;
379
	}
380
	rzone = dzone;
381 382

	start = jiffies;
383
	if (dmz_is_cache(dzone) || dmz_is_rnd(dzone)) {
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
		if (!dmz_weight(dzone)) {
			/* Empty zone */
			dmz_reclaim_empty(zrc, dzone);
			ret = 0;
		} else {
			/*
			 * Reclaim the random data zone by moving its
			 * valid data blocks to a free sequential zone.
			 */
			ret = dmz_reclaim_rnd_data(zrc, dzone);
		}
	} else {
		struct dm_zone *bzone = dzone->bzone;
		sector_t chunk_block = 0;

		ret = dmz_first_valid_block(zmd, bzone, &chunk_block);
		if (ret < 0)
			goto out;

		if (ret == 0 || chunk_block >= dzone->wp_block) {
			/*
			 * The buffer zone is empty or its valid blocks are
			 * after the data zone write pointer.
			 */
			ret = dmz_reclaim_buf(zrc, dzone);
			rzone = bzone;
		} else {
			/*
			 * Reclaim the data zone by merging it into the
			 * buffer zone so that the buffer zone itself can
			 * be later reclaimed.
			 */
			ret = dmz_reclaim_seq_data(zrc, dzone);
		}
	}
out:
	if (ret) {
421
		if (ret == -EINTR)
H
Hannes Reinecke 已提交
422 423 424
			DMDEBUG("(%s/%u): reclaim zone %u interrupted",
				dmz_metadata_label(zmd), zrc->dev_idx,
				rzone->id);
425
		else
H
Hannes Reinecke 已提交
426 427 428
			DMDEBUG("(%s/%u): Failed to reclaim zone %u, err %d",
				dmz_metadata_label(zmd), zrc->dev_idx,
				rzone->id, ret);
429
		dmz_unlock_zone_reclaim(dzone);
430
		return ret;
431 432
	}

433 434
	ret = dmz_flush_metadata(zrc->metadata);
	if (ret) {
H
Hannes Reinecke 已提交
435 436
		DMDEBUG("(%s/%u): Metadata flush for zone %u failed, err %d",
			dmz_metadata_label(zmd), zrc->dev_idx, rzone->id, ret);
437 438
		return ret;
	}
439

H
Hannes Reinecke 已提交
440 441
	DMDEBUG("(%s/%u): Reclaimed zone %u in %u ms",
		dmz_metadata_label(zmd), zrc->dev_idx,
442
		rzone->id, jiffies_to_msecs(jiffies - start));
443
	return 0;
444 445
}

446
static unsigned int dmz_reclaim_percentage(struct dmz_reclaim *zrc)
447 448
{
	struct dmz_metadata *zmd = zrc->metadata;
449 450
	unsigned int nr_cache = dmz_nr_cache_zones(zmd);
	unsigned int nr_unmap, nr_zones;
451

452 453 454 455
	if (nr_cache) {
		nr_zones = nr_cache;
		nr_unmap = dmz_nr_unmap_cache_zones(zmd);
	} else {
456 457
		nr_zones = dmz_nr_rnd_zones(zmd, zrc->dev_idx);
		nr_unmap = dmz_nr_unmap_rnd_zones(zmd, zrc->dev_idx);
458
	}
459 460
	if (nr_unmap <= 1)
		return 0;
461 462 463 464 465 466 467 468
	return nr_unmap * 100 / nr_zones;
}

/*
 * Test if reclaim is necessary.
 */
static bool dmz_should_reclaim(struct dmz_reclaim *zrc, unsigned int p_unmap)
{
H
Hannes Reinecke 已提交
469 470
	unsigned int nr_reclaim;

471
	nr_reclaim = dmz_nr_rnd_zones(zrc->metadata, zrc->dev_idx);
472

H
Hannes Reinecke 已提交
473 474 475 476 477 478 479 480
	if (dmz_nr_cache_zones(zrc->metadata)) {
		/*
		 * The first device in a multi-device
		 * setup only contains cache zones, so
		 * never start reclaim there.
		 */
		if (zrc->dev_idx == 0)
			return false;
481
		nr_reclaim += dmz_nr_cache_zones(zrc->metadata);
H
Hannes Reinecke 已提交
482
	}
483

484
	/* Reclaim when idle */
485
	if (dmz_target_idle(zrc) && nr_reclaim)
486 487
		return true;

488 489
	/* If there are still plenty of cache zones, do not reclaim */
	if (p_unmap >= DMZ_RECLAIM_HIGH_UNMAP_ZONES)
490 491 492
		return false;

	/*
493
	 * If the percentage of unmapped cache zones is low,
494 495
	 * reclaim even if the target is busy.
	 */
496
	return p_unmap <= DMZ_RECLAIM_LOW_UNMAP_ZONES;
497 498 499 500 501 502 503 504 505
}

/*
 * Reclaim work function.
 */
static void dmz_reclaim_work(struct work_struct *work)
{
	struct dmz_reclaim *zrc = container_of(work, struct dmz_reclaim, work.work);
	struct dmz_metadata *zmd = zrc->metadata;
506
	unsigned int p_unmap;
507
	int ret;
508

509
	if (dmz_dev_is_dying(zmd))
510 511
		return;

512 513
	p_unmap = dmz_reclaim_percentage(zrc);
	if (!dmz_should_reclaim(zrc, p_unmap)) {
514 515 516 517 518 519 520 521 522 523
		mod_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD);
		return;
	}

	/*
	 * We need to start reclaiming random zones: set up zone copy
	 * throttling to either go fast if we are very low on random zones
	 * and slower if there are still some free random zones to avoid
	 * as much as possible to negatively impact the user workload.
	 */
524
	if (dmz_target_idle(zrc) || p_unmap < DMZ_RECLAIM_LOW_UNMAP_ZONES / 2) {
525 526 527 528
		/* Idle or very low percentage: go fast */
		zrc->kc_throttle.throttle = 100;
	} else {
		/* Busy but we still have some random zone: throttle */
529
		zrc->kc_throttle.throttle = min(75U, 100U - p_unmap / 2);
530 531
	}

H
Hannes Reinecke 已提交
532 533
	DMDEBUG("(%s/%u): Reclaim (%u): %s, %u%% free zones (%u/%u cache %u/%u random)",
		dmz_metadata_label(zmd), zrc->dev_idx,
534 535
		zrc->kc_throttle.throttle,
		(dmz_target_idle(zrc) ? "Idle" : "Busy"),
536 537
		p_unmap, dmz_nr_unmap_cache_zones(zmd),
		dmz_nr_cache_zones(zmd),
538 539
		dmz_nr_unmap_rnd_zones(zmd, zrc->dev_idx),
		dmz_nr_rnd_zones(zmd, zrc->dev_idx));
540

541
	ret = dmz_do_reclaim(zrc);
542
	if (ret && ret != -EINTR) {
543
		if (!dmz_check_dev(zmd))
544 545
			return;
	}
546 547 548 549 550 551 552

	dmz_schedule_reclaim(zrc);
}

/*
 * Initialize reclaim.
 */
553
int dmz_ctr_reclaim(struct dmz_metadata *zmd,
H
Hannes Reinecke 已提交
554
		    struct dmz_reclaim **reclaim, int idx)
555 556 557 558 559 560 561 562 563 564
{
	struct dmz_reclaim *zrc;
	int ret;

	zrc = kzalloc(sizeof(struct dmz_reclaim), GFP_KERNEL);
	if (!zrc)
		return -ENOMEM;

	zrc->metadata = zmd;
	zrc->atime = jiffies;
H
Hannes Reinecke 已提交
565
	zrc->dev_idx = idx;
566 567 568 569 570 571 572 573 574 575 576

	/* Reclaim kcopyd client */
	zrc->kc = dm_kcopyd_client_create(&zrc->kc_throttle);
	if (IS_ERR(zrc->kc)) {
		ret = PTR_ERR(zrc->kc);
		zrc->kc = NULL;
		goto err;
	}

	/* Reclaim work */
	INIT_DELAYED_WORK(&zrc->work, dmz_reclaim_work);
H
Hannes Reinecke 已提交
577 578
	zrc->wq = alloc_ordered_workqueue("dmz_rwq_%s_%d", WQ_MEM_RECLAIM,
					  dmz_metadata_label(zmd), idx);
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	if (!zrc->wq) {
		ret = -ENOMEM;
		goto err;
	}

	*reclaim = zrc;
	queue_delayed_work(zrc->wq, &zrc->work, 0);

	return 0;
err:
	if (zrc->kc)
		dm_kcopyd_client_destroy(zrc->kc);
	kfree(zrc);

	return ret;
}

/*
 * Terminate reclaim.
 */
void dmz_dtr_reclaim(struct dmz_reclaim *zrc)
{
	cancel_delayed_work_sync(&zrc->work);
	destroy_workqueue(zrc->wq);
	dm_kcopyd_client_destroy(zrc->kc);
	kfree(zrc);
}

/*
 * Suspend reclaim.
 */
void dmz_suspend_reclaim(struct dmz_reclaim *zrc)
{
	cancel_delayed_work_sync(&zrc->work);
}

/*
 * Resume reclaim.
 */
void dmz_resume_reclaim(struct dmz_reclaim *zrc)
{
	queue_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD);
}

/*
 * BIO accounting.
 */
void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc)
{
	zrc->atime = jiffies;
}

/*
 * Start reclaim if necessary.
 */
void dmz_schedule_reclaim(struct dmz_reclaim *zrc)
{
636 637 638
	unsigned int p_unmap = dmz_reclaim_percentage(zrc);

	if (dmz_should_reclaim(zrc, p_unmap))
639 640
		mod_delayed_work(zrc->wq, &zrc->work, 0);
}