blk-cgroup.h 9.7 KB

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