Skip to content

Commit 9328e0d

Browse files
Jakub Kicinskiborkmann
authored andcommitted
bpf: hashtab: move checks out of alloc function
Use the new callback to perform allocation checks for hash maps. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent daffc5a commit 9328e0d

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

kernel/bpf/hashtab.c

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static int alloc_extra_elems(struct bpf_htab *htab)
227227
}
228228

229229
/* Called from syscall */
230-
static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
230+
static int htab_map_alloc_check(union bpf_attr *attr)
231231
{
232232
bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
233233
attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
@@ -241,9 +241,6 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
241241
bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
242242
bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
243243
int numa_node = bpf_map_attr_numa_node(attr);
244-
struct bpf_htab *htab;
245-
int err, i;
246-
u64 cost;
247244

248245
BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
249246
offsetof(struct htab_elem, hash_node.pprev));
@@ -254,33 +251,33 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
254251
/* LRU implementation is much complicated than other
255252
* maps. Hence, limit to CAP_SYS_ADMIN for now.
256253
*/
257-
return ERR_PTR(-EPERM);
254+
return -EPERM;
258255

259256
if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK)
260257
/* reserved bits should not be used */
261-
return ERR_PTR(-EINVAL);
258+
return -EINVAL;
262259

263260
if (!lru && percpu_lru)
264-
return ERR_PTR(-EINVAL);
261+
return -EINVAL;
265262

266263
if (lru && !prealloc)
267-
return ERR_PTR(-ENOTSUPP);
264+
return -ENOTSUPP;
268265

269266
if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
270-
return ERR_PTR(-EINVAL);
267+
return -EINVAL;
271268

272269
/* check sanity of attributes.
273270
* value_size == 0 may be allowed in the future to use map as a set
274271
*/
275272
if (attr->max_entries == 0 || attr->key_size == 0 ||
276273
attr->value_size == 0)
277-
return ERR_PTR(-EINVAL);
274+
return -EINVAL;
278275

279276
if (attr->key_size > MAX_BPF_STACK)
280277
/* eBPF programs initialize keys on stack, so they cannot be
281278
* larger than max stack size
282279
*/
283-
return ERR_PTR(-E2BIG);
280+
return -E2BIG;
284281

285282
if (attr->value_size >= KMALLOC_MAX_SIZE -
286283
MAX_BPF_STACK - sizeof(struct htab_elem))
@@ -289,7 +286,28 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
289286
* sure that the elem_size doesn't overflow and it's
290287
* kmalloc-able later in htab_map_update_elem()
291288
*/
292-
return ERR_PTR(-E2BIG);
289+
return -E2BIG;
290+
291+
return 0;
292+
}
293+
294+
static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
295+
{
296+
bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
297+
attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
298+
bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
299+
attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
300+
/* percpu_lru means each cpu has its own LRU list.
301+
* it is different from BPF_MAP_TYPE_PERCPU_HASH where
302+
* the map's value itself is percpu. percpu_lru has
303+
* nothing to do with the map's value.
304+
*/
305+
bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
306+
bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
307+
int numa_node = bpf_map_attr_numa_node(attr);
308+
struct bpf_htab *htab;
309+
int err, i;
310+
u64 cost;
293311

294312
htab = kzalloc(sizeof(*htab), GFP_USER);
295313
if (!htab)
@@ -1142,6 +1160,7 @@ static void htab_map_free(struct bpf_map *map)
11421160
}
11431161

11441162
const struct bpf_map_ops htab_map_ops = {
1163+
.map_alloc_check = htab_map_alloc_check,
11451164
.map_alloc = htab_map_alloc,
11461165
.map_free = htab_map_free,
11471166
.map_get_next_key = htab_map_get_next_key,
@@ -1152,6 +1171,7 @@ const struct bpf_map_ops htab_map_ops = {
11521171
};
11531172

11541173
const struct bpf_map_ops htab_lru_map_ops = {
1174+
.map_alloc_check = htab_map_alloc_check,
11551175
.map_alloc = htab_map_alloc,
11561176
.map_free = htab_map_free,
11571177
.map_get_next_key = htab_map_get_next_key,
@@ -1235,6 +1255,7 @@ int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
12351255
}
12361256

12371257
const struct bpf_map_ops htab_percpu_map_ops = {
1258+
.map_alloc_check = htab_map_alloc_check,
12381259
.map_alloc = htab_map_alloc,
12391260
.map_free = htab_map_free,
12401261
.map_get_next_key = htab_map_get_next_key,
@@ -1244,6 +1265,7 @@ const struct bpf_map_ops htab_percpu_map_ops = {
12441265
};
12451266

12461267
const struct bpf_map_ops htab_lru_percpu_map_ops = {
1268+
.map_alloc_check = htab_map_alloc_check,
12471269
.map_alloc = htab_map_alloc,
12481270
.map_free = htab_map_free,
12491271
.map_get_next_key = htab_map_get_next_key,
@@ -1252,11 +1274,11 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = {
12521274
.map_delete_elem = htab_lru_map_delete_elem,
12531275
};
12541276

1255-
static struct bpf_map *fd_htab_map_alloc(union bpf_attr *attr)
1277+
static int fd_htab_map_alloc_check(union bpf_attr *attr)
12561278
{
12571279
if (attr->value_size != sizeof(u32))
1258-
return ERR_PTR(-EINVAL);
1259-
return htab_map_alloc(attr);
1280+
return -EINVAL;
1281+
return htab_map_alloc_check(attr);
12601282
}
12611283

12621284
static void fd_htab_map_free(struct bpf_map *map)
@@ -1327,7 +1349,7 @@ static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
13271349
if (IS_ERR(inner_map_meta))
13281350
return inner_map_meta;
13291351

1330-
map = fd_htab_map_alloc(attr);
1352+
map = htab_map_alloc(attr);
13311353
if (IS_ERR(map)) {
13321354
bpf_map_meta_free(inner_map_meta);
13331355
return map;
@@ -1371,6 +1393,7 @@ static void htab_of_map_free(struct bpf_map *map)
13711393
}
13721394

13731395
const struct bpf_map_ops htab_of_maps_map_ops = {
1396+
.map_alloc_check = fd_htab_map_alloc_check,
13741397
.map_alloc = htab_of_map_alloc,
13751398
.map_free = htab_of_map_free,
13761399
.map_get_next_key = htab_map_get_next_key,

0 commit comments

Comments
 (0)