1=================================== 2Using flexible arrays in the kernel 3=================================== 4 5:Updated: Last updated for 2.6.32 6:Author: Jonathan Corbet <corbet@lwn.net> 7 8Large contiguous memory allocations can be unreliable in the Linux kernel. 9Kernel programmers will sometimes respond to this problem by allocating 10pages with vmalloc(). This solution not ideal, though. On 32-bit systems, 11memory from vmalloc() must be mapped into a relatively small address space; 12it's easy to run out. On SMP systems, the page table changes required by 13vmalloc() allocations can require expensive cross-processor interrupts on 14all CPUs. And, on all systems, use of space in the vmalloc() range 15increases pressure on the translation lookaside buffer (TLB), reducing the 16performance of the system. 17 18In many cases, the need for memory from vmalloc() can be eliminated by 19piecing together an array from smaller parts; the flexible array library 20exists to make this task easier. 21 22A flexible array holds an arbitrary (within limits) number of fixed-sized 23objects, accessed via an integer index. Sparse arrays are handled 24reasonably well. Only single-page allocations are made, so memory 25allocation failures should be relatively rare. The down sides are that the 26arrays cannot be indexed directly, individual object size cannot exceed the 27system page size, and putting data into a flexible array requires a copy 28operation. It's also worth noting that flexible arrays do no internal 29locking at all; if concurrent access to an array is possible, then the 30caller must arrange for appropriate mutual exclusion. 31 32The creation of a flexible array is done with:: 33 34 #include <linux/flex_array.h> 35 36 struct flex_array *flex_array_alloc(int element_size, 37 unsigned int total, 38 gfp_t flags); 39 40The individual object size is provided by element_size, while total is the 41maximum number of objects which can be stored in the array. The flags 42argument is passed directly to the internal memory allocation calls. With 43the current code, using flags to ask for high memory is likely to lead to 44notably unpleasant side effects. 45 46It is also possible to define flexible arrays at compile time with:: 47 48 DEFINE_FLEX_ARRAY(name, element_size, total); 49 50This macro will result in a definition of an array with the given name; the 51element size and total will be checked for validity at compile time. 52 53Storing data into a flexible array is accomplished with a call to:: 54 55 int flex_array_put(struct flex_array *array, unsigned int element_nr, 56 void *src, gfp_t flags); 57 58This call will copy the data from src into the array, in the position 59indicated by element_nr (which must be less than the maximum specified when 60the array was created). If any memory allocations must be performed, flags 61will be used. The return value is zero on success, a negative error code 62otherwise. 63 64There might possibly be a need to store data into a flexible array while 65running in some sort of atomic context; in this situation, sleeping in the 66memory allocator would be a bad thing. That can be avoided by using 67GFP_ATOMIC for the flags value, but, often, there is a better way. The 68trick is to ensure that any needed memory allocations are done before 69entering atomic context, using:: 70 71 int flex_array_prealloc(struct flex_array *array, unsigned int start, 72 unsigned int nr_elements, gfp_t flags); 73 74This function will ensure that memory for the elements indexed in the range 75defined by start and nr_elements has been allocated. Thereafter, a 76flex_array_put() call on an element in that range is guaranteed not to 77block. 78 79Getting data back out of the array is done with:: 80 81 void *flex_array_get(struct flex_array *fa, unsigned int element_nr); 82 83The return value is a pointer to the data element, or NULL if that 84particular element has never been allocated. 85 86Note that it is possible to get back a valid pointer for an element which 87has never been stored in the array. Memory for array elements is allocated 88one page at a time; a single allocation could provide memory for several 89adjacent elements. Flexible array elements are normally initialized to the 90value FLEX_ARRAY_FREE (defined as 0x6c in <linux/poison.h>), so errors 91involving that number probably result from use of unstored array entries. 92Note that, if array elements are allocated with __GFP_ZERO, they will be 93initialized to zero and this poisoning will not happen. 94 95Individual elements in the array can be cleared with:: 96 97 int flex_array_clear(struct flex_array *array, unsigned int element_nr); 98 99This function will set the given element to FLEX_ARRAY_FREE and return 100zero. If storage for the indicated element is not allocated for the array, 101flex_array_clear() will return -EINVAL instead. Note that clearing an 102element does not release the storage associated with it; to reduce the 103allocated size of an array, call:: 104 105 int flex_array_shrink(struct flex_array *array); 106 107The return value will be the number of pages of memory actually freed. 108This function works by scanning the array for pages containing nothing but 109FLEX_ARRAY_FREE bytes, so (1) it can be expensive, and (2) it will not work 110if the array's pages are allocated with __GFP_ZERO. 111 112It is possible to remove all elements of an array with a call to:: 113 114 void flex_array_free_parts(struct flex_array *array); 115 116This call frees all elements, but leaves the array itself in place. 117Freeing the entire array is done with:: 118 119 void flex_array_free(struct flex_array *array); 120 121As of this writing, there are no users of flexible arrays in the mainline 122kernel. The functions described here are also not exported to modules; 123that will probably be fixed when somebody comes up with a need for it. 124