blk-cgroup.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BLK_CGROUP_PRIVATE_H
  3. #define _BLK_CGROUP_PRIVATE_H
  4. /*
  5. * block cgroup private header
  6. *
  7. * Based on ideas and code from CFQ, CFS and BFQ:
  8. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  9. *
  10. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  11. * Paolo Valente <paolo.valente@unimore.it>
  12. *
  13. * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
  14. * Nauman Rafique <nauman@google.com>
  15. */
  16. #include <linux/blk-cgroup.h>
  17. #include <linux/cgroup.h>
  18. #include <linux/kthread.h>
  19. #include <linux/blk-mq.h>
  20. #include <linux/llist.h>
  21. #include "blk.h"
  22. struct blkcg_gq;
  23. struct blkg_policy_data;
  24. /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
  25. #define BLKG_STAT_CPU_BATCH (INT_MAX / 2)
  26. #ifdef CONFIG_BLK_CGROUP
  27. enum blkg_iostat_type {
  28. BLKG_IOSTAT_READ,
  29. BLKG_IOSTAT_WRITE,
  30. BLKG_IOSTAT_DISCARD,
  31. BLKG_IOSTAT_NR,
  32. };
  33. struct blkg_iostat {
  34. u64 bytes[BLKG_IOSTAT_NR];
  35. u64 ios[BLKG_IOSTAT_NR];
  36. };
  37. struct blkg_iostat_set {
  38. struct u64_stats_sync sync;
  39. struct blkcg_gq *blkg;
  40. struct llist_node lnode;
  41. int lqueued; /* queued in llist */
  42. struct blkg_iostat cur;
  43. struct blkg_iostat last;
  44. };
  45. /* association between a blk cgroup and a request queue */
  46. struct blkcg_gq {
  47. /* Pointer to the associated request_queue */
  48. struct request_queue *q;
  49. struct list_head q_node;
  50. struct hlist_node blkcg_node;
  51. struct blkcg *blkcg;
  52. /* all non-root blkcg_gq's are guaranteed to have access to parent */
  53. struct blkcg_gq *parent;
  54. /* reference count */
  55. struct percpu_ref refcnt;
  56. /* is this blkg online? protected by both blkcg and q locks */
  57. bool online;
  58. struct blkg_iostat_set __percpu *iostat_cpu;
  59. struct blkg_iostat_set iostat;
  60. struct blkg_policy_data *pd[BLKCG_MAX_POLS];
  61. #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
  62. spinlock_t async_bio_lock;
  63. struct bio_list async_bios;
  64. #endif
  65. union {
  66. struct work_struct async_bio_work;
  67. struct work_struct free_work;
  68. };
  69. atomic_t use_delay;
  70. atomic64_t delay_nsec;
  71. atomic64_t delay_start;
  72. u64 last_delay;
  73. int last_use;
  74. struct rcu_head rcu_head;
  75. };
  76. struct blkcg {
  77. struct cgroup_subsys_state css;
  78. spinlock_t lock;
  79. refcount_t online_pin;
  80. /* If there is block congestion on this cgroup. */
  81. atomic_t congestion_count;
  82. struct radix_tree_root blkg_tree;
  83. struct blkcg_gq __rcu *blkg_hint;
  84. struct hlist_head blkg_list;
  85. struct blkcg_policy_data *cpd[BLKCG_MAX_POLS];
  86. struct list_head all_blkcgs_node;
  87. /*
  88. * List of updated percpu blkg_iostat_set's since the last flush.
  89. */
  90. struct llist_head __percpu *lhead;
  91. #ifdef CONFIG_BLK_CGROUP_FC_APPID
  92. char fc_app_id[FC_APPID_LEN];
  93. #endif
  94. #ifdef CONFIG_CGROUP_WRITEBACK
  95. struct list_head cgwb_list;
  96. #endif
  97. };
  98. static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
  99. {
  100. return css ? container_of(css, struct blkcg, css) : NULL;
  101. }
  102. /*
  103. * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
  104. * request_queue (q). This is used by blkcg policies which need to track
  105. * information per blkcg - q pair.
  106. *
  107. * There can be multiple active blkcg policies and each blkg:policy pair is
  108. * represented by a blkg_policy_data which is allocated and freed by each
  109. * policy's pd_alloc/free_fn() methods. A policy can allocate private data
  110. * area by allocating larger data structure which embeds blkg_policy_data
  111. * at the beginning.
  112. */
  113. struct blkg_policy_data {
  114. /* the blkg and policy id this per-policy data belongs to */
  115. struct blkcg_gq *blkg;
  116. int plid;
  117. bool online;
  118. };
  119. /*
  120. * Policies that need to keep per-blkcg data which is independent from any
  121. * request_queue associated to it should implement cpd_alloc/free_fn()
  122. * methods. A policy can allocate private data area by allocating larger
  123. * data structure which embeds blkcg_policy_data at the beginning.
  124. * cpd_init() is invoked to let each policy handle per-blkcg data.
  125. */
  126. struct blkcg_policy_data {
  127. /* the blkcg and policy id this per-policy data belongs to */
  128. struct blkcg *blkcg;
  129. int plid;
  130. };
  131. typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
  132. typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
  133. typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
  134. typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
  135. typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(struct gendisk *disk,
  136. struct blkcg *blkcg, gfp_t gfp);
  137. typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
  138. typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
  139. typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
  140. typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
  141. typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
  142. typedef void (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd,
  143. struct seq_file *s);
  144. struct blkcg_policy {
  145. int plid;
  146. /* cgroup files for the policy */
  147. struct cftype *dfl_cftypes;
  148. struct cftype *legacy_cftypes;
  149. /* operations */
  150. blkcg_pol_alloc_cpd_fn *cpd_alloc_fn;
  151. blkcg_pol_free_cpd_fn *cpd_free_fn;
  152. blkcg_pol_alloc_pd_fn *pd_alloc_fn;
  153. blkcg_pol_init_pd_fn *pd_init_fn;
  154. blkcg_pol_online_pd_fn *pd_online_fn;
  155. blkcg_pol_offline_pd_fn *pd_offline_fn;
  156. blkcg_pol_free_pd_fn *pd_free_fn;
  157. blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
  158. blkcg_pol_stat_pd_fn *pd_stat_fn;
  159. };
  160. extern struct blkcg blkcg_root;
  161. extern bool blkcg_debug_stats;
  162. void blkg_init_queue(struct request_queue *q);
  163. int blkcg_init_disk(struct gendisk *disk);
  164. void blkcg_exit_disk(struct gendisk *disk);
  165. /* Blkio controller policy registration */
  166. int blkcg_policy_register(struct blkcg_policy *pol);
  167. void blkcg_policy_unregister(struct blkcg_policy *pol);
  168. int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol);
  169. void blkcg_deactivate_policy(struct gendisk *disk,
  170. const struct blkcg_policy *pol);
  171. const char *blkg_dev_name(struct blkcg_gq *blkg);
  172. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  173. u64 (*prfill)(struct seq_file *,
  174. struct blkg_policy_data *, int),
  175. const struct blkcg_policy *pol, int data,
  176. bool show_total);
  177. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  178. struct blkg_conf_ctx {
  179. char *input;
  180. char *body;
  181. struct block_device *bdev;
  182. struct blkcg_gq *blkg;
  183. };
  184. void blkg_conf_init(struct blkg_conf_ctx *ctx, char *input);
  185. int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx);
  186. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  187. struct blkg_conf_ctx *ctx);
  188. void blkg_conf_exit(struct blkg_conf_ctx *ctx);
  189. /**
  190. * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
  191. * @return: true if this bio needs to be submitted with the root blkg context.
  192. *
  193. * In order to avoid priority inversions we sometimes need to issue a bio as if
  194. * it were attached to the root blkg, and then backcharge to the actual owning
  195. * blkg. The idea is we do bio_blkcg_css() to look up the actual context for
  196. * the bio and attach the appropriate blkg to the bio. Then we call this helper
  197. * and if it is true run with the root blkg for that queue and then do any
  198. * backcharging to the originating cgroup once the io is complete.
  199. */
  200. static inline bool bio_issue_as_root_blkg(struct bio *bio)
  201. {
  202. return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
  203. }
  204. /**
  205. * blkg_lookup - lookup blkg for the specified blkcg - q pair
  206. * @blkcg: blkcg of interest
  207. * @q: request_queue of interest
  208. *
  209. * Lookup blkg for the @blkcg - @q pair.
  210. * Must be called in a RCU critical section.
  211. */
  212. static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
  213. struct request_queue *q)
  214. {
  215. struct blkcg_gq *blkg;
  216. if (blkcg == &blkcg_root)
  217. return q->root_blkg;
  218. blkg = rcu_dereference_check(blkcg->blkg_hint,
  219. lockdep_is_held(&q->queue_lock));
  220. if (blkg && blkg->q == q)
  221. return blkg;
  222. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  223. if (blkg && blkg->q != q)
  224. blkg = NULL;
  225. return blkg;
  226. }
  227. /**
  228. * blkg_to_pdata - get policy private data
  229. * @blkg: blkg of interest
  230. * @pol: policy of interest
  231. *
  232. * Return pointer to private data associated with the @blkg-@pol pair.
  233. */
  234. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  235. struct blkcg_policy *pol)
  236. {
  237. return blkg ? blkg->pd[pol->plid] : NULL;
  238. }
  239. static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
  240. struct blkcg_policy *pol)
  241. {
  242. return blkcg ? blkcg->cpd[pol->plid] : NULL;
  243. }
  244. /**
  245. * pdata_to_blkg - get blkg associated with policy private data
  246. * @pd: policy private data of interest
  247. *
  248. * @pd is policy private data. Determine the blkg it's associated with.
  249. */
  250. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
  251. {
  252. return pd ? pd->blkg : NULL;
  253. }
  254. static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
  255. {
  256. return cpd ? cpd->blkcg : NULL;
  257. }
  258. /**
  259. * blkg_get - get a blkg reference
  260. * @blkg: blkg to get
  261. *
  262. * The caller should be holding an existing reference.
  263. */
  264. static inline void blkg_get(struct blkcg_gq *blkg)
  265. {
  266. percpu_ref_get(&blkg->refcnt);
  267. }
  268. /**
  269. * blkg_tryget - try and get a blkg reference
  270. * @blkg: blkg to get
  271. *
  272. * This is for use when doing an RCU lookup of the blkg. We may be in the midst
  273. * of freeing this blkg, so we can only use it if the refcnt is not zero.
  274. */
  275. static inline bool blkg_tryget(struct blkcg_gq *blkg)
  276. {
  277. return blkg && percpu_ref_tryget(&blkg->refcnt);
  278. }
  279. /**
  280. * blkg_put - put a blkg reference
  281. * @blkg: blkg to put
  282. */
  283. static inline void blkg_put(struct blkcg_gq *blkg)
  284. {
  285. percpu_ref_put(&blkg->refcnt);
  286. }
  287. /**
  288. * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
  289. * @d_blkg: loop cursor pointing to the current descendant
  290. * @pos_css: used for iteration
  291. * @p_blkg: target blkg to walk descendants of
  292. *
  293. * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
  294. * read locked. If called under either blkcg or queue lock, the iteration
  295. * is guaranteed to include all and only online blkgs. The caller may
  296. * update @pos_css by calling css_rightmost_descendant() to skip subtree.
  297. * @p_blkg is included in the iteration and the first node to be visited.
  298. */
  299. #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
  300. css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
  301. if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css), \
  302. (p_blkg)->q)))
  303. /**
  304. * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
  305. * @d_blkg: loop cursor pointing to the current descendant
  306. * @pos_css: used for iteration
  307. * @p_blkg: target blkg to walk descendants of
  308. *
  309. * Similar to blkg_for_each_descendant_pre() but performs post-order
  310. * traversal instead. Synchronization rules are the same. @p_blkg is
  311. * included in the iteration and the last node to be visited.
  312. */
  313. #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
  314. css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
  315. if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css), \
  316. (p_blkg)->q)))
  317. static inline void blkcg_bio_issue_init(struct bio *bio)
  318. {
  319. bio_issue_init(&bio->bi_issue, bio_sectors(bio));
  320. }
  321. static inline void blkcg_use_delay(struct blkcg_gq *blkg)
  322. {
  323. if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
  324. return;
  325. if (atomic_add_return(1, &blkg->use_delay) == 1)
  326. atomic_inc(&blkg->blkcg->congestion_count);
  327. }
  328. static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
  329. {
  330. int old = atomic_read(&blkg->use_delay);
  331. if (WARN_ON_ONCE(old < 0))
  332. return 0;
  333. if (old == 0)
  334. return 0;
  335. /*
  336. * We do this song and dance because we can race with somebody else
  337. * adding or removing delay. If we just did an atomic_dec we'd end up
  338. * negative and we'd already be in trouble. We need to subtract 1 and
  339. * then check to see if we were the last delay so we can drop the
  340. * congestion count on the cgroup.
  341. */
  342. while (old && !atomic_try_cmpxchg(&blkg->use_delay, &old, old - 1))
  343. ;
  344. if (old == 0)
  345. return 0;
  346. if (old == 1)
  347. atomic_dec(&blkg->blkcg->congestion_count);
  348. return 1;
  349. }
  350. /**
  351. * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount
  352. * @blkg: target blkg
  353. * @delay: delay duration in nsecs
  354. *
  355. * When enabled with this function, the delay is not decayed and must be
  356. * explicitly cleared with blkcg_clear_delay(). Must not be mixed with
  357. * blkcg_[un]use_delay() and blkcg_add_delay() usages.
  358. */
  359. static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
  360. {
  361. int old = atomic_read(&blkg->use_delay);
  362. /* We only want 1 person setting the congestion count for this blkg. */
  363. if (!old && atomic_try_cmpxchg(&blkg->use_delay, &old, -1))
  364. atomic_inc(&blkg->blkcg->congestion_count);
  365. atomic64_set(&blkg->delay_nsec, delay);
  366. }
  367. /**
  368. * blkcg_clear_delay - Disable allocator delay mechanism
  369. * @blkg: target blkg
  370. *
  371. * Disable use_delay mechanism. See blkcg_set_delay().
  372. */
  373. static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
  374. {
  375. int old = atomic_read(&blkg->use_delay);
  376. /* We only want 1 person clearing the congestion count for this blkg. */
  377. if (old && atomic_try_cmpxchg(&blkg->use_delay, &old, 0))
  378. atomic_dec(&blkg->blkcg->congestion_count);
  379. }
  380. /**
  381. * blk_cgroup_mergeable - Determine whether to allow or disallow merges
  382. * @rq: request to merge into
  383. * @bio: bio to merge
  384. *
  385. * @bio and @rq should belong to the same cgroup and their issue_as_root should
  386. * match. The latter is necessary as we don't want to throttle e.g. a metadata
  387. * update because it happens to be next to a regular IO.
  388. */
  389. static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio)
  390. {
  391. return rq->bio->bi_blkg == bio->bi_blkg &&
  392. bio_issue_as_root_blkg(rq->bio) == bio_issue_as_root_blkg(bio);
  393. }
  394. void blk_cgroup_bio_start(struct bio *bio);
  395. void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
  396. #else /* CONFIG_BLK_CGROUP */
  397. struct blkg_policy_data {
  398. };
  399. struct blkcg_policy_data {
  400. };
  401. struct blkcg_policy {
  402. };
  403. struct blkcg {
  404. };
  405. static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
  406. static inline void blkg_init_queue(struct request_queue *q) { }
  407. static inline int blkcg_init_disk(struct gendisk *disk) { return 0; }
  408. static inline void blkcg_exit_disk(struct gendisk *disk) { }
  409. static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
  410. static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
  411. static inline int blkcg_activate_policy(struct gendisk *disk,
  412. const struct blkcg_policy *pol) { return 0; }
  413. static inline void blkcg_deactivate_policy(struct gendisk *disk,
  414. const struct blkcg_policy *pol) { }
  415. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  416. struct blkcg_policy *pol) { return NULL; }
  417. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
  418. static inline void blkg_get(struct blkcg_gq *blkg) { }
  419. static inline void blkg_put(struct blkcg_gq *blkg) { }
  420. static inline void blkcg_bio_issue_init(struct bio *bio) { }
  421. static inline void blk_cgroup_bio_start(struct bio *bio) { }
  422. static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) { return true; }
  423. #define blk_queue_for_each_rl(rl, q) \
  424. for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
  425. #endif /* CONFIG_BLK_CGROUP */
  426. #endif /* _BLK_CGROUP_PRIVATE_H */