]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/lightnvm/pblk-rl.c
lightnvm: pblk: set mempool and workqueue params.
[mirror_ubuntu-artful-kernel.git] / drivers / lightnvm / pblk-rl.c
CommitLineData
a4bd217b
JG
1/*
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
26int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries)
27{
28 int rb_user_cnt = atomic_read(&rl->rb_user_cnt);
29
b20ba1bc 30 return (!(rb_user_cnt >= rl->rb_user_max));
a4bd217b
JG
31}
32
33int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries)
34{
35 int rb_gc_cnt = atomic_read(&rl->rb_gc_cnt);
36 int rb_user_active;
37
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);
b20ba1bc 40 return (!(rb_gc_cnt >= rl->rb_gc_max && rb_user_active));
a4bd217b
JG
41}
42
43void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries)
44{
45 atomic_add(nr_entries, &rl->rb_user_cnt);
46
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);
50}
51
52void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries)
53{
54 atomic_add(nr_entries, &rl->rb_gc_cnt);
55}
56
57void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc)
58{
59 atomic_sub(nr_user, &rl->rb_user_cnt);
60 atomic_sub(nr_gc, &rl->rb_gc_cnt);
61}
62
63unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl)
64{
65 return atomic_read(&rl->free_blocks);
66}
67
68/*
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.
72 *
73 * Only the total number of free blocks is used to configure the rate limiter.
74 */
75static int pblk_rl_update_rates(struct pblk_rl *rl, unsigned long max)
76{
77 unsigned long free_blocks = pblk_rl_nr_free_blks(rl);
78
79 if (free_blocks >= rl->high) {
b20ba1bc
JG
80 rl->rb_user_max = max;
81 rl->rb_gc_max = 0;
a4bd217b
JG
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;
a4bd217b
JG
87
88 rl->rb_user_max = user_max;
b20ba1bc
JG
89 rl->rb_gc_max = max - user_max;
90
91 if (free_blocks <= rl->rsv_blocks) {
92 rl->rb_user_max = 0;
93 rl->rb_gc_max = max;
94 }
95
96 /* In the worst case, we will need to GC lines in the low list
97 * (high valid sector count). If there are lines to GC on high
98 * or mid lists, these will be prioritized
99 */
100 rl->rb_state = PBLK_RL_LOW;
a4bd217b
JG
101 }
102
103 return rl->rb_state;
104}
105
a4bd217b
JG
106void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line)
107{
108 struct pblk *pblk = container_of(rl, struct pblk, rl);
a44f53fa 109 int blk_in_line = atomic_read(&line->blk_in_line);
a4bd217b
JG
110 int ret;
111
a44f53fa 112 atomic_add(blk_in_line, &rl->free_blocks);
a4bd217b
JG
113 /* Rates will not change that often - no need to lock update */
114 ret = pblk_rl_update_rates(rl, rl->rb_budget);
115
116 if (ret == (PBLK_RL_MID | PBLK_RL_LOW))
117 pblk_gc_should_start(pblk);
118 else
119 pblk_gc_should_stop(pblk);
120}
121
122void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line)
123{
a44f53fa 124 int blk_in_line = atomic_read(&line->blk_in_line);
a4bd217b 125
a44f53fa 126 atomic_sub(blk_in_line, &rl->free_blocks);
b20ba1bc
JG
127}
128
129void pblk_gc_should_kick(struct pblk *pblk)
130{
131 struct pblk_rl *rl = &pblk->rl;
132 int ret;
a4bd217b
JG
133
134 /* Rates will not change that often - no need to lock update */
135 ret = pblk_rl_update_rates(rl, rl->rb_budget);
136 if (ret == (PBLK_RL_MID | PBLK_RL_LOW))
137 pblk_gc_should_start(pblk);
138 else
139 pblk_gc_should_stop(pblk);
140}
141
b20ba1bc 142int pblk_rl_high_thrs(struct pblk_rl *rl)
a4bd217b
JG
143{
144 return rl->high;
145}
146
b20ba1bc
JG
147int pblk_rl_low_thrs(struct pblk_rl *rl)
148{
149 return rl->low;
150}
151
a4bd217b
JG
152int pblk_rl_sysfs_rate_show(struct pblk_rl *rl)
153{
154 return rl->rb_user_max;
155}
156
157static void pblk_rl_u_timer(unsigned long data)
158{
159 struct pblk_rl *rl = (struct pblk_rl *)data;
160
161 /* Release user I/O state. Protect from GC */
162 smp_store_release(&rl->rb_user_active, 0);
163}
164
165void pblk_rl_free(struct pblk_rl *rl)
166{
167 del_timer(&rl->u_timer);
168}
169
170void pblk_rl_init(struct pblk_rl *rl, int budget)
171{
b20ba1bc
JG
172 struct pblk *pblk = container_of(rl, struct pblk, rl);
173 struct pblk_line_meta *lm = &pblk->lm;
174 int min_blocks = lm->blk_per_line * PBLK_GC_RSV_LINE;
a4bd217b
JG
175 unsigned int rb_windows;
176
177 rl->high = rl->total_blocks / PBLK_USER_HIGH_THRS;
a4bd217b
JG
178 rl->high_pw = get_count_order(rl->high);
179
b20ba1bc
JG
180 rl->low = rl->total_blocks / PBLK_USER_LOW_THRS;
181 if (rl->low < min_blocks)
182 rl->low = min_blocks;
183
184 rl->rsv_blocks = min_blocks;
185
a4bd217b
JG
186 /* This will always be a power-of-2 */
187 rb_windows = budget / PBLK_MAX_REQ_ADDRS;
b20ba1bc 188 rl->rb_windows_pw = get_count_order(rb_windows);
a4bd217b
JG
189
190 /* To start with, all buffer is available to user I/O writers */
191 rl->rb_budget = budget;
192 rl->rb_user_max = budget;
193 atomic_set(&rl->rb_user_cnt, 0);
194 rl->rb_gc_max = 0;
195 rl->rb_state = PBLK_RL_HIGH;
196 atomic_set(&rl->rb_gc_cnt, 0);
197
198 setup_timer(&rl->u_timer, pblk_rl_u_timer, (unsigned long)rl);
b20ba1bc 199
a4bd217b 200 rl->rb_user_active = 0;
b20ba1bc 201 rl->rb_gc_active = 0;
a4bd217b 202}