blk-cgroup.h 14 KB

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