xref: /openbmc/linux/block/bfq-cgroup.c (revision c5e4cb0f)
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;
560ea25da48SPaolo Valente 	bfqg->rq_pos_tree = RB_ROOT;
561ea25da48SPaolo Valente }
562ea25da48SPaolo Valente 
563dfb79af5SBart Van Assche static void bfq_pd_free(struct blkg_policy_data *pd)
564ea25da48SPaolo Valente {
565ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
566ea25da48SPaolo Valente 
567ea25da48SPaolo Valente 	bfqg_stats_exit(&bfqg->stats);
5688f9bebc3SPaolo Valente 	bfqg_put(bfqg);
569ea25da48SPaolo Valente }
570ea25da48SPaolo Valente 
571dfb79af5SBart Van Assche static void bfq_pd_reset_stats(struct blkg_policy_data *pd)
572ea25da48SPaolo Valente {
573ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
574ea25da48SPaolo Valente 
575ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
576ea25da48SPaolo Valente }
577ea25da48SPaolo Valente 
578ea25da48SPaolo Valente static void bfq_group_set_parent(struct bfq_group *bfqg,
579ea25da48SPaolo Valente 					struct bfq_group *parent)
580ea25da48SPaolo Valente {
581ea25da48SPaolo Valente 	struct bfq_entity *entity;
582ea25da48SPaolo Valente 
583ea25da48SPaolo Valente 	entity = &bfqg->entity;
584ea25da48SPaolo Valente 	entity->parent = parent->my_entity;
585ea25da48SPaolo Valente 	entity->sched_data = &parent->sched_data;
586ea25da48SPaolo Valente }
587ea25da48SPaolo Valente 
588ea25da48SPaolo Valente static struct bfq_group *bfq_lookup_bfqg(struct bfq_data *bfqd,
589ea25da48SPaolo Valente 					 struct blkcg *blkcg)
590ea25da48SPaolo Valente {
591ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
592ea25da48SPaolo Valente 
593ea25da48SPaolo Valente 	blkg = blkg_lookup(blkcg, bfqd->queue);
594ea25da48SPaolo Valente 	if (likely(blkg))
595ea25da48SPaolo Valente 		return blkg_to_bfqg(blkg);
596ea25da48SPaolo Valente 	return NULL;
597ea25da48SPaolo Valente }
598ea25da48SPaolo Valente 
599ea25da48SPaolo Valente struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd,
600ea25da48SPaolo Valente 				     struct blkcg *blkcg)
601ea25da48SPaolo Valente {
602ea25da48SPaolo Valente 	struct bfq_group *bfqg, *parent;
603ea25da48SPaolo Valente 	struct bfq_entity *entity;
604ea25da48SPaolo Valente 
605ea25da48SPaolo Valente 	bfqg = bfq_lookup_bfqg(bfqd, blkcg);
606ea25da48SPaolo Valente 
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;
648*c5e4cb0fSYu Kuai 	struct bfq_group *old_parent = bfqq_group(bfqq);
649*c5e4cb0fSYu Kuai 
650*c5e4cb0fSYu Kuai 	/*
651*c5e4cb0fSYu Kuai 	 * No point to move bfqq to the same group, which can happen when
652*c5e4cb0fSYu Kuai 	 * root group is offlined
653*c5e4cb0fSYu Kuai 	 */
654*c5e4cb0fSYu Kuai 	if (old_parent == bfqg)
655*c5e4cb0fSYu Kuai 		return;
656ea25da48SPaolo Valente 
657fd1bb3aeSPaolo Valente 	/*
658fd1bb3aeSPaolo Valente 	 * Get extra reference to prevent bfqq from being freed in
659fd1bb3aeSPaolo Valente 	 * next possible expire or deactivate.
660fd1bb3aeSPaolo Valente 	 */
661fd1bb3aeSPaolo Valente 	bfqq->ref++;
662fd1bb3aeSPaolo Valente 
663ea25da48SPaolo Valente 	/* If bfqq is empty, then bfq_bfqq_expire also invokes
664ea25da48SPaolo Valente 	 * bfq_del_bfqq_busy, thereby removing bfqq and its entity
665ea25da48SPaolo Valente 	 * from data structures related to current group. Otherwise we
666ea25da48SPaolo Valente 	 * need to remove bfqq explicitly with bfq_deactivate_bfqq, as
667ea25da48SPaolo Valente 	 * we do below.
668ea25da48SPaolo Valente 	 */
669ea25da48SPaolo Valente 	if (bfqq == bfqd->in_service_queue)
670ea25da48SPaolo Valente 		bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
671ea25da48SPaolo Valente 				false, BFQQE_PREEMPTED);
672ea25da48SPaolo Valente 
673ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq))
674ea25da48SPaolo Valente 		bfq_deactivate_bfqq(bfqd, bfqq, false, false);
67533a16a98SPaolo Valente 	else if (entity->on_st_or_in_serv)
676ea25da48SPaolo Valente 		bfq_put_idle_entity(bfq_entity_service_tree(entity), entity);
677*c5e4cb0fSYu Kuai 	bfqg_and_blkg_put(old_parent);
678ea25da48SPaolo Valente 
679d29bd414SPaolo Valente 	if (entity->parent &&
680d29bd414SPaolo Valente 	    entity->parent->last_bfqq_created == bfqq)
681d29bd414SPaolo Valente 		entity->parent->last_bfqq_created = NULL;
682d29bd414SPaolo Valente 	else if (bfqd->last_bfqq_created == bfqq)
683d29bd414SPaolo Valente 		bfqd->last_bfqq_created = NULL;
684d29bd414SPaolo Valente 
685ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity;
686ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
6878f9bebc3SPaolo Valente 	/* pin down bfqg and its associated blkg  */
6888f9bebc3SPaolo Valente 	bfqg_and_blkg_get(bfqg);
689ea25da48SPaolo Valente 
690ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq)) {
6918cacc5abSPaolo Valente 		if (unlikely(!bfqd->nonrot_with_queueing))
692ea25da48SPaolo Valente 			bfq_pos_tree_add_move(bfqd, bfqq);
693ea25da48SPaolo Valente 		bfq_activate_bfqq(bfqd, bfqq);
694ea25da48SPaolo Valente 	}
695ea25da48SPaolo Valente 
696ea25da48SPaolo Valente 	if (!bfqd->in_service_queue && !bfqd->rq_in_driver)
697ea25da48SPaolo Valente 		bfq_schedule_dispatch(bfqd);
698fd1bb3aeSPaolo Valente 	/* release extra ref taken above, bfqq may happen to be freed now */
699ecedd3d7SPaolo Valente 	bfq_put_queue(bfqq);
700ea25da48SPaolo Valente }
701ea25da48SPaolo Valente 
702ea25da48SPaolo Valente /**
703ea25da48SPaolo Valente  * __bfq_bic_change_cgroup - move @bic to @cgroup.
704ea25da48SPaolo Valente  * @bfqd: the queue descriptor.
705ea25da48SPaolo Valente  * @bic: the bic to move.
706ea25da48SPaolo Valente  * @blkcg: the blk-cgroup to move to.
707ea25da48SPaolo Valente  *
7088f9bebc3SPaolo Valente  * Move bic to blkcg, assuming that bfqd->lock is held; which makes
7098f9bebc3SPaolo Valente  * sure that the reference to cgroup is valid across the call (see
7108f9bebc3SPaolo Valente  * comments in bfq_bic_update_cgroup on this issue)
711ea25da48SPaolo Valente  *
712ea25da48SPaolo Valente  * NOTE: an alternative approach might have been to store the current
713ea25da48SPaolo Valente  * cgroup in bfqq and getting a reference to it, reducing the lookup
714ea25da48SPaolo Valente  * time here, at the price of slightly more complex code.
715ea25da48SPaolo Valente  */
716ea25da48SPaolo Valente static struct bfq_group *__bfq_bic_change_cgroup(struct bfq_data *bfqd,
717ea25da48SPaolo Valente 						struct bfq_io_cq *bic,
718ea25da48SPaolo Valente 						struct blkcg *blkcg)
719ea25da48SPaolo Valente {
720ea25da48SPaolo Valente 	struct bfq_queue *async_bfqq = bic_to_bfqq(bic, 0);
721ea25da48SPaolo Valente 	struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, 1);
722ea25da48SPaolo Valente 	struct bfq_group *bfqg;
723ea25da48SPaolo Valente 	struct bfq_entity *entity;
724ea25da48SPaolo Valente 
725ea25da48SPaolo Valente 	bfqg = bfq_find_set_group(bfqd, blkcg);
726ea25da48SPaolo Valente 
727ea25da48SPaolo Valente 	if (unlikely(!bfqg))
728ea25da48SPaolo Valente 		bfqg = bfqd->root_group;
729ea25da48SPaolo Valente 
730ea25da48SPaolo Valente 	if (async_bfqq) {
731ea25da48SPaolo Valente 		entity = &async_bfqq->entity;
732ea25da48SPaolo Valente 
733ea25da48SPaolo Valente 		if (entity->sched_data != &bfqg->sched_data) {
734ea25da48SPaolo Valente 			bic_set_bfqq(bic, NULL, 0);
735c8997736SPaolo Valente 			bfq_release_process_ref(bfqd, async_bfqq);
736ea25da48SPaolo Valente 		}
737ea25da48SPaolo Valente 	}
738ea25da48SPaolo Valente 
739ea25da48SPaolo Valente 	if (sync_bfqq) {
740ea25da48SPaolo Valente 		entity = &sync_bfqq->entity;
741ea25da48SPaolo Valente 		if (entity->sched_data != &bfqg->sched_data)
742ea25da48SPaolo Valente 			bfq_bfqq_move(bfqd, sync_bfqq, bfqg);
743ea25da48SPaolo Valente 	}
744ea25da48SPaolo Valente 
745ea25da48SPaolo Valente 	return bfqg;
746ea25da48SPaolo Valente }
747ea25da48SPaolo Valente 
748ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio)
749ea25da48SPaolo Valente {
750ea25da48SPaolo Valente 	struct bfq_data *bfqd = bic_to_bfqd(bic);
751ea25da48SPaolo Valente 	struct bfq_group *bfqg = NULL;
752ea25da48SPaolo Valente 	uint64_t serial_nr;
753ea25da48SPaolo Valente 
754ea25da48SPaolo Valente 	rcu_read_lock();
7550fe061b9SDennis Zhou 	serial_nr = __bio_blkcg(bio)->css.serial_nr;
756ea25da48SPaolo Valente 
757ea25da48SPaolo Valente 	/*
758ea25da48SPaolo Valente 	 * Check whether blkcg has changed.  The condition may trigger
759ea25da48SPaolo Valente 	 * spuriously on a newly created cic but there's no harm.
760ea25da48SPaolo Valente 	 */
761ea25da48SPaolo Valente 	if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr))
762ea25da48SPaolo Valente 		goto out;
763ea25da48SPaolo Valente 
7640fe061b9SDennis Zhou 	bfqg = __bfq_bic_change_cgroup(bfqd, bic, __bio_blkcg(bio));
7658f9bebc3SPaolo Valente 	/*
7668f9bebc3SPaolo Valente 	 * Update blkg_path for bfq_log_* functions. We cache this
7678f9bebc3SPaolo Valente 	 * path, and update it here, for the following
7688f9bebc3SPaolo Valente 	 * reasons. Operations on blkg objects in blk-cgroup are
7698f9bebc3SPaolo Valente 	 * protected with the request_queue lock, and not with the
7708f9bebc3SPaolo Valente 	 * lock that protects the instances of this scheduler
7718f9bebc3SPaolo Valente 	 * (bfqd->lock). This exposes BFQ to the following sort of
7728f9bebc3SPaolo Valente 	 * race.
7738f9bebc3SPaolo Valente 	 *
7748f9bebc3SPaolo Valente 	 * The blkg_lookup performed in bfq_get_queue, protected
7758f9bebc3SPaolo Valente 	 * through rcu, may happen to return the address of a copy of
7768f9bebc3SPaolo Valente 	 * the original blkg. If this is the case, then the
7778f9bebc3SPaolo Valente 	 * bfqg_and_blkg_get performed in bfq_get_queue, to pin down
7788f9bebc3SPaolo Valente 	 * the blkg, is useless: it does not prevent blk-cgroup code
7798f9bebc3SPaolo Valente 	 * from destroying both the original blkg and all objects
7808f9bebc3SPaolo Valente 	 * directly or indirectly referred by the copy of the
7818f9bebc3SPaolo Valente 	 * blkg.
7828f9bebc3SPaolo Valente 	 *
7838f9bebc3SPaolo Valente 	 * On the bright side, destroy operations on a blkg invoke, as
7848f9bebc3SPaolo Valente 	 * a first step, hooks of the scheduler associated with the
7858f9bebc3SPaolo Valente 	 * blkg. And these hooks are executed with bfqd->lock held for
7868f9bebc3SPaolo Valente 	 * BFQ. As a consequence, for any blkg associated with the
7878f9bebc3SPaolo Valente 	 * request queue this instance of the scheduler is attached
7888f9bebc3SPaolo Valente 	 * to, we are guaranteed that such a blkg is not destroyed, and
7898f9bebc3SPaolo Valente 	 * that all the pointers it contains are consistent, while we
7908f9bebc3SPaolo Valente 	 * are holding bfqd->lock. A blkg_lookup performed with
7918f9bebc3SPaolo Valente 	 * bfqd->lock held then returns a fully consistent blkg, which
7928f9bebc3SPaolo Valente 	 * remains consistent until this lock is held.
7938f9bebc3SPaolo Valente 	 *
7948f9bebc3SPaolo Valente 	 * Thanks to the last fact, and to the fact that: (1) bfqg has
7958f9bebc3SPaolo Valente 	 * been obtained through a blkg_lookup in the above
7968f9bebc3SPaolo Valente 	 * assignment, and (2) bfqd->lock is being held, here we can
7978f9bebc3SPaolo Valente 	 * safely use the policy data for the involved blkg (i.e., the
7988f9bebc3SPaolo Valente 	 * field bfqg->pd) to get to the blkg associated with bfqg,
7998f9bebc3SPaolo Valente 	 * and then we can safely use any field of blkg. After we
8008f9bebc3SPaolo Valente 	 * release bfqd->lock, even just getting blkg through this
8018f9bebc3SPaolo Valente 	 * bfqg may cause dangling references to be traversed, as
8028f9bebc3SPaolo Valente 	 * bfqg->pd may not exist any more.
8038f9bebc3SPaolo Valente 	 *
8048f9bebc3SPaolo Valente 	 * In view of the above facts, here we cache, in the bfqg, any
8058f9bebc3SPaolo Valente 	 * blkg data we may need for this bic, and for its associated
8068f9bebc3SPaolo Valente 	 * bfq_queue. As of now, we need to cache only the path of the
8078f9bebc3SPaolo Valente 	 * blkg, which is used in the bfq_log_* functions.
8088f9bebc3SPaolo Valente 	 *
8098f9bebc3SPaolo Valente 	 * Finally, note that bfqg itself needs to be protected from
8108f9bebc3SPaolo Valente 	 * destruction on the blkg_free of the original blkg (which
8118f9bebc3SPaolo Valente 	 * invokes bfq_pd_free). We use an additional private
8128f9bebc3SPaolo Valente 	 * refcounter for bfqg, to let it disappear only after no
8138f9bebc3SPaolo Valente 	 * bfq_queue refers to it any longer.
8148f9bebc3SPaolo Valente 	 */
8158f9bebc3SPaolo Valente 	blkg_path(bfqg_to_blkg(bfqg), bfqg->blkg_path, sizeof(bfqg->blkg_path));
816ea25da48SPaolo Valente 	bic->blkcg_serial_nr = serial_nr;
817ea25da48SPaolo Valente out:
818ea25da48SPaolo Valente 	rcu_read_unlock();
819ea25da48SPaolo Valente }
820ea25da48SPaolo Valente 
821ea25da48SPaolo Valente /**
822ea25da48SPaolo Valente  * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
823ea25da48SPaolo Valente  * @st: the service tree being flushed.
824ea25da48SPaolo Valente  */
825ea25da48SPaolo Valente static void bfq_flush_idle_tree(struct bfq_service_tree *st)
826ea25da48SPaolo Valente {
827ea25da48SPaolo Valente 	struct bfq_entity *entity = st->first_idle;
828ea25da48SPaolo Valente 
829ea25da48SPaolo Valente 	for (; entity ; entity = st->first_idle)
830ea25da48SPaolo Valente 		__bfq_deactivate_entity(entity, false);
831ea25da48SPaolo Valente }
832ea25da48SPaolo Valente 
833ea25da48SPaolo Valente /**
834ea25da48SPaolo Valente  * bfq_reparent_leaf_entity - move leaf entity to the root_group.
835ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
836576682faSPaolo Valente  * @entity: the entity to move, if entity is a leaf; or the parent entity
837576682faSPaolo Valente  *	    of an active leaf entity to move, if entity is not a leaf.
838ea25da48SPaolo Valente  */
839ea25da48SPaolo Valente static void bfq_reparent_leaf_entity(struct bfq_data *bfqd,
840576682faSPaolo Valente 				     struct bfq_entity *entity,
841576682faSPaolo Valente 				     int ioprio_class)
842ea25da48SPaolo Valente {
843576682faSPaolo Valente 	struct bfq_queue *bfqq;
844576682faSPaolo Valente 	struct bfq_entity *child_entity = entity;
845ea25da48SPaolo Valente 
846576682faSPaolo Valente 	while (child_entity->my_sched_data) { /* leaf not reached yet */
847576682faSPaolo Valente 		struct bfq_sched_data *child_sd = child_entity->my_sched_data;
848576682faSPaolo Valente 		struct bfq_service_tree *child_st = child_sd->service_tree +
849576682faSPaolo Valente 			ioprio_class;
850576682faSPaolo Valente 		struct rb_root *child_active = &child_st->active;
851576682faSPaolo Valente 
852576682faSPaolo Valente 		child_entity = bfq_entity_of(rb_first(child_active));
853576682faSPaolo Valente 
854576682faSPaolo Valente 		if (!child_entity)
855576682faSPaolo Valente 			child_entity = child_sd->in_service_entity;
856576682faSPaolo Valente 	}
857576682faSPaolo Valente 
858576682faSPaolo Valente 	bfqq = bfq_entity_to_bfqq(child_entity);
859ea25da48SPaolo Valente 	bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
860ea25da48SPaolo Valente }
861ea25da48SPaolo Valente 
862ea25da48SPaolo Valente /**
863576682faSPaolo Valente  * bfq_reparent_active_queues - move to the root group all active queues.
864ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
865ea25da48SPaolo Valente  * @bfqg: the group to move from.
866576682faSPaolo Valente  * @st: the service tree to start the search from.
867ea25da48SPaolo Valente  */
868576682faSPaolo Valente static void bfq_reparent_active_queues(struct bfq_data *bfqd,
869ea25da48SPaolo Valente 				       struct bfq_group *bfqg,
870576682faSPaolo Valente 				       struct bfq_service_tree *st,
871576682faSPaolo Valente 				       int ioprio_class)
872ea25da48SPaolo Valente {
873ea25da48SPaolo Valente 	struct rb_root *active = &st->active;
874576682faSPaolo Valente 	struct bfq_entity *entity;
875ea25da48SPaolo Valente 
876576682faSPaolo Valente 	while ((entity = bfq_entity_of(rb_first(active))))
877576682faSPaolo Valente 		bfq_reparent_leaf_entity(bfqd, entity, ioprio_class);
878ea25da48SPaolo Valente 
879ea25da48SPaolo Valente 	if (bfqg->sched_data.in_service_entity)
880ea25da48SPaolo Valente 		bfq_reparent_leaf_entity(bfqd,
881576682faSPaolo Valente 					 bfqg->sched_data.in_service_entity,
882576682faSPaolo Valente 					 ioprio_class);
883ea25da48SPaolo Valente }
884ea25da48SPaolo Valente 
885ea25da48SPaolo Valente /**
886ea25da48SPaolo Valente  * bfq_pd_offline - deactivate the entity associated with @pd,
887ea25da48SPaolo Valente  *		    and reparent its children entities.
888ea25da48SPaolo Valente  * @pd: descriptor of the policy going offline.
889ea25da48SPaolo Valente  *
890ea25da48SPaolo Valente  * blkio already grabs the queue_lock for us, so no need to use
891ea25da48SPaolo Valente  * RCU-based magic
892ea25da48SPaolo Valente  */
893dfb79af5SBart Van Assche static void bfq_pd_offline(struct blkg_policy_data *pd)
894ea25da48SPaolo Valente {
895ea25da48SPaolo Valente 	struct bfq_service_tree *st;
896ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
897ea25da48SPaolo Valente 	struct bfq_data *bfqd = bfqg->bfqd;
898ea25da48SPaolo Valente 	struct bfq_entity *entity = bfqg->my_entity;
899ea25da48SPaolo Valente 	unsigned long flags;
900ea25da48SPaolo Valente 	int i;
901ea25da48SPaolo Valente 
902ea25da48SPaolo Valente 	spin_lock_irqsave(&bfqd->lock, flags);
90352257ffbSPaolo Valente 
90452257ffbSPaolo Valente 	if (!entity) /* root group */
90552257ffbSPaolo Valente 		goto put_async_queues;
90652257ffbSPaolo Valente 
907ea25da48SPaolo Valente 	/*
908ea25da48SPaolo Valente 	 * Empty all service_trees belonging to this group before
909ea25da48SPaolo Valente 	 * deactivating the group itself.
910ea25da48SPaolo Valente 	 */
911ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
912ea25da48SPaolo Valente 		st = bfqg->sched_data.service_tree + i;
913ea25da48SPaolo Valente 
914ea25da48SPaolo Valente 		/*
915ea25da48SPaolo Valente 		 * It may happen that some queues are still active
916ea25da48SPaolo Valente 		 * (busy) upon group destruction (if the corresponding
917ea25da48SPaolo Valente 		 * processes have been forced to terminate). We move
918ea25da48SPaolo Valente 		 * all the leaf entities corresponding to these queues
919ea25da48SPaolo Valente 		 * to the root_group.
920ea25da48SPaolo Valente 		 * Also, it may happen that the group has an entity
921ea25da48SPaolo Valente 		 * in service, which is disconnected from the active
922ea25da48SPaolo Valente 		 * tree: it must be moved, too.
923ea25da48SPaolo Valente 		 * There is no need to put the sync queues, as the
924ea25da48SPaolo Valente 		 * scheduler has taken no reference.
925ea25da48SPaolo Valente 		 */
926576682faSPaolo Valente 		bfq_reparent_active_queues(bfqd, bfqg, st, i);
9274d38a87fSPaolo Valente 
9284d38a87fSPaolo Valente 		/*
9294d38a87fSPaolo Valente 		 * The idle tree may still contain bfq_queues
9304d38a87fSPaolo Valente 		 * belonging to exited task because they never
9314d38a87fSPaolo Valente 		 * migrated to a different cgroup from the one being
9324d38a87fSPaolo Valente 		 * destroyed now. In addition, even
9334d38a87fSPaolo Valente 		 * bfq_reparent_active_queues() may happen to add some
9344d38a87fSPaolo Valente 		 * entities to the idle tree. It happens if, in some
9354d38a87fSPaolo Valente 		 * of the calls to bfq_bfqq_move() performed by
9364d38a87fSPaolo Valente 		 * bfq_reparent_active_queues(), the queue to move is
9374d38a87fSPaolo Valente 		 * empty and gets expired.
9384d38a87fSPaolo Valente 		 */
9394d38a87fSPaolo Valente 		bfq_flush_idle_tree(st);
940ea25da48SPaolo Valente 	}
941ea25da48SPaolo Valente 
942ea25da48SPaolo Valente 	__bfq_deactivate_entity(entity, false);
94352257ffbSPaolo Valente 
94452257ffbSPaolo Valente put_async_queues:
945ea25da48SPaolo Valente 	bfq_put_async_queues(bfqd, bfqg);
946ea25da48SPaolo Valente 
947ea25da48SPaolo Valente 	spin_unlock_irqrestore(&bfqd->lock, flags);
948ea25da48SPaolo Valente 	/*
949ea25da48SPaolo Valente 	 * @blkg is going offline and will be ignored by
950ea25da48SPaolo Valente 	 * blkg_[rw]stat_recursive_sum().  Transfer stats to the parent so
951ea25da48SPaolo Valente 	 * that they don't get lost.  If IOs complete after this point, the
952ea25da48SPaolo Valente 	 * stats for them will be lost.  Oh well...
953ea25da48SPaolo Valente 	 */
954ea25da48SPaolo Valente 	bfqg_stats_xfer_dead(bfqg);
955ea25da48SPaolo Valente }
956ea25da48SPaolo Valente 
957ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
958ea25da48SPaolo Valente {
959ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
960ea25da48SPaolo Valente 
961ea25da48SPaolo Valente 	list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) {
962ea25da48SPaolo Valente 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
963ea25da48SPaolo Valente 
964ea25da48SPaolo Valente 		bfq_end_wr_async_queues(bfqd, bfqg);
965ea25da48SPaolo Valente 	}
966ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
967ea25da48SPaolo Valente }
968ea25da48SPaolo Valente 
969795fe54cSFam Zheng static int bfq_io_show_weight_legacy(struct seq_file *sf, void *v)
970ea25da48SPaolo Valente {
971ea25da48SPaolo Valente 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
972ea25da48SPaolo Valente 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
973ea25da48SPaolo Valente 	unsigned int val = 0;
974ea25da48SPaolo Valente 
975ea25da48SPaolo Valente 	if (bfqgd)
976ea25da48SPaolo Valente 		val = bfqgd->weight;
977ea25da48SPaolo Valente 
978ea25da48SPaolo Valente 	seq_printf(sf, "%u\n", val);
979ea25da48SPaolo Valente 
980ea25da48SPaolo Valente 	return 0;
981ea25da48SPaolo Valente }
982ea25da48SPaolo Valente 
983795fe54cSFam Zheng static u64 bfqg_prfill_weight_device(struct seq_file *sf,
984795fe54cSFam Zheng 				     struct blkg_policy_data *pd, int off)
985ea25da48SPaolo Valente {
986795fe54cSFam Zheng 	struct bfq_group *bfqg = pd_to_bfqg(pd);
987795fe54cSFam Zheng 
988795fe54cSFam Zheng 	if (!bfqg->entity.dev_weight)
989795fe54cSFam Zheng 		return 0;
990795fe54cSFam Zheng 	return __blkg_prfill_u64(sf, pd, bfqg->entity.dev_weight);
991795fe54cSFam Zheng }
992795fe54cSFam Zheng 
993795fe54cSFam Zheng static int bfq_io_show_weight(struct seq_file *sf, void *v)
994795fe54cSFam Zheng {
995795fe54cSFam Zheng 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
996795fe54cSFam Zheng 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
997795fe54cSFam Zheng 
998795fe54cSFam Zheng 	seq_printf(sf, "default %u\n", bfqgd->weight);
999795fe54cSFam Zheng 	blkcg_print_blkgs(sf, blkcg, bfqg_prfill_weight_device,
1000795fe54cSFam Zheng 			  &blkcg_policy_bfq, 0, false);
1001795fe54cSFam Zheng 	return 0;
1002795fe54cSFam Zheng }
1003795fe54cSFam Zheng 
1004795fe54cSFam Zheng static void bfq_group_set_weight(struct bfq_group *bfqg, u64 weight, u64 dev_weight)
1005795fe54cSFam Zheng {
1006795fe54cSFam Zheng 	weight = dev_weight ?: weight;
1007795fe54cSFam Zheng 
1008795fe54cSFam Zheng 	bfqg->entity.dev_weight = dev_weight;
1009ea25da48SPaolo Valente 	/*
1010ea25da48SPaolo Valente 	 * Setting the prio_changed flag of the entity
1011ea25da48SPaolo Valente 	 * to 1 with new_weight == weight would re-set
1012ea25da48SPaolo Valente 	 * the value of the weight to its ioprio mapping.
1013ea25da48SPaolo Valente 	 * Set the flag only if necessary.
1014ea25da48SPaolo Valente 	 */
10155ff047e3SFam Zheng 	if ((unsigned short)weight != bfqg->entity.new_weight) {
10165ff047e3SFam Zheng 		bfqg->entity.new_weight = (unsigned short)weight;
1017ea25da48SPaolo Valente 		/*
1018ea25da48SPaolo Valente 		 * Make sure that the above new value has been
1019ea25da48SPaolo Valente 		 * stored in bfqg->entity.new_weight before
1020ea25da48SPaolo Valente 		 * setting the prio_changed flag. In fact,
1021ea25da48SPaolo Valente 		 * this flag may be read asynchronously (in
1022ea25da48SPaolo Valente 		 * critical sections protected by a different
1023ea25da48SPaolo Valente 		 * lock than that held here), and finding this
1024ea25da48SPaolo Valente 		 * flag set may cause the execution of the code
1025ea25da48SPaolo Valente 		 * for updating parameters whose value may
1026ea25da48SPaolo Valente 		 * depend also on bfqg->entity.new_weight (in
1027ea25da48SPaolo Valente 		 * __bfq_entity_update_weight_prio).
1028ea25da48SPaolo Valente 		 * This barrier makes sure that the new value
1029ea25da48SPaolo Valente 		 * of bfqg->entity.new_weight is correctly
1030ea25da48SPaolo Valente 		 * seen in that code.
1031ea25da48SPaolo Valente 		 */
1032ea25da48SPaolo Valente 		smp_wmb();
1033ea25da48SPaolo Valente 		bfqg->entity.prio_changed = 1;
1034ea25da48SPaolo Valente 	}
1035ea25da48SPaolo Valente }
10365ff047e3SFam Zheng 
10375ff047e3SFam Zheng static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css,
10385ff047e3SFam Zheng 				    struct cftype *cftype,
10395ff047e3SFam Zheng 				    u64 val)
10405ff047e3SFam Zheng {
10415ff047e3SFam Zheng 	struct blkcg *blkcg = css_to_blkcg(css);
10425ff047e3SFam Zheng 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
10435ff047e3SFam Zheng 	struct blkcg_gq *blkg;
10445ff047e3SFam Zheng 	int ret = -ERANGE;
10455ff047e3SFam Zheng 
10465ff047e3SFam Zheng 	if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT)
10475ff047e3SFam Zheng 		return ret;
10485ff047e3SFam Zheng 
10495ff047e3SFam Zheng 	ret = 0;
10505ff047e3SFam Zheng 	spin_lock_irq(&blkcg->lock);
10515ff047e3SFam Zheng 	bfqgd->weight = (unsigned short)val;
10525ff047e3SFam Zheng 	hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
10535ff047e3SFam Zheng 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
10545ff047e3SFam Zheng 
10555ff047e3SFam Zheng 		if (bfqg)
1056795fe54cSFam Zheng 			bfq_group_set_weight(bfqg, val, 0);
10575ff047e3SFam Zheng 	}
1058ea25da48SPaolo Valente 	spin_unlock_irq(&blkcg->lock);
1059ea25da48SPaolo Valente 
1060ea25da48SPaolo Valente 	return ret;
1061ea25da48SPaolo Valente }
1062ea25da48SPaolo Valente 
1063795fe54cSFam Zheng static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of,
1064795fe54cSFam Zheng 					char *buf, size_t nbytes,
1065795fe54cSFam Zheng 					loff_t off)
1066795fe54cSFam Zheng {
1067795fe54cSFam Zheng 	int ret;
1068795fe54cSFam Zheng 	struct blkg_conf_ctx ctx;
1069795fe54cSFam Zheng 	struct blkcg *blkcg = css_to_blkcg(of_css(of));
1070795fe54cSFam Zheng 	struct bfq_group *bfqg;
1071795fe54cSFam Zheng 	u64 v;
1072795fe54cSFam Zheng 
1073795fe54cSFam Zheng 	ret = blkg_conf_prep(blkcg, &blkcg_policy_bfq, buf, &ctx);
1074795fe54cSFam Zheng 	if (ret)
1075795fe54cSFam Zheng 		return ret;
1076795fe54cSFam Zheng 
1077795fe54cSFam Zheng 	if (sscanf(ctx.body, "%llu", &v) == 1) {
1078795fe54cSFam Zheng 		/* require "default" on dfl */
1079795fe54cSFam Zheng 		ret = -ERANGE;
1080795fe54cSFam Zheng 		if (!v)
1081795fe54cSFam Zheng 			goto out;
1082795fe54cSFam Zheng 	} else if (!strcmp(strim(ctx.body), "default")) {
1083795fe54cSFam Zheng 		v = 0;
1084795fe54cSFam Zheng 	} else {
1085795fe54cSFam Zheng 		ret = -EINVAL;
1086795fe54cSFam Zheng 		goto out;
1087795fe54cSFam Zheng 	}
1088795fe54cSFam Zheng 
1089795fe54cSFam Zheng 	bfqg = blkg_to_bfqg(ctx.blkg);
1090795fe54cSFam Zheng 
1091795fe54cSFam Zheng 	ret = -ERANGE;
1092795fe54cSFam Zheng 	if (!v || (v >= BFQ_MIN_WEIGHT && v <= BFQ_MAX_WEIGHT)) {
1093795fe54cSFam Zheng 		bfq_group_set_weight(bfqg, bfqg->entity.weight, v);
1094795fe54cSFam Zheng 		ret = 0;
1095795fe54cSFam Zheng 	}
1096795fe54cSFam Zheng out:
1097795fe54cSFam Zheng 	blkg_conf_finish(&ctx);
1098795fe54cSFam Zheng 	return ret ?: nbytes;
1099795fe54cSFam Zheng }
1100795fe54cSFam Zheng 
1101ea25da48SPaolo Valente static ssize_t bfq_io_set_weight(struct kernfs_open_file *of,
1102ea25da48SPaolo Valente 				 char *buf, size_t nbytes,
1103ea25da48SPaolo Valente 				 loff_t off)
1104ea25da48SPaolo Valente {
1105795fe54cSFam Zheng 	char *endp;
1106795fe54cSFam Zheng 	int ret;
1107795fe54cSFam Zheng 	u64 v;
1108ea25da48SPaolo Valente 
1109795fe54cSFam Zheng 	buf = strim(buf);
1110ea25da48SPaolo Valente 
1111795fe54cSFam Zheng 	/* "WEIGHT" or "default WEIGHT" sets the default weight */
1112795fe54cSFam Zheng 	v = simple_strtoull(buf, &endp, 0);
1113795fe54cSFam Zheng 	if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
1114795fe54cSFam Zheng 		ret = bfq_io_set_weight_legacy(of_css(of), NULL, v);
1115fc8ebd01SMaciej S. Szmigiero 		return ret ?: nbytes;
1116ea25da48SPaolo Valente 	}
1117ea25da48SPaolo Valente 
1118795fe54cSFam Zheng 	return bfq_io_set_device_weight(of, buf, nbytes, off);
1119795fe54cSFam Zheng }
1120795fe54cSFam Zheng 
1121ea25da48SPaolo Valente static int bfqg_print_rwstat(struct seq_file *sf, void *v)
1122ea25da48SPaolo Valente {
1123ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1124ea25da48SPaolo Valente 			  &blkcg_policy_bfq, seq_cft(sf)->private, true);
1125ea25da48SPaolo Valente 	return 0;
1126ea25da48SPaolo Valente }
1127ea25da48SPaolo Valente 
1128a557f1c7STejun Heo static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf,
1129a557f1c7STejun Heo 					struct blkg_policy_data *pd, int off)
1130a557f1c7STejun Heo {
1131a557f1c7STejun Heo 	struct blkg_rwstat_sample sum;
1132a557f1c7STejun Heo 
1133a557f1c7STejun Heo 	blkg_rwstat_recursive_sum(pd_to_blkg(pd), &blkcg_policy_bfq, off, &sum);
1134a557f1c7STejun Heo 	return __blkg_prfill_rwstat(sf, pd, &sum);
1135a557f1c7STejun Heo }
1136a557f1c7STejun Heo 
1137a557f1c7STejun Heo static int bfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
1138a557f1c7STejun Heo {
1139a557f1c7STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1140a557f1c7STejun Heo 			  bfqg_prfill_rwstat_recursive, &blkcg_policy_bfq,
1141a557f1c7STejun Heo 			  seq_cft(sf)->private, true);
1142a557f1c7STejun Heo 	return 0;
1143a557f1c7STejun Heo }
1144a557f1c7STejun Heo 
1145fd41e603STejun Heo #ifdef CONFIG_BFQ_CGROUP_DEBUG
1146a557f1c7STejun Heo static int bfqg_print_stat(struct seq_file *sf, void *v)
1147a557f1c7STejun Heo {
1148a557f1c7STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1149a557f1c7STejun Heo 			  &blkcg_policy_bfq, seq_cft(sf)->private, false);
1150a557f1c7STejun Heo 	return 0;
1151a557f1c7STejun Heo }
1152a557f1c7STejun Heo 
1153ea25da48SPaolo Valente static u64 bfqg_prfill_stat_recursive(struct seq_file *sf,
1154ea25da48SPaolo Valente 				      struct blkg_policy_data *pd, int off)
1155ea25da48SPaolo Valente {
1156d6258980SChristoph Hellwig 	struct blkcg_gq *blkg = pd_to_blkg(pd);
1157d6258980SChristoph Hellwig 	struct blkcg_gq *pos_blkg;
1158d6258980SChristoph Hellwig 	struct cgroup_subsys_state *pos_css;
1159d6258980SChristoph Hellwig 	u64 sum = 0;
1160d6258980SChristoph Hellwig 
1161d6258980SChristoph Hellwig 	lockdep_assert_held(&blkg->q->queue_lock);
1162d6258980SChristoph Hellwig 
1163d6258980SChristoph Hellwig 	rcu_read_lock();
1164d6258980SChristoph Hellwig 	blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
1165d6258980SChristoph Hellwig 		struct bfq_stat *stat;
1166d6258980SChristoph Hellwig 
1167d6258980SChristoph Hellwig 		if (!pos_blkg->online)
1168d6258980SChristoph Hellwig 			continue;
1169d6258980SChristoph Hellwig 
1170d6258980SChristoph Hellwig 		stat = (void *)blkg_to_pd(pos_blkg, &blkcg_policy_bfq) + off;
1171d6258980SChristoph Hellwig 		sum += bfq_stat_read(stat) + atomic64_read(&stat->aux_cnt);
1172d6258980SChristoph Hellwig 	}
1173d6258980SChristoph Hellwig 	rcu_read_unlock();
1174d6258980SChristoph Hellwig 
1175ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum);
1176ea25da48SPaolo Valente }
1177ea25da48SPaolo Valente 
1178ea25da48SPaolo Valente static int bfqg_print_stat_recursive(struct seq_file *sf, void *v)
1179ea25da48SPaolo Valente {
1180ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1181ea25da48SPaolo Valente 			  bfqg_prfill_stat_recursive, &blkcg_policy_bfq,
1182ea25da48SPaolo Valente 			  seq_cft(sf)->private, false);
1183ea25da48SPaolo Valente 	return 0;
1184ea25da48SPaolo Valente }
1185ea25da48SPaolo Valente 
1186ea25da48SPaolo Valente static u64 bfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1187ea25da48SPaolo Valente 			       int off)
1188ea25da48SPaolo Valente {
1189fd41e603STejun Heo 	struct bfq_group *bfqg = blkg_to_bfqg(pd->blkg);
1190fd41e603STejun Heo 	u64 sum = blkg_rwstat_total(&bfqg->stats.bytes);
1191ea25da48SPaolo Valente 
1192ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum >> 9);
1193ea25da48SPaolo Valente }
1194ea25da48SPaolo Valente 
1195ea25da48SPaolo Valente static int bfqg_print_stat_sectors(struct seq_file *sf, void *v)
1196ea25da48SPaolo Valente {
1197ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1198ea25da48SPaolo Valente 			  bfqg_prfill_sectors, &blkcg_policy_bfq, 0, false);
1199ea25da48SPaolo Valente 	return 0;
1200ea25da48SPaolo Valente }
1201ea25da48SPaolo Valente 
1202ea25da48SPaolo Valente static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf,
1203ea25da48SPaolo Valente 					 struct blkg_policy_data *pd, int off)
1204ea25da48SPaolo Valente {
12057af6fd91SChristoph Hellwig 	struct blkg_rwstat_sample tmp;
12065d0b6e48SChristoph Hellwig 
1207fd41e603STejun Heo 	blkg_rwstat_recursive_sum(pd->blkg, &blkcg_policy_bfq,
1208fd41e603STejun Heo 			offsetof(struct bfq_group, stats.bytes), &tmp);
1209ea25da48SPaolo Valente 
12107af6fd91SChristoph Hellwig 	return __blkg_prfill_u64(sf, pd,
12117af6fd91SChristoph Hellwig 		(tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]) >> 9);
1212ea25da48SPaolo Valente }
1213ea25da48SPaolo Valente 
1214ea25da48SPaolo Valente static int bfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1215ea25da48SPaolo Valente {
1216ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1217ea25da48SPaolo Valente 			  bfqg_prfill_sectors_recursive, &blkcg_policy_bfq, 0,
1218ea25da48SPaolo Valente 			  false);
1219ea25da48SPaolo Valente 	return 0;
1220ea25da48SPaolo Valente }
1221ea25da48SPaolo Valente 
1222ea25da48SPaolo Valente static u64 bfqg_prfill_avg_queue_size(struct seq_file *sf,
1223ea25da48SPaolo Valente 				      struct blkg_policy_data *pd, int off)
1224ea25da48SPaolo Valente {
1225ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
1226c0ce79dcSChristoph Hellwig 	u64 samples = bfq_stat_read(&bfqg->stats.avg_queue_size_samples);
1227ea25da48SPaolo Valente 	u64 v = 0;
1228ea25da48SPaolo Valente 
1229ea25da48SPaolo Valente 	if (samples) {
1230c0ce79dcSChristoph Hellwig 		v = bfq_stat_read(&bfqg->stats.avg_queue_size_sum);
1231ea25da48SPaolo Valente 		v = div64_u64(v, samples);
1232ea25da48SPaolo Valente 	}
1233ea25da48SPaolo Valente 	__blkg_prfill_u64(sf, pd, v);
1234ea25da48SPaolo Valente 	return 0;
1235ea25da48SPaolo Valente }
1236ea25da48SPaolo Valente 
1237ea25da48SPaolo Valente /* print avg_queue_size */
1238ea25da48SPaolo Valente static int bfqg_print_avg_queue_size(struct seq_file *sf, void *v)
1239ea25da48SPaolo Valente {
1240ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1241ea25da48SPaolo Valente 			  bfqg_prfill_avg_queue_size, &blkcg_policy_bfq,
1242ea25da48SPaolo Valente 			  0, false);
1243ea25da48SPaolo Valente 	return 0;
1244ea25da48SPaolo Valente }
12458060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
1246ea25da48SPaolo Valente 
1247ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1248ea25da48SPaolo Valente {
1249ea25da48SPaolo Valente 	int ret;
1250ea25da48SPaolo Valente 
1251ea25da48SPaolo Valente 	ret = blkcg_activate_policy(bfqd->queue, &blkcg_policy_bfq);
1252ea25da48SPaolo Valente 	if (ret)
1253ea25da48SPaolo Valente 		return NULL;
1254ea25da48SPaolo Valente 
1255ea25da48SPaolo Valente 	return blkg_to_bfqg(bfqd->queue->root_blkg);
1256ea25da48SPaolo Valente }
1257ea25da48SPaolo Valente 
1258ea25da48SPaolo Valente struct blkcg_policy blkcg_policy_bfq = {
1259ea25da48SPaolo Valente 	.dfl_cftypes		= bfq_blkg_files,
1260ea25da48SPaolo Valente 	.legacy_cftypes		= bfq_blkcg_legacy_files,
1261ea25da48SPaolo Valente 
1262ea25da48SPaolo Valente 	.cpd_alloc_fn		= bfq_cpd_alloc,
1263ea25da48SPaolo Valente 	.cpd_init_fn		= bfq_cpd_init,
1264ea25da48SPaolo Valente 	.cpd_bind_fn	        = bfq_cpd_init,
1265ea25da48SPaolo Valente 	.cpd_free_fn		= bfq_cpd_free,
1266ea25da48SPaolo Valente 
1267ea25da48SPaolo Valente 	.pd_alloc_fn		= bfq_pd_alloc,
1268ea25da48SPaolo Valente 	.pd_init_fn		= bfq_pd_init,
1269ea25da48SPaolo Valente 	.pd_offline_fn		= bfq_pd_offline,
1270ea25da48SPaolo Valente 	.pd_free_fn		= bfq_pd_free,
1271ea25da48SPaolo Valente 	.pd_reset_stats_fn	= bfq_pd_reset_stats,
1272ea25da48SPaolo Valente };
1273ea25da48SPaolo Valente 
1274ea25da48SPaolo Valente struct cftype bfq_blkcg_legacy_files[] = {
1275ea25da48SPaolo Valente 	{
1276ea25da48SPaolo Valente 		.name = "bfq.weight",
1277cf892988SJens Axboe 		.flags = CFTYPE_NOT_ON_ROOT,
1278795fe54cSFam Zheng 		.seq_show = bfq_io_show_weight_legacy,
1279ea25da48SPaolo Valente 		.write_u64 = bfq_io_set_weight_legacy,
1280ea25da48SPaolo Valente 	},
1281795fe54cSFam Zheng 	{
1282795fe54cSFam Zheng 		.name = "bfq.weight_device",
1283795fe54cSFam Zheng 		.flags = CFTYPE_NOT_ON_ROOT,
1284795fe54cSFam Zheng 		.seq_show = bfq_io_show_weight,
1285795fe54cSFam Zheng 		.write = bfq_io_set_weight,
1286795fe54cSFam Zheng 	},
1287ea25da48SPaolo Valente 
1288ea25da48SPaolo Valente 	/* statistics, covers only the tasks in the bfqg */
1289ea25da48SPaolo Valente 	{
1290ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes",
1291fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.bytes),
1292fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat,
1293ea25da48SPaolo Valente 	},
1294ea25da48SPaolo Valente 	{
1295ea25da48SPaolo Valente 		.name = "bfq.io_serviced",
1296fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.ios),
1297fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat,
1298ea25da48SPaolo Valente 	},
12998060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
1300a33801e8SLuca Miccio 	{
1301a33801e8SLuca Miccio 		.name = "bfq.time",
1302a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1303a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat,
1304a33801e8SLuca Miccio 	},
1305a33801e8SLuca Miccio 	{
1306a33801e8SLuca Miccio 		.name = "bfq.sectors",
1307a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors,
1308a33801e8SLuca Miccio 	},
1309ea25da48SPaolo Valente 	{
1310ea25da48SPaolo Valente 		.name = "bfq.io_service_time",
1311ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1312ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1313ea25da48SPaolo Valente 	},
1314ea25da48SPaolo Valente 	{
1315ea25da48SPaolo Valente 		.name = "bfq.io_wait_time",
1316ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1317ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1318ea25da48SPaolo Valente 	},
1319ea25da48SPaolo Valente 	{
1320ea25da48SPaolo Valente 		.name = "bfq.io_merged",
1321ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1322ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1323ea25da48SPaolo Valente 	},
1324ea25da48SPaolo Valente 	{
1325ea25da48SPaolo Valente 		.name = "bfq.io_queued",
1326ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1327ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1328ea25da48SPaolo Valente 	},
13298060c47bSChristoph Hellwig #endif /* CONFIG_BFQ_CGROUP_DEBUG */
1330ea25da48SPaolo Valente 
1331636b8fe8SAngelo Ruocco 	/* the same statistics which cover the bfqg and its descendants */
1332ea25da48SPaolo Valente 	{
1333ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes_recursive",
1334fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.bytes),
1335fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat_recursive,
1336ea25da48SPaolo Valente 	},
1337ea25da48SPaolo Valente 	{
1338ea25da48SPaolo Valente 		.name = "bfq.io_serviced_recursive",
1339fd41e603STejun Heo 		.private = offsetof(struct bfq_group, stats.ios),
1340fd41e603STejun Heo 		.seq_show = bfqg_print_rwstat_recursive,
1341ea25da48SPaolo Valente 	},
13428060c47bSChristoph Hellwig #ifdef CONFIG_BFQ_CGROUP_DEBUG
1343a33801e8SLuca Miccio 	{
1344a33801e8SLuca Miccio 		.name = "bfq.time_recursive",
1345a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1346a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_recursive,
1347a33801e8SLuca Miccio 	},
1348a33801e8SLuca Miccio 	{
1349a33801e8SLuca Miccio 		.name = "bfq.sectors_recursive",
1350a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors_recursive,
1351a33801e8SLuca Miccio 	},
1352ea25da48SPaolo Valente 	{
1353ea25da48SPaolo Valente 		.name = "bfq.io_service_time_recursive",
1354ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1355ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1356ea25da48SPaolo Valente 	},
1357ea25da48SPaolo Valente 	{
1358ea25da48SPaolo Valente 		.name = "bfq.io_wait_time_recursive",
1359ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1360ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1361ea25da48SPaolo Valente 	},
1362ea25da48SPaolo Valente 	{
1363ea25da48SPaolo Valente 		.name = "bfq.io_merged_recursive",
1364ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1365ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1366ea25da48SPaolo Valente 	},
1367ea25da48SPaolo Valente 	{
1368ea25da48SPaolo Valente 		.name = "bfq.io_queued_recursive",
1369ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1370ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1371ea25da48SPaolo Valente 	},
1372ea25da48SPaolo Valente 	{
1373ea25da48SPaolo Valente 		.name = "bfq.avg_queue_size",
1374ea25da48SPaolo Valente 		.seq_show = bfqg_print_avg_queue_size,
1375ea25da48SPaolo Valente 	},
1376ea25da48SPaolo Valente 	{
1377ea25da48SPaolo Valente 		.name = "bfq.group_wait_time",
1378ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.group_wait_time),
1379ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1380ea25da48SPaolo Valente 	},
1381ea25da48SPaolo Valente 	{
1382ea25da48SPaolo Valente 		.name = "bfq.idle_time",
1383ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.idle_time),
1384ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1385ea25da48SPaolo Valente 	},
1386ea25da48SPaolo Valente 	{
1387ea25da48SPaolo Valente 		.name = "bfq.empty_time",
1388ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.empty_time),
1389ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1390ea25da48SPaolo Valente 	},
1391ea25da48SPaolo Valente 	{
1392ea25da48SPaolo Valente 		.name = "bfq.dequeue",
1393ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.dequeue),
1394ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1395ea25da48SPaolo Valente 	},
13968060c47bSChristoph Hellwig #endif	/* CONFIG_BFQ_CGROUP_DEBUG */
1397ea25da48SPaolo Valente 	{ }	/* terminate */
1398ea25da48SPaolo Valente };
1399ea25da48SPaolo Valente 
1400ea25da48SPaolo Valente struct cftype bfq_blkg_files[] = {
1401ea25da48SPaolo Valente 	{
1402ea25da48SPaolo Valente 		.name = "bfq.weight",
1403cf892988SJens Axboe 		.flags = CFTYPE_NOT_ON_ROOT,
1404ea25da48SPaolo Valente 		.seq_show = bfq_io_show_weight,
1405ea25da48SPaolo Valente 		.write = bfq_io_set_weight,
1406ea25da48SPaolo Valente 	},
1407ea25da48SPaolo Valente 	{} /* terminate */
1408ea25da48SPaolo Valente };
1409ea25da48SPaolo Valente 
1410ea25da48SPaolo Valente #else	/* CONFIG_BFQ_GROUP_IOSCHED */
1411ea25da48SPaolo Valente 
1412ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1413ea25da48SPaolo Valente 		   struct bfq_group *bfqg) {}
1414ea25da48SPaolo Valente 
1415ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
1416ea25da48SPaolo Valente {
1417ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
1418ea25da48SPaolo Valente 
1419ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
1420ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
1421ea25da48SPaolo Valente 	if (bfqq) {
1422ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
1423ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
1424ea25da48SPaolo Valente 	}
1425ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
1426ea25da48SPaolo Valente }
1427ea25da48SPaolo Valente 
1428ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {}
1429ea25da48SPaolo Valente 
1430ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
1431ea25da48SPaolo Valente {
1432ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1433ea25da48SPaolo Valente }
1434ea25da48SPaolo Valente 
1435ea25da48SPaolo Valente struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, struct blkcg *blkcg)
1436ea25da48SPaolo Valente {
1437ea25da48SPaolo Valente 	return bfqd->root_group;
1438ea25da48SPaolo Valente }
1439ea25da48SPaolo Valente 
1440ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
1441ea25da48SPaolo Valente {
1442ea25da48SPaolo Valente 	return bfqq->bfqd->root_group;
1443ea25da48SPaolo Valente }
1444ea25da48SPaolo Valente 
14454d8340d0SPaolo Valente void bfqg_and_blkg_get(struct bfq_group *bfqg) {}
14464d8340d0SPaolo Valente 
14474d8340d0SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg) {}
14484d8340d0SPaolo Valente 
1449ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1450ea25da48SPaolo Valente {
1451ea25da48SPaolo Valente 	struct bfq_group *bfqg;
1452ea25da48SPaolo Valente 	int i;
1453ea25da48SPaolo Valente 
1454ea25da48SPaolo Valente 	bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
1455ea25da48SPaolo Valente 	if (!bfqg)
1456ea25da48SPaolo Valente 		return NULL;
1457ea25da48SPaolo Valente 
1458ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
1459ea25da48SPaolo Valente 		bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
1460ea25da48SPaolo Valente 
1461ea25da48SPaolo Valente 	return bfqg;
1462ea25da48SPaolo Valente }
1463ea25da48SPaolo Valente #endif	/* CONFIG_BFQ_GROUP_IOSCHED */
1464