Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp599462lqm; Wed, 1 May 2024 09:50:40 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV9LbZ2z2dQ7Qs96ng/Hss5CyJfSpNRJRychCiTNN6ytH0GdFsMNGsAyhcH+qyicos7iY+KQU5iNxcRw/bclW+H7pf9UDzI4/k3tN4qfg== X-Google-Smtp-Source: AGHT+IEqYM1nZmXRWIt9+wbbytZLYN3a542OeSC+ez5trEsjW50Nz1EkdtTFYA93m6F/8CQA9Ewd X-Received: by 2002:a9d:3e10:0:b0:6e6:c84b:4e2c with SMTP id a16-20020a9d3e10000000b006e6c84b4e2cmr3022297otd.22.1714582240259; Wed, 01 May 2024 09:50:40 -0700 (PDT) Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id u8-20020a05620a084800b0079088fab85esi18986298qku.147.2024.05.01.09.50.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 09:50:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165606-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=bw4iEbZq; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-165606-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165606-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E495E1C209DA for ; Wed, 1 May 2024 16:50:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5331013ADA; Wed, 1 May 2024 16:50:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bw4iEbZq" Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 E97783234; Wed, 1 May 2024 16:50:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714582231; cv=none; b=uwEexJWVPBT+UXSE4rNJrBNqZOQH2u1KQ0/Sh4lmXfTa5BsX6CD9OU9HCm9KM47uGNo+28Ntp16trdOSkUryRW8ClneCPZoTjV703O3v4/8f4sMpQWYgj41qMug2RIj3hvIjiljVRtzKwSgvb0W+AvMlajR413Qehs//bb+RnDU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714582231; c=relaxed/simple; bh=T5uWjQn0TdTai7lZsSs6aSnBx3aKCjraRrFmpU3ySSc=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=R1IMewGxdvRNyMcOpAMkIfz7dVDvi2/u4qwCH2+kvRygrqgGijN86dMhibCDLw3rIfM4yGEh1N6cuFhPAt7lgw6kFakSGhu16xS1QMx492sRj0CsCOS+1PuOxULxk98Ia/qdiInA/EHeiXPXCPksPsLbwEluTaaqTWX9XtgEbzA= 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=bw4iEbZq; arc=none smtp.client-ip=209.85.214.176 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-pl1-f176.google.com with SMTP id d9443c01a7336-1ec69e3dbe5so14030255ad.0; Wed, 01 May 2024 09:50:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714582229; x=1715187029; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Aksft0yTn/V06U1VI9NL6+QbOurl0GnC7c4G4bkaC4s=; b=bw4iEbZqMgtryJK8jVcUh+L7aGw1WXn2xuw+mp3DnHua2S0t8xrhjUNBua6TmyRQly aW3TV9PEoa8gx/T2fh7IcGG9SjdaYzKCW8YU+BU7BQ/AQgKwjnRAvx6Xi9eFrl4i27e2 9HwiVAeq2H0CqobYNmNrp3oYQfEPbELWJooN4yqVn68QgsIkF2BSuOjqTO/dIvAVcGJV SnhfDIZQGAGnvmS25JfUsstTaz9xD6xE2Wts81ycNaztE3CAlNcRew0Jj6onbEMsRccp gb0GpokqfcIUj/yjTcg38Sg8kaPC9vScq9dsoNw869ZwMJdDUkE3km1Ke863H2bC2yXZ hXaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714582229; x=1715187029; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Aksft0yTn/V06U1VI9NL6+QbOurl0GnC7c4G4bkaC4s=; b=Kc3NfFKi8Go7+RFk+QXQUrdVC0RjZVpdot88TdPWVCXsMMGUMpHlauKH4/o/NT2bTN WOO3hRrajzlq2R2DTQKAa4YC/Dr0xmoklJZ7l+XQ+onqZ2NV5hkWd4jQWNOfa4Orz9e8 /XcULzsdT0tF6K/C1srTpa+Ro/DKaJicy76fhdZkHzX82YlKCDj6bK2WbNC7r9udwqGD Pp0ude6FMHhNVdcIEeujKujAGf4S1kGSXplzSkfTpWFd9BkaPnGBvkPu7H1qmUuU2MGL Fu3cPUJ9t+0sI8MY/TAnxmeeYHeDSkPbSMhX9vBvvOod3DiW3sayMy7hSvTnBVNmr2Kx 5ShQ== X-Forwarded-Encrypted: i=1; AJvYcCUEOpzP7o4uRNRXm+Fgvank3qORKMczukEmA+Jq0aKAx34Kc9yeZnjosZZrY3mI0VJZc2axtPVRQrSe9fPk5SW06/Hs2whltD+3MHwlQyGF0zYGzn2bsaiSqYX8N1jzHTvo X-Gm-Message-State: AOJu0YwMz51EwFmliUBWnSy128l1l/f15vhAtZpjG0g7vz64y/zxTjUp 0NeA45ogi5LWxdi4sV0BzcAzdKr21ROfeq5zssCLERY2jwHvwlLWcYuIo9Lw15q11x0gOny0uNH rl8KDgs/aob/DBYxhQ259y02uUeQ= X-Received: by 2002:a17:90b:68e:b0:2a5:4e39:6a8 with SMTP id m14-20020a17090b068e00b002a54e3906a8mr3089123pjz.33.1714582229167; Wed, 01 May 2024 09:50:29 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240430234739.79185-1-puranjay@kernel.org> <20240430234739.79185-3-puranjay@kernel.org> In-Reply-To: <20240430234739.79185-3-puranjay@kernel.org> From: Andrii Nakryiko Date: Wed, 1 May 2024 09:50:16 -0700 Message-ID: Subject: Re: [PATCH bpf-next v5 2/2] bpf, arm64: inline bpf_get_smp_processor_id() helper To: Puranjay Mohan Cc: 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 , puranjay12@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Apr 30, 2024 at 4:48=E2=80=AFPM Puranjay Mohan wrote: > > 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 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > BEFORE AFTER > -------- ------- > > int cpu =3D bpf_get_smp_processor_id(); int cpu =3D bpf_get_smp_pr= ocessor_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 =C2=B1 1.675M/s | 25.893 =C2=B1 0.026M/s | + 1= 0.74% | > | arr-inc | 23.928 =C2=B1 0.034M/s | 25.213 =C2=B1 0.063M/s | + 5= 37% | > | hash-inc | 12.352 =C2=B1 0.005M/s | 12.609 =C2=B1 0.013M/s | + 2= 08% | > +---------------+-------------------+-------------------+--------------+ > > [1] https://github.com/anakryiko/linux/commit/8dec900975ef > > Signed-off-by: Puranjay Mohan > --- > arch/arm64/include/asm/insn.h | 1 + > arch/arm64/net/bpf_jit.h | 2 ++ > arch/arm64/net/bpf_jit_comp.c | 23 +++++++++++++++++++++++ > 3 files changed, 26 insertions(+) > Nice improvements! I suggest combining arm64 and risc-v patches together when resubmitting, so that we can land them in one go. This one depends on RISC-V patches landing first to avoid a warning about global function without a prototype, right? Please add my ack as well Acked-by: Andrii Nakryiko > 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 =3D 0x4684, > AARCH64_INSN_SYSREG_TPIDR_EL2 =3D 0x6682, > + AARCH64_INSN_SYSREG_SP_EL0 =3D 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..8084f3e61e0b 100644 > --- a/arch/arm64/net/bpf_jit_comp.c > +++ b/arch/arm64/net/bpf_jit_comp.c > @@ -1215,6 +1215,19 @@ static int build_insn(const struct bpf_insn *insn,= struct jit_ctx *ctx, > const u8 r0 =3D bpf2a64[BPF_REG_0]; > bool func_addr_fixed; > u64 func_addr; > + u32 cpu_offset =3D offsetof(struct thread_info, cpu); > + > + /* Implement helper call to bpf_get_smp_processor_id() in= line */ > + if (insn->src_reg =3D=3D 0 && insn->imm =3D=3D BPF_FUNC_g= et_smp_processor_id) { > + 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 =3D bpf_jit_get_func_addr(ctx->prog, insn, extra_pass= , > &func_addr, &func_addr_fixed)= ; > @@ -2541,6 +2554,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; > + } > + > + return false; same minor nit to use default: return false inside the switch itself > +} > + > void bpf_jit_free(struct bpf_prog *prog) > { > if (prog->jited) { > -- > 2.40.1 >