blk-cgroup.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
  97. u64 (*prfill)(struct seq_file *, void *, int),
  98. const struct blkio_policy_type *pol, int data,
  99. bool show_total);
  100. u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v);
  101. u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata,
  102. const struct blkg_rwstat *rwstat);
  103. u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off);
  104. u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off);
  105. struct blkg_conf_ctx {
  106. struct gendisk *disk;
  107. struct blkio_group *blkg;
  108. u64 v;
  109. };
  110. int blkg_conf_prep(struct blkio_cgroup *blkcg,
  111. const struct blkio_policy_type *pol, const char *input,
  112. struct blkg_conf_ctx *ctx);
  113. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  114. /**
  115. * blkg_to_pdata - get policy private data
  116. * @blkg: blkg of interest
  117. * @pol: policy of interest
  118. *
  119. * Return pointer to private data associated with the @blkg-@pol pair.
  120. */
  121. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  122. struct blkio_policy_type *pol)
  123. {
  124. return blkg ? blkg->pd[pol->plid]->pdata : NULL;
  125. }
  126. /**
  127. * pdata_to_blkg - get blkg associated with policy private data
  128. * @pdata: policy private data of interest
  129. *
  130. * @pdata is policy private data. Determine the blkg it's associated with.
  131. */
  132. static inline struct blkio_group *pdata_to_blkg(void *pdata)
  133. {
  134. if (pdata) {
  135. struct blkg_policy_data *pd =
  136. container_of(pdata, struct blkg_policy_data, pdata);
  137. return pd->blkg;
  138. }
  139. return NULL;
  140. }
  141. static inline char *blkg_path(struct blkio_group *blkg)
  142. {
  143. return blkg->path;
  144. }
  145. /**
  146. * blkg_get - get a blkg reference
  147. * @blkg: blkg to get
  148. *
  149. * The caller should be holding queue_lock and an existing reference.
  150. */
  151. static inline void blkg_get(struct blkio_group *blkg)
  152. {
  153. lockdep_assert_held(blkg->q->queue_lock);
  154. WARN_ON_ONCE(!blkg->refcnt);
  155. blkg->refcnt++;
  156. }
  157. void __blkg_release(struct blkio_group *blkg);
  158. /**
  159. * blkg_put - put a blkg reference
  160. * @blkg: blkg to put
  161. *
  162. * The caller should be holding queue_lock.
  163. */
  164. static inline void blkg_put(struct blkio_group *blkg)
  165. {
  166. lockdep_assert_held(blkg->q->queue_lock);
  167. WARN_ON_ONCE(blkg->refcnt <= 0);
  168. if (!--blkg->refcnt)
  169. __blkg_release(blkg);
  170. }
  171. /**
  172. * blkg_stat_add - add a value to a blkg_stat
  173. * @stat: target blkg_stat
  174. * @val: value to add
  175. *
  176. * Add @val to @stat. The caller is responsible for synchronizing calls to
  177. * this function.
  178. */
  179. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  180. {
  181. u64_stats_update_begin(&stat->syncp);
  182. stat->cnt += val;
  183. u64_stats_update_end(&stat->syncp);
  184. }
  185. /**
  186. * blkg_stat_read - read the current value of a blkg_stat
  187. * @stat: blkg_stat to read
  188. *
  189. * Read the current value of @stat. This function can be called without
  190. * synchroniztion and takes care of u64 atomicity.
  191. */
  192. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  193. {
  194. unsigned int start;
  195. uint64_t v;
  196. do {
  197. start = u64_stats_fetch_begin(&stat->syncp);
  198. v = stat->cnt;
  199. } while (u64_stats_fetch_retry(&stat->syncp, start));
  200. return v;
  201. }
  202. /**
  203. * blkg_stat_reset - reset a blkg_stat
  204. * @stat: blkg_stat to reset
  205. */
  206. static inline void blkg_stat_reset(struct blkg_stat *stat)
  207. {
  208. stat->cnt = 0;
  209. }
  210. /**
  211. * blkg_rwstat_add - add a value to a blkg_rwstat
  212. * @rwstat: target blkg_rwstat
  213. * @rw: mask of REQ_{WRITE|SYNC}
  214. * @val: value to add
  215. *
  216. * Add @val to @rwstat. The counters are chosen according to @rw. The
  217. * caller is responsible for synchronizing calls to this function.
  218. */
  219. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  220. int rw, uint64_t val)
  221. {
  222. u64_stats_update_begin(&rwstat->syncp);
  223. if (rw & REQ_WRITE)
  224. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  225. else
  226. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  227. if (rw & REQ_SYNC)
  228. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  229. else
  230. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  231. u64_stats_update_end(&rwstat->syncp);
  232. }
  233. /**
  234. * blkg_rwstat_read - read the current values of a blkg_rwstat
  235. * @rwstat: blkg_rwstat to read
  236. *
  237. * Read the current snapshot of @rwstat and return it as the return value.
  238. * This function can be called without synchronization and takes care of
  239. * u64 atomicity.
  240. */
  241. static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  242. {
  243. unsigned int start;
  244. struct blkg_rwstat tmp;
  245. do {
  246. start = u64_stats_fetch_begin(&rwstat->syncp);
  247. tmp = *rwstat;
  248. } while (u64_stats_fetch_retry(&rwstat->syncp, start));
  249. return tmp;
  250. }
  251. /**
  252. * blkg_rwstat_sum - read the total count of a blkg_rwstat
  253. * @rwstat: blkg_rwstat to read
  254. *
  255. * Return the total count of @rwstat regardless of the IO direction. This
  256. * function can be called without synchronization and takes care of u64
  257. * atomicity.
  258. */
  259. static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
  260. {
  261. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  262. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  263. }
  264. /**
  265. * blkg_rwstat_reset - reset a blkg_rwstat
  266. * @rwstat: blkg_rwstat to reset
  267. */
  268. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
  269. {
  270. memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
  271. }
  272. #else
  273. struct blkio_group {
  274. };
  275. struct blkio_policy_type {
  276. };
  277. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  278. static inline void blkcg_drain_queue(struct request_queue *q) { }
  279. static inline void blkcg_exit_queue(struct request_queue *q) { }
  280. static inline int blkio_policy_register(struct blkio_policy_type *blkiop) { return 0; }
  281. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  282. static inline int blkcg_activate_policy(struct request_queue *q,
  283. const struct blkio_policy_type *pol) { return 0; }
  284. static inline void blkcg_deactivate_policy(struct request_queue *q,
  285. const struct blkio_policy_type *pol) { }
  286. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  287. struct blkio_policy_type *pol) { return NULL; }
  288. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  289. struct blkio_policy_type *pol) { return NULL; }
  290. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  291. static inline void blkg_get(struct blkio_group *blkg) { }
  292. static inline void blkg_put(struct blkio_group *blkg) { }
  293. #endif
  294. #ifdef CONFIG_BLK_CGROUP
  295. extern struct blkio_cgroup blkio_root_cgroup;
  296. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  297. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  298. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  299. struct request_queue *q);
  300. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  301. struct request_queue *q);
  302. #else
  303. struct cgroup;
  304. static inline struct blkio_cgroup *
  305. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  306. static inline struct blkio_cgroup *
  307. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  308. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  309. void *key) { return NULL; }
  310. #endif
  311. #endif /* _BLK_CGROUP_H */