datapath: Backport flex_arrays.
[sliver-openvswitch.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 *flex_array_alloc(int element_size, unsigned int total,
93                                         gfp_t flags)
94 {
95         struct flex_array *ret;
96         int elems_per_part = 0;
97         int reciprocal_elems = 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
122 static int fa_element_to_part_nr(struct flex_array *fa,
123                                         unsigned int element_nr)
124 {
125         return reciprocal_divide(element_nr, fa->reciprocal_elems);
126 }
127
128 /**
129  * flex_array_free_parts - just free the second-level pages
130  * @fa:         the flex array from which to free parts
131  *
132  * This is to be used in cases where the base 'struct flex_array'
133  * has been statically allocated and should not be free.
134  */
135 void flex_array_free_parts(struct flex_array *fa)
136 {
137         int part_nr;
138
139         if (elements_fit_in_base(fa))
140                 return;
141         for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
142                 kfree(fa->parts[part_nr]);
143 }
144
145 void flex_array_free(struct flex_array *fa)
146 {
147         flex_array_free_parts(fa);
148         kfree(fa);
149 }
150
151 static unsigned int index_inside_part(struct flex_array *fa,
152                                         unsigned int element_nr,
153                                         unsigned int part_nr)
154 {
155         unsigned int part_offset;
156
157         part_offset = element_nr - part_nr * fa->elems_per_part;
158         return part_offset * fa->element_size;
159 }
160
161 static struct flex_array_part *
162 __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
163 {
164         struct flex_array_part *part = fa->parts[part_nr];
165         if (!part) {
166                 part = kmalloc(sizeof(struct flex_array_part), flags);
167                 if (!part)
168                         return NULL;
169                 if (!(flags & __GFP_ZERO))
170                         memset(part, FLEX_ARRAY_FREE,
171                                 sizeof(struct flex_array_part));
172                 fa->parts[part_nr] = part;
173         }
174         return part;
175 }
176
177 /**
178  * flex_array_put - copy data into the array at @element_nr
179  * @fa:         the flex array to copy data into
180  * @element_nr: index of the position in which to insert
181  *              the new element.
182  * @src:        address of data to copy into the array
183  * @flags:      page allocation flags to use for array expansion
184  *
185  *
186  * Note that this *copies* the contents of @src into
187  * the array.  If you are trying to store an array of
188  * pointers, make sure to pass in &ptr instead of ptr.
189  * You may instead wish to use the flex_array_put_ptr()
190  * helper function.
191  *
192  * Locking must be provided by the caller.
193  */
194 int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
195                         gfp_t flags)
196 {
197         int part_nr = 0;
198         struct flex_array_part *part;
199         void *dst;
200
201         if (element_nr >= fa->total_nr_elements)
202                 return -ENOSPC;
203         if (!fa->element_size)
204                 return 0;
205         if (elements_fit_in_base(fa))
206                 part = (struct flex_array_part *)&fa->parts[0];
207         else {
208                 part_nr = fa_element_to_part_nr(fa, element_nr);
209                 part = __fa_get_part(fa, part_nr, flags);
210                 if (!part)
211                         return -ENOMEM;
212         }
213         dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
214         memcpy(dst, src, fa->element_size);
215         return 0;
216 }
217
218 /**
219  * flex_array_clear - clear element in array at @element_nr
220  * @fa:         the flex array of the element.
221  * @element_nr: index of the position to clear.
222  *
223  * Locking must be provided by the caller.
224  */
225 int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
226 {
227         int part_nr = 0;
228         struct flex_array_part *part;
229         void *dst;
230
231         if (element_nr >= fa->total_nr_elements)
232                 return -ENOSPC;
233         if (!fa->element_size)
234                 return 0;
235         if (elements_fit_in_base(fa))
236                 part = (struct flex_array_part *)&fa->parts[0];
237         else {
238                 part_nr = fa_element_to_part_nr(fa, element_nr);
239                 part = fa->parts[part_nr];
240                 if (!part)
241                         return -EINVAL;
242         }
243         dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
244         memset(dst, FLEX_ARRAY_FREE, fa->element_size);
245         return 0;
246 }
247
248 /**
249  * flex_array_prealloc - guarantee that array space exists
250  * @fa:                 the flex array for which to preallocate parts
251  * @start:              index of first array element for which space is allocated
252  * @nr_elements:        number of elements for which space is allocated
253  * @flags:              page allocation flags
254  *
255  * This will guarantee that no future calls to flex_array_put()
256  * will allocate memory.  It can be used if you are expecting to
257  * be holding a lock or in some atomic context while writing
258  * data into the array.
259  *
260  * Locking must be provided by the caller.
261  */
262 int flex_array_prealloc(struct flex_array *fa, unsigned int start,
263                         unsigned int nr_elements, gfp_t flags)
264 {
265         int start_part;
266         int end_part;
267         int part_nr;
268         unsigned int end;
269         struct flex_array_part *part;
270
271         if (!start && !nr_elements)
272                 return 0;
273         if (start >= fa->total_nr_elements)
274                 return -ENOSPC;
275         if (!nr_elements)
276                 return 0;
277
278         end = start + nr_elements - 1;
279
280         if (end >= fa->total_nr_elements)
281                 return -ENOSPC;
282         if (!fa->element_size)
283                 return 0;
284         if (elements_fit_in_base(fa))
285                 return 0;
286         start_part = fa_element_to_part_nr(fa, start);
287         end_part = fa_element_to_part_nr(fa, end);
288         for (part_nr = start_part; part_nr <= end_part; part_nr++) {
289                 part = __fa_get_part(fa, part_nr, flags);
290                 if (!part)
291                         return -ENOMEM;
292         }
293         return 0;
294 }
295
296 /**
297  * flex_array_get - pull data back out of the array
298  * @fa:         the flex array from which to extract data
299  * @element_nr: index of the element to fetch from the array
300  *
301  * Returns a pointer to the data at index @element_nr.  Note
302  * that this is a copy of the data that was passed in.  If you
303  * are using this to store pointers, you'll get back &ptr.  You
304  * may instead wish to use the flex_array_get_ptr helper.
305  *
306  * Locking must be provided by the caller.
307  */
308 void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
309 {
310         int part_nr = 0;
311         struct flex_array_part *part;
312
313         if (!fa->element_size)
314                 return NULL;
315         if (element_nr >= fa->total_nr_elements)
316                 return NULL;
317         if (elements_fit_in_base(fa))
318                 part = (struct flex_array_part *)&fa->parts[0];
319         else {
320                 part_nr = fa_element_to_part_nr(fa, element_nr);
321                 part = fa->parts[part_nr];
322                 if (!part)
323                         return NULL;
324         }
325         return &part->elements[index_inside_part(fa, element_nr, part_nr)];
326 }
327
328 /**
329  * flex_array_get_ptr - pull a ptr back out of the array
330  * @fa:         the flex array from which to extract data
331  * @element_nr: index of the element to fetch from the array
332  *
333  * Returns the pointer placed in the flex array at element_nr using
334  * flex_array_put_ptr().  This function should not be called if the
335  * element in question was not set using the _put_ptr() helper.
336  */
337 void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
338 {
339         void **tmp;
340
341         tmp = flex_array_get(fa, element_nr);
342         if (!tmp)
343                 return NULL;
344
345         return *tmp;
346 }
347
348 static int part_is_free(struct flex_array_part *part)
349 {
350         int i;
351
352         for (i = 0; i < sizeof(struct flex_array_part); i++)
353                 if (part->elements[i] != FLEX_ARRAY_FREE)
354                         return 0;
355         return 1;
356 }
357
358 /**
359  * flex_array_shrink - free unused second-level pages
360  * @fa:         the flex array to shrink
361  *
362  * Frees all second-level pages that consist solely of unused
363  * elements.  Returns the number of pages freed.
364  *
365  * Locking must be provided by the caller.
366  */
367 int flex_array_shrink(struct flex_array *fa)
368 {
369         struct flex_array_part *part;
370         int part_nr;
371         int ret = 0;
372
373         if (!fa->total_nr_elements || !fa->element_size)
374                 return 0;
375         if (elements_fit_in_base(fa))
376                 return ret;
377         for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
378                 part = fa->parts[part_nr];
379                 if (!part)
380                         continue;
381                 if (part_is_free(part)) {
382                         fa->parts[part_nr] = NULL;
383                         kfree(part);
384                         ret++;
385                 }
386         }
387         return ret;
388 }
389
390 #endif /* Linux version < 3.0.0 */