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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C136EC43334 for ; Fri, 10 Jun 2022 16:35:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349747AbiFJQff (ORCPT ); Fri, 10 Jun 2022 12:35:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242819AbiFJQfM (ORCPT ); Fri, 10 Jun 2022 12:35:12 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4284856F96 for ; Fri, 10 Jun 2022 09:34:53 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id s1so14121455wra.9 for ; Fri, 10 Jun 2022 09:34:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=hWmTHzeJ7LLCWgq5ZaSnXt7Oc8qo/rMKpNHQ9yrt1Qc=; b=Bu5jzBOk61aAQk5Bc/WUTd8pRtciBgX0PKuqgykQLJZhLKrAvpdoydl6fsITgaucf0 X/qgWQKYaGRpCz9o905waICabXUsctKm0WjUo3ZH1TBke1a+W66fTyV1ygtFDmWwKMWS lwijBvm73I0pAZC0PQm3m3Rds1A6ApLM+wyvkWdMwL6U7jNBjq468V7vB11bmR8iVUss pTRGc9PMvMym49XMJ8FMelhVQCMfQvWXdtpzoXIo4KMBEZGp0VB8Ad6PXOsrugkSM9mp PGZOmpOWcIz5i6DGBGwP+1tKj0ZeWuT6ZRTVUbmnoHm8MaDr4xIoW4LQo4d9S30FzrPY +Odg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=hWmTHzeJ7LLCWgq5ZaSnXt7Oc8qo/rMKpNHQ9yrt1Qc=; b=dt20U4OXTaxxga9XfcjgLTUMZa1DF75TlTcMR6+m7UJO9iTlE4+TLfKTH7L7QwePex XJ2V4VIkvpKvzVrH+X8FgSb3OxpPGxyNIRLBJjRHiKzFT/sRtul27lBIfYx/h1MB/s8m 7uhTVgr6TTsuPufGR3vfjIDCfbF7jC0erfWlQAPGFtL9234fYqnrrVYzf5403M+8vw5I n02ClWuCGn/+ujvWNMAouIPnXIlfvRUidqj4OFmpvV7p606J/9Uu4zKuJn2JUc2KnGAz MiFtfFZK3eHyaNbs3cRAwD6hAqiXsHIE53QizPlCkVNyCmK1DPU7S8OXpUTOrLo2Byep mcaQ== X-Gm-Message-State: AOAM532/LLA3RMg8VR7nKWefI6LfiD2435HXu5jd1ImQ0e2+2Hp7/HWx GeKr6jffGpqVjjYc0U6HrsxPNg== X-Google-Smtp-Source: ABdhPJycH1MCJm4GWBSrD4Wp9OJUT6py82j/uRSoeKccN2E1PUjWHZl5HuAOog6vFR16rGzgToqUeA== X-Received: by 2002:a5d:65c1:0:b0:210:33b7:4525 with SMTP id e1-20020a5d65c1000000b0021033b74525mr45341201wrw.494.1654878891585; Fri, 10 Jun 2022 09:34:51 -0700 (PDT) Received: from [192.168.178.21] ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id u2-20020a7bcb02000000b0039c5cecf206sm3565168wmj.4.2022.06.10.09.34.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Jun 2022 09:34:51 -0700 (PDT) Message-ID: Date: Fri, 10 Jun 2022 17:34:50 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [PATCH bpf-next 1/2] Revert "bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK" Content-Language: en-GB To: sdf@google.com Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Yafang Shao , Harsh Modi , Paul Chaignon , netdev@vger.kernel.org, bpf@vger.kernel.org References: <20220610112648.29695-1-quentin@isovalent.com> <20220610112648.29695-2-quentin@isovalent.com> From: Quentin Monnet In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 2022-06-10 09:07 UTC-0700 ~ sdf@google.com > On 06/10, Quentin Monnet wrote: >> This reverts commit a777e18f1bcd32528ff5dfd10a6629b655b05eb8. > >> In commit a777e18f1bcd ("bpftool: Use libbpf 1.0 API mode instead of >> RLIMIT_MEMLOCK"), we removed the rlimit bump in bpftool, because the >> kernel has switched to memcg-based memory accounting. Thanks to the >> LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK, we attempted to keep compatibility >> with other systems and ask libbpf to raise the limit for us if >> necessary. > >> How do we know if memcg-based accounting is supported? There is a probe >> in libbpf to check this. But this probe currently relies on the >> availability of a given BPF helper, bpf_ktime_get_coarse_ns(), which >> landed in the same kernel version as the memory accounting change. This >> works in the generic case, but it may fail, for example, if the helper >> function has been backported to an older kernel. This has been observed >> for Google Cloud's Container-Optimized OS (COS), where the helper is >> available but rlimit is still in use. The probe succeeds, the rlimit is >> not raised, and probing features with bpftool, for example, fails. > >> A patch was submitted [0] to update this probe in libbpf, based on what >> the cilium/ebpf Go library does [1]. It would lower the soft rlimit to >> 0, attempt to load a BPF object, and reset the rlimit. But it may induce >> some hard-to-debug flakiness if another process starts, or the current >> application is killed, while the rlimit is reduced, and the approach was >> discarded. > >> As a workaround to ensure that the rlimit bump does not depend on the >> availability of a given helper, we restore the unconditional rlimit bump >> in bpftool for now. > >> [0] >> https://lore.kernel.org/bpf/20220609143614.97837-1-quentin@isovalent.com/ >> [1] https://github.com/cilium/ebpf/blob/v0.9.0/rlimit/rlimit.go#L39 > >> Cc: Yafang Shao >> Signed-off-by: Quentin Monnet >> --- >>   tools/bpf/bpftool/common.c     | 8 ++++++++ >>   tools/bpf/bpftool/feature.c    | 2 ++ >>   tools/bpf/bpftool/main.c       | 6 +++--- >>   tools/bpf/bpftool/main.h       | 2 ++ >>   tools/bpf/bpftool/map.c        | 2 ++ >>   tools/bpf/bpftool/pids.c       | 1 + >>   tools/bpf/bpftool/prog.c       | 3 +++ >>   tools/bpf/bpftool/struct_ops.c | 2 ++ >>   8 files changed, 23 insertions(+), 3 deletions(-) > >> diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c >> index a45b42ee8ab0..a0d4acd7c54a 100644 >> --- a/tools/bpf/bpftool/common.c >> +++ b/tools/bpf/bpftool/common.c >> @@ -17,6 +17,7 @@ >>   #include >>   #include >>   #include >> +#include >>   #include >>   #include > >> @@ -72,6 +73,13 @@ static bool is_bpffs(char *path) >>       return (unsigned long)st_fs.f_type == BPF_FS_MAGIC; >>   } > >> +void set_max_rlimit(void) >> +{ >> +    struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; >> + >> +    setrlimit(RLIMIT_MEMLOCK, &rinf); > > Do you think it might make sense to print to stderr some warning if > we actually happen to adjust this limit? > > if (getrlimit(MEMLOCK) != RLIM_INFINITY) { >     fprintf(stderr, "Warning: resetting MEMLOCK rlimit to >     infinity!\n"); >     setrlimit(RLIMIT_MEMLOCK, &rinf); > } > > ? > > Because while it's nice that we automatically do this, this might still > lead to surprises for some users. OTOH, not sure whether people > actually read those warnings? :-/ I'm not strictly opposed to a warning, but I'm not completely sure this is desirable. Bpftool has raised the rlimit for a long time, it changed only in April, so I don't think it would come up as a surprise for people who have used it for a while. I think this is also something that several other BPF-related applications (BCC I think?, bpftrace, Cilium come to mind) have been doing too. For new users, I agree the warning may be helpful. But then the message is likely to appear the very first time a user runs the command - likely as root - and I fear this might worry people not familiar with rlimits, who would wonder if they just broke something on their system? Maybe with a different phrasing. Alternatively we could document it in the relevant man pages (not that people would see it better, but at least it would be mentioned somewhere if people take the time to read the docs)? What do you think? Quentin