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_user_may_insert(struct pblk_rl *rl, int nr_entries)
28 int rb_user_cnt = atomic_read(&rl->rb_user_cnt);
30 return (!(rb_user_cnt + nr_entries > rl->rb_user_max));
33 int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries)
35 int rb_gc_cnt = atomic_read(&rl->rb_gc_cnt);
38 /* If there is no user I/O let GC take over space on the write buffer */
39 rb_user_active = READ_ONCE(rl->rb_user_active);
40 return (!(rb_gc_cnt + nr_entries > rl->rb_gc_max && rb_user_active));
43 void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries)
45 atomic_add(nr_entries, &rl->rb_user_cnt);
47 /* Release user I/O state. Protect from GC */
48 smp_store_release(&rl->rb_user_active, 1);
49 pblk_rl_kick_u_timer(rl);
52 void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries)
54 atomic_add(nr_entries, &rl->rb_gc_cnt);
57 void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc)
59 atomic_sub(nr_user, &rl->rb_user_cnt);
60 atomic_sub(nr_gc, &rl->rb_gc_cnt);
63 unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl)
65 return atomic_read(&rl->free_blocks);
69 * We check for (i) the number of free blocks in the current LUN and (ii) the
70 * total number of free blocks in the pblk instance. This is to even out the
71 * number of free blocks on each LUN when GC kicks in.
73 * Only the total number of free blocks is used to configure the rate limiter.
75 static int pblk_rl_update_rates(struct pblk_rl *rl, unsigned long max)
77 unsigned long free_blocks = pblk_rl_nr_free_blks(rl);
79 if (free_blocks >= rl->high) {
80 rl->rb_user_max = max - rl->rb_gc_rsv;
81 rl->rb_gc_max = rl->rb_gc_rsv;
82 rl->rb_state = PBLK_RL_HIGH;
83 } else if (free_blocks < rl->high) {
84 int shift = rl->high_pw - rl->rb_windows_pw;
85 int user_windows = free_blocks >> shift;
86 int user_max = user_windows << PBLK_MAX_REQ_ADDRS_PW;
89 rl->rb_user_max = user_max;
90 gc_max = max - rl->rb_user_max;
91 rl->rb_gc_max = max(gc_max, rl->rb_gc_rsv);
93 if (free_blocks > rl->low)
94 rl->rb_state = PBLK_RL_MID;
96 rl->rb_state = PBLK_RL_LOW;
102 void pblk_rl_set_gc_rsc(struct pblk_rl *rl, int rsv)
104 rl->rb_gc_rsv = rl->rb_gc_max = rsv;
107 void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line)
109 struct pblk *pblk = container_of(rl, struct pblk, rl);
110 int blk_in_line = atomic_read(&line->blk_in_line);
113 atomic_add(blk_in_line, &rl->free_blocks);
114 /* Rates will not change that often - no need to lock update */
115 ret = pblk_rl_update_rates(rl, rl->rb_budget);
117 if (ret == (PBLK_RL_MID | PBLK_RL_LOW))
118 pblk_gc_should_start(pblk);
120 pblk_gc_should_stop(pblk);
123 void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line)
125 struct pblk *pblk = container_of(rl, struct pblk, rl);
126 int blk_in_line = atomic_read(&line->blk_in_line);
129 atomic_sub(blk_in_line, &rl->free_blocks);
131 /* Rates will not change that often - no need to lock update */
132 ret = pblk_rl_update_rates(rl, rl->rb_budget);
133 if (ret == (PBLK_RL_MID | PBLK_RL_LOW))
134 pblk_gc_should_start(pblk);
136 pblk_gc_should_stop(pblk);
139 int pblk_rl_gc_thrs(struct pblk_rl *rl)
144 int pblk_rl_sysfs_rate_show(struct pblk_rl *rl)
146 return rl->rb_user_max;
149 static void pblk_rl_u_timer(unsigned long data)
151 struct pblk_rl *rl = (struct pblk_rl *)data;
153 /* Release user I/O state. Protect from GC */
154 smp_store_release(&rl->rb_user_active, 0);
157 void pblk_rl_free(struct pblk_rl *rl)
159 del_timer(&rl->u_timer);
162 void pblk_rl_init(struct pblk_rl *rl, int budget)
164 unsigned int rb_windows;
166 rl->high = rl->total_blocks / PBLK_USER_HIGH_THRS;
167 rl->low = rl->total_blocks / PBLK_USER_LOW_THRS;
168 rl->high_pw = get_count_order(rl->high);
170 /* This will always be a power-of-2 */
171 rb_windows = budget / PBLK_MAX_REQ_ADDRS;
172 rl->rb_windows_pw = get_count_order(rb_windows) + 1;
174 /* To start with, all buffer is available to user I/O writers */
175 rl->rb_budget = budget;
176 rl->rb_user_max = budget;
177 atomic_set(&rl->rb_user_cnt, 0);
179 rl->rb_state = PBLK_RL_HIGH;
180 atomic_set(&rl->rb_gc_cnt, 0);
182 setup_timer(&rl->u_timer, pblk_rl_u_timer, (unsigned long)rl);
183 rl->rb_user_active = 0;