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