// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2017 Western Digital Corporation or its affiliates. * * This file is released under the GPL.
*/
if (dmz_is_seq(dst_zone))
flags |= BIT(DM_KCOPYD_WRITE_SEQ);
while (block < end_block) { if (src_zone->dev->flags & DMZ_BDEV_DYING) return -EIO; if (dst_zone->dev->flags & DMZ_BDEV_DYING) return -EIO;
if (dmz_reclaim_should_terminate(src_zone)) return -EINTR;
/* 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;
}
/* Copy the valid region */
set_bit(DMZ_RECLAIM_KCOPY, &zrc->flags);
dm_kcopyd_copy(zrc->kc, &src, 1, &dst, flags,
dmz_reclaim_kcopy_end, zrc);
/* 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.
*/ staticint 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;
DMDEBUG("(%s/%u): Chunk %u, move buf zone %u (weight %u) to data zone %u (weight %u)",
dmz_metadata_label(zmd), zrc->dev_idx,
dzone->chunk, bzone->id, dmz_weight(bzone),
dzone->id, dmz_weight(dzone));
/* 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 */
dmz_invalidate_blocks(zmd, bzone, 0, dmz_zone_nr_blocks(zmd));
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);
return ret;
}
/* * Merge valid blocks of dzone into its buffer zone and free dzone.
*/ staticint dmz_reclaim_seq_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
{ unsignedint chunk = dzone->chunk; struct dm_zone *bzone = dzone->bzone; struct dmz_metadata *zmd = zrc->metadata; int ret = 0;
DMDEBUG("(%s/%u): Chunk %u, move data zone %u (weight %u) to buf zone %u (weight %u)",
dmz_metadata_label(zmd), zrc->dev_idx,
chunk, dzone->id, dmz_weight(dzone),
bzone->id, dmz_weight(bzone));
/* 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.
*/
dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd));
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);
return ret;
}
/* * 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.
*/ staticint dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone)
{ unsignedint chunk = dzone->chunk; struct dm_zone *szone = NULL; struct dmz_metadata *zmd = zrc->metadata; int ret; int alloc_flags = DMZ_ALLOC_SEQ;
/* Get a free random or sequential zone */
dmz_lock_map(zmd);
again:
szone = dmz_alloc_zone(zmd, zrc->dev_idx,
alloc_flags | DMZ_ALLOC_RECLAIM); if (!szone && alloc_flags == DMZ_ALLOC_SEQ && dmz_nr_cache_zones(zmd)) {
alloc_flags = DMZ_ALLOC_RND; goto again;
}
dmz_unlock_map(zmd); if (!szone) return -ENOSPC;
DMDEBUG("(%s/%u): Chunk %u, move %s zone %u (weight %u) to %s zone %u",
dmz_metadata_label(zmd), zrc->dev_idx, chunk,
dmz_is_cache(dzone) ? "cache" : "rnd",
dzone->id, dmz_weight(dzone),
dmz_is_rnd(szone) ? "rnd" : "seq", szone->id);
/* 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 */
dmz_invalidate_blocks(zmd, dzone, 0, dmz_zone_nr_blocks(zmd));
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);
}
/* * Test if the target device is idle.
*/ staticinlineint dmz_target_idle(struct dmz_reclaim *zrc)
{ return time_is_before_jiffies(zrc->atime + DMZ_IDLE_PERIOD);
}
/* * Find a candidate zone for reclaim and process it.
*/ staticint dmz_do_reclaim(struct dmz_reclaim *zrc)
{ struct dmz_metadata *zmd = zrc->metadata; struct dm_zone *dzone; struct dm_zone *rzone; unsignedlong start; int ret;
/* Get a data zone */
dzone = dmz_get_zone_for_reclaim(zmd, zrc->dev_idx,
dmz_target_idle(zrc)); if (!dzone) {
DMDEBUG("(%s/%u): No zone found to reclaim",
dmz_metadata_label(zmd), zrc->dev_idx); return -EBUSY;
}
rzone = dzone;
start = jiffies; if (dmz_is_cache(dzone) || dmz_is_rnd(dzone)) { 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) { if (ret == -EINTR)
DMDEBUG("(%s/%u): reclaim zone %u interrupted",
dmz_metadata_label(zmd), zrc->dev_idx,
rzone->id); else
DMDEBUG("(%s/%u): Failed to reclaim zone %u, err %d",
dmz_metadata_label(zmd), zrc->dev_idx,
rzone->id, ret);
dmz_unlock_zone_reclaim(dzone); return ret;
}
ret = dmz_flush_metadata(zrc->metadata); if (ret) {
DMDEBUG("(%s/%u): Metadata flush for zone %u failed, err %d",
dmz_metadata_label(zmd), zrc->dev_idx, rzone->id, ret); return ret;
}
DMDEBUG("(%s/%u): Reclaimed zone %u in %u ms",
dmz_metadata_label(zmd), zrc->dev_idx,
rzone->id, jiffies_to_msecs(jiffies - start)); return 0;
}
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) returnfalse;
nr_reclaim += dmz_nr_cache_zones(zrc->metadata);
}
/* Reclaim when idle */ if (dmz_target_idle(zrc) && nr_reclaim) returntrue;
/* If there are still plenty of cache zones, do not reclaim */ if (p_unmap >= DMZ_RECLAIM_HIGH_UNMAP_ZONES) returnfalse;
/* * If the percentage of unmapped cache zones is low, * reclaim even if the target is busy.
*/ return p_unmap <= DMZ_RECLAIM_LOW_UNMAP_ZONES;
}
/* * 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.
*/ if (dmz_target_idle(zrc) || p_unmap < DMZ_RECLAIM_LOW_UNMAP_ZONES / 2) { /* Idle or very low percentage: go fast */
zrc->kc_throttle.throttle = 100;
} else { /* Busy but we still have some random zone: throttle */
zrc->kc_throttle.throttle = min(75U, 100U - p_unmap / 2);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.