From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 658ECC369C2 for ; Mon, 21 Apr 2025 02:17:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ln+VsoO9KPDNvwVmf9j44KpIUAfprGCpmnU9QOz5Oo4=; b=pHsNX2hvfCkHyfTwsEBujfNEBa Zh1QIsGSLDAhhqiKg5Hi7QMGSM37bUdq8tid3U66fcVC2v6NAM4TrSiwngzlhC9o57wHbUv39aIv5 EiRgBwjaXyYTI6td9sI0DDmESLP49ddpyiy65FJex+N9ZizJwETJpNiYIJxI8yegBemCNg2hYa5Z2 DpCXH/QJMhe8Oc/uMic0u9zzlSfRlPeOUAus58kvukHPOuPiFMMaZaIAsEC9U4bLHnkB+UDJNFTLF QgYzd6hzP2a++FSNUxQiXg3FRPrlAhmdxBxRu/CG1nbOc4LKbxj7GoYN3uh8ZounsxhbzzrP5XE3W eTbRoucw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1u6giL-00000003P9w-0kHU; Mon, 21 Apr 2025 02:16:53 +0000 Received: from dggsgout11.his.huawei.com ([45.249.212.51]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1u6ggT-00000003P0b-1nvj for linux-arm-kernel@lists.infradead.org; Mon, 21 Apr 2025 02:14:59 +0000 Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZgppM6MTzz4f3mJ5 for ; Mon, 21 Apr 2025 10:14:19 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 4AA7B1A158A for ; Mon, 21 Apr 2025 10:14:45 +0800 (CST) Received: from [10.67.111.192] (unknown [10.67.111.192]) by APP2 (Coremail) with SMTP id Syh0CgDX4WQTqgVo5dAZKA--.45218S2; Mon, 21 Apr 2025 10:14:44 +0800 (CST) Message-ID: <8b800c09-eade-4dcf-90f6-2f5a78170bc4@huaweicloud.com> Date: Mon, 21 Apr 2025 10:14:43 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH RFC bpf-next 1/4] bpf: add struct largest member size in func model Content-Language: en-US To: =?UTF-8?Q?Alexis_Lothor=C3=A9?= , Xu Kuohai , Andrii Nakryiko Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Puranjay Mohan , Catalin Marinas , Will Deacon , Mykola Lysenko , Shuah Khan , Maxime Coquelin , Alexandre Torgue , Florent Revest , Bastien Curutchet , ebpf@linuxfoundation.org, Thomas Petazzoni , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kselftest@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com References: <20250411-many_args_arm64-v1-0-0a32fe72339e@bootlin.com> <20250411-many_args_arm64-v1-1-0a32fe72339e@bootlin.com> <9da88811-cce0-41df-8069-2e8b67541c39@huaweicloud.com> From: Xu Kuohai In-Reply-To: Content-Type: text/plain; charset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xVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWI evJa73UjIFyTuYvjTRM6wCDUUUU X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250420_191457_820531_579E9861 X-CRM114-Status: GOOD ( 27.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 4/21/2025 12:02 AM, Alexis Lothoré wrote: > Hi Xu, > > On Thu Apr 17, 2025 at 4:10 PM CEST, Xu Kuohai wrote: >> On 4/17/2025 3:14 PM, Alexis Lothoré wrote: >>> Hi Andrii, >>> >>> On Wed Apr 16, 2025 at 11:24 PM CEST, Andrii Nakryiko wrote: >>>> On Fri, Apr 11, 2025 at 1:32 PM Alexis Lothoré (eBPF Foundation) >>>> wrote: > > [...] > >>>> I might be missing something, but how can the *size* of the field be >>>> used to calculate that argument's *alignment*? i.e., I don't >>>> understand why arg_largest_member_size needs to be calculated instead >>>> of arg_largest_member_alignment... >>> >>> Indeed I initially checked whether I could return directly some alignment >>> info from btf, but it then involves the alignment computation in the btf >>> module. Since there could be minor differences between architectures about >>> alignment requirements, I though it would be better to in fact keep alignment >>> computation out of the btf module. For example, I see that 128 bits values >>> are aligned on 16 bytes on ARM64, while being aligned on 8 bytes on S390. >>> >>> And since for ARM64, all needed alignments are somehow derived from size >>> (it is either directly size for fundamental types, or alignment of the >>> largest member for structs, which is then size of largest member), >>> returning the size seems to be enough to allow the JIT side to compute >>> alignments. >>> >> >> Not exactly. The compiler's "packed" and "alignment" attributes cause a >> structure to be aligned differently from its natural alignment. >> >> For example, with the following three structures: >> >> struct s0 { >> __int128 x; >> }; >> >> struct s1 { >> __int128 x; >> } __attribute__((packed)); >> >> struct s2 { >> __int128 x; >> } __attribute__((aligned(64))); >> >> Even though the largest member size is the same, s0 will be aligned to 16 >> bytes, s1 and s2 are not aligned the same way. s1 has no alignment due to >> the "packed" attribute, while s2 will be aligned to 64 bytes. >> >> When these three structures are passed as function arguments, they will be >> located on different positions on the stack. >> >> For the following three functions: >> >> int f0(__int128 a, __int128 b, __int128 c, int64_t d, __int128 e, int64_t f, struct s0 g); >> int f1(__int128 a, __int128 b, __int128 c, int64_t d, __int128 e, int64_t f, struct s1 g); >> int f2(__int128 a, __int128 b, __int128 c, int64_t d, __int128 e, int64_t f, struct s2 g); >> >> g will be located at sp+32 in f0, sp + 24 in f1, and some 64-byte aligned >> stack address in f2. > > Ah, thanks for those clear examples, I completely overlooked this > possibility. And now that you mention it, I feel a bit dumb because I now > remember that you mentioned this in Puranjay's series... > > I took a quick look at the x86 JIT compiler for reference, and saw no code > related to this specific case neither. So I searched in the kernel for > actual functions taking struct arguments by value AND being declared with some > packed or aligned attribute. I only found a handful of those, and none > seems to take enough arguments to have the corresponding struct passed on the > stack. So rather than supporting this very specific case, I am tempted > to just return an error for now during trampoline creation if we detect such > structure (and then the JIT compiler can keep using data size to compute > alignment, now that it is sure not to receive custom alignments). Or am I > missing some actual cases involving those very specific alignments ? > How can we reliably 'detect' the case? If a function has such a parameter but we fail to detect it, the BPF trampoline will pass an incorrect value to the function, which is also unacceptable. > Thanks, > > Alexis >