blk-cgroup.h 9.9 KB

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