Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1130250lqm; Thu, 2 May 2024 06:17:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWWmTkwL8SAah9O92VUUWUcwlpRpNSumbXEwG7cP8fzmfB3DG9/qnifgcLHXgRAaBypbzfwErEbHKinqcFfCU0gs8BBTCRMCHBTLdjYvw== X-Google-Smtp-Source: AGHT+IE2Tzde25mXHYFQ7cCugLUdkGyTC7/URO0ocl2O72IVB5YDjfG1WQ5HJlTE/kbEF3GasRKr X-Received: by 2002:a05:6214:d02:b0:6a0:d3df:d965 with SMTP id 2-20020a0562140d0200b006a0d3dfd965mr6224510qvh.62.1714655842586; Thu, 02 May 2024 06:17:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714655842; cv=pass; d=google.com; s=arc-20160816; b=unF45dqWYSNIIb+fvGEY9J2nu0NH9fvdmc7ZApvCtAHqPHGAdtOHtbIoPq/Lwomg91 zWL9mVEq9p5FrUTiGvVq62TGeqQdpxRql4djfF3MzAzg+wL8i/HFAJoqgy/+XDKHslZZ TNU8lrjdzQbYVEiKcsENtrhBTjYxtXTNZC5Sjaj8bxKuukNLb2UL3vacvJYRP8R0daaW vACut2Xtm3mN4VPeD+Pl4rg69VGrTglodWLw4j4W8EwvTstB86DVNvL2F87WVFHlOTLl rhM9cjpiozJBJxEnXTbgStnqrqCmn9UtVidglibQYpC6xU+XLUo2J4LtAav9GpQM0jCY oqKw== 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:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=wzErqxxR/YW3fkTHnSDYSc9Tc1OL+l0lOMoyrDsVmyM=; fh=fv4hk75Zo2I/ymdpc6gumselWVGyt/myGQUq4wM6S6Y=; b=TbtPDuWRfJMAck+1PslcA2mGyKGnYvEGxuXO8U0/EkaA1snQDqzIfMnTVV8MRXKqsq GBbdamlC5ZlAIiQ6GImmMg+Ow3Ovk2DSjfdX4c8pc3vIhKUzO4wnkFww0rfwX0Kd1Zgx ECPV429ZASqy6vXc40ipWCljVQZBM2wyrY2wbX8bGSoyNsnDSsByXE26FbByEejVa0KS zcU+tip0beVg751kL5n/xuRy5h5kohFoUgSiy20lYmMXo8Trk33+7pUV3D9mxAhdmVHN 8ny3zWv4bxpeIZAGeEh4rKp994sVjttG2TREgYkEUhL3FayY4LkSicya7CT812I3oGAn rZ1Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sY3nTQsk; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-166494-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166494-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id kk6-20020a056214508600b006a09382d900si913327qvb.66.2024.05.02.06.17.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 06:17:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-166494-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=pass header.i=@kernel.org header.s=k20201202 header.b=sY3nTQsk; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-166494-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166494-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 4DE1C1C22392 for ; Thu, 2 May 2024 13:17:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D4E883CD0; Thu, 2 May 2024 13:16:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sY3nTQsk" 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 4D13DC8F3; Thu, 2 May 2024 13:16:55 +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=1714655816; cv=none; b=PbKkaky+STJAoIRcQ9dYkEccmfdJCVTplv/AV/cRPwIBeuGpAB5ENNkpfyLwjSq8DTF7h7wBvFxNG4LBjObvlQWhgKsLXUPHGkGFQPastsFI8cI1YUtLnnH00BFN2csHQAK74gmjHPnWfAZQHEY3Q7nO/6z7qj03hM4l5nHci2c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714655816; c=relaxed/simple; bh=w6H08j+R8foRTFOw41AFNnzrEJ9YvknLQmjZmPzZqeg=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=NVIbvCFdGtKlhLHRj9P+xdiJeFlOGoCrTy6/l33aC3ZkNPQ5NfcOwLsGSMWvWipeZ6NxS14JLQK3sl4caZ8Re93heZSabjoBCDDxOoSz4+e3v0ya8tFN8km8v/jPtwHSVEFS0MtL4uzrkMn1Qm8yNJZvM4vAjxfMoMasuOdVTBo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sY3nTQsk; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78E1DC113CC; Thu, 2 May 2024 13:16:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714655815; bh=w6H08j+R8foRTFOw41AFNnzrEJ9YvknLQmjZmPzZqeg=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=sY3nTQskk7vNgYeyYTzpB4pbuFH03SPkrPHSPWha53MbJ2I9QaBvetSVC9FLQA9Cb Rr16e80Wxtm36HemMa8595Zrk4tAH5ovpMruDcdGy5KOBYXJb9OtvnEU+U9eEmwLkM KK9Q5afWGBJsY+cjlw2SnJ55eLFALNedVkBi51lX+7vrDSkzYsidf/Cw0FJ+HPQmAE PDFWs2L2tv/NanO2s55if5kvu+W3hreqLwmbDXH/WdYuJQs7vKA4/EJYBn1yJac+gq Ycl4ToG9I5wqlQ0G6/TU7M88c7ns413EvU0ChjaMOSyhqRm7/0TXCPiZfRZWOXtxxI ekb0tn/7bVlqA== From: Puranjay Mohan To: Andrii Nakryiko 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 Subject: Re: [PATCH bpf-next v2 2/2] riscv, bpf: inline bpf_get_smp_processor_id() In-Reply-To: References: <20240430175834.33152-1-puranjay@kernel.org> <20240430175834.33152-3-puranjay@kernel.org> Date: Thu, 02 May 2024 13:16:52 +0000 Message-ID: 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: quoted-printable Andrii Nakryiko writes: > 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"). I feel in x86-64's case JIT can not do a (much) better job compared to the current approach in the verifier. On RISC-V and ARM64, JIT was able to do it better because both of these architectures save a pointer to the task struct in a special CPU register. As x86-64 doesn't have enough extra registers, it uses a percpu variable to store task struct, thread_info, and the cpu number. P.S. - While doing this for BPF, I realized that ARM64 kernel code is also not optimal as it is using the percpu variable and is not reading the CPU register directly. So, I sent a patch[1] to fix it in the kernel and get rid of the per-cpu variable in ARM64. [1] https://lore.kernel.org/all/20240502123449.2690-2-puranjay@kernel.org/ > Anyways, great work, a small nit below. > > Acked-by: Andrii Nakryiko Thanks, Puranjay