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 12233C54EBE for ; Fri, 13 Jan 2023 11:53:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8CE518E0002; Fri, 13 Jan 2023 06:53:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 87F048E0001; Fri, 13 Jan 2023 06:53:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 745EB8E0002; Fri, 13 Jan 2023 06:53:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 63E318E0001 for ; Fri, 13 Jan 2023 06:53:46 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 289291C5C83 for ; Fri, 13 Jan 2023 11:53:46 +0000 (UTC) X-FDA: 80349616452.04.18631DC Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) by imf01.hostedemail.com (Postfix) with ESMTP id 79A8C4001B for ; Fri, 13 Jan 2023 11:53:44 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=JSAEeOHh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673610824; 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=emIokTK4nKwkwZPI13VlsDTgDe45jFLOJz4i2BcmlzY=; b=boKpYqS81y2S/Q1L5R+AE+h2TPcMBJtfDywHghXQPUXeLWk5R+3lGbRur349EvYis26dtX FLFn6raCHsQFXxutbvIvaRNYlaJvTecWgH49ZElTp0SCj6mEphzUWefKe8yMJPmZQvIMNS yTwDgfZ9T7Plu+OWG4RU7U83jMgigtg= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=JSAEeOHh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.208.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673610824; a=rsa-sha256; cv=none; b=68j6/lP+NpEJWxKGVr0VBsnBzDCfJZGABYgvys7znPhltmHgKJ6OZE8XH17KUOtjiiKbB6 4BJf0EUuf7HLzOFnKplpFg3CUZEyG/2uLxHfuIEUkarxZ+q3h5Oca0iqbAB5Y1zITBqIhj uo1obMcpcQWsiptt/LEVEoM4+S+/+pQ= Received: by mail-lj1-f177.google.com with SMTP id f20so22228101lja.4 for ; Fri, 13 Jan 2023 03:53:44 -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=emIokTK4nKwkwZPI13VlsDTgDe45jFLOJz4i2BcmlzY=; b=JSAEeOHhVrTHsJFHIzbH6snvdRKEzkS5FmhbedFjSb3N/rfgw21RH9pEwNAM3A6s4F OWC8UwtdJ2meRYeqXF4rE0UqXYY0GNOKuMqCh7lD3eYTHeDUJ8M8YeLmTpfpWfgek7lH 0R+jdDaA+7XTp2uqHkNZYHDzuyNw1mKmIjSrLZfqpsKapuOh+Z1nj8DQWMjDyJj3pLNQ iceYsX4gxyWgNvbzP901zcLDA22/QGWyltya6I5/wqQKb2V/8ExX2BATCp+KBp2/PGk0 q7Y6BgvMadqDdFAK7eKciKCtFf+XhraVd05Mj4GJYRqQyPWdzfVN94CCMKbqG+buR1kA TNCQ== 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=emIokTK4nKwkwZPI13VlsDTgDe45jFLOJz4i2BcmlzY=; b=GmJENaPKSjAQcC+FL+IxwyommlkEaX198fr3CLty9ISjH0Us8HZejHk3x0kfky+4NN Nf5uVEVzqaFbcXjJTSnT2jMQ16FQN/e3+Ot283Y4e4aueU0tZbZ+MwWBoYtw7bX5Ei/Q sDq73u6lEo6sGKKqaQUfelhPizwTvom4kzCcUKQySG0A812hGcjBxWJmzDjVNnParo+M 4QcbEwrVKnxM44TsBlCq2Qly6hG1hsmwScrjJcSovkWz0WkDLQkY14ik0FRO8W0PJJeD 3G1uuwjwgagDRsuwWBjzlqpPjy8SRzihIpSdDrZHuhQwMtplbBcT4I9vCbUvjFx9zL9J AepQ== X-Gm-Message-State: AFqh2krBGt6YGGvPSbJqUgYhGpRg3i3aBRvUFfJZD0DUKjcOsV6dh81y V/dTw3HjVjF9CfVUDAbc04TnUkIdzn3IjcAd/mg= X-Google-Smtp-Source: AMrXdXtsxjamjE8mI7mZIBHqqNz94tF7cDf2yHEHbepzsl5KW9g/pi6hsFdVLuYiNi6uCK/NnQ7qgZQji+B17tt1GdY= X-Received: by 2002:a2e:86cf:0:b0:282:8f7f:1e90 with SMTP id n15-20020a2e86cf000000b002828f7f1e90mr2241754ljj.475.1673610822654; Fri, 13 Jan 2023 03:53:42 -0800 (PST) MIME-Version: 1.0 References: <20230112155326.26902-1-laoar.shao@gmail.com> In-Reply-To: From: Yafang Shao Date: Fri, 13 Jan 2023 19:53:06 +0800 Message-ID: Subject: Re: [RFC PATCH bpf-next v2 00/11] mm, bpf: Add BPF into /proc/meminfo To: Alexei Starovoitov 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-Rspamd-Queue-Id: 79A8C4001B X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: e1ne9tae1rg3z7f7u6y7ibu13bmw587t X-HE-Tag: 1673610824-228704 X-HE-Meta: U2FsdGVkX1+1LQlgINbMIesQIe2hgKR83IkQG/5MhTCWWe74r/XptJR9RwTpPNxJlEbYQn02bHv2fLyC+gmnncvh9a25mE1HKAru54BPwPabEInwHpGWxksBGY8qJxmdd0MZhIalzHBBnOq6gwXwUzRHuU4ciHTY4BvYVoBRt8Ef8EMfQiqjPvFM3AUEh+s2zDsP9v6/9vtmS8sk1X650bC9eRzcRjHe+qFN+W5omQm23wd59RvbVdLkRVpokFcZnnMKddk0KsvsCEb/2VNUvKV8TVGnrI1dGM/K1k9WRUTYCwhrk1ewOwCFBBIIz8WtQEh6o6eNoMQeOWKwoyqmaeO8REOLT5nAESsXQYyCPadPfUTTrIaySoi3j7roAX7LJakbpxLI0D7vY8CBY8jaYVuUjKvYW6PfDNg4Pq4AKzqVYwR0f03j+YUjv4c/neoCN3g70MepaJot4lzuLb5bZR9idmJKEJNLdNxwSu93iB6AYvd+hzKSRjphzITFj+rNB77ZxCxQ1A2GKuoiJUne0xNCb6mq9UVBz0YgDLs+ov/aYdWD+WyjdQ9YvjT8L3xFOQC+F+MaQapHdJhIeMF+XHSc1+4AqDYaOyyvmer9R72AxV7Ju4ZK6idfywA2FBMnPn4j8djZjdexmNQLDYK6znMXbpZkZq1bQZDRbo/4gPWs8NldsoF04X3E3u3P6yLKSWzEZHnzuv9KkAW+xFJjMCHvqWEEBVCl69IC1YpN7V9m2ylynT7GQlORecebWkMKL2GaeMnDnDNdkCLfVaW8pXwzWdeTfJzgaj3SIV2tpIvOlokzKfTs2kUNUTxB1ZFtCI1ZzP6vavsa8KscIDUwEN3qr9jtBfOBkrEPdOB+mVYbOcdI6/+FNX+LH7VTWrXoAupKryYhkfxeXKXY4B8enxgF1qQ1+ji6bQ9p2rIUDADkkoTrw6LGwINfpMBMLkB6g6QqoHvCn+QJlKuc2Gl OcaRYBAN scCLft/muQQAWZKmXx77rx75jrFeRK1vHKvi/8+50rk4nNC9bDvTo5bD4o5RUbnNGmKbLXmvulwGFcUAifGgfRuXZBO3fYRK6ibH4S1+RupB7gyh/7ujDRkMqEYHe6PpMo0QKfkYyaXFBDhywR2/6Jc5aTMGWImhtBUze 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 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. > 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. > Higher level point: Thanks for your thoughts. > 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 ? > People should be able to "bpftool map show|awk sum of fields" > and get the same number. -- Regards Yafang