Skip to content

Commit 1eb4169

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Alexei Starovoitov says: ==================== pull-request: bpf 2019-06-15 The following pull-request contains BPF updates for your *net* tree. The main changes are: 1) fix stack layout of JITed x64 bpf code, from Alexei. 2) fix out of bounds memory access in bpf_sk_storage, from Arthur. 3) fix lpm trie walk, from Jonathan. 4) fix nested bpf_perf_event_output, from Matt. 5) and several other fixes. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 5db2e7c + 9594dc3 commit 1eb4169

File tree

14 files changed

+188
-95
lines changed

14 files changed

+188
-95
lines changed

arch/powerpc/include/asm/ppc-opcode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@
338338
#define PPC_INST_MADDLD 0x10000033
339339
#define PPC_INST_DIVWU 0x7c000396
340340
#define PPC_INST_DIVD 0x7c0003d2
341+
#define PPC_INST_DIVDU 0x7c000392
341342
#define PPC_INST_RLWINM 0x54000000
342343
#define PPC_INST_RLWINM_DOT 0x54000001
343344
#define PPC_INST_RLWIMI 0x50000000

arch/powerpc/net/bpf_jit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
___PPC_RA(a) | IMM_L(i))
117117
#define PPC_DIVWU(d, a, b) EMIT(PPC_INST_DIVWU | ___PPC_RT(d) | \
118118
___PPC_RA(a) | ___PPC_RB(b))
119-
#define PPC_DIVD(d, a, b) EMIT(PPC_INST_DIVD | ___PPC_RT(d) | \
119+
#define PPC_DIVDU(d, a, b) EMIT(PPC_INST_DIVDU | ___PPC_RT(d) | \
120120
___PPC_RA(a) | ___PPC_RB(b))
121121
#define PPC_AND(d, a, b) EMIT(PPC_INST_AND | ___PPC_RA(d) | \
122122
___PPC_RS(a) | ___PPC_RB(b))

