xref: /linux/block/blk-cgroup.c (revision 77f77fb7923feab3edd0accf793235623ef4b342)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common Block IO controller cgroup interface
4  *
5  * Based on ideas and code from CFQ, CFS and BFQ:
6  * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7  *
8  * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
9  *		      Paolo Valente <paolo.valente@unimore.it>
10  *
11  * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
12  * 	              Nauman Rafique <nauman@google.com>
13  *
14  * For policy-specific per-blkcg data:
15  * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
16  *                    Arianna Avanzini <avanzini.arianna@gmail.com>
17  */
18 #include <linux/ioprio.h>
19 #include <linux/kdev_t.h>
20 #include <linux/module.h>
21 #include <linux/sched/signal.h>
22 #include <linux/err.h>
23 #include <linux/blkdev.h>
24 #include <linux/backing-dev.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/wait_bit.h>
28 #include <linux/atomic.h>
29 #include <linux/ctype.h>
30 #include <linux/resume_user_mode.h>
31 #include <linux/psi.h>
32 #include <linux/part_stat.h>
33 #include "blk.h"
34 #include "blk-cgroup.h"
35 #include "blk-ioprio.h"
36 #include "blk-throttle.h"
37 
38 static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu);
39 
40 /*
41  * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation.
42  * blkcg_pol_register_mutex nests outside of it and synchronizes entire
43  * policy [un]register operations including cgroup file additions /
44  * removals.  Putting cgroup file registration outside blkcg_pol_mutex
45  * allows grabbing it from cgroup callbacks.
46  */
47 static DEFINE_MUTEX(blkcg_pol_register_mutex);
48 static DEFINE_MUTEX(blkcg_pol_mutex);
49 
50 struct blkcg blkcg_root;
51 EXPORT_SYMBOL_GPL(blkcg_root);
52 
53 struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
54 EXPORT_SYMBOL_GPL(blkcg_root_css);
55 
56 /* number of blkcgs with a non-zero congestion_count */
57 atomic_t blkcg_nr_congested __read_mostly = ATOMIC_INIT(0);
58 
59 static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
60 
61 static LIST_HEAD(all_blkcgs);		/* protected by blkcg_pol_mutex */
62 
63 bool blkcg_debug_stats = false;
64 
65 static DEFINE_RAW_SPINLOCK(blkg_stat_lock);
66 
67 #define BLKG_DESTROY_BATCH_SIZE  64
68 
69 /*
70  * Lockless lists for tracking IO stats update
71  *
72  * New IO stats are stored in the percpu iostat_cpu within blkcg_gq (blkg).
73  * There are multiple blkg's (one for each block device) attached to each
74  * blkcg. The rstat code keeps track of which cpu has IO stats updated,
75  * but it doesn't know which blkg has the updated stats. If there are many
76  * block devices in a system, the cost of iterating all the blkg's to flush
77  * out the IO stats can be high. To reduce such overhead, a set of percpu
78  * lockless lists (lhead) per blkcg are used to track the set of recently
79  * updated iostat_cpu's since the last flush. An iostat_cpu will be put
80  * onto the lockless list on the update side [blk_cgroup_bio_start()] if
81  * not there yet and then removed when being flushed [blkcg_rstat_flush()].
82  * References to blkg are gotten and then put back in the process to
83  * protect against blkg removal.
84  *
85  * Return: 0 if successful or -ENOMEM if allocation fails.
86  */
87 static int init_blkcg_llists(struct blkcg *blkcg)
88 {
89 	int cpu;
90 
91 	blkcg->lhead = alloc_percpu_gfp(struct llist_head, GFP_KERNEL);
92 	if (!blkcg->lhead)
93 		return -ENOMEM;
94 
95 	for_each_possible_cpu(cpu)
96 		init_llist_head(per_cpu_ptr(blkcg->lhead, cpu));
97 	return 0;
98 }
99 
100 /**
101  * blkcg_css - find the current css
102  *
103  * Find the css associated with either the kthread or the current task.
104  * This may return a dying css, so it is up to the caller to use tryget logic
105  * to confirm it is alive and well.
106  */
107 static struct cgroup_subsys_state *blkcg_css(void)
108 {
109 	struct cgroup_subsys_state *css;
110 
111 	css = kthread_blkcg();
112 	if (css)
113 		return css;
114 	return task_css(current, io_cgrp_id);
115 }
116 
117 static void blkg_free_workfn(struct work_struct *work)
118 {
119 	struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
120 					     free_work);
121 	struct request_queue *q = blkg->q;
122 	int i;
123 
124 	/*
125 	 * pd_free_fn() can also be called from blkcg_deactivate_policy(),
126 	 * in order to make sure pd_free_fn() is called in order, the deletion
127 	 * of the list blkg->q_node is delayed to here from blkg_destroy(), and
128 	 * blkcg_mutex is used to synchronize blkg_free_workfn() and
129 	 * blkcg_deactivate_policy().
130 	 */
131 	mutex_lock(&q->blkcg_mutex);
132 	for (i = 0; i < BLKCG_MAX_POLS; i++)
133 		if (blkg->pd[i])
134 			blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
135 	if (blkg->parent)
136 		blkg_put(blkg->parent);
137 	spin_lock_irq(&q->queue_lock);
138 	list_del_init(&blkg->q_node);
139 	spin_unlock_irq(&q->queue_lock);
140 	mutex_unlock(&q->blkcg_mutex);
141 
142 	/*
143 	 * Release blkcg css ref only after blkg is removed from q->blkg_list,
144 	 * so concurrent iterators won't see a blkg with a freed blkcg.
145 	 */
146 	css_put(&blkg->blkcg->css);
147 	blk_put_queue(q);
148 	free_percpu(blkg->iostat_cpu);
149 	percpu_ref_exit(&blkg->refcnt);
150 	kfree(blkg);
151 }
152 
153 /**
154  * blkg_free - free a blkg
155  * @blkg: blkg to free
156  *
157  * Free @blkg which may be partially allocated.
158  */
159 static void blkg_free(struct blkcg_gq *blkg)
160 {
161 	if (!blkg)
162 		return;
163 
164 	/*
165 	 * Both ->pd_free_fn() and request queue's release handler may
166 	 * sleep, so free us by scheduling one work func
167 	 */
168 	INIT_WORK(&blkg->free_work, blkg_free_workfn);
169 	schedule_work(&blkg->free_work);
170 }
171 
172 static void __blkg_release(struct rcu_head *rcu)
173 {
174 	struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);
175 
176 #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
177 	WARN_ON(!bio_list_empty(&blkg->async_bios));
178 #endif
179 
180 	blkg_free(blkg);
181 }
182 
183 /*
184  * A group is RCU protected, but having an rcu lock does not mean that one
185  * can access all the fields of blkg and assume these are valid.  For
186  * example, don't try to follow throtl_data and request queue links.
187  *
188  * Having a reference to blkg under an rcu allows accesses to only values
189  * local to groups like group stats and group rate limits.
190  */
191 static void blkg_release(struct percpu_ref *ref)
192 {
193 	struct blkcg_gq *blkg = container_of(ref, struct blkcg_gq, refcnt);
194 	struct blkcg *blkcg = blkg->blkcg;
195 	int cpu;
196 
197 	/*
198 	 * Flush all the non-empty percpu lockless lists before releasing
199 	 * us, given these stat belongs to us.
200 	 *
201 	 * blkg_stat_lock is for serializing blkg stat update
202 	 */
203 	for_each_possible_cpu(cpu)
204 		__blkcg_rstat_flush(blkcg, cpu);
205 
206 	call_rcu(&blkg->rcu_head, __blkg_release);
207 }
208 
209 #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
210 static struct workqueue_struct *blkcg_punt_bio_wq;
211 
212 static void blkg_async_bio_workfn(struct work_struct *work)
213 {
214 	struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
215 					     async_bio_work);
216 	struct bio_list bios = BIO_EMPTY_LIST;
217 	struct bio *bio;
218 	struct blk_plug plug;
219 	bool need_plug = false;
220 
221 	/* as long as there are pending bios, @blkg can't go away */
222 	spin_lock(&blkg->async_bio_lock);
223 	bio_list_merge_init(&bios, &blkg->async_bios);
224 	spin_unlock(&blkg->async_bio_lock);
225 
226 	/* start plug only when bio_list contains at least 2 bios */
227 	if (bios.head && bios.head->bi_next) {
228 		need_plug = true;
229 		blk_start_plug(&plug);
230 	}
231 	while ((bio = bio_list_pop(&bios)))
232 		submit_bio(bio);
233 	if (need_plug)
234 		blk_finish_plug(&plug);
235 }
236 
237 /*
238  * When a shared kthread issues a bio for a cgroup, doing so synchronously can
239  * lead to priority inversions as the kthread can be trapped waiting for that
240  * cgroup.  Use this helper instead of submit_bio to punt the actual issuing to
241  * a dedicated per-blkcg work item to avoid such priority inversions.
242  */
243 void blkcg_punt_bio_submit(struct bio *bio)
244 {
245 	struct blkcg_gq *blkg = bio->bi_blkg;
246 
247 	if (blkg->parent) {
248 		spin_lock(&blkg->async_bio_lock);
249 		bio_list_add(&blkg->async_bios, bio);
250 		spin_unlock(&blkg->async_bio_lock);
251 		queue_work(blkcg_punt_bio_wq, &blkg->async_bio_work);
252 	} else {
253 		/* never bounce for the root cgroup */
254 		submit_bio(bio);
255 	}
256 }
257 EXPORT_SYMBOL_GPL(blkcg_punt_bio_submit);
258 
259 static int __init blkcg_punt_bio_init(void)
260 {
261 	blkcg_punt_bio_wq = alloc_workqueue("blkcg_punt_bio",
262 					    WQ_MEM_RECLAIM | WQ_FREEZABLE |
263 					    WQ_UNBOUND | WQ_SYSFS, 0);
264 	if (!blkcg_punt_bio_wq)
265 		return -ENOMEM;
266 	return 0;
267 }
268 subsys_initcall(blkcg_punt_bio_init);
269 #endif /* CONFIG_BLK_CGROUP_PUNT_BIO */
270 
271 /**
272  * bio_blkcg_css - return the blkcg CSS associated with a bio
273  * @bio: target bio
274  *
275  * This returns the CSS for the blkcg associated with a bio, or %NULL if not
276  * associated. Callers are expected to either handle %NULL or know association
277  * has been done prior to calling this.
278  */
279 struct cgroup_subsys_state *bio_blkcg_css(struct bio *bio)
280 {
281 	if (!bio || !bio->bi_blkg)
282 		return NULL;
283 	return &bio->bi_blkg->blkcg->css;
284 }
285 EXPORT_SYMBOL_GPL(bio_blkcg_css);
286 
287 /**
288  * blkcg_parent - get the parent of a blkcg
289  * @blkcg: blkcg of interest
290  *
291  * Return the parent blkcg of @blkcg.  Can be called anytime.
292  */
293 static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
294 {
295 	return css_to_blkcg(blkcg->css.parent);
296 }
297 
298 /**
299  * blkg_alloc - allocate a blkg
300  * @blkcg: block cgroup the new blkg is associated with
301  * @disk: gendisk the new blkg is associated with
302  * @gfp_mask: allocation mask to use
303  *
304  * Allocate a new blkg associating @blkcg and @disk.
305  */
306 static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
307 				   gfp_t gfp_mask)
308 {
309 	struct blkcg_gq *blkg;
310 	int i, cpu;
311 
312 	/* alloc and init base part */
313 	blkg = kzalloc_node(sizeof(*blkg), gfp_mask, disk->queue->node);
314 	if (!blkg)
315 		return NULL;
316 	if (percpu_ref_init(&blkg->refcnt, blkg_release, 0, gfp_mask))
317 		goto out_free_blkg;
318 	blkg->iostat_cpu = alloc_percpu_gfp(struct blkg_iostat_set, gfp_mask);
319 	if (!blkg->iostat_cpu)
320 		goto out_exit_refcnt;
321 	if (!blk_get_queue(disk->queue))
322 		goto out_free_iostat;
323 	/* blkg holds a reference to blkcg */
324 	if (!css_tryget_online(&blkcg->css))
325 		goto out_put_queue;
326 
327 	blkg->q = disk->queue;
328 	INIT_LIST_HEAD(&blkg->q_node);
329 	blkg->blkcg = blkcg;
330 	blkg->iostat.blkg = blkg;
331 #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
332 	spin_lock_init(&blkg->async_bio_lock);
333 	bio_list_init(&blkg->async_bios);
334 	INIT_WORK(&blkg->async_bio_work, blkg_async_bio_workfn);
335 #endif
336 
337 	u64_stats_init(&blkg->iostat.sync);
338 	for_each_possible_cpu(cpu) {
339 		u64_stats_init(&per_cpu_ptr(blkg->iostat_cpu, cpu)->sync);
340 		per_cpu_ptr(blkg->iostat_cpu, cpu)->blkg = blkg;
341 	}
342 
343 	for (i = 0; i < BLKCG_MAX_POLS; i++) {
344 		struct blkcg_policy *pol = blkcg_policy[i];
345 		struct blkg_policy_data *pd;
346 
347 		if (!blkcg_policy_enabled(disk->queue, pol))
348 			continue;
349 
350 		/* alloc per-policy data and attach it to blkg */
351 		pd = pol->pd_alloc_fn(disk, blkcg, gfp_mask);
352 		if (!pd)
353 			goto out_free_pds;
354 		blkg->pd[i] = pd;
355 		pd->blkg = blkg;
356 		pd->plid = i;
357 		pd->online = false;
358 	}
359 
360 	return blkg;
361 
362 out_free_pds:
363 	while (--i >= 0)
364 		if (blkg->pd[i])
365 			blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
366 	css_put(&blkcg->css);
367 out_put_queue:
368 	blk_put_queue(disk->queue);
369 out_free_iostat:
370 	free_percpu(blkg->iostat_cpu);
371 out_exit_refcnt:
372 	percpu_ref_exit(&blkg->refcnt);
373 out_free_blkg:
374 	kfree(blkg);
375 	return NULL;
376 }
377 
378 /*
379  * If @new_blkg is %NULL, this function tries to allocate a new one as
380  * necessary using %GFP_NOWAIT.  @new_blkg is always consumed on return.
381  */
382 static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,
383 				    struct blkcg_gq *new_blkg)
384 {
385 	struct blkcg_gq *blkg;
386 	int i, ret;
387 
388 	lockdep_assert_held(&disk->queue->queue_lock);
389 
390 	/* request_queue is dying, do not create/recreate a blkg */
391 	if (blk_queue_dying(disk->queue)) {
392 		ret = -ENODEV;
393 		goto err_free_blkg;
394 	}
395 
396 	/* allocate */
397 	if (!new_blkg) {
398 		new_blkg = blkg_alloc(blkcg, disk, GFP_NOWAIT);
399 		if (unlikely(!new_blkg)) {
400 			ret = -ENOMEM;
401 			goto err_free_blkg;
402 		}
403 	}
404 	blkg = new_blkg;
405 
406 	/* link parent */
407 	if (blkcg_parent(blkcg)) {
408 		blkg->parent = blkg_lookup(blkcg_parent(blkcg), disk->queue);
409 		if (WARN_ON_ONCE(!blkg->parent)) {
410 			ret = -ENODEV;
411 			goto err_free_blkg;
412 		}
413 		blkg_get(blkg->parent);
414 	}
415 
416 	/* invoke per-policy init */
417 	for (i = 0; i < BLKCG_MAX_POLS; i++) {
418 		struct blkcg_policy *pol = blkcg_policy[i];
419 
420 		if (blkg->pd[i] && pol->pd_init_fn)
421 			pol->pd_init_fn(blkg->pd[i]);
422 	}
423 
424 	/* insert */
425 	spin_lock(&blkcg->lock);
426 	ret = radix_tree_insert(&blkcg->blkg_tree, disk->queue->id, blkg);
427 	if (likely(!ret)) {
428 		hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
429 		list_add(&blkg->q_node, &disk->queue->blkg_list);
430 
431 		for (i = 0; i < BLKCG_MAX_POLS; i++) {
432 			struct blkcg_policy *pol = blkcg_policy[i];
433 
434 			if (blkg->pd[i]) {
435 				if (pol->pd_online_fn)
436 					pol->pd_online_fn(blkg->pd[i]);
437 				blkg->pd[i]->online = true;
438 			}
439 		}
440 		blkg->online = true;
441 	}
442 	spin_unlock(&blkcg->lock);
443 
444 	if (!ret)
445 		return blkg;
446 
447 	/* @blkg failed fully initialized, use the usual release path */
448 	percpu_ref_kill(&blkg->refcnt);
449 	return ERR_PTR(ret);
450 
451 err_free_blkg:
452 	if (new_blkg)
453 		blkg_free(new_blkg);
454 	return ERR_PTR(ret);
455 }
456 
457 /**
458  * blkg_lookup_create - lookup blkg, try to create one if not there
459  * @blkcg: blkcg of interest
460  * @disk: gendisk of interest
461  *
462  * Lookup blkg for the @blkcg - @disk pair.  If it doesn't exist, try to
463  * create one.  blkg creation is performed recursively from blkcg_root such
464  * that all non-root blkg's have access to the parent blkg.  This function
465  * should be called under RCU read lock and takes @disk->queue->queue_lock.
466  *
467  * Returns the blkg or the closest blkg if blkg_create() fails as it walks
468  * down from root.
469  */
470 static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
471 		struct gendisk *disk)
472 {
473 	struct request_queue *q = disk->queue;
474 	struct blkcg_gq *blkg;
475 
476 	rcu_read_lock();
477 	blkg = blkg_lookup(blkcg, q);
478 	if (blkg) {
479 		if (blkcg != &blkcg_root &&
480 		    blkg != rcu_dereference(blkcg->blkg_hint))
481 			rcu_assign_pointer(blkcg->blkg_hint, blkg);
482 		rcu_read_unlock();
483 		return blkg;
484 	}
485 	rcu_read_unlock();
486 
487 	/*
488 	 * Create blkgs walking down from blkcg_root to @blkcg, so that all
489 	 * non-root blkgs have access to their parents.  Returns the closest
490 	 * blkg to the intended blkg should blkg_create() fail.
491 	 */
492 	while (true) {
493 		struct blkcg *pos = blkcg;
494 		struct blkcg *parent = blkcg_parent(blkcg);
495 		struct blkcg_gq *ret_blkg = q->root_blkg;
496 
497 		while (parent) {
498 			blkg = blkg_lookup(parent, q);
499 			if (blkg) {
500 				/* remember closest blkg */
501 				ret_blkg = blkg;
502 				break;
503 			}
504 			pos = parent;
505 			parent = blkcg_parent(parent);
506 		}
507 
508 		blkg = blkg_create(pos, disk, NULL);
509 		if (IS_ERR(blkg)) {
510 			blkg = ret_blkg;
511 			break;
512 		}
513 		if (pos == blkcg)
514 			break;
515 	}
516 
517 	return blkg;
518 }
519 
520 static void blkg_destroy(struct blkcg_gq *blkg)
521 {
522 	struct blkcg *blkcg = blkg->blkcg;
523 	int i;
524 
525 	lockdep_assert_held(&blkg->q->queue_lock);
526 	lockdep_assert_held(&blkcg->lock);
527 
528 	/*
529 	 * blkg stays on the queue list until blkg_free_workfn(), see details in
530 	 * blkg_free_workfn(), hence this function can be called from
531 	 * blkcg_destroy_blkgs() first and again from blkg_destroy_all() before
532 	 * blkg_free_workfn().
533 	 */
534 	if (hlist_unhashed(&blkg->blkcg_node))
535 		return;
536 
537 	for (i = 0; i < BLKCG_MAX_POLS; i++) {
538 		struct blkcg_policy *pol = blkcg_policy[i];
539 
540 		if (blkg->pd[i] && blkg->pd[i]->online) {
541 			blkg->pd[i]->online = false;
542 			if (pol->pd_offline_fn)
543 				pol->pd_offline_fn(blkg->pd[i]);
544 		}
545 	}
546 
547 	blkg->online = false;
548 
549 	radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
550 	hlist_del_init_rcu(&blkg->blkcg_node);
551 
552 	/*
553 	 * Both setting lookup hint to and clearing it from @blkg are done
554 	 * under queue_lock.  If it's not pointing to @blkg now, it never
555 	 * will.  Hint assignment itself can race safely.
556 	 */
557 	if (rcu_access_pointer(blkcg->blkg_hint) == blkg)
558 		rcu_assign_pointer(blkcg->blkg_hint, NULL);
559 
560 	/*
561 	 * Put the reference taken at the time of creation so that when all
562 	 * queues are gone, group can be destroyed.
563 	 */
564 	percpu_ref_kill(&blkg->refcnt);
565 }
566 
567 static void blkg_destroy_all(struct gendisk *disk)
568 {
569 	struct request_queue *q = disk->queue;
570 	struct blkcg_gq *blkg;
571 	int count = BLKG_DESTROY_BATCH_SIZE;
572 	int i;
573 
574 restart:
575 	mutex_lock(&q->blkcg_mutex);
576 	spin_lock_irq(&q->queue_lock);
577 	list_for_each_entry(blkg, &q->blkg_list, q_node) {
578 		struct blkcg *blkcg = blkg->blkcg;
579 
580 		if (hlist_unhashed(&blkg->blkcg_node))
581 			continue;
582 
583 		spin_lock(&blkcg->lock);
584 		blkg_destroy(blkg);
585 		spin_unlock(&blkcg->lock);
586 
587 		/*
588 		 * in order to avoid holding the spin lock for too long, release
589 		 * it when a batch of blkgs are destroyed.
590 		 */
591 		if (!(--count)) {
592 			count = BLKG_DESTROY_BATCH_SIZE;
593 			spin_unlock_irq(&q->queue_lock);
594 			mutex_unlock(&q->blkcg_mutex);
595 			cond_resched();
596 			goto restart;
597 		}
598 	}
599 
600 	/*
601 	 * Mark policy deactivated since policy offline has been done, and
602 	 * the free is scheduled, so future blkcg_deactivate_policy() can
603 	 * be bypassed
604 	 */
605 	for (i = 0; i < BLKCG_MAX_POLS; i++) {
606 		struct blkcg_policy *pol = blkcg_policy[i];
607 
608 		if (pol)
609 			__clear_bit(pol->plid, q->blkcg_pols);
610 	}
611 
612 	q->root_blkg = NULL;
613 	spin_unlock_irq(&q->queue_lock);
614 	mutex_unlock(&q->blkcg_mutex);
615 
616 	wake_up_var(&q->root_blkg);
617 }
618 
619 static void blkg_iostat_set(struct blkg_iostat *dst, struct blkg_iostat *src)
620 {
621 	int i;
622 
623 	for (i = 0; i < BLKG_IOSTAT_NR; i++) {
624 		dst->bytes[i] = src->bytes[i];
625 		dst->ios[i] = src->ios[i];
626 	}
627 }
628 
629 static void __blkg_clear_stat(struct blkg_iostat_set *bis)
630 {
631 	struct blkg_iostat cur = {0};
632 	unsigned long flags;
633 
634 	flags = u64_stats_update_begin_irqsave(&bis->sync);
635 	blkg_iostat_set(&bis->cur, &cur);
636 	blkg_iostat_set(&bis->last, &cur);
637 	u64_stats_update_end_irqrestore(&bis->sync, flags);
638 }
639 
640 static void blkg_clear_stat(struct blkcg_gq *blkg)
641 {
642 	int cpu;
643 
644 	for_each_possible_cpu(cpu) {
645 		struct blkg_iostat_set *s = per_cpu_ptr(blkg->iostat_cpu, cpu);
646 
647 		__blkg_clear_stat(s);
648 	}
649 	__blkg_clear_stat(&blkg->iostat);
650 }
651 
652 static int blkcg_reset_stats(struct cgroup_subsys_state *css,
653 			     struct cftype *cftype, u64 val)
654 {
655 	struct blkcg *blkcg = css_to_blkcg(css);
656 	struct blkcg_gq *blkg;
657 	int i;
658 
659 	pr_info_once("blkio.%s is deprecated\n", cftype->name);
660 	mutex_lock(&blkcg_pol_mutex);
661 	spin_lock_irq(&blkcg->lock);
662 
663 	/*
664 	 * Note that stat reset is racy - it doesn't synchronize against
665 	 * stat updates.  This is a debug feature which shouldn't exist
666 	 * anyway.  If you get hit by a race, retry.
667 	 */
668 	hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
669 		blkg_clear_stat(blkg);
670 		for (i = 0; i < BLKCG_MAX_POLS; i++) {
671 			struct blkcg_policy *pol = blkcg_policy[i];
672 
673 			if (blkg->pd[i] && pol->pd_reset_stats_fn)
674 				pol->pd_reset_stats_fn(blkg->pd[i]);
675 		}
676 	}
677 
678 	spin_unlock_irq(&blkcg->lock);
679 	mutex_unlock(&blkcg_pol_mutex);
680 	return 0;
681 }
682 
683 const char *blkg_dev_name(struct blkcg_gq *blkg)
684 {
685 	if (!blkg->q->disk)
686 		return NULL;
687 	return bdi_dev_name(blkg->q->disk->bdi);
688 }
689 
690 /**
691  * blkcg_print_blkgs - helper for printing per-blkg data
692  * @sf: seq_file to print to
693  * @blkcg: blkcg of interest
694  * @prfill: fill function to print out a blkg
695  * @pol: policy in question
696  * @data: data to be passed to @prfill
697  * @show_total: to print out sum of prfill return values or not
698  *
699  * This function invokes @prfill on each blkg of @blkcg if pd for the
700  * policy specified by @pol exists.  @prfill is invoked with @sf, the
701  * policy data and @data under RCU read lock.  If @show_total is %true, the
702  * sum of the return values from @prfill is printed with "Total" label at the
703  * end.
704  *
705  * This is to be used to construct print functions for
706  * cftype->read_seq_string method.
707  */
708 void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
709 		       u64 (*prfill)(struct seq_file *,
710 				     struct blkg_policy_data *, int),
711 		       const struct blkcg_policy *pol, int data,
712 		       bool show_total)
713 {
714 	struct blkcg_gq *blkg;
715 	u64 total = 0;
716 
717 	rcu_read_lock();
718 	hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
719 		struct blkg_policy_data *pd;
720 
721 		if (!blkcg_policy_enabled(blkg->q, pol))
722 			continue;
723 
724 		pd = blkg_to_pd(blkg, pol);
725 		if (pd)
726 			total += prfill(sf, pd, data);
727 	}
728 	rcu_read_unlock();
729 
730 	if (show_total)
731 		seq_printf(sf, "Total %llu\n", (unsigned long long)total);
732 }
733 EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
734 
735 /**
736  * __blkg_prfill_u64 - prfill helper for a single u64 value
737  * @sf: seq_file to print to
738  * @pd: policy private data of interest
739  * @v: value to print
740  *
741  * Print @v to @sf for the device associated with @pd.
742  */
743 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
744 {
745 	const char *dname = blkg_dev_name(pd->blkg);
746 
747 	if (!dname)
748 		return 0;
749 
750 	seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
751 	return v;
752 }
753 EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
754 
755 /**
756  * blkg_conf_init - initialize a blkg_conf_ctx
757  * @ctx: blkg_conf_ctx to initialize
758  * @input: input string
759  *
760  * Initialize @ctx which can be used to parse blkg config input string @input.
761  * Once initialized, @ctx can be used with blkg_conf_open_bdev() and
762  * blkg_conf_prep().
763  */
764 void blkg_conf_init(struct blkg_conf_ctx *ctx, char *input)
765 {
766 	*ctx = (struct blkg_conf_ctx){ .input = input };
767 }
768 EXPORT_SYMBOL_GPL(blkg_conf_init);
769 
770 /**
771  * blkg_conf_open_bdev - parse and open bdev for per-blkg config update
772  * @ctx: blkg_conf_ctx initialized with blkg_conf_init()
773  *
774  * Parse the device node prefix part, MAJ:MIN, of per-blkg config update from
775  * @ctx->input and get and store the matching bdev in @ctx->bdev. @ctx->body is
776  * set to point past the device node prefix.
777  *
778  * Returns: -errno on error.
779  */
780 int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx)
781 {
782 	char *input = ctx->input;
783 	unsigned int major, minor;
784 	struct block_device *bdev;
785 	int key_len;
786 
787 	if (WARN_ON_ONCE(ctx->bdev))
788 		return -EINVAL;
789 
790 	if (sscanf(input, "%u:%u%n", &major, &minor, &key_len) != 2)
791 		return -EINVAL;
792 
793 	input += key_len;
794 	if (!isspace(*input))
795 		return -EINVAL;
796 	input = skip_spaces(input);
797 
798 	bdev = blkdev_get_no_open(MKDEV(major, minor), false);
799 	if (!bdev)
800 		return -ENODEV;
801 	if (bdev_is_partition(bdev)) {
802 		blkdev_put_no_open(bdev);
803 		return -ENODEV;
804 	}
805 
806 	mutex_lock(&bdev->bd_queue->rq_qos_mutex);
807 	if (!disk_live(bdev->bd_disk)) {
808 		blkdev_put_no_open(bdev);
809 		mutex_unlock(&bdev->bd_queue->rq_qos_mutex);
810 		return -ENODEV;
811 	}
812 
813 	ctx->body = input;
814 	ctx->bdev = bdev;
815 	return 0;
816 }
817 EXPORT_SYMBOL_GPL(blkg_conf_open_bdev);
818 
819 /**
820  * blkg_conf_prep - parse and prepare for per-blkg config update
821  * @blkcg: target block cgroup
822  * @pol: target policy
823  * @ctx: blkg_conf_ctx initialized with blkg_conf_init()
824  *
825  * Parse per-blkg config update from @ctx->input and initialize @ctx
826  * accordingly. On success, @ctx->body points to the part of @ctx->input
827  * following MAJ:MIN, @ctx->bdev points to the target block device and
828  * @ctx->blkg to the blkg being configured.
829  *
830  * blkg_conf_open_bdev() must be called on @ctx beforehand. On success, this
831  * function returns with queue lock held and must be followed by
832  * blkg_conf_close_bdev().
833  */
834 int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
835 		   struct blkg_conf_ctx *ctx)
836 {
837 	struct gendisk *disk;
838 	struct request_queue *q;
839 	struct blkcg_gq *blkg;
840 	int ret;
841 
842 	if (WARN_ON_ONCE(!ctx->bdev))
843 		return -EINVAL;
844 
845 	disk = ctx->bdev->bd_disk;
846 	q = disk->queue;
847 
848 	/* Prevent concurrent with blkcg_deactivate_policy() */
849 	mutex_lock(&q->blkcg_mutex);
850 	spin_lock_irq(&q->queue_lock);
851 
852 	if (!blkcg_policy_enabled(q, pol)) {
853 		ret = -EOPNOTSUPP;
854 		goto fail_unlock;
855 	}
856 
857 	blkg = blkg_lookup(blkcg, q);
858 	if (blkg)
859 		goto success;
860 
861 	/*
862 	 * Create blkgs walking down from blkcg_root to @blkcg, so that all
863 	 * non-root blkgs have access to their parents.
864 	 */
865 	while (true) {
866 		struct blkcg *pos = blkcg;
867 		struct blkcg *parent;
868 		struct blkcg_gq *new_blkg;
869 
870 		parent = blkcg_parent(blkcg);
871 		while (parent && !blkg_lookup(parent, q)) {
872 			pos = parent;
873 			parent = blkcg_parent(parent);
874 		}
875 
876 		/* Drop locks to do new blkg allocation with GFP_KERNEL. */
877 		spin_unlock_irq(&q->queue_lock);
878 
879 		new_blkg = blkg_alloc(pos, disk, GFP_NOIO);
880 		if (unlikely(!new_blkg)) {
881 			ret = -ENOMEM;
882 			goto fail_exit;
883 		}
884 
885 		if (radix_tree_preload(GFP_KERNEL)) {
886 			blkg_free(new_blkg);
887 			ret = -ENOMEM;
888 			goto fail_exit;
889 		}
890 
891 		spin_lock_irq(&q->queue_lock);
892 
893 		if (!blkcg_policy_enabled(q, pol)) {
894 			blkg_free(new_blkg);
895 			ret = -EOPNOTSUPP;
896 			goto fail_preloaded;
897 		}
898 
899 		blkg = blkg_lookup(pos, q);
900 		if (blkg) {
901 			blkg_free(new_blkg);
902 		} else {
903 			blkg = blkg_create(pos, disk, new_blkg);
904 			if (IS_ERR(blkg)) {
905 				ret = PTR_ERR(blkg);
906 				goto fail_preloaded;
907 			}
908 		}
909 
910 		radix_tree_preload_end();
911 
912 		if (pos == blkcg)
913 			goto success;
914 	}
915 success:
916 	mutex_unlock(&q->blkcg_mutex);
917 	ctx->blkg = blkg;
918 	return 0;
919 
920 fail_preloaded:
921 	radix_tree_preload_end();
922 fail_unlock:
923 	spin_unlock_irq(&q->queue_lock);
924 fail_exit:
925 	mutex_unlock(&q->blkcg_mutex);
926 	/*
927 	 * If queue was bypassing, we should retry.  Do so after a
928 	 * short msleep().  It isn't strictly necessary but queue
929 	 * can be bypassing for some time and it's always nice to
930 	 * avoid busy looping.
931 	 */
932 	if (ret == -EBUSY) {
933 		msleep(10);
934 		ret = restart_syscall();
935 	}
936 	return ret;
937 }
938 EXPORT_SYMBOL_GPL(blkg_conf_prep);
939 
940 /**
941  * blkg_conf_unprep - counterpart of blkg_conf_prep()
942  * @ctx: blkg_conf_ctx initialized with blkg_conf_init()
943  */
944 void blkg_conf_unprep(struct blkg_conf_ctx *ctx)
945 {
946 	WARN_ON_ONCE(!ctx->blkg);
947 	spin_unlock_irq(&ctx->bdev->bd_disk->queue->queue_lock);
948 	ctx->blkg = NULL;
949 }
950 EXPORT_SYMBOL_GPL(blkg_conf_unprep);
951 
952 /**
953  * blkg_conf_close_bdev - counterpart of blkg_conf_open_bdev()
954  * @ctx: blkg_conf_ctx initialized with blkg_conf_init()
955  */
956 void blkg_conf_close_bdev(struct blkg_conf_ctx *ctx)
957 {
958 	mutex_unlock(&ctx->bdev->bd_queue->rq_qos_mutex);
959 	blkdev_put_no_open(ctx->bdev);
960 	ctx->body = NULL;
961 	ctx->bdev = NULL;
962 }
963 EXPORT_SYMBOL_GPL(blkg_conf_close_bdev);
964 
965 static void blkg_iostat_add(struct blkg_iostat *dst, struct blkg_iostat *src)
966 {
967 	int i;
968 
969 	for (i = 0; i < BLKG_IOSTAT_NR; i++) {
970 		dst->bytes[i] += src->bytes[i];
971 		dst->ios[i] += src->ios[i];
972 	}
973 }
974 
975 static void blkg_iostat_sub(struct blkg_iostat *dst, struct blkg_iostat *src)
976 {
977 	int i;
978 
979 	for (i = 0; i < BLKG_IOSTAT_NR; i++) {
980 		dst->bytes[i] -= src->bytes[i];
981 		dst->ios[i] -= src->ios[i];
982 	}
983 }
984 
985 static void blkcg_iostat_update(struct blkcg_gq *blkg, struct blkg_iostat *cur,
986 				struct blkg_iostat *last)
987 {
988 	struct blkg_iostat delta;
989 	unsigned long flags;
990 
991 	/* propagate percpu delta to global */
992 	flags = u64_stats_update_begin_irqsave(&blkg->iostat.sync);
993 	blkg_iostat_set(&delta, cur);
994 	blkg_iostat_sub(&delta, last);
995 	blkg_iostat_add(&blkg->iostat.cur, &delta);
996 	blkg_iostat_add(last, &delta);
997 	u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags);
998 }
999 
1000 static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu)
1001 {
1002 	struct llist_head *lhead = per_cpu_ptr(blkcg->lhead, cpu);
1003 	struct llist_node *lnode;
1004 	struct blkg_iostat_set *bisc, *next_bisc;
1005 	unsigned long flags;
1006 
1007 	rcu_read_lock();
1008 
1009 	lnode = llist_del_all(lhead);
1010 	if (!lnode)
1011 		goto out;
1012 
1013 	/*
1014 	 * For covering concurrent parent blkg update from blkg_release().
1015 	 *
1016 	 * When flushing from cgroup, the subsystem rstat lock is always held,
1017 	 * so this lock won't cause contention most of time.
1018 	 */
1019 	raw_spin_lock_irqsave(&blkg_stat_lock, flags);
1020 
1021 	/*
1022 	 * Iterate only the iostat_cpu's queued in the lockless list.
1023 	 */
1024 	llist_for_each_entry_safe(bisc, next_bisc, lnode, lnode) {
1025 		struct blkcg_gq *blkg = bisc->blkg;
1026 		struct blkcg_gq *parent = blkg->parent;
1027 		struct blkg_iostat cur;
1028 		unsigned int seq;
1029 
1030 		/*
1031 		 * Order assignment of `next_bisc` from `bisc->lnode.next` in
1032 		 * llist_for_each_entry_safe and clearing `bisc->lqueued` for
1033 		 * avoiding to assign `next_bisc` with new next pointer added
1034 		 * in blk_cgroup_bio_start() in case of re-ordering.
1035 		 *
1036 		 * The pair barrier is implied in llist_add() in blk_cgroup_bio_start().
1037 		 */
1038 		smp_mb();
1039 
1040 		WRITE_ONCE(bisc->lqueued, false);
1041 		if (bisc == &blkg->iostat)
1042 			goto propagate_up; /* propagate up to parent only */
1043 
1044 		/* fetch the current per-cpu values */
1045 		do {
1046 			seq = u64_stats_fetch_begin(&bisc->sync);
1047 			blkg_iostat_set(&cur, &bisc->cur);
1048 		} while (u64_stats_fetch_retry(&bisc->sync, seq));
1049 
1050 		blkcg_iostat_update(blkg, &cur, &bisc->last);
1051 
1052 propagate_up:
1053 		/* propagate global delta to parent (unless that's root) */
1054 		if (parent && parent->parent) {
1055 			blkcg_iostat_update(parent, &blkg->iostat.cur,
1056 					    &blkg->iostat.last);
1057 			/*
1058 			 * Queue parent->iostat to its blkcg's lockless
1059 			 * list to propagate up to the grandparent if the
1060 			 * iostat hasn't been queued yet.
1061 			 */
1062 			if (!parent->iostat.lqueued) {
1063 				struct llist_head *plhead;
1064 
1065 				plhead = per_cpu_ptr(parent->blkcg->lhead, cpu);
1066 				llist_add(&parent->iostat.lnode, plhead);
1067 				parent->iostat.lqueued = true;
1068 			}
1069 		}
1070 	}
1071 	raw_spin_unlock_irqrestore(&blkg_stat_lock, flags);
1072 out:
1073 	rcu_read_unlock();
1074 }
1075 
1076 static void blkcg_rstat_flush(struct cgroup_subsys_state *css, int cpu)
1077 {
1078 	/* Root-level stats are sourced from system-wide IO stats */
1079 	if (cgroup_parent(css->cgroup))
1080 		__blkcg_rstat_flush(css_to_blkcg(css), cpu);
1081 }
1082 
1083 /*
1084  * We source root cgroup stats from the system-wide stats to avoid
1085  * tracking the same information twice and incurring overhead when no
1086  * cgroups are defined. For that reason, css_rstat_flush in
1087  * blkcg_print_stat does not actually fill out the iostat in the root
1088  * cgroup's blkcg_gq.
1089  *
1090  * However, we would like to re-use the printing code between the root and
1091  * non-root cgroups to the extent possible. For that reason, we simulate
1092  * flushing the root cgroup's stats by explicitly filling in the iostat
1093  * with disk level statistics.
1094  */
1095 static void blkcg_fill_root_iostats(void)
1096 {
1097 	struct class_dev_iter iter;
1098 	struct device *dev;
1099 
1100 	class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1101 	while ((dev = class_dev_iter_next(&iter))) {
1102 		struct block_device *bdev = dev_to_bdev(dev);
1103 		struct blkcg_gq *blkg = bdev->bd_disk->queue->root_blkg;
1104 		struct blkg_iostat tmp;
1105 		int cpu;
1106 		unsigned long flags;
1107 
1108 		memset(&tmp, 0, sizeof(tmp));
1109 		for_each_possible_cpu(cpu) {
1110 			struct disk_stats *cpu_dkstats;
1111 
1112 			cpu_dkstats = per_cpu_ptr(bdev->bd_stats, cpu);
1113 			tmp.ios[BLKG_IOSTAT_READ] +=
1114 				cpu_dkstats->ios[STAT_READ];
1115 			tmp.ios[BLKG_IOSTAT_WRITE] +=
1116 				cpu_dkstats->ios[STAT_WRITE];
1117 			tmp.ios[BLKG_IOSTAT_DISCARD] +=
1118 				cpu_dkstats->ios[STAT_DISCARD];
1119 			// convert sectors to bytes
1120 			tmp.bytes[BLKG_IOSTAT_READ] +=
1121 				cpu_dkstats->sectors[STAT_READ] << 9;
1122 			tmp.bytes[BLKG_IOSTAT_WRITE] +=
1123 				cpu_dkstats->sectors[STAT_WRITE] << 9;
1124 			tmp.bytes[BLKG_IOSTAT_DISCARD] +=
1125 				cpu_dkstats->sectors[STAT_DISCARD] << 9;
1126 		}
1127 
1128 		flags = u64_stats_update_begin_irqsave(&blkg->iostat.sync);
1129 		blkg_iostat_set(&blkg->iostat.cur, &tmp);
1130 		u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags);
1131 	}
1132 	class_dev_iter_exit(&iter);
1133 }
1134 
1135 static void blkcg_print_one_stat(struct blkcg_gq *blkg, struct seq_file *s)
1136 {
1137 	struct blkg_iostat_set *bis = &blkg->iostat;
1138 	u64 rbytes, wbytes, rios, wios, dbytes, dios;
1139 	const char *dname;
1140 	unsigned seq;
1141 	int i;
1142 
1143 	if (!blkg->online)
1144 		return;
1145 
1146 	dname = blkg_dev_name(blkg);
1147 	if (!dname)
1148 		return;
1149 
1150 	seq_printf(s, "%s ", dname);
1151 
1152 	do {
1153 		seq = u64_stats_fetch_begin(&bis->sync);
1154 
1155 		rbytes = bis->cur.bytes[BLKG_IOSTAT_READ];
1156 		wbytes = bis->cur.bytes[BLKG_IOSTAT_WRITE];
1157 		dbytes = bis->cur.bytes[BLKG_IOSTAT_DISCARD];
1158 		rios = bis->cur.ios[BLKG_IOSTAT_READ];
1159 		wios = bis->cur.ios[BLKG_IOSTAT_WRITE];
1160 		dios = bis->cur.ios[BLKG_IOSTAT_DISCARD];
1161 	} while (u64_stats_fetch_retry(&bis->sync, seq));
1162 
1163 	if (rbytes || wbytes || rios || wios) {
1164 		seq_printf(s, "rbytes=%llu wbytes=%llu rios=%llu wios=%llu dbytes=%llu dios=%llu",
1165 			rbytes, wbytes, rios, wios,
1166 			dbytes, dios);
1167 	}
1168 
1169 	if (blkcg_debug_stats && atomic_read(&blkg->use_delay)) {
1170 		seq_printf(s, " use_delay=%d delay_nsec=%llu",
1171 			atomic_read(&blkg->use_delay),
1172 			atomic64_read(&blkg->delay_nsec));
1173 	}
1174 
1175 	for (i = 0; i < BLKCG_MAX_POLS; i++) {
1176 		struct blkcg_policy *pol = blkcg_policy[i];
1177 
1178 		if (!blkg->pd[i] || !pol->pd_stat_fn)
1179 			continue;
1180 
1181 		pol->pd_stat_fn(blkg->pd[i], s);
1182 	}
1183 
1184 	seq_puts(s, "\n");
1185 }
1186 
1187 static int blkcg_print_stat(struct seq_file *sf, void *v)
1188 {
1189 	struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1190 	struct blkcg_gq *blkg;
1191 
1192 	if (!seq_css(sf)->parent) {
1193 		blkcg_fill_root_iostats();
1194 	} else {
1195 		/*
1196 		 * Descendant blkgs imply ancestor blkgs, so an empty
1197 		 * ->blkg_list has no stats to report.
1198 		 */
1199 		if (hlist_empty(&blkcg->blkg_list))
1200 			return 0;
1201 		css_rstat_flush(&blkcg->css);
1202 	}
1203 
1204 	guard(spinlock_irq)(&blkcg->lock);
1205 	hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node)
1206 		blkcg_print_one_stat(blkg, sf);
1207 
1208 	return 0;
1209 }
1210 
1211 static struct cftype blkcg_files[] = {
1212 	{
1213 		.name = "stat",
1214 		.seq_show = blkcg_print_stat,
1215 	},
1216 	{ }	/* terminate */
1217 };
1218 
1219 static struct cftype blkcg_legacy_files[] = {
1220 	{
1221 		.name = "reset_stats",
1222 		.write_u64 = blkcg_reset_stats,
1223 	},
1224 	{ }	/* terminate */
1225 };
1226 
1227 #ifdef CONFIG_CGROUP_WRITEBACK
1228 struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css)
1229 {
1230 	return &css_to_blkcg(css)->cgwb_list;
1231 }
1232 #endif
1233 
1234 /*
1235  * blkcg destruction is a three-stage process.
1236  *
1237  * 1. Destruction starts.  The blkcg_css_offline() callback is invoked
1238  *    which offlines writeback.  Here we tie the next stage of blkg destruction
1239  *    to the completion of writeback associated with the blkcg.  This lets us
1240  *    avoid punting potentially large amounts of outstanding writeback to root
1241  *    while maintaining any ongoing policies.  The next stage is triggered when
1242  *    the nr_cgwbs count goes to zero.
1243  *
1244  * 2. When the nr_cgwbs count goes to zero, blkcg_destroy_blkgs() is called
1245  *    and handles the destruction of blkgs.  Here the css reference held by
1246  *    the blkg is put back eventually allowing blkcg_css_free() to be called.
1247  *    This work may occur in cgwb_release_workfn() on the cgwb_release
1248  *    workqueue.  Any submitted ios that fail to get the blkg ref will be
1249  *    punted to the root_blkg.
1250  *
1251  * 3. Once the blkcg ref count goes to zero, blkcg_css_free() is called.
1252  *    This finally frees the blkcg.
1253  */
1254 
1255 static struct blkcg_gq *blkcg_get_first_blkg(struct blkcg *blkcg)
1256 {
1257 	struct blkcg_gq *blkg = NULL;
1258 
1259 	spin_lock_irq(&blkcg->lock);
1260 	if (!hlist_empty(&blkcg->blkg_list)) {
1261 		blkg = hlist_entry(blkcg->blkg_list.first, struct blkcg_gq,
1262 				   blkcg_node);
1263 		blkg_get(blkg);
1264 	}
1265 	spin_unlock_irq(&blkcg->lock);
1266 
1267 	return blkg;
1268 }
1269 
1270 /**
1271  * blkcg_destroy_blkgs - responsible for shooting down blkgs
1272  * @blkcg: blkcg of interest
1273  *
1274  * blkgs should be removed while holding both q and blkcg locks.  As blkcg lock
1275  * is nested inside q lock, this function performs reverse double lock dancing.
1276  * Destroying the blkgs releases the reference held on the blkcg's css allowing
1277  * blkcg_css_free to eventually be called.
1278  *
1279  * This is the blkcg counterpart of ioc_release_fn().
1280  */
1281 static void blkcg_destroy_blkgs(struct blkcg *blkcg)
1282 {
1283 	struct blkcg_gq *blkg;
1284 
1285 	might_sleep();
1286 
1287 	while ((blkg = blkcg_get_first_blkg(blkcg))) {
1288 		struct request_queue *q = blkg->q;
1289 
1290 		spin_lock_irq(&q->queue_lock);
1291 		spin_lock(&blkcg->lock);
1292 
1293 		blkg_destroy(blkg);
1294 
1295 		spin_unlock(&blkcg->lock);
1296 		spin_unlock_irq(&q->queue_lock);
1297 
1298 		blkg_put(blkg);
1299 		cond_resched();
1300 	}
1301 }
1302 
1303 /**
1304  * blkcg_pin_online - pin online state
1305  * @blkcg_css: blkcg of interest
1306  *
1307  * While pinned, a blkcg is kept online.  This is primarily used to
1308  * impedance-match blkg and cgwb lifetimes so that blkg doesn't go offline
1309  * while an associated cgwb is still active.
1310  */
1311 void blkcg_pin_online(struct cgroup_subsys_state *blkcg_css)
1312 {
1313 	refcount_inc(&css_to_blkcg(blkcg_css)->online_pin);
1314 }
1315 
1316 /**
1317  * blkcg_unpin_online - unpin online state
1318  * @blkcg_css: blkcg of interest
1319  *
1320  * This is primarily used to impedance-match blkg and cgwb lifetimes so
1321  * that blkg doesn't go offline while an associated cgwb is still active.
1322  * When this count goes to zero, all active cgwbs have finished so the
1323  * blkcg can continue destruction by calling blkcg_destroy_blkgs().
1324  */
1325 void blkcg_unpin_online(struct cgroup_subsys_state *blkcg_css)
1326 {
1327 	struct blkcg *blkcg = css_to_blkcg(blkcg_css);
1328 
1329 	do {
1330 		struct blkcg *parent;
1331 
1332 		if (!refcount_dec_and_test(&blkcg->online_pin))
1333 			break;
1334 
1335 		parent = blkcg_parent(blkcg);
1336 		blkcg_destroy_blkgs(blkcg);
1337 		blkcg = parent;
1338 	} while (blkcg);
1339 }
1340 
1341 /**
1342  * blkcg_css_offline - cgroup css_offline callback
1343  * @css: css of interest
1344  *
1345  * This function is called when @css is about to go away.  Here the cgwbs are
1346  * offlined first and only once writeback associated with the blkcg has
1347  * finished do we start step 2 (see above).
1348  */
1349 static void blkcg_css_offline(struct cgroup_subsys_state *css)
1350 {
1351 	/* this prevents anyone from attaching or migrating to this blkcg */
1352 	wb_blkcg_offline(css);
1353 
1354 	/* put the base online pin allowing step 2 to be triggered */
1355 	blkcg_unpin_online(css);
1356 }
1357 
1358 static void blkcg_css_free(struct cgroup_subsys_state *css)
1359 {
1360 	struct blkcg *blkcg = css_to_blkcg(css);
1361 	int i;
1362 
1363 	/*
1364 	 * Every blkg holds a reference on this css and drops any delay it
1365 	 * still has from pd_free_fn(), so this is expected to be zero.  Should
1366 	 * a policy ever leave one behind, drop it here rather than let it pin
1367 	 * blkcg_nr_congested and disable the fast path for the rest of the
1368 	 * boot.  Nothing can race with us at this point.
1369 	 */
1370 	if (WARN_ON_ONCE(atomic_xchg(&blkcg->congestion_count, 0) > 0))
1371 		atomic_dec(&blkcg_nr_congested);
1372 
1373 	mutex_lock(&blkcg_pol_mutex);
1374 
1375 	list_del(&blkcg->all_blkcgs_node);
1376 
1377 	for (i = 0; i < BLKCG_MAX_POLS; i++)
1378 		if (blkcg->cpd[i])
1379 			blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
1380 
1381 	mutex_unlock(&blkcg_pol_mutex);
1382 
1383 	free_percpu(blkcg->lhead);
1384 	kfree(blkcg);
1385 }
1386 
1387 static struct cgroup_subsys_state *
1388 blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
1389 {
1390 	struct blkcg *blkcg;
1391 	int i;
1392 
1393 	mutex_lock(&blkcg_pol_mutex);
1394 
1395 	if (!parent_css) {
1396 		blkcg = &blkcg_root;
1397 	} else {
1398 		blkcg = kzalloc_obj(*blkcg);
1399 		if (!blkcg)
1400 			goto unlock;
1401 	}
1402 
1403 	if (init_blkcg_llists(blkcg))
1404 		goto free_blkcg;
1405 
1406 	for (i = 0; i < BLKCG_MAX_POLS ; i++) {
1407 		struct blkcg_policy *pol = blkcg_policy[i];
1408 		struct blkcg_policy_data *cpd;
1409 
1410 		/*
1411 		 * If the policy hasn't been attached yet, wait for it
1412 		 * to be attached before doing anything else. Otherwise,
1413 		 * check if the policy requires any specific per-cgroup
1414 		 * data: if it does, allocate and initialize it.
1415 		 */
1416 		if (!pol || !pol->cpd_alloc_fn)
1417 			continue;
1418 
1419 		cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1420 		if (!cpd)
1421 			goto free_pd_blkcg;
1422 
1423 		blkcg->cpd[i] = cpd;
1424 		cpd->blkcg = blkcg;
1425 		cpd->plid = i;
1426 	}
1427 
1428 	spin_lock_init(&blkcg->lock);
1429 	refcount_set(&blkcg->online_pin, 1);
1430 	INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT);
1431 	INIT_HLIST_HEAD(&blkcg->blkg_list);
1432 #ifdef CONFIG_CGROUP_WRITEBACK
1433 	INIT_LIST_HEAD(&blkcg->cgwb_list);
1434 #endif
1435 	list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs);
1436 
1437 	mutex_unlock(&blkcg_pol_mutex);
1438 	return &blkcg->css;
1439 
1440 free_pd_blkcg:
1441 	for (i--; i >= 0; i--)
1442 		if (blkcg->cpd[i])
1443 			blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
1444 	free_percpu(blkcg->lhead);
1445 free_blkcg:
1446 	if (blkcg != &blkcg_root)
1447 		kfree(blkcg);
1448 unlock:
1449 	mutex_unlock(&blkcg_pol_mutex);
1450 	return ERR_PTR(-ENOMEM);
1451 }
1452 
1453 static int blkcg_css_online(struct cgroup_subsys_state *css)
1454 {
1455 	struct blkcg *parent = blkcg_parent(css_to_blkcg(css));
1456 
1457 	/*
1458 	 * blkcg_pin_online() is used to delay blkcg offline so that blkgs
1459 	 * don't go offline while cgwbs are still active on them.  Pin the
1460 	 * parent so that offline always happens towards the root.
1461 	 */
1462 	if (parent)
1463 		blkcg_pin_online(&parent->css);
1464 	return 0;
1465 }
1466 
1467 void blkg_init_queue(struct request_queue *q)
1468 {
1469 	INIT_LIST_HEAD(&q->blkg_list);
1470 	mutex_init(&q->blkcg_mutex);
1471 }
1472 
1473 int blkcg_init_disk(struct gendisk *disk)
1474 {
1475 	struct request_queue *q = disk->queue;
1476 	struct blkcg_gq *new_blkg, *blkg;
1477 	bool preloaded;
1478 
1479 	/*
1480 	 * If the queue is shared across disk rebind (e.g., SCSI), the
1481 	 * previous disk's blkcg state is cleaned up asynchronously via
1482 	 * disk_release() -> blkcg_exit_disk(). Wait for that cleanup to
1483 	 * finish (indicated by root_blkg becoming NULL) before setting up
1484 	 * new blkcg state. Otherwise, we may overwrite q->root_blkg while
1485 	 * the old one is still alive, and radix_tree_insert() in
1486 	 * blkg_create() will fail with -EEXIST because the old entries
1487 	 * still occupy the same queue id slot in blkcg->blkg_tree.
1488 	 */
1489 	wait_var_event(&q->root_blkg, !READ_ONCE(q->root_blkg));
1490 
1491 	new_blkg = blkg_alloc(&blkcg_root, disk, GFP_KERNEL);
1492 	if (!new_blkg)
1493 		return -ENOMEM;
1494 
1495 	preloaded = !radix_tree_preload(GFP_KERNEL);
1496 
1497 	/* Make sure the root blkg exists. */
1498 	/* spin_lock_irq can serve as RCU read-side critical section. */
1499 	spin_lock_irq(&q->queue_lock);
1500 	blkg = blkg_create(&blkcg_root, disk, new_blkg);
1501 	if (IS_ERR(blkg))
1502 		goto err_unlock;
1503 	q->root_blkg = blkg;
1504 	spin_unlock_irq(&q->queue_lock);
1505 
1506 	if (preloaded)
1507 		radix_tree_preload_end();
1508 
1509 	return 0;
1510 
1511 err_unlock:
1512 	spin_unlock_irq(&q->queue_lock);
1513 	if (preloaded)
1514 		radix_tree_preload_end();
1515 	return PTR_ERR(blkg);
1516 }
1517 
1518 void blkcg_exit_disk(struct gendisk *disk)
1519 {
1520 	blkg_destroy_all(disk);
1521 	blk_throtl_exit(disk);
1522 }
1523 
1524 static void blkcg_exit(struct task_struct *tsk)
1525 {
1526 	if (tsk->throttle_disk)
1527 		put_disk(tsk->throttle_disk);
1528 	tsk->throttle_disk = NULL;
1529 }
1530 
1531 struct cgroup_subsys io_cgrp_subsys = {
1532 	.css_alloc = blkcg_css_alloc,
1533 	.css_online = blkcg_css_online,
1534 	.css_offline = blkcg_css_offline,
1535 	.css_free = blkcg_css_free,
1536 	.css_rstat_flush = blkcg_rstat_flush,
1537 	.dfl_cftypes = blkcg_files,
1538 	.legacy_cftypes = blkcg_legacy_files,
1539 	.legacy_name = "blkio",
1540 	.exit = blkcg_exit,
1541 #ifdef CONFIG_MEMCG
1542 	/*
1543 	 * This ensures that, if available, memcg is automatically enabled
1544 	 * together on the default hierarchy so that the owner cgroup can
1545 	 * be retrieved from writeback pages.
1546 	 */
1547 	.depends_on = 1 << memory_cgrp_id,
1548 #endif
1549 };
1550 EXPORT_SYMBOL_GPL(io_cgrp_subsys);
1551 
1552 /*
1553  * Tear down per-blkg policy data for @pol on @q.
1554  */
1555 static void blkcg_policy_teardown_pds(struct request_queue *q,
1556 				      const struct blkcg_policy *pol)
1557 {
1558 	struct blkcg_gq *blkg;
1559 
1560 	list_for_each_entry(blkg, &q->blkg_list, q_node) {
1561 		struct blkcg *blkcg = blkg->blkcg;
1562 		struct blkg_policy_data *pd;
1563 
1564 		spin_lock(&blkcg->lock);
1565 		pd = blkg->pd[pol->plid];
1566 		if (pd) {
1567 			if (pd->online && pol->pd_offline_fn)
1568 				pol->pd_offline_fn(pd);
1569 			pd->online = false;
1570 			pol->pd_free_fn(pd);
1571 			WRITE_ONCE(blkg->pd[pol->plid], NULL);
1572 		}
1573 		spin_unlock(&blkcg->lock);
1574 	}
1575 }
1576 
1577 /**
1578  * blkcg_activate_policy - activate a blkcg policy on a gendisk
1579  * @disk: gendisk of interest
1580  * @pol: blkcg policy to activate
1581  *
1582  * Activate @pol on @disk.  Requires %GFP_KERNEL context.  @disk goes through
1583  * bypass mode to populate its blkgs with policy_data for @pol.
1584  *
1585  * Activation happens with @disk bypassed, so nobody would be accessing blkgs
1586  * from IO path.  Update of each blkg is protected by both queue and blkcg
1587  * locks so that holding either lock and testing blkcg_policy_enabled() is
1588  * always enough for dereferencing policy data.
1589  *
1590  * The caller is responsible for synchronizing [de]activations and policy
1591  * [un]registerations.  Returns 0 on success, -errno on failure.
1592  */
1593 int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)
1594 {
1595 	struct request_queue *q = disk->queue;
1596 	struct blkg_policy_data *pd_prealloc = NULL;
1597 	struct blkcg_gq *blkg, *pinned_blkg = NULL;
1598 	unsigned int memflags;
1599 	int ret;
1600 
1601 	if (blkcg_policy_enabled(q, pol))
1602 		return 0;
1603 
1604 	/*
1605 	 * Policy is allowed to be registered without pd_alloc_fn/pd_free_fn,
1606 	 * for example, ioprio. Such policy will work on blkcg level, not disk
1607 	 * level, and don't need to be activated.
1608 	 */
1609 	if (WARN_ON_ONCE(!pol->pd_alloc_fn || !pol->pd_free_fn))
1610 		return -EINVAL;
1611 
1612 	if (queue_is_mq(q))
1613 		memflags = blk_mq_freeze_queue(q);
1614 
1615 	mutex_lock(&q->blkcg_mutex);
1616 retry:
1617 	spin_lock_irq(&q->queue_lock);
1618 
1619 	/* blkg_list is pushed at the head, reverse walk to initialize parents first */
1620 	list_for_each_entry_reverse(blkg, &q->blkg_list, q_node) {
1621 		struct blkg_policy_data *pd;
1622 
1623 		if (blkg->pd[pol->plid])
1624 			continue;
1625 		if (hlist_unhashed(&blkg->blkcg_node))
1626 			continue;
1627 
1628 		/* If prealloc matches, use it; otherwise try GFP_NOWAIT */
1629 		if (blkg == pinned_blkg) {
1630 			pd = pd_prealloc;
1631 			pd_prealloc = NULL;
1632 		} else {
1633 			pd = pol->pd_alloc_fn(disk, blkg->blkcg,
1634 					      GFP_NOWAIT);
1635 		}
1636 
1637 		if (!pd) {
1638 			/*
1639 			 * GFP_NOWAIT failed.  Free the existing one and
1640 			 * prealloc for @blkg w/ GFP_KERNEL.
1641 			 */
1642 			if (pinned_blkg)
1643 				blkg_put(pinned_blkg);
1644 			blkg_get(blkg);
1645 			pinned_blkg = blkg;
1646 
1647 			spin_unlock_irq(&q->queue_lock);
1648 
1649 			if (pd_prealloc)
1650 				pol->pd_free_fn(pd_prealloc);
1651 			pd_prealloc = pol->pd_alloc_fn(disk, blkg->blkcg,
1652 						       GFP_KERNEL);
1653 			if (pd_prealloc)
1654 				goto retry;
1655 			else
1656 				goto enomem;
1657 		}
1658 
1659 		spin_lock(&blkg->blkcg->lock);
1660 
1661 		pd->blkg = blkg;
1662 		pd->plid = pol->plid;
1663 		WRITE_ONCE(blkg->pd[pol->plid], pd);
1664 
1665 		if (pol->pd_init_fn)
1666 			pol->pd_init_fn(pd);
1667 
1668 		if (pol->pd_online_fn)
1669 			pol->pd_online_fn(pd);
1670 		pd->online = true;
1671 
1672 		spin_unlock(&blkg->blkcg->lock);
1673 	}
1674 
1675 	__set_bit(pol->plid, q->blkcg_pols);
1676 	ret = 0;
1677 
1678 	spin_unlock_irq(&q->queue_lock);
1679 out:
1680 	mutex_unlock(&q->blkcg_mutex);
1681 	if (queue_is_mq(q))
1682 		blk_mq_unfreeze_queue(q, memflags);
1683 	if (pinned_blkg)
1684 		blkg_put(pinned_blkg);
1685 	if (pd_prealloc)
1686 		pol->pd_free_fn(pd_prealloc);
1687 	return ret;
1688 
1689 enomem:
1690 	/* alloc failed, take down everything */
1691 	spin_lock_irq(&q->queue_lock);
1692 	blkcg_policy_teardown_pds(q, pol);
1693 	spin_unlock_irq(&q->queue_lock);
1694 	ret = -ENOMEM;
1695 	goto out;
1696 }
1697 EXPORT_SYMBOL_GPL(blkcg_activate_policy);
1698 
1699 /**
1700  * blkcg_deactivate_policy - deactivate a blkcg policy on a gendisk
1701  * @disk: gendisk of interest
1702  * @pol: blkcg policy to deactivate
1703  *
1704  * Deactivate @pol on @disk.  Follows the same synchronization rules as
1705  * blkcg_activate_policy().
1706  */
1707 void blkcg_deactivate_policy(struct gendisk *disk,
1708 			     const struct blkcg_policy *pol)
1709 {
1710 	struct request_queue *q = disk->queue;
1711 	unsigned int memflags;
1712 
1713 	if (!blkcg_policy_enabled(q, pol))
1714 		return;
1715 
1716 	if (queue_is_mq(q))
1717 		memflags = blk_mq_freeze_queue(q);
1718 
1719 	mutex_lock(&q->blkcg_mutex);
1720 	spin_lock_irq(&q->queue_lock);
1721 
1722 	__clear_bit(pol->plid, q->blkcg_pols);
1723 	blkcg_policy_teardown_pds(q, pol);
1724 	spin_unlock_irq(&q->queue_lock);
1725 	mutex_unlock(&q->blkcg_mutex);
1726 
1727 	if (queue_is_mq(q))
1728 		blk_mq_unfreeze_queue(q, memflags);
1729 }
1730 EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
1731 
1732 static void blkcg_free_all_cpd(struct blkcg_policy *pol)
1733 {
1734 	struct blkcg *blkcg;
1735 
1736 	list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
1737 		if (blkcg->cpd[pol->plid]) {
1738 			pol->cpd_free_fn(blkcg->cpd[pol->plid]);
1739 			blkcg->cpd[pol->plid] = NULL;
1740 		}
1741 	}
1742 }
1743 
1744 /**
1745  * blkcg_policy_register - register a blkcg policy
1746  * @pol: blkcg policy to register
1747  *
1748  * Register @pol with blkcg core.  Might sleep and @pol may be modified on
1749  * successful registration.  Returns 0 on success and -errno on failure.
1750  */
1751 int blkcg_policy_register(struct blkcg_policy *pol)
1752 {
1753 	struct blkcg *blkcg;
1754 	int i, ret;
1755 
1756 	/*
1757 	 * Make sure cpd/pd_alloc_fn and cpd/pd_free_fn in pairs, and policy
1758 	 * without pd_alloc_fn/pd_free_fn can't be activated.
1759 	 */
1760 	if ((!pol->cpd_alloc_fn ^ !pol->cpd_free_fn) ||
1761 	    (!pol->pd_alloc_fn ^ !pol->pd_free_fn))
1762 		return -EINVAL;
1763 
1764 	mutex_lock(&blkcg_pol_register_mutex);
1765 	mutex_lock(&blkcg_pol_mutex);
1766 
1767 	/* find an empty slot */
1768 	for (i = 0; i < BLKCG_MAX_POLS; i++)
1769 		if (!blkcg_policy[i])
1770 			break;
1771 	if (i >= BLKCG_MAX_POLS) {
1772 		pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n");
1773 		ret = -ENOSPC;
1774 		goto err_unlock;
1775 	}
1776 
1777 	/* register @pol */
1778 	pol->plid = i;
1779 	blkcg_policy[pol->plid] = pol;
1780 
1781 	/* allocate and install cpd's */
1782 	if (pol->cpd_alloc_fn) {
1783 		list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
1784 			struct blkcg_policy_data *cpd;
1785 
1786 			cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1787 			if (!cpd) {
1788 				ret = -ENOMEM;
1789 				goto err_free_cpds;
1790 			}
1791 
1792 			blkcg->cpd[pol->plid] = cpd;
1793 			cpd->blkcg = blkcg;
1794 			cpd->plid = pol->plid;
1795 		}
1796 	}
1797 
1798 	mutex_unlock(&blkcg_pol_mutex);
1799 
1800 	/* everything is in place, add intf files for the new policy */
1801 	if (pol->dfl_cftypes == pol->legacy_cftypes) {
1802 		WARN_ON(cgroup_add_cftypes(&io_cgrp_subsys,
1803 					   pol->dfl_cftypes));
1804 	} else {
1805 		WARN_ON(cgroup_add_dfl_cftypes(&io_cgrp_subsys,
1806 					       pol->dfl_cftypes));
1807 		WARN_ON(cgroup_add_legacy_cftypes(&io_cgrp_subsys,
1808 						  pol->legacy_cftypes));
1809 	}
1810 	mutex_unlock(&blkcg_pol_register_mutex);
1811 	return 0;
1812 
1813 err_free_cpds:
1814 	if (pol->cpd_free_fn)
1815 		blkcg_free_all_cpd(pol);
1816 
1817 	blkcg_policy[pol->plid] = NULL;
1818 err_unlock:
1819 	mutex_unlock(&blkcg_pol_mutex);
1820 	mutex_unlock(&blkcg_pol_register_mutex);
1821 	return ret;
1822 }
1823 EXPORT_SYMBOL_GPL(blkcg_policy_register);
1824 
1825 /**
1826  * blkcg_policy_unregister - unregister a blkcg policy
1827  * @pol: blkcg policy to unregister
1828  *
1829  * Undo blkcg_policy_register(@pol).  Might sleep.
1830  */
1831 void blkcg_policy_unregister(struct blkcg_policy *pol)
1832 {
1833 	mutex_lock(&blkcg_pol_register_mutex);
1834 
1835 	if (WARN_ON(blkcg_policy[pol->plid] != pol))
1836 		goto out_unlock;
1837 
1838 	/* kill the intf files first */
1839 	if (pol->dfl_cftypes)
1840 		cgroup_rm_cftypes(pol->dfl_cftypes);
1841 	if (pol->legacy_cftypes)
1842 		cgroup_rm_cftypes(pol->legacy_cftypes);
1843 
1844 	/* remove cpds and unregister */
1845 	mutex_lock(&blkcg_pol_mutex);
1846 
1847 	if (pol->cpd_free_fn)
1848 		blkcg_free_all_cpd(pol);
1849 
1850 	blkcg_policy[pol->plid] = NULL;
1851 
1852 	mutex_unlock(&blkcg_pol_mutex);
1853 out_unlock:
1854 	mutex_unlock(&blkcg_pol_register_mutex);
1855 }
1856 EXPORT_SYMBOL_GPL(blkcg_policy_unregister);
1857 
1858 /*
1859  * Scale the accumulated delay based on how long it has been since we updated
1860  * the delay.  We only call this when we are adding delay, in case it's been a
1861  * while since we added delay, and when we are checking to see if we need to
1862  * delay a task, to account for any delays that may have occurred.
1863  */
1864 static void blkcg_scale_delay(struct blkcg_gq *blkg, u64 now)
1865 {
1866 	u64 old = atomic64_read(&blkg->delay_start);
1867 
1868 	/* negative use_delay means no scaling, see blkcg_set_delay() */
1869 	if (atomic_read(&blkg->use_delay) < 0)
1870 		return;
1871 
1872 	/*
1873 	 * We only want to scale down every second.  The idea here is that we
1874 	 * want to delay people for min(delay_nsec, NSEC_PER_SEC) in a certain
1875 	 * time window.  We only want to throttle tasks for recent delay that
1876 	 * has occurred, in 1 second time windows since that's the maximum
1877 	 * things can be throttled.  We save the current delay window in
1878 	 * blkg->last_delay so we know what amount is still left to be charged
1879 	 * to the blkg from this point onward.  blkg->last_use keeps track of
1880 	 * the use_delay counter.  The idea is if we're unthrottling the blkg we
1881 	 * are ok with whatever is happening now, and we can take away more of
1882 	 * the accumulated delay as we've already throttled enough that
1883 	 * everybody is happy with their IO latencies.
1884 	 */
1885 	if (time_before64(old + NSEC_PER_SEC, now) &&
1886 	    atomic64_try_cmpxchg(&blkg->delay_start, &old, now)) {
1887 		u64 cur = atomic64_read(&blkg->delay_nsec);
1888 		u64 sub = min_t(u64, blkg->last_delay, now - old);
1889 		int cur_use = atomic_read(&blkg->use_delay);
1890 
1891 		/*
1892 		 * We've been unthrottled, subtract a larger chunk of our
1893 		 * accumulated delay.
1894 		 */
1895 		if (cur_use < blkg->last_use)
1896 			sub = max_t(u64, sub, blkg->last_delay >> 1);
1897 
1898 		/*
1899 		 * This shouldn't happen, but handle it anyway.  Our delay_nsec
1900 		 * should only ever be growing except here where we subtract out
1901 		 * min(last_delay, 1 second), but lord knows bugs happen and I'd
1902 		 * rather not end up with negative numbers.
1903 		 */
1904 		if (unlikely(cur < sub)) {
1905 			atomic64_set(&blkg->delay_nsec, 0);
1906 			blkg->last_delay = 0;
1907 		} else {
1908 			atomic64_sub(sub, &blkg->delay_nsec);
1909 			blkg->last_delay = cur - sub;
1910 		}
1911 		blkg->last_use = cur_use;
1912 	}
1913 }
1914 
1915 /*
1916  * This is called when we want to actually walk up the hierarchy and check to
1917  * see if we need to throttle, and then actually throttle if there is some
1918  * accumulated delay.  This should only be called upon return to user space so
1919  * we're not holding some lock that would induce a priority inversion.
1920  */
1921 static void blkcg_maybe_throttle_blkg(struct blkcg_gq *blkg, bool use_memdelay)
1922 {
1923 	unsigned long pflags;
1924 	bool clamp;
1925 	u64 now = blk_time_get_ns();
1926 	u64 exp;
1927 	u64 delay_nsec = 0;
1928 	int tok;
1929 
1930 	while (blkg->parent) {
1931 		int use_delay = atomic_read(&blkg->use_delay);
1932 
1933 		if (use_delay) {
1934 			u64 this_delay;
1935 
1936 			blkcg_scale_delay(blkg, now);
1937 			this_delay = atomic64_read(&blkg->delay_nsec);
1938 			if (this_delay > delay_nsec) {
1939 				delay_nsec = this_delay;
1940 				clamp = use_delay > 0;
1941 			}
1942 		}
1943 		blkg = blkg->parent;
1944 	}
1945 
1946 	if (!delay_nsec)
1947 		return;
1948 
1949 	/*
1950 	 * Let's not sleep for all eternity if we've amassed a huge delay.
1951 	 * Swapping or metadata IO can accumulate 10's of seconds worth of
1952 	 * delay, and we want userspace to be able to do _something_ so cap the
1953 	 * delays at 0.25s. If there's 10's of seconds worth of delay then the
1954 	 * tasks will be delayed for 0.25 second for every syscall. If
1955 	 * blkcg_set_delay() was used as indicated by negative use_delay, the
1956 	 * caller is responsible for regulating the range.
1957 	 */
1958 	if (clamp)
1959 		delay_nsec = min_t(u64, delay_nsec, 250 * NSEC_PER_MSEC);
1960 
1961 	if (use_memdelay)
1962 		psi_memstall_enter(&pflags);
1963 
1964 	exp = ktime_add_ns(now, delay_nsec);
1965 	tok = io_schedule_prepare();
1966 	do {
1967 		__set_current_state(TASK_KILLABLE);
1968 		if (!schedule_hrtimeout(&exp, HRTIMER_MODE_ABS))
1969 			break;
1970 	} while (!fatal_signal_pending(current));
1971 	io_schedule_finish(tok);
1972 
1973 	if (use_memdelay)
1974 		psi_memstall_leave(&pflags);
1975 }
1976 
1977 /**
1978  * blkcg_maybe_throttle_current - throttle the current task if it has been marked
1979  *
1980  * This is only called if we've been marked with set_notify_resume().  Obviously
1981  * we can be set_notify_resume() for reasons other than blkcg throttling, so we
1982  * check to see if current->throttle_disk is set and if not this doesn't do
1983  * anything.  This should only ever be called by the resume code, it's not meant
1984  * to be called by people willy-nilly as it will actually do the work to
1985  * throttle the task if it is setup for throttling.
1986  */
1987 void blkcg_maybe_throttle_current(void)
1988 {
1989 	struct gendisk *disk = current->throttle_disk;
1990 	struct blkcg *blkcg;
1991 	struct blkcg_gq *blkg;
1992 	bool use_memdelay = current->use_memdelay;
1993 
1994 	if (!disk)
1995 		return;
1996 
1997 	current->throttle_disk = NULL;
1998 	current->use_memdelay = false;
1999 
2000 	rcu_read_lock();
2001 	blkcg = css_to_blkcg(blkcg_css());
2002 	if (!blkcg)
2003 		goto out;
2004 	blkg = blkg_lookup(blkcg, disk->queue);
2005 	if (!blkg)
2006 		goto out;
2007 	if (!blkg_tryget(blkg))
2008 		goto out;
2009 	rcu_read_unlock();
2010 
2011 	blkcg_maybe_throttle_blkg(blkg, use_memdelay);
2012 	blkg_put(blkg);
2013 	put_disk(disk);
2014 	return;
2015 out:
2016 	rcu_read_unlock();
2017 	put_disk(disk);
2018 }
2019 
2020 /**
2021  * blkcg_schedule_throttle - this task needs to check for throttling
2022  * @disk: disk to throttle
2023  * @use_memdelay: do we charge this to memory delay for PSI
2024  *
2025  * This is called by the IO controller when we know there's delay accumulated
2026  * for the blkg for this task.  We do not pass the blkg because there are places
2027  * we call this that may not have that information, the swapping code for
2028  * instance will only have a block_device at that point.  This set's the
2029  * notify_resume for the task to check and see if it requires throttling before
2030  * returning to user space.
2031  *
2032  * We will only schedule once per syscall.  You can call this over and over
2033  * again and it will only do the check once upon return to user space, and only
2034  * throttle once.  If the task needs to be throttled again it'll need to be
2035  * re-set at the next time we see the task.
2036  */
2037 void blkcg_schedule_throttle(struct gendisk *disk, bool use_memdelay)
2038 {
2039 	if (unlikely(current->flags & PF_KTHREAD))
2040 		return;
2041 
2042 	if (current->throttle_disk != disk) {
2043 		if (test_bit(GD_DEAD, &disk->state))
2044 			return;
2045 		get_device(disk_to_dev(disk));
2046 
2047 		if (current->throttle_disk)
2048 			put_disk(current->throttle_disk);
2049 		current->throttle_disk = disk;
2050 	}
2051 
2052 	if (use_memdelay)
2053 		current->use_memdelay = use_memdelay;
2054 	set_notify_resume(current);
2055 }
2056 
2057 /**
2058  * blkcg_add_delay - add delay to this blkg
2059  * @blkg: blkg of interest
2060  * @now: the current time in nanoseconds
2061  * @delta: how many nanoseconds of delay to add
2062  *
2063  * Charge @delta to the blkg's current delay accumulation.  This is used to
2064  * throttle tasks if an IO controller thinks we need more throttling.
2065  */
2066 void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta)
2067 {
2068 	if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
2069 		return;
2070 	blkcg_scale_delay(blkg, now);
2071 	atomic64_add(delta, &blkg->delay_nsec);
2072 }
2073 
2074 static inline struct blkcg_gq *blkg_lookup_tryget(struct blkcg_gq *blkg)
2075 {
2076 retry:
2077 	if (blkg_tryget(blkg))
2078 		return blkg;
2079 
2080 	blkg = blkg->parent;
2081 	if (blkg)
2082 		goto retry;
2083 
2084 	return NULL;
2085 }
2086 /**
2087  * blkg_tryget_closest - try and get a blkg ref on the closet blkg
2088  * @bio: target bio
2089  * @css: target css
2090  *
2091  * As the failure mode here is to walk up the blkg tree, this ensure that the
2092  * blkg->parent pointers are always valid.  This returns the blkg that it ended
2093  * up taking a reference on or %NULL if no reference was taken.
2094  */
2095 static inline struct blkcg_gq *blkg_tryget_closest(struct bio *bio,
2096 		struct cgroup_subsys_state *css)
2097 {
2098 	struct request_queue *q = bio->bi_bdev->bd_queue;
2099 	struct blkcg *blkcg = css_to_blkcg(css);
2100 	struct blkcg_gq *blkg;
2101 	unsigned long flags;
2102 
2103 	rcu_read_lock();
2104 	blkg = blkg_lookup(blkcg, q);
2105 	if (likely(blkg))
2106 		blkg = blkg_lookup_tryget(blkg);
2107 	rcu_read_unlock();
2108 
2109 	if (blkg)
2110 		return blkg;
2111 
2112 	/*
2113 	 * Fast path failed, we're probably issuing IO in this cgroup the first
2114 	 * time, hold lock to create new blkg.
2115 	 */
2116 	spin_lock_irqsave(&q->queue_lock, flags);
2117 	blkg = blkg_lookup_create(blkcg, bio->bi_bdev->bd_disk);
2118 	if (blkg)
2119 		blkg = blkg_lookup_tryget(blkg);
2120 	spin_unlock_irqrestore(&q->queue_lock, flags);
2121 
2122 	return blkg;
2123 }
2124 
2125 /**
2126  * bio_associate_blkg_from_css - associate a bio with a specified css
2127  * @bio: target bio
2128  * @css: target css
2129  *
2130  * Associate @bio with the blkg found by combining the css's blkg and the
2131  * request_queue of the @bio.  An association failure is handled by walking up
2132  * the blkg tree.  Therefore, the blkg associated can be anything between @blkg
2133  * and q->root_blkg.  This situation only happens when a cgroup is dying and
2134  * then the remaining bios will spill to the closest alive blkg.
2135  *
2136  * A reference will be taken on the blkg and will be released when @bio is
2137  * freed.
2138  */
2139 void bio_associate_blkg_from_css(struct bio *bio,
2140 				 struct cgroup_subsys_state *css)
2141 {
2142 	if (bio->bi_blkg)
2143 		blkg_put(bio->bi_blkg);
2144 
2145 	if (css && css->parent) {
2146 		bio->bi_blkg = blkg_tryget_closest(bio, css);
2147 	} else {
2148 		blkg_get(bdev_get_queue(bio->bi_bdev)->root_blkg);
2149 		bio->bi_blkg = bdev_get_queue(bio->bi_bdev)->root_blkg;
2150 	}
2151 }
2152 EXPORT_SYMBOL_GPL(bio_associate_blkg_from_css);
2153 
2154 /**
2155  * bio_associate_blkg - associate a bio with a blkg
2156  * @bio: target bio
2157  *
2158  * Associate @bio with the blkg found from the bio's css and request_queue.
2159  * If one is not found, bio_lookup_blkg() creates the blkg.  If a blkg is
2160  * already associated, the css is reused and association redone as the
2161  * request_queue may have changed.
2162  */
2163 void bio_associate_blkg(struct bio *bio)
2164 {
2165 	struct cgroup_subsys_state *css;
2166 
2167 	if (blk_op_is_passthrough(bio->bi_opf))
2168 		return;
2169 
2170 	if (bio->bi_blkg) {
2171 		css = bio_blkcg_css(bio);
2172 		bio_associate_blkg_from_css(bio, css);
2173 	} else {
2174 		rcu_read_lock();
2175 		css = blkcg_css();
2176 		if (!css_tryget_online(css))
2177 			css = NULL;
2178 		rcu_read_unlock();
2179 
2180 		bio_associate_blkg_from_css(bio, css);
2181 		if (css)
2182 			css_put(css);
2183 	}
2184 }
2185 EXPORT_SYMBOL_GPL(bio_associate_blkg);
2186 
2187 /**
2188  * bio_clone_blkg_association - clone blkg association from src to dst bio
2189  * @dst: destination bio
2190  * @src: source bio
2191  */
2192 void bio_clone_blkg_association(struct bio *dst, struct bio *src)
2193 {
2194 	if (src->bi_blkg)
2195 		bio_associate_blkg_from_css(dst, bio_blkcg_css(src));
2196 }
2197 EXPORT_SYMBOL_GPL(bio_clone_blkg_association);
2198 
2199 static int blk_cgroup_io_type(struct bio *bio)
2200 {
2201 	if (op_is_discard(bio->bi_opf))
2202 		return BLKG_IOSTAT_DISCARD;
2203 	if (op_is_write(bio->bi_opf))
2204 		return BLKG_IOSTAT_WRITE;
2205 	return BLKG_IOSTAT_READ;
2206 }
2207 
2208 void blk_cgroup_bio_start(struct bio *bio)
2209 {
2210 	struct blkcg *blkcg = bio->bi_blkg->blkcg;
2211 	int rwd = blk_cgroup_io_type(bio), cpu;
2212 	struct blkg_iostat_set *bis;
2213 	unsigned long flags;
2214 
2215 	if (!cgroup_subsys_on_dfl(io_cgrp_subsys))
2216 		return;
2217 
2218 	/* Root-level stats are sourced from system-wide IO stats */
2219 	if (!cgroup_parent(blkcg->css.cgroup))
2220 		return;
2221 
2222 	cpu = get_cpu();
2223 	bis = per_cpu_ptr(bio->bi_blkg->iostat_cpu, cpu);
2224 	flags = u64_stats_update_begin_irqsave(&bis->sync);
2225 
2226 	/*
2227 	 * If the bio is flagged with BIO_CGROUP_ACCT it means this is a split
2228 	 * bio and we would have already accounted for the size of the bio.
2229 	 */
2230 	if (!bio_flagged(bio, BIO_CGROUP_ACCT)) {
2231 		bio_set_flag(bio, BIO_CGROUP_ACCT);
2232 		bis->cur.bytes[rwd] += bio->bi_iter.bi_size;
2233 	}
2234 	bis->cur.ios[rwd]++;
2235 
2236 	/*
2237 	 * If the iostat_cpu isn't in a lockless list, put it into the
2238 	 * list to indicate that a stat update is pending.
2239 	 */
2240 	if (!READ_ONCE(bis->lqueued)) {
2241 		struct llist_head *lhead = this_cpu_ptr(blkcg->lhead);
2242 
2243 		llist_add(&bis->lnode, lhead);
2244 		WRITE_ONCE(bis->lqueued, true);
2245 	}
2246 
2247 	u64_stats_update_end_irqrestore(&bis->sync, flags);
2248 	__css_rstat_updated(&blkcg->css, cpu);
2249 	put_cpu();
2250 }
2251 
2252 bool __blk_cgroup_congested(void)
2253 {
2254 	struct blkcg *blkcg;
2255 	bool ret = false;
2256 
2257 	rcu_read_lock();
2258 	for (blkcg = css_to_blkcg(blkcg_css()); blkcg;
2259 	     blkcg = blkcg_parent(blkcg)) {
2260 		if (atomic_read(&blkcg->congestion_count)) {
2261 			ret = true;
2262 			break;
2263 		}
2264 	}
2265 	rcu_read_unlock();
2266 	return ret;
2267 }
2268 
2269 module_param(blkcg_debug_stats, bool, 0644);
2270 MODULE_PARM_DESC(blkcg_debug_stats, "True if you want debug stats, false if not");
2271