Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp177234rwd; Wed, 7 Jun 2023 21:58:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6YvA2ZLWMHYkaTY5TpQUTIiGzqhcNYWeJ8/5b762hz9+YntPk5A+Xhu3lajoO0F0ICnzGW X-Received: by 2002:a17:903:11cf:b0:1af:e302:123 with SMTP id q15-20020a17090311cf00b001afe3020123mr1547269plh.3.1686200328650; Wed, 07 Jun 2023 21:58:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686200328; cv=none; d=google.com; s=arc-20160816; b=kb5JK5wgF0eelE/cJKTJKUb1S9C/ADzRaPV4HBeIH87ec/SeGY8JG772HjohDE2PMb WFMPRdd/MzwuohJoBwNrc8oi97Yn0bWCcwS8XmSUxZ8SOISNgnyDjNoil3ChYogsAs/V LSuEU4dcKWjdWeL76vnbbOoD/Hwtc1ezu2UZvmKb1sUrkU/4gjgyWAcUVY7k2Hwla/WD SXyg7+wObHgKh+NTTIF09XeTcd99Vd1qfVoRN14I9D+qmg//Fido2A+mZTFRaVxD5LLm HHjzS6AWhVH1dLsd3Hg9X90jLjukKbr8yRS6IALiH4q8kakdrLkfGpica6opEQQIQICJ Yweg== 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=k2JsskG49VjWJfT73tF3irTp5aLOY9bRkVhiXnj04Ck=; b=mPd/ze8KaPrDi6KJgg+i4SSPrPn+T+0/aykYkjyW34cbuD148MEf4V927rbzntbuKS puY7r7iXEPRc7Re9wpxwi1rYV9yn87Cnqls9IGXRMO7rX+qP8iDNnTKWaziGVpJ6ru5w /WPmQ7eAX3tCJqdLhoKHQq4V60R+BtD3V0l2/ny89HYF2925E9+qSJRVYID8v5J/g/cV RZDsQZ5wsup9oKBrTM7gIm/lxTgtP+0rxumDV951eAio3XZJMKLuFqPRlVp33jts4x52 qYlS39+o4BTZ0W/pguoG8eL30EvNSs7NB8ft4aDOCVdVOrOPpzCZQrP115oveFzTwqlV DybA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=fqncFh2v; 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 l13-20020a170903004d00b001a1af4abdedsi473607pla.212.2023.06.07.21.58.33; Wed, 07 Jun 2023 21:58:48 -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=fqncFh2v; 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 S234117AbjFHEio (ORCPT + 99 others); Thu, 8 Jun 2023 00:38:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234140AbjFHEih (ORCPT ); Thu, 8 Jun 2023 00:38:37 -0400 Received: from mail-yw1-x1144.google.com (mail-yw1-x1144.google.com [IPv6:2607:f8b0:4864:20::1144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B504272B; Wed, 7 Jun 2023 21:38:35 -0700 (PDT) Received: by mail-yw1-x1144.google.com with SMTP id 00721157ae682-568928af8f5so3489197b3.1; Wed, 07 Jun 2023 21:38:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686199114; x=1688791114; 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=k2JsskG49VjWJfT73tF3irTp5aLOY9bRkVhiXnj04Ck=; b=fqncFh2vZGdwEr/7SVimGxwbGcBm2werxnOOqG/EuVM5l2UVMbhjfYp6twgUElGhHD IhYM4a6JPfjTnEwwRVDj7nNISnUAAVJOshKMOzBSd9u5iPQxKxTl+gEbEz5vfjzvfY+g T5AUxo2xfC+gb7hSP70hj+y+uDOsADRb2ms18/0jzqAPP4KMrmw6baN4+vB1p+2+TZDd UZAyzvfvFWU/1FtKhgux0wTR1O2EKggx07EXX8rYukx3OOW4WvhtTPZ+3Rnyo2TUSG1D jTktlwXiizBkp1SPDCDnF89arE67vAXbffSxhSlntIVAMz8ByghvEgTdCsHModLKu4Xz c6pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686199114; x=1688791114; 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=k2JsskG49VjWJfT73tF3irTp5aLOY9bRkVhiXnj04Ck=; b=gszl81os443GCIfhvvLwavDa1NgLFclyak8LzJO6ormN/+AyrI+nOlxNACLhkh4kuk IRXjJ9AFr/obAaI6K4u8dp2RK59hfwD+J9egOFORcTrueqEXVSJht9PJUs2Id7/grVmu +UisLswR6zhXhEC7kyFbgyulJvxlEX67ZckaSSjuhaLmTgzZnYC/oi4lrdk/sDAxGSyo 5nnFQbdSndL74LbAhHWBoCJcAby+sucJz3oAMFBH+YnhLLeRRwJHT80ZHLc2/Vyg/zVc 1FlclalpWX0VohVLJ1Xn5To2+OQtlNJMcVDs9SSsOl9R/2Amgn8ncjDpQ+oyU5+nlOe/ jqRg== X-Gm-Message-State: AC+VfDzgVvd6FMZcJbY1IeqsQsvFJerPKY8fYuH1cBXU9I9WFBk2zly9 KvSv9mHDGsuF9wsk6VKpkT4ULuozBWF/qgtJeRqATAfl/yA+Pw== X-Received: by 2002:a0d:d811:0:b0:568:f050:7c47 with SMTP id a17-20020a0dd811000000b00568f0507c47mr1410905ywe.0.1686199114561; Wed, 07 Jun 2023 21:38:34 -0700 (PDT) MIME-Version: 1.0 References: <20230607125911.145345-1-imagedong@tencent.com> <20230607125911.145345-3-imagedong@tencent.com> <20230607200348.dprmfvpzdvk5ldpp@macbook-pro-8.dhcp.thefacebook.com> In-Reply-To: <20230607200348.dprmfvpzdvk5ldpp@macbook-pro-8.dhcp.thefacebook.com> From: Menglong Dong Date: Thu, 8 Jun 2023 12:38:23 +0800 Message-ID: Subject: Re: [PATCH bpf-next v3 2/3] bpf, x86: clean garbage value in the stack of trampoline To: Alexei Starovoitov Cc: 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 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 Thu, Jun 8, 2023 at 4:03=E2=80=AFAM Alexei Starovoitov wrote: > > On Wed, Jun 07, 2023 at 08:59:10PM +0800, menglong8.dong@gmail.com wrote: > > From: Menglong Dong > > > > There are garbage values in upper bytes when we store the arguments > > into stack in save_regs() if the size of the argument less then 8. > > > > As we already reserve 8 byte for the arguments in regs and stack, > > it is ok to store/restore the regs in BPF_DW size. Then, the garbage > > values in upper bytes will be cleaned. > > > > Reviewed-by: Jiang Biao > > Signed-off-by: Menglong Dong > > --- > > arch/x86/net/bpf_jit_comp.c | 19 ++++++------------- > > 1 file changed, 6 insertions(+), 13 deletions(-) > > > > diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c > > index 413b986b5afd..e9bc0b50656b 100644 > > --- a/arch/x86/net/bpf_jit_comp.c > > +++ b/arch/x86/net/bpf_jit_comp.c > > @@ -1878,20 +1878,16 @@ static void save_regs(const struct btf_func_mod= el *m, u8 **prog, int nr_regs, > > > > if (i <=3D 5) { > > /* copy function arguments from regs into stack *= / > > - emit_stx(prog, bytes_to_bpf_size(arg_size), > > - BPF_REG_FP, > > + emit_stx(prog, BPF_DW, BPF_REG_FP, > > i =3D=3D 5 ? X86_REG_R9 : BPF_REG_1 + i, > > -(stack_size - i * 8)); > > This is ok, > > > } else { > > /* copy function arguments from origin stack fram= e > > * into current stack frame. > > */ > > - emit_ldx(prog, bytes_to_bpf_size(arg_size), > > - BPF_REG_0, BPF_REG_FP, > > + emit_ldx(prog, BPF_DW, BPF_REG_0, BPF_REG_FP, > > (i - 6) * 8 + 0x18); > > - emit_stx(prog, bytes_to_bpf_size(arg_size), > > - BPF_REG_FP, > > - BPF_REG_0, > > + emit_stx(prog, BPF_DW, BPF_REG_FP, BPF_REG_0, > > -(stack_size - i * 8)); > > But this is not. > See https://godbolt.org/z/qW17f6cYe > mov dword ptr [rsp], 6 > > the compiler will store 32-bit only. The upper 32-bit are still garbage. Enn......I didn't expect this case, and it seems that this only happens on clang. With gcc, "push 6" is used. I haven't found a solution for this case, and it seems not worth it to add an extra insn to clean the garbage values. Does anyone have any ideas here? Thanks! Menglong Dong > > > } > > > > @@ -1918,7 +1914,7 @@ static void restore_regs(const struct btf_func_mo= del *m, u8 **prog, int nr_regs, > > next_same_struct =3D !next_same_struct; > > } > > > > - emit_ldx(prog, bytes_to_bpf_size(arg_size), > > + emit_ldx(prog, BPF_DW, > > i =3D=3D 5 ? X86_REG_R9 : BPF_REG_1 + i, > > BPF_REG_FP, > > -(stack_size - i * 8)); > > @@ -1949,12 +1945,9 @@ static void prepare_origin_stack(const struct bt= f_func_model *m, u8 **prog, > > } > > > > if (i > 5) { > > - emit_ldx(prog, bytes_to_bpf_size(arg_size), > > - BPF_REG_0, BPF_REG_FP, > > + emit_ldx(prog, BPF_DW, BPF_REG_0, BPF_REG_FP, > > (i - 6) * 8 + 0x18); > > - emit_stx(prog, bytes_to_bpf_size(arg_size), > > - BPF_REG_FP, > > - BPF_REG_0, > > + emit_stx(prog, BPF_DW, BPF_REG_FP, BPF_REG_0, > > -(stack_size - (i - 6) * 8)); > > } > > > > -- > > 2.40.1 > >