arch/powerpc/net/bpf_jit_comp64.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,12 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
399399
case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
400400
case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
401401
if (BPF_OP(code) == BPF_MOD) {
402-
PPC_DIVD(b2p[TMP_REG_1], dst_reg, src_reg);
402+
PPC_DIVDU(b2p[TMP_REG_1], dst_reg, src_reg);
403403
PPC_MULD(b2p[TMP_REG_1], src_reg,
404404
b2p[TMP_REG_1]);
405405
PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
406406
} else
407-
PPC_DIVD(dst_reg, dst_reg, src_reg);
407+
PPC_DIVDU(dst_reg, dst_reg, src_reg);
408408
break;
409409
case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
410410
case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
@@ -432,15 +432,15 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
432432
break;
433433
case BPF_ALU64:
434434
if (BPF_OP(code) == BPF_MOD) {
435-
PPC_DIVD(b2p[TMP_REG_2], dst_reg,
435+
PPC_DIVDU(b2p[TMP_REG_2], dst_reg,
436436
b2p[TMP_REG_1]);
437437
PPC_MULD(b2p[TMP_REG_1],
438438
b2p[TMP_REG_1],
439439
b2p[TMP_REG_2]);
440440
PPC_SUB(dst_reg, dst_reg,
441441
b2p[TMP_REG_1]);
442442
} else
443-
PPC_DIVD(dst_reg, dst_reg,
443+
PPC_DIVDU(dst_reg, dst_reg,
444444
b2p[TMP_REG_1]);
445445
break;
446446
}

arch/x86/net/bpf_jit_comp.c

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ struct jit_context {
190190
#define BPF_MAX_INSN_SIZE 128
191191
#define BPF_INSN_SAFETY 64
192192

193-
#define AUX_STACK_SPACE 40 /* Space for RBX, R13, R14, R15, tailcnt */
194-
195-
#define PROLOGUE_SIZE 37
193+
#define PROLOGUE_SIZE 20
196194

197195
/*
198196
* Emit x86-64 prologue code for BPF program and check its size.
@@ -203,44 +201,19 @@ static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf)
203201
u8 *prog = *pprog;
204202
int cnt = 0;
205203

206-
/* push rbp */
207-
EMIT1(0x55);
208-
209-
/* mov rbp,rsp */
210-
EMIT3(0x48, 0x89, 0xE5);
211-
212-
/* sub rsp, rounded_stack_depth + AUX_STACK_SPACE */
213-
EMIT3_off32(0x48, 0x81, 0xEC,
214-
round_up(stack_depth, 8) + AUX_STACK_SPACE);
215-
216-
/* sub rbp, AUX_STACK_SPACE */
217-
EMIT4(0x48, 0x83, 0xED, AUX_STACK_SPACE);
218-
219-
/* mov qword ptr [rbp+0],rbx */
220-
EMIT4(0x48, 0x89, 0x5D, 0);
221-
/* mov qword ptr [rbp+8],r13 */
222-
EMIT4(0x4C, 0x89, 0x6D, 8);
223-
/* mov qword ptr [rbp+16],r14 */
224-
EMIT4(0x4C, 0x89, 0x75, 16);
225-
/* mov qword ptr [rbp+24],r15 */
226-
EMIT4(0x4C, 0x89, 0x7D, 24);
227-
204+
EMIT1(0x55); /* push rbp */
205+
EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
206+
/* sub rsp, rounded_stack_depth */
207+
EMIT3_off32(0x48, 0x81, 0xEC, round_up(stack_depth, 8));
208+
EMIT1(0x53); /* push rbx */
209+
EMIT2(0x41, 0x55); /* push r13 */
210+
EMIT2(0x41, 0x56); /* push r14 */
211+
EMIT2(0x41, 0x57); /* push r15 */
228212
if (!ebpf_from_cbpf) {
229-
/*
230-
* Clear the tail call counter (tail_call_cnt): for eBPF tail
231-
* calls we need to reset the counter to 0. It's done in two
232-
* instructions, resetting RAX register to 0, and moving it
233-
* to the counter location.
234-
*/
235-
236-
/* xor eax, eax */
237-
EMIT2(0x31, 0xc0);
238-
/* mov qword ptr [rbp+32], rax */
239-
EMIT4(0x48, 0x89, 0x45, 32);
240-
213+
/* zero init tail_call_cnt */
214+
EMIT2(0x6a, 0x00);
241215
BUILD_BUG_ON(cnt != PROLOGUE_SIZE);
242216
}
243-
244217
*pprog = prog;
245218
}
246219

@@ -285,13 +258,13 @@ static void emit_bpf_tail_call(u8 **pprog)
285258
* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
286259
* goto out;
287260
*/
288-
EMIT2_off32(0x8B, 0x85, 36); /* mov eax, dword ptr [rbp + 36] */
261+
EMIT2_off32(0x8B, 0x85, -36 - MAX_BPF_STACK); /* mov eax, dword ptr [rbp - 548] */
289262
EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
290263
#define OFFSET2 (30 + RETPOLINE_RAX_BPF_JIT_SIZE)
291264
EMIT2(X86_JA, OFFSET2); /* ja out */
292265
label2 = cnt;
293266
EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
294-
EMIT2_off32(0x89, 0x85, 36); /* mov dword ptr [rbp + 36], eax */
267+
EMIT2_off32(0x89, 0x85, -36 - MAX_BPF_STACK); /* mov dword ptr [rbp -548], eax */
295268

296269
/* prog = array->ptrs[index]; */
297270
EMIT4_off32(0x48, 0x8B, 0x84, 0xD6, /* mov rax, [rsi + rdx * 8 + offsetof(...)] */
@@ -1040,19 +1013,14 @@ xadd: if (is_imm8(insn->off))
10401013
seen_exit = true;
10411014
/* Update cleanup_addr */
10421015
ctx->cleanup_addr = proglen;
1043-
/* mov rbx, qword ptr [rbp+0] */
1044-
EMIT4(0x48, 0x8B, 0x5D, 0);
1045-
/* mov r13, qword ptr [rbp+8] */
1046-
EMIT4(0x4C, 0x8B, 0x6D, 8);
1047-
/* mov r14, qword ptr [rbp+16] */
1048-
EMIT4(0x4C, 0x8B, 0x75, 16);
1049-
/* mov r15, qword ptr [rbp+24] */
1050-
EMIT4(0x4C, 0x8B, 0x7D, 24);
1051-
1052-
/* add rbp, AUX_STACK_SPACE */
1053-
EMIT4(0x48, 0x83, 0xC5, AUX_STACK_SPACE);
1054-
EMIT1(0xC9); /* leave */
1055-
EMIT1(0xC3); /* ret */
1016+
if (!bpf_prog_was_classic(bpf_prog))
1017+
EMIT1(0x5B); /* get rid of tail_call_cnt */
1018+
EMIT2(0x41, 0x5F); /* pop r15 */
1019+
EMIT2(0x41, 0x5E); /* pop r14 */
1020+
EMIT2(0x41, 0x5D); /* pop r13 */
1021+
EMIT1(0x5B); /* pop rbx */
1022+
EMIT1(0xC9); /* leave */
1023+
EMIT1(0xC3); /* ret */
10561024
break;
10571025

10581026
default:

include/uapi/linux/bpf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,8 +3378,8 @@ struct bpf_raw_tracepoint_args {
33783378
/* DIRECT: Skip the FIB rules and go to FIB table associated with device
33793379
* OUTPUT: Do lookup from egress perspective; default is ingress
33803380
*/
3381-
#define BPF_FIB_LOOKUP_DIRECT BIT(0)
3382-
#define BPF_FIB_LOOKUP_OUTPUT BIT(1)
3381+
#define BPF_FIB_LOOKUP_DIRECT (1U << 0)
3382+
#define BPF_FIB_LOOKUP_OUTPUT (1U << 1)
33833383

33843384
enum {
33853385
BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */

kernel/bpf/devmap.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ static void dev_map_free(struct bpf_map *map)
186186
if (!dev)
187187
continue;
188188

189+
free_percpu(dev->bulkq);
189190
dev_put(dev->dev);
190191
kfree(dev);
191192
}
@@ -281,6 +282,7 @@ void __dev_map_flush(struct bpf_map *map)
281282
unsigned long *bitmap = this_cpu_ptr(dtab->flush_needed);
282283
u32 bit;
283284

285+
rcu_read_lock();
284286
for_each_set_bit(bit, bitmap, map->max_entries) {
285287
struct bpf_dtab_netdev *dev = READ_ONCE(dtab->netdev_map[bit]);
286288
struct xdp_bulk_queue *bq;
@@ -291,11 +293,12 @@ void __dev_map_flush(struct bpf_map *map)
291293
if (unlikely(!dev))
292294
continue;
293295

294-
__clear_bit(bit, bitmap);
295-
296296
bq = this_cpu_ptr(dev->bulkq);
297297
bq_xmit_all(dev, bq, XDP_XMIT_FLUSH, true);
298+
299+
__clear_bit(bit, bitmap);
298300
}
301+
rcu_read_unlock();
299302
}
300303

301304
/* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or
@@ -388,13 +391,15 @@ static void dev_map_flush_old(struct bpf_dtab_netdev *dev)
388391

389392
int cpu;
390393

394+
rcu_read_lock();
391395
for_each_online_cpu(cpu) {
392396
bitmap = per_cpu_ptr(dev->dtab->flush_needed, cpu);
393397
__clear_bit(dev->bit, bitmap);
394398

395399
bq = per_cpu_ptr(dev->bulkq, cpu);
396400
bq_xmit_all(dev, bq, XDP_XMIT_FLUSH, false);
397401
}
402+
rcu_read_unlock();
398403
}
399404
}
400405

kernel/bpf/lpm_trie.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,14 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
716716
* have exact two children, so this function will never return NULL.
717717
*/
718718
for (node = search_root; node;) {
719-
if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
719+
if (node->flags & LPM_TREE_NODE_FLAG_IM) {
720+
node = rcu_dereference(node->child[0]);
721+
} else {
720722
next_node = node;
721-
node = rcu_dereference(node->child[0]);
723+
node = rcu_dereference(node->child[0]);
724+
if (!node)
725+
node = rcu_dereference(next_node->child[1]);
726+
}
722727
}
723728
do_copy:
724729
next_key->prefixlen = next_node->prefixlen;

kernel/trace/bpf_trace.c

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,6 @@ static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
410410
.arg4_type = ARG_CONST_SIZE,
411411
};
412412

413-
static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
414-
415413
static __always_inline u64
416414
__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
417415
u64 flags, struct perf_sample_data *sd)
@@ -442,24 +440,50 @@ __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
442440
return perf_event_output(event, sd, regs);
443441
}
444442

