blk-cgroup.h 10 KB

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