Skip to content

Commit d00f26b

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Alexei Starovoitov says: ==================== pull-request: bpf-next 2020-05-14 The following pull-request contains BPF updates for your *net-next* tree. The main changes are: 1) Merged tag 'perf-for-bpf-2020-05-06' from tip tree that includes CAP_PERFMON. 2) support for narrow loads in bpf_sock_addr progs and additional helpers in cg-skb progs, from Andrey. 3) bpf benchmark runner, from Andrii. 4) arm and riscv JIT optimizations, from Luke. 5) bpf iterator infrastructure, from Yonghong. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 9b65d2f + b92d44b commit d00f26b

File tree

139 files changed

+5253
-544
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+5253
-544
lines changed

arch/arm/net/bpf_jit_32.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,9 @@ static inline void emit_a32_alu_i(const s8 dst, const u32 val,
795795
case BPF_RSH:
796796
emit(ARM_LSR_I(rd, rd, val), ctx);
797797
break;
798+
case BPF_ARSH:
799+
emit(ARM_ASR_I(rd, rd, val), ctx);
800+
break;
798801
case BPF_NEG:
799802
emit(ARM_RSB_I(rd, rd, val), ctx);
800803
break;
@@ -860,8 +863,8 @@ static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[],
860863
emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
861864
emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
862865
emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
863-
_emit(ARM_COND_MI, ARM_B(0), ctx);
864-
emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx);
866+
_emit(ARM_COND_PL,
867+
ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx);
865868
emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx);
866869

867870
arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
@@ -1408,7 +1411,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
14081411
case BPF_ALU | BPF_MUL | BPF_X:
14091412
case BPF_ALU | BPF_LSH | BPF_X:
14101413
case BPF_ALU | BPF_RSH | BPF_X:
1411-
case BPF_ALU | BPF_ARSH | BPF_K:
14121414
case BPF_ALU | BPF_ARSH | BPF_X:
14131415
case BPF_ALU64 | BPF_ADD | BPF_K:
14141416
case BPF_ALU64 | BPF_ADD | BPF_X:
@@ -1465,10 +1467,12 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
14651467
case BPF_ALU64 | BPF_MOD | BPF_K:
14661468
case BPF_ALU64 | BPF_MOD | BPF_X:
14671469
goto notyet;
1468-
/* dst = dst >> imm */
14691470
/* dst = dst << imm */
1470-
case BPF_ALU | BPF_RSH | BPF_K:
1471+
/* dst = dst >> imm */
1472+
/* dst = dst >> imm (signed) */
14711473
case BPF_ALU | BPF_LSH | BPF_K:
1474+
case BPF_ALU | BPF_RSH | BPF_K:
1475+
case BPF_ALU | BPF_ARSH | BPF_K:
14721476
if (unlikely(imm > 31))
14731477
return -EINVAL;
14741478
if (imm)

arch/arm/net/bpf_jit_32.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
#define ARM_INST_LSR_I 0x01a00020
9595
#define ARM_INST_LSR_R 0x01a00030
9696

97+
#define ARM_INST_ASR_I 0x01a00040
98+
#define ARM_INST_ASR_R 0x01a00050
99+
97100
#define ARM_INST_MOV_R 0x01a00000
98101
#define ARM_INST_MOVS_R 0x01b00000
99102
#define ARM_INST_MOV_I 0x03a00000

arch/riscv/net/bpf_jit_comp64.c

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
515515
case BPF_ALU | BPF_LSH | BPF_X:
516516
case BPF_ALU64 | BPF_LSH | BPF_X:
517517
emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx);
518-
if (!is64)
518+
if (!is64 && !aux->verifier_zext)
519519
emit_zext_32(rd, ctx);
520520
break;
521521
case BPF_ALU | BPF_RSH | BPF_X:
@@ -542,13 +542,21 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
542542

543543
/* dst = BSWAP##imm(dst) */
544544
case BPF_ALU | BPF_END | BPF_FROM_LE:
545-
{
546-
int shift = 64 - imm;
547-
548-
emit(rv_slli(rd, rd, shift), ctx);
549-
emit(rv_srli(rd, rd, shift), ctx);
545+
switch (imm) {
546+
case 16:
547+
emit(rv_slli(rd, rd, 48), ctx);
548+
emit(rv_srli(rd, rd, 48), ctx);
549+
break;
550+
case 32:
551+
if (!aux->verifier_zext)
552+
emit_zext_32(rd, ctx);
553+
break;
554+
case 64:
555+
/* Do nothing */
556+
break;
557+
}
550558
break;
551-
}
559+
552560
case BPF_ALU | BPF_END | BPF_FROM_BE:
553561
emit(rv_addi(RV_REG_T2, RV_REG_ZERO, 0), ctx);
554562

