blk-cgroup.h 10 KB

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