Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1167796imm; Wed, 1 Aug 2018 11:18:28 -0700 (PDT) X-Google-Smtp-Source: AAOMgpegM8b9kSreCm+boJ0G95G56CeuVKVNJLXaRnUplw95qg6GprlOIRg1PKRyUR7uE6QEbwQz X-Received: by 2002:a65:498c:: with SMTP id r12-v6mr26108916pgs.112.1533147508444; Wed, 01 Aug 2018 11:18:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533147508; cv=none; d=google.com; s=arc-20160816; b=xr7cxSmo63DUkNx3YFmzAsyAEvSQuM5X+eOyPu/WQlhMOOrQ8PXu5/yXlktvD8TaUE D+QVLiFcpoEbCbaftePKAqPwW4uDojn1x5lJa5jMEL5pyCOhcfMs/3FlR8KBBl5pRZsZ x+RHyHCL8C7jl1zBABaLOlNs5CK9UGieSz0luTOw5ZBSH4HbXg3emonHoKbenemiRZPr 9rKgNniq/CUg/Mq4T+hvlWp1sIZUW9VrpPf21bC1PZCXHdmjQh7mmzyHGoCME0RLXclM xqt6yqpFOEbQETNuGooCBXIvf985FdnteSLHjlV1zDnCjggsnPvG3kPoLp4K6unaijBj MFmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=jrjDFw24Cdlil16UDMz5yxZhOA1AK3Pyhlx3KpDDk/w=; b=eAUKTwCAcuCt5Jy935VQr+GxEX2YY+Aucouhb+MKVq9Pp79F6Qc1CYwkGIQZlhYpsT QJnEYW3LcoScZXMdyz6swsF8lfKFrAFWY/RVww3k3QH7jINJm7XZeEO/ldLZOIuCQT+H EPomvA040YOsyR5cnRvM7DaE82puDGclujVtyWtiaq8rZ5kW9yLqUdHQG2XNz8lLw0bt cGlaon9ri1Sivddy2W+LIceR6hRhA2InKLmlAgeaxhTUb1yqZujf657ujKz4fu6mo+9a SZ73byWJ1gxzGzFZDNQUkXrQb/RSHLzf4DWSQjdJ+p+2FBMhnRc6QSB8xnMRfnQ1CiZ0 2e3w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 68-v6si17606591pff.55.2018.08.01.11.18.13; Wed, 01 Aug 2018 11:18:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731722AbeHAUDu (ORCPT + 99 others); Wed, 1 Aug 2018 16:03:50 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:47890 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405088AbeHATFn (ORCPT ); Wed, 1 Aug 2018 15:05:43 -0400 Received: from localhost (D57E6652.static.ziggozakelijk.nl [213.126.102.82]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 22937D0A; Wed, 1 Aug 2018 17:19:01 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sandipan Das , Daniel Borkmann , Sasha Levin Subject: [PATCH 4.14 108/246] bpf: powerpc64: pad function address loads with NOPs Date: Wed, 1 Aug 2018 18:50:18 +0200 Message-Id: <20180801165016.878372335@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180801165011.700991984@linuxfoundation.org> References: <20180801165011.700991984@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sandipan Das [ Upstream commit 4ea69b2fd623dee2bbc77d3b6b7d8c0924e2026a ] For multi-function programs, loading the address of a callee function to a register requires emitting instructions whose count varies from one to five depending on the nature of the address. Since we come to know of the callee's address only before the extra pass, the number of instructions required to load this address may vary from what was previously generated. This can make the JITed image grow or shrink. To avoid this, we should generate a constant five-instruction when loading function addresses by padding the optimized load sequence with NOPs. Signed-off-by: Sandipan Das Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- arch/powerpc/net/bpf_jit_comp64.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) --- a/arch/powerpc/net/bpf_jit_comp64.c +++ b/arch/powerpc/net/bpf_jit_comp64.c @@ -203,25 +203,37 @@ static void bpf_jit_build_epilogue(u32 * static void bpf_jit_emit_func_call(u32 *image, struct codegen_context *ctx, u64 func) { + unsigned int i, ctx_idx = ctx->idx; + + /* Load function address into r12 */ + PPC_LI64(12, func); + + /* For bpf-to-bpf function calls, the callee's address is unknown + * until the last extra pass. As seen above, we use PPC_LI64() to + * load the callee's address, but this may optimize the number of + * instructions required based on the nature of the address. + * + * Since we don't want the number of instructions emitted to change, + * we pad the optimized PPC_LI64() call with NOPs to guarantee that + * we always have a five-instruction sequence, which is the maximum + * that PPC_LI64() can emit. + */ + for (i = ctx->idx - ctx_idx; i < 5; i++) + PPC_NOP(); + #ifdef PPC64_ELF_ABI_v1 - /* func points to the function descriptor */ - PPC_LI64(b2p[TMP_REG_2], func); - /* Load actual entry point from function descriptor */ - PPC_BPF_LL(b2p[TMP_REG_1], b2p[TMP_REG_2], 0); - /* ... and move it to LR */ - PPC_MTLR(b2p[TMP_REG_1]); /* * Load TOC from function descriptor at offset 8. * We can clobber r2 since we get called through a * function pointer (so caller will save/restore r2) * and since we don't use a TOC ourself. */ - PPC_BPF_LL(2, b2p[TMP_REG_2], 8); -#else - /* We can clobber r12 */ - PPC_FUNC_ADDR(12, func); - PPC_MTLR(12); + PPC_BPF_LL(2, 12, 8); + /* Load actual entry point from function descriptor */ + PPC_BPF_LL(12, 12, 0); #endif + + PPC_MTLR(12); PPC_BLRL(); }