blk-cgroup.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /* Per cpu stats */
  49. enum stat_type_cpu {
  50. BLKIO_STAT_CPU_SECTORS,
  51. /* Total bytes transferred */
  52. BLKIO_STAT_CPU_SERVICE_BYTES,
  53. /* Total IOs serviced, post merge */
  54. BLKIO_STAT_CPU_SERVICED,
  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. /* Time spent idling for this blkio_group */
  120. uint64_t idle_time;
  121. /*
  122. * Total time when we have requests queued and do not contain the
  123. * current active queue.
  124. */
  125. uint64_t empty_time;
  126. /* fields after this shouldn't be cleared on stat reset */
  127. uint64_t start_group_wait_time;
  128. uint64_t start_idle_time;
  129. uint64_t start_empty_time;
  130. uint16_t flags;
  131. #endif
  132. };
  133. #ifdef CONFIG_DEBUG_BLK_CGROUP
  134. #define BLKG_STATS_DEBUG_CLEAR_START \
  135. offsetof(struct blkio_group_stats, unaccounted_time)
  136. #define BLKG_STATS_DEBUG_CLEAR_SIZE \
  137. (offsetof(struct blkio_group_stats, start_group_wait_time) - \
  138. BLKG_STATS_DEBUG_CLEAR_START)
  139. #endif
  140. /* Per cpu blkio group stats */
  141. struct blkio_group_stats_cpu {
  142. uint64_t sectors;
  143. uint64_t stat_arr_cpu[BLKIO_STAT_CPU_NR][BLKIO_STAT_TOTAL];
  144. struct u64_stats_sync syncp;
  145. };
  146. struct blkio_group_conf {
  147. unsigned int weight;
  148. unsigned int iops[2];
  149. u64 bps[2];
  150. };
  151. /* per-blkg per-policy data */
  152. struct blkg_policy_data {
  153. /* the blkg this per-policy data belongs to */
  154. struct blkio_group *blkg;
  155. /* Configuration */
  156. struct blkio_group_conf conf;
  157. struct blkio_group_stats stats;
  158. /* Per cpu stats pointer */
  159. struct blkio_group_stats_cpu __percpu *stats_cpu;
  160. /* pol->pdata_size bytes of private data used by policy impl */
  161. char pdata[] __aligned(__alignof__(unsigned long long));
  162. };
  163. struct blkio_group {
  164. /* Pointer to the associated request_queue */
  165. struct request_queue *q;
  166. struct list_head q_node;
  167. struct hlist_node blkcg_node;
  168. struct blkio_cgroup *blkcg;
  169. /* Store cgroup path */
  170. char path[128];
  171. /* reference count */
  172. int refcnt;
  173. /* Need to serialize the stats in the case of reset/update */
  174. spinlock_t stats_lock;
  175. struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
  176. /* List of blkg waiting for per cpu stats memory to be allocated */
  177. struct list_head alloc_node;
  178. struct rcu_head rcu_head;
  179. };
  180. typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
  181. typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
  182. struct blkio_group *blkg, unsigned int weight);
  183. typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
  184. struct blkio_group *blkg, u64 read_bps);
  185. typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
  186. struct blkio_group *blkg, u64 write_bps);
  187. typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
  188. struct blkio_group *blkg, unsigned int read_iops);
  189. typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
  190. struct blkio_group *blkg, unsigned int write_iops);
  191. struct blkio_policy_ops {
  192. blkio_init_group_fn *blkio_init_group_fn;
  193. blkio_update_group_weight_fn *blkio_update_group_weight_fn;
  194. blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
  195. blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
  196. blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
  197. blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
  198. };
  199. struct blkio_policy_type {
  200. struct list_head list;
  201. struct blkio_policy_ops ops;
  202. enum blkio_policy_id plid;
  203. size_t pdata_size; /* policy specific private data size */
  204. };
  205. extern int blkcg_init_queue(struct request_queue *q);
  206. extern void blkcg_drain_queue(struct request_queue *q);
  207. extern void blkcg_exit_queue(struct request_queue *q);
  208. /* Blkio controller policy registration */
  209. extern void blkio_policy_register(struct blkio_policy_type *);
  210. extern void blkio_policy_unregister(struct blkio_policy_type *);
  211. extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
  212. extern void update_root_blkg_pd(struct request_queue *q,
  213. enum blkio_policy_id plid);
  214. /**
  215. * blkg_to_pdata - get policy private data
  216. * @blkg: blkg of interest
  217. * @pol: policy of interest
  218. *
  219. * Return pointer to private data associated with the @blkg-@pol pair.
  220. */
  221. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  222. struct blkio_policy_type *pol)
  223. {
  224. return blkg ? blkg->pd[pol->plid]->pdata : NULL;
  225. }
  226. /**
  227. * pdata_to_blkg - get blkg associated with policy private data
  228. * @pdata: policy private data of interest
  229. * @pol: policy @pdata is for
  230. *
  231. * @pdata is policy private data for @pol. Determine the blkg it's
  232. * associated with.
  233. */
  234. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  235. struct blkio_policy_type *pol)
  236. {
  237. if (pdata) {
  238. struct blkg_policy_data *pd =
  239. container_of(pdata, struct blkg_policy_data, pdata);
  240. return pd->blkg;
  241. }
  242. return NULL;
  243. }
  244. static inline char *blkg_path(struct blkio_group *blkg)
  245. {
  246. return blkg->path;
  247. }
  248. /**
  249. * blkg_get - get a blkg reference
  250. * @blkg: blkg to get
  251. *
  252. * The caller should be holding queue_lock and an existing reference.
  253. */
  254. static inline void blkg_get(struct blkio_group *blkg)
  255. {
  256. lockdep_assert_held(blkg->q->queue_lock);
  257. WARN_ON_ONCE(!blkg->refcnt);
  258. blkg->refcnt++;
  259. }
  260. void __blkg_release(struct blkio_group *blkg);
  261. /**
  262. * blkg_put - put a blkg reference
  263. * @blkg: blkg to put
  264. *
  265. * The caller should be holding queue_lock.
  266. */
  267. static inline void blkg_put(struct blkio_group *blkg)
  268. {
  269. lockdep_assert_held(blkg->q->queue_lock);
  270. WARN_ON_ONCE(blkg->refcnt <= 0);
  271. if (!--blkg->refcnt)
  272. __blkg_release(blkg);
  273. }
  274. #else
  275. struct blkio_group {
  276. };
  277. struct blkio_policy_type {
  278. };
  279. static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
  280. static inline void blkcg_drain_queue(struct request_queue *q) { }
  281. static inline void blkcg_exit_queue(struct request_queue *q) { }
  282. static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
  283. static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
  284. static inline void blkg_destroy_all(struct request_queue *q,
  285. bool destory_root) { }
  286. static inline void update_root_blkg_pd(struct request_queue *q,
  287. enum blkio_policy_id plid) { }
  288. static inline void *blkg_to_pdata(struct blkio_group *blkg,
  289. struct blkio_policy_type *pol) { return NULL; }
  290. static inline struct blkio_group *pdata_to_blkg(void *pdata,
  291. struct blkio_policy_type *pol) { return NULL; }
  292. static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
  293. static inline void blkg_get(struct blkio_group *blkg) { }
  294. static inline void blkg_put(struct blkio_group *blkg) { }
  295. #endif
  296. #define BLKIO_WEIGHT_MIN 10
  297. #define BLKIO_WEIGHT_MAX 1000
  298. #define BLKIO_WEIGHT_DEFAULT 500
  299. #ifdef CONFIG_DEBUG_BLK_CGROUP
  300. void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  301. struct blkio_policy_type *pol);
  302. void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  303. struct blkio_policy_type *pol,
  304. unsigned long dequeue);
  305. void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  306. struct blkio_policy_type *pol);
  307. void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  308. struct blkio_policy_type *pol);
  309. void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  310. struct blkio_policy_type *pol);
  311. #define BLKG_FLAG_FNS(name) \
  312. static inline void blkio_mark_blkg_##name( \
  313. struct blkio_group_stats *stats) \
  314. { \
  315. stats->flags |= (1 << BLKG_##name); \
  316. } \
  317. static inline void blkio_clear_blkg_##name( \
  318. struct blkio_group_stats *stats) \
  319. { \
  320. stats->flags &= ~(1 << BLKG_##name); \
  321. } \
  322. static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
  323. { \
  324. return (stats->flags & (1 << BLKG_##name)) != 0; \
  325. } \
  326. BLKG_FLAG_FNS(waiting)
  327. BLKG_FLAG_FNS(idling)
  328. BLKG_FLAG_FNS(empty)
  329. #undef BLKG_FLAG_FNS
  330. #else
  331. static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
  332. struct blkio_policy_type *pol) { }
  333. static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
  334. struct blkio_policy_type *pol, unsigned long dequeue) { }
  335. static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
  336. struct blkio_policy_type *pol) { }
  337. static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
  338. struct blkio_policy_type *pol) { }
  339. static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
  340. struct blkio_policy_type *pol) { }
  341. #endif
  342. #ifdef CONFIG_BLK_CGROUP
  343. extern struct blkio_cgroup blkio_root_cgroup;
  344. extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
  345. extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
  346. extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  347. struct request_queue *q);
  348. struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
  349. struct request_queue *q,
  350. enum blkio_policy_id plid,
  351. bool for_root);
  352. void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  353. struct blkio_policy_type *pol,
  354. unsigned long time,
  355. unsigned long unaccounted_time);
  356. void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  357. struct blkio_policy_type *pol,
  358. uint64_t bytes, bool direction, bool sync);
  359. void blkiocg_update_completion_stats(struct blkio_group *blkg,
  360. struct blkio_policy_type *pol,
  361. uint64_t start_time,
  362. uint64_t io_start_time, bool direction,
  363. bool sync);
  364. void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  365. struct blkio_policy_type *pol,
  366. bool direction, bool sync);
  367. void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  368. struct blkio_policy_type *pol,
  369. struct blkio_group *curr_blkg, bool direction,
  370. bool sync);
  371. void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  372. struct blkio_policy_type *pol,
  373. bool direction, bool sync);
  374. #else
  375. struct cgroup;
  376. static inline struct blkio_cgroup *
  377. cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
  378. static inline struct blkio_cgroup *
  379. bio_blkio_cgroup(struct bio *bio) { return NULL; }
  380. static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
  381. void *key) { return NULL; }
  382. static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
  383. struct blkio_policy_type *pol, unsigned long time,
  384. unsigned long unaccounted_time) { }
  385. static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
  386. struct blkio_policy_type *pol, uint64_t bytes,
  387. bool direction, bool sync) { }
  388. static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
  389. struct blkio_policy_type *pol, uint64_t start_time,
  390. uint64_t io_start_time, bool direction, bool sync) { }
  391. static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
  392. struct blkio_policy_type *pol, bool direction,
  393. bool sync) { }
  394. static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
  395. struct blkio_policy_type *pol,
  396. struct blkio_group *curr_blkg, bool direction,
  397. bool sync) { }
  398. static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
  399. struct blkio_policy_type *pol, bool direction,
  400. bool sync) { }
  401. #endif
  402. #endif /* _BLK_CGROUP_H */