blk-cgroup.h 14 KB

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