blk-cgroup.h 9.9 KB

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