xref: /openbmc/linux/block/bfq-cgroup.c (revision 0fe061b9)
1ea25da48SPaolo Valente /*
2ea25da48SPaolo Valente  * cgroups support for the BFQ I/O scheduler.
3ea25da48SPaolo Valente  *
4ea25da48SPaolo Valente  *  This program is free software; you can redistribute it and/or
5ea25da48SPaolo Valente  *  modify it under the terms of the GNU General Public License as
6ea25da48SPaolo Valente  *  published by the Free Software Foundation; either version 2 of the
7ea25da48SPaolo Valente  *  License, or (at your option) any later version.
8ea25da48SPaolo Valente  *
9ea25da48SPaolo Valente  *  This program is distributed in the hope that it will be useful,
10ea25da48SPaolo Valente  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11ea25da48SPaolo Valente  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12ea25da48SPaolo Valente  *  General Public License for more details.
13ea25da48SPaolo Valente  */
14ea25da48SPaolo Valente #include <linux/module.h>
15ea25da48SPaolo Valente #include <linux/slab.h>
16ea25da48SPaolo Valente #include <linux/blkdev.h>
17ea25da48SPaolo Valente #include <linux/cgroup.h>
18ea25da48SPaolo Valente #include <linux/elevator.h>
19ea25da48SPaolo Valente #include <linux/ktime.h>
20ea25da48SPaolo Valente #include <linux/rbtree.h>
21ea25da48SPaolo Valente #include <linux/ioprio.h>
22ea25da48SPaolo Valente #include <linux/sbitmap.h>
23ea25da48SPaolo Valente #include <linux/delay.h>
24ea25da48SPaolo Valente 
25ea25da48SPaolo Valente #include "bfq-iosched.h"
26ea25da48SPaolo Valente 
27a33801e8SLuca Miccio #if defined(CONFIG_BFQ_GROUP_IOSCHED) &&  defined(CONFIG_DEBUG_BLK_CGROUP)
28ea25da48SPaolo Valente 
29ea25da48SPaolo Valente /* bfqg stats flags */
30ea25da48SPaolo Valente enum bfqg_stats_flags {
31ea25da48SPaolo Valente 	BFQG_stats_waiting = 0,
32ea25da48SPaolo Valente 	BFQG_stats_idling,
33ea25da48SPaolo Valente 	BFQG_stats_empty,
34ea25da48SPaolo Valente };
35ea25da48SPaolo Valente 
36ea25da48SPaolo Valente #define BFQG_FLAG_FNS(name)						\
37ea25da48SPaolo Valente static void bfqg_stats_mark_##name(struct bfqg_stats *stats)	\
38ea25da48SPaolo Valente {									\
39ea25da48SPaolo Valente 	stats->flags |= (1 << BFQG_stats_##name);			\
40ea25da48SPaolo Valente }									\
41ea25da48SPaolo Valente static void bfqg_stats_clear_##name(struct bfqg_stats *stats)	\
42ea25da48SPaolo Valente {									\
43ea25da48SPaolo Valente 	stats->flags &= ~(1 << BFQG_stats_##name);			\
44ea25da48SPaolo Valente }									\
45ea25da48SPaolo Valente static int bfqg_stats_##name(struct bfqg_stats *stats)		\
46ea25da48SPaolo Valente {									\
47ea25da48SPaolo Valente 	return (stats->flags & (1 << BFQG_stats_##name)) != 0;		\
48ea25da48SPaolo Valente }									\
49ea25da48SPaolo Valente 
50ea25da48SPaolo Valente BFQG_FLAG_FNS(waiting)
51ea25da48SPaolo Valente BFQG_FLAG_FNS(idling)
52ea25da48SPaolo Valente BFQG_FLAG_FNS(empty)
53ea25da48SPaolo Valente #undef BFQG_FLAG_FNS
54ea25da48SPaolo Valente 
558f9bebc3SPaolo Valente /* This should be called with the scheduler lock held. */
56ea25da48SPaolo Valente static void bfqg_stats_update_group_wait_time(struct bfqg_stats *stats)
57ea25da48SPaolo Valente {
5884c7afceSOmar Sandoval 	u64 now;
59ea25da48SPaolo Valente 
60ea25da48SPaolo Valente 	if (!bfqg_stats_waiting(stats))
61ea25da48SPaolo Valente 		return;
62ea25da48SPaolo Valente 
6384c7afceSOmar Sandoval 	now = ktime_get_ns();
6484c7afceSOmar Sandoval 	if (now > stats->start_group_wait_time)
65ea25da48SPaolo Valente 		blkg_stat_add(&stats->group_wait_time,
66ea25da48SPaolo Valente 			      now - stats->start_group_wait_time);
67ea25da48SPaolo Valente 	bfqg_stats_clear_waiting(stats);
68ea25da48SPaolo Valente }
69ea25da48SPaolo Valente 
708f9bebc3SPaolo Valente /* This should be called with the scheduler lock held. */
71ea25da48SPaolo Valente static void bfqg_stats_set_start_group_wait_time(struct bfq_group *bfqg,
72ea25da48SPaolo Valente 						 struct bfq_group *curr_bfqg)
73ea25da48SPaolo Valente {
74ea25da48SPaolo Valente 	struct bfqg_stats *stats = &bfqg->stats;
75ea25da48SPaolo Valente 
76ea25da48SPaolo Valente 	if (bfqg_stats_waiting(stats))
77ea25da48SPaolo Valente 		return;
78ea25da48SPaolo Valente 	if (bfqg == curr_bfqg)
79ea25da48SPaolo Valente 		return;
8084c7afceSOmar Sandoval 	stats->start_group_wait_time = ktime_get_ns();
81ea25da48SPaolo Valente 	bfqg_stats_mark_waiting(stats);
82ea25da48SPaolo Valente }
83ea25da48SPaolo Valente 
848f9bebc3SPaolo Valente /* This should be called with the scheduler lock held. */
85ea25da48SPaolo Valente static void bfqg_stats_end_empty_time(struct bfqg_stats *stats)
86ea25da48SPaolo Valente {
8784c7afceSOmar Sandoval 	u64 now;
88ea25da48SPaolo Valente 
89ea25da48SPaolo Valente 	if (!bfqg_stats_empty(stats))
90ea25da48SPaolo Valente 		return;
91ea25da48SPaolo Valente 
9284c7afceSOmar Sandoval 	now = ktime_get_ns();
9384c7afceSOmar Sandoval 	if (now > stats->start_empty_time)
94ea25da48SPaolo Valente 		blkg_stat_add(&stats->empty_time,
95ea25da48SPaolo Valente 			      now - stats->start_empty_time);
96ea25da48SPaolo Valente 	bfqg_stats_clear_empty(stats);
97ea25da48SPaolo Valente }
98ea25da48SPaolo Valente 
99ea25da48SPaolo Valente void bfqg_stats_update_dequeue(struct bfq_group *bfqg)
100ea25da48SPaolo Valente {
101ea25da48SPaolo Valente 	blkg_stat_add(&bfqg->stats.dequeue, 1);
102ea25da48SPaolo Valente }
103ea25da48SPaolo Valente 
104ea25da48SPaolo Valente void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg)
105ea25da48SPaolo Valente {
106ea25da48SPaolo Valente 	struct bfqg_stats *stats = &bfqg->stats;
107ea25da48SPaolo Valente 
108ea25da48SPaolo Valente 	if (blkg_rwstat_total(&stats->queued))
109ea25da48SPaolo Valente 		return;
110ea25da48SPaolo Valente 
111ea25da48SPaolo Valente 	/*
112ea25da48SPaolo Valente 	 * group is already marked empty. This can happen if bfqq got new
113ea25da48SPaolo Valente 	 * request in parent group and moved to this group while being added
114ea25da48SPaolo Valente 	 * to service tree. Just ignore the event and move on.
115ea25da48SPaolo Valente 	 */
116ea25da48SPaolo Valente 	if (bfqg_stats_empty(stats))
117ea25da48SPaolo Valente 		return;
118ea25da48SPaolo Valente 
11984c7afceSOmar Sandoval 	stats->start_empty_time = ktime_get_ns();
120ea25da48SPaolo Valente 	bfqg_stats_mark_empty(stats);
121ea25da48SPaolo Valente }
122ea25da48SPaolo Valente 
123ea25da48SPaolo Valente void bfqg_stats_update_idle_time(struct bfq_group *bfqg)
124ea25da48SPaolo Valente {
125ea25da48SPaolo Valente 	struct bfqg_stats *stats = &bfqg->stats;
126ea25da48SPaolo Valente 
127ea25da48SPaolo Valente 	if (bfqg_stats_idling(stats)) {
12884c7afceSOmar Sandoval 		u64 now = ktime_get_ns();
129ea25da48SPaolo Valente 
13084c7afceSOmar Sandoval 		if (now > stats->start_idle_time)
131ea25da48SPaolo Valente 			blkg_stat_add(&stats->idle_time,
132ea25da48SPaolo Valente 				      now - stats->start_idle_time);
133ea25da48SPaolo Valente 		bfqg_stats_clear_idling(stats);
134ea25da48SPaolo Valente 	}
135ea25da48SPaolo Valente }
136ea25da48SPaolo Valente 
137ea25da48SPaolo Valente void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg)
138ea25da48SPaolo Valente {
139ea25da48SPaolo Valente 	struct bfqg_stats *stats = &bfqg->stats;
140ea25da48SPaolo Valente 
14184c7afceSOmar Sandoval 	stats->start_idle_time = ktime_get_ns();
142ea25da48SPaolo Valente 	bfqg_stats_mark_idling(stats);
143ea25da48SPaolo Valente }
144ea25da48SPaolo Valente 
145ea25da48SPaolo Valente void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg)
146ea25da48SPaolo Valente {
147ea25da48SPaolo Valente 	struct bfqg_stats *stats = &bfqg->stats;
148ea25da48SPaolo Valente 
149ea25da48SPaolo Valente 	blkg_stat_add(&stats->avg_queue_size_sum,
150ea25da48SPaolo Valente 		      blkg_rwstat_total(&stats->queued));
151ea25da48SPaolo Valente 	blkg_stat_add(&stats->avg_queue_size_samples, 1);
152ea25da48SPaolo Valente 	bfqg_stats_update_group_wait_time(stats);
153ea25da48SPaolo Valente }
154ea25da48SPaolo Valente 
155a33801e8SLuca Miccio void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
156a33801e8SLuca Miccio 			      unsigned int op)
157a33801e8SLuca Miccio {
158a33801e8SLuca Miccio 	blkg_rwstat_add(&bfqg->stats.queued, op, 1);
159a33801e8SLuca Miccio 	bfqg_stats_end_empty_time(&bfqg->stats);
160a33801e8SLuca Miccio 	if (!(bfqq == ((struct bfq_data *)bfqg->bfqd)->in_service_queue))
161a33801e8SLuca Miccio 		bfqg_stats_set_start_group_wait_time(bfqg, bfqq_group(bfqq));
162a33801e8SLuca Miccio }
163a33801e8SLuca Miccio 
164a33801e8SLuca Miccio void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op)
165a33801e8SLuca Miccio {
166a33801e8SLuca Miccio 	blkg_rwstat_add(&bfqg->stats.queued, op, -1);
167a33801e8SLuca Miccio }
168a33801e8SLuca Miccio 
169a33801e8SLuca Miccio void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op)
170a33801e8SLuca Miccio {
171a33801e8SLuca Miccio 	blkg_rwstat_add(&bfqg->stats.merged, op, 1);
172a33801e8SLuca Miccio }
173a33801e8SLuca Miccio 
17484c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
17584c7afceSOmar Sandoval 				  u64 io_start_time_ns, unsigned int op)
176a33801e8SLuca Miccio {
177a33801e8SLuca Miccio 	struct bfqg_stats *stats = &bfqg->stats;
17884c7afceSOmar Sandoval 	u64 now = ktime_get_ns();
179a33801e8SLuca Miccio 
18084c7afceSOmar Sandoval 	if (now > io_start_time_ns)
181a33801e8SLuca Miccio 		blkg_rwstat_add(&stats->service_time, op,
18284c7afceSOmar Sandoval 				now - io_start_time_ns);
18384c7afceSOmar Sandoval 	if (io_start_time_ns > start_time_ns)
184a33801e8SLuca Miccio 		blkg_rwstat_add(&stats->wait_time, op,
18584c7afceSOmar Sandoval 				io_start_time_ns - start_time_ns);
186a33801e8SLuca Miccio }
187a33801e8SLuca Miccio 
188a33801e8SLuca Miccio #else /* CONFIG_BFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
189a33801e8SLuca Miccio 
190a33801e8SLuca Miccio void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
191a33801e8SLuca Miccio 			      unsigned int op) { }
192a33801e8SLuca Miccio void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op) { }
193a33801e8SLuca Miccio void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op) { }
19484c7afceSOmar Sandoval void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
19584c7afceSOmar Sandoval 				  u64 io_start_time_ns, unsigned int op) { }
196a33801e8SLuca Miccio void bfqg_stats_update_dequeue(struct bfq_group *bfqg) { }
197a33801e8SLuca Miccio void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg) { }
198a33801e8SLuca Miccio void bfqg_stats_update_idle_time(struct bfq_group *bfqg) { }
199a33801e8SLuca Miccio void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) { }
200a33801e8SLuca Miccio void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg) { }
201a33801e8SLuca Miccio 
202a33801e8SLuca Miccio #endif /* CONFIG_BFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
203a33801e8SLuca Miccio 
204a33801e8SLuca Miccio #ifdef CONFIG_BFQ_GROUP_IOSCHED
205a33801e8SLuca Miccio 
206ea25da48SPaolo Valente /*
207ea25da48SPaolo Valente  * blk-cgroup policy-related handlers
208ea25da48SPaolo Valente  * The following functions help in converting between blk-cgroup
209ea25da48SPaolo Valente  * internal structures and BFQ-specific structures.
210ea25da48SPaolo Valente  */
211ea25da48SPaolo Valente 
212ea25da48SPaolo Valente static struct bfq_group *pd_to_bfqg(struct blkg_policy_data *pd)
213ea25da48SPaolo Valente {
214ea25da48SPaolo Valente 	return pd ? container_of(pd, struct bfq_group, pd) : NULL;
215ea25da48SPaolo Valente }
216ea25da48SPaolo Valente 
217ea25da48SPaolo Valente struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg)
218ea25da48SPaolo Valente {
219ea25da48SPaolo Valente 	return pd_to_blkg(&bfqg->pd);
220ea25da48SPaolo Valente }
221ea25da48SPaolo Valente 
222ea25da48SPaolo Valente static struct bfq_group *blkg_to_bfqg(struct blkcg_gq *blkg)
223ea25da48SPaolo Valente {
224ea25da48SPaolo Valente 	return pd_to_bfqg(blkg_to_pd(blkg, &blkcg_policy_bfq));
225ea25da48SPaolo Valente }
226ea25da48SPaolo Valente 
227ea25da48SPaolo Valente /*
228ea25da48SPaolo Valente  * bfq_group handlers
229ea25da48SPaolo Valente  * The following functions help in navigating the bfq_group hierarchy
230ea25da48SPaolo Valente  * by allowing to find the parent of a bfq_group or the bfq_group
231ea25da48SPaolo Valente  * associated to a bfq_queue.
232ea25da48SPaolo Valente  */
233ea25da48SPaolo Valente 
234ea25da48SPaolo Valente static struct bfq_group *bfqg_parent(struct bfq_group *bfqg)
235ea25da48SPaolo Valente {
236ea25da48SPaolo Valente 	struct blkcg_gq *pblkg = bfqg_to_blkg(bfqg)->parent;
237ea25da48SPaolo Valente 
238ea25da48SPaolo Valente 	return pblkg ? blkg_to_bfqg(pblkg) : NULL;
239ea25da48SPaolo Valente }
240ea25da48SPaolo Valente 
241ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
242ea25da48SPaolo Valente {
243ea25da48SPaolo Valente 	struct bfq_entity *group_entity = bfqq->entity.parent;
244ea25da48SPaolo Valente 
245ea25da48SPaolo Valente 	return group_entity ? container_of(group_entity, struct bfq_group,
246ea25da48SPaolo Valente 					   entity) :
247ea25da48SPaolo Valente 			      bfqq->bfqd->root_group;
248ea25da48SPaolo Valente }
249ea25da48SPaolo Valente 
250ea25da48SPaolo Valente /*
251ea25da48SPaolo Valente  * The following two functions handle get and put of a bfq_group by
252ea25da48SPaolo Valente  * wrapping the related blk-cgroup hooks.
253ea25da48SPaolo Valente  */
254ea25da48SPaolo Valente 
255ea25da48SPaolo Valente static void bfqg_get(struct bfq_group *bfqg)
256ea25da48SPaolo Valente {
2578f9bebc3SPaolo Valente 	bfqg->ref++;
258ea25da48SPaolo Valente }
259ea25da48SPaolo Valente 
260dfb79af5SBart Van Assche static void bfqg_put(struct bfq_group *bfqg)
261ea25da48SPaolo Valente {
2628f9bebc3SPaolo Valente 	bfqg->ref--;
2638f9bebc3SPaolo Valente 
2648f9bebc3SPaolo Valente 	if (bfqg->ref == 0)
2658f9bebc3SPaolo Valente 		kfree(bfqg);
2668f9bebc3SPaolo Valente }
2678f9bebc3SPaolo Valente 
2688f9bebc3SPaolo Valente static void bfqg_and_blkg_get(struct bfq_group *bfqg)
2698f9bebc3SPaolo Valente {
2708f9bebc3SPaolo Valente 	/* see comments in bfq_bic_update_cgroup for why refcounting bfqg */
2718f9bebc3SPaolo Valente 	bfqg_get(bfqg);
2728f9bebc3SPaolo Valente 
2738f9bebc3SPaolo Valente 	blkg_get(bfqg_to_blkg(bfqg));
2748f9bebc3SPaolo Valente }
2758f9bebc3SPaolo Valente 
2768f9bebc3SPaolo Valente void bfqg_and_blkg_put(struct bfq_group *bfqg)
2778f9bebc3SPaolo Valente {
2788f9bebc3SPaolo Valente 	blkg_put(bfqg_to_blkg(bfqg));
279d5274b3cSKonstantin Khlebnikov 
280d5274b3cSKonstantin Khlebnikov 	bfqg_put(bfqg);
281ea25da48SPaolo Valente }
282ea25da48SPaolo Valente 
283ea25da48SPaolo Valente /* @stats = 0 */
284ea25da48SPaolo Valente static void bfqg_stats_reset(struct bfqg_stats *stats)
285ea25da48SPaolo Valente {
286a33801e8SLuca Miccio #ifdef CONFIG_DEBUG_BLK_CGROUP
287ea25da48SPaolo Valente 	/* queued stats shouldn't be cleared */
288ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->merged);
289ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->service_time);
290ea25da48SPaolo Valente 	blkg_rwstat_reset(&stats->wait_time);
291ea25da48SPaolo Valente 	blkg_stat_reset(&stats->time);
292ea25da48SPaolo Valente 	blkg_stat_reset(&stats->avg_queue_size_sum);
293ea25da48SPaolo Valente 	blkg_stat_reset(&stats->avg_queue_size_samples);
294ea25da48SPaolo Valente 	blkg_stat_reset(&stats->dequeue);
295ea25da48SPaolo Valente 	blkg_stat_reset(&stats->group_wait_time);
296ea25da48SPaolo Valente 	blkg_stat_reset(&stats->idle_time);
297ea25da48SPaolo Valente 	blkg_stat_reset(&stats->empty_time);
298a33801e8SLuca Miccio #endif
299ea25da48SPaolo Valente }
300ea25da48SPaolo Valente 
301ea25da48SPaolo Valente /* @to += @from */
302ea25da48SPaolo Valente static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from)
303ea25da48SPaolo Valente {
304ea25da48SPaolo Valente 	if (!to || !from)
305ea25da48SPaolo Valente 		return;
306ea25da48SPaolo Valente 
307a33801e8SLuca Miccio #ifdef CONFIG_DEBUG_BLK_CGROUP
308ea25da48SPaolo Valente 	/* queued stats shouldn't be cleared */
309ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->merged, &from->merged);
310ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->service_time, &from->service_time);
311ea25da48SPaolo Valente 	blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
312ea25da48SPaolo Valente 	blkg_stat_add_aux(&from->time, &from->time);
313ea25da48SPaolo Valente 	blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
314ea25da48SPaolo Valente 	blkg_stat_add_aux(&to->avg_queue_size_samples,
315ea25da48SPaolo Valente 			  &from->avg_queue_size_samples);
316ea25da48SPaolo Valente 	blkg_stat_add_aux(&to->dequeue, &from->dequeue);
317ea25da48SPaolo Valente 	blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
318ea25da48SPaolo Valente 	blkg_stat_add_aux(&to->idle_time, &from->idle_time);
319ea25da48SPaolo Valente 	blkg_stat_add_aux(&to->empty_time, &from->empty_time);
320a33801e8SLuca Miccio #endif
321ea25da48SPaolo Valente }
322ea25da48SPaolo Valente 
323ea25da48SPaolo Valente /*
324ea25da48SPaolo Valente  * Transfer @bfqg's stats to its parent's aux counts so that the ancestors'
325ea25da48SPaolo Valente  * recursive stats can still account for the amount used by this bfqg after
326ea25da48SPaolo Valente  * it's gone.
327ea25da48SPaolo Valente  */
328ea25da48SPaolo Valente static void bfqg_stats_xfer_dead(struct bfq_group *bfqg)
329ea25da48SPaolo Valente {
330ea25da48SPaolo Valente 	struct bfq_group *parent;
331ea25da48SPaolo Valente 
332ea25da48SPaolo Valente 	if (!bfqg) /* root_group */
333ea25da48SPaolo Valente 		return;
334ea25da48SPaolo Valente 
335ea25da48SPaolo Valente 	parent = bfqg_parent(bfqg);
336ea25da48SPaolo Valente 
3370d945c1fSChristoph Hellwig 	lockdep_assert_held(&bfqg_to_blkg(bfqg)->q->queue_lock);
338ea25da48SPaolo Valente 
339ea25da48SPaolo Valente 	if (unlikely(!parent))
340ea25da48SPaolo Valente 		return;
341ea25da48SPaolo Valente 
342ea25da48SPaolo Valente 	bfqg_stats_add_aux(&parent->stats, &bfqg->stats);
343ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
344ea25da48SPaolo Valente }
345ea25da48SPaolo Valente 
346ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
347ea25da48SPaolo Valente {
348ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
349ea25da48SPaolo Valente 
350ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
351ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
352ea25da48SPaolo Valente 	if (bfqq) {
353ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
354ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
3558f9bebc3SPaolo Valente 		/*
3568f9bebc3SPaolo Valente 		 * Make sure that bfqg and its associated blkg do not
3578f9bebc3SPaolo Valente 		 * disappear before entity.
3588f9bebc3SPaolo Valente 		 */
3598f9bebc3SPaolo Valente 		bfqg_and_blkg_get(bfqg);
360ea25da48SPaolo Valente 	}
361ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity; /* NULL for root group */
362ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
363ea25da48SPaolo Valente }
364ea25da48SPaolo Valente 
365ea25da48SPaolo Valente static void bfqg_stats_exit(struct bfqg_stats *stats)
366ea25da48SPaolo Valente {
367a33801e8SLuca Miccio #ifdef CONFIG_DEBUG_BLK_CGROUP
368ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->merged);
369ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->service_time);
370ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->wait_time);
371ea25da48SPaolo Valente 	blkg_rwstat_exit(&stats->queued);
372ea25da48SPaolo Valente 	blkg_stat_exit(&stats->time);
373ea25da48SPaolo Valente 	blkg_stat_exit(&stats->avg_queue_size_sum);
374ea25da48SPaolo Valente 	blkg_stat_exit(&stats->avg_queue_size_samples);
375ea25da48SPaolo Valente 	blkg_stat_exit(&stats->dequeue);
376ea25da48SPaolo Valente 	blkg_stat_exit(&stats->group_wait_time);
377ea25da48SPaolo Valente 	blkg_stat_exit(&stats->idle_time);
378ea25da48SPaolo Valente 	blkg_stat_exit(&stats->empty_time);
379a33801e8SLuca Miccio #endif
380ea25da48SPaolo Valente }
381ea25da48SPaolo Valente 
382ea25da48SPaolo Valente static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp)
383ea25da48SPaolo Valente {
384a33801e8SLuca Miccio #ifdef CONFIG_DEBUG_BLK_CGROUP
385ea25da48SPaolo Valente 	if (blkg_rwstat_init(&stats->merged, gfp) ||
386ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->service_time, gfp) ||
387ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->wait_time, gfp) ||
388ea25da48SPaolo Valente 	    blkg_rwstat_init(&stats->queued, gfp) ||
389ea25da48SPaolo Valente 	    blkg_stat_init(&stats->time, gfp) ||
390ea25da48SPaolo Valente 	    blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
391ea25da48SPaolo Valente 	    blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
392ea25da48SPaolo Valente 	    blkg_stat_init(&stats->dequeue, gfp) ||
393ea25da48SPaolo Valente 	    blkg_stat_init(&stats->group_wait_time, gfp) ||
394ea25da48SPaolo Valente 	    blkg_stat_init(&stats->idle_time, gfp) ||
395ea25da48SPaolo Valente 	    blkg_stat_init(&stats->empty_time, gfp)) {
396ea25da48SPaolo Valente 		bfqg_stats_exit(stats);
397ea25da48SPaolo Valente 		return -ENOMEM;
398ea25da48SPaolo Valente 	}
399a33801e8SLuca Miccio #endif
400ea25da48SPaolo Valente 
401ea25da48SPaolo Valente 	return 0;
402ea25da48SPaolo Valente }
403ea25da48SPaolo Valente 
404ea25da48SPaolo Valente static struct bfq_group_data *cpd_to_bfqgd(struct blkcg_policy_data *cpd)
405ea25da48SPaolo Valente {
406ea25da48SPaolo Valente 	return cpd ? container_of(cpd, struct bfq_group_data, pd) : NULL;
407ea25da48SPaolo Valente }
408ea25da48SPaolo Valente 
409ea25da48SPaolo Valente static struct bfq_group_data *blkcg_to_bfqgd(struct blkcg *blkcg)
410ea25da48SPaolo Valente {
411ea25da48SPaolo Valente 	return cpd_to_bfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_bfq));
412ea25da48SPaolo Valente }
413ea25da48SPaolo Valente 
414dfb79af5SBart Van Assche static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp)
415ea25da48SPaolo Valente {
416ea25da48SPaolo Valente 	struct bfq_group_data *bgd;
417ea25da48SPaolo Valente 
418ea25da48SPaolo Valente 	bgd = kzalloc(sizeof(*bgd), gfp);
419ea25da48SPaolo Valente 	if (!bgd)
420ea25da48SPaolo Valente 		return NULL;
421ea25da48SPaolo Valente 	return &bgd->pd;
422ea25da48SPaolo Valente }
423ea25da48SPaolo Valente 
424dfb79af5SBart Van Assche static void bfq_cpd_init(struct blkcg_policy_data *cpd)
425ea25da48SPaolo Valente {
426ea25da48SPaolo Valente 	struct bfq_group_data *d = cpd_to_bfqgd(cpd);
427ea25da48SPaolo Valente 
428ea25da48SPaolo Valente 	d->weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
429ea25da48SPaolo Valente 		CGROUP_WEIGHT_DFL : BFQ_WEIGHT_LEGACY_DFL;
430ea25da48SPaolo Valente }
431ea25da48SPaolo Valente 
432dfb79af5SBart Van Assche static void bfq_cpd_free(struct blkcg_policy_data *cpd)
433ea25da48SPaolo Valente {
434ea25da48SPaolo Valente 	kfree(cpd_to_bfqgd(cpd));
435ea25da48SPaolo Valente }
436ea25da48SPaolo Valente 
437dfb79af5SBart Van Assche static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, int node)
438ea25da48SPaolo Valente {
439ea25da48SPaolo Valente 	struct bfq_group *bfqg;
440ea25da48SPaolo Valente 
441ea25da48SPaolo Valente 	bfqg = kzalloc_node(sizeof(*bfqg), gfp, node);
442ea25da48SPaolo Valente 	if (!bfqg)
443ea25da48SPaolo Valente 		return NULL;
444ea25da48SPaolo Valente 
445ea25da48SPaolo Valente 	if (bfqg_stats_init(&bfqg->stats, gfp)) {
446ea25da48SPaolo Valente 		kfree(bfqg);
447ea25da48SPaolo Valente 		return NULL;
448ea25da48SPaolo Valente 	}
449ea25da48SPaolo Valente 
4508f9bebc3SPaolo Valente 	/* see comments in bfq_bic_update_cgroup for why refcounting */
4518f9bebc3SPaolo Valente 	bfqg_get(bfqg);
452ea25da48SPaolo Valente 	return &bfqg->pd;
453ea25da48SPaolo Valente }
454ea25da48SPaolo Valente 
455dfb79af5SBart Van Assche static void bfq_pd_init(struct blkg_policy_data *pd)
456ea25da48SPaolo Valente {
457ea25da48SPaolo Valente 	struct blkcg_gq *blkg = pd_to_blkg(pd);
458ea25da48SPaolo Valente 	struct bfq_group *bfqg = blkg_to_bfqg(blkg);
459ea25da48SPaolo Valente 	struct bfq_data *bfqd = blkg->q->elevator->elevator_data;
460ea25da48SPaolo Valente 	struct bfq_entity *entity = &bfqg->entity;
461ea25da48SPaolo Valente 	struct bfq_group_data *d = blkcg_to_bfqgd(blkg->blkcg);
462ea25da48SPaolo Valente 
463ea25da48SPaolo Valente 	entity->orig_weight = entity->weight = entity->new_weight = d->weight;
464ea25da48SPaolo Valente 	entity->my_sched_data = &bfqg->sched_data;
465ea25da48SPaolo Valente 	bfqg->my_entity = entity; /*
466ea25da48SPaolo Valente 				   * the root_group's will be set to NULL
467ea25da48SPaolo Valente 				   * in bfq_init_queue()
468ea25da48SPaolo Valente 				   */
469ea25da48SPaolo Valente 	bfqg->bfqd = bfqd;
470ea25da48SPaolo Valente 	bfqg->active_entities = 0;
471ea25da48SPaolo Valente 	bfqg->rq_pos_tree = RB_ROOT;
472ea25da48SPaolo Valente }
473ea25da48SPaolo Valente 
474dfb79af5SBart Van Assche static void bfq_pd_free(struct blkg_policy_data *pd)
475ea25da48SPaolo Valente {
476ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
477ea25da48SPaolo Valente 
478ea25da48SPaolo Valente 	bfqg_stats_exit(&bfqg->stats);
4798f9bebc3SPaolo Valente 	bfqg_put(bfqg);
480ea25da48SPaolo Valente }
481ea25da48SPaolo Valente 
482dfb79af5SBart Van Assche static void bfq_pd_reset_stats(struct blkg_policy_data *pd)
483ea25da48SPaolo Valente {
484ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
485ea25da48SPaolo Valente 
486ea25da48SPaolo Valente 	bfqg_stats_reset(&bfqg->stats);
487ea25da48SPaolo Valente }
488ea25da48SPaolo Valente 
489ea25da48SPaolo Valente static void bfq_group_set_parent(struct bfq_group *bfqg,
490ea25da48SPaolo Valente 					struct bfq_group *parent)
491ea25da48SPaolo Valente {
492ea25da48SPaolo Valente 	struct bfq_entity *entity;
493ea25da48SPaolo Valente 
494ea25da48SPaolo Valente 	entity = &bfqg->entity;
495ea25da48SPaolo Valente 	entity->parent = parent->my_entity;
496ea25da48SPaolo Valente 	entity->sched_data = &parent->sched_data;
497ea25da48SPaolo Valente }
498ea25da48SPaolo Valente 
499ea25da48SPaolo Valente static struct bfq_group *bfq_lookup_bfqg(struct bfq_data *bfqd,
500ea25da48SPaolo Valente 					 struct blkcg *blkcg)
501ea25da48SPaolo Valente {
502ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
503ea25da48SPaolo Valente 
504ea25da48SPaolo Valente 	blkg = blkg_lookup(blkcg, bfqd->queue);
505ea25da48SPaolo Valente 	if (likely(blkg))
506ea25da48SPaolo Valente 		return blkg_to_bfqg(blkg);
507ea25da48SPaolo Valente 	return NULL;
508ea25da48SPaolo Valente }
509ea25da48SPaolo Valente 
510ea25da48SPaolo Valente struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd,
511ea25da48SPaolo Valente 				     struct blkcg *blkcg)
512ea25da48SPaolo Valente {
513ea25da48SPaolo Valente 	struct bfq_group *bfqg, *parent;
514ea25da48SPaolo Valente 	struct bfq_entity *entity;
515ea25da48SPaolo Valente 
516ea25da48SPaolo Valente 	bfqg = bfq_lookup_bfqg(bfqd, blkcg);
517ea25da48SPaolo Valente 
518ea25da48SPaolo Valente 	if (unlikely(!bfqg))
519ea25da48SPaolo Valente 		return NULL;
520ea25da48SPaolo Valente 
521ea25da48SPaolo Valente 	/*
522ea25da48SPaolo Valente 	 * Update chain of bfq_groups as we might be handling a leaf group
523ea25da48SPaolo Valente 	 * which, along with some of its relatives, has not been hooked yet
524ea25da48SPaolo Valente 	 * to the private hierarchy of BFQ.
525ea25da48SPaolo Valente 	 */
526ea25da48SPaolo Valente 	entity = &bfqg->entity;
527ea25da48SPaolo Valente 	for_each_entity(entity) {
528ea25da48SPaolo Valente 		bfqg = container_of(entity, struct bfq_group, entity);
529ea25da48SPaolo Valente 		if (bfqg != bfqd->root_group) {
530ea25da48SPaolo Valente 			parent = bfqg_parent(bfqg);
531ea25da48SPaolo Valente 			if (!parent)
532ea25da48SPaolo Valente 				parent = bfqd->root_group;
533ea25da48SPaolo Valente 			bfq_group_set_parent(bfqg, parent);
534ea25da48SPaolo Valente 		}
535ea25da48SPaolo Valente 	}
536ea25da48SPaolo Valente 
537ea25da48SPaolo Valente 	return bfqg;
538ea25da48SPaolo Valente }
539ea25da48SPaolo Valente 
540ea25da48SPaolo Valente /**
541ea25da48SPaolo Valente  * bfq_bfqq_move - migrate @bfqq to @bfqg.
542ea25da48SPaolo Valente  * @bfqd: queue descriptor.
543ea25da48SPaolo Valente  * @bfqq: the queue to move.
544ea25da48SPaolo Valente  * @bfqg: the group to move to.
545ea25da48SPaolo Valente  *
546ea25da48SPaolo Valente  * Move @bfqq to @bfqg, deactivating it from its old group and reactivating
547ea25da48SPaolo Valente  * it on the new one.  Avoid putting the entity on the old group idle tree.
548ea25da48SPaolo Valente  *
5498f9bebc3SPaolo Valente  * Must be called under the scheduler lock, to make sure that the blkg
5508f9bebc3SPaolo Valente  * owning @bfqg does not disappear (see comments in
5518f9bebc3SPaolo Valente  * bfq_bic_update_cgroup on guaranteeing the consistency of blkg
5528f9bebc3SPaolo Valente  * objects).
553ea25da48SPaolo Valente  */
554ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
555ea25da48SPaolo Valente 		   struct bfq_group *bfqg)
556ea25da48SPaolo Valente {
557ea25da48SPaolo Valente 	struct bfq_entity *entity = &bfqq->entity;
558ea25da48SPaolo Valente 
559ea25da48SPaolo Valente 	/* If bfqq is empty, then bfq_bfqq_expire also invokes
560ea25da48SPaolo Valente 	 * bfq_del_bfqq_busy, thereby removing bfqq and its entity
561ea25da48SPaolo Valente 	 * from data structures related to current group. Otherwise we
562ea25da48SPaolo Valente 	 * need to remove bfqq explicitly with bfq_deactivate_bfqq, as
563ea25da48SPaolo Valente 	 * we do below.
564ea25da48SPaolo Valente 	 */
565ea25da48SPaolo Valente 	if (bfqq == bfqd->in_service_queue)
566ea25da48SPaolo Valente 		bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
567ea25da48SPaolo Valente 				false, BFQQE_PREEMPTED);
568ea25da48SPaolo Valente 
569ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq))
570ea25da48SPaolo Valente 		bfq_deactivate_bfqq(bfqd, bfqq, false, false);
571ea25da48SPaolo Valente 	else if (entity->on_st)
572ea25da48SPaolo Valente 		bfq_put_idle_entity(bfq_entity_service_tree(entity), entity);
5738f9bebc3SPaolo Valente 	bfqg_and_blkg_put(bfqq_group(bfqq));
574ea25da48SPaolo Valente 
575ea25da48SPaolo Valente 	entity->parent = bfqg->my_entity;
576ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
5778f9bebc3SPaolo Valente 	/* pin down bfqg and its associated blkg  */
5788f9bebc3SPaolo Valente 	bfqg_and_blkg_get(bfqg);
579ea25da48SPaolo Valente 
580ea25da48SPaolo Valente 	if (bfq_bfqq_busy(bfqq)) {
581ea25da48SPaolo Valente 		bfq_pos_tree_add_move(bfqd, bfqq);
582ea25da48SPaolo Valente 		bfq_activate_bfqq(bfqd, bfqq);
583ea25da48SPaolo Valente 	}
584ea25da48SPaolo Valente 
585ea25da48SPaolo Valente 	if (!bfqd->in_service_queue && !bfqd->rq_in_driver)
586ea25da48SPaolo Valente 		bfq_schedule_dispatch(bfqd);
587ea25da48SPaolo Valente }
588ea25da48SPaolo Valente 
589ea25da48SPaolo Valente /**
590ea25da48SPaolo Valente  * __bfq_bic_change_cgroup - move @bic to @cgroup.
591ea25da48SPaolo Valente  * @bfqd: the queue descriptor.
592ea25da48SPaolo Valente  * @bic: the bic to move.
593ea25da48SPaolo Valente  * @blkcg: the blk-cgroup to move to.
594ea25da48SPaolo Valente  *
5958f9bebc3SPaolo Valente  * Move bic to blkcg, assuming that bfqd->lock is held; which makes
5968f9bebc3SPaolo Valente  * sure that the reference to cgroup is valid across the call (see
5978f9bebc3SPaolo Valente  * comments in bfq_bic_update_cgroup on this issue)
598ea25da48SPaolo Valente  *
599ea25da48SPaolo Valente  * NOTE: an alternative approach might have been to store the current
600ea25da48SPaolo Valente  * cgroup in bfqq and getting a reference to it, reducing the lookup
601ea25da48SPaolo Valente  * time here, at the price of slightly more complex code.
602ea25da48SPaolo Valente  */
603ea25da48SPaolo Valente static struct bfq_group *__bfq_bic_change_cgroup(struct bfq_data *bfqd,
604ea25da48SPaolo Valente 						struct bfq_io_cq *bic,
605ea25da48SPaolo Valente 						struct blkcg *blkcg)
606ea25da48SPaolo Valente {
607ea25da48SPaolo Valente 	struct bfq_queue *async_bfqq = bic_to_bfqq(bic, 0);
608ea25da48SPaolo Valente 	struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, 1);
609ea25da48SPaolo Valente 	struct bfq_group *bfqg;
610ea25da48SPaolo Valente 	struct bfq_entity *entity;
611ea25da48SPaolo Valente 
612ea25da48SPaolo Valente 	bfqg = bfq_find_set_group(bfqd, blkcg);
613ea25da48SPaolo Valente 
614ea25da48SPaolo Valente 	if (unlikely(!bfqg))
615ea25da48SPaolo Valente 		bfqg = bfqd->root_group;
616ea25da48SPaolo Valente 
617ea25da48SPaolo Valente 	if (async_bfqq) {
618ea25da48SPaolo Valente 		entity = &async_bfqq->entity;
619ea25da48SPaolo Valente 
620ea25da48SPaolo Valente 		if (entity->sched_data != &bfqg->sched_data) {
621ea25da48SPaolo Valente 			bic_set_bfqq(bic, NULL, 0);
622ea25da48SPaolo Valente 			bfq_log_bfqq(bfqd, async_bfqq,
623ea25da48SPaolo Valente 				     "bic_change_group: %p %d",
624ea25da48SPaolo Valente 				     async_bfqq, async_bfqq->ref);
625ea25da48SPaolo Valente 			bfq_put_queue(async_bfqq);
626ea25da48SPaolo Valente 		}
627ea25da48SPaolo Valente 	}
628ea25da48SPaolo Valente 
629ea25da48SPaolo Valente 	if (sync_bfqq) {
630ea25da48SPaolo Valente 		entity = &sync_bfqq->entity;
631ea25da48SPaolo Valente 		if (entity->sched_data != &bfqg->sched_data)
632ea25da48SPaolo Valente 			bfq_bfqq_move(bfqd, sync_bfqq, bfqg);
633ea25da48SPaolo Valente 	}
634ea25da48SPaolo Valente 
635ea25da48SPaolo Valente 	return bfqg;
636ea25da48SPaolo Valente }
637ea25da48SPaolo Valente 
638ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio)
639ea25da48SPaolo Valente {
640ea25da48SPaolo Valente 	struct bfq_data *bfqd = bic_to_bfqd(bic);
641ea25da48SPaolo Valente 	struct bfq_group *bfqg = NULL;
642ea25da48SPaolo Valente 	uint64_t serial_nr;
643ea25da48SPaolo Valente 
644ea25da48SPaolo Valente 	rcu_read_lock();
6450fe061b9SDennis Zhou 	serial_nr = __bio_blkcg(bio)->css.serial_nr;
646ea25da48SPaolo Valente 
647ea25da48SPaolo Valente 	/*
648ea25da48SPaolo Valente 	 * Check whether blkcg has changed.  The condition may trigger
649ea25da48SPaolo Valente 	 * spuriously on a newly created cic but there's no harm.
650ea25da48SPaolo Valente 	 */
651ea25da48SPaolo Valente 	if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr))
652ea25da48SPaolo Valente 		goto out;
653ea25da48SPaolo Valente 
6540fe061b9SDennis Zhou 	bfqg = __bfq_bic_change_cgroup(bfqd, bic, __bio_blkcg(bio));
6558f9bebc3SPaolo Valente 	/*
6568f9bebc3SPaolo Valente 	 * Update blkg_path for bfq_log_* functions. We cache this
6578f9bebc3SPaolo Valente 	 * path, and update it here, for the following
6588f9bebc3SPaolo Valente 	 * reasons. Operations on blkg objects in blk-cgroup are
6598f9bebc3SPaolo Valente 	 * protected with the request_queue lock, and not with the
6608f9bebc3SPaolo Valente 	 * lock that protects the instances of this scheduler
6618f9bebc3SPaolo Valente 	 * (bfqd->lock). This exposes BFQ to the following sort of
6628f9bebc3SPaolo Valente 	 * race.
6638f9bebc3SPaolo Valente 	 *
6648f9bebc3SPaolo Valente 	 * The blkg_lookup performed in bfq_get_queue, protected
6658f9bebc3SPaolo Valente 	 * through rcu, may happen to return the address of a copy of
6668f9bebc3SPaolo Valente 	 * the original blkg. If this is the case, then the
6678f9bebc3SPaolo Valente 	 * bfqg_and_blkg_get performed in bfq_get_queue, to pin down
6688f9bebc3SPaolo Valente 	 * the blkg, is useless: it does not prevent blk-cgroup code
6698f9bebc3SPaolo Valente 	 * from destroying both the original blkg and all objects
6708f9bebc3SPaolo Valente 	 * directly or indirectly referred by the copy of the
6718f9bebc3SPaolo Valente 	 * blkg.
6728f9bebc3SPaolo Valente 	 *
6738f9bebc3SPaolo Valente 	 * On the bright side, destroy operations on a blkg invoke, as
6748f9bebc3SPaolo Valente 	 * a first step, hooks of the scheduler associated with the
6758f9bebc3SPaolo Valente 	 * blkg. And these hooks are executed with bfqd->lock held for
6768f9bebc3SPaolo Valente 	 * BFQ. As a consequence, for any blkg associated with the
6778f9bebc3SPaolo Valente 	 * request queue this instance of the scheduler is attached
6788f9bebc3SPaolo Valente 	 * to, we are guaranteed that such a blkg is not destroyed, and
6798f9bebc3SPaolo Valente 	 * that all the pointers it contains are consistent, while we
6808f9bebc3SPaolo Valente 	 * are holding bfqd->lock. A blkg_lookup performed with
6818f9bebc3SPaolo Valente 	 * bfqd->lock held then returns a fully consistent blkg, which
6828f9bebc3SPaolo Valente 	 * remains consistent until this lock is held.
6838f9bebc3SPaolo Valente 	 *
6848f9bebc3SPaolo Valente 	 * Thanks to the last fact, and to the fact that: (1) bfqg has
6858f9bebc3SPaolo Valente 	 * been obtained through a blkg_lookup in the above
6868f9bebc3SPaolo Valente 	 * assignment, and (2) bfqd->lock is being held, here we can
6878f9bebc3SPaolo Valente 	 * safely use the policy data for the involved blkg (i.e., the
6888f9bebc3SPaolo Valente 	 * field bfqg->pd) to get to the blkg associated with bfqg,
6898f9bebc3SPaolo Valente 	 * and then we can safely use any field of blkg. After we
6908f9bebc3SPaolo Valente 	 * release bfqd->lock, even just getting blkg through this
6918f9bebc3SPaolo Valente 	 * bfqg may cause dangling references to be traversed, as
6928f9bebc3SPaolo Valente 	 * bfqg->pd may not exist any more.
6938f9bebc3SPaolo Valente 	 *
6948f9bebc3SPaolo Valente 	 * In view of the above facts, here we cache, in the bfqg, any
6958f9bebc3SPaolo Valente 	 * blkg data we may need for this bic, and for its associated
6968f9bebc3SPaolo Valente 	 * bfq_queue. As of now, we need to cache only the path of the
6978f9bebc3SPaolo Valente 	 * blkg, which is used in the bfq_log_* functions.
6988f9bebc3SPaolo Valente 	 *
6998f9bebc3SPaolo Valente 	 * Finally, note that bfqg itself needs to be protected from
7008f9bebc3SPaolo Valente 	 * destruction on the blkg_free of the original blkg (which
7018f9bebc3SPaolo Valente 	 * invokes bfq_pd_free). We use an additional private
7028f9bebc3SPaolo Valente 	 * refcounter for bfqg, to let it disappear only after no
7038f9bebc3SPaolo Valente 	 * bfq_queue refers to it any longer.
7048f9bebc3SPaolo Valente 	 */
7058f9bebc3SPaolo Valente 	blkg_path(bfqg_to_blkg(bfqg), bfqg->blkg_path, sizeof(bfqg->blkg_path));
706ea25da48SPaolo Valente 	bic->blkcg_serial_nr = serial_nr;
707ea25da48SPaolo Valente out:
708ea25da48SPaolo Valente 	rcu_read_unlock();
709ea25da48SPaolo Valente }
710ea25da48SPaolo Valente 
711ea25da48SPaolo Valente /**
712ea25da48SPaolo Valente  * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
713ea25da48SPaolo Valente  * @st: the service tree being flushed.
714ea25da48SPaolo Valente  */
715ea25da48SPaolo Valente static void bfq_flush_idle_tree(struct bfq_service_tree *st)
716ea25da48SPaolo Valente {
717ea25da48SPaolo Valente 	struct bfq_entity *entity = st->first_idle;
718ea25da48SPaolo Valente 
719ea25da48SPaolo Valente 	for (; entity ; entity = st->first_idle)
720ea25da48SPaolo Valente 		__bfq_deactivate_entity(entity, false);
721ea25da48SPaolo Valente }
722ea25da48SPaolo Valente 
723ea25da48SPaolo Valente /**
724ea25da48SPaolo Valente  * bfq_reparent_leaf_entity - move leaf entity to the root_group.
725ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
726ea25da48SPaolo Valente  * @entity: the entity to move.
727ea25da48SPaolo Valente  */
728ea25da48SPaolo Valente static void bfq_reparent_leaf_entity(struct bfq_data *bfqd,
729ea25da48SPaolo Valente 				     struct bfq_entity *entity)
730ea25da48SPaolo Valente {
731ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
732ea25da48SPaolo Valente 
733ea25da48SPaolo Valente 	bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
734ea25da48SPaolo Valente }
735ea25da48SPaolo Valente 
736ea25da48SPaolo Valente /**
737ea25da48SPaolo Valente  * bfq_reparent_active_entities - move to the root group all active
738ea25da48SPaolo Valente  *                                entities.
739ea25da48SPaolo Valente  * @bfqd: the device data structure with the root group.
740ea25da48SPaolo Valente  * @bfqg: the group to move from.
741ea25da48SPaolo Valente  * @st: the service tree with the entities.
742ea25da48SPaolo Valente  */
743ea25da48SPaolo Valente static void bfq_reparent_active_entities(struct bfq_data *bfqd,
744ea25da48SPaolo Valente 					 struct bfq_group *bfqg,
745ea25da48SPaolo Valente 					 struct bfq_service_tree *st)
746ea25da48SPaolo Valente {
747ea25da48SPaolo Valente 	struct rb_root *active = &st->active;
748ea25da48SPaolo Valente 	struct bfq_entity *entity = NULL;
749ea25da48SPaolo Valente 
750ea25da48SPaolo Valente 	if (!RB_EMPTY_ROOT(&st->active))
751ea25da48SPaolo Valente 		entity = bfq_entity_of(rb_first(active));
752ea25da48SPaolo Valente 
753ea25da48SPaolo Valente 	for (; entity ; entity = bfq_entity_of(rb_first(active)))
754ea25da48SPaolo Valente 		bfq_reparent_leaf_entity(bfqd, entity);
755ea25da48SPaolo Valente 
756ea25da48SPaolo Valente 	if (bfqg->sched_data.in_service_entity)
757ea25da48SPaolo Valente 		bfq_reparent_leaf_entity(bfqd,
758ea25da48SPaolo Valente 			bfqg->sched_data.in_service_entity);
759ea25da48SPaolo Valente }
760ea25da48SPaolo Valente 
761ea25da48SPaolo Valente /**
762ea25da48SPaolo Valente  * bfq_pd_offline - deactivate the entity associated with @pd,
763ea25da48SPaolo Valente  *		    and reparent its children entities.
764ea25da48SPaolo Valente  * @pd: descriptor of the policy going offline.
765ea25da48SPaolo Valente  *
766ea25da48SPaolo Valente  * blkio already grabs the queue_lock for us, so no need to use
767ea25da48SPaolo Valente  * RCU-based magic
768ea25da48SPaolo Valente  */
769dfb79af5SBart Van Assche static void bfq_pd_offline(struct blkg_policy_data *pd)
770ea25da48SPaolo Valente {
771ea25da48SPaolo Valente 	struct bfq_service_tree *st;
772ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
773ea25da48SPaolo Valente 	struct bfq_data *bfqd = bfqg->bfqd;
774ea25da48SPaolo Valente 	struct bfq_entity *entity = bfqg->my_entity;
775ea25da48SPaolo Valente 	unsigned long flags;
776ea25da48SPaolo Valente 	int i;
777ea25da48SPaolo Valente 
778ea25da48SPaolo Valente 	spin_lock_irqsave(&bfqd->lock, flags);
77952257ffbSPaolo Valente 
78052257ffbSPaolo Valente 	if (!entity) /* root group */
78152257ffbSPaolo Valente 		goto put_async_queues;
78252257ffbSPaolo Valente 
783ea25da48SPaolo Valente 	/*
784ea25da48SPaolo Valente 	 * Empty all service_trees belonging to this group before
785ea25da48SPaolo Valente 	 * deactivating the group itself.
786ea25da48SPaolo Valente 	 */
787ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
788ea25da48SPaolo Valente 		st = bfqg->sched_data.service_tree + i;
789ea25da48SPaolo Valente 
790ea25da48SPaolo Valente 		/*
791ea25da48SPaolo Valente 		 * The idle tree may still contain bfq_queues belonging
792ea25da48SPaolo Valente 		 * to exited task because they never migrated to a different
7938f9bebc3SPaolo Valente 		 * cgroup from the one being destroyed now.
794ea25da48SPaolo Valente 		 */
795ea25da48SPaolo Valente 		bfq_flush_idle_tree(st);
796ea25da48SPaolo Valente 
797ea25da48SPaolo Valente 		/*
798ea25da48SPaolo Valente 		 * It may happen that some queues are still active
799ea25da48SPaolo Valente 		 * (busy) upon group destruction (if the corresponding
800ea25da48SPaolo Valente 		 * processes have been forced to terminate). We move
801ea25da48SPaolo Valente 		 * all the leaf entities corresponding to these queues
802ea25da48SPaolo Valente 		 * to the root_group.
803ea25da48SPaolo Valente 		 * Also, it may happen that the group has an entity
804ea25da48SPaolo Valente 		 * in service, which is disconnected from the active
805ea25da48SPaolo Valente 		 * tree: it must be moved, too.
806ea25da48SPaolo Valente 		 * There is no need to put the sync queues, as the
807ea25da48SPaolo Valente 		 * scheduler has taken no reference.
808ea25da48SPaolo Valente 		 */
809ea25da48SPaolo Valente 		bfq_reparent_active_entities(bfqd, bfqg, st);
810ea25da48SPaolo Valente 	}
811ea25da48SPaolo Valente 
812ea25da48SPaolo Valente 	__bfq_deactivate_entity(entity, false);
81352257ffbSPaolo Valente 
81452257ffbSPaolo Valente put_async_queues:
815ea25da48SPaolo Valente 	bfq_put_async_queues(bfqd, bfqg);
816ea25da48SPaolo Valente 
817ea25da48SPaolo Valente 	spin_unlock_irqrestore(&bfqd->lock, flags);
818ea25da48SPaolo Valente 	/*
819ea25da48SPaolo Valente 	 * @blkg is going offline and will be ignored by
820ea25da48SPaolo Valente 	 * blkg_[rw]stat_recursive_sum().  Transfer stats to the parent so
821ea25da48SPaolo Valente 	 * that they don't get lost.  If IOs complete after this point, the
822ea25da48SPaolo Valente 	 * stats for them will be lost.  Oh well...
823ea25da48SPaolo Valente 	 */
824ea25da48SPaolo Valente 	bfqg_stats_xfer_dead(bfqg);
825ea25da48SPaolo Valente }
826ea25da48SPaolo Valente 
827ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
828ea25da48SPaolo Valente {
829ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
830ea25da48SPaolo Valente 
831ea25da48SPaolo Valente 	list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) {
832ea25da48SPaolo Valente 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
833ea25da48SPaolo Valente 
834ea25da48SPaolo Valente 		bfq_end_wr_async_queues(bfqd, bfqg);
835ea25da48SPaolo Valente 	}
836ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
837ea25da48SPaolo Valente }
838ea25da48SPaolo Valente 
839ea25da48SPaolo Valente static int bfq_io_show_weight(struct seq_file *sf, void *v)
840ea25da48SPaolo Valente {
841ea25da48SPaolo Valente 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
842ea25da48SPaolo Valente 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
843ea25da48SPaolo Valente 	unsigned int val = 0;
844ea25da48SPaolo Valente 
845ea25da48SPaolo Valente 	if (bfqgd)
846ea25da48SPaolo Valente 		val = bfqgd->weight;
847ea25da48SPaolo Valente 
848ea25da48SPaolo Valente 	seq_printf(sf, "%u\n", val);
849ea25da48SPaolo Valente 
850ea25da48SPaolo Valente 	return 0;
851ea25da48SPaolo Valente }
852ea25da48SPaolo Valente 
853ea25da48SPaolo Valente static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css,
854ea25da48SPaolo Valente 				    struct cftype *cftype,
855ea25da48SPaolo Valente 				    u64 val)
856ea25da48SPaolo Valente {
857ea25da48SPaolo Valente 	struct blkcg *blkcg = css_to_blkcg(css);
858ea25da48SPaolo Valente 	struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
859ea25da48SPaolo Valente 	struct blkcg_gq *blkg;
860ea25da48SPaolo Valente 	int ret = -ERANGE;
861ea25da48SPaolo Valente 
862ea25da48SPaolo Valente 	if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT)
863ea25da48SPaolo Valente 		return ret;
864ea25da48SPaolo Valente 
865ea25da48SPaolo Valente 	ret = 0;
866ea25da48SPaolo Valente 	spin_lock_irq(&blkcg->lock);
867ea25da48SPaolo Valente 	bfqgd->weight = (unsigned short)val;
868ea25da48SPaolo Valente 	hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
869ea25da48SPaolo Valente 		struct bfq_group *bfqg = blkg_to_bfqg(blkg);
870ea25da48SPaolo Valente 
871ea25da48SPaolo Valente 		if (!bfqg)
872ea25da48SPaolo Valente 			continue;
873ea25da48SPaolo Valente 		/*
874ea25da48SPaolo Valente 		 * Setting the prio_changed flag of the entity
875ea25da48SPaolo Valente 		 * to 1 with new_weight == weight would re-set
876ea25da48SPaolo Valente 		 * the value of the weight to its ioprio mapping.
877ea25da48SPaolo Valente 		 * Set the flag only if necessary.
878ea25da48SPaolo Valente 		 */
879ea25da48SPaolo Valente 		if ((unsigned short)val != bfqg->entity.new_weight) {
880ea25da48SPaolo Valente 			bfqg->entity.new_weight = (unsigned short)val;
881ea25da48SPaolo Valente 			/*
882ea25da48SPaolo Valente 			 * Make sure that the above new value has been
883ea25da48SPaolo Valente 			 * stored in bfqg->entity.new_weight before
884ea25da48SPaolo Valente 			 * setting the prio_changed flag. In fact,
885ea25da48SPaolo Valente 			 * this flag may be read asynchronously (in
886ea25da48SPaolo Valente 			 * critical sections protected by a different
887ea25da48SPaolo Valente 			 * lock than that held here), and finding this
888ea25da48SPaolo Valente 			 * flag set may cause the execution of the code
889ea25da48SPaolo Valente 			 * for updating parameters whose value may
890ea25da48SPaolo Valente 			 * depend also on bfqg->entity.new_weight (in
891ea25da48SPaolo Valente 			 * __bfq_entity_update_weight_prio).
892ea25da48SPaolo Valente 			 * This barrier makes sure that the new value
893ea25da48SPaolo Valente 			 * of bfqg->entity.new_weight is correctly
894ea25da48SPaolo Valente 			 * seen in that code.
895ea25da48SPaolo Valente 			 */
896ea25da48SPaolo Valente 			smp_wmb();
897ea25da48SPaolo Valente 			bfqg->entity.prio_changed = 1;
898ea25da48SPaolo Valente 		}
899ea25da48SPaolo Valente 	}
900ea25da48SPaolo Valente 	spin_unlock_irq(&blkcg->lock);
901ea25da48SPaolo Valente 
902ea25da48SPaolo Valente 	return ret;
903ea25da48SPaolo Valente }
904ea25da48SPaolo Valente 
905ea25da48SPaolo Valente static ssize_t bfq_io_set_weight(struct kernfs_open_file *of,
906ea25da48SPaolo Valente 				 char *buf, size_t nbytes,
907ea25da48SPaolo Valente 				 loff_t off)
908ea25da48SPaolo Valente {
909ea25da48SPaolo Valente 	u64 weight;
910ea25da48SPaolo Valente 	/* First unsigned long found in the file is used */
911ea25da48SPaolo Valente 	int ret = kstrtoull(strim(buf), 0, &weight);
912ea25da48SPaolo Valente 
913ea25da48SPaolo Valente 	if (ret)
914ea25da48SPaolo Valente 		return ret;
915ea25da48SPaolo Valente 
916fc8ebd01SMaciej S. Szmigiero 	ret = bfq_io_set_weight_legacy(of_css(of), NULL, weight);
917fc8ebd01SMaciej S. Szmigiero 	return ret ?: nbytes;
918ea25da48SPaolo Valente }
919ea25da48SPaolo Valente 
920a33801e8SLuca Miccio #ifdef CONFIG_DEBUG_BLK_CGROUP
921ea25da48SPaolo Valente static int bfqg_print_stat(struct seq_file *sf, void *v)
922ea25da48SPaolo Valente {
923ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
924ea25da48SPaolo Valente 			  &blkcg_policy_bfq, seq_cft(sf)->private, false);
925ea25da48SPaolo Valente 	return 0;
926ea25da48SPaolo Valente }
927ea25da48SPaolo Valente 
928ea25da48SPaolo Valente static int bfqg_print_rwstat(struct seq_file *sf, void *v)
929ea25da48SPaolo Valente {
930ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
931ea25da48SPaolo Valente 			  &blkcg_policy_bfq, seq_cft(sf)->private, true);
932ea25da48SPaolo Valente 	return 0;
933ea25da48SPaolo Valente }
934ea25da48SPaolo Valente 
935ea25da48SPaolo Valente static u64 bfqg_prfill_stat_recursive(struct seq_file *sf,
936ea25da48SPaolo Valente 				      struct blkg_policy_data *pd, int off)
937ea25da48SPaolo Valente {
938ea25da48SPaolo Valente 	u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
939ea25da48SPaolo Valente 					  &blkcg_policy_bfq, off);
940ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum);
941ea25da48SPaolo Valente }
942ea25da48SPaolo Valente 
943ea25da48SPaolo Valente static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf,
944ea25da48SPaolo Valente 					struct blkg_policy_data *pd, int off)
945ea25da48SPaolo Valente {
946ea25da48SPaolo Valente 	struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
947ea25da48SPaolo Valente 							   &blkcg_policy_bfq,
948ea25da48SPaolo Valente 							   off);
949ea25da48SPaolo Valente 	return __blkg_prfill_rwstat(sf, pd, &sum);
950ea25da48SPaolo Valente }
951ea25da48SPaolo Valente 
952ea25da48SPaolo Valente static int bfqg_print_stat_recursive(struct seq_file *sf, void *v)
953ea25da48SPaolo Valente {
954ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
955ea25da48SPaolo Valente 			  bfqg_prfill_stat_recursive, &blkcg_policy_bfq,
956ea25da48SPaolo Valente 			  seq_cft(sf)->private, false);
957ea25da48SPaolo Valente 	return 0;
958ea25da48SPaolo Valente }
959ea25da48SPaolo Valente 
960ea25da48SPaolo Valente static int bfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
961ea25da48SPaolo Valente {
962ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
963ea25da48SPaolo Valente 			  bfqg_prfill_rwstat_recursive, &blkcg_policy_bfq,
964ea25da48SPaolo Valente 			  seq_cft(sf)->private, true);
965ea25da48SPaolo Valente 	return 0;
966ea25da48SPaolo Valente }
967ea25da48SPaolo Valente 
968ea25da48SPaolo Valente static u64 bfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
969ea25da48SPaolo Valente 			       int off)
970ea25da48SPaolo Valente {
971ea25da48SPaolo Valente 	u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
972ea25da48SPaolo Valente 
973ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum >> 9);
974ea25da48SPaolo Valente }
975ea25da48SPaolo Valente 
976ea25da48SPaolo Valente static int bfqg_print_stat_sectors(struct seq_file *sf, void *v)
977ea25da48SPaolo Valente {
978ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
979ea25da48SPaolo Valente 			  bfqg_prfill_sectors, &blkcg_policy_bfq, 0, false);
980ea25da48SPaolo Valente 	return 0;
981ea25da48SPaolo Valente }
982ea25da48SPaolo Valente 
983ea25da48SPaolo Valente static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf,
984ea25da48SPaolo Valente 					 struct blkg_policy_data *pd, int off)
985ea25da48SPaolo Valente {
986ea25da48SPaolo Valente 	struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
987ea25da48SPaolo Valente 					offsetof(struct blkcg_gq, stat_bytes));
988ea25da48SPaolo Valente 	u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
989ea25da48SPaolo Valente 		atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
990ea25da48SPaolo Valente 
991ea25da48SPaolo Valente 	return __blkg_prfill_u64(sf, pd, sum >> 9);
992ea25da48SPaolo Valente }
993ea25da48SPaolo Valente 
994ea25da48SPaolo Valente static int bfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
995ea25da48SPaolo Valente {
996ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
997ea25da48SPaolo Valente 			  bfqg_prfill_sectors_recursive, &blkcg_policy_bfq, 0,
998ea25da48SPaolo Valente 			  false);
999ea25da48SPaolo Valente 	return 0;
1000ea25da48SPaolo Valente }
1001ea25da48SPaolo Valente 
1002ea25da48SPaolo Valente static u64 bfqg_prfill_avg_queue_size(struct seq_file *sf,
1003ea25da48SPaolo Valente 				      struct blkg_policy_data *pd, int off)
1004ea25da48SPaolo Valente {
1005ea25da48SPaolo Valente 	struct bfq_group *bfqg = pd_to_bfqg(pd);
1006ea25da48SPaolo Valente 	u64 samples = blkg_stat_read(&bfqg->stats.avg_queue_size_samples);
1007ea25da48SPaolo Valente 	u64 v = 0;
1008ea25da48SPaolo Valente 
1009ea25da48SPaolo Valente 	if (samples) {
1010ea25da48SPaolo Valente 		v = blkg_stat_read(&bfqg->stats.avg_queue_size_sum);
1011ea25da48SPaolo Valente 		v = div64_u64(v, samples);
1012ea25da48SPaolo Valente 	}
1013ea25da48SPaolo Valente 	__blkg_prfill_u64(sf, pd, v);
1014ea25da48SPaolo Valente 	return 0;
1015ea25da48SPaolo Valente }
1016ea25da48SPaolo Valente 
1017ea25da48SPaolo Valente /* print avg_queue_size */
1018ea25da48SPaolo Valente static int bfqg_print_avg_queue_size(struct seq_file *sf, void *v)
1019ea25da48SPaolo Valente {
1020ea25da48SPaolo Valente 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1021ea25da48SPaolo Valente 			  bfqg_prfill_avg_queue_size, &blkcg_policy_bfq,
1022ea25da48SPaolo Valente 			  0, false);
1023ea25da48SPaolo Valente 	return 0;
1024ea25da48SPaolo Valente }
1025a33801e8SLuca Miccio #endif /* CONFIG_DEBUG_BLK_CGROUP */
1026ea25da48SPaolo Valente 
1027ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1028ea25da48SPaolo Valente {
1029ea25da48SPaolo Valente 	int ret;
1030ea25da48SPaolo Valente 
1031ea25da48SPaolo Valente 	ret = blkcg_activate_policy(bfqd->queue, &blkcg_policy_bfq);
1032ea25da48SPaolo Valente 	if (ret)
1033ea25da48SPaolo Valente 		return NULL;
1034ea25da48SPaolo Valente 
1035ea25da48SPaolo Valente 	return blkg_to_bfqg(bfqd->queue->root_blkg);
1036ea25da48SPaolo Valente }
1037ea25da48SPaolo Valente 
1038ea25da48SPaolo Valente struct blkcg_policy blkcg_policy_bfq = {
1039ea25da48SPaolo Valente 	.dfl_cftypes		= bfq_blkg_files,
1040ea25da48SPaolo Valente 	.legacy_cftypes		= bfq_blkcg_legacy_files,
1041ea25da48SPaolo Valente 
1042ea25da48SPaolo Valente 	.cpd_alloc_fn		= bfq_cpd_alloc,
1043ea25da48SPaolo Valente 	.cpd_init_fn		= bfq_cpd_init,
1044ea25da48SPaolo Valente 	.cpd_bind_fn	        = bfq_cpd_init,
1045ea25da48SPaolo Valente 	.cpd_free_fn		= bfq_cpd_free,
1046ea25da48SPaolo Valente 
1047ea25da48SPaolo Valente 	.pd_alloc_fn		= bfq_pd_alloc,
1048ea25da48SPaolo Valente 	.pd_init_fn		= bfq_pd_init,
1049ea25da48SPaolo Valente 	.pd_offline_fn		= bfq_pd_offline,
1050ea25da48SPaolo Valente 	.pd_free_fn		= bfq_pd_free,
1051ea25da48SPaolo Valente 	.pd_reset_stats_fn	= bfq_pd_reset_stats,
1052ea25da48SPaolo Valente };
1053ea25da48SPaolo Valente 
1054ea25da48SPaolo Valente struct cftype bfq_blkcg_legacy_files[] = {
1055ea25da48SPaolo Valente 	{
1056ea25da48SPaolo Valente 		.name = "bfq.weight",
1057ea25da48SPaolo Valente 		.flags = CFTYPE_NOT_ON_ROOT,
1058ea25da48SPaolo Valente 		.seq_show = bfq_io_show_weight,
1059ea25da48SPaolo Valente 		.write_u64 = bfq_io_set_weight_legacy,
1060ea25da48SPaolo Valente 	},
1061ea25da48SPaolo Valente 
1062ea25da48SPaolo Valente 	/* statistics, covers only the tasks in the bfqg */
1063ea25da48SPaolo Valente 	{
1064ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes",
1065ea25da48SPaolo Valente 		.private = (unsigned long)&blkcg_policy_bfq,
1066ea25da48SPaolo Valente 		.seq_show = blkg_print_stat_bytes,
1067ea25da48SPaolo Valente 	},
1068ea25da48SPaolo Valente 	{
1069ea25da48SPaolo Valente 		.name = "bfq.io_serviced",
1070ea25da48SPaolo Valente 		.private = (unsigned long)&blkcg_policy_bfq,
1071ea25da48SPaolo Valente 		.seq_show = blkg_print_stat_ios,
1072ea25da48SPaolo Valente 	},
1073a33801e8SLuca Miccio #ifdef CONFIG_DEBUG_BLK_CGROUP
1074a33801e8SLuca Miccio 	{
1075a33801e8SLuca Miccio 		.name = "bfq.time",
1076a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1077a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat,
1078a33801e8SLuca Miccio 	},
1079a33801e8SLuca Miccio 	{
1080a33801e8SLuca Miccio 		.name = "bfq.sectors",
1081a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors,
1082a33801e8SLuca Miccio 	},
1083ea25da48SPaolo Valente 	{
1084ea25da48SPaolo Valente 		.name = "bfq.io_service_time",
1085ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1086ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1087ea25da48SPaolo Valente 	},
1088ea25da48SPaolo Valente 	{
1089ea25da48SPaolo Valente 		.name = "bfq.io_wait_time",
1090ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1091ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1092ea25da48SPaolo Valente 	},
1093ea25da48SPaolo Valente 	{
1094ea25da48SPaolo Valente 		.name = "bfq.io_merged",
1095ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1096ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1097ea25da48SPaolo Valente 	},
1098ea25da48SPaolo Valente 	{
1099ea25da48SPaolo Valente 		.name = "bfq.io_queued",
1100ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1101ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat,
1102ea25da48SPaolo Valente 	},
1103a33801e8SLuca Miccio #endif /* CONFIG_DEBUG_BLK_CGROUP */
1104ea25da48SPaolo Valente 
1105ea25da48SPaolo Valente 	/* the same statictics which cover the bfqg and its descendants */
1106ea25da48SPaolo Valente 	{
1107ea25da48SPaolo Valente 		.name = "bfq.io_service_bytes_recursive",
1108ea25da48SPaolo Valente 		.private = (unsigned long)&blkcg_policy_bfq,
1109ea25da48SPaolo Valente 		.seq_show = blkg_print_stat_bytes_recursive,
1110ea25da48SPaolo Valente 	},
1111ea25da48SPaolo Valente 	{
1112ea25da48SPaolo Valente 		.name = "bfq.io_serviced_recursive",
1113ea25da48SPaolo Valente 		.private = (unsigned long)&blkcg_policy_bfq,
1114ea25da48SPaolo Valente 		.seq_show = blkg_print_stat_ios_recursive,
1115ea25da48SPaolo Valente 	},
1116a33801e8SLuca Miccio #ifdef CONFIG_DEBUG_BLK_CGROUP
1117a33801e8SLuca Miccio 	{
1118a33801e8SLuca Miccio 		.name = "bfq.time_recursive",
1119a33801e8SLuca Miccio 		.private = offsetof(struct bfq_group, stats.time),
1120a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_recursive,
1121a33801e8SLuca Miccio 	},
1122a33801e8SLuca Miccio 	{
1123a33801e8SLuca Miccio 		.name = "bfq.sectors_recursive",
1124a33801e8SLuca Miccio 		.seq_show = bfqg_print_stat_sectors_recursive,
1125a33801e8SLuca Miccio 	},
1126ea25da48SPaolo Valente 	{
1127ea25da48SPaolo Valente 		.name = "bfq.io_service_time_recursive",
1128ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.service_time),
1129ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1130ea25da48SPaolo Valente 	},
1131ea25da48SPaolo Valente 	{
1132ea25da48SPaolo Valente 		.name = "bfq.io_wait_time_recursive",
1133ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.wait_time),
1134ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1135ea25da48SPaolo Valente 	},
1136ea25da48SPaolo Valente 	{
1137ea25da48SPaolo Valente 		.name = "bfq.io_merged_recursive",
1138ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.merged),
1139ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1140ea25da48SPaolo Valente 	},
1141ea25da48SPaolo Valente 	{
1142ea25da48SPaolo Valente 		.name = "bfq.io_queued_recursive",
1143ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.queued),
1144ea25da48SPaolo Valente 		.seq_show = bfqg_print_rwstat_recursive,
1145ea25da48SPaolo Valente 	},
1146ea25da48SPaolo Valente 	{
1147ea25da48SPaolo Valente 		.name = "bfq.avg_queue_size",
1148ea25da48SPaolo Valente 		.seq_show = bfqg_print_avg_queue_size,
1149ea25da48SPaolo Valente 	},
1150ea25da48SPaolo Valente 	{
1151ea25da48SPaolo Valente 		.name = "bfq.group_wait_time",
1152ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.group_wait_time),
1153ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1154ea25da48SPaolo Valente 	},
1155ea25da48SPaolo Valente 	{
1156ea25da48SPaolo Valente 		.name = "bfq.idle_time",
1157ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.idle_time),
1158ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1159ea25da48SPaolo Valente 	},
1160ea25da48SPaolo Valente 	{
1161ea25da48SPaolo Valente 		.name = "bfq.empty_time",
1162ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.empty_time),
1163ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1164ea25da48SPaolo Valente 	},
1165ea25da48SPaolo Valente 	{
1166ea25da48SPaolo Valente 		.name = "bfq.dequeue",
1167ea25da48SPaolo Valente 		.private = offsetof(struct bfq_group, stats.dequeue),
1168ea25da48SPaolo Valente 		.seq_show = bfqg_print_stat,
1169ea25da48SPaolo Valente 	},
1170a33801e8SLuca Miccio #endif	/* CONFIG_DEBUG_BLK_CGROUP */
1171ea25da48SPaolo Valente 	{ }	/* terminate */
1172ea25da48SPaolo Valente };
1173ea25da48SPaolo Valente 
1174ea25da48SPaolo Valente struct cftype bfq_blkg_files[] = {
1175ea25da48SPaolo Valente 	{
1176ea25da48SPaolo Valente 		.name = "bfq.weight",
1177ea25da48SPaolo Valente 		.flags = CFTYPE_NOT_ON_ROOT,
1178ea25da48SPaolo Valente 		.seq_show = bfq_io_show_weight,
1179ea25da48SPaolo Valente 		.write = bfq_io_set_weight,
1180ea25da48SPaolo Valente 	},
1181ea25da48SPaolo Valente 	{} /* terminate */
1182ea25da48SPaolo Valente };
1183ea25da48SPaolo Valente 
1184ea25da48SPaolo Valente #else	/* CONFIG_BFQ_GROUP_IOSCHED */
1185ea25da48SPaolo Valente 
1186ea25da48SPaolo Valente void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1187ea25da48SPaolo Valente 		   struct bfq_group *bfqg) {}
1188ea25da48SPaolo Valente 
1189ea25da48SPaolo Valente void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
1190ea25da48SPaolo Valente {
1191ea25da48SPaolo Valente 	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
1192ea25da48SPaolo Valente 
1193ea25da48SPaolo Valente 	entity->weight = entity->new_weight;
1194ea25da48SPaolo Valente 	entity->orig_weight = entity->new_weight;
1195ea25da48SPaolo Valente 	if (bfqq) {
1196ea25da48SPaolo Valente 		bfqq->ioprio = bfqq->new_ioprio;
1197ea25da48SPaolo Valente 		bfqq->ioprio_class = bfqq->new_ioprio_class;
1198ea25da48SPaolo Valente 	}
1199ea25da48SPaolo Valente 	entity->sched_data = &bfqg->sched_data;
1200ea25da48SPaolo Valente }
1201ea25da48SPaolo Valente 
1202ea25da48SPaolo Valente void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {}
1203ea25da48SPaolo Valente 
1204ea25da48SPaolo Valente void bfq_end_wr_async(struct bfq_data *bfqd)
1205ea25da48SPaolo Valente {
1206ea25da48SPaolo Valente 	bfq_end_wr_async_queues(bfqd, bfqd->root_group);
1207ea25da48SPaolo Valente }
1208ea25da48SPaolo Valente 
1209ea25da48SPaolo Valente struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, struct blkcg *blkcg)
1210ea25da48SPaolo Valente {
1211ea25da48SPaolo Valente 	return bfqd->root_group;
1212ea25da48SPaolo Valente }
1213ea25da48SPaolo Valente 
1214ea25da48SPaolo Valente struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
1215ea25da48SPaolo Valente {
1216ea25da48SPaolo Valente 	return bfqq->bfqd->root_group;
1217ea25da48SPaolo Valente }
1218ea25da48SPaolo Valente 
1219ea25da48SPaolo Valente struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
1220ea25da48SPaolo Valente {
1221ea25da48SPaolo Valente 	struct bfq_group *bfqg;
1222ea25da48SPaolo Valente 	int i;
1223ea25da48SPaolo Valente 
1224ea25da48SPaolo Valente 	bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
1225ea25da48SPaolo Valente 	if (!bfqg)
1226ea25da48SPaolo Valente 		return NULL;
1227ea25da48SPaolo Valente 
1228ea25da48SPaolo Valente 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
1229ea25da48SPaolo Valente 		bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
1230ea25da48SPaolo Valente 
1231ea25da48SPaolo Valente 	return bfqg;
1232ea25da48SPaolo Valente }
1233ea25da48SPaolo Valente #endif	/* CONFIG_BFQ_GROUP_IOSCHED */
1234