Skip to content

Commit 6698100

Browse files
author
Alexei Starovoitov
committed
Merge branch '"map_extra" and bloom filter fixups'
Joanne Koong says: ==================== There are 3 patches in this patchset: 1/3 - Bloom filter naming fixups (kernel/bpf/bloom_filter.c) 2/3 - Add alignment padding for map_extra, rearrange fields in bpf_map struct to consolidate holes 3/3 - Bloom filter tests (prog_tests/bloom_filter_map): Add test for successful userspace calls, some refactoring to use bpf_create_map instead of bpf_create_map_xattr v1 -> v2: * In prog_tests/bloom_filter_map: remove unneeded line break, also change the inner_map_test to use bpf_create_map instead of bpf_create_map_xattr. * Add acked-bys to commit messages ==================== Acked-by: Martin KaFai Lau <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents f27a6fa + 7a67087 commit 6698100

File tree

5 files changed

+64
-52
lines changed

5 files changed

+64
-52
lines changed

include/linux/bpf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,23 @@ struct bpf_map {
168168
u32 key_size;
169169
u32 value_size;
170170
u32 max_entries;
171-
u32 map_flags;
172171
u64 map_extra; /* any per-map-type extra fields */
172+
u32 map_flags;
173173
int spin_lock_off; /* >=0 valid offset, <0 error */
174174
int timer_off; /* >=0 valid offset, <0 error */
175175
u32 id;
176176
int numa_node;
177177
u32 btf_key_type_id;
178178
u32 btf_value_type_id;
179+
u32 btf_vmlinux_value_type_id;
179180
struct btf *btf;
180181
#ifdef CONFIG_MEMCG_KMEM
181182
struct mem_cgroup *memcg;
182183
#endif
183184
char name[BPF_OBJ_NAME_LEN];
184-
u32 btf_vmlinux_value_type_id;
185185
bool bypass_spec_v1;
186186
bool frozen; /* write-once; write-protected by freeze_mutex */
187-
/* 22 bytes hole */
187+
/* 14 bytes hole */
188188

189189
/* The 3rd and 4th cacheline with misc members to avoid false sharing
190190
* particularly with refcounting.

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5662,6 +5662,7 @@ struct bpf_map_info {
56625662
__u32 btf_id;
56635663
__u32 btf_key_type_id;
56645664
__u32 btf_value_type_id;
5665+
__u32 :32; /* alignment pad */
56655666
__u64 map_extra;
56665667
} __attribute__((aligned(8)));
56675668

kernel/bpf/bloom_filter.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static u32 hash(struct bpf_bloom_filter *bloom, void *value,
4040
return h & bloom->bitset_mask;
4141
}
4242

43-
static int peek_elem(struct bpf_map *map, void *value)
43+
static int bloom_map_peek_elem(struct bpf_map *map, void *value)
4444
{
4545
struct bpf_bloom_filter *bloom =
4646
container_of(map, struct bpf_bloom_filter, map);
@@ -55,7 +55,7 @@ static int peek_elem(struct bpf_map *map, void *value)
5555
return 0;
5656
}
5757

58-
static int push_elem(struct bpf_map *map, void *value, u64 flags)
58+
static int bloom_map_push_elem(struct bpf_map *map, void *value, u64 flags)
5959
{
6060
struct bpf_bloom_filter *bloom =
6161
container_of(map, struct bpf_bloom_filter, map);
@@ -72,12 +72,12 @@ static int push_elem(struct bpf_map *map, void *value, u64 flags)
7272
return 0;
7373
}
7474

75-
static int pop_elem(struct bpf_map *map, void *value)
75+
static int bloom_map_pop_elem(struct bpf_map *map, void *value)
7676
{
7777
return -EOPNOTSUPP;
7878
}
7979

