blk-cgroup.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. enum blkio_policy_id {
  19. BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
  20. BLKIO_POLICY_THROTL, /* Throttling */
  21. BLKIO_NR_POLICIES,
  22. };
  23. /* Max limits for throttle policy */
  24. #define THROTL_IOPS_MAX UINT_MAX
  25. #ifdef CONFIG_BLK_CGROUP
  26. /* CFQ specific, out here for blkcg->cfq_weight */
  27. #define CFQ_WEIGHT_MIN 10
  28. #define CFQ_WEIGHT_MAX 1000
  29. #define CFQ_WEIGHT_DEFAULT 500
  30. /* cft->private [un]packing for stat printing */
  31. #define BLKCG_STAT_PRIV(pol, off) (((unsigned)(pol) << 16) | (off))
  32. #define BLKCG_STAT_POL(prv) ((unsigned)(prv) >> 16)
  33. #define BLKCG_STAT_OFF(prv) ((unsigned)(prv) & 0xffff)
  34. enum blkg_rwstat_type {
  35. BLKG_RWSTAT_READ,
  36. BLKG_RWSTAT_WRITE,
  37. BLKG_RWSTAT_SYNC,
  38. BLKG_RWSTAT_ASYNC,
  39. BLKG_RWSTAT_NR,
  40. BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
  41. };
  42. struct blkio_cgroup {
  43. struct cgroup_subsys_state css;
  44. spinlock_t lock;
  45. struct hlist_head blkg_list;
  46. /* for policies to test whether associated blkcg has changed */
  47. uint64_t id;
  48. /* TODO: per-policy storage in blkio_cgroup */
  49. unsigned int cfq_weight; /* belongs to cfq */
  50. };
  51. struct blkg_stat {
  52. struct u64_stats_sync syncp;
  53. uint64_t cnt;
  54. };
  55. struct blkg_rwstat {
  56. struct u64_stats_sync syncp;
  57. uint64_t cnt[BLKG_RWSTAT_NR];
  58. };
  59. struct blkio_group_conf {
  60. u64 iops[2];
  61. u64 bps[2];
  62. };
  63. /* per-blkg per-policy data */
  64. struct blkg_policy_data {
  65. /* the blkg this per-policy data belongs to */
  66. struct blkio_group *blkg;
  67. /* Configuration */
  68. struct blkio_group_conf conf;
  69. /* pol->pdata_size bytes of private data used by policy impl */
  70. char pdata[] __aligned(__alignof__(unsigned long long));
  71. };
  72. struct blkio_group {
  73. /* Pointer to the associated request_queue */
  74. struct request_queue *q;
  75. struct list_head q_node;
  76. struct hlist_node blkcg_node;
  77. struct blkio_cgroup *blkcg;
  78. /* Store cgroup path */
  79. char path[128];
  80. /* reference count */
  81. int refcnt;
  82. struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
  83. struct rcu_head rcu_head;
  84. };
  85. typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
  86. typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
  87. typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
  88. struct blkio_policy_ops {
  89. blkio_init_group_fn *blkio_init_group_fn;
  90. blkio_exit_group_fn *blkio_exit_group_fn;
  91. blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
  92. };
  93. struct blkio_policy_type {
  94. struct list_head list;
  95. struct blkio_policy_ops ops;
  96. enum blkio_policy_id plid;
  97. size_t pdata_size; /* policy specific private data size */
  98. struct cftype *cftypes; /* cgroup files for the policy */
  99. };
  100. extern int blkcg_init_queue(struct request_queue *q);
  101. extern void blkcg_drain_queue(struct request_queue *q);
  102. extern void blkcg_exit_queue(struct request_queue *q);
  103. /* Blkio controller policy registration */
  104. extern void blkio_policy_register(struct blkio_policy_type *);
  105. extern void blkio_policy_unregister(struct blkio_policy_type *);
  106. extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
  107. extern void update_root_blkg_pd(struct request_queue *q,
  108. enum blkio_policy_id plid);
  109. void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
  110. u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
  111. int pol, int data, bool show_total);
  112. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  113. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  114. const struct blkg_rwstat *rwstat);
  115. int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
  116. struct seq_file *sf);
  117. int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
  118. struct seq_file *sf);
  119. struct blkg_conf_ctx {
  120. struct gendisk *disk;
  121. struct blkio_group *blkg;
  122. u64 v;
  123. };
  124. int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
  125. struct blkg_conf_ctx *ctx);
  126. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  127. /**
  128. * blkg_to_pdata - get policy private data
  129. * @blkg: blkg of interest
  130. * @pol: policy of interest
  131. *
  132. * Return pointer to private data associated with the @blkg-@pol pair.
  133. */
  134. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  135. struct blkio_policy_type *pol)
  136. {
  137. return blkg ? blkg->pd[pol->plid]->pdata : NULL;
  138. }
  139. /**
  140. * pdata_to_blkg - get blkg associated with policy private data
  141. * @pdata: policy private data of interest
  142. *
  143. * @pdata is policy private data. Determine the blkg it's associated with.
  144. */
  145. static inline struct blkio_group *pdata_to_blkg(void *pdata)
  146. {
  147. if (pdata) {
  148. struct blkg_policy_data *pd =
  149. container_of(pdata, struct blkg_policy_data, pdata);
  150. return pd->blkg;
  151. }
  152. return NULL;
  153. }
  154. static inline char *blkg_path(struct blkio_group *blkg)
  155. {
  156. return blkg->path;
  157. }
  158. /**
  159. * blkg_get - get a blkg reference
  160. * @blkg: blkg to get
  161. *
  162. * The caller should be holding queue_lock and an existing reference.
  163. */
  164. static inline void blkg_get(struct blkio_group *blkg)
  165. {
  166. lockdep_assert_held(blkg->q->queue_lock);
  167. WARN_ON_ONCE(!blkg->refcnt);
  168. blkg->refcnt++;
  169. }
  170. void __blkg_release(struct blkio_group *blkg);
  171. /**
  172. * blkg_put - put a blkg reference
  173. * @blkg: blkg to put
  174. *
  175. * The caller should be holding queue_lock.
  176. */
  177. static inline void blkg_put(struct blkio_group *blkg)
  178. {
  179. lockdep_assert_held(blkg->q->queue_lock);
  180. WARN_ON_ONCE(blkg->refcnt <= 0);
  181. if (!--blkg->refcnt)
  182. __blkg_release(blkg);
  183. }
  184. /**
  185. * blkg_stat_add - add a value to a blkg_stat
  186. * @stat: target blkg_stat
  187. * @val: value to add
  188. *
  189. * Add @val to @stat. The caller is responsible for synchronizing calls to
  190. * this function.
  191. */
  192. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  193. {
  194. u64_stats_update_begin(&stat->syncp);
  195. stat->cnt += val;
  196. u64_stats_update_end(&stat->syncp);
  197. }
  198. /**
  199. * blkg_stat_read - read the current value of a blkg_stat
  200. * @stat: blkg_stat to read
  201. *
  202. * Read the current value of @stat. This function can be called without
  203. * synchroniztion and takes care of u64 atomicity.
  204. */
  205. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  206. {
  207. unsigned int start;
  208. uint64_t v;
  209. do {
  210. start = u64_stats_fetch_begin(&stat->syncp);
  211. v = stat->cnt;
  212. } while (u64_stats_fetch_retry(&stat->syncp, start));
  213. return v;
  214. }
  215. /**
  216. * blkg_stat_reset - reset a blkg_stat
  217. * @stat: blkg_stat to reset
  218. */
  219. static inline void blkg_stat_reset(struct blkg_stat *stat)
  220. {
  221. stat->cnt = 0;
  222. }
  223. /**
  224. * blkg_rwstat_add - add a value to a blkg_rwstat
  225. * @rwstat: target blkg_rwstat
  226. * @rw: mask of REQ_{WRITE|SYNC}
  227. * @val: value to add
  228. *
  229. * Add @val to @rwstat. The counters are chosen according to @rw. The
  230. * caller is responsible for synchronizing calls to this function.
  231. */
  232. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  233. int rw, uint64_t val)
  234. {
  235. u64_stats_update_begin(&rwstat->syncp);
  236. if (rw & REQ_WRITE)
  237. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  238. else
  239. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  240. if (rw & REQ_SYNC)
  241. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  242. else
  243. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  244. u64_stats_update_end(&rwstat->syncp);
  245. }
  246. /**
  247. * blkg_rwstat_read - read the current values of a blkg_rwstat
  248. * @rwstat: blkg_rwstat to read
  249. *
  250. * Read the current snapshot of @rwstat and return it as the return value.
  251. * This function can be called without synchronization and takes care of
  252. * u64 atomicity.
  253. */
  254. static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  255. {
  256. unsigned int start;
  257. struct blkg_rwstat tmp;
  258. do {
  259. start = u64_stats_fetch_begin(&rwstat->syncp);
  260. tmp = *rwstat;
  261. } while (u64_stats_fetch_retry(&rwstat->syncp, start));
  262. return tmp;
  263. }
  264. /**
  265. * blkg_rwstat_sum - read the total count of a blkg_rwstat
  266. * @rwstat: blkg_rwstat to read
  267. *
  268. * Return the total count of @rwstat regardless of the IO direction. This
  269. * function can be called without synchronization and takes care of u64
  270. * atomicity.
  271. */
  272. static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
  273. {
  274. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  275. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  276. }
  277. /**
  278. * blkg_rwstat_reset - reset a blkg_rwstat
  279. * @rwstat: blkg_rwstat to reset
  280. */
  281. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
  282. {
  283. memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
  284. }
  285. #else
  286. struct blkio_group {
  287. };
  288. struct blkio_policy_type {
  289. };
  290. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  291. static inline void blkcg_drain_queue(struct request_queue *q) { }
  292. static inline void blkcg_exit_queue(struct request_queue *q) { }
  293. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  294. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  295. static inline void blkg_destroy_all(struct request_queue *q,
  296. bool destory_root) { }
  297. static inline void update_root_blkg_pd(struct request_queue *q,
  298. enum blkio_policy_id plid) { }
  299. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  300. struct blkio_policy_type *pol) { return NULL; }
  301. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  302. struct blkio_policy_type *pol) { return NULL; }
  303. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  304. static inline void blkg_get(struct blkio_group *blkg) { }
  305. static inline void blkg_put(struct blkio_group *blkg) { }
  306. #endif
  307. #ifdef CONFIG_BLK_CGROUP
  308. extern struct blkio_cgroup blkio_root_cgroup;
  309. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  310. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  311. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  312. struct request_queue *q);
  313. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  314. struct request_queue *q,
  315. bool for_root);
  316. #else
  317. struct cgroup;
  318. static inline struct blkio_cgroup *
  319. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  320. static inline struct blkio_cgroup *
  321. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  322. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  323. void *key) { return NULL; }
  324. #endif
  325. #endif /* _BLK_CGROUP_H */