Skip to content

Commit 4c042a8

Browse files
committed
Merge branch 'bpf-misc-next'
Daniel Borkmann says: ==================== Misc BPF updates This set cleans up ldimm64 leftovers from early eBPF days and adds couple of test cases related to this to the verifier test suite. It also cleans up the kallsyms spinlock (had same patch also in queue) by relaxing it through switching to _bh variant. It fixes up test_progs in relation to htons/ntohs and adds accessor macros for the percpu tests in test_maps. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents cd5487f + f3515b5 commit 4c042a8

File tree

9 files changed

+199
-56
lines changed

9 files changed

+199
-56
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
604604
const struct bpf_insn insn1 = insn[1];
605605
u64 imm64;
606606

607-
if (insn1.code != 0 || insn1.src_reg != 0 ||
608-
insn1.dst_reg != 0 || insn1.off != 0) {
609-
/* Note: verifier in BPF core must catch invalid
610-
* instructions.
611-
*/
612-
pr_err_once("Invalid BPF_LD_IMM64 instruction\n");
613-
return -EINVAL;
614-
}
615-
616607
imm64 = (u64)insn1.imm << 32 | (u32)imm;
617608
emit_a64_mov_i64(dst, imm64, ctx);
618609

arch/x86/net/bpf_jit_comp.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,6 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
490490
break;
491491

492492
case BPF_LD | BPF_IMM | BPF_DW:
493-
if (insn[1].code != 0 || insn[1].src_reg != 0 ||
494-
insn[1].dst_reg != 0 || insn[1].off != 0) {
495-
/* verifier must catch invalid insns */
496-
pr_err("invalid BPF_LD_IMM64 insn\n");
497-
return -EINVAL;
498-
}
499-
500493
/* optimization: if imm64 is zero, use 'xor <dst>,<dst>'
501494
* to save 7 bytes.
502495
*/

kernel/bpf/core.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,27 +394,23 @@ static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
394394

395395
void bpf_prog_kallsyms_add(struct bpf_prog *fp)
396396
{
397-
unsigned long flags;
398-
399397
if (!bpf_prog_kallsyms_candidate(fp) ||
400398
!capable(CAP_SYS_ADMIN))
401399
return;
402400

403-
spin_lock_irqsave(&bpf_lock, flags);
401+
spin_lock_bh(&bpf_lock);
404402
bpf_prog_ksym_node_add(fp->aux);
405-
spin_unlock_irqrestore(&bpf_lock, flags);
403+
spin_unlock_bh(&bpf_lock);
406404
}
407405

408406
void bpf_prog_kallsyms_del(struct bpf_prog *fp)
409407
{
410-
unsigned long flags;
411-
412408
if (!bpf_prog_kallsyms_candidate(fp))
413409
return;
414410

415-
spin_lock_irqsave(&bpf_lock, flags);
411+
spin_lock_bh(&bpf_lock);
416412
bpf_prog_ksym_node_del(fp->aux);
417-
spin_unlock_irqrestore(&bpf_lock, flags);
413+
spin_unlock_bh(&bpf_lock);
418414
}
419415

420416
static struct bpf_prog *bpf_prog_kallsyms_find(unsigned long addr)

tools/testing/selftests/bpf/bpf_util.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
#include <string.h>
77
#include <errno.h>
88

9+
#include <asm/byteorder.h>
10+
11+
#if __BYTE_ORDER == __LITTLE_ENDIAN
12+
# define __bpf_ntohs(x) __builtin_bswap16(x)
13+
# define __bpf_htons(x) __builtin_bswap16(x)
14+
#elif __BYTE_ORDER == __BIG_ENDIAN
15+
# define __bpf_ntohs(x) (x)
16+
# define __bpf_htons(x) (x)
17+
#else
18+
# error "Fix your __BYTE_ORDER?!"
19+
#endif
20+
21+
#define bpf_htons(x) \
22+
(__builtin_constant_p(x) ? \
23+
__constant_htons(x) : __bpf_htons(x))
24+
#define bpf_ntohs(x) \
25+
(__builtin_constant_p(x) ? \
26+
__constant_ntohs(x) : __bpf_ntohs(x))
27+
928
static inline unsigned int bpf_num_possible_cpus(void)
1029
{
1130
static const char *fcpu = "/sys/devices/system/cpu/possible";
@@ -35,4 +54,11 @@ static inline unsigned int bpf_num_possible_cpus(void)
3554
return possible_cpus;
3655
}
3756

57+
#define __bpf_percpu_val_align __attribute__((__aligned__(8)))
58+
59+
#define BPF_DECLARE_PERCPU(type, name) \
60+
struct { type v; /* padding */ } __bpf_percpu_val_align \
61+
name[bpf_num_possible_cpus()]
62+
#define bpf_percpu(name, cpu) name[(cpu)].v
63+
3864
#endif /* __BPF_UTIL__ */

