// SPDX-License-Identifier: GPL-2.0 /* * Block rq-qos base io controller * * This works similar to wbt with a few exceptions * * - It's bio based, so the latency covers the whole block layer in addition to * the actual io. * - We will throttle all IO that comes in here if we need to. * - We use the mean latency over the 100ms window. This is because writes can * be particularly fast, which could give us a false sense of the impact of * other workloads on our protected workload. * - By default there's no throttling, we set the queue_depth to UINT_MAX so * that we can have as many outstanding bio's as we're allowed to. Only at * throttle time do we pay attention to the actual queue depth. * * The hierarchy works like the cpu controller does, we track the latency at * every configured node, and each configured node has it's own independent * queue depth. This means that we only care about our latency targets at the * peer level. Some group at the bottom of the hierarchy isn't going to affect * a group at the end of some other path if we're only configred at leaf level. * * Consider the following * * root blkg * / \ * fast (target=5ms) slow (target=10ms) * / \ / \ * a b normal(15ms) unloved * * "a" and "b" have no target, but their combined io under "fast" cannot exceed * an average latency of 5ms. If it does then we will throttle the "slow" * group. In the case of "normal", if it exceeds its 15ms target, we will * throttle "unloved", but nobody else. * * In this example "fast", "slow", and "normal" will be the only groups actually * accounting their io latencies. We have to walk up the heirarchy to the root * on every submit and complete so we can do the appropriate stat recording and * adjust the queue depth of ourselves if needed. * * There are 2 ways we throttle IO. * * 1) Queue depth throttling. As we throttle down we will adjust the maximum * number of IO's we're allowed to have in flight. This starts at (u64)-1 down * to 1. If the group is only ever submitting IO for itself then this is the * only way we throttle. * * 2) Induced delay throttling. This is for the case that a group is generating * IO that has to be issued by the root cg to avoid priority inversion. So think * REQ_META or REQ_SWAP. If we are already at qd == 1 and we're getting a lot * of work done for us on behalf of the root cg and are being asked to scale * down more then we induce a latency at userspace return. We accumulate the * total amount of time we need to be punished by doing * * total_time += min_lat_nsec - actual_io_completion * * and then at throttle time will do * * throttle_time = min(total_time, NSEC_PER_SEC) * * This induced delay will throttle back the activity that is generating the * root cg issued io's, wethere that's some metadata intensive operation or the * group is using so much memory that it is pushing us into swap. * * Copyright (C) 2018 Josef Bacik
*/ #include <linux/kernel.h> #include <linux/blk_types.h> #include <linux/backing-dev.h> #include <linux/module.h> #include <linux/timer.h> #include <linux/memcontrol.h> #include <linux/sched/loadavg.h> #include <linux/sched/signal.h> #include <trace/events/block.h> #include <linux/blk-mq.h> #include"blk-rq-qos.h" #include"blk-stat.h" #include"blk-cgroup.h" #include"blk.h"
/* * ->enabled is the master enable switch gating the throttling logic and * inflight tracking. The number of cgroups which have iolat enabled is * tracked in ->enable_cnt, and ->enable is flipped on/off accordingly * from ->enable_work with the request_queue frozen. For details, See * blkiolatency_enable_work_fn().
*/ bool enabled;
atomic_t enable_cnt; struct work_struct enable_work;
};
/* total running average of our io latency. */
u64 lat_avg;
/* Our current number of IO's for the last summation. */
u64 nr_samples;
bool ssd; struct child_latency_info child_lat;
};
#define BLKIOLATENCY_MIN_WIN_SIZE (100 * NSEC_PER_MSEC) #define BLKIOLATENCY_MAX_WIN_SIZE NSEC_PER_SEC /* * These are the constants used to fake the fixed-point moving average * calculation just like load average. The call to calc_load() folds * (FIXED_1 (2048) - exp_factor) * new_sample into lat_avg. The sampling * window size is bucketed to try to approximately calculate average * latency such that 1/exp (decay rate) is [1 min, 2.5 min) when windows * elapse immediately. Note, windows only elapse with IO activity. Idle * periods extend the most recent window.
*/ #define BLKIOLATENCY_NR_EXP_FACTORS 5 #define BLKIOLATENCY_EXP_BUCKET_SIZE (BLKIOLATENCY_MAX_WIN_SIZE / \
(BLKIOLATENCY_NR_EXP_FACTORS - 1)) staticconst u64 iolatency_exp_factors[BLKIOLATENCY_NR_EXP_FACTORS] = {
2045, // exp(1/600) - 600 samples
2039, // exp(1/240) - 240 samples
2031, // exp(1/120) - 120 samples
2023, // exp(1/80) - 80 samples
2014, // exp(1/60) - 60 samples
};
staticinlinevoid iolat_update_total_lat_avg(struct iolatency_grp *iolat, struct latency_stat *stat)
{ int exp_idx;
if (iolat->ssd) return;
/* * calc_load() takes in a number stored in fixed point representation. * Because we are using this for IO time in ns, the values stored * are significantly larger than the FIXED_1 denominator (2048). * Therefore, rounding errors in the calculation are negligible and * can be ignored.
*/
exp_idx = min_t(int, BLKIOLATENCY_NR_EXP_FACTORS - 1,
div64_u64(iolat->cur_win_nsec,
BLKIOLATENCY_EXP_BUCKET_SIZE));
iolat->lat_avg = calc_load(iolat->lat_avg,
iolatency_exp_factors[exp_idx],
stat->rqs.mean);
}
if (use_delay)
blkcg_schedule_throttle(rqos->disk, use_memdelay);
/* * To avoid priority inversions we want to just take a slot if we are * issuing as root. If we're being killed off there's no point in * delaying things, we may have been killed by OOM so throttling may * make recovery take even longer, so just let the IO's through so the * task can go away.
*/ if (issue_as_root || fatal_signal_pending(current)) {
atomic_inc(&rqw->inflight); return;
}
/* * We scale the qd down faster than we scale up, so we need to use this helper * to adjust the scale_cookie accordingly so we don't prematurely get * scale_cookie at DEFAULT_SCALE_COOKIE and unthrottle too much. * * Each group has their own local copy of the last scale cookie they saw, so if * the global scale cookie goes up or down they know which way they need to go * based on their last knowledge of it.
*/ staticvoid scale_cookie_change(struct blk_iolatency *blkiolat, struct child_latency_info *lat_info, bool up)
{ unsignedlong qd = blkiolat->rqos.disk->queue->nr_requests; unsignedlong scale = scale_amount(qd, up); unsignedlong old = atomic_read(&lat_info->scale_cookie); unsignedlong max_scale = qd << 1; unsignedlong diff = 0;
if (old < DEFAULT_SCALE_COOKIE)
diff = DEFAULT_SCALE_COOKIE - old;
if (up) { if (scale + old > DEFAULT_SCALE_COOKIE)
atomic_set(&lat_info->scale_cookie,
DEFAULT_SCALE_COOKIE); elseif (diff > qd)
atomic_inc(&lat_info->scale_cookie); else
atomic_add(scale, &lat_info->scale_cookie);
} else { /* * We don't want to dig a hole so deep that it takes us hours to * dig out of it. Just enough that we don't throttle/unthrottle * with jagged workloads but can still unthrottle once pressure * has sufficiently dissipated.
*/ if (diff > qd) { if (diff < max_scale)
atomic_dec(&lat_info->scale_cookie);
} else {
atomic_sub(scale, &lat_info->scale_cookie);
}
}
}
/* * Change the queue depth of the iolatency_grp. We add 1/16th of the * queue depth at a time so we don't get wild swings and hopefully dial in to * fairer distribution of the overall queue depth. We halve the queue depth * at a time so we can scale down queue depth quickly from default unlimited * to target.
*/ staticvoid scale_change(struct iolatency_grp *iolat, bool up)
{ unsignedlong qd = iolat->blkiolat->rqos.disk->queue->nr_requests; unsignedlong scale = scale_amount(qd, up); unsignedlong old = iolat->max_depth;
if (old > qd)
old = qd;
if (up) { if (old == 1 && blkcg_unuse_delay(lat_to_blkg(iolat))) return;
if (old < qd) {
old += scale;
old = min(old, qd);
iolat->max_depth = old;
wake_up_all(&iolat->rq_wait.wait);
}
} else {
old >>= 1;
iolat->max_depth = max(old, 1UL);
}
}
/* Check our parent and see if the scale cookie has changed. */ staticvoid check_scale_change(struct iolatency_grp *iolat)
{ struct iolatency_grp *parent; struct child_latency_info *lat_info; unsignedint cur_cookie; unsignedint our_cookie = atomic_read(&iolat->scale_cookie);
u64 scale_lat; int direction = 0;
parent = blkg_to_lat(lat_to_blkg(iolat)->parent); if (!parent) return;
if (cur_cookie < our_cookie)
direction = -1; elseif (cur_cookie > our_cookie)
direction = 1; else return;
if (!atomic_try_cmpxchg(&iolat->scale_cookie, &our_cookie, cur_cookie)) { /* Somebody beat us to the punch, just bail. */ return;
}
if (direction < 0 && iolat->min_lat_nsec) {
u64 samples_thresh;
if (!scale_lat || iolat->min_lat_nsec <= scale_lat) return;
/* * Sometimes high priority groups are their own worst enemy, so * instead of taking it out on some poor other group that did 5% * or less of the IO's for the last summation just skip this * scale down event.
*/
samples_thresh = lat_info->nr_samples * 5;
samples_thresh = max(1ULL, div64_u64(samples_thresh, 100)); if (iolat->nr_samples <= samples_thresh) return;
}
/* We're as low as we can go. */ if (iolat->max_depth == 1 && direction < 0) {
blkcg_use_delay(lat_to_blkg(iolat)); return;
}
/* We're back to the default cookie, unthrottle all the things. */ if (cur_cookie == DEFAULT_SCALE_COOKIE) {
blkcg_clear_delay(lat_to_blkg(iolat));
iolat->max_depth = UINT_MAX;
wake_up_all(&iolat->rq_wait.wait); return;
}
/* * We don't want to count issue_as_root bio's in the cgroups latency * statistics as it could skew the numbers downwards.
*/ if (unlikely(issue_as_root && iolat->max_depth != UINT_MAX)) {
u64 sub = iolat->min_lat_nsec; if (req_time < sub)
blkcg_add_delay(lat_to_blkg(iolat), now, sub - req_time); return;
}
parent = blkg_to_lat(blkg->parent); if (!parent) return;
lat_info = &parent->child_lat;
iolat_update_total_lat_avg(iolat, &stat);
/* Everything is ok and we don't need to adjust the scale. */ if (latency_sum_ok(iolat, &stat) &&
atomic_read(&lat_info->scale_cookie) == DEFAULT_SCALE_COOKIE) return;
/* Somebody beat us to the punch, just bail. */
spin_lock_irqsave(&lat_info->lock, flags);
spin_lock_irqsave(&lat_info->lock, flags); if (lat_info->last_scale_event >= now) goto next_lock;
/* * We scaled down but don't have a scale_grp, scale up and carry * on.
*/ if (lat_info->scale_grp == NULL) {
scale_cookie_change(iolat->blkiolat, lat_info, true); goto next_lock;
}
/* * It's been 5 seconds since our last scale event, clear the * scale grp in case the group that needed the scale down isn't * doing any IO currently.
*/ if (now - lat_info->last_scale_event >=
((u64)NSEC_PER_SEC * 5))
lat_info->scale_grp = NULL;
next_lock:
spin_unlock_irqrestore(&lat_info->lock, flags);
next:
blkg_put(blkg);
}
rcu_read_unlock();
}
/** * blkiolatency_enable_work_fn - Enable or disable iolatency on the device * @work: enable_work of the blk_iolatency of interest * * iolatency needs to keep track of the number of in-flight IOs per cgroup. This * is relatively expensive as it involves walking up the hierarchy twice for * every IO. Thus, if iolatency is not enabled in any cgroup for the device, we * want to disable the in-flight tracking. * * We have to make sure that the counting is balanced - we don't want to leak * the in-flight counts by disabling accounting in the completion path while IOs * are in flight. This is achieved by ensuring that no IO is in flight by * freezing the queue while flipping ->enabled. As this requires a sleepable * context, ->enabled flipping is punted to this work function.
*/ staticvoid blkiolatency_enable_work_fn(struct work_struct *work)
{ struct blk_iolatency *blkiolat = container_of(work, struct blk_iolatency,
enable_work); bool enabled;
/* * There can only be one instance of this function running for @blkiolat * and it's guaranteed to be executed at least once after the latest * ->enabled_cnt modification. Acting on the latest ->enable_cnt is * sufficient. * * Also, we know @blkiolat is safe to access as ->enable_work is flushed * in blkcg_iolatency_exit().
*/
enabled = atomic_read(&blkiolat->enable_cnt); if (enabled != blkiolat->enabled) { unsignedint memflags;
staticint blk_iolatency_init(struct gendisk *disk)
{ struct blk_iolatency *blkiolat; int ret;
blkiolat = kzalloc(sizeof(*blkiolat), GFP_KERNEL); if (!blkiolat) return -ENOMEM;
ret = rq_qos_add(&blkiolat->rqos, disk, RQ_QOS_LATENCY,
&blkcg_iolatency_ops); if (ret) goto err_free;
ret = blkcg_activate_policy(disk, &blkcg_policy_iolatency); if (ret) goto err_qos_del;
ret = blkg_conf_open_bdev(&ctx); if (ret) goto out;
/* * blk_iolatency_init() may fail after rq_qos_add() succeeds which can * confuse iolat_rq_qos() test. Make the test and init atomic.
*/
lockdep_assert_held(&ctx.bdev->bd_queue->rq_qos_mutex); if (!iolat_rq_qos(ctx.bdev->bd_queue))
ret = blk_iolatency_init(ctx.bdev->bd_disk); if (ret) goto out;
ret = blkg_conf_prep(blkcg, &blkcg_policy_iolatency, &ctx); if (ret) goto out;
iolat = blkg_to_lat(ctx.blkg);
p = ctx.body;
ret = -EINVAL; while ((tok = strsep(&p, " "))) { char key[16]; char val[21]; /* 18446744073709551616 */
if (sscanf(tok, "%15[^=]=%20s", key, val) != 2) goto out;
/* * We init things in list order, so the pd for the parent may not be * init'ed yet for whatever reason.
*/ if (blkg->parent && blkg_to_pd(blkg->parent, &blkcg_policy_iolatency)) { struct iolatency_grp *parent = blkg_to_lat(blkg->parent);
atomic_set(&iolat->scale_cookie,
atomic_read(&parent->child_lat.scale_cookie));
} else {
atomic_set(&iolat->scale_cookie, DEFAULT_SCALE_COOKIE);
}
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 ist noch experimentell.