blk-cgroup.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #ifndef _BLK_CGROUP_H
  2. #define _BLK_CGROUP_H
  3. /*
  4. * Common Block IO controller cgroup interface
  5. *
  6. * Based on ideas and code from CFQ, CFS and BFQ:
  7. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  8. *
  9. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  10. * Paolo Valente <paolo.valente@unimore.it>
  11. *
  12. * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
  13. * Nauman Rafique <nauman@google.com>
  14. */
  15. #include <linux/cgroup.h>
  16. #include <linux/u64_stats_sync.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/radix-tree.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/atomic.h>
  21. /* Max limits for throttle policy */
  22. #define THROTL_IOPS_MAX UINT_MAX
  23. /* CFQ specific, out here for blkcg->cfq_weight */
  24. #define CFQ_WEIGHT_MIN 10
  25. #define CFQ_WEIGHT_MAX 1000
  26. #define CFQ_WEIGHT_DEFAULT 500
  27. #ifdef CONFIG_BLK_CGROUP
  28. enum blkg_rwstat_type {
  29. BLKG_RWSTAT_READ,
  30. BLKG_RWSTAT_WRITE,
  31. BLKG_RWSTAT_SYNC,
  32. BLKG_RWSTAT_ASYNC,
  33. BLKG_RWSTAT_NR,
  34. BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
  35. };
  36. struct blkcg_gq;
  37. struct blkcg {
  38. struct cgroup_subsys_state css;
  39. spinlock_t lock;
  40. struct radix_tree_root blkg_tree;
  41. struct blkcg_gq *blkg_hint;
  42. struct hlist_head blkg_list;
  43. /* TODO: per-policy storage in blkcg */
  44. unsigned int cfq_weight; /* belongs to cfq */
  45. unsigned int cfq_leaf_weight;
  46. };
  47. struct blkg_stat {
  48. struct u64_stats_sync syncp;
  49. uint64_t cnt;
  50. };
  51. struct blkg_rwstat {
  52. struct u64_stats_sync syncp;
  53. uint64_t cnt[BLKG_RWSTAT_NR];
  54. };
  55. /*
  56. * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
  57. * request_queue (q). This is used by blkcg policies which need to track
  58. * information per blkcg - q pair.
  59. *
  60. * There can be multiple active blkcg policies and each has its private
  61. * data on each blkg, the size of which is determined by
  62. * blkcg_policy->pd_size. blkcg core allocates and frees such areas
  63. * together with blkg and invokes pd_init/exit_fn() methods.
  64. *
  65. * Such private data must embed struct blkg_policy_data (pd) at the
  66. * beginning and pd_size can't be smaller than pd.
  67. */
  68. struct blkg_policy_data {
  69. /* the blkg and policy id this per-policy data belongs to */
  70. struct blkcg_gq *blkg;
  71. int plid;
  72. /* used during policy activation */
  73. struct list_head alloc_node;
  74. };
  75. /* association between a blk cgroup and a request queue */
  76. struct blkcg_gq {
  77. /* Pointer to the associated request_queue */
  78. struct request_queue *q;
  79. struct list_head q_node;
  80. struct hlist_node blkcg_node;
  81. struct blkcg *blkcg;
  82. /* all non-root blkcg_gq's are guaranteed to have access to parent */
  83. struct blkcg_gq *parent;
  84. /* request allocation list for this blkcg-q pair */
  85. struct request_list rl;
  86. /* reference count */
  87. atomic_t refcnt;
  88. /* is this blkg online? protected by both blkcg and q locks */
  89. bool online;
  90. struct blkg_policy_data *pd[BLKCG_MAX_POLS];
  91. struct rcu_head rcu_head;
  92. };
  93. typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
  94. typedef void (blkcg_pol_online_pd_fn)(struct blkcg_gq *blkg);
  95. typedef void (blkcg_pol_offline_pd_fn)(struct blkcg_gq *blkg);
  96. typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg);
  97. typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg);
  98. struct blkcg_policy {
  99. int plid;
  100. /* policy specific private data size */
  101. size_t pd_size;
  102. /* cgroup files for the policy */
  103. struct cftype *cftypes;
  104. /* operations */
  105. blkcg_pol_init_pd_fn *pd_init_fn;
  106. blkcg_pol_online_pd_fn *pd_online_fn;
  107. blkcg_pol_offline_pd_fn *pd_offline_fn;
  108. blkcg_pol_exit_pd_fn *pd_exit_fn;
  109. blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
  110. };
  111. extern struct blkcg blkcg_root;
  112. struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q);
  113. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  114. struct request_queue *q);
  115. int blkcg_init_queue(struct request_queue *q);
  116. void blkcg_drain_queue(struct request_queue *q);
  117. void blkcg_exit_queue(struct request_queue *q);
  118. /* Blkio controller policy registration */
  119. int blkcg_policy_register(struct blkcg_policy *pol);
  120. void blkcg_policy_unregister(struct blkcg_policy *pol);
  121. int blkcg_activate_policy(struct request_queue *q,
  122. const struct blkcg_policy *pol);
  123. void blkcg_deactivate_policy(struct request_queue *q,
  124. const struct blkcg_policy *pol);
  125. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  126. u64 (*prfill)(struct seq_file *,
  127. struct blkg_policy_data *, int),
  128. const struct blkcg_policy *pol, int data,
  129. bool show_total);
  130. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  131. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  132. const struct blkg_rwstat *rwstat);
  133. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
  134. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  135. int off);
  136. u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off);
  137. struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
  138. int off);
  139. struct blkg_conf_ctx {
  140. struct gendisk *disk;
  141. struct blkcg_gq *blkg;
  142. u64 v;
  143. };
  144. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  145. const char *input, struct blkg_conf_ctx *ctx);
  146. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  147. static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
  148. {
  149. return css ? container_of(css, struct blkcg, css) : NULL;
  150. }
  151. static inline struct blkcg *task_blkcg(struct task_struct *tsk)
  152. {
  153. return css_to_blkcg(task_css(tsk, blkio_cgrp_id));
  154. }
  155. static inline struct blkcg *bio_blkcg(struct bio *bio)
  156. {
  157. if (bio && bio->bi_css)
  158. return css_to_blkcg(bio->bi_css);
  159. return task_blkcg(current);
  160. }
  161. /**
  162. * blkcg_parent - get the parent of a blkcg
  163. * @blkcg: blkcg of interest
  164. *
  165. * Return the parent blkcg of @blkcg. Can be called anytime.
  166. */
  167. static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
  168. {
  169. return css_to_blkcg(blkcg->css.parent);
  170. }
  171. /**
  172. * blkg_to_pdata - get policy private data
  173. * @blkg: blkg of interest
  174. * @pol: policy of interest
  175. *
  176. * Return pointer to private data associated with the @blkg-@pol pair.
  177. */
  178. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  179. struct blkcg_policy *pol)
  180. {
  181. return blkg ? blkg->pd[pol->plid] : NULL;
  182. }
  183. /**
  184. * pdata_to_blkg - get blkg associated with policy private data
  185. * @pd: policy private data of interest
  186. *
  187. * @pd is policy private data. Determine the blkg it's associated with.
  188. */
  189. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
  190. {
  191. return pd ? pd->blkg : NULL;
  192. }
  193. /**
  194. * blkg_path - format cgroup path of blkg
  195. * @blkg: blkg of interest
  196. * @buf: target buffer
  197. * @buflen: target buffer length
  198. *
  199. * Format the path of the cgroup of @blkg into @buf.
  200. */
  201. static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
  202. {
  203. char *p;
  204. p = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
  205. if (!p) {
  206. strncpy(buf, "<unavailable>", buflen);
  207. return -ENAMETOOLONG;
  208. }
  209. memmove(buf, p, buf + buflen - p);
  210. return 0;
  211. }
  212. /**
  213. * blkg_get - get a blkg reference
  214. * @blkg: blkg to get
  215. *
  216. * The caller should be holding an existing reference.
  217. */
  218. static inline void blkg_get(struct blkcg_gq *blkg)
  219. {
  220. WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
  221. atomic_inc(&blkg->refcnt);
  222. }
  223. void __blkg_release_rcu(struct rcu_head *rcu);
  224. /**
  225. * blkg_put - put a blkg reference
  226. * @blkg: blkg to put
  227. */
  228. static inline void blkg_put(struct blkcg_gq *blkg)
  229. {
  230. WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
  231. if (atomic_dec_and_test(&blkg->refcnt))
  232. call_rcu(&blkg->rcu_head, __blkg_release_rcu);
  233. }
  234. struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, struct request_queue *q,
  235. bool update_hint);
  236. /**
  237. * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
  238. * @d_blkg: loop cursor pointing to the current descendant
  239. * @pos_css: used for iteration
  240. * @p_blkg: target blkg to walk descendants of
  241. *
  242. * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
  243. * read locked. If called under either blkcg or queue lock, the iteration
  244. * is guaranteed to include all and only online blkgs. The caller may
  245. * update @pos_css by calling css_rightmost_descendant() to skip subtree.
  246. * @p_blkg is included in the iteration and the first node to be visited.
  247. */
  248. #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
  249. css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
  250. if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
  251. (p_blkg)->q, false)))
  252. /**
  253. * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
  254. * @d_blkg: loop cursor pointing to the current descendant
  255. * @pos_css: used for iteration
  256. * @p_blkg: target blkg to walk descendants of
  257. *
  258. * Similar to blkg_for_each_descendant_pre() but performs post-order
  259. * traversal instead. Synchronization rules are the same. @p_blkg is
  260. * included in the iteration and the last node to be visited.
  261. */
  262. #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
  263. css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
  264. if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
  265. (p_blkg)->q, false)))
  266. /**
  267. * blk_get_rl - get request_list to use
  268. * @q: request_queue of interest
  269. * @bio: bio which will be attached to the allocated request (may be %NULL)
  270. *
  271. * The caller wants to allocate a request from @q to use for @bio. Find
  272. * the request_list to use and obtain a reference on it. Should be called
  273. * under queue_lock. This function is guaranteed to return non-%NULL
  274. * request_list.
  275. */
  276. static inline struct request_list *blk_get_rl(struct request_queue *q,
  277. struct bio *bio)
  278. {
  279. struct blkcg *blkcg;
  280. struct blkcg_gq *blkg;
  281. rcu_read_lock();
  282. blkcg = bio_blkcg(bio);
  283. /* bypass blkg lookup and use @q->root_rl directly for root */
  284. if (blkcg == &blkcg_root)
  285. goto root_rl;
  286. /*
  287. * Try to use blkg->rl. blkg lookup may fail under memory pressure
  288. * or if either the blkcg or queue is going away. Fall back to
  289. * root_rl in such cases.
  290. */
  291. blkg = blkg_lookup_create(blkcg, q);
  292. if (unlikely(IS_ERR(blkg)))
  293. goto root_rl;
  294. blkg_get(blkg);
  295. rcu_read_unlock();
  296. return &blkg->rl;
  297. root_rl:
  298. rcu_read_unlock();
  299. return &q->root_rl;
  300. }
  301. /**
  302. * blk_put_rl - put request_list
  303. * @rl: request_list to put
  304. *
  305. * Put the reference acquired by blk_get_rl(). Should be called under
  306. * queue_lock.
  307. */
  308. static inline void blk_put_rl(struct request_list *rl)
  309. {
  310. /* root_rl may not have blkg set */
  311. if (rl->blkg && rl->blkg->blkcg != &blkcg_root)
  312. blkg_put(rl->blkg);
  313. }
  314. /**
  315. * blk_rq_set_rl - associate a request with a request_list
  316. * @rq: request of interest
  317. * @rl: target request_list
  318. *
  319. * Associate @rq with @rl so that accounting and freeing can know the
  320. * request_list @rq came from.
  321. */
  322. static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl)
  323. {
  324. rq->rl = rl;
  325. }
  326. /**
  327. * blk_rq_rl - return the request_list a request came from
  328. * @rq: request of interest
  329. *
  330. * Return the request_list @rq is allocated from.
  331. */
  332. static inline struct request_list *blk_rq_rl(struct request *rq)
  333. {
  334. return rq->rl;
  335. }
  336. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  337. struct request_queue *q);
  338. /**
  339. * blk_queue_for_each_rl - iterate through all request_lists of a request_queue
  340. *
  341. * Should be used under queue_lock.
  342. */
  343. #define blk_queue_for_each_rl(rl, q) \
  344. for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q)))
  345. static inline void blkg_stat_init(struct blkg_stat *stat)
  346. {
  347. u64_stats_init(&stat->syncp);
  348. }
  349. /**
  350. * blkg_stat_add - add a value to a blkg_stat
  351. * @stat: target blkg_stat
  352. * @val: value to add
  353. *
  354. * Add @val to @stat. The caller is responsible for synchronizing calls to
  355. * this function.
  356. */
  357. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  358. {
  359. u64_stats_update_begin(&stat->syncp);
  360. stat->cnt += val;
  361. u64_stats_update_end(&stat->syncp);
  362. }
  363. /**
  364. * blkg_stat_read - read the current value of a blkg_stat
  365. * @stat: blkg_stat to read
  366. *
  367. * Read the current value of @stat. This function can be called without
  368. * synchroniztion and takes care of u64 atomicity.
  369. */
  370. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  371. {
  372. unsigned int start;
  373. uint64_t v;
  374. do {
  375. start = u64_stats_fetch_begin_irq(&stat->syncp);
  376. v = stat->cnt;
  377. } while (u64_stats_fetch_retry_irq(&stat->syncp, start));
  378. return v;
  379. }
  380. /**
  381. * blkg_stat_reset - reset a blkg_stat
  382. * @stat: blkg_stat to reset
  383. */
  384. static inline void blkg_stat_reset(struct blkg_stat *stat)
  385. {
  386. stat->cnt = 0;
  387. }
  388. /**
  389. * blkg_stat_merge - merge a blkg_stat into another
  390. * @to: the destination blkg_stat
  391. * @from: the source
  392. *
  393. * Add @from's count to @to.
  394. */
  395. static inline void blkg_stat_merge(struct blkg_stat *to, struct blkg_stat *from)
  396. {
  397. blkg_stat_add(to, blkg_stat_read(from));
  398. }
  399. static inline void blkg_rwstat_init(struct blkg_rwstat *rwstat)
  400. {
  401. u64_stats_init(&rwstat->syncp);
  402. }
  403. /**
  404. * blkg_rwstat_add - add a value to a blkg_rwstat
  405. * @rwstat: target blkg_rwstat
  406. * @rw: mask of REQ_{WRITE|SYNC}
  407. * @val: value to add
  408. *
  409. * Add @val to @rwstat. The counters are chosen according to @rw. The
  410. * caller is responsible for synchronizing calls to this function.
  411. */
  412. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  413. int rw, uint64_t val)
  414. {
  415. u64_stats_update_begin(&rwstat->syncp);
  416. if (rw & REQ_WRITE)
  417. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  418. else
  419. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  420. if (rw & REQ_SYNC)
  421. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  422. else
  423. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  424. u64_stats_update_end(&rwstat->syncp);
  425. }
  426. /**
  427. * blkg_rwstat_read - read the current values of a blkg_rwstat
  428. * @rwstat: blkg_rwstat to read
  429. *
  430. * Read the current snapshot of @rwstat and return it as the return value.
  431. * This function can be called without synchronization and takes care of
  432. * u64 atomicity.
  433. */
  434. static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  435. {
  436. unsigned int start;
  437. struct blkg_rwstat tmp;
  438. do {
  439. start = u64_stats_fetch_begin_irq(&rwstat->syncp);
  440. tmp = *rwstat;
  441. } while (u64_stats_fetch_retry_irq(&rwstat->syncp, start));
  442. return tmp;
  443. }
  444. /**
  445. * blkg_rwstat_total - read the total count of a blkg_rwstat
  446. * @rwstat: blkg_rwstat to read
  447. *
  448. * Return the total count of @rwstat regardless of the IO direction. This
  449. * function can be called without synchronization and takes care of u64
  450. * atomicity.
  451. */
  452. static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat)
  453. {
  454. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  455. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  456. }
  457. /**
  458. * blkg_rwstat_reset - reset a blkg_rwstat
  459. * @rwstat: blkg_rwstat to reset
  460. */
  461. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
  462. {
  463. memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
  464. }
  465. /**
  466. * blkg_rwstat_merge - merge a blkg_rwstat into another
  467. * @to: the destination blkg_rwstat
  468. * @from: the source
  469. *
  470. * Add @from's counts to @to.
  471. */
  472. static inline void blkg_rwstat_merge(struct blkg_rwstat *to,
  473. struct blkg_rwstat *from)
  474. {
  475. struct blkg_rwstat v = blkg_rwstat_read(from);
  476. int i;
  477. u64_stats_update_begin(&to->syncp);
  478. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  479. to->cnt[i] += v.cnt[i];
  480. u64_stats_update_end(&to->syncp);
  481. }
  482. #else /* CONFIG_BLK_CGROUP */
  483. struct cgroup;
  484. struct blkcg;
  485. struct blkg_policy_data {
  486. };
  487. struct blkcg_gq {
  488. };
  489. struct blkcg_policy {
  490. };
  491. static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
  492. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  493. static inline void blkcg_drain_queue(struct request_queue *q) { }
  494. static inline void blkcg_exit_queue(struct request_queue *q) { }
  495. static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
  496. static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
  497. static inline int blkcg_activate_policy(struct request_queue *q,
  498. const struct blkcg_policy *pol) { return 0; }
  499. static inline void blkcg_deactivate_policy(struct request_queue *q,
  500. const struct blkcg_policy *pol) { }
  501. static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
  502. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  503. struct blkcg_policy *pol) { return NULL; }
  504. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
  505. static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
  506. static inline void blkg_get(struct blkcg_gq *blkg) { }
  507. static inline void blkg_put(struct blkcg_gq *blkg) { }
  508. static inline struct request_list *blk_get_rl(struct request_queue *q,
  509. struct bio *bio) { return &q->root_rl; }
  510. static inline void blk_put_rl(struct request_list *rl) { }
  511. static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) { }
  512. static inline struct request_list *blk_rq_rl(struct request *rq) { return &rq->q->root_rl; }
  513. #define blk_queue_for_each_rl(rl, q) \
  514. for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
  515. #endif /* CONFIG_BLK_CGROUP */
  516. #endif /* _BLK_CGROUP_H */