tools/testing/selftests/bpf/test_l4lb.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
#include <linux/udp.h>
2020
#include "bpf_helpers.h"
2121
#include "test_iptunnel_common.h"
22+
#include "bpf_util.h"
2223

23-
#define htons __builtin_bswap16
24-
#define ntohs __builtin_bswap16
2524
int _version SEC("version") = 1;
2625

2726
static inline __u32 rol32(__u32 word, unsigned int shift)
@@ -355,7 +354,7 @@ static __always_inline int process_packet(void *data, __u64 off, void *data_end,
355354
iph_len = sizeof(struct ipv6hdr);
356355
protocol = ip6h->nexthdr;
357356
pckt.proto = protocol;
358-
pkt_bytes = ntohs(ip6h->payload_len);
357+
pkt_bytes = bpf_ntohs(ip6h->payload_len);
359358
off += iph_len;
360359
if (protocol == IPPROTO_FRAGMENT) {
361360
return TC_ACT_SHOT;
@@ -377,7 +376,7 @@ static __always_inline int process_packet(void *data, __u64 off, void *data_end,
377376

378377
protocol = iph->protocol;
379378
pckt.proto = protocol;
380-
pkt_bytes = ntohs(iph->tot_len);
379+
pkt_bytes = bpf_ntohs(iph->tot_len);
381380
off += IPV4_HDR_LEN_NO_OPT;
382381

383382
if (iph->frag_off & PCKT_FRAGMENTED)
@@ -464,9 +463,9 @@ int balancer_ingress(struct __sk_buff *ctx)
464463
if (data + nh_off > data_end)
465464
return TC_ACT_SHOT;
466465
eth_proto = eth->eth_proto;
467-
if (eth_proto == htons(ETH_P_IP))
466+
if (eth_proto == bpf_htons(ETH_P_IP))
468467
return process_packet(data, nh_off, data_end, false, ctx);
469-
else if (eth_proto == htons(ETH_P_IPV6))
468+
else if (eth_proto == bpf_htons(ETH_P_IPV6))
470469
return process_packet(data, nh_off, data_end, true, ctx);
471470
else
472471
return TC_ACT_SHOT;

tools/testing/selftests/bpf/test_maps.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,20 @@ static void test_hashmap_sizes(int task, void *data)
137137
static void test_hashmap_percpu(int task, void *data)
138138
{
139139
unsigned int nr_cpus = bpf_num_possible_cpus();
140-
long long value[nr_cpus];
140+
BPF_DECLARE_PERCPU(long, value);
141141
long long key, next_key, first_key;
142142
int expected_key_mask = 0;
143143
int fd, i;
144144

145145
fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
146-
sizeof(value[0]), 2, map_flags);
146+
sizeof(bpf_percpu(value, 0)), 2, map_flags);
147147
if (fd < 0) {
148148
printf("Failed to create hashmap '%s'!\n", strerror(errno));
149149
exit(1);
150150
}
151151

152152
for (i = 0; i < nr_cpus; i++)
153-
value[i] = i + 100;
153+
bpf_percpu(value, i) = i + 100;
154154

155155
key = 1;
156156
/* Insert key=1 element. */
@@ -170,8 +170,9 @@ static void test_hashmap_percpu(int task, void *data)
170170
/* Check that key=1 can be found. Value could be 0 if the lookup
171171
* was run from a different CPU.
172172
*/
173-
value[0] = 1;
174-
assert(bpf_map_lookup_elem(fd, &key, value) == 0 && value[0] == 100);
173+
bpf_percpu(value, 0) = 1;
174+
assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
175+
bpf_percpu(value, 0) == 100);
175176

176177
key = 2;
177178
/* Check that key=2 is not found. */
@@ -211,7 +212,7 @@ static void test_hashmap_percpu(int task, void *data)
211212
assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
212213

213214
for (i = 0; i < nr_cpus; i++)
214-
assert(value[i] == i + 100);
215+
assert(bpf_percpu(value, i) == i + 100);
215216

216217
key = next_key;
217218
}
@@ -296,34 +297,36 @@ static void test_arraymap(int task, void *data)
296297
static void test_arraymap_percpu(int task, void *data)
297298
{
298299
unsigned int nr_cpus = bpf_num_possible_cpus();
300+
BPF_DECLARE_PERCPU(long, values);
299301
int key, next_key, fd, i;
300-
long long values[nr_cpus];
301302

302303
fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
303-
sizeof(values[0]), 2, 0);
304+
sizeof(bpf_percpu(values, 0)), 2, 0);
304305
if (fd < 0) {
305306
printf("Failed to create arraymap '%s'!\n", strerror(errno));
306307
exit(1);
307308
}
308309

309310
for (i = 0; i < nr_cpus; i++)
310-
values[i] = i + 100;
311+
bpf_percpu(values, i) = i + 100;
311312

312313
key = 1;
313314
/* Insert key=1 element. */
314315
assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
315316

316-
values[0] = 0;
317+
bpf_percpu(values, 0) = 0;
317318
assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
318319
errno == EEXIST);
319320

