Skip to content

Commit

Permalink
flex_array: flex_array_prealloc takes a number of elements, not an end
Browse files Browse the repository at this point in the history
Change flex_array_prealloc to take the number of elements for which space
should be allocated instead of the last (inclusive) element. Users
and documentation are updated accordingly.  flex_arrays got introduced before
they had users.  When folks started using it, they ended up needing a
different API than was coded up originally.  This swaps over to the API that
folks apparently need.

Based-on-patch-by: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Eric Paris <eparis@redhat.com>
Tested-by: Chris Richards <gizmo@giz-works.com>
Acked-by: Dave Hansen <dave@linux.vnet.ibm.com>
Cc: stable@kernel.org [2.6.38+]
  • Loading branch information
eparis committed Apr 28, 2011
1 parent cb1e922 commit 5d30b10
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Documentation/flexible-arrays.txt
Expand Up @@ -66,10 +66,10 @@ trick is to ensure that any needed memory allocations are done before
entering atomic context, using:

int flex_array_prealloc(struct flex_array *array, unsigned int start,
unsigned int end, gfp_t flags);
unsigned int nr_elements, gfp_t flags);

This function will ensure that memory for the elements indexed in the range
defined by start and end has been allocated. Thereafter, a
defined by start and nr_elements has been allocated. Thereafter, a
flex_array_put() call on an element in that range is guaranteed not to
block.

Expand Down
2 changes: 1 addition & 1 deletion include/linux/flex_array.h
Expand Up @@ -61,7 +61,7 @@ struct flex_array {
struct flex_array *flex_array_alloc(int element_size, unsigned int total,
gfp_t flags);
int flex_array_prealloc(struct flex_array *fa, unsigned int start,
unsigned int end, gfp_t flags);
unsigned int nr_elements, 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, unsigned int element_nr, void *src,
Expand Down
13 changes: 8 additions & 5 deletions lib/flex_array.c
Expand Up @@ -232,10 +232,10 @@ EXPORT_SYMBOL(flex_array_clear);

/**
* flex_array_prealloc - guarantee that array space exists
* @fa: the flex array for which to preallocate parts
* @start: index of first array element for which space is allocated
* @end: index of last (inclusive) element for which space is allocated
* @flags: page allocation flags
* @fa: the flex array for which to preallocate parts
* @start: index of first array element for which space is allocated
* @nr_elements: number of elements for which space is allocated
* @flags: page allocation flags
*
* This will guarantee that no future calls to flex_array_put()
* will allocate memory. It can be used if you are expecting to
Expand All @@ -245,13 +245,16 @@ EXPORT_SYMBOL(flex_array_clear);
* Locking must be provided by the caller.
*/
int flex_array_prealloc(struct flex_array *fa, unsigned int start,
unsigned int end, gfp_t flags)
unsigned int nr_elements, gfp_t flags)
{
int start_part;
int end_part;
int part_nr;
unsigned int end;
struct flex_array_part *part;

end = start + nr_elements - 1;

if (start >= fa->total_nr_elements || end >= fa->total_nr_elements)
return -ENOSPC;
if (elements_fit_in_base(fa))
Expand Down
6 changes: 3 additions & 3 deletions security/selinux/ss/policydb.c
Expand Up @@ -502,7 +502,7 @@ static int policydb_index(struct policydb *p)
goto out;

rc = flex_array_prealloc(p->type_val_to_struct_array, 0,
p->p_types.nprim - 1, GFP_KERNEL | __GFP_ZERO);
p->p_types.nprim, GFP_KERNEL | __GFP_ZERO);
if (rc)
goto out;

Expand All @@ -519,7 +519,7 @@ static int policydb_index(struct policydb *p)
goto out;

rc = flex_array_prealloc(p->sym_val_to_name[i],
0, p->symtab[i].nprim - 1,
0, p->symtab[i].nprim,
GFP_KERNEL | __GFP_ZERO);
if (rc)
goto out;
Expand Down Expand Up @@ -2375,7 +2375,7 @@ int policydb_read(struct policydb *p, void *fp)
goto bad;

/* preallocate so we don't have to worry about the put ever failing */
rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim - 1,
rc = flex_array_prealloc(p->type_attr_map_array, 0, p->p_types.nprim,
GFP_KERNEL | __GFP_ZERO);
if (rc)
goto bad;
Expand Down

0 comments on commit 5d30b10

Please sign in to comment.