blk-cgroup.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. struct blkio_policy_ops {
  134. blkio_init_group_fn *blkio_init_group_fn;
  135. };
  136. struct blkio_policy_type {
  137. struct list_head list;
  138. struct blkio_policy_ops ops;
  139. enum blkio_policy_id plid;
  140. size_t pdata_size; /* policy specific private data size */
  141. struct cftype *cftypes; /* cgroup files for the policy */
  142. };
  143. extern int blkcg_init_queue(struct request_queue *q);
  144. extern void blkcg_drain_queue(struct request_queue *q);
  145. extern void blkcg_exit_queue(struct request_queue *q);
  146. /* Blkio controller policy registration */
  147. extern void blkio_policy_register(struct blkio_policy_type *);
  148. extern void blkio_policy_unregister(struct blkio_policy_type *);
  149. extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
  150. extern void update_root_blkg_pd(struct request_queue *q,
  151. enum blkio_policy_id plid);
  152. void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
  153. u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
  154. int pol, int data, bool show_total);
  155. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  156. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  157. const struct blkg_rwstat *rwstat);
  158. int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
  159. struct seq_file *sf);
  160. int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
  161. struct seq_file *sf);
  162. struct blkg_conf_ctx {
  163. struct gendisk *disk;
  164. struct blkio_group *blkg;
  165. u64 v;
  166. };
  167. int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
  168. struct blkg_conf_ctx *ctx);
  169. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  170. /**
  171. * blkg_to_pdata - get policy private data
  172. * @blkg: blkg of interest
  173. * @pol: policy of interest
  174. *
  175. * Return pointer to private data associated with the @blkg-@pol pair.
  176. */
  177. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  178. struct blkio_policy_type *pol)
  179. {
  180. return blkg ? blkg->pd[pol->plid]->pdata : NULL;
  181. }
  182. /**
  183. * pdata_to_blkg - get blkg associated with policy private data
  184. * @pdata: policy private data of interest
  185. *
  186. * @pdata is policy private data. Determine the blkg it's associated with.
  187. */
  188. static inline struct blkio_group *pdata_to_blkg(void *pdata)
  189. {
  190. if (pdata) {
  191. struct blkg_policy_data *pd =
  192. container_of(pdata, struct blkg_policy_data, pdata);
  193. return pd->blkg;
  194. }
  195. return NULL;
  196. }
  197. static inline char *blkg_path(struct blkio_group *blkg)
  198. {
  199. return blkg->path;
  200. }
  201. /**
  202. * blkg_get - get a blkg reference
  203. * @blkg: blkg to get
  204. *
  205. * The caller should be holding queue_lock and an existing reference.
  206. */
  207. static inline void blkg_get(struct blkio_group *blkg)
  208. {
  209. lockdep_assert_held(blkg->q->queue_lock);
  210. WARN_ON_ONCE(!blkg->refcnt);
  211. blkg->refcnt++;
  212. }
  213. void __blkg_release(struct blkio_group *blkg);
  214. /**
  215. * blkg_put - put a blkg reference
  216. * @blkg: blkg to put
  217. *
  218. * The caller should be holding queue_lock.
  219. */
  220. static inline void blkg_put(struct blkio_group *blkg)
  221. {
  222. lockdep_assert_held(blkg->q->queue_lock);
  223. WARN_ON_ONCE(blkg->refcnt <= 0);
  224. if (!--blkg->refcnt)
  225. __blkg_release(blkg);
  226. }
  227. /**
  228. * blkg_stat_add - add a value to a blkg_stat
  229. * @stat: target blkg_stat
  230. * @val: value to add
  231. *
  232. * Add @val to @stat. The caller is responsible for synchronizing calls to
  233. * this function.
  234. */
  235. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  236. {
  237. u64_stats_update_begin(&stat->syncp);
  238. stat->cnt += val;
  239. u64_stats_update_end(&stat->syncp);
  240. }
  241. /**
  242. * blkg_stat_read - read the current value of a blkg_stat
  243. * @stat: blkg_stat to read
  244. *
  245. * Read the current value of @stat. This function can be called without
  246. * synchroniztion and takes care of u64 atomicity.
  247. */
  248. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  249. {
  250. unsigned int start;
  251. uint64_t v;
  252. do {
  253. start = u64_stats_fetch_begin(&stat->syncp);
  254. v = stat->cnt;
  255. } while (u64_stats_fetch_retry(&stat->syncp, start));
  256. return v;
  257. }
  258. /**
  259. * blkg_stat_reset - reset a blkg_stat
  260. * @stat: blkg_stat to reset
  261. */
  262. static inline void blkg_stat_reset(struct blkg_stat *stat)
  263. {
  264. stat->cnt = 0;
  265. }
  266. /**
  267. * blkg_rwstat_add - add a value to a blkg_rwstat
  268. * @rwstat: target blkg_rwstat
  269. * @rw: mask of REQ_{WRITE|SYNC}
  270. * @val: value to add
  271. *
  272. * Add @val to @rwstat. The counters are chosen according to @rw. The
  273. * caller is responsible for synchronizing calls to this function.
  274. */
  275. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  276. int rw, uint64_t val)
  277. {
  278. u64_stats_update_begin(&rwstat->syncp);
  279. if (rw & REQ_WRITE)
  280. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  281. else
  282. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  283. if (rw & REQ_SYNC)
  284. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  285. else
  286. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  287. u64_stats_update_end(&rwstat->syncp);
  288. }
  289. /**
  290. * blkg_rwstat_read - read the current values of a blkg_rwstat
  291. * @rwstat: blkg_rwstat to read
  292. *
  293. * Read the current snapshot of @rwstat and return it as the return value.
  294. * This function can be called without synchronization and takes care of
  295. * u64 atomicity.
  296. */
  297. static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  298. {
  299. unsigned int start;
  300. struct blkg_rwstat tmp;
  301. do {
  302. start = u64_stats_fetch_begin(&rwstat->syncp);
  303. tmp = *rwstat;
  304. } while (u64_stats_fetch_retry(&rwstat->syncp, start));
  305. return tmp;
  306. }
  307. /**
  308. * blkg_rwstat_sum - read the total count of a blkg_rwstat
  309. * @rwstat: blkg_rwstat to read
  310. *
  311. * Return the total count of @rwstat regardless of the IO direction. This
  312. * function can be called without synchronization and takes care of u64
  313. * atomicity.
  314. */
  315. static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
  316. {
  317. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  318. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  319. }
  320. /**
  321. * blkg_rwstat_reset - reset a blkg_rwstat
  322. * @rwstat: blkg_rwstat to reset
  323. */
  324. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
  325. {
  326. memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
  327. }
  328. #else
  329. struct blkio_group {
  330. };
  331. struct blkio_policy_type {
  332. };
  333. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  334. static inline void blkcg_drain_queue(struct request_queue *q) { }
  335. static inline void blkcg_exit_queue(struct request_queue *q) { }
  336. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  337. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  338. static inline void blkg_destroy_all(struct request_queue *q,
  339. bool destory_root) { }
  340. static inline void update_root_blkg_pd(struct request_queue *q,
  341. enum blkio_policy_id plid) { }
  342. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  343. struct blkio_policy_type *pol) { return NULL; }
  344. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  345. struct blkio_policy_type *pol) { return NULL; }
  346. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  347. static inline void blkg_get(struct blkio_group *blkg) { }
  348. static inline void blkg_put(struct blkio_group *blkg) { }
  349. #endif
  350. #define BLKIO_WEIGHT_MIN 10
  351. #define BLKIO_WEIGHT_MAX 1000
  352. #define BLKIO_WEIGHT_DEFAULT 500
  353. #ifdef CONFIG_BLK_CGROUP
  354. extern struct blkio_cgroup blkio_root_cgroup;
  355. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  356. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  357. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  358. struct request_queue *q);
  359. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  360. struct request_queue *q,
  361. bool for_root);
  362. #else
  363. struct cgroup;
  364. static inline struct blkio_cgroup *
  365. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  366. static inline struct blkio_cgroup *
  367. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  368. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  369. void *key) { return NULL; }
  370. #endif
  371. #endif /* _BLK_CGROUP_H */