Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 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" |
Josef Bacik | a790504 | 2018-07-03 09:32:35 -0600 | [diff] [blame] | 12 | #include "blk-rq-qos.h" |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 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 */ |
Jens Axboe | 782f569 | 2018-05-07 10:03:23 -0600 | [diff] [blame] | 18 | WBT_DISCARD = 8, /* discard */ |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 19 | |
Jens Axboe | 782f569 | 2018-05-07 10:03:23 -0600 | [diff] [blame] | 20 | WBT_NR_BITS = 4, /* number of bits */ |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | enum { |
Jens Axboe | 8bea609 | 2018-05-07 09:57:08 -0600 | [diff] [blame] | 24 | WBT_RWQ_BG = 0, |
| 25 | WBT_RWQ_KSWAPD, |
Jens Axboe | 782f569 | 2018-05-07 10:03:23 -0600 | [diff] [blame] | 26 | WBT_RWQ_DISCARD, |
Jens Axboe | 8bea609 | 2018-05-07 09:57:08 -0600 | [diff] [blame] | 27 | WBT_NUM_RWQ, |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Jens Axboe | d62118b | 2016-11-28 09:40:34 -0700 | [diff] [blame] | 30 | /* |
| 31 | * Enable states. Either off, or on by default (done at init time), |
| 32 | * or on through manual setup in sysfs. |
| 33 | */ |
| 34 | enum { |
| 35 | WBT_STATE_ON_DEFAULT = 1, |
| 36 | WBT_STATE_ON_MANUAL = 2, |
Zhang Yi | 1d0903d | 2021-06-19 17:36:59 +0800 | [diff] [blame] | 37 | WBT_STATE_OFF_DEFAULT |
Jens Axboe | d62118b | 2016-11-28 09:40:34 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 40 | struct 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 Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 46 | |
Jens Axboe | d62118b | 2016-11-28 09:40:34 -0700 | [diff] [blame] | 47 | short enable_state; /* WBT_STATE_* */ |
| 48 | |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 49 | /* |
| 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 Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 58 | struct blk_stat_callback *cb; |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 59 | |
Omar Sandoval | 544ccc8d | 2018-05-09 02:08:50 -0700 | [diff] [blame] | 60 | u64 sync_issue; |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 61 | void *sync_cookie; |
| 62 | |
| 63 | unsigned int wc; |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 64 | |
| 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 Bacik | a790504 | 2018-07-03 09:32:35 -0600 | [diff] [blame] | 68 | struct rq_qos rqos; |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 69 | struct rq_wait rq_wait[WBT_NUM_RWQ]; |
Josef Bacik | a790504 | 2018-07-03 09:32:35 -0600 | [diff] [blame] | 70 | struct rq_depth rq_depth; |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Josef Bacik | a790504 | 2018-07-03 09:32:35 -0600 | [diff] [blame] | 73 | static inline struct rq_wb *RQWB(struct rq_qos *rqos) |
| 74 | { |
| 75 | return container_of(rqos, struct rq_wb, rqos); |
| 76 | } |
| 77 | |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 78 | static 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 Bacik | a790504 | 2018-07-03 09:32:35 -0600 | [diff] [blame] | 88 | |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 89 | #ifdef CONFIG_BLK_WBT |
| 90 | |
Jens Axboe | 8054b89 | 2016-11-10 21:50:51 -0700 | [diff] [blame] | 91 | int wbt_init(struct request_queue *); |
Jens Axboe | fa224ee | 2016-11-28 09:25:50 -0700 | [diff] [blame] | 92 | void wbt_disable_default(struct request_queue *); |
Jan Kara | 8330cdb | 2017-04-19 11:33:27 +0200 | [diff] [blame] | 93 | void wbt_enable_default(struct request_queue *); |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 94 | |
Josef Bacik | a790504 | 2018-07-03 09:32:35 -0600 | [diff] [blame] | 95 | u64 wbt_get_min_lat(struct request_queue *q); |
| 96 | void wbt_set_min_lat(struct request_queue *q, u64 val); |
| 97 | |
Josef Bacik | a790504 | 2018-07-03 09:32:35 -0600 | [diff] [blame] | 98 | void wbt_set_write_cache(struct request_queue *, bool); |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 99 | |
Jens Axboe | 80e091d | 2016-11-28 09:22:47 -0700 | [diff] [blame] | 100 | u64 wbt_default_latency_nsec(struct request_queue *); |
| 101 | |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 102 | #else |
| 103 | |
Omar Sandoval | a8a4594 | 2018-05-09 02:08:48 -0700 | [diff] [blame] | 104 | static inline void wbt_track(struct request *rq, enum wbt_flags flags) |
Omar Sandoval | 934031a | 2018-05-09 02:08:47 -0700 | [diff] [blame] | 105 | { |
| 106 | } |
Jens Axboe | 8054b89 | 2016-11-10 21:50:51 -0700 | [diff] [blame] | 107 | static inline int wbt_init(struct request_queue *q) |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 108 | { |
| 109 | return -EINVAL; |
| 110 | } |
Jens Axboe | fa224ee | 2016-11-28 09:25:50 -0700 | [diff] [blame] | 111 | static inline void wbt_disable_default(struct request_queue *q) |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 112 | { |
| 113 | } |
Jan Kara | 8330cdb | 2017-04-19 11:33:27 +0200 | [diff] [blame] | 114 | static inline void wbt_enable_default(struct request_queue *q) |
| 115 | { |
| 116 | } |
Josef Bacik | a790504 | 2018-07-03 09:32:35 -0600 | [diff] [blame] | 117 | static inline void wbt_set_write_cache(struct request_queue *q, bool wc) |
| 118 | { |
| 119 | } |
| 120 | static inline u64 wbt_get_min_lat(struct request_queue *q) |
| 121 | { |
| 122 | return 0; |
| 123 | } |
| 124 | static inline void wbt_set_min_lat(struct request_queue *q, u64 val) |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 125 | { |
| 126 | } |
Jens Axboe | 80e091d | 2016-11-28 09:22:47 -0700 | [diff] [blame] | 127 | static inline u64 wbt_default_latency_nsec(struct request_queue *q) |
| 128 | { |
| 129 | return 0; |
| 130 | } |
Jens Axboe | e34cbd3 | 2016-11-09 12:36:15 -0700 | [diff] [blame] | 131 | |
| 132 | #endif /* CONFIG_BLK_WBT */ |
| 133 | |
| 134 | #endif |