From patchwork Sat Nov 7 05:36:17 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 7574821 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 64672C05C6 for ; Sat, 7 Nov 2015 05:58:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 809672062A for ; Sat, 7 Nov 2015 05:58:07 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 945DA20626 for ; Sat, 7 Nov 2015 05:58:06 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZuwSt-0003mp-SP; Sat, 07 Nov 2015 05:55:03 +0000 Received: from mail-pa0-x230.google.com ([2607:f8b0:400e:c03::230]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZuwSp-0003eT-Dh for linux-arm-kernel@lists.infradead.org; Sat, 07 Nov 2015 05:55:01 +0000 Received: by pasz6 with SMTP id z6so147775014pas.2 for ; Fri, 06 Nov 2015 21:54:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=m7oZ07+YjmqeUpNb/f4RYFzvAJUDpfK0KLPkxw0KSh4=; b=oVJh/Iv7/Ls1Z+4MukxSCdxo7olkoORPgYLlfwgKgoun3gkhYTBTXwaTEg6ayrWc2v /yESt1ecfaogfOZT2omvzHdG1ZH/KWlpwvMZM/6cnbWTwoJYX3F7esYLMABT0ssZwmkG 7jjgW98dEv1blSgyVf63LTvINcrXEF3CMc4dKC8wZp9MTwUsWes+c/S/mjbjCmMGyWxf z8Ucyp/iiu7g6E3urYD/hf7G2+NQbEFo97aEoKEiioB15E8/HqCucABDyumE+SxDMwiG AllnFppeyLRjvSyF4xXXEnYLRzPeRS8QPbxcRz55CDicOIpaS3OZWQLE4yYRBqNGlaE+ xhiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=m7oZ07+YjmqeUpNb/f4RYFzvAJUDpfK0KLPkxw0KSh4=; b=B8UUIzPUAumY2ix++Q2e9AC51FH/+k0Qq9mJGzpLbdqcA+r0H7cCNkrTq3pEfpaLx/ FAfFGHynK++FmrZAJSpINWo2BfvyDVVL8WS07hXUNnVK3In6rc3saRRMgHanozPH59NG 9Tt7eAFZH9Fp4+3Q4LXzPzbWZvymB0oU38dVmMgZUpNeWjcewC2roF/rmGyBIF/au0my wLwtRyDX6hALH8aeXBWvQRBMhUYpdy7gbhtzHYYvw/dwhqTqlzLdM/3QJavIyJFPSYHK v+gplI2ltO1/tz1lzKEgoVB7BKZvJNLGo6ES6XHv1dnUFt39RZS2w5J6HfgqviksTD+v O/KQ== X-Gm-Message-State: ALoCoQm+OyyhBz7SSbgHXEYw/iHC7h9HHBg4IZstfnbV9PNTrdZ3p4IhBwEIuK/q89t6y0gcj2oW X-Received: by 10.66.141.42 with SMTP id rl10mr23070805pab.18.1446875678560; Fri, 06 Nov 2015 21:54:38 -0800 (PST) Received: from yshi-Precision-T5600.corp.ad.wrs.com (unknown-216-82.windriver.com. [147.11.216.82]) by smtp.gmail.com with ESMTPSA id ro7sm3377873pbb.30.2015.11.06.21.54.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Nov 2015 21:54:38 -0800 (PST) From: Yang Shi To: ast@kernel.org, daniel@iogearbox.net, catalin.marinas@arm.com, will.deacon@arm.com Subject: [PATCH] arm64: bpf: fix JIT stack setup Date: Fri, 6 Nov 2015 21:36:17 -0800 Message-Id: <1446874577-14539-1-git-send-email-yang.shi@linaro.org> X-Mailer: git-send-email 2.0.2 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151106_215459_514994_C6473B49 X-CRM114-Status: GOOD ( 16.55 ) X-Spam-Score: -2.6 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.shi@linaro.org, linaro-kernel@lists.linaro.org, zlim.lnx@gmail.com, linux-kernel@vger.kernel.org, xi.wang@gmail.com, netdev@vger.kernel.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP ARM64 JIT used FP (x29) as eBPF fp register, but FP is subjected to change during function call so it may cause the BPF prog stack base address change too. Whenever, it pointed to the bottom of BPF prog stack instead of the top. So, when copying data via bpf_probe_read, it will be copied to (SP - offset), then it may overwrite the saved FP/LR. Use x25 to replace FP as BPF stack base register (fp). Since x25 is callee saved register, so it will keep intact during function call. It is initialized in BPF prog prologue when BPF prog is started to run everytime. When BPF prog exits, it could be just tossed. Other than this the BPf prog stack base need to be setup before function call stack. So, the BPF stack layout looks like: high original A64_SP => 0:+-----+ BPF prologue | | FP/LR and callee saved registers BPF fp register => +64:+-----+ | | | ... | BPF prog stack | | | | current A64_SP => +-----+ | | | ... | Function call stack | | +-----+ low Signed-off-by: Yang Shi CC: Zi Shen Lim CC: Xi Wang --- arch/arm64/net/bpf_jit_comp.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index a44e529..6809647 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -50,7 +50,7 @@ static const int bpf2a64[] = { [BPF_REG_8] = A64_R(21), [BPF_REG_9] = A64_R(22), /* read-only frame pointer to access stack */ - [BPF_REG_FP] = A64_FP, + [BPF_REG_FP] = A64_R(25), /* temporary register for internal BPF JIT */ [TMP_REG_1] = A64_R(23), [TMP_REG_2] = A64_R(24), @@ -155,18 +155,42 @@ static void build_prologue(struct jit_ctx *ctx) stack_size += 4; /* extra for skb_copy_bits buffer */ stack_size = STACK_ALIGN(stack_size); + /* + * BPF prog stack layout + * + * high + * original A64_SP => 0:+-----+ BPF prologue + * | | FP/LR and callee saved registers + * BPF fp register => +64:+-----+ + * | | + * | ... | BPF prog stack + * | | + * | | + * current A64_SP => +-----+ + * | | + * | ... | Function call stack + * | | + * +-----+ + * low + * + */ + + /* Save FP and LR registers to stay align with ARM64 AAPCS */ + emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx); + /* Save callee-saved register */ emit(A64_PUSH(r6, r7, A64_SP), ctx); emit(A64_PUSH(r8, r9, A64_SP), ctx); if (ctx->tmp_used) emit(A64_PUSH(tmp1, tmp2, A64_SP), ctx); - /* Set up BPF stack */ - emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx); - - /* Set up frame pointer */ + /* Set up BPF prog stack base register (x25) */ emit(A64_MOV(1, fp, A64_SP), ctx); + /* Set up function call stack */ + emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx); + emit(A64_MOV(1, A64_FP, A64_SP), ctx); + /* Clear registers A and X */ emit_a64_mov_i64(ra, 0, ctx); emit_a64_mov_i64(rx, 0, ctx); @@ -196,8 +220,8 @@ static void build_epilogue(struct jit_ctx *ctx) emit(A64_POP(r8, r9, A64_SP), ctx); emit(A64_POP(r6, r7, A64_SP), ctx); - /* Restore frame pointer */ - emit(A64_MOV(1, fp, A64_SP), ctx); + /* Restore FP/LR registers */ + emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx); /* Set return value */ emit(A64_MOV(1, A64_R(0), r0), ctx);