Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1301428lqd; Thu, 25 Apr 2024 11:09:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX18k6OytHBxYtEvq9kQOOBQvFo+l2KdRa2ZQrAF01Edp78YtlrEN/j+qBfJZx9hGXSSgh7Dr7S+owRVI8zwURbm9f0BARI7wv1zOljXQ== X-Google-Smtp-Source: AGHT+IFcLX6ie3h8hpG2MXcC5VY5w2FZeT6YMkr1OFvaCWxvrWArvCP2r5lczUZzls1r2N74irAk X-Received: by 2002:a05:6e02:148e:b0:36b:12f5:14dc with SMTP id n14-20020a056e02148e00b0036b12f514dcmr633568ilk.32.1714068593482; Thu, 25 Apr 2024 11:09:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714068593; cv=pass; d=google.com; s=arc-20160816; b=OsUu27B5Z/tryrzruUe2kLqwedmc455XWnH83/5IqPGMTpJS3sS+bRHlSX+/NrCOj5 PKHKfEeN6ZW/3Qm5bSh+ldZysldHoUB8VShhvOBAlVeFQ5v4cTKejFHb0zKS3HVHpD++ +V1jDHSF4Eb4xigUuOtxFzK1dhRqMl52q24minXjPSdZHpV4bsIFtuey/NR8kTizVqes AYHs75frYVN6w0rkU9/w+pdAfmIDEqQt/mOCU66tvgyM65HrgvUM9aj1JTCWr4GsEYAC +UyVseDTJlakdw94TxKi1cr8II6AisQaPafvgYXOeAEBebAiEprHr4ZgnYslqB9jWD0R 4Enw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=kUfRY8/TaMSQGOdro4l4cqF/rmzCcmj41uoC+1ic+Ok=; fh=tumru1+/6KFQbOQbC7M6WMK56ygZ/9BtCZBL4aPBiqg=; b=fXDNHQRj2jt+A5IhyzbkZ0aixjABJ2lLsjjwFIe1ZdP8Vvxp2sU71U5HpemKufY8z/ IA7krC621u0XSTmCBOoS2eP9hruoT45/pr4VxgRn/DKq7y7O48KcMx3b/u8bCCjZ5Mic v9OQUcFRTdGZE/HkIfnwBsCc7cx/dfD9f94C0oCUKW8Qh1wViVRbnKl83VH0PoRFoERy Wsjy7AetNu0gLRHhq/MzIRl+lIwsK/BEQYEO9nPSCdeuKAfxLQHAaz9FYAZk3It6sMH/ FsOe5ZjX6yNCOTqHPOTMhu9AJJ9C9TmOXp5qKm06HGRfIRSxyE5w2Br3H+YhV8UsxMjV hd5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=EQd1vIFv; 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-158996-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158996-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id t6-20020a63b246000000b005f7f6a880dasi10044318pgo.161.2024.04.25.11.09.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 11:09:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158996-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=EQd1vIFv; 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-158996-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158996-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id F06F5282290 for ; Thu, 25 Apr 2024 18:09:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A54B514F116; Thu, 25 Apr 2024 18:09:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EQd1vIFv" Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (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 3824C14EC41; Thu, 25 Apr 2024 18:09:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714068577; cv=none; b=Y1I7VYoy+on2i4s0xzWLa0sDcnnZjJQpF6xjFu0YQro8EOVoJ8ZAe1ZtRIgeiCfbVS+6a1hFdfSiCaWqcF2M8vuW2AMwAUbtnoTThQMGATVDnTew32CXKU5ASW9L163mQbkCp8w9LzHOhHtpLvSTFYGWqT8QBQXubk5aajVuxvQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714068577; c=relaxed/simple; bh=FhVxwyldaMWlZYLBUnistSextrhu2pSDcd1+kY++dfI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=PwIOu10gWKwtBTSvQA7MxrRkno+CPA7NNYAMUvyxxCB06FVxVdEl8Xn4ECbonXhMLTrkpAQM6TWyn5sX5uq/iMvg+88eGZWHu4B4eN/NkqsXP/AUvvGJMYM0gOnhTlS+uktO41IyLT0OcpMEb1Zept56RQiAC7Z2JKt/wXdmHOQ= 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=EQd1vIFv; arc=none smtp.client-ip=209.85.215.178 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-pg1-f178.google.com with SMTP id 41be03b00d2f7-60c49bdbcd3so232988a12.0; Thu, 25 Apr 2024 11:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714068575; x=1714673375; 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=kUfRY8/TaMSQGOdro4l4cqF/rmzCcmj41uoC+1ic+Ok=; b=EQd1vIFvHmDKphKqo6Mcf2rZu2Kq9RYgDs7mEby49ACFOltugioj7WWBZhM7T8WK1n /PJaZ6Bmmxi7UtqAf0PFO2Ww0Ye1nX0bjsvRR+5FCbCTdXBhWQiCVzEYgC4g6MFVICXi ETywf4dAb8fllVeDUQ0gt5yDtIFSi4D3E/dJzVHIPYTk75SYdMEWnN5fsKB2gMWw1aMl 8qaqA7egIqrvWemGaAzxaMdXuzkgPXmVeDEyrGb85iUTfU0ANumYUr/ksO5qgB1KoNMZ 9rXJ1w8AWxIEybBkaO/7UK8y4TomhAHTElgxLRQaBqB2IT3QxMVep9HbJhyPRhrG6UXM qg2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714068575; x=1714673375; 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=kUfRY8/TaMSQGOdro4l4cqF/rmzCcmj41uoC+1ic+Ok=; b=V2LB3DAdL1Oe8z6sjh+53wx2RRbHZfpQyKUjMd1AxKqaOV2IK6zOksTxLN/e6fPMuO Vh+cgBjNbeiENhiBG/DYYlbEzlI0Nnpvk07r/c5wPLgv0HP9jImZQS/4MGfE5x/p+ioG Kgku/A63DCzBtFb6Cm85InHt3LhSMMghRxlPE1vUHs9TeqMfpGq6SYrxVWQp42ZmYreT WcNiphPbYV05L0wTnKTmU6ab3/9cgugNKh4kf+f8DX+QDYfsnoKl+yBl12F4SFlNVAAf HT2boEEzt14Z6n4ckUXebYgtlzS4USvOrdWIYCLN2X58L+AAw5+ISjkY73/9D6afpFmC hc0g== X-Forwarded-Encrypted: i=1; AJvYcCVrsw7a5xQFixIBCTRP+RhW7Gu8IQsSNkDLjX2Vm9DkuRmwvwvX3Ku6ictBekGJaw2Rw4L6lyi7N+VBsgCZJgHHIzjwJdI/9pTDtttHEEg2py2MXz1cGefsU3QRiRcuCgjX X-Gm-Message-State: AOJu0YxE+tgO8WOQ74lSldlpRouoQZ94sIiiBPL8rlirGDwFuM6S/sm4 bFdtUVzIvFypcyEZub9glkdH3r2OZDGV8umIVkG9mVsMo4bv1lzZFyyZ6J5MYu2Fdfa6h/HWBzY sRuf0/wImPzor8VxfEeJVnaCDg+Y= X-Received: by 2002:a17:90b:3446:b0:2ad:be01:6bb9 with SMTP id lj6-20020a17090b344600b002adbe016bb9mr324227pjb.48.1714068575425; Thu, 25 Apr 2024 11:09:35 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240424173550.16359-1-puranjay@kernel.org> <20240424173550.16359-3-puranjay@kernel.org> In-Reply-To: From: Andrii Nakryiko Date: Thu, 25 Apr 2024 11:09:22 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 25, 2024 at 3:14=E2=80=AFAM Puranjay Mohan wrote: > > Andrii Nakryiko writes: > > > On Wed, Apr 24, 2024 at 10:36=E2=80=AFAM Puranjay Mohan wrote: > >> > >> As ARM64 JIT now implements BPF_MOV64_PERCPU_REG instruction, inline > >> bpf_get_smp_processor_id(). > >> > >> ARM64 uses the per-cpu variable cpu_number to store the cpu id. > >> > >> Here is how the BPF and ARM64 JITed assembly changes after this commit= : > >> > >> BPF > >> =3D=3D=3D=3D=3D > >> BEFORE AFTER > >> -------- ------- > >> > >> int cpu =3D bpf_get_smp_processor_id(); int cpu =3D bpf_get_= smp_processor_id(); > >> (85) call bpf_get_smp_processor_id#229032 (18) r0 =3D 0xffff8000= 82072008 > >> (bf) r0 =3D r0 > > > > nit: hmm, you are probably using a bit outdated bpftool, it should be > > emitted as: > > > > (bf) r0 =3D &(void __percpu *)(r0) > > Yes, I was using the bpftool shipped with the distro. I tried it again > with the latest bpftool and it emitted this as expected. Cool, would be nice to update the commit message with the right syntax for next revision, thanks! > > > > >> (61) r0 =3D *(u32 *)(r= 0 +0) > >> > >> 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_p= rocessor_id(); > >> mov x10, #0xfffffffffffff4d0 mov x7, #0xffff8000ffff= ffff > >> movk x10, #0x802b, lsl #16 movk x7, #0x8207, lsl #1= 6 > >> movk x10, #0x8000, lsl #32 movk x7, #0x2008 > >> blr x10 mrs x10, tpidr_el1 > >> add x7, x0, #0x0 add x7, x7, x10 > >> ldr w7, [x7] > >> > >> Performance improvement using benchmark[1] > >> > >> BEFORE AFTER > >> -------- ------- > >> > >> glob-arr-inc : 23.817 =C2=B1 0.019M/s glob-arr-inc : 24.6= 31 =C2=B1 0.027M/s > >> arr-inc : 23.253 =C2=B1 0.019M/s arr-inc : 23.7= 42 =C2=B1 0.023M/s > >> hash-inc : 12.258 =C2=B1 0.010M/s hash-inc : 12.6= 25 =C2=B1 0.004M/s > >> > >> [1] https://github.com/anakryiko/linux/commit/8dec900975ef > >> > >> Signed-off-by: Puranjay Mohan > >> --- > >> kernel/bpf/verifier.c | 11 ++++++++++- > >> 1 file changed, 10 insertions(+), 1 deletion(-) > >> > > > > Besides the nits, lgtm. > > > > Acked-by: Andrii Nakryiko > > > >> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > >> index 9715c88cc025..3373be261889 100644 > >> --- a/kernel/bpf/verifier.c > >> +++ b/kernel/bpf/verifier.c > >> @@ -20205,7 +20205,7 @@ static int do_misc_fixups(struct bpf_verifier_= env *env) > >> goto next_insn; > >> } > >> > >> -#ifdef CONFIG_X86_64 > >> +#if defined(CONFIG_X86_64) || defined(CONFIG_ARM64) > > > > I think you can drop this, we are protected by > > bpf_jit_supports_percpu_insn() check and newly added inner #if/#elif > > checks? > > If I remove this and later add support of percpu_insn on RISCV without > inlining bpf_get_smp_processor_id() then it will cause problems here > right? because then the last 5-6 lines inside this if(){} will be > executed for RISCV. Just add #else return -EFAULT; #endif ? I'm trying to avoid this duplication of the defined(CONFIG_xxx) checks for supported architectures. > > > > >> /* Implement bpf_get_smp_processor_id() inline. */ > >> if (insn->imm =3D=3D BPF_FUNC_get_smp_processor_id && > >> prog->jit_requested && bpf_jit_supports_percpu_ins= n()) { > >> @@ -20214,11 +20214,20 @@ static int do_misc_fixups(struct bpf_verifie= r_env *env) > >> * changed in some incompatible and hard to su= pport > >> * way, it's fine to back out this inlining lo= gic > >> */ > >> +#if defined(CONFIG_X86_64) > >> insn_buf[0] =3D BPF_MOV32_IMM(BPF_REG_0, (u32)= (unsigned long)&pcpu_hot.cpu_number); > >> insn_buf[1] =3D BPF_MOV64_PERCPU_REG(BPF_REG_0= , BPF_REG_0); > >> insn_buf[2] =3D BPF_LDX_MEM(BPF_W, BPF_REG_0, = BPF_REG_0, 0); > >> cnt =3D 3; > >> +#elif defined(CONFIG_ARM64) > >> + struct bpf_insn cpu_number_addr[2] =3D { BPF_L= D_IMM64(BPF_REG_0, (u64)&cpu_number) }; > >> > > > > this &cpu_number offset is not guaranteed to be within 4GB on arm64? > > Unfortunately, the per-cpu section is not placed in the first 4GB and > therefore the per-cpu pointers are not 32-bit on ARM64. I see. It might make sense to turn x86-64 code into using MOV64_IMM as well to keep more of the logic common. Then it will be just the difference of an offset that's loaded. Give it a try? > > > > >> + insn_buf[0] =3D cpu_number_addr[0]; > >> + insn_buf[1] =3D cpu_number_addr[1]; > >> + insn_buf[2] =3D BPF_MOV64_PERCPU_REG(BPF_REG_0= , BPF_REG_0); > >> + insn_buf[3] =3D BPF_LDX_MEM(BPF_W, BPF_REG_0, = BPF_REG_0, 0); > >> + cnt =3D 4; > >> +#endif > >> new_prog =3D bpf_patch_insn_data(env, i + delt= a, insn_buf, cnt); > >> if (!new_prog) > >> return -ENOMEM; > >> -- > >> 2.40.1 > >>