blk-cgroup.h 11 KB

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