Skip to content

Commit d5d325e

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Alexei Starovoitov says: ==================== pull-request: bpf 2020-09-15 The following pull-request contains BPF updates for your *net* tree. We've added 12 non-merge commits during the last 19 day(s) which contain a total of 10 files changed, 47 insertions(+), 38 deletions(-). The main changes are: 1) docs/bpf fixes, from Andrii. 2) ld_abs fix, from Daniel. 3) socket casting helpers fix, from Martin. 4) hash iterator fixes, from Yonghong. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 2fbc6e8 + ce880cb commit d5d325e

File tree

10 files changed

+47
-38
lines changed

10 files changed

+47
-38
lines changed

Documentation/bpf/ringbuf.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ in the order of reservations, but only after all previous records where
182182
already committed. It is thus possible for slow producers to temporarily hold
183183
off submitted records, that were reserved later.
184184

185-
Reservation/commit/consumer protocol is verified by litmus tests in
186-
Documentation/litmus_tests/bpf-rb/_.
187-
188185
One interesting implementation bit, that significantly simplifies (and thus
189186
speeds up as well) implementation of both producers and consumers is how data
190187
area is mapped twice contiguously back-to-back in the virtual memory. This
@@ -200,7 +197,7 @@ a self-pacing notifications of new data being availability.
200197
being available after commit only if consumer has already caught up right up to
201198
the record being committed. If not, consumer still has to catch up and thus
202199
will see new data anyways without needing an extra poll notification.
203-
Benchmarks (see tools/testing/selftests/bpf/benchs/bench_ringbuf.c_) show that
200+
Benchmarks (see tools/testing/selftests/bpf/benchs/bench_ringbufs.c) show that
204201
this allows to achieve a very high throughput without having to resort to
205202
tricks like "notify only every Nth sample", which are necessary with perf
206203
buffer. For extreme cases, when BPF program wants more manual control of

kernel/bpf/hashtab.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,6 @@ struct bpf_iter_seq_hash_map_info {
16221622
struct bpf_map *map;
16231623
struct bpf_htab *htab;
16241624
void *percpu_value_buf; // non-zero means percpu hash
1625-
unsigned long flags;
16261625
u32 bucket_id;
16271626
u32 skip_elems;
16281627
};
@@ -1632,7 +1631,6 @@ bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info *info,
16321631
struct htab_elem *prev_elem)
16331632
{
16341633
const struct bpf_htab *htab = info->htab;
1635-
unsigned long flags = info->flags;
16361634
u32 skip_elems = info->skip_elems;
16371635
u32 bucket_id = info->bucket_id;
16381636
struct hlist_nulls_head *head;
@@ -1656,27 +1654,26 @@ bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info *info,
16561654

16571655
/* not found, unlock and go to the next bucket */
16581656
b = &htab->buckets[bucket_id++];
1659-
htab_unlock_bucket(htab, b, flags);
1657+
rcu_read_unlock();
16601658
skip_elems = 0;
16611659
}
16621660

16631661
for (i = bucket_id; i < htab->n_buckets; i++) {
16641662
b = &htab->buckets[i];
1665-
flags = htab_lock_bucket(htab, b);
1663+
rcu_read_lock();
16661664

16671665
count = 0;
16681666
head = &b->head;
16691667
hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
16701668
if (count >= skip_elems) {
1671-
info->flags = flags;
16721669
info->bucket_id = i;
16731670
info->skip_elems = count;
16741671
return elem;
16751672
}
16761673
count++;
16771674
}
16781675

1679-
htab_unlock_bucket(htab, b, flags);
1676+
rcu_read_unlock();
16801677
skip_elems = 0;
16811678
}
16821679

@@ -1754,14 +1751,10 @@ static int bpf_hash_map_seq_show(struct seq_file *seq, void *v)
17541751

17551752
static void bpf_hash_map_seq_stop(struct seq_file *seq, void *v)
17561753
{
1757-
struct bpf_iter_seq_hash_map_info *info = seq->private;
1758-
17591754
if (!v)
17601755
(void)__bpf_hash_map_seq_show(seq, NULL);
17611756
else
1762-
htab_unlock_bucket(info->htab,
1763-
&info->htab->buckets[info->bucket_id],
1764-
info->flags);
1757+
rcu_read_unlock();
17651758
}
17661759

17671760
static int bpf_iter_init_hash_map(void *priv_data,

kernel/bpf/inode.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,12 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
226226
else
227227
prev_key = key;
228228

229+
rcu_read_lock();
229230
if (map->ops->map_get_next_key(map, prev_key, key)) {
230231
map_iter(m)->done = true;
231-
return NULL;
232+
key = NULL;
232233
}
234+
rcu_read_unlock();
233235
return key;
234236
}
235237

