123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- #include "blk-rq-qos.h"
- static bool atomic_inc_below(atomic_t *v, unsigned int below)
- {
- unsigned int cur = atomic_read(v);
- do {
- if (cur >= below)
- return false;
- } while (!atomic_try_cmpxchg(v, &cur, cur + 1));
- return true;
- }
- bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit)
- {
- return atomic_inc_below(&rq_wait->inflight, limit);
- }
- void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio)
- {
- do {
- if (rqos->ops->cleanup)
- rqos->ops->cleanup(rqos, bio);
- rqos = rqos->next;
- } while (rqos);
- }
- void __rq_qos_done(struct rq_qos *rqos, struct request *rq)
- {
- do {
- if (rqos->ops->done)
- rqos->ops->done(rqos, rq);
- rqos = rqos->next;
- } while (rqos);
- }
- void __rq_qos_issue(struct rq_qos *rqos, struct request *rq)
- {
- do {
- if (rqos->ops->issue)
- rqos->ops->issue(rqos, rq);
- rqos = rqos->next;
- } while (rqos);
- }
- void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq)
- {
- do {
- if (rqos->ops->requeue)
- rqos->ops->requeue(rqos, rq);
- rqos = rqos->next;
- } while (rqos);
- }
- void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio)
- {
- do {
- if (rqos->ops->throttle)
- rqos->ops->throttle(rqos, bio);
- rqos = rqos->next;
- } while (rqos);
- }
- void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio)
- {
- do {
- if (rqos->ops->track)
- rqos->ops->track(rqos, rq, bio);
- rqos = rqos->next;
- } while (rqos);
- }
- void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio)
- {
- do {
- if (rqos->ops->merge)
- rqos->ops->merge(rqos, rq, bio);
- rqos = rqos->next;
- } while (rqos);
- }
- void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio)
- {
- do {
- if (rqos->ops->done_bio)
- rqos->ops->done_bio(rqos, bio);
- rqos = rqos->next;
- } while (rqos);
- }
- void __rq_qos_queue_depth_changed(struct rq_qos *rqos)
- {
- do {
- if (rqos->ops->queue_depth_changed)
- rqos->ops->queue_depth_changed(rqos);
- rqos = rqos->next;
- } while (rqos);
- }
- bool rq_depth_calc_max_depth(struct rq_depth *rqd)
- {
- unsigned int depth;
- bool ret = false;
-
- if (rqd->queue_depth == 1) {
- if (rqd->scale_step > 0)
- rqd->max_depth = 1;
- else {
- rqd->max_depth = 2;
- ret = true;
- }
- } else {
-
- depth = min_t(unsigned int, rqd->default_depth,
- rqd->queue_depth);
- if (rqd->scale_step > 0)
- depth = 1 + ((depth - 1) >> min(31, rqd->scale_step));
- else if (rqd->scale_step < 0) {
- unsigned int maxd = 3 * rqd->queue_depth / 4;
- depth = 1 + ((depth - 1) << -rqd->scale_step);
- if (depth > maxd) {
- depth = maxd;
- ret = true;
- }
- }
- rqd->max_depth = depth;
- }
- return ret;
- }
- bool rq_depth_scale_up(struct rq_depth *rqd)
- {
-
- if (rqd->scaled_max)
- return false;
- rqd->scale_step--;
- rqd->scaled_max = rq_depth_calc_max_depth(rqd);
- return true;
- }
- bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle)
- {
-
- if (rqd->max_depth == 1)
- return false;
- if (rqd->scale_step < 0 && hard_throttle)
- rqd->scale_step = 0;
- else
- rqd->scale_step++;
- rqd->scaled_max = false;
- rq_depth_calc_max_depth(rqd);
- return true;
- }
- struct rq_qos_wait_data {
- struct wait_queue_entry wq;
- struct task_struct *task;
- struct rq_wait *rqw;
- acquire_inflight_cb_t *cb;
- void *private_data;
- bool got_token;
- };
- static int rq_qos_wake_function(struct wait_queue_entry *curr,
- unsigned int mode, int wake_flags, void *key)
- {
- struct rq_qos_wait_data *data = container_of(curr,
- struct rq_qos_wait_data,
- wq);
-
- if (!data->cb(data->rqw, data->private_data))
- return -1;
- data->got_token = true;
- smp_wmb();
- list_del_init(&curr->entry);
- wake_up_process(data->task);
- return 1;
- }
- void rq_qos_wait(struct rq_wait *rqw, void *private_data,
- acquire_inflight_cb_t *acquire_inflight_cb,
- cleanup_cb_t *cleanup_cb)
- {
- struct rq_qos_wait_data data = {
- .wq = {
- .func = rq_qos_wake_function,
- .entry = LIST_HEAD_INIT(data.wq.entry),
- },
- .task = current,
- .rqw = rqw,
- .cb = acquire_inflight_cb,
- .private_data = private_data,
- };
- bool has_sleeper;
- has_sleeper = wq_has_sleeper(&rqw->wait);
- if (!has_sleeper && acquire_inflight_cb(rqw, private_data))
- return;
- has_sleeper = !prepare_to_wait_exclusive(&rqw->wait, &data.wq,
- TASK_UNINTERRUPTIBLE);
- do {
-
- if (data.got_token)
- break;
- if (!has_sleeper && acquire_inflight_cb(rqw, private_data)) {
- finish_wait(&rqw->wait, &data.wq);
-
- smp_rmb();
- if (data.got_token)
- cleanup_cb(rqw, private_data);
- break;
- }
- io_schedule();
- has_sleeper = true;
- set_current_state(TASK_UNINTERRUPTIBLE);
- } while (1);
- finish_wait(&rqw->wait, &data.wq);
- }
- void rq_qos_exit(struct request_queue *q)
- {
- while (q->rq_qos) {
- struct rq_qos *rqos = q->rq_qos;
- q->rq_qos = rqos->next;
- rqos->ops->exit(rqos);
- }
- }
|