From patchwork Thu Feb 2 01:41:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13125201 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 685BDC636CD for ; Thu, 2 Feb 2023 01:42:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8EB5F6B0073; Wed, 1 Feb 2023 20:42:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 89C656B0075; Wed, 1 Feb 2023 20:42:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 73C736B0078; Wed, 1 Feb 2023 20:42:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 641AD6B0073 for ; Wed, 1 Feb 2023 20:42:15 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 28E5B160DCB for ; Thu, 2 Feb 2023 01:42:15 +0000 (UTC) X-FDA: 80420651430.10.826EB78 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf07.hostedemail.com (Postfix) with ESMTP id 6E2774001A for ; Thu, 2 Feb 2023 01:42:13 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=OVEoY+w1; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf07.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.43 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=1675302133; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=tEUWQcIFypFvmGsLhrzIc9UQlWdveTNsYAoWJAcmc5w=; b=hV0cajLpNyqD3gHLx8TH8NsajJsj1EMsPWv5/Kl7FyqEgQAS0TdHWhAo7BPTmEwr3ZAn6e TH5M6mgi7uTukjkGupnSjm3Nfq0aau16neZmRFbTaAGXsJwwJtPwxPr7Bl6ReQPSMNYEOn f+QXxQgrUi+S3mMo2ToNy+4wfIFbeiY= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=OVEoY+w1; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf07.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675302133; a=rsa-sha256; cv=none; b=zyPhjdANFwEnHjlDG1v4vfLJ1CbJGu0m3N9K2avgilghCtc/rPs5wlpS+PssjwB6YohjIf XpZA0+QEPwjtMTbbLy3ON0fdksZuAcRE3vK8epDXw+JD7BUpRJLqdZs0N4RMCZ6oRiy/MN UeskBwrecRCR6nV4Bc9bOUv2cZTpWX0= Received: by mail-pj1-f43.google.com with SMTP id e8-20020a17090a9a8800b0022c387f0f93so4122618pjp.3 for ; Wed, 01 Feb 2023 17:42:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=tEUWQcIFypFvmGsLhrzIc9UQlWdveTNsYAoWJAcmc5w=; b=OVEoY+w14AwJwy7MDVvhpOaiUp7boXvCyKlJVjk881mNKpBAiwWPsBES/NckQ5Fues +bh4aOWMhfNZGQmEVuatZCYkWLDx8py/TR68p7kWe0EvJ/OSaHQtnBp6ikJpr87dq+lt 3N6+A2IKg0D/8BbA6/sp2+T0KIhuIYKmi8F5Sjuaps+Bh15HkcVlReyPTAC5vaNMTwoU DSHNnQj/zPp1mBk3oPcHbJWiJK2/WOkEXR1J9rTMD3wSWOnviuOhhYeQIXwDPWPs8oT1 aWPYaGidf2GzP1YQ/Uszkei08wNN59qrR7x4ruhLfMztngpHwO6GEdPXk/Zv05lKcpwN niMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tEUWQcIFypFvmGsLhrzIc9UQlWdveTNsYAoWJAcmc5w=; b=3wPq+ZiUuGxHS2OrUqnd//qqSXWONlc6M3QDEta8auVeNkm7uKDTYtnncnzF27Nais Jl/glweRs97So4BCMPjv+iKlVyICw3AqQ7exWOxHKuUiudayx6lMokZEpwKXfZO9rJUv jyeHAcaeBb3ZhsQL+fMCcA1DGkhhCX8gygarz2ctbh7hq8laJLtdCdimJiVeDQnNDG+P lEdSqnkuO1XI4cRrQrZrnLtq0BJxwFp4P+du02f88VFbKKQpR0ba6Yz1Zh/VvCW1lxo5 Ori1NSGMor6hW9S9W7gQnlxhNCV1PRzworHzVymtoy47Ts7rYRtBvTGT4hgd1jWy6Pca mAZQ== X-Gm-Message-State: AO0yUKXsl4wPP7XpJddFZoQpu5gPTQISmt4V5sdx94fWDXgeZS1iYT8p 5etkFHsztKngeCGEZui4ZcA= X-Google-Smtp-Source: AK7set/8Jbr0DB4NDuS1o52qIhfr6sS9+aXXJdodhq6t7Qxd+CRa+PxZk46WOrJfj8EX23AbWFi/sg== X-Received: by 2002:a05:6a20:d906:b0:bc:f45f:ecf7 with SMTP id jd6-20020a056a20d90600b000bcf45fecf7mr4748352pzb.2.1675302132057; Wed, 01 Feb 2023 17:42:12 -0800 (PST) Received: from vultr.guest ([2001:19f0:7001:3f48:5400:4ff:fe4a:8c8b]) by smtp.gmail.com with ESMTPSA id t191-20020a6381c8000000b004e8f7f23c4bsm6594205pgd.76.2023.02.01.17.42.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42:11 -0800 (PST) From: Yafang Shao To: 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, 42.hyeyoo@gmail.com, vbabka@suse.cz, urezki@gmail.com Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 0/7] bpf, mm: bpf memory usage Date: Thu, 2 Feb 2023 01:41:51 +0000 Message-Id: <20230202014158.19616-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 6E2774001A X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: i41ip9a8n4yqponeijtj4sh8xuchin8r X-HE-Tag: 1675302133-363887 X-HE-Meta: U2FsdGVkX19E4meiFotuLAcWIStxn8KPtw+K66MaRNGqt6Zz7/Oj3aF30TSKpJH9mUSdBnFL2/1Y2KNyxe23jluj4bBxDLPbnkMrK3IYeXhZnoL1d9Q92xGheu956ufQFM7UigYUXq538IJNfVrf7/cdJBY0xnAdT+/g7+QdUkf1QaTahICSJlh0FRkik6suf0p54gVJE3x+HJe1l5jd95s+MDAOsADAm2ioDf3Cw9f+NrFlbN/v64/feKAk84+RD5LjRgR2NW6KX9i0rt9hXKx6Wx2nusRu1AzH7uyNIN9gTeyrxfm9EEgipO6mxhjO0Sc/h/brLtLGqOT0ePT1o9wUksW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Y6u6XQyKjKVNZcWEFNPne64jtwQ0bfx+GN8IjqRwXsswkXlTvF/NIXT4+O6nmd+Ca/gvcSB19SiQJZUmhy78ZSmW/tTy6AVLvhT6AJCIi0bZczewq5hEvPqVmY6/Xh/e1EnHKReN3TSAFaeiLVMkej+97viTSBfDIdPPudk1PYtTXPqefa3OM+t2SNl+F/v0ZGq66R+k/S5mrA== 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: Currently we can't get bpf memory usage reliably. bpftool now shows the bpf memory footprint, which is difference with bpf memory usage. The difference can be quite great between the footprint showed in bpftool and the memory actually allocated by bpf in some cases, for example, - non-preallocated bpf map The non-preallocated bpf map memory usage is dynamically changed. The allocated elements count can be from 0 to the max entries. But the memory footprint in bpftool only shows a fixed number. - bpf metadata consumes more memory than bpf element In some corner cases, the bpf metadata can consumes a lot more memory than bpf element consumes. For example, it can happen when the element size is quite small. We need a way to get the bpf memory usage especially there will be more and more bpf programs running on the production environment and thus the bpf memory usage is not trivial. This patchset introduces a new map ops ->map_mem_usage to get the memory usage. In this ops, the memory usage is got from the pointers which is already allocated by a bpf map. To make the code simple, we igore some small pointers as their size are quite small compared with the total usage. In order to get the memory size from the pointers, some generic mm helpers are introduced firstly, for example, percpu_size(), vsize() and kvsize(). This patchset only implements the bpf memory usage for hashtab. I will extend it to other maps and bpf progs (bpf progs can dynamically allocate memory via bpf_obj_new()) in the future. The detailed result can be found in patch #7. Patch #1~#4: Generic mm helpers Patch #5 : Introduce new ops Patch #6 : Helpers for bpf_mem_alloc Patch #7 : hashtab memory usage Future works: - extend it to other maps - extend it to bpf prog - per-container bpf memory usage Historical discussions, - RFC PATCH v1 mm, bpf: Add BPF into /proc/meminfo https://lwn.net/Articles/917647/ - RFC PATCH v2 mm, bpf: Add BPF into /proc/meminfo https://lwn.net/Articles/919848/ Yafang Shao (7): mm: percpu: fix incorrect size in pcpu_obj_full_size() mm: percpu: introduce percpu_size() mm: vmalloc: introduce vsize() mm: util: introduce kvsize() bpf: add new map ops ->map_mem_usage bpf: introduce bpf_mem_alloc_size() bpf: hashtab memory usage include/linux/bpf.h | 2 ++ include/linux/bpf_mem_alloc.h | 2 ++ include/linux/percpu.h | 1 + include/linux/slab.h | 1 + include/linux/vmalloc.h | 1 + kernel/bpf/hashtab.c | 80 ++++++++++++++++++++++++++++++++++++++++++- kernel/bpf/memalloc.c | 70 +++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 18 ++++++---- mm/percpu-internal.h | 4 ++- mm/percpu.c | 35 +++++++++++++++++++ mm/util.c | 15 ++++++++ mm/vmalloc.c | 17 +++++++++ 12 files changed, 237 insertions(+), 9 deletions(-)