blob: 2eb01becde8c412c11a7ffedd8a04bd8e41521ac [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"
Josef Bacika7905042018-07-03 09:32:35 -060012#include "blk-rq-qos.h"
Jens Axboee34cbd32016-11-09 12:36:15 -070013
14enum wbt_flags {
15 WBT_TRACKED = 1, /* write, tracked for throttling */
16 WBT_READ = 2, /* read */
17 WBT_KSWAPD = 4, /* write, from kswapd */
Jens Axboe782f5692018-05-07 10:03:23 -060018 WBT_DISCARD = 8, /* discard */
Jens Axboee34cbd32016-11-09 12:36:15 -070019
Jens Axboe782f5692018-05-07 10:03:23 -060020 WBT_NR_BITS = 4, /* number of bits */
Jens Axboee34cbd32016-11-09 12:36:15 -070021};
22
23enum {
Jens Axboe8bea6092018-05-07 09:57:08 -060024 WBT_RWQ_BG = 0,
25 WBT_RWQ_KSWAPD,
Jens Axboe782f5692018-05-07 10:03:23 -060026 WBT_RWQ_DISCARD,
Jens Axboe8bea6092018-05-07 09:57:08 -060027 WBT_NUM_RWQ,
Jens Axboee34cbd32016-11-09 12:36:15 -070028};
29
Jens Axboed62118b2016-11-28 09:40:34 -070030/*
31 * Enable states. Either off, or on by default (done at init time),
32 * or on through manual setup in sysfs.
33 */
34enum {
35 WBT_STATE_ON_DEFAULT = 1,
36 WBT_STATE_ON_MANUAL = 2,
Zhang Yi1d0903d2021-06-19 17:36:59 +080037 WBT_STATE_OFF_DEFAULT
Jens Axboed62118b2016-11-28 09:40:34 -070038};
39
Jens Axboee34cbd32016-11-09 12:36:15 -070040struct rq_wb {
41 /*
42 * Settings that govern how we throttle
43 */
44 unsigned int wb_background; /* background writeback */
45 unsigned int wb_normal; /* normal writeback */
Jens Axboee34cbd32016-11-09 12:36:15 -070046
Jens Axboed62118b2016-11-28 09:40:34 -070047 short enable_state; /* WBT_STATE_* */
48
Jens Axboee34cbd32016-11-09 12:36:15 -070049 /*
50 * Number of consecutive periods where we don't have enough
51 * information to make a firm scale up/down decision.
52 */
53 unsigned int unknown_cnt;
54
55 u64 win_nsec; /* default window size */
56 u64 cur_win_nsec; /* current window size */
57
Omar Sandoval34dbad52017-03-21 08:56:08 -070058 struct blk_stat_callback *cb;
Jens Axboee34cbd32016-11-09 12:36:15 -070059
Omar Sandoval544ccc8d2018-05-09 02:08:50 -070060 u64 sync_issue;
Jens Axboee34cbd32016-11-09 12:36:15 -070061 void *sync_cookie;
62
63 unsigned int wc;
Jens Axboee34cbd32016-11-09 12:36:15 -070064
65 unsigned long last_issue; /* last non-throttled issue */
66 unsigned long last_comp; /* last non-throttled comp */
67 unsigned long min_lat_nsec;
Josef Bacika7905042018-07-03 09:32:35 -060068 struct rq_qos rqos;
Jens Axboee34cbd32016-11-09 12:36:15 -070069 struct rq_wait rq_wait[WBT_NUM_RWQ];
Josef Bacika7905042018-07-03 09:32:35 -060070 struct rq_depth rq_depth;
Jens Axboee34cbd32016-11-09 12:36:15 -070071};
72
Josef Bacika7905042018-07-03 09:32:35 -060073static inline struct rq_wb *RQWB(struct rq_qos *rqos)
74{
75 return container_of(rqos, struct rq_wb, rqos);
76}
77
Jens Axboee34cbd32016-11-09 12:36:15 -070078static inline unsigned int wbt_inflight(struct rq_wb *rwb)
79{
80 unsigned int i, ret = 0;
81
82 for (i = 0; i < WBT_NUM_RWQ; i++)
83 ret += atomic_read(&rwb->rq_wait[i].inflight);
84
85 return ret;
86}
87
Josef Bacika7905042018-07-03 09:32:35 -060088
Jens Axboee34cbd32016-11-09 12:36:15 -070089#ifdef CONFIG_BLK_WBT
90
Jens Axboe8054b892016-11-10 21:50:51 -070091int wbt_init(struct request_queue *);
Jens Axboefa224ee2016-11-28 09:25:50 -070092void wbt_disable_default(struct request_queue *);
Jan Kara8330cdb2017-04-19 11:33:27 +020093void wbt_enable_default(struct request_queue *);
Jens Axboee34cbd32016-11-09 12:36:15 -070094
Josef Bacika7905042018-07-03 09:32:35 -060095u64 wbt_get_min_lat(struct request_queue *q);
96void wbt_set_min_lat(struct request_queue *q, u64 val);
97
Josef Bacika7905042018-07-03 09:32:35 -060098void wbt_set_write_cache(struct request_queue *, bool);
Jens Axboee34cbd32016-11-09 12:36:15 -070099
Jens Axboe80e091d2016-11-28 09:22:47 -0700100u64 wbt_default_latency_nsec(struct request_queue *);
101
Jens Axboee34cbd32016-11-09 12:36:15 -0700102#else
103
Omar Sandovala8a45942018-05-09 02:08:48 -0700104static inline void wbt_track(struct request *rq, enum wbt_flags flags)
Omar Sandoval934031a2018-05-09 02:08:47 -0700105{
106}
Jens Axboe8054b892016-11-10 21:50:51 -0700107static inline int wbt_init(struct request_queue *q)
Jens Axboee34cbd32016-11-09 12:36:15 -0700108{
109 return -EINVAL;
110}
Jens Axboefa224ee2016-11-28 09:25:50 -0700111static inline void wbt_disable_default(struct request_queue *q)
Jens Axboee34cbd32016-11-09 12:36:15 -0700112{
113}
Jan Kara8330cdb2017-04-19 11:33:27 +0200114static inline void wbt_enable_default(struct request_queue *q)
115{
116}
Josef Bacika7905042018-07-03 09:32:35 -0600117static inline void wbt_set_write_cache(struct request_queue *q, bool wc)
118{
119}
120static inline u64 wbt_get_min_lat(struct request_queue *q)
121{
122 return 0;
123}
124static inline void wbt_set_min_lat(struct request_queue *q, u64 val)
Jens Axboee34cbd32016-11-09 12:36:15 -0700125{
126}
Jens Axboe80e091d2016-11-28 09:22:47 -0700127static inline u64 wbt_default_latency_nsec(struct request_queue *q)
128{
129 return 0;
130}
Jens Axboee34cbd32016-11-09 12:36:15 -0700131
132#endif /* CONFIG_BLK_WBT */
133
134#endif