blob: 0896439a91b0ffd492829e2b1017570be0b1e737 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * pblk-rl.c - pblk's rate limiter for user I/O
16 *
17 */
18
19#include "pblk.h"
20
21static void pblk_rl_kick_u_timer(struct pblk_rl *rl)
22{
23 mod_timer(&rl->u_timer, jiffies + msecs_to_jiffies(5000));
24}
25
Javier González588726d32017-06-26 11:57:29 +020026int pblk_rl_is_limit(struct pblk_rl *rl)
27{
28 int rb_space;
29
30 rb_space = atomic_read(&rl->rb_space);
31
32 return (rb_space == 0);
33}
34
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020035int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries)
36{
37 int rb_user_cnt = atomic_read(&rl->rb_user_cnt);
Javier González588726d32017-06-26 11:57:29 +020038 int rb_space = atomic_read(&rl->rb_space);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020039
Javier González588726d32017-06-26 11:57:29 +020040 if (unlikely(rb_space >= 0) && (rb_space - nr_entries < 0))
41 return NVM_IO_ERR;
42
43 if (rb_user_cnt >= rl->rb_user_max)
44 return NVM_IO_REQUEUE;
45
46 return NVM_IO_OK;
47}
48
49void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries)
50{
51 int rb_space = atomic_read(&rl->rb_space);
52
53 if (unlikely(rb_space >= 0))
54 atomic_sub(nr_entries, &rl->rb_space);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020055}
56
57int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries)
58{
59 int rb_gc_cnt = atomic_read(&rl->rb_gc_cnt);
60 int rb_user_active;
61
62 /* If there is no user I/O let GC take over space on the write buffer */
63 rb_user_active = READ_ONCE(rl->rb_user_active);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020064 return (!(rb_gc_cnt >= rl->rb_gc_max && rb_user_active));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020065}
66
67void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries)
68{
69 atomic_add(nr_entries, &rl->rb_user_cnt);
70
71 /* Release user I/O state. Protect from GC */
72 smp_store_release(&rl->rb_user_active, 1);
73 pblk_rl_kick_u_timer(rl);
74}
75
76void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries)
77{
78 atomic_add(nr_entries, &rl->rb_gc_cnt);
79}
80
81void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc)
82{
83 atomic_sub(nr_user, &rl->rb_user_cnt);
84 atomic_sub(nr_gc, &rl->rb_gc_cnt);
85}
86
87unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl)
88{
89 return atomic_read(&rl->free_blocks);
90}
91
92/*
93 * We check for (i) the number of free blocks in the current LUN and (ii) the
94 * total number of free blocks in the pblk instance. This is to even out the
95 * number of free blocks on each LUN when GC kicks in.
96 *
97 * Only the total number of free blocks is used to configure the rate limiter.
98 */
Rakesh Pandit32825eb2017-10-13 14:46:00 +020099static void pblk_rl_update_rates(struct pblk_rl *rl)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200100{
Rakesh Pandit32825eb2017-10-13 14:46:00 +0200101 struct pblk *pblk = container_of(rl, struct pblk, rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200102 unsigned long free_blocks = pblk_rl_nr_free_blks(rl);
Rakesh Pandit32825eb2017-10-13 14:46:00 +0200103 int max = rl->rb_budget;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104
105 if (free_blocks >= rl->high) {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200106 rl->rb_user_max = max;
107 rl->rb_gc_max = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200108 rl->rb_state = PBLK_RL_HIGH;
109 } else if (free_blocks < rl->high) {
110 int shift = rl->high_pw - rl->rb_windows_pw;
111 int user_windows = free_blocks >> shift;
112 int user_max = user_windows << PBLK_MAX_REQ_ADDRS_PW;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200113
114 rl->rb_user_max = user_max;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200115 rl->rb_gc_max = max - user_max;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200116
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200117 if (free_blocks <= rl->rsv_blocks) {
118 rl->rb_user_max = 0;
119 rl->rb_gc_max = max;
120 }
121
122 /* In the worst case, we will need to GC lines in the low list
123 * (high valid sector count). If there are lines to GC on high
124 * or mid lists, these will be prioritized
125 */
126 rl->rb_state = PBLK_RL_LOW;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200127 }
128
Rakesh Pandit32825eb2017-10-13 14:46:00 +0200129 if (rl->rb_state == (PBLK_RL_MID | PBLK_RL_LOW))
130 pblk_gc_should_start(pblk);
131 else
132 pblk_gc_should_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200133}
134
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200135void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line)
136{
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200137 int blk_in_line = atomic_read(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200138
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200139 atomic_add(blk_in_line, &rl->free_blocks);
Rakesh Pandit32825eb2017-10-13 14:46:00 +0200140 pblk_rl_update_rates(rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200141}
142
143void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line)
144{
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200145 int blk_in_line = atomic_read(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200146
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200147 atomic_sub(blk_in_line, &rl->free_blocks);
Rakesh Pandit32825eb2017-10-13 14:46:00 +0200148 pblk_rl_update_rates(rl);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200149}
150
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200151int pblk_rl_high_thrs(struct pblk_rl *rl)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200152{
153 return rl->high;
154}
155
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200156int pblk_rl_low_thrs(struct pblk_rl *rl)
157{
158 return rl->low;
159}
160
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200161int pblk_rl_sysfs_rate_show(struct pblk_rl *rl)
162{
163 return rl->rb_user_max;
164}
165
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200166int pblk_rl_max_io(struct pblk_rl *rl)
167{
168 return rl->rb_max_io;
169}
170
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200171static void pblk_rl_u_timer(unsigned long data)
172{
173 struct pblk_rl *rl = (struct pblk_rl *)data;
174
175 /* Release user I/O state. Protect from GC */
176 smp_store_release(&rl->rb_user_active, 0);
177}
178
179void pblk_rl_free(struct pblk_rl *rl)
180{
181 del_timer(&rl->u_timer);
182}
183
184void pblk_rl_init(struct pblk_rl *rl, int budget)
185{
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200186 struct pblk *pblk = container_of(rl, struct pblk, rl);
187 struct pblk_line_meta *lm = &pblk->lm;
188 int min_blocks = lm->blk_per_line * PBLK_GC_RSV_LINE;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200189 unsigned int rb_windows;
190
191 rl->high = rl->total_blocks / PBLK_USER_HIGH_THRS;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200192 rl->high_pw = get_count_order(rl->high);
193
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200194 rl->low = rl->total_blocks / PBLK_USER_LOW_THRS;
195 if (rl->low < min_blocks)
196 rl->low = min_blocks;
197
198 rl->rsv_blocks = min_blocks;
199
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200200 /* This will always be a power-of-2 */
201 rb_windows = budget / PBLK_MAX_REQ_ADDRS;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200202 rl->rb_windows_pw = get_count_order(rb_windows);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200203
204 /* To start with, all buffer is available to user I/O writers */
205 rl->rb_budget = budget;
206 rl->rb_user_max = budget;
Javier Gonzálezda67e68f2017-10-13 14:46:05 +0200207 rl->rb_max_io = budget >> 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208 rl->rb_gc_max = 0;
209 rl->rb_state = PBLK_RL_HIGH;
Javier González588726d32017-06-26 11:57:29 +0200210
211 atomic_set(&rl->rb_user_cnt, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212 atomic_set(&rl->rb_gc_cnt, 0);
Javier González588726d32017-06-26 11:57:29 +0200213 atomic_set(&rl->rb_space, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200214
215 setup_timer(&rl->u_timer, pblk_rl_u_timer, (unsigned long)rl);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200216
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200217 rl->rb_user_active = 0;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200218 rl->rb_gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200219}