Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1223599lqm; Thu, 2 May 2024 08:28:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW4S/Ok7HdlKZG6GrKSDmpEsjTF5l66EIYW0sOretJ/23fpPCqX/c5Sx/wHvQo7C0qV8yzSuhSWrnHLGrDgx7MeOFfvgkrtRU4w7hr7Bw== X-Google-Smtp-Source: AGHT+IHT6EbwOrpY7c3iTXISgRdlyeQNVXv26EiBYpy3M5UC2MKw0VzHN/trHNk46hRmStwDTRKK X-Received: by 2002:a05:6358:5910:b0:18d:9ebb:f006 with SMTP id g16-20020a056358591000b0018d9ebbf006mr2816755rwf.9.1714663732145; Thu, 02 May 2024 08:28:52 -0700 (PDT) Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id f14-20020a63e30e000000b0061a7099fed0si912484pgh.141.2024.05.02.08.28.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 08:28:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-166631-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@kernel.org header.s=k20201202 header.b=WhDthDON; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-166631-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166631-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 95C58289641 for ; Thu, 2 May 2024 15:20:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 71E5515E5DB; Thu, 2 May 2024 15:19:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WhDthDON" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FA5D15E5A7; Thu, 2 May 2024 15:19:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714663163; cv=none; b=mPuwwp2HWlgUXH7zPfINggBSubD6WsBlKqcxRcsuBBwFxKifNDEuPhy7vSAuzpAcNiD/G8OA9AWsSMNVbXFB2OaITj4TsWADHLOru6Z3qF1828iuBxNWGyEh9J5f0Vjcm10cXj6xKQtZNsm55mN15e0LSuEwEFFcUfZCo3BLZWE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714663163; c=relaxed/simple; bh=wLtf6Vhthcho1ROwU7Gg0KD5v6EHV93qrM8f17IjXSU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=rpNMf4m+9c1NdlRmHhujvsE7VzTCdphFmQ3mmOGW2g/6CPKAf0NTyB83J4LHE+kN/vuAuMiDxxGTgkOe7yBo7pjjy82WwbAnG7GJMdXVt1f8XessCtCSZQKWmQNZvFuuS7H4jw2ESpZdTyyfRJcuedhTCJgCeT9oOjcehLDEF4E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WhDthDON; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B66FDC4AF18; Thu, 2 May 2024 15:19:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714663163; bh=wLtf6Vhthcho1ROwU7Gg0KD5v6EHV93qrM8f17IjXSU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WhDthDON+xN2zOmrVb86uMduxB9JoK+964Ynr9bscOVgfionZPZrWJuvcY9km+UhX ujzzBdg6NEu+3i6KDHx6wvxjG4VNQ7BzbffVoP5ehB73JQQb2sR1Oxk/72OauDKTe5 TCeXXMRQQWFYmD114mphR44vWjKccBc/JS5VUXKOz6BYLUZxGYBtJH0kW7IKJDC6Yr 6rmZRdh8ywgwCD86LQe5g0B2R0TkMqqqThoJB48ipEnVJezZPCiuy4gGrbVMnS7b4o uK3Q7Eo7fcehiOqPO3jVliARlb3kqHbIwEZ7LpYiAv0g/pwcTY+EeFmxT/dxC7CVq0 kCnqQ1VqAU+Qg== 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, Kumar Kartikeya Dwivedi , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= Cc: puranjay12@gmail.com Subject: [PATCH bpf-next v6 4/4] bpf, arm64: inline bpf_get_smp_processor_id() helper Date: Thu, 2 May 2024 15:18:54 +0000 Message-Id: <20240502151854.9810-5-puranjay@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240502151854.9810-1-puranjay@kernel.org> References: <20240502151854.9810-1-puranjay@kernel.org> 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 Inline calls to bpf_get_smp_processor_id() helper in the JIT by emitting a read from struct thread_info. The SP_EL0 system register holds the pointer to the task_struct and thread_info is the first member of this struct. We can read the cpu number from the thread_info. Here is how the ARM64 JITed assembly changes after this commit: ARM64 JIT =========== BEFORE AFTER -------- ------- int cpu = bpf_get_smp_processor_id(); int cpu = bpf_get_smp_processor_id(); mov x10, #0xfffffffffffff4d0 mrs x10, sp_el0 movk x10, #0x802b, lsl #16 ldr w7, [x10, #24] movk x10, #0x8000, lsl #32 blr x10 add x7, x0, #0x0 Performance improvement using benchmark[1] /benchs/run_bench_trigger.sh glob-arr-inc arr-inc hash-inc +---------------+-------------------+-------------------+--------------+ | Name | Before | After | % change | |---------------+-------------------+-------------------+--------------| | glob-arr-inc | 23.380 ± 1.675M/s | 25.893 ± 0.026M/s | + 10.74% | | arr-inc | 23.928 ± 0.034M/s | 25.213 ± 0.063M/s | + 5.37% | | hash-inc | 12.352 ± 0.005M/s | 12.609 ± 0.013M/s | + 2.08% | +---------------+-------------------+-------------------+--------------+ [1] https://github.com/anakryiko/linux/commit/8dec900975ef Signed-off-by: Puranjay Mohan Acked-by: Andrii Nakryiko --- arch/arm64/include/asm/insn.h | 1 + arch/arm64/net/bpf_jit.h | 2 ++ arch/arm64/net/bpf_jit_comp.c | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/arch/arm64/include/asm/insn.h b/arch/arm64/include/asm/insn.h index 8de0e39b29f3..8c0a36f72d6f 100644 --- a/arch/arm64/include/asm/insn.h +++ b/arch/arm64/include/asm/insn.h @@ -138,6 +138,7 @@ enum aarch64_insn_special_register { enum aarch64_insn_system_register { AARCH64_INSN_SYSREG_TPIDR_EL1 = 0x4684, AARCH64_INSN_SYSREG_TPIDR_EL2 = 0x6682, + AARCH64_INSN_SYSREG_SP_EL0 = 0x4208, }; enum aarch64_insn_variant { diff --git a/arch/arm64/net/bpf_jit.h b/arch/arm64/net/bpf_jit.h index b627ef7188c7..b22ab2f97a30 100644 --- a/arch/arm64/net/bpf_jit.h +++ b/arch/arm64/net/bpf_jit.h @@ -302,5 +302,7 @@ 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) +#define A64_MRS_SP_EL0(Rt) \ + aarch64_insn_gen_mrs(Rt, AARCH64_INSN_SYSREG_SP_EL0) #endif /* _BPF_JIT_H */ diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index ed8f9716d9d5..1cebe9c92f51 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -1215,6 +1215,21 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, const u8 r0 = bpf2a64[BPF_REG_0]; bool func_addr_fixed; u64 func_addr; + u32 cpu_offset; + + /* Implement helper call to bpf_get_smp_processor_id() inline */ + if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) { + cpu_offset = offsetof(struct thread_info, cpu); + + emit(A64_MRS_SP_EL0(tmp), ctx); + if (is_lsi_offset(cpu_offset, 2)) { + emit(A64_LDR32I(r0, tmp, cpu_offset), ctx); + } else { + emit_a64_mov_i(1, tmp2, cpu_offset, ctx); + emit(A64_LDR32(r0, tmp, tmp2), ctx); + } + break; + } ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, &func_addr, &func_addr_fixed); @@ -2541,6 +2556,16 @@ bool bpf_jit_supports_percpu_insn(void) return true; } +bool bpf_jit_inlines_helper_call(s32 imm) +{ + switch (imm) { + case BPF_FUNC_get_smp_processor_id: + return true; + default: + return false; + } +} + void bpf_jit_free(struct bpf_prog *prog) { if (prog->jited) { -- 2.40.1