blk-cgroup.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. enum blkio_policy_id {
  19. BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
  20. BLKIO_POLICY_THROTL, /* Throttling */
  21. BLKIO_NR_POLICIES,
  22. };
  23. /* Max limits for throttle policy */
  24. #define THROTL_IOPS_MAX UINT_MAX
  25. #ifdef CONFIG_BLK_CGROUP
  26. /* cft->private [un]packing for stat printing */
  27. #define BLKCG_STAT_PRIV(pol, off) (((unsigned)(pol) << 16) | (off))
  28. #define BLKCG_STAT_POL(prv) ((unsigned)(prv) >> 16)
  29. #define BLKCG_STAT_OFF(prv) ((unsigned)(prv) & 0xffff)
  30. enum blkg_rwstat_type {
  31. BLKG_RWSTAT_READ,
  32. BLKG_RWSTAT_WRITE,
  33. BLKG_RWSTAT_SYNC,
  34. BLKG_RWSTAT_ASYNC,
  35. BLKG_RWSTAT_NR,
  36. BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
  37. };
  38. struct blkio_cgroup {
  39. struct cgroup_subsys_state css;
  40. unsigned int weight;
  41. spinlock_t lock;
  42. struct hlist_head blkg_list;
  43. /* for policies to test whether associated blkcg has changed */
  44. uint64_t id;
  45. };
  46. struct blkg_stat {
  47. struct u64_stats_sync syncp;
  48. uint64_t cnt;
  49. };
  50. struct blkg_rwstat {
  51. struct u64_stats_sync syncp;
  52. uint64_t cnt[BLKG_RWSTAT_NR];
  53. };
  54. struct blkio_group_stats {
  55. /* total bytes transferred */
  56. struct blkg_rwstat service_bytes;
  57. /* total IOs serviced, post merge */
  58. struct blkg_rwstat serviced;
  59. /* number of ios merged */
  60. struct blkg_rwstat merged;
  61. /* total time spent on device in ns, may not be accurate w/ queueing */
  62. struct blkg_rwstat service_time;
  63. /* total time spent waiting in scheduler queue in ns */
  64. struct blkg_rwstat wait_time;
  65. /* number of IOs queued up */
  66. struct blkg_rwstat queued;
  67. /* total sectors transferred */
  68. struct blkg_stat sectors;
  69. /* total disk time and nr sectors dispatched by this group */
  70. struct blkg_stat time;
  71. #ifdef CONFIG_DEBUG_BLK_CGROUP
  72. /* time not charged to this cgroup */
  73. struct blkg_stat unaccounted_time;
  74. /* sum of number of ios queued across all samples */
  75. struct blkg_stat avg_queue_size_sum;
  76. /* count of samples taken for average */
  77. struct blkg_stat avg_queue_size_samples;
  78. /* how many times this group has been removed from service tree */
  79. struct blkg_stat dequeue;
  80. /* total time spent waiting for it to be assigned a timeslice. */
  81. struct blkg_stat group_wait_time;
  82. /* time spent idling for this blkio_group */
  83. struct blkg_stat idle_time;
  84. /* total time with empty current active q with other requests queued */
  85. struct blkg_stat empty_time;
  86. /* fields after this shouldn't be cleared on stat reset */
  87. uint64_t start_group_wait_time;
  88. uint64_t start_idle_time;
  89. uint64_t start_empty_time;
  90. uint16_t flags;
  91. #endif
  92. };
  93. /* Per cpu blkio group stats */
  94. struct blkio_group_stats_cpu {
  95. /* total bytes transferred */
  96. struct blkg_rwstat service_bytes;
  97. /* total IOs serviced, post merge */
  98. struct blkg_rwstat serviced;
  99. };
  100. struct blkio_group_conf {
  101. unsigned int weight;
  102. u64 iops[2];
  103. u64 bps[2];
  104. };
  105. /* per-blkg per-policy data */
  106. struct blkg_policy_data {
  107. /* the blkg this per-policy data belongs to */
  108. struct blkio_group *blkg;
  109. /* Configuration */
  110. struct blkio_group_conf conf;
  111. struct blkio_group_stats stats;
  112. /* Per cpu stats pointer */
  113. struct blkio_group_stats_cpu __percpu *stats_cpu;
  114. /* pol->pdata_size bytes of private data used by policy impl */
  115. char pdata[] __aligned(__alignof__(unsigned long long));
  116. };
  117. struct blkio_group {
  118. /* Pointer to the associated request_queue */
  119. struct request_queue *q;
  120. struct list_head q_node;
  121. struct hlist_node blkcg_node;
  122. struct blkio_cgroup *blkcg;
  123. /* Store cgroup path */
  124. char path[128];
  125. /* reference count */
  126. int refcnt;
  127. struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
  128. /* List of blkg waiting for per cpu stats memory to be allocated */
  129. struct list_head alloc_node;
  130. struct rcu_head rcu_head;
  131. };
  132. typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
  133. typedef void (blkio_exit_group_fn)(struct blkio_group *blkg);
  134. typedef void (blkio_reset_group_stats_fn)(struct blkio_group *blkg);
  135. struct blkio_policy_ops {
  136. blkio_init_group_fn *blkio_init_group_fn;
  137. blkio_exit_group_fn *blkio_exit_group_fn;
  138. blkio_reset_group_stats_fn *blkio_reset_group_stats_fn;
  139. };
  140. struct blkio_policy_type {
  141. struct list_head list;
  142. struct blkio_policy_ops ops;
  143. enum blkio_policy_id plid;
  144. size_t pdata_size; /* policy specific private data size */
  145. struct cftype *cftypes; /* cgroup files for the policy */
  146. };
  147. extern int blkcg_init_queue(struct request_queue *q);
  148. extern void blkcg_drain_queue(struct request_queue *q);
  149. extern void blkcg_exit_queue(struct request_queue *q);
  150. /* Blkio controller policy registration */
  151. extern void blkio_policy_register(struct blkio_policy_type *);
  152. extern void blkio_policy_unregister(struct blkio_policy_type *);
  153. extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
  154. extern void update_root_blkg_pd(struct request_queue *q,
  155. enum blkio_policy_id plid);
  156. void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
  157. u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
  158. int pol, int data, bool show_total);
  159. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  160. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  161. const struct blkg_rwstat *rwstat);
  162. int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
  163. struct seq_file *sf);
  164. int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
  165. struct seq_file *sf);
  166. struct blkg_conf_ctx {
  167. struct gendisk *disk;
  168. struct blkio_group *blkg;
  169. u64 v;
  170. };
  171. int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
  172. struct blkg_conf_ctx *ctx);
  173. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  174. /**
  175. * blkg_to_pdata - get policy private data
  176. * @blkg: blkg of interest
  177. * @pol: policy of interest
  178. *
  179. * Return pointer to private data associated with the @blkg-@pol pair.
  180. */
  181. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  182. struct blkio_policy_type *pol)
  183. {
  184. return blkg ? blkg->pd[pol->plid]->pdata : NULL;
  185. }
  186. /**
  187. * pdata_to_blkg - get blkg associated with policy private data
  188. * @pdata: policy private data of interest
  189. *
  190. * @pdata is policy private data. Determine the blkg it's associated with.
  191. */
  192. static inline struct blkio_group *pdata_to_blkg(void *pdata)
  193. {
  194. if (pdata) {
  195. struct blkg_policy_data *pd =
  196. container_of(pdata, struct blkg_policy_data, pdata);
  197. return pd->blkg;
  198. }
  199. return NULL;
  200. }
  201. static inline char *blkg_path(struct blkio_group *blkg)
  202. {
  203. return blkg->path;
  204. }
  205. /**
  206. * blkg_get - get a blkg reference
  207. * @blkg: blkg to get
  208. *
  209. * The caller should be holding queue_lock and an existing reference.
  210. */
  211. static inline void blkg_get(struct blkio_group *blkg)
  212. {
  213. lockdep_assert_held(blkg->q->queue_lock);
  214. WARN_ON_ONCE(!blkg->refcnt);
  215. blkg->refcnt++;
  216. }
  217. void __blkg_release(struct blkio_group *blkg);
  218. /**
  219. * blkg_put - put a blkg reference
  220. * @blkg: blkg to put
  221. *
  222. * The caller should be holding queue_lock.
  223. */
  224. static inline void blkg_put(struct blkio_group *blkg)
  225. {
  226. lockdep_assert_held(blkg->q->queue_lock);
  227. WARN_ON_ONCE(blkg->refcnt <= 0);
  228. if (!--blkg->refcnt)
  229. __blkg_release(blkg);
  230. }
  231. /**
  232. * blkg_stat_add - add a value to a blkg_stat
  233. * @stat: target blkg_stat
  234. * @val: value to add
  235. *
  236. * Add @val to @stat. The caller is responsible for synchronizing calls to
  237. * this function.
  238. */
  239. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  240. {
  241. u64_stats_update_begin(&stat->syncp);
  242. stat->cnt += val;
  243. u64_stats_update_end(&stat->syncp);
  244. }
  245. /**
  246. * blkg_stat_read - read the current value of a blkg_stat
  247. * @stat: blkg_stat to read
  248. *
  249. * Read the current value of @stat. This function can be called without
  250. * synchroniztion and takes care of u64 atomicity.
  251. */
  252. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  253. {
  254. unsigned int start;
  255. uint64_t v;
  256. do {
  257. start = u64_stats_fetch_begin(&stat->syncp);
  258. v = stat->cnt;
  259. } while (u64_stats_fetch_retry(&stat->syncp, start));
  260. return v;
  261. }
  262. /**
  263. * blkg_stat_reset - reset a blkg_stat
  264. * @stat: blkg_stat to reset
  265. */
  266. static inline void blkg_stat_reset(struct blkg_stat *stat)
  267. {
  268. stat->cnt = 0;
  269. }
  270. /**
  271. * blkg_rwstat_add - add a value to a blkg_rwstat
  272. * @rwstat: target blkg_rwstat
  273. * @rw: mask of REQ_{WRITE|SYNC}
  274. * @val: value to add
  275. *
  276. * Add @val to @rwstat. The counters are chosen according to @rw. The
  277. * caller is responsible for synchronizing calls to this function.
  278. */
  279. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  280. int rw, uint64_t val)
  281. {
  282. u64_stats_update_begin(&rwstat->syncp);
  283. if (rw & REQ_WRITE)
  284. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  285. else
  286. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  287. if (rw & REQ_SYNC)
  288. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  289. else
  290. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  291. u64_stats_update_end(&rwstat->syncp);
  292. }
  293. /**
  294. * blkg_rwstat_read - read the current values of a blkg_rwstat
  295. * @rwstat: blkg_rwstat to read
  296. *
  297. * Read the current snapshot of @rwstat and return it as the return value.
  298. * This function can be called without synchronization and takes care of
  299. * u64 atomicity.
  300. */
  301. static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  302. {
  303. unsigned int start;
  304. struct blkg_rwstat tmp;
  305. do {
  306. start = u64_stats_fetch_begin(&rwstat->syncp);
  307. tmp = *rwstat;
  308. } while (u64_stats_fetch_retry(&rwstat->syncp, start));
  309. return tmp;
  310. }
  311. /**
  312. * blkg_rwstat_sum - read the total count of a blkg_rwstat
  313. * @rwstat: blkg_rwstat to read
  314. *
  315. * Return the total count of @rwstat regardless of the IO direction. This
  316. * function can be called without synchronization and takes care of u64
  317. * atomicity.
  318. */
  319. static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
  320. {
  321. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  322. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  323. }
  324. /**
  325. * blkg_rwstat_reset - reset a blkg_rwstat
  326. * @rwstat: blkg_rwstat to reset
  327. */
  328. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
  329. {
  330. memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
  331. }
  332. #else
  333. struct blkio_group {
  334. };
  335. struct blkio_policy_type {
  336. };
  337. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  338. static inline void blkcg_drain_queue(struct request_queue *q) { }
  339. static inline void blkcg_exit_queue(struct request_queue *q) { }
  340. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  341. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  342. static inline void blkg_destroy_all(struct request_queue *q,
  343. bool destory_root) { }
  344. static inline void update_root_blkg_pd(struct request_queue *q,
  345. enum blkio_policy_id plid) { }
  346. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  347. struct blkio_policy_type *pol) { return NULL; }
  348. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  349. struct blkio_policy_type *pol) { return NULL; }
  350. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  351. static inline void blkg_get(struct blkio_group *blkg) { }
  352. static inline void blkg_put(struct blkio_group *blkg) { }
  353. #endif
  354. #define BLKIO_WEIGHT_MIN 10
  355. #define BLKIO_WEIGHT_MAX 1000
  356. #define BLKIO_WEIGHT_DEFAULT 500
  357. #ifdef CONFIG_BLK_CGROUP
  358. extern struct blkio_cgroup blkio_root_cgroup;
  359. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  360. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  361. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  362. struct request_queue *q);
  363. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  364. struct request_queue *q,
  365. bool for_root);
  366. #else
  367. struct cgroup;
  368. static inline struct blkio_cgroup *
  369. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  370. static inline struct blkio_cgroup *
  371. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  372. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  373. void *key) { return NULL; }
  374. #endif
  375. #endif /* _BLK_CGROUP_H */