Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1486663rwd; Thu, 8 Jun 2023 19:52:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6q5uL59S76n/hp0jCAvJgHrgDARVqfhBYpvsUzrXDr3h1OpeXqQvtK10qsCr4xMXvFLdlM X-Received: by 2002:a17:90a:f184:b0:258:edec:62e1 with SMTP id bv4-20020a17090af18400b00258edec62e1mr232943pjb.21.1686279163880; Thu, 08 Jun 2023 19:52:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686279163; cv=none; d=google.com; s=arc-20160816; b=Cr0AYUMthqaMBEnVY2lnUz35Q9BB8Nnx5tVLX5A1eQPXkrQv1Nnhc+IWChxkOHqONe qdtkL4XfT8le4+X81bRNTFwDVVN3y63OD6tzaR6c+1+TwScv2P8hNaK91TGg2RPl75FI U/sp49lTlA2Uu6NWAc5p1KBhzSoBo43tZmmOAA45Dq26//vzPE+r9Vtu/ga1UxyWIn05 8MG2mi6f+hV1jTw0kMOohpLI7L8HYirBDLqZKIYAEw2CZ1N+w3oL255nncurDHOCOkfU HxXTQb28AitKt4kkPhrAWE3ls6YSfn3caMeHAWK8rGmCzjgJsvKaYnlphND7/Hy1NcP4 wP3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=YkLcX6B6WSlNdg/K0cMsRbNItTJ8LTlhdnmPvGk8+4E=; b=yqNzBpUDO3QgOWJdez3Vhcn6axFZKYZmjVG09BS2q2TSHTzPl9qgKe5ZHby6Jc6zAX Ei7OoCencFfRUZbLj16A6RPvi8n7QsPaFPphFgAbIKFuFsRYnMwjX6FStXoQCXQ93zWP q/XcJYEyAYa4zx25qb1+2k5wttPkCVuFeWEpR7uQZhMzfkpzSbWhqiAolLaLjhh71fh+ L7CoSZiDzcy4OBeyJaPZhTZQqGPtuFyR4pDPgokt9vx45wo+jwSssyhChnxV2vcOM40B /36vcvRlZDcGxxXOyHk7E7lIruS/jIM3RqsUeTX8WusOvESLWH6D366F9zqeCVCGoPJl P/RQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=iqdVVzSi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s10-20020a170902a50a00b001a51bb4ad81si1919082plq.44.2023.06.08.19.52.29; Thu, 08 Jun 2023 19:52:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=iqdVVzSi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237582AbjFICer (ORCPT + 99 others); Thu, 8 Jun 2023 22:34:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232126AbjFICep (ORCPT ); Thu, 8 Jun 2023 22:34:45 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F81F1993; Thu, 8 Jun 2023 19:34:44 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id d75a77b69052e-3f9c2e3914aso9544051cf.3; Thu, 08 Jun 2023 19:34:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686278083; x=1688870083; 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=YkLcX6B6WSlNdg/K0cMsRbNItTJ8LTlhdnmPvGk8+4E=; b=iqdVVzSiVHpUjKb/j+Dwp26pCM/IcL7+wiS2XORZFeatoH9ZipAomeJAzWv0URa/Cw 1o15Ral2iftlE4iZdwVyY+OkF53jiT8YAXpxzRgQjAoaUXESCj8hCzJyldFaNLbB3lci 2QTuoVB9KAB+XYiabxB3I+EEzkWZiXr/PU9O05CwDxjb/ONVIjUqMXIJRgDYG/c2v0wY lQd8rcV0+z3U4yWEseoSf6f9x89vLUll3ZjeIQWFrgva/LWHmvHLkHRVoZv6boK68ucF V1pyb2XUBFNI5siqYqV0q3oZ8Kl/js/LCMenoOBq8JTbuuLVZreRj8hHjK8ccHZ2RdoF NorQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686278083; x=1688870083; 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=YkLcX6B6WSlNdg/K0cMsRbNItTJ8LTlhdnmPvGk8+4E=; b=hD+SJsewWR5wcNvtwZWHSjxcatJryKxk0zApq1GtpUnUD+IIlZOOTpTgzCoxFvS/Yz cxHRqSoe2AyENjsMgvEzxNvtBk6xfgzeZtK0Yam/mlau2mGPRtVu0YyV1l7RxOEBybJj HAwhUGge4rgaeSV3QSojNz1OY5KKULGu2qoKsBEuiUf9gA3deh2+OYoAnDcknu3NkJEy UBG1oN/3rsqdQIv0LlmhdCjVg7xZ16lG4+idsIiBbQ4a4eslw+zpEsxvv9/wOlQgn+W/ tvqxlaFxRHiQ7Ijr9ZhdVFA8xcTkxB8YmgZR6S3xVB9iJkU8WB4OMTPGBKVnMRH9xV8U TWuw== X-Gm-Message-State: AC+VfDwr+aqYfOkEWJpzODdA+ar7npVuynXJmGMs9XE0kO3Svb64vkA/ gxRZNTzbilHauyg9KFn6mWRhsnSCLBu76nqJ4L+WQXeB19DT8QYa X-Received: by 2002:a05:622a:85:b0:3f5:3851:873f with SMTP id o5-20020a05622a008500b003f53851873fmr311290qtw.8.1686278083524; Thu, 08 Jun 2023 19:34:43 -0700 (PDT) MIME-Version: 1.0 References: <20230607125911.145345-1-imagedong@tencent.com> <20230607125911.145345-2-imagedong@tencent.com> <20230607200905.5tbosnupodvydezq@macbook-pro-8.dhcp.thefacebook.com> <2fb8c454-1ae7-27cd-a9fa-0d8dda18a900@meta.com> In-Reply-To: <2fb8c454-1ae7-27cd-a9fa-0d8dda18a900@meta.com> From: Menglong Dong Date: Fri, 9 Jun 2023 10:34:32 +0800 Message-ID: Subject: Re: [PATCH bpf-next v3 1/3] bpf, x86: allow function arguments up to 12 for TRACING To: Yonghong Song Cc: Alexei Starovoitov , davem@davemloft.net, dsahern@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, x86@kernel.org, imagedong@tencent.com, benbjiang@tencent.com, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 9, 2023 at 5:12=E2=80=AFAM Yonghong Song wrote: > > > > On 6/7/23 8:17 PM, Menglong Dong wrote: > > On Thu, Jun 8, 2023 at 4:09=E2=80=AFAM Alexei Starovoitov > > wrote: > >> > >> On Wed, Jun 07, 2023 at 08:59:09PM +0800, menglong8.dong@gmail.com wro= te: > >>> From: Menglong Dong > >>> > >>> For now, the BPF program of type BPF_PROG_TYPE_TRACING can only be us= ed > >>> on the kernel functions whose arguments count less than 6. This is no= t > >>> friendly at all, as too many functions have arguments count more than= 6. > >>> > >>> Therefore, let's enhance it by increasing the function arguments coun= t > >>> allowed in arch_prepare_bpf_trampoline(), for now, only x86_64. > >>> > >>> For the case that we don't need to call origin function, which means > >>> without BPF_TRAMP_F_CALL_ORIG, we need only copy the function argumen= ts > >>> that stored in the frame of the caller to current frame. The argument= s > >>> of arg6-argN are stored in "$rbp + 0x18", we need copy them to > >>> "$rbp - regs_off + (6 * 8)". > >>> > >>> For the case with BPF_TRAMP_F_CALL_ORIG, we need prepare the argument= s > >>> in stack before call origin function, which means we need alloc extra > >>> "8 * (arg_count - 6)" memory in the top of the stack. Note, there sho= uld > >>> not be any data be pushed to the stack before call the origin functio= n. > >>> Then, we have to store rbx with 'mov' instead of 'push'. > >> > >> x86-64 psABI requires stack to be 16-byte aligned when args are passed= on the stack. > >> I don't see this logic in the patch. > > > > Yeah, it seems I missed this logic......:) > > > > I have not figure out the rule of the alignment, but after > > observing the behavior of the compiler, the stack seems > > should be like this: > > > > ------ stack frame begin > > rbp > > > > xxx -- this part should be aligned in 16-byte > > > > ------ end of arguments in stack > > xxx > > ------ begin of arguments in stack > > > > So the code should be: > > > > + if (nr_regs > 6 && (flags & BPF_TRAMP_F_CALL_ORIG)) { > > + stack_size =3D ALIGN(stack_size, 16); > > + stack_size +=3D (nr_regs - 6) * 8; > > + } > > > > Am I right? > > This is the stack_size, you should ensure stack pointer is 16-byte aligne= d. Oh, I see. Considering the begin of the stack frame should already be 16-byte aligned, what we should do here is to make the size of the current stack frame 16-byte aligned. Then, rsp will be 16-byte aligned. Am I right? Which means the code should be: + if (nr_regs > 6 && (flags & BPF_TRAMP_F_CALL_ORIG)) { + stack_size +=3D (nr_regs - 6) * 8; + stack_size =3D ALIGN(stack_size, 16); + } Then, the size of current stack frame will be: stack_size + 8(rbp) + 8(rip) This is the example that I refer to: https://godbolt.org/z/7o9nh4nbc > > > > > Thanks! > > Menglong Dong