Received: by 2002:a4f:b056:0:0:0:0:0 with SMTP id m22csp2665758ivi; Tue, 15 Sep 2020 16:08:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzHDpIcnwtOBQ/VkDO9hv6mEm/yxqYM03bDWnDmsmtRPSvRLZNDTEDiN3AWWhkNXzdY5HIV X-Received: by 2002:aa7:c707:: with SMTP id i7mr24946883edq.107.1600211298195; Tue, 15 Sep 2020 16:08:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600211298; cv=none; d=google.com; s=arc-20160816; b=qS/xTd6ZDeUCsjB4d661pyyV1HiFSs4u23iuMmfDY1F+MgF+Rw9tGXkr7JN7KL6tTz 6DvtmA/nrkLGLGZrYgDF67tC0+6X+Dg6GMUmrAxD7GQSx/DAcxkYccn/0PlCJVXSVZEl QOUqewe0vs903PXHWkiwLbPtkhJHYFoRMp00zfqNSkPXnbkAldqCg2sr9O/2OnejZ+xq ATAij3qK0cUxoGjQFWQfey2HZlvXpChY81DF22OCtoZlHWlUCgoPs1U/M5lPoUGHWyG0 IekWafTeMTjgto21/qyscCYkoyitFXv/HFJY1CZBb9+XdQhhiW/l8ef0JnJhbeGs6l1o xuBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=mpt5/mY5yPLcWPN5dAxKWZ1iKOvnWN/naNw5pxswiPQ=; b=g2CC8bD+kuZlqvAJfWPFb6mNZ5GAOK0rHWMjTAwlyGr09Yz2eoC3euSeqH/ppAGBof oCC0oBVUM/VtrzVZ7tOvEVd4QYKcv+rweiOIKUqfA3fKClzCkWtas6o0C2WlLL/Alrvt JMZ2TSojx92YhJIv1GlIr7gcMfYOgrzju1WcVkEa5knk2Aj30MArmjyK980GWLFDZn4c ekFA9iXeBRJhCMOuK9M1xwCiCvYWeyaI+rTG3Ag216l8OriNnFjwjevmmQlLtZ1hQwK5 YT3+Zwp6oUaT5GceOtnZ4IaxsooHrQDt1FYI5bAyppa1Yu1YrxCDzzUoxv2FZtfXz6ii X2hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pWfAOQep; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j17si10108387ejc.26.2020.09.15.16.07.55; Tue, 15 Sep 2020 16:08:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pWfAOQep; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727521AbgIOXGG (ORCPT + 99 others); Tue, 15 Sep 2020 19:06:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727221AbgIOOsG (ORCPT ); Tue, 15 Sep 2020 10:48:06 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75737C0612F2 for ; Tue, 15 Sep 2020 06:53:49 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id w2so3479885wmi.1 for ; Tue, 15 Sep 2020 06:53:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=mpt5/mY5yPLcWPN5dAxKWZ1iKOvnWN/naNw5pxswiPQ=; b=pWfAOQepoqKVe8FVv3PSRnE2yNbCHL5v9IqR0LbagyAtChnGvMpbyFpYy+ah7a+MDp VHUKM4CuzkFAiSoTN8zgTwkV2aT3nxZP2r7v/FxeCqfIP5gfpAe3HZlsnuuGZ9ccjvYm S1AH7wlrJhWUXEGlA4ipsYAl+ChToZ9akzjOnAQ8MMRU8odgwXVqgE37YELU22uOWKgn 3mAKu2tw8898QfUe05BFKktyMTRrAoo+3ySa3XY0iN33XUiOL7EFVniVk43BB6B7R48Q uVfXKufART+6oiSDUqycqChiI70DAA7zh61WEJ+0JYLEv9ZnvNfbLHoOFVh4eNbifNUu QVow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=mpt5/mY5yPLcWPN5dAxKWZ1iKOvnWN/naNw5pxswiPQ=; b=SndTwMSfOunVrwvoH74ixPIZCXOvd8cMgKNFnqtnZ7djhOlIqKEj7CaqngrUmkX/P1 V4FvNTDbg0TVNQi1u92Ur1IwIcPzMJ6rBRSKp1AzpktOoO0+I+AaLt++5A1Gz9zhggFL 7wn79M6tH9KETuuPJRRfAqduS9kPXxkztGw0gmQVU7+3wBFISlHP3oweSHm1PDOZ08qd 94OWZopnZzaJzCGWMLFC5z6RimccBv8K2EEB1vr80t5CMdczMFy1vSZ5fRbxFd4/VCfV vKeGlZTup7VsCRixOQJu1qma0Bd9ICF2ZQdUdR6r6T0S7rP7U4Poc+QARfwYDW+mPPA/ GjTg== X-Gm-Message-State: AOAM532ypzN8Wp/EmKkxvWjGQUsh/YqKzJ9IBLsI5v8VyslD+AvSTNEC qRODzdEeAJMizl7QSX1FVClAgw== X-Received: by 2002:a1c:2bc7:: with SMTP id r190mr4993218wmr.116.1600178027842; Tue, 15 Sep 2020 06:53:47 -0700 (PDT) Received: from apalos.home (athedsl-246545.home.otenet.gr. [85.73.10.175]) by smtp.gmail.com with ESMTPSA id a10sm23545590wmj.38.2020.09.15.06.53.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Sep 2020 06:53:47 -0700 (PDT) Date: Tue, 15 Sep 2020 16:53:44 +0300 From: Ilias Apalodimas To: Will Deacon Cc: bpf@vger.kernel.org, ardb@kernel.org, naresh.kamboju@linaro.org, Jiri Olsa , Jean-Philippe Brucker , Yauheni Kaliuta , Daniel Borkmann , Alexei Starovoitov , Zi Shen Lim , Catalin Marinas , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , netdev@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] arm64: bpf: Fix branch offset in JIT Message-ID: <20200915135344.GA113966@apalos.home> References: <20200914160355.19179-1-ilias.apalodimas@linaro.org> <20200915131102.GA26439@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200915131102.GA26439@willie-the-truck> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Sep 15, 2020 at 02:11:03PM +0100, Will Deacon wrote: > Hi Ilias, > > On Mon, Sep 14, 2020 at 07:03:55PM +0300, Ilias Apalodimas wrote: > > Running the eBPF test_verifier leads to random errors looking like this: > > > > [ 6525.735488] Unexpected kernel BRK exception at EL1 > > [ 6525.735502] Internal error: ptrace BRK handler: f2000100 [#1] SMP > > Does this happen because we poison the BPF memory with BRK instructions? > Maybe we should look at using a special immediate so we can detect this, > rather than end up in the ptrace handler. As discussed offline this is what aarch64_insn_gen_branch_imm() will return for offsets > 128M and yes replacing the handler with a more suitable message would be good. > > > diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c > > index f8912e45be7a..0974effff58c 100644 > > --- a/arch/arm64/net/bpf_jit_comp.c > > +++ b/arch/arm64/net/bpf_jit_comp.c > > @@ -143,9 +143,13 @@ static inline void emit_addr_mov_i64(const int reg, const u64 val, > > } > > } > > > > -static inline int bpf2a64_offset(int bpf_to, int bpf_from, > > +static inline int bpf2a64_offset(int bpf_insn, int off, > > const struct jit_ctx *ctx) > > { > > + /* arm64 offset is relative to the branch instruction */ > > + int bpf_from = bpf_insn + 1; > > + /* BPF JMP offset is relative to the next instruction */ > > + int bpf_to = bpf_insn + off + 1; > > int to = ctx->offset[bpf_to]; > > /* -1 to account for the Branch instruction */ > > int from = ctx->offset[bpf_from] - 1; > > I think this is a bit confusing with all the variables. How about just > doing: > > /* BPF JMP offset is relative to the next BPF instruction */ > bpf_insn++; > > /* > * Whereas arm64 branch instructions encode the offset from the > * branch itself, so we must subtract 1 from the instruction offset. > */ > return ctx->offset[bpf_insn + off] - ctx->offset[bpf_insn] - 1; > Sure > > @@ -642,7 +646,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, > > > > /* JUMP off */ > > case BPF_JMP | BPF_JA: > > - jmp_offset = bpf2a64_offset(i + off, i, ctx); > > + jmp_offset = bpf2a64_offset(i, off, ctx); > > check_imm26(jmp_offset); > > emit(A64_B(jmp_offset), ctx); > > break; > > @@ -669,7 +673,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, > > case BPF_JMP32 | BPF_JSLE | BPF_X: > > emit(A64_CMP(is64, dst, src), ctx); > > emit_cond_jmp: > > - jmp_offset = bpf2a64_offset(i + off, i, ctx); > > + jmp_offset = bpf2a64_offset(i, off, ctx); > > check_imm19(jmp_offset); > > switch (BPF_OP(code)) { > > case BPF_JEQ: > > @@ -912,18 +916,26 @@ static int build_body(struct jit_ctx *ctx, bool extra_pass) > > const struct bpf_insn *insn = &prog->insnsi[i]; > > int ret; > > > > + /* > > + * offset[0] offset of the end of prologue, start of the > > + * first insn. > > + * offset[x] - offset of the end of x insn. > > So does offset[1] point at the last arm64 instruction for the first BPF > instruction, or does it point to the first arm64 instruction for the second > BPF instruction? > Right this isn't exactly a good comment. I'll change it to something like: offset[0] - offset of the end of prologue, start of the 1st insn. offset[1] - offset of the end of 1st insn. > > + */ > > + if (ctx->image == NULL) > > + ctx->offset[i] = ctx->idx; > > + > > ret = build_insn(insn, ctx, extra_pass); > > if (ret > 0) { > > i++; > > if (ctx->image == NULL) > > - ctx->offset[i] = ctx->idx; > > + ctx->offset[i] = ctx->offset[i - 1]; > > Does it matter that we set the offset for both halves of a 16-byte BPF > instruction? I think that's a change in behaviour here. Yes it is, but from reading around that's what I understood. for 16-byte eBPF instructions both should point to the start of the corresponding jited arm64 instruction. If I am horribly wrong about this, please shout. > > > continue; > > } > > - if (ctx->image == NULL) > > - ctx->offset[i] = ctx->idx; > > if (ret) > > return ret; > > } > > + if (ctx->image == NULL) > > + ctx->offset[i] = ctx->idx; > > I think it would be cleared to set ctx->offset[0] before the for loop (with > a comment about what it is) and then change the for loop to iterate from 1 > all the way to prog->len. > Sure > Will Thanks /Ilias