1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/kernel.h>
6 #include <linux/atomic.h>
7 #include <linux/wait.h>
8 #include <linux/timer.h>
9 #include <linux/ktime.h>
14 WBT_TRACKED = 1, /* write, tracked for throttling */
15 WBT_READ = 2, /* read */
16 WBT_KSWAPD = 4, /* write, from kswapd */
18 WBT_NR_BITS = 3, /* number of bits */
26 * Enable states. Either off, or on by default (done at init time),
27 * or on through manual setup in sysfs.
30 WBT_STATE_ON_DEFAULT = 1,
31 WBT_STATE_ON_MANUAL = 2,
34 static inline void wbt_clear_state(struct blk_issue_stat *stat)
36 stat->stat &= ~BLK_STAT_RES_MASK;
39 static inline enum wbt_flags wbt_stat_to_mask(struct blk_issue_stat *stat)
41 return (stat->stat & BLK_STAT_RES_MASK) >> BLK_STAT_RES_SHIFT;
44 static inline void wbt_track(struct blk_issue_stat *stat, enum wbt_flags wb_acct)
46 stat->stat |= ((u64) wb_acct) << BLK_STAT_RES_SHIFT;
49 static inline bool wbt_is_tracked(struct blk_issue_stat *stat)
51 return (stat->stat >> BLK_STAT_RES_SHIFT) & WBT_TRACKED;
54 static inline bool wbt_is_read(struct blk_issue_stat *stat)
56 return (stat->stat >> BLK_STAT_RES_SHIFT) & WBT_READ;
60 wait_queue_head_t wait;
66 * Settings that govern how we throttle
68 unsigned int wb_background; /* background writeback */
69 unsigned int wb_normal; /* normal writeback */
70 unsigned int wb_max; /* max throughput writeback */
74 short enable_state; /* WBT_STATE_* */
77 * Number of consecutive periods where we don't have enough
78 * information to make a firm scale up/down decision.
80 unsigned int unknown_cnt;
82 u64 win_nsec; /* default window size */
83 u64 cur_win_nsec; /* current window size */
85 struct blk_stat_callback *cb;
91 unsigned int queue_depth;
93 unsigned long last_issue; /* last non-throttled issue */
94 unsigned long last_comp; /* last non-throttled comp */
95 unsigned long min_lat_nsec;
96 struct request_queue *queue;
97 struct rq_wait rq_wait[WBT_NUM_RWQ];
100 static inline unsigned int wbt_inflight(struct rq_wb *rwb)
102 unsigned int i, ret = 0;
104 for (i = 0; i < WBT_NUM_RWQ; i++)
105 ret += atomic_read(&rwb->rq_wait[i].inflight);
110 #ifdef CONFIG_BLK_WBT
112 void __wbt_done(struct rq_wb *, enum wbt_flags);
113 void wbt_done(struct rq_wb *, struct blk_issue_stat *);
114 enum wbt_flags wbt_wait(struct rq_wb *, struct bio *, spinlock_t *);
115 int wbt_init(struct request_queue *);
116 void wbt_exit(struct request_queue *);
117 void wbt_update_limits(struct rq_wb *);
118 void wbt_requeue(struct rq_wb *, struct blk_issue_stat *);
119 void wbt_issue(struct rq_wb *, struct blk_issue_stat *);
120 void wbt_disable_default(struct request_queue *);
121 void wbt_enable_default(struct request_queue *);
123 void wbt_set_queue_depth(struct rq_wb *, unsigned int);
124 void wbt_set_write_cache(struct rq_wb *, bool);
126 u64 wbt_default_latency_nsec(struct request_queue *);
130 static inline void __wbt_done(struct rq_wb *rwb, enum wbt_flags flags)
133 static inline void wbt_done(struct rq_wb *rwb, struct blk_issue_stat *stat)
136 static inline enum wbt_flags wbt_wait(struct rq_wb *rwb, struct bio *bio,
141 static inline int wbt_init(struct request_queue *q)
145 static inline void wbt_exit(struct request_queue *q)
148 static inline void wbt_update_limits(struct rq_wb *rwb)
151 static inline void wbt_requeue(struct rq_wb *rwb, struct blk_issue_stat *stat)
154 static inline void wbt_issue(struct rq_wb *rwb, struct blk_issue_stat *stat)
157 static inline void wbt_disable_default(struct request_queue *q)
160 static inline void wbt_enable_default(struct request_queue *q)
163 static inline void wbt_set_queue_depth(struct rq_wb *rwb, unsigned int depth)
166 static inline void wbt_set_write_cache(struct rq_wb *rwb, bool wc)
169 static inline u64 wbt_default_latency_nsec(struct request_queue *q)
174 #endif /* CONFIG_BLK_WBT */