@@ -692,19 +700,19 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
692700
case BPF_ALU | BPF_LSH | BPF_K:
693701
case BPF_ALU64 | BPF_LSH | BPF_K:
694702
emit(is64 ? rv_slli(rd, rd, imm) : rv_slliw(rd, rd, imm), ctx);
695-
if (!is64)
703+
if (!is64 && !aux->verifier_zext)
696704
emit_zext_32(rd, ctx);
697705
break;
698706
case BPF_ALU | BPF_RSH | BPF_K:
699707
case BPF_ALU64 | BPF_RSH | BPF_K:
700708
emit(is64 ? rv_srli(rd, rd, imm) : rv_srliw(rd, rd, imm), ctx);
701-
if (!is64)
709+
if (!is64 && !aux->verifier_zext)
702710
emit_zext_32(rd, ctx);
703711
break;
704712
case BPF_ALU | BPF_ARSH | BPF_K:
705713
case BPF_ALU64 | BPF_ARSH | BPF_K:
706714
emit(is64 ? rv_srai(rd, rd, imm) : rv_sraiw(rd, rd, imm), ctx);
707-
if (!is64)
715+
if (!is64 && !aux->verifier_zext)
708716
emit_zext_32(rd, ctx);
709717
break;
710718

@@ -784,11 +792,15 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
784792
case BPF_JMP32 | BPF_JSGE | BPF_K:
785793
case BPF_JMP | BPF_JSLE | BPF_K:
786794
case BPF_JMP32 | BPF_JSLE | BPF_K:
787-
case BPF_JMP | BPF_JSET | BPF_K:
788-
case BPF_JMP32 | BPF_JSET | BPF_K:
789795
rvoff = rv_offset(i, off, ctx);
790796
s = ctx->ninsns;
791-
emit_imm(RV_REG_T1, imm, ctx);
797+
if (imm) {
798+
emit_imm(RV_REG_T1, imm, ctx);
799+
rs = RV_REG_T1;
800+
} else {
801+
/* If imm is 0, simply use zero register. */
802+
rs = RV_REG_ZERO;
803+
}
792804
if (!is64) {
793805
if (is_signed_bpf_cond(BPF_OP(code)))
794806
emit_sext_32_rd(&rd, ctx);
@@ -799,16 +811,28 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
799811

800812
/* Adjust for extra insns */
801813
rvoff -= (e - s) << 2;
814+
emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
815+
break;
802816

803-
if (BPF_OP(code) == BPF_JSET) {
804-
/* Adjust for and */
805-
rvoff -= 4;
806-
emit(rv_and(RV_REG_T1, rd, RV_REG_T1), ctx);
807-
emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff,
808-
ctx);
817+
case BPF_JMP | BPF_JSET | BPF_K:
818+
case BPF_JMP32 | BPF_JSET | BPF_K:
819+
rvoff = rv_offset(i, off, ctx);
820+
s = ctx->ninsns;
821+
if (is_12b_int(imm)) {
822+
emit(rv_andi(RV_REG_T1, rd, imm), ctx);
809823
} else {
810-
emit_branch(BPF_OP(code), rd, RV_REG_T1, rvoff, ctx);
824+
emit_imm(RV_REG_T1, imm, ctx);
825+
emit(rv_and(RV_REG_T1, rd, RV_REG_T1), ctx);
811826
}
827+
/* For jset32, we should clear the upper 32 bits of t1, but
828+
* sign-extension is sufficient here and saves one instruction,
829+
* as t1 is used only in comparison against zero.
830+
*/
831+
if (!is64 && imm < 0)
832+
emit(rv_addiw(RV_REG_T1, RV_REG_T1, 0), ctx);
833+
e = ctx->ninsns;
834+
rvoff -= (e - s) << 2;
835+
emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
812836
break;
813837

814838
/* function call */

arch/x86/net/bpf_jit_comp32.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,8 +1475,8 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
14751475
for (i = 0; i < insn_cnt; i++, insn++) {
14761476
const s32 imm32 = insn->imm;
14771477
const bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
1478-
const bool dstk = insn->dst_reg == BPF_REG_AX ? false : true;
1479-
const bool sstk = insn->src_reg == BPF_REG_AX ? false : true;
1478+
const bool dstk = insn->dst_reg != BPF_REG_AX;
1479+
const bool sstk = insn->src_reg != BPF_REG_AX;
14801480
const u8 code = insn->code;
14811481
const u8 *dst = bpf2ia32[insn->dst_reg];
14821482
const u8 *src = bpf2ia32[insn->src_reg];

fs/proc/proc_net.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ static const struct proc_ops proc_net_seq_ops = {
9898
.proc_release = seq_release_net,
9999
};
100100

101+
int bpf_iter_init_seq_net(void *priv_data)
102+
{
103+
#ifdef CONFIG_NET_NS
104+
struct seq_net_private *p = priv_data;
105+
106+
p->net = get_net(current->nsproxy->net_ns);
107+
#endif
108+
return 0;
109+
}
110+
111+
void bpf_iter_fini_seq_net(void *priv_data)
112+
{
113+
#ifdef CONFIG_NET_NS
114+
struct seq_net_private *p = priv_data;
115+
116+
put_net(p->net);
117+
#endif
118+
}
119+
101120
struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
102121
struct proc_dir_entry *parent, const struct seq_operations *ops,
103122
unsigned int state_size, void *data)

include/linux/bpf.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct seq_file;
3131
struct btf;
3232
struct btf_type;
3333
struct exception_table_entry;
34+
struct seq_operations;
3435

3536
extern struct idr btf_idr;
3637
extern spinlock_t btf_idr_lock;
@@ -319,6 +320,7 @@ enum bpf_reg_type {
319320
PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
320321
PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
321322
PTR_TO_BTF_ID, /* reg points to kernel struct */
323+
PTR_TO_BTF_ID_OR_NULL, /* reg points to kernel struct or NULL */
322324
};
323325

324326
/* The information passed from prog-specific *_is_valid_access
@@ -641,6 +643,12 @@ struct bpf_jit_poke_descriptor {
641643
u16 reason;
642644
};
643645

646+
/* reg_type info for ctx arguments */
647+
struct bpf_ctx_arg_aux {
648+
u32 offset;
649+
enum bpf_reg_type reg_type;
650+
};
651+
644652
struct bpf_prog_aux {
645653
atomic64_t refcnt;
646654
u32 used_map_cnt;
@@ -652,6 +660,8 @@ struct bpf_prog_aux {
652660
u32 func_cnt; /* used by non-func prog as the number of func progs */
653661
u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
654662
u32 attach_btf_id; /* in-kernel BTF type id to attach to */
663+
u32 ctx_arg_info_size;
664+
const struct bpf_ctx_arg_aux *ctx_arg_info;
655665
struct bpf_prog *linked_prog;
656666
bool verifier_zext; /* Zero extensions has been inserted by verifier. */
657667
bool offload_requested;
@@ -1021,6 +1031,7 @@ static inline void bpf_enable_instrumentation(void)
10211031

10221032
extern const struct file_operations bpf_map_fops;
10231033
extern const struct file_operations bpf_prog_fops;
1034+
extern const struct file_operations bpf_iter_fops;
10241035

10251036
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
10261037
extern const struct bpf_prog_ops _name ## _prog_ops; \
@@ -1080,6 +1091,7 @@ int generic_map_update_batch(struct bpf_map *map,
10801091
int generic_map_delete_batch(struct bpf_map *map,
10811092
const union bpf_attr *attr,
10821093
union bpf_attr __user *uattr);
1094+
struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
10831095

10841096
extern int sysctl_unprivileged_bpf_disabled;
10851097

@@ -1126,6 +1138,40 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd);
11261138
int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
11271139
int bpf_obj_get_user(const char __user *pathname, int flags);
11281140

1141+
#define BPF_ITER_FUNC_PREFIX "bpf_iter_"
1142+
#define DEFINE_BPF_ITER_FUNC(target, args...) \
1143+
extern int bpf_iter_ ## target(args); \
1144+
int __init bpf_iter_ ## target(args) { return 0; }
1145+
1146+
typedef int (*bpf_iter_init_seq_priv_t)(void *private_data);
1147+
typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
1148+
1149+
#define BPF_ITER_CTX_ARG_MAX 2
1150+
struct bpf_iter_reg {
1151+
const char *target;
1152+
const struct seq_operations *seq_ops;
1153+
bpf_iter_init_seq_priv_t init_seq_private;
1154+
bpf_iter_fini_seq_priv_t fini_seq_private;
1155+
u32 seq_priv_size;
1156+
u32 ctx_arg_info_size;
1157+
struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
1158+
};
1159+
1160+
struct bpf_iter_meta {
1161+
__bpf_md_ptr(struct seq_file *, seq);
1162+
u64 session_id;
1163+
u64 seq_num;
1164+
};
1165+
1166+
int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
1167+
void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
1168+
bool bpf_iter_prog_supported(struct bpf_prog *prog);
1169+
int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
1170+
int bpf_iter_new_fd(struct bpf_link *link);
1171+
bool bpf_link_is_iter(struct bpf_link *link);
1172+
struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1173+
int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
1174+
11291175
int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
11301176
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
11311177
int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,

include/linux/bpf_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,4 @@ BPF_LINK_TYPE(BPF_LINK_TYPE_TRACING, tracing)
124124
#ifdef CONFIG_CGROUP_BPF
125125
BPF_LINK_TYPE(BPF_LINK_TYPE_CGROUP, cgroup)
126126
#endif
127+
BPF_LINK_TYPE(BPF_LINK_TYPE_ITER, iter)

include/linux/capability.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ extern bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct
251251
extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
252252
extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
253253
extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
254+
static inline bool perfmon_capable(void)
255+
{
256+
return capable(CAP_PERFMON) || capable(CAP_SYS_ADMIN);
257+
}
254258

255259
/* audit system wants to get cap info from files as well */
256260
extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);

include/linux/filter.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,8 @@ struct bpf_prog {
545545
unsigned int (*bpf_func)(const void *ctx,
546546
const struct bpf_insn *insn);
547547
/* Instructions for interpreter */
548-
union {
549-
struct sock_filter insns[0];
550-
struct bpf_insn insnsi[0];
551-
};
548+
struct sock_filter insns[0];
549+
struct bpf_insn insnsi[];
552550
};
553551

554552
struct sk_filter {

include/linux/proc_fs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mo
105105
void *data);
106106
extern struct pid *tgid_pidfd_to_pid(const struct file *file);
107107

108+
extern int bpf_iter_init_seq_net(void *priv_data);
109+
extern void bpf_iter_fini_seq_net(void *priv_data);
110+
108111
#ifdef CONFIG_PROC_PID_ARCH_STATUS
109112
/*
110113
* The architecture which selects CONFIG_PROC_PID_ARCH_STATUS must

include/net/inet_common.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ int inet_shutdown(struct socket *sock, int how);
3535
int inet_listen(struct socket *sock, int backlog);
3636
void inet_sock_destruct(struct sock *sk);
3737
int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len);
38+
/* Don't allocate port at this moment, defer to connect. */
39+
#define BIND_FORCE_ADDRESS_NO_PORT (1 << 0)
40+
/* Grab and release socket lock. */
41+
#define BIND_WITH_LOCK (1 << 1)
42+
/* Called from BPF program. */
43+
#define BIND_FROM_BPF (1 << 2)
3844
int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
39-
bool force_bind_address_no_port, bool with_lock);
45+
u32 flags);
4046
int inet_getname(struct socket *sock, struct sockaddr *uaddr,
4147
int peer);
4248
int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);

