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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD965C43334 for ; Sun, 10 Jul 2022 05:26:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D33B96B0072; Sun, 10 Jul 2022 01:26:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE2C86B0073; Sun, 10 Jul 2022 01:26:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B83C98E0001; Sun, 10 Jul 2022 01:26:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id A29146B0072 for ; Sun, 10 Jul 2022 01:26:36 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 461F2602C2 for ; Sun, 10 Jul 2022 05:26:36 +0000 (UTC) X-FDA: 79670055192.29.1B062B9 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) by imf09.hostedemail.com (Postfix) with ESMTP id DACF6140069 for ; Sun, 10 Jul 2022 05:26:35 +0000 (UTC) Received: by mail-ed1-f52.google.com with SMTP id r6so2815929edd.7 for ; Sat, 09 Jul 2022 22:26:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wDVEfSp7j7rrH6uZySTOAMFaVch8GzcV2oP+h15+4f8=; b=cxn6WxpddW8a//WEA6pE7C12C6F1ERJGrv0bGni1Ph1kWPMES/xL1TWmq7DejugUZF 241NzEoIOoa6ImSMZ0i0PrcwcEm6mnrfUlabKrxgEeEXotYB7LxqDk9pujLFGVZ9v94Y VGpvoS0UycRf5bipul0BEVAFxsgISSmOVGloc/qqWZUjKYwEk5tu7szZOf1XsVlnbyUj Ohc7dnbGreDWxkM2C5CcAtW0cRgu4PDLwqVtV5igFl1FslPpIqpDlvxH9ytt37b11Iwl YIOeNbJy9vyZk5vKLmmwPnkiufvD3jET+L2fCR7I3ED+es3SdFD+z1HkI4/5rhZn6g4Z 40wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wDVEfSp7j7rrH6uZySTOAMFaVch8GzcV2oP+h15+4f8=; b=vfVULOUUZT2lxzubTpzCrIoJwTNGd/u0Yx6S8XyJrX2mcjmStuWXOt4Om/N+3RVkOY t0stB400qwFEq0sP2xlhxdPKkkARZ3sVlLXJV97QARKzV2P9UDYf9jrsoMSbmHO4AVEU lFH+iais/a8txBbR7JhO4Axu+UE0co80METiFlq/0pD4GYIMl0HXPT6v2uBkIkZJXm1T rVn2d46QS72Q2h7xonZEHRkmYEjOGnpvJjRP+D8u3bhRdKyAKMsrGCpX8nYeDHF2FMm2 reMEodglflk3jic39o5CqHTg9tupfPXXPpYxUB2W3y/gtNArqxYmQBXaYuTlrBXblOCR rxSQ== X-Gm-Message-State: AJIora/PUFZA3ky7Gy+gB+iVo9Kn9azjW0la9VWWM9IlS889oL/yPmtT FNp/VVnoC3W/TvlEEe93YujnNMs0OzA1JfDzN8E= X-Google-Smtp-Source: AGRyM1uxxeQCHWO9HmlkE9+gADlTr7tNhUBrS31ALVHHvHblk78pqvMPm3x6PZDQqR/csmlv9AKRVKTUaz5FeCsPgo0= X-Received: by 2002:a05:6402:5cb:b0:434:eb48:754f with SMTP id n11-20020a05640205cb00b00434eb48754fmr16293468edx.421.1657430794424; Sat, 09 Jul 2022 22:26:34 -0700 (PDT) MIME-Version: 1.0 References: <20220623003230.37497-1-alexei.starovoitov@gmail.com> <20220706175034.y4hw5gfbswxya36z@MacBook-Pro-3.local> <20220706180525.ozkxnbifgd4vzxym@MacBook-Pro-3.local.dhcp.thefacebook.com> <20220708174858.6gl2ag3asmoimpoe@macbook-pro-3.dhcp.thefacebook.com> <20220708215536.pqclxdqvtrfll2y4@google.com> In-Reply-To: <20220708215536.pqclxdqvtrfll2y4@google.com> From: Alexei Starovoitov Date: Sat, 9 Jul 2022 22:26:23 -0700 Message-ID: Subject: Re: [PATCH bpf-next 0/5] bpf: BPF specific memory allocator. To: Shakeel Butt Cc: Michal Hocko , Matthew Wilcox , Christoph Hellwig , "David S. Miller" , Daniel Borkmann , Andrii Nakryiko , Tejun Heo , Martin KaFai Lau , bpf , Kernel Team , linux-mm , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka Content-Type: text/plain; charset="UTF-8" ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=cxn6Wxpd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.208.52 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657430795; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wDVEfSp7j7rrH6uZySTOAMFaVch8GzcV2oP+h15+4f8=; b=F1nWYPapdKoguC1mSm4bNVidWzNukPVWe78cLoL97exjEPdsH9rSVVMVdpaGreaA1hbthd 0uJRdUbu9sU7heZyqi7KlZhWcS8rcZGlbZtkh6KPJ2PUA2u9MoSnQ5RYVT2SMKwczQYPlI iMxv0tM25wj1duTZdpV6KXTPsZkOoI4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657430795; a=rsa-sha256; cv=none; b=lmwkiaC2m7MuCgMzlNETgr2VvG5V01d31e71j+++ECoWjXyP9J+QHYB755esro4yPlVTGf UJPDedJIJLf8Ento/PpRUZX4hY+fUg4fUcjeDqW74XuGTHu3R/a23m9UqS28Av8yD56X1u A4zkgit+it87MNlbJOX1OsiC6PufxGk= X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=cxn6Wxpd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.208.52 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Stat-Signature: c5darh9q77qxtcgt5i9h6me8fjpqbn4d X-Rspamd-Queue-Id: DACF6140069 X-Rspamd-Server: rspam04 X-HE-Tag: 1657430795-493203 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Jul 8, 2022 at 2:55 PM Shakeel Butt wrote: > > On Fri, Jul 08, 2022 at 10:48:58AM -0700, Alexei Starovoitov wrote: > > On Fri, Jul 08, 2022 at 03:41:47PM +0200, Michal Hocko wrote: > > > On Wed 06-07-22 11:05:25, Alexei Starovoitov wrote: > > > > On Wed, Jul 06, 2022 at 06:55:36PM +0100, Matthew Wilcox wrote: > > > [...] > > > > > For example, I assume that a BPF program > > > > > has a fairly tight limit on how much memory it can cause to be allocated. > > > > > Right? > > > > > > > > No. It's constrained by memcg limits only. It can allocate gigabytes. > > > > > > I have very briefly had a look at the core allocator parts (please note > > > that my understanding of BPF is really close to zero so I might be > > > missing a lot of implicit stuff). So by constrained by memcg you mean > > > __GFP_ACCOUNT done from the allocation context (irq_work). The complete > > > gfp mask is GFP_ATOMIC | __GFP_NOMEMALLOC | __GFP_NOWARN | __GFP_ACCOUNT > > > which means this allocation is not allowed to sleep and GFP_ATOMIC > > > implies __GFP_HIGH to say that access to memory reserves is allowed. > > > Memcg charging code interprets this that the hard limit can be breached > > > under assumption that these are rare and will be compensated in some > > > way. The bulk allocator implemented here, however, doesn't reflect that > > > and continues allocating as it sees a success so the breach of the limit > > > is only bound by the number of objects to be allocated. If those can be > > > really large then this is a clear problem and __GFP_HIGH usage is not > > > really appropriate. > > > > That was a copy paste from the networking stack. See kmalloc_reserve(). > > Not sure whether it's a bug there or not. > > kmalloc_reserve() is good. Most of calls to kmalloc_reserve() are for > skbs and we don't use __GFP_ACCOUNT for skbs. Actually skbs are charged > to memcg through a separate interface (i.e. mem_cgroup_charge_skmem()) > > > In a separate thread we've agreed to convert all of bpf allocations > > to GFP_NOWAIT. For this patch set I've already fixed it in my branch. > > > > > Also, I do not see any tracking of the overall memory sitting in these > > > pools and I think this would be really appropriate. As there doesn't > > > seem to be any reclaim mechanism implemented this can hide quite some > > > unreachable memory. > > > > > > Finally it is not really clear to what kind of entity is the life time > > > of these caches bound to. Let's say the system goes OOM, is any process > > > responsible for it and a clean up would be done if it gets killed? > > > > We've been asking these questions for years and have been trying to > > come up with a solution. > > bpf progs are not analogous to user space processes. > > There are bpf progs that function completely without user space component. > > bpf progs are pretty close to be full featured kernel modules with > > the difference that bpf progs are safe, portable and users have > > full visibility into them (source code, line info, type info, etc) > > They are not binary blobs unlike kernel modules. > > But from OOM perspective they're pretty much like .ko-s. > > Which kernel module would you force unload when system is OOMing ? > > Force unloading ko-s will likely crash the system. > > Force unloading bpf progs maybe equally bad. The system won't crash, > > but it may be a sorrow state. The bpf could have been doing security > > enforcement or network firewall or providing key insights to critical > > user space components like systemd or health check daemon. > > We've been discussing ideas on how to rank and auto cleanup > > the system state when progs have to be unloaded. Some sort of > > destructor mechanism. Fingers crossed we will have it eventually. > > bpf infra keeps track of everything, of course. > > Technically we can detach, unpin and unload everything and all memory > > will be returned back to the system. > > Anyhow not a new problem. Orthogonal to this patch set. > > bpf progs have been doing memory allocation from day one. 8 years ago. > > This patch set is trying to make it 100% safe. > > Currently it's 99% safe. > > Most probably Michal's comment was on free objects sitting in the caches > (also pointed out by Yosry). Should we drain them on memory pressure / > OOM or should we ignore them as the amount of memory is not significant? Are you suggesting to design a shrinker for 0.01% of the memory consumed by bpf? And such drain would help... how?