blk-wbt.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef WB_THROTTLE_H
  3. #define WB_THROTTLE_H
  4. #include <linux/kernel.h>
  5. #include <linux/atomic.h>
  6. #include <linux/wait.h>
  7. #include <linux/timer.h>
  8. #include <linux/ktime.h>
  9. #include "blk-stat.h"
  10. #include "blk-rq-qos.h"
  11. enum wbt_flags {
  12. WBT_TRACKED = 1, /* write, tracked for throttling */
  13. WBT_READ = 2, /* read */
  14. WBT_KSWAPD = 4, /* write, from kswapd */
  15. WBT_DISCARD = 8, /* discard */
  16. WBT_NR_BITS = 4, /* number of bits */
  17. };
  18. enum {
  19. WBT_RWQ_BG = 0,
  20. WBT_RWQ_KSWAPD,
  21. WBT_RWQ_DISCARD,
  22. WBT_NUM_RWQ,
  23. };
  24. /*
  25. * Enable states. Either off, or on by default (done at init time),
  26. * or on through manual setup in sysfs.
  27. */
  28. enum {
  29. WBT_STATE_ON_DEFAULT = 1,
  30. WBT_STATE_ON_MANUAL = 2,
  31. };
  32. struct rq_wb {
  33. /*
  34. * Settings that govern how we throttle
  35. */
  36. unsigned int wb_background; /* background writeback */
  37. unsigned int wb_normal; /* normal writeback */
  38. short enable_state; /* WBT_STATE_* */
  39. /*
  40. * Number of consecutive periods where we don't have enough
  41. * information to make a firm scale up/down decision.
  42. */
  43. unsigned int unknown_cnt;
  44. u64 win_nsec; /* default window size */
  45. u64 cur_win_nsec; /* current window size */
  46. struct blk_stat_callback *cb;
  47. u64 sync_issue;
  48. void *sync_cookie;
  49. unsigned int wc;
  50. unsigned long last_issue; /* last non-throttled issue */
  51. unsigned long last_comp; /* last non-throttled comp */
  52. unsigned long min_lat_nsec;
  53. struct rq_qos rqos;
  54. struct rq_wait rq_wait[WBT_NUM_RWQ];
  55. struct rq_depth rq_depth;
  56. };
  57. static inline struct rq_wb *RQWB(struct rq_qos *rqos)
  58. {
  59. return container_of(rqos, struct rq_wb, rqos);
  60. }
  61. static inline unsigned int wbt_inflight(struct rq_wb *rwb)
  62. {
  63. unsigned int i, ret = 0;
  64. for (i = 0; i < WBT_NUM_RWQ; i++)
  65. ret += atomic_read(&rwb->rq_wait[i].inflight);
  66. return ret;
  67. }
  68. #ifdef CONFIG_BLK_WBT
  69. int wbt_init(struct request_queue *);
  70. void wbt_update_limits(struct request_queue *);
  71. void wbt_disable_default(struct request_queue *);
  72. void wbt_enable_default(struct request_queue *);
  73. u64 wbt_get_min_lat(struct request_queue *q);
  74. void wbt_set_min_lat(struct request_queue *q, u64 val);
  75. void wbt_set_queue_depth(struct request_queue *, unsigned int);
  76. void wbt_set_write_cache(struct request_queue *, bool);
  77. u64 wbt_default_latency_nsec(struct request_queue *);
  78. #else
  79. static inline void wbt_track(struct request *rq, enum wbt_flags flags)
  80. {
  81. }
  82. static inline int wbt_init(struct request_queue *q)
  83. {
  84. return -EINVAL;
  85. }
  86. static inline void wbt_update_limits(struct request_queue *q)
  87. {
  88. }
  89. static inline void wbt_disable_default(struct request_queue *q)
  90. {
  91. }
  92. static inline void wbt_enable_default(struct request_queue *q)
  93. {
  94. }
  95. static inline void wbt_set_queue_depth(struct request_queue *q, unsigned int depth)
  96. {
  97. }
  98. static inline void wbt_set_write_cache(struct request_queue *q, bool wc)
  99. {
  100. }
  101. static inline u64 wbt_get_min_lat(struct request_queue *q)
  102. {
  103. return 0;
  104. }
  105. static inline void wbt_set_min_lat(struct request_queue *q, u64 val)
  106. {
  107. }
  108. static inline u64 wbt_default_latency_nsec(struct request_queue *q)
  109. {
  110. return 0;
  111. }
  112. #endif /* CONFIG_BLK_WBT */
  113. #endif