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 9ED6BC4332F for ; Wed, 14 Dec 2022 10:43:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 396248E0007; Wed, 14 Dec 2022 05:43:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 348618E0002; Wed, 14 Dec 2022 05:43:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20EC98E0007; Wed, 14 Dec 2022 05:43:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 152B98E0002 for ; Wed, 14 Dec 2022 05:43:48 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E12EE80DC5 for ; Wed, 14 Dec 2022 10:43:47 +0000 (UTC) X-FDA: 80240576094.07.6BF252D Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) by imf02.hostedemail.com (Postfix) with ESMTP id 418EB80007 for ; Wed, 14 Dec 2022 10:43:45 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=hqwPO6hM; spf=pass (imf02.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.51 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671014626; a=rsa-sha256; cv=none; b=g3vE3PFM4UFih4/VRSbHTxHe6M9GLBOxf0DTeQZqhbHgILcZCOfz4k96Fe60hmCXVIp1KN b3YaOGJIrLRMwuiQe5dku5aZlrHM+48lai7tROa/fWrJyEk1ZIRIAPR2jTQzQEmuztHBYQ m3ZGfWRdVUuWNhP4Us3+6GQF58nKl8U= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=hqwPO6hM; spf=pass (imf02.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.51 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671014626; 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=9072pfD4khAVmynoaUf7bmD/ysLw6+DbwffZbjZQC+w=; b=fcarEmDdIg2QALjLtqTvu/j34ZED6JwImtDs/U1eWO+RWsusVmxz0UtP32jyW8x7Q7K8kM ekeD1IO97xtDTwAmv+kVES8HbEUw/nORfoxVOeNk1BF8GXZTtRCGFvK+FNViOtP3+yWIs2 i16aE7tbuKYVB6XgvEm1CDWQF/xHpk0= Received: by mail-lf1-f51.google.com with SMTP id y25so9695089lfa.9 for ; Wed, 14 Dec 2022 02:43:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=9072pfD4khAVmynoaUf7bmD/ysLw6+DbwffZbjZQC+w=; b=hqwPO6hMBngEV4sVE2aZbZTigq8r0SgYW2rttM0AE7HD79vYelQqVHSwAkOon6ngP/ dPH/RA+QhI5wTGv1/AD8pGqLNS/DWOlp2kbNE2g31ljGsqhRogN4QndUBZ6sK/1j3IeJ PrCuDQbOrySqVxwTjOxybPk8j/cAJ9XnbQiPmjplX/w4t0nEMn+bLqxcCnfzzy4Kaqoe 61OyajpuW0xivYZf9brbrixBwNqghxqP3/IqoTAqomoFXx+tkF50Emomi1rsoZAmfRw0 OrfhD08NGNe5jhS8C6QKx3272KoQWJPKJdPEdf6bLN3eiwO1/PVpbN2ogePXGEctUbs6 rpgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=9072pfD4khAVmynoaUf7bmD/ysLw6+DbwffZbjZQC+w=; b=Q3N32M1ZdCM8qL7BIUI5y8SscrKD8cCDxHJTK87CiCWc99gYYm5FvMrOd44bizmxi4 Z7w/DzTasNL2jt9ioM3YyZQ1wg2LTqqoLT2W599XjwRS0vb3LPFZmqAEgUpn1pOakLlD DAmgpzSvejipntd22VEVdGDNuTACeyLSwAVWNCRgoAHihp1ec7PorrrTvkvlU1obgssM EjwoGuC6YwPtnOwRYx0oyVoqthnVvmTzqRP+ijNXF5y8OdwWHAz021u9Rzh6sxOE0WSr is0Z0UUl0mXbzap6BXzUNAEJBVvvavrAdDZO1/JMpiffE/FHM8OH4bNhGH8AOdQYEO2A bnmA== X-Gm-Message-State: ANoB5pmN+iuw9oXlteftU/qQZU79s4aoywRUG79tcNEXBoIIEfX1Ss+1 zqnzYrNquKHJGHEjGj3DW19moyuXwF67lGDJY/8= X-Google-Smtp-Source: AA0mqf7hbxccayd2yNfLFzEZHLz1HImBMMaf/WXz9EgPAAVmTeRXeLpbRdd89tpdN27v0V0u8FCQodZD89egol2lbtI= X-Received: by 2002:a05:6512:6d3:b0:4b5:62e4:bff2 with SMTP id u19-20020a05651206d300b004b562e4bff2mr8065015lff.492.1671014624485; Wed, 14 Dec 2022 02:43:44 -0800 (PST) MIME-Version: 1.0 References: <20221212003711.24977-1-laoar.shao@gmail.com> <6f9bb391-580e-cfc2-e039-25f47d162d17@suse.cz> In-Reply-To: <6f9bb391-580e-cfc2-e039-25f47d162d17@suse.cz> From: Yafang Shao Date: Wed, 14 Dec 2022 18:43:08 +0800 Message-ID: Subject: Re: [RFC PATCH bpf-next 0/9] mm, bpf: Add BPF into /proc/meminfo To: Vlastimil Babka Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, dennis@kernel.org, cl@linux.com, akpm@linux-foundation.org, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, roman.gushchin@linux.dev, linux-mm@kvack.org, bpf@vger.kernel.org, rcu@vger.kernel.org, Matthew Wilcox Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 418EB80007 X-Stat-Signature: 6jk4h14uw6kieomrmuhso1mj5snwc537 X-HE-Tag: 1671014625-883441 X-HE-Meta: U2FsdGVkX18qbFRH18UtpOBBt5rGyPOnIFNnr/LX2LmbhLtDQAKS8I++2Vgutz15iZctdxM1SFSZTv4Sa/6xvKMjYOImKfMRDCUcc5zQEz7uwbQUcHxXim9mIJ0GOlEd2bVadbBFlB22uKpiUAUBTTGvN+eeu+UGUZZ7PnvdkxxZGYAsbVkeMmK2DpJnlWRuR/DdiRV8WONsZmwoGfdyaX0ZpxC7VeeKhOOW0ysZRqyhbpWjjmjQQnnzNC9BsfvDhINDrYi+23/2rj2SdDswJZJxcruk1WuyeLUcO59QszytvSKfvJmjfveeuZGemwpF7Gy9MCW/jRg1b0lDkdctSl0jixeXs69Hysw/sMJFb4hgNFbfEhS9uNxUNU7aF/zJGW2dAcxkCjM1CgbpnYIIzF+FUBiAzEVdIZsIdlpcWN1V91uRcZ109aSrWYCgYaISu2LV/b4eJ40jZe1ApjIMk912ugG4gzGCSz1FRFJjNIWyHb1b7OEuglqHqmKIenn3+H6xeQ0saQ9m6LVbHYKczzDtVXADzGeFyU9GgcXEUDqwg+kz4BOEmbtiL5Hap/PD236ZQcbty+n7XCB7qIjr9xl8LFB4G+VtzlN/jtM58z25KqlUrfGmpmUHirxRGyK1MCCkHb/QNrJzh/wcPnuybGf/AAa6KKxUMdzXEUexSdhAnGuqgof3usBcW2xrQs8aZCHBKfMzKpGgVlM3wli/zjmzPzRLxlgvEdeaKE3n+NQk+ahH6OUq0/BQ/0/xGq3Yjduij3lf4fgJCzpCGYtIej5nnWIlRvKGjI55icQpSN/bsGOkijVM4cQbY5Swnj9sQ9d3HJnbOWjrkHyuPPKddsfPma3CVbS9tZjNkIxwe/jr3Gy6ASWN6sE90wcypCfpDqOtiT3VAk69zqHWbeHAh0egd0xD1jBwfS022aGqMTmA813eCwm8b3Q2kT9D5G0Vst4MvMaKg4OP7vzkcQP M2LcKEqN GwLAxbbElxXSEyBkg1VIdlx0lw4qmAOHX5BPeGnJxJabstvjrBF3xqO8j6/8VBpcxl/37KfubpJHsByCkAjw6WVPEF1ifPUUjq88+0XKbRV9sYFhjVliQen8SL1BTEtiFV3S4IqAyhVJk46ySV5rM5OXmGGPv2vkfjDv7 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 Tue, Dec 13, 2022 at 11:52 PM Vlastimil Babka wrote: > > On 12/13/22 15:56, Hyeonggon Yoo wrote: > > On Tue, Dec 13, 2022 at 07:52:42PM +0800, Yafang Shao wrote: > >> On Tue, Dec 13, 2022 at 1:54 AM Vlastimil Babka wrote: > >> > > >> > On 12/12/22 01:37, Yafang Shao wrote: > >> > > Currently there's no way to get BPF memory usage, while we can only > >> > > estimate the usage by bpftool or memcg, both of which are not reliable. > >> > > > >> > > - bpftool > >> > > `bpftool {map,prog} show` can show us the memlock of each map and > >> > > prog, but the memlock is vary from the real memory size. The memlock > >> > > of a bpf object is approximately > >> > > `round_up(key_size + value_size, 8) * max_entries`, > >> > > so 1) it can't apply to the non-preallocated bpf map which may > >> > > increase or decrease the real memory size dynamically. 2) the element > >> > > size of some bpf map is not `key_size + value_size`, for example the > >> > > element size of htab is > >> > > `sizeof(struct htab_elem) + round_up(key_size, 8) + round_up(value_size, 8)` > >> > > That said the differece between these two values may be very great if > >> > > the key_size and value_size is small. For example in my verifaction, > >> > > the size of memlock and real memory of a preallocated hash map are, > >> > > > >> > > $ grep BPF /proc/meminfo > >> > > BPF: 1026048 B <<< the size of preallocated memalloc pool > >> > > > >> > > (create hash map) > >> > > > >> > > $ bpftool map show > >> > > 3: hash name count_map flags 0x0 > >> > > key 4B value 4B max_entries 1048576 memlock 8388608B > >> > > > >> > > $ grep BPF /proc/meminfo > >> > > BPF: 84919344 B > >> > > > >> > > So the real memory size is $((84919344 - 1026048)) which is 83893296 > >> > > bytes while the memlock is only 8388608 bytes. > >> > > > >> > > - memcg > >> > > With memcg we only know that the BPF memory usage is less than > >> > > memory.usage_in_bytes (or memory.current in v2). Furthermore, we only > >> > > know that the BPF memory usage is less than $MemTotal if the BPF > >> > > object is charged into root memcg :) > >> > > > >> > > So we need a way to get the BPF memory usage especially there will be > >> > > more and more bpf programs running on the production environment. The > >> > > memory usage of BPF memory is not trivial, which deserves a new item in > >> > > /proc/meminfo. > >> > > > >> > > This patchset introduce a solution to calculate the BPF memory usage. > >> > > This solution is similar to how memory is charged into memcg, so it is > >> > > easy to understand. It counts three types of memory usage - > >> > > - page > >> > > via kmalloc, vmalloc, kmem_cache_alloc or alloc pages directly and > >> > > their families. > >> > > When a page is allocated, we will count its size and mark the head > >> > > page, and then check the head page at page freeing. > >> > > - slab > >> > > via kmalloc, kmem_cache_alloc and their families. > >> > > When a slab object is allocated, we will mark this object in this > >> > > slab and check it at slab object freeing. That said we need extra memory > >> > > to store the information of each object in a slab. > >> > > - percpu > >> > > via alloc_percpu and its family. > >> > > When a percpu area is allocated, we will mark this area in this > >> > > percpu chunk and check it at percpu area freeing. That said we need > >> > > extra memory to store the information of each area in a percpu chunk. > >> > > > >> > > So we only need to annotate the allcation to add the BPF memory size, > >> > > and the sub of the BPF memory size will be handled automatically at > >> > > freeing. We can annotate it in irq, softirq or process context. To avoid > >> > > counting the nested allcations, for example the percpu backing allocator, > >> > > we reuse the __GFP_ACCOUNT to filter them out. __GFP_ACCOUNT also make > >> > > the count consistent with memcg accounting. > >> > > >> > So you can't easily annotate the freeing places as well, to avoid the whole > >> > tracking infrastructure? > >> > >> The trouble is kfree_rcu(). for example, > >> old_item = active_vm_item_set(ACTIVE_VM_BPF); > >> kfree_rcu(); > >> active_vm_item_set(old_item); > >> If we want to pass the ACTIVE_VM_BPF into the deferred rcu context, we > >> will change lots of code in the RCU subsystem. I'm not sure if it is > >> worth it. > > > > (+Cc rcu folks) > > > > IMO adding new kfree_rcu() varient for BPF that accounts BPF memory > > usage would be much less churn :) > > Alternatively, just account the bpf memory as freed already when calling > kfree_rcu()? I think the amount of memory "in flight" to be freed by rcu is > a separate issue (if it's actually an issue) and not something each > kfree_rcu() user should think about separately? > Not sure if it is a problem for other users as well. But I can explain why it is an issue for BPF accounting. In BPF accounting, we need to store something into the task_struct and use it later. So if the 'storer' and the 'user' are different tasks, the information will be lost. > >> > >> > I thought there was a patchset for a whole > >> > bfp-specific memory allocator, where accounting would be implemented > >> > naturally, I would imagine. > >> > > >> > >> I posted a patchset[1] which annotates both allocating and freeing > >> several months ago. > >> But unfortunately after more investigation and verification I found > >> the deferred freeing context is a problem, which can't be resolved > >> easily. > >> That's why I finally decided to annotate allocating only. > >> > >> [1]. https://lore.kernel.org/linux-mm/20220921170002.29557-1-laoar.shao@gmail.com/ > >> > >> > > To store the information of a slab or a page, we need to create a new > >> > > member in struct page, but we can do it in page extension which can > >> > > avoid changing the size of struct page. So a new page extension > >> > > active_vm is introduced. Each page and each slab which is allocated as > >> > > BPF memory will have a struct active_vm. The reason it is named as > >> > > active_vm is that we can extend it to other areas easily, for example in > >> > > the future we may use it to count other memory usage. > >> > > > >> > > The new page extension active_vm can be disabled via CONFIG_ACTIVE_VM at > >> > > compile time or kernel parameter `active_vm=` at runtime. > >> > > >> > The issue with page_ext is the extra memory usage, so it was rather intended > >> > for debugging features that can be always compiled in, but only enabled at > >> > runtime when debugging is needed. The overhead is only paid when enabled. > >> > That's at least the case of page_owner and page_table_check. The 32bit > >> > page_idle is rather an oddity that could have instead stayed 64-bit only. > >> > > >> > >> Right, it seems currently page_ext is for debugging purposes only. > >> > >> > But this is proposing a page_ext functionality supposed to be enabled at all > >> > times in production, with the goal of improved accounting. Not an on-demand > >> > debugging. I'm afraid the costs will outweight the benefits. > >> > > >> > >> The memory overhead of this new page extension is (8/4096), which is > >> 0.2% of total memory. Not too big to be acceptable. > > > > It's generally unacceptable to increase sizeof(struct page) > > (nor enabling page_ext by default, and that's the why page_ext is for > > debugging purposes only) > > > >> If the user really > >> thinks this overhead is not accepted, he can set "active_vm=off" to > >> disable it. > > > > I'd say many people won't welcome adding 0.2% of total memory by default > > to get BPF memory usage. > > Agreed. > > >> To reduce the memory overhead further, I have a bold idea. > >> Actually we don't need to allocate such a page extension for every > >> page, while we only need to allocate it if the user needs to access > >> it. That said it seems that we can allocate some kind of page > >> extensions dynamically rather than preallocate at booting, but I > >> haven't investigated it deeply to check if it can work. What do you > >> think? > > There's lots of benefits (simplicity) of page_ext being allocated as it is > today. These benefits also lead it to debugging purposes only :) If we can make it run on production env, it will be more useful. > What you're suggesting will be better solved (in few years :) by > Matthew's bold ideas about shrinking the current struct page and allocating > usecase-specific descriptors. > So the memory overhead won't be a problem in the future, right ? -- Regards Yafang