Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp573037lqp; Fri, 5 Apr 2024 02:27:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWncGA8hOigWb40gQ1v0Wmlc+dAVxCnDHWIU7yS3eN8RSiNlVFqv3LkuiezYBpngthpW6PHMDir0QMkSU4GqRNcvD5ExOSN1aXNs8TA0g== X-Google-Smtp-Source: AGHT+IHuYTAy6ruoFkVrylQYMBlaHGlMNynP0764kLMVrscNuOsaxGDF5auwqHkzwb5gF17o+fps X-Received: by 2002:a17:906:aeda:b0:a51:a0c7:6567 with SMTP id me26-20020a170906aeda00b00a51a0c76567mr613397ejb.8.1712309277839; Fri, 05 Apr 2024 02:27:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712309277; cv=pass; d=google.com; s=arc-20160816; b=PlmwC/l5cCp+lXPwv1vCSnamDZndqDCnjWBXxUp0Cc/RDqilU+9q0Zr+wx5RAtVIuq eSAsa+eV4vGWddWXZ8lUN9OVbaz47QGQtZ+ZnoZ4+rd5NA8mQ3fjtVKKqvzuMdRIDLDP LYBu0kUfks2Nr//e11hWDQAz/eTTJz8KjFL04a3cM9O+uBBNNmrWcO7MtWMtByXRmxZP w9U19OtbUmH5C4lEBN4niAmO8G6NORYStiBW6KrNqEhNhwxTbR/u/4Dtf4v4q3Xw+T7Q tZHfDYJdBfMg6dd3epu4SGDyhpvEMsRPfClgZ6rzk+761V6abHGX6WFTdk67DNN6W4XZ vWRA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=LX/ma26TDvYVdpCIWg6iUhgi/+t6jX0tkNjGdFErDfk=; fh=QUqfRXFaIww4aJedF27pPPhLHrDoZ+W9eqVChclau+Q=; b=J7zynd9UqDIJgUMDikXau5nwrrcaR28Y9GtpOizu0qu48MTxvWAy8ewL7q7POyO+gx uOko8pvehxlgfzAfqKFanMmJxcbeKMfIZTu6Z8xO0bbGy4uaRK/TyJxWF/SpLxwxD4Zy oawg2xvVO+iHb0443Oj/yM1Uo958B8rnIlWvPtsoqKW6pRqvG2FzjuSPSUt0pVb4aDx0 ooS70LMMOzpiw2yc1JvUcxUzohlYys8mA1kI0jIycuPwvbGgs6zfMk6xiNmEMepaFIA3 zbYFQMv2ZXvwbGR6sIb/Eqs+9J/T6oB99gu0hVktFqM8hz1HWVeDiVf0qdYnMGi1CsPk hpag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=kzJtCOip; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-132722-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-132722-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id du19-20020a17090772d300b00a465f304271si542336ejc.483.2024.04.05.02.27.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 02:27:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-132722-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=kzJtCOip; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-132722-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-132722-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5E5EC1F2455F for ; Fri, 5 Apr 2024 09:18:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 397C4161305; Fri, 5 Apr 2024 09:17:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kzJtCOip" Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F18015FD1B; Fri, 5 Apr 2024 09:17:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712308653; cv=none; b=hm7ZAkaF2oUPiNBnQHblQfA6tUH07ksvE0+z3V2VaB0zhRhSAqdlmcSV//M4MhfkzJxC3Em9lzstB8pQulTdTg4uaz33pnLAxqxHCd9orGzNxGJ0PyBpE+1iIX8g4WFtjETp/x3dFnlkf102MIEScgsRBsmwdhSDP+KiKFyLmSQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712308653; c=relaxed/simple; bh=arne33H5//u9DO+V442IJ7DrQF6kabb/of8ByX5eni0=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=qPOghpStzjlat6loyHGvcvM3f0uO8PoX7Z90/0M3FmehnJu+NMEMoo1ULP6kb8yRqYmwR3vyg7sFJf2WCYP5je6w/9YZyt/z0chsM6/fdTPlsYtNpGp74TTb4DsH/HzXyPSSqXZFcyDUuYuxiFo4d52oVZHpLUzrlYv5CZr77dg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kzJtCOip; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-41551639550so13904365e9.2; Fri, 05 Apr 2024 02:17:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712308649; x=1712913449; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=LX/ma26TDvYVdpCIWg6iUhgi/+t6jX0tkNjGdFErDfk=; b=kzJtCOipc0PGWTAIzhWCt42nTXcjST16Q0DJqovup6Kl2OipUZ4lCEas0BGjcX51J9 JVRUrbL6Qpyj7tMZWtmXOUgP9oFqQC116k116DECpseJK3hBU8AGmqf83gZePNFZEAyp FIajbu9wkXBLPurCvFNgyMfHRs90gQE15FPN6aAnn44AFDF+YQB09GGtrI9ygg7zTxLZ FaGJE695m22z0/o+HfHdYGWVepN+selmKRrgA6sCyH8xLSOZ1eYyow4aXUC9PRjth2Rx Bu836SqoX5pjs4GLVxDZwW0Pp1lPWrBpxfhFoVrhH36qEgT56lQfTE7svPwkTRmdHV+Z UoLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712308649; x=1712913449; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LX/ma26TDvYVdpCIWg6iUhgi/+t6jX0tkNjGdFErDfk=; b=WxG6H3i/Kyxx1dqweEV97VTZXatzVNm7XaOtSgEe9GIhovxVT0WSWcu09P2YVcHaRJ C3uRz5UsCtVVjfMWdjzhwHXUFNN9r5LTRBCTHPbMZ2OcWMsb3NaNMMuHARe00KWHaZKQ 8XxgcOTrHmlt+3TcfF+2GhKYNtscNgC/oh2lslGGBihGdXji3HikPZZDChomCnsx8SrI HhJHk1mtF/aPkjtarl6CriqKuD5GfboNC5A0m3C9e1UwzgyTGfSpBr9pJeNcgQrpSZY2 UUjQAP1enyQAxs9XY3E5uiWU4gGcxcRHBcX5gnuFMwoIBv3szXm6pClSyWQxctGAWpQ3 E7Qg== X-Forwarded-Encrypted: i=1; AJvYcCUEIlqKh0a50/dMExoO3GYBRsrA4OTJvKBAl8bcBzx70QhmC/0pX39m1n6QmPhh0VtOwQK142riwmm2GSSGKF5sXW613IbiaVAVCFkUKFpMIKtglhvBC0Mu7mdMgEuhoChP X-Gm-Message-State: AOJu0YxKSUmYKDRl8/LfGcjSV/z7fQGofcgFMKWl2oX4QVj68oIJMvJ2 2BtQ7QDpXE3jMVgq2WOCqhT/qfatjlz2UnIwTgn1qrugcKFk8LimuHD2fDl9KlyPaeyx X-Received: by 2002:a05:600c:4f41:b0:416:1df0:7c06 with SMTP id m1-20020a05600c4f4100b004161df07c06mr933170wmq.6.1712308648855; Fri, 05 Apr 2024 02:17:28 -0700 (PDT) Received: from localhost (54-240-197-231.amazon.com. [54.240.197.231]) by smtp.gmail.com with ESMTPSA id v13-20020a05600c444d00b00416306c17basm1017286wmn.14.2024.04.05.02.17.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 Apr 2024 02:17:28 -0700 (PDT) From: Puranjay Mohan To: Catalin Marinas , Will Deacon , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Zi Shen Lim , Xu Kuohai , Florent Revest , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: puranjay12@gmail.com Subject: [PATCH bpf-next] arm64, bpf: add internal-only MOV instruction to resolve per-CPU addrs Date: Fri, 5 Apr 2024 09:17:07 +0000 Message-Id: <20240405091707.66675-1-puranjay12@gmail.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Support an instruction for resolving absolute addresses of per-CPU data from their per-CPU offsets. This instruction is internal-only and users are not allowed to use them directly. They will only be used for internal inlining optimizations for now between BPF verifier and BPF JITs. Since commit 7158627686f0 ("arm64: percpu: implement optimised pcpu access using tpidr_el1"), the per-cpu offset for the CPU is stored in the tpidr_el1/2 register of that CPU. To support this BPF instruction in the ARM64 JIT, the following ARM64 instructions are emitted: mov dst, src // Move src to dst, if src != dst mrs tmp, tpidr_el1/2 // Move per-cpu offset of the current cpu in tmp. add dst, dst, tmp // Add the per cpu offset to the dst. If CONFIG_SMP is not defined, then nothing is emitted if src == dst, and mov dst, src is emitted if dst != src. To measure the performance improvement provided by this change, the benchmark in [1] was used: Before: glob-arr-inc : 23.597 ± 0.012M/s arr-inc : 23.173 ± 0.019M/s hash-inc : 12.186 ± 0.028M/s After: glob-arr-inc : 23.819 ± 0.034M/s arr-inc : 23.285 ± 0.017M/s hash-inc : 12.419 ± 0.011M/s [1] https://github.com/anakryiko/linux/commit/8dec900975ef Signed-off-by: Puranjay Mohan --- arch/arm64/include/asm/insn.h | 7 +++++++ arch/arm64/lib/insn.c | 11 +++++++++++ arch/arm64/net/bpf_jit.h | 6 ++++++ arch/arm64/net/bpf_jit_comp.c | 16 ++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/arch/arm64/include/asm/insn.h b/arch/arm64/include/asm/insn.h index db1aeacd4cd9..d16d68550c22 100644 --- a/arch/arm64/include/asm/insn.h +++ b/arch/arm64/include/asm/insn.h @@ -135,6 +135,11 @@ enum aarch64_insn_special_register { AARCH64_INSN_SPCLREG_SP_EL2 = 0xF210 }; +enum aarch64_insn_system_register { + AARCH64_INSN_SYSREG_TPIDR_EL1 = 0xC684, + AARCH64_INSN_SYSREG_TPIDR_EL2 = 0xE682, +}; + enum aarch64_insn_variant { AARCH64_INSN_VARIANT_32BIT, AARCH64_INSN_VARIANT_64BIT @@ -686,6 +691,8 @@ u32 aarch64_insn_gen_cas(enum aarch64_insn_register result, } #endif u32 aarch64_insn_gen_dmb(enum aarch64_insn_mb_type type); +u32 aarch64_insn_gen_mrs(enum aarch64_insn_register result, + enum aarch64_insn_system_register sysreg); s32 aarch64_get_branch_offset(u32 insn); u32 aarch64_set_branch_offset(u32 insn, s32 offset); diff --git a/arch/arm64/lib/insn.c b/arch/arm64/lib/insn.c index a635ab83fee3..b008a9b46a7f 100644 --- a/arch/arm64/lib/insn.c +++ b/arch/arm64/lib/insn.c @@ -1515,3 +1515,14 @@ u32 aarch64_insn_gen_dmb(enum aarch64_insn_mb_type type) return insn; } + +u32 aarch64_insn_gen_mrs(enum aarch64_insn_register result, + enum aarch64_insn_system_register sysreg) +{ + u32 insn = aarch64_insn_get_mrs_value(); + + insn &= ~GENMASK(19, 0); + insn |= sysreg << 5; + return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, + insn, result); +} diff --git a/arch/arm64/net/bpf_jit.h b/arch/arm64/net/bpf_jit.h index 23b1b34db088..b627ef7188c7 100644 --- a/arch/arm64/net/bpf_jit.h +++ b/arch/arm64/net/bpf_jit.h @@ -297,4 +297,10 @@ #define A64_ADR(Rd, offset) \ aarch64_insn_gen_adr(0, offset, Rd, AARCH64_INSN_ADR_TYPE_ADR) +/* MRS */ +#define A64_MRS_TPIDR_EL1(Rt) \ + aarch64_insn_gen_mrs(Rt, AARCH64_INSN_SYSREG_TPIDR_EL1) +#define A64_MRS_TPIDR_EL2(Rt) \ + aarch64_insn_gen_mrs(Rt, AARCH64_INSN_SYSREG_TPIDR_EL2) + #endif /* _BPF_JIT_H */ diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 76b91f36c729..e9ad9f257a18 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -877,6 +877,17 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, emit(A64_ORR(1, tmp, dst, tmp), ctx); emit(A64_MOV(1, dst, tmp), ctx); break; + } else if (insn_is_mov_percpu_addr(insn)) { + if (dst != src) + emit(A64_MOV(1, dst, src), ctx); +#ifdef CONFIG_SMP + if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN)) + emit(A64_MRS_TPIDR_EL2(tmp), ctx); + else + emit(A64_MRS_TPIDR_EL1(tmp), ctx); + emit(A64_ADD(1, dst, dst, tmp), ctx); +#endif + break; } switch (insn->off) { case 0: @@ -2527,6 +2538,11 @@ bool bpf_jit_supports_arena(void) return true; } +bool bpf_jit_supports_percpu_insn(void) +{ + return true; +} + void bpf_jit_free(struct bpf_prog *prog) { if (prog->jited) { -- 2.40.1