320321
/* Check that key=1 can be found. */
321-
assert(bpf_map_lookup_elem(fd, &key, values) == 0 && values[0] == 100);
322+
assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
323+
bpf_percpu(values, 0) == 100);
322324

323325
key = 0;
324326
/* Check that key=0 is also found and zero initialized. */
325327
assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
326-
values[0] == 0 && values[nr_cpus - 1] == 0);
328+
bpf_percpu(values, 0) == 0 &&
329+
bpf_percpu(values, nr_cpus - 1) == 0);
327330

328331
/* Check that key=2 cannot be inserted due to max_entries limit. */
329332
key = 2;
@@ -353,35 +356,35 @@ static void test_arraymap_percpu(int task, void *data)
353356
static void test_arraymap_percpu_many_keys(void)
354357
{
355358
unsigned int nr_cpus = bpf_num_possible_cpus();
359+
BPF_DECLARE_PERCPU(long, values);
356360
/* nr_keys is not too large otherwise the test stresses percpu
357361
* allocator more than anything else
358362
*/
359363
unsigned int nr_keys = 2000;
360-
long long values[nr_cpus];
361364
int key, fd, i;
362365

363366
fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
364-
sizeof(values[0]), nr_keys, 0);
367+
sizeof(bpf_percpu(values, 0)), nr_keys, 0);
365368
if (fd < 0) {
366369
printf("Failed to create per-cpu arraymap '%s'!\n",
367370
strerror(errno));
368371
exit(1);
369372
}
370373

371374
for (i = 0; i < nr_cpus; i++)
372-
values[i] = i + 10;
375+
bpf_percpu(values, i) = i + 10;
373376

374377
for (key = 0; key < nr_keys; key++)
375378
assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
376379

377380
for (key = 0; key < nr_keys; key++) {
378381
for (i = 0; i < nr_cpus; i++)
379-
values[i] = 0;
382+
bpf_percpu(values, i) = 0;
380383

381384
assert(bpf_map_lookup_elem(fd, &key, values) == 0);
382385

383386
for (i = 0; i < nr_cpus; i++)
384-
assert(values[i] == i + 10);
387+
assert(bpf_percpu(values, i) == i + 10);
385388
}
386389

387390
close(fd);

tools/testing/selftests/bpf/test_pkt_access.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include <linux/tcp.h>
1515
#include <linux/pkt_cls.h>
1616
#include "bpf_helpers.h"
17+
#include "bpf_util.h"
1718

18-
#define _htons __builtin_bswap16
1919
#define barrier() __asm__ __volatile__("": : :"memory")
2020
int _version SEC("version") = 1;
2121

@@ -32,15 +32,15 @@ int process(struct __sk_buff *skb)
3232
if (eth + 1 > data_end)
3333
return TC_ACT_SHOT;
3434

35-
if (eth->h_proto == _htons(ETH_P_IP)) {
35+
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
3636
struct iphdr *iph = (struct iphdr *)(eth + 1);
3737

3838
if (iph + 1 > data_end)
3939
return TC_ACT_SHOT;
4040
ihl_len = iph->ihl * 4;
4141
proto = iph->protocol;
4242
tcp = (struct tcphdr *)((void *)(iph) + ihl_len);
43-
} else if (eth->h_proto == _htons(ETH_P_IPV6)) {
43+
} else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
4444
struct ipv6hdr *ip6h = (struct ipv6hdr *)(eth + 1);
4545

4646
if (ip6h + 1 > data_end)

tools/testing/selftests/bpf/test_progs.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ typedef __u16 __sum16;
3030
#include "test_iptunnel_common.h"
3131
#include "bpf_util.h"
3232

33-
#define _htons __builtin_bswap16
34-
3533
static int error_cnt, pass_cnt;
3634

3735
#define MAGIC_BYTES 123
@@ -42,10 +40,10 @@ static struct {
4240
struct iphdr iph;
4341
struct tcphdr tcp;
4442
} __packed pkt_v4 = {
45-
.eth.h_proto = _htons(ETH_P_IP),
43+
.eth.h_proto = bpf_htons(ETH_P_IP),
4644
.iph.ihl = 5,
4745
.iph.protocol = 6,
48-
.iph.tot_len = _htons(MAGIC_BYTES),
46+
.iph.tot_len = bpf_htons(MAGIC_BYTES),
4947
.tcp.urg_ptr = 123,
5048
};
5149

@@ -55,9 +53,9 @@ static struct {
5553
struct ipv6hdr iph;
5654
struct tcphdr tcp;
5755
} __packed pkt_v6 = {
58-
.eth.h_proto = _htons(ETH_P_IPV6),
56+
.eth.h_proto = bpf_htons(ETH_P_IPV6),
5957
.iph.nexthdr = 6,
60-
.iph.payload_len = _htons(MAGIC_BYTES),
58+
.iph.payload_len = bpf_htons(MAGIC_BYTES),
6159
.tcp.urg_ptr = 123,
6260
};
6361

0 commit comments

Comments
 (0)