Skip to contentSkip to conversation

flex_array: flex_array_prealloc takes a number of elements, not an end

PATCH In review Eric Paris 3 participants 11 messages Apr 26, 2011

Subsystems (3)
lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/3 flex_array: flex_array_prealloc takes a number of elements, not an end

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.

---

 Documentation/flexible-arrays.txt |    4 ++--
 include/linux/flex_array.h        |    2 +-
 lib/flex_array.c                  |   13 ++++++++-----
 security/selinux/ss/policydb.c    |    6 +++---
 4 files changed, 14 insertions(+), 11 deletions(-)
Documentation/flexible-arrays.txt +2 -2
index cb8a3a0..df904ae 100644
@@ -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.
 
include/linux/flex_array.h +1 -1
index 70e4efa..ebeb2f3 100644
@@ -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,
lib/flex_array.c +8 -5
index c0ea40b..0c33b24 100644
@@ -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
@@ -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))
security/selinux/ss/policydb.c +3 -3
index 3258799..1b150d7 100644
@@ -551,7 +551,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;
 
@@ -568,7 +568,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;
@@ -2445,7 +2445,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;

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

author
#1 lore raw

PATCH 2/3 flex_arrays: allow zero length flex arrays

Just like kmalloc will allow one to allocate a 0 length segment of memory flex arrays should do the same thing. It should bomb if you try to use something, but it should at least allow the allocation.

---

 lib/flex_array.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
lib/flex_array.c +10 -1
index 0c33b24..2554a5f 100644
@@ -253,9 +253,16 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
 	unsigned int end;
 	struct flex_array_part *part;
 
+	if (!fa->total_nr_elements && !start)
+		return 0;
+	if (start >= fa->total_nr_elements)
+		return -ENOSPC;
+	if (!nr_elements)
+		return 0;
+
 	end = start + nr_elements - 1;
 
-	if (start >= fa->total_nr_elements || end >= fa->total_nr_elements)
+	if (end >= fa->total_nr_elements)
 		return -ENOSPC;
 	if (elements_fit_in_base(fa))
 		return 0;
@@ -346,6 +353,8 @@ int flex_array_shrink(struct flex_array *fa)
 	int part_nr;
 	int ret = 0;
 
+	if (!fa->total_nr_elements)
+		return 0;
 	if (elements_fit_in_base(fa))
 		return ret;
 	for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

maintainer
#2 lore raw

Re: [PATCH 2/3] flex_arrays: allow zero length flex arrays

Quoted 3 lines · On Mon, 25 Apr 2011 21:45:37 -0400 Eric Paris <[email protected]> wrote:

Just like kmalloc will allow one to allocate a 0 length segment of memory flex arrays should do the same thing. It should bomb if you try to use something, but it should at least allow the allocation.

Well I suppose that's vaguely sensible, but it adds additional code for no apparent benefit.

IOW: why?

#3 lore raw

Re: [PATCH 2/3] flex_arrays: allow zero length flex arrays

In reply to Eric Paris
Quoted 16 lines · Eric Paris

Just like kmalloc will allow one to allocate a 0 length segment of memory flex arrays should do the same thing. It should bomb if you try to use something, but it should at least allow the allocation.

---

 lib/flex_array.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
lib/flex_array.c +0 -0
index 0c33b24..2554a5f 100644

