blk-cgroup.h 15 KB

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