blk-cgroup.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. };
  159. extern int blkcg_init_queue(struct request_queue *q);
  160. extern void blkcg_drain_queue(struct request_queue *q);
  161. extern void blkcg_exit_queue(struct request_queue *q);
  162. /* Blkio controller policy registration */
  163. extern void blkio_policy_register(struct blkio_policy_type *);
  164. extern void blkio_policy_unregister(struct blkio_policy_type *);
  165. extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
  166. extern void update_root_blkg_pd(struct request_queue *q,
  167. enum blkio_policy_id plid);
  168. void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
  169. u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
  170. int pol, int data, bool show_total);
  171. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  172. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  173. const struct blkg_rwstat *rwstat);
  174. int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
  175. struct seq_file *sf);
  176. int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
  177. struct seq_file *sf);
  178. int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft,
  179. struct seq_file *sf);
  180. int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  181. struct seq_file *sf);
  182. struct blkg_conf_ctx {
  183. struct gendisk *disk;
  184. struct blkio_group *blkg;
  185. u64 v;
  186. };
  187. int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
  188. struct blkg_conf_ctx *ctx);
  189. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  190. /**
  191. * blkg_to_pdata - get policy private data
  192. * @blkg: blkg of interest
  193. * @pol: policy of interest
  194. *
  195. * Return pointer to private data associated with the @blkg-@pol pair.
  196. */
  197. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  198. struct blkio_policy_type *pol)
  199. {
  200. return blkg ? blkg->pd[pol->plid]->pdata : NULL;
  201. }
  202. /**
  203. * pdata_to_blkg - get blkg associated with policy private data
  204. * @pdata: policy private data of interest
  205. *
  206. * @pdata is policy private data. Determine the blkg it's associated with.
  207. */
  208. static inline struct blkio_group *pdata_to_blkg(void *pdata)
  209. {
  210. if (pdata) {
  211. struct blkg_policy_data *pd =
  212. container_of(pdata, struct blkg_policy_data, pdata);
  213. return pd->blkg;
  214. }
  215. return NULL;
  216. }
  217. static inline char *blkg_path(struct blkio_group *blkg)
  218. {
  219. return blkg->path;
  220. }
  221. /**
  222. * blkg_get - get a blkg reference
  223. * @blkg: blkg to get
  224. *
  225. * The caller should be holding queue_lock and an existing reference.
  226. */
  227. static inline void blkg_get(struct blkio_group *blkg)
  228. {
  229. lockdep_assert_held(blkg->q->queue_lock);
  230. WARN_ON_ONCE(!blkg->refcnt);
  231. blkg->refcnt++;
  232. }
  233. void __blkg_release(struct blkio_group *blkg);
  234. /**
  235. * blkg_put - put a blkg reference
  236. * @blkg: blkg to put
  237. *
  238. * The caller should be holding queue_lock.
  239. */
  240. static inline void blkg_put(struct blkio_group *blkg)
  241. {
  242. lockdep_assert_held(blkg->q->queue_lock);
  243. WARN_ON_ONCE(blkg->refcnt <= 0);
  244. if (!--blkg->refcnt)
  245. __blkg_release(blkg);
  246. }
  247. /**
  248. * blkg_stat_add - add a value to a blkg_stat
  249. * @stat: target blkg_stat
  250. * @val: value to add
  251. *
  252. * Add @val to @stat. The caller is responsible for synchronizing calls to
  253. * this function.
  254. */
  255. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  256. {
  257. u64_stats_update_begin(&stat->syncp);
  258. stat->cnt += val;
  259. u64_stats_update_end(&stat->syncp);
  260. }
  261. /**
  262. * blkg_stat_read - read the current value of a blkg_stat
  263. * @stat: blkg_stat to read
  264. *
  265. * Read the current value of @stat. This function can be called without
  266. * synchroniztion and takes care of u64 atomicity.
  267. */
  268. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  269. {
  270. unsigned int start;
  271. uint64_t v;
  272. do {
  273. start = u64_stats_fetch_begin(&stat->syncp);
  274. v = stat->cnt;
  275. } while (u64_stats_fetch_retry(&stat->syncp, start));
  276. return v;
  277. }
  278. /**
  279. * blkg_stat_reset - reset a blkg_stat
  280. * @stat: blkg_stat to reset
  281. */
  282. static inline void blkg_stat_reset(struct blkg_stat *stat)
  283. {
  284. stat->cnt = 0;
  285. }
  286. /**
  287. * blkg_rwstat_add - add a value to a blkg_rwstat
  288. * @rwstat: target blkg_rwstat
  289. * @rw: mask of REQ_{WRITE|SYNC}
  290. * @val: value to add
  291. *
  292. * Add @val to @rwstat. The counters are chosen according to @rw. The
  293. * caller is responsible for synchronizing calls to this function.
  294. */
  295. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  296. int rw, uint64_t val)
  297. {
  298. u64_stats_update_begin(&rwstat->syncp);
  299. if (rw & REQ_WRITE)
  300. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  301. else
  302. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  303. if (rw & REQ_SYNC)
  304. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  305. else
  306. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  307. u64_stats_update_end(&rwstat->syncp);
  308. }
  309. /**
  310. * blkg_rwstat_read - read the current values of a blkg_rwstat
  311. * @rwstat: blkg_rwstat to read
  312. *
  313. * Read the current snapshot of @rwstat and return it as the return value.
  314. * This function can be called without synchronization and takes care of
  315. * u64 atomicity.
  316. */
  317. static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  318. {
  319. unsigned int start;
  320. struct blkg_rwstat tmp;
  321. do {
  322. start = u64_stats_fetch_begin(&rwstat->syncp);
  323. tmp = *rwstat;
  324. } while (u64_stats_fetch_retry(&rwstat->syncp, start));
  325. return tmp;
  326. }
  327. /**
  328. * blkg_rwstat_sum - read the total count of a blkg_rwstat
  329. * @rwstat: blkg_rwstat to read
  330. *
  331. * Return the total count of @rwstat regardless of the IO direction. This
  332. * function can be called without synchronization and takes care of u64
  333. * atomicity.
  334. */
  335. static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
  336. {
  337. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  338. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  339. }
  340. /**
  341. * blkg_rwstat_reset - reset a blkg_rwstat
  342. * @rwstat: blkg_rwstat to reset
  343. */
  344. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
  345. {
  346. memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
  347. }
  348. #else
  349. struct blkio_group {
  350. };
  351. struct blkio_policy_type {
  352. };
  353. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  354. static inline void blkcg_drain_queue(struct request_queue *q) { }
  355. static inline void blkcg_exit_queue(struct request_queue *q) { }
  356. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  357. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  358. static inline void blkg_destroy_all(struct request_queue *q,
  359. bool destory_root) { }
  360. static inline void update_root_blkg_pd(struct request_queue *q,
  361. enum blkio_policy_id plid) { }
  362. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  363. struct blkio_policy_type *pol) { return NULL; }
  364. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  365. struct blkio_policy_type *pol) { return NULL; }
  366. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  367. static inline void blkg_get(struct blkio_group *blkg) { }
  368. static inline void blkg_put(struct blkio_group *blkg) { }
  369. #endif
  370. #define BLKIO_WEIGHT_MIN 10
  371. #define BLKIO_WEIGHT_MAX 1000
  372. #define BLKIO_WEIGHT_DEFAULT 500
  373. #ifdef CONFIG_DEBUG_BLK_CGROUP
  374. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  375. struct blkio_policy_type *pol);
  376. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  377. struct blkio_policy_type *pol,
  378. unsigned long dequeue);
  379. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  380. struct blkio_policy_type *pol);
  381. void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  382. struct blkio_policy_type *pol);
  383. void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  384. struct blkio_policy_type *pol);
  385. #define BLKG_FLAG_FNS(name) \
  386. static inline void blkio_mark_blkg_##name( \
  387. struct blkio_group_stats *stats) \
  388. { \
  389. stats->flags |= (1 << BLKG_##name); \
  390. } \
  391. static inline void blkio_clear_blkg_##name( \
  392. struct blkio_group_stats *stats) \
  393. { \
  394. stats->flags &= ~(1 << BLKG_##name); \
  395. } \
  396. static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
  397. { \
  398. return (stats->flags & (1 << BLKG_##name)) != 0; \
  399. } \
  400. BLKG_FLAG_FNS(waiting)
  401. BLKG_FLAG_FNS(idling)
  402. BLKG_FLAG_FNS(empty)
  403. #undef BLKG_FLAG_FNS
  404. #else
  405. static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  406. struct blkio_policy_type *pol) { }
  407. static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  408. struct blkio_policy_type *pol, unsigned long dequeue) { }
  409. static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  410. struct blkio_policy_type *pol) { }
  411. static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  412. struct blkio_policy_type *pol) { }
  413. static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  414. struct blkio_policy_type *pol) { }
  415. #endif
  416. #ifdef CONFIG_BLK_CGROUP
  417. extern struct blkio_cgroup blkio_root_cgroup;
  418. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  419. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  420. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  421. struct request_queue *q);
  422. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  423. struct request_queue *q,
  424. bool for_root);
  425. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  426. struct blkio_policy_type *pol,
  427. unsigned long time,
  428. unsigned long unaccounted_time);
  429. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  430. struct blkio_policy_type *pol,
  431. uint64_t bytes, bool direction, bool sync);
  432. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  433. struct blkio_policy_type *pol,
  434. uint64_t start_time,
  435. uint64_t io_start_time, bool direction,
  436. bool sync);
  437. void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  438. struct blkio_policy_type *pol,
  439. bool direction, bool sync);
  440. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  441. struct blkio_policy_type *pol,
  442. struct blkio_group *curr_blkg, bool direction,
  443. bool sync);
  444. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  445. struct blkio_policy_type *pol,
  446. bool direction, bool sync);
  447. #else
  448. struct cgroup;
  449. static inline struct blkio_cgroup *
  450. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  451. static inline struct blkio_cgroup *
  452. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  453. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  454. void *key) { return NULL; }
  455. static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  456. struct blkio_policy_type *pol, unsigned long time,
  457. unsigned long unaccounted_time) { }
  458. static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  459. struct blkio_policy_type *pol, uint64_t bytes,
  460. bool direction, bool sync) { }
  461. static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
  462. struct blkio_policy_type *pol, uint64_t start_time,
  463. uint64_t io_start_time, bool direction, bool sync) { }
  464. static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  465. struct blkio_policy_type *pol, bool direction,
  466. bool sync) { }
  467. static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  468. struct blkio_policy_type *pol,
  469. struct blkio_group *curr_blkg, bool direction,
  470. bool sync) { }
  471. static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  472. struct blkio_policy_type *pol, bool direction,
  473. bool sync) { }
  474. #endif
  475. #endif /* _BLK_CGROUP_H */