blk-cgroup.h 13 KB

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