443+
/*
444+
* Support executing tracepoints in normal, irq, and nmi context that each call
445+
* bpf_perf_event_output
446+
*/
447+
struct bpf_trace_sample_data {
448+
struct perf_sample_data sds[3];
449+
};
450+
451+
static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
452+
static DEFINE_PER_CPU(int, bpf_trace_nest_level);
445453
BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
446454
u64, flags, void *, data, u64, size)
447455
{
448-
struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
456+
struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
457+
int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
449458
struct perf_raw_record raw = {
450459
.frag = {
451460
.size = size,
452461
.data = data,
453462
},
454463
};
464+
struct perf_sample_data *sd;
465+
int err;
455466

456-
if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
457-
return -EINVAL;
467+
if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
468+
err = -EBUSY;
469+
goto out;
470+
}
471+
472+
sd = &sds->sds[nest_level - 1];
473+
474+
if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
475+
err = -EINVAL;
476+
goto out;
477+
}
458478

459479
perf_sample_data_init(sd, 0, 0);
460480
sd->raw = &raw;
461481

462-
return __bpf_perf_event_output(regs, map, flags, sd);
482+
err = __bpf_perf_event_output(regs, map, flags, sd);
483+
484+
out:
485+
this_cpu_dec(bpf_trace_nest_level);
486+
return err;
463487
}
464488

