blob: d6a125e49db5761bc9c1624791b72acb363977c3 [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 */
Jens Axboe782f5692018-05-07 10:03:23 -060017 WBT_DISCARD = 8, /* discard */
Jens Axboee34cbd32016-11-09 12:36:15 -070018
Jens Axboe782f5692018-05-07 10:03:23 -060019 WBT_NR_BITS = 4, /* number of bits */
Jens Axboee34cbd32016-11-09 12:36:15 -070020};
21
22enum {
Jens Axboe8bea6092018-05-07 09:57:08 -060023 WBT_RWQ_BG = 0,
24 WBT_RWQ_KSWAPD,
Jens Axboe782f5692018-05-07 10:03:23 -060025 WBT_RWQ_DISCARD,
Jens Axboe8bea6092018-05-07 09:57:08 -060026 WBT_NUM_RWQ,
Jens Axboee34cbd32016-11-09 12:36:15 -070027};
28
Jens Axboed62118b2016-11-28 09:40:34 -070029/*
30 * Enable states. Either off, or on by default (done at init time),
31 * or on through manual setup in sysfs.
32 */
33enum {
34 WBT_STATE_ON_DEFAULT = 1,
35 WBT_STATE_ON_MANUAL = 2,
36};
37
Jens Axboee34cbd32016-11-09 12:36:15 -070038static inline void wbt_clear_state(struct blk_issue_stat *stat)
39{
Shaohua Li88eeca42017-03-27 15:19:41 -070040 stat->stat &= ~BLK_STAT_RES_MASK;
Jens Axboee34cbd32016-11-09 12:36:15 -070041}
42
43static inline enum wbt_flags wbt_stat_to_mask(struct blk_issue_stat *stat)
44{
Shaohua Li88eeca42017-03-27 15:19:41 -070045 return (stat->stat & BLK_STAT_RES_MASK) >> BLK_STAT_RES_SHIFT;
Jens Axboee34cbd32016-11-09 12:36:15 -070046}
47
48static inline void wbt_track(struct blk_issue_stat *stat, enum wbt_flags wb_acct)
49{
Shaohua Li88eeca42017-03-27 15:19:41 -070050 stat->stat |= ((u64) wb_acct) << BLK_STAT_RES_SHIFT;
Jens Axboee34cbd32016-11-09 12:36:15 -070051}
52
53static inline bool wbt_is_tracked(struct blk_issue_stat *stat)
54{
Shaohua Li88eeca42017-03-27 15:19:41 -070055 return (stat->stat >> BLK_STAT_RES_SHIFT) & WBT_TRACKED;
Jens Axboee34cbd32016-11-09 12:36:15 -070056}
57
58static inline bool wbt_is_read(struct blk_issue_stat *stat)
59{
Shaohua Li88eeca42017-03-27 15:19:41 -070060 return (stat->stat >> BLK_STAT_RES_SHIFT) & WBT_READ;
Jens Axboee34cbd32016-11-09 12:36:15 -070061}
62
Jens Axboee34cbd32016-11-09 12:36:15 -070063struct rq_wait {
64 wait_queue_head_t wait;
65 atomic_t inflight;
66};
67
68struct rq_wb {
69 /*
70 * Settings that govern how we throttle
71 */
72 unsigned int wb_background; /* background writeback */
73 unsigned int wb_normal; /* normal writeback */
74 unsigned int wb_max; /* max throughput writeback */
75 int scale_step;
76 bool scaled_max;
77
Jens Axboed62118b2016-11-28 09:40:34 -070078 short enable_state; /* WBT_STATE_* */
79
Jens Axboee34cbd32016-11-09 12:36:15 -070080 /*
81 * Number of consecutive periods where we don't have enough
82 * information to make a firm scale up/down decision.
83 */
84 unsigned int unknown_cnt;
85
86 u64 win_nsec; /* default window size */
87 u64 cur_win_nsec; /* current window size */
88
Omar Sandoval34dbad52017-03-21 08:56:08 -070089 struct blk_stat_callback *cb;
Jens Axboee34cbd32016-11-09 12:36:15 -070090
91 s64 sync_issue;
92 void *sync_cookie;
93
94 unsigned int wc;
95 unsigned int queue_depth;
96
97 unsigned long last_issue; /* last non-throttled issue */
98 unsigned long last_comp; /* last non-throttled comp */
99 unsigned long min_lat_nsec;
Jens Axboed8a0cbf2016-11-10 21:52:53 -0700100 struct request_queue *queue;
Jens Axboee34cbd32016-11-09 12:36:15 -0700101 struct rq_wait rq_wait[WBT_NUM_RWQ];
Jens Axboee34cbd32016-11-09 12:36:15 -0700102};
103
104static inline unsigned int wbt_inflight(struct rq_wb *rwb)
105{
106 unsigned int i, ret = 0;
107
108 for (i = 0; i < WBT_NUM_RWQ; i++)
109 ret += atomic_read(&rwb->rq_wait[i].inflight);
110
111 return ret;
112}
113
Jens Axboee34cbd32016-11-09 12:36:15 -0700114#ifdef CONFIG_BLK_WBT
115
116void __wbt_done(struct rq_wb *, enum wbt_flags);
117void wbt_done(struct rq_wb *, struct blk_issue_stat *);
118enum wbt_flags wbt_wait(struct rq_wb *, struct bio *, spinlock_t *);
Jens Axboe8054b892016-11-10 21:50:51 -0700119int wbt_init(struct request_queue *);
Jens Axboee34cbd32016-11-09 12:36:15 -0700120void wbt_exit(struct request_queue *);
121void wbt_update_limits(struct rq_wb *);
122void wbt_requeue(struct rq_wb *, struct blk_issue_stat *);
123void wbt_issue(struct rq_wb *, struct blk_issue_stat *);
Jens Axboefa224ee2016-11-28 09:25:50 -0700124void wbt_disable_default(struct request_queue *);
Jan Kara8330cdb2017-04-19 11:33:27 +0200125void wbt_enable_default(struct request_queue *);
Jens Axboee34cbd32016-11-09 12:36:15 -0700126
127void wbt_set_queue_depth(struct rq_wb *, unsigned int);
128void wbt_set_write_cache(struct rq_wb *, bool);
129
Jens Axboe80e091d2016-11-28 09:22:47 -0700130u64 wbt_default_latency_nsec(struct request_queue *);
131
Jens Axboee34cbd32016-11-09 12:36:15 -0700132#else
133
134static inline void __wbt_done(struct rq_wb *rwb, enum wbt_flags flags)
135{
136}
137static inline void wbt_done(struct rq_wb *rwb, struct blk_issue_stat *stat)
138{
139}
140static inline enum wbt_flags wbt_wait(struct rq_wb *rwb, struct bio *bio,
141 spinlock_t *lock)
142{
143 return 0;
144}
Jens Axboe8054b892016-11-10 21:50:51 -0700145static inline int wbt_init(struct request_queue *q)
Jens Axboee34cbd32016-11-09 12:36:15 -0700146{
147 return -EINVAL;
148}
149static inline void wbt_exit(struct request_queue *q)
150{
151}
152static inline void wbt_update_limits(struct rq_wb *rwb)
153{
154}
155static inline void wbt_requeue(struct rq_wb *rwb, struct blk_issue_stat *stat)
156{
157}
158static inline void wbt_issue(struct rq_wb *rwb, struct blk_issue_stat *stat)
159{
160}
Jens Axboefa224ee2016-11-28 09:25:50 -0700161static inline void wbt_disable_default(struct request_queue *q)
Jens Axboee34cbd32016-11-09 12:36:15 -0700162{
163}
Jan Kara8330cdb2017-04-19 11:33:27 +0200164static inline void wbt_enable_default(struct request_queue *q)
165{
166}
Jens Axboee34cbd32016-11-09 12:36:15 -0700167static inline void wbt_set_queue_depth(struct rq_wb *rwb, unsigned int depth)
168{
169}
170static inline void wbt_set_write_cache(struct rq_wb *rwb, bool wc)
171{
172}
Jens Axboe80e091d2016-11-28 09:22:47 -0700173static inline u64 wbt_default_latency_nsec(struct request_queue *q)
174{
175 return 0;
176}
Jens Axboee34cbd32016-11-09 12:36:15 -0700177
178#endif /* CONFIG_BLK_WBT */
179
180#endif