aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/flex_array.h
diff options
context:
space:
mode:
authorDavid Rientjes <rientjes@google.com>2009-09-21 20:04:33 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2009-09-22 10:17:47 -0400
commit45b588d6e5cc172704bac0c998ce54873b149b22 (patch)
tree57a9d3478af60fcf6932c1f61b083b9203ef1ee8 /include/linux/flex_array.h
parent4af5a2f770cc8575840ccb1514ec76ecb592985c (diff)
flex_array: introduce DEFINE_FLEX_ARRAY
FLEX_ARRAY_INIT(element_size, total_nr_elements) cannot determine if either parameter is valid, so flex arrays which are statically allocated with this interface can easily become corrupted or reference beyond its allocated memory. This removes FLEX_ARRAY_INIT() as a struct flex_array initializer since no initializer may perform the required checking. Instead, the array is now defined with a new interface: DEFINE_FLEX_ARRAY(name, element_size, total_nr_elements) This may be prefixed with `static' for file scope. This interface includes compile-time checking of the parameters to ensure they are valid. Since the validity of both element_size and total_nr_elements depend on FLEX_ARRAY_BASE_SIZE and FLEX_ARRAY_PART_SIZE, the kernel build will fail if either of these predefined values changes such that the array parameters are no longer valid. Since BUILD_BUG_ON() requires compile time constants, several of the static inline functions that were once local to lib/flex_array.c had to be moved to include/linux/flex_array.h. Signed-off-by: David Rientjes <rientjes@google.com> Acked-by: Dave Hansen <dave@linux.vnet.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'include/linux/flex_array.h')
-rw-r--r--include/linux/flex_array.h30
1 files changed, 26 insertions, 4 deletions
diff --git a/include/linux/flex_array.h b/include/linux/flex_array.h
index f12401e485fe..1d747f72298b 100644
--- a/include/linux/flex_array.h
+++ b/include/linux/flex_array.h
@@ -31,10 +31,32 @@ struct flex_array {
31 }; 31 };
32}; 32};
33 33
34#define FLEX_ARRAY_INIT(size, total) { { {\ 34/* Number of bytes left in base struct flex_array, excluding metadata */
35 .element_size = (size), \ 35#define FLEX_ARRAY_BASE_BYTES_LEFT \
36 .total_nr_elements = (total), \ 36 (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
37} } } 37
38/* Number of pointers in base to struct flex_array_part pages */
39#define FLEX_ARRAY_NR_BASE_PTRS \
40 (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
41
42/* Number of elements of size that fit in struct flex_array_part */
43#define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
44 (FLEX_ARRAY_PART_SIZE / size)
45
46/*
47 * Defines a statically allocated flex array and ensures its parameters are
48 * valid.
49 */
50#define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
51 struct flex_array __arrayname = { { { \
52 .element_size = (__element_size), \
53 .total_nr_elements = (__total), \
54 } } }; \
55 static inline void __arrayname##_invalid_parameter(void) \
56 { \
57 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
58 FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
59 }
38 60
39struct flex_array *flex_array_alloc(int element_size, unsigned int total, 61struct flex_array *flex_array_alloc(int element_size, unsigned int total,
40 gfp_t flags); 62 gfp_t flags);