lib/flex_array.c int flex_array_prealloc(struct flex_array *fa, unsigned int start, · line 257

Quoted 5 lines
lib/flex_array.c +2 -0
@@ -253,9 +253,16 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
 	unsigned int end;
 	struct flex_array_part *part;
 
+	if (!fa->total_nr_elements && !start)
+		return 0;

I guess it works either way, but I'd say that checking for a zero 'len' prealloc would be more important (and meaningful) than checking a zero 'start'.

If someone passed start=0 and len=44 for a fa->total_nr_elements=0 array, I'd expect -ENOSPC, but this would return 0.

-- Dave

author
#4 lore raw

Re: [PATCH 2/3] flex_arrays: allow zero length flex arrays

Quoted 27 lines · Dave Hansen
Quoted 16 lines · Eric Paris

Just like kmalloc will allow one to allocate a 0 length segment of memory flex arrays should do the same thing. It should bomb if you try to use something, but it should at least allow the allocation.

---

 lib/flex_array.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
lib/flex_array.c +0 -0
index 0c33b24..2554a5f 100644

lib/flex_array.c int flex_array_prealloc(struct flex_array *fa, unsigned int start, · line 257

Quoted 5 lines
lib/flex_array.c +2 -0
@@ -253,9 +253,16 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
 	unsigned int end;
 	struct flex_array_part *part;
 
+	if (!fa->total_nr_elements && !start)
+		return 0;

I guess it works either way, but I'd say that checking for a zero 'len' prealloc would be more important (and meaningful) than checking a zero 'start'.

Quoted 2 lines

If someone passed start=0 and len=44 for a fa->total_nr_elements=0 array, I'd expect -ENOSPC, but this would return 0.

will fix.

if (!nr_elements && !start)

	return 0;

-Eric

Quoted 3 lines

-- Dave

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

author
#5 lore raw

PATCH 3/3 flex_array: allow 0 length elements

In reply to Eric Paris

allow elements of 0 length. We already allow flex_arrays with 0 elements, but this patch allow us to have elements that are 0 bytes long.

---

 lib/flex_array.c |   26 ++++++++++++++++++++------
 1 files changed, 20 insertions(+), 6 deletions(-)
lib/flex_array.c +20 -6
index 2554a5f..dcd91d5 100644
@@ -88,8 +88,11 @@ struct flex_array *flex_array_alloc(int element_size, unsigned int total,
 					gfp_t flags)
 {
 	struct flex_array *ret;
-	int max_size = FLEX_ARRAY_NR_BASE_PTRS *
-				FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
+	int max_size = 0;
+
+	if (element_size)
+		max_size = FLEX_ARRAY_NR_BASE_PTRS *
+			   FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
 
 	/* max_size will end up 0 if element_size > PAGE_SIZE */
 	if (total > max_size)
@@ -183,15 +186,18 @@ __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
 int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
 			gfp_t flags)
 {
-	int part_nr = fa_element_to_part_nr(fa, element_nr);
+	int part_nr;
 	struct flex_array_part *part;
 	void *dst;
 
 	if (element_nr >= fa->total_nr_elements)
 		return -ENOSPC;
+	if (!fa->element_size)
+		return 0;
 	if (elements_fit_in_base(fa))
 		part = (struct flex_array_part *)&fa->parts[0];
 	else {
+		part_nr = fa_element_to_part_nr(fa, element_nr);
 		part = __fa_get_part(fa, part_nr, flags);
 		if (!part)
 			return -ENOMEM;
@@ -211,15 +217,18 @@ EXPORT_SYMBOL(flex_array_put);
  */
 int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
 {
-	int part_nr = fa_element_to_part_nr(fa, element_nr);
+	int part_nr;
 	struct flex_array_part *part;
 	void *dst;
 
 	if (element_nr >= fa->total_nr_elements)
 		return -ENOSPC;
+	if (!fa->element_size)
+		return 0;
 	if (elements_fit_in_base(fa))
 		part = (struct flex_array_part *)&fa->parts[0];
 	else {
+		part_nr = fa_element_to_part_nr(fa, element_nr);
 		part = fa->parts[part_nr];
 		if (!part)
 			return -EINVAL;
@@ -264,6 +273,8 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
 
 	if (end >= fa->total_nr_elements)
 		return -ENOSPC;
+	if (!fa->element_size)
+		return 0;
 	if (elements_fit_in_base(fa))
 		return 0;
 	start_part = fa_element_to_part_nr(fa, start);
@@ -291,14 +302,17 @@ EXPORT_SYMBOL(flex_array_prealloc);
  */
 void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
 {
-	int part_nr = fa_element_to_part_nr(fa, element_nr);
+	int part_nr;
 	struct flex_array_part *part;
 
+	if (!fa->element_size)
+		return NULL;
 	if (element_nr >= fa->total_nr_elements)
 		return NULL;
 	if (elements_fit_in_base(fa))
 		part = (struct flex_array_part *)&fa->parts[0];
 	else {
+		part_nr = fa_element_to_part_nr(fa, element_nr);
 		part = fa->parts[part_nr];
 		if (!part)
 			return NULL;
@@ -353,7 +367,7 @@ int flex_array_shrink(struct flex_array *fa)
 	int part_nr;
 	int ret = 0;
 
-	if (!fa->total_nr_elements)
+	if (!fa->total_nr_elements || !fa->element_size)
 		return 0;
 	if (elements_fit_in_base(fa))
 		return ret;

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

maintainer
#6 lore raw

Re: [PATCH 3/3] flex_array: allow 0 length elements

Quoted 2 lines · On Mon, 25 Apr 2011 21:45:43 -0400 Eric Paris <[email protected]> wrote:

allow elements of 0 length. We already allow flex_arrays with 0 elements, but this patch allow us to have elements that are 0 bytes long.

Again, sounds vaguely useful but it adds code and churns the code for no apparent benefit.

IOW2: why?

Acked-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] flex_array: allow 0 length elements

In reply to Eric Paris
Quoted 6 lines · Eric Paris

allow elements of 0 length. We already allow flex_arrays with 0 elements, but
this patch allow us to have elements that are 0 bytes long.

I'd like to see a bit of the history in the changelog. kmalloc(0) returns NULL, and we'd effectively like to do the same since flex_arrays really are like a mini allocator. The 0-sized elements come from userspace and we *have* to handle them in the security code, etc...

The code looks good, though.

-- Dave

maintainer
#8 lore raw
In reply to Eric Paris
Quoted 3 lines · On Mon, 25 Apr 2011 21:45:31 -0400 Eric Paris <[email protected]> wrote:

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.

Why?

author
#9 lore raw
Quoted 8 lines · Andrew Morton
Quoted 3 lines · On Mon, 25 Apr 2011 21:45:31 -0400 Eric Paris <[email protected]> wrote:

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.

Why?

All three of these patches came from these discussions:
http://marc.info/?t=129552800100005&r=1&w=2
http://marc.info/?t=129646395700004&r=1&w=2
http://marc.info/?t=129708178200007&r=1&w=2

1/3 was changed just because it seemed to be the way the interface was used

2/3 was changed because the 2.6.38 kernel fails to load certain selinux policies which worked under 2.6.37 because of this lack of functionality

3/3 was changed just because that's how maybe it should work, although there are no known users.

I can certainly update 2/3 to better explain the rational but I can't really do any better on 1/3 and 3/3 other than to just say what the change is......

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

Acked-by on v1 1/3
#10 lore raw
In reply to Eric Paris
Quoted 6 lines · Eric Paris

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.

Perhaps add this to the description?

flex_arrays got introduced before they had users. When folks started using it, they ended up needing a different API than we coded up originally. This swaps over to the API that folks apparently need.

-- Dave