blk-cgroup.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. unsigned long blkg_conf_open_bdev_frozen(struct blkg_conf_ctx *ctx);
  187. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  188. struct blkg_conf_ctx *ctx);
  189. void blkg_conf_exit(struct blkg_conf_ctx *ctx);
  190. void blkg_conf_exit_frozen(struct blkg_conf_ctx *ctx, unsigned long memflags);
  191. /**
  192. * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
  193. * @bio: the target &bio
  194. *
  195. * Return: true if this bio needs to be submitted with the root blkg context.
  196. *
  197. * In order to avoid priority inversions we sometimes need to issue a bio as if
  198. * it were attached to the root blkg, and then backcharge to the actual owning
  199. * blkg. The idea is we do bio_blkcg_css() to look up the actual context for
  200. * the bio and attach the appropriate blkg to the bio. Then we call this helper
  201. * and if it is true run with the root blkg for that queue and then do any
  202. * backcharging to the originating cgroup once the io is complete.
  203. */
  204. static inline bool bio_issue_as_root_blkg(struct bio *bio)
  205. {
  206. return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
  207. }
  208. /**
  209. * blkg_lookup - lookup blkg for the specified blkcg - q pair
  210. * @blkcg: blkcg of interest
  211. * @q: request_queue of interest
  212. *
  213. * Lookup blkg for the @blkcg - @q pair.
  214. *
  215. * Must be called in a RCU critical section.
  216. */
  217. static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
  218. struct request_queue *q)
  219. {
  220. struct blkcg_gq *blkg;
  221. if (blkcg == &blkcg_root)
  222. return q->root_blkg;
  223. blkg = rcu_dereference_check(blkcg->blkg_hint,
  224. lockdep_is_held(&q->queue_lock));
  225. if (blkg && blkg->q == q)
  226. return blkg;
  227. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  228. if (blkg && blkg->q != q)
  229. blkg = NULL;
  230. return blkg;
  231. }
  232. /**
  233. * blkg_to_pd - get policy private data
  234. * @blkg: blkg of interest
  235. * @pol: policy of interest
  236. *
  237. * Return pointer to private data associated with the @blkg-@pol pair.
  238. */
  239. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  240. struct blkcg_policy *pol)
  241. {
  242. return blkg ? blkg->pd[pol->plid] : NULL;
  243. }
  244. static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
  245. struct blkcg_policy *pol)
  246. {
  247. return blkcg ? blkcg->cpd[pol->plid] : NULL;
  248. }
  249. /**
  250. * pd_to_blkg - get blkg associated with policy private data
  251. * @pd: policy private data of interest
  252. *
  253. * @pd is policy private data. Determine the blkg it's associated with.
  254. */
  255. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
  256. {
  257. return pd ? pd->blkg : NULL;
  258. }
  259. static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
  260. {
  261. return cpd ? cpd->blkcg : NULL;
  262. }
  263. /**
  264. * blkg_get - get a blkg reference
  265. * @blkg: blkg to get
  266. *
  267. * The caller should be holding an existing reference.
  268. */
  269. static inline void blkg_get(struct blkcg_gq *blkg)
  270. {
  271. percpu_ref_get(&blkg->refcnt);
  272. }
  273. /**
  274. * blkg_tryget - try and get a blkg reference
  275. * @blkg: blkg to get
  276. *
  277. * This is for use when doing an RCU lookup of the blkg. We may be in the midst
  278. * of freeing this blkg, so we can only use it if the refcnt is not zero.
  279. */
  280. static inline bool blkg_tryget(struct blkcg_gq *blkg)
  281. {
  282. return blkg && percpu_ref_tryget(&blkg->refcnt);
  283. }
  284. /**
  285. * blkg_put - put a blkg reference
  286. * @blkg: blkg to put
  287. */
  288. static inline void blkg_put(struct blkcg_gq *blkg)
  289. {
  290. percpu_ref_put(&blkg->refcnt);
  291. }
  292. /**
  293. * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
  294. * @d_blkg: loop cursor pointing to the current descendant
  295. * @pos_css: used for iteration
  296. * @p_blkg: target blkg to walk descendants of
  297. *
  298. * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
  299. * read locked. If called under either blkcg or queue lock, the iteration
  300. * is guaranteed to include all and only online blkgs. The caller may
  301. * update @pos_css by calling css_rightmost_descendant() to skip subtree.
  302. * @p_blkg is included in the iteration and the first node to be visited.
  303. */
  304. #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
  305. css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
  306. if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css), \
  307. (p_blkg)->q)))
  308. /**
  309. * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
  310. * @d_blkg: loop cursor pointing to the current descendant
  311. * @pos_css: used for iteration
  312. * @p_blkg: target blkg to walk descendants of
  313. *
  314. * Similar to blkg_for_each_descendant_pre() but performs post-order
  315. * traversal instead. Synchronization rules are the same. @p_blkg is
  316. * included in the iteration and the last node to be visited.
  317. */
  318. #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
  319. css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
  320. if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css), \
  321. (p_blkg)->q)))
  322. static inline void blkcg_use_delay(struct blkcg_gq *blkg)
  323. {
  324. if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
  325. return;
  326. if (atomic_add_return(1, &blkg->use_delay) == 1)
  327. atomic_inc(&blkg->blkcg->congestion_count);
  328. }
  329. static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
  330. {
  331. int old = atomic_read(&blkg->use_delay);
  332. if (WARN_ON_ONCE(old < 0))
  333. return 0;
  334. if (old == 0)
  335. return 0;
  336. /*
  337. * We do this song and dance because we can race with somebody else
  338. * adding or removing delay. If we just did an atomic_dec we'd end up
  339. * negative and we'd already be in trouble. We need to subtract 1 and
  340. * then check to see if we were the last delay so we can drop the
  341. * congestion count on the cgroup.
  342. */
  343. while (old && !atomic_try_cmpxchg(&blkg->use_delay, &old, old - 1))
  344. ;
  345. if (old == 0)
  346. return 0;
  347. if (old == 1)
  348. atomic_dec(&blkg->blkcg->congestion_count);
  349. return 1;
  350. }
  351. /**
  352. * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount
  353. * @blkg: target blkg
  354. * @delay: delay duration in nsecs
  355. *
  356. * When enabled with this function, the delay is not decayed and must be
  357. * explicitly cleared with blkcg_clear_delay(). Must not be mixed with
  358. * blkcg_[un]use_delay() and blkcg_add_delay() usages.
  359. */
  360. static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
  361. {
  362. int old = atomic_read(&blkg->use_delay);
  363. /* We only want 1 person setting the congestion count for this blkg. */
  364. if (!old && atomic_try_cmpxchg(&blkg->use_delay, &old, -1))
  365. atomic_inc(&blkg->blkcg->congestion_count);
  366. atomic64_set(&blkg->delay_nsec, delay);
  367. }
  368. /**
  369. * blkcg_clear_delay - Disable allocator delay mechanism
  370. * @blkg: target blkg
  371. *
  372. * Disable use_delay mechanism. See blkcg_set_delay().
  373. */
  374. static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
  375. {
  376. int old = atomic_read(&blkg->use_delay);
  377. /* We only want 1 person clearing the congestion count for this blkg. */
  378. if (old && atomic_try_cmpxchg(&blkg->use_delay, &old, 0))
  379. atomic_dec(&blkg->blkcg->congestion_count);
  380. }
  381. /**
  382. * blk_cgroup_mergeable - Determine whether to allow or disallow merges
  383. * @rq: request to merge into
  384. * @bio: bio to merge
  385. *
  386. * @bio and @rq should belong to the same cgroup and their issue_as_root should
  387. * match. The latter is necessary as we don't want to throttle e.g. a metadata
  388. * update because it happens to be next to a regular IO.
  389. */
  390. static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio)
  391. {
  392. return rq->bio->bi_blkg == bio->bi_blkg &&
  393. bio_issue_as_root_blkg(rq->bio) == bio_issue_as_root_blkg(bio);
  394. }
  395. static inline bool blkcg_policy_enabled(struct request_queue *q,
  396. const struct blkcg_policy *pol)
  397. {
  398. return pol && test_bit(pol->plid, q->blkcg_pols);
  399. }
  400. void blk_cgroup_bio_start(struct bio *bio);
  401. void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
  402. #else /* CONFIG_BLK_CGROUP */
  403. struct blkg_policy_data {
  404. };
  405. struct blkcg_policy_data {
  406. };
  407. struct blkcg_policy {
  408. };
  409. struct blkcg {
  410. };
  411. static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
  412. static inline void blkg_init_queue(struct request_queue *q) { }
  413. static inline int blkcg_init_disk(struct gendisk *disk) { return 0; }
  414. static inline void blkcg_exit_disk(struct gendisk *disk) { }
  415. static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
  416. static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
  417. static inline int blkcg_activate_policy(struct gendisk *disk,
  418. const struct blkcg_policy *pol) { return 0; }
  419. static inline void blkcg_deactivate_policy(struct gendisk *disk,
  420. const struct blkcg_policy *pol) { }
  421. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  422. struct blkcg_policy *pol) { return NULL; }
  423. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
  424. static inline void blkg_get(struct blkcg_gq *blkg) { }
  425. static inline void blkg_put(struct blkcg_gq *blkg) { }
  426. static inline void blk_cgroup_bio_start(struct bio *bio) { }
  427. static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) { return true; }
  428. #define blk_queue_for_each_rl(rl, q) \
  429. for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
  430. #endif /* CONFIG_BLK_CGROUP */
  431. #endif /* _BLK_CGROUP_PRIVATE_H */