@@ -31,27 +31,6 @@ EXPORT_SYMBOL_GPL(blkcg_root);
3131
3232static struct blkcg_policy * blkcg_policy [BLKCG_MAX_POLS ];
3333
34- struct blkcg * cgroup_to_blkcg (struct cgroup * cgroup )
35- {
36- return container_of (cgroup_subsys_state (cgroup , blkio_subsys_id ),
37- struct blkcg , css );
38- }
39- EXPORT_SYMBOL_GPL (cgroup_to_blkcg );
40-
41- static struct blkcg * task_blkcg (struct task_struct * tsk )
42- {
43- return container_of (task_subsys_state (tsk , blkio_subsys_id ),
44- struct blkcg , css );
45- }
46-
47- struct blkcg * bio_blkcg (struct bio * bio )
48- {
49- if (bio && bio -> bi_css )
50- return container_of (bio -> bi_css , struct blkcg , css );
51- return task_blkcg (current );
52- }
53- EXPORT_SYMBOL_GPL (bio_blkcg );
54-
5534static bool blkcg_policy_enabled (struct request_queue * q ,
5635 const struct blkcg_policy * pol )
5736{
@@ -84,23 +63,26 @@ static void blkg_free(struct blkcg_gq *blkg)
8463 kfree (pd );
8564 }
8665
66+ blk_exit_rl (& blkg -> rl );
8767 kfree (blkg );
8868}
8969
9070/**
9171 * blkg_alloc - allocate a blkg
9272 * @blkcg: block cgroup the new blkg is associated with
9373 * @q: request_queue the new blkg is associated with
74+ * @gfp_mask: allocation mask to use
9475 *
9576 * Allocate a new blkg assocating @blkcg and @q.
9677 */
97- static struct blkcg_gq * blkg_alloc (struct blkcg * blkcg , struct request_queue * q )
78+ static struct blkcg_gq * blkg_alloc (struct blkcg * blkcg , struct request_queue * q ,
79+ gfp_t gfp_mask )
9880{
9981 struct blkcg_gq * blkg ;
10082 int i ;
10183
10284 /* alloc and init base part */
103- blkg = kzalloc_node (sizeof (* blkg ), GFP_ATOMIC , q -> node );
85+ blkg = kzalloc_node (sizeof (* blkg ), gfp_mask , q -> node );
10486 if (!blkg )
10587 return NULL ;
10688
@@ -109,6 +91,13 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q)
10991 blkg -> blkcg = blkcg ;
11092 blkg -> refcnt = 1 ;
11193
94+ /* root blkg uses @q->root_rl, init rl only for !root blkgs */
95+ if (blkcg != & blkcg_root ) {
96+ if (blk_init_rl (& blkg -> rl , q , gfp_mask ))
97+ goto err_free ;
98+ blkg -> rl .blkg = blkg ;
99+ }
100+
112101 for (i = 0 ; i < BLKCG_MAX_POLS ; i ++ ) {
113102 struct blkcg_policy * pol = blkcg_policy [i ];
114103 struct blkg_policy_data * pd ;
@@ -117,11 +106,9 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q)
117106 continue ;
118107
119108 /* alloc per-policy data and attach it to blkg */
120- pd = kzalloc_node (pol -> pd_size , GFP_ATOMIC , q -> node );
121- if (!pd ) {
122- blkg_free (blkg );
123- return NULL ;
124- }
109+ pd = kzalloc_node (pol -> pd_size , gfp_mask , q -> node );
110+ if (!pd )
111+ goto err_free ;
125112
126113 blkg -> pd [i ] = pd ;
127114 pd -> blkg = blkg ;
@@ -132,6 +119,10 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q)
132119 }
133120
134121 return blkg ;
122+
123+ err_free :
124+ blkg_free (blkg );
125+ return NULL ;
135126}
136127
137128static struct blkcg_gq * __blkg_lookup (struct blkcg * blkcg ,
@@ -175,9 +166,13 @@ struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
175166}
176167EXPORT_SYMBOL_GPL (blkg_lookup );
177168
169+ /*
170+ * If @new_blkg is %NULL, this function tries to allocate a new one as
171+ * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
172+ */
178173static struct blkcg_gq * __blkg_lookup_create (struct blkcg * blkcg ,
179- struct request_queue * q )
180- __releases ( q - > queue_lock ) __acquires ( q - > queue_lock )
174+ struct request_queue * q ,
175+ struct blkcg_gq * new_blkg )
181176{
182177 struct blkcg_gq * blkg ;
183178 int ret ;
@@ -189,24 +184,26 @@ static struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
189184 blkg = __blkg_lookup (blkcg , q );
190185 if (blkg ) {
191186 rcu_assign_pointer (blkcg -> blkg_hint , blkg );
192- return blkg ;
187+ goto out_free ;
193188 }
194189
195190 /* blkg holds a reference to blkcg */
196- if (!css_tryget (& blkcg -> css ))
197- return ERR_PTR (- EINVAL );
191+ if (!css_tryget (& blkcg -> css )) {
192+ blkg = ERR_PTR (- EINVAL );
193+ goto out_free ;
194+ }
198195
199196 /* allocate */
200- ret = - ENOMEM ;
201- blkg = blkg_alloc (blkcg , q );
202- if (unlikely (!blkg ))
203- goto err_put ;
197+ if (!new_blkg ) {
198+ new_blkg = blkg_alloc (blkcg , q , GFP_ATOMIC );
199+ if (unlikely (!new_blkg )) {
200+ blkg = ERR_PTR (- ENOMEM );
201+ goto out_put ;
202+ }
203+ }
204+ blkg = new_blkg ;
204205
205206 /* insert */
206- ret = radix_tree_preload (GFP_ATOMIC );
207- if (ret )
208- goto err_free ;
209-
210207 spin_lock (& blkcg -> lock );
211208 ret = radix_tree_insert (& blkcg -> blkg_tree , q -> id , blkg );
212209 if (likely (!ret )) {
@@ -215,15 +212,15 @@ static struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
215212 }
216213 spin_unlock (& blkcg -> lock );
217214
218- radix_tree_preload_end ();
219-
220215 if (!ret )
221216 return blkg ;
222- err_free :
223- blkg_free ( blkg );
224- err_put :
217+
218+ blkg = ERR_PTR ( ret );
219+ out_put :
225220 css_put (& blkcg -> css );
226- return ERR_PTR (ret );
221+ out_free :
222+ blkg_free (new_blkg );
223+ return blkg ;
227224}
228225
229226struct blkcg_gq * blkg_lookup_create (struct blkcg * blkcg ,
@@ -235,7 +232,7 @@ struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
235232 */
236233 if (unlikely (blk_queue_bypass (q )))
237234 return ERR_PTR (blk_queue_dead (q ) ? - EINVAL : - EBUSY );
238- return __blkg_lookup_create (blkcg , q );
235+ return __blkg_lookup_create (blkcg , q , NULL );
239236}
240237EXPORT_SYMBOL_GPL (blkg_lookup_create );
241238
@@ -313,6 +310,38 @@ void __blkg_release(struct blkcg_gq *blkg)
313310}
314311EXPORT_SYMBOL_GPL (__blkg_release );
315312
313+ /*
314+ * The next function used by blk_queue_for_each_rl(). It's a bit tricky
315+ * because the root blkg uses @q->root_rl instead of its own rl.
316+ */
317+ struct request_list * __blk_queue_next_rl (struct request_list * rl ,
318+ struct request_queue * q )
319+ {
320+ struct list_head * ent ;
321+ struct blkcg_gq * blkg ;
322+
323+ /*
324+ * Determine the current blkg list_head. The first entry is
325+ * root_rl which is off @q->blkg_list and mapped to the head.
326+ */
327+ if (rl == & q -> root_rl ) {
328+ ent = & q -> blkg_list ;
329+ } else {
330+ blkg = container_of (rl , struct blkcg_gq , rl );
331+ ent = & blkg -> q_node ;
332+ }
333+
334+ /* walk to the next list_head, skip root blkcg */
335+ ent = ent -> next ;
336+ if (ent == & q -> root_blkg -> q_node )
337+ ent = ent -> next ;
338+ if (ent == & q -> blkg_list )
339+ return NULL ;
340+
341+ blkg = container_of (ent , struct blkcg_gq , q_node );
342+ return & blkg -> rl ;
343+ }
344+
316345static int blkcg_reset_stats (struct cgroup * cgroup , struct cftype * cftype ,
317346 u64 val )
318347{
@@ -734,24 +763,36 @@ int blkcg_activate_policy(struct request_queue *q,
734763 struct blkcg_gq * blkg ;
735764 struct blkg_policy_data * pd , * n ;
736765 int cnt = 0 , ret ;
766+ bool preloaded ;
737767
738768 if (blkcg_policy_enabled (q , pol ))
739769 return 0 ;
740770
771+ /* preallocations for root blkg */
772+ blkg = blkg_alloc (& blkcg_root , q , GFP_KERNEL );
773+ if (!blkg )
774+ return - ENOMEM ;
775+
776+ preloaded = !radix_tree_preload (GFP_KERNEL );
777+
741778 blk_queue_bypass_start (q );
742779
743780 /* make sure the root blkg exists and count the existing blkgs */
744781 spin_lock_irq (q -> queue_lock );
745782
746783 rcu_read_lock ();
747- blkg = __blkg_lookup_create (& blkcg_root , q );
784+ blkg = __blkg_lookup_create (& blkcg_root , q , blkg );
748785 rcu_read_unlock ();
749786
787+ if (preloaded )
788+ radix_tree_preload_end ();
789+
750790 if (IS_ERR (blkg )) {
751791 ret = PTR_ERR (blkg );
752792 goto out_unlock ;
753793 }
754794 q -> root_blkg = blkg ;
795+ q -> root_rl .blkg = blkg ;
755796
756797 list_for_each_entry (blkg , & q -> blkg_list , q_node )
757798 cnt ++ ;
0 commit comments