Skip to content

Commit

Permalink
selinux: convert to kvmalloc
Browse files Browse the repository at this point in the history
The flex arrays were being used for constant sized arrays, so there's no
benefit to using flex_arrays over something simpler.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Kent Overstreet <[email protected]>
Cc: Paul Moore <[email protected]>
Cc: Stephen Smalley <[email protected]>
Cc: Eric Paris <[email protected]>
Cc: Alexey Dobriyan <[email protected]>
Cc: Al Viro <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Marcelo Ricardo Leitner <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Neil Horman <[email protected]>
Cc: Pravin B Shelar <[email protected]>
Cc: Shaohua Li <[email protected]>
Cc: Vlad Yasevich <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
koverstreet authored and torvalds committed Mar 12, 2019
1 parent b330e6a commit acdf52d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 144 deletions.
40 changes: 19 additions & 21 deletions security/selinux/ss/avtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = &h->htable[hvalue];

newnode->next = *n;
*n = newnode;
}

h->nel++;
Expand All @@ -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 &&
Expand Down Expand Up @@ -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 &&
Expand All @@ -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 &&
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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;
Expand All @@ -295,14 +293,15 @@ 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;
}

int avtab_init(struct avtab *h)
{
kvfree(h->htable);
h->htable = NULL;
h->nel = 0;
return 0;
Expand All @@ -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 = kvcalloc(nslot, sizeof(void *), GFP_KERNEL);
if (!h->htable)
return -ENOMEM;

Expand All @@ -353,7 +351,7 @@ void avtab_hash_eval(struct avtab *h, char *tag)
max_chain_len = 0;
chain2_len_sum = 0;
for (i = 0; i < h->nslot; i++) {
cur = flex_array_get_ptr(h->htable, i);
cur = h->htable[i];
if (cur) {
slots_used++;
chain_len = 0;
Expand Down Expand Up @@ -646,7 +644,7 @@ int avtab_write(struct policydb *p, struct avtab *a, void *fp)
return rc;

for (i = 0; i < a->nslot; i++) {
for (cur = flex_array_get_ptr(a->htable, i); cur;
for (cur = a->htable[i]; cur;
cur = cur->next) {
rc = avtab_write_item(p, cur, fp);
if (rc)
Expand Down
4 changes: 1 addition & 3 deletions security/selinux/ss/avtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#define _SS_AVTAB_H_

#include "security.h"
#include <linux/flex_array.h>

struct avtab_key {
u16 source_type; /* source type */
Expand Down Expand Up @@ -84,11 +83,10 @@ struct avtab_node {
};

struct avtab {
struct flex_array *htable;
struct avtab_node **htable;
u32 nel; /* number of elements */
u32 nslot; /* number of hash slots */
u32 mask; /* mask to compute hash func */

};

int avtab_init(struct avtab *);
Expand Down
6 changes: 1 addition & 5 deletions security/selinux/ss/conditional.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,14 @@ int cond_index_bool(void *key, void *datum, void *datap)
{
struct policydb *p;
struct cond_bool_datum *booldatum;
struct flex_array *fa;

booldatum = datum;
p = datap;

if (!booldatum->value || booldatum->value > p->p_bools.nprim)
return -EINVAL;

fa = p->sym_val_to_name[SYM_BOOLS];
if (flex_array_put_ptr(fa, booldatum->value - 1, key,
GFP_KERNEL | __GFP_ZERO))
BUG();
p->sym_val_to_name[SYM_BOOLS][booldatum->value - 1] = key;
p->bool_val_to_struct[booldatum->value - 1] = booldatum;

return 0;
Expand Down
Loading

0 comments on commit acdf52d

Please sign in to comment.