1a497ee34SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0-or-later 2ea25da48SPaolo Valente /* 3ea25da48SPaolo Valente * cgroups support for the BFQ I/O scheduler. 4ea25da48SPaolo Valente */ 5ea25da48SPaolo Valente #include <linux/module.h> 6ea25da48SPaolo Valente #include <linux/slab.h> 7ea25da48SPaolo Valente #include <linux/blkdev.h> 8ea25da48SPaolo Valente #include <linux/cgroup.h> 9ea25da48SPaolo Valente #include <linux/elevator.h> 10ea25da48SPaolo Valente #include <linux/ktime.h> 11ea25da48SPaolo Valente #include <linux/rbtree.h> 12ea25da48SPaolo Valente #include <linux/ioprio.h> 13ea25da48SPaolo Valente #include <linux/sbitmap.h> 14ea25da48SPaolo Valente #include <linux/delay.h> 15ea25da48SPaolo Valente 16ea25da48SPaolo Valente #include "bfq-iosched.h" 17ea25da48SPaolo Valente 188060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG 19c0ce79dcSChristoph Hellwig static int bfq_stat_init(struct bfq_stat *stat, gfp_t gfp) 20c0ce79dcSChristoph Hellwig { 21c0ce79dcSChristoph Hellwig int ret; 22c0ce79dcSChristoph Hellwig 23c0ce79dcSChristoph Hellwig ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp); 24c0ce79dcSChristoph Hellwig if (ret) 25c0ce79dcSChristoph Hellwig return ret; 26c0ce79dcSChristoph Hellwig 27c0ce79dcSChristoph Hellwig atomic64_set(&stat->aux_cnt, 0); 28c0ce79dcSChristoph Hellwig return 0; 29c0ce79dcSChristoph Hellwig } 30c0ce79dcSChristoph Hellwig 31c0ce79dcSChristoph Hellwig static void bfq_stat_exit(struct bfq_stat *stat) 32c0ce79dcSChristoph Hellwig { 33c0ce79dcSChristoph Hellwig percpu_counter_destroy(&stat->cpu_cnt); 34c0ce79dcSChristoph Hellwig } 35c0ce79dcSChristoph Hellwig 36c0ce79dcSChristoph Hellwig /** 37c0ce79dcSChristoph Hellwig * bfq_stat_add - add a value to a bfq_stat 38c0ce79dcSChristoph Hellwig * @stat: target bfq_stat 39c0ce79dcSChristoph Hellwig * @val: value to add 40c0ce79dcSChristoph Hellwig * 41c0ce79dcSChristoph Hellwig * Add @val to @stat. The caller must ensure that IRQ on the same CPU 42c0ce79dcSChristoph Hellwig * don't re-enter this function for the same counter. 43c0ce79dcSChristoph Hellwig */ 44c0ce79dcSChristoph Hellwig static inline void bfq_stat_add(struct bfq_stat *stat, uint64_t val) 45c0ce79dcSChristoph Hellwig { 46c0ce79dcSChristoph Hellwig percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH); 47c0ce79dcSChristoph Hellwig } 48c0ce79dcSChristoph Hellwig 49c0ce79dcSChristoph Hellwig /** 50c0ce79dcSChristoph Hellwig * bfq_stat_read - read the current value of a bfq_stat 51c0ce79dcSChristoph Hellwig * @stat: bfq_stat to read 52c0ce79dcSChristoph Hellwig */ 53c0ce79dcSChristoph Hellwig static inline uint64_t bfq_stat_read(struct bfq_stat *stat) 54c0ce79dcSChristoph Hellwig { 55c0ce79dcSChristoph Hellwig return percpu_counter_sum_positive(&stat->cpu_cnt); 56c0ce79dcSChristoph Hellwig } 57c0ce79dcSChristoph Hellwig 58c0ce79dcSChristoph Hellwig /** 59c0ce79dcSChristoph Hellwig * bfq_stat_reset - reset a bfq_stat 60c0ce79dcSChristoph Hellwig * @stat: bfq_stat to reset 61c0ce79dcSChristoph Hellwig */ 62c0ce79dcSChristoph Hellwig static inline void bfq_stat_reset(struct bfq_stat *stat) 63c0ce79dcSChristoph Hellwig { 64c0ce79dcSChristoph Hellwig percpu_counter_set(&stat->cpu_cnt, 0); 65c0ce79dcSChristoph Hellwig atomic64_set(&stat->aux_cnt, 0); 66c0ce79dcSChristoph Hellwig } 67c0ce79dcSChristoph Hellwig 68c0ce79dcSChristoph Hellwig /** 69c0ce79dcSChristoph Hellwig * bfq_stat_add_aux - add a bfq_stat into another's aux count 70c0ce79dcSChristoph Hellwig * @to: the destination bfq_stat 71c0ce79dcSChristoph Hellwig * @from: the source 72c0ce79dcSChristoph Hellwig * 73c0ce79dcSChristoph Hellwig * Add @from's count including the aux one to @to's aux count. 74c0ce79dcSChristoph Hellwig */ 75c0ce79dcSChristoph Hellwig static inline void bfq_stat_add_aux(struct bfq_stat *to, 76c0ce79dcSChristoph Hellwig struct bfq_stat *from) 77c0ce79dcSChristoph Hellwig { 78c0ce79dcSChristoph Hellwig atomic64_add(bfq_stat_read(from) + atomic64_read(&from->aux_cnt), 79c0ce79dcSChristoph Hellwig &to->aux_cnt); 80c0ce79dcSChristoph Hellwig } 81c0ce79dcSChristoph Hellwig 82c0ce79dcSChristoph Hellwig /** 83c0ce79dcSChristoph Hellwig * blkg_prfill_stat - prfill callback for bfq_stat 84c0ce79dcSChristoph Hellwig * @sf: seq_file to print to 85c0ce79dcSChristoph Hellwig * @pd: policy private data of interest 86c0ce79dcSChristoph Hellwig * @off: offset to the bfq_stat in @pd 87c0ce79dcSChristoph Hellwig * 88c0ce79dcSChristoph Hellwig * prfill callback for printing a bfq_stat. 89c0ce79dcSChristoph Hellwig */ 90c0ce79dcSChristoph Hellwig static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, 91c0ce79dcSChristoph Hellwig int off) 92c0ce79dcSChristoph Hellwig { 93c0ce79dcSChristoph Hellwig return __blkg_prfill_u64(sf, pd, bfq_stat_read((void *)pd + off)); 94c0ce79dcSChristoph Hellwig } 95c0ce79dcSChristoph Hellwig 96ea25da48SPaolo Valente /* bfqg stats flags */ 97ea25da48SPaolo Valente enum bfqg_stats_flags { 98ea25da48SPaolo Valente BFQG_stats_waiting = 0, 99ea25da48SPaolo Valente BFQG_stats_idling, 100ea25da48SPaolo Valente BFQG_stats_empty, 101ea25da48SPaolo Valente }; 102ea25da48SPaolo Valente 103ea25da48SPaolo Valente #define BFQG_FLAG_FNS(name) \ 104ea25da48SPaolo Valente static void bfqg_stats_mark_##name(struct bfqg_stats *stats) \ 105ea25da48SPaolo Valente { \ 106ea25da48SPaolo Valente stats->flags |= (1 << BFQG_stats_##name); \ 107ea25da48SPaolo Valente } \ 108ea25da48SPaolo Valente static void bfqg_stats_clear_##name(struct bfqg_stats *stats) \ 109ea25da48SPaolo Valente { \ 110ea25da48SPaolo Valente stats->flags &= ~(1 << BFQG_stats_##name); \ 111ea25da48SPaolo Valente } \ 112ea25da48SPaolo Valente static int bfqg_stats_##name(struct bfqg_stats *stats) \ 113ea25da48SPaolo Valente { \ 114ea25da48SPaolo Valente return (stats->flags & (1 << BFQG_stats_##name)) != 0; \ 115ea25da48SPaolo Valente } \ 116ea25da48SPaolo Valente 117ea25da48SPaolo Valente BFQG_FLAG_FNS(waiting) 118ea25da48SPaolo Valente BFQG_FLAG_FNS(idling) 119ea25da48SPaolo Valente BFQG_FLAG_FNS(empty) 120ea25da48SPaolo Valente #undef BFQG_FLAG_FNS 121ea25da48SPaolo Valente 1228f9bebc3SPaolo Valente /* This should be called with the scheduler lock held. */ 123ea25da48SPaolo Valente static void bfqg_stats_update_group_wait_time(struct bfqg_stats *stats) 124ea25da48SPaolo Valente { 12584c7afceSOmar Sandoval u64 now; 126ea25da48SPaolo Valente 127ea25da48SPaolo Valente if (!bfqg_stats_waiting(stats)) 128ea25da48SPaolo Valente return; 129ea25da48SPaolo Valente 13084c7afceSOmar Sandoval now = ktime_get_ns(); 13184c7afceSOmar Sandoval if (now > stats->start_group_wait_time) 132c0ce79dcSChristoph Hellwig bfq_stat_add(&stats->group_wait_time, 133ea25da48SPaolo Valente now - stats->start_group_wait_time); 134ea25da48SPaolo Valente bfqg_stats_clear_waiting(stats); 135ea25da48SPaolo Valente } 136ea25da48SPaolo Valente 1378f9bebc3SPaolo Valente /* This should be called with the scheduler lock held. */ 138ea25da48SPaolo Valente static void bfqg_stats_set_start_group_wait_time(struct bfq_group *bfqg, 139ea25da48SPaolo Valente struct bfq_group *curr_bfqg) 140ea25da48SPaolo Valente { 141ea25da48SPaolo Valente struct bfqg_stats *stats = &bfqg->stats; 142ea25da48SPaolo Valente 143ea25da48SPaolo Valente if (bfqg_stats_waiting(stats)) 144ea25da48SPaolo Valente return; 145ea25da48SPaolo Valente if (bfqg == curr_bfqg) 146ea25da48SPaolo Valente return; 14784c7afceSOmar Sandoval stats->start_group_wait_time = ktime_get_ns(); 148ea25da48SPaolo Valente bfqg_stats_mark_waiting(stats); 149ea25da48SPaolo Valente } 150ea25da48SPaolo Valente 1518f9bebc3SPaolo Valente /* This should be called with the scheduler lock held. */ 152ea25da48SPaolo Valente static void bfqg_stats_end_empty_time(struct bfqg_stats *stats) 153ea25da48SPaolo Valente { 15484c7afceSOmar Sandoval u64 now; 155ea25da48SPaolo Valente 156ea25da48SPaolo Valente if (!bfqg_stats_empty(stats)) 157ea25da48SPaolo Valente return; 158ea25da48SPaolo Valente 15984c7afceSOmar Sandoval now = ktime_get_ns(); 16084c7afceSOmar Sandoval if (now > stats->start_empty_time) 161c0ce79dcSChristoph Hellwig bfq_stat_add(&stats->empty_time, 162ea25da48SPaolo Valente now - stats->start_empty_time); 163ea25da48SPaolo Valente bfqg_stats_clear_empty(stats); 164ea25da48SPaolo Valente } 165ea25da48SPaolo Valente 166ea25da48SPaolo Valente void bfqg_stats_update_dequeue(struct bfq_group *bfqg) 167ea25da48SPaolo Valente { 168c0ce79dcSChristoph Hellwig bfq_stat_add(&bfqg->stats.dequeue, 1); 169ea25da48SPaolo Valente } 170ea25da48SPaolo Valente 171ea25da48SPaolo Valente void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg) 172ea25da48SPaolo Valente { 173ea25da48SPaolo Valente struct bfqg_stats *stats = &bfqg->stats; 174ea25da48SPaolo Valente 175ea25da48SPaolo Valente if (blkg_rwstat_total(&stats->queued)) 176ea25da48SPaolo Valente return; 177ea25da48SPaolo Valente 178ea25da48SPaolo Valente /* 179ea25da48SPaolo Valente * group is already marked empty. This can happen if bfqq got new 180ea25da48SPaolo Valente * request in parent group and moved to this group while being added 181ea25da48SPaolo Valente * to service tree. Just ignore the event and move on. 182ea25da48SPaolo Valente */ 183ea25da48SPaolo Valente if (bfqg_stats_empty(stats)) 184ea25da48SPaolo Valente return; 185ea25da48SPaolo Valente 18684c7afceSOmar Sandoval stats->start_empty_time = ktime_get_ns(); 187ea25da48SPaolo Valente bfqg_stats_mark_empty(stats); 188ea25da48SPaolo Valente } 189ea25da48SPaolo Valente 190ea25da48SPaolo Valente void bfqg_stats_update_idle_time(struct bfq_group *bfqg) 191ea25da48SPaolo Valente { 192ea25da48SPaolo Valente struct bfqg_stats *stats = &bfqg->stats; 193ea25da48SPaolo Valente 194ea25da48SPaolo Valente if (bfqg_stats_idling(stats)) { 19584c7afceSOmar Sandoval u64 now = ktime_get_ns(); 196ea25da48SPaolo Valente 19784c7afceSOmar Sandoval if (now > stats->start_idle_time) 198c0ce79dcSChristoph Hellwig bfq_stat_add(&stats->idle_time, 199ea25da48SPaolo Valente now - stats->start_idle_time); 200ea25da48SPaolo Valente bfqg_stats_clear_idling(stats); 201ea25da48SPaolo Valente } 202ea25da48SPaolo Valente } 203ea25da48SPaolo Valente 204ea25da48SPaolo Valente void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) 205ea25da48SPaolo Valente { 206ea25da48SPaolo Valente struct bfqg_stats *stats = &bfqg->stats; 207ea25da48SPaolo Valente 20884c7afceSOmar Sandoval stats->start_idle_time = ktime_get_ns(); 209ea25da48SPaolo Valente bfqg_stats_mark_idling(stats); 210ea25da48SPaolo Valente } 211ea25da48SPaolo Valente 212ea25da48SPaolo Valente void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg) 213ea25da48SPaolo Valente { 214ea25da48SPaolo Valente struct bfqg_stats *stats = &bfqg->stats; 215ea25da48SPaolo Valente 216c0ce79dcSChristoph Hellwig bfq_stat_add(&stats->avg_queue_size_sum, 217ea25da48SPaolo Valente blkg_rwstat_total(&stats->queued)); 218c0ce79dcSChristoph Hellwig bfq_stat_add(&stats->avg_queue_size_samples, 1); 219ea25da48SPaolo Valente bfqg_stats_update_group_wait_time(stats); 220ea25da48SPaolo Valente } 221ea25da48SPaolo Valente 222a33801e8SLuca Miccio void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq, 223a33801e8SLuca Miccio unsigned int op) 224a33801e8SLuca Miccio { 225a33801e8SLuca Miccio blkg_rwstat_add(&bfqg->stats.queued, op, 1); 226a33801e8SLuca Miccio bfqg_stats_end_empty_time(&bfqg->stats); 227a33801e8SLuca Miccio if (!(bfqq == ((struct bfq_data *)bfqg->bfqd)->in_service_queue)) 228a33801e8SLuca Miccio bfqg_stats_set_start_group_wait_time(bfqg, bfqq_group(bfqq)); 229a33801e8SLuca Miccio } 230a33801e8SLuca Miccio 231a33801e8SLuca Miccio void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op) 232a33801e8SLuca Miccio { 233a33801e8SLuca Miccio blkg_rwstat_add(&bfqg->stats.queued, op, -1); 234a33801e8SLuca Miccio } 235a33801e8SLuca Miccio 236a33801e8SLuca Miccio void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op) 237a33801e8SLuca Miccio { 238a33801e8SLuca Miccio blkg_rwstat_add(&bfqg->stats.merged, op, 1); 239a33801e8SLuca Miccio } 240a33801e8SLuca Miccio 24184c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns, 24284c7afceSOmar Sandoval u64 io_start_time_ns, unsigned int op) 243a33801e8SLuca Miccio { 244a33801e8SLuca Miccio struct bfqg_stats *stats = &bfqg->stats; 24584c7afceSOmar Sandoval u64 now = ktime_get_ns(); 246a33801e8SLuca Miccio 24784c7afceSOmar Sandoval if (now > io_start_time_ns) 248a33801e8SLuca Miccio blkg_rwstat_add(&stats->service_time, op, 24984c7afceSOmar Sandoval now - io_start_time_ns); 25084c7afceSOmar Sandoval if (io_start_time_ns > start_time_ns) 251a33801e8SLuca Miccio blkg_rwstat_add(&stats->wait_time, op, 25284c7afceSOmar Sandoval io_start_time_ns - start_time_ns); 253a33801e8SLuca Miccio } 254a33801e8SLuca Miccio 2558060c47bSChristoph Hellwig #else /* CONFIG_BFQ_CGROUP_DEBUG */ 256a33801e8SLuca Miccio 257a33801e8SLuca Miccio void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq, 258a33801e8SLuca Miccio unsigned int op) { } 259a33801e8SLuca Miccio void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op) { } 260a33801e8SLuca Miccio void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op) { } 26184c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns, 26284c7afceSOmar Sandoval u64 io_start_time_ns, unsigned int op) { } 263a33801e8SLuca Miccio void bfqg_stats_update_dequeue(struct bfq_group *bfqg) { } 264a33801e8SLuca Miccio void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg) { } 265a33801e8SLuca Miccio void bfqg_stats_update_idle_time(struct bfq_group *bfqg) { } 266a33801e8SLuca Miccio void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) { } 267a33801e8SLuca Miccio void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg) { } 268a33801e8SLuca Miccio 2698060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */ 270a33801e8SLuca Miccio 271a33801e8SLuca Miccio #ifdef CONFIG_BFQ_GROUP_IOSCHED 272a33801e8SLuca Miccio 273ea25da48SPaolo Valente /* 274ea25da48SPaolo Valente * blk-cgroup policy-related handlers 275ea25da48SPaolo Valente * The following functions help in converting between blk-cgroup 276ea25da48SPaolo Valente * internal structures and BFQ-specific structures. 277ea25da48SPaolo Valente */ 278ea25da48SPaolo Valente 279ea25da48SPaolo Valente static struct bfq_group *pd_to_bfqg(struct blkg_policy_data *pd) 280ea25da48SPaolo Valente { 281ea25da48SPaolo Valente return pd ? container_of(pd, struct bfq_group, pd) : NULL; 282ea25da48SPaolo Valente } 283ea25da48SPaolo Valente 284ea25da48SPaolo Valente struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg) 285ea25da48SPaolo Valente { 286ea25da48SPaolo Valente return pd_to_blkg(&bfqg->pd); 287ea25da48SPaolo Valente } 288ea25da48SPaolo Valente 289ea25da48SPaolo Valente static struct bfq_group *blkg_to_bfqg(struct blkcg_gq *blkg) 290ea25da48SPaolo Valente { 291ea25da48SPaolo Valente return pd_to_bfqg(blkg_to_pd(blkg, &blkcg_policy_bfq)); 292ea25da48SPaolo Valente } 293ea25da48SPaolo Valente 294ea25da48SPaolo Valente /* 295ea25da48SPaolo Valente * bfq_group handlers 296ea25da48SPaolo Valente * The following functions help in navigating the bfq_group hierarchy 297ea25da48SPaolo Valente * by allowing to find the parent of a bfq_group or the bfq_group 298ea25da48SPaolo Valente * associated to a bfq_queue. 299ea25da48SPaolo Valente */ 300ea25da48SPaolo Valente 301ea25da48SPaolo Valente static struct bfq_group *bfqg_parent(struct bfq_group *bfqg) 302ea25da48SPaolo Valente { 303ea25da48SPaolo Valente struct blkcg_gq *pblkg = bfqg_to_blkg(bfqg)->parent; 304ea25da48SPaolo Valente 305ea25da48SPaolo Valente return pblkg ? blkg_to_bfqg(pblkg) : NULL; 306ea25da48SPaolo Valente } 307ea25da48SPaolo Valente 308ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq) 309ea25da48SPaolo Valente { 310ea25da48SPaolo Valente struct bfq_entity *group_entity = bfqq->entity.parent; 311ea25da48SPaolo Valente 312ea25da48SPaolo Valente return group_entity ? container_of(group_entity, struct bfq_group, 313ea25da48SPaolo Valente entity) : 314ea25da48SPaolo Valente bfqq->bfqd->root_group; 315ea25da48SPaolo Valente } 316ea25da48SPaolo Valente 317ea25da48SPaolo Valente /* 318ea25da48SPaolo Valente * The following two functions handle get and put of a bfq_group by 319ea25da48SPaolo Valente * wrapping the related blk-cgroup hooks. 320ea25da48SPaolo Valente */ 321ea25da48SPaolo Valente 322ea25da48SPaolo Valente static void bfqg_get(struct bfq_group *bfqg) 323ea25da48SPaolo Valente { 3248f9bebc3SPaolo Valente bfqg->ref++; 325ea25da48SPaolo Valente } 326ea25da48SPaolo Valente 327dfb79af5SBart Van Assche static void bfqg_put(struct bfq_group *bfqg) 328ea25da48SPaolo Valente { 3298f9bebc3SPaolo Valente bfqg->ref--; 3308f9bebc3SPaolo Valente 3318f9bebc3SPaolo Valente if (bfqg->ref == 0) 3328f9bebc3SPaolo Valente kfree(bfqg); 3338f9bebc3SPaolo Valente } 3348f9bebc3SPaolo Valente 335db37a34cSPaolo Valente void bfqg_and_blkg_get(struct bfq_group *bfqg) 3368f9bebc3SPaolo Valente { 3378f9bebc3SPaolo Valente /* see comments in bfq_bic_update_cgroup for why refcounting bfqg */ 3388f9bebc3SPaolo Valente bfqg_get(bfqg); 3398f9bebc3SPaolo Valente 3408f9bebc3SPaolo Valente blkg_get(bfqg_to_blkg(bfqg)); 3418f9bebc3SPaolo Valente } 3428f9bebc3SPaolo Valente 3438f9bebc3SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg) 3448f9bebc3SPaolo Valente { 3458f9bebc3SPaolo Valente blkg_put(bfqg_to_blkg(bfqg)); 346d5274b3cSKonstantin Khlebnikov 347d5274b3cSKonstantin Khlebnikov bfqg_put(bfqg); 348ea25da48SPaolo Valente } 349ea25da48SPaolo Valente 350fd41e603STejun Heo void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq) 351fd41e603STejun Heo { 352fd41e603STejun Heo struct bfq_group *bfqg = blkg_to_bfqg(rq->bio->bi_blkg); 353fd41e603STejun Heo 35408802ed6SHou Tao if (!bfqg) 35508802ed6SHou Tao return; 35608802ed6SHou Tao 357fd41e603STejun Heo blkg_rwstat_add(&bfqg->stats.bytes, rq->cmd_flags, blk_rq_bytes(rq)); 358fd41e603STejun Heo blkg_rwstat_add(&bfqg->stats.ios, rq->cmd_flags, 1); 359fd41e603STejun Heo } 360fd41e603STejun Heo 361ea25da48SPaolo Valente /* @stats = 0 */ 362ea25da48SPaolo Valente static void bfqg_stats_reset(struct bfqg_stats *stats) 363ea25da48SPaolo Valente { 3648060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG 365ea25da48SPaolo Valente /* queued stats shouldn't be cleared */ 366ea25da48SPaolo Valente blkg_rwstat_reset(&stats->merged); 367ea25da48SPaolo Valente blkg_rwstat_reset(&stats->service_time); 368ea25da48SPaolo Valente blkg_rwstat_reset(&stats->wait_time); 369c0ce79dcSChristoph Hellwig bfq_stat_reset(&stats->time); 370c0ce79dcSChristoph Hellwig bfq_stat_reset(&stats->avg_queue_size_sum); 371c0ce79dcSChristoph Hellwig bfq_stat_reset(&stats->avg_queue_size_samples); 372c0ce79dcSChristoph Hellwig bfq_stat_reset(&stats->dequeue); 373c0ce79dcSChristoph Hellwig bfq_stat_reset(&stats->group_wait_time); 374c0ce79dcSChristoph Hellwig bfq_stat_reset(&stats->idle_time); 375c0ce79dcSChristoph Hellwig bfq_stat_reset(&stats->empty_time); 376a33801e8SLuca Miccio #endif 377ea25da48SPaolo Valente } 378ea25da48SPaolo Valente 379ea25da48SPaolo Valente /* @to += @from */ 380ea25da48SPaolo Valente static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from) 381ea25da48SPaolo Valente { 382ea25da48SPaolo Valente if (!to || !from) 383ea25da48SPaolo Valente return; 384ea25da48SPaolo Valente 3858060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG 386ea25da48SPaolo Valente /* queued stats shouldn't be cleared */ 387ea25da48SPaolo Valente blkg_rwstat_add_aux(&to->merged, &from->merged); 388ea25da48SPaolo Valente blkg_rwstat_add_aux(&to->service_time, &from->service_time); 389ea25da48SPaolo Valente blkg_rwstat_add_aux(&to->wait_time, &from->wait_time); 390c0ce79dcSChristoph Hellwig bfq_stat_add_aux(&from->time, &from->time); 391c0ce79dcSChristoph Hellwig bfq_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum); 392c0ce79dcSChristoph Hellwig bfq_stat_add_aux(&to->avg_queue_size_samples, 393ea25da48SPaolo Valente &from->avg_queue_size_samples); 394c0ce79dcSChristoph Hellwig bfq_stat_add_aux(&to->dequeue, &from->dequeue); 395c0ce79dcSChristoph Hellwig bfq_stat_add_aux(&to->group_wait_time, &from->group_wait_time); 396c0ce79dcSChristoph Hellwig bfq_stat_add_aux(&to->idle_time, &from->idle_time); 397c0ce79dcSChristoph Hellwig bfq_stat_add_aux(&to->empty_time, &from->empty_time); 398a33801e8SLuca Miccio #endif 399ea25da48SPaolo Valente } 400ea25da48SPaolo Valente 401ea25da48SPaolo Valente /* 402ea25da48SPaolo Valente * Transfer @bfqg's stats to its parent's aux counts so that the ancestors' 403ea25da48SPaolo Valente * recursive stats can still account for the amount used by this bfqg after 404ea25da48SPaolo Valente * it's gone. 405ea25da48SPaolo Valente */ 406ea25da48SPaolo Valente static void bfqg_stats_xfer_dead(struct bfq_group *bfqg) 407ea25da48SPaolo Valente { 408ea25da48SPaolo Valente struct bfq_group *parent; 409ea25da48SPaolo Valente 410ea25da48SPaolo Valente if (!bfqg) /* root_group */ 411ea25da48SPaolo Valente return; 412ea25da48SPaolo Valente 413ea25da48SPaolo Valente parent = bfqg_parent(bfqg); 414ea25da48SPaolo Valente 4150d945c1fSChristoph Hellwig lockdep_assert_held(&bfqg_to_blkg(bfqg)->q->queue_lock); 416ea25da48SPaolo Valente 417ea25da48SPaolo Valente if (unlikely(!parent)) 418ea25da48SPaolo Valente return; 419ea25da48SPaolo Valente 420ea25da48SPaolo Valente bfqg_stats_add_aux(&parent->stats, &bfqg->stats); 421ea25da48SPaolo Valente bfqg_stats_reset(&bfqg->stats); 422ea25da48SPaolo Valente } 423ea25da48SPaolo Valente 424ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg) 425ea25da48SPaolo Valente { 426ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); 427ea25da48SPaolo Valente 428ea25da48SPaolo Valente entity->weight = entity->new_weight; 429ea25da48SPaolo Valente entity->orig_weight = entity->new_weight; 430ea25da48SPaolo Valente if (bfqq) { 431ea25da48SPaolo Valente bfqq->ioprio = bfqq->new_ioprio; 432ea25da48SPaolo Valente bfqq->ioprio_class = bfqq->new_ioprio_class; 4338f9bebc3SPaolo Valente /* 4348f9bebc3SPaolo Valente * Make sure that bfqg and its associated blkg do not 4358f9bebc3SPaolo Valente * disappear before entity. 4368f9bebc3SPaolo Valente */ 4378f9bebc3SPaolo Valente bfqg_and_blkg_get(bfqg); 438ea25da48SPaolo Valente } 439ea25da48SPaolo Valente entity->parent = bfqg->my_entity; /* NULL for root group */ 440ea25da48SPaolo Valente entity->sched_data = &bfqg->sched_data; 441ea25da48SPaolo Valente } 442ea25da48SPaolo Valente 443ea25da48SPaolo Valente static void bfqg_stats_exit(struct bfqg_stats *stats) 444ea25da48SPaolo Valente { 445fd41e603STejun Heo blkg_rwstat_exit(&stats->bytes); 446fd41e603STejun Heo blkg_rwstat_exit(&stats->ios); 4478060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG 448ea25da48SPaolo Valente blkg_rwstat_exit(&stats->merged); 449ea25da48SPaolo Valente blkg_rwstat_exit(&stats->service_time); 450ea25da48SPaolo Valente blkg_rwstat_exit(&stats->wait_time); 451ea25da48SPaolo Valente blkg_rwstat_exit(&stats->queued); 452c0ce79dcSChristoph Hellwig bfq_stat_exit(&stats->time); 453c0ce79dcSChristoph Hellwig bfq_stat_exit(&stats->avg_queue_size_sum); 454c0ce79dcSChristoph Hellwig bfq_stat_exit(&stats->avg_queue_size_samples); 455c0ce79dcSChristoph Hellwig bfq_stat_exit(&stats->dequeue); 456c0ce79dcSChristoph Hellwig bfq_stat_exit(&stats->group_wait_time); 457c0ce79dcSChristoph Hellwig bfq_stat_exit(&stats->idle_time); 458c0ce79dcSChristoph Hellwig bfq_stat_exit(&stats->empty_time); 459a33801e8SLuca Miccio #endif 460ea25da48SPaolo Valente } 461ea25da48SPaolo Valente 462ea25da48SPaolo Valente static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp) 463ea25da48SPaolo Valente { 464fd41e603STejun Heo if (blkg_rwstat_init(&stats->bytes, gfp) || 465fd41e603STejun Heo blkg_rwstat_init(&stats->ios, gfp)) 466fd41e603STejun Heo return -ENOMEM; 467fd41e603STejun Heo 4688060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG 469ea25da48SPaolo Valente if (blkg_rwstat_init(&stats->merged, gfp) || 470ea25da48SPaolo Valente blkg_rwstat_init(&stats->service_time, gfp) || 471ea25da48SPaolo Valente blkg_rwstat_init(&stats->wait_time, gfp) || 472ea25da48SPaolo Valente blkg_rwstat_init(&stats->queued, gfp) || 473c0ce79dcSChristoph Hellwig bfq_stat_init(&stats->time, gfp) || 474c0ce79dcSChristoph Hellwig bfq_stat_init(&stats->avg_queue_size_sum, gfp) || 475c0ce79dcSChristoph Hellwig bfq_stat_init(&stats->avg_queue_size_samples, gfp) || 476c0ce79dcSChristoph Hellwig bfq_stat_init(&stats->dequeue, gfp) || 477c0ce79dcSChristoph Hellwig bfq_stat_init(&stats->group_wait_time, gfp) || 478c0ce79dcSChristoph Hellwig bfq_stat_init(&stats->idle_time, gfp) || 479c0ce79dcSChristoph Hellwig bfq_stat_init(&stats->empty_time, gfp)) { 480ea25da48SPaolo Valente bfqg_stats_exit(stats); 481ea25da48SPaolo Valente return -ENOMEM; 482ea25da48SPaolo Valente } 483a33801e8SLuca Miccio #endif 484ea25da48SPaolo Valente 485ea25da48SPaolo Valente return 0; 486ea25da48SPaolo Valente } 487ea25da48SPaolo Valente 488ea25da48SPaolo Valente static struct bfq_group_data *cpd_to_bfqgd(struct blkcg_policy_data *cpd) 489ea25da48SPaolo Valente { 490ea25da48SPaolo Valente return cpd ? container_of(cpd, struct bfq_group_data, pd) : NULL; 491ea25da48SPaolo Valente } 492ea25da48SPaolo Valente 493ea25da48SPaolo Valente static struct bfq_group_data *blkcg_to_bfqgd(struct blkcg *blkcg) 494ea25da48SPaolo Valente { 495ea25da48SPaolo Valente return cpd_to_bfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_bfq)); 496ea25da48SPaolo Valente } 497ea25da48SPaolo Valente 498dfb79af5SBart Van Assche static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp) 499ea25da48SPaolo Valente { 500ea25da48SPaolo Valente struct bfq_group_data *bgd; 501ea25da48SPaolo Valente 502ea25da48SPaolo Valente bgd = kzalloc(sizeof(*bgd), gfp); 503ea25da48SPaolo Valente if (!bgd) 504ea25da48SPaolo Valente return NULL; 505ea25da48SPaolo Valente return &bgd->pd; 506ea25da48SPaolo Valente } 507ea25da48SPaolo Valente 508dfb79af5SBart Van Assche static void bfq_cpd_init(struct blkcg_policy_data *cpd) 509ea25da48SPaolo Valente { 510ea25da48SPaolo Valente struct bfq_group_data *d = cpd_to_bfqgd(cpd); 511ea25da48SPaolo Valente 512ea25da48SPaolo Valente d->weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ? 513ea25da48SPaolo Valente CGROUP_WEIGHT_DFL : BFQ_WEIGHT_LEGACY_DFL; 514ea25da48SPaolo Valente } 515ea25da48SPaolo Valente 516dfb79af5SBart Van Assche static void bfq_cpd_free(struct blkcg_policy_data *cpd) 517ea25da48SPaolo Valente { 518ea25da48SPaolo Valente kfree(cpd_to_bfqgd(cpd)); 519ea25da48SPaolo Valente } 520ea25da48SPaolo Valente 521cf09a8eeSTejun Heo static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, struct request_queue *q, 522cf09a8eeSTejun Heo struct blkcg *blkcg) 523ea25da48SPaolo Valente { 524ea25da48SPaolo Valente struct bfq_group *bfqg; 525ea25da48SPaolo Valente 526cf09a8eeSTejun Heo bfqg = kzalloc_node(sizeof(*bfqg), gfp, q->node); 527ea25da48SPaolo Valente if (!bfqg) 528ea25da48SPaolo Valente return NULL; 529ea25da48SPaolo Valente 530ea25da48SPaolo Valente if (bfqg_stats_init(&bfqg->stats, gfp)) { 531ea25da48SPaolo Valente kfree(bfqg); 532ea25da48SPaolo Valente return NULL; 533ea25da48SPaolo Valente } 534ea25da48SPaolo Valente 5358f9bebc3SPaolo Valente /* see comments in bfq_bic_update_cgroup for why refcounting */ 5368f9bebc3SPaolo Valente bfqg_get(bfqg); 537ea25da48SPaolo Valente return &bfqg->pd; 538ea25da48SPaolo Valente } 539ea25da48SPaolo Valente 540dfb79af5SBart Van Assche static void bfq_pd_init(struct blkg_policy_data *pd) 541ea25da48SPaolo Valente { 542ea25da48SPaolo Valente struct blkcg_gq *blkg = pd_to_blkg(pd); 543ea25da48SPaolo Valente struct bfq_group *bfqg = blkg_to_bfqg(blkg); 544ea25da48SPaolo Valente struct bfq_data *bfqd = blkg->q->elevator->elevator_data; 545ea25da48SPaolo Valente struct bfq_entity *entity = &bfqg->entity; 546ea25da48SPaolo Valente struct bfq_group_data *d = blkcg_to_bfqgd(blkg->blkcg); 547ea25da48SPaolo Valente 548ea25da48SPaolo Valente entity->orig_weight = entity->weight = entity->new_weight = d->weight; 549ea25da48SPaolo Valente entity->my_sched_data = &bfqg->sched_data; 550ea25da48SPaolo Valente bfqg->my_entity = entity; /* 551ea25da48SPaolo Valente * the root_group's will be set to NULL 552ea25da48SPaolo Valente * in bfq_init_queue() 553ea25da48SPaolo Valente */ 554ea25da48SPaolo Valente bfqg->bfqd = bfqd; 555ea25da48SPaolo Valente bfqg->active_entities = 0; 556ea25da48SPaolo Valente bfqg->rq_pos_tree = RB_ROOT; 557ea25da48SPaolo Valente } 558ea25da48SPaolo Valente 559dfb79af5SBart Van Assche static void bfq_pd_free(struct blkg_policy_data *pd) 560ea25da48SPaolo Valente { 561ea25da48SPaolo Valente struct bfq_group *bfqg = pd_to_bfqg(pd); 562ea25da48SPaolo Valente 563ea25da48SPaolo Valente bfqg_stats_exit(&bfqg->stats); 5648f9bebc3SPaolo Valente bfqg_put(bfqg); 565ea25da48SPaolo Valente } 566ea25da48SPaolo Valente 567dfb79af5SBart Van Assche static void bfq_pd_reset_stats(struct blkg_policy_data *pd) 568ea25da48SPaolo Valente { 569ea25da48SPaolo Valente struct bfq_group *bfqg = pd_to_bfqg(pd); 570ea25da48SPaolo Valente 571ea25da48SPaolo Valente bfqg_stats_reset(&bfqg->stats); 572ea25da48SPaolo Valente } 573ea25da48SPaolo Valente 574ea25da48SPaolo Valente static void bfq_group_set_parent(struct bfq_group *bfqg, 575ea25da48SPaolo Valente struct bfq_group *parent) 576ea25da48SPaolo Valente { 577ea25da48SPaolo Valente struct bfq_entity *entity; 578ea25da48SPaolo Valente 579ea25da48SPaolo Valente entity = &bfqg->entity; 580ea25da48SPaolo Valente entity->parent = parent->my_entity; 581ea25da48SPaolo Valente entity->sched_data = &parent->sched_data; 582ea25da48SPaolo Valente } 583ea25da48SPaolo Valente 584ea25da48SPaolo Valente static struct bfq_group *bfq_lookup_bfqg(struct bfq_data *bfqd, 585ea25da48SPaolo Valente struct blkcg *blkcg) 586ea25da48SPaolo Valente { 587ea25da48SPaolo Valente struct blkcg_gq *blkg; 588ea25da48SPaolo Valente 589ea25da48SPaolo Valente blkg = blkg_lookup(blkcg, bfqd->queue); 590ea25da48SPaolo Valente if (likely(blkg)) 591ea25da48SPaolo Valente return blkg_to_bfqg(blkg); 592ea25da48SPaolo Valente return NULL; 593ea25da48SPaolo Valente } 594ea25da48SPaolo Valente 595ea25da48SPaolo Valente struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, 596ea25da48SPaolo Valente struct blkcg *blkcg) 597ea25da48SPaolo Valente { 598ea25da48SPaolo Valente struct bfq_group *bfqg, *parent; 599ea25da48SPaolo Valente struct bfq_entity *entity; 600ea25da48SPaolo Valente 601ea25da48SPaolo Valente bfqg = bfq_lookup_bfqg(bfqd, blkcg); 602ea25da48SPaolo Valente 603ea25da48SPaolo Valente if (unlikely(!bfqg)) 604ea25da48SPaolo Valente return NULL; 605ea25da48SPaolo Valente 606ea25da48SPaolo Valente /* 607ea25da48SPaolo Valente * Update chain of bfq_groups as we might be handling a leaf group 608ea25da48SPaolo Valente * which, along with some of its relatives, has not been hooked yet 609ea25da48SPaolo Valente * to the private hierarchy of BFQ. 610ea25da48SPaolo Valente */ 611ea25da48SPaolo Valente entity = &bfqg->entity; 612ea25da48SPaolo Valente for_each_entity(entity) { 61314afc593SCarlo Nonato struct bfq_group *curr_bfqg = container_of(entity, 61414afc593SCarlo Nonato struct bfq_group, entity); 61514afc593SCarlo Nonato if (curr_bfqg != bfqd->root_group) { 61614afc593SCarlo Nonato parent = bfqg_parent(curr_bfqg); 617ea25da48SPaolo Valente if (!parent) 618ea25da48SPaolo Valente parent = bfqd->root_group; 61914afc593SCarlo Nonato bfq_group_set_parent(curr_bfqg, parent); 620ea25da48SPaolo Valente } 621ea25da48SPaolo Valente } 622ea25da48SPaolo Valente 623ea25da48SPaolo Valente return bfqg; 624ea25da48SPaolo Valente } 625ea25da48SPaolo Valente 626ea25da48SPaolo Valente /** 627ea25da48SPaolo Valente * bfq_bfqq_move - migrate @bfqq to @bfqg. 628ea25da48SPaolo Valente * @bfqd: queue descriptor. 629ea25da48SPaolo Valente * @bfqq: the queue to move. 630ea25da48SPaolo Valente * @bfqg: the group to move to. 631ea25da48SPaolo Valente * 632ea25da48SPaolo Valente * Move @bfqq to @bfqg, deactivating it from its old group and reactivating 633ea25da48SPaolo Valente * it on the new one. Avoid putting the entity on the old group idle tree. 634ea25da48SPaolo Valente * 6358f9bebc3SPaolo Valente * Must be called under the scheduler lock, to make sure that the blkg 6368f9bebc3SPaolo Valente * owning @bfqg does not disappear (see comments in 6378f9bebc3SPaolo Valente * bfq_bic_update_cgroup on guaranteeing the consistency of blkg 6388f9bebc3SPaolo Valente * objects). 639ea25da48SPaolo Valente */ 640ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq, 641ea25da48SPaolo Valente struct bfq_group *bfqg) 642ea25da48SPaolo Valente { 643ea25da48SPaolo Valente struct bfq_entity *entity = &bfqq->entity; 644ea25da48SPaolo Valente 645ea25da48SPaolo Valente /* If bfqq is empty, then bfq_bfqq_expire also invokes 646ea25da48SPaolo Valente * bfq_del_bfqq_busy, thereby removing bfqq and its entity 647ea25da48SPaolo Valente * from data structures related to current group. Otherwise we 648ea25da48SPaolo Valente * need to remove bfqq explicitly with bfq_deactivate_bfqq, as 649ea25da48SPaolo Valente * we do below. 650ea25da48SPaolo Valente */ 651ea25da48SPaolo Valente if (bfqq == bfqd->in_service_queue) 652ea25da48SPaolo Valente bfq_bfqq_expire(bfqd, bfqd->in_service_queue, 653ea25da48SPaolo Valente false, BFQQE_PREEMPTED); 654ea25da48SPaolo Valente 655ecedd3d7SPaolo Valente /* 656ecedd3d7SPaolo Valente * get extra reference to prevent bfqq from being freed in 657ecedd3d7SPaolo Valente * next possible deactivate 658ecedd3d7SPaolo Valente */ 659ecedd3d7SPaolo Valente bfqq->ref++; 660ecedd3d7SPaolo Valente 661ea25da48SPaolo Valente if (bfq_bfqq_busy(bfqq)) 662ea25da48SPaolo Valente bfq_deactivate_bfqq(bfqd, bfqq, false, false); 66333a16a98SPaolo Valente else if (entity->on_st_or_in_serv) 664ea25da48SPaolo Valente bfq_put_idle_entity(bfq_entity_service_tree(entity), entity); 6658f9bebc3SPaolo Valente bfqg_and_blkg_put(bfqq_group(bfqq)); 666ea25da48SPaolo Valente 667ea25da48SPaolo Valente entity->parent = bfqg->my_entity; 668ea25da48SPaolo Valente entity->sched_data = &bfqg->sched_data; 6698f9bebc3SPaolo Valente /* pin down bfqg and its associated blkg */ 6708f9bebc3SPaolo Valente bfqg_and_blkg_get(bfqg); 671ea25da48SPaolo Valente 672ea25da48SPaolo Valente if (bfq_bfqq_busy(bfqq)) { 6738cacc5abSPaolo Valente if (unlikely(!bfqd->nonrot_with_queueing)) 674ea25da48SPaolo Valente bfq_pos_tree_add_move(bfqd, bfqq); 675ea25da48SPaolo Valente bfq_activate_bfqq(bfqd, bfqq); 676ea25da48SPaolo Valente } 677ea25da48SPaolo Valente 678ea25da48SPaolo Valente if (!bfqd->in_service_queue && !bfqd->rq_in_driver) 679ea25da48SPaolo Valente bfq_schedule_dispatch(bfqd); 680ecedd3d7SPaolo Valente /* release extra ref taken above */ 681ecedd3d7SPaolo Valente bfq_put_queue(bfqq); 682ea25da48SPaolo Valente } 683ea25da48SPaolo Valente 684ea25da48SPaolo Valente /** 685ea25da48SPaolo Valente * __bfq_bic_change_cgroup - move @bic to @cgroup. 686ea25da48SPaolo Valente * @bfqd: the queue descriptor. 687ea25da48SPaolo Valente * @bic: the bic to move. 688ea25da48SPaolo Valente * @blkcg: the blk-cgroup to move to. 689ea25da48SPaolo Valente * 6908f9bebc3SPaolo Valente * Move bic to blkcg, assuming that bfqd->lock is held; which makes 6918f9bebc3SPaolo Valente * sure that the reference to cgroup is valid across the call (see 6928f9bebc3SPaolo Valente * comments in bfq_bic_update_cgroup on this issue) 693ea25da48SPaolo Valente * 694ea25da48SPaolo Valente * NOTE: an alternative approach might have been to store the current 695ea25da48SPaolo Valente * cgroup in bfqq and getting a reference to it, reducing the lookup 696ea25da48SPaolo Valente * time here, at the price of slightly more complex code. 697ea25da48SPaolo Valente */ 698ea25da48SPaolo Valente static struct bfq_group *__bfq_bic_change_cgroup(struct bfq_data *bfqd, 699ea25da48SPaolo Valente struct bfq_io_cq *bic, 700ea25da48SPaolo Valente struct blkcg *blkcg) 701ea25da48SPaolo Valente { 702ea25da48SPaolo Valente struct bfq_queue *async_bfqq = bic_to_bfqq(bic, 0); 703ea25da48SPaolo Valente struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, 1); 704ea25da48SPaolo Valente struct bfq_group *bfqg; 705ea25da48SPaolo Valente struct bfq_entity *entity; 706ea25da48SPaolo Valente 707ea25da48SPaolo Valente bfqg = bfq_find_set_group(bfqd, blkcg); 708ea25da48SPaolo Valente 709ea25da48SPaolo Valente if (unlikely(!bfqg)) 710ea25da48SPaolo Valente bfqg = bfqd->root_group; 711ea25da48SPaolo Valente 712ea25da48SPaolo Valente if (async_bfqq) { 713ea25da48SPaolo Valente entity = &async_bfqq->entity; 714ea25da48SPaolo Valente 715ea25da48SPaolo Valente if (entity->sched_data != &bfqg->sched_data) { 716ea25da48SPaolo Valente bic_set_bfqq(bic, NULL, 0); 717ea25da48SPaolo Valente bfq_log_bfqq(bfqd, async_bfqq, 718ea25da48SPaolo Valente "bic_change_group: %p %d", 719ea25da48SPaolo Valente async_bfqq, async_bfqq->ref); 720ea25da48SPaolo Valente bfq_put_queue(async_bfqq); 721ea25da48SPaolo Valente } 722ea25da48SPaolo Valente } 723ea25da48SPaolo Valente 724ea25da48SPaolo Valente if (sync_bfqq) { 725ea25da48SPaolo Valente entity = &sync_bfqq->entity; 726ea25da48SPaolo Valente if (entity->sched_data != &bfqg->sched_data) 727ea25da48SPaolo Valente bfq_bfqq_move(bfqd, sync_bfqq, bfqg); 728ea25da48SPaolo Valente } 729ea25da48SPaolo Valente 730ea25da48SPaolo Valente return bfqg; 731ea25da48SPaolo Valente } 732ea25da48SPaolo Valente 733ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) 734ea25da48SPaolo Valente { 735ea25da48SPaolo Valente struct bfq_data *bfqd = bic_to_bfqd(bic); 736ea25da48SPaolo Valente struct bfq_group *bfqg = NULL; 737ea25da48SPaolo Valente uint64_t serial_nr; 738ea25da48SPaolo Valente 739ea25da48SPaolo Valente rcu_read_lock(); 7400fe061b9SDennis Zhou serial_nr = __bio_blkcg(bio)->css.serial_nr; 741ea25da48SPaolo Valente 742ea25da48SPaolo Valente /* 743ea25da48SPaolo Valente * Check whether blkcg has changed. The condition may trigger 744ea25da48SPaolo Valente * spuriously on a newly created cic but there's no harm. 745ea25da48SPaolo Valente */ 746ea25da48SPaolo Valente if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr)) 747ea25da48SPaolo Valente goto out; 748ea25da48SPaolo Valente 7490fe061b9SDennis Zhou bfqg = __bfq_bic_change_cgroup(bfqd, bic, __bio_blkcg(bio)); 7508f9bebc3SPaolo Valente /* 7518f9bebc3SPaolo Valente * Update blkg_path for bfq_log_* functions. We cache this 7528f9bebc3SPaolo Valente * path, and update it here, for the following 7538f9bebc3SPaolo Valente * reasons. Operations on blkg objects in blk-cgroup are 7548f9bebc3SPaolo Valente * protected with the request_queue lock, and not with the 7558f9bebc3SPaolo Valente * lock that protects the instances of this scheduler 7568f9bebc3SPaolo Valente * (bfqd->lock). This exposes BFQ to the following sort of 7578f9bebc3SPaolo Valente * race. 7588f9bebc3SPaolo Valente * 7598f9bebc3SPaolo Valente * The blkg_lookup performed in bfq_get_queue, protected 7608f9bebc3SPaolo Valente * through rcu, may happen to return the address of a copy of 7618f9bebc3SPaolo Valente * the original blkg. If this is the case, then the 7628f9bebc3SPaolo Valente * bfqg_and_blkg_get performed in bfq_get_queue, to pin down 7638f9bebc3SPaolo Valente * the blkg, is useless: it does not prevent blk-cgroup code 7648f9bebc3SPaolo Valente * from destroying both the original blkg and all objects 7658f9bebc3SPaolo Valente * directly or indirectly referred by the copy of the 7668f9bebc3SPaolo Valente * blkg. 7678f9bebc3SPaolo Valente * 7688f9bebc3SPaolo Valente * On the bright side, destroy operations on a blkg invoke, as 7698f9bebc3SPaolo Valente * a first step, hooks of the scheduler associated with the 7708f9bebc3SPaolo Valente * blkg. And these hooks are executed with bfqd->lock held for 7718f9bebc3SPaolo Valente * BFQ. As a consequence, for any blkg associated with the 7728f9bebc3SPaolo Valente * request queue this instance of the scheduler is attached 7738f9bebc3SPaolo Valente * to, we are guaranteed that such a blkg is not destroyed, and 7748f9bebc3SPaolo Valente * that all the pointers it contains are consistent, while we 7758f9bebc3SPaolo Valente * are holding bfqd->lock. A blkg_lookup performed with 7768f9bebc3SPaolo Valente * bfqd->lock held then returns a fully consistent blkg, which 7778f9bebc3SPaolo Valente * remains consistent until this lock is held. 7788f9bebc3SPaolo Valente * 7798f9bebc3SPaolo Valente * Thanks to the last fact, and to the fact that: (1) bfqg has 7808f9bebc3SPaolo Valente * been obtained through a blkg_lookup in the above 7818f9bebc3SPaolo Valente * assignment, and (2) bfqd->lock is being held, here we can 7828f9bebc3SPaolo Valente * safely use the policy data for the involved blkg (i.e., the 7838f9bebc3SPaolo Valente * field bfqg->pd) to get to the blkg associated with bfqg, 7848f9bebc3SPaolo Valente * and then we can safely use any field of blkg. After we 7858f9bebc3SPaolo Valente * release bfqd->lock, even just getting blkg through this 7868f9bebc3SPaolo Valente * bfqg may cause dangling references to be traversed, as 7878f9bebc3SPaolo Valente * bfqg->pd may not exist any more. 7888f9bebc3SPaolo Valente * 7898f9bebc3SPaolo Valente * In view of the above facts, here we cache, in the bfqg, any 7908f9bebc3SPaolo Valente * blkg data we may need for this bic, and for its associated 7918f9bebc3SPaolo Valente * bfq_queue. As of now, we need to cache only the path of the 7928f9bebc3SPaolo Valente * blkg, which is used in the bfq_log_* functions. 7938f9bebc3SPaolo Valente * 7948f9bebc3SPaolo Valente * Finally, note that bfqg itself needs to be protected from 7958f9bebc3SPaolo Valente * destruction on the blkg_free of the original blkg (which 7968f9bebc3SPaolo Valente * invokes bfq_pd_free). We use an additional private 7978f9bebc3SPaolo Valente * refcounter for bfqg, to let it disappear only after no 7988f9bebc3SPaolo Valente * bfq_queue refers to it any longer. 7998f9bebc3SPaolo Valente */ 8008f9bebc3SPaolo Valente blkg_path(bfqg_to_blkg(bfqg), bfqg->blkg_path, sizeof(bfqg->blkg_path)); 801ea25da48SPaolo Valente bic->blkcg_serial_nr = serial_nr; 802ea25da48SPaolo Valente out: 803ea25da48SPaolo Valente rcu_read_unlock(); 804ea25da48SPaolo Valente } 805ea25da48SPaolo Valente 806ea25da48SPaolo Valente /** 807ea25da48SPaolo Valente * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st. 808ea25da48SPaolo Valente * @st: the service tree being flushed. 809ea25da48SPaolo Valente */ 810ea25da48SPaolo Valente static void bfq_flush_idle_tree(struct bfq_service_tree *st) 811ea25da48SPaolo Valente { 812ea25da48SPaolo Valente struct bfq_entity *entity = st->first_idle; 813ea25da48SPaolo Valente 814ea25da48SPaolo Valente for (; entity ; entity = st->first_idle) 815ea25da48SPaolo Valente __bfq_deactivate_entity(entity, false); 816ea25da48SPaolo Valente } 817ea25da48SPaolo Valente 818ea25da48SPaolo Valente /** 819ea25da48SPaolo Valente * bfq_reparent_leaf_entity - move leaf entity to the root_group. 820ea25da48SPaolo Valente * @bfqd: the device data structure with the root group. 821ea25da48SPaolo Valente * @entity: the entity to move. 822ea25da48SPaolo Valente */ 823ea25da48SPaolo Valente static void bfq_reparent_leaf_entity(struct bfq_data *bfqd, 824ea25da48SPaolo Valente struct bfq_entity *entity) 825ea25da48SPaolo Valente { 826ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); 827ea25da48SPaolo Valente 828ea25da48SPaolo Valente bfq_bfqq_move(bfqd, bfqq, bfqd->root_group); 829ea25da48SPaolo Valente } 830ea25da48SPaolo Valente 831ea25da48SPaolo Valente /** 832ea25da48SPaolo Valente * bfq_reparent_active_entities - move to the root group all active 833ea25da48SPaolo Valente * entities. 834ea25da48SPaolo Valente * @bfqd: the device data structure with the root group. 835ea25da48SPaolo Valente * @bfqg: the group to move from. 836ea25da48SPaolo Valente * @st: the service tree with the entities. 837ea25da48SPaolo Valente */ 838ea25da48SPaolo Valente static void bfq_reparent_active_entities(struct bfq_data *bfqd, 839ea25da48SPaolo Valente struct bfq_group *bfqg, 840ea25da48SPaolo Valente struct bfq_service_tree *st) 841ea25da48SPaolo Valente { 842ea25da48SPaolo Valente struct rb_root *active = &st->active; 843ea25da48SPaolo Valente struct bfq_entity *entity = NULL; 844ea25da48SPaolo Valente 845ea25da48SPaolo Valente if (!RB_EMPTY_ROOT(&st->active)) 846ea25da48SPaolo Valente entity = bfq_entity_of(rb_first(active)); 847ea25da48SPaolo Valente 848ea25da48SPaolo Valente for (; entity ; entity = bfq_entity_of(rb_first(active))) 849ea25da48SPaolo Valente bfq_reparent_leaf_entity(bfqd, entity); 850ea25da48SPaolo Valente 851ea25da48SPaolo Valente if (bfqg->sched_data.in_service_entity) 852ea25da48SPaolo Valente bfq_reparent_leaf_entity(bfqd, 853ea25da48SPaolo Valente bfqg->sched_data.in_service_entity); 854ea25da48SPaolo Valente } 855ea25da48SPaolo Valente 856ea25da48SPaolo Valente /** 857ea25da48SPaolo Valente * bfq_pd_offline - deactivate the entity associated with @pd, 858ea25da48SPaolo Valente * and reparent its children entities. 859ea25da48SPaolo Valente * @pd: descriptor of the policy going offline. 860ea25da48SPaolo Valente * 861ea25da48SPaolo Valente * blkio already grabs the queue_lock for us, so no need to use 862ea25da48SPaolo Valente * RCU-based magic 863ea25da48SPaolo Valente */ 864dfb79af5SBart Van Assche static void bfq_pd_offline(struct blkg_policy_data *pd) 865ea25da48SPaolo Valente { 866ea25da48SPaolo Valente struct bfq_service_tree *st; 867ea25da48SPaolo Valente struct bfq_group *bfqg = pd_to_bfqg(pd); 868ea25da48SPaolo Valente struct bfq_data *bfqd = bfqg->bfqd; 869ea25da48SPaolo Valente struct bfq_entity *entity = bfqg->my_entity; 870ea25da48SPaolo Valente unsigned long flags; 871ea25da48SPaolo Valente int i; 872ea25da48SPaolo Valente 873ea25da48SPaolo Valente spin_lock_irqsave(&bfqd->lock, flags); 87452257ffbSPaolo Valente 87552257ffbSPaolo Valente if (!entity) /* root group */ 87652257ffbSPaolo Valente goto put_async_queues; 87752257ffbSPaolo Valente 878ea25da48SPaolo Valente /* 879ea25da48SPaolo Valente * Empty all service_trees belonging to this group before 880ea25da48SPaolo Valente * deactivating the group itself. 881ea25da48SPaolo Valente */ 882ea25da48SPaolo Valente for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) { 883ea25da48SPaolo Valente st = bfqg->sched_data.service_tree + i; 884ea25da48SPaolo Valente 885ea25da48SPaolo Valente /* 886ea25da48SPaolo Valente * The idle tree may still contain bfq_queues belonging 887ea25da48SPaolo Valente * to exited task because they never migrated to a different 8888f9bebc3SPaolo Valente * cgroup from the one being destroyed now. 889ea25da48SPaolo Valente */ 890ea25da48SPaolo Valente bfq_flush_idle_tree(st); 891ea25da48SPaolo Valente 892ea25da48SPaolo Valente /* 893ea25da48SPaolo Valente * It may happen that some queues are still active 894ea25da48SPaolo Valente * (busy) upon group destruction (if the corresponding 895ea25da48SPaolo Valente * processes have been forced to terminate). We move 896ea25da48SPaolo Valente * all the leaf entities corresponding to these queues 897ea25da48SPaolo Valente * to the root_group. 898ea25da48SPaolo Valente * Also, it may happen that the group has an entity 899ea25da48SPaolo Valente * in service, which is disconnected from the active 900ea25da48SPaolo Valente * tree: it must be moved, too. 901ea25da48SPaolo Valente * There is no need to put the sync queues, as the 902ea25da48SPaolo Valente * scheduler has taken no reference. 903ea25da48SPaolo Valente */ 904ea25da48SPaolo Valente bfq_reparent_active_entities(bfqd, bfqg, st); 905ea25da48SPaolo Valente } 906ea25da48SPaolo Valente 907ea25da48SPaolo Valente __bfq_deactivate_entity(entity, false); 90852257ffbSPaolo Valente 90952257ffbSPaolo Valente put_async_queues: 910ea25da48SPaolo Valente bfq_put_async_queues(bfqd, bfqg); 911ea25da48SPaolo Valente 912ea25da48SPaolo Valente spin_unlock_irqrestore(&bfqd->lock, flags); 913ea25da48SPaolo Valente /* 914ea25da48SPaolo Valente * @blkg is going offline and will be ignored by 915ea25da48SPaolo Valente * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so 916ea25da48SPaolo Valente * that they don't get lost. If IOs complete after this point, the 917ea25da48SPaolo Valente * stats for them will be lost. Oh well... 918ea25da48SPaolo Valente */ 919ea25da48SPaolo Valente bfqg_stats_xfer_dead(bfqg); 920ea25da48SPaolo Valente } 921ea25da48SPaolo Valente 922ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd) 923ea25da48SPaolo Valente { 924ea25da48SPaolo Valente struct blkcg_gq *blkg; 925ea25da48SPaolo Valente 926ea25da48SPaolo Valente list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) { 927ea25da48SPaolo Valente struct bfq_group *bfqg = blkg_to_bfqg(blkg); 928ea25da48SPaolo Valente 929ea25da48SPaolo Valente bfq_end_wr_async_queues(bfqd, bfqg); 930ea25da48SPaolo Valente } 931ea25da48SPaolo Valente bfq_end_wr_async_queues(bfqd, bfqd->root_group); 932ea25da48SPaolo Valente } 933ea25da48SPaolo Valente 934795fe54cSFam Zheng static int bfq_io_show_weight_legacy(struct seq_file *sf, void *v) 935ea25da48SPaolo Valente { 936ea25da48SPaolo Valente struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); 937ea25da48SPaolo Valente struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg); 938ea25da48SPaolo Valente unsigned int val = 0; 939ea25da48SPaolo Valente 940ea25da48SPaolo Valente if (bfqgd) 941ea25da48SPaolo Valente val = bfqgd->weight; 942ea25da48SPaolo Valente 943ea25da48SPaolo Valente seq_printf(sf, "%u\n", val); 944ea25da48SPaolo Valente 945ea25da48SPaolo Valente return 0; 946ea25da48SPaolo Valente } 947ea25da48SPaolo Valente 948795fe54cSFam Zheng static u64 bfqg_prfill_weight_device(struct seq_file *sf, 949795fe54cSFam Zheng struct blkg_policy_data *pd, int off) 950ea25da48SPaolo Valente { 951795fe54cSFam Zheng struct bfq_group *bfqg = pd_to_bfqg(pd); 952795fe54cSFam Zheng 953795fe54cSFam Zheng if (!bfqg->entity.dev_weight) 954795fe54cSFam Zheng return 0; 955795fe54cSFam Zheng return __blkg_prfill_u64(sf, pd, bfqg->entity.dev_weight); 956795fe54cSFam Zheng } 957795fe54cSFam Zheng 958795fe54cSFam Zheng static int bfq_io_show_weight(struct seq_file *sf, void *v) 959795fe54cSFam Zheng { 960795fe54cSFam Zheng struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); 961795fe54cSFam Zheng struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg); 962795fe54cSFam Zheng 963795fe54cSFam Zheng seq_printf(sf, "default %u\n", bfqgd->weight); 964795fe54cSFam Zheng blkcg_print_blkgs(sf, blkcg, bfqg_prfill_weight_device, 965795fe54cSFam Zheng &blkcg_policy_bfq, 0, false); 966795fe54cSFam Zheng return 0; 967795fe54cSFam Zheng } 968795fe54cSFam Zheng 969795fe54cSFam Zheng static void bfq_group_set_weight(struct bfq_group *bfqg, u64 weight, u64 dev_weight) 970795fe54cSFam Zheng { 971795fe54cSFam Zheng weight = dev_weight ?: weight; 972795fe54cSFam Zheng 973795fe54cSFam Zheng bfqg->entity.dev_weight = dev_weight; 974ea25da48SPaolo Valente /* 975ea25da48SPaolo Valente * Setting the prio_changed flag of the entity 976ea25da48SPaolo Valente * to 1 with new_weight == weight would re-set 977ea25da48SPaolo Valente * the value of the weight to its ioprio mapping. 978ea25da48SPaolo Valente * Set the flag only if necessary. 979ea25da48SPaolo Valente */ 9805ff047e3SFam Zheng if ((unsigned short)weight != bfqg->entity.new_weight) { 9815ff047e3SFam Zheng bfqg->entity.new_weight = (unsigned short)weight; 982ea25da48SPaolo Valente /* 983ea25da48SPaolo Valente * Make sure that the above new value has been 984ea25da48SPaolo Valente * stored in bfqg->entity.new_weight before 985ea25da48SPaolo Valente * setting the prio_changed flag. In fact, 986ea25da48SPaolo Valente * this flag may be read asynchronously (in 987ea25da48SPaolo Valente * critical sections protected by a different 988ea25da48SPaolo Valente * lock than that held here), and finding this 989ea25da48SPaolo Valente * flag set may cause the execution of the code 990ea25da48SPaolo Valente * for updating parameters whose value may 991ea25da48SPaolo Valente * depend also on bfqg->entity.new_weight (in 992ea25da48SPaolo Valente * __bfq_entity_update_weight_prio). 993ea25da48SPaolo Valente * This barrier makes sure that the new value 994ea25da48SPaolo Valente * of bfqg->entity.new_weight is correctly 995ea25da48SPaolo Valente * seen in that code. 996ea25da48SPaolo Valente */ 997ea25da48SPaolo Valente smp_wmb(); 998ea25da48SPaolo Valente bfqg->entity.prio_changed = 1; 999ea25da48SPaolo Valente } 1000ea25da48SPaolo Valente } 10015ff047e3SFam Zheng 10025ff047e3SFam Zheng static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css, 10035ff047e3SFam Zheng struct cftype *cftype, 10045ff047e3SFam Zheng u64 val) 10055ff047e3SFam Zheng { 10065ff047e3SFam Zheng struct blkcg *blkcg = css_to_blkcg(css); 10075ff047e3SFam Zheng struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg); 10085ff047e3SFam Zheng struct blkcg_gq *blkg; 10095ff047e3SFam Zheng int ret = -ERANGE; 10105ff047e3SFam Zheng 10115ff047e3SFam Zheng if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT) 10125ff047e3SFam Zheng return ret; 10135ff047e3SFam Zheng 10145ff047e3SFam Zheng ret = 0; 10155ff047e3SFam Zheng spin_lock_irq(&blkcg->lock); 10165ff047e3SFam Zheng bfqgd->weight = (unsigned short)val; 10175ff047e3SFam Zheng hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) { 10185ff047e3SFam Zheng struct bfq_group *bfqg = blkg_to_bfqg(blkg); 10195ff047e3SFam Zheng 10205ff047e3SFam Zheng if (bfqg) 1021795fe54cSFam Zheng bfq_group_set_weight(bfqg, val, 0); 10225ff047e3SFam Zheng } 1023ea25da48SPaolo Valente spin_unlock_irq(&blkcg->lock); 1024ea25da48SPaolo Valente 1025ea25da48SPaolo Valente return ret; 1026ea25da48SPaolo Valente } 1027ea25da48SPaolo Valente 1028795fe54cSFam Zheng static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of, 1029795fe54cSFam Zheng char *buf, size_t nbytes, 1030795fe54cSFam Zheng loff_t off) 1031795fe54cSFam Zheng { 1032795fe54cSFam Zheng int ret; 1033795fe54cSFam Zheng struct blkg_conf_ctx ctx; 1034795fe54cSFam Zheng struct blkcg *blkcg = css_to_blkcg(of_css(of)); 1035795fe54cSFam Zheng struct bfq_group *bfqg; 1036795fe54cSFam Zheng u64 v; 1037795fe54cSFam Zheng 1038795fe54cSFam Zheng ret = blkg_conf_prep(blkcg, &blkcg_policy_bfq, buf, &ctx); 1039795fe54cSFam Zheng if (ret) 1040795fe54cSFam Zheng return ret; 1041795fe54cSFam Zheng 1042795fe54cSFam Zheng if (sscanf(ctx.body, "%llu", &v) == 1) { 1043795fe54cSFam Zheng /* require "default" on dfl */ 1044795fe54cSFam Zheng ret = -ERANGE; 1045795fe54cSFam Zheng if (!v) 1046795fe54cSFam Zheng goto out; 1047795fe54cSFam Zheng } else if (!strcmp(strim(ctx.body), "default")) { 1048795fe54cSFam Zheng v = 0; 1049795fe54cSFam Zheng } else { 1050795fe54cSFam Zheng ret = -EINVAL; 1051795fe54cSFam Zheng goto out; 1052795fe54cSFam Zheng } 1053795fe54cSFam Zheng 1054795fe54cSFam Zheng bfqg = blkg_to_bfqg(ctx.blkg); 1055795fe54cSFam Zheng 1056795fe54cSFam Zheng ret = -ERANGE; 1057795fe54cSFam Zheng if (!v || (v >= BFQ_MIN_WEIGHT && v <= BFQ_MAX_WEIGHT)) { 1058795fe54cSFam Zheng bfq_group_set_weight(bfqg, bfqg->entity.weight, v); 1059795fe54cSFam Zheng ret = 0; 1060795fe54cSFam Zheng } 1061795fe54cSFam Zheng out: 1062795fe54cSFam Zheng blkg_conf_finish(&ctx); 1063795fe54cSFam Zheng return ret ?: nbytes; 1064795fe54cSFam Zheng } 1065795fe54cSFam Zheng 1066ea25da48SPaolo Valente static ssize_t bfq_io_set_weight(struct kernfs_open_file *of, 1067ea25da48SPaolo Valente char *buf, size_t nbytes, 1068ea25da48SPaolo Valente loff_t off) 1069ea25da48SPaolo Valente { 1070795fe54cSFam Zheng char *endp; 1071795fe54cSFam Zheng int ret; 1072795fe54cSFam Zheng u64 v; 1073ea25da48SPaolo Valente 1074795fe54cSFam Zheng buf = strim(buf); 1075ea25da48SPaolo Valente 1076795fe54cSFam Zheng /* "WEIGHT" or "default WEIGHT" sets the default weight */ 1077795fe54cSFam Zheng v = simple_strtoull(buf, &endp, 0); 1078795fe54cSFam Zheng if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) { 1079795fe54cSFam Zheng ret = bfq_io_set_weight_legacy(of_css(of), NULL, v); 1080fc8ebd01SMaciej S. Szmigiero return ret ?: nbytes; 1081ea25da48SPaolo Valente } 1082ea25da48SPaolo Valente 1083795fe54cSFam Zheng return bfq_io_set_device_weight(of, buf, nbytes, off); 1084795fe54cSFam Zheng } 1085795fe54cSFam Zheng 1086ea25da48SPaolo Valente static int bfqg_print_rwstat(struct seq_file *sf, void *v) 1087ea25da48SPaolo Valente { 1088ea25da48SPaolo Valente blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat, 1089ea25da48SPaolo Valente &blkcg_policy_bfq, seq_cft(sf)->private, true); 1090ea25da48SPaolo Valente return 0; 1091ea25da48SPaolo Valente } 1092ea25da48SPaolo Valente 1093a557f1c7STejun Heo static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf, 1094a557f1c7STejun Heo struct blkg_policy_data *pd, int off) 1095a557f1c7STejun Heo { 1096a557f1c7STejun Heo struct blkg_rwstat_sample sum; 1097a557f1c7STejun Heo 1098a557f1c7STejun Heo blkg_rwstat_recursive_sum(pd_to_blkg(pd), &blkcg_policy_bfq, off, &sum); 1099a557f1c7STejun Heo return __blkg_prfill_rwstat(sf, pd, &sum); 1100a557f1c7STejun Heo } 1101a557f1c7STejun Heo 1102a557f1c7STejun Heo static int bfqg_print_rwstat_recursive(struct seq_file *sf, void *v) 1103a557f1c7STejun Heo { 1104a557f1c7STejun Heo blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1105a557f1c7STejun Heo bfqg_prfill_rwstat_recursive, &blkcg_policy_bfq, 1106a557f1c7STejun Heo seq_cft(sf)->private, true); 1107a557f1c7STejun Heo return 0; 1108a557f1c7STejun Heo } 1109a557f1c7STejun Heo 1110fd41e603STejun Heo #ifdef CONFIG_BFQ_CGROUP_DEBUG 1111a557f1c7STejun Heo static int bfqg_print_stat(struct seq_file *sf, void *v) 1112a557f1c7STejun Heo { 1113a557f1c7STejun Heo blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat, 1114a557f1c7STejun Heo &blkcg_policy_bfq, seq_cft(sf)->private, false); 1115a557f1c7STejun Heo return 0; 1116a557f1c7STejun Heo } 1117a557f1c7STejun Heo 1118ea25da48SPaolo Valente static u64 bfqg_prfill_stat_recursive(struct seq_file *sf, 1119ea25da48SPaolo Valente struct blkg_policy_data *pd, int off) 1120ea25da48SPaolo Valente { 1121d6258980SChristoph Hellwig struct blkcg_gq *blkg = pd_to_blkg(pd); 1122d6258980SChristoph Hellwig struct blkcg_gq *pos_blkg; 1123d6258980SChristoph Hellwig struct cgroup_subsys_state *pos_css; 1124d6258980SChristoph Hellwig u64 sum = 0; 1125d6258980SChristoph Hellwig 1126d6258980SChristoph Hellwig lockdep_assert_held(&blkg->q->queue_lock); 1127d6258980SChristoph Hellwig 1128d6258980SChristoph Hellwig rcu_read_lock(); 1129d6258980SChristoph Hellwig blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) { 1130d6258980SChristoph Hellwig struct bfq_stat *stat; 1131d6258980SChristoph Hellwig 1132d6258980SChristoph Hellwig if (!pos_blkg->online) 1133d6258980SChristoph Hellwig continue; 1134d6258980SChristoph Hellwig 1135d6258980SChristoph Hellwig stat = (void *)blkg_to_pd(pos_blkg, &blkcg_policy_bfq) + off; 1136d6258980SChristoph Hellwig sum += bfq_stat_read(stat) + atomic64_read(&stat->aux_cnt); 1137d6258980SChristoph Hellwig } 1138d6258980SChristoph Hellwig rcu_read_unlock(); 1139d6258980SChristoph Hellwig 1140ea25da48SPaolo Valente return __blkg_prfill_u64(sf, pd, sum); 1141ea25da48SPaolo Valente } 1142ea25da48SPaolo Valente 1143ea25da48SPaolo Valente static int bfqg_print_stat_recursive(struct seq_file *sf, void *v) 1144ea25da48SPaolo Valente { 1145ea25da48SPaolo Valente blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1146ea25da48SPaolo Valente bfqg_prfill_stat_recursive, &blkcg_policy_bfq, 1147ea25da48SPaolo Valente seq_cft(sf)->private, false); 1148ea25da48SPaolo Valente return 0; 1149ea25da48SPaolo Valente } 1150ea25da48SPaolo Valente 1151ea25da48SPaolo Valente static u64 bfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd, 1152ea25da48SPaolo Valente int off) 1153ea25da48SPaolo Valente { 1154fd41e603STejun Heo struct bfq_group *bfqg = blkg_to_bfqg(pd->blkg); 1155fd41e603STejun Heo u64 sum = blkg_rwstat_total(&bfqg->stats.bytes); 1156ea25da48SPaolo Valente 1157ea25da48SPaolo Valente return __blkg_prfill_u64(sf, pd, sum >> 9); 1158ea25da48SPaolo Valente } 1159ea25da48SPaolo Valente 1160ea25da48SPaolo Valente static int bfqg_print_stat_sectors(struct seq_file *sf, void *v) 1161ea25da48SPaolo Valente { 1162ea25da48SPaolo Valente blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1163ea25da48SPaolo Valente bfqg_prfill_sectors, &blkcg_policy_bfq, 0, false); 1164ea25da48SPaolo Valente return 0; 1165ea25da48SPaolo Valente } 1166ea25da48SPaolo Valente 1167ea25da48SPaolo Valente static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf, 1168ea25da48SPaolo Valente struct blkg_policy_data *pd, int off) 1169ea25da48SPaolo Valente { 11707af6fd91SChristoph Hellwig struct blkg_rwstat_sample tmp; 11715d0b6e48SChristoph Hellwig 1172fd41e603STejun Heo blkg_rwstat_recursive_sum(pd->blkg, &blkcg_policy_bfq, 1173fd41e603STejun Heo offsetof(struct bfq_group, stats.bytes), &tmp); 1174ea25da48SPaolo Valente 11757af6fd91SChristoph Hellwig return __blkg_prfill_u64(sf, pd, 11767af6fd91SChristoph Hellwig (tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]) >> 9); 1177ea25da48SPaolo Valente } 1178ea25da48SPaolo Valente 1179ea25da48SPaolo Valente static int bfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v) 1180ea25da48SPaolo Valente { 1181ea25da48SPaolo Valente blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1182ea25da48SPaolo Valente bfqg_prfill_sectors_recursive, &blkcg_policy_bfq, 0, 1183ea25da48SPaolo Valente false); 1184ea25da48SPaolo Valente return 0; 1185ea25da48SPaolo Valente } 1186ea25da48SPaolo Valente 1187ea25da48SPaolo Valente static u64 bfqg_prfill_avg_queue_size(struct seq_file *sf, 1188ea25da48SPaolo Valente struct blkg_policy_data *pd, int off) 1189ea25da48SPaolo Valente { 1190ea25da48SPaolo Valente struct bfq_group *bfqg = pd_to_bfqg(pd); 1191c0ce79dcSChristoph Hellwig u64 samples = bfq_stat_read(&bfqg->stats.avg_queue_size_samples); 1192ea25da48SPaolo Valente u64 v = 0; 1193ea25da48SPaolo Valente 1194ea25da48SPaolo Valente if (samples) { 1195c0ce79dcSChristoph Hellwig v = bfq_stat_read(&bfqg->stats.avg_queue_size_sum); 1196ea25da48SPaolo Valente v = div64_u64(v, samples); 1197ea25da48SPaolo Valente } 1198ea25da48SPaolo Valente __blkg_prfill_u64(sf, pd, v); 1199ea25da48SPaolo Valente return 0; 1200ea25da48SPaolo Valente } 1201ea25da48SPaolo Valente 1202ea25da48SPaolo Valente /* print avg_queue_size */ 1203ea25da48SPaolo Valente static int bfqg_print_avg_queue_size(struct seq_file *sf, void *v) 1204ea25da48SPaolo Valente { 1205ea25da48SPaolo Valente blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), 1206ea25da48SPaolo Valente bfqg_prfill_avg_queue_size, &blkcg_policy_bfq, 1207ea25da48SPaolo Valente 0, false); 1208ea25da48SPaolo Valente return 0; 1209ea25da48SPaolo Valente } 12108060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */ 1211ea25da48SPaolo Valente 1212ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node) 1213ea25da48SPaolo Valente { 1214ea25da48SPaolo Valente int ret; 1215ea25da48SPaolo Valente 1216ea25da48SPaolo Valente ret = blkcg_activate_policy(bfqd->queue, &blkcg_policy_bfq); 1217ea25da48SPaolo Valente if (ret) 1218ea25da48SPaolo Valente return NULL; 1219ea25da48SPaolo Valente 1220ea25da48SPaolo Valente return blkg_to_bfqg(bfqd->queue->root_blkg); 1221ea25da48SPaolo Valente } 1222ea25da48SPaolo Valente 1223ea25da48SPaolo Valente struct blkcg_policy blkcg_policy_bfq = { 1224ea25da48SPaolo Valente .dfl_cftypes = bfq_blkg_files, 1225ea25da48SPaolo Valente .legacy_cftypes = bfq_blkcg_legacy_files, 1226ea25da48SPaolo Valente 1227ea25da48SPaolo Valente .cpd_alloc_fn = bfq_cpd_alloc, 1228ea25da48SPaolo Valente .cpd_init_fn = bfq_cpd_init, 1229ea25da48SPaolo Valente .cpd_bind_fn = bfq_cpd_init, 1230ea25da48SPaolo Valente .cpd_free_fn = bfq_cpd_free, 1231ea25da48SPaolo Valente 1232ea25da48SPaolo Valente .pd_alloc_fn = bfq_pd_alloc, 1233ea25da48SPaolo Valente .pd_init_fn = bfq_pd_init, 1234ea25da48SPaolo Valente .pd_offline_fn = bfq_pd_offline, 1235ea25da48SPaolo Valente .pd_free_fn = bfq_pd_free, 1236ea25da48SPaolo Valente .pd_reset_stats_fn = bfq_pd_reset_stats, 1237ea25da48SPaolo Valente }; 1238ea25da48SPaolo Valente 1239ea25da48SPaolo Valente struct cftype bfq_blkcg_legacy_files[] = { 1240ea25da48SPaolo Valente { 1241ea25da48SPaolo Valente .name = "bfq.weight", 1242cf892988SJens Axboe .flags = CFTYPE_NOT_ON_ROOT, 1243795fe54cSFam Zheng .seq_show = bfq_io_show_weight_legacy, 1244ea25da48SPaolo Valente .write_u64 = bfq_io_set_weight_legacy, 1245ea25da48SPaolo Valente }, 1246795fe54cSFam Zheng { 1247795fe54cSFam Zheng .name = "bfq.weight_device", 1248795fe54cSFam Zheng .flags = CFTYPE_NOT_ON_ROOT, 1249795fe54cSFam Zheng .seq_show = bfq_io_show_weight, 1250795fe54cSFam Zheng .write = bfq_io_set_weight, 1251795fe54cSFam Zheng }, 1252ea25da48SPaolo Valente 1253ea25da48SPaolo Valente /* statistics, covers only the tasks in the bfqg */ 1254ea25da48SPaolo Valente { 1255ea25da48SPaolo Valente .name = "bfq.io_service_bytes", 1256fd41e603STejun Heo .private = offsetof(struct bfq_group, stats.bytes), 1257fd41e603STejun Heo .seq_show = bfqg_print_rwstat, 1258ea25da48SPaolo Valente }, 1259ea25da48SPaolo Valente { 1260ea25da48SPaolo Valente .name = "bfq.io_serviced", 1261fd41e603STejun Heo .private = offsetof(struct bfq_group, stats.ios), 1262fd41e603STejun Heo .seq_show = bfqg_print_rwstat, 1263ea25da48SPaolo Valente }, 12648060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG 1265a33801e8SLuca Miccio { 1266a33801e8SLuca Miccio .name = "bfq.time", 1267a33801e8SLuca Miccio .private = offsetof(struct bfq_group, stats.time), 1268a33801e8SLuca Miccio .seq_show = bfqg_print_stat, 1269a33801e8SLuca Miccio }, 1270a33801e8SLuca Miccio { 1271a33801e8SLuca Miccio .name = "bfq.sectors", 1272a33801e8SLuca Miccio .seq_show = bfqg_print_stat_sectors, 1273a33801e8SLuca Miccio }, 1274ea25da48SPaolo Valente { 1275ea25da48SPaolo Valente .name = "bfq.io_service_time", 1276ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.service_time), 1277ea25da48SPaolo Valente .seq_show = bfqg_print_rwstat, 1278ea25da48SPaolo Valente }, 1279ea25da48SPaolo Valente { 1280ea25da48SPaolo Valente .name = "bfq.io_wait_time", 1281ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.wait_time), 1282ea25da48SPaolo Valente .seq_show = bfqg_print_rwstat, 1283ea25da48SPaolo Valente }, 1284ea25da48SPaolo Valente { 1285ea25da48SPaolo Valente .name = "bfq.io_merged", 1286ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.merged), 1287ea25da48SPaolo Valente .seq_show = bfqg_print_rwstat, 1288ea25da48SPaolo Valente }, 1289ea25da48SPaolo Valente { 1290ea25da48SPaolo Valente .name = "bfq.io_queued", 1291ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.queued), 1292ea25da48SPaolo Valente .seq_show = bfqg_print_rwstat, 1293ea25da48SPaolo Valente }, 12948060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */ 1295ea25da48SPaolo Valente 1296636b8fe8SAngelo Ruocco /* the same statistics which cover the bfqg and its descendants */ 1297ea25da48SPaolo Valente { 1298ea25da48SPaolo Valente .name = "bfq.io_service_bytes_recursive", 1299fd41e603STejun Heo .private = offsetof(struct bfq_group, stats.bytes), 1300fd41e603STejun Heo .seq_show = bfqg_print_rwstat_recursive, 1301ea25da48SPaolo Valente }, 1302ea25da48SPaolo Valente { 1303ea25da48SPaolo Valente .name = "bfq.io_serviced_recursive", 1304fd41e603STejun Heo .private = offsetof(struct bfq_group, stats.ios), 1305fd41e603STejun Heo .seq_show = bfqg_print_rwstat_recursive, 1306ea25da48SPaolo Valente }, 13078060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG 1308a33801e8SLuca Miccio { 1309a33801e8SLuca Miccio .name = "bfq.time_recursive", 1310a33801e8SLuca Miccio .private = offsetof(struct bfq_group, stats.time), 1311a33801e8SLuca Miccio .seq_show = bfqg_print_stat_recursive, 1312a33801e8SLuca Miccio }, 1313a33801e8SLuca Miccio { 1314a33801e8SLuca Miccio .name = "bfq.sectors_recursive", 1315a33801e8SLuca Miccio .seq_show = bfqg_print_stat_sectors_recursive, 1316a33801e8SLuca Miccio }, 1317ea25da48SPaolo Valente { 1318ea25da48SPaolo Valente .name = "bfq.io_service_time_recursive", 1319ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.service_time), 1320ea25da48SPaolo Valente .seq_show = bfqg_print_rwstat_recursive, 1321ea25da48SPaolo Valente }, 1322ea25da48SPaolo Valente { 1323ea25da48SPaolo Valente .name = "bfq.io_wait_time_recursive", 1324ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.wait_time), 1325ea25da48SPaolo Valente .seq_show = bfqg_print_rwstat_recursive, 1326ea25da48SPaolo Valente }, 1327ea25da48SPaolo Valente { 1328ea25da48SPaolo Valente .name = "bfq.io_merged_recursive", 1329ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.merged), 1330ea25da48SPaolo Valente .seq_show = bfqg_print_rwstat_recursive, 1331ea25da48SPaolo Valente }, 1332ea25da48SPaolo Valente { 1333ea25da48SPaolo Valente .name = "bfq.io_queued_recursive", 1334ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.queued), 1335ea25da48SPaolo Valente .seq_show = bfqg_print_rwstat_recursive, 1336ea25da48SPaolo Valente }, 1337ea25da48SPaolo Valente { 1338ea25da48SPaolo Valente .name = "bfq.avg_queue_size", 1339ea25da48SPaolo Valente .seq_show = bfqg_print_avg_queue_size, 1340ea25da48SPaolo Valente }, 1341ea25da48SPaolo Valente { 1342ea25da48SPaolo Valente .name = "bfq.group_wait_time", 1343ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.group_wait_time), 1344ea25da48SPaolo Valente .seq_show = bfqg_print_stat, 1345ea25da48SPaolo Valente }, 1346ea25da48SPaolo Valente { 1347ea25da48SPaolo Valente .name = "bfq.idle_time", 1348ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.idle_time), 1349ea25da48SPaolo Valente .seq_show = bfqg_print_stat, 1350ea25da48SPaolo Valente }, 1351ea25da48SPaolo Valente { 1352ea25da48SPaolo Valente .name = "bfq.empty_time", 1353ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.empty_time), 1354ea25da48SPaolo Valente .seq_show = bfqg_print_stat, 1355ea25da48SPaolo Valente }, 1356ea25da48SPaolo Valente { 1357ea25da48SPaolo Valente .name = "bfq.dequeue", 1358ea25da48SPaolo Valente .private = offsetof(struct bfq_group, stats.dequeue), 1359ea25da48SPaolo Valente .seq_show = bfqg_print_stat, 1360ea25da48SPaolo Valente }, 13618060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */ 1362ea25da48SPaolo Valente { } /* terminate */ 1363ea25da48SPaolo Valente }; 1364ea25da48SPaolo Valente 1365ea25da48SPaolo Valente struct cftype bfq_blkg_files[] = { 1366ea25da48SPaolo Valente { 1367ea25da48SPaolo Valente .name = "bfq.weight", 1368cf892988SJens Axboe .flags = CFTYPE_NOT_ON_ROOT, 1369ea25da48SPaolo Valente .seq_show = bfq_io_show_weight, 1370ea25da48SPaolo Valente .write = bfq_io_set_weight, 1371ea25da48SPaolo Valente }, 1372ea25da48SPaolo Valente {} /* terminate */ 1373ea25da48SPaolo Valente }; 1374ea25da48SPaolo Valente 1375ea25da48SPaolo Valente #else /* CONFIG_BFQ_GROUP_IOSCHED */ 1376ea25da48SPaolo Valente 1377ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq, 1378ea25da48SPaolo Valente struct bfq_group *bfqg) {} 1379ea25da48SPaolo Valente 1380ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg) 1381ea25da48SPaolo Valente { 1382ea25da48SPaolo Valente struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); 1383ea25da48SPaolo Valente 1384ea25da48SPaolo Valente entity->weight = entity->new_weight; 1385ea25da48SPaolo Valente entity->orig_weight = entity->new_weight; 1386ea25da48SPaolo Valente if (bfqq) { 1387ea25da48SPaolo Valente bfqq->ioprio = bfqq->new_ioprio; 1388ea25da48SPaolo Valente bfqq->ioprio_class = bfqq->new_ioprio_class; 1389ea25da48SPaolo Valente } 1390ea25da48SPaolo Valente entity->sched_data = &bfqg->sched_data; 1391ea25da48SPaolo Valente } 1392ea25da48SPaolo Valente 1393ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {} 1394ea25da48SPaolo Valente 1395ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd) 1396ea25da48SPaolo Valente { 1397ea25da48SPaolo Valente bfq_end_wr_async_queues(bfqd, bfqd->root_group); 1398ea25da48SPaolo Valente } 1399ea25da48SPaolo Valente 1400ea25da48SPaolo Valente struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, struct blkcg *blkcg) 1401ea25da48SPaolo Valente { 1402ea25da48SPaolo Valente return bfqd->root_group; 1403ea25da48SPaolo Valente } 1404ea25da48SPaolo Valente 1405ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq) 1406ea25da48SPaolo Valente { 1407ea25da48SPaolo Valente return bfqq->bfqd->root_group; 1408ea25da48SPaolo Valente } 1409ea25da48SPaolo Valente 14104d8340d0SPaolo Valente void bfqg_and_blkg_get(struct bfq_group *bfqg) {} 14114d8340d0SPaolo Valente 14124d8340d0SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg) {} 14134d8340d0SPaolo Valente 1414ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node) 1415ea25da48SPaolo Valente { 1416ea25da48SPaolo Valente struct bfq_group *bfqg; 1417ea25da48SPaolo Valente int i; 1418ea25da48SPaolo Valente 1419ea25da48SPaolo Valente bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node); 1420ea25da48SPaolo Valente if (!bfqg) 1421ea25da48SPaolo Valente return NULL; 1422ea25da48SPaolo Valente 1423ea25da48SPaolo Valente for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) 1424ea25da48SPaolo Valente bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT; 1425ea25da48SPaolo Valente 1426ea25da48SPaolo Valente return bfqg; 1427ea25da48SPaolo Valente } 1428ea25da48SPaolo Valente #endif /* CONFIG_BFQ_GROUP_IOSCHED */ 1429