blob: 8038b4a0d4efcccf907d8f1e141d1e8e730db917 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Jens Axboee34cbd32016-11-09 12:36:15 -07002#ifndef WB_THROTTLE_H
3#define WB_THROTTLE_H
4
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>
10
11#include "blk-stat.h"
12
13enum wbt_flags {
14 WBT_TRACKED = 1, /* write, tracked for throttling */
15 WBT_READ = 2, /* read */
16 WBT_KSWAPD = 4, /* write, from kswapd */
17
18 WBT_NR_BITS = 3, /* number of bits */
19};
20
21enum {
Jens Axboe8bea6092018-05-07 09:57:08 -060022 WBT_RWQ_BG = 0,
23 WBT_RWQ_KSWAPD,
24 WBT_NUM_RWQ,
Jens Axboee34cbd32016-11-09 12:36:15 -070025};
26
Jens Axboed62118b2016-11-28 09:40:34 -070027/*
28 * Enable states. Either off, or on by default (done at init time),
29 * or on through manual setup in sysfs.
30 */
31enum {
32 WBT_STATE_ON_DEFAULT = 1,
33 WBT_STATE_ON_MANUAL = 2,
34};
35
Jens Axboee34cbd32016-11-09 12:36:15 -070036static inline void wbt_clear_state(struct blk_issue_stat *stat)
37{
Shaohua Li88eeca42017-03-27 15:19:41 -070038 stat->stat &= ~BLK_STAT_RES_MASK;
Jens Axboee34cbd32016-11-09 12:36:15 -070039}
40
41static inline enum wbt_flags wbt_stat_to_mask(struct blk_issue_stat *stat)
42{
Shaohua Li88eeca42017-03-27 15:19:41 -070043 return (stat->stat & BLK_STAT_RES_MASK) >> BLK_STAT_RES_SHIFT;
Jens Axboee34cbd32016-11-09 12:36:15 -070044}
45
46static inline void wbt_track(struct blk_issue_stat *stat, enum wbt_flags wb_acct)
47{
Shaohua Li88eeca42017-03-27 15:19:41 -070048 stat->stat |= ((u64) wb_acct) << BLK_STAT_RES_SHIFT;
Jens Axboee34cbd32016-11-09 12:36:15 -070049}
50
51static inline bool wbt_is_tracked(struct blk_issue_stat *stat)
52{
Shaohua Li88eeca42017-03-27 15:19:41 -070053 return (stat->stat >> BLK_STAT_RES_SHIFT) & WBT_TRACKED;
Jens Axboee34cbd32016-11-09 12:36:15 -070054}
55
56static inline bool wbt_is_read(struct blk_issue_stat *stat)
57{
Shaohua Li88eeca42017-03-27 15:19:41 -070058 return (stat->stat >> BLK_STAT_RES_SHIFT) & WBT_READ;
Jens Axboee34cbd32016-11-09 12:36:15 -070059}
60
Jens Axboee34cbd32016-11-09 12:36:15 -070061struct rq_wait {
62 wait_queue_head_t wait;
63 atomic_t inflight;
64};
65
66struct rq_wb {
67 /*
68 * Settings that govern how we throttle
69 */
70 unsigned int wb_background; /* background writeback */
71 unsigned int wb_normal; /* normal writeback */
72 unsigned int wb_max; /* max throughput writeback */
73 int scale_step;
74 bool scaled_max;
75
Jens Axboed62118b2016-11-28 09:40:34 -070076 short enable_state; /* WBT_STATE_* */
77
Jens Axboee34cbd32016-11-09 12:36:15 -070078 /*
79 * Number of consecutive periods where we don't have enough
80 * information to make a firm scale up/down decision.
81 */
82 unsigned int unknown_cnt;
83
84 u64 win_nsec; /* default window size */
85 u64 cur_win_nsec; /* current window size */
86
Omar Sandoval34dbad52017-03-21 08:56:08 -070087 struct blk_stat_callback *cb;
Jens Axboee34cbd32016-11-09 12:36:15 -070088
89 s64 sync_issue;
90 void *sync_cookie;
91
92 unsigned int wc;
93 unsigned int queue_depth;
94
95 unsigned long last_issue; /* last non-throttled issue */
96 unsigned long last_comp; /* last non-throttled comp */
97 unsigned long min_lat_nsec;
Jens Axboed8a0cbf2016-11-10 21:52:53 -070098 struct request_queue *queue;
Jens Axboee34cbd32016-11-09 12:36:15 -070099 struct rq_wait rq_wait[WBT_NUM_RWQ];
Jens Axboee34cbd32016-11-09 12:36:15 -0700100};
101
102static inline unsigned int wbt_inflight(struct rq_wb *rwb)
103{
104 unsigned int i, ret = 0;
105
106 for (i = 0; i < WBT_NUM_RWQ; i++)
107 ret += atomic_read(&rwb->rq_wait[i].inflight);
108
109 return ret;
110}
111
Jens Axboee34cbd32016-11-09 12:36:15 -0700112#ifdef CONFIG_BLK_WBT
113
114void __wbt_done(struct rq_wb *, enum wbt_flags);
115void wbt_done(struct rq_wb *, struct blk_issue_stat *);
116enum wbt_flags wbt_wait(struct rq_wb *, struct bio *, spinlock_t *);
Jens Axboe8054b892016-11-10 21:50:51 -0700117int wbt_init(struct request_queue *);
Jens Axboee34cbd32016-11-09 12:36:15 -0700118void wbt_exit(struct request_queue *);
119void wbt_update_limits(struct rq_wb *);
120void wbt_requeue(struct rq_wb *, struct blk_issue_stat *);
121void wbt_issue(struct rq_wb *, struct blk_issue_stat *);
Jens Axboefa224ee2016-11-28 09:25:50 -0700122void wbt_disable_default(struct request_queue *);
Jan Kara8330cdb2017-04-19 11:33:27 +0200123void wbt_enable_default(struct request_queue *);
Jens Axboee34cbd32016-11-09 12:36:15 -0700124
125void wbt_set_queue_depth(struct rq_wb *, unsigned int);
126void wbt_set_write_cache(struct rq_wb *, bool);
127
Jens Axboe80e091d2016-11-28 09:22:47 -0700128u64 wbt_default_latency_nsec(struct request_queue *);
129
Jens Axboee34cbd32016-11-09 12:36:15 -0700130#else
131
132static inline void __wbt_done(struct rq_wb *rwb, enum wbt_flags flags)
133{
134}
135static inline void wbt_done(struct rq_wb *rwb, struct blk_issue_stat *stat)
136{
137}
138static inline enum wbt_flags wbt_wait(struct rq_wb *rwb, struct bio *bio,
139 spinlock_t *lock)
140{
141 return 0;
142}
Jens Axboe8054b892016-11-10 21:50:51 -0700143static inline int wbt_init(struct request_queue *q)
Jens Axboee34cbd32016-11-09 12:36:15 -0700144{
145 return -EINVAL;
146}
147static inline void wbt_exit(struct request_queue *q)
148{
149}
150static inline void wbt_update_limits(struct rq_wb *rwb)
151{
152}
153static inline void wbt_requeue(struct rq_wb *rwb, struct blk_issue_stat *stat)
154{
155}
156static inline void wbt_issue(struct rq_wb *rwb, struct blk_issue_stat *stat)
157{
158}
Jens Axboefa224ee2016-11-28 09:25:50 -0700159static inline void wbt_disable_default(struct request_queue *q)
Jens Axboee34cbd32016-11-09 12:36:15 -0700160{
161}
Jan Kara8330cdb2017-04-19 11:33:27 +0200162static inline void wbt_enable_default(struct request_queue *q)
163{
164}
Jens Axboee34cbd32016-11-09 12:36:15 -0700165static inline void wbt_set_queue_depth(struct rq_wb *rwb, unsigned int depth)
166{
167}
168static inline void wbt_set_write_cache(struct rq_wb *rwb, bool wc)
169{
170}
Jens Axboe80e091d2016-11-28 09:22:47 -0700171static inline u64 wbt_default_latency_nsec(struct request_queue *q)
172{
173 return 0;
174}
Jens Axboee34cbd32016-11-09 12:36:15 -0700175
176#endif /* CONFIG_BLK_WBT */
177
178#endif