Re: [PATCH 3/6] selinux: convert to kvmalloc

2018-09-07 Thread Kent Overstreet
On Sat, Sep 08, 2018 at 02:08:03AM +0900, Tetsuo Handa wrote:
> On 2018/09/08 1:56, Kent Overstreet wrote:
> > @@ -329,8 +328,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)
> > nslot = MAX_AVTAB_HASH_BUCKETS;
> > mask = nslot - 1;
> >  
> > -   h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
> > -GFP_KERNEL | __GFP_ZERO);
> > +   h->htable = kvmalloc_array(nslot, sizeof(void *), GFP_KERNEL);
> > if (!h->htable)
> > return -ENOMEM;
> >  
> 
> kvmalloc_array() does not imply __GFP_ZERO.

Thanks, fixed


Re: [PATCH 3/6] selinux: convert to kvmalloc

2018-09-07 Thread Kent Overstreet
On Sat, Sep 08, 2018 at 02:08:03AM +0900, Tetsuo Handa wrote:
> On 2018/09/08 1:56, Kent Overstreet wrote:
> > @@ -329,8 +328,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)
> > nslot = MAX_AVTAB_HASH_BUCKETS;
> > mask = nslot - 1;
> >  
> > -   h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
> > -GFP_KERNEL | __GFP_ZERO);
> > +   h->htable = kvmalloc_array(nslot, sizeof(void *), GFP_KERNEL);
> > if (!h->htable)
> > return -ENOMEM;
> >  
> 
> kvmalloc_array() does not imply __GFP_ZERO.

Thanks, fixed


Re: [PATCH 3/6] selinux: convert to kvmalloc

2018-09-07 Thread Tetsuo Handa
On 2018/09/08 1:56, Kent Overstreet wrote:
> @@ -329,8 +328,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)
>   nslot = MAX_AVTAB_HASH_BUCKETS;
>   mask = nslot - 1;
>  
> - h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
> -  GFP_KERNEL | __GFP_ZERO);
> + h->htable = kvmalloc_array(nslot, sizeof(void *), GFP_KERNEL);
>   if (!h->htable)
>   return -ENOMEM;
>  

kvmalloc_array() does not imply __GFP_ZERO.



Re: [PATCH 3/6] selinux: convert to kvmalloc

2018-09-07 Thread Tetsuo Handa
On 2018/09/08 1:56, Kent Overstreet wrote:
> @@ -329,8 +328,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)
>   nslot = MAX_AVTAB_HASH_BUCKETS;
>   mask = nslot - 1;
>  
> - h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
> -  GFP_KERNEL | __GFP_ZERO);
> + h->htable = kvmalloc_array(nslot, sizeof(void *), GFP_KERNEL);
>   if (!h->htable)
>   return -ENOMEM;
>  

kvmalloc_array() does not imply __GFP_ZERO.



[PATCH 3/6] selinux: convert to kvmalloc

2018-09-07 Thread Kent Overstreet
The flex arrays were being used for constant sized arrays, so there's no
benefit to using flex_arrays over something simpler.

Signed-off-by: Kent Overstreet 
Cc: linux-security-mod...@vger.kernel.org
---
 security/selinux/ss/avtab.c   |  40 +-
 security/selinux/ss/avtab.h   |   4 +-
 security/selinux/ss/conditional.c |   6 +-
 security/selinux/ss/policydb.c| 122 --
 security/selinux/ss/policydb.h|  12 +--
 security/selinux/ss/services.c|  22 ++
 6 files changed, 62 insertions(+), 144 deletions(-)

diff --git a/security/selinux/ss/avtab.c b/security/selinux/ss/avtab.c
index a2c9148b06..5a7fd5f0b7 100644
--- a/security/selinux/ss/avtab.c
+++ b/security/selinux/ss/avtab.c
@@ -93,12 +93,10 @@ avtab_insert_node(struct avtab *h, int hvalue,
newnode->next = prev->next;
prev->next = newnode;
} else {
-   newnode->next = flex_array_get_ptr(h->htable, hvalue);
-   if (flex_array_put_ptr(h->htable, hvalue, newnode,
-  GFP_KERNEL|__GFP_ZERO)) {
-   kmem_cache_free(avtab_node_cachep, newnode);
-   return NULL;
-   }
+   struct avtab_node **n = >htable[hvalue];
+
+   newnode->next = *n;
+   *n = newnode;
}
 
