blk-cgroup.h 10 KB

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