Skip to content

Commit f8e1100

Browse files
committed
Merge branch 'bpf-updates'
Daniel Borkmann says: ==================== BPF updates Some minor updates to {cls,act}_bpf to retrieve routing realms and to make skb->priority writable. Thanks! v1 -> v2: - Dropped preclassify patch for now from the series as the rest is pretty much independent of it - Rest unchanged, only rebased and already posted Acked-by's kept ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents bd8762b + 754f1e6 commit f8e1100

File tree

13 files changed

+63
-16
lines changed

13 files changed

+63
-16
lines changed

arch/arm/net/bpf_jit_32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ void bpf_jit_compile(struct bpf_prog *fp)
10471047

10481048
set_memory_ro((unsigned long)header, header->pages);
10491049
fp->bpf_func = (void *)ctx.target;
1050-
fp->jited = true;
1050+
fp->jited = 1;
10511051
out:
10521052
kfree(ctx.offsets);
10531053
return;

arch/arm64/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ void bpf_int_jit_compile(struct bpf_prog *prog)
744744

745745
set_memory_ro((unsigned long)header, header->pages);
746746
prog->bpf_func = (void *)ctx.image;
747-
prog->jited = true;
747+
prog->jited = 1;
748748
out:
749749
kfree(ctx.offset);
750750
}

arch/mips/net/bpf_jit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ void bpf_jit_compile(struct bpf_prog *fp)
12511251
bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
12521252

12531253
fp->bpf_func = (void *)ctx.target;
1254-
fp->jited = true;
1254+
fp->jited = 1;
12551255

12561256
out:
12571257
kfree(ctx.offsets);

arch/powerpc/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ void bpf_jit_compile(struct bpf_prog *fp)
679679
((u64 *)image)[1] = local_paca->kernel_toc;
680680
#endif
681681
fp->bpf_func = (void *)image;
682-
fp->jited = true;
682+
fp->jited = 1;
683683
}
684684
out:
685685
kfree(addrs);

arch/s390/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ void bpf_int_jit_compile(struct bpf_prog *fp)
13101310
if (jit.prg_buf) {
13111311
set_memory_ro((unsigned long)header, header->pages);
13121312
fp->bpf_func = (void *) jit.prg_buf;
1313-
fp->jited = true;
1313+
fp->jited = 1;
13141314
}
13151315
free_addrs:
13161316
kfree(jit.addrs);

arch/sparc/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf];
812812
if (image) {
813813
bpf_flush_icache(image, image + proglen);
814814
fp->bpf_func = (void *)image;
815-
fp->jited = true;
815+
fp->jited = 1;
816816
}
817817
out:
818818
kfree(addrs);

arch/x86/net/bpf_jit_comp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ void bpf_int_jit_compile(struct bpf_prog *prog)
11091109
bpf_flush_icache(header, image + proglen);
11101110
set_memory_ro((unsigned long)header, header->pages);
11111111
prog->bpf_func = (void *)image;
1112-
prog->jited = true;
1112+
prog->jited = 1;
11131113
}
11141114
out:
11151115
kfree(addrs);