h->nel++;
@@ -111,11 +109,11 @@ static int avtab_insert(struct avtab *h, struct avtab_key 
*key, struct avtab_dat
struct avtab_node *prev, *cur, *newnode;
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
 
-   if (!h || !h->htable)
+   if (!h)
return -EINVAL;
 
hvalue = avtab_hash(key, h->mask);
-   for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
+   for (prev = NULL, cur = h->htable[hvalue];
 cur;
 prev = cur, cur = cur->next) {
if (key->source_type == cur->key.source_type &&
@@ -156,10 +154,10 @@ avtab_insert_nonunique(struct avtab *h, struct avtab_key 
*key, struct avtab_datu
struct avtab_node *prev, *cur;
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
 
-   if (!h || !h->htable)
+   if (!h)
return NULL;
hvalue = avtab_hash(key, h->mask);
-   for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
+   for (prev = NULL, cur = h->htable[hvalue];
 cur;
 prev = cur, cur = cur->next) {
if (key->source_type == cur->key.source_type &&
@@ -186,11 +184,11 @@ struct avtab_datum *avtab_search(struct avtab *h, struct 
avtab_key *key)
struct avtab_node *cur;
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
 
-   if (!h || !h->htable)
+   if (!h)
return NULL;
 
hvalue = avtab_hash(key, h->mask);
-   for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
+   for (cur = h->htable[hvalue]; cur;
 cur = cur->next) {
if (key->source_type == cur->key.source_type &&
key->target_type == cur->key.target_type &&
@@ -222,11 +220,11 @@ avtab_search_node(struct avtab *h, struct avtab_key *key)
struct avtab_node *cur;
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
 
-   if (!h || !h->htable)
+   if (!h)
return NULL;
 
hvalue = avtab_hash(key, h->mask);
-   for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
+   for (cur = h->htable[hvalue]; cur;
 cur = cur->next) {
if (key->source_type == cur->key.source_type &&
key->target_type == cur->key.target_type &&
@@ -281,11 +279,11 @@ void avtab_destroy(struct avtab *h)
int i;
struct avtab_node *cur, *temp;
 
-   if (!h || !h->htable)
+   if (!h)
return;
 
for (i = 0; i < h->nslot; i++) {
-   cur = flex_array_get_ptr(h->htable, i);
+   cur = h->htable[i];
while (cur) {
temp = cur;
cur = cur->next;
@@ -295,7 +293,7 @@ void avtab_destroy(struct avtab *h)
kmem_cache_free(avtab_node_cachep, temp);
}
}
-   flex_array_free(h->htable);
+   kvfree(h->htable);
h->htable = NULL;
h->nslot = 0;
h->mask = 0;
@@ -303,6 +301,7 @@ void avtab_destroy(struct avtab *h)
 
 int avtab_init(struct avtab *h)
 {
+   kvfree(h->htable);
h->htable = NULL;
h->nel = 0;
return 0;
@@ -329,8 +328,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)
nslot = MAX_AVTAB_HASH_BUCKETS;
mask = nslot - 1;
 
-   h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
-GFP_KERNEL | __GFP_ZERO);
+   h->htable = kvmalloc_array(nslot, sizeof(void *), GFP_KERNEL);

[PATCH 3/6] selinux: convert to kvmalloc

2018-09-07 Thread Kent Overstreet
The flex arrays were being used for constant sized arrays, so there's no
benefit to using flex_arrays over something simpler.

Signed-off-by: Kent Overstreet 
Cc: linux-security-mod...@vger.kernel.org
---
 security/selinux/ss/avtab.c   |  40 +-
 security/selinux/ss/avtab.h   |   4 +-
 security/selinux/ss/conditional.c |   6 +-
 security/selinux/ss/policydb.c| 122 --
 security/selinux/ss/policydb.h|  12 +--
 security/selinux/ss/services.c|  22 ++
 6 files changed, 62 insertions(+), 144 deletions(-)

diff --git a/security/selinux/ss/avtab.c b/security/selinux/ss/avtab.c
index a2c9148b06..5a7fd5f0b7 100644
--- a/security/selinux/ss/avtab.c
+++ b/security/selinux/ss/avtab.c
@@ -93,12 +93,10 @@ avtab_insert_node(struct avtab *h, int hvalue,
newnode->next = prev->next;
prev->next = newnode;
} else {
-   newnode->next = flex_array_get_ptr(h->htable, hvalue);
-   if (flex_array_put_ptr(h->htable, hvalue, newnode,
-  GFP_KERNEL|__GFP_ZERO)) {
-   kmem_cache_free(avtab_node_cachep, newnode);
-   return NULL;
-   }
+   struct avtab_node **n = >htable[hvalue];
+
+   newnode->next = *n;
+   *n = newnode;
}
 
h->nel++;
@@ -111,11 +109,11 @@ static int avtab_insert(struct avtab *h, struct avtab_key 
*key, struct avtab_dat
struct avtab_node *prev, *cur, *newnode;
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
 
-   if (!h || !h->htable)
+   if (!h)
return -EINVAL;
 
hvalue = avtab_hash(key, h->mask);
-   for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
+   for (prev = NULL, cur = h->htable[hvalue];
 cur;
 prev = cur, cur = cur->next) {
if (key->source_type == cur->key.source_type &&
@@ -156,10 +154,10 @@ avtab_insert_nonunique(struct avtab *h, struct avtab_key 
*key, struct avtab_datu
struct avtab_node *prev, *cur;
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
 
-   if (!h || !h->htable)
+   if (!h)
return NULL;
hvalue = avtab_hash(key, h->mask);
-   for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
+   for (prev = NULL, cur = h->htable[hvalue];
 cur;
 prev = cur, cur = cur->next) {
if (key->source_type == cur->key.source_type &&
@@ -186,11 +184,11 @@ struct avtab_datum *avtab_search(struct avtab *h, struct 
avtab_key *key)
struct avtab_node *cur;
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
 
-   if (!h || !h->htable)
+   if (!h)
return NULL;
 
hvalue = avtab_hash(key, h->mask);
-   for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
+   for (cur = h->htable[hvalue]; cur;
 cur = cur->next) {
if (key->source_type == cur->key.source_type &&
key->target_type == cur->key.target_type &&
@@ -222,11 +220,11 @@ avtab_search_node(struct avtab *h, struct avtab_key *key)
struct avtab_node *cur;
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
 
-   if (!h || !h->htable)
+   if (!h)
return NULL;
 
hvalue = avtab_hash(key, h->mask);
-   for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
+   for (cur = h->htable[hvalue]; cur;
 cur = cur->next) {
if (key->source_type == cur->key.source_type &&
key->target_type == cur->key.target_type &&
@@ -281,11 +279,11 @@ void avtab_destroy(struct avtab *h)
int i;
struct avtab_node *cur, *temp;
 
-   if (!h || !h->htable)
+   if (!h)
return;
 
for (i = 0; i < h->nslot; i++) {
-   cur = flex_array_get_ptr(h->htable, i);
+   cur = h->htable[i];
while (cur) {
temp = cur;
cur = cur->next;
@@ -295,7 +293,7 @@ void avtab_destroy(struct avtab *h)
kmem_cache_free(avtab_node_cachep, temp);
}
}
-   flex_array_free(h->htable);
+   kvfree(h->htable);
h->htable = NULL;
h->nslot = 0;
h->mask = 0;
@@ -303,6 +301,7 @@ void avtab_destroy(struct avtab *h)
 
 int avtab_init(struct avtab *h)
 {
+   kvfree(h->htable);
h->htable = NULL;
h->nel = 0;
return 0;
@@ -329,8 +328,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)
nslot = MAX_AVTAB_HASH_BUCKETS;
mask = nslot - 1;
 
-   h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
-GFP_KERNEL | __GFP_ZERO);
+   h->htable = kvmalloc_array(nslot, sizeof(void *), GFP_KERNEL);