465489
static const struct bpf_func_proto bpf_perf_event_output_proto = {
@@ -822,16 +846,48 @@ pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
822846
/*
823847
* bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
824848
* to avoid potential recursive reuse issue when/if tracepoints are added
825-
* inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack
849+
* inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
850+
*
851+
* Since raw tracepoints run despite bpf_prog_active, support concurrent usage
852+
* in normal, irq, and nmi context.
826853
*/
827-
static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
854+
struct bpf_raw_tp_regs {
855+
struct pt_regs regs[3];
856+
};
857+
static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
858+
static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
859+
static struct pt_regs *get_bpf_raw_tp_regs(void)
860+
{
861+
struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
862+
int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
863+
864+
if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
865+
this_cpu_dec(bpf_raw_tp_nest_level);
866+
return ERR_PTR(-EBUSY);
867+
}
868+
869+
return &tp_regs->regs[nest_level - 1];
870+
}
871+
872+
static void put_bpf_raw_tp_regs(void)
873+
{
874+
this_cpu_dec(bpf_raw_tp_nest_level);
875+
}
876+
828877
BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
829878
struct bpf_map *, map, u64, flags, void *, data, u64, size)
830879
{
831-
struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
880+
struct pt_regs *regs = get_bpf_raw_tp_regs();
881+
int ret;
882+
883+
if (IS_ERR(regs))
884+
return PTR_ERR(regs);
832885

833886
perf_fetch_caller_regs(regs);
834-
return ____bpf_perf_event_output(regs, map, flags, data, size);
887+
ret = ____bpf_perf_event_output(regs, map, flags, data, size);
888+
889+
put_bpf_raw_tp_regs();
890+
return ret;
835891
}
836892

837893
static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
@@ -848,12 +904,18 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
848904
BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
849905
struct bpf_map *, map, u64, flags)
850906
{
851-
struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
907+
struct pt_regs *regs = get_bpf_raw_tp_regs();
908+
int ret;
909+
910+
if (IS_ERR(regs))
911+
return PTR_ERR(regs);
852912

853913
perf_fetch_caller_regs(regs);
854914
/* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
855-
return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
856-
flags, 0, 0);
915+
ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
916+
flags, 0, 0);
917+
put_bpf_raw_tp_regs();
918+
return ret;
857919
}
858920

859921
static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
@@ -868,11 +930,17 @@ static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
868930
BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
869931
void *, buf, u32, size, u64, flags)
870932
{
871-
struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
933+
struct pt_regs *regs = get_bpf_raw_tp_regs();
934+
int ret;
935+
936+
if (IS_ERR(regs))
937+
return PTR_ERR(regs);
872938

873939
perf_fetch_caller_regs(regs);
874-
return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
875-
(unsigned long) size, flags, 0);
940+
ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
941+
(unsigned long) size, flags, 0);
942+
put_bpf_raw_tp_regs();
943+
return ret;
876944
}
877945

878946
static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {

0 commit comments

Comments
 (0)