net/core/filter.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7066,15 +7066,15 @@ static int bpf_gen_ld_abs(const struct bpf_insn *orig,
70667066
bool indirect = BPF_MODE(orig->code) == BPF_IND;
70677067
struct bpf_insn *insn = insn_buf;
70687068

7069-
/* We're guaranteed here that CTX is in R6. */
7070-
*insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
70717069
if (!indirect) {
70727070
*insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
70737071
} else {
70747072
*insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
70757073
if (orig->imm)
70767074
*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
70777075
}
7076+
/* We're guaranteed here that CTX is in R6. */
7077+
*insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
70787078

70797079
switch (BPF_SIZE(orig->code)) {
70807080
case BPF_B:
@@ -9523,7 +9523,7 @@ BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
95239523
* trigger an explicit type generation here.
95249524
*/
95259525
BTF_TYPE_EMIT(struct tcp6_sock);
9526-
if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
9526+
if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
95279527
sk->sk_family == AF_INET6)
95289528
return (unsigned long)sk;
95299529

@@ -9541,7 +9541,7 @@ const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
95419541

95429542
BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
95439543
{
9544-
if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
9544+
if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
95459545
return (unsigned long)sk;
95469546

95479547
return (unsigned long)NULL;
@@ -9559,12 +9559,12 @@ const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
95599559
BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
95609560
{
95619561
#ifdef CONFIG_INET
9562-
if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
9562+
if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
95639563
return (unsigned long)sk;
95649564
#endif
95659565

95669566
#if IS_BUILTIN(CONFIG_IPV6)
9567-
if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
9567+
if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
95689568
return (unsigned long)sk;
95699569
#endif
95709570

@@ -9583,12 +9583,12 @@ const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
95839583
BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
95849584
{
95859585
#ifdef CONFIG_INET
9586-
if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
9586+
if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
95879587
return (unsigned long)sk;
95889588
#endif
95899589

95909590
#if IS_BUILTIN(CONFIG_IPV6)
9591-
if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
9591+
if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
95929592
return (unsigned long)sk;
95939593
#endif
95949594

@@ -9610,7 +9610,7 @@ BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
96109610
* trigger an explicit type generation here.
96119611
*/
96129612
BTF_TYPE_EMIT(struct udp6_sock);
9613-
if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
9613+
if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
96149614
sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
96159615
return (unsigned long)sk;
96169616

net/xdp/xdp_umem.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,10 @@ static int xdp_umem_account_pages(struct xdp_umem *umem)
303303

304304
static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
305305
{
306+
u32 npgs_rem, chunk_size = mr->chunk_size, headroom = mr->headroom;
306307
bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
307-
u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
308308
u64 npgs, addr = mr->addr, size = mr->len;
309-
unsigned int chunks, chunks_per_page;
309+
unsigned int chunks, chunks_rem;
310310
int err;
311311

312312
if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) {
@@ -336,19 +336,18 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
336336
if ((addr + size) < addr)
337337
return -EINVAL;
338338

339-
npgs = size >> PAGE_SHIFT;
339+
npgs = div_u64_rem(size, PAGE_SIZE, &npgs_rem);
340+
if (npgs_rem)
341+
npgs++;
340342
if (npgs > U32_MAX)
341343
return -EINVAL;
342344

343-
chunks = (unsigned int)div_u64(size, chunk_size);
345+
chunks = (unsigned int)div_u64_rem(size, chunk_size, &chunks_rem);
344346
if (chunks == 0)
345347
return -EINVAL;
346348

347-
if (!unaligned_chunks) {
348-
chunks_per_page = PAGE_SIZE / chunk_size;
349-
if (chunks < chunks_per_page || chunks % chunks_per_page)
350-
return -EINVAL;
351-
}
349+
if (!unaligned_chunks && chunks_rem)
350+
return -EINVAL;
352351

353352
if (headroom >= chunk_size - XDP_PACKET_HEADROOM)
354353
return -EINVAL;

tools/bpf/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ FEATURE_TESTS = libbfd disassembler-four-args
3838
FEATURE_DISPLAY = libbfd disassembler-four-args
3939

4040
check_feat := 1
41-
NON_CHECK_FEAT_TARGETS := clean bpftool_clean runqslower_clean
41+
NON_CHECK_FEAT_TARGETS := clean bpftool_clean runqslower_clean resolve_btfids_clean
4242
ifdef MAKECMDGOALS
4343
ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
4444
check_feat := 0
@@ -89,7 +89,7 @@ $(OUTPUT)bpf_exp.lex.c: $(OUTPUT)bpf_exp.yacc.c
8989
$(OUTPUT)bpf_exp.yacc.o: $(OUTPUT)bpf_exp.yacc.c
9090
$(OUTPUT)bpf_exp.lex.o: $(OUTPUT)bpf_exp.lex.c
9191

92-
clean: bpftool_clean runqslower_clean
92+
clean: bpftool_clean runqslower_clean resolve_btfids_clean
9393
$(call QUIET_CLEAN, bpf-progs)
9494
$(Q)$(RM) -r -- $(OUTPUT)*.o $(OUTPUT)bpf_jit_disasm $(OUTPUT)bpf_dbg \
9595
$(OUTPUT)bpf_asm $(OUTPUT)bpf_exp.yacc.* $(OUTPUT)bpf_exp.lex.*

tools/bpf/resolve_btfids/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ libbpf-clean:
8080
clean: libsubcmd-clean libbpf-clean fixdep-clean
8181
$(call msg,CLEAN,$(BINARY))
8282
$(Q)$(RM) -f $(BINARY); \
83+
$(RM) -rf $(if $(OUTPUT),$(OUTPUT),.)/feature; \
8384
find $(if $(OUTPUT),$(OUTPUT),.) -name \*.o -or -name \*.o.cmd -or -name \*.o.d | xargs $(RM)
8485

8586
tags:

tools/lib/bpf/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ FEATURE_USER = .libbpf
5959
FEATURE_TESTS = libelf libelf-mmap zlib bpf reallocarray
6060
FEATURE_DISPLAY = libelf zlib bpf
6161

62-
INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi
62+
INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/include/uapi
6363
FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES)
6464

6565
check_feat := 1
@@ -152,6 +152,7 @@ GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN_SHARED) | \
152152
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}' | \
153153
sort -u | wc -l)
154154
VERSIONED_SYM_COUNT = $(shell readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \
155+
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}' | \
155156
grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l)
156157

