blk-cgroup.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. /* used during policy activation */
  55. struct list_head alloc_node;
  56. /* pol->pdata_size bytes of private data used by policy impl */
  57. char pdata[] __aligned(__alignof__(unsigned long long));
  58. };
  59. struct blkio_group {
  60. /* Pointer to the associated request_queue */
  61. struct request_queue *q;
  62. struct list_head q_node;
  63. struct hlist_node blkcg_node;
  64. struct blkio_cgroup *blkcg;
  65. /* Store cgroup path */
  66. char path[128];
  67. /* reference count */
  68. int refcnt;
  69. struct blkg_policy_data *pd[BLKCG_MAX_POLS];
  70. struct rcu_head rcu_head;
  71. };
  72. typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
  73. typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
  74. typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
  75. struct blkio_policy_ops {
  76. blkio_init_group_fn *blkio_init_group_fn;
  77. blkio_exit_group_fn *blkio_exit_group_fn;
  78. blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
  79. };
  80. struct blkio_policy_type {
  81. struct blkio_policy_ops ops;
  82. int plid;
  83. size_t pdata_size; /* policy specific private data size */
  84. struct cftype *cftypes; /* cgroup files for the policy */
  85. };
  86. extern int blkcg_init_queue(struct request_queue *q);
  87. extern void blkcg_drain_queue(struct request_queue *q);
  88. extern void blkcg_exit_queue(struct request_queue *q);
  89. /* Blkio controller policy registration */
  90. extern int blkio_policy_register(struct blkio_policy_type *);
  91. extern void blkio_policy_unregister(struct blkio_policy_type *);
  92. extern int blkcg_activate_policy(struct request_queue *q,
  93. const struct blkio_policy_type *pol);
  94. extern void blkcg_deactivate_policy(struct request_queue *q,
  95. const struct blkio_policy_type *pol);
  96. extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
  97. void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
  98. u64 (*prfill)(struct seq_file *, void *, int),
  99. const struct blkio_policy_type *pol, int data,
  100. bool show_total);
  101. u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v);
  102. u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
  103. const struct blkg_rwstat *rwstat);
  104. u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off);
  105. u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off);
  106. struct blkg_conf_ctx {
  107. struct gendisk *disk;
  108. struct blkio_group *blkg;
  109. u64 v;
  110. };
  111. int blkg_conf_prep(struct blkio_cgroup *blkcg,
  112. const struct blkio_policy_type *pol, 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 int blkio_policy_register(struct blkio_policy_type *blkiop) { return 0; }
  282. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  283. static inline int blkcg_activate_policy(struct request_queue *q,
  284. const struct blkio_policy_type *pol) { return 0; }
  285. static inline void blkcg_deactivate_policy(struct request_queue *q,
  286. const struct blkio_policy_type *pol) { }
  287. static inline void blkg_destroy_all(struct request_queue *q,
  288. bool destory_root) { }
  289. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  290. struct blkio_policy_type *pol) { return NULL; }
  291. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  292. struct blkio_policy_type *pol) { return NULL; }
  293. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  294. static inline void blkg_get(struct blkio_group *blkg) { }
  295. static inline void blkg_put(struct blkio_group *blkg) { }
  296. #endif
  297. #ifdef CONFIG_BLK_CGROUP
  298. extern struct blkio_cgroup blkio_root_cgroup;
  299. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  300. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  301. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  302. struct request_queue *q);
  303. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  304. struct request_queue *q,
  305. bool for_root);
  306. #else
  307. struct cgroup;
  308. static inline struct blkio_cgroup *
  309. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  310. static inline struct blkio_cgroup *
  311. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  312. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  313. void *key) { return NULL; }
  314. #endif
  315. #endif /* _BLK_CGROUP_H */