blk-cgroup.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. /* Max limits for throttle policy */
  21. #define THROTL_IOPS_MAX UINT_MAX
  22. /* CFQ specific, out here for blkcg->cfq_weight */
  23. #define CFQ_WEIGHT_MIN 10
  24. #define CFQ_WEIGHT_MAX 1000
  25. #define CFQ_WEIGHT_DEFAULT 500
  26. #ifdef CONFIG_BLK_CGROUP
  27. enum blkg_rwstat_type {
  28. BLKG_RWSTAT_READ,
  29. BLKG_RWSTAT_WRITE,
  30. BLKG_RWSTAT_SYNC,
  31. BLKG_RWSTAT_ASYNC,
  32. BLKG_RWSTAT_NR,
  33. BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
  34. };
  35. struct blkcg_gq;
  36. struct blkcg {
  37. struct cgroup_subsys_state css;
  38. spinlock_t lock;
  39. struct radix_tree_root blkg_tree;
  40. struct blkcg_gq *blkg_hint;
  41. struct hlist_head blkg_list;
  42. /* for policies to test whether associated blkcg has changed */
  43. uint64_t id;
  44. /* TODO: per-policy storage in blkcg */
  45. unsigned int cfq_weight; /* belongs to cfq */
  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 this per-policy data belongs to */
  70. struct blkcg_gq *blkg;
  71. /* used during policy activation */
  72. struct list_head alloc_node;
  73. };
  74. /* association between a blk cgroup and a request queue */
  75. struct blkcg_gq {
  76. /* Pointer to the associated request_queue */
  77. struct request_queue *q;
  78. struct list_head q_node;
  79. struct hlist_node blkcg_node;
  80. struct blkcg *blkcg;
  81. /* all non-root blkcg_gq's are guaranteed to have access to parent */
  82. struct blkcg_gq *parent;
  83. /* request allocation list for this blkcg-q pair */
  84. struct request_list rl;
  85. /* reference count */
  86. int refcnt;
  87. struct blkg_policy_data *pd[BLKCG_MAX_POLS];
  88. struct rcu_head rcu_head;
  89. };
  90. typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
  91. typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg);
  92. typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg);
  93. struct blkcg_policy {
  94. int plid;
  95. /* policy specific private data size */
  96. size_t pd_size;
  97. /* cgroup files for the policy */
  98. struct cftype *cftypes;
  99. /* operations */
  100. blkcg_pol_init_pd_fn *pd_init_fn;
  101. blkcg_pol_exit_pd_fn *pd_exit_fn;
  102. blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
  103. };
  104. extern struct blkcg blkcg_root;
  105. struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q);
  106. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  107. struct request_queue *q);
  108. int blkcg_init_queue(struct request_queue *q);
  109. void blkcg_drain_queue(struct request_queue *q);
  110. void blkcg_exit_queue(struct request_queue *q);
  111. /* Blkio controller policy registration */
  112. int blkcg_policy_register(struct blkcg_policy *pol);
  113. void blkcg_policy_unregister(struct blkcg_policy *pol);
  114. int blkcg_activate_policy(struct request_queue *q,
  115. const struct blkcg_policy *pol);
  116. void blkcg_deactivate_policy(struct request_queue *q,
  117. const struct blkcg_policy *pol);
  118. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  119. u64 (*prfill)(struct seq_file *,
  120. struct blkg_policy_data *, int),
  121. const struct blkcg_policy *pol, int data,
  122. bool show_total);
  123. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  124. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  125. const struct blkg_rwstat *rwstat);
  126. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
  127. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  128. int off);
  129. struct blkg_conf_ctx {
  130. struct gendisk *disk;
  131. struct blkcg_gq *blkg;
  132. u64 v;
  133. };
  134. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  135. const char *input, struct blkg_conf_ctx *ctx);
  136. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  137. static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup)
  138. {
  139. return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
  140. struct blkcg, css);
  141. }
  142. static inline struct blkcg *task_blkcg(struct task_struct *tsk)
  143. {
  144. return container_of(task_subsys_state(tsk, blkio_subsys_id),
  145. struct blkcg, css);
  146. }
  147. static inline struct blkcg *bio_blkcg(struct bio *bio)
  148. {
  149. if (bio && bio->bi_css)
  150. return container_of(bio->bi_css, struct blkcg, css);
  151. return task_blkcg(current);
  152. }
  153. /**
  154. * blkcg_parent - get the parent of a blkcg
  155. * @blkcg: blkcg of interest
  156. *
  157. * Return the parent blkcg of @blkcg. Can be called anytime.
  158. */
  159. static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
  160. {
  161. struct cgroup *pcg = blkcg->css.cgroup->parent;
  162. return pcg ? cgroup_to_blkcg(pcg) : NULL;
  163. }
  164. /**
  165. * blkg_to_pdata - get policy private data
  166. * @blkg: blkg of interest
  167. * @pol: policy of interest
  168. *
  169. * Return pointer to private data associated with the @blkg-@pol pair.
  170. */
  171. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  172. struct blkcg_policy *pol)
  173. {
  174. return blkg ? blkg->pd[pol->plid] : NULL;
  175. }
  176. /**
  177. * pdata_to_blkg - get blkg associated with policy private data
  178. * @pd: policy private data of interest
  179. *
  180. * @pd is policy private data. Determine the blkg it's associated with.
  181. */
  182. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
  183. {
  184. return pd ? pd->blkg : NULL;
  185. }
  186. /**
  187. * blkg_path - format cgroup path of blkg
  188. * @blkg: blkg of interest
  189. * @buf: target buffer
  190. * @buflen: target buffer length
  191. *
  192. * Format the path of the cgroup of @blkg into @buf.
  193. */
  194. static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
  195. {
  196. int ret;
  197. rcu_read_lock();
  198. ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
  199. rcu_read_unlock();
  200. if (ret)
  201. strncpy(buf, "<unavailable>", buflen);
  202. return ret;
  203. }
  204. /**
  205. * blkg_get - get a blkg reference
  206. * @blkg: blkg to get
  207. *
  208. * The caller should be holding queue_lock and an existing reference.
  209. */
  210. static inline void blkg_get(struct blkcg_gq *blkg)
  211. {
  212. lockdep_assert_held(blkg->q->queue_lock);
  213. WARN_ON_ONCE(!blkg->refcnt);
  214. blkg->refcnt++;
  215. }
  216. void __blkg_release(struct blkcg_gq *blkg);
  217. /**
  218. * blkg_put - put a blkg reference
  219. * @blkg: blkg to put
  220. *
  221. * The caller should be holding queue_lock.
  222. */
  223. static inline void blkg_put(struct blkcg_gq *blkg)
  224. {
  225. lockdep_assert_held(blkg->q->queue_lock);
  226. WARN_ON_ONCE(blkg->refcnt <= 0);
  227. if (!--blkg->refcnt)
  228. __blkg_release(blkg);
  229. }
  230. /**
  231. * blk_get_rl - get request_list to use
  232. * @q: request_queue of interest
  233. * @bio: bio which will be attached to the allocated request (may be %NULL)
  234. *
  235. * The caller wants to allocate a request from @q to use for @bio. Find
  236. * the request_list to use and obtain a reference on it. Should be called
  237. * under queue_lock. This function is guaranteed to return non-%NULL
  238. * request_list.
  239. */
  240. static inline struct request_list *blk_get_rl(struct request_queue *q,
  241. struct bio *bio)
  242. {
  243. struct blkcg *blkcg;
  244. struct blkcg_gq *blkg;
  245. rcu_read_lock();
  246. blkcg = bio_blkcg(bio);
  247. /* bypass blkg lookup and use @q->root_rl directly for root */
  248. if (blkcg == &blkcg_root)
  249. goto root_rl;
  250. /*
  251. * Try to use blkg->rl. blkg lookup may fail under memory pressure
  252. * or if either the blkcg or queue is going away. Fall back to
  253. * root_rl in such cases.
  254. */
  255. blkg = blkg_lookup_create(blkcg, q);
  256. if (unlikely(IS_ERR(blkg)))
  257. goto root_rl;
  258. blkg_get(blkg);
  259. rcu_read_unlock();
  260. return &blkg->rl;
  261. root_rl:
  262. rcu_read_unlock();
  263. return &q->root_rl;
  264. }
  265. /**
  266. * blk_put_rl - put request_list
  267. * @rl: request_list to put
  268. *
  269. * Put the reference acquired by blk_get_rl(). Should be called under
  270. * queue_lock.
  271. */
  272. static inline void blk_put_rl(struct request_list *rl)
  273. {
  274. /* root_rl may not have blkg set */
  275. if (rl->blkg && rl->blkg->blkcg != &blkcg_root)
  276. blkg_put(rl->blkg);
  277. }
  278. /**
  279. * blk_rq_set_rl - associate a request with a request_list
  280. * @rq: request of interest
  281. * @rl: target request_list
  282. *
  283. * Associate @rq with @rl so that accounting and freeing can know the
  284. * request_list @rq came from.
  285. */
  286. static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl)
  287. {
  288. rq->rl = rl;
  289. }
  290. /**
  291. * blk_rq_rl - return the request_list a request came from
  292. * @rq: request of interest
  293. *
  294. * Return the request_list @rq is allocated from.
  295. */
  296. static inline struct request_list *blk_rq_rl(struct request *rq)
  297. {
  298. return rq->rl;
  299. }
  300. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  301. struct request_queue *q);
  302. /**
  303. * blk_queue_for_each_rl - iterate through all request_lists of a request_queue
  304. *
  305. * Should be used under queue_lock.
  306. */
  307. #define blk_queue_for_each_rl(rl, q) \
  308. for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q)))
  309. /**
  310. * blkg_stat_add - add a value to a blkg_stat
  311. * @stat: target blkg_stat
  312. * @val: value to add
  313. *
  314. * Add @val to @stat. The caller is responsible for synchronizing calls to
  315. * this function.
  316. */
  317. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  318. {
  319. u64_stats_update_begin(&stat->syncp);
  320. stat->cnt += val;
  321. u64_stats_update_end(&stat->syncp);
  322. }
  323. /**
  324. * blkg_stat_read - read the current value of a blkg_stat
  325. * @stat: blkg_stat to read
  326. *
  327. * Read the current value of @stat. This function can be called without
  328. * synchroniztion and takes care of u64 atomicity.
  329. */
  330. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  331. {
  332. unsigned int start;
  333. uint64_t v;
  334. do {
  335. start = u64_stats_fetch_begin(&stat->syncp);
  336. v = stat->cnt;
  337. } while (u64_stats_fetch_retry(&stat->syncp, start));
  338. return v;
  339. }
  340. /**
  341. * blkg_stat_reset - reset a blkg_stat
  342. * @stat: blkg_stat to reset
  343. */
  344. static inline void blkg_stat_reset(struct blkg_stat *stat)
  345. {
  346. stat->cnt = 0;
  347. }
  348. /**
  349. * blkg_rwstat_add - add a value to a blkg_rwstat
  350. * @rwstat: target blkg_rwstat
  351. * @rw: mask of REQ_{WRITE|SYNC}
  352. * @val: value to add
  353. *
  354. * Add @val to @rwstat. The counters are chosen according to @rw. The
  355. * caller is responsible for synchronizing calls to this function.
  356. */
  357. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  358. int rw, uint64_t val)
  359. {
  360. u64_stats_update_begin(&rwstat->syncp);
  361. if (rw & REQ_WRITE)
  362. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  363. else
  364. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  365. if (rw & REQ_SYNC)
  366. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  367. else
  368. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  369. u64_stats_update_end(&rwstat->syncp);
  370. }
  371. /**
  372. * blkg_rwstat_read - read the current values of a blkg_rwstat
  373. * @rwstat: blkg_rwstat to read
  374. *
  375. * Read the current snapshot of @rwstat and return it as the return value.
  376. * This function can be called without synchronization and takes care of
  377. * u64 atomicity.
  378. */
  379. static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  380. {
  381. unsigned int start;
  382. struct blkg_rwstat tmp;
  383. do {
  384. start = u64_stats_fetch_begin(&rwstat->syncp);
  385. tmp = *rwstat;
  386. } while (u64_stats_fetch_retry(&rwstat->syncp, start));
  387. return tmp;
  388. }
  389. /**
  390. * blkg_rwstat_sum - read the total count of a blkg_rwstat
  391. * @rwstat: blkg_rwstat to read
  392. *
  393. * Return the total count of @rwstat regardless of the IO direction. This
  394. * function can be called without synchronization and takes care of u64
  395. * atomicity.
  396. */
  397. static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
  398. {
  399. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  400. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  401. }
  402. /**
  403. * blkg_rwstat_reset - reset a blkg_rwstat
  404. * @rwstat: blkg_rwstat to reset
  405. */
  406. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
  407. {
  408. memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
  409. }
  410. #else /* CONFIG_BLK_CGROUP */
  411. struct cgroup;
  412. struct blkcg;
  413. struct blkg_policy_data {
  414. };
  415. struct blkcg_gq {
  416. };
  417. struct blkcg_policy {
  418. };
  419. static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
  420. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  421. static inline void blkcg_drain_queue(struct request_queue *q) { }
  422. static inline void blkcg_exit_queue(struct request_queue *q) { }
  423. static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
  424. static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
  425. static inline int blkcg_activate_policy(struct request_queue *q,
  426. const struct blkcg_policy *pol) { return 0; }
  427. static inline void blkcg_deactivate_policy(struct request_queue *q,
  428. const struct blkcg_policy *pol) { }
  429. static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup) { return NULL; }
  430. static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
  431. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  432. struct blkcg_policy *pol) { return NULL; }
  433. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
  434. static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
  435. static inline void blkg_get(struct blkcg_gq *blkg) { }
  436. static inline void blkg_put(struct blkcg_gq *blkg) { }
  437. static inline struct request_list *blk_get_rl(struct request_queue *q,
  438. struct bio *bio) { return &q->root_rl; }
  439. static inline void blk_put_rl(struct request_list *rl) { }
  440. static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) { }
  441. static inline struct request_list *blk_rq_rl(struct request *rq) { return &rq->q->root_rl; }
  442. #define blk_queue_for_each_rl(rl, q) \
  443. for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
  444. #endif /* CONFIG_BLK_CGROUP */
  445. #endif /* _BLK_CGROUP_H */