Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
e34cbd30 JA |
2 | #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" | |
a7905043 | 12 | #include "blk-rq-qos.h" |
e34cbd30 JA |
13 | |
14 | enum wbt_flags { | |
15 | WBT_TRACKED = 1, /* write, tracked for throttling */ | |
16 | WBT_READ = 2, /* read */ | |
17 | WBT_KSWAPD = 4, /* write, from kswapd */ | |
782f5697 | 18 | WBT_DISCARD = 8, /* discard */ |
e34cbd30 | 19 | |
782f5697 | 20 | WBT_NR_BITS = 4, /* number of bits */ |
e34cbd30 JA |
21 | }; |
22 | ||
23 | enum { | |
8bea6090 JA |
24 | WBT_RWQ_BG = 0, |
25 | WBT_RWQ_KSWAPD, | |
782f5697 | 26 | WBT_RWQ_DISCARD, |
8bea6090 | 27 | WBT_NUM_RWQ, |
e34cbd30 JA |
28 | }; |
29 | ||
d62118b6 | 30 | /* |
a9a236d2 YK |
31 | * If current state is WBT_STATE_ON/OFF_DEFAULT, it can be covered to any other |
32 | * state, if current state is WBT_STATE_ON/OFF_MANUAL, it can only be covered | |
33 | * to WBT_STATE_OFF/ON_MANUAL. | |
d62118b6 JA |
34 | */ |
35 | enum { | |
a9a236d2 YK |
36 | WBT_STATE_ON_DEFAULT = 1, /* on by default */ |
37 | WBT_STATE_ON_MANUAL = 2, /* on manually by sysfs */ | |
38 | WBT_STATE_OFF_DEFAULT = 3, /* off by default */ | |
39 | WBT_STATE_OFF_MANUAL = 4, /* off manually by sysfs */ | |
d62118b6 JA |
40 | }; |
41 | ||
e34cbd30 JA |
42 | struct rq_wb { |
43 | /* | |
44 | * Settings that govern how we throttle | |
45 | */ | |
46 | unsigned int wb_background; /* background writeback */ | |
47 | unsigned int wb_normal; /* normal writeback */ | |
e34cbd30 | 48 | |
d62118b6 JA |
49 | short enable_state; /* WBT_STATE_* */ |
50 | ||
e34cbd30 JA |
51 | /* |
52 | * Number of consecutive periods where we don't have enough | |
53 | * information to make a firm scale up/down decision. | |
54 | */ | |
55 | unsigned int unknown_cnt; | |
56 | ||
57 | u64 win_nsec; /* default window size */ | |
58 | u64 cur_win_nsec; /* current window size */ | |
59 | ||
34dbad5d | 60 | struct blk_stat_callback *cb; |
e34cbd30 | 61 | |
544ccc8d | 62 | u64 sync_issue; |
e34cbd30 JA |
63 | void *sync_cookie; |
64 | ||
65 | unsigned int wc; | |
e34cbd30 JA |
66 | |
67 | unsigned long last_issue; /* last non-throttled issue */ | |
68 | unsigned long last_comp; /* last non-throttled comp */ | |
69 | unsigned long min_lat_nsec; | |
a7905043 | 70 | struct rq_qos rqos; |
e34cbd30 | 71 | struct rq_wait rq_wait[WBT_NUM_RWQ]; |
a7905043 | 72 | struct rq_depth rq_depth; |
e34cbd30 JA |
73 | }; |
74 | ||
a7905043 JB |
75 | static inline struct rq_wb *RQWB(struct rq_qos *rqos) |
76 | { | |
77 | return container_of(rqos, struct rq_wb, rqos); | |
78 | } | |
79 | ||
e34cbd30 JA |
80 | static inline unsigned int wbt_inflight(struct rq_wb *rwb) |
81 | { | |
82 | unsigned int i, ret = 0; | |
83 | ||
84 | for (i = 0; i < WBT_NUM_RWQ; i++) | |
85 | ret += atomic_read(&rwb->rq_wait[i].inflight); | |
86 | ||
87 | return ret; | |
88 | } | |
89 | ||
a7905043 | 90 | |
e34cbd30 JA |
91 | #ifdef CONFIG_BLK_WBT |
92 | ||
8054b89f | 93 | int wbt_init(struct request_queue *); |
fa224eed | 94 | void wbt_disable_default(struct request_queue *); |
8330cdb0 | 95 | void wbt_enable_default(struct request_queue *); |
e34cbd30 | 96 | |
a7905043 JB |
97 | u64 wbt_get_min_lat(struct request_queue *q); |
98 | void wbt_set_min_lat(struct request_queue *q, u64 val); | |
3642ef4d | 99 | bool wbt_disabled(struct request_queue *); |
a7905043 | 100 | |
a7905043 | 101 | void wbt_set_write_cache(struct request_queue *, bool); |
e34cbd30 | 102 | |
80e091d1 JA |
103 | u64 wbt_default_latency_nsec(struct request_queue *); |
104 | ||
e34cbd30 JA |
105 | #else |
106 | ||
8054b89f | 107 | static inline int wbt_init(struct request_queue *q) |
e34cbd30 JA |
108 | { |
109 | return -EINVAL; | |
110 | } | |
a7905043 | 111 | static inline void wbt_disable_default(struct request_queue *q) |
e34cbd30 JA |
112 | { |
113 | } | |
a7905043 | 114 | static inline void wbt_enable_default(struct request_queue *q) |
e34cbd30 JA |
115 | { |
116 | } | |
a7905043 | 117 | static inline void wbt_set_write_cache(struct request_queue *q, bool wc) |
8330cdb0 JK |
118 | { |
119 | } | |
a7905043 | 120 | static inline u64 wbt_get_min_lat(struct request_queue *q) |
e34cbd30 | 121 | { |
a7905043 | 122 | return 0; |
e34cbd30 | 123 | } |
a7905043 | 124 | static inline void wbt_set_min_lat(struct request_queue *q, u64 val) |
e34cbd30 JA |
125 | { |
126 | } | |
80e091d1 JA |
127 | static inline u64 wbt_default_latency_nsec(struct request_queue *q) |
128 | { | |
129 | return 0; | |
130 | } | |
3642ef4d YK |
131 | static inline bool wbt_disabled(struct request_queue *q) |
132 | { | |
133 | return true; | |
134 | } | |
e34cbd30 JA |
135 | |
136 | #endif /* CONFIG_BLK_WBT */ | |
137 | ||
138 | #endif |