2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
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.
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.
15 * pblk-rl.c - pblk's rate limiter for user I/O
21 static void pblk_rl_kick_u_timer(struct pblk_rl
*rl
)
23 mod_timer(&rl
->u_timer
, jiffies
+ msecs_to_jiffies(5000));
26 int pblk_rl_is_limit(struct pblk_rl
*rl
)
30 rb_space
= atomic_read(&rl
->rb_space
);
32 return (rb_space
== 0);
35 int pblk_rl_user_may_insert(struct pblk_rl
*rl
, int nr_entries
)
37 int rb_user_cnt
= atomic_read(&rl
->rb_user_cnt
);
38 int rb_space
= atomic_read(&rl
->rb_space
);
40 if (unlikely(rb_space
>= 0) && (rb_space
- nr_entries
< 0))
43 if (rb_user_cnt
>= rl
->rb_user_max
)
44 return NVM_IO_REQUEUE
;
49 void pblk_rl_inserted(struct pblk_rl
*rl
, int nr_entries
)
51 int rb_space
= atomic_read(&rl
->rb_space
);
53 if (unlikely(rb_space
>= 0))
54 atomic_sub(nr_entries
, &rl
->rb_space
);
57 int pblk_rl_gc_may_insert(struct pblk_rl
*rl
, int nr_entries
)
59 int rb_gc_cnt
= atomic_read(&rl
->rb_gc_cnt
);
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
);
64 return (!(rb_gc_cnt
>= rl
->rb_gc_max
&& rb_user_active
));
67 void pblk_rl_user_in(struct pblk_rl
*rl
, int nr_entries
)
69 atomic_add(nr_entries
, &rl
->rb_user_cnt
);
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
);
76 void pblk_rl_gc_in(struct pblk_rl
*rl
, int nr_entries
)
78 atomic_add(nr_entries
, &rl
->rb_gc_cnt
);
81 void pblk_rl_out(struct pblk_rl
*rl
, int nr_user
, int nr_gc
)
83 atomic_sub(nr_user
, &rl
->rb_user_cnt
);
84 atomic_sub(nr_gc
, &rl
->rb_gc_cnt
);
87 unsigned long pblk_rl_nr_free_blks(struct pblk_rl
*rl
)
89 return atomic_read(&rl
->free_blocks
);
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.
97 * Only the total number of free blocks is used to configure the rate limiter.
99 static int pblk_rl_update_rates(struct pblk_rl
*rl
, unsigned long max
)
101 unsigned long free_blocks
= pblk_rl_nr_free_blks(rl
);
103 if (free_blocks
>= rl
->high
) {
104 rl
->rb_user_max
= max
;
106 rl
->rb_state
= PBLK_RL_HIGH
;
107 } else if (free_blocks
< rl
->high
) {
108 int shift
= rl
->high_pw
- rl
->rb_windows_pw
;
109 int user_windows
= free_blocks
>> shift
;
110 int user_max
= user_windows
<< PBLK_MAX_REQ_ADDRS_PW
;
112 rl
->rb_user_max
= user_max
;
113 rl
->rb_gc_max
= max
- user_max
;
115 if (free_blocks
<= rl
->rsv_blocks
) {
120 /* In the worst case, we will need to GC lines in the low list
121 * (high valid sector count). If there are lines to GC on high
122 * or mid lists, these will be prioritized
124 rl
->rb_state
= PBLK_RL_LOW
;
130 void pblk_rl_free_lines_inc(struct pblk_rl
*rl
, struct pblk_line
*line
)
132 struct pblk
*pblk
= container_of(rl
, struct pblk
, rl
);
133 int blk_in_line
= atomic_read(&line
->blk_in_line
);
136 atomic_add(blk_in_line
, &rl
->free_blocks
);
137 /* Rates will not change that often - no need to lock update */
138 ret
= pblk_rl_update_rates(rl
, rl
->rb_budget
);
140 if (ret
== (PBLK_RL_MID
| PBLK_RL_LOW
))
141 pblk_gc_should_start(pblk
);
143 pblk_gc_should_stop(pblk
);
146 void pblk_rl_free_lines_dec(struct pblk_rl
*rl
, struct pblk_line
*line
)
148 int blk_in_line
= atomic_read(&line
->blk_in_line
);
150 atomic_sub(blk_in_line
, &rl
->free_blocks
);
153 void pblk_gc_should_kick(struct pblk
*pblk
)
155 struct pblk_rl
*rl
= &pblk
->rl
;
158 /* Rates will not change that often - no need to lock update */
159 ret
= pblk_rl_update_rates(rl
, rl
->rb_budget
);
160 if (ret
== (PBLK_RL_MID
| PBLK_RL_LOW
))
161 pblk_gc_should_start(pblk
);
163 pblk_gc_should_stop(pblk
);
166 int pblk_rl_high_thrs(struct pblk_rl
*rl
)
171 int pblk_rl_low_thrs(struct pblk_rl
*rl
)
176 int pblk_rl_sysfs_rate_show(struct pblk_rl
*rl
)
178 return rl
->rb_user_max
;
181 static void pblk_rl_u_timer(unsigned long data
)
183 struct pblk_rl
*rl
= (struct pblk_rl
*)data
;
185 /* Release user I/O state. Protect from GC */
186 smp_store_release(&rl
->rb_user_active
, 0);
189 void pblk_rl_free(struct pblk_rl
*rl
)
191 del_timer(&rl
->u_timer
);
194 void pblk_rl_init(struct pblk_rl
*rl
, int budget
)
196 struct pblk
*pblk
= container_of(rl
, struct pblk
, rl
);
197 struct pblk_line_meta
*lm
= &pblk
->lm
;
198 int min_blocks
= lm
->blk_per_line
* PBLK_GC_RSV_LINE
;
199 unsigned int rb_windows
;
201 rl
->high
= rl
->total_blocks
/ PBLK_USER_HIGH_THRS
;
202 rl
->high_pw
= get_count_order(rl
->high
);
204 rl
->low
= rl
->total_blocks
/ PBLK_USER_LOW_THRS
;
205 if (rl
->low
< min_blocks
)
206 rl
->low
= min_blocks
;
208 rl
->rsv_blocks
= min_blocks
;
210 /* This will always be a power-of-2 */
211 rb_windows
= budget
/ PBLK_MAX_REQ_ADDRS
;
212 rl
->rb_windows_pw
= get_count_order(rb_windows
);
214 /* To start with, all buffer is available to user I/O writers */
215 rl
->rb_budget
= budget
;
216 rl
->rb_user_max
= budget
;
218 rl
->rb_state
= PBLK_RL_HIGH
;
220 atomic_set(&rl
->rb_user_cnt
, 0);
221 atomic_set(&rl
->rb_gc_cnt
, 0);
222 atomic_set(&rl
->rb_space
, -1);
224 setup_timer(&rl
->u_timer
, pblk_rl_u_timer
, (unsigned long)rl
);
226 rl
->rb_user_active
= 0;
227 rl
->rb_gc_active
= 0;