blk-cgroup.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. /* blkg state flags */
  39. enum blkg_state_flags {
  40. BLKG_waiting = 0,
  41. BLKG_idling,
  42. BLKG_empty,
  43. };
  44. struct blkio_cgroup {
  45. struct cgroup_subsys_state css;
  46. unsigned int weight;
  47. spinlock_t lock;
  48. struct hlist_head blkg_list;
  49. /* for policies to test whether associated blkcg has changed */
  50. uint64_t id;
  51. };
  52. struct blkg_stat {
  53. struct u64_stats_sync syncp;
  54. uint64_t cnt;
  55. };
  56. struct blkg_rwstat {
  57. struct u64_stats_sync syncp;
  58. uint64_t cnt[BLKG_RWSTAT_NR];
  59. };
  60. struct blkio_group_stats {
  61. /* number of ios merged */
  62. struct blkg_rwstat merged;
  63. /* total time spent on device in ns, may not be accurate w/ queueing */
  64. struct blkg_rwstat service_time;
  65. /* total time spent waiting in scheduler queue in ns */
  66. struct blkg_rwstat wait_time;
  67. /* number of IOs queued up */
  68. struct blkg_rwstat queued;
  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. /* total sectors transferred */
  100. struct blkg_stat sectors;
  101. };
  102. struct blkio_group_conf {
  103. unsigned int weight;
  104. u64 iops[2];
  105. u64 bps[2];
  106. };
  107. /* per-blkg per-policy data */
  108. struct blkg_policy_data {
  109. /* the blkg this per-policy data belongs to */
  110. struct blkio_group *blkg;
  111. /* Configuration */
  112. struct blkio_group_conf conf;
  113. struct blkio_group_stats stats;
  114. /* Per cpu stats pointer */
  115. struct blkio_group_stats_cpu __percpu *stats_cpu;
  116. /* pol->pdata_size bytes of private data used by policy impl */
  117. char pdata[] __aligned(__alignof__(unsigned long long));
  118. };
  119. struct blkio_group {
  120. /* Pointer to the associated request_queue */
  121. struct request_queue *q;
  122. struct list_head q_node;
  123. struct hlist_node blkcg_node;
  124. struct blkio_cgroup *blkcg;
  125. /* Store cgroup path */
  126. char path[128];
  127. /* reference count */
  128. int refcnt;
  129. struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
  130. /* List of blkg waiting for per cpu stats memory to be allocated */
  131. struct list_head alloc_node;
  132. struct rcu_head rcu_head;
  133. };
  134. typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
  135. typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
  136. struct blkio_group *blkg, unsigned int weight);
  137. typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
  138. struct blkio_group *blkg, u64 read_bps);
  139. typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
  140. struct blkio_group *blkg, u64 write_bps);
  141. typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
  142. struct blkio_group *blkg, unsigned int read_iops);
  143. typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
  144. struct blkio_group *blkg, unsigned int write_iops);
  145. struct blkio_policy_ops {
  146. blkio_init_group_fn *blkio_init_group_fn;
  147. blkio_update_group_weight_fn *blkio_update_group_weight_fn;
  148. blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
  149. blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
  150. blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
  151. blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
  152. };
  153. struct blkio_policy_type {
  154. struct list_head list;
  155. struct blkio_policy_ops ops;
  156. enum blkio_policy_id plid;
  157. size_t pdata_size; /* policy specific private data size */
  158. struct cftype *cftypes; /* cgroup files for the policy */
  159. };
  160. extern int blkcg_init_queue(struct request_queue *q);
  161. extern void blkcg_drain_queue(struct request_queue *q);
  162. extern void blkcg_exit_queue(struct request_queue *q);
  163. /* Blkio controller policy registration */
  164. extern void blkio_policy_register(struct blkio_policy_type *);
  165. extern void blkio_policy_unregister(struct blkio_policy_type *);
  166. extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
  167. extern void update_root_blkg_pd(struct request_queue *q,
  168. enum blkio_policy_id plid);
  169. void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
  170. u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
  171. int pol, int data, bool show_total);
  172. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  173. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  174. const struct blkg_rwstat *rwstat);
  175. int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
  176. struct seq_file *sf);
  177. int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
  178. struct seq_file *sf);
  179. int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft,
  180. struct seq_file *sf);
  181. int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  182. struct seq_file *sf);
  183. struct blkg_conf_ctx {
  184. struct gendisk *disk;
  185. struct blkio_group *blkg;
  186. u64 v;
  187. };
  188. int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
  189. struct blkg_conf_ctx *ctx);
  190. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  191. /**
  192. * blkg_to_pdata - get policy private data
  193. * @blkg: blkg of interest
  194. * @pol: policy of interest
  195. *
  196. * Return pointer to private data associated with the @blkg-@pol pair.
  197. */
  198. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  199. struct blkio_policy_type *pol)
  200. {
  201. return blkg ? blkg->pd[pol->plid]->pdata : NULL;
  202. }
  203. /**
  204. * pdata_to_blkg - get blkg associated with policy private data
  205. * @pdata: policy private data of interest
  206. *
  207. * @pdata is policy private data. Determine the blkg it's associated with.
  208. */
  209. static inline struct blkio_group *pdata_to_blkg(void *pdata)
  210. {
  211. if (pdata) {
  212. struct blkg_policy_data *pd =
  213. container_of(pdata, struct blkg_policy_data, pdata);
  214. return pd->blkg;
  215. }
  216. return NULL;
  217. }
  218. static inline char *blkg_path(struct blkio_group *blkg)
  219. {
  220. return blkg->path;
  221. }
  222. /**
  223. * blkg_get - get a blkg reference
  224. * @blkg: blkg to get
  225. *
  226. * The caller should be holding queue_lock and an existing reference.
  227. */
  228. static inline void blkg_get(struct blkio_group *blkg)
  229. {
  230. lockdep_assert_held(blkg->q->queue_lock);
  231. WARN_ON_ONCE(!blkg->refcnt);
  232. blkg->refcnt++;
  233. }
  234. void __blkg_release(struct blkio_group *blkg);
  235. /**
  236. * blkg_put - put a blkg reference
  237. * @blkg: blkg to put
  238. *
  239. * The caller should be holding queue_lock.
  240. */
  241. static inline void blkg_put(struct blkio_group *blkg)
  242. {
  243. lockdep_assert_held(blkg->q->queue_lock);
  244. WARN_ON_ONCE(blkg->refcnt <= 0);
  245. if (!--blkg->refcnt)
  246. __blkg_release(blkg);
  247. }
  248. /**
  249. * blkg_stat_add - add a value to a blkg_stat
  250. * @stat: target blkg_stat
  251. * @val: value to add
  252. *
  253. * Add @val to @stat. The caller is responsible for synchronizing calls to
  254. * this function.
  255. */
  256. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  257. {
  258. u64_stats_update_begin(&stat->syncp);
  259. stat->cnt += val;
  260. u64_stats_update_end(&stat->syncp);
  261. }
  262. /**
  263. * blkg_stat_read - read the current value of a blkg_stat
  264. * @stat: blkg_stat to read
  265. *
  266. * Read the current value of @stat. This function can be called without
  267. * synchroniztion and takes care of u64 atomicity.
  268. */
  269. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  270. {
  271. unsigned int start;
  272. uint64_t v;
  273. do {
  274. start = u64_stats_fetch_begin(&stat->syncp);
  275. v = stat->cnt;
  276. } while (u64_stats_fetch_retry(&stat->syncp, start));
  277. return v;
  278. }
  279. /**
  280. * blkg_stat_reset - reset a blkg_stat
  281. * @stat: blkg_stat to reset
  282. */
  283. static inline void blkg_stat_reset(struct blkg_stat *stat)
  284. {
  285. stat->cnt = 0;
  286. }
  287. /**
  288. * blkg_rwstat_add - add a value to a blkg_rwstat
  289. * @rwstat: target blkg_rwstat
  290. * @rw: mask of REQ_{WRITE|SYNC}
  291. * @val: value to add
  292. *
  293. * Add @val to @rwstat. The counters are chosen according to @rw. The
  294. * caller is responsible for synchronizing calls to this function.
  295. */
  296. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  297. int rw, uint64_t val)
  298. {
  299. u64_stats_update_begin(&rwstat->syncp);
  300. if (rw & REQ_WRITE)
  301. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  302. else
  303. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  304. if (rw & REQ_SYNC)
  305. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  306. else
  307. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  308. u64_stats_update_end(&rwstat->syncp);
  309. }
  310. /**
  311. * blkg_rwstat_read - read the current values of a blkg_rwstat
  312. * @rwstat: blkg_rwstat to read
  313. *
  314. * Read the current snapshot of @rwstat and return it as the return value.
  315. * This function can be called without synchronization and takes care of
  316. * u64 atomicity.
  317. */
  318. static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  319. {
  320. unsigned int start;
  321. struct blkg_rwstat tmp;
  322. do {
  323. start = u64_stats_fetch_begin(&rwstat->syncp);
  324. tmp = *rwstat;
  325. } while (u64_stats_fetch_retry(&rwstat->syncp, start));
  326. return tmp;
  327. }
  328. /**
  329. * blkg_rwstat_sum - read the total count of a blkg_rwstat
  330. * @rwstat: blkg_rwstat to read
  331. *
  332. * Return the total count of @rwstat regardless of the IO direction. This
  333. * function can be called without synchronization and takes care of u64
  334. * atomicity.
  335. */
  336. static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
  337. {
  338. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  339. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  340. }
  341. /**
  342. * blkg_rwstat_reset - reset a blkg_rwstat
  343. * @rwstat: blkg_rwstat to reset
  344. */
  345. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
  346. {
  347. memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
  348. }
  349. #else
  350. struct blkio_group {
  351. };
  352. struct blkio_policy_type {
  353. };
  354. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  355. static inline void blkcg_drain_queue(struct request_queue *q) { }
  356. static inline void blkcg_exit_queue(struct request_queue *q) { }
  357. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  358. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  359. static inline void blkg_destroy_all(struct request_queue *q,
  360. bool destory_root) { }
  361. static inline void update_root_blkg_pd(struct request_queue *q,
  362. enum blkio_policy_id plid) { }
  363. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  364. struct blkio_policy_type *pol) { return NULL; }
  365. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  366. struct blkio_policy_type *pol) { return NULL; }
  367. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  368. static inline void blkg_get(struct blkio_group *blkg) { }
  369. static inline void blkg_put(struct blkio_group *blkg) { }
  370. #endif
  371. #define BLKIO_WEIGHT_MIN 10
  372. #define BLKIO_WEIGHT_MAX 1000
  373. #define BLKIO_WEIGHT_DEFAULT 500
  374. #ifdef CONFIG_DEBUG_BLK_CGROUP
  375. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  376. struct blkio_policy_type *pol);
  377. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  378. struct blkio_policy_type *pol,
  379. unsigned long dequeue);
  380. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  381. struct blkio_policy_type *pol);
  382. void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  383. struct blkio_policy_type *pol);
  384. void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  385. struct blkio_policy_type *pol);
  386. #define BLKG_FLAG_FNS(name) \
  387. static inline void blkio_mark_blkg_##name( \
  388. struct blkio_group_stats *stats) \
  389. { \
  390. stats->flags |= (1 << BLKG_##name); \
  391. } \
  392. static inline void blkio_clear_blkg_##name( \
  393. struct blkio_group_stats *stats) \
  394. { \
  395. stats->flags &= ~(1 << BLKG_##name); \
  396. } \
  397. static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
  398. { \
  399. return (stats->flags & (1 << BLKG_##name)) != 0; \
  400. } \
  401. BLKG_FLAG_FNS(waiting)
  402. BLKG_FLAG_FNS(idling)
  403. BLKG_FLAG_FNS(empty)
  404. #undef BLKG_FLAG_FNS
  405. #else
  406. static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  407. struct blkio_policy_type *pol) { }
  408. static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  409. struct blkio_policy_type *pol, unsigned long dequeue) { }
  410. static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  411. struct blkio_policy_type *pol) { }
  412. static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  413. struct blkio_policy_type *pol) { }
  414. static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  415. struct blkio_policy_type *pol) { }
  416. #endif
  417. #ifdef CONFIG_BLK_CGROUP
  418. extern struct blkio_cgroup blkio_root_cgroup;
  419. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  420. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  421. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  422. struct request_queue *q);
  423. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  424. struct request_queue *q,
  425. bool for_root);
  426. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  427. struct blkio_policy_type *pol,
  428. unsigned long time,
  429. unsigned long unaccounted_time);
  430. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  431. struct blkio_policy_type *pol,
  432. uint64_t bytes, bool direction, bool sync);
  433. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  434. struct blkio_policy_type *pol,
  435. uint64_t start_time,
  436. uint64_t io_start_time, bool direction,
  437. bool sync);
  438. void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  439. struct blkio_policy_type *pol,
  440. bool direction, bool sync);
  441. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  442. struct blkio_policy_type *pol,
  443. struct blkio_group *curr_blkg, bool direction,
  444. bool sync);
  445. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  446. struct blkio_policy_type *pol,
  447. bool direction, bool sync);
  448. #else
  449. struct cgroup;
  450. static inline struct blkio_cgroup *
  451. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  452. static inline struct blkio_cgroup *
  453. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  454. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  455. void *key) { return NULL; }
  456. static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  457. struct blkio_policy_type *pol, unsigned long time,
  458. unsigned long unaccounted_time) { }
  459. static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  460. struct blkio_policy_type *pol, uint64_t bytes,
  461. bool direction, bool sync) { }
  462. static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
  463. struct blkio_policy_type *pol, uint64_t start_time,
  464. uint64_t io_start_time, bool direction, bool sync) { }
  465. static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  466. struct blkio_policy_type *pol, bool direction,
  467. bool sync) { }
  468. static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  469. struct blkio_policy_type *pol,
  470. struct blkio_group *curr_blkg, bool direction,
  471. bool sync) { }
  472. static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  473. struct blkio_policy_type *pol, bool direction,
  474. bool sync) { }
  475. #endif
  476. #endif /* _BLK_CGROUP_H */