include/net/ip6_fib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,13 @@ static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
544544
return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
545545
}
546546

547+
#if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
548+
struct bpf_iter__ipv6_route {
549+
__bpf_md_ptr(struct bpf_iter_meta *, meta);
550+
__bpf_md_ptr(struct fib6_info *, rt);
551+
};
552+
#endif
553+
547554
#ifdef CONFIG_IPV6_MULTIPLE_TABLES
548555
static inline bool fib6_has_custom_rules(const struct net *net)
549556
{

include/net/ipv6_stubs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extern const struct ipv6_stub *ipv6_stub __read_mostly;
6363
/* A stub used by bpf helpers. Similarly ugly as ipv6_stub */
6464
struct ipv6_bpf_stub {
6565
int (*inet6_bind)(struct sock *sk, struct sockaddr *uaddr, int addr_len,
66-
bool force_bind_address_no_port, bool with_lock);
66+
u32 flags);
6767
struct sock *(*udp6_lib_lookup)(struct net *net,
6868
const struct in6_addr *saddr, __be16 sport,
6969
const struct in6_addr *daddr, __be16 dport,

include/net/xdp_sock.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ struct xdp_umem {
5050
u32 headroom;
5151
u32 chunk_size_nohr;
5252
struct user_struct *user;
53-
unsigned long address;
5453
refcount_t users;
5554
struct work_struct work;
5655
struct page **pgs;
@@ -62,8 +61,8 @@ struct xdp_umem {
6261
struct net_device *dev;
6362
struct xdp_umem_fq_reuse *fq_reuse;
6463
bool zc;
65-
spinlock_t xsk_list_lock;
66-
struct list_head xsk_list;
64+
spinlock_t xsk_tx_list_lock;
65+
struct list_head xsk_tx_list;
6766
};
6867

6968
/* Nodes are linked in the struct xdp_sock map_list field, and used to

0 commit comments

Comments
 (0)