xref: /openbmc/linux/block/bfq-cgroup.c (revision 09f87186)
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,
223a33801e8SLuca Miccio 			      unsigned int op)
224a33801e8SLuca Miccio {
225a33801e8SLuca Miccio 	blkg_rwstat_add(&bfqg->stats.queued, op, 1);
226a33801e8SLuca Miccio 	bfqg_stats_end_empty_time(&bfqg->stats);
227a33801e8SLuca Miccio 	if (!(bfqq == ((struct bfq_data *)bfqg->bfqd)->in_service_queue))
228a33801e8SLuca Miccio 		bfqg_stats_set_start_group_wait_time(bfqg, bfqq_group(bfqq));
229a33801e8SLuca Miccio }
230a33801e8SLuca Miccio 
231a33801e8SLuca Miccio void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op)
232a33801e8SLuca Miccio {
233a33801e8SLuca Miccio 	blkg_rwstat_add(&bfqg->stats.queued, op, -1);
234a33801e8SLuca Miccio }
235a33801e8SLuca Miccio 
236a33801e8SLuca Miccio void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op)
237a33801e8SLuca Miccio {
238a33801e8SLuca Miccio 	blkg_rwstat_add(&bfqg->stats.merged, op, 1);
239a33801e8SLuca Miccio }
240a33801e8SLuca Miccio 
24184c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
24284c7afceSOmar Sandoval 				  u64 io_start_time_ns, unsigned int op)
243a33801e8SLuca Miccio {
244a33801e8SLuca Miccio 	struct bfqg_stats *stats = &bfqg->stats;
24584c7afceSOmar Sandoval 	u64 now = ktime_get_ns();
246a33801e8SLuca Miccio 
24784c7afceSOmar Sandoval 	if (now > io_start_time_ns)
248a33801e8SLuca Miccio 		blkg_rwstat_add(&stats->service_time, op,
24984c7afceSOmar Sandoval 				now - io_start_time_ns);
25084c7afceSOmar Sandoval 	if (io_start_time_ns > start_time_ns)
251a33801e8SLuca Miccio 		blkg_rwstat_add(&stats->wait_time, op,
25284c7afceSOmar Sandoval 				io_start_time_ns - start_time_ns);
253a33801e8SLuca Miccio }
254a33801e8SLuca Miccio 
2558060c47bSChristoph Hellwig #else /* CONFIG_BFQ_CGROUP_DEBUG */
256a33801e8SLuca Miccio 
257a33801e8SLuca Miccio void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
258a33801e8SLuca Miccio 			      unsigned int op) { }
259a33801e8SLuca Miccio void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op) { }
260a33801e8SLuca Miccio void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op) { }
26184c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
26284c7afceSOmar Sandoval 				  u64 io_start_time_ns, unsigned int op) { }
263a33801e8SLuca Miccio void bfqg_stats_update_dequeue(struct bfq_group *bfqg) { }
264a33801e8SLuca Miccio void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg) { }
265a33801e8SLuca Miccio void bfqg_stats_update_idle_time(struct bfq_group *bfqg) { }
266a33801e8SLuca Miccio void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) { }
267a33801e8SLuca Miccio void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg) { }
268a33801e8SLuca Miccio 
2698060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
270a33801e8SLuca Miccio 
271a33801e8SLuca Miccio #ifdef CONFIG_BFQ_GROUP_IOSCHED
272a33801e8SLuca Miccio 
273ea25da48SPaolo Valente /*
274ea25da48SPaolo Valente  * blk-cgroup policy-related handlers
275ea25da48SPaolo Valente  * The following functions help in converting between blk-cgroup
276ea25da48SPaolo Valente  * internal structures and BFQ-specific structures.
277ea25da48SPaolo Valente  */
278ea25da48SPaolo Valente 
279ea25da48SPaolo Valente static struct bfq_group *pd_to_bfqg(struct blkg_policy_data *pd)
280ea25da48SPaolo Valente {
281ea25da48SPaolo Valente 	return pd ? container_of(pd, struct bfq_group, pd) : NULL;
282ea25da48SPaolo Valente }
283ea25da48SPaolo Valente 
284ea25da48SPaolo Valente struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg)
285ea25da48SPaolo Valente {
286ea25da48SPaolo Valente 	return pd_to_blkg(&bfqg->pd);
287ea25da48SPaolo Valente }
288ea25da48SPaolo Valente 
289ea25da48SPaolo Valente static struct bfq_group *blkg_to_bfqg(struct blkcg_gq *blkg)
290ea25da48SPaolo Valente {
291ea25da48SPaolo Valente 	return pd_to_bfqg(blkg_to_pd(blkg, &blkcg_policy_bfq));
292ea25da48SPaolo Valente }
293ea25da48SPaolo Valente 
294ea25da48SPaolo Valente /*
295ea25da48SPaolo Valente  * bfq_group handlers
296ea25da48SPaolo Valente  * The following functions help in navigating the bfq_group hierarchy
297ea25da48SPaolo Valente  * by allowing to find the parent of a bfq_group or the bfq_group
298ea25da48SPaolo Valente  * associated to a bfq_queue.
299ea25da48SPaolo Valente  */
300ea25da48SPaolo Valente 
301ea25da48SPaolo Valente static struct bfq_group *bfqg_parent(struct bfq_group *bfqg)
302ea25da48SPaolo Valente {
303ea25da48SPaolo Valente 	struct blkcg_gq *pblkg = bfqg_to_blkg(bfqg)->parent;
304ea25da48SPaolo Valente 
305ea25da48SPaolo Valente 	return pblkg ? blkg_to_bfqg(pblkg) : NULL;
306ea25da48SPaolo Valente }
307ea25da48SPaolo Valente 
308ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
309ea25da48SPaolo Valente {
310ea25da48SPaolo Valente 	struct bfq_entity *group_entity = bfqq->entity.parent;
311ea25da48SPaolo Valente 
312ea25da48SPaolo Valente 	return group_entity ? container_of(group_entity, struct bfq_group,
313ea25da48SPaolo Valente 					   entity) :
314ea25da48SPaolo Valente 			      bfqq->bfqd->root_group;
315ea25da48SPaolo Valente }
316ea25da48SPaolo Valente 
317ea25da48SPaolo Valente /*
318ea25da48SPaolo Valente  * The following two functions handle get and put of a bfq_group by
319ea25da48SPaolo Valente  * wrapping the related blk-cgroup hooks.
320ea25da48SPaolo Valente  */
321ea25da48SPaolo Valente 
322ea25da48SPaolo Valente static void bfqg_get(struct bfq_group *bfqg)
323ea25da48SPaolo Valente {
3248f9bebc3SPaolo Valente 	bfqg->ref++;
325ea25da48SPaolo Valente }
326ea25da48SPaolo Valente 
327dfb79af5SBart Van Assche static void bfqg_put(struct bfq_group *bfqg)
328ea25da48SPaolo Valente {
3298f9bebc3SPaolo Valente 	bfqg->ref--;
3308f9bebc3SPaolo Valente 
3318f9bebc3SPaolo Valente 	if (bfqg->ref == 0)
3328f9bebc3SPaolo Valente 		kfree(bfqg);
3338f9bebc3SPaolo Valente }
3348f9bebc3SPaolo Valente 
3352de791abSDmitry Monakhov static void bfqg_and_blkg_get(struct bfq_group *bfqg)
3368f9bebc3SPaolo Valente {
3378f9bebc3SPaolo Valente 	/* see comments in bfq_bic_update_cgroup for why refcounting bfqg */
3388f9bebc3SPaolo Valente 	bfqg_get(bfqg);
3398f9bebc3SPaolo Valente 
3408f9bebc3SPaolo Valente 	blkg_get(bfqg_to_blkg(bfqg));
3418f9bebc3SPaolo Valente }
3428f9bebc3SPaolo Valente 
3438f9bebc3SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg)
3448f9bebc3SPaolo Valente {
3458f9bebc3SPaolo Valente 	blkg_put(bfqg_to_blkg(bfqg));
346d5274b3cSKonstantin Khlebnikov 
347d5274b3cSKonstantin Khlebnikov 	bfqg_put(bfqg);
348ea25da48SPaolo Valente }
349ea25da48SPaolo Valente 
350fd41e603STejun Heo void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq)
351fd41e603STejun Heo {
352fd41e603STejun Heo 	struct bfq_group *bfqg = blkg_to_bfqg(rq->bio->bi_blkg);
353fd41e603STejun Heo 
35408802ed6SHou Tao 	if (!bfqg)
35508802ed6SHou Tao 		return;
35608802ed6SHou Tao 
357fd41e603STejun Heo 	blkg_rwstat_add(&bfqg->stats.bytes, rq->cmd_flags, blk_rq_bytes(rq));
358fd41e603STejun Heo 	blkg_rwstat_add(&bfqg->stats.ios, rq->cmd_flags, 1);
359fd41e603STejun Heo }
360fd41e603STejun Heo 
361ea25da48SPaolo Valente /* @stats = 0 */
362ea25da48SPaolo Valente static void bfqg_stats_reset(struct bfqg_stats *stats)
363ea25da48SPaolo Valente {
3648060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
365ea25da48SPaolo Valente 	/* queued stats shouldn't be cleared */
366ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->merged);
367ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->service_time);
368ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->wait_time);
369c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->time);
370c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->avg_queue_size_sum);
371c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->avg_queue_size_samples);
372c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->dequeue);
373c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->group_wait_time);
374c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->idle_time);
375c0ce79dcSChristoph Hellwig 	bfq_stat_reset(&stats->empty_time);
376a33801e8SLuca Miccio #endif
377ea25da48SPaolo Valente }
378ea25da48SPaolo Valente 
379ea25da48SPaolo Valente /* @to += @from */
380ea25da48SPaolo Valente static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from)
381ea25da48SPaolo Valente {
382ea25da48SPaolo Valente 	if (!to || !from)
383ea25da48SPaolo Valente 		return;
384ea25da48SPaolo Valente 
3858060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
386ea25da48SPaolo Valente 	/* queued stats shouldn't be cleared */
387ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->merged, &from->merged);
388ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->service_time, &from->service_time);
389ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
390c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&from->time, &from->time);
391c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
392c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->avg_queue_size_samples,
393ea25da48SPaolo Valente 			  &from->avg_queue_size_samples);
394c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->dequeue, &from->dequeue);
395c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
396c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->idle_time, &from->idle_time);
397c0ce79dcSChristoph Hellwig 	bfq_stat_add_aux(&to->empty_time, &from->empty_time);
398a33801e8SLuca Miccio #endif
399ea25da48SPaolo Valente }
400ea25da48SPaolo Valente 
401ea25da48SPaolo Valente /*
402ea25da48SPaolo Valente  * Transfer @bfqg's stats to its parent's aux counts so that the ancestors'
403ea25da48SPaolo Valente  * recursive stats can still account for the amount used by this bfqg after
404ea25da48SPaolo Valente  * it's gone.
405ea25da48SPaolo Valente  */
406ea25da48SPaolo Valente static void bfqg_stats_xfer_dead(struct bfq_group *bfqg)
407ea25da48SPaolo Valente {
408ea25da48SPaolo Valente 	struct bfq_group *parent;
409ea25da48SPaolo Valente 
410ea25da48SPaolo Valente 	if (!bfqg) /* root_group */
411ea25da48SPaolo Valente 		return;
412ea25da48SPaolo Valente 
413ea25da48SPaolo Valente 	parent = bfqg_parent(bfqg);
414ea25da48SPaolo Valente 
4150d945c1fSChristoph Hellwig 	lockdep_assert_held(&bfqg_to_blkg(bfqg)->q->queue_lock);
416ea25da48SPaolo Valente 
417ea25da48SPaolo Valente 	if (unlikely(!parent))
418ea25da48SPaolo Valente 		return;
419ea25da48SPaolo Valente 
420ea25da48SPaolo Valente 	bfqg_stats_add_aux(&parent->stats, &bfqg->stats);
421ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
422ea25da48SPaolo Valente }
423ea25da48SPaolo Valente 
424ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
425ea25da48SPaolo Valente {
426ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
427ea25da48SPaolo Valente 
428ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
429ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
430ea25da48SPaolo Valente 	if (bfqq) {
431ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
432ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
4338f9bebc3SPaolo Valente 		/*
4348f9bebc3SPaolo Valente 		 * Make sure that bfqg and its associated blkg do not
4358f9bebc3SPaolo Valente 		 * disappear before entity.
4368f9bebc3SPaolo Valente 		 */
4378f9bebc3SPaolo Valente 		bfqg_and_blkg_get(bfqg);
438ea25da48SPaolo Valente 	}
439ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity; /* NULL for root group */
440ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
441ea25da48SPaolo Valente }
442ea25da48SPaolo Valente 
443ea25da48SPaolo Valente static void bfqg_stats_exit(struct bfqg_stats *stats)
444ea25da48SPaolo Valente {
445fd41e603STejun Heo 	blkg_rwstat_exit(&stats->bytes);
446fd41e603STejun Heo 	blkg_rwstat_exit(&stats->ios);
4478060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
448ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->merged);
449ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->service_time);
450ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->wait_time);
451ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->queued);
452c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->time);
453c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->avg_queue_size_sum);
454c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->avg_queue_size_samples);
455c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->dequeue);
456c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->group_wait_time);
457c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->idle_time);
458c0ce79dcSChristoph Hellwig 	bfq_stat_exit(&stats->empty_time);
459a33801e8SLuca Miccio #endif
460ea25da48SPaolo Valente }
461ea25da48SPaolo Valente 
462ea25da48SPaolo Valente static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp)
463ea25da48SPaolo Valente {
464fd41e603STejun Heo 	if (blkg_rwstat_init(&stats->bytes, gfp) ||
465fd41e603STejun Heo 	    blkg_rwstat_init(&stats->ios, gfp))
4662fc428f6SZheng Liang 		goto error;
467fd41e603STejun Heo 
4688060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
469ea25da48SPaolo Valente 	if (blkg_rwstat_init(&stats->merged, gfp) ||
470ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->service_time, gfp) ||
471ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->wait_time, gfp) ||
472ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->queued, gfp) ||
473c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->time, gfp) ||
474c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->avg_queue_size_sum, gfp) ||
475c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->avg_queue_size_samples, gfp) ||
476c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->dequeue, gfp) ||
477c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->group_wait_time, gfp) ||
478c0ce79dcSChristoph Hellwig 	    bfq_stat_init(&stats->idle_time, gfp) ||
4792fc428f6SZheng Liang 	    bfq_stat_init(&stats->empty_time, gfp))
4802fc428f6SZheng Liang 		goto error;
481a33801e8SLuca Miccio #endif
482ea25da48SPaolo Valente 
483ea25da48SPaolo Valente 	return 0;
4842fc428f6SZheng Liang 
4852fc428f6SZheng Liang error:
4862fc428f6SZheng Liang 	bfqg_stats_exit(stats);
4872fc428f6SZheng Liang 	return -ENOMEM;
488ea25da48SPaolo Valente }
489ea25da48SPaolo Valente 
490ea25da48SPaolo Valente static struct bfq_group_data *cpd_to_bfqgd(struct blkcg_policy_data *cpd)
491ea25da48SPaolo Valente {
492ea25da48SPaolo Valente 	return cpd ? container_of(cpd, struct bfq_group_data, pd) : NULL;
493ea25da48SPaolo Valente }
494ea25da48SPaolo Valente 
495ea25da48SPaolo Valente static struct bfq_group_data *blkcg_to_bfqgd(struct blkcg *blkcg)
496ea25da48SPaolo Valente {
497ea25da48SPaolo Valente 	return cpd_to_bfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_bfq));
498ea25da48SPaolo Valente }
499ea25da48SPaolo Valente 
500dfb79af5SBart Van Assche static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp)
501ea25da48SPaolo Valente {
502ea25da48SPaolo Valente 	struct bfq_group_data *bgd;
503ea25da48SPaolo Valente 
504ea25da48SPaolo Valente 	bgd = kzalloc(sizeof(*bgd), gfp);
505ea25da48SPaolo Valente 	if (!bgd)
506ea25da48SPaolo Valente 		return NULL;
507ea25da48SPaolo Valente 	return &bgd->pd;
508ea25da48SPaolo Valente }
509ea25da48SPaolo Valente 
510dfb79af5SBart Van Assche static void bfq_cpd_init(struct blkcg_policy_data *cpd)
511ea25da48SPaolo Valente {
512ea25da48SPaolo Valente 	struct bfq_group_data *d = cpd_to_bfqgd(cpd);
513ea25da48SPaolo Valente 
514ea25da48SPaolo Valente 	d->weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
515ea25da48SPaolo Valente 		CGROUP_WEIGHT_DFL : BFQ_WEIGHT_LEGACY_DFL;
516ea25da48SPaolo Valente }
517ea25da48SPaolo Valente 
518dfb79af5SBart Van Assche static void bfq_cpd_free(struct blkcg_policy_data *cpd)
519ea25da48SPaolo Valente {
520ea25da48SPaolo Valente 	kfree(cpd_to_bfqgd(cpd));
521ea25da48SPaolo Valente }
522ea25da48SPaolo Valente 
523cf09a8eeSTejun Heo static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, struct request_queue *q,
524cf09a8eeSTejun Heo 					     struct blkcg *blkcg)
525ea25da48SPaolo Valente {
526ea25da48SPaolo Valente 	struct bfq_group *bfqg;
527ea25da48SPaolo Valente 
528cf09a8eeSTejun Heo 	bfqg = kzalloc_node(sizeof(*bfqg), gfp, q->node);
529ea25da48SPaolo Valente 	if (!bfqg)
530ea25da48SPaolo Valente 		return NULL;
531ea25da48SPaolo Valente 
532ea25da48SPaolo Valente 	if (bfqg_stats_init(&bfqg->stats, gfp)) {
533ea25da48SPaolo Valente 		kfree(bfqg);
534ea25da48SPaolo Valente 		return NULL;
535ea25da48SPaolo Valente 	}
536ea25da48SPaolo Valente 
5378f9bebc3SPaolo Valente 	/* see comments in bfq_bic_update_cgroup for why refcounting */
5388f9bebc3SPaolo Valente 	bfqg_get(bfqg);
539ea25da48SPaolo Valente 	return &bfqg->pd;
540ea25da48SPaolo Valente }
541ea25da48SPaolo Valente 
542dfb79af5SBart Van Assche static void bfq_pd_init(struct blkg_policy_data *pd)
543ea25da48SPaolo Valente {
544ea25da48SPaolo Valente 	struct blkcg_gq *blkg = pd_to_blkg(pd);
545ea25da48SPaolo Valente 	struct bfq_group *bfqg = blkg_to_bfqg(blkg);
546ea25da48SPaolo Valente 	struct bfq_data *bfqd = blkg->q->elevator->elevator_data;
547ea25da48SPaolo Valente 	struct bfq_entity *entity = &bfqg->entity;
548ea25da48SPaolo Valente 	struct bfq_group_data *d = blkcg_to_bfqgd(blkg->blkcg);
549ea25da48SPaolo Valente 
550ea25da48SPaolo Valente 	entity->orig_weight = entity->weight = entity->new_weight = d->weight;
551ea25da48SPaolo Valente 	entity->my_sched_data = &bfqg->sched_data;
552430a67f9SPaolo Valente 	entity->last_bfqq_created = NULL;
553430a67f9SPaolo Valente 
554ea25da48SPaolo Valente 	bfqg->my_entity = entity; /*
555ea25da48SPaolo Valente 				   * the root_group's will be set to NULL
556ea25da48SPaolo Valente 				   * in bfq_init_queue()
557ea25da48SPaolo Valente 				   */
558ea25da48SPaolo Valente 	bfqg->bfqd = bfqd;
559ea25da48SPaolo Valente 	bfqg->active_entities = 0;
560*09f87186SJan Kara 	bfqg->online = true;
561ea25da48SPaolo Valente 	bfqg->rq_pos_tree = RB_ROOT;
562ea25da48SPaolo Valente }
563ea25da48SPaolo Valente 
564dfb79af5SBart Van Assche static void bfq_pd_free(struct blkg_policy_data *pd)
565ea25da48SPaolo Valente {
566ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
567ea25da48SPaolo Valente 
568ea25da48SPaolo Valente 	bfqg_stats_exit(&bfqg->stats);
5698f9bebc3SPaolo Valente 	bfqg_put(bfqg);
570ea25da48SPaolo Valente }
571ea25da48SPaolo Valente 
572dfb79af5SBart Van Assche static void bfq_pd_reset_stats(struct blkg_policy_data *pd)
573ea25da48SPaolo Valente {
574ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
575ea25da48SPaolo Valente 
576ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
577ea25da48SPaolo Valente }
578ea25da48SPaolo Valente 
579ea25da48SPaolo Valente static void bfq_group_set_parent(struct bfq_group *bfqg,
580ea25da48SPaolo Valente 					struct bfq_group *parent)
581ea25da48SPaolo Valente {
582ea25da48SPaolo Valente 	struct bfq_entity *entity;
583ea25da48SPaolo Valente 
584ea25da48SPaolo Valente 	entity = &bfqg->entity;
585ea25da48SPaolo Valente 	entity->parent = parent->my_entity;
586ea25da48SPaolo Valente 	entity->sched_data = &parent->sched_data;
587ea25da48SPaolo Valente }
588ea25da48SPaolo Valente 
589ea25da48SPaolo Valente static struct bfq_group *bfq_lookup_bfqg(struct bfq_data *bfqd,
590ea25da48SPaolo Valente 					 struct blkcg *blkcg)
591ea25da48SPaolo Valente {
592ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
593ea25da48SPaolo Valente 
594ea25da48SPaolo Valente 	blkg = blkg_lookup(blkcg, bfqd->queue);
595ea25da48SPaolo Valente 	if (likely(blkg))
596ea25da48SPaolo Valente 		return blkg_to_bfqg(blkg);
597ea25da48SPaolo Valente 	return NULL;
598ea25da48SPaolo Valente }
599ea25da48SPaolo Valente 
600ea25da48SPaolo Valente struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd,
601ea25da48SPaolo Valente 				     struct blkcg *blkcg)
602ea25da48SPaolo Valente {
603ea25da48SPaolo Valente 	struct bfq_group *bfqg, *parent;
604ea25da48SPaolo Valente 	struct bfq_entity *entity;
605ea25da48SPaolo Valente 
606ea25da48SPaolo Valente 	bfqg = bfq_lookup_bfqg(bfqd, blkcg);
607ea25da48SPaolo Valente 	if (unlikely(!bfqg))
608ea25da48SPaolo Valente 		return NULL;
609ea25da48SPaolo Valente 
610ea25da48SPaolo Valente 	/*
611ea25da48SPaolo Valente 	 * Update chain of bfq_groups as we might be handling a leaf group
612ea25da48SPaolo Valente 	 * which, along with some of its relatives, has not been hooked yet
613ea25da48SPaolo Valente 	 * to the private hierarchy of BFQ.
614ea25da48SPaolo Valente 	 */
615ea25da48SPaolo Valente 	entity = &bfqg->entity;
616ea25da48SPaolo Valente 	for_each_entity(entity) {
61714afc593SCarlo Nonato 		struct bfq_group *curr_bfqg = container_of(entity,
61814afc593SCarlo Nonato 						struct bfq_group, entity);
61914afc593SCarlo Nonato 		if (curr_bfqg != bfqd->root_group) {
62014afc593SCarlo Nonato 			parent = bfqg_parent(curr_bfqg);
621ea25da48SPaolo Valente 			if (!parent)
622ea25da48SPaolo Valente 				parent = bfqd->root_group;
62314afc593SCarlo Nonato 			bfq_group_set_parent(curr_bfqg, parent);
624ea25da48SPaolo Valente 		}
625ea25da48SPaolo Valente 	}
626ea25da48SPaolo Valente 
627ea25da48SPaolo Valente 	return bfqg;
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);
649c5e4cb0fSYu Kuai 
650c5e4cb0fSYu Kuai 	/*
651c5e4cb0fSYu Kuai 	 * No point to move bfqq to the same group, which can happen when
652c5e4cb0fSYu Kuai 	 * root group is offlined
653c5e4cb0fSYu Kuai 	 */
654c5e4cb0fSYu Kuai 	if (old_parent == bfqg)
655c5e4cb0fSYu Kuai 		return;
656ea25da48SPaolo Valente 
657fd1bb3aeSPaolo Valente 	/*
6588410f709SYu Kuai 	 * oom_bfqq is not allowed to move, oom_bfqq will hold ref to root_group
6598410f709SYu Kuai 	 * until elevator exit.
6608410f709SYu Kuai 	 */
6618410f709SYu Kuai 	if (bfqq == &bfqd->oom_bfqq)
6628410f709SYu Kuai 		return;
6638410f709SYu Kuai 	/*
664fd1bb3aeSPaolo Valente 	 * Get extra reference to prevent bfqq from being freed in
665fd1bb3aeSPaolo Valente 	 * next possible expire or deactivate.
666fd1bb3aeSPaolo Valente 	 */
667fd1bb3aeSPaolo Valente 	bfqq->ref++;
668fd1bb3aeSPaolo Valente 
669ea25da48SPaolo Valente 	/* If bfqq is empty, then bfq_bfqq_expire also invokes
670ea25da48SPaolo Valente 	 * bfq_del_bfqq_busy, thereby removing bfqq and its entity
671ea25da48SPaolo Valente 	 * from data structures related to current group. Otherwise we
672ea25da48SPaolo Valente 	 * need to remove bfqq explicitly with bfq_deactivate_bfqq, as
673ea25da48SPaolo Valente 	 * we do below.
674ea25da48SPaolo Valente 	 */
675ea25da48SPaolo Valente 	if (bfqq == bfqd->in_service_queue)
676ea25da48SPaolo Valente 		bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
677ea25da48SPaolo Valente 				false, BFQQE_PREEMPTED);
678ea25da48SPaolo Valente 
679ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq))
680ea25da48SPaolo Valente 		bfq_deactivate_bfqq(bfqd, bfqq, false, false);
68133a16a98SPaolo Valente 	else if (entity->on_st_or_in_serv)
682ea25da48SPaolo Valente 		bfq_put_idle_entity(bfq_entity_service_tree(entity), entity);
683c5e4cb0fSYu Kuai 	bfqg_and_blkg_put(old_parent);
684ea25da48SPaolo Valente 
685d29bd414SPaolo Valente 	if (entity->parent &&
686d29bd414SPaolo Valente 	    entity->parent->last_bfqq_created == bfqq)
687d29bd414SPaolo Valente 		entity->parent->last_bfqq_created = NULL;
688d29bd414SPaolo Valente 	else if (bfqd->last_bfqq_created == bfqq)
689d29bd414SPaolo Valente 		bfqd->last_bfqq_created = NULL;
690d29bd414SPaolo Valente 
691ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity;
692ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
6938f9bebc3SPaolo Valente 	/* pin down bfqg and its associated blkg  */
6948f9bebc3SPaolo Valente 	bfqg_and_blkg_get(bfqg);
695ea25da48SPaolo Valente 
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 
702ea25da48SPaolo Valente 	if (!bfqd->in_service_queue && !bfqd->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 
708ea25da48SPaolo Valente /**
709ea25da48SPaolo Valente  * __bfq_bic_change_cgroup - move @bic to @cgroup.
710ea25da48SPaolo Valente  * @bfqd: the queue descriptor.
711ea25da48SPaolo Valente  * @bic: the bic to move.
712ea25da48SPaolo Valente  * @blkcg: the blk-cgroup to move to.
713ea25da48SPaolo Valente  *
7148f9bebc3SPaolo Valente  * Move bic to blkcg, assuming that bfqd->lock is held; which makes
7158f9bebc3SPaolo Valente  * sure that the reference to cgroup is valid across the call (see
7168f9bebc3SPaolo Valente  * comments in bfq_bic_update_cgroup on this issue)
717ea25da48SPaolo Valente  *
718ea25da48SPaolo Valente  * NOTE: an alternative approach might have been to store the current
719ea25da48SPaolo Valente  * cgroup in bfqq and getting a reference to it, reducing the lookup
720ea25da48SPaolo Valente  * time here, at the price of slightly more complex code.
721ea25da48SPaolo Valente  */
722ea25da48SPaolo Valente static struct bfq_group *__bfq_bic_change_cgroup(struct bfq_data *bfqd,
723ea25da48SPaolo Valente 						struct bfq_io_cq *bic,
724ea25da48SPaolo Valente 						struct blkcg *blkcg)
725ea25da48SPaolo Valente {
726ea25da48SPaolo Valente 	struct bfq_queue *async_bfqq = bic_to_bfqq(bic, 0);
727ea25da48SPaolo Valente 	struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, 1);
728ea25da48SPaolo Valente 	struct bfq_group *bfqg;
729ea25da48SPaolo Valente 	struct bfq_entity *entity;
730ea25da48SPaolo Valente 
731ea25da48SPaolo Valente 	bfqg = bfq_find_set_group(bfqd, blkcg);
732ea25da48SPaolo Valente 
733ea25da48SPaolo Valente 	if (unlikely(!bfqg))
734ea25da48SPaolo Valente 		bfqg = bfqd->root_group;
735ea25da48SPaolo Valente 
736ea25da48SPaolo Valente 	if (async_bfqq) {
737ea25da48SPaolo Valente 		entity = &async_bfqq->entity;
738ea25da48SPaolo Valente 
739ea25da48SPaolo Valente 		if (entity->sched_data != &bfqg->sched_data) {
740ea25da48SPaolo Valente 			bic_set_bfqq(bic, NULL, 0);
741c8997736SPaolo Valente 			bfq_release_process_ref(bfqd, async_bfqq);
742ea25da48SPaolo Valente 		}
743ea25da48SPaolo Valente 	}
744ea25da48SPaolo Valente 
745ea25da48SPaolo Valente 	if (sync_bfqq) {
7463bc5e683SJan Kara 		if (!sync_bfqq->new_bfqq && !bfq_bfqq_coop(sync_bfqq)) {
7473bc5e683SJan Kara 			/* We are the only user of this bfqq, just move it */
7483bc5e683SJan Kara 			if (sync_bfqq->entity.sched_data != &bfqg->sched_data)
749ea25da48SPaolo Valente 				bfq_bfqq_move(bfqd, sync_bfqq, bfqg);
7503bc5e683SJan Kara 		} else {
7513bc5e683SJan Kara 			struct bfq_queue *bfqq;
7523bc5e683SJan Kara 
7533bc5e683SJan Kara 			/*
7543bc5e683SJan Kara 			 * The queue was merged to a different queue. Check
7553bc5e683SJan Kara 			 * that the merge chain still belongs to the same
7563bc5e683SJan Kara 			 * cgroup.
7573bc5e683SJan Kara 			 */
7583bc5e683SJan Kara 			for (bfqq = sync_bfqq; bfqq; bfqq = bfqq->new_bfqq)
7593bc5e683SJan Kara 				if (bfqq->entity.sched_data !=
7603bc5e683SJan Kara 				    &bfqg->sched_data)
7613bc5e683SJan Kara 					break;
7623bc5e683SJan Kara 			if (bfqq) {
7633bc5e683SJan Kara 				/*
7643bc5e683SJan Kara 				 * Some queue changed cgroup so the merge is
7653bc5e683SJan Kara 				 * not valid anymore. We cannot easily just
7663bc5e683SJan Kara 				 * cancel the merge (by clearing new_bfqq) as
7673bc5e683SJan Kara 				 * there may be other processes using this
7683bc5e683SJan Kara 				 * queue and holding refs to all queues below
7693bc5e683SJan Kara 				 * sync_bfqq->new_bfqq. Similarly if the merge
7703bc5e683SJan Kara 				 * already happened, we need to detach from
7713bc5e683SJan Kara 				 * bfqq now so that we cannot merge bio to a
7723bc5e683SJan Kara 				 * request from the old cgroup.
7733bc5e683SJan Kara 				 */
7743bc5e683SJan Kara 				bfq_put_cooperator(sync_bfqq);
7753bc5e683SJan Kara 				bfq_release_process_ref(bfqd, sync_bfqq);
7763bc5e683SJan Kara 				bic_set_bfqq(bic, NULL, 1);
7773bc5e683SJan Kara 			}
7783bc5e683SJan Kara 		}
779ea25da48SPaolo Valente 	}
780ea25da48SPaolo Valente 
781ea25da48SPaolo Valente 	return bfqg;
782ea25da48SPaolo Valente }
783ea25da48SPaolo Valente 
784ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio)
785ea25da48SPaolo Valente {
786ea25da48SPaolo Valente 	struct bfq_data *bfqd = bic_to_bfqd(bic);
787ea25da48SPaolo Valente 	struct bfq_group *bfqg = NULL;
788ea25da48SPaolo Valente 	uint64_t serial_nr;
789ea25da48SPaolo Valente 
790ea25da48SPaolo Valente 	rcu_read_lock();
7910fe061b9SDennis Zhou 	serial_nr = __bio_blkcg(bio)->css.serial_nr;
792ea25da48SPaolo Valente 
793ea25da48SPaolo Valente 	/*
794ea25da48SPaolo Valente 	 * Check whether blkcg has changed.  The condition may trigger
795ea25da48SPaolo Valente 	 * spuriously on a newly created cic but there's no harm.
796ea25da48SPaolo Valente 	 */
797ea25da48SPaolo Valente 	if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr))
798ea25da48SPaolo Valente 		goto out;
799ea25da48SPaolo Valente 
8000fe061b9SDennis Zhou 	bfqg = __bfq_bic_change_cgroup(bfqd, bic, __bio_blkcg(bio));
8018f9bebc3SPaolo Valente 	/*
8028f9bebc3SPaolo Valente 	 * Update blkg_path for bfq_log_* functions. We cache this
8038f9bebc3SPaolo Valente 	 * path, and update it here, for the following
8048f9bebc3SPaolo Valente 	 * reasons. Operations on blkg objects in blk-cgroup are
8058f9bebc3SPaolo Valente 	 * protected with the request_queue lock, and not with the
8068f9bebc3SPaolo Valente 	 * lock that protects the instances of this scheduler
8078f9bebc3SPaolo Valente 	 * (bfqd->lock). This exposes BFQ to the following sort of
8088f9bebc3SPaolo Valente 	 * race.
8098f9bebc3SPaolo Valente 	 *
8108f9bebc3SPaolo Valente 	 * The blkg_lookup performed in bfq_get_queue, protected
8118f9bebc3SPaolo Valente 	 * through rcu, may happen to return the address of a copy of
8128f9bebc3SPaolo Valente 	 * the original blkg. If this is the case, then the
8138f9bebc3SPaolo Valente 	 * bfqg_and_blkg_get performed in bfq_get_queue, to pin down
8148f9bebc3SPaolo Valente 	 * the blkg, is useless: it does not prevent blk-cgroup code
8158f9bebc3SPaolo Valente 	 * from destroying both the original blkg and all objects
8168f9bebc3SPaolo Valente 	 * directly or indirectly referred by the copy of the
8178f9bebc3SPaolo Valente 	 * blkg.
8188f9bebc3SPaolo Valente 	 *
8198f9bebc3SPaolo Valente 	 * On the bright side, destroy operations on a blkg invoke, as
8208f9bebc3SPaolo Valente 	 * a first step, hooks of the scheduler associated with the
8218f9bebc3SPaolo Valente 	 * blkg. And these hooks are executed with bfqd->lock held for
8228f9bebc3SPaolo Valente 	 * BFQ. As a consequence, for any blkg associated with the
8238f9bebc3SPaolo Valente 	 * request queue this instance of the scheduler is attached
8248f9bebc3SPaolo Valente 	 * to, we are guaranteed that such a blkg is not destroyed, and
8258f9bebc3SPaolo Valente 	 * that all the pointers it contains are consistent, while we
8268f9bebc3SPaolo Valente 	 * are holding bfqd->lock. A blkg_lookup performed with
8278f9bebc3SPaolo Valente 	 * bfqd->lock held then returns a fully consistent blkg, which
8288f9bebc3SPaolo Valente 	 * remains consistent until this lock is held.
8298f9bebc3SPaolo Valente 	 *
8308f9bebc3SPaolo Valente 	 * Thanks to the last fact, and to the fact that: (1) bfqg has
8318f9bebc3SPaolo Valente 	 * been obtained through a blkg_lookup in the above
8328f9bebc3SPaolo Valente 	 * assignment, and (2) bfqd->lock is being held, here we can
8338f9bebc3SPaolo Valente 	 * safely use the policy data for the involved blkg (i.e., the
8348f9bebc3SPaolo Valente 	 * field bfqg->pd) to get to the blkg associated with bfqg,
8358f9bebc3SPaolo Valente 	 * and then we can safely use any field of blkg. After we
8368f9bebc3SPaolo Valente 	 * release bfqd->lock, even just getting blkg through this
8378f9bebc3SPaolo Valente 	 * bfqg may cause dangling references to be traversed, as
8388f9bebc3SPaolo Valente 	 * bfqg->pd may not exist any more.
8398f9bebc3SPaolo Valente 	 *
8408f9bebc3SPaolo Valente 	 * In view of the above facts, here we cache, in the bfqg, any
8418f9bebc3SPaolo Valente 	 * blkg data we may need for this bic, and for its associated
8428f9bebc3SPaolo Valente 	 * bfq_queue. As of now, we need to cache only the path of the
8438f9bebc3SPaolo Valente 	 * blkg, which is used in the bfq_log_* functions.
8448f9bebc3SPaolo Valente 	 *
8458f9bebc3SPaolo Valente 	 * Finally, note that bfqg itself needs to be protected from
8468f9bebc3SPaolo Valente 	 * destruction on the blkg_free of the original blkg (which
8478f9bebc3SPaolo Valente 	 * invokes bfq_pd_free). We use an additional private
8488f9bebc3SPaolo Valente 	 * refcounter for bfqg, to let it disappear only after no
8498f9bebc3SPaolo Valente 	 * bfq_queue refers to it any longer.
8508f9bebc3SPaolo Valente 	 */
8518f9bebc3SPaolo Valente 	blkg_path(bfqg_to_blkg(bfqg), bfqg->blkg_path, sizeof(bfqg->blkg_path));
852ea25da48SPaolo Valente 	bic->blkcg_serial_nr = serial_nr;
853ea25da48SPaolo Valente out:
854ea25da48SPaolo Valente 	rcu_read_unlock();
855ea25da48SPaolo Valente }
856ea25da48SPaolo Valente 
857ea25da48SPaolo Valente /**
858ea25da48SPaolo Valente  * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
859ea25da48SPaolo Valente  * @st: the service tree being flushed.
860ea25da48SPaolo Valente  */
861ea25da48SPaolo Valente static void bfq_flush_idle_tree(struct bfq_service_tree *st)
862ea25da48SPaolo Valente {
863ea25da48SPaolo Valente 	struct bfq_entity *entity = st->first_idle;
864ea25da48SPaolo Valente 
865ea25da48SPaolo Valente 	for (; entity ; entity = st->first_idle)
866ea25da48SPaolo Valente 		__bfq_deactivate_entity(entity, false);
867ea25da48SPaolo Valente }
868ea25da48SPaolo Valente 
869ea25da48SPaolo Valente /**
870ea25da48SPaolo Valente  * bfq_reparent_leaf_entity - move leaf entity to the root_group.
871ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
872576682faSPaolo Valente  * @entity: the entity to move, if entity is a leaf; or the parent entity
873576682faSPaolo Valente  *	    of an active leaf entity to move, if entity is not a leaf.
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.
903ea25da48SPaolo Valente  */
904576682faSPaolo Valente static void bfq_reparent_active_queues(struct bfq_data *bfqd,
905ea25da48SPaolo Valente 				       struct bfq_group *bfqg,
906576682faSPaolo Valente 				       struct bfq_service_tree *st,
907576682faSPaolo Valente 				       int ioprio_class)
908ea25da48SPaolo Valente {
909ea25da48SPaolo Valente 	struct rb_root *active = &st->active;
910576682faSPaolo Valente 	struct bfq_entity *entity;
911ea25da48SPaolo Valente 
912576682faSPaolo Valente 	while ((entity = bfq_entity_of(rb_first(active))))
913576682faSPaolo Valente 		bfq_reparent_leaf_entity(bfqd, entity, ioprio_class);
914ea25da48SPaolo Valente 
915ea25da48SPaolo Valente 	if (bfqg->sched_data.in_service_entity)
916ea25da48SPaolo Valente 		bfq_reparent_leaf_entity(bfqd,
917576682faSPaolo Valente 					 bfqg->sched_data.in_service_entity,
918576682faSPaolo Valente 					 ioprio_class);
919ea25da48SPaolo Valente }
920ea25da48SPaolo Valente 
921ea25da48SPaolo Valente /**
922ea25da48SPaolo Valente  * bfq_pd_offline - deactivate the entity associated with @pd,
923ea25da48SPaolo Valente  *		    and reparent its children entities.
924ea25da48SPaolo Valente  * @pd: descriptor of the policy going offline.
925ea25da48SPaolo Valente  *
926ea25da48SPaolo Valente  * blkio already grabs the queue_lock for us, so no need to use
927ea25da48SPaolo Valente  * RCU-based magic
928ea25da48SPaolo Valente  */
929dfb79af5SBart Van Assche static void bfq_pd_offline(struct blkg_policy_data *pd)
930ea25da48SPaolo Valente {
931ea25da48SPaolo Valente 	struct bfq_service_tree *st;
932ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
933ea25da48SPaolo Valente 	struct bfq_data *bfqd = bfqg->bfqd;
934ea25da48SPaolo Valente 	struct bfq_entity *entity = bfqg->my_entity;
935ea25da48SPaolo Valente 	unsigned long flags;
936ea25da48SPaolo Valente 	int i;
937ea25da48SPaolo Valente 
938ea25da48SPaolo Valente 	spin_lock_irqsave(&bfqd->lock, flags);
93952257ffbSPaolo Valente 
94052257ffbSPaolo Valente 	if (!entity) /* root group */
94152257ffbSPaolo Valente 		goto put_async_queues;
94252257ffbSPaolo Valente 
943ea25da48SPaolo Valente 	/*
944ea25da48SPaolo Valente 	 * Empty all service_trees belonging to this group before
945ea25da48SPaolo Valente 	 * deactivating the group itself.
946ea25da48SPaolo Valente 	 */
947ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
948ea25da48SPaolo Valente 		st = bfqg->sched_data.service_tree + i;
949ea25da48SPaolo Valente 
950ea25da48SPaolo Valente 		/*
951ea25da48SPaolo Valente 		 * It may happen that some queues are still active
952ea25da48SPaolo Valente 		 * (busy) upon group destruction (if the corresponding
953ea25da48SPaolo Valente 		 * processes have been forced to terminate). We move
954ea25da48SPaolo Valente 		 * all the leaf entities corresponding to these queues
955ea25da48SPaolo Valente 		 * to the root_group.
956ea25da48SPaolo Valente 		 * Also, it may happen that the group has an entity
957ea25da48SPaolo Valente 		 * in service, which is disconnected from the active
958ea25da48SPaolo Valente 		 * tree: it must be moved, too.
959ea25da48SPaolo Valente 		 * There is no need to put the sync queues, as the
960ea25da48SPaolo Valente 		 * scheduler has taken no reference.
961ea25da48SPaolo Valente 		 */
962576682faSPaolo Valente 		bfq_reparent_active_queues(bfqd, bfqg, st, i);
9634d38a87fSPaolo Valente 
9644d38a87fSPaolo Valente 		/*
9654d38a87fSPaolo Valente 		 * The idle tree may still contain bfq_queues
9664d38a87fSPaolo Valente 		 * belonging to exited task because they never
9674d38a87fSPaolo Valente 		 * migrated to a different cgroup from the one being
9684d38a87fSPaolo Valente 		 * destroyed now. In addition, even
9694d38a87fSPaolo Valente 		 * bfq_reparent_active_queues() may happen to add some
9704d38a87fSPaolo Valente 		 * entities to the idle tree. It happens if, in some
9714d38a87fSPaolo Valente 		 * of the calls to bfq_bfqq_move() performed by
9724d38a87fSPaolo Valente 		 * bfq_reparent_active_queues(), the queue to move is
9734d38a87fSPaolo Valente 		 * empty and gets expired.
9744d38a87fSPaolo Valente 		 */
9754d38a87fSPaolo Valente 		bfq_flush_idle_tree(st);
976ea25da48SPaolo Valente 	}
977ea25da48SPaolo Valente 
978ea25da48SPaolo Valente 	__bfq_deactivate_entity(entity, false);
97952257ffbSPaolo Valente 
98052257ffbSPaolo Valente put_async_queues:
981ea25da48SPaolo Valente 	bfq_put_async_queues(bfqd, bfqg);
982*09f87186SJan Kara 	bfqg->online = false;
983ea25da48SPaolo Valente 
984ea25da48SPaolo Valente 	spin_unlock_irqrestore(&bfqd->lock, flags);
985ea25da48SPaolo Valente 	/*
986ea25da48SPaolo Valente 	 * @blkg is going offline and will be ignored by
987ea25da48SPaolo Valente 	 * blkg_[rw]stat_recursive_sum().  Transfer stats to the parent so
988ea25da48SPaolo Valente 	 * that they don't get lost.  If IOs complete after this point, the
989ea25da48SPaolo Valente 	 * stats for them will be lost.  Oh well...
990ea25da48SPaolo Valente 	 */
991ea25da48SPaolo Valente 	bfqg_stats_xfer_dead(bfqg);
992ea25da48SPaolo Valente }
993ea25da48SPaolo Valente 
994ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
995ea25da48SPaolo Valente {
996ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
997ea25da48SPaolo Valente 
998ea25da48SPaolo Valente 	list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) {
999ea25da48SPaolo Valente 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
1000ea25da48SPaolo Valente 
1001ea25da48SPaolo Valente 		bfq_end_wr_async_queues(bfqd, bfqg);
1002ea25da48SPaolo Valente 	}
1003ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1004ea25da48SPaolo Valente }
1005ea25da48SPaolo Valente 
1006795fe54cSFam Zheng static int bfq_io_show_weight_legacy(struct seq_file *sf, void *v)
1007ea25da48SPaolo Valente {
1008ea25da48SPaolo Valente 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1009ea25da48SPaolo Valente 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
1010ea25da48SPaolo Valente 	unsigned int val = 0;
1011ea25da48SPaolo Valente 
1012ea25da48SPaolo Valente 	if (bfqgd)
1013ea25da48SPaolo Valente 		val = bfqgd->weight;
1014ea25da48SPaolo Valente 
1015ea25da48SPaolo Valente 	seq_printf(sf, "%u\n", val);
1016ea25da48SPaolo Valente 
1017ea25da48SPaolo Valente 	return 0;
1018ea25da48SPaolo Valente }
1019ea25da48SPaolo Valente 
1020795fe54cSFam Zheng static u64 bfqg_prfill_weight_device(struct seq_file *sf,
1021795fe54cSFam Zheng 				     struct blkg_policy_data *pd, int off)
1022ea25da48SPaolo Valente {
1023795fe54cSFam Zheng 	struct bfq_group *bfqg = pd_to_bfqg(pd);
1024795fe54cSFam Zheng 
1025795fe54cSFam Zheng 	if (!bfqg->entity.dev_weight)
1026795fe54cSFam Zheng 		return 0;
1027795fe54cSFam Zheng 	return __blkg_prfill_u64(sf, pd, bfqg->entity.dev_weight);
1028795fe54cSFam Zheng }
1029795fe54cSFam Zheng 
1030795fe54cSFam Zheng static int bfq_io_show_weight(struct seq_file *sf, void *v)
1031795fe54cSFam Zheng {
1032795fe54cSFam Zheng 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1033795fe54cSFam Zheng 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
1034795fe54cSFam Zheng 
1035795fe54cSFam Zheng 	seq_printf(sf, "default %u\n", bfqgd->weight);
1036795fe54cSFam Zheng 	blkcg_print_blkgs(sf, blkcg, bfqg_prfill_weight_device,
1037795fe54cSFam Zheng 			  &blkcg_policy_bfq, 0, false);
1038795fe54cSFam Zheng 	return 0;
1039795fe54cSFam Zheng }
1040795fe54cSFam Zheng 
1041795fe54cSFam Zheng static void bfq_group_set_weight(struct bfq_group *bfqg, u64 weight, u64 dev_weight)
1042795fe54cSFam Zheng {
1043795fe54cSFam Zheng 	weight = dev_weight ?: weight;
1044795fe54cSFam Zheng 
1045795fe54cSFam Zheng 	bfqg->entity.dev_weight = dev_weight;
1046ea25da48SPaolo Valente 	/*
1047ea25da48SPaolo Valente 	 * Setting the prio_changed flag of the entity
1048ea25da48SPaolo Valente 	 * to 1 with new_weight == weight would re-set
1049ea25da48SPaolo Valente 	 * the value of the weight to its ioprio mapping.
1050ea25da48SPaolo Valente 	 * Set the flag only if necessary.
1051ea25da48SPaolo Valente 	 */
10525ff047e3SFam Zheng 	if ((unsigned short)weight != bfqg->entity.new_weight) {
10535ff047e3SFam Zheng 		bfqg->entity.new_weight = (unsigned short)weight;
1054ea25da48SPaolo Valente 		/*
1055ea25da48SPaolo Valente 		 * Make sure that the above new value has been
1056ea25da48SPaolo Valente 		 * stored in bfqg->entity.new_weight before
1057ea25da48SPaolo Valente 		 * setting the prio_changed flag. In fact,
1058ea25da48SPaolo Valente 		 * this flag may be read asynchronously (in
1059ea25da48SPaolo Valente 		 * critical sections protected by a different
1060ea25da48SPaolo Valente 		 * lock than that held here), and finding this
1061ea25da48SPaolo Valente 		 * flag set may cause the execution of the code
1062ea25da48SPaolo Valente 		 * for updating parameters whose value may
1063ea25da48SPaolo Valente 		 * depend also on bfqg->entity.new_weight (in
1064ea25da48SPaolo Valente 		 * __bfq_entity_update_weight_prio).
1065ea25da48SPaolo Valente 		 * This barrier makes sure that the new value
1066ea25da48SPaolo Valente 		 * of bfqg->entity.new_weight is correctly
1067ea25da48SPaolo Valente 		 * seen in that code.
1068ea25da48SPaolo Valente 		 */
1069ea25da48SPaolo Valente 		smp_wmb();
1070ea25da48SPaolo Valente 		bfqg->entity.prio_changed = 1;
1071ea25da48SPaolo Valente 	}
1072ea25da48SPaolo Valente }
10735ff047e3SFam Zheng 
10745ff047e3SFam Zheng static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css,
10755ff047e3SFam Zheng 				    struct cftype *cftype,
10765ff047e3SFam Zheng 				    u64 val)
10775ff047e3SFam Zheng {
10785ff047e3SFam Zheng 	struct blkcg *blkcg = css_to_blkcg(css);
10795ff047e3SFam Zheng 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
10805ff047e3SFam Zheng 	struct blkcg_gq *blkg;
10815ff047e3SFam Zheng 	int ret = -ERANGE;
10825ff047e3SFam Zheng 
10835ff047e3SFam Zheng 	if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT)
10845ff047e3SFam Zheng 		return ret;
10855ff047e3SFam Zheng 
10865ff047e3SFam Zheng 	ret = 0;
10875ff047e3SFam Zheng 	spin_lock_irq(&blkcg->lock);
10885ff047e3SFam Zheng 	bfqgd->weight = (unsigned short)val;
10895ff047e3SFam Zheng 	hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
10905ff047e3SFam Zheng 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
10915ff047e3SFam Zheng 
10925ff047e3SFam Zheng 		if (bfqg)
1093795fe54cSFam Zheng 			bfq_group_set_weight(bfqg, val, 0);
10945ff047e3SFam Zheng 	}
1095ea25da48SPaolo Valente 	spin_unlock_irq(&blkcg->lock);
1096ea25da48SPaolo Valente 
1097ea25da48SPaolo Valente 	return ret;
1098ea25da48SPaolo Valente }
1099ea25da48SPaolo Valente 
1100795fe54cSFam Zheng static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of,
1101795fe54cSFam Zheng 					char *buf, size_t nbytes,
1102795fe54cSFam Zheng 					loff_t off)
1103795fe54cSFam Zheng {
1104795fe54cSFam Zheng 	int ret;
1105795fe54cSFam Zheng 	struct blkg_conf_ctx ctx;
1106795fe54cSFam Zheng 	struct blkcg *blkcg = css_to_blkcg(of_css(of));
1107795fe54cSFam Zheng 	struct bfq_group *bfqg;
1108795fe54cSFam Zheng 	u64 v;
1109795fe54cSFam Zheng 
1110795fe54cSFam Zheng 	ret = blkg_conf_prep(blkcg, &blkcg_policy_bfq, buf, &ctx);
1111795fe54cSFam Zheng 	if (ret)
1112795fe54cSFam Zheng 		return ret;
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:
1134795fe54cSFam Zheng 	blkg_conf_finish(&ctx);
1135795fe54cSFam Zheng 	return ret ?: nbytes;
1136795fe54cSFam Zheng }
1137795fe54cSFam Zheng 
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 
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 
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 
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
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 
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 
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 
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 
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 
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 
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 
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 */
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 
1284ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1285ea25da48SPaolo Valente {
1286ea25da48SPaolo Valente 	int ret;
1287ea25da48SPaolo Valente 
1288ea25da48SPaolo Valente 	ret = blkcg_activate_policy(bfqd->queue, &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_init_fn		= bfq_cpd_init,
1301ea25da48SPaolo Valente 	.cpd_bind_fn	        = bfq_cpd_init,
1302ea25da48SPaolo Valente 	.cpd_free_fn		= bfq_cpd_free,
1303ea25da48SPaolo Valente 
1304ea25da48SPaolo Valente 	.pd_alloc_fn		= bfq_pd_alloc,
1305ea25da48SPaolo Valente 	.pd_init_fn		= bfq_pd_init,
1306ea25da48SPaolo Valente 	.pd_offline_fn		= bfq_pd_offline,
1307ea25da48SPaolo Valente 	.pd_free_fn		= bfq_pd_free,
1308ea25da48SPaolo Valente 	.pd_reset_stats_fn	= bfq_pd_reset_stats,
1309ea25da48SPaolo Valente };
1310ea25da48SPaolo Valente 
1311ea25da48SPaolo Valente struct cftype bfq_blkcg_legacy_files[] = {
1312ea25da48SPaolo Valente 	{
1313ea25da48SPaolo Valente 		.name = "bfq.weight",
1314cf892988SJens Axboe 		.flags = CFTYPE_NOT_ON_ROOT,
1315795fe54cSFam Zheng 		.seq_show = bfq_io_show_weight_legacy,
1316ea25da48SPaolo Valente 		.write_u64 = bfq_io_set_weight_legacy,
1317ea25da48SPaolo Valente 	},
1318795fe54cSFam Zheng 	{
1319795fe54cSFam Zheng 		.name = "bfq.weight_device",
1320795fe54cSFam Zheng 		.flags = CFTYPE_NOT_ON_ROOT,
1321795fe54cSFam Zheng 		.seq_show = bfq_io_show_weight,
1322795fe54cSFam Zheng 		.write = bfq_io_set_weight,
1323795fe54cSFam Zheng 	},
1324ea25da48SPaolo Valente 
1325ea25da48SPaolo Valente 	/* statistics, covers only the tasks in the bfqg */
1326ea25da48SPaolo Valente 	{
1327ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes",
1328fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.bytes),
1329fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat,
1330ea25da48SPaolo Valente 	},
1331ea25da48SPaolo Valente 	{
1332ea25da48SPaolo Valente 		.name = "bfq.io_serviced",
1333fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.ios),
1334fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat,
1335ea25da48SPaolo Valente 	},
13368060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
1337a33801e8SLuca Miccio 	{
1338a33801e8SLuca Miccio 		.name = "bfq.time",
1339a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1340a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat,
1341a33801e8SLuca Miccio 	},
1342a33801e8SLuca Miccio 	{
1343a33801e8SLuca Miccio 		.name = "bfq.sectors",
1344a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors,
1345a33801e8SLuca Miccio 	},
1346ea25da48SPaolo Valente 	{
1347ea25da48SPaolo Valente 		.name = "bfq.io_service_time",
1348ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1349ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1350ea25da48SPaolo Valente 	},
1351ea25da48SPaolo Valente 	{
1352ea25da48SPaolo Valente 		.name = "bfq.io_wait_time",
1353ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1354ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1355ea25da48SPaolo Valente 	},
1356ea25da48SPaolo Valente 	{
1357ea25da48SPaolo Valente 		.name = "bfq.io_merged",
1358ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1359ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1360ea25da48SPaolo Valente 	},
1361ea25da48SPaolo Valente 	{
1362ea25da48SPaolo Valente 		.name = "bfq.io_queued",
1363ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1364ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1365ea25da48SPaolo Valente 	},
13668060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
1367ea25da48SPaolo Valente 
1368636b8fe8SAngelo Ruocco 	/* the same statistics which cover the bfqg and its descendants */
1369ea25da48SPaolo Valente 	{
1370ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes_recursive",
1371fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.bytes),
1372fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat_recursive,
1373ea25da48SPaolo Valente 	},
1374ea25da48SPaolo Valente 	{
1375ea25da48SPaolo Valente 		.name = "bfq.io_serviced_recursive",
1376fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.ios),
1377fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat_recursive,
1378ea25da48SPaolo Valente 	},
13798060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
1380a33801e8SLuca Miccio 	{
1381a33801e8SLuca Miccio 		.name = "bfq.time_recursive",
1382a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1383a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_recursive,
1384a33801e8SLuca Miccio 	},
1385a33801e8SLuca Miccio 	{
1386a33801e8SLuca Miccio 		.name = "bfq.sectors_recursive",
1387a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors_recursive,
1388a33801e8SLuca Miccio 	},
1389ea25da48SPaolo Valente 	{
1390ea25da48SPaolo Valente 		.name = "bfq.io_service_time_recursive",
1391ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1392ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1393ea25da48SPaolo Valente 	},
1394ea25da48SPaolo Valente 	{
1395ea25da48SPaolo Valente 		.name = "bfq.io_wait_time_recursive",
1396ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1397ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1398ea25da48SPaolo Valente 	},
1399ea25da48SPaolo Valente 	{
1400ea25da48SPaolo Valente 		.name = "bfq.io_merged_recursive",
1401ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1402ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1403ea25da48SPaolo Valente 	},
1404ea25da48SPaolo Valente 	{
1405ea25da48SPaolo Valente 		.name = "bfq.io_queued_recursive",
1406ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1407ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1408ea25da48SPaolo Valente 	},
1409ea25da48SPaolo Valente 	{
1410ea25da48SPaolo Valente 		.name = "bfq.avg_queue_size",
1411ea25da48SPaolo Valente 		.seq_show = bfqg_print_avg_queue_size,
1412ea25da48SPaolo Valente 	},
1413ea25da48SPaolo Valente 	{
1414ea25da48SPaolo Valente 		.name = "bfq.group_wait_time",
1415ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.group_wait_time),
1416ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1417ea25da48SPaolo Valente 	},
1418ea25da48SPaolo Valente 	{
1419ea25da48SPaolo Valente 		.name = "bfq.idle_time",
1420ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.idle_time),
1421ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1422ea25da48SPaolo Valente 	},
1423ea25da48SPaolo Valente 	{
1424ea25da48SPaolo Valente 		.name = "bfq.empty_time",
1425ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.empty_time),
1426ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1427ea25da48SPaolo Valente 	},
1428ea25da48SPaolo Valente 	{
1429ea25da48SPaolo Valente 		.name = "bfq.dequeue",
1430ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.dequeue),
1431ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1432ea25da48SPaolo Valente 	},
14338060c47bSChristoph Hellwig #endif	/* CONFIG_BFQ_CGROUP_DEBUG */
1434ea25da48SPaolo Valente 	{ }	/* terminate */
1435ea25da48SPaolo Valente };
1436ea25da48SPaolo Valente 
1437ea25da48SPaolo Valente struct cftype bfq_blkg_files[] = {
1438ea25da48SPaolo Valente 	{
1439ea25da48SPaolo Valente 		.name = "bfq.weight",
1440cf892988SJens Axboe 		.flags = CFTYPE_NOT_ON_ROOT,
1441ea25da48SPaolo Valente 		.seq_show = bfq_io_show_weight,
1442ea25da48SPaolo Valente 		.write = bfq_io_set_weight,
1443ea25da48SPaolo Valente 	},
1444ea25da48SPaolo Valente 	{} /* terminate */
1445ea25da48SPaolo Valente };
1446ea25da48SPaolo Valente 
1447ea25da48SPaolo Valente #else	/* CONFIG_BFQ_GROUP_IOSCHED */
1448ea25da48SPaolo Valente 
1449ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1450ea25da48SPaolo Valente 		   struct bfq_group *bfqg) {}
1451ea25da48SPaolo Valente 
1452ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
1453ea25da48SPaolo Valente {
1454ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
1455ea25da48SPaolo Valente 
1456ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
1457ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
1458ea25da48SPaolo Valente 	if (bfqq) {
1459ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
1460ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
1461ea25da48SPaolo Valente 	}
1462ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
1463ea25da48SPaolo Valente }
1464ea25da48SPaolo Valente 
1465ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {}
1466ea25da48SPaolo Valente 
1467ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
1468ea25da48SPaolo Valente {
1469ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1470ea25da48SPaolo Valente }
1471ea25da48SPaolo Valente 
1472ea25da48SPaolo Valente struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, struct blkcg *blkcg)
1473ea25da48SPaolo Valente {
1474ea25da48SPaolo Valente 	return bfqd->root_group;
1475ea25da48SPaolo Valente }
1476ea25da48SPaolo Valente 
1477ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
1478ea25da48SPaolo Valente {
1479ea25da48SPaolo Valente 	return bfqq->bfqd->root_group;
1480ea25da48SPaolo Valente }
1481ea25da48SPaolo Valente 
14824d8340d0SPaolo Valente void bfqg_and_blkg_get(struct bfq_group *bfqg) {}
14834d8340d0SPaolo Valente 
14844d8340d0SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg) {}
14854d8340d0SPaolo Valente 
1486ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1487ea25da48SPaolo Valente {
1488ea25da48SPaolo Valente 	struct bfq_group *bfqg;
1489ea25da48SPaolo Valente 	int i;
1490ea25da48SPaolo Valente 
1491ea25da48SPaolo Valente 	bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
1492ea25da48SPaolo Valente 	if (!bfqg)
1493ea25da48SPaolo Valente 		return NULL;
1494ea25da48SPaolo Valente 
1495ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
1496ea25da48SPaolo Valente 		bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
1497ea25da48SPaolo Valente 
1498ea25da48SPaolo Valente 	return bfqg;
1499ea25da48SPaolo Valente }
1500ea25da48SPaolo Valente #endif	/* CONFIG_BFQ_GROUP_IOSCHED */
1501