diff options
author | Jiri Pirko <jiri@mellanox.com> | 2017-02-03 04:29:06 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2017-02-03 16:35:42 -0500 |
commit | 44091d29f2075972aede47ef17e1e70db3d51190 (patch) | |
tree | df943cac9dd4a634ae3432340b274076180ea420 | |
parent | b862815c3ee7b49ec20a9ab25da55a5f0bcbb95e (diff) |
lib: Introduce priority array area manager
This introduces a infrastructure for management of linear priority
areas. Priority order in an array matters, however order of items inside
a priority group does not matter.
As an initial implementation, L-sort algorithm is used. It is quite
trivial. More advanced algorithm called P-sort will be introduced as a
follow-up. The infrastructure is prepared for other algos.
Alongside this, a testing module is introduced as well.
Signed-off-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r-- | MAINTAINERS | 8 | ||||
-rw-r--r-- | include/linux/parman.h | 76 | ||||
-rw-r--r-- | lib/Kconfig | 3 | ||||
-rw-r--r-- | lib/Kconfig.debug | 10 | ||||
-rw-r--r-- | lib/Makefile | 3 | ||||
-rw-r--r-- | lib/parman.c | 376 | ||||
-rw-r--r-- | lib/test_parman.c | 395 |
7 files changed, 871 insertions, 0 deletions
diff --git a/MAINTAINERS b/MAINTAINERS index c15dc53f9d66..a9368bba9b37 100644 --- a/MAINTAINERS +++ b/MAINTAINERS | |||
@@ -9382,6 +9382,14 @@ F: drivers/video/fbdev/sti* | |||
9382 | F: drivers/video/console/sti* | 9382 | F: drivers/video/console/sti* |
9383 | F: drivers/video/logo/logo_parisc* | 9383 | F: drivers/video/logo/logo_parisc* |
9384 | 9384 | ||
9385 | PARMAN | ||
9386 | M: Jiri Pirko <jiri@mellanox.com> | ||
9387 | L: netdev@vger.kernel.org | ||
9388 | S: Supported | ||
9389 | F: lib/parman.c | ||
9390 | F: lib/test_parman.c | ||
9391 | F: include/linux/parman.h | ||
9392 | |||
9385 | PC87360 HARDWARE MONITORING DRIVER | 9393 | PC87360 HARDWARE MONITORING DRIVER |
9386 | M: Jim Cromie <jim.cromie@gmail.com> | 9394 | M: Jim Cromie <jim.cromie@gmail.com> |
9387 | L: linux-hwmon@vger.kernel.org | 9395 | L: linux-hwmon@vger.kernel.org |
diff --git a/include/linux/parman.h b/include/linux/parman.h new file mode 100644 index 000000000000..3c8cccc7d4da --- /dev/null +++ b/include/linux/parman.h | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | * include/linux/parman.h - Manager for linear priority array areas | ||
3 | * Copyright (c) 2017 Mellanox Technologies. All rights reserved. | ||
4 | * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com> | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions are met: | ||
8 | * | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the names of the copyright holders nor the names of its | ||
15 | * contributors may be used to endorse or promote products derived from | ||
16 | * this software without specific prior written permission. | ||
17 | * | ||
18 | * Alternatively, this software may be distributed under the terms of the | ||
19 | * GNU General Public License ("GPL") version 2 as published by the Free | ||
20 | * Software Foundation. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
32 | * POSSIBILITY OF SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | #ifndef _PARMAN_H | ||
36 | #define _PARMAN_H | ||
37 | |||
38 | #include <linux/list.h> | ||
39 | |||
40 | enum parman_algo_type { | ||
41 | PARMAN_ALGO_TYPE_LSORT, | ||
42 | }; | ||
43 | |||
44 | struct parman_item { | ||
45 | struct list_head list; | ||
46 | unsigned long index; | ||
47 | }; | ||
48 | |||
49 | struct parman_prio { | ||
50 | struct list_head list; | ||
51 | struct list_head item_list; | ||
52 | unsigned long priority; | ||
53 | }; | ||
54 | |||
55 | struct parman_ops { | ||
56 | unsigned long base_count; | ||
57 | unsigned long resize_step; | ||
58 | int (*resize)(void *priv, unsigned long new_count); | ||
59 | void (*move)(void *priv, unsigned long from_index, | ||
60 | unsigned long to_index, unsigned long count); | ||
61 | enum parman_algo_type algo; | ||
62 | }; | ||
63 | |||
64 | struct parman; | ||
65 | |||
66 | struct parman *parman_create(const struct parman_ops *ops, void *priv); | ||
67 | void parman_destroy(struct parman *parman); | ||
68 | void parman_prio_init(struct parman *parman, struct parman_prio *prio, | ||
69 | unsigned long priority); | ||
70 | void parman_prio_fini(struct parman_prio *prio); | ||
71 | int parman_item_add(struct parman *parman, struct parman_prio *prio, | ||
72 | struct parman_item *item); | ||
73 | void parman_item_remove(struct parman *parman, struct parman_prio *prio, | ||
74 | struct parman_item *item); | ||
75 | |||
76 | #endif | ||
diff --git a/lib/Kconfig b/lib/Kconfig index 260a80e313b9..5d644f180fe5 100644 --- a/lib/Kconfig +++ b/lib/Kconfig | |||
@@ -550,4 +550,7 @@ config STACKDEPOT | |||
550 | config SBITMAP | 550 | config SBITMAP |
551 | bool | 551 | bool |
552 | 552 | ||
553 | config PARMAN | ||
554 | tristate "parman" | ||
555 | |||
553 | endmenu | 556 | endmenu |
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 15969abf00a6..433a788500be 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug | |||
@@ -1826,6 +1826,16 @@ config TEST_HASH | |||
1826 | This is intended to help people writing architecture-specific | 1826 | This is intended to help people writing architecture-specific |
1827 | optimized versions. If unsure, say N. | 1827 | optimized versions. If unsure, say N. |
1828 | 1828 | ||
1829 | config TEST_PARMAN | ||
1830 | tristate "Perform selftest on priority array manager" | ||
1831 | default n | ||
1832 | depends on PARMAN | ||
1833 | help | ||
1834 | Enable this option to test priority array manager on boot | ||
1835 | (or module load). | ||
1836 | |||
1837 | If unsure, say N. | ||
1838 | |||
1829 | endmenu # runtime tests | 1839 | endmenu # runtime tests |
1830 | 1840 | ||
1831 | config PROVIDE_OHCI1394_DMA_INIT | 1841 | config PROVIDE_OHCI1394_DMA_INIT |
diff --git a/lib/Makefile b/lib/Makefile index 7b3008d58600..1c039a4f60e5 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -56,6 +56,7 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o | |||
56 | obj-$(CONFIG_TEST_PRINTF) += test_printf.o | 56 | obj-$(CONFIG_TEST_PRINTF) += test_printf.o |
57 | obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o | 57 | obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o |
58 | obj-$(CONFIG_TEST_UUID) += test_uuid.o | 58 | obj-$(CONFIG_TEST_UUID) += test_uuid.o |
59 | obj-$(CONFIG_TEST_PARMAN) += test_parman.o | ||
59 | 60 | ||
60 | ifeq ($(CONFIG_DEBUG_KOBJECT),y) | 61 | ifeq ($(CONFIG_DEBUG_KOBJECT),y) |
61 | CFLAGS_kobject.o += -DDEBUG | 62 | CFLAGS_kobject.o += -DDEBUG |
@@ -230,3 +231,5 @@ obj-$(CONFIG_UBSAN) += ubsan.o | |||
230 | UBSAN_SANITIZE_ubsan.o := n | 231 | UBSAN_SANITIZE_ubsan.o := n |
231 | 232 | ||
232 | obj-$(CONFIG_SBITMAP) += sbitmap.o | 233 | obj-$(CONFIG_SBITMAP) += sbitmap.o |
234 | |||
235 | obj-$(CONFIG_PARMAN) += parman.o | ||
diff --git a/lib/parman.c b/lib/parman.c new file mode 100644 index 000000000000..c6e42a8db824 --- /dev/null +++ b/lib/parman.c | |||
@@ -0,0 +1,376 @@ | |||
1 | /* | ||
2 | * lib/parman.c - Manager for linear priority array areas | ||
3 | * Copyright (c) 2017 Mellanox Technologies. All rights reserved. | ||
4 | * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com> | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions are met: | ||
8 | * | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the names of the copyright holders nor the names of its | ||
15 | * contributors may be used to endorse or promote products derived from | ||
16 | * this software without specific prior written permission. | ||
17 | * | ||
18 | * Alternatively, this software may be distributed under the terms of the | ||
19 | * GNU General Public License ("GPL") version 2 as published by the Free | ||
20 | * Software Foundation. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
32 | * POSSIBILITY OF SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | #include <linux/kernel.h> | ||
36 | #include <linux/module.h> | ||
37 | #include <linux/slab.h> | ||
38 | #include <linux/export.h> | ||
39 | #include <linux/list.h> | ||
40 | #include <linux/err.h> | ||
41 | #include <linux/parman.h> | ||
42 | |||
43 | struct parman_algo { | ||
44 | int (*item_add)(struct parman *parman, struct parman_prio *prio, | ||
45 | struct parman_item *item); | ||
46 | void (*item_remove)(struct parman *parman, struct parman_prio *prio, | ||
47 | struct parman_item *item); | ||
48 | }; | ||
49 | |||
50 | struct parman { | ||
51 | const struct parman_ops *ops; | ||
52 | void *priv; | ||
53 | const struct parman_algo *algo; | ||
54 | unsigned long count; | ||
55 | unsigned long limit_count; | ||
56 | struct list_head prio_list; | ||
57 | }; | ||
58 | |||
59 | static int parman_enlarge(struct parman *parman) | ||
60 | { | ||
61 | unsigned long new_count = parman->limit_count + | ||
62 | parman->ops->resize_step; | ||
63 | int err; | ||
64 | |||
65 | err = parman->ops->resize(parman->priv, new_count); | ||
66 | if (err) | ||
67 | return err; | ||
68 | parman->limit_count = new_count; | ||
69 | return 0; | ||
70 | } | ||
71 | |||
72 | static int parman_shrink(struct parman *parman) | ||
73 | { | ||
74 | unsigned long new_count = parman->limit_count - | ||
75 | parman->ops->resize_step; | ||
76 | int err; | ||
77 | |||
78 | if (new_count < parman->ops->base_count) | ||
79 | return 0; | ||
80 | err = parman->ops->resize(parman->priv, new_count); | ||
81 | if (err) | ||
82 | return err; | ||
83 | parman->limit_count = new_count; | ||
84 | return 0; | ||
85 | } | ||
86 | |||
87 | static bool parman_prio_used(struct parman_prio *prio) | ||
88 | |||
89 | { | ||
90 | return !list_empty(&prio->item_list); | ||
91 | } | ||
92 | |||
93 | static struct parman_item *parman_prio_first_item(struct parman_prio *prio) | ||
94 | { | ||
95 | return list_first_entry(&prio->item_list, | ||
96 | typeof(struct parman_item), list); | ||
97 | } | ||
98 | |||
99 | static unsigned long parman_prio_first_index(struct parman_prio *prio) | ||
100 | { | ||
101 | return parman_prio_first_item(prio)->index; | ||
102 | } | ||
103 | |||
104 | static struct parman_item *parman_prio_last_item(struct parman_prio *prio) | ||
105 | { | ||
106 | return list_last_entry(&prio->item_list, | ||
107 | typeof(struct parman_item), list); | ||
108 | } | ||
109 | |||
110 | static unsigned long parman_prio_last_index(struct parman_prio *prio) | ||
111 | { | ||
112 | return parman_prio_last_item(prio)->index; | ||
113 | } | ||
114 | |||
115 | static unsigned long parman_lsort_new_index_find(struct parman *parman, | ||
116 | struct parman_prio *prio) | ||
117 | { | ||
118 | list_for_each_entry_from_reverse(prio, &parman->prio_list, list) { | ||
119 | if (!parman_prio_used(prio)) | ||
120 | continue; | ||
121 | return parman_prio_last_index(prio) + 1; | ||
122 | } | ||
123 | return 0; | ||
124 | } | ||
125 | |||
126 | static void __parman_prio_move(struct parman *parman, struct parman_prio *prio, | ||
127 | struct parman_item *item, unsigned long to_index, | ||
128 | unsigned long count) | ||
129 | { | ||
130 | parman->ops->move(parman->priv, item->index, to_index, count); | ||
131 | } | ||
132 | |||
133 | static void parman_prio_shift_down(struct parman *parman, | ||
134 | struct parman_prio *prio) | ||
135 | { | ||
136 | struct parman_item *item; | ||
137 | unsigned long to_index; | ||
138 | |||
139 | if (!parman_prio_used(prio)) | ||
140 | return; | ||
141 | item = parman_prio_first_item(prio); | ||
142 | to_index = parman_prio_last_index(prio) + 1; | ||
143 | __parman_prio_move(parman, prio, item, to_index, 1); | ||
144 | list_move_tail(&item->list, &prio->item_list); | ||
145 | item->index = to_index; | ||
146 | } | ||
147 | |||
148 | static void parman_prio_shift_up(struct parman *parman, | ||
149 | struct parman_prio *prio) | ||
150 | { | ||
151 | struct parman_item *item; | ||
152 | unsigned long to_index; | ||
153 | |||
154 | if (!parman_prio_used(prio)) | ||
155 | return; | ||
156 | item = parman_prio_last_item(prio); | ||
157 | to_index = parman_prio_first_index(prio) - 1; | ||
158 | __parman_prio_move(parman, prio, item, to_index, 1); | ||
159 | list_move(&item->list, &prio->item_list); | ||
160 | item->index = to_index; | ||
161 | } | ||
162 | |||
163 | static void parman_prio_item_remove(struct parman *parman, | ||
164 | struct parman_prio *prio, | ||
165 | struct parman_item *item) | ||
166 | { | ||
167 | struct parman_item *last_item; | ||
168 | unsigned long to_index; | ||
169 | |||
170 | last_item = parman_prio_last_item(prio); | ||
171 | if (last_item == item) { | ||
172 | list_del(&item->list); | ||
173 | return; | ||
174 | } | ||
175 | to_index = item->index; | ||
176 | __parman_prio_move(parman, prio, last_item, to_index, 1); | ||
177 | list_del(&last_item->list); | ||
178 | list_replace(&item->list, &last_item->list); | ||
179 | last_item->index = to_index; | ||
180 | } | ||
181 | |||
182 | static int parman_lsort_item_add(struct parman *parman, | ||
183 | struct parman_prio *prio, | ||
184 | struct parman_item *item) | ||
185 | { | ||
186 | struct parman_prio *prio2; | ||
187 | unsigned long new_index; | ||
188 | int err; | ||
189 | |||
190 | if (parman->count + 1 > parman->limit_count) { | ||
191 | err = parman_enlarge(parman); | ||
192 | if (err) | ||
193 | return err; | ||
194 | } | ||
195 | |||
196 | new_index = parman_lsort_new_index_find(parman, prio); | ||
197 | list_for_each_entry_reverse(prio2, &parman->prio_list, list) { | ||
198 | if (prio2 == prio) | ||
199 | break; | ||
200 | parman_prio_shift_down(parman, prio2); | ||
201 | } | ||
202 | item->index = new_index; | ||
203 | list_add_tail(&item->list, &prio->item_list); | ||
204 | parman->count++; | ||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | static void parman_lsort_item_remove(struct parman *parman, | ||
209 | struct parman_prio *prio, | ||
210 | struct parman_item *item) | ||
211 | { | ||
212 | parman_prio_item_remove(parman, prio, item); | ||
213 | list_for_each_entry_continue(prio, &parman->prio_list, list) | ||
214 | parman_prio_shift_up(parman, prio); | ||
215 | parman->count--; | ||
216 | if (parman->limit_count - parman->count >= parman->ops->resize_step) | ||
217 | parman_shrink(parman); | ||
218 | } | ||
219 | |||
220 | static const struct parman_algo parman_lsort = { | ||
221 | .item_add = parman_lsort_item_add, | ||
222 | .item_remove = parman_lsort_item_remove, | ||
223 | }; | ||
224 | |||
225 | static const struct parman_algo *parman_algos[] = { | ||
226 | &parman_lsort, | ||
227 | }; | ||
228 | |||
229 | /** | ||
230 | * parman_create - creates a new parman instance | ||
231 | * @ops: caller-specific callbacks | ||
232 | * @priv: pointer to a private data passed to the ops | ||
233 | * | ||
234 | * Note: all locking must be provided by the caller. | ||
235 | * | ||
236 | * Each parman instance manages an array area with chunks of entries | ||
237 | * with the same priority. Consider following example: | ||
238 | * | ||
239 | * item 1 with prio 10 | ||
240 | * item 2 with prio 10 | ||
241 | * item 3 with prio 10 | ||
242 | * item 4 with prio 20 | ||
243 | * item 5 with prio 20 | ||
244 | * item 6 with prio 30 | ||
245 | * item 7 with prio 30 | ||
246 | * item 8 with prio 30 | ||
247 | * | ||
248 | * In this example, there are 3 priority chunks. The order of the priorities | ||
249 | * matters, however the order of items within a single priority chunk does not | ||
250 | * matter. So the same array could be ordered as follows: | ||
251 | * | ||
252 | * item 2 with prio 10 | ||
253 | * item 3 with prio 10 | ||
254 | * item 1 with prio 10 | ||
255 | * item 5 with prio 20 | ||
256 | * item 4 with prio 20 | ||
257 | * item 7 with prio 30 | ||
258 | * item 8 with prio 30 | ||
259 | * item 6 with prio 30 | ||
260 | * | ||
261 | * The goal of parman is to maintain the priority ordering. The caller | ||
262 | * provides @ops with callbacks parman uses to move the items | ||
263 | * and resize the array area. | ||
264 | * | ||
265 | * Returns a pointer to newly created parman instance in case of success, | ||
266 | * otherwise it returns NULL. | ||
267 | */ | ||
268 | struct parman *parman_create(const struct parman_ops *ops, void *priv) | ||
269 | { | ||
270 | struct parman *parman; | ||
271 | |||
272 | parman = kzalloc(sizeof(*parman), GFP_KERNEL); | ||
273 | if (!parman) | ||
274 | return NULL; | ||
275 | INIT_LIST_HEAD(&parman->prio_list); | ||
276 | parman->ops = ops; | ||
277 | parman->priv = priv; | ||
278 | parman->limit_count = ops->base_count; | ||
279 | parman->algo = parman_algos[ops->algo]; | ||
280 | return parman; | ||
281 | } | ||
282 | EXPORT_SYMBOL(parman_create); | ||
283 | |||
284 | /** | ||
285 | * parman_destroy - destroys existing parman instance | ||
286 | * @parman: parman instance | ||
287 | * | ||
288 | * Note: all locking must be provided by the caller. | ||
289 | */ | ||
290 | void parman_destroy(struct parman *parman) | ||
291 | { | ||
292 | WARN_ON(!list_empty(&parman->prio_list)); | ||
293 | kfree(parman); | ||
294 | } | ||
295 | EXPORT_SYMBOL(parman_destroy); | ||
296 | |||
297 | /** | ||
298 | * parman_prio_init - initializes a parman priority chunk | ||
299 | * @parman: parman instance | ||
300 | * @prio: parman prio structure to be initialized | ||
301 | * @prority: desired priority of the chunk | ||
302 | * | ||
303 | * Note: all locking must be provided by the caller. | ||
304 | * | ||
305 | * Before caller could add an item with certain priority, he has to | ||
306 | * initialize a priority chunk for it using this function. | ||
307 | */ | ||
308 | void parman_prio_init(struct parman *parman, struct parman_prio *prio, | ||
309 | unsigned long priority) | ||
310 | { | ||
311 | struct parman_prio *prio2; | ||
312 | struct list_head *pos; | ||
313 | |||
314 | INIT_LIST_HEAD(&prio->item_list); | ||
315 | prio->priority = priority; | ||
316 | |||
317 | /* Position inside the list according to priority */ | ||
318 | list_for_each(pos, &parman->prio_list) { | ||
319 | prio2 = list_entry(pos, typeof(*prio2), list); | ||
320 | if (prio2->priority > prio->priority) | ||
321 | break; | ||
322 | } | ||
323 | list_add_tail(&prio->list, pos); | ||
324 | } | ||
325 | EXPORT_SYMBOL(parman_prio_init); | ||
326 | |||
327 | /** | ||
328 | * parman_prio_fini - finalizes use of parman priority chunk | ||
329 | * @prio: parman prio structure | ||
330 | * | ||
331 | * Note: all locking must be provided by the caller. | ||
332 | */ | ||
333 | void parman_prio_fini(struct parman_prio *prio) | ||
334 | { | ||
335 | WARN_ON(parman_prio_used(prio)); | ||
336 | list_del(&prio->list); | ||
337 | } | ||
338 | EXPORT_SYMBOL(parman_prio_fini); | ||
339 | |||
340 | /** | ||
341 | * parman_item_add - adds a parman item under defined priority | ||
342 | * @parman: parman instance | ||
343 | * @prio: parman prio instance to add the item to | ||
344 | * @item: parman item instance | ||
345 | * | ||
346 | * Note: all locking must be provided by the caller. | ||
347 | * | ||
348 | * Adds item to a array managed by parman instance under the specified priority. | ||
349 | * | ||
350 | * Returns 0 in case of success, negative number to indicate an error. | ||
351 | */ | ||
352 | int parman_item_add(struct parman *parman, struct parman_prio *prio, | ||
353 | struct parman_item *item) | ||
354 | { | ||
355 | return parman->algo->item_add(parman, prio, item); | ||
356 | } | ||
357 | EXPORT_SYMBOL(parman_item_add); | ||
358 | |||
359 | /** | ||
360 | * parman_item_del - deletes parman item | ||
361 | * @parman: parman instance | ||
362 | * @prio: parman prio instance to delete the item from | ||
363 | * @item: parman item instance | ||
364 | * | ||
365 | * Note: all locking must be provided by the caller. | ||
366 | */ | ||
367 | void parman_item_remove(struct parman *parman, struct parman_prio *prio, | ||
368 | struct parman_item *item) | ||
369 | { | ||
370 | parman->algo->item_remove(parman, prio, item); | ||
371 | } | ||
372 | EXPORT_SYMBOL(parman_item_remove); | ||
373 | |||
374 | MODULE_LICENSE("Dual BSD/GPL"); | ||
375 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); | ||
376 | MODULE_DESCRIPTION("Priority-based array manager"); | ||
diff --git a/lib/test_parman.c b/lib/test_parman.c new file mode 100644 index 000000000000..fe9f3a785804 --- /dev/null +++ b/lib/test_parman.c | |||
@@ -0,0 +1,395 @@ | |||
1 | /* | ||
2 | * lib/test_parman.c - Test module for parman | ||
3 | * Copyright (c) 2017 Mellanox Technologies. All rights reserved. | ||
4 | * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com> | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions are met: | ||
8 | * | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the names of the copyright holders nor the names of its | ||
15 | * contributors may be used to endorse or promote products derived from | ||
16 | * this software without specific prior written permission. | ||
17 | * | ||
18 | * Alternatively, this software may be distributed under the terms of the | ||
19 | * GNU General Public License ("GPL") version 2 as published by the Free | ||
20 | * Software Foundation. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
32 | * POSSIBILITY OF SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
36 | |||
37 | #include <linux/kernel.h> | ||
38 | #include <linux/module.h> | ||
39 | #include <linux/slab.h> | ||
40 | #include <linux/bitops.h> | ||
41 | #include <linux/err.h> | ||
42 | #include <linux/random.h> | ||
43 | #include <linux/parman.h> | ||
44 | |||
45 | #define TEST_PARMAN_PRIO_SHIFT 7 /* defines number of prios for testing */ | ||
46 | #define TEST_PARMAN_PRIO_COUNT BIT(TEST_PARMAN_PRIO_SHIFT) | ||
47 | #define TEST_PARMAN_PRIO_MASK (TEST_PARMAN_PRIO_COUNT - 1) | ||
48 | |||
49 | #define TEST_PARMAN_ITEM_SHIFT 13 /* defines a total number | ||
50 | * of items for testing | ||
51 | */ | ||
52 | #define TEST_PARMAN_ITEM_COUNT BIT(TEST_PARMAN_ITEM_SHIFT) | ||
53 | #define TEST_PARMAN_ITEM_MASK (TEST_PARMAN_ITEM_COUNT - 1) | ||
54 | |||
55 | #define TEST_PARMAN_BASE_SHIFT 8 | ||
56 | #define TEST_PARMAN_BASE_COUNT BIT(TEST_PARMAN_BASE_SHIFT) | ||
57 | #define TEST_PARMAN_RESIZE_STEP_SHIFT 7 | ||
58 | #define TEST_PARMAN_RESIZE_STEP_COUNT BIT(TEST_PARMAN_RESIZE_STEP_SHIFT) | ||
59 | |||
60 | #define TEST_PARMAN_BULK_MAX_SHIFT (2 + TEST_PARMAN_RESIZE_STEP_SHIFT) | ||
61 | #define TEST_PARMAN_BULK_MAX_COUNT BIT(TEST_PARMAN_BULK_MAX_SHIFT) | ||
62 | #define TEST_PARMAN_BULK_MAX_MASK (TEST_PARMAN_BULK_MAX_COUNT - 1) | ||
63 | |||
64 | #define TEST_PARMAN_RUN_BUDGET (TEST_PARMAN_ITEM_COUNT * 256) | ||
65 | |||
66 | struct test_parman_prio { | ||
67 | struct parman_prio parman_prio; | ||
68 | unsigned long priority; | ||
69 | }; | ||
70 | |||
71 | struct test_parman_item { | ||
72 | struct parman_item parman_item; | ||
73 | struct test_parman_prio *prio; | ||
74 | bool used; | ||
75 | }; | ||
76 | |||
77 | struct test_parman { | ||
78 | struct parman *parman; | ||
79 | struct test_parman_item **prio_array; | ||
80 | unsigned long prio_array_limit; | ||
81 | struct test_parman_prio prios[TEST_PARMAN_PRIO_COUNT]; | ||
82 | struct test_parman_item items[TEST_PARMAN_ITEM_COUNT]; | ||
83 | struct rnd_state rnd; | ||
84 | unsigned long run_budget; | ||
85 | unsigned long bulk_budget; | ||
86 | bool bulk_noop; | ||
87 | unsigned int used_items; | ||
88 | }; | ||
89 | |||
90 | #define ITEM_PTRS_SIZE(count) (sizeof(struct test_parman_item *) * (count)) | ||
91 | |||
92 | static int test_parman_resize(void *priv, unsigned long new_count) | ||
93 | { | ||
94 | struct test_parman *test_parman = priv; | ||
95 | struct test_parman_item **prio_array; | ||
96 | unsigned long old_count; | ||
97 | |||
98 | prio_array = krealloc(test_parman->prio_array, | ||
99 | ITEM_PTRS_SIZE(new_count), GFP_KERNEL); | ||
100 | if (new_count == 0) | ||
101 | return 0; | ||
102 | if (!prio_array) | ||
103 | return -ENOMEM; | ||
104 | old_count = test_parman->prio_array_limit; | ||
105 | if (new_count > old_count) | ||
106 | memset(&prio_array[old_count], 0, | ||
107 | ITEM_PTRS_SIZE(new_count - old_count)); | ||
108 | test_parman->prio_array = prio_array; | ||
109 | test_parman->prio_array_limit = new_count; | ||
110 | return 0; | ||
111 | } | ||
112 | |||
113 | static void test_parman_move(void *priv, unsigned long from_index, | ||
114 | unsigned long to_index, unsigned long count) | ||
115 | { | ||
116 | struct test_parman *test_parman = priv; | ||
117 | struct test_parman_item **prio_array = test_parman->prio_array; | ||
118 | |||
119 | memmove(&prio_array[to_index], &prio_array[from_index], | ||
120 | ITEM_PTRS_SIZE(count)); | ||
121 | memset(&prio_array[from_index], 0, ITEM_PTRS_SIZE(count)); | ||
122 | } | ||
123 | |||
124 | static const struct parman_ops test_parman_lsort_ops = { | ||
125 | .base_count = TEST_PARMAN_BASE_COUNT, | ||
126 | .resize_step = TEST_PARMAN_RESIZE_STEP_COUNT, | ||
127 | .resize = test_parman_resize, | ||
128 | .move = test_parman_move, | ||
129 | .algo = PARMAN_ALGO_TYPE_LSORT, | ||
130 | }; | ||
131 | |||
132 | static void test_parman_rnd_init(struct test_parman *test_parman) | ||
133 | { | ||
134 | prandom_seed_state(&test_parman->rnd, 3141592653589793238ULL); | ||
135 | } | ||
136 | |||
137 | static u32 test_parman_rnd_get(struct test_parman *test_parman) | ||
138 | { | ||
139 | return prandom_u32_state(&test_parman->rnd); | ||
140 | } | ||
141 | |||
142 | static unsigned long test_parman_priority_gen(struct test_parman *test_parman) | ||
143 | { | ||
144 | unsigned long priority; | ||
145 | int i; | ||
146 | |||
147 | again: | ||
148 | priority = test_parman_rnd_get(test_parman); | ||
149 | if (priority == 0) | ||
150 | goto again; | ||
151 | |||
152 | for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) { | ||
153 | struct test_parman_prio *prio = &test_parman->prios[i]; | ||
154 | |||
155 | if (prio->priority == 0) | ||
156 | break; | ||
157 | if (prio->priority == priority) | ||
158 | goto again; | ||
159 | } | ||
160 | return priority; | ||
161 | } | ||
162 | |||
163 | static void test_parman_prios_init(struct test_parman *test_parman) | ||
164 | { | ||
165 | int i; | ||
166 | |||
167 | for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) { | ||
168 | struct test_parman_prio *prio = &test_parman->prios[i]; | ||
169 | |||
170 | /* Assign random uniqueue priority to each prio structure */ | ||
171 | prio->priority = test_parman_priority_gen(test_parman); | ||
172 | parman_prio_init(test_parman->parman, &prio->parman_prio, | ||
173 | prio->priority); | ||
174 | } | ||
175 | } | ||
176 | |||
177 | static void test_parman_prios_fini(struct test_parman *test_parman) | ||
178 | { | ||
179 | int i; | ||
180 | |||
181 | for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) { | ||
182 | struct test_parman_prio *prio = &test_parman->prios[i]; | ||
183 | |||
184 | parman_prio_fini(&prio->parman_prio); | ||
185 | } | ||
186 | } | ||
187 | |||
188 | static void test_parman_items_init(struct test_parman *test_parman) | ||
189 | { | ||
190 | int i; | ||
191 | |||
192 | for (i = 0; i < TEST_PARMAN_ITEM_COUNT; i++) { | ||
193 | struct test_parman_item *item = &test_parman->items[i]; | ||
194 | unsigned int prio_index = test_parman_rnd_get(test_parman) & | ||
195 | TEST_PARMAN_PRIO_MASK; | ||
196 | |||
197 | /* Assign random prio to each item structure */ | ||
198 | item->prio = &test_parman->prios[prio_index]; | ||
199 | } | ||
200 | } | ||
201 | |||
202 | static void test_parman_items_fini(struct test_parman *test_parman) | ||
203 | { | ||
204 | int i; | ||
205 | |||
206 | for (i = 0; i < TEST_PARMAN_ITEM_COUNT; i++) { | ||
207 | struct test_parman_item *item = &test_parman->items[i]; | ||
208 | |||
209 | if (!item->used) | ||
210 | continue; | ||
211 | parman_item_remove(test_parman->parman, | ||
212 | &item->prio->parman_prio, | ||
213 | &item->parman_item); | ||
214 | } | ||
215 | } | ||
216 | |||
217 | static struct test_parman *test_parman_create(const struct parman_ops *ops) | ||
218 | { | ||
219 | struct test_parman *test_parman; | ||
220 | int err; | ||
221 | |||
222 | test_parman = kzalloc(sizeof(*test_parman), GFP_KERNEL); | ||
223 | if (!test_parman) | ||
224 | return ERR_PTR(-ENOMEM); | ||
225 | err = test_parman_resize(test_parman, TEST_PARMAN_BASE_COUNT); | ||
226 | if (err) | ||
227 | goto err_resize; | ||
228 | test_parman->parman = parman_create(ops, test_parman); | ||
229 | if (!test_parman->parman) { | ||
230 | err = -ENOMEM; | ||
231 | goto err_parman_create; | ||
232 | } | ||
233 | test_parman_rnd_init(test_parman); | ||
234 | test_parman_prios_init(test_parman); | ||
235 | test_parman_items_init(test_parman); | ||
236 | test_parman->run_budget = TEST_PARMAN_RUN_BUDGET; | ||
237 | return test_parman; | ||
238 | |||
239 | err_parman_create: | ||
240 | test_parman_resize(test_parman, 0); | ||
241 | err_resize: | ||
242 | kfree(test_parman); | ||
243 | return ERR_PTR(err); | ||
244 | } | ||
245 | |||
246 | static void test_parman_destroy(struct test_parman *test_parman) | ||
247 | { | ||
248 | test_parman_items_fini(test_parman); | ||
249 | test_parman_prios_fini(test_parman); | ||
250 | parman_destroy(test_parman->parman); | ||
251 | test_parman_resize(test_parman, 0); | ||
252 | kfree(test_parman); | ||
253 | } | ||
254 | |||
255 | static bool test_parman_run_check_budgets(struct test_parman *test_parman) | ||
256 | { | ||
257 | if (test_parman->run_budget-- == 0) | ||
258 | return false; | ||
259 | if (test_parman->bulk_budget-- != 0) | ||
260 | return true; | ||
261 | |||
262 | test_parman->bulk_budget = test_parman_rnd_get(test_parman) & | ||
263 | TEST_PARMAN_BULK_MAX_MASK; | ||
264 | test_parman->bulk_noop = test_parman_rnd_get(test_parman) & 1; | ||
265 | return true; | ||
266 | } | ||
267 | |||
268 | static int test_parman_run(struct test_parman *test_parman) | ||
269 | { | ||
270 | unsigned int i = test_parman_rnd_get(test_parman); | ||
271 | int err; | ||
272 | |||
273 | while (test_parman_run_check_budgets(test_parman)) { | ||
274 | unsigned int item_index = i++ & TEST_PARMAN_ITEM_MASK; | ||
275 | struct test_parman_item *item = &test_parman->items[item_index]; | ||
276 | |||
277 | if (test_parman->bulk_noop) | ||
278 | continue; | ||
279 | |||
280 | if (!item->used) { | ||
281 | err = parman_item_add(test_parman->parman, | ||
282 | &item->prio->parman_prio, | ||
283 | &item->parman_item); | ||
284 | if (err) | ||
285 | return err; | ||
286 | test_parman->prio_array[item->parman_item.index] = item; | ||
287 | test_parman->used_items++; | ||
288 | } else { | ||
289 | test_parman->prio_array[item->parman_item.index] = NULL; | ||
290 | parman_item_remove(test_parman->parman, | ||
291 | &item->prio->parman_prio, | ||
292 | &item->parman_item); | ||
293 | test_parman->used_items--; | ||
294 | } | ||
295 | item->used = !item->used; | ||
296 | } | ||
297 | return 0; | ||
298 | } | ||
299 | |||
300 | static int test_parman_check_array(struct test_parman *test_parman, | ||
301 | bool gaps_allowed) | ||
302 | { | ||
303 | unsigned int last_unused_items = 0; | ||
304 | unsigned long last_priority = 0; | ||
305 | unsigned int used_items = 0; | ||
306 | int i; | ||
307 | |||
308 | if (test_parman->prio_array_limit < TEST_PARMAN_BASE_COUNT) { | ||
309 | pr_err("Array limit is lower than the base count (%lu < %lu)\n", | ||
310 | test_parman->prio_array_limit, TEST_PARMAN_BASE_COUNT); | ||
311 | return -EINVAL; | ||
312 | } | ||
313 | |||
314 | for (i = 0; i < test_parman->prio_array_limit; i++) { | ||
315 | struct test_parman_item *item = test_parman->prio_array[i]; | ||
316 | |||
317 | if (!item) { | ||
318 | last_unused_items++; | ||
319 | continue; | ||
320 | } | ||
321 | if (last_unused_items && !gaps_allowed) { | ||
322 | pr_err("Gap found in array even though they are forbidden\n"); | ||
323 | return -EINVAL; | ||
324 | } | ||
325 | |||
326 | last_unused_items = 0; | ||
327 | used_items++; | ||
328 | |||
329 | if (item->prio->priority < last_priority) { | ||
330 | pr_err("Item belongs under higher priority then the last one (current: %lu, previous: %lu)\n", | ||
331 | item->prio->priority, last_priority); | ||
332 | return -EINVAL; | ||
333 | } | ||
334 | last_priority = item->prio->priority; | ||
335 | |||
336 | if (item->parman_item.index != i) { | ||
337 | pr_err("Item has different index in compare to where it actualy is (%lu != %d)\n", | ||
338 | item->parman_item.index, i); | ||
339 | return -EINVAL; | ||
340 | } | ||
341 | } | ||
342 | |||
343 | if (used_items != test_parman->used_items) { | ||
344 | pr_err("Number of used items in array does not match (%u != %u)\n", | ||
345 | used_items, test_parman->used_items); | ||
346 | return -EINVAL; | ||
347 | } | ||
348 | |||
349 | if (last_unused_items >= TEST_PARMAN_RESIZE_STEP_COUNT) { | ||
350 | pr_err("Number of unused item at the end of array is bigger than resize step (%u >= %lu)\n", | ||
351 | last_unused_items, TEST_PARMAN_RESIZE_STEP_COUNT); | ||
352 | return -EINVAL; | ||
353 | } | ||
354 | |||
355 | pr_info("Priority array check successful\n"); | ||
356 | |||
357 | return 0; | ||
358 | } | ||
359 | |||
360 | static int test_parman_lsort(void) | ||
361 | { | ||
362 | struct test_parman *test_parman; | ||
363 | int err; | ||
364 | |||
365 | test_parman = test_parman_create(&test_parman_lsort_ops); | ||
366 | if (IS_ERR(test_parman)) | ||
367 | return PTR_ERR(test_parman); | ||
368 | |||
369 | err = test_parman_run(test_parman); | ||
370 | if (err) | ||
371 | goto out; | ||
372 | |||
373 | err = test_parman_check_array(test_parman, false); | ||
374 | if (err) | ||
375 | goto out; | ||
376 | out: | ||
377 | test_parman_destroy(test_parman); | ||
378 | return err; | ||
379 | } | ||
380 | |||
381 | static int __init test_parman_init(void) | ||
382 | { | ||
383 | return test_parman_lsort(); | ||
384 | } | ||
385 | |||
386 | static void __exit test_parman_exit(void) | ||
387 | { | ||
388 | } | ||
389 | |||
390 | module_init(test_parman_init); | ||
391 | module_exit(test_parman_exit); | ||
392 | |||
393 | MODULE_LICENSE("Dual BSD/GPL"); | ||
394 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); | ||
395 | MODULE_DESCRIPTION("Test module for parman"); | ||