Skip to content

Commit acdf52d

Browse files
koverstreettorvalds
authored andcommitted
selinux: convert to kvmalloc
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/20181217131929.11727-4-kent.overstreet@gmail.com Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com> Cc: Paul Moore <paul@paul-moore.com> Cc: Stephen Smalley <sds@tycho.nsa.gov> Cc: Eric Paris <eparis@parisplace.org> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Neil Horman <nhorman@tuxdriver.com> Cc: Pravin B Shelar <pshelar@ovn.org> Cc: Shaohua Li <shli@kernel.org> Cc: Vlad Yasevich <vyasevich@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b330e6a commit acdf52d

File tree

6 files changed

+62
-144
lines changed

6 files changed

+62
-144
lines changed

security/selinux/ss/avtab.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@ avtab_insert_node(struct avtab *h, int hvalue,
9393
newnode->next = prev->next;
9494
prev->next = newnode;
9595
} else {
96-
newnode->next = flex_array_get_ptr(h->htable, hvalue);
97-
if (flex_array_put_ptr(h->htable, hvalue, newnode,
98-
GFP_KERNEL|__GFP_ZERO)) {
99-
kmem_cache_free(avtab_node_cachep, newnode);
100-
return NULL;
101-
}
96+
struct avtab_node **n = &h->htable[hvalue];
97+
98+
newnode->next = *n;
99+
*n = newnode;
102100
}
103101

104102
h->nel++;
@@ -111,11 +109,11 @@ static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_dat
111109
struct avtab_node *prev, *cur, *newnode;
112110
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
113111

114-
if (!h || !h->htable)
112+
if (!h)
115113
return -EINVAL;
116114

117115
hvalue = avtab_hash(key, h->mask);
118-
for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
116+
for (prev = NULL, cur = h->htable[hvalue];
119117
cur;
120118
prev = cur, cur = cur->next) {
121119
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
156154
struct avtab_node *prev, *cur;
157155
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
158156

159-
if (!h || !h->htable)
157+
if (!h)
160158
return NULL;
161159
hvalue = avtab_hash(key, h->mask);
162-
for (prev = NULL, cur = flex_array_get_ptr(h->htable, hvalue);
160+
for (prev = NULL, cur = h->htable[hvalue];
163161
cur;
164162
prev = cur, cur = cur->next) {
165163
if (key->source_type == cur->key.source_type &&
@@ -186,11 +184,11 @@ struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key)
186184
struct avtab_node *cur;
187185
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
188186

189-
if (!h || !h->htable)
187+
if (!h)
190188
return NULL;
191189

192190
hvalue = avtab_hash(key, h->mask);
193-
for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
191+
for (cur = h->htable[hvalue]; cur;
194192
cur = cur->next) {
195193
if (key->source_type == cur->key.source_type &&
196194
key->target_type == cur->key.target_type &&
@@ -222,11 +220,11 @@ avtab_search_node(struct avtab *h, struct avtab_key *key)
222220
struct avtab_node *cur;
223221
u16 specified = key->specified & ~(AVTAB_ENABLED|AVTAB_ENABLED_OLD);
224222

225-
if (!h || !h->htable)
223+
if (!h)
226224
return NULL;
227225

228226
hvalue = avtab_hash(key, h->mask);
229-
for (cur = flex_array_get_ptr(h->htable, hvalue); cur;
227+
for (cur = h->htable[hvalue]; cur;
230228
cur = cur->next) {
231229
if (key->source_type == cur->key.source_type &&
232230
key->target_type == cur->key.target_type &&
@@ -281,11 +279,11 @@ void avtab_destroy(struct avtab *h)
281279
int i;
282280
struct avtab_node *cur, *temp;
283281

284-
if (!h || !h->htable)
282+
if (!h)
285283
return;
286284

287285
for (i = 0; i < h->nslot; i++) {
288-
cur = flex_array_get_ptr(h->htable, i);
286+
cur = h->htable[i];
289287
while (cur) {
290288
temp = cur;
291289
cur = cur->next;
@@ -295,14 +293,15 @@ void avtab_destroy(struct avtab *h)
295293
kmem_cache_free(avtab_node_cachep, temp);
296294
}
297295
}
298-
flex_array_free(h->htable);
296+
kvfree(h->htable);
299297
h->htable = NULL;
300298
h->nslot = 0;
301299
h->mask = 0;
302300
}
303301

304302
int avtab_init(struct avtab *h)
305303
{
304+
kvfree(h->htable);
306305
h->htable = NULL;
307306
h->nel = 0;
308307
return 0;
@@ -329,8 +328,7 @@ int avtab_alloc(struct avtab *h, u32 nrules)
329328
nslot = MAX_AVTAB_HASH_BUCKETS;
330329
mask = nslot - 1;
331330

332-
h->htable = flex_array_alloc(sizeof(struct avtab_node *), nslot,
333-
GFP_KERNEL | __GFP_ZERO);
331+
h->htable = kvcalloc(nslot, sizeof(void *), GFP_KERNEL);
334332
if (!h->htable)
335333
return -ENOMEM;
336334

@@ -353,7 +351,7 @@ void avtab_hash_eval(struct avtab *h, char *tag)
353351
max_chain_len = 0;
354352
chain2_len_sum = 0;
355353
for (i = 0; i < h->nslot; i++) {
356-
cur = flex_array_get_ptr(h->htable, i);
354+
cur = h->htable[i];
357355
if (cur) {
358356
slots_used++;
359357
chain_len = 0;
@@ -646,7 +644,7 @@ int avtab_write(struct policydb *p, struct avtab *a, void *fp)
646644
return rc;
647645

648646
for (i = 0; i < a->nslot; i++) {
649-
for (cur = flex_array_get_ptr(a->htable, i); cur;
647+
for (cur = a->htable[i]; cur;
650648
cur = cur->next) {
651649
rc = avtab_write_item(p, cur, fp);
652650
if (rc)

security/selinux/ss/avtab.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#define _SS_AVTAB_H_
2525

2626
#include "security.h"
27-
#include <linux/flex_array.h>
2827

2928
struct avtab_key {
3029
u16 source_type; /* source type */
@@ -84,11 +83,10 @@ struct avtab_node {
8483
};
8584

8685
struct avtab {
87-
struct flex_array *htable;
86+
struct avtab_node **htable;
8887
u32 nel; /* number of elements */
8988
u32 nslot; /* number of hash slots */
9089
u32 mask; /* mask to compute hash func */
91-
9290
};
9391

9492
int avtab_init(struct avtab *);

security/selinux/ss/conditional.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,14 @@ int cond_index_bool(void *key, void *datum, void *datap)
195195
{
196196
struct policydb *p;
197197
struct cond_bool_datum *booldatum;
198-
struct flex_array *fa;
199198

200199
booldatum = datum;
201200
p = datap;
202201

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

206-
fa = p->sym_val_to_name[SYM_BOOLS];
207-
if (flex_array_put_ptr(fa, booldatum->value - 1, key,
208-
GFP_KERNEL | __GFP_ZERO))
209-
BUG();
205+
p->sym_val_to_name[SYM_BOOLS][booldatum->value - 1] = key;
210206
p->bool_val_to_struct[booldatum->value - 1] = booldatum;
211207

212208
return 0;

0 commit comments

Comments
 (0)