From patchwork Tue Oct 2 20:35:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Stringer X-Patchwork-Id: 978013 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=wand.net.nz Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Aq3dNlqK"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42PrWp37Htz9s3C for ; Wed, 3 Oct 2018 06:35:50 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727351AbeJCDU5 (ORCPT ); Tue, 2 Oct 2018 23:20:57 -0400 Received: from mail-yw1-f68.google.com ([209.85.161.68]:40953 "EHLO mail-yw1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726769AbeJCDU4 (ORCPT ); Tue, 2 Oct 2018 23:20:56 -0400 Received: by mail-yw1-f68.google.com with SMTP id l79-v6so1348999ywc.7 for ; Tue, 02 Oct 2018 13:35:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=10Ri1MhKGKtPGsV02V/8LL7fpCXg2TguwdhOxWVVYFw=; b=Aq3dNlqKsp0Y5mIukRr6fikAC/kgbMOFuMxvfet/2l8LvRq/uszhemoGyQkQqak6J0 3nTmdJ8uSIVCEWvc/exYd68QZbz7fcRreVhISqdSSkM4Y7vSjriDUhkUqxBHkD4JB7oI LxEAHORXAsxCu3aeHpSadSGLVv1Hx8h4sdh94IVISFe85zsDW9OuPQxUTsq67o7jnrBF RmZdYHzq6MZbBo25m4muB9RM1hJ8/vs5+OzU/NhJvI6Sz97Wlm5FC5aNwiXAt8W5g2sV r0PN0eMzufuoQqOOLPF1e2ZcYeRHbX2nhBYOxDtM5QUWWJ7BeaiRxEV1v1FO032Alfio ll7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=10Ri1MhKGKtPGsV02V/8LL7fpCXg2TguwdhOxWVVYFw=; b=K/Y0tyPWVE3gZPb957u3p34+9XQaT1xgR9uoj5G4Oc87taMORRPtCwRiobEQ1/9vAy 9kzZYiuE8VXUanseTtS5z4HznkKmkoMD0vgow3d74bXeWoKrXF+TJ0mHBpdHImF2n2jx lgKCTF9TepIRFxnqp0Y/r49ge5A1cr1EPZy2lz8w5aDuB05DsgnLIpM1hrvGD/nB3ztg LswkONTLuAPPM1KVGpl/j9OEMcciCZAw3PQdcUW5tlD4AD05X5424NpLdFZCVdlEwvy9 LKjcP/pXRqMn8lsbDP/S5lrJuxthVKS+bl9mKbDoPCUiQ2qUdVRnbGnvjtdgwgxBWRBL lkLQ== X-Gm-Message-State: ABuFfog9BAkRvcmU29SsHiaXk2LGzVYGt8G909lU4JOREn5GaSiwGUaV i+C7v8aR0D0Rfin4z9lh2dQ= X-Google-Smtp-Source: ACcGV6059lbMYOmScfoXFYByb9d+S25nAFVeYmk/Xe226q6yGHD9rNNR6n7ZwUCWhCWxxSZwaQfr8A== X-Received: by 2002:a81:f8f:: with SMTP id 137-v6mr9539595ywp.430.1538512546943; Tue, 02 Oct 2018 13:35:46 -0700 (PDT) Received: from localhost.localdomain ([99.0.85.34]) by smtp.gmail.com with ESMTPSA id o131-v6sm4555361ywb.107.2018.10.02.13.35.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Oct 2018 13:35:46 -0700 (PDT) From: Joe Stringer To: daniel@iogearbox.net Cc: netdev@vger.kernel.org, ast@kernel.org, john.fastabend@gmail.com, tgraf@suug.ch, kafai@fb.com, nitin.hande@gmail.com, mauricio.vasquez@polito.it Subject: [PATCHv4 bpf-next 01/13] bpf: Add iterator for spilled registers Date: Tue, 2 Oct 2018 13:35:29 -0700 Message-Id: <20181002203541.26599-2-joe@wand.net.nz> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181002203541.26599-1-joe@wand.net.nz> References: <20181002203541.26599-1-joe@wand.net.nz> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add this iterator for spilled registers, it concentrates the details of how to get the current frame's spilled registers into a single macro while clarifying the intention of the code which is calling the macro. Signed-off-by: Joe Stringer Acked-by: Alexei Starovoitov --- v2-v3: No change. v4: Prefix globally defined macros with "bpf_". --- include/linux/bpf_verifier.h | 11 +++++++++++ kernel/bpf/verifier.c | 16 +++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index b42b60a83e19..d0e7f97e8b60 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -131,6 +131,17 @@ struct bpf_verifier_state { u32 curframe; }; +#define bpf_get_spilled_reg(slot, frame) \ + (((slot < frame->allocated_stack / BPF_REG_SIZE) && \ + (frame->stack[slot].slot_type[0] == STACK_SPILL)) \ + ? &frame->stack[slot].spilled_ptr : NULL) + +/* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */ +#define bpf_for_each_spilled_reg(iter, frame, reg) \ + for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \ + iter < frame->allocated_stack / BPF_REG_SIZE; \ + iter++, reg = bpf_get_spilled_reg(iter, frame)) + /* linked list of verifier states used to prune search */ struct bpf_verifier_state_list { struct bpf_verifier_state state; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index a8cc83a970d1..9c82d8f58085 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2252,10 +2252,9 @@ static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, if (reg_is_pkt_pointer_any(®s[i])) mark_reg_unknown(env, regs, i); - for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { - if (state->stack[i].slot_type[0] != STACK_SPILL) + bpf_for_each_spilled_reg(i, state, reg) { + if (!reg) continue; - reg = &state->stack[i].spilled_ptr; if (reg_is_pkt_pointer_any(reg)) __mark_reg_unknown(reg); } @@ -3395,10 +3394,9 @@ static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, for (j = 0; j <= vstate->curframe; j++) { state = vstate->frame[j]; - for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { - if (state->stack[i].slot_type[0] != STACK_SPILL) + bpf_for_each_spilled_reg(i, state, reg) { + if (!reg) continue; - reg = &state->stack[i].spilled_ptr; if (reg->type == type && reg->id == dst_reg->id) reg->range = max(reg->range, new_range); } @@ -3643,7 +3641,7 @@ static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, bool is_null) { struct bpf_func_state *state = vstate->frame[vstate->curframe]; - struct bpf_reg_state *regs = state->regs; + struct bpf_reg_state *reg, *regs = state->regs; u32 id = regs[regno].id; int i, j; @@ -3652,8 +3650,8 @@ static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, for (j = 0; j <= vstate->curframe; j++) { state = vstate->frame[j]; - for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { - if (state->stack[i].slot_type[0] != STACK_SPILL) + bpf_for_each_spilled_reg(i, state, reg) { + if (!reg) continue; mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); }