80-
static struct bpf_map *map_alloc(union bpf_attr *attr)
80+
static struct bpf_map *bloom_map_alloc(union bpf_attr *attr)
8181
{
8282
u32 bitset_bytes, bitset_mask, nr_hash_funcs, nr_bits;
8383
int numa_node = bpf_map_attr_numa_node(attr);
@@ -90,11 +90,13 @@ static struct bpf_map *map_alloc(union bpf_attr *attr)
9090
attr->max_entries == 0 ||
9191
attr->map_flags & ~BLOOM_CREATE_FLAG_MASK ||
9292
!bpf_map_flags_access_ok(attr->map_flags) ||
93+
/* The lower 4 bits of map_extra (0xF) specify the number
94+
* of hash functions
95+
*/
9396
(attr->map_extra & ~0xF))
9497
return ERR_PTR(-EINVAL);
9598

96-
/* The lower 4 bits of map_extra specify the number of hash functions */
97-
nr_hash_funcs = attr->map_extra & 0xF;
99+
nr_hash_funcs = attr->map_extra;
98100
if (nr_hash_funcs == 0)
99101
/* Default to using 5 hash functions if unspecified */
100102
nr_hash_funcs = 5;
@@ -150,46 +152,47 @@ static struct bpf_map *map_alloc(union bpf_attr *attr)
150152
return &bloom->map;
151153
}
152154

153-
static void map_free(struct bpf_map *map)
155+
static void bloom_map_free(struct bpf_map *map)
154156
{
155157
struct bpf_bloom_filter *bloom =
156158
container_of(map, struct bpf_bloom_filter, map);
157159

158160
bpf_map_area_free(bloom);
159161
}
160162

161-
static void *lookup_elem(struct bpf_map *map, void *key)
163+
static void *bloom_map_lookup_elem(struct bpf_map *map, void *key)
162164
{
163165
/* The eBPF program should use map_peek_elem instead */
164166
return ERR_PTR(-EINVAL);
165167
}
166168

167-
static int update_elem(struct bpf_map *map, void *key,
168-
void *value, u64 flags)
169+
static int bloom_map_update_elem(struct bpf_map *map, void *key,
170+
void *value, u64 flags)
169171
{
170172
/* The eBPF program should use map_push_elem instead */
171173
return -EINVAL;
172174
}
173175

174-
static int check_btf(const struct bpf_map *map, const struct btf *btf,
175-
const struct btf_type *key_type,
176-
const struct btf_type *value_type)
176+
static int bloom_map_check_btf(const struct bpf_map *map,
177+
const struct btf *btf,
178+
const struct btf_type *key_type,
179+
const struct btf_type *value_type)
177180
{
178181
/* Bloom filter maps are keyless */
179182
return btf_type_is_void(key_type) ? 0 : -EINVAL;
180183
}
181184

182-
static int bpf_bloom_btf_id;
185+
static int bpf_bloom_map_btf_id;
183186
const struct bpf_map_ops bloom_filter_map_ops = {
184187
.map_meta_equal = bpf_map_meta_equal,
185-
.map_alloc = map_alloc,
186-
.map_free = map_free,
187-
.map_push_elem = push_elem,
188-
.map_peek_elem = peek_elem,
189-
.map_pop_elem = pop_elem,
190-
.map_lookup_elem = lookup_elem,
191-
.map_update_elem = update_elem,
192-
.map_check_btf = check_btf,
188+
.map_alloc = bloom_map_alloc,
189+
.map_free = bloom_map_free,
190+
.map_push_elem = bloom_map_push_elem,
191+
.map_peek_elem = bloom_map_peek_elem,
192+
.map_pop_elem = bloom_map_pop_elem,
193+
.map_lookup_elem = bloom_map_lookup_elem,
194+
.map_update_elem = bloom_map_update_elem,
195+
.map_check_btf = bloom_map_check_btf,
193196
.map_btf_name = "bpf_bloom_filter",
194-
.map_btf_id = &bpf_bloom_btf_id,
197+
.map_btf_id = &bpf_bloom_map_btf_id,
195198
};

tools/include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5662,6 +5662,7 @@ struct bpf_map_info {
56625662
__u32 btf_id;
56635663
__u32 btf_key_type_id;
56645664
__u32 btf_value_type_id;
5665+
__u32 :32; /* alignment pad */
56655666
__u64 map_extra;
56665667
} __attribute__((aligned(8)));
56675668

tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,31 @@
77

88
static void test_fail_cases(void)
99
{
10-
struct bpf_create_map_attr xattr = {
11-
.name = "bloom_filter_map",
12-
.map_type = BPF_MAP_TYPE_BLOOM_FILTER,
13-
.max_entries = 100,
14-
.value_size = 11,
15-
};
1610
__u32 value;
1711
int fd, err;
1812

1913
/* Invalid key size */
20-
xattr.key_size = 4;
21-
fd = bpf_create_map_xattr(&xattr);
14+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 4, sizeof(value), 100, 0);
2215
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid key size"))
2316
close(fd);
24-
xattr.key_size = 0;
2517

2618
/* Invalid value size */
27-
xattr.value_size = 0;
28-
fd = bpf_create_map_xattr(&xattr);
19+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, 0, 100, 0);
2920
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid value size 0"))
3021
close(fd);
31-
xattr.value_size = 11;
3222

3323
/* Invalid max entries size */
34-
xattr.max_entries = 0;
35-
fd = bpf_create_map_xattr(&xattr);
24+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 0, 0);
3625
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid max entries size"))
3726
close(fd);
38-
xattr.max_entries = 100;
3927

4028
/* Bloom filter maps do not support BPF_F_NO_PREALLOC */
41-
xattr.map_flags = BPF_F_NO_PREALLOC;
42-
fd = bpf_create_map_xattr(&xattr);
29+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
30+
BPF_F_NO_PREALLOC);
4331
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid flags"))
4432
close(fd);
45-
xattr.map_flags = 0;
4633

47-
fd = bpf_create_map_xattr(&xattr);
34+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100, 0);
4835
if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter"))
4936
return;
5037

@@ -67,6 +54,30 @@ static void test_fail_cases(void)
6754
close(fd);
6855
}
6956

57+
static void test_success_cases(void)
58+
{
59+
char value[11];
60+
int fd, err;
61+
62+
/* Create a map */
63+
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
64+
BPF_F_ZERO_SEED | BPF_F_NUMA_NODE);
65+
if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter success case"))
66+
return;
67+
68+
/* Add a value to the bloom filter */
69+
err = bpf_map_update_elem(fd, NULL, &value, 0);
70+
if (!ASSERT_OK(err, "bpf_map_update_elem bloom filter success case"))
71+
goto done;
72+
73+
/* Lookup a value in the bloom filter */
74+
err = bpf_map_lookup_elem(fd, NULL, &value);
75+
ASSERT_OK(err, "bpf_map_update_elem bloom filter success case");
76+
77+
done:
78+
close(fd);
79+
}
80+
7081
static void check_bloom(struct bloom_filter_map *skel)
7182
{
7283
struct bpf_link *link;
@@ -86,16 +97,11 @@ static void test_inner_map(struct bloom_filter_map *skel, const __u32 *rand_vals
8697
__u32 nr_rand_vals)
8798
{
8899
int outer_map_fd, inner_map_fd, err, i, key = 0;
89-
struct bpf_create_map_attr xattr = {
90-
.name = "bloom_filter_inner_map",
91-
.map_type = BPF_MAP_TYPE_BLOOM_FILTER,
92-
.value_size = sizeof(__u32),
93-
.max_entries = nr_rand_vals,
94-
};
95100
struct bpf_link *link;
96101

97102
/* Create a bloom filter map that will be used as the inner map */
98-
inner_map_fd = bpf_create_map_xattr(&xattr);
103+
inner_map_fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(*rand_vals),
104+
nr_rand_vals, 0);
99105
if (!ASSERT_GE(inner_map_fd, 0, "bpf_create_map bloom filter inner map"))
100106
return;
101107

@@ -190,6 +196,7 @@ void test_bloom_filter_map(void)
190196
int err;
191197

192198
test_fail_cases();
199+
test_success_cases();
193200

194201
err = setup_progs(&skel, &rand_vals, &nr_rand_vals);
195202
if (err)

0 commit comments

Comments
 (0)