From patchwork Thu Feb 2 01:41:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13125206 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 4EDBEC05027 for ; Thu, 2 Feb 2023 01:42:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC09E6B0083; Wed, 1 Feb 2023 20:42:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A70716B0085; Wed, 1 Feb 2023 20:42:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9380C6B0087; Wed, 1 Feb 2023 20:42:40 -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 86C4F6B0083 for ; Wed, 1 Feb 2023 20:42:40 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 63363C0D88 for ; Thu, 2 Feb 2023 01:42:40 +0000 (UTC) X-FDA: 80420652480.15.A8D0B8C Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf14.hostedemail.com (Postfix) with ESMTP id 8F38F100018 for ; Thu, 2 Feb 2023 01:42:38 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=l3QJYqqB; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 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=1675302158; 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:in-reply-to:references:references:dkim-signature; bh=lKQM5vml6SlvXVBphY+FPvXDlacnwZjaLK4//iP66Rc=; b=5P0IVFr9iIJ1yPtLmvrwkrhIu6WariSnPmBxk7y91AQBDX7ZVTKFcSQAkItb+FbnnPOCgm EWoQyA1Ft+Ahemms6Mxdl9UTdCH5v3tzzmWz7cnbJa0i4dp4zL+N109ka4+Bjatdek2yHG EI0exZzWD1XVKQndqalewITsu/KxLac= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=l3QJYqqB; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 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=1675302158; a=rsa-sha256; cv=none; b=3o/w0Rs9TfDAca9zbKTH2jPS9ZJxbwO65XC8/k7DgaOZM4m+2xhfQZSAnRkx31pZAQQoSb JHf+UqwYzW4JjBEUgpD9s7NBvwRrYZT1RmjrKY3S5uf69sCEhGv0MzibINnWFayUV3HKNR 9Foe7mMirIzweUCUj3UoMTglr84M4qk= Received: by mail-pl1-f171.google.com with SMTP id k13so431046plg.0 for ; Wed, 01 Feb 2023 17:42:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lKQM5vml6SlvXVBphY+FPvXDlacnwZjaLK4//iP66Rc=; b=l3QJYqqBg/FLpgLF3w9RJzT6LCxhxV+XkCfXCmvbcqX9dmtvv9gqm2MxgFHNi3KsIH L+1/Jzgydg9Pvnr7vXfwlJEXyt4Oe1rE4kn05ybn6xcyMfcQMRzN4eG0/xL7461EBLbU vf8jElG3ItPsPUFX6RfXXViWPo74PSCLHWNs+XuIEBTlGbqhkMQOkJwZ0PF3jG4JRuIe i7wvYJRzchy9EPmYoVbqXKwf7JSxNNIoB43hXNxIn/Q4QNrIUw3Vxkf18pSPBnzibW3l 6u0M80ydQuUtrqXBFb2K8aWPsqAn+/XoHwWw7L3Uk3NbQoeBfk+DJU4Lww1vd2yFr3da kx9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lKQM5vml6SlvXVBphY+FPvXDlacnwZjaLK4//iP66Rc=; b=c7Z/i461o4KVtMRSiVdZj8qHZhKqMZa3gBA0Tw+WZVkXMP93frGfBXEltbo8uJo7gB 8nL2K/ZD/F0GzZFd32vcLfzLOBxnBnGLhS644/58qjwD8CRd8t1zIbK6C3HBD/VyTFTN X2ljXm9n6NQB8LNxrZ1vZW1/OSi17e/hUA4ay2DqjynD98gty6H9Geg1gN0u5gUHI1wy e/51vy1ghM23Ux3mzPveSo1246/gVQ4JLWZvTP730eF13zJhjgQUiFWxhPryloz7mWOX q7oTyL/8dq5j/5pZAyFptgFVQMLKQKuRSMI3QFLpCW9PBxuEj6xsSLTALliWdYGfNpyK losg== X-Gm-Message-State: AO0yUKXWRCKFvfm6aWqer4pQDpWfq/I5PF1xWRq93sczRSIn5dVBZghz UwM79jdad3jXGiz6DpqMAms= X-Google-Smtp-Source: AK7set9ELt2JOyjgVr4+8gniE1xArw7bOYKUK5RSQApxYJDzJ0/kzRDbUTtaVk55GHthACef4ci2vQ== X-Received: by 2002:a05:6a20:1603:b0:bc:2665:cbe6 with SMTP id l3-20020a056a20160300b000bc2665cbe6mr5916806pzj.5.1675302157573; Wed, 01 Feb 2023 17:42:37 -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.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42:37 -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 5/7] bpf: add new map ops ->map_mem_usage Date: Thu, 2 Feb 2023 01:41:56 +0000 Message-Id: <20230202014158.19616-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230202014158.19616-1-laoar.shao@gmail.com> References: <20230202014158.19616-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8F38F100018 X-Stat-Signature: gat3acq16metbg6bydtzwgb6kjbr9tjs X-Rspam-User: X-HE-Tag: 1675302158-845551 X-HE-Meta: U2FsdGVkX1/sNEIfH6uVc1Ew72PmRwg6a4HFQqL99GaPZc2FHT9KuEtmu5wJBxfW4fdTYBNpYMZ+Y7R6IacpzxdtYXRym+cbsfR1WwvXV3HZNoQzjf5UgBDRElWQS+ogi06CXYdAwt7hAUOTOKLZxxZ00ktsYAGEogA3a5H0XPP7vdUpprQvSFSbAMdABAcRCxrnWnZfjEW2Atgb0XLD94e+XRzEqhEq1ZkZd7Y4X074y7QgPY1RDcXN+fVoXZBt3CZkcWq0ppNotTBzugP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6N0gOFfrZYTC1oHk6pZMGY9nIw1NeoHfyz2dHg8+QsXoVfuM4/rua59Mzgv1VsPLQuDWHpf0BogXZ+xoEAd/Jdyu+5XdbTigo2HmtD+30oAhXOjfSMnJhhyQDwspPZRTgB2hta5JihvIuP4eMlvGnhD5KX82boZGYxTzfDzwnNDLDEj6LDsmUWxr8jWQgMo9dmrSBecu9gCQHi75uYz/1edtZ4Q== 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: Add a new map ops ->map_mem_usage to print the memory usage of a bpf map. ->map_mem_usage will get the map memory usage from the pointers which will be freed in ->map_free. So it is very similar to ->map_free except that it only get the underlaying memory size from the pointers rather than freeing them. We just need to keep the pointers used in ->map_mem_usage in sync with the pointers in ->map_free. This is a preparation for the followup change. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index e11db75..10eb8e9 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -160,6 +160,8 @@ struct bpf_map_ops { bpf_callback_t callback_fn, void *callback_ctx, u64 flags); + unsigned long (*map_mem_usage)(const struct bpf_map *map); + /* BTF id of struct allocated by map_alloc */ int *map_btf_id; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 99417b3..df52853 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -758,16 +758,20 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) } #ifdef CONFIG_PROC_FS -/* Provides an approximation of the map's memory footprint. - * Used only to provide a backward compatibility and display - * a reasonable "memlock" info. - */ -static unsigned long bpf_map_memory_footprint(const struct bpf_map *map) +/* Show the memory usage of a bpf map */ +static unsigned long bpf_map_memory_usage(const struct bpf_map *map) { unsigned long size; - size = round_up(map->key_size + bpf_map_value_size(map), 8); + /* ->map_mem_usage will get the map memory size from the pointers + * which will be freed in ->map_free. So it is very similar to + * ->map_free except that it only get the underlaying memory size + * from the pointers rather than freeing them. + */ + if (map->ops->map_mem_usage) + return map->ops->map_mem_usage(map); + size = round_up(map->key_size + bpf_map_value_size(map), 8); return round_up(map->max_entries * size, PAGE_SIZE); } @@ -799,7 +803,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) map->max_entries, map->map_flags, (unsigned long long)map->map_extra, - bpf_map_memory_footprint(map), + bpf_map_memory_usage(map), map->id, READ_ONCE(map->frozen)); if (type) {