blk-cgroup.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. enum stat_type {
  26. /* Number of IOs merged */
  27. BLKIO_STAT_MERGED,
  28. /* Total time spent (in ns) between request dispatch to the driver and
  29. * request completion for IOs doen by this cgroup. This may not be
  30. * accurate when NCQ is turned on. */
  31. BLKIO_STAT_SERVICE_TIME,
  32. /* Total time spent waiting in scheduler queue in ns */
  33. BLKIO_STAT_WAIT_TIME,
  34. /* Number of IOs queued up */
  35. BLKIO_STAT_QUEUED,
  36. /* All the single valued stats go below this */
  37. BLKIO_STAT_TIME,
  38. #ifdef CONFIG_DEBUG_BLK_CGROUP
  39. /* Time not charged to this cgroup */
  40. BLKIO_STAT_UNACCOUNTED_TIME,
  41. BLKIO_STAT_AVG_QUEUE_SIZE,
  42. BLKIO_STAT_IDLE_TIME,
  43. BLKIO_STAT_EMPTY_TIME,
  44. BLKIO_STAT_GROUP_WAIT_TIME,
  45. BLKIO_STAT_DEQUEUE
  46. #endif
  47. };
  48. /* Types lower than this live in stat_arr and have subtypes */
  49. #define BLKIO_STAT_ARR_NR (BLKIO_STAT_QUEUED + 1)
  50. /* Per cpu stats */
  51. enum stat_type_cpu {
  52. BLKIO_STAT_CPU_SECTORS,
  53. /* Total bytes transferred */
  54. BLKIO_STAT_CPU_SERVICE_BYTES,
  55. /* Total IOs serviced, post merge */
  56. BLKIO_STAT_CPU_SERVICED,
  57. BLKIO_STAT_CPU_NR
  58. };
  59. enum stat_sub_type {
  60. BLKIO_STAT_READ = 0,
  61. BLKIO_STAT_WRITE,
  62. BLKIO_STAT_SYNC,
  63. BLKIO_STAT_ASYNC,
  64. BLKIO_STAT_TOTAL
  65. };
  66. /* blkg state flags */
  67. enum blkg_state_flags {
  68. BLKG_waiting = 0,
  69. BLKG_idling,
  70. BLKG_empty,
  71. };
  72. /* cgroup files owned by proportional weight policy */
  73. enum blkcg_file_name_prop {
  74. BLKIO_PROP_weight = 1,
  75. BLKIO_PROP_weight_device,
  76. BLKIO_PROP_io_service_bytes,
  77. BLKIO_PROP_io_serviced,
  78. BLKIO_PROP_time,
  79. BLKIO_PROP_sectors,
  80. BLKIO_PROP_unaccounted_time,
  81. BLKIO_PROP_io_service_time,
  82. BLKIO_PROP_io_wait_time,
  83. BLKIO_PROP_io_merged,
  84. BLKIO_PROP_io_queued,
  85. BLKIO_PROP_avg_queue_size,
  86. BLKIO_PROP_group_wait_time,
  87. BLKIO_PROP_idle_time,
  88. BLKIO_PROP_empty_time,
  89. BLKIO_PROP_dequeue,
  90. };
  91. /* cgroup files owned by throttle policy */
  92. enum blkcg_file_name_throtl {
  93. BLKIO_THROTL_read_bps_device,
  94. BLKIO_THROTL_write_bps_device,
  95. BLKIO_THROTL_read_iops_device,
  96. BLKIO_THROTL_write_iops_device,
  97. BLKIO_THROTL_io_service_bytes,
  98. BLKIO_THROTL_io_serviced,
  99. };
  100. struct blkio_cgroup {
  101. struct cgroup_subsys_state css;
  102. unsigned int weight;
  103. spinlock_t lock;
  104. struct hlist_head blkg_list;
  105. /* for policies to test whether associated blkcg has changed */
  106. uint64_t id;
  107. };
  108. struct blkio_group_stats {
  109. struct u64_stats_sync syncp;
  110. /* total disk time and nr sectors dispatched by this group */
  111. uint64_t time;
  112. uint64_t stat_arr[BLKIO_STAT_ARR_NR][BLKIO_STAT_TOTAL];
  113. #ifdef CONFIG_DEBUG_BLK_CGROUP
  114. /* Time not charged to this cgroup */
  115. uint64_t unaccounted_time;
  116. /* Sum of number of IOs queued across all samples */
  117. uint64_t avg_queue_size_sum;
  118. /* Count of samples taken for average */
  119. uint64_t avg_queue_size_samples;
  120. /* How many times this group has been removed from service tree */
  121. unsigned long dequeue;
  122. /* Total time spent waiting for it to be assigned a timeslice. */
  123. uint64_t group_wait_time;
  124. /* Time spent idling for this blkio_group */
  125. uint64_t idle_time;
  126. /*
  127. * Total time when we have requests queued and do not contain the
  128. * current active queue.
  129. */
  130. uint64_t empty_time;
  131. /* fields after this shouldn't be cleared on stat reset */
  132. uint64_t start_group_wait_time;
  133. uint64_t start_idle_time;
  134. uint64_t start_empty_time;
  135. uint16_t flags;
  136. #endif
  137. };
  138. #ifdef CONFIG_DEBUG_BLK_CGROUP
  139. #define BLKG_STATS_DEBUG_CLEAR_START \
  140. offsetof(struct blkio_group_stats, unaccounted_time)
  141. #define BLKG_STATS_DEBUG_CLEAR_SIZE \
  142. (offsetof(struct blkio_group_stats, start_group_wait_time) - \
  143. BLKG_STATS_DEBUG_CLEAR_START)
  144. #endif
  145. /* Per cpu blkio group stats */
  146. struct blkio_group_stats_cpu {
  147. uint64_t sectors;
  148. uint64_t stat_arr_cpu[BLKIO_STAT_CPU_NR][BLKIO_STAT_TOTAL];
  149. struct u64_stats_sync syncp;
  150. };
  151. struct blkio_group_conf {
  152. unsigned int weight;
  153. unsigned int iops[2];
  154. u64 bps[2];
  155. };
  156. /* per-blkg per-policy data */
  157. struct blkg_policy_data {
  158. /* the blkg this per-policy data belongs to */
  159. struct blkio_group *blkg;
  160. /* Configuration */
  161. struct blkio_group_conf conf;
  162. struct blkio_group_stats stats;
  163. /* Per cpu stats pointer */
  164. struct blkio_group_stats_cpu __percpu *stats_cpu;
  165. /* pol->pdata_size bytes of private data used by policy impl */
  166. char pdata[] __aligned(__alignof__(unsigned long long));
  167. };
  168. struct blkio_group {
  169. /* Pointer to the associated request_queue */
  170. struct request_queue *q;
  171. struct list_head q_node;
  172. struct hlist_node blkcg_node;
  173. struct blkio_cgroup *blkcg;
  174. /* Store cgroup path */
  175. char path[128];
  176. /* reference count */
  177. int refcnt;
  178. struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
  179. /* List of blkg waiting for per cpu stats memory to be allocated */
  180. struct list_head alloc_node;
  181. struct rcu_head rcu_head;
  182. };
  183. typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
  184. typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
  185. struct blkio_group *blkg, unsigned int weight);
  186. typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
  187. struct blkio_group *blkg, u64 read_bps);
  188. typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
  189. struct blkio_group *blkg, u64 write_bps);
  190. typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
  191. struct blkio_group *blkg, unsigned int read_iops);
  192. typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
  193. struct blkio_group *blkg, unsigned int write_iops);
  194. struct blkio_policy_ops {
  195. blkio_init_group_fn *blkio_init_group_fn;
  196. blkio_update_group_weight_fn *blkio_update_group_weight_fn;
  197. blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
  198. blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
  199. blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
  200. blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
  201. };
  202. struct blkio_policy_type {
  203. struct list_head list;
  204. struct blkio_policy_ops ops;
  205. enum blkio_policy_id plid;
  206. size_t pdata_size; /* policy specific private data size */
  207. };
  208. extern int blkcg_init_queue(struct request_queue *q);
  209. extern void blkcg_drain_queue(struct request_queue *q);
  210. extern void blkcg_exit_queue(struct request_queue *q);
  211. /* Blkio controller policy registration */
  212. extern void blkio_policy_register(struct blkio_policy_type *);
  213. extern void blkio_policy_unregister(struct blkio_policy_type *);
  214. extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
  215. extern void update_root_blkg_pd(struct request_queue *q,
  216. enum blkio_policy_id plid);
  217. /**
  218. * blkg_to_pdata - get policy private data
  219. * @blkg: blkg of interest
  220. * @pol: policy of interest
  221. *
  222. * Return pointer to private data associated with the @blkg-@pol pair.
  223. */
  224. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  225. struct blkio_policy_type *pol)
  226. {
  227. return blkg ? blkg->pd[pol->plid]->pdata : NULL;
  228. }
  229. /**
  230. * pdata_to_blkg - get blkg associated with policy private data
  231. * @pdata: policy private data of interest
  232. * @pol: policy @pdata is for
  233. *
  234. * @pdata is policy private data for @pol. Determine the blkg it's
  235. * associated with.
  236. */
  237. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  238. struct blkio_policy_type *pol)
  239. {
  240. if (pdata) {
  241. struct blkg_policy_data *pd =
  242. container_of(pdata, struct blkg_policy_data, pdata);
  243. return pd->blkg;
  244. }
  245. return NULL;
  246. }
  247. static inline char *blkg_path(struct blkio_group *blkg)
  248. {
  249. return blkg->path;
  250. }
  251. /**
  252. * blkg_get - get a blkg reference
  253. * @blkg: blkg to get
  254. *
  255. * The caller should be holding queue_lock and an existing reference.
  256. */
  257. static inline void blkg_get(struct blkio_group *blkg)
  258. {
  259. lockdep_assert_held(blkg->q->queue_lock);
  260. WARN_ON_ONCE(!blkg->refcnt);
  261. blkg->refcnt++;
  262. }
  263. void __blkg_release(struct blkio_group *blkg);
  264. /**
  265. * blkg_put - put a blkg reference
  266. * @blkg: blkg to put
  267. *
  268. * The caller should be holding queue_lock.
  269. */
  270. static inline void blkg_put(struct blkio_group *blkg)
  271. {
  272. lockdep_assert_held(blkg->q->queue_lock);
  273. WARN_ON_ONCE(blkg->refcnt <= 0);
  274. if (!--blkg->refcnt)
  275. __blkg_release(blkg);
  276. }
  277. #else
  278. struct blkio_group {
  279. };
  280. struct blkio_policy_type {
  281. };
  282. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  283. static inline void blkcg_drain_queue(struct request_queue *q) { }
  284. static inline void blkcg_exit_queue(struct request_queue *q) { }
  285. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  286. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  287. static inline void blkg_destroy_all(struct request_queue *q,
  288. bool destory_root) { }
  289. static inline void update_root_blkg_pd(struct request_queue *q,
  290. enum blkio_policy_id plid) { }
  291. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  292. struct blkio_policy_type *pol) { return NULL; }
  293. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  294. struct blkio_policy_type *pol) { return NULL; }
  295. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  296. static inline void blkg_get(struct blkio_group *blkg) { }
  297. static inline void blkg_put(struct blkio_group *blkg) { }
  298. #endif
  299. #define BLKIO_WEIGHT_MIN 10
  300. #define BLKIO_WEIGHT_MAX 1000
  301. #define BLKIO_WEIGHT_DEFAULT 500
  302. #ifdef CONFIG_DEBUG_BLK_CGROUP
  303. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  304. struct blkio_policy_type *pol);
  305. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  306. struct blkio_policy_type *pol,
  307. unsigned long dequeue);
  308. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  309. struct blkio_policy_type *pol);
  310. void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  311. struct blkio_policy_type *pol);
  312. void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  313. struct blkio_policy_type *pol);
  314. #define BLKG_FLAG_FNS(name) \
  315. static inline void blkio_mark_blkg_##name( \
  316. struct blkio_group_stats *stats) \
  317. { \
  318. stats->flags |= (1 << BLKG_##name); \
  319. } \
  320. static inline void blkio_clear_blkg_##name( \
  321. struct blkio_group_stats *stats) \
  322. { \
  323. stats->flags &= ~(1 << BLKG_##name); \
  324. } \
  325. static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
  326. { \
  327. return (stats->flags & (1 << BLKG_##name)) != 0; \
  328. } \
  329. BLKG_FLAG_FNS(waiting)
  330. BLKG_FLAG_FNS(idling)
  331. BLKG_FLAG_FNS(empty)
  332. #undef BLKG_FLAG_FNS
  333. #else
  334. static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  335. struct blkio_policy_type *pol) { }
  336. static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  337. struct blkio_policy_type *pol, unsigned long dequeue) { }
  338. static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  339. struct blkio_policy_type *pol) { }
  340. static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  341. struct blkio_policy_type *pol) { }
  342. static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  343. struct blkio_policy_type *pol) { }
  344. #endif
  345. #ifdef CONFIG_BLK_CGROUP
  346. extern struct blkio_cgroup blkio_root_cgroup;
  347. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  348. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  349. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  350. struct request_queue *q);
  351. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  352. struct request_queue *q,
  353. enum blkio_policy_id plid,
  354. bool for_root);
  355. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  356. struct blkio_policy_type *pol,
  357. unsigned long time,
  358. unsigned long unaccounted_time);
  359. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  360. struct blkio_policy_type *pol,
  361. uint64_t bytes, bool direction, bool sync);
  362. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  363. struct blkio_policy_type *pol,
  364. uint64_t start_time,
  365. uint64_t io_start_time, bool direction,
  366. bool sync);
  367. void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  368. struct blkio_policy_type *pol,
  369. bool direction, bool sync);
  370. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  371. struct blkio_policy_type *pol,
  372. struct blkio_group *curr_blkg, bool direction,
  373. bool sync);
  374. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  375. struct blkio_policy_type *pol,
  376. bool direction, bool sync);
  377. #else
  378. struct cgroup;
  379. static inline struct blkio_cgroup *
  380. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  381. static inline struct blkio_cgroup *
  382. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  383. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  384. void *key) { return NULL; }
  385. static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  386. struct blkio_policy_type *pol, unsigned long time,
  387. unsigned long unaccounted_time) { }
  388. static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  389. struct blkio_policy_type *pol, uint64_t bytes,
  390. bool direction, bool sync) { }
  391. static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
  392. struct blkio_policy_type *pol, uint64_t start_time,
  393. uint64_t io_start_time, bool direction, bool sync) { }
  394. static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  395. struct blkio_policy_type *pol, bool direction,
  396. bool sync) { }
  397. static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  398. struct blkio_policy_type *pol,
  399. struct blkio_group *curr_blkg, bool direction,
  400. bool sync) { }
  401. static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  402. struct blkio_policy_type *pol, bool direction,
  403. bool sync) { }
  404. #endif
  405. #endif /* _BLK_CGROUP_H */