xref: /openbmc/linux/block/bfq-cgroup.c (revision 452af7dc)
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
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,
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 
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 
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 
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 
257dc469ba2SBart Van Assche void bfqg_stats_update_io_remove(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) { }
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) { }
261a33801e8SLuca Miccio void bfqg_stats_update_dequeue(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 
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 
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 
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 
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 
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 
317ea25da48SPaolo Valente static void bfqg_get(struct bfq_group *bfqg)
318ea25da48SPaolo Valente {
3198f9bebc3SPaolo Valente 	bfqg->ref++;
320ea25da48SPaolo Valente }
321ea25da48SPaolo Valente 
322dfb79af5SBart Van Assche static void bfqg_put(struct bfq_group *bfqg)
323ea25da48SPaolo Valente {
3248f9bebc3SPaolo Valente 	bfqg->ref--;
3258f9bebc3SPaolo Valente 
3268f9bebc3SPaolo Valente 	if (bfqg->ref == 0)
3278f9bebc3SPaolo Valente 		kfree(bfqg);
3288f9bebc3SPaolo Valente }
3298f9bebc3SPaolo Valente 
3302de791abSDmitry Monakhov static void bfqg_and_blkg_get(struct bfq_group *bfqg)
3318f9bebc3SPaolo Valente {
3328f9bebc3SPaolo Valente 	/* see comments in bfq_bic_update_cgroup for why refcounting bfqg */
3338f9bebc3SPaolo Valente 	bfqg_get(bfqg);
3348f9bebc3SPaolo Valente 
3358f9bebc3SPaolo Valente 	blkg_get(bfqg_to_blkg(bfqg));
3368f9bebc3SPaolo Valente }
3378f9bebc3SPaolo Valente 
3388f9bebc3SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg)
3398f9bebc3SPaolo Valente {
3408f9bebc3SPaolo Valente 	blkg_put(bfqg_to_blkg(bfqg));
341d5274b3cSKonstantin Khlebnikov 
342d5274b3cSKonstantin Khlebnikov 	bfqg_put(bfqg);
343ea25da48SPaolo Valente }
344ea25da48SPaolo Valente 
345fd41e603STejun Heo void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq)
346fd41e603STejun Heo {
347fd41e603STejun Heo 	struct bfq_group *bfqg = blkg_to_bfqg(rq->bio->bi_blkg);
348fd41e603STejun Heo 
34908802ed6SHou Tao 	if (!bfqg)
35008802ed6SHou Tao 		return;
35108802ed6SHou Tao 
352fd41e603STejun Heo 	blkg_rwstat_add(&bfqg->stats.bytes, rq->cmd_flags, blk_rq_bytes(rq));
353fd41e603STejun Heo 	blkg_rwstat_add(&bfqg->stats.ios, rq->cmd_flags, 1);
354fd41e603STejun Heo }
355fd41e603STejun Heo 
356ea25da48SPaolo Valente /* @stats = 0 */
357ea25da48SPaolo Valente static void bfqg_stats_reset(struct bfqg_stats *stats)
358ea25da48SPaolo Valente {
3598060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
360ea25da48SPaolo Valente 	/* queued stats shouldn't be cleared */
361ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->merged);
362ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->service_time);
363ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->wait_time);
364c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->time);
365c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->avg_queue_size_sum);
366c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->avg_queue_size_samples);
367c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->dequeue);
368c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->group_wait_time);
369c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->idle_time);
370c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->empty_time);
371a33801e8SLuca Miccio #endif
372ea25da48SPaolo Valente }
373ea25da48SPaolo Valente 
374ea25da48SPaolo Valente /* @to += @from */
375ea25da48SPaolo Valente static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from)
376ea25da48SPaolo Valente {
377ea25da48SPaolo Valente 	if (!to || !from)
378ea25da48SPaolo Valente 		return;
379ea25da48SPaolo Valente 
3808060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
381ea25da48SPaolo Valente 	/* queued stats shouldn't be cleared */
382ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->merged, &from->merged);
383ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->service_time, &from->service_time);
384ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
385c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&from->time, &from->time);
386c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
387c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->avg_queue_size_samples,
388ea25da48SPaolo Valente 			  &from->avg_queue_size_samples);
389c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->dequeue, &from->dequeue);
390c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
391c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->idle_time, &from->idle_time);
392c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->empty_time, &from->empty_time);
393a33801e8SLuca Miccio #endif
394ea25da48SPaolo Valente }
395ea25da48SPaolo Valente 
396ea25da48SPaolo Valente /*
397ea25da48SPaolo Valente  * Transfer @bfqg's stats to its parent's aux counts so that the ancestors'
398ea25da48SPaolo Valente  * recursive stats can still account for the amount used by this bfqg after
399ea25da48SPaolo Valente  * it's gone.
400ea25da48SPaolo Valente  */
401ea25da48SPaolo Valente static void bfqg_stats_xfer_dead(struct bfq_group *bfqg)
402ea25da48SPaolo Valente {
403ea25da48SPaolo Valente 	struct bfq_group *parent;
404ea25da48SPaolo Valente 
405ea25da48SPaolo Valente 	if (!bfqg) /* root_group */
406ea25da48SPaolo Valente 		return;
407ea25da48SPaolo Valente 
408ea25da48SPaolo Valente 	parent = bfqg_parent(bfqg);
409ea25da48SPaolo Valente 
4100d945c1fSChristoph Hellwig 	lockdep_assert_held(&bfqg_to_blkg(bfqg)->q->queue_lock);
411ea25da48SPaolo Valente 
412ea25da48SPaolo Valente 	if (unlikely(!parent))
413ea25da48SPaolo Valente 		return;
414ea25da48SPaolo Valente 
415ea25da48SPaolo Valente 	bfqg_stats_add_aux(&parent->stats, &bfqg->stats);
416ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
417ea25da48SPaolo Valente }
418ea25da48SPaolo Valente 
419ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
420ea25da48SPaolo Valente {
421ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
422ea25da48SPaolo Valente 
423ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
424ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
425ea25da48SPaolo Valente 	if (bfqq) {
426ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
427ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
4288f9bebc3SPaolo Valente 		/*
4298f9bebc3SPaolo Valente 		 * Make sure that bfqg and its associated blkg do not
4308f9bebc3SPaolo Valente 		 * disappear before entity.
4318f9bebc3SPaolo Valente 		 */
4328f9bebc3SPaolo Valente 		bfqg_and_blkg_get(bfqg);
433ea25da48SPaolo Valente 	}
434ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity; /* NULL for root group */
435ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
436ea25da48SPaolo Valente }
437ea25da48SPaolo Valente 
438ea25da48SPaolo Valente static void bfqg_stats_exit(struct bfqg_stats *stats)
439ea25da48SPaolo Valente {
440fd41e603STejun Heo 	blkg_rwstat_exit(&stats->bytes);
441fd41e603STejun Heo 	blkg_rwstat_exit(&stats->ios);
4428060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
443ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->merged);
444ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->service_time);
445ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->wait_time);
446ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->queued);
447c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->time);
448c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->avg_queue_size_sum);
449c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->avg_queue_size_samples);
450c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->dequeue);
451c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->group_wait_time);
452c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->idle_time);
453c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->empty_time);
454a33801e8SLuca Miccio #endif
455ea25da48SPaolo Valente }
456ea25da48SPaolo Valente 
457ea25da48SPaolo Valente static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp)
458ea25da48SPaolo Valente {
459fd41e603STejun Heo 	if (blkg_rwstat_init(&stats->bytes, gfp) ||
460fd41e603STejun Heo 	    blkg_rwstat_init(&stats->ios, gfp))
4612fc428f6SZheng Liang 		goto error;
462fd41e603STejun Heo 
4638060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
464ea25da48SPaolo Valente 	if (blkg_rwstat_init(&stats->merged, gfp) ||
465ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->service_time, gfp) ||
466ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->wait_time, gfp) ||
467ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->queued, gfp) ||
468c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->time, gfp) ||
469c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->avg_queue_size_sum, gfp) ||
470c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->avg_queue_size_samples, gfp) ||
471c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->dequeue, gfp) ||
472c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->group_wait_time, gfp) ||
473c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->idle_time, gfp) ||
4742fc428f6SZheng Liang 	    bfq_stat_init(&stats->empty_time, gfp))
4752fc428f6SZheng Liang 		goto error;
476a33801e8SLuca Miccio #endif
477ea25da48SPaolo Valente 
478ea25da48SPaolo Valente 	return 0;
4792fc428f6SZheng Liang 
4802fc428f6SZheng Liang error:
4812fc428f6SZheng Liang 	bfqg_stats_exit(stats);
4822fc428f6SZheng Liang 	return -ENOMEM;
483ea25da48SPaolo Valente }
484ea25da48SPaolo Valente 
485ea25da48SPaolo Valente static struct bfq_group_data *cpd_to_bfqgd(struct blkcg_policy_data *cpd)
486ea25da48SPaolo Valente {
487ea25da48SPaolo Valente 	return cpd ? container_of(cpd, struct bfq_group_data, pd) : NULL;
488ea25da48SPaolo Valente }
489ea25da48SPaolo Valente 
490ea25da48SPaolo Valente static struct bfq_group_data *blkcg_to_bfqgd(struct blkcg *blkcg)
491ea25da48SPaolo Valente {
492ea25da48SPaolo Valente 	return cpd_to_bfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_bfq));
493ea25da48SPaolo Valente }
494ea25da48SPaolo Valente 
495dfb79af5SBart Van Assche static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp)
496ea25da48SPaolo Valente {
497ea25da48SPaolo Valente 	struct bfq_group_data *bgd;
498ea25da48SPaolo Valente 
499ea25da48SPaolo Valente 	bgd = kzalloc(sizeof(*bgd), gfp);
500ea25da48SPaolo Valente 	if (!bgd)
501ea25da48SPaolo Valente 		return NULL;
502ea25da48SPaolo Valente 	return &bgd->pd;
503ea25da48SPaolo Valente }
504ea25da48SPaolo Valente 
505dfb79af5SBart Van Assche static void bfq_cpd_init(struct blkcg_policy_data *cpd)
506ea25da48SPaolo Valente {
507ea25da48SPaolo Valente 	struct bfq_group_data *d = cpd_to_bfqgd(cpd);
508ea25da48SPaolo Valente 
509ea25da48SPaolo Valente 	d->weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
510ea25da48SPaolo Valente 		CGROUP_WEIGHT_DFL : BFQ_WEIGHT_LEGACY_DFL;
511ea25da48SPaolo Valente }
512ea25da48SPaolo Valente 
513dfb79af5SBart Van Assche static void bfq_cpd_free(struct blkcg_policy_data *cpd)
514ea25da48SPaolo Valente {
515ea25da48SPaolo Valente 	kfree(cpd_to_bfqgd(cpd));
516ea25da48SPaolo Valente }
517ea25da48SPaolo Valente 
518cf09a8eeSTejun Heo static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, struct request_queue *q,
519cf09a8eeSTejun Heo 					     struct blkcg *blkcg)
520ea25da48SPaolo Valente {
521ea25da48SPaolo Valente 	struct bfq_group *bfqg;
522ea25da48SPaolo Valente 
523cf09a8eeSTejun Heo 	bfqg = kzalloc_node(sizeof(*bfqg), gfp, q->node);
524ea25da48SPaolo Valente 	if (!bfqg)
525ea25da48SPaolo Valente 		return NULL;
526ea25da48SPaolo Valente 
527ea25da48SPaolo Valente 	if (bfqg_stats_init(&bfqg->stats, gfp)) {
528ea25da48SPaolo Valente 		kfree(bfqg);
529ea25da48SPaolo Valente 		return NULL;
530ea25da48SPaolo Valente 	}
531ea25da48SPaolo Valente 
5328f9bebc3SPaolo Valente 	/* see comments in bfq_bic_update_cgroup for why refcounting */
5338f9bebc3SPaolo Valente 	bfqg_get(bfqg);
534ea25da48SPaolo Valente 	return &bfqg->pd;
535ea25da48SPaolo Valente }
536ea25da48SPaolo Valente 
537dfb79af5SBart Van Assche static void bfq_pd_init(struct blkg_policy_data *pd)
538ea25da48SPaolo Valente {
539ea25da48SPaolo Valente 	struct blkcg_gq *blkg = pd_to_blkg(pd);
540ea25da48SPaolo Valente 	struct bfq_group *bfqg = blkg_to_bfqg(blkg);
541ea25da48SPaolo Valente 	struct bfq_data *bfqd = blkg->q->elevator->elevator_data;
542ea25da48SPaolo Valente 	struct bfq_entity *entity = &bfqg->entity;
543ea25da48SPaolo Valente 	struct bfq_group_data *d = blkcg_to_bfqgd(blkg->blkcg);
544ea25da48SPaolo Valente 
545ea25da48SPaolo Valente 	entity->orig_weight = entity->weight = entity->new_weight = d->weight;
546ea25da48SPaolo Valente 	entity->my_sched_data = &bfqg->sched_data;
547430a67f9SPaolo Valente 	entity->last_bfqq_created = NULL;
548430a67f9SPaolo Valente 
549ea25da48SPaolo Valente 	bfqg->my_entity = entity; /*
550ea25da48SPaolo Valente 				   * the root_group's will be set to NULL
551ea25da48SPaolo Valente 				   * in bfq_init_queue()
552ea25da48SPaolo Valente 				   */
553ea25da48SPaolo Valente 	bfqg->bfqd = bfqd;
554ea25da48SPaolo Valente 	bfqg->active_entities = 0;
55560a6e10cSYu Kuai 	bfqg->num_queues_with_pending_reqs = 0;
55609f87186SJan Kara 	bfqg->online = true;
557ea25da48SPaolo Valente 	bfqg->rq_pos_tree = RB_ROOT;
558ea25da48SPaolo Valente }
559ea25da48SPaolo Valente 
560dfb79af5SBart Van Assche static void bfq_pd_free(struct blkg_policy_data *pd)
561ea25da48SPaolo Valente {
562ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
563ea25da48SPaolo Valente 
564ea25da48SPaolo Valente 	bfqg_stats_exit(&bfqg->stats);
5658f9bebc3SPaolo Valente 	bfqg_put(bfqg);
566ea25da48SPaolo Valente }
567ea25da48SPaolo Valente 
568dfb79af5SBart Van Assche static void bfq_pd_reset_stats(struct blkg_policy_data *pd)
569ea25da48SPaolo Valente {
570ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
571ea25da48SPaolo Valente 
572ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
573ea25da48SPaolo Valente }
574ea25da48SPaolo Valente 
575ea25da48SPaolo Valente static void bfq_group_set_parent(struct bfq_group *bfqg,
576ea25da48SPaolo Valente 					struct bfq_group *parent)
577ea25da48SPaolo Valente {
578ea25da48SPaolo Valente 	struct bfq_entity *entity;
579ea25da48SPaolo Valente 
580ea25da48SPaolo Valente 	entity = &bfqg->entity;
581ea25da48SPaolo Valente 	entity->parent = parent->my_entity;
582ea25da48SPaolo Valente 	entity->sched_data = &parent->sched_data;
583ea25da48SPaolo Valente }
584ea25da48SPaolo Valente 
5854e54a249SJan Kara static void bfq_link_bfqg(struct bfq_data *bfqd, struct bfq_group *bfqg)
586ea25da48SPaolo Valente {
5874e54a249SJan Kara 	struct bfq_group *parent;
588ea25da48SPaolo Valente 	struct bfq_entity *entity;
589ea25da48SPaolo Valente 
590ea25da48SPaolo Valente 	/*
591ea25da48SPaolo Valente 	 * Update chain of bfq_groups as we might be handling a leaf group
592ea25da48SPaolo Valente 	 * which, along with some of its relatives, has not been hooked yet
593ea25da48SPaolo Valente 	 * to the private hierarchy of BFQ.
594ea25da48SPaolo Valente 	 */
595ea25da48SPaolo Valente 	entity = &bfqg->entity;
596ea25da48SPaolo Valente 	for_each_entity(entity) {
59714afc593SCarlo Nonato 		struct bfq_group *curr_bfqg = container_of(entity,
59814afc593SCarlo Nonato 						struct bfq_group, entity);
59914afc593SCarlo Nonato 		if (curr_bfqg != bfqd->root_group) {
60014afc593SCarlo Nonato 			parent = bfqg_parent(curr_bfqg);
601ea25da48SPaolo Valente 			if (!parent)
602ea25da48SPaolo Valente 				parent = bfqd->root_group;
60314afc593SCarlo Nonato 			bfq_group_set_parent(curr_bfqg, parent);
604ea25da48SPaolo Valente 		}
605ea25da48SPaolo Valente 	}
6064e54a249SJan Kara }
607ea25da48SPaolo Valente 
6084e54a249SJan Kara struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio)
6094e54a249SJan Kara {
6104e54a249SJan Kara 	struct blkcg_gq *blkg = bio->bi_blkg;
611075a53b7SJan Kara 	struct bfq_group *bfqg;
6124e54a249SJan Kara 
613075a53b7SJan Kara 	while (blkg) {
614f02be900SYu Kuai 		if (!blkg->online) {
615f02be900SYu Kuai 			blkg = blkg->parent;
616f02be900SYu Kuai 			continue;
617f02be900SYu Kuai 		}
618075a53b7SJan Kara 		bfqg = blkg_to_bfqg(blkg);
619075a53b7SJan Kara 		if (bfqg->online) {
620075a53b7SJan Kara 			bio_associate_blkg_from_css(bio, &blkg->blkcg->css);
621075a53b7SJan Kara 			return bfqg;
622075a53b7SJan Kara 		}
623075a53b7SJan Kara 		blkg = blkg->parent;
624075a53b7SJan Kara 	}
625075a53b7SJan Kara 	bio_associate_blkg_from_css(bio,
626075a53b7SJan Kara 				&bfqg_to_blkg(bfqd->root_group)->blkcg->css);
6274e54a249SJan Kara 	return bfqd->root_group;
628ea25da48SPaolo Valente }
629ea25da48SPaolo Valente 
630ea25da48SPaolo Valente /**
631ea25da48SPaolo Valente  * bfq_bfqq_move - migrate @bfqq to @bfqg.
632ea25da48SPaolo Valente  * @bfqd: queue descriptor.
633ea25da48SPaolo Valente  * @bfqq: the queue to move.
634ea25da48SPaolo Valente  * @bfqg: the group to move to.
635ea25da48SPaolo Valente  *
636ea25da48SPaolo Valente  * Move @bfqq to @bfqg, deactivating it from its old group and reactivating
637ea25da48SPaolo Valente  * it on the new one.  Avoid putting the entity on the old group idle tree.
638ea25da48SPaolo Valente  *
6398f9bebc3SPaolo Valente  * Must be called under the scheduler lock, to make sure that the blkg
6408f9bebc3SPaolo Valente  * owning @bfqg does not disappear (see comments in
6418f9bebc3SPaolo Valente  * bfq_bic_update_cgroup on guaranteeing the consistency of blkg
6428f9bebc3SPaolo Valente  * objects).
643ea25da48SPaolo Valente  */
644ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
645ea25da48SPaolo Valente 		   struct bfq_group *bfqg)
646ea25da48SPaolo Valente {
647ea25da48SPaolo Valente 	struct bfq_entity *entity = &bfqq->entity;
648c5e4cb0fSYu Kuai 	struct bfq_group *old_parent = bfqq_group(bfqq);
64960a6e10cSYu Kuai 	bool has_pending_reqs = false;
650c5e4cb0fSYu Kuai 
651c5e4cb0fSYu Kuai 	/*
652c5e4cb0fSYu Kuai 	 * No point to move bfqq to the same group, which can happen when
653c5e4cb0fSYu Kuai 	 * root group is offlined
654c5e4cb0fSYu Kuai 	 */
655c5e4cb0fSYu Kuai 	if (old_parent == bfqg)
656c5e4cb0fSYu Kuai 		return;
657ea25da48SPaolo Valente 
658fd1bb3aeSPaolo Valente 	/*
6598410f709SYu Kuai 	 * oom_bfqq is not allowed to move, oom_bfqq will hold ref to root_group
6608410f709SYu Kuai 	 * until elevator exit.
6618410f709SYu Kuai 	 */
6628410f709SYu Kuai 	if (bfqq == &bfqd->oom_bfqq)
6638410f709SYu Kuai 		return;
6648410f709SYu Kuai 	/*
665fd1bb3aeSPaolo Valente 	 * Get extra reference to prevent bfqq from being freed in
666fd1bb3aeSPaolo Valente 	 * next possible expire or deactivate.
667fd1bb3aeSPaolo Valente 	 */
668fd1bb3aeSPaolo Valente 	bfqq->ref++;
669fd1bb3aeSPaolo Valente 
67060a6e10cSYu Kuai 	if (entity->in_groups_with_pending_reqs) {
67160a6e10cSYu Kuai 		has_pending_reqs = true;
67260a6e10cSYu Kuai 		bfq_del_bfqq_in_groups_with_pending_reqs(bfqq);
67360a6e10cSYu Kuai 	}
67460a6e10cSYu Kuai 
675ea25da48SPaolo Valente 	/* If bfqq is empty, then bfq_bfqq_expire also invokes
676ea25da48SPaolo Valente 	 * bfq_del_bfqq_busy, thereby removing bfqq and its entity
677ea25da48SPaolo Valente 	 * from data structures related to current group. Otherwise we
678ea25da48SPaolo Valente 	 * need to remove bfqq explicitly with bfq_deactivate_bfqq, as
679ea25da48SPaolo Valente 	 * we do below.
680ea25da48SPaolo Valente 	 */
681ea25da48SPaolo Valente 	if (bfqq == bfqd->in_service_queue)
682ea25da48SPaolo Valente 		bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
683ea25da48SPaolo Valente 				false, BFQQE_PREEMPTED);
684ea25da48SPaolo Valente 
685ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq))
686ea25da48SPaolo Valente 		bfq_deactivate_bfqq(bfqd, bfqq, false, false);
68733a16a98SPaolo Valente 	else if (entity->on_st_or_in_serv)
688ea25da48SPaolo Valente 		bfq_put_idle_entity(bfq_entity_service_tree(entity), entity);
689c5e4cb0fSYu Kuai 	bfqg_and_blkg_put(old_parent);
690ea25da48SPaolo Valente 
691d29bd414SPaolo Valente 	if (entity->parent &&
692d29bd414SPaolo Valente 	    entity->parent->last_bfqq_created == bfqq)
693d29bd414SPaolo Valente 		entity->parent->last_bfqq_created = NULL;
694d29bd414SPaolo Valente 	else if (bfqd->last_bfqq_created == bfqq)
695d29bd414SPaolo Valente 		bfqd->last_bfqq_created = NULL;
696d29bd414SPaolo Valente 
697ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity;
698ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
6998f9bebc3SPaolo Valente 	/* pin down bfqg and its associated blkg  */
7008f9bebc3SPaolo Valente 	bfqg_and_blkg_get(bfqg);
701ea25da48SPaolo Valente 
70260a6e10cSYu Kuai 	if (has_pending_reqs)
70360a6e10cSYu Kuai 		bfq_add_bfqq_in_groups_with_pending_reqs(bfqq);
70460a6e10cSYu Kuai 
705ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq)) {
7068cacc5abSPaolo Valente 		if (unlikely(!bfqd->nonrot_with_queueing))
707ea25da48SPaolo Valente 			bfq_pos_tree_add_move(bfqd, bfqq);
708ea25da48SPaolo Valente 		bfq_activate_bfqq(bfqd, bfqq);
709ea25da48SPaolo Valente 	}
710ea25da48SPaolo Valente 
711ea25da48SPaolo Valente 	if (!bfqd->in_service_queue && !bfqd->rq_in_driver)
712ea25da48SPaolo Valente 		bfq_schedule_dispatch(bfqd);
713fd1bb3aeSPaolo Valente 	/* release extra ref taken above, bfqq may happen to be freed now */
714ecedd3d7SPaolo Valente 	bfq_put_queue(bfqq);
715ea25da48SPaolo Valente }
716ea25da48SPaolo Valente 
717ea25da48SPaolo Valente /**
7181d87be82SBart Van Assche  * __bfq_bic_change_cgroup - move @bic to @bfqg.
719ea25da48SPaolo Valente  * @bfqd: the queue descriptor.
720ea25da48SPaolo Valente  * @bic: the bic to move.
7211d87be82SBart Van Assche  * @bfqg: the group to move to.
722ea25da48SPaolo Valente  *
7238f9bebc3SPaolo Valente  * Move bic to blkcg, assuming that bfqd->lock is held; which makes
7248f9bebc3SPaolo Valente  * sure that the reference to cgroup is valid across the call (see
7258f9bebc3SPaolo Valente  * comments in bfq_bic_update_cgroup on this issue)
726ea25da48SPaolo Valente  */
727*452af7dcSYu Kuai static void __bfq_bic_change_cgroup(struct bfq_data *bfqd,
728ea25da48SPaolo Valente 				    struct bfq_io_cq *bic,
7294e54a249SJan Kara 				    struct bfq_group *bfqg)
730ea25da48SPaolo Valente {
731ea25da48SPaolo Valente 	struct bfq_queue *async_bfqq = bic_to_bfqq(bic, 0);
732ea25da48SPaolo Valente 	struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, 1);
733ea25da48SPaolo Valente 	struct bfq_entity *entity;
734ea25da48SPaolo Valente 
735ea25da48SPaolo Valente 	if (async_bfqq) {
736ea25da48SPaolo Valente 		entity = &async_bfqq->entity;
737ea25da48SPaolo Valente 
738ea25da48SPaolo Valente 		if (entity->sched_data != &bfqg->sched_data) {
739ea25da48SPaolo Valente 			bic_set_bfqq(bic, NULL, 0);
740c8997736SPaolo Valente 			bfq_release_process_ref(bfqd, async_bfqq);
741ea25da48SPaolo Valente 		}
742ea25da48SPaolo Valente 	}
743ea25da48SPaolo Valente 
744ea25da48SPaolo Valente 	if (sync_bfqq) {
7453bc5e683SJan Kara 		if (!sync_bfqq->new_bfqq && !bfq_bfqq_coop(sync_bfqq)) {
7463bc5e683SJan Kara 			/* We are the only user of this bfqq, just move it */
7473bc5e683SJan Kara 			if (sync_bfqq->entity.sched_data != &bfqg->sched_data)
748ea25da48SPaolo Valente 				bfq_bfqq_move(bfqd, sync_bfqq, bfqg);
7493bc5e683SJan Kara 		} else {
7503bc5e683SJan Kara 			struct bfq_queue *bfqq;
7513bc5e683SJan Kara 
7523bc5e683SJan Kara 			/*
7533bc5e683SJan Kara 			 * The queue was merged to a different queue. Check
7543bc5e683SJan Kara 			 * that the merge chain still belongs to the same
7553bc5e683SJan Kara 			 * cgroup.
7563bc5e683SJan Kara 			 */
7573bc5e683SJan Kara 			for (bfqq = sync_bfqq; bfqq; bfqq = bfqq->new_bfqq)
7583bc5e683SJan Kara 				if (bfqq->entity.sched_data !=
7593bc5e683SJan Kara 				    &bfqg->sched_data)
7603bc5e683SJan Kara 					break;
7613bc5e683SJan Kara 			if (bfqq) {
7623bc5e683SJan Kara 				/*
7633bc5e683SJan Kara 				 * Some queue changed cgroup so the merge is
7643bc5e683SJan Kara 				 * not valid anymore. We cannot easily just
7653bc5e683SJan Kara 				 * cancel the merge (by clearing new_bfqq) as
7663bc5e683SJan Kara 				 * there may be other processes using this
7673bc5e683SJan Kara 				 * queue and holding refs to all queues below
7683bc5e683SJan Kara 				 * sync_bfqq->new_bfqq. Similarly if the merge
7693bc5e683SJan Kara 				 * already happened, we need to detach from
7703bc5e683SJan Kara 				 * bfqq now so that we cannot merge bio to a
7713bc5e683SJan Kara 				 * request from the old cgroup.
7723bc5e683SJan Kara 				 */
7733bc5e683SJan Kara 				bfq_put_cooperator(sync_bfqq);
7743bc5e683SJan Kara 				bfq_release_process_ref(bfqd, sync_bfqq);
7753bc5e683SJan Kara 				bic_set_bfqq(bic, NULL, 1);
7763bc5e683SJan Kara 			}
7773bc5e683SJan Kara 		}
778ea25da48SPaolo Valente 	}
779ea25da48SPaolo Valente }
780ea25da48SPaolo Valente 
781ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio)
782ea25da48SPaolo Valente {
783ea25da48SPaolo Valente 	struct bfq_data *bfqd = bic_to_bfqd(bic);
7844e54a249SJan Kara 	struct bfq_group *bfqg = bfq_bio_bfqg(bfqd, bio);
785ea25da48SPaolo Valente 	uint64_t serial_nr;
786ea25da48SPaolo Valente 
7874e54a249SJan Kara 	serial_nr = bfqg_to_blkg(bfqg)->blkcg->css.serial_nr;
788ea25da48SPaolo Valente 
789ea25da48SPaolo Valente 	/*
790ea25da48SPaolo Valente 	 * Check whether blkcg has changed.  The condition may trigger
791ea25da48SPaolo Valente 	 * spuriously on a newly created cic but there's no harm.
792ea25da48SPaolo Valente 	 */
793ea25da48SPaolo Valente 	if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr))
7944e54a249SJan Kara 		return;
795ea25da48SPaolo Valente 
7964e54a249SJan Kara 	/*
7974e54a249SJan Kara 	 * New cgroup for this process. Make sure it is linked to bfq internal
7984e54a249SJan Kara 	 * cgroup hierarchy.
7994e54a249SJan Kara 	 */
8004e54a249SJan Kara 	bfq_link_bfqg(bfqd, bfqg);
8014e54a249SJan Kara 	__bfq_bic_change_cgroup(bfqd, bic, bfqg);
8028f9bebc3SPaolo Valente 	/*
8038f9bebc3SPaolo Valente 	 * Update blkg_path for bfq_log_* functions. We cache this
8048f9bebc3SPaolo Valente 	 * path, and update it here, for the following
8058f9bebc3SPaolo Valente 	 * reasons. Operations on blkg objects in blk-cgroup are
8068f9bebc3SPaolo Valente 	 * protected with the request_queue lock, and not with the
8078f9bebc3SPaolo Valente 	 * lock that protects the instances of this scheduler
8088f9bebc3SPaolo Valente 	 * (bfqd->lock). This exposes BFQ to the following sort of
8098f9bebc3SPaolo Valente 	 * race.
8108f9bebc3SPaolo Valente 	 *
8118f9bebc3SPaolo Valente 	 * The blkg_lookup performed in bfq_get_queue, protected
8128f9bebc3SPaolo Valente 	 * through rcu, may happen to return the address of a copy of
8138f9bebc3SPaolo Valente 	 * the original blkg. If this is the case, then the
8148f9bebc3SPaolo Valente 	 * bfqg_and_blkg_get performed in bfq_get_queue, to pin down
8158f9bebc3SPaolo Valente 	 * the blkg, is useless: it does not prevent blk-cgroup code
8168f9bebc3SPaolo Valente 	 * from destroying both the original blkg and all objects
8178f9bebc3SPaolo Valente 	 * directly or indirectly referred by the copy of the
8188f9bebc3SPaolo Valente 	 * blkg.
8198f9bebc3SPaolo Valente 	 *
8208f9bebc3SPaolo Valente 	 * On the bright side, destroy operations on a blkg invoke, as
8218f9bebc3SPaolo Valente 	 * a first step, hooks of the scheduler associated with the
8228f9bebc3SPaolo Valente 	 * blkg. And these hooks are executed with bfqd->lock held for
8238f9bebc3SPaolo Valente 	 * BFQ. As a consequence, for any blkg associated with the
8248f9bebc3SPaolo Valente 	 * request queue this instance of the scheduler is attached
8258f9bebc3SPaolo Valente 	 * to, we are guaranteed that such a blkg is not destroyed, and
8268f9bebc3SPaolo Valente 	 * that all the pointers it contains are consistent, while we
8278f9bebc3SPaolo Valente 	 * are holding bfqd->lock. A blkg_lookup performed with
8288f9bebc3SPaolo Valente 	 * bfqd->lock held then returns a fully consistent blkg, which
8298f9bebc3SPaolo Valente 	 * remains consistent until this lock is held.
8308f9bebc3SPaolo Valente 	 *
8318f9bebc3SPaolo Valente 	 * Thanks to the last fact, and to the fact that: (1) bfqg has
8328f9bebc3SPaolo Valente 	 * been obtained through a blkg_lookup in the above
8338f9bebc3SPaolo Valente 	 * assignment, and (2) bfqd->lock is being held, here we can
8348f9bebc3SPaolo Valente 	 * safely use the policy data for the involved blkg (i.e., the
8358f9bebc3SPaolo Valente 	 * field bfqg->pd) to get to the blkg associated with bfqg,
8368f9bebc3SPaolo Valente 	 * and then we can safely use any field of blkg. After we
8378f9bebc3SPaolo Valente 	 * release bfqd->lock, even just getting blkg through this
8388f9bebc3SPaolo Valente 	 * bfqg may cause dangling references to be traversed, as
8398f9bebc3SPaolo Valente 	 * bfqg->pd may not exist any more.
8408f9bebc3SPaolo Valente 	 *
8418f9bebc3SPaolo Valente 	 * In view of the above facts, here we cache, in the bfqg, any
8428f9bebc3SPaolo Valente 	 * blkg data we may need for this bic, and for its associated
8438f9bebc3SPaolo Valente 	 * bfq_queue. As of now, we need to cache only the path of the
8448f9bebc3SPaolo Valente 	 * blkg, which is used in the bfq_log_* functions.
8458f9bebc3SPaolo Valente 	 *
8468f9bebc3SPaolo Valente 	 * Finally, note that bfqg itself needs to be protected from
8478f9bebc3SPaolo Valente 	 * destruction on the blkg_free of the original blkg (which
8488f9bebc3SPaolo Valente 	 * invokes bfq_pd_free). We use an additional private
8498f9bebc3SPaolo Valente 	 * refcounter for bfqg, to let it disappear only after no
8508f9bebc3SPaolo Valente 	 * bfq_queue refers to it any longer.
8518f9bebc3SPaolo Valente 	 */
8528f9bebc3SPaolo Valente 	blkg_path(bfqg_to_blkg(bfqg), bfqg->blkg_path, sizeof(bfqg->blkg_path));
853ea25da48SPaolo Valente 	bic->blkcg_serial_nr = serial_nr;
854ea25da48SPaolo Valente }
855ea25da48SPaolo Valente 
856ea25da48SPaolo Valente /**
857ea25da48SPaolo Valente  * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
858ea25da48SPaolo Valente  * @st: the service tree being flushed.
859ea25da48SPaolo Valente  */
860ea25da48SPaolo Valente static void bfq_flush_idle_tree(struct bfq_service_tree *st)
861ea25da48SPaolo Valente {
862ea25da48SPaolo Valente 	struct bfq_entity *entity = st->first_idle;
863ea25da48SPaolo Valente 
864ea25da48SPaolo Valente 	for (; entity ; entity = st->first_idle)
865ea25da48SPaolo Valente 		__bfq_deactivate_entity(entity, false);
866ea25da48SPaolo Valente }
867ea25da48SPaolo Valente 
868ea25da48SPaolo Valente /**
869ea25da48SPaolo Valente  * bfq_reparent_leaf_entity - move leaf entity to the root_group.
870ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
871576682faSPaolo Valente  * @entity: the entity to move, if entity is a leaf; or the parent entity
872576682faSPaolo Valente  *	    of an active leaf entity to move, if entity is not a leaf.
8731d87be82SBart Van Assche  * @ioprio_class: I/O priority class to reparent.
874ea25da48SPaolo Valente  */
875ea25da48SPaolo Valente static void bfq_reparent_leaf_entity(struct bfq_data *bfqd,
876576682faSPaolo Valente 				     struct bfq_entity *entity,
877576682faSPaolo Valente 				     int ioprio_class)
878ea25da48SPaolo Valente {
879576682faSPaolo Valente 	struct bfq_queue *bfqq;
880576682faSPaolo Valente 	struct bfq_entity *child_entity = entity;
881ea25da48SPaolo Valente 
882576682faSPaolo Valente 	while (child_entity->my_sched_data) { /* leaf not reached yet */
883576682faSPaolo Valente 		struct bfq_sched_data *child_sd = child_entity->my_sched_data;
884576682faSPaolo Valente 		struct bfq_service_tree *child_st = child_sd->service_tree +
885576682faSPaolo Valente 			ioprio_class;
886576682faSPaolo Valente 		struct rb_root *child_active = &child_st->active;
887576682faSPaolo Valente 
888576682faSPaolo Valente 		child_entity = bfq_entity_of(rb_first(child_active));
889576682faSPaolo Valente 
890576682faSPaolo Valente 		if (!child_entity)
891576682faSPaolo Valente 			child_entity = child_sd->in_service_entity;
892576682faSPaolo Valente 	}
893576682faSPaolo Valente 
894576682faSPaolo Valente 	bfqq = bfq_entity_to_bfqq(child_entity);
895ea25da48SPaolo Valente 	bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
896ea25da48SPaolo Valente }
897ea25da48SPaolo Valente 
898ea25da48SPaolo Valente /**
899576682faSPaolo Valente  * bfq_reparent_active_queues - move to the root group all active queues.
900ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
901ea25da48SPaolo Valente  * @bfqg: the group to move from.
902576682faSPaolo Valente  * @st: the service tree to start the search from.
9031d87be82SBart Van Assche  * @ioprio_class: I/O priority class to reparent.
904ea25da48SPaolo Valente  */
905576682faSPaolo Valente static void bfq_reparent_active_queues(struct bfq_data *bfqd,
906ea25da48SPaolo Valente 				       struct bfq_group *bfqg,
907576682faSPaolo Valente 				       struct bfq_service_tree *st,
908576682faSPaolo Valente 				       int ioprio_class)
909ea25da48SPaolo Valente {
910ea25da48SPaolo Valente 	struct rb_root *active = &st->active;
911576682faSPaolo Valente 	struct bfq_entity *entity;
912ea25da48SPaolo Valente 
913576682faSPaolo Valente 	while ((entity = bfq_entity_of(rb_first(active))))
914576682faSPaolo Valente 		bfq_reparent_leaf_entity(bfqd, entity, ioprio_class);
915ea25da48SPaolo Valente 
916ea25da48SPaolo Valente 	if (bfqg->sched_data.in_service_entity)
917ea25da48SPaolo Valente 		bfq_reparent_leaf_entity(bfqd,
918576682faSPaolo Valente 					 bfqg->sched_data.in_service_entity,
919576682faSPaolo Valente 					 ioprio_class);
920ea25da48SPaolo Valente }
921ea25da48SPaolo Valente 
922ea25da48SPaolo Valente /**
923ea25da48SPaolo Valente  * bfq_pd_offline - deactivate the entity associated with @pd,
924ea25da48SPaolo Valente  *		    and reparent its children entities.
925ea25da48SPaolo Valente  * @pd: descriptor of the policy going offline.
926ea25da48SPaolo Valente  *
927ea25da48SPaolo Valente  * blkio already grabs the queue_lock for us, so no need to use
928ea25da48SPaolo Valente  * RCU-based magic
929ea25da48SPaolo Valente  */
930dfb79af5SBart Van Assche static void bfq_pd_offline(struct blkg_policy_data *pd)
931ea25da48SPaolo Valente {
932ea25da48SPaolo Valente 	struct bfq_service_tree *st;
933ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
934ea25da48SPaolo Valente 	struct bfq_data *bfqd = bfqg->bfqd;
935ea25da48SPaolo Valente 	struct bfq_entity *entity = bfqg->my_entity;
936ea25da48SPaolo Valente 	unsigned long flags;
937ea25da48SPaolo Valente 	int i;
938ea25da48SPaolo Valente 
939ea25da48SPaolo Valente 	spin_lock_irqsave(&bfqd->lock, flags);
94052257ffbSPaolo Valente 
94152257ffbSPaolo Valente 	if (!entity) /* root group */
94252257ffbSPaolo Valente 		goto put_async_queues;
94352257ffbSPaolo Valente 
944ea25da48SPaolo Valente 	/*
945ea25da48SPaolo Valente 	 * Empty all service_trees belonging to this group before
946ea25da48SPaolo Valente 	 * deactivating the group itself.
947ea25da48SPaolo Valente 	 */
948ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
949ea25da48SPaolo Valente 		st = bfqg->sched_data.service_tree + i;
950ea25da48SPaolo Valente 
951ea25da48SPaolo Valente 		/*
952ea25da48SPaolo Valente 		 * It may happen that some queues are still active
953ea25da48SPaolo Valente 		 * (busy) upon group destruction (if the corresponding
954ea25da48SPaolo Valente 		 * processes have been forced to terminate). We move
955ea25da48SPaolo Valente 		 * all the leaf entities corresponding to these queues
956ea25da48SPaolo Valente 		 * to the root_group.
957ea25da48SPaolo Valente 		 * Also, it may happen that the group has an entity
958ea25da48SPaolo Valente 		 * in service, which is disconnected from the active
959ea25da48SPaolo Valente 		 * tree: it must be moved, too.
960ea25da48SPaolo Valente 		 * There is no need to put the sync queues, as the
961ea25da48SPaolo Valente 		 * scheduler has taken no reference.
962ea25da48SPaolo Valente 		 */
963576682faSPaolo Valente 		bfq_reparent_active_queues(bfqd, bfqg, st, i);
9644d38a87fSPaolo Valente 
9654d38a87fSPaolo Valente 		/*
9664d38a87fSPaolo Valente 		 * The idle tree may still contain bfq_queues
9674d38a87fSPaolo Valente 		 * belonging to exited task because they never
9684d38a87fSPaolo Valente 		 * migrated to a different cgroup from the one being
9694d38a87fSPaolo Valente 		 * destroyed now. In addition, even
9704d38a87fSPaolo Valente 		 * bfq_reparent_active_queues() may happen to add some
9714d38a87fSPaolo Valente 		 * entities to the idle tree. It happens if, in some
9724d38a87fSPaolo Valente 		 * of the calls to bfq_bfqq_move() performed by
9734d38a87fSPaolo Valente 		 * bfq_reparent_active_queues(), the queue to move is
9744d38a87fSPaolo Valente 		 * empty and gets expired.
9754d38a87fSPaolo Valente 		 */
9764d38a87fSPaolo Valente 		bfq_flush_idle_tree(st);
977ea25da48SPaolo Valente 	}
978ea25da48SPaolo Valente 
979ea25da48SPaolo Valente 	__bfq_deactivate_entity(entity, false);
98052257ffbSPaolo Valente 
98152257ffbSPaolo Valente put_async_queues:
982ea25da48SPaolo Valente 	bfq_put_async_queues(bfqd, bfqg);
98309f87186SJan Kara 	bfqg->online = false;
984ea25da48SPaolo Valente 
985ea25da48SPaolo Valente 	spin_unlock_irqrestore(&bfqd->lock, flags);
986ea25da48SPaolo Valente 	/*
987ea25da48SPaolo Valente 	 * @blkg is going offline and will be ignored by
988ea25da48SPaolo Valente 	 * blkg_[rw]stat_recursive_sum().  Transfer stats to the parent so
989ea25da48SPaolo Valente 	 * that they don't get lost.  If IOs complete after this point, the
990ea25da48SPaolo Valente 	 * stats for them will be lost.  Oh well...
991ea25da48SPaolo Valente 	 */
992ea25da48SPaolo Valente 	bfqg_stats_xfer_dead(bfqg);
993ea25da48SPaolo Valente }
994ea25da48SPaolo Valente 
995ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
996ea25da48SPaolo Valente {
997ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
998ea25da48SPaolo Valente 
999ea25da48SPaolo Valente 	list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) {
1000ea25da48SPaolo Valente 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
1001ea25da48SPaolo Valente 
1002ea25da48SPaolo Valente 		bfq_end_wr_async_queues(bfqd, bfqg);
1003ea25da48SPaolo Valente 	}
1004ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1005ea25da48SPaolo Valente }
1006ea25da48SPaolo Valente 
1007795fe54cSFam Zheng static int bfq_io_show_weight_legacy(struct seq_file *sf, void *v)
1008ea25da48SPaolo Valente {
1009ea25da48SPaolo Valente 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1010ea25da48SPaolo Valente 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
1011ea25da48SPaolo Valente 	unsigned int val = 0;
1012ea25da48SPaolo Valente 
1013ea25da48SPaolo Valente 	if (bfqgd)
1014ea25da48SPaolo Valente 		val = bfqgd->weight;
1015ea25da48SPaolo Valente 
1016ea25da48SPaolo Valente 	seq_printf(sf, "%u\n", val);
1017ea25da48SPaolo Valente 
1018ea25da48SPaolo Valente 	return 0;
1019ea25da48SPaolo Valente }
1020ea25da48SPaolo Valente 
1021795fe54cSFam Zheng static u64 bfqg_prfill_weight_device(struct seq_file *sf,
1022795fe54cSFam Zheng 				     struct blkg_policy_data *pd, int off)
1023ea25da48SPaolo Valente {
1024795fe54cSFam Zheng 	struct bfq_group *bfqg = pd_to_bfqg(pd);
1025795fe54cSFam Zheng 
1026795fe54cSFam Zheng 	if (!bfqg->entity.dev_weight)
1027795fe54cSFam Zheng 		return 0;
1028795fe54cSFam Zheng 	return __blkg_prfill_u64(sf, pd, bfqg->entity.dev_weight);
1029795fe54cSFam Zheng }
1030795fe54cSFam Zheng 
1031795fe54cSFam Zheng static int bfq_io_show_weight(struct seq_file *sf, void *v)
1032795fe54cSFam Zheng {
1033795fe54cSFam Zheng 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1034795fe54cSFam Zheng 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
1035795fe54cSFam Zheng 
1036795fe54cSFam Zheng 	seq_printf(sf, "default %u\n", bfqgd->weight);
1037795fe54cSFam Zheng 	blkcg_print_blkgs(sf, blkcg, bfqg_prfill_weight_device,
1038795fe54cSFam Zheng 			  &blkcg_policy_bfq, 0, false);
1039795fe54cSFam Zheng 	return 0;
1040795fe54cSFam Zheng }
1041795fe54cSFam Zheng 
1042795fe54cSFam Zheng static void bfq_group_set_weight(struct bfq_group *bfqg, u64 weight, u64 dev_weight)
1043795fe54cSFam Zheng {
1044795fe54cSFam Zheng 	weight = dev_weight ?: weight;
1045795fe54cSFam Zheng 
1046795fe54cSFam Zheng 	bfqg->entity.dev_weight = dev_weight;
1047ea25da48SPaolo Valente 	/*
1048ea25da48SPaolo Valente 	 * Setting the prio_changed flag of the entity
1049ea25da48SPaolo Valente 	 * to 1 with new_weight == weight would re-set
1050ea25da48SPaolo Valente 	 * the value of the weight to its ioprio mapping.
1051ea25da48SPaolo Valente 	 * Set the flag only if necessary.
1052ea25da48SPaolo Valente 	 */
10535ff047e3SFam Zheng 	if ((unsigned short)weight != bfqg->entity.new_weight) {
10545ff047e3SFam Zheng 		bfqg->entity.new_weight = (unsigned short)weight;
1055ea25da48SPaolo Valente 		/*
1056ea25da48SPaolo Valente 		 * Make sure that the above new value has been
1057ea25da48SPaolo Valente 		 * stored in bfqg->entity.new_weight before
1058ea25da48SPaolo Valente 		 * setting the prio_changed flag. In fact,
1059ea25da48SPaolo Valente 		 * this flag may be read asynchronously (in
1060ea25da48SPaolo Valente 		 * critical sections protected by a different
1061ea25da48SPaolo Valente 		 * lock than that held here), and finding this
1062ea25da48SPaolo Valente 		 * flag set may cause the execution of the code
1063ea25da48SPaolo Valente 		 * for updating parameters whose value may
1064ea25da48SPaolo Valente 		 * depend also on bfqg->entity.new_weight (in
1065ea25da48SPaolo Valente 		 * __bfq_entity_update_weight_prio).
1066ea25da48SPaolo Valente 		 * This barrier makes sure that the new value
1067ea25da48SPaolo Valente 		 * of bfqg->entity.new_weight is correctly
1068ea25da48SPaolo Valente 		 * seen in that code.
1069ea25da48SPaolo Valente 		 */
1070ea25da48SPaolo Valente 		smp_wmb();
1071ea25da48SPaolo Valente 		bfqg->entity.prio_changed = 1;
1072ea25da48SPaolo Valente 	}
1073ea25da48SPaolo Valente }
10745ff047e3SFam Zheng 
10755ff047e3SFam Zheng static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css,
10765ff047e3SFam Zheng 				    struct cftype *cftype,
10775ff047e3SFam Zheng 				    u64 val)
10785ff047e3SFam Zheng {
10795ff047e3SFam Zheng 	struct blkcg *blkcg = css_to_blkcg(css);
10805ff047e3SFam Zheng 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
10815ff047e3SFam Zheng 	struct blkcg_gq *blkg;
10825ff047e3SFam Zheng 	int ret = -ERANGE;
10835ff047e3SFam Zheng 
10845ff047e3SFam Zheng 	if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT)
10855ff047e3SFam Zheng 		return ret;
10865ff047e3SFam Zheng 
10875ff047e3SFam Zheng 	ret = 0;
10885ff047e3SFam Zheng 	spin_lock_irq(&blkcg->lock);
10895ff047e3SFam Zheng 	bfqgd->weight = (unsigned short)val;
10905ff047e3SFam Zheng 	hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
10915ff047e3SFam Zheng 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
10925ff047e3SFam Zheng 
10935ff047e3SFam Zheng 		if (bfqg)
1094795fe54cSFam Zheng 			bfq_group_set_weight(bfqg, val, 0);
10955ff047e3SFam Zheng 	}
1096ea25da48SPaolo Valente 	spin_unlock_irq(&blkcg->lock);
1097ea25da48SPaolo Valente 
1098ea25da48SPaolo Valente 	return ret;
1099ea25da48SPaolo Valente }
1100ea25da48SPaolo Valente 
1101795fe54cSFam Zheng static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of,
1102795fe54cSFam Zheng 					char *buf, size_t nbytes,
1103795fe54cSFam Zheng 					loff_t off)
1104795fe54cSFam Zheng {
1105795fe54cSFam Zheng 	int ret;
1106795fe54cSFam Zheng 	struct blkg_conf_ctx ctx;
1107795fe54cSFam Zheng 	struct blkcg *blkcg = css_to_blkcg(of_css(of));
1108795fe54cSFam Zheng 	struct bfq_group *bfqg;
1109795fe54cSFam Zheng 	u64 v;
1110795fe54cSFam Zheng 
1111795fe54cSFam Zheng 	ret = blkg_conf_prep(blkcg, &blkcg_policy_bfq, buf, &ctx);
1112795fe54cSFam Zheng 	if (ret)
1113795fe54cSFam Zheng 		return ret;
1114795fe54cSFam Zheng 
1115795fe54cSFam Zheng 	if (sscanf(ctx.body, "%llu", &v) == 1) {
1116795fe54cSFam Zheng 		/* require "default" on dfl */
1117795fe54cSFam Zheng 		ret = -ERANGE;
1118795fe54cSFam Zheng 		if (!v)
1119795fe54cSFam Zheng 			goto out;
1120795fe54cSFam Zheng 	} else if (!strcmp(strim(ctx.body), "default")) {
1121795fe54cSFam Zheng 		v = 0;
1122795fe54cSFam Zheng 	} else {
1123795fe54cSFam Zheng 		ret = -EINVAL;
1124795fe54cSFam Zheng 		goto out;
1125795fe54cSFam Zheng 	}
1126795fe54cSFam Zheng 
1127795fe54cSFam Zheng 	bfqg = blkg_to_bfqg(ctx.blkg);
1128795fe54cSFam Zheng 
1129795fe54cSFam Zheng 	ret = -ERANGE;
1130795fe54cSFam Zheng 	if (!v || (v >= BFQ_MIN_WEIGHT && v <= BFQ_MAX_WEIGHT)) {
1131795fe54cSFam Zheng 		bfq_group_set_weight(bfqg, bfqg->entity.weight, v);
1132795fe54cSFam Zheng 		ret = 0;
1133795fe54cSFam Zheng 	}
1134795fe54cSFam Zheng out:
1135795fe54cSFam Zheng 	blkg_conf_finish(&ctx);
1136795fe54cSFam Zheng 	return ret ?: nbytes;
1137795fe54cSFam Zheng }
1138795fe54cSFam Zheng 
1139ea25da48SPaolo Valente static ssize_t bfq_io_set_weight(struct kernfs_open_file *of,
1140ea25da48SPaolo Valente 				 char *buf, size_t nbytes,
1141ea25da48SPaolo Valente 				 loff_t off)
1142ea25da48SPaolo Valente {
1143795fe54cSFam Zheng 	char *endp;
1144795fe54cSFam Zheng 	int ret;
1145795fe54cSFam Zheng 	u64 v;
1146ea25da48SPaolo Valente 
1147795fe54cSFam Zheng 	buf = strim(buf);
1148ea25da48SPaolo Valente 
1149795fe54cSFam Zheng 	/* "WEIGHT" or "default WEIGHT" sets the default weight */
1150795fe54cSFam Zheng 	v = simple_strtoull(buf, &endp, 0);
1151795fe54cSFam Zheng 	if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
1152795fe54cSFam Zheng 		ret = bfq_io_set_weight_legacy(of_css(of), NULL, v);
1153fc8ebd01SMaciej S. Szmigiero 		return ret ?: nbytes;
1154ea25da48SPaolo Valente 	}
1155ea25da48SPaolo Valente 
1156795fe54cSFam Zheng 	return bfq_io_set_device_weight(of, buf, nbytes, off);
1157795fe54cSFam Zheng }
1158795fe54cSFam Zheng 
1159ea25da48SPaolo Valente static int bfqg_print_rwstat(struct seq_file *sf, void *v)
1160ea25da48SPaolo Valente {
1161ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1162ea25da48SPaolo Valente 			  &blkcg_policy_bfq, seq_cft(sf)->private, true);
1163ea25da48SPaolo Valente 	return 0;
1164ea25da48SPaolo Valente }
1165ea25da48SPaolo Valente 
1166a557f1c7STejun Heo static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf,
1167a557f1c7STejun Heo 					struct blkg_policy_data *pd, int off)
1168a557f1c7STejun Heo {
1169a557f1c7STejun Heo 	struct blkg_rwstat_sample sum;
1170a557f1c7STejun Heo 
1171a557f1c7STejun Heo 	blkg_rwstat_recursive_sum(pd_to_blkg(pd), &blkcg_policy_bfq, off, &sum);
1172a557f1c7STejun Heo 	return __blkg_prfill_rwstat(sf, pd, &sum);
1173a557f1c7STejun Heo }
1174a557f1c7STejun Heo 
1175a557f1c7STejun Heo static int bfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
1176a557f1c7STejun Heo {
1177a557f1c7STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1178a557f1c7STejun Heo 			  bfqg_prfill_rwstat_recursive, &blkcg_policy_bfq,
1179a557f1c7STejun Heo 			  seq_cft(sf)->private, true);
1180a557f1c7STejun Heo 	return 0;
1181a557f1c7STejun Heo }
1182a557f1c7STejun Heo 
1183fd41e603STejun Heo #ifdef CONFIG_BFQ_CGROUP_DEBUG
1184a557f1c7STejun Heo static int bfqg_print_stat(struct seq_file *sf, void *v)
1185a557f1c7STejun Heo {
1186a557f1c7STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1187a557f1c7STejun Heo 			  &blkcg_policy_bfq, seq_cft(sf)->private, false);
1188a557f1c7STejun Heo 	return 0;
1189a557f1c7STejun Heo }
1190a557f1c7STejun Heo 
1191ea25da48SPaolo Valente static u64 bfqg_prfill_stat_recursive(struct seq_file *sf,
1192ea25da48SPaolo Valente 				      struct blkg_policy_data *pd, int off)
1193ea25da48SPaolo Valente {
1194d6258980SChristoph Hellwig 	struct blkcg_gq *blkg = pd_to_blkg(pd);
1195d6258980SChristoph Hellwig 	struct blkcg_gq *pos_blkg;
1196d6258980SChristoph Hellwig 	struct cgroup_subsys_state *pos_css;
1197d6258980SChristoph Hellwig 	u64 sum = 0;
1198d6258980SChristoph Hellwig 
1199d6258980SChristoph Hellwig 	lockdep_assert_held(&blkg->q->queue_lock);
1200d6258980SChristoph Hellwig 
1201d6258980SChristoph Hellwig 	rcu_read_lock();
1202d6258980SChristoph Hellwig 	blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
1203d6258980SChristoph Hellwig 		struct bfq_stat *stat;
1204d6258980SChristoph Hellwig 
1205d6258980SChristoph Hellwig 		if (!pos_blkg->online)
1206d6258980SChristoph Hellwig 			continue;
1207d6258980SChristoph Hellwig 
1208d6258980SChristoph Hellwig 		stat = (void *)blkg_to_pd(pos_blkg, &blkcg_policy_bfq) + off;
1209d6258980SChristoph Hellwig 		sum += bfq_stat_read(stat) + atomic64_read(&stat->aux_cnt);
1210d6258980SChristoph Hellwig 	}
1211d6258980SChristoph Hellwig 	rcu_read_unlock();
1212d6258980SChristoph Hellwig 
1213ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum);
1214ea25da48SPaolo Valente }
1215ea25da48SPaolo Valente 
1216ea25da48SPaolo Valente static int bfqg_print_stat_recursive(struct seq_file *sf, void *v)
1217ea25da48SPaolo Valente {
1218ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1219ea25da48SPaolo Valente 			  bfqg_prfill_stat_recursive, &blkcg_policy_bfq,
1220ea25da48SPaolo Valente 			  seq_cft(sf)->private, false);
1221ea25da48SPaolo Valente 	return 0;
1222ea25da48SPaolo Valente }
1223ea25da48SPaolo Valente 
1224ea25da48SPaolo Valente static u64 bfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1225ea25da48SPaolo Valente 			       int off)
1226ea25da48SPaolo Valente {
1227fd41e603STejun Heo 	struct bfq_group *bfqg = blkg_to_bfqg(pd->blkg);
1228fd41e603STejun Heo 	u64 sum = blkg_rwstat_total(&bfqg->stats.bytes);
1229ea25da48SPaolo Valente 
1230ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum >> 9);
1231ea25da48SPaolo Valente }
1232ea25da48SPaolo Valente 
1233ea25da48SPaolo Valente static int bfqg_print_stat_sectors(struct seq_file *sf, void *v)
1234ea25da48SPaolo Valente {
1235ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1236ea25da48SPaolo Valente 			  bfqg_prfill_sectors, &blkcg_policy_bfq, 0, false);
1237ea25da48SPaolo Valente 	return 0;
1238ea25da48SPaolo Valente }
1239ea25da48SPaolo Valente 
1240ea25da48SPaolo Valente static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf,
1241ea25da48SPaolo Valente 					 struct blkg_policy_data *pd, int off)
1242ea25da48SPaolo Valente {
12437af6fd91SChristoph Hellwig 	struct blkg_rwstat_sample tmp;
12445d0b6e48SChristoph Hellwig 
1245fd41e603STejun Heo 	blkg_rwstat_recursive_sum(pd->blkg, &blkcg_policy_bfq,
1246fd41e603STejun Heo 			offsetof(struct bfq_group, stats.bytes), &tmp);
1247ea25da48SPaolo Valente 
12487af6fd91SChristoph Hellwig 	return __blkg_prfill_u64(sf, pd,
12497af6fd91SChristoph Hellwig 		(tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]) >> 9);
1250ea25da48SPaolo Valente }
1251ea25da48SPaolo Valente 
1252ea25da48SPaolo Valente static int bfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1253ea25da48SPaolo Valente {
1254ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1255ea25da48SPaolo Valente 			  bfqg_prfill_sectors_recursive, &blkcg_policy_bfq, 0,
1256ea25da48SPaolo Valente 			  false);
1257ea25da48SPaolo Valente 	return 0;
1258ea25da48SPaolo Valente }
1259ea25da48SPaolo Valente 
1260ea25da48SPaolo Valente static u64 bfqg_prfill_avg_queue_size(struct seq_file *sf,
1261ea25da48SPaolo Valente 				      struct blkg_policy_data *pd, int off)
1262ea25da48SPaolo Valente {
1263ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
1264c0ce79dcSChristoph Hellwig 	u64 samples = bfq_stat_read(&bfqg->stats.avg_queue_size_samples);
1265ea25da48SPaolo Valente 	u64 v = 0;
1266ea25da48SPaolo Valente 
1267ea25da48SPaolo Valente 	if (samples) {
1268c0ce79dcSChristoph Hellwig 		v = bfq_stat_read(&bfqg->stats.avg_queue_size_sum);
1269ea25da48SPaolo Valente 		v = div64_u64(v, samples);
1270ea25da48SPaolo Valente 	}
1271ea25da48SPaolo Valente 	__blkg_prfill_u64(sf, pd, v);
1272ea25da48SPaolo Valente 	return 0;
1273ea25da48SPaolo Valente }
1274ea25da48SPaolo Valente 
1275ea25da48SPaolo Valente /* print avg_queue_size */
1276ea25da48SPaolo Valente static int bfqg_print_avg_queue_size(struct seq_file *sf, void *v)
1277ea25da48SPaolo Valente {
1278ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1279ea25da48SPaolo Valente 			  bfqg_prfill_avg_queue_size, &blkcg_policy_bfq,
1280ea25da48SPaolo Valente 			  0, false);
1281ea25da48SPaolo Valente 	return 0;
1282ea25da48SPaolo Valente }
12838060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
1284ea25da48SPaolo Valente 
1285ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1286ea25da48SPaolo Valente {
1287ea25da48SPaolo Valente 	int ret;
1288ea25da48SPaolo Valente 
1289ea25da48SPaolo Valente 	ret = blkcg_activate_policy(bfqd->queue, &blkcg_policy_bfq);
1290ea25da48SPaolo Valente 	if (ret)
1291ea25da48SPaolo Valente 		return NULL;
1292ea25da48SPaolo Valente 
1293ea25da48SPaolo Valente 	return blkg_to_bfqg(bfqd->queue->root_blkg);
1294ea25da48SPaolo Valente }
1295ea25da48SPaolo Valente 
1296ea25da48SPaolo Valente struct blkcg_policy blkcg_policy_bfq = {
1297ea25da48SPaolo Valente 	.dfl_cftypes		= bfq_blkg_files,
1298ea25da48SPaolo Valente 	.legacy_cftypes		= bfq_blkcg_legacy_files,
1299ea25da48SPaolo Valente 
1300ea25da48SPaolo Valente 	.cpd_alloc_fn		= bfq_cpd_alloc,
1301ea25da48SPaolo Valente 	.cpd_init_fn		= bfq_cpd_init,
1302ea25da48SPaolo Valente 	.cpd_bind_fn	        = bfq_cpd_init,
1303ea25da48SPaolo Valente 	.cpd_free_fn		= bfq_cpd_free,
1304ea25da48SPaolo Valente 
1305ea25da48SPaolo Valente 	.pd_alloc_fn		= bfq_pd_alloc,
1306ea25da48SPaolo Valente 	.pd_init_fn		= bfq_pd_init,
1307ea25da48SPaolo Valente 	.pd_offline_fn		= bfq_pd_offline,
1308ea25da48SPaolo Valente 	.pd_free_fn		= bfq_pd_free,
1309ea25da48SPaolo Valente 	.pd_reset_stats_fn	= bfq_pd_reset_stats,
1310ea25da48SPaolo Valente };
1311ea25da48SPaolo Valente 
1312ea25da48SPaolo Valente struct cftype bfq_blkcg_legacy_files[] = {
1313ea25da48SPaolo Valente 	{
1314ea25da48SPaolo Valente 		.name = "bfq.weight",
1315cf892988SJens Axboe 		.flags = CFTYPE_NOT_ON_ROOT,
1316795fe54cSFam Zheng 		.seq_show = bfq_io_show_weight_legacy,
1317ea25da48SPaolo Valente 		.write_u64 = bfq_io_set_weight_legacy,
1318ea25da48SPaolo Valente 	},
1319795fe54cSFam Zheng 	{
1320795fe54cSFam Zheng 		.name = "bfq.weight_device",
1321795fe54cSFam Zheng 		.flags = CFTYPE_NOT_ON_ROOT,
1322795fe54cSFam Zheng 		.seq_show = bfq_io_show_weight,
1323795fe54cSFam Zheng 		.write = bfq_io_set_weight,
1324795fe54cSFam Zheng 	},
1325ea25da48SPaolo Valente 
1326ea25da48SPaolo Valente 	/* statistics, covers only the tasks in the bfqg */
1327ea25da48SPaolo Valente 	{
1328ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes",
1329fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.bytes),
1330fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat,
1331ea25da48SPaolo Valente 	},
1332ea25da48SPaolo Valente 	{
1333ea25da48SPaolo Valente 		.name = "bfq.io_serviced",
1334fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.ios),
1335fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat,
1336ea25da48SPaolo Valente 	},
13378060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
1338a33801e8SLuca Miccio 	{
1339a33801e8SLuca Miccio 		.name = "bfq.time",
1340a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1341a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat,
1342a33801e8SLuca Miccio 	},
1343a33801e8SLuca Miccio 	{
1344a33801e8SLuca Miccio 		.name = "bfq.sectors",
1345a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors,
1346a33801e8SLuca Miccio 	},
1347ea25da48SPaolo Valente 	{
1348ea25da48SPaolo Valente 		.name = "bfq.io_service_time",
1349ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1350ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1351ea25da48SPaolo Valente 	},
1352ea25da48SPaolo Valente 	{
1353ea25da48SPaolo Valente 		.name = "bfq.io_wait_time",
1354ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1355ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1356ea25da48SPaolo Valente 	},
1357ea25da48SPaolo Valente 	{
1358ea25da48SPaolo Valente 		.name = "bfq.io_merged",
1359ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1360ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1361ea25da48SPaolo Valente 	},
1362ea25da48SPaolo Valente 	{
1363ea25da48SPaolo Valente 		.name = "bfq.io_queued",
1364ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1365ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1366ea25da48SPaolo Valente 	},
13678060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
1368ea25da48SPaolo Valente 
1369636b8fe8SAngelo Ruocco 	/* the same statistics which cover the bfqg and its descendants */
1370ea25da48SPaolo Valente 	{
1371ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes_recursive",
1372fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.bytes),
1373fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat_recursive,
1374ea25da48SPaolo Valente 	},
1375ea25da48SPaolo Valente 	{
1376ea25da48SPaolo Valente 		.name = "bfq.io_serviced_recursive",
1377fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.ios),
1378fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat_recursive,
1379ea25da48SPaolo Valente 	},
13808060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
1381a33801e8SLuca Miccio 	{
1382a33801e8SLuca Miccio 		.name = "bfq.time_recursive",
1383a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1384a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_recursive,
1385a33801e8SLuca Miccio 	},
1386a33801e8SLuca Miccio 	{
1387a33801e8SLuca Miccio 		.name = "bfq.sectors_recursive",
1388a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors_recursive,
1389a33801e8SLuca Miccio 	},
1390ea25da48SPaolo Valente 	{
1391ea25da48SPaolo Valente 		.name = "bfq.io_service_time_recursive",
1392ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1393ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1394ea25da48SPaolo Valente 	},
1395ea25da48SPaolo Valente 	{
1396ea25da48SPaolo Valente 		.name = "bfq.io_wait_time_recursive",
1397ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1398ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1399ea25da48SPaolo Valente 	},
1400ea25da48SPaolo Valente 	{
1401ea25da48SPaolo Valente 		.name = "bfq.io_merged_recursive",
1402ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1403ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1404ea25da48SPaolo Valente 	},
1405ea25da48SPaolo Valente 	{
1406ea25da48SPaolo Valente 		.name = "bfq.io_queued_recursive",
1407ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1408ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1409ea25da48SPaolo Valente 	},
1410ea25da48SPaolo Valente 	{
1411ea25da48SPaolo Valente 		.name = "bfq.avg_queue_size",
1412ea25da48SPaolo Valente 		.seq_show = bfqg_print_avg_queue_size,
1413ea25da48SPaolo Valente 	},
1414ea25da48SPaolo Valente 	{
1415ea25da48SPaolo Valente 		.name = "bfq.group_wait_time",
1416ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.group_wait_time),
1417ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1418ea25da48SPaolo Valente 	},
1419ea25da48SPaolo Valente 	{
1420ea25da48SPaolo Valente 		.name = "bfq.idle_time",
1421ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.idle_time),
1422ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1423ea25da48SPaolo Valente 	},
1424ea25da48SPaolo Valente 	{
1425ea25da48SPaolo Valente 		.name = "bfq.empty_time",
1426ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.empty_time),
1427ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1428ea25da48SPaolo Valente 	},
1429ea25da48SPaolo Valente 	{
1430ea25da48SPaolo Valente 		.name = "bfq.dequeue",
1431ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.dequeue),
1432ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1433ea25da48SPaolo Valente 	},
14348060c47bSChristoph Hellwig #endif	/* CONFIG_BFQ_CGROUP_DEBUG */
1435ea25da48SPaolo Valente 	{ }	/* terminate */
1436ea25da48SPaolo Valente };
1437ea25da48SPaolo Valente 
1438ea25da48SPaolo Valente struct cftype bfq_blkg_files[] = {
1439ea25da48SPaolo Valente 	{
1440ea25da48SPaolo Valente 		.name = "bfq.weight",
1441cf892988SJens Axboe 		.flags = CFTYPE_NOT_ON_ROOT,
1442ea25da48SPaolo Valente 		.seq_show = bfq_io_show_weight,
1443ea25da48SPaolo Valente 		.write = bfq_io_set_weight,
1444ea25da48SPaolo Valente 	},
1445ea25da48SPaolo Valente 	{} /* terminate */
1446ea25da48SPaolo Valente };
1447ea25da48SPaolo Valente 
1448ea25da48SPaolo Valente #else	/* CONFIG_BFQ_GROUP_IOSCHED */
1449ea25da48SPaolo Valente 
1450ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1451ea25da48SPaolo Valente 		   struct bfq_group *bfqg) {}
1452ea25da48SPaolo Valente 
1453ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
1454ea25da48SPaolo Valente {
1455ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
1456ea25da48SPaolo Valente 
1457ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
1458ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
1459ea25da48SPaolo Valente 	if (bfqq) {
1460ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
1461ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
1462ea25da48SPaolo Valente 	}
1463ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
1464ea25da48SPaolo Valente }
1465ea25da48SPaolo Valente 
1466ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {}
1467ea25da48SPaolo Valente 
1468ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
1469ea25da48SPaolo Valente {
1470ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1471ea25da48SPaolo Valente }
1472ea25da48SPaolo Valente 
14734e54a249SJan Kara struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio)
1474ea25da48SPaolo Valente {
1475ea25da48SPaolo Valente 	return bfqd->root_group;
1476ea25da48SPaolo Valente }
1477ea25da48SPaolo Valente 
1478ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
1479ea25da48SPaolo Valente {
1480ea25da48SPaolo Valente 	return bfqq->bfqd->root_group;
1481ea25da48SPaolo Valente }
1482ea25da48SPaolo Valente 
14834d8340d0SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg) {}
14844d8340d0SPaolo Valente 
1485ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1486ea25da48SPaolo Valente {
1487ea25da48SPaolo Valente 	struct bfq_group *bfqg;
1488ea25da48SPaolo Valente 	int i;
1489ea25da48SPaolo Valente 
1490ea25da48SPaolo Valente 	bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
1491ea25da48SPaolo Valente 	if (!bfqg)
1492ea25da48SPaolo Valente 		return NULL;
1493ea25da48SPaolo Valente 
1494ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
1495ea25da48SPaolo Valente 		bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
1496ea25da48SPaolo Valente 
1497ea25da48SPaolo Valente 	return bfqg;
1498ea25da48SPaolo Valente }
1499ea25da48SPaolo Valente #endif	/* CONFIG_BFQ_GROUP_IOSCHED */
1500