Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2870822rwb; Fri, 16 Dec 2022 07:30:01 -0800 (PST) X-Google-Smtp-Source: AA0mqf6rWGVulkYFOuZG3D8BfysJ3MVIA7fsQjErgVGsz3aA4wG5uuOwilBIcX3RmRRqweci4Y4T X-Received: by 2002:a17:90b:3111:b0:219:9ff8:3d20 with SMTP id gc17-20020a17090b311100b002199ff83d20mr33309424pjb.35.1671204600809; Fri, 16 Dec 2022 07:30:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671204600; cv=none; d=google.com; s=arc-20160816; b=BC5nH9ocAF8xTjZkjJtp3HhTtL6ZzEsQCO6h9XFueShdIHAqJuaZElgybXHNIo8RXP u0IdBoqduLJyHbqM6ex/h0qej7UrLtFHsiuRgCG+/eVFpKVCHodctAG7S8X+SsC74SE8 PX3AkFmWJxm9ja6mLTGyIqCNMEM7wFwkX6QCgxfKBTY6/DxDAov/8QXuadxgKJk/QrHX 7OKS3PDKgieMPQaLo3PvMsB47Qf1KTM+WV5rOddkNAWVv3inQsC9Q63nSklcZURyCTh8 8LnNipLevVjAAUVA7pSg6YK4zS6K64joLnpALP+1/MWzPDUO7DrV048a9oKlPZsd44lE Fsyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=eAtAae41mvEwglBG5pb+80zKG+/SZ4sVxKCDZkBjORE=; b=I5u3XxM+ERbUjCCDq6gnoAETahaFMJMa2GbAXO59sZZypGOse9uc7vpST86J4b+ii4 WHhr5Xaek6El2GjxIRTB4fYHPDNWQKBjvbTbfXBkvT5E7Dva25QayD4udX7F8bdWCoUw vd7QZORGxTIf4MZaLqCv81pxXo5h+xU1jCpGGlQKYSsmLg5d5jUWUe2h8RL6Dbo2hLif FQC+zoMDNIjzGo4TQGtzQiiLcbdYwnmkwACjy5+GpHRzsZrscP4zVgiheuFwc5lwmOZC lg1HH3IkCPIuAsDogPpR9//ZonYpRJVGsbZB3YVjW6jYZUKrUMImZfvtrFNbeYe+Qf50 sMcw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rm11-20020a17090b3ecb00b00218592c28casi9691975pjb.33.2022.12.16.07.29.51; Fri, 16 Dec 2022 07:30:00 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229614AbiLPPYk (ORCPT + 68 others); Fri, 16 Dec 2022 10:24:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229475AbiLPPYi (ORCPT ); Fri, 16 Dec 2022 10:24:38 -0500 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5FC9532E9; Fri, 16 Dec 2022 07:24:37 -0800 (PST) Received: from sslproxy05.your-server.de ([78.46.172.2]) by www62.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1p6CZc-0006Fh-LT; Fri, 16 Dec 2022 16:24:32 +0100 Received: from [85.1.206.226] (helo=linux.home) by sslproxy05.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p6CZc-0004I2-6M; Fri, 16 Dec 2022 16:24:32 +0100 Subject: Re: [PATCH bpf-next] bpf: dup xlated insns with kvmalloc+memcpy To: Hao Sun , Yonghong Song Cc: bpf , Alexei Starovoitov , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , jolsa@kernel.org, David Miller , Linux Kernel Mailing List References: <20221216055436.4698-1-sunhao.th@gmail.com> <6523CED9-3532-40CB-8B50-5C137555E58E@gmail.com> From: Daniel Borkmann Message-ID: <7d274284-0fcc-061c-582e-3dfb629c6a44@iogearbox.net> Date: Fri, 16 Dec 2022 16:24:31 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <6523CED9-3532-40CB-8B50-5C137555E58E@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.7/26752/Fri Dec 16 09:25:27 2022) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 12/16/22 8:18 AM, Hao Sun wrote: > > >> On 16 Dec 2022, at 3:03 PM, Yonghong Song wrote: >> >> >> >> On 12/15/22 9:54 PM, Hao Sun wrote: >>> Currently, kmemdup() is used for allocating and copying xlated insns >>> in bpf_insn_prepare_dump(). The following warning can be triggered >>> when dup large amount of insns (roughly BPF_COMPLEXITY_LIMIT_INSNS/2) >>> because kmemdup() uses kmalloc() which would fail when allocing size >>> is too big, leading to failure in dump xlated insns: >>> WARNING: CPU: 2 PID: 7060 at mm/page_alloc.c:5534 >>> Call Trace: >>> >>> __alloc_pages_node include/linux/gfp.h:237 [inline] >>> alloc_pages_node include/linux/gfp.h:260 [inline] >>> __kmalloc_large_node+0x81/0x160 mm/slab_common.c:1096 >>> __do_kmalloc_node mm/slab_common.c:943 [inline] >>> __kmalloc_node_track_caller.cold+0x5/0x5d mm/slab_common.c:975 >>> kmemdup+0x29/0x60 mm/util.c:129 >>> kmemdup include/linux/fortify-string.h:585 [inline] >>> bpf_insn_prepare_dump kernel/bpf/syscall.c:3820 [inline] >>> bpf_prog_get_info_by_fd+0x9a3/0x2cb0 kernel/bpf/syscall.c:3975 >>> bpf_obj_get_info_by_fd kernel/bpf/syscall.c:4297 [inline] >>> __sys_bpf+0x3928/0x56f0 kernel/bpf/syscall.c:5004 >>> __do_sys_bpf kernel/bpf/syscall.c:5069 [inline] >>> __se_sys_bpf kernel/bpf/syscall.c:5067 [inline] >>> ... >>> So use kvmalloc()+memcpy() to fix this, for small size of insns, >>> this is same as kmemdup(), but this also support dup large amount >>> of xlated insns. >>> Signed-off-by: Hao Sun >>> --- >>> kernel/bpf/syscall.c | 8 ++++---- >>> 1 file changed, 4 insertions(+), 4 deletions(-) >>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c >>> index 35972afb6850..06229fddac0d 100644 >>> --- a/kernel/bpf/syscall.c >>> +++ b/kernel/bpf/syscall.c >>> @@ -3831,10 +3831,10 @@ static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, >>> u8 code; >>> int i; >>> - insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), >>> - GFP_USER); >> >> Does kmemdup(prog->insnsi, bpf_prog_insn_size(prog), GFP_USER | __GFP_NOWARN) work? > > This only suppress the warning, bpf_insn_prepare_dump() still fails because of > the failure of kmalloc() invoked by kmemdup(). Ok, instead of open coding, would be nice if we add a helper to mm/util.c : void *kvmemdup(const void *src, size_t len, gfp_t gfp) { void *p; p = kvmalloc(len, gfp); if (p) memcpy(p, src, len); return p; } EXPORT_SYMBOL(kvmemdup); And then bpf and in future others could make use of it. Thanks, Daniel