157158
CMD_TARGETS = $(LIB_TARGET) $(PC_FILE)
@@ -219,6 +220,7 @@ check_abi: $(OUTPUT)libbpf.so
219220
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}'| \
220221
sort -u > $(OUTPUT)libbpf_global_syms.tmp; \
221222
readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \
223+
awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}'| \
222224
grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | \
223225
sort -u > $(OUTPUT)libbpf_versioned_syms.tmp; \
224226
diff -u $(OUTPUT)libbpf_global_syms.tmp \

tools/lib/bpf/libbpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5203,8 +5203,8 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,
52035203
int i, j, nrels, new_sz;
52045204
const struct btf_var_secinfo *vi = NULL;
52055205
const struct btf_type *sec, *var, *def;
5206+
struct bpf_map *map = NULL, *targ_map;
52065207
const struct btf_member *member;
5207-
struct bpf_map *map, *targ_map;
52085208
const char *name, *mname;
52095209
Elf_Data *symbols;
52105210
unsigned int moff;

tools/testing/selftests/bpf/progs/bpf_iter_bpf_hash_map.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ int dump_bpf_hash_map(struct bpf_iter__bpf_map_elem *ctx)
4747
__u32 seq_num = ctx->meta->seq_num;
4848
struct bpf_map *map = ctx->map;
4949
struct key_t *key = ctx->key;
50+
struct key_t tmp_key;
5051
__u64 *val = ctx->value;
52+
__u64 tmp_val = 0;
53+
int ret;
5154

5255
if (in_test_mode) {
5356
/* test mode is used by selftests to
@@ -61,6 +64,18 @@ int dump_bpf_hash_map(struct bpf_iter__bpf_map_elem *ctx)
6164
if (key == (void *)0 || val == (void *)0)
6265
return 0;
6366

67+
/* update the value and then delete the <key, value> pair.
68+
* it should not impact the existing 'val' which is still
69+
* accessible under rcu.
70+
*/
71+
__builtin_memcpy(&tmp_key, key, sizeof(struct key_t));
72+
ret = bpf_map_update_elem(&hashmap1, &tmp_key, &tmp_val, 0);
73+
if (ret)
74+
return 0;
75+
ret = bpf_map_delete_elem(&hashmap1, &tmp_key);
76+
if (ret)
77+
return 0;
78+
6479
key_sum_a += key->a;
6580
key_sum_b += key->b;
6681
key_sum_c += key->c;

0 commit comments

Comments
 (0)