flex_array.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Flexible array managed in PAGE_SIZE parts
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2009
  19. *
  20. * Author: Dave Hansen <dave@linux.vnet.ibm.com>
  21. */
  22. #include <linux/flex_array.h>
  23. #include <linux/slab.h>
  24. #include <linux/stddef.h>
  25. #include <linux/export.h>
  26. #include <linux/reciprocal_div.h>
  27. struct flex_array_part {
  28. char elements[FLEX_ARRAY_PART_SIZE];
  29. };
  30. /*
  31. * If a user requests an allocation which is small
  32. * enough, we may simply use the space in the
  33. * flex_array->parts[] array to store the user
  34. * data.
  35. */
  36. static inline int elements_fit_in_base(struct flex_array *fa)
  37. {
  38. int data_size = fa->element_size * fa->total_nr_elements;
  39. if (data_size <= FLEX_ARRAY_BASE_BYTES_LEFT)
  40. return 1;
  41. return 0;
  42. }
  43. /**
  44. * flex_array_alloc - allocate a new flexible array
  45. * @element_size: the size of individual elements in the array
  46. * @total: total number of elements that this should hold
  47. * @flags: page allocation flags to use for base array
  48. *
  49. * Note: all locking must be provided by the caller.
  50. *
  51. * @total is used to size internal structures. If the user ever
  52. * accesses any array indexes >=@total, it will produce errors.
  53. *
  54. * The maximum number of elements is defined as: the number of
  55. * elements that can be stored in a page times the number of
  56. * page pointers that we can fit in the base structure or (using
  57. * integer math):
  58. *
  59. * (PAGE_SIZE/element_size) * (PAGE_SIZE-8)/sizeof(void *)
  60. *
  61. * Here's a table showing example capacities. Note that the maximum
  62. * index that the get/put() functions is just nr_objects-1. This
  63. * basically means that you get 4MB of storage on 32-bit and 2MB on
  64. * 64-bit.
  65. *
  66. *
  67. * Element size | Objects | Objects |
  68. * PAGE_SIZE=4k | 32-bit | 64-bit |
  69. * ---------------------------------|
  70. * 1 bytes | 4177920 | 2088960 |
  71. * 2 bytes | 2088960 | 1044480 |
  72. * 3 bytes | 1392300 | 696150 |
  73. * 4 bytes | 1044480 | 522240 |
  74. * 32 bytes | 130560 | 65408 |
  75. * 33 bytes | 126480 | 63240 |
  76. * 2048 bytes | 2040 | 1020 |
  77. * 2049 bytes | 1020 | 510 |
  78. * void * | 1044480 | 261120 |
  79. *
  80. * Since 64-bit pointers are twice the size, we lose half the
  81. * capacity in the base structure. Also note that no effort is made
  82. * to efficiently pack objects across page boundaries.
  83. */
  84. struct flex_array *flex_array_alloc(int element_size, unsigned int total,
  85. gfp_t flags)
  86. {
  87. struct flex_array *ret;
  88. int elems_per_part = 0;
  89. int max_size = 0;
  90. struct reciprocal_value reciprocal_elems = { 0 };
  91. if (element_size) {
  92. elems_per_part = FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
  93. reciprocal_elems = reciprocal_value(elems_per_part);
  94. max_size = FLEX_ARRAY_NR_BASE_PTRS * elems_per_part;
  95. }
  96. /* max_size will end up 0 if element_size > PAGE_SIZE */
  97. if (total > max_size)
  98. return NULL;
  99. ret = kzalloc(sizeof(struct flex_array), flags);
  100. if (!ret)
  101. return NULL;
  102. ret->element_size = element_size;
  103. ret->total_nr_elements = total;
  104. ret->elems_per_part = elems_per_part;
  105. ret->reciprocal_elems = reciprocal_elems;
  106. if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO))
  107. memset(&ret->parts[0], FLEX_ARRAY_FREE,
  108. FLEX_ARRAY_BASE_BYTES_LEFT);
  109. return ret;
  110. }
  111. EXPORT_SYMBOL(flex_array_alloc);
  112. static int fa_element_to_part_nr(struct flex_array *fa,
  113. unsigned int element_nr)
  114. {
  115. /*
  116. * if element_size == 0 we don't get here, so we never touch
  117. * the zeroed fa->reciprocal_elems, which would yield invalid
  118. * results
  119. */
  120. return reciprocal_divide(element_nr, fa->reciprocal_elems);
  121. }
  122. /**
  123. * flex_array_free_parts - just free the second-level pages
  124. * @fa: the flex array from which to free parts
  125. *
  126. * This is to be used in cases where the base 'struct flex_array'
  127. * has been statically allocated and should not be free.
  128. */
  129. void flex_array_free_parts(struct flex_array *fa)
  130. {
  131. int part_nr;
  132. if (elements_fit_in_base(fa))
  133. return;
  134. for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
  135. kfree(fa->parts[part_nr]);
  136. }
  137. EXPORT_SYMBOL(flex_array_free_parts);
  138. void flex_array_free(struct flex_array *fa)
  139. {
  140. flex_array_free_parts(fa);
  141. kfree(fa);
  142. }
  143. EXPORT_SYMBOL(flex_array_free);
  144. static unsigned int index_inside_part(struct flex_array *fa,
  145. unsigned int element_nr,
  146. unsigned int part_nr)
  147. {
  148. unsigned int part_offset;
  149. part_offset = element_nr - part_nr * fa->elems_per_part;
  150. return part_offset * fa->element_size;
  151. }
  152. static struct flex_array_part *
  153. __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
  154. {
  155. struct flex_array_part *part = fa->parts[part_nr];
  156. if (!part) {
  157. part = kmalloc(sizeof(struct flex_array_part), flags);
  158. if (!part)
  159. return NULL;
  160. if (!(flags & __GFP_ZERO))
  161. memset(part, FLEX_ARRAY_FREE,
  162. sizeof(struct flex_array_part));
  163. fa->parts[part_nr] = part;
  164. }
  165. return part;
  166. }
  167. /**
  168. * flex_array_put - copy data into the array at @element_nr
  169. * @fa: the flex array to copy data into
  170. * @element_nr: index of the position in which to insert
  171. * the new element.
  172. * @src: address of data to copy into the array
  173. * @flags: page allocation flags to use for array expansion
  174. *
  175. *
  176. * Note that this *copies* the contents of @src into
  177. * the array. If you are trying to store an array of
  178. * pointers, make sure to pass in &ptr instead of ptr.
  179. * You may instead wish to use the flex_array_put_ptr()
  180. * helper function.
  181. *
  182. * Locking must be provided by the caller.
  183. */
  184. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  185. gfp_t flags)
  186. {
  187. int part_nr = 0;
  188. struct flex_array_part *part;
  189. void *dst;
  190. if (element_nr >= fa->total_nr_elements)
  191. return -ENOSPC;
  192. if (!fa->element_size)
  193. return 0;
  194. if (elements_fit_in_base(fa))
  195. part = (struct flex_array_part *)&fa->parts[0];
  196. else {
  197. part_nr = fa_element_to_part_nr(fa, element_nr);
  198. part = __fa_get_part(fa, part_nr, flags);
  199. if (!part)
  200. return -ENOMEM;
  201. }
  202. dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
  203. memcpy(dst, src, fa->element_size);
  204. return 0;
  205. }
  206. EXPORT_SYMBOL(flex_array_put);
  207. /**
  208. * flex_array_clear - clear element in array at @element_nr
  209. * @fa: the flex array of the element.
  210. * @element_nr: index of the position to clear.
  211. *
  212. * Locking must be provided by the caller.
  213. */
  214. int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
  215. {
  216. int part_nr = 0;
  217. struct flex_array_part *part;
  218. void *dst;
  219. if (element_nr >= fa->total_nr_elements)
  220. return -ENOSPC;
  221. if (!fa->element_size)
  222. return 0;
  223. if (elements_fit_in_base(fa))
  224. part = (struct flex_array_part *)&fa->parts[0];
  225. else {
  226. part_nr = fa_element_to_part_nr(fa, element_nr);
  227. part = fa->parts[part_nr];
  228. if (!part)
  229. return -EINVAL;
  230. }
  231. dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
  232. memset(dst, FLEX_ARRAY_FREE, fa->element_size);
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(flex_array_clear);
  236. /**
  237. * flex_array_prealloc - guarantee that array space exists
  238. * @fa: the flex array for which to preallocate parts
  239. * @start: index of first array element for which space is allocated
  240. * @nr_elements: number of elements for which space is allocated
  241. * @flags: page allocation flags
  242. *
  243. * This will guarantee that no future calls to flex_array_put()
  244. * will allocate memory. It can be used if you are expecting to
  245. * be holding a lock or in some atomic context while writing
  246. * data into the array.
  247. *
  248. * Locking must be provided by the caller.
  249. */
  250. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  251. unsigned int nr_elements, gfp_t flags)
  252. {
  253. int start_part;
  254. int end_part;
  255. int part_nr;
  256. unsigned int end;
  257. struct flex_array_part *part;
  258. if (!start && !nr_elements)
  259. return 0;
  260. if (start >= fa->total_nr_elements)
  261. return -ENOSPC;
  262. if (!nr_elements)
  263. return 0;
  264. end = start + nr_elements - 1;
  265. if (end >= fa->total_nr_elements)
  266. return -ENOSPC;
  267. if (!fa->element_size)
  268. return 0;
  269. if (elements_fit_in_base(fa))
  270. return 0;
  271. start_part = fa_element_to_part_nr(fa, start);
  272. end_part = fa_element_to_part_nr(fa, end);
  273. for (part_nr = start_part; part_nr <= end_part; part_nr++) {
  274. part = __fa_get_part(fa, part_nr, flags);
  275. if (!part)
  276. return -ENOMEM;
  277. }
  278. return 0;
  279. }
  280. EXPORT_SYMBOL(flex_array_prealloc);
  281. /**
  282. * flex_array_get - pull data back out of the array
  283. * @fa: the flex array from which to extract data
  284. * @element_nr: index of the element to fetch from the array
  285. *
  286. * Returns a pointer to the data at index @element_nr. Note
  287. * that this is a copy of the data that was passed in. If you
  288. * are using this to store pointers, you'll get back &ptr. You
  289. * may instead wish to use the flex_array_get_ptr helper.
  290. *
  291. * Locking must be provided by the caller.
  292. */
  293. void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
  294. {
  295. int part_nr = 0;
  296. struct flex_array_part *part;
  297. if (!fa->element_size)
  298. return NULL;
  299. if (element_nr >= fa->total_nr_elements)
  300. return NULL;
  301. if (elements_fit_in_base(fa))
  302. part = (struct flex_array_part *)&fa->parts[0];
  303. else {
  304. part_nr = fa_element_to_part_nr(fa, element_nr);
  305. part = fa->parts[part_nr];
  306. if (!part)
  307. return NULL;
  308. }
  309. return &part->elements[index_inside_part(fa, element_nr, part_nr)];
  310. }
  311. EXPORT_SYMBOL(flex_array_get);
  312. /**
  313. * flex_array_get_ptr - pull a ptr back out of the array
  314. * @fa: the flex array from which to extract data
  315. * @element_nr: index of the element to fetch from the array
  316. *
  317. * Returns the pointer placed in the flex array at element_nr using
  318. * flex_array_put_ptr(). This function should not be called if the
  319. * element in question was not set using the _put_ptr() helper.
  320. */
  321. void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
  322. {
  323. void **tmp;
  324. tmp = flex_array_get(fa, element_nr);
  325. if (!tmp)
  326. return NULL;
  327. return *tmp;
  328. }
  329. EXPORT_SYMBOL(flex_array_get_ptr);
  330. static int part_is_free(struct flex_array_part *part)
  331. {
  332. int i;
  333. for (i = 0; i < sizeof(struct flex_array_part); i++)
  334. if (part->elements[i] != FLEX_ARRAY_FREE)
  335. return 0;
  336. return 1;
  337. }
  338. /**
  339. * flex_array_shrink - free unused second-level pages
  340. * @fa: the flex array to shrink
  341. *
  342. * Frees all second-level pages that consist solely of unused
  343. * elements. Returns the number of pages freed.
  344. *
  345. * Locking must be provided by the caller.
  346. */
  347. int flex_array_shrink(struct flex_array *fa)
  348. {
  349. struct flex_array_part *part;
  350. int part_nr;
  351. int ret = 0;
  352. if (!fa->total_nr_elements || !fa->element_size)
  353. return 0;
  354. if (elements_fit_in_base(fa))
  355. return ret;
  356. for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
  357. part = fa->parts[part_nr];
  358. if (!part)
  359. continue;
  360. if (part_is_free(part)) {
  361. fa->parts[part_nr] = NULL;
  362. kfree(part);
  363. ret++;
  364. }
  365. }
  366. return ret;
  367. }
  368. EXPORT_SYMBOL(flex_array_shrink);