postProcessingDataMemory.h 12 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. #include <linux/seq_file.h>
  18. #include <linux/radix-tree.h>
  19. #include <linux/blkdev.h>
  20. /* Max limits for throttle policy */
  21. #define THROTL_IOPS_MAX UINT_MAX
  22. /* CFQ specific, out here for blkcg->cfq_weight */
  23. #define CFQ_WEIGHT_MIN 10
  24. #define CFQ_WEIGHT_MAX 1000
  25. #define CFQ_WEIGHT_DEFAULT 500
  26. #ifdef CONFIG_BLK_CGROUP
  27. enum blkg_rwstat_type {
  28. BLKG_RWSTAT_READ,
  29. BLKG_RWSTAT_WRITE,
  30. BLKG_RWSTAT_SYNC,
  31. BLKG_RWSTAT_ASYNC,
  32. BLKG_RWSTAT_NR,
  33. BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
  34. };
  35. struct blkcg_gq;
  36. struct blkcg {
  37. struct cgroup_subsys_state css;
  38. spinlock_t lock;
  39. struct radix_tree_root blkg_tree;
  40. struct blkcg_gq *blkg_hint;
  41. struct hlist_head blkg_list;
  42. /* for policies to test whether associated blkcg has changed */
  43. uint64_t id;
  44. /* TODO: per-policy storage in blkcg */
  45. unsigned int cfq_weight; /* belongs to cfq */
  46. };
  47. struct blkg_stat {
  48. struct u64_stats_sync syncp;
  49. uint64_t cnt;
  50. };
  51. struct blkg_rwstat {
  52. struct u64_stats_sync syncp;
  53. uint64_t cnt[BLKG_RWSTAT_NR];
  54. };
  55. /*
  56. * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
  57. * request_queue (q). This is used by blkcg policies which need to track
  58. * information per blkcg - q pair.
  59. *
  60. * There can be multiple active blkcg policies and each has its private
  61. * data on each blkg, the size of which is determined by
  62. * blkcg_policy->pd_size. blkcg core allocates and frees such areas
  63. * together with blkg and invokes pd_init/exit_fn() methods.
  64. *
  65. * Such private data must embed struct blkg_policy_data (pd) at the
  66. * beginning and pd_size can't be smaller than pd.
  67. */
  68. struct blkg_policy_data {
  69. /* the blkg this per-policy data belongs to */
  70. struct blkcg_gq *blkg;
  71. /* used during policy activation */
  72. struct list_head alloc_node;
  73. };
  74. /* association between a blk cgroup and a request queue */
  75. struct blkcg_gq {
  76. /* Pointer to the associated request_queue */
  77. struct request_queue *q;
  78. struct list_head q_node;
  79. struct hlist_node blkcg_node;
  80. struct blkcg *blkcg;
  81. /* request allocation list for this blkcg-q pair */
  82. struct request_list rl;
  83. /* reference count */
  84. int refcnt;
  85. struct blkg_policy_data *pd[BLKCG_MAX_POLS];
  86. struct rcu_head rcu_head;
  87. };
  88. typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
  89. typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg);
  90. typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg);
  91. struct blkcg_policy {
  92. int plid;
  93. /* policy specific private data size */
  94. size_t pd_size;
  95. /* cgroup files for the policy */
  96. struct cftype *cftypes;
  97. /* operations */
  98. blkcg_pol_init_pd_fn *pd_init_fn;
  99. blkcg_pol_exit_pd_fn *pd_exit_fn;
  100. blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
  101. };
  102. extern struct blkcg blkcg_root;
  103. struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q);
  104. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  105. struct request_queue *q);
  106. int blkcg_init_queue(struct request_queue *q);
  107. void blkcg_drain_queue(struct request_queue *q);
  108. void blkcg_exit_queue(struct request_queue *q);
  109. /* Blkio controller policy registration */
  110. int blkcg_policy_register(struct blkcg_policy *pol);
  111. void blkcg_policy_unregister(struct blkcg_policy *pol);
  112. int blkcg_activate_policy(struct request_queue *q,
  113. const struct blkcg_policy *pol);
  114. void blkcg_deactivate_policy(struct request_queue *q,
  115. const struct blkcg_policy *pol);
  116. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  117. u64 (*prfill)(struct seq_file *,
  118. struct blkg_policy_data *, int),
  119. const struct blkcg_policy *pol, int data,
  120. bool show_total);
  121. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
  122. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  123. const struct blkg_rwstat *rwstat);
  124. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
  125. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  126. int off);
  127. struct blkg_conf_ctx {
  128. struct gendisk *disk;
  129. struct blkcg_gq *blkg;
  130. u64 v;
  131. };
  132. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  133. const char *input, struct blkg_conf_ctx *ctx);
  134. void blkg_conf_finish(struct blkg_conf_ctx *ctx);
  135. static inline struct blkcg *cgroup_to_blkcg(struct cgroup *cgroup)
  136. {
  137. return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
  138. struct blkcg, css);
  139. }
  140. static inline struct blkcg *task_blkcg(struct task_struct *tsk)
  141. {
  142. return container_of(task_subsys_state(tsk, blkio_subsys_id),
  143. struct blkcg, css);
  144. }
  145. static inline struct blkcg *bio_blkcg(struct bio *bio)
  146. {
  147. if (bio && bio->bi_css)
  148. return container_of(bio->bi_css, struct blkcg, css);
  149. return task_blkcg(current);
  150. }
  151. /**
  152. * blkg_to_pdata - get policy private data
  153. * @blkg: blkg of interest
  154. * @pol: policy of interest
  155. *
  156. * Return pointer to private data associated with the @blkg-@pol pair.
  157. */
  158. static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
  159. struct blkcg_policy *pol)
  160. {
  161. return blkg ? blkg->pd[pol->plid] : NULL;
  162. }
  163. /**
  164. * pdata_to_blkg - get blkg associated with policy private data
  165. * @pd: policy private data of interest
  166. *
  167. * @pd is policy private data. Determine the blkg it's associated with.
  168. */
  169. static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
  170. {
  171. return pd ? pd->blkg : NULL;
  172. }
  173. /**
  174. * blkg_path - format cgroup path of blkg
  175. * @blkg: blkg of interest
  176. * @buf: target buffer
  177. * @buflen: target buffer length
  178. *
  179. * Format the path of the cgroup of @blkg into @buf.
  180. */
  181. static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
  182. {
  183. int ret;
  184. rcu_read_lock();
  185. ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
  186. rcu_read_unlock();
  187. if (ret)
  188. strncpy(buf, "<unavailable>", buflen);
  189. return ret;
  190. }
  191. /**
  192. * blkg_get - get a blkg reference
  193. * @blkg: blkg to get
  194. *
  195. * The caller should be holding queue_lock and an existing reference.
  196. */
  197. static inline void blkg_get(struct blkcg_gq *blkg)
  198. {
  199. lockdep_assert_held(blkg->q->queue_lock);
  200. WARN_ON_ONCE(!blkg->refcnt);
  201. blkg->refcnt++;
  202. }
  203. void __blkg_release(struct blkcg_gq *blkg);
  204. /**
  205. * blkg_put - put a blkg reference
  206. * @blkg: blkg to put
  207. *
  208. * The caller should be holding queue_lock.
  209. */
  210. static inline void blkg_put(struct blkcg_gq *blkg)
  211. {
  212. lockdep_assert_held(blkg->q->queue_lock);
  213. WARN_ON_ONCE(blkg->refcnt <= 0);
  214. if (!--blkg->refcnt)
  215. __blkg_release(blkg);
  216. }
  217. /**
  218. * blk_get_rl - get request_list to use
  219. * @q: request_queue of interest
  220. * @bio: bio which will be attached to the allocated request (may be %NULL)
  221. *
  222. * The caller wants to allocate a request from @q to use for @bio. Find
  223. * the request_list to use and obtain a reference on it. Should be called
  224. * under queue_lock. This function is guaranteed to return non-%NULL
  225. * request_list.
  226. */
  227. static inline struct request_list *blk_get_rl(struct request_queue *q,
  228. struct bio *bio)
  229. {
  230. struct blkcg *blkcg;
  231. struct blkcg_gq *blkg;
  232. rcu_read_lock();
  233. blkcg = bio_blkcg(bio);
  234. /* bypass blkg lookup and use @q->root_rl directly for root */
  235. if (blkcg == &blkcg_root)
  236. goto root_rl;
  237. /*
  238. * Try to use blkg->rl. blkg lookup may fail under memory pressure
  239. * or if either the blkcg or queue is going away. Fall back to
  240. * root_rl in such cases.
  241. */
  242. blkg = blkg_lookup_create(blkcg, q);
  243. if (unlikely(IS_ERR(blkg)))
  244. goto root_rl;
  245. blkg_get(blkg);
  246. rcu_read_unlock();
  247. return &blkg->rl;
  248. root_rl:
  249. rcu_read_unlock();
  250. return &q->root_rl;
  251. }
  252. /**
  253. * blk_put_rl - put request_list
  254. * @rl: request_list to put
  255. *
  256. * Put the reference acquired by blk_get_rl(). Should be called under
  257. * queue_lock.
  258. */
  259. static inline void blk_put_rl(struct request_list *rl)
  260. {
  261. /* root_rl may not have blkg set */
  262. if (rl->blkg && rl->blkg->blkcg != &blkcg_root)
  263. blkg_put(rl->blkg);
  264. }
  265. /**
  266. * blk_rq_set_rl - associate a request with a request_list
  267. * @rq: request of interest
  268. * @rl: target request_list
  269. *
  270. * Associate @rq with @rl so that accounting and freeing can know the
  271. * request_list @rq came from.
  272. */
  273. static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl)
  274. {
  275. rq->rl = rl;
  276. }
  277. /**
  278. * blk_rq_rl - return the request_list a request came from
  279. * @rq: request of interest
  280. *
  281. * Return the request_list @rq is allocated from.
  282. */
  283. static inline struct request_list *blk_rq_rl(struct request *rq)
  284. {
  285. return rq->rl;
  286. }
  287. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  288. struct request_queue *q);
  289. /**
  290. * blk_queue_for_each_rl - iterate through all request_lists of a request_queue
  291. *
  292. * Should be used under queue_lock.
  293. */
  294. #define blk_queue_for_each_rl(rl, q) \
  295. for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q)))
  296. /**
  297. * blkg_stat_add - add a value to a blkg_stat
  298. * @stat: target blkg_stat
  299. * @val: value to add
  300. *
  301. * Add @val to @stat. The caller is responsible for synchronizing calls to
  302. * this function.
  303. */
  304. static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
  305. {
  306. u64_stats_update_begin(&stat->syncp);
  307. stat->cnt += val;
  308. u64_stats_update_end(&stat->syncp);
  309. }
  310. /**
  311. * blkg_stat_read - read the current value of a blkg_stat
  312. * @stat: blkg_stat to read
  313. *
  314. * Read the current value of @stat. This function can be called without
  315. * synchroniztion and takes care of u64 atomicity.
  316. */
  317. static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
  318. {
  319. unsigned int start;
  320. uint64_t v;
  321. do {
  322. start = u64_stats_fetch_begin(&stat->syncp);
  323. v = stat->cnt;
  324. } while (u64_stats_fetch_retry(&stat->syncp, start));
  325. return v;
  326. }
  327. /**
  328. * blkg_stat_reset - reset a blkg_stat
  329. * @stat: blkg_stat to reset
  330. */
  331. static inline void blkg_stat_reset(struct blkg_stat *stat)
  332. {
  333. stat->cnt = 0;
  334. }
  335. /**
  336. * blkg_rwstat_add - add a value to a blkg_rwstat
  337. * @rwstat: target blkg_rwstat
  338. * @rw: mask of REQ_{WRITE|SYNC}
  339. * @val: value to add
  340. *
  341. * Add @val to @rwstat. The counters are chosen according to @rw. The
  342. * caller is responsible for synchronizing calls to this function.
  343. */
  344. static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
  345. int rw, uint64_t val)
  346. {
  347. u64_stats_update_begin(&rwstat->syncp);
  348. if (rw & REQ_WRITE)
  349. rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
  350. else
  351. rwstat->cnt[BLKG_RWSTAT_READ] += val;
  352. if (rw & REQ_SYNC)
  353. rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
  354. else
  355. rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
  356. u64_stats_update_end(&rwstat->syncp);
  357. }
  358. /**
  359. * blkg_rwstat_read - read the current values of a blkg_rwstat
  360. * @rwstat: blkg_rwstat to read
  361. *
  362. * Read the current snapshot of @rwstat and return it as the return value.
  363. * This function can be called without synchronization and takes care of
  364. * u64 atomicity.
  365. */
  366. static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
  367. {
  368. unsigned int start;
  369. struct blkg_rwstat tmp;
  370. do {
  371. start = u64_stats_fetch_begin(&rwstat->syncp);
  372. tmp = *rwstat;
  373. } while (u64_stats_fetch_retry(&rwstat->syncp, start));
  374. return tmp;
  375. }
  376. /**
  377. * blkg_rwstat_sum - read the total count of a blkg_rwstat
  378. * @rwstat: blkg_rwstat to read
  379. *
  380. * Return the total count of @rwstat regardless of the IO direction. This
  381. * function can be called without synchronization and takes care of u64
  382. * atomicity.
  383. */
  384. static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
  385. {
  386. struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
  387. return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
  388. }
  389. /**
  390. * blkg_rwstat_reset - reset a blkg_rwstat
  391. * @rwstat: blkg_rwstat to reset
  392. */
  393. static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)