ce73198ff5d88db43b6ffcaaf92569ee7e3a7954
[cascardo/ovs.git] / datapath / linux / compat / flex_array.c
1 #include <linux/version.h>
2
3 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
4
5 /*
6  * Flexible array managed in PAGE_SIZE parts
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  * Copyright IBM Corporation, 2009
23  *
24  * Author: Dave Hansen <dave@linux.vnet.ibm.com>
25  */
26
27 #include <linux/flex_array.h>
28 #include <linux/slab.h>
29 #include <linux/stddef.h>
30 #include <linux/module.h>
31 #include <linux/reciprocal_div.h>
32
33 struct flex_array_part {
34         char elements[FLEX_ARRAY_PART_SIZE];
35 };
36
37 /*
38  * If a user requests an allocation which is small
39  * enough, we may simply use the space in the
40  * flex_array->parts[] array to store the user
41  * data.
42  */
43 static inline int elements_fit_in_base(struct flex_array *fa)
44 {
45         int data_size = fa->element_size * fa->total_nr_elements;
46         if (data_size <= FLEX_ARRAY_BASE_BYTES_LEFT)
47                 return 1;
48         return 0;
49 }
50
51 /**
52  * flex_array_alloc - allocate a new flexible array
53  * @element_size:       the size of individual elements in the array
54  * @total:              total number of elements that this should hold
55  * @flags:              page allocation flags to use for base array
56  *
57  * Note: all locking must be provided by the caller.
58  *
59  * @total is used to size internal structures.  If the user ever
60  * accesses any array indexes >=@total, it will produce errors.
61  *
62  * The maximum number of elements is defined as: the number of
63  * elements that can be stored in a page times the number of
64  * page pointers that we can fit in the base structure or (using
65  * integer math):
66  *
67  *      (PAGE_SIZE/element_size) * (PAGE_SIZE-8)/sizeof(void *)
68  *
69  * Here's a table showing example capacities.  Note that the maximum
70  * index that the get/put() functions is just nr_objects-1.   This
71  * basically means that you get 4MB of storage on 32-bit and 2MB on
72  * 64-bit.
73  *
74  *
75  * Element size | Objects | Objects |
76  * PAGE_SIZE=4k |  32-bit |  64-bit |
77  * ---------------------------------|
78  *      1 bytes | 4177920 | 2088960 |
79  *      2 bytes | 2088960 | 1044480 |
80  *      3 bytes | 1392300 |  696150 |
81  *      4 bytes | 1044480 |  522240 |
82  *     32 bytes |  130560 |   65408 |
83  *     33 bytes |  126480 |   63240 |
84  *   2048 bytes |    2040 |    1020 |
85  *   2049 bytes |    1020 |     510 |
86  *       void * | 1044480 |  261120 |
87  *
88  * Since 64-bit pointers are twice the size, we lose half the
89  * capacity in the base structure.  Also note that no effort is made
90  * to efficiently pack objects across page boundaries.
91  */
92 struct flex_array *rpl_flex_array_alloc(int element_size, unsigned int total,
93                                         gfp_t flags)
94 {
95         struct flex_array *ret;
96         struct reciprocal_value reciprocal_elems = {0};
97         int elems_per_part = 0;
98         int max_size = 0;
99
100         if (element_size) {
101                 elems_per_part = FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
102                 reciprocal_elems = reciprocal_value(elems_per_part);
103                 max_size = FLEX_ARRAY_NR_BASE_PTRS * elems_per_part;
104         }
105
106         /* max_size will end up 0 if element_size > PAGE_SIZE */
107         if (total > max_size)
108                 return NULL;
109         ret = kzalloc(sizeof(struct flex_array), flags);
110         if (!ret)
111                 return NULL;
112         ret->element_size = element_size;
113         ret->total_nr_elements = total;
114         ret->elems_per_part = elems_per_part;
115         ret->reciprocal_elems = reciprocal_elems;
116         if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO))
117                 memset(&ret->parts[0], FLEX_ARRAY_FREE,
118                                                 FLEX_ARRAY_BASE_BYTES_LEFT);
119         return ret;
120 }
121 EXPORT_SYMBOL_GPL(rpl_flex_array_alloc);
122
123 static int fa_element_to_part_nr(struct flex_array *fa,
124                                         unsigned int element_nr)
125 {
126         return reciprocal_divide(element_nr, fa->reciprocal_elems);
127 }
128
129 /**
130  * flex_array_free_parts - just free the second-level pages
131  * @fa:         the flex array from which to free parts
132  *
133  * This is to be used in cases where the base 'struct flex_array'
134  * has been statically allocated and should not be free.
135  */
136 void rpl_flex_array_free_parts(struct flex_array *fa)
137 {
138         int part_nr;
139
140         if (elements_fit_in_base(fa))
141                 return;
142         for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
143                 kfree(fa->parts[part_nr]);
144 }
145 EXPORT_SYMBOL_GPL(rpl_flex_array_free_parts);
146
147 void rpl_flex_array_free(struct flex_array *fa)
148 {
149         flex_array_free_parts(fa);
150         kfree(fa);
151 }
152 EXPORT_SYMBOL_GPL(rpl_flex_array_free);
153
154 static unsigned int index_inside_part(struct flex_array *fa,
155                                         unsigned int element_nr,
156                                         unsigned int part_nr)
157 {
158         unsigned int part_offset;
159
160         part_offset = element_nr - part_nr * fa->elems_per_part;
161         return part_offset * fa->element_size;
162 }
163
164 static struct flex_array_part *
165 __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
166 {
167         struct flex_array_part *part = fa->parts[part_nr];
168         if (!part) {
169                 part = kmalloc(sizeof(struct flex_array_part), flags);
170                 if (!part)
171                         return NULL;
172                 if (!(flags & __GFP_ZERO))
173                         memset(part, FLEX_ARRAY_FREE,
174                                 sizeof(struct flex_array_part));
175                 fa->parts[part_nr] = part;
176         }
177         return part;
178 }
179
180 /**
181  * flex_array_put - copy data into the array at @element_nr
182  * @fa:         the flex array to copy data into
183  * @element_nr: index of the position in which to insert
184  *              the new element.
185  * @src:        address of data to copy into the array
186  * @flags:      page allocation flags to use for array expansion
187  *
188  *
189  * Note that this *copies* the contents of @src into
190  * the array.  If you are trying to store an array of
191  * pointers, make sure to pass in &ptr instead of ptr.
192  * You may instead wish to use the flex_array_put_ptr()
193  * helper function.
194  *
195  * Locking must be provided by the caller.
196  */
197 int rpl_flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
198                         gfp_t flags)
199 {
200         int part_nr = 0;
201         struct flex_array_part *part;
202         void *dst;
203
204         if (element_nr >= fa->total_nr_elements)
205                 return -ENOSPC;
206         if (!fa->element_size)
207                 return 0;
208         if (elements_fit_in_base(fa))
209                 part = (struct flex_array_part *)&fa->parts[0];
210         else {
211                 part_nr = fa_element_to_part_nr(fa, element_nr);
212                 part = __fa_get_part(fa, part_nr, flags);
213                 if (!part)
214                         return -ENOMEM;
215         }
216         dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
217         memcpy(dst, src, fa->element_size);
218         return 0;
219 }
220 EXPORT_SYMBOL_GPL(rpl_flex_array_put);
221
222 /**
223  * flex_array_clear - clear element in array at @element_nr
224  * @fa:         the flex array of the element.
225  * @element_nr: index of the position to clear.
226  *
227  * Locking must be provided by the caller.
228  */
229 int rpl_flex_array_clear(struct flex_array *fa, unsigned int element_nr)
230 {
231         int part_nr = 0;
232         struct flex_array_part *part;
233         void *dst;
234
235         if (element_nr >= fa->total_nr_elements)
236                 return -ENOSPC;
237         if (!fa->element_size)
238                 return 0;
239         if (elements_fit_in_base(fa))
240                 part = (struct flex_array_part *)&fa->parts[0];
241         else {
242                 part_nr = fa_element_to_part_nr(fa, element_nr);
243                 part = fa->parts[part_nr];
244                 if (!part)
245                         return -EINVAL;
246         }
247         dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
248         memset(dst, FLEX_ARRAY_FREE, fa->element_size);
249         return 0;
250 }
251 EXPORT_SYMBOL_GPL(rpl_flex_array_clear);
252
253 /**
254  * flex_array_prealloc - guarantee that array space exists
255  * @fa:                 the flex array for which to preallocate parts
256  * @start:              index of first array element for which space is
257  *                      allocated
258  * @nr_elements:        number of elements for which space is allocated
259  * @flags:              page allocation flags
260  *
261  * This will guarantee that no future calls to flex_array_put()
262  * will allocate memory.  It can be used if you are expecting to
263  * be holding a lock or in some atomic context while writing
264  * data into the array.
265  *
266  * Locking must be provided by the caller.
267  */
268 int rpl_flex_array_prealloc(struct flex_array *fa, unsigned int start,
269                         unsigned int nr_elements, gfp_t flags)
270 {
271         int start_part;
272         int end_part;
273         int part_nr;
274         unsigned int end;
275         struct flex_array_part *part;
276
277         if (!start && !nr_elements)
278                 return 0;
279         if (start >= fa->total_nr_elements)
280                 return -ENOSPC;
281         if (!nr_elements)
282                 return 0;
283
284         end = start + nr_elements - 1;
285
286         if (end >= fa->total_nr_elements)
287                 return -ENOSPC;
288         if (!fa->element_size)
289                 return 0;
290         if (elements_fit_in_base(fa))
291                 return 0;
292         start_part = fa_element_to_part_nr(fa, start);
293         end_part = fa_element_to_part_nr(fa, end);
294         for (part_nr = start_part; part_nr <= end_part; part_nr++) {
295                 part = __fa_get_part(fa, part_nr, flags);
296                 if (!part)
297                         return -ENOMEM;
298         }
299         return 0;
300 }
301 EXPORT_SYMBOL_GPL(rpl_flex_array_prealloc);
302
303 /**
304  * flex_array_get - pull data back out of the array
305  * @fa:         the flex array from which to extract data
306  * @element_nr: index of the element to fetch from the array
307  *
308  * Returns a pointer to the data at index @element_nr.  Note
309  * that this is a copy of the data that was passed in.  If you
310  * are using this to store pointers, you'll get back &ptr.  You
311  * may instead wish to use the flex_array_get_ptr helper.
312  *
313  * Locking must be provided by the caller.
314  */
315 void *rpl_flex_array_get(struct flex_array *fa, unsigned int element_nr)
316 {
317         int part_nr = 0;
318         struct flex_array_part *part;
319
320         if (!fa->element_size)
321                 return NULL;
322         if (element_nr >= fa->total_nr_elements)
323                 return NULL;
324         if (elements_fit_in_base(fa))
325                 part = (struct flex_array_part *)&fa->parts[0];
326         else {
327                 part_nr = fa_element_to_part_nr(fa, element_nr);
328                 part = fa->parts[part_nr];
329                 if (!part)
330                         return NULL;
331         }
332         return &part->elements[index_inside_part(fa, element_nr, part_nr)];
333 }
334 EXPORT_SYMBOL_GPL(rpl_flex_array_get);
335
336 /**
337  * flex_array_get_ptr - pull a ptr back out of the array
338  * @fa:         the flex array from which to extract data
339  * @element_nr: index of the element to fetch from the array
340  *
341  * Returns the pointer placed in the flex array at element_nr using
342  * flex_array_put_ptr().  This function should not be called if the
343  * element in question was not set using the _put_ptr() helper.
344  */
345 void *rpl_flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
346 {
347         void **tmp;
348
349         tmp = flex_array_get(fa, element_nr);
350         if (!tmp)
351                 return NULL;
352
353         return *tmp;
354 }
355 EXPORT_SYMBOL_GPL(rpl_flex_array_get_ptr);
356
357 static int part_is_free(struct flex_array_part *part)
358 {
359         int i;
360
361         for (i = 0; i < sizeof(struct flex_array_part); i++)
362                 if (part->elements[i] != FLEX_ARRAY_FREE)
363                         return 0;
364         return 1;
365 }
366
367 /**
368  * flex_array_shrink - free unused second-level pages
369  * @fa:         the flex array to shrink
370  *
371  * Frees all second-level pages that consist solely of unused
372  * elements.  Returns the number of pages freed.
373  *
374  * Locking must be provided by the caller.
375  */
376 int rpl_flex_array_shrink(struct flex_array *fa)
377 {
378         struct flex_array_part *part;
379         int part_nr;
380         int ret = 0;
381
382         if (!fa->total_nr_elements || !fa->element_size)
383                 return 0;
384         if (elements_fit_in_base(fa))
385                 return ret;
386         for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
387                 part = fa->parts[part_nr];
388                 if (!part)
389                         continue;
390                 if (part_is_free(part)) {
391                         fa->parts[part_nr] = NULL;
392                         kfree(part);
393                         ret++;
394                 }
395         }
396         return ret;
397 }
398 EXPORT_SYMBOL_GPL(rpl_flex_array_shrink);
399
400 #endif /* Linux version < 3.0.0 */