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