blk-cgroup.h 10 KB

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