blk-cgroup.h 14 KB

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