flexible-arrays.txt 5.5 KB

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