blk-cgroup.h 15 KB

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