include/linux/filter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,11 @@ struct bpf_binary_header {
326326

327327
struct bpf_prog {
328328
u16 pages; /* Number of allocated pages */
329-
bool jited; /* Is our filter JIT'ed? */
330-
bool gpl_compatible; /* Is our filter GPL compatible? */
329+
kmemcheck_bitfield_begin(meta);
330+
u16 jited:1, /* Is our filter JIT'ed? */
331+
gpl_compatible:1, /* Is filter GPL compatible? */
332+
dst_needed:1; /* Do we need dst entry? */
333+
kmemcheck_bitfield_end(meta);
331334
u32 len; /* Number of filter blocks */
332335
enum bpf_prog_type type; /* Type of BPF program */
333336
struct bpf_prog_aux *aux; /* Auxiliary fields */

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ enum bpf_func_id {
280280
* Return: TC_ACT_REDIRECT
281281
*/
282282
BPF_FUNC_redirect,
283+
284+
/**
285+
* bpf_get_route_realm(skb) - retrieve a dst's tclassid
286+
* @skb: pointer to skb
287+
* Return: realm if != 0
288+
*/
289+
BPF_FUNC_get_route_realm,
283290
__BPF_FUNC_MAX_ID,
284291
};
285292

kernel/bpf/core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
8282
if (fp == NULL)
8383
return NULL;
8484

85+
kmemcheck_annotate_bitfield(fp, meta);
86+
8587
aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
8688
if (aux == NULL) {
8789
vfree(fp);
@@ -110,6 +112,8 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
110112

111113
fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);
112114
if (fp != NULL) {
115+
kmemcheck_annotate_bitfield(fp, meta);
116+
113117
memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
114118
fp->pages = size / PAGE_SIZE;
115119

kernel/bpf/syscall.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ static void fixup_bpf_calls(struct bpf_prog *prog)
402402
*/
403403
BUG_ON(!prog->aux->ops->get_func_proto);
404404

405+
if (insn->imm == BPF_FUNC_get_route_realm)
406+
prog->dst_needed = 1;
405407
if (insn->imm == BPF_FUNC_tail_call) {
406408
/* mark bpf_tail_call as different opcode
407409
* to avoid conditional branch in
@@ -553,10 +555,10 @@ static int bpf_prog_load(union bpf_attr *attr)
553555
goto free_prog;
554556

555557
prog->orig_prog = NULL;
556-
prog->jited = false;
558+
prog->jited = 0;
557559

558560
atomic_set(&prog->aux->refcnt, 1);
559-
prog->gpl_compatible = is_gpl;
561+
prog->gpl_compatible = is_gpl ? 1 : 0;
560562

561563
/* find program type: socket_filter vs tracing_filter */
562564
err = find_prog_type(type, prog);

net/core/filter.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <net/sch_generic.h>
5050
#include <net/cls_cgroup.h>
5151
#include <net/dst_metadata.h>
52+
#include <net/dst.h>
5253

5354
/**
5455
* sk_filter - run a packet through a socket filter
@@ -1001,7 +1002,7 @@ static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
10011002
int err;
10021003

10031004
fp->bpf_func = NULL;
1004-
fp->jited = false;
1005+
fp->jited = 0;
10051006

10061007
err = bpf_check_classic(fp->insns, fp->len);
10071008
if (err) {
@@ -1478,6 +1479,25 @@ static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
14781479
.arg1_type = ARG_PTR_TO_CTX,
14791480
};
14801481

1482+
static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1483+
{
1484+
#ifdef CONFIG_IP_ROUTE_CLASSID
1485+
const struct dst_entry *dst;
1486+
1487+
dst = skb_dst((struct sk_buff *) (unsigned long) r1);
1488+
if (dst)
1489+
return dst->tclassid;
1490+
#endif
1491+
return 0;
1492+
}
1493+
1494+
static const struct bpf_func_proto bpf_get_route_realm_proto = {
1495+
.func = bpf_get_route_realm,
1496+
.gpl_only = false,
1497+
.ret_type = RET_INTEGER,
1498+
.arg1_type = ARG_PTR_TO_CTX,
1499+
};
1500+
14811501
static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
14821502
{
14831503
struct sk_buff *skb = (struct sk_buff *) (long) r1;
@@ -1648,6 +1668,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)
16481668
return bpf_get_skb_set_tunnel_key_proto();
16491669
case BPF_FUNC_redirect:
16501670
return &bpf_redirect_proto;
1671+
case BPF_FUNC_get_route_realm:
1672+
return &bpf_get_route_realm_proto;
16511673
default:
16521674
return sk_filter_func_proto(func_id);
16531675
}
@@ -1699,6 +1721,7 @@ static bool tc_cls_act_is_valid_access(int off, int size,
16991721
switch (off) {
17001722
case offsetof(struct __sk_buff, mark):
17011723
case offsetof(struct __sk_buff, tc_index):
1724+
case offsetof(struct __sk_buff, priority):
17021725
case offsetof(struct __sk_buff, cb[0]) ...
17031726
offsetof(struct __sk_buff, cb[4]):
17041727
break;
@@ -1740,8 +1763,12 @@ static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
17401763
case offsetof(struct __sk_buff, priority):
17411764
BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
17421765

1743-
*insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1744-
offsetof(struct sk_buff, priority));
1766+
if (type == BPF_WRITE)
1767+
*insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
1768+
offsetof(struct sk_buff, priority));
1769+
else
1770+
*insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1771+
offsetof(struct sk_buff, priority));
17451772
break;
17461773

17471774
case offsetof(struct __sk_buff, ingress_ifindex):

net/sched/cls_bpf.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
262262
return 0;
263263
}
264264

265-
static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog)
265+
static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
266+
const struct tcf_proto *tp)
266267
{
267268
struct bpf_prog *fp;
268269
char *name = NULL;
@@ -294,6 +295,9 @@ static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog)
294295
prog->bpf_name = name;
295296
prog->filter = fp;
296297

298+
if (fp->dst_needed)
299+
netif_keep_dst(qdisc_dev(tp->q));
300+
297301
return 0;
298302
}
299303

@@ -330,7 +334,7 @@ static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
330334
prog->exts_integrated = have_exts;
331335

332336
ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
333-
cls_bpf_prog_from_efd(tb, prog);
337+
cls_bpf_prog_from_efd(tb, prog, tp);
334338
if (ret < 0) {
335339
tcf_exts_destroy(&exts);
336340
return ret;

0 commit comments

Comments
 (0)