xref: /openbmc/linux/block/bfq-cgroup.c (revision faffaab2)
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/ktime.h>
10ea25da48SPaolo Valente #include <linux/rbtree.h>
11ea25da48SPaolo Valente #include <linux/ioprio.h>
12ea25da48SPaolo Valente #include <linux/sbitmap.h>
13ea25da48SPaolo Valente #include <linux/delay.h>
14ea25da48SPaolo Valente 
152e9bc346SChristoph Hellwig #include "elevator.h"
16ea25da48SPaolo Valente #include "bfq-iosched.h"
17ea25da48SPaolo Valente 
188060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
bfq_stat_init(struct bfq_stat * stat,gfp_t gfp)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 
bfq_stat_exit(struct bfq_stat * stat)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  */
bfq_stat_add(struct bfq_stat * stat,uint64_t val)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  */
bfq_stat_read(struct bfq_stat * stat)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  */
bfq_stat_reset(struct bfq_stat * stat)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  */
bfq_stat_add_aux(struct bfq_stat * to,struct bfq_stat * from)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  */
blkg_prfill_stat(struct seq_file * sf,struct blkg_policy_data * pd,int off)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)
BFQG_FLAG_FNS(idling)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. */
bfqg_stats_set_start_group_wait_time(struct bfq_group * bfqg,struct bfq_group * curr_bfqg)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. */
bfqg_stats_end_empty_time(struct bfqg_stats * stats)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 
bfqg_stats_update_dequeue(struct bfq_group * bfqg)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 
bfqg_stats_set_start_empty_time(struct bfq_group * bfqg)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 
bfqg_stats_update_idle_time(struct bfq_group * bfqg)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 
bfqg_stats_set_start_idle_time(struct bfq_group * bfqg)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 
bfqg_stats_update_avg_queue_size(struct bfq_group * bfqg)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 
bfqg_stats_update_io_add(struct bfq_group * bfqg,struct bfq_queue * bfqq,blk_opf_t opf)222a33801e8SLuca Miccio void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
223dc469ba2SBart Van Assche 			      blk_opf_t opf)
224a33801e8SLuca Miccio {
225dc469ba2SBart Van Assche 	blkg_rwstat_add(&bfqg->stats.queued, opf, 1);
226a33801e8SLuca Miccio 	bfqg_stats_end_empty_time(&bfqg->stats);
227aa625117SYu Kuai 	if (!(bfqq == bfqg->bfqd->in_service_queue))
228a33801e8SLuca Miccio 		bfqg_stats_set_start_group_wait_time(bfqg, bfqq_group(bfqq));
229a33801e8SLuca Miccio }
230a33801e8SLuca Miccio 
bfqg_stats_update_io_remove(struct bfq_group * bfqg,blk_opf_t opf)231dc469ba2SBart Van Assche void bfqg_stats_update_io_remove(struct bfq_group *bfqg, blk_opf_t opf)
232a33801e8SLuca Miccio {
233dc469ba2SBart Van Assche 	blkg_rwstat_add(&bfqg->stats.queued, opf, -1);
234a33801e8SLuca Miccio }
235a33801e8SLuca Miccio 
bfqg_stats_update_io_merged(struct bfq_group * bfqg,blk_opf_t opf)236dc469ba2SBart Van Assche void bfqg_stats_update_io_merged(struct bfq_group *bfqg, blk_opf_t opf)
237a33801e8SLuca Miccio {
238dc469ba2SBart Van Assche 	blkg_rwstat_add(&bfqg->stats.merged, opf, 1);
239a33801e8SLuca Miccio }
240a33801e8SLuca Miccio 
bfqg_stats_update_completion(struct bfq_group * bfqg,u64 start_time_ns,u64 io_start_time_ns,blk_opf_t opf)24184c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
242dc469ba2SBart Van Assche 				  u64 io_start_time_ns, blk_opf_t opf)
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)
248dc469ba2SBart Van Assche 		blkg_rwstat_add(&stats->service_time, opf,
24984c7afceSOmar Sandoval 				now - io_start_time_ns);
25084c7afceSOmar Sandoval 	if (io_start_time_ns > start_time_ns)
251dc469ba2SBart Van Assche 		blkg_rwstat_add(&stats->wait_time, opf,
25284c7afceSOmar Sandoval 				io_start_time_ns - start_time_ns);
253a33801e8SLuca Miccio }
254a33801e8SLuca Miccio 
2558060c47bSChristoph Hellwig #else /* CONFIG_BFQ_CGROUP_DEBUG */
256a33801e8SLuca Miccio 
bfqg_stats_update_io_remove(struct bfq_group * bfqg,blk_opf_t opf)257dc469ba2SBart Van Assche void bfqg_stats_update_io_remove(struct bfq_group *bfqg, blk_opf_t opf) { }
bfqg_stats_update_io_merged(struct bfq_group * bfqg,blk_opf_t opf)258dc469ba2SBart Van Assche void bfqg_stats_update_io_merged(struct bfq_group *bfqg, blk_opf_t opf) { }
bfqg_stats_update_completion(struct bfq_group * bfqg,u64 start_time_ns,u64 io_start_time_ns,blk_opf_t opf)25984c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
260dc469ba2SBart Van Assche 				  u64 io_start_time_ns, blk_opf_t opf) { }
bfqg_stats_update_dequeue(struct bfq_group * bfqg)261a33801e8SLuca Miccio void bfqg_stats_update_dequeue(struct bfq_group *bfqg) { }
bfqg_stats_set_start_idle_time(struct bfq_group * bfqg)262a33801e8SLuca Miccio void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) { }
263a33801e8SLuca Miccio 
2648060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
265a33801e8SLuca Miccio 
266a33801e8SLuca Miccio #ifdef CONFIG_BFQ_GROUP_IOSCHED
267a33801e8SLuca Miccio 
268ea25da48SPaolo Valente /*
269ea25da48SPaolo Valente  * blk-cgroup policy-related handlers
270ea25da48SPaolo Valente  * The following functions help in converting between blk-cgroup
271ea25da48SPaolo Valente  * internal structures and BFQ-specific structures.
272ea25da48SPaolo Valente  */
273ea25da48SPaolo Valente 
pd_to_bfqg(struct blkg_policy_data * pd)274ea25da48SPaolo Valente static struct bfq_group *pd_to_bfqg(struct blkg_policy_data *pd)
275ea25da48SPaolo Valente {
276ea25da48SPaolo Valente 	return pd ? container_of(pd, struct bfq_group, pd) : NULL;
277ea25da48SPaolo Valente }
278ea25da48SPaolo Valente 
bfqg_to_blkg(struct bfq_group * bfqg)279ea25da48SPaolo Valente struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg)
280ea25da48SPaolo Valente {
281ea25da48SPaolo Valente 	return pd_to_blkg(&bfqg->pd);
282ea25da48SPaolo Valente }
283ea25da48SPaolo Valente 
blkg_to_bfqg(struct blkcg_gq * blkg)284ea25da48SPaolo Valente static struct bfq_group *blkg_to_bfqg(struct blkcg_gq *blkg)
285ea25da48SPaolo Valente {
286ea25da48SPaolo Valente 	return pd_to_bfqg(blkg_to_pd(blkg, &blkcg_policy_bfq));
287ea25da48SPaolo Valente }
288ea25da48SPaolo Valente 
289ea25da48SPaolo Valente /*
290ea25da48SPaolo Valente  * bfq_group handlers
291ea25da48SPaolo Valente  * The following functions help in navigating the bfq_group hierarchy
292ea25da48SPaolo Valente  * by allowing to find the parent of a bfq_group or the bfq_group
293ea25da48SPaolo Valente  * associated to a bfq_queue.
294ea25da48SPaolo Valente  */
295ea25da48SPaolo Valente 
bfqg_parent(struct bfq_group * bfqg)296ea25da48SPaolo Valente static struct bfq_group *bfqg_parent(struct bfq_group *bfqg)
297ea25da48SPaolo Valente {
298ea25da48SPaolo Valente 	struct blkcg_gq *pblkg = bfqg_to_blkg(bfqg)->parent;
299ea25da48SPaolo Valente 
300ea25da48SPaolo Valente 	return pblkg ? blkg_to_bfqg(pblkg) : NULL;
301ea25da48SPaolo Valente }
302ea25da48SPaolo Valente 
bfqq_group(struct bfq_queue * bfqq)303ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
304ea25da48SPaolo Valente {
305ea25da48SPaolo Valente 	struct bfq_entity *group_entity = bfqq->entity.parent;
306ea25da48SPaolo Valente 
307ea25da48SPaolo Valente 	return group_entity ? container_of(group_entity, struct bfq_group,
308ea25da48SPaolo Valente 					   entity) :
309ea25da48SPaolo Valente 			      bfqq->bfqd->root_group;
310ea25da48SPaolo Valente }
311ea25da48SPaolo Valente 
312ea25da48SPaolo Valente /*
313ea25da48SPaolo Valente  * The following two functions handle get and put of a bfq_group by
314ea25da48SPaolo Valente  * wrapping the related blk-cgroup hooks.
315ea25da48SPaolo Valente  */
316ea25da48SPaolo Valente 
bfqg_get(struct bfq_group * bfqg)317ea25da48SPaolo Valente static void bfqg_get(struct bfq_group *bfqg)
318ea25da48SPaolo Valente {
319216f7647SYu Kuai 	refcount_inc(&bfqg->ref);
320ea25da48SPaolo Valente }
321ea25da48SPaolo Valente 
bfqg_put(struct bfq_group * bfqg)322dfb79af5SBart Van Assche static void bfqg_put(struct bfq_group *bfqg)
323ea25da48SPaolo Valente {
324216f7647SYu Kuai 	if (refcount_dec_and_test(&bfqg->ref))
3258f9bebc3SPaolo Valente 		kfree(bfqg);
3268f9bebc3SPaolo Valente }
3278f9bebc3SPaolo Valente 
bfqg_and_blkg_get(struct bfq_group * bfqg)3282de791abSDmitry Monakhov static void bfqg_and_blkg_get(struct bfq_group *bfqg)
3298f9bebc3SPaolo Valente {
3308f9bebc3SPaolo Valente 	/* see comments in bfq_bic_update_cgroup for why refcounting bfqg */
3318f9bebc3SPaolo Valente 	bfqg_get(bfqg);
3328f9bebc3SPaolo Valente 
3338f9bebc3SPaolo Valente 	blkg_get(bfqg_to_blkg(bfqg));
3348f9bebc3SPaolo Valente }
3358f9bebc3SPaolo Valente 
bfqg_and_blkg_put(struct bfq_group * bfqg)3368f9bebc3SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg)
3378f9bebc3SPaolo Valente {
3388f9bebc3SPaolo Valente 	blkg_put(bfqg_to_blkg(bfqg));
339d5274b3cSKonstantin Khlebnikov 
340d5274b3cSKonstantin Khlebnikov 	bfqg_put(bfqg);
341ea25da48SPaolo Valente }
342ea25da48SPaolo Valente 
bfqg_stats_update_legacy_io(struct request_queue * q,struct request * rq)343fd41e603STejun Heo void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq)
344fd41e603STejun Heo {
345fd41e603STejun Heo 	struct bfq_group *bfqg = blkg_to_bfqg(rq->bio->bi_blkg);
346fd41e603STejun Heo 
34708802ed6SHou Tao 	if (!bfqg)
34808802ed6SHou Tao 		return;
34908802ed6SHou Tao 
350fd41e603STejun Heo 	blkg_rwstat_add(&bfqg->stats.bytes, rq->cmd_flags, blk_rq_bytes(rq));
351fd41e603STejun Heo 	blkg_rwstat_add(&bfqg->stats.ios, rq->cmd_flags, 1);
352fd41e603STejun Heo }
353fd41e603STejun Heo 
354ea25da48SPaolo Valente /* @stats = 0 */
bfqg_stats_reset(struct bfqg_stats * stats)355ea25da48SPaolo Valente static void bfqg_stats_reset(struct bfqg_stats *stats)
356ea25da48SPaolo Valente {
3578060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
358ea25da48SPaolo Valente 	/* queued stats shouldn't be cleared */
359ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->merged);
360ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->service_time);
361ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->wait_time);
362c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->time);
363c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->avg_queue_size_sum);
364c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->avg_queue_size_samples);
365c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->dequeue);
366c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->group_wait_time);
367c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->idle_time);
368c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->empty_time);
369a33801e8SLuca Miccio #endif
370ea25da48SPaolo Valente }
371ea25da48SPaolo Valente 
372ea25da48SPaolo Valente /* @to += @from */
bfqg_stats_add_aux(struct bfqg_stats * to,struct bfqg_stats * from)373ea25da48SPaolo Valente static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from)
374ea25da48SPaolo Valente {
375ea25da48SPaolo Valente 	if (!to || !from)
376ea25da48SPaolo Valente 		return;
377ea25da48SPaolo Valente 
3788060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
379ea25da48SPaolo Valente 	/* queued stats shouldn't be cleared */
380ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->merged, &from->merged);
381ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->service_time, &from->service_time);
382ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
383c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&from->time, &from->time);
384c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
385c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->avg_queue_size_samples,
386ea25da48SPaolo Valente 			  &from->avg_queue_size_samples);
387c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->dequeue, &from->dequeue);
388c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
389c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->idle_time, &from->idle_time);
390c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->empty_time, &from->empty_time);
391a33801e8SLuca Miccio #endif
392ea25da48SPaolo Valente }
393ea25da48SPaolo Valente 
394ea25da48SPaolo Valente /*
395ea25da48SPaolo Valente  * Transfer @bfqg's stats to its parent's aux counts so that the ancestors'
396ea25da48SPaolo Valente  * recursive stats can still account for the amount used by this bfqg after
397ea25da48SPaolo Valente  * it's gone.
398ea25da48SPaolo Valente  */
bfqg_stats_xfer_dead(struct bfq_group * bfqg)399ea25da48SPaolo Valente static void bfqg_stats_xfer_dead(struct bfq_group *bfqg)
400ea25da48SPaolo Valente {
401ea25da48SPaolo Valente 	struct bfq_group *parent;
402ea25da48SPaolo Valente 
403ea25da48SPaolo Valente 	if (!bfqg) /* root_group */
404ea25da48SPaolo Valente 		return;
405ea25da48SPaolo Valente 
406ea25da48SPaolo Valente 	parent = bfqg_parent(bfqg);
407ea25da48SPaolo Valente 
4080d945c1fSChristoph Hellwig 	lockdep_assert_held(&bfqg_to_blkg(bfqg)->q->queue_lock);
409ea25da48SPaolo Valente 
410ea25da48SPaolo Valente 	if (unlikely(!parent))
411ea25da48SPaolo Valente 		return;
412ea25da48SPaolo Valente 
413ea25da48SPaolo Valente 	bfqg_stats_add_aux(&parent->stats, &bfqg->stats);
414ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
415ea25da48SPaolo Valente }
416ea25da48SPaolo Valente 
bfq_init_entity(struct bfq_entity * entity,struct bfq_group * bfqg)417ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
418ea25da48SPaolo Valente {
419ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
420ea25da48SPaolo Valente 
421ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
422ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
423ea25da48SPaolo Valente 	if (bfqq) {
424ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
425ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
4268f9bebc3SPaolo Valente 		/*
4278f9bebc3SPaolo Valente 		 * Make sure that bfqg and its associated blkg do not
4288f9bebc3SPaolo Valente 		 * disappear before entity.
4298f9bebc3SPaolo Valente 		 */
4308f9bebc3SPaolo Valente 		bfqg_and_blkg_get(bfqg);
431ea25da48SPaolo Valente 	}
432ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity; /* NULL for root group */
433ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
434ea25da48SPaolo Valente }
435ea25da48SPaolo Valente 
bfqg_stats_exit(struct bfqg_stats * stats)436ea25da48SPaolo Valente static void bfqg_stats_exit(struct bfqg_stats *stats)
437ea25da48SPaolo Valente {
438fd41e603STejun Heo 	blkg_rwstat_exit(&stats->bytes);
439fd41e603STejun Heo 	blkg_rwstat_exit(&stats->ios);
4408060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
441ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->merged);
442ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->service_time);
443ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->wait_time);
444ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->queued);
445c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->time);
446c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->avg_queue_size_sum);
447c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->avg_queue_size_samples);
448c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->dequeue);
449c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->group_wait_time);
450c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->idle_time);
451c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->empty_time);
452a33801e8SLuca Miccio #endif
453ea25da48SPaolo Valente }
454ea25da48SPaolo Valente 
bfqg_stats_init(struct bfqg_stats * stats,gfp_t gfp)455ea25da48SPaolo Valente static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp)
456ea25da48SPaolo Valente {
457fd41e603STejun Heo 	if (blkg_rwstat_init(&stats->bytes, gfp) ||
458fd41e603STejun Heo 	    blkg_rwstat_init(&stats->ios, gfp))
4592fc428f6SZheng Liang 		goto error;
460fd41e603STejun Heo 
4618060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
462ea25da48SPaolo Valente 	if (blkg_rwstat_init(&stats->merged, gfp) ||
463ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->service_time, gfp) ||
464ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->wait_time, gfp) ||
465ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->queued, gfp) ||
466c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->time, gfp) ||
467c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->avg_queue_size_sum, gfp) ||
468c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->avg_queue_size_samples, gfp) ||
469c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->dequeue, gfp) ||
470c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->group_wait_time, gfp) ||
471c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->idle_time, gfp) ||
4722fc428f6SZheng Liang 	    bfq_stat_init(&stats->empty_time, gfp))
4732fc428f6SZheng Liang 		goto error;
474a33801e8SLuca Miccio #endif
475ea25da48SPaolo Valente 
476ea25da48SPaolo Valente 	return 0;
4772fc428f6SZheng Liang 
4782fc428f6SZheng Liang error:
4792fc428f6SZheng Liang 	bfqg_stats_exit(stats);
4802fc428f6SZheng Liang 	return -ENOMEM;
481ea25da48SPaolo Valente }
482ea25da48SPaolo Valente 
cpd_to_bfqgd(struct blkcg_policy_data * cpd)483ea25da48SPaolo Valente static struct bfq_group_data *cpd_to_bfqgd(struct blkcg_policy_data *cpd)
484ea25da48SPaolo Valente {
485ea25da48SPaolo Valente 	return cpd ? container_of(cpd, struct bfq_group_data, pd) : NULL;
486ea25da48SPaolo Valente }
487ea25da48SPaolo Valente 
blkcg_to_bfqgd(struct blkcg * blkcg)488ea25da48SPaolo Valente static struct bfq_group_data *blkcg_to_bfqgd(struct blkcg *blkcg)
489ea25da48SPaolo Valente {
490ea25da48SPaolo Valente 	return cpd_to_bfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_bfq));
491ea25da48SPaolo Valente }
492ea25da48SPaolo Valente 
bfq_cpd_alloc(gfp_t gfp)493dfb79af5SBart Van Assche static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp)
494ea25da48SPaolo Valente {
495ea25da48SPaolo Valente 	struct bfq_group_data *bgd;
496ea25da48SPaolo Valente 
497ea25da48SPaolo Valente 	bgd = kzalloc(sizeof(*bgd), gfp);
498ea25da48SPaolo Valente 	if (!bgd)
499ea25da48SPaolo Valente 		return NULL;
500650e2cb5SChengming Zhou 
501650e2cb5SChengming Zhou 	bgd->weight = CGROUP_WEIGHT_DFL;
502ea25da48SPaolo Valente 	return &bgd->pd;
503ea25da48SPaolo Valente }
504ea25da48SPaolo Valente 
bfq_cpd_free(struct blkcg_policy_data * cpd)505dfb79af5SBart Van Assche static void bfq_cpd_free(struct blkcg_policy_data *cpd)
506ea25da48SPaolo Valente {
507ea25da48SPaolo Valente 	kfree(cpd_to_bfqgd(cpd));
508ea25da48SPaolo Valente }
509ea25da48SPaolo Valente 
bfq_pd_alloc(struct gendisk * disk,struct blkcg * blkcg,gfp_t gfp)5100a0b4f79SChristoph Hellwig static struct blkg_policy_data *bfq_pd_alloc(struct gendisk *disk,
5110a0b4f79SChristoph Hellwig 		struct blkcg *blkcg, gfp_t gfp)
512ea25da48SPaolo Valente {
513ea25da48SPaolo Valente 	struct bfq_group *bfqg;
514ea25da48SPaolo Valente 
5150a0b4f79SChristoph Hellwig 	bfqg = kzalloc_node(sizeof(*bfqg), gfp, disk->node_id);
516ea25da48SPaolo Valente 	if (!bfqg)
517ea25da48SPaolo Valente 		return NULL;
518ea25da48SPaolo Valente 
519ea25da48SPaolo Valente 	if (bfqg_stats_init(&bfqg->stats, gfp)) {
520ea25da48SPaolo Valente 		kfree(bfqg);
521ea25da48SPaolo Valente 		return NULL;
522ea25da48SPaolo Valente 	}
523ea25da48SPaolo Valente 
5248f9bebc3SPaolo Valente 	/* see comments in bfq_bic_update_cgroup for why refcounting */
525216f7647SYu Kuai 	refcount_set(&bfqg->ref, 1);
526ea25da48SPaolo Valente 	return &bfqg->pd;
527ea25da48SPaolo Valente }
528ea25da48SPaolo Valente 
bfq_pd_init(struct blkg_policy_data * pd)529dfb79af5SBart Van Assche static void bfq_pd_init(struct blkg_policy_data *pd)
530ea25da48SPaolo Valente {
531ea25da48SPaolo Valente 	struct blkcg_gq *blkg = pd_to_blkg(pd);
532ea25da48SPaolo Valente 	struct bfq_group *bfqg = blkg_to_bfqg(blkg);
533ea25da48SPaolo Valente 	struct bfq_data *bfqd = blkg->q->elevator->elevator_data;
534ea25da48SPaolo Valente 	struct bfq_entity *entity = &bfqg->entity;
535ea25da48SPaolo Valente 	struct bfq_group_data *d = blkcg_to_bfqgd(blkg->blkcg);
536ea25da48SPaolo Valente 
537ea25da48SPaolo Valente 	entity->orig_weight = entity->weight = entity->new_weight = d->weight;
538ea25da48SPaolo Valente 	entity->my_sched_data = &bfqg->sched_data;
539430a67f9SPaolo Valente 	entity->last_bfqq_created = NULL;
540430a67f9SPaolo Valente 
541ea25da48SPaolo Valente 	bfqg->my_entity = entity; /*
542ea25da48SPaolo Valente 				   * the root_group's will be set to NULL
543ea25da48SPaolo Valente 				   * in bfq_init_queue()
544ea25da48SPaolo Valente 				   */
545ea25da48SPaolo Valente 	bfqg->bfqd = bfqd;
546ea25da48SPaolo Valente 	bfqg->active_entities = 0;
54760a6e10cSYu Kuai 	bfqg->num_queues_with_pending_reqs = 0;
548ea25da48SPaolo Valente 	bfqg->rq_pos_tree = RB_ROOT;
549ea25da48SPaolo Valente }
550ea25da48SPaolo Valente 
bfq_pd_free(struct blkg_policy_data * pd)551dfb79af5SBart Van Assche static void bfq_pd_free(struct blkg_policy_data *pd)
552ea25da48SPaolo Valente {
553ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
554ea25da48SPaolo Valente 
555ea25da48SPaolo Valente 	bfqg_stats_exit(&bfqg->stats);
5568f9bebc3SPaolo Valente 	bfqg_put(bfqg);
557ea25da48SPaolo Valente }
558ea25da48SPaolo Valente 
bfq_pd_reset_stats(struct blkg_policy_data * pd)559dfb79af5SBart Van Assche static void bfq_pd_reset_stats(struct blkg_policy_data *pd)
560ea25da48SPaolo Valente {
561ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
562ea25da48SPaolo Valente 
563ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
564ea25da48SPaolo Valente }
565ea25da48SPaolo Valente 
bfq_group_set_parent(struct bfq_group * bfqg,struct bfq_group * parent)566ea25da48SPaolo Valente static void bfq_group_set_parent(struct bfq_group *bfqg,
567ea25da48SPaolo Valente 					struct bfq_group *parent)
568ea25da48SPaolo Valente {
569ea25da48SPaolo Valente 	struct bfq_entity *entity;
570ea25da48SPaolo Valente 
571ea25da48SPaolo Valente 	entity = &bfqg->entity;
572ea25da48SPaolo Valente 	entity->parent = parent->my_entity;
573ea25da48SPaolo Valente 	entity->sched_data = &parent->sched_data;
574ea25da48SPaolo Valente }
575ea25da48SPaolo Valente 
bfq_link_bfqg(struct bfq_data * bfqd,struct bfq_group * bfqg)5764e54a249SJan Kara static void bfq_link_bfqg(struct bfq_data *bfqd, struct bfq_group *bfqg)
577ea25da48SPaolo Valente {
5784e54a249SJan Kara 	struct bfq_group *parent;
579ea25da48SPaolo Valente 	struct bfq_entity *entity;
580ea25da48SPaolo Valente 
581ea25da48SPaolo Valente 	/*
582ea25da48SPaolo Valente 	 * Update chain of bfq_groups as we might be handling a leaf group
583ea25da48SPaolo Valente 	 * which, along with some of its relatives, has not been hooked yet
584ea25da48SPaolo Valente 	 * to the private hierarchy of BFQ.
585ea25da48SPaolo Valente 	 */
586ea25da48SPaolo Valente 	entity = &bfqg->entity;
587ea25da48SPaolo Valente 	for_each_entity(entity) {
58814afc593SCarlo Nonato 		struct bfq_group *curr_bfqg = container_of(entity,
58914afc593SCarlo Nonato 						struct bfq_group, entity);
59014afc593SCarlo Nonato 		if (curr_bfqg != bfqd->root_group) {
59114afc593SCarlo Nonato 			parent = bfqg_parent(curr_bfqg);
592ea25da48SPaolo Valente 			if (!parent)
593ea25da48SPaolo Valente 				parent = bfqd->root_group;
59414afc593SCarlo Nonato 			bfq_group_set_parent(curr_bfqg, parent);
595ea25da48SPaolo Valente 		}
596ea25da48SPaolo Valente 	}
5974e54a249SJan Kara }
598ea25da48SPaolo Valente 
bfq_bio_bfqg(struct bfq_data * bfqd,struct bio * bio)5994e54a249SJan Kara struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio)
6004e54a249SJan Kara {
6014e54a249SJan Kara 	struct blkcg_gq *blkg = bio->bi_blkg;
602075a53b7SJan Kara 	struct bfq_group *bfqg;
6034e54a249SJan Kara 
604075a53b7SJan Kara 	while (blkg) {
605f02be900SYu Kuai 		if (!blkg->online) {
606f02be900SYu Kuai 			blkg = blkg->parent;
607f02be900SYu Kuai 			continue;
608f02be900SYu Kuai 		}
609075a53b7SJan Kara 		bfqg = blkg_to_bfqg(blkg);
610f37bf75cSYu Kuai 		if (bfqg->pd.online) {
611075a53b7SJan Kara 			bio_associate_blkg_from_css(bio, &blkg->blkcg->css);
612075a53b7SJan Kara 			return bfqg;
613075a53b7SJan Kara 		}
614075a53b7SJan Kara 		blkg = blkg->parent;
615075a53b7SJan Kara 	}
616075a53b7SJan Kara 	bio_associate_blkg_from_css(bio,
617075a53b7SJan Kara 				&bfqg_to_blkg(bfqd->root_group)->blkcg->css);
6184e54a249SJan Kara 	return bfqd->root_group;
619ea25da48SPaolo Valente }
620ea25da48SPaolo Valente 
621ea25da48SPaolo Valente /**
622ea25da48SPaolo Valente  * bfq_bfqq_move - migrate @bfqq to @bfqg.
623ea25da48SPaolo Valente  * @bfqd: queue descriptor.
624ea25da48SPaolo Valente  * @bfqq: the queue to move.
625ea25da48SPaolo Valente  * @bfqg: the group to move to.
626ea25da48SPaolo Valente  *
627ea25da48SPaolo Valente  * Move @bfqq to @bfqg, deactivating it from its old group and reactivating
628ea25da48SPaolo Valente  * it on the new one.  Avoid putting the entity on the old group idle tree.
629ea25da48SPaolo Valente  *
6308f9bebc3SPaolo Valente  * Must be called under the scheduler lock, to make sure that the blkg
6318f9bebc3SPaolo Valente  * owning @bfqg does not disappear (see comments in
6328f9bebc3SPaolo Valente  * bfq_bic_update_cgroup on guaranteeing the consistency of blkg
6338f9bebc3SPaolo Valente  * objects).
634ea25da48SPaolo Valente  */
bfq_bfqq_move(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct bfq_group * bfqg)635ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
636ea25da48SPaolo Valente 		   struct bfq_group *bfqg)
637ea25da48SPaolo Valente {
638ea25da48SPaolo Valente 	struct bfq_entity *entity = &bfqq->entity;
639c5e4cb0fSYu Kuai 	struct bfq_group *old_parent = bfqq_group(bfqq);
64060a6e10cSYu Kuai 	bool has_pending_reqs = false;
641c5e4cb0fSYu Kuai 
642c5e4cb0fSYu Kuai 	/*
643c5e4cb0fSYu Kuai 	 * No point to move bfqq to the same group, which can happen when
644c5e4cb0fSYu Kuai 	 * root group is offlined
645c5e4cb0fSYu Kuai 	 */
646c5e4cb0fSYu Kuai 	if (old_parent == bfqg)
647c5e4cb0fSYu Kuai 		return;
648ea25da48SPaolo Valente 
649fd1bb3aeSPaolo Valente 	/*
6508410f709SYu Kuai 	 * oom_bfqq is not allowed to move, oom_bfqq will hold ref to root_group
6518410f709SYu Kuai 	 * until elevator exit.
6528410f709SYu Kuai 	 */
6538410f709SYu Kuai 	if (bfqq == &bfqd->oom_bfqq)
6548410f709SYu Kuai 		return;
6558410f709SYu Kuai 	/*
656fd1bb3aeSPaolo Valente 	 * Get extra reference to prevent bfqq from being freed in
657fd1bb3aeSPaolo Valente 	 * next possible expire or deactivate.
658fd1bb3aeSPaolo Valente 	 */
659fd1bb3aeSPaolo Valente 	bfqq->ref++;
660fd1bb3aeSPaolo Valente 
66160a6e10cSYu Kuai 	if (entity->in_groups_with_pending_reqs) {
66260a6e10cSYu Kuai 		has_pending_reqs = true;
66360a6e10cSYu Kuai 		bfq_del_bfqq_in_groups_with_pending_reqs(bfqq);
66460a6e10cSYu Kuai 	}
66560a6e10cSYu Kuai 
666ea25da48SPaolo Valente 	/* If bfqq is empty, then bfq_bfqq_expire also invokes
667ea25da48SPaolo Valente 	 * bfq_del_bfqq_busy, thereby removing bfqq and its entity
668ea25da48SPaolo Valente 	 * from data structures related to current group. Otherwise we
669ea25da48SPaolo Valente 	 * need to remove bfqq explicitly with bfq_deactivate_bfqq, as
670ea25da48SPaolo Valente 	 * we do below.
671ea25da48SPaolo Valente 	 */
672ea25da48SPaolo Valente 	if (bfqq == bfqd->in_service_queue)
673ea25da48SPaolo Valente 		bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
674ea25da48SPaolo Valente 				false, BFQQE_PREEMPTED);
675ea25da48SPaolo Valente 
676ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq))
677ea25da48SPaolo Valente 		bfq_deactivate_bfqq(bfqd, bfqq, false, false);
67833a16a98SPaolo Valente 	else if (entity->on_st_or_in_serv)
679ea25da48SPaolo Valente 		bfq_put_idle_entity(bfq_entity_service_tree(entity), entity);
680c5e4cb0fSYu Kuai 	bfqg_and_blkg_put(old_parent);
681ea25da48SPaolo Valente 
682d29bd414SPaolo Valente 	if (entity->parent &&
683d29bd414SPaolo Valente 	    entity->parent->last_bfqq_created == bfqq)
684d29bd414SPaolo Valente 		entity->parent->last_bfqq_created = NULL;
685d29bd414SPaolo Valente 	else if (bfqd->last_bfqq_created == bfqq)
686d29bd414SPaolo Valente 		bfqd->last_bfqq_created = NULL;
687d29bd414SPaolo Valente 
688ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity;
689ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
6908f9bebc3SPaolo Valente 	/* pin down bfqg and its associated blkg  */
6918f9bebc3SPaolo Valente 	bfqg_and_blkg_get(bfqg);
692ea25da48SPaolo Valente 
69360a6e10cSYu Kuai 	if (has_pending_reqs)
69460a6e10cSYu Kuai 		bfq_add_bfqq_in_groups_with_pending_reqs(bfqq);
69560a6e10cSYu Kuai 
696ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq)) {
6978cacc5abSPaolo Valente 		if (unlikely(!bfqd->nonrot_with_queueing))
698ea25da48SPaolo Valente 			bfq_pos_tree_add_move(bfqd, bfqq);
699ea25da48SPaolo Valente 		bfq_activate_bfqq(bfqd, bfqq);
700ea25da48SPaolo Valente 	}
701ea25da48SPaolo Valente 
7022d31c684SDavide Zini 	if (!bfqd->in_service_queue && !bfqd->tot_rq_in_driver)
703ea25da48SPaolo Valente 		bfq_schedule_dispatch(bfqd);
704fd1bb3aeSPaolo Valente 	/* release extra ref taken above, bfqq may happen to be freed now */
705ecedd3d7SPaolo Valente 	bfq_put_queue(bfqq);
706ea25da48SPaolo Valente }
707ea25da48SPaolo Valente 
bfq_sync_bfqq_move(struct bfq_data * bfqd,struct bfq_queue * sync_bfqq,struct bfq_io_cq * bic,struct bfq_group * bfqg,unsigned int act_idx)7089778369aSPaolo Valente static void bfq_sync_bfqq_move(struct bfq_data *bfqd,
7099778369aSPaolo Valente 			       struct bfq_queue *sync_bfqq,
7109778369aSPaolo Valente 			       struct bfq_io_cq *bic,
7119778369aSPaolo Valente 			       struct bfq_group *bfqg,
7129778369aSPaolo Valente 			       unsigned int act_idx)
7139778369aSPaolo Valente {
7149778369aSPaolo Valente 	struct bfq_queue *bfqq;
7159778369aSPaolo Valente 
7169778369aSPaolo Valente 	if (!sync_bfqq->new_bfqq && !bfq_bfqq_coop(sync_bfqq)) {
7179778369aSPaolo Valente 		/* We are the only user of this bfqq, just move it */
7189778369aSPaolo Valente 		if (sync_bfqq->entity.sched_data != &bfqg->sched_data)
7199778369aSPaolo Valente 			bfq_bfqq_move(bfqd, sync_bfqq, bfqg);
7209778369aSPaolo Valente 		return;
7219778369aSPaolo Valente 	}
7229778369aSPaolo Valente 
7239778369aSPaolo Valente 	/*
7249778369aSPaolo Valente 	 * The queue was merged to a different queue. Check
7259778369aSPaolo Valente 	 * that the merge chain still belongs to the same
7269778369aSPaolo Valente 	 * cgroup.
7279778369aSPaolo Valente 	 */
7289778369aSPaolo Valente 	for (bfqq = sync_bfqq; bfqq; bfqq = bfqq->new_bfqq)
7299778369aSPaolo Valente 		if (bfqq->entity.sched_data != &bfqg->sched_data)
7309778369aSPaolo Valente 			break;
7319778369aSPaolo Valente 	if (bfqq) {
7329778369aSPaolo Valente 		/*
7339778369aSPaolo Valente 		 * Some queue changed cgroup so the merge is not valid
7349778369aSPaolo Valente 		 * anymore. We cannot easily just cancel the merge (by
7359778369aSPaolo Valente 		 * clearing new_bfqq) as there may be other processes
7369778369aSPaolo Valente 		 * using this queue and holding refs to all queues
7379778369aSPaolo Valente 		 * below sync_bfqq->new_bfqq. Similarly if the merge
7389778369aSPaolo Valente 		 * already happened, we need to detach from bfqq now
7399778369aSPaolo Valente 		 * so that we cannot merge bio to a request from the
7409778369aSPaolo Valente 		 * old cgroup.
7419778369aSPaolo Valente 		 */
7429778369aSPaolo Valente 		bfq_put_cooperator(sync_bfqq);
7439778369aSPaolo Valente 		bic_set_bfqq(bic, NULL, true, act_idx);
7445b0ed596SLinus Torvalds 		bfq_release_process_ref(bfqd, sync_bfqq);
7459778369aSPaolo Valente 	}
7469778369aSPaolo Valente }
7479778369aSPaolo Valente 
748ea25da48SPaolo Valente /**
7491d87be82SBart Van Assche  * __bfq_bic_change_cgroup - move @bic to @bfqg.
750ea25da48SPaolo Valente  * @bfqd: the queue descriptor.
751ea25da48SPaolo Valente  * @bic: the bic to move.
7521d87be82SBart Van Assche  * @bfqg: the group to move to.
753ea25da48SPaolo Valente  *
7548f9bebc3SPaolo Valente  * Move bic to blkcg, assuming that bfqd->lock is held; which makes
7558f9bebc3SPaolo Valente  * sure that the reference to cgroup is valid across the call (see
7568f9bebc3SPaolo Valente  * comments in bfq_bic_update_cgroup on this issue)
757ea25da48SPaolo Valente  */
__bfq_bic_change_cgroup(struct bfq_data * bfqd,struct bfq_io_cq * bic,struct bfq_group * bfqg)758452af7dcSYu Kuai static void __bfq_bic_change_cgroup(struct bfq_data *bfqd,
759ea25da48SPaolo Valente 				    struct bfq_io_cq *bic,
7604e54a249SJan Kara 				    struct bfq_group *bfqg)
761ea25da48SPaolo Valente {
7629778369aSPaolo Valente 	unsigned int act_idx;
763ea25da48SPaolo Valente 
7649778369aSPaolo Valente 	for (act_idx = 0; act_idx < bfqd->num_actuators; act_idx++) {
7659778369aSPaolo Valente 		struct bfq_queue *async_bfqq = bic_to_bfqq(bic, false, act_idx);
7669778369aSPaolo Valente 		struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, true, act_idx);
767ea25da48SPaolo Valente 
7689778369aSPaolo Valente 		if (async_bfqq &&
7699778369aSPaolo Valente 		    async_bfqq->entity.sched_data != &bfqg->sched_data) {
7709778369aSPaolo Valente 			bic_set_bfqq(bic, NULL, false, act_idx);
771c8997736SPaolo Valente 			bfq_release_process_ref(bfqd, async_bfqq);
772ea25da48SPaolo Valente 		}
773ea25da48SPaolo Valente 
7749778369aSPaolo Valente 		if (sync_bfqq)
7759778369aSPaolo Valente 			bfq_sync_bfqq_move(bfqd, sync_bfqq, bic, bfqg, act_idx);
776ea25da48SPaolo Valente 	}
777ea25da48SPaolo Valente }
778ea25da48SPaolo Valente 
bfq_bic_update_cgroup(struct bfq_io_cq * bic,struct bio * bio)779ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio)
780ea25da48SPaolo Valente {
781ea25da48SPaolo Valente 	struct bfq_data *bfqd = bic_to_bfqd(bic);
7824e54a249SJan Kara 	struct bfq_group *bfqg = bfq_bio_bfqg(bfqd, bio);
783ea25da48SPaolo Valente 	uint64_t serial_nr;
784ea25da48SPaolo Valente 
7854e54a249SJan Kara 	serial_nr = bfqg_to_blkg(bfqg)->blkcg->css.serial_nr;
786ea25da48SPaolo Valente 
787ea25da48SPaolo Valente 	/*
788ea25da48SPaolo Valente 	 * Check whether blkcg has changed.  The condition may trigger
789ea25da48SPaolo Valente 	 * spuriously on a newly created cic but there's no harm.
790ea25da48SPaolo Valente 	 */
791ea25da48SPaolo Valente 	if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr))
7924e54a249SJan Kara 		return;
793ea25da48SPaolo Valente 
7944e54a249SJan Kara 	/*
7954e54a249SJan Kara 	 * New cgroup for this process. Make sure it is linked to bfq internal
7964e54a249SJan Kara 	 * cgroup hierarchy.
7974e54a249SJan Kara 	 */
7984e54a249SJan Kara 	bfq_link_bfqg(bfqd, bfqg);
7994e54a249SJan Kara 	__bfq_bic_change_cgroup(bfqd, bic, bfqg);
8008f9bebc3SPaolo Valente 	/*
8018f9bebc3SPaolo Valente 	 * Update blkg_path for bfq_log_* functions. We cache this
8028f9bebc3SPaolo Valente 	 * path, and update it here, for the following
8038f9bebc3SPaolo Valente 	 * reasons. Operations on blkg objects in blk-cgroup are
8048f9bebc3SPaolo Valente 	 * protected with the request_queue lock, and not with the
8058f9bebc3SPaolo Valente 	 * lock that protects the instances of this scheduler
8068f9bebc3SPaolo Valente 	 * (bfqd->lock). This exposes BFQ to the following sort of
8078f9bebc3SPaolo Valente 	 * race.
8088f9bebc3SPaolo Valente 	 *
8098f9bebc3SPaolo Valente 	 * The blkg_lookup performed in bfq_get_queue, protected
8108f9bebc3SPaolo Valente 	 * through rcu, may happen to return the address of a copy of
8118f9bebc3SPaolo Valente 	 * the original blkg. If this is the case, then the
8128f9bebc3SPaolo Valente 	 * bfqg_and_blkg_get performed in bfq_get_queue, to pin down
8138f9bebc3SPaolo Valente 	 * the blkg, is useless: it does not prevent blk-cgroup code
8148f9bebc3SPaolo Valente 	 * from destroying both the original blkg and all objects
8158f9bebc3SPaolo Valente 	 * directly or indirectly referred by the copy of the
8168f9bebc3SPaolo Valente 	 * blkg.
8178f9bebc3SPaolo Valente 	 *
8188f9bebc3SPaolo Valente 	 * On the bright side, destroy operations on a blkg invoke, as
8198f9bebc3SPaolo Valente 	 * a first step, hooks of the scheduler associated with the
8208f9bebc3SPaolo Valente 	 * blkg. And these hooks are executed with bfqd->lock held for
8218f9bebc3SPaolo Valente 	 * BFQ. As a consequence, for any blkg associated with the
8228f9bebc3SPaolo Valente 	 * request queue this instance of the scheduler is attached
8238f9bebc3SPaolo Valente 	 * to, we are guaranteed that such a blkg is not destroyed, and
8248f9bebc3SPaolo Valente 	 * that all the pointers it contains are consistent, while we
8258f9bebc3SPaolo Valente 	 * are holding bfqd->lock. A blkg_lookup performed with
8268f9bebc3SPaolo Valente 	 * bfqd->lock held then returns a fully consistent blkg, which
8278f9bebc3SPaolo Valente 	 * remains consistent until this lock is held.
8288f9bebc3SPaolo Valente 	 *
8298f9bebc3SPaolo Valente 	 * Thanks to the last fact, and to the fact that: (1) bfqg has
8308f9bebc3SPaolo Valente 	 * been obtained through a blkg_lookup in the above
8318f9bebc3SPaolo Valente 	 * assignment, and (2) bfqd->lock is being held, here we can
8328f9bebc3SPaolo Valente 	 * safely use the policy data for the involved blkg (i.e., the
8338f9bebc3SPaolo Valente 	 * field bfqg->pd) to get to the blkg associated with bfqg,
8348f9bebc3SPaolo Valente 	 * and then we can safely use any field of blkg. After we
8358f9bebc3SPaolo Valente 	 * release bfqd->lock, even just getting blkg through this
8368f9bebc3SPaolo Valente 	 * bfqg may cause dangling references to be traversed, as
8378f9bebc3SPaolo Valente 	 * bfqg->pd may not exist any more.
8388f9bebc3SPaolo Valente 	 *
8398f9bebc3SPaolo Valente 	 * In view of the above facts, here we cache, in the bfqg, any
8408f9bebc3SPaolo Valente 	 * blkg data we may need for this bic, and for its associated
8418f9bebc3SPaolo Valente 	 * bfq_queue. As of now, we need to cache only the path of the
8428f9bebc3SPaolo Valente 	 * blkg, which is used in the bfq_log_* functions.
8438f9bebc3SPaolo Valente 	 *
8448f9bebc3SPaolo Valente 	 * Finally, note that bfqg itself needs to be protected from
8458f9bebc3SPaolo Valente 	 * destruction on the blkg_free of the original blkg (which
8468f9bebc3SPaolo Valente 	 * invokes bfq_pd_free). We use an additional private
8478f9bebc3SPaolo Valente 	 * refcounter for bfqg, to let it disappear only after no
8488f9bebc3SPaolo Valente 	 * bfq_queue refers to it any longer.
8498f9bebc3SPaolo Valente 	 */
8508f9bebc3SPaolo Valente 	blkg_path(bfqg_to_blkg(bfqg), bfqg->blkg_path, sizeof(bfqg->blkg_path));
851ea25da48SPaolo Valente 	bic->blkcg_serial_nr = serial_nr;
852ea25da48SPaolo Valente }
853ea25da48SPaolo Valente 
854ea25da48SPaolo Valente /**
855ea25da48SPaolo Valente  * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
856ea25da48SPaolo Valente  * @st: the service tree being flushed.
857ea25da48SPaolo Valente  */
bfq_flush_idle_tree(struct bfq_service_tree * st)858ea25da48SPaolo Valente static void bfq_flush_idle_tree(struct bfq_service_tree *st)
859ea25da48SPaolo Valente {
860ea25da48SPaolo Valente 	struct bfq_entity *entity = st->first_idle;
861ea25da48SPaolo Valente 
862ea25da48SPaolo Valente 	for (; entity ; entity = st->first_idle)
863ea25da48SPaolo Valente 		__bfq_deactivate_entity(entity, false);
864ea25da48SPaolo Valente }
865ea25da48SPaolo Valente 
866ea25da48SPaolo Valente /**
867ea25da48SPaolo Valente  * bfq_reparent_leaf_entity - move leaf entity to the root_group.
868ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
869576682faSPaolo Valente  * @entity: the entity to move, if entity is a leaf; or the parent entity
870576682faSPaolo Valente  *	    of an active leaf entity to move, if entity is not a leaf.
8711d87be82SBart Van Assche  * @ioprio_class: I/O priority class to reparent.
872ea25da48SPaolo Valente  */
bfq_reparent_leaf_entity(struct bfq_data * bfqd,struct bfq_entity * entity,int ioprio_class)873ea25da48SPaolo Valente static void bfq_reparent_leaf_entity(struct bfq_data *bfqd,
874576682faSPaolo Valente 				     struct bfq_entity *entity,
875576682faSPaolo Valente 				     int ioprio_class)
876ea25da48SPaolo Valente {
877576682faSPaolo Valente 	struct bfq_queue *bfqq;
878576682faSPaolo Valente 	struct bfq_entity *child_entity = entity;
879ea25da48SPaolo Valente 
880576682faSPaolo Valente 	while (child_entity->my_sched_data) { /* leaf not reached yet */
881576682faSPaolo Valente 		struct bfq_sched_data *child_sd = child_entity->my_sched_data;
882576682faSPaolo Valente 		struct bfq_service_tree *child_st = child_sd->service_tree +
883576682faSPaolo Valente 			ioprio_class;
884576682faSPaolo Valente 		struct rb_root *child_active = &child_st->active;
885576682faSPaolo Valente 
886576682faSPaolo Valente 		child_entity = bfq_entity_of(rb_first(child_active));
887576682faSPaolo Valente 
888576682faSPaolo Valente 		if (!child_entity)
889576682faSPaolo Valente 			child_entity = child_sd->in_service_entity;
890576682faSPaolo Valente 	}
891576682faSPaolo Valente 
892576682faSPaolo Valente 	bfqq = bfq_entity_to_bfqq(child_entity);
893ea25da48SPaolo Valente 	bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
894ea25da48SPaolo Valente }
895ea25da48SPaolo Valente 
896ea25da48SPaolo Valente /**
897576682faSPaolo Valente  * bfq_reparent_active_queues - move to the root group all active queues.
898ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
899ea25da48SPaolo Valente  * @bfqg: the group to move from.
900576682faSPaolo Valente  * @st: the service tree to start the search from.
9011d87be82SBart Van Assche  * @ioprio_class: I/O priority class to reparent.
902ea25da48SPaolo Valente  */
bfq_reparent_active_queues(struct bfq_data * bfqd,struct bfq_group * bfqg,struct bfq_service_tree * st,int ioprio_class)903576682faSPaolo Valente static void bfq_reparent_active_queues(struct bfq_data *bfqd,
904ea25da48SPaolo Valente 				       struct bfq_group *bfqg,
905576682faSPaolo Valente 				       struct bfq_service_tree *st,
906576682faSPaolo Valente 				       int ioprio_class)
907ea25da48SPaolo Valente {
908ea25da48SPaolo Valente 	struct rb_root *active = &st->active;
909576682faSPaolo Valente 	struct bfq_entity *entity;
910ea25da48SPaolo Valente 
911576682faSPaolo Valente 	while ((entity = bfq_entity_of(rb_first(active))))
912576682faSPaolo Valente 		bfq_reparent_leaf_entity(bfqd, entity, ioprio_class);
913ea25da48SPaolo Valente 
914ea25da48SPaolo Valente 	if (bfqg->sched_data.in_service_entity)
915ea25da48SPaolo Valente 		bfq_reparent_leaf_entity(bfqd,
916576682faSPaolo Valente 					 bfqg->sched_data.in_service_entity,
917576682faSPaolo Valente 					 ioprio_class);
918ea25da48SPaolo Valente }
919ea25da48SPaolo Valente 
920ea25da48SPaolo Valente /**
921ea25da48SPaolo Valente  * bfq_pd_offline - deactivate the entity associated with @pd,
922ea25da48SPaolo Valente  *		    and reparent its children entities.
923ea25da48SPaolo Valente  * @pd: descriptor of the policy going offline.
924ea25da48SPaolo Valente  *
925ea25da48SPaolo Valente  * blkio already grabs the queue_lock for us, so no need to use
926ea25da48SPaolo Valente  * RCU-based magic
927ea25da48SPaolo Valente  */
bfq_pd_offline(struct blkg_policy_data * pd)928dfb79af5SBart Van Assche static void bfq_pd_offline(struct blkg_policy_data *pd)
929ea25da48SPaolo Valente {
930ea25da48SPaolo Valente 	struct bfq_service_tree *st;
931ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
932ea25da48SPaolo Valente 	struct bfq_data *bfqd = bfqg->bfqd;
933ea25da48SPaolo Valente 	struct bfq_entity *entity = bfqg->my_entity;
934ea25da48SPaolo Valente 	unsigned long flags;
935ea25da48SPaolo Valente 	int i;
936ea25da48SPaolo Valente 
937ea25da48SPaolo Valente 	spin_lock_irqsave(&bfqd->lock, flags);
93852257ffbSPaolo Valente 
93952257ffbSPaolo Valente 	if (!entity) /* root group */
94052257ffbSPaolo Valente 		goto put_async_queues;
94152257ffbSPaolo Valente 
942ea25da48SPaolo Valente 	/*
943ea25da48SPaolo Valente 	 * Empty all service_trees belonging to this group before
944ea25da48SPaolo Valente 	 * deactivating the group itself.
945ea25da48SPaolo Valente 	 */
946ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
947ea25da48SPaolo Valente 		st = bfqg->sched_data.service_tree + i;
948ea25da48SPaolo Valente 
949ea25da48SPaolo Valente 		/*
950ea25da48SPaolo Valente 		 * It may happen that some queues are still active
951ea25da48SPaolo Valente 		 * (busy) upon group destruction (if the corresponding
952ea25da48SPaolo Valente 		 * processes have been forced to terminate). We move
953ea25da48SPaolo Valente 		 * all the leaf entities corresponding to these queues
954ea25da48SPaolo Valente 		 * to the root_group.
955ea25da48SPaolo Valente 		 * Also, it may happen that the group has an entity
956ea25da48SPaolo Valente 		 * in service, which is disconnected from the active
957ea25da48SPaolo Valente 		 * tree: it must be moved, too.
958ea25da48SPaolo Valente 		 * There is no need to put the sync queues, as the
959ea25da48SPaolo Valente 		 * scheduler has taken no reference.
960ea25da48SPaolo Valente 		 */
961576682faSPaolo Valente 		bfq_reparent_active_queues(bfqd, bfqg, st, i);
9624d38a87fSPaolo Valente 
9634d38a87fSPaolo Valente 		/*
9644d38a87fSPaolo Valente 		 * The idle tree may still contain bfq_queues
9654d38a87fSPaolo Valente 		 * belonging to exited task because they never
9664d38a87fSPaolo Valente 		 * migrated to a different cgroup from the one being
9674d38a87fSPaolo Valente 		 * destroyed now. In addition, even
9684d38a87fSPaolo Valente 		 * bfq_reparent_active_queues() may happen to add some
9694d38a87fSPaolo Valente 		 * entities to the idle tree. It happens if, in some
9704d38a87fSPaolo Valente 		 * of the calls to bfq_bfqq_move() performed by
9714d38a87fSPaolo Valente 		 * bfq_reparent_active_queues(), the queue to move is
9724d38a87fSPaolo Valente 		 * empty and gets expired.
9734d38a87fSPaolo Valente 		 */
9744d38a87fSPaolo Valente 		bfq_flush_idle_tree(st);
975ea25da48SPaolo Valente 	}
976ea25da48SPaolo Valente 
977ea25da48SPaolo Valente 	__bfq_deactivate_entity(entity, false);
97852257ffbSPaolo Valente 
97952257ffbSPaolo Valente put_async_queues:
980ea25da48SPaolo Valente 	bfq_put_async_queues(bfqd, bfqg);
981ea25da48SPaolo Valente 
982ea25da48SPaolo Valente 	spin_unlock_irqrestore(&bfqd->lock, flags);
983ea25da48SPaolo Valente 	/*
984ea25da48SPaolo Valente 	 * @blkg is going offline and will be ignored by
985ea25da48SPaolo Valente 	 * blkg_[rw]stat_recursive_sum().  Transfer stats to the parent so
986ea25da48SPaolo Valente 	 * that they don't get lost.  If IOs complete after this point, the
987ea25da48SPaolo Valente 	 * stats for them will be lost.  Oh well...
988ea25da48SPaolo Valente 	 */
989ea25da48SPaolo Valente 	bfqg_stats_xfer_dead(bfqg);
990ea25da48SPaolo Valente }
991ea25da48SPaolo Valente 
bfq_end_wr_async(struct bfq_data * bfqd)992ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
993ea25da48SPaolo Valente {
994ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
995ea25da48SPaolo Valente 
996ea25da48SPaolo Valente 	list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) {
997ea25da48SPaolo Valente 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
998ea25da48SPaolo Valente 
999ea25da48SPaolo Valente 		bfq_end_wr_async_queues(bfqd, bfqg);
1000ea25da48SPaolo Valente 	}
1001ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1002ea25da48SPaolo Valente }
1003ea25da48SPaolo Valente 
bfq_io_show_weight_legacy(struct seq_file * sf,void * v)1004795fe54cSFam Zheng static int bfq_io_show_weight_legacy(struct seq_file *sf, void *v)
1005ea25da48SPaolo Valente {
1006ea25da48SPaolo Valente 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1007ea25da48SPaolo Valente 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
1008ea25da48SPaolo Valente 	unsigned int val = 0;
1009ea25da48SPaolo Valente 
1010ea25da48SPaolo Valente 	if (bfqgd)
1011ea25da48SPaolo Valente 		val = bfqgd->weight;
1012ea25da48SPaolo Valente 
1013ea25da48SPaolo Valente 	seq_printf(sf, "%u\n", val);
1014ea25da48SPaolo Valente 
1015ea25da48SPaolo Valente 	return 0;
1016ea25da48SPaolo Valente }
1017ea25da48SPaolo Valente 
bfqg_prfill_weight_device(struct seq_file * sf,struct blkg_policy_data * pd,int off)1018795fe54cSFam Zheng static u64 bfqg_prfill_weight_device(struct seq_file *sf,
1019795fe54cSFam Zheng 				     struct blkg_policy_data *pd, int off)
1020ea25da48SPaolo Valente {
1021795fe54cSFam Zheng 	struct bfq_group *bfqg = pd_to_bfqg(pd);
1022795fe54cSFam Zheng 
1023795fe54cSFam Zheng 	if (!bfqg->entity.dev_weight)
1024795fe54cSFam Zheng 		return 0;
1025795fe54cSFam Zheng 	return __blkg_prfill_u64(sf, pd, bfqg->entity.dev_weight);
1026795fe54cSFam Zheng }
1027795fe54cSFam Zheng 
bfq_io_show_weight(struct seq_file * sf,void * v)1028795fe54cSFam Zheng static int bfq_io_show_weight(struct seq_file *sf, void *v)
1029795fe54cSFam Zheng {
1030795fe54cSFam Zheng 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1031795fe54cSFam Zheng 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
1032795fe54cSFam Zheng 
1033795fe54cSFam Zheng 	seq_printf(sf, "default %u\n", bfqgd->weight);
1034795fe54cSFam Zheng 	blkcg_print_blkgs(sf, blkcg, bfqg_prfill_weight_device,
1035795fe54cSFam Zheng 			  &blkcg_policy_bfq, 0, false);
1036795fe54cSFam Zheng 	return 0;
1037795fe54cSFam Zheng }
1038795fe54cSFam Zheng 
bfq_group_set_weight(struct bfq_group * bfqg,u64 weight,u64 dev_weight)1039795fe54cSFam Zheng static void bfq_group_set_weight(struct bfq_group *bfqg, u64 weight, u64 dev_weight)
1040795fe54cSFam Zheng {
1041795fe54cSFam Zheng 	weight = dev_weight ?: weight;
1042795fe54cSFam Zheng 
1043795fe54cSFam Zheng 	bfqg->entity.dev_weight = dev_weight;
1044ea25da48SPaolo Valente 	/*
1045ea25da48SPaolo Valente 	 * Setting the prio_changed flag of the entity
1046ea25da48SPaolo Valente 	 * to 1 with new_weight == weight would re-set
1047ea25da48SPaolo Valente 	 * the value of the weight to its ioprio mapping.
1048ea25da48SPaolo Valente 	 * Set the flag only if necessary.
1049ea25da48SPaolo Valente 	 */
10505ff047e3SFam Zheng 	if ((unsigned short)weight != bfqg->entity.new_weight) {
10515ff047e3SFam Zheng 		bfqg->entity.new_weight = (unsigned short)weight;
1052ea25da48SPaolo Valente 		/*
1053ea25da48SPaolo Valente 		 * Make sure that the above new value has been
1054ea25da48SPaolo Valente 		 * stored in bfqg->entity.new_weight before
1055ea25da48SPaolo Valente 		 * setting the prio_changed flag. In fact,
1056ea25da48SPaolo Valente 		 * this flag may be read asynchronously (in
1057ea25da48SPaolo Valente 		 * critical sections protected by a different
1058ea25da48SPaolo Valente 		 * lock than that held here), and finding this
1059ea25da48SPaolo Valente 		 * flag set may cause the execution of the code
1060ea25da48SPaolo Valente 		 * for updating parameters whose value may
1061ea25da48SPaolo Valente 		 * depend also on bfqg->entity.new_weight (in
1062ea25da48SPaolo Valente 		 * __bfq_entity_update_weight_prio).
1063ea25da48SPaolo Valente 		 * This barrier makes sure that the new value
1064ea25da48SPaolo Valente 		 * of bfqg->entity.new_weight is correctly
1065ea25da48SPaolo Valente 		 * seen in that code.
1066ea25da48SPaolo Valente 		 */
1067ea25da48SPaolo Valente 		smp_wmb();
1068ea25da48SPaolo Valente 		bfqg->entity.prio_changed = 1;
1069ea25da48SPaolo Valente 	}
1070ea25da48SPaolo Valente }
10715ff047e3SFam Zheng 
bfq_io_set_weight_legacy(struct cgroup_subsys_state * css,struct cftype * cftype,u64 val)10725ff047e3SFam Zheng static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css,
10735ff047e3SFam Zheng 				    struct cftype *cftype,
10745ff047e3SFam Zheng 				    u64 val)
10755ff047e3SFam Zheng {
10765ff047e3SFam Zheng 	struct blkcg *blkcg = css_to_blkcg(css);
10775ff047e3SFam Zheng 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
10785ff047e3SFam Zheng 	struct blkcg_gq *blkg;
10795ff047e3SFam Zheng 	int ret = -ERANGE;
10805ff047e3SFam Zheng 
10815ff047e3SFam Zheng 	if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT)
10825ff047e3SFam Zheng 		return ret;
10835ff047e3SFam Zheng 
10845ff047e3SFam Zheng 	ret = 0;
10855ff047e3SFam Zheng 	spin_lock_irq(&blkcg->lock);
10865ff047e3SFam Zheng 	bfqgd->weight = (unsigned short)val;
10875ff047e3SFam Zheng 	hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
10885ff047e3SFam Zheng 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
10895ff047e3SFam Zheng 
10905ff047e3SFam Zheng 		if (bfqg)
1091795fe54cSFam Zheng 			bfq_group_set_weight(bfqg, val, 0);
10925ff047e3SFam Zheng 	}
1093ea25da48SPaolo Valente 	spin_unlock_irq(&blkcg->lock);
1094ea25da48SPaolo Valente 
1095ea25da48SPaolo Valente 	return ret;
1096ea25da48SPaolo Valente }
1097ea25da48SPaolo Valente 
bfq_io_set_device_weight(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1098795fe54cSFam Zheng static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of,
1099795fe54cSFam Zheng 					char *buf, size_t nbytes,
1100795fe54cSFam Zheng 					loff_t off)
1101795fe54cSFam Zheng {
1102795fe54cSFam Zheng 	int ret;
1103795fe54cSFam Zheng 	struct blkg_conf_ctx ctx;
1104795fe54cSFam Zheng 	struct blkcg *blkcg = css_to_blkcg(of_css(of));
1105795fe54cSFam Zheng 	struct bfq_group *bfqg;
1106795fe54cSFam Zheng 	u64 v;
1107795fe54cSFam Zheng 
1108*faffaab2STejun Heo 	blkg_conf_init(&ctx, buf);
1109*faffaab2STejun Heo 
1110*faffaab2STejun Heo 	ret = blkg_conf_prep(blkcg, &blkcg_policy_bfq, &ctx);
1111795fe54cSFam Zheng 	if (ret)
1112*faffaab2STejun Heo 		goto out;
1113795fe54cSFam Zheng 
1114795fe54cSFam Zheng 	if (sscanf(ctx.body, "%llu", &v) == 1) {
1115795fe54cSFam Zheng 		/* require "default" on dfl */
1116795fe54cSFam Zheng 		ret = -ERANGE;
1117795fe54cSFam Zheng 		if (!v)
1118795fe54cSFam Zheng 			goto out;
1119795fe54cSFam Zheng 	} else if (!strcmp(strim(ctx.body), "default")) {
1120795fe54cSFam Zheng 		v = 0;
1121795fe54cSFam Zheng 	} else {
1122795fe54cSFam Zheng 		ret = -EINVAL;
1123795fe54cSFam Zheng 		goto out;
1124795fe54cSFam Zheng 	}
1125795fe54cSFam Zheng 
1126795fe54cSFam Zheng 	bfqg = blkg_to_bfqg(ctx.blkg);
1127795fe54cSFam Zheng 
1128795fe54cSFam Zheng 	ret = -ERANGE;
1129795fe54cSFam Zheng 	if (!v || (v >= BFQ_MIN_WEIGHT && v <= BFQ_MAX_WEIGHT)) {
1130795fe54cSFam Zheng 		bfq_group_set_weight(bfqg, bfqg->entity.weight, v);
1131795fe54cSFam Zheng 		ret = 0;
1132795fe54cSFam Zheng 	}
1133795fe54cSFam Zheng out:
1134*faffaab2STejun Heo 	blkg_conf_exit(&ctx);
1135795fe54cSFam Zheng 	return ret ?: nbytes;
1136795fe54cSFam Zheng }
1137795fe54cSFam Zheng 
bfq_io_set_weight(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)1138ea25da48SPaolo Valente static ssize_t bfq_io_set_weight(struct kernfs_open_file *of,
1139ea25da48SPaolo Valente 				 char *buf, size_t nbytes,
1140ea25da48SPaolo Valente 				 loff_t off)
1141ea25da48SPaolo Valente {
1142795fe54cSFam Zheng 	char *endp;
1143795fe54cSFam Zheng 	int ret;
1144795fe54cSFam Zheng 	u64 v;
1145ea25da48SPaolo Valente 
1146795fe54cSFam Zheng 	buf = strim(buf);
1147ea25da48SPaolo Valente 
1148795fe54cSFam Zheng 	/* "WEIGHT" or "default WEIGHT" sets the default weight */
1149795fe54cSFam Zheng 	v = simple_strtoull(buf, &endp, 0);
1150795fe54cSFam Zheng 	if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
1151795fe54cSFam Zheng 		ret = bfq_io_set_weight_legacy(of_css(of), NULL, v);
1152fc8ebd01SMaciej S. Szmigiero 		return ret ?: nbytes;
1153ea25da48SPaolo Valente 	}
1154ea25da48SPaolo Valente 
1155795fe54cSFam Zheng 	return bfq_io_set_device_weight(of, buf, nbytes, off);
1156795fe54cSFam Zheng }
1157795fe54cSFam Zheng 
bfqg_print_rwstat(struct seq_file * sf,void * v)1158ea25da48SPaolo Valente static int bfqg_print_rwstat(struct seq_file *sf, void *v)
1159ea25da48SPaolo Valente {
1160ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1161ea25da48SPaolo Valente 			  &blkcg_policy_bfq, seq_cft(sf)->private, true);
1162ea25da48SPaolo Valente 	return 0;
1163ea25da48SPaolo Valente }
1164ea25da48SPaolo Valente 
bfqg_prfill_rwstat_recursive(struct seq_file * sf,struct blkg_policy_data * pd,int off)1165a557f1c7STejun Heo static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf,
1166a557f1c7STejun Heo 					struct blkg_policy_data *pd, int off)
1167a557f1c7STejun Heo {
1168a557f1c7STejun Heo 	struct blkg_rwstat_sample sum;
1169a557f1c7STejun Heo 
1170a557f1c7STejun Heo 	blkg_rwstat_recursive_sum(pd_to_blkg(pd), &blkcg_policy_bfq, off, &sum);
1171a557f1c7STejun Heo 	return __blkg_prfill_rwstat(sf, pd, &sum);
1172a557f1c7STejun Heo }
1173a557f1c7STejun Heo 
bfqg_print_rwstat_recursive(struct seq_file * sf,void * v)1174a557f1c7STejun Heo static int bfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
1175a557f1c7STejun Heo {
1176a557f1c7STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1177a557f1c7STejun Heo 			  bfqg_prfill_rwstat_recursive, &blkcg_policy_bfq,
1178a557f1c7STejun Heo 			  seq_cft(sf)->private, true);
1179a557f1c7STejun Heo 	return 0;
1180a557f1c7STejun Heo }
1181a557f1c7STejun Heo 
1182fd41e603STejun Heo #ifdef CONFIG_BFQ_CGROUP_DEBUG
bfqg_print_stat(struct seq_file * sf,void * v)1183a557f1c7STejun Heo static int bfqg_print_stat(struct seq_file *sf, void *v)
1184a557f1c7STejun Heo {
1185a557f1c7STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1186a557f1c7STejun Heo 			  &blkcg_policy_bfq, seq_cft(sf)->private, false);
1187a557f1c7STejun Heo 	return 0;
1188a557f1c7STejun Heo }
1189a557f1c7STejun Heo 
bfqg_prfill_stat_recursive(struct seq_file * sf,struct blkg_policy_data * pd,int off)1190ea25da48SPaolo Valente static u64 bfqg_prfill_stat_recursive(struct seq_file *sf,
1191ea25da48SPaolo Valente 				      struct blkg_policy_data *pd, int off)
1192ea25da48SPaolo Valente {
1193d6258980SChristoph Hellwig 	struct blkcg_gq *blkg = pd_to_blkg(pd);
1194d6258980SChristoph Hellwig 	struct blkcg_gq *pos_blkg;
1195d6258980SChristoph Hellwig 	struct cgroup_subsys_state *pos_css;
1196d6258980SChristoph Hellwig 	u64 sum = 0;
1197d6258980SChristoph Hellwig 
1198d6258980SChristoph Hellwig 	lockdep_assert_held(&blkg->q->queue_lock);
1199d6258980SChristoph Hellwig 
1200d6258980SChristoph Hellwig 	rcu_read_lock();
1201d6258980SChristoph Hellwig 	blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
1202d6258980SChristoph Hellwig 		struct bfq_stat *stat;
1203d6258980SChristoph Hellwig 
1204d6258980SChristoph Hellwig 		if (!pos_blkg->online)
1205d6258980SChristoph Hellwig 			continue;
1206d6258980SChristoph Hellwig 
1207d6258980SChristoph Hellwig 		stat = (void *)blkg_to_pd(pos_blkg, &blkcg_policy_bfq) + off;
1208d6258980SChristoph Hellwig 		sum += bfq_stat_read(stat) + atomic64_read(&stat->aux_cnt);
1209d6258980SChristoph Hellwig 	}
1210d6258980SChristoph Hellwig 	rcu_read_unlock();
1211d6258980SChristoph Hellwig 
1212ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum);
1213ea25da48SPaolo Valente }
1214ea25da48SPaolo Valente 
bfqg_print_stat_recursive(struct seq_file * sf,void * v)1215ea25da48SPaolo Valente static int bfqg_print_stat_recursive(struct seq_file *sf, void *v)
1216ea25da48SPaolo Valente {
1217ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1218ea25da48SPaolo Valente 			  bfqg_prfill_stat_recursive, &blkcg_policy_bfq,
1219ea25da48SPaolo Valente 			  seq_cft(sf)->private, false);
1220ea25da48SPaolo Valente 	return 0;
1221ea25da48SPaolo Valente }
1222ea25da48SPaolo Valente 
bfqg_prfill_sectors(struct seq_file * sf,struct blkg_policy_data * pd,int off)1223ea25da48SPaolo Valente static u64 bfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1224ea25da48SPaolo Valente 			       int off)
1225ea25da48SPaolo Valente {
1226fd41e603STejun Heo 	struct bfq_group *bfqg = blkg_to_bfqg(pd->blkg);
1227fd41e603STejun Heo 	u64 sum = blkg_rwstat_total(&bfqg->stats.bytes);
1228ea25da48SPaolo Valente 
1229ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum >> 9);
1230ea25da48SPaolo Valente }
1231ea25da48SPaolo Valente 
bfqg_print_stat_sectors(struct seq_file * sf,void * v)1232ea25da48SPaolo Valente static int bfqg_print_stat_sectors(struct seq_file *sf, void *v)
1233ea25da48SPaolo Valente {
1234ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1235ea25da48SPaolo Valente 			  bfqg_prfill_sectors, &blkcg_policy_bfq, 0, false);
1236ea25da48SPaolo Valente 	return 0;
1237ea25da48SPaolo Valente }
1238ea25da48SPaolo Valente 
bfqg_prfill_sectors_recursive(struct seq_file * sf,struct blkg_policy_data * pd,int off)1239ea25da48SPaolo Valente static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf,
1240ea25da48SPaolo Valente 					 struct blkg_policy_data *pd, int off)
1241ea25da48SPaolo Valente {
12427af6fd91SChristoph Hellwig 	struct blkg_rwstat_sample tmp;
12435d0b6e48SChristoph Hellwig 
1244fd41e603STejun Heo 	blkg_rwstat_recursive_sum(pd->blkg, &blkcg_policy_bfq,
1245fd41e603STejun Heo 			offsetof(struct bfq_group, stats.bytes), &tmp);
1246ea25da48SPaolo Valente 
12477af6fd91SChristoph Hellwig 	return __blkg_prfill_u64(sf, pd,
12487af6fd91SChristoph Hellwig 		(tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]) >> 9);
1249ea25da48SPaolo Valente }
1250ea25da48SPaolo Valente 
bfqg_print_stat_sectors_recursive(struct seq_file * sf,void * v)1251ea25da48SPaolo Valente static int bfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1252ea25da48SPaolo Valente {
1253ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1254ea25da48SPaolo Valente 			  bfqg_prfill_sectors_recursive, &blkcg_policy_bfq, 0,
1255ea25da48SPaolo Valente 			  false);
1256ea25da48SPaolo Valente 	return 0;
1257ea25da48SPaolo Valente }
1258ea25da48SPaolo Valente 
bfqg_prfill_avg_queue_size(struct seq_file * sf,struct blkg_policy_data * pd,int off)1259ea25da48SPaolo Valente static u64 bfqg_prfill_avg_queue_size(struct seq_file *sf,
1260ea25da48SPaolo Valente 				      struct blkg_policy_data *pd, int off)
1261ea25da48SPaolo Valente {
1262ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
1263c0ce79dcSChristoph Hellwig 	u64 samples = bfq_stat_read(&bfqg->stats.avg_queue_size_samples);
1264ea25da48SPaolo Valente 	u64 v = 0;
1265ea25da48SPaolo Valente 
1266ea25da48SPaolo Valente 	if (samples) {
1267c0ce79dcSChristoph Hellwig 		v = bfq_stat_read(&bfqg->stats.avg_queue_size_sum);
1268ea25da48SPaolo Valente 		v = div64_u64(v, samples);
1269ea25da48SPaolo Valente 	}
1270ea25da48SPaolo Valente 	__blkg_prfill_u64(sf, pd, v);
1271ea25da48SPaolo Valente 	return 0;
1272ea25da48SPaolo Valente }
1273ea25da48SPaolo Valente 
1274ea25da48SPaolo Valente /* print avg_queue_size */
bfqg_print_avg_queue_size(struct seq_file * sf,void * v)1275ea25da48SPaolo Valente static int bfqg_print_avg_queue_size(struct seq_file *sf, void *v)
1276ea25da48SPaolo Valente {
1277ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1278ea25da48SPaolo Valente 			  bfqg_prfill_avg_queue_size, &blkcg_policy_bfq,
1279ea25da48SPaolo Valente 			  0, false);
1280ea25da48SPaolo Valente 	return 0;
1281ea25da48SPaolo Valente }
12828060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
1283ea25da48SPaolo Valente 
bfq_create_group_hierarchy(struct bfq_data * bfqd,int node)1284ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1285ea25da48SPaolo Valente {
1286ea25da48SPaolo Valente 	int ret;
1287ea25da48SPaolo Valente 
128840e4996eSChristoph Hellwig 	ret = blkcg_activate_policy(bfqd->queue->disk, &blkcg_policy_bfq);
1289ea25da48SPaolo Valente 	if (ret)
1290ea25da48SPaolo Valente 		return NULL;
1291ea25da48SPaolo Valente 
1292ea25da48SPaolo Valente 	return blkg_to_bfqg(bfqd->queue->root_blkg);
1293ea25da48SPaolo Valente }
1294ea25da48SPaolo Valente 
1295ea25da48SPaolo Valente struct blkcg_policy blkcg_policy_bfq = {
1296ea25da48SPaolo Valente 	.dfl_cftypes		= bfq_blkg_files,
1297ea25da48SPaolo Valente 	.legacy_cftypes		= bfq_blkcg_legacy_files,
1298ea25da48SPaolo Valente 
1299ea25da48SPaolo Valente 	.cpd_alloc_fn		= bfq_cpd_alloc,
1300ea25da48SPaolo Valente 	.cpd_free_fn		= bfq_cpd_free,
1301ea25da48SPaolo Valente 
1302ea25da48SPaolo Valente 	.pd_alloc_fn		= bfq_pd_alloc,
1303ea25da48SPaolo Valente 	.pd_init_fn		= bfq_pd_init,
1304ea25da48SPaolo Valente 	.pd_offline_fn		= bfq_pd_offline,
1305ea25da48SPaolo Valente 	.pd_free_fn		= bfq_pd_free,
1306ea25da48SPaolo Valente 	.pd_reset_stats_fn	= bfq_pd_reset_stats,
1307ea25da48SPaolo Valente };
1308ea25da48SPaolo Valente 
1309ea25da48SPaolo Valente struct cftype bfq_blkcg_legacy_files[] = {
1310ea25da48SPaolo Valente 	{
1311ea25da48SPaolo Valente 		.name = "bfq.weight",
1312cf892988SJens Axboe 		.flags = CFTYPE_NOT_ON_ROOT,
1313795fe54cSFam Zheng 		.seq_show = bfq_io_show_weight_legacy,
1314ea25da48SPaolo Valente 		.write_u64 = bfq_io_set_weight_legacy,
1315ea25da48SPaolo Valente 	},
1316795fe54cSFam Zheng 	{
1317795fe54cSFam Zheng 		.name = "bfq.weight_device",
1318795fe54cSFam Zheng 		.flags = CFTYPE_NOT_ON_ROOT,
1319795fe54cSFam Zheng 		.seq_show = bfq_io_show_weight,
1320795fe54cSFam Zheng 		.write = bfq_io_set_weight,
1321795fe54cSFam Zheng 	},
1322ea25da48SPaolo Valente 
1323ea25da48SPaolo Valente 	/* statistics, covers only the tasks in the bfqg */
1324ea25da48SPaolo Valente 	{
1325ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes",
1326fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.bytes),
1327fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat,
1328ea25da48SPaolo Valente 	},
1329ea25da48SPaolo Valente 	{
1330ea25da48SPaolo Valente 		.name = "bfq.io_serviced",
1331fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.ios),
1332fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat,
1333ea25da48SPaolo Valente 	},
13348060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
1335a33801e8SLuca Miccio 	{
1336a33801e8SLuca Miccio 		.name = "bfq.time",
1337a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1338a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat,
1339a33801e8SLuca Miccio 	},
1340a33801e8SLuca Miccio 	{
1341a33801e8SLuca Miccio 		.name = "bfq.sectors",
1342a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors,
1343a33801e8SLuca Miccio 	},
1344ea25da48SPaolo Valente 	{
1345ea25da48SPaolo Valente 		.name = "bfq.io_service_time",
1346ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1347ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1348ea25da48SPaolo Valente 	},
1349ea25da48SPaolo Valente 	{
1350ea25da48SPaolo Valente 		.name = "bfq.io_wait_time",
1351ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1352ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1353ea25da48SPaolo Valente 	},
1354ea25da48SPaolo Valente 	{
1355ea25da48SPaolo Valente 		.name = "bfq.io_merged",
1356ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1357ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1358ea25da48SPaolo Valente 	},
1359ea25da48SPaolo Valente 	{
1360ea25da48SPaolo Valente 		.name = "bfq.io_queued",
1361ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1362ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1363ea25da48SPaolo Valente 	},
13648060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
1365ea25da48SPaolo Valente 
1366636b8fe8SAngelo Ruocco 	/* the same statistics which cover the bfqg and its descendants */
1367ea25da48SPaolo Valente 	{
1368ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes_recursive",
1369fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.bytes),
1370fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat_recursive,
1371ea25da48SPaolo Valente 	},
1372ea25da48SPaolo Valente 	{
1373ea25da48SPaolo Valente 		.name = "bfq.io_serviced_recursive",
1374fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.ios),
1375fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat_recursive,
1376ea25da48SPaolo Valente 	},
13778060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
1378a33801e8SLuca Miccio 	{
1379a33801e8SLuca Miccio 		.name = "bfq.time_recursive",
1380a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1381a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_recursive,
1382a33801e8SLuca Miccio 	},
1383a33801e8SLuca Miccio 	{
1384a33801e8SLuca Miccio 		.name = "bfq.sectors_recursive",
1385a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors_recursive,
1386a33801e8SLuca Miccio 	},
1387ea25da48SPaolo Valente 	{
1388ea25da48SPaolo Valente 		.name = "bfq.io_service_time_recursive",
1389ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1390ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1391ea25da48SPaolo Valente 	},
1392ea25da48SPaolo Valente 	{
1393ea25da48SPaolo Valente 		.name = "bfq.io_wait_time_recursive",
1394ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1395ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1396ea25da48SPaolo Valente 	},
1397ea25da48SPaolo Valente 	{
1398ea25da48SPaolo Valente 		.name = "bfq.io_merged_recursive",
1399ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1400ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1401ea25da48SPaolo Valente 	},
1402ea25da48SPaolo Valente 	{
1403ea25da48SPaolo Valente 		.name = "bfq.io_queued_recursive",
1404ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1405ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1406ea25da48SPaolo Valente 	},
1407ea25da48SPaolo Valente 	{
1408ea25da48SPaolo Valente 		.name = "bfq.avg_queue_size",
1409ea25da48SPaolo Valente 		.seq_show = bfqg_print_avg_queue_size,
1410ea25da48SPaolo Valente 	},
1411ea25da48SPaolo Valente 	{
1412ea25da48SPaolo Valente 		.name = "bfq.group_wait_time",
1413ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.group_wait_time),
1414ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1415ea25da48SPaolo Valente 	},
1416ea25da48SPaolo Valente 	{
1417ea25da48SPaolo Valente 		.name = "bfq.idle_time",
1418ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.idle_time),
1419ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1420ea25da48SPaolo Valente 	},
1421ea25da48SPaolo Valente 	{
1422ea25da48SPaolo Valente 		.name = "bfq.empty_time",
1423ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.empty_time),
1424ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1425ea25da48SPaolo Valente 	},
1426ea25da48SPaolo Valente 	{
1427ea25da48SPaolo Valente 		.name = "bfq.dequeue",
1428ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.dequeue),
1429ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1430ea25da48SPaolo Valente 	},
14318060c47bSChristoph Hellwig #endif	/* CONFIG_BFQ_CGROUP_DEBUG */
1432ea25da48SPaolo Valente 	{ }	/* terminate */
1433ea25da48SPaolo Valente };
1434ea25da48SPaolo Valente 
1435ea25da48SPaolo Valente struct cftype bfq_blkg_files[] = {
1436ea25da48SPaolo Valente 	{
1437ea25da48SPaolo Valente 		.name = "bfq.weight",
1438cf892988SJens Axboe 		.flags = CFTYPE_NOT_ON_ROOT,
1439ea25da48SPaolo Valente 		.seq_show = bfq_io_show_weight,
1440ea25da48SPaolo Valente 		.write = bfq_io_set_weight,
1441ea25da48SPaolo Valente 	},
1442ea25da48SPaolo Valente 	{} /* terminate */
1443ea25da48SPaolo Valente };
1444ea25da48SPaolo Valente 
1445ea25da48SPaolo Valente #else	/* CONFIG_BFQ_GROUP_IOSCHED */
1446ea25da48SPaolo Valente 
bfq_bfqq_move(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct bfq_group * bfqg)1447ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1448ea25da48SPaolo Valente 		   struct bfq_group *bfqg) {}
1449ea25da48SPaolo Valente 
bfq_init_entity(struct bfq_entity * entity,struct bfq_group * bfqg)1450ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
1451ea25da48SPaolo Valente {
1452ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
1453ea25da48SPaolo Valente 
1454ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
1455ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
1456ea25da48SPaolo Valente 	if (bfqq) {
1457ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
1458ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
1459ea25da48SPaolo Valente 	}
1460ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
1461ea25da48SPaolo Valente }
1462ea25da48SPaolo Valente 
bfq_bic_update_cgroup(struct bfq_io_cq * bic,struct bio * bio)1463ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {}
1464ea25da48SPaolo Valente 
bfq_end_wr_async(struct bfq_data * bfqd)1465ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
1466ea25da48SPaolo Valente {
1467ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1468ea25da48SPaolo Valente }
1469ea25da48SPaolo Valente 
bfq_bio_bfqg(struct bfq_data * bfqd,struct bio * bio)14704e54a249SJan Kara struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio)
1471ea25da48SPaolo Valente {
1472ea25da48SPaolo Valente 	return bfqd->root_group;
1473ea25da48SPaolo Valente }
1474ea25da48SPaolo Valente 
bfqq_group(struct bfq_queue * bfqq)1475ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
1476ea25da48SPaolo Valente {
1477ea25da48SPaolo Valente 	return bfqq->bfqd->root_group;
1478ea25da48SPaolo Valente }
1479ea25da48SPaolo Valente 
bfqg_and_blkg_put(struct bfq_group * bfqg)14804d8340d0SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg) {}
14814d8340d0SPaolo Valente 
bfq_create_group_hierarchy(struct bfq_data * bfqd,int node)1482ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1483ea25da48SPaolo Valente {
1484ea25da48SPaolo Valente 	struct bfq_group *bfqg;
1485ea25da48SPaolo Valente 	int i;
1486ea25da48SPaolo Valente 
1487ea25da48SPaolo Valente 	bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
1488ea25da48SPaolo Valente 	if (!bfqg)
1489ea25da48SPaolo Valente 		return NULL;
1490ea25da48SPaolo Valente 
1491ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
1492ea25da48SPaolo Valente 		bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
1493ea25da48SPaolo Valente 
1494ea25da48SPaolo Valente 	return bfqg;
1495ea25da48SPaolo Valente }
1496ea25da48SPaolo Valente #endif	/* CONFIG_BFQ_GROUP_IOSCHED */
1497