mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-26 10:49:33 +00:00
![Dave Hansen](/assets/img/avatar_default.png)
Once a structure goes over PAGE_SIZE*2, we see occasional allocation failures. Some people have chosen to switch over to things like vmalloc() that will let them keep array-like access to such a large structures. But, vmalloc() has plenty of downsides. Here's an alternative. I think it's what Andrew was suggesting here: http://lkml.org/lkml/2009/7/2/518 I call it a flexible array. It does all of its work in PAGE_SIZE bits, so never does an order>0 allocation. The base level has PAGE_SIZE-2*sizeof(int) bytes of storage for pointers to the second level. So, with a 32-bit arch, you get about 4MB (4183112 bytes) of total storage when the objects pack nicely into a page. It is half that on 64-bit because the pointers are twice the size. There's a table detailing this in the code. There are kerneldocs for the functions, but here's an overview: flex_array_alloc() - dynamically allocate a base structure flex_array_free() - free the array and all of the second-level pages flex_array_free_parts() - free the second-level pages, but not the base (for static bases) flex_array_put() - copy into the array at the given index flex_array_get() - copy out of the array at the given index flex_array_prealloc() - preallocate the second-level pages between the given indexes to guarantee no allocs will occur at put() time. We could also potentially just pass the "element_size" into each of the API functions instead of storing it internally. That would get us one more base pointer on 32-bit. I've been testing this by running it in userspace. The header and patch that I've been using are here, as well as the little script I'm using to generate the size table which goes in the kerneldocs. http://sr71.net/~dave/linux/flexarray/ [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com> Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
47 lines
1.2 KiB
C
47 lines
1.2 KiB
C
#ifndef _FLEX_ARRAY_H
|
|
#define _FLEX_ARRAY_H
|
|
|
|
#include <linux/types.h>
|
|
#include <asm/page.h>
|
|
|
|
#define FLEX_ARRAY_PART_SIZE PAGE_SIZE
|
|
#define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
|
|
|
|
struct flex_array_part;
|
|
|
|
/*
|
|
* This is meant to replace cases where an array-like
|
|
* structure has gotten too big to fit into kmalloc()
|
|
* and the developer is getting tempted to use
|
|
* vmalloc().
|
|
*/
|
|
|
|
struct flex_array {
|
|
union {
|
|
struct {
|
|
int element_size;
|
|
int total_nr_elements;
|
|
struct flex_array_part *parts[0];
|
|
};
|
|
/*
|
|
* This little trick makes sure that
|
|
* sizeof(flex_array) == PAGE_SIZE
|
|
*/
|
|
char padding[FLEX_ARRAY_BASE_SIZE];
|
|
};
|
|
};
|
|
|
|
#define FLEX_ARRAY_INIT(size, total) { { {\
|
|
.element_size = (size), \
|
|
.total_nr_elements = (total), \
|
|
} } }
|
|
|
|
struct flex_array *flex_array_alloc(int element_size, int total, gfp_t flags);
|
|
int flex_array_prealloc(struct flex_array *fa, int start, int end, gfp_t flags);
|
|
void flex_array_free(struct flex_array *fa);
|
|
void flex_array_free_parts(struct flex_array *fa);
|
|
int flex_array_put(struct flex_array *fa, int element_nr, void *src,
|
|
gfp_t flags);
|
|
void *flex_array_get(struct flex_array *fa, int element_nr);
|
|
|
|
#endif /* _FLEX_ARRAY_H */
|