Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp597605lqm; Wed, 1 May 2024 09:47:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXV+XR1OqSalQ+i20XBmTSTtzVxxec3VtEWrcvy6euEBSs09noz+zYSIrQHyRlZmF3tlIaTkSyvb9EW5haFXaE0NqhlJYuaKb9Nj9vz4g== X-Google-Smtp-Source: AGHT+IFKCTzECUAN/TVqgCzdxe2LnQxU2hQtqo93pq2K9ZiNUuZeolOFIgHNVouqTJZaZjznAqyW X-Received: by 2002:a17:907:1703:b0:a58:7982:6282 with SMTP id le3-20020a170907170300b00a5879826282mr2191596ejc.62.1714582043493; Wed, 01 May 2024 09:47:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714582043; cv=pass; d=google.com; s=arc-20160816; b=acRsv5mwbNz/KJIIZnPhJzQFq7jOQQO6a6x5Uaf62jra/R3MYe0QYKHSwvIVijFlOt ocIh9kzH6Pj0VLfP8U8AhaXOrOyFnmoqWVR9gtFYCQuS4gsAo9onPfG3UKXb5BuBZ1C/ FDTOtYPm45luGXgata7a+oBoqwybLiVBsHjg91dzLn2tYqdx0qq7aJ7g/LXdVahxuEdI NnnvGQHgCQHt45iR7pwmKPtngmuIjd85smov5cxFD0ScD70A3gHX3D+ffvZ8+111OjOi Q1AkHLL2AtfRFfwehHMioYNW39n45XwF47BPTux9YMBCUuH6v2ynrEKp7Ly09Fo3nIry UzYw== 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=jHP+c6lTfQ0DdbvCxrrnGweU4MtbNmvE+GdweOg/na4=; fh=wJYQ3/WJvrPwmks5x/3p1puIZKi5GOj7Pq/l1AETh2M=; b=n3tlUNjJdsqj/NhyZ+mwlU0osNMDlF15d8VlTuC5gg6JolXCEHHv+BEyIh2X7mEX9h CFih2tvzix/Y2P/HrD+FU8eLhxXa3UoHGAEhIbyxBjiltdGoO/y3o2fJM4xAEjokmrhe THuXG1KbS7cQghO4xVP8cHtQ2lqVvhen1Us6gHSbQ5y/6on6ms7vQMAK1K9mqkudImv1 +M/BgeOlRIiUXl/xMiyAKUXVKnSyNT3M29pN5pk4rYEjoDbs5HKqiwfDqZhOuC2qXJyw yEI7/oUiypiblG7F/BJM44KmQljPAsQHcqD3+cEvxFSbfiqrZ0RJfBoZrutc8u1RMb3i xoUA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=D2wqHq6S; 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-165603-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165603-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 hu9-20020a170907a08900b00a58d2b45b4csi6804878ejc.296.2024.05.01.09.47.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 09:47:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165603-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=D2wqHq6S; 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-165603-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165603-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 15CA71F21BC7 for ; Wed, 1 May 2024 16:47:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AC3FA1759F; Wed, 1 May 2024 16:47:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D2wqHq6S" Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.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 06303FBE8; Wed, 1 May 2024 16:47:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714582029; cv=none; b=K1z/887els1TMEOiwRlgUFAjXIyIYbRK7IEbk0S14S9Yjhdyrkgf69ZAvOmqJwyoT1jaYLIKjsMf5ATq0HgR6ADsvPt8dySqSSUQvFUsTzMTfWvYpgeyU/gzKatM8OtrDxpEpJJLS6NjppEkpRHz91kztf3loc0gIjphApfb2As= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714582029; c=relaxed/simple; bh=e536afwY25Ws7+stPkl0xnIZHsIsxrvz5kZMx58cUVQ=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=hQnz2xefHr3DzBM2SshqNCEl47zhVgzaoHY/njhEhr6K62zaDnkUtpob4pl4PLtbU3/q+H36yleI7KIujR31Unmuf3HLag9Hy9s7O0/gH0VxIq3x1fk0bnsRi4eZhifQBGkeVAsEIDr/zSie/3CcH+J4y9vnA1A7mekVEHf6eCA= 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=D2wqHq6S; arc=none smtp.client-ip=209.85.215.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-pg1-f176.google.com with SMTP id 41be03b00d2f7-5f415fd71f8so5528344a12.3; Wed, 01 May 2024 09:47:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714582027; x=1715186827; 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=jHP+c6lTfQ0DdbvCxrrnGweU4MtbNmvE+GdweOg/na4=; b=D2wqHq6S4pzFwT20TNIkmS1VyMuu7J+unRxWqJbN2PHS7QrPHsZ9wG/mGpZCIG4w7V DpMbiV+AXhTWJ66S95qPoAzyBtzedtXyyIT7Sk8lEryik2HVaAG4fBkmNNVReuwX5zLz 3Y/IgD7i7Fc+GixstN0JGajIAfXobkRwlNrijgv80B5srBH1kjPA/rBJS3L2lCNBHPMX FhpdKiezvZIqx0KT9PMO3lbtd3aGWQUnqWh6CRFBw4T0zkqoyyAXDEhgJySLDW5vfEE8 +ftGlHfYVolkuO1Wuzolaw5DE4FYBxfSE1DJHAxNHnXyPkVydrB6JvmNAoB4GrNSvmfu FsjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714582027; x=1715186827; 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=jHP+c6lTfQ0DdbvCxrrnGweU4MtbNmvE+GdweOg/na4=; b=PoHB1oqtU3XxJgK8pQST56HQ5BMzxrge2xZ1IGHg3AStbjvlU/Pu3YqID+5t6ii6eB 7xvL+psgovYdQ0CSt3KSpA1W/QVeNrVyG/0y4y+gLBfq+U3VYwGmUj58TxXxThH10rGH aPwJI82crqFvIBwbC9v/f99J4q1tdLMjy64dVMUVgva5rEYf+NehtuWmLeOcBrGLguE7 DTC/kVcafhTRWxuBH4Cd/dXcpSV7qv284pQ6X6T8XWauwWkFzto0MrUGKrdWrFzbqtWD RvWv7w7xuErWKPxItI5bWmVg/6cjZDZWAGEnFof+/SFm/0QWVrfb3wx6RR0Q7LPP2pPY dXHg== X-Forwarded-Encrypted: i=1; AJvYcCUGcSBhqskbzKFgpIlGkglFrvGv6sPAjuAz22LeS2e6o7kfFasAUluuf28WZ5NkXFmkMZI8dyYJWJydbilp4BMMjxCpzm5fR15/aXsArRsZcCsKfIHgHYgLQ5Y7tFk+kvER X-Gm-Message-State: AOJu0Ywp5xqsy6K12Oz0iN/SENt9CfjkGjKlqljj5u4Qyz3QlE/9IMWR 5YHhLO6E7cM7CYQ1Wlv+d8zW5tHWgOxU/yc8a9Id0lSHnSNoNL+9Ro6TKtjaXsNTZJGxhaLYeF3 T4NsIdx7yFkvg8tl/lw7koh0q+/IcYXUu X-Received: by 2002:a17:90a:394c:b0:2ac:6355:c6dc with SMTP id n12-20020a17090a394c00b002ac6355c6dcmr2747597pjf.19.1714582027082; Wed, 01 May 2024 09:47:07 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240430175834.33152-1-puranjay@kernel.org> <20240430175834.33152-3-puranjay@kernel.org> In-Reply-To: <20240430175834.33152-3-puranjay@kernel.org> From: Andrii Nakryiko Date: Wed, 1 May 2024 09:46:55 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2 2/2] riscv, bpf: inline bpf_get_smp_processor_id() To: Puranjay Mohan Cc: 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 , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Paul Walmsley , Palmer Dabbelt , Albert Ou , bpf@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Pu Lehui , puranjay12@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Apr 30, 2024 at 10:59=E2=80=AFAM Puranjay Mohan wrote: > > Inline the calls to bpf_get_smp_processor_id() in the riscv bpf jit. > > RISCV saves the pointer to the CPU's task_struct in the TP (thread > pointer) register. This makes it trivial to get the CPU's processor id. > As thread_info is the first member of task_struct, we can read the > processor id from TP + offsetof(struct thread_info, cpu). > > RISCV64 JIT output for `call bpf_get_smp_processor_id` > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > > Before After > -------- ------- > > auipc t1,0x848c ld a5,32(tp) > jalr 604(t1) > mv a5,a0 > Nice, great find! Would you be able to do similar inlining for x86-64 as well? Disassembling bpf_get_smp_processor_id for x86-64 shows this: Dump of assembler code for function bpf_get_smp_processor_id: 0xffffffff810f91a0 <+0>: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 0xffffffff810f91a5 <+5>: 65 8b 05 60 79 f3 7e mov %gs:0x7ef37960(%rip),%eax # 0x30b0c 0xffffffff810f91ac <+12>: 48 98 cltq 0xffffffff810f91ae <+14>: c3 ret End of assembler dump. We should be able to do the same in x86-64 BPF JIT. (it's actually how I started initially, I had a dedicated instruction reading per-cpu memory, but ended up with more general "calculate per-cpu address"). Anyways, great work, a small nit below. Acked-by: Andrii Nakryiko > Benchmark using [1] on Qemu. > > ./benchs/run_bench_trigger.sh glob-arr-inc arr-inc hash-inc > > +---------------+------------------+------------------+--------------+ > | Name | Before | After | % change | > |---------------+------------------+------------------+--------------| > | glob-arr-inc | 1.077 =C2=B1 0.006M/s | 1.336 =C2=B1 0.010M/s | + 24.= 04% | > | arr-inc | 1.078 =C2=B1 0.002M/s | 1.332 =C2=B1 0.015M/s | + 23.= 56% | > | hash-inc | 0.494 =C2=B1 0.004M/s | 0.653 =C2=B1 0.001M/s | + 32.= 18% | > +---------------+------------------+------------------+--------------+ > > NOTE: This benchmark includes changes from this patch and the previous > patch that implemented the per-cpu insn. > > [1] https://github.com/anakryiko/linux/commit/8dec900975ef > > Signed-off-by: Puranjay Mohan > --- > arch/riscv/net/bpf_jit_comp64.c | 26 ++++++++++++++++++++++++++ > include/linux/filter.h | 1 + > kernel/bpf/core.c | 11 +++++++++++ > kernel/bpf/verifier.c | 2 ++ > 4 files changed, 40 insertions(+) > > diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_com= p64.c > index 99d7006f1420..5789b7afae47 100644 > --- a/arch/riscv/net/bpf_jit_comp64.c > +++ b/arch/riscv/net/bpf_jit_comp64.c > @@ -1493,6 +1493,22 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn,= struct rv_jit_context *ctx, > bool fixed_addr; > u64 addr; > > + /* Inline calls to bpf_get_smp_processor_id() > + * > + * RV_REG_TP holds the address of the current CPU's task_= struct and thread_info is > + * at offset 0 in task_struct. > + * Load cpu from thread_info: > + * Set R0 to ((struct thread_info *)(RV_REG_TP))->cpu > + * > + * This replicates the implementation of raw_smp_processo= r_id() on RISCV > + */ > + if (insn->src_reg =3D=3D 0 && insn->imm =3D=3D BPF_FUNC_g= et_smp_processor_id) { > + /* Load current CPU number in R0 */ > + emit_ld(bpf_to_rv_reg(BPF_REG_0, ctx), offsetof(s= truct thread_info, cpu), > + RV_REG_TP, ctx); > + break; > + } > + > mark_call(ctx); > ret =3D bpf_jit_get_func_addr(ctx->prog, insn, extra_pass= , > &addr, &fixed_addr); > @@ -2062,3 +2078,13 @@ 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; nit: why not default: return false; to keep everything within the switch? > +} > diff --git a/include/linux/filter.h b/include/linux/filter.h > index 7a27f19bf44d..3e19bb62ed1a 100644 > --- a/include/linux/filter.h > +++ b/include/linux/filter.h > @@ -993,6 +993,7 @@ u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u= 64 r5); > struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog); > void bpf_jit_compile(struct bpf_prog *prog); > bool bpf_jit_needs_zext(void); > +bool bpf_jit_inlines_helper_call(s32 imm); > bool bpf_jit_supports_subprog_tailcalls(void); > bool bpf_jit_supports_percpu_insn(void); > bool bpf_jit_supports_kfunc_call(void); > diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c > index 99b8b1c9a248..aa59af9f9bd9 100644 > --- a/kernel/bpf/core.c > +++ b/kernel/bpf/core.c > @@ -2941,6 +2941,17 @@ bool __weak bpf_jit_needs_zext(void) > return false; > } > > +/* Return true if the JIT inlines the call to the helper corresponding t= o > + * the imm. > + * > + * The verifier will not patch the insn->imm for the call to the helper = if > + * this returns true. > + */ > +bool __weak bpf_jit_inlines_helper_call(s32 imm) > +{ > + return false; > +} > + > /* Return TRUE if the JIT backend supports mixing bpf2bpf and tailcalls.= */ > bool __weak bpf_jit_supports_subprog_tailcalls(void) > { > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index 5d42db05315e..e78f766d7f91 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -20013,6 +20013,8 @@ static int do_misc_fixups(struct bpf_verifier_env= *env) > goto next_insn; > } > > + if (bpf_jit_inlines_helper_call(insn->imm)) > + goto next_insn; It's nice to be able to allow BPF JIT to do a higher-performance implementation. Let's add a short comment above to mention that this is bypassing normal inlining because BPF JIT will do it better (I know you have this description for the function definition, but a short remark here would be helpful). And please add an empty line after this check to logically separate it from the rest of helper inlining logic in verifier, thanks! pw-bot: cr > if (insn->imm =3D=3D BPF_FUNC_get_route_realm) > prog->dst_needed =3D 1; > if (insn->imm =3D=3D BPF_FUNC_get_prandom_u32) > -- > 2.40.1 >