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 6187EC6379F for ; Tue, 17 Jan 2023 17:25:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D89D46B0075; Tue, 17 Jan 2023 12:25:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D38926B0078; Tue, 17 Jan 2023 12:25:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C27746B007B; Tue, 17 Jan 2023 12:25:26 -0500 (EST) 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 B4A3F6B0075 for ; Tue, 17 Jan 2023 12:25:26 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7A309AB06C for ; Tue, 17 Jan 2023 17:25:26 +0000 (UTC) X-FDA: 80364967452.18.EAED2B4 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) by imf12.hostedemail.com (Postfix) with ESMTP id C3B3D40005 for ; Tue, 17 Jan 2023 17:25:24 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PaOLMjbQ; spf=pass (imf12.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.218.45 as permitted sender) smtp.mailfrom=alexei.starovoitov@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=1673976324; 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=PbngI1x45WG0Dj+cWyNRbiJ8XHkY6KMeJj98Xvf7rH4=; b=PpsOud7wDWLeaK3cEPhO3gyq6ATGs/1IcVDldkcmTxTM4YN5fJEB/vMZ47gWFOyFV83Bpn t99NqJvioUe9Ta6SwcIpiKN4D/6XNOxZqAs35J6DPUCWZFmiG+XVQsB9dlZg583/OGi+2E w303wcTiuv1zDweWjIg2GMn0XjtVbVA= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PaOLMjbQ; spf=pass (imf12.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.218.45 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673976324; a=rsa-sha256; cv=none; b=QRysx42Z8JZ1RfOdFvRTCMbg3suaAhP+yudEMRV63owHPLw6AQRh/NUCeI1y2mAEY5syXr 7iDDXDmi0ny+JnmBO4AIKcrIBdTf2+oH/LWJ76dp+LuMXD3GhciYf5uVFdxcUPnyzrxdym FaBEisX8bIbHHBZClX/9ygagL3R/RWo= Received: by mail-ej1-f45.google.com with SMTP id vw16so13880344ejc.12 for ; Tue, 17 Jan 2023 09:25:24 -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=PbngI1x45WG0Dj+cWyNRbiJ8XHkY6KMeJj98Xvf7rH4=; b=PaOLMjbQDrZqFWDLjlOIoVP7YXr91GRcToXJSf5eVe1y3F+nzbmT/QnbEjFNh/A3fC elesWXMQtaFTv3cIDAhLwzv5PcCEVcVB1j/PIt/jO9X9h+cX3/aJkGFlBOnJzVLBqHLl vRD5Of124M53bWwaykcaW0B6mqqrAdxQV0l2lCHts8gebi0b6EA12IeNW+F4ogneFQyO qft6bJ9NjZaGx5VobOy5SyoXGEn6q6wFaaTe/gc2jf4dmNavAv4agL8LalmbFKtXYkRQ x6JEZ4rOuMDJw08XfJ1u5FjWp2xHH7ioD2i/zZnExvJd07SYum7oiAvzjuSKtCak5+nL XVfQ== 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=PbngI1x45WG0Dj+cWyNRbiJ8XHkY6KMeJj98Xvf7rH4=; b=gZNL/UNPrPoN1iDWC9FJSreS/TAhQ8PfMahVLU//pBmL4PBPXChvBczaLy7JMOZM60 Z3ZoY/SEjcIsfUWI2ZLuupZkEwmIyp1gq/0BdAFOWbf39zxR2/W17iv3in5CAIqq2fxi Xy2B/Td/vc8/PnE2Ww4SzVPe57UZPqrXyNv2cwkbh/eglsO5cF+qFqRaW8roSxd9mPsK zHYDTBGiGcUjIIVz3x1LYRhqZxqgebG7bFmwJGJ02aIt4DIdm2XpWwrqz/CV/2oC+h18 J/5H0PS58IA4CX2yzj1rEznoyu5i8DBA97aB8/qsLdFBymxcMqv0rdiH6Rf0FPBu2c2m HYCA== X-Gm-Message-State: AFqh2kpY49t7D5ipnT8a0OX0rsuh+bPn26wDQLXZ9OSrnUN+1/wP+Jyd u/403CyBf9NFTj61XJYCK9XLQ1if5Csn/kbwPpE= X-Google-Smtp-Source: AMrXdXueaFau9Bs5d0hYqlfJnVo24HUntnH6hlew8tVrtRc09TNgiq02I58e4KbOGC7E1TScI4uZgMpseIjdVWsptWQ= X-Received: by 2002:a17:906:40d7:b0:836:e897:648a with SMTP id a23-20020a17090640d700b00836e897648amr218299ejk.94.1673976323181; Tue, 17 Jan 2023 09:25:23 -0800 (PST) MIME-Version: 1.0 References: <20230112155326.26902-1-laoar.shao@gmail.com> In-Reply-To: From: Alexei Starovoitov Date: Tue, 17 Jan 2023 09:25:12 -0800 Message-ID: Subject: Re: [RFC PATCH bpf-next v2 00/11] mm, bpf: Add BPF into /proc/meminfo To: Yafang Shao Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>, Vlastimil Babka , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Tejun Heo , dennis@kernel.org, Chris Lameter , Andrew Morton , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , linux-mm , bpf Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: j57dgmf5s9i5fp64fje7kwg67xseziqu X-Rspam-User: X-Rspamd-Queue-Id: C3B3D40005 X-Rspamd-Server: rspam06 X-HE-Tag: 1673976324-77333 X-HE-Meta: U2FsdGVkX19vyxQr7asjL+tYe0AhqFcDy0r7YtJ7038QvqLBiEQST/3CKC0KyvW9+11WEQGnW9iRe0LtAwWrOUy7/kAm4qXfJ2FdVTXQP/8kH2unBBqArKlgu6IsRGDJd8412fbBzPhYg0O0AWDNS+lMTybs/t+s2qooFBx70dn/wUDQDwH6LzvQOmLJeUyQhnY3vnSNDy1cXqlwYNbd0nFVkcz2qgFQ7BeonJVl8PqVhvbf7WfZTwcmd4K+HxE6cXPCVSwXfzrh770gvT0CMWQx5yw0Dp9O/e7/cvBX1yhS9JNtY727J4B9t5rYQTYsuHdnAIRo32h+4uEN/moZZQ6BSUuAt4AvB/BdFGJKq3CVo+O1jH02a4aVbGql+Y0wsSq7cBVHz6NrVFrAwTnYkov5tk1iopJs1wj7dNb9qLO8O7Ld6/lWhQcPoHeP7S+c5fmVjXb2AbqAXisEBBYDLJUBRkUDaa0UmAuLOb8CG+qyZxhpKNIEo0XHbS4ASe0hMuBpnLdjbISS8td1dDOU5LG6VV8VJixv3eaQntsl4lCQnzhx3mbmHCOBDQjE/ZQs6+T6nqXSJkap8abQA1qnttaUGgaW3jqsu2swNiD3Karxx83JUDOo/s59k/laPoxgujDMMrvVMg0sD4Y57Ckp+R9DvcdGx+rw1SuWPAIs35+Y4b+bCsSz11zvK+Ar98nR96q7uFxGoZElfFOMrIxEdrfiWATVyFF2gFVDMmkJhk5IHZfrY48ht2CZdW1JzUV486IaPEREMXlc+RnKY+wl9jFwrDIf/A3VbTcQNTxTKbevjkmrJ/MJpnE4rSyo8Mz6X9535rNoWonE84dvI01YXuX/7mWNh7PxUhxApDLfhcqtsMy9o4iVSWkB5lvUDjGcDZTXRNbZxpOvVnROX3SfGAZpOAmnRxrcWTHHmBkeUvTBTRTLhyhDdQXU/UBghKNX5EBfrsvE7IyKqIf9SXZ GQMWqWYq wp5qOhrTTZNPdpKWmH5yJNwttzbvC9WwiYPGbfGMmcIDjrELAaOM42LBv6zH5bJMg0+IHeE2vvsrtK16wGBt+p0a59PvriW/fNm/NLGDDYhowPiPps1D3O08Jdj8Gg6CqdQVE3BMFuEauVBSpiQXa42kET/9SiT8FbBNz 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, Jan 13, 2023 at 3:53 AM Yafang Shao wrote: > > On Fri, Jan 13, 2023 at 5:05 AM Alexei Starovoitov > wrote: > > > > On Thu, Jan 12, 2023 at 7:53 AM 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: 350 kB <<< the size of preallocated memalloc pool > > > > > > (create hash map) > > > > > > $ bpftool map show > > > 41549: hash name count_map flags 0x0 > > > key 4B value 4B max_entries 1048576 memlock 8388608B > > > > > > $ grep BPF /proc/meminfo > > > BPF: 82284 kB > > > > > > So the real memory size is $((82284 - 350)) which is 81934 kB > > > while the memlock is only 8192 kB. > > > > hashmap with key 4b and value 4b looks artificial to me, > > but since you're concerned with accuracy of bpftool reporting, > > please fix the estimation in bpf_map_memory_footprint(). > > I thought bpf_map_memory_footprint() was deprecated, so I didn't try > to fix it before. It's not deprecated. It's trying to be accurate. See bpf_map_value_size(). In the past we had to be precise when we calculated the required memory before we allocated and that was causing ongoing maintenance issues. Now bpf_map_memory_footprint() is an estimate for show_fdinfo. It can be made more accurate for this map with corner case key/value sizes. > > You're correct that: > > > > > 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)` > > > > So just teach bpf_map_memory_footprint() to do this more accurately. > > Add bucket size to it as well. > > Make it even more accurate with prealloc vs not. > > Much simpler change than adding run-time overhead to every alloc/free > > on bpf side. > > > > It seems that we'd better introduce ->memory_footprint for some > specific bpf maps. I will think about it. No. Don't build it into a replica of what we had before. Making existing bpf_map_memory_footprint() more accurate. > > bpf side tracks all of its allocation. There is no need to do that > > in generic mm side. > > Exposing an aggregated single number if /proc/meminfo also looks wrong. > > Do you mean that we shouldn't expose it in /proc/meminfo ? We should not because it helps one particular use case only. Somebody else might want map mem info per container, then somebody would need it per user, etc. bpftool map show | awk solves all those cases without adding new uapi-s.