From patchwork Mon Feb 27 15:20:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153784 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 149E4C64ED6 for ; Mon, 27 Feb 2023 15:20:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229974AbjB0PUy (ORCPT ); Mon, 27 Feb 2023 10:20:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230131AbjB0PUx (ORCPT ); Mon, 27 Feb 2023 10:20:53 -0500 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6AA622A20 for ; Mon, 27 Feb 2023 07:20:48 -0800 (PST) Received: by mail-pg1-x529.google.com with SMTP id bn17so3741421pgb.10 for ; Mon, 27 Feb 2023 07:20:48 -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=EJddoMByRjHQiNUI72hW0/VP9TMtY4CHZ+jz5kdN4Dk=; b=ML0n077AfddxJBCHhaNpn/6+Y1LdZ/ciH4DnF+yE0e/G7AfHanvJRZrxsgdaFs5cms F1jf18DL+jQYCUaruCv+sMLNb68vRHK4bliwI2sTE1LxhpfqVfI5hPYBZN5s/yqxvSVq T7N78whzgPpSAs4LfnuDR4At95hpw/PLV9R8rhUV9PM9U5aezTcOn5i4SSWhYSXL5OCW BIh5K1PZm+7bCKnh5vvvIlvZT/Fek73X+BabasTTJ6RO/MYE0+uwoXrCUKFk8UpILU8k s0jPeSeBvPQgNMRtp/oLa5gKZPykdDJunydAGjBg+hEp00Kir19OKTj67h7ppyPEQ9tL ypIg== 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=EJddoMByRjHQiNUI72hW0/VP9TMtY4CHZ+jz5kdN4Dk=; b=o5XuaLaVWXwq90xAu5OqO2JIT/3a3dTLXGCTsQQOognHKLutN+MnBv7VMHvnGdwhEw +laDYo386yGTINJVMakn/4hPk1Ck4uGBsLIQDDd4QTjpXuCatt2upKyugjdRcu0Tc+QV c7WFXqPCJ0W2SYxlaCTRyMTNEC3+Aqe3qIy5PEs1Yk84N+l6pWeCQQK44WHHowRnCYej LJGn6YjLLHeevdd0YE6w3jViD1g2kID8cFvGSojzYD/Z4qxBtqZMJ6aL98ONv+1kgliX VByXJ0+sCWD2k7nyZcGO3tkdivw/U0Xudz1Nx9hOwZCDIMGwkDlhgvIlrXDGMGNZbHT0 iS8Q== X-Gm-Message-State: AO0yUKWEbFECKrydIOr5q50u0OBzyy0A+JvhX3uARr2YvEDZ4C/TGjsW WT4mIkAoZ01B5I4d6tbmhak= X-Google-Smtp-Source: AK7set+l9FNmyImZupwLcpZal1e/zt1ODkBNMqz0bY8tGVWURAUwvDxVH+XtDuwQ6zxppUEGKWcwug== X-Received: by 2002:aa7:95a4:0:b0:5a8:c6c1:c9ae with SMTP id a4-20020aa795a4000000b005a8c6c1c9aemr22056386pfk.30.1677511248302; Mon, 27 Feb 2023 07:20:48 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.20.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:20:47 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 01/18] bpf: add new map ops ->map_mem_usage Date: Mon, 27 Feb 2023 15:20:15 +0000 Message-Id: <20230227152032.12359-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add a new map ops ->map_mem_usage to print the memory usage of a bpf map. This is a preparation for the followup change. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 15 +++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 520b238..bca0963 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -161,6 +161,8 @@ struct bpf_map_ops { bpf_callback_t callback_fn, void *callback_ctx, u64 flags); + u64 (*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 e3fcdc9..8333aa0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -771,16 +771,15 @@ 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 u64 bpf_map_memory_usage(const struct bpf_map *map) { unsigned long size; - size = round_up(map->key_size + bpf_map_value_size(map), 8); + 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); } @@ -803,7 +802,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) "max_entries:\t%u\n" "map_flags:\t%#x\n" "map_extra:\t%#llx\n" - "memlock:\t%lu\n" + "memlock:\t%llu\n" "map_id:\t%u\n" "frozen:\t%u\n", map->map_type, @@ -812,7 +811,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) { From patchwork Mon Feb 27 15:20:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153785 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B68D4C64ED8 for ; Mon, 27 Feb 2023 15:20:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230131AbjB0PU4 (ORCPT ); Mon, 27 Feb 2023 10:20:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbjB0PUz (ORCPT ); Mon, 27 Feb 2023 10:20:55 -0500 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4368C22DF8 for ; Mon, 27 Feb 2023 07:20:52 -0800 (PST) Received: by mail-pg1-x536.google.com with SMTP id 132so3732779pgh.13 for ; Mon, 27 Feb 2023 07:20:52 -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=Fq2nVwWGrK3a0pC1Iu9mkxB4TlvHW+/gHuyRu9N7pEk=; b=Twx5i7unsp4aKk6w3XQTYkH3OnyZTdoNsmm1vZhFfN+7Kgp5ttFpWKd42sVzddqWwH 4K3c/jjFWsG2CtolqTVWclzcv80sdDpikMYbozih6dQRfkXjuj67dl5+JjMlvAvUloAZ W3lN2VWO9A0VHNv1W7vq9abRkkHurUaIbnlRnsEoKObkbWuj74OkZBmHMDisS9IkiQvm SIgfl2WyxUk4bzJRDhiXGCiWnF710lC0QTSyAPx8xQldGL+C2hhjcTXUJKZAu54OI4zd iaM8LBtxH64evspHP8ZPgOEKS2/cPcUZfTipSFOUzKqpKxGo9Ugy1hDYP0DMWxZSGwn7 JRZw== 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=Fq2nVwWGrK3a0pC1Iu9mkxB4TlvHW+/gHuyRu9N7pEk=; b=47r2D3YWSdHfeepr4Aw23QeGWYIsBLTgYf4lK2ioBijea1EZYDskuYwFrfqWdge/9e ZrC82vtum+M4nSVT+6kWxTYt7TT5MPCgneNSMwHeLxrYaj3hUQwAuBgH+ALiv1wVD2wW eLX6oR53+JD7XBiM+iWYlFy1bzihy7s7W9h8rAMBCZkyn8A5b7USNSe4iJ3K+3AjowN4 qlVr+ob2916RSWouYw4vIDDlUGECa5H1wZgo4rkxprfteMM3QIimP4GdWiNMHr8i8ole vIz8PQ+kagZgc3shOEd16duYxE67Sp0an5rObw0dNy1WbFnfX9Pc2YraTtPIlkp9BkAk CI7w== X-Gm-Message-State: AO0yUKV4iocCt2HDwdnUlKFClmuU3W04MbYT2oDKVO+hkDUJxyRdRTyy sp1tPpA+TeMyI7qxXHxBUpM= X-Google-Smtp-Source: AK7set8EvB2hw+Ezr/FUCw3qH3m7547RAyp6gbaepcFygtWS9prxB93R3yQLwWOA3jP7U70R8zdiDQ== X-Received: by 2002:a62:8494:0:b0:5a8:9fc6:6fb5 with SMTP id k142-20020a628494000000b005a89fc66fb5mr27121834pfd.16.1677511251730; Mon, 27 Feb 2023 07:20:51 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.20.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:20:51 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 02/18] bpf: lpm_trie memory usage Date: Mon, 27 Feb 2023 15:20:16 +0000 Message-Id: <20230227152032.12359-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net trie_mem_usage() is introduced to calculate the lpm_trie memory usage. Some small memory allocations are ignored. The inner node is also ignored. The result as follows, - before 10: lpm_trie flags 0x1 key 8B value 8B max_entries 65536 memlock 1048576B - after 10: lpm_trie flags 0x1 key 8B value 8B max_entries 65536 memlock 2291536B Signed-off-by: Yafang Shao --- kernel/bpf/lpm_trie.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c index d833496..e0ca08e 100644 --- a/kernel/bpf/lpm_trie.c +++ b/kernel/bpf/lpm_trie.c @@ -720,6 +720,16 @@ static int trie_check_btf(const struct bpf_map *map, -EINVAL : 0; } +static u64 trie_mem_usage(const struct bpf_map *map) +{ + struct lpm_trie *trie = container_of(map, struct lpm_trie, map); + u64 elem_size; + + elem_size = sizeof(struct lpm_trie_node) + trie->data_size + + trie->map.value_size; + return elem_size * trie->n_entries; +} + BTF_ID_LIST_SINGLE(trie_map_btf_ids, struct, lpm_trie) const struct bpf_map_ops trie_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -733,5 +743,6 @@ static int trie_check_btf(const struct bpf_map *map, .map_update_batch = generic_map_update_batch, .map_delete_batch = generic_map_delete_batch, .map_check_btf = trie_check_btf, + .map_mem_usage = trie_mem_usage, .map_btf_id = &trie_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153786 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E96FDC64ED8 for ; Mon, 27 Feb 2023 15:21:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229470AbjB0PVA (ORCPT ); Mon, 27 Feb 2023 10:21:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbjB0PVA (ORCPT ); Mon, 27 Feb 2023 10:21:00 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F04222A16 for ; Mon, 27 Feb 2023 07:20:55 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id c1so7077191plg.4 for ; Mon, 27 Feb 2023 07:20:55 -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=YxpNF8yA/J5C+4abp2EOSZEhHe3V+KL1xLxCSAvbtOY=; b=pNaTN9Ilg4/QvSkjfBXARAhukWDcthc6LI8K7vHfHt6hYO9c3UDpjM25hG3BdG23ZP tJbM0Ars4uuYwjrAElMkuLVAW9X2snSM9bMnvEd/nuxjPY14z32auFu8SB2palNlewr6 Cdab7akGdyDF5H+dmTxw/6BJUeo+RnibgH6PHrmXTLiQMFjZQdAxS9U38b57ivMHAyPD D6sK6ikjqBSe5NazCb+lrdE+KhpyIAmTlUcsZysGkMZAJdBAMQ4HdQ+s/Ln0nTVuuO31 ECiO7/762ZsUAVRX8bbpxC+2IXcL7EeF4kz/pOZnBZ1X7JUaxROMrz6jNnpeBD7zB6Jk 31kw== 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=YxpNF8yA/J5C+4abp2EOSZEhHe3V+KL1xLxCSAvbtOY=; b=bZSDIODUSgi41338gKv5MERtEsfhlrOu+JPyLrCPhqImEUl2fj8SAlEAhcwSYAIFoz eHJn4m386Ywl0tmR4n/WfZiyMjBOJrzwhNyaVtNOPrOhihY3C03GzDmkK/LrL/vnPiiw Ociid1q2dOl1z/07EAyl0FIXm7EbqiXJF8dPcVfnI4pP7eJFp89PqLCXVvxoG+xUCD4D F/rLE0xrkjYzOTg6RvdJ4D7MOFjq5vrJ9iQLO86ApPZs5KEtMIMnVEaYhbswWbLx0CE/ scxuBIyNxxZYBryaaLqn2Hh5nEdd/HtlwW4KY1rf6wbSb0AZYAgzUiMl8cP8cE4o4t9R F+Gg== X-Gm-Message-State: AO0yUKUtG+E+APut44gjYcBo4Kk2DLx8ITSQAGHzr3ajTGaREJJ7/pun uGvZj1z5yGF+1E7aX1FmDn4= X-Google-Smtp-Source: AK7set8dm/Te9f5D41VKK8TT2RpjS1INmSuaN9TWkRsvbeoeJoynIBbERvX029Z+DtP3WSJIswpyxw== X-Received: by 2002:a05:6a20:6d8a:b0:cd:18d7:f131 with SMTP id gl10-20020a056a206d8a00b000cd18d7f131mr4764233pzb.7.1677511255104; Mon, 27 Feb 2023 07:20:55 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.20.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:20:54 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 03/18] bpf: hashtab memory usage Date: Mon, 27 Feb 2023 15:20:17 +0000 Message-Id: <20230227152032.12359-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net htab_map_mem_usage() is introduced to calculate hashmap memory usage. In this helper, some small memory allocations are ignore, as their size is quite small compared with the total size. The inner_map_meta in hash_of_map is also ignored. The result for hashtab as follows, - before this change 1: hash name count_map flags 0x1 <<<< no prealloc, fully set key 16B value 24B max_entries 1048576 memlock 41943040B 2: hash name count_map flags 0x1 <<<< no prealloc, none set key 16B value 24B max_entries 1048576 memlock 41943040B 3: hash name count_map flags 0x0 <<<< prealloc key 16B value 24B max_entries 1048576 memlock 41943040B The memlock is always a fixed size whatever it is preallocated or not, and whatever the count of allocated elements is. - after this change 1: hash name count_map flags 0x1 <<<< non prealloc, fully set key 16B value 24B max_entries 1048576 memlock 117441536B 2: hash name count_map flags 0x1 <<<< non prealloc, non set key 16B value 24B max_entries 1048576 memlock 16778240B 3: hash name count_map flags 0x0 <<<< prealloc key 16B value 24B max_entries 1048576 memlock 109056000B The memlock now is hashtab actually allocated. The result for percpu hash map as follows, - before this change 4: percpu_hash name count_map flags 0x0 <<<< prealloc key 16B value 24B max_entries 1048576 memlock 822083584B 5: percpu_hash name count_map flags 0x1 <<<< no prealloc key 16B value 24B max_entries 1048576 memlock 822083584B - after this change 4: percpu_hash name count_map flags 0x0 key 16B value 24B max_entries 1048576 memlock 897582080B 5: percpu_hash name count_map flags 0x1 key 16B value 24B max_entries 1048576 memlock 922748736B At worst, the difference can be 10x, for example, - before this change 6: hash name count_map flags 0x0 key 4B value 4B max_entries 1048576 memlock 8388608B - after this change 6: hash name count_map flags 0x0 key 4B value 4B max_entries 1048576 memlock 83889408B Signed-off-by: Yafang Shao Acked-by: Hou Tao --- kernel/bpf/hashtab.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 5dfcb5a..6913b92 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -2175,6 +2175,44 @@ static int bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_f return num_elems; } +static u64 htab_map_mem_usage(const struct bpf_map *map) +{ + struct bpf_htab *htab = container_of(map, struct bpf_htab, map); + u32 value_size = round_up(htab->map.value_size, 8); + bool prealloc = htab_is_prealloc(htab); + bool percpu = htab_is_percpu(htab); + bool lru = htab_is_lru(htab); + u64 num_entries; + u64 usage = sizeof(struct bpf_htab); + + usage += sizeof(struct bucket) * htab->n_buckets; + usage += sizeof(int) * num_possible_cpus() * HASHTAB_MAP_LOCK_COUNT; + if (prealloc) { + num_entries = map->max_entries; + if (htab_has_extra_elems(htab)) + num_entries += num_possible_cpus(); + + usage += htab->elem_size * num_entries; + + if (percpu) + usage += value_size * num_possible_cpus() * num_entries; + else if (!lru) + usage += sizeof(struct htab_elem *) * num_possible_cpus(); + } else { +#define LLIST_NODE_SZ sizeof(struct llist_node) + + num_entries = htab->use_percpu_counter ? + percpu_counter_sum(&htab->pcount) : + atomic_read(&htab->count); + usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries; + if (percpu) { + usage += (LLIST_NODE_SZ + sizeof(void *)) * num_entries; + usage += value_size * num_possible_cpus() * num_entries; + } + } + return usage; +} + BTF_ID_LIST_SINGLE(htab_map_btf_ids, struct, bpf_htab) const struct bpf_map_ops htab_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -2191,6 +2229,7 @@ static int bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_f .map_seq_show_elem = htab_map_seq_show_elem, .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_hash_elem, + .map_mem_usage = htab_map_mem_usage, BATCH_OPS(htab), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2212,6 +2251,7 @@ static int bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_f .map_seq_show_elem = htab_map_seq_show_elem, .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_hash_elem, + .map_mem_usage = htab_map_mem_usage, BATCH_OPS(htab_lru), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2363,6 +2403,7 @@ static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key, .map_seq_show_elem = htab_percpu_map_seq_show_elem, .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_hash_elem, + .map_mem_usage = htab_map_mem_usage, BATCH_OPS(htab_percpu), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2382,6 +2423,7 @@ static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key, .map_seq_show_elem = htab_percpu_map_seq_show_elem, .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_hash_elem, + .map_mem_usage = htab_map_mem_usage, BATCH_OPS(htab_lru_percpu), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2519,6 +2561,7 @@ static void htab_of_map_free(struct bpf_map *map) .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem, .map_gen_lookup = htab_of_map_gen_lookup, .map_check_btf = map_check_no_btf, + .map_mem_usage = htab_map_mem_usage, BATCH_OPS(htab), .map_btf_id = &htab_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153787 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D14DC64ED8 for ; Mon, 27 Feb 2023 15:21:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229761AbjB0PVG (ORCPT ); Mon, 27 Feb 2023 10:21:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbjB0PVF (ORCPT ); Mon, 27 Feb 2023 10:21:05 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F307E22A27 for ; Mon, 27 Feb 2023 07:20:58 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id me6-20020a17090b17c600b0023816b0c7ceso2494835pjb.2 for ; Mon, 27 Feb 2023 07:20:58 -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=r4L8zLEmMO4gVgjC79EcUNMmrd+fUR2UoiFuFPERS2A=; b=SGao2w5R9Hr92U2/gUnqpxI2mnWAMexs2CUYFvSpuKS1NKIRwT9+oBieNQ7CIGagdg ip8DVfaeh26vzXlmZIqluE8ofzvHgdg+UP38KPnA/rJXtjhyw02T4ew+dADcwYElO0io ZGQmanuxLwMb3h9jFo23P0N+P36FahHetHTueVXVOuucJY6+jTM5f1AHCiHyubyT8TlI NKsY7ww9jBHbwOkdI/4b/iql8ym3jY1M7yTHrm52VrtQ/qHdTYDTx67MkUlD9GTB2Q/u V3dGNsv1DVrKR9f6+H2LLz2wf//rPZscJWi9empw9/SyAxq+qNvuaYHm64PuiFwQgbX8 4qxQ== 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=r4L8zLEmMO4gVgjC79EcUNMmrd+fUR2UoiFuFPERS2A=; b=0lLC8kchaF8mKfNj8ls0nfx9LKf8FY0Ni/DJ9wAdSeDl1aGCIF4QvJr1j4VMSFOTPD v+fD9fpoXRyJVwxW8oT+asKUGRW4dqSJrwKU3W3RAnMFTrBjzbTlMO1tLO8QzTy916YV luRQs2or0qNGWFt9SS4roUSEiLAwsk8jgBE6r9rsB4eduWRlU3k/hX7+kMTd2xSgWXqx tAlDiE3rG1C31YmK6I9Pbal93NU01D8Kug1sxFiy9+LXCQULcshvkdxXBFvtSdpdwdA/ kK969jSVuwfBLP7Hf6g6vtS61adimze4qxfsIH4SGwmyzIoctrRKryaWtPX31cxopEjL X4TA== X-Gm-Message-State: AO0yUKVTJ+pYnLsVbpE5WEp4isiRp/kkqLJOjfiHDAaMTmSrBazplbYY DzVkfU3cDGVBjvpOAjLjwdc= X-Google-Smtp-Source: AK7set8O6MMrvpAAUgJamG8GfTPm1b8GmIF2O7yUELLOAE5nOJ+0OfTS9Pti+152P9YQ5Agwr7+kuA== X-Received: by 2002:a05:6a20:918c:b0:cd:9654:d97d with SMTP id v12-20020a056a20918c00b000cd9654d97dmr1167651pzd.57.1677511258531; Mon, 27 Feb 2023 07:20:58 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.20.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:20:58 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 04/18] bpf: arraymap memory usage Date: Mon, 27 Feb 2023 15:20:18 +0000 Message-Id: <20230227152032.12359-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Introduce array_map_mem_usage() to calculate arraymap memory usage. In this helper, some small memory allocations are ignored, like the allocation of struct bpf_array_aux in prog_array. The inner_map_meta in array_of_map is also ignored. The result as follows, - before 11: array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524288B 12: percpu_array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 8912896B 13: perf_event_array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524288B 14: prog_array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524288B 15: cgroup_array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524288B - after 11: array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524608B 12: percpu_array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 17301824B 13: perf_event_array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524608B 14: prog_array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524608B 15: cgroup_array name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524608B Signed-off-by: Yafang Shao --- kernel/bpf/arraymap.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 4847069..1588c79 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -721,6 +721,28 @@ static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_ return num_elems; } +static u64 array_map_mem_usage(const struct bpf_map *map) +{ + struct bpf_array *array = container_of(map, struct bpf_array, map); + bool percpu = map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY; + u32 elem_size = array->elem_size; + u64 entries = map->max_entries; + u64 usage = sizeof(*array); + + if (percpu) { + usage += entries * sizeof(void *); + usage += entries * elem_size * num_possible_cpus(); + } else { + if (map->map_flags & BPF_F_MMAPABLE) { + usage = PAGE_ALIGN(usage); + usage += PAGE_ALIGN(entries * elem_size); + } else { + usage += entries * elem_size; + } + } + return usage; +} + BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array) const struct bpf_map_ops array_map_ops = { .map_meta_equal = array_map_meta_equal, @@ -742,6 +764,7 @@ static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_ .map_update_batch = generic_map_update_batch, .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_array_elem, + .map_mem_usage = array_map_mem_usage, .map_btf_id = &array_map_btf_ids[0], .iter_seq_info = &iter_seq_info, }; @@ -762,6 +785,7 @@ static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_ .map_update_batch = generic_map_update_batch, .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_array_elem, + .map_mem_usage = array_map_mem_usage, .map_btf_id = &array_map_btf_ids[0], .iter_seq_info = &iter_seq_info, }; @@ -1156,6 +1180,7 @@ static void prog_array_map_free(struct bpf_map *map) .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem, .map_release_uref = prog_array_map_clear, .map_seq_show_elem = prog_array_map_seq_show_elem, + .map_mem_usage = array_map_mem_usage, .map_btf_id = &array_map_btf_ids[0], }; @@ -1257,6 +1282,7 @@ static void perf_event_fd_array_map_free(struct bpf_map *map) .map_fd_put_ptr = perf_event_fd_array_put_ptr, .map_release = perf_event_fd_array_release, .map_check_btf = map_check_no_btf, + .map_mem_usage = array_map_mem_usage, .map_btf_id = &array_map_btf_ids[0], }; @@ -1291,6 +1317,7 @@ static void cgroup_fd_array_free(struct bpf_map *map) .map_fd_get_ptr = cgroup_fd_array_get_ptr, .map_fd_put_ptr = cgroup_fd_array_put_ptr, .map_check_btf = map_check_no_btf, + .map_mem_usage = array_map_mem_usage, .map_btf_id = &array_map_btf_ids[0], }; #endif @@ -1379,5 +1406,6 @@ static int array_of_map_gen_lookup(struct bpf_map *map, .map_lookup_batch = generic_map_lookup_batch, .map_update_batch = generic_map_update_batch, .map_check_btf = map_check_no_btf, + .map_mem_usage = array_map_mem_usage, .map_btf_id = &array_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153788 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6195FC64ED8 for ; Mon, 27 Feb 2023 15:21:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229562AbjB0PVM (ORCPT ); Mon, 27 Feb 2023 10:21:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbjB0PVK (ORCPT ); Mon, 27 Feb 2023 10:21:10 -0500 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87DC121A11 for ; Mon, 27 Feb 2023 07:21:02 -0800 (PST) Received: by mail-pg1-x52a.google.com with SMTP id s17so3767093pgv.4 for ; Mon, 27 Feb 2023 07:21:02 -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=CQHwjTJNmWcaYoQZxEEd3KAAGtFjO42sdOiYOa2Xw5Y=; b=EGHxG5AQPREWj5Ue4Hll9H2ysnzprW5t5FeOti8EnP/qRJDRYCuda2SL4ifL1BLGkJ nHnTtwPTqDu7rKZZpb169rmeMM5z7NeI2ZQ1rjOznzKxAoLIXoAiPBc6p18iFhz/lZ9q JbqYPWkvYiViKgza4/qvriMppj5fJsFz0eoskgo1BziXBhPiHVjAthV4aep06exiLh7o H5L7f8UXUpo/tmVETDt1OSNbk7ucO2Wk6gAhnGLb47GeRjMozFZViMePJ2nvE4P+O27i 2jn+ks37q2xi/5elXMkbfa+AGRXE3xZZJQ6C46ZRUaH6RHni/BIROhff/vU9ww/hiGdi 1ZIA== 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=CQHwjTJNmWcaYoQZxEEd3KAAGtFjO42sdOiYOa2Xw5Y=; b=uL0ZbaiPIbT0IlXoCjreMc5FNaR1EiT6pVzZhN+lBU74FP2wlsn1Kc2WUCmXQcVvFz R/BSR2lSEHlinAvzKdiWtjDnAYp2NNJODJ5pNI+IPopSQT+Pg/88iE/G32BXeFVLWcn/ vCMgUYUYtk5uoAy6W7Xn81RGqFL+vR38fU0nw+R3nw9Muczssa0PWs5+W9x6CqOLCUWi STfPs9gNduGTy++Befc4g7SbrfySjIIof5JCoqEGezRMrM5A5q0sIo8viQgF3eHa+XtK SmcLXFj1xbAxIK+whbtcYdKdHCsq5tZyOxgvMQn0ezKc+/yce2D6LMqD6Lo1fxEvtNCP 153w== X-Gm-Message-State: AO0yUKXmVEj/4RlKfYhcIMT4yoOSRLePVtOm4S3dnPcsvJeuU4VXMmSm JJjthqtVeDQ45Wi/bYyexuo= X-Google-Smtp-Source: AK7set/nl3Fv4n8yu5m269/5IWtamEOWuEx4H8Dn9A+iULNnFUwxFUMUL/oMJHhnB7HPcmtAcp0xKA== X-Received: by 2002:aa7:9466:0:b0:5aa:4df7:7ef6 with SMTP id t6-20020aa79466000000b005aa4df77ef6mr22781213pfq.7.1677511261916; Mon, 27 Feb 2023 07:21:01 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.20.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:01 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 05/18] bpf: stackmap memory usage Date: Mon, 27 Feb 2023 15:20:19 +0000 Message-Id: <20230227152032.12359-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to get stackmap memory usage. Some small memory allocations are ignored as their memory size is quite small compared to the totol usage. The result as follows, - before 16: stack_trace name count_map flags 0x0 key 4B value 8B max_entries 65536 memlock 1048576B - after 16: stack_trace name count_map flags 0x0 key 4B value 8B max_entries 65536 memlock 2097472B Signed-off-by: Yafang Shao --- kernel/bpf/stackmap.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index aecea74..0f1d8dc 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -654,6 +654,19 @@ static void stack_map_free(struct bpf_map *map) put_callchain_buffers(); } +static u64 stack_map_mem_usage(const struct bpf_map *map) +{ + struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map); + u64 value_size = map->value_size; + u64 n_buckets = smap->n_buckets; + u64 enties = map->max_entries; + u64 usage = sizeof(*smap); + + usage += n_buckets * sizeof(struct stack_map_bucket *); + usage += enties * (sizeof(struct stack_map_bucket) + value_size); + return usage; +} + BTF_ID_LIST_SINGLE(stack_trace_map_btf_ids, struct, bpf_stack_map) const struct bpf_map_ops stack_trace_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -664,5 +677,6 @@ static void stack_map_free(struct bpf_map *map) .map_update_elem = stack_map_update_elem, .map_delete_elem = stack_map_delete_elem, .map_check_btf = map_check_no_btf, + .map_mem_usage = stack_map_mem_usage, .map_btf_id = &stack_trace_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153789 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43BD8C64ED6 for ; Mon, 27 Feb 2023 15:21:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229996AbjB0PVN (ORCPT ); Mon, 27 Feb 2023 10:21:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230132AbjB0PVL (ORCPT ); Mon, 27 Feb 2023 10:21:11 -0500 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD7DA22A1F for ; Mon, 27 Feb 2023 07:21:05 -0800 (PST) Received: by mail-pg1-x52e.google.com with SMTP id p6so3790829pga.0 for ; Mon, 27 Feb 2023 07:21:05 -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=uQuLzFY+KbTi70OVyTdLGmPLsVqTEok0qqP2ZiCsjVY=; b=kPV1P61Kd0VE4A/sLs+rCgugutJbre1p41Jp9AewL8nwfkSQmj85/GyTCWADug9ewR p/5HqctY4ymjNfXE05aBzhq1gjUD+CE08kHyUXNas9jYtVnM1KnjuG6Apdf1HAqu0YK9 0vO95QdYSksS+4Icu5HEmCeXql2TQ8rMrv+xN0BC9mg3ktlTMEcdkyUUa5UBBQNCdGev 0pspuVc5MaXtjryhse+sRWUGv1ehoEJqp5M406FF7OIRNLSEvzEPuekwG49o1WTpXucE YOPpivQFUItrDGyudyfXL2BzRTGqqu5HyEzH2r00J00vMy8ozVlM78QvDoegnV22U/CN ettA== 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=uQuLzFY+KbTi70OVyTdLGmPLsVqTEok0qqP2ZiCsjVY=; b=lew/JPukHKYSGyBjz6g1W/aWjfO0komwzXxBpqhg72FqWp3uR75lbuJu19wzkvu+g0 nWWbJ/gpy6juRic1e2Pg7c+2jrSBjujWX9s3EZy6lavrG73ZCBHw36U5lY1ynmAIPgfS t7mI04K8Hhfvk8kG07/itVaiMddkPLM+s87yha+OCPcuR6F/w5Q1cgEADyEJLkbu5aBz Ac0y/95kmYOQS6DM7cfkn6Y5zgLlrtbvKIA1ravNNXUjQh/Q+cioFmzhyA7D0lgMRG01 tfeFebhaNzm1jLvJIO601OytD0fwUjE6JphdoCRyhufk0VUZ5DMimSvoqCJW7ATsfFew gyYQ== X-Gm-Message-State: AO0yUKWyHYpgSFAG+JireHCtNv2CuGD4huJ9+1pHgsa5SZd2tBXVhbt2 9wDHrL7wmUlLcf4MV2mo0xE= X-Google-Smtp-Source: AK7set90cqPKHF+c9azvhxFYskYHfqBJBWIgjcvjhGerZlvJH5CMd+VY9e8lDOaCIXonspx1OL1oRQ== X-Received: by 2002:aa7:9798:0:b0:593:da8:6f34 with SMTP id o24-20020aa79798000000b005930da86f34mr22639253pfp.5.1677511265390; Mon, 27 Feb 2023 07:21:05 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:04 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 06/18] bpf: reuseport_array memory usage Date: Mon, 27 Feb 2023 15:20:20 +0000 Message-Id: <20230227152032.12359-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to calculate reuseport_array memory usage. The result as follows, - before 14: reuseport_sockarray name count_map flags 0x0 key 4B value 8B max_entries 65536 memlock 1048576B - after 14: reuseport_sockarray name count_map flags 0x0 key 4B value 8B max_entries 65536 memlock 524544B Signed-off-by: Yafang Shao --- kernel/bpf/reuseport_array.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/bpf/reuseport_array.c b/kernel/bpf/reuseport_array.c index 82c6161..71cb72f 100644 --- a/kernel/bpf/reuseport_array.c +++ b/kernel/bpf/reuseport_array.c @@ -335,6 +335,13 @@ static int reuseport_array_get_next_key(struct bpf_map *map, void *key, return 0; } +static u64 reuseport_array_mem_usage(const struct bpf_map *map) +{ + struct reuseport_array *array; + + return struct_size(array, ptrs, map->max_entries); +} + BTF_ID_LIST_SINGLE(reuseport_array_map_btf_ids, struct, reuseport_array) const struct bpf_map_ops reuseport_array_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -344,5 +351,6 @@ static int reuseport_array_get_next_key(struct bpf_map *map, void *key, .map_lookup_elem = reuseport_array_lookup_elem, .map_get_next_key = reuseport_array_get_next_key, .map_delete_elem = reuseport_array_delete_elem, + .map_mem_usage = reuseport_array_mem_usage, .map_btf_id = &reuseport_array_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153790 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CA01C7EE23 for ; Mon, 27 Feb 2023 15:21:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229950AbjB0PVP (ORCPT ); Mon, 27 Feb 2023 10:21:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230127AbjB0PVN (ORCPT ); Mon, 27 Feb 2023 10:21:13 -0500 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44ABD22A2D for ; Mon, 27 Feb 2023 07:21:09 -0800 (PST) Received: by mail-pg1-x52e.google.com with SMTP id 130so3767285pgg.3 for ; Mon, 27 Feb 2023 07:21:09 -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=pEfQ8gbb6YM9AUGhgYD61SWx4aCRStdaB4FDTThUGWI=; b=GmOHWNfUOrQ2t1qOEgUxHOS/Y+sD2zO9zkiEtOKPLlXUd4t/fUfkQgCc7JLziQs6/U knmK7ZJXZZBSY0JG7v9ZmP/uJX/ZqRHMOc3dwaXO+n1FwTE00ND7fUayERYiADKCHOiQ JmtMK932vw2fr5uJvd0qKyvzdSyBSYV39JDabnJ1f5NuZPPOFsphjXkiqRJZ+KcXoWH0 9QXcsfZicgPLeg0ScVdvLTImGIxtvgJ//+D9awCOrvf4S2Q/WjXm06rayLYOsqYkOicC Ospl0wvV21MLRU0giz0oF4Tk5jvGpa7+MeKZ/OP58pn/SIXU+AvOnT73w84Ga5eAHg6m iVFw== 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=pEfQ8gbb6YM9AUGhgYD61SWx4aCRStdaB4FDTThUGWI=; b=eRx5sm0Eg5/gfRiyJLiowD4pHpX7a4fsH8f5QFJVtiStiZNm6dSMkRoqXecVt/RKyF fCyerMC7CXYGkUKBOsE65cogZUu5qJqlJs3R5GuzYrsLrBVwoqhgb7NEp/EPwGsBRMnZ nBmXDO6MbFWlZQExosIxuCgzd36CLkrnJMT/Fs0O17fWc7u9cYnI7yd0oEtWb3uTryWM EcksU8Adqbsb5TL126qcjDGTi2m5xJysl5VaGCYch3O+ZUdTQ+CemMvLd/j6017hD9Fa 4DqpmHCxMAdXTpg8lagao7v8qWVae3+8UIc9mwF7iUog6UnRj14lLzduidzBc9Mbw4gD xk/w== X-Gm-Message-State: AO0yUKXU8FRb4TFmSR2Vq6VvX7bBho3Nby2lKKzjDxE3qkY0fdC2n5ei dlo9jrkcwyvpsxALeuXeo8WO2jvuwjcUBOKCvIA= X-Google-Smtp-Source: AK7set+cd8HA7AkH+09OCnifAFyYuuoM9iTcyNGmr3u/JXN2szP6b11Op6IJWrufhMAYiuv1Bf4Hpw== X-Received: by 2002:a05:6a00:17a3:b0:5a8:4ae7:25d5 with SMTP id s35-20020a056a0017a300b005a84ae725d5mr11239192pfg.8.1677511268822; Mon, 27 Feb 2023 07:21:08 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:08 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 07/18] bpf: ringbuf memory usage Date: Mon, 27 Feb 2023 15:20:21 +0000 Message-Id: <20230227152032.12359-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper ringbuf_map_mem_usage() is introduced to calculate ringbuf memory usage. The result as follows, - before 15: ringbuf name count_map flags 0x0 key 0B value 0B max_entries 65536 memlock 0B - after 15: ringbuf name count_map flags 0x0 key 0B value 0B max_entries 65536 memlock 78424B Signed-off-by: Yafang Shao Acked-by: Andrii Nakryiko --- kernel/bpf/ringbuf.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index 80f4b4d..2bbf6e2 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -336,6 +336,23 @@ static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp, return 0; } +static u64 ringbuf_map_mem_usage(const struct bpf_map *map) +{ + struct bpf_ringbuf_map *rb_map; + struct bpf_ringbuf *rb; + int nr_data_pages; + int nr_meta_pages; + u64 usage = sizeof(struct bpf_ringbuf_map); + + rb_map = container_of(map, struct bpf_ringbuf_map, map); + rb = rb_map->rb; + usage += (u64)rb->nr_pages << PAGE_SHIFT; + nr_meta_pages = RINGBUF_PGOFF + RINGBUF_POS_PAGES; + nr_data_pages = map->max_entries >> PAGE_SHIFT; + usage += (nr_meta_pages + 2 * nr_data_pages) * sizeof(struct page *); + return usage; +} + BTF_ID_LIST_SINGLE(ringbuf_map_btf_ids, struct, bpf_ringbuf_map) const struct bpf_map_ops ringbuf_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -347,6 +364,7 @@ static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp, .map_update_elem = ringbuf_map_update_elem, .map_delete_elem = ringbuf_map_delete_elem, .map_get_next_key = ringbuf_map_get_next_key, + .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &ringbuf_map_btf_ids[0], }; @@ -361,6 +379,7 @@ static __poll_t ringbuf_map_poll_user(struct bpf_map *map, struct file *filp, .map_update_elem = ringbuf_map_update_elem, .map_delete_elem = ringbuf_map_delete_elem, .map_get_next_key = ringbuf_map_get_next_key, + .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &user_ringbuf_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153791 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8810C64ED6 for ; Mon, 27 Feb 2023 15:21:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230206AbjB0PVV (ORCPT ); Mon, 27 Feb 2023 10:21:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229972AbjB0PVR (ORCPT ); Mon, 27 Feb 2023 10:21:17 -0500 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC4EF2278B for ; Mon, 27 Feb 2023 07:21:12 -0800 (PST) Received: by mail-pf1-x42c.google.com with SMTP id a7so3722884pfx.10 for ; Mon, 27 Feb 2023 07:21:12 -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=BoFsRKqLdKP02I7g694Qbh39KZF7kQZ2F0Pz2UqBWn0=; b=SzHZN6J2pneLONd0RoNUm+5p31mxowMaJKrZLOvnzgIHpZFxntY48EP4LBvuE6PusF p2AvIL32Qg6wgSvE3xbWlxvIW25q3Dre7MCUETTzGlS5cFRxymusIJFtbOpyw5AhhoQi bfQY2/dtTKjZ2BLO4MQbDXyAjlPFOGYM422loXfqmImFLihtcDRcW6xTH/SQnD2Ce74T x2etirvWQYBT7IngqsRtDzLwsT+irCqN2CIAOaaj+PrxtiG2xEE7ShotAgzP6R5EQ9Gt bk7iQhyftqwOqKkhrfd7sKMJbU7fMAWe6nDKtuvhEZ2+GgRhVBj/ahg6HCVSjMO4y9DR 0qow== 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=BoFsRKqLdKP02I7g694Qbh39KZF7kQZ2F0Pz2UqBWn0=; b=HzMPJqXL+pxXfvtcjjU4peX1up5V5BGNw7P8zMREYgA7PRqRAWX4seyc0mSXINmZCc fBq8zqKQql2CQV6sBDTv0gib/jVtuirRaBLZrEacYWVk6IKs1KcAP1CDYM4SXGUoOZNV 0aIS1kv10fZLGgUcx3opu6bQiKUl711Xg5HE1fJInXVq3g+70EMibYmBwRYkCtIVAqNX zz+hjFBgUtf33gV72Th2i/BvWIut1IzZ+GkTNo+aBsIny26vA/rjNRFqxbDp5QiK5PYY 28OXcGBBdjHTr7wkjbC2rZozoShs4X6ccAqj0pzOLw9PLQs27kr7acb1Xzfc9xglNV2P 0lXw== X-Gm-Message-State: AO0yUKVBVgwHy3URGvjNsFnJBfAQICLDcmFm+AImjJfgoJHXV6w8Nl5M ubwL1AHkBwSiU+7ppoUoAK8= X-Google-Smtp-Source: AK7set/aO0xiePMsxOjDpq6FFYshpINkQPItrjluzOvhm9pOAX8NXQOLAZYRLfWR5sFOGnLltiJWRA== X-Received: by 2002:aa7:978e:0:b0:5de:7ef1:d03a with SMTP id o14-20020aa7978e000000b005de7ef1d03amr13440938pfp.19.1677511272361; Mon, 27 Feb 2023 07:21:12 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21: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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 08/18] bpf: bloom_filter memory usage Date: Mon, 27 Feb 2023 15:20:22 +0000 Message-Id: <20230227152032.12359-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Introduce a new helper to calculate the bloom_filter memory usage. The result as follows, - before 16: bloom_filter flags 0x0 key 0B value 8B max_entries 65536 memlock 524288B - after 16: bloom_filter flags 0x0 key 0B value 8B max_entries 65536 memlock 65856B Signed-off-by: Yafang Shao --- kernel/bpf/bloom_filter.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/kernel/bpf/bloom_filter.c b/kernel/bpf/bloom_filter.c index 48ee750..6350c5d 100644 --- a/kernel/bpf/bloom_filter.c +++ b/kernel/bpf/bloom_filter.c @@ -193,6 +193,17 @@ static int bloom_map_check_btf(const struct bpf_map *map, return btf_type_is_void(key_type) ? 0 : -EINVAL; } +static u64 bloom_map_mem_usage(const struct bpf_map *map) +{ + struct bpf_bloom_filter *bloom; + u64 bitset_bytes; + + bloom = container_of(map, struct bpf_bloom_filter, map); + bitset_bytes = BITS_TO_BYTES((u64)bloom->bitset_mask + 1); + bitset_bytes = roundup(bitset_bytes, sizeof(unsigned long)); + return sizeof(*bloom) + bitset_bytes; +} + BTF_ID_LIST_SINGLE(bpf_bloom_map_btf_ids, struct, bpf_bloom_filter) const struct bpf_map_ops bloom_filter_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -206,5 +217,6 @@ static int bloom_map_check_btf(const struct bpf_map *map, .map_update_elem = bloom_map_update_elem, .map_delete_elem = bloom_map_delete_elem, .map_check_btf = bloom_map_check_btf, + .map_mem_usage = bloom_map_mem_usage, .map_btf_id = &bpf_bloom_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153792 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA229C64ED8 for ; Mon, 27 Feb 2023 15:21:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230226AbjB0PV3 (ORCPT ); Mon, 27 Feb 2023 10:21:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230191AbjB0PVU (ORCPT ); Mon, 27 Feb 2023 10:21:20 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 355C722A2A for ; Mon, 27 Feb 2023 07:21:16 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id 6-20020a17090a190600b00237c5b6ecd7so5942097pjg.4 for ; Mon, 27 Feb 2023 07:21:16 -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=eiBhjvmvPZBvwRpF2x1Rob9RQFfAqLPRi1VOzXV3z/U=; b=V1VhTdVq/yi8VaxqYBcAmaGIldLBlYmDNHTbQfJ51ZjUuYni7H5iB5VEDkU3qAOAv5 72qxS+yqTeO5aYyuIG5FFvakkBDDkMPTDE+0QKiJfkWSK//czstDqjcV3sA0PZA74jsD V1IpDyayPwLmf0dnmITQ3YsIn6y+VlMVwDfsx/qL2xNXLfBmx2sFnfxIis+1Zv7GxCnK cs4aPFJhcRYEL2ISO39JSTKQDC+b2cw5iJ0cwdMA2Ge8yv1cYmpu0DPs+ZMzgXUpX6vg iv9O1jRvGSQZJGA3GX2+VTWy+mbwEOTdD6Q2KONfchxh0EIexDJzzpano2D3WkM4XfJr YRYQ== 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=eiBhjvmvPZBvwRpF2x1Rob9RQFfAqLPRi1VOzXV3z/U=; b=49JRT/jE8pIxyXo0S8INlKYERlyM3LkQ/ay05UFG6mtrI47yXKaB5h4OSunTW6Lmly iu5agt9n1OMSX+4+RXO5TZmLVNGUES5/5C3rpY087AAJcMse5jiThnE4yz0aRBZ17V4M Rr2cFPz7oFO6YtoByX4r2vDsyW169EAgEx9qhWPEuyFUU3ZsZZ5M6nB8LCPAX5i7otXq 04hsjzGJZxGFahWY0jzL2etd4aNGUS4dN4jsvJP/a2Ibuzv8Q1NGlllz8zNYY44UFwWS NeLLK4zlHb58tjiAarCjNFPpv5WOvbKrtBNOd6d0frEbPS0POM9M5Lr6HJVYsURVTuwx svlA== X-Gm-Message-State: AO0yUKWzRyyA0bTSMw8l/8HkwREnsYUfamnggfas2et6MaVkC+vjc574 xvn3ENyhd1uuiH0STp1Tuew= X-Google-Smtp-Source: AK7set+DXiEwTIrO0ppF6lO5LzdmT7W94D4OTf/iKjo55kbMeBIQ+HzZCrsbZSJIibynCFgBaAqntw== X-Received: by 2002:a05:6a20:b562:b0:cd:9673:9c07 with SMTP id ev34-20020a056a20b56200b000cd96739c07mr1182850pzb.7.1677511275752; Mon, 27 Feb 2023 07:21:15 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:15 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 09/18] bpf: cpumap memory usage Date: Mon, 27 Feb 2023 15:20:23 +0000 Message-Id: <20230227152032.12359-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to calculate cpumap memory usage. The size of cpu_entries can be dynamically changed when we update or delete a cpumap element, but this patch doesn't include the memory size of cpu_entry yet. We can dynamically calculate the memory usage when we alloc or free a cpu_entry, but it will take extra runtime overhead, so let just put it aside currently. Note that the size of different cpu_entry may be different as well. The result as follows, - before 48: cpumap name count_map flags 0x4 key 4B value 4B max_entries 64 memlock 4096B - after 48: cpumap name count_map flags 0x4 key 4B value 4B max_entries 64 memlock 832B Signed-off-by: Yafang Shao --- kernel/bpf/cpumap.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kernel/bpf/cpumap.c b/kernel/bpf/cpumap.c index d2110c1..871809e 100644 --- a/kernel/bpf/cpumap.c +++ b/kernel/bpf/cpumap.c @@ -673,6 +673,15 @@ static int cpu_map_redirect(struct bpf_map *map, u64 index, u64 flags) __cpu_map_lookup_elem); } +static u64 cpu_map_mem_usage(const struct bpf_map *map) +{ + u64 usage = sizeof(struct bpf_cpu_map); + + /* Currently the dynamically allocated elements are not counted */ + usage += (u64)map->max_entries * sizeof(struct bpf_cpu_map_entry *); + return usage; +} + BTF_ID_LIST_SINGLE(cpu_map_btf_ids, struct, bpf_cpu_map) const struct bpf_map_ops cpu_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -683,6 +692,7 @@ static int cpu_map_redirect(struct bpf_map *map, u64 index, u64 flags) .map_lookup_elem = cpu_map_lookup_elem, .map_get_next_key = cpu_map_get_next_key, .map_check_btf = map_check_no_btf, + .map_mem_usage = cpu_map_mem_usage, .map_btf_id = &cpu_map_btf_ids[0], .map_redirect = cpu_map_redirect, }; From patchwork Mon Feb 27 15:20:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153793 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB871C7EE23 for ; Mon, 27 Feb 2023 15:21:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230220AbjB0PVb (ORCPT ); Mon, 27 Feb 2023 10:21:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230217AbjB0PVV (ORCPT ); Mon, 27 Feb 2023 10:21:21 -0500 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B20EC22A37 for ; Mon, 27 Feb 2023 07:21:19 -0800 (PST) Received: by mail-pf1-x432.google.com with SMTP id ce7so3730853pfb.9 for ; Mon, 27 Feb 2023 07:21:19 -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=WmDHP81h9Zc5yHGdhmIdZKJM07O38CQheGMoXufBbrk=; b=cQ1CwSSmjsCxnR7BHnD8jTKQazh8b9zNxTimZocLyOXPWAybthTE9XcnIeDC2Zcu7x qmTKPvuFUUrZhx2XXpc2Q5E+TuBqV9RiG4t0ufMZ3t8MVOrMt/rLIvo0DtsOKTZWkVTT ipyXQiMrYfPFyu/HqJnyTqswHifUtY0Zv5jKbo+5TEwSEO/FjfvJrRr6oL8eRzjKczrE 5xcoluTMLTO4uwOiniuvamjxKcvKM71sjw4CcvR4MXf5dIMtOuiytgpR5Yi0+R3TDwhK 7GlU0hLpq0dORSmaoDG4wy2reIu1XAgLB811Sm7oM3cobkxOmW8nfDLO6i1hTEq5RGMI 3yBw== 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=WmDHP81h9Zc5yHGdhmIdZKJM07O38CQheGMoXufBbrk=; b=guHUe/UbhptUrzFZKN0FEifdb/wy+N2UKsXZTpGeBfDvdAcYv42A+uNyb9MR6atw+i DB8pCHqZzJxjalGv7TQwMuDbOcGclS6MJcZmF8FTgdVvUz16MoAJx6LxYUEIRu80BBGA vKpylm0fjnoh3xFUZ/U3fRAszR39yMZU/ddwW2bwEFPvcR+u9VveuRYV/uTAGzp+fpwB u2A3z29vXoJJ9jkQzqvAXbpA6w/iZF7L+J2z3KCbOrJ+4R/2+Sp/cm1piqhsx1M/mgzz tclWgQRD6xsOQnTgH3PSQauOdp92guLxqyL1h+xmPsI8KTbM7oPuQZU6PZn16/mhuRxB Z1RQ== X-Gm-Message-State: AO0yUKVN/A5n/B2wHKdo30t3mSS8PhjHpmjuoaFOo+Egs8OadkAQGnnz dtKPZvgBTOkbGNlDEtKml8Y= X-Google-Smtp-Source: AK7set8AsZsiIhTqCbkpPbatVLv/rZv54prkKISTYQYfi4VcdF9dKKhJaxka1kRc1RDTYTV4JLiEzA== X-Received: by 2002:a05:6a00:1c98:b0:594:1f1c:3d3a with SMTP id y24-20020a056a001c9800b005941f1c3d3amr6555231pfw.15.1677511279197; Mon, 27 Feb 2023 07:21:19 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:18 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 10/18] bpf: devmap memory usage Date: Mon, 27 Feb 2023 15:20:24 +0000 Message-Id: <20230227152032.12359-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to calculate the memory usage of devmap and devmap_hash. The number of dynamically allocated elements are recored for devmap_hash already, but not for devmap. To track the memory size of dynamically allocated elements, this patch also count the numbers for devmap. The result as follows, - before 40: devmap name count_map flags 0x80 key 4B value 4B max_entries 65536 memlock 524288B 41: devmap_hash name count_map flags 0x80 key 4B value 4B max_entries 65536 memlock 524288B - after 40: devmap name count_map flags 0x80 <<<< no elements key 4B value 4B max_entries 65536 memlock 524608B 41: devmap_hash name count_map flags 0x80 <<<< no elements key 4B value 4B max_entries 65536 memlock 524608B Note that the number of buckets is same with max_entries for devmap_hash in this case. Signed-off-by: Yafang Shao --- kernel/bpf/devmap.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c index 2675fef..19b036a 100644 --- a/kernel/bpf/devmap.c +++ b/kernel/bpf/devmap.c @@ -819,8 +819,10 @@ static int dev_map_delete_elem(struct bpf_map *map, void *key) return -EINVAL; old_dev = unrcu_pointer(xchg(&dtab->netdev_map[k], NULL)); - if (old_dev) + if (old_dev) { call_rcu(&old_dev->rcu, __dev_map_entry_free); + atomic_dec((atomic_t *)&dtab->items); + } return 0; } @@ -931,6 +933,8 @@ static int __dev_map_update_elem(struct net *net, struct bpf_map *map, old_dev = unrcu_pointer(xchg(&dtab->netdev_map[i], RCU_INITIALIZER(dev))); if (old_dev) call_rcu(&old_dev->rcu, __dev_map_entry_free); + else + atomic_inc((atomic_t *)&dtab->items); return 0; } @@ -1016,6 +1020,20 @@ static int dev_hash_map_redirect(struct bpf_map *map, u64 ifindex, u64 flags) __dev_map_hash_lookup_elem); } +static u64 dev_map_mem_usage(const struct bpf_map *map) +{ + struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); + u64 usage = sizeof(struct bpf_dtab); + + if (map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) + usage += (u64)dtab->n_buckets * sizeof(struct hlist_head); + else + usage += (u64)map->max_entries * sizeof(struct bpf_dtab_netdev *); + usage += atomic_read((atomic_t *)&dtab->items) * + (u64)sizeof(struct bpf_dtab_netdev); + return usage; +} + BTF_ID_LIST_SINGLE(dev_map_btf_ids, struct, bpf_dtab) const struct bpf_map_ops dev_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -1026,6 +1044,7 @@ static int dev_hash_map_redirect(struct bpf_map *map, u64 ifindex, u64 flags) .map_update_elem = dev_map_update_elem, .map_delete_elem = dev_map_delete_elem, .map_check_btf = map_check_no_btf, + .map_mem_usage = dev_map_mem_usage, .map_btf_id = &dev_map_btf_ids[0], .map_redirect = dev_map_redirect, }; @@ -1039,6 +1058,7 @@ static int dev_hash_map_redirect(struct bpf_map *map, u64 ifindex, u64 flags) .map_update_elem = dev_map_hash_update_elem, .map_delete_elem = dev_map_hash_delete_elem, .map_check_btf = map_check_no_btf, + .map_mem_usage = dev_map_mem_usage, .map_btf_id = &dev_map_btf_ids[0], .map_redirect = dev_hash_map_redirect, }; @@ -1109,9 +1129,11 @@ static int dev_map_notification(struct notifier_block *notifier, if (!dev || netdev != dev->dev) continue; odev = unrcu_pointer(cmpxchg(&dtab->netdev_map[i], RCU_INITIALIZER(dev), NULL)); - if (dev == odev) + if (dev == odev) { call_rcu(&dev->rcu, __dev_map_entry_free); + atomic_dec((atomic_t *)&dtab->items); + } } } rcu_read_unlock(); From patchwork Mon Feb 27 15:20:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153794 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C755BC64ED8 for ; Mon, 27 Feb 2023 15:21:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230157AbjB0PVd (ORCPT ); Mon, 27 Feb 2023 10:21:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230228AbjB0PVa (ORCPT ); Mon, 27 Feb 2023 10:21:30 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3486C22A30 for ; Mon, 27 Feb 2023 07:21:23 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id h17-20020a17090aea9100b0023739b10792so6506269pjz.1 for ; Mon, 27 Feb 2023 07:21:23 -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=eSsP2cTbcAO8kYH41y4wWbsUNHvs4mxp7DEwTns4GbQ=; b=Yr4SFFtPkZvwVK75AV3zwTrAqWt5rE0nlqVxsyYcahWuZX7ri2/Ayv26fnF4X1EWfn nNe6tSr83LYOpEpxi3jtsjOebAkbek4aVJwv0mluWzOc9IC4Hw8nwr7SL2x2LmKZB9Gz YXHIhVHgqu9timdfGivNg6X4TtzTGB5ju6r0dmJqBVOGVgs2XxCxXjiijshdm22JD3Gy MT1o1jR7fFYURo0cADrHRN4d4a62s0PVU9av361oEvAVEobHhfyOCGC1WI6mZXN6sfP3 fMwRipMtF7RjiBGU9YUnh9zR7oix9KGrGfy9wCllssrxzrkEF2Rx4iDrZYb6pGLANLp8 2HEQ== 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=eSsP2cTbcAO8kYH41y4wWbsUNHvs4mxp7DEwTns4GbQ=; b=Js1ZN7+0lyRXuRBb+99oXKks1GAO1g2Cx6NbSZ7LTPhMnAIoBlsfTYH0EoF+rrRvz0 yRXiaJ5KHKiLx2Y5M0KW9QW/HKxQCNsrUcpKCH8dNRyKcOp8OMfo10KOdPmPgBY29/v/ g/hCqGi8HVooBaTCtUvo7ubaXYCcrW24Rw3WQJ9IAZd6TAAt4AwC/dWcV62Np4U0DCI6 3N8/eIGgKV31iOcz2VRt9z2QYAQX4I9wskMyw1CLTYHYRCZtBwWZYw61/P480u/omRBn moLLRKLm+lMWB4q+noHjiWWZnpDBa+DOmqSTFxL/sOqg7QHF+CQQwSDaL0u78Rra32tg E7fg== X-Gm-Message-State: AO0yUKWgwXF47FThmVOYCthAhFiDF60BPKDU4/kMyGvjzUrccKWDlRZH v1EJ0DvVa7Sm9qvnF51vNi0= X-Google-Smtp-Source: AK7set8fB5grJBEEjo7ePIzmqAWZXISL/o6cbC87msv0hK56/BPFBPvMwH8PBobSNXxdCghNQpe5Xg== X-Received: by 2002:a05:6a21:9999:b0:cc:1184:6d0 with SMTP id ve25-20020a056a21999900b000cc118406d0mr18397390pzb.23.1677511282774; Mon, 27 Feb 2023 07:21:22 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:22 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 11/18] bpf: queue_stack_maps memory usage Date: Mon, 27 Feb 2023 15:20:25 +0000 Message-Id: <20230227152032.12359-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to calculate queue_stack_maps memory usage. The result as follows, - before 20: queue name count_map flags 0x0 key 0B value 4B max_entries 65536 memlock 266240B 21: stack name count_map flags 0x0 key 0B value 4B max_entries 65536 memlock 266240B - after 20: queue name count_map flags 0x0 key 0B value 4B max_entries 65536 memlock 524288B 21: stack name count_map flags 0x0 key 0B value 4B max_entries 65536 memlock 524288B Signed-off-by: Yafang Shao --- kernel/bpf/queue_stack_maps.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kernel/bpf/queue_stack_maps.c b/kernel/bpf/queue_stack_maps.c index 8a5e060..63ecbbc 100644 --- a/kernel/bpf/queue_stack_maps.c +++ b/kernel/bpf/queue_stack_maps.c @@ -246,6 +246,14 @@ static int queue_stack_map_get_next_key(struct bpf_map *map, void *key, return -EINVAL; } +static u64 queue_stack_map_mem_usage(const struct bpf_map *map) +{ + u64 usage = sizeof(struct bpf_queue_stack); + + usage += ((u64)map->max_entries + 1) * map->value_size; + return usage; +} + BTF_ID_LIST_SINGLE(queue_map_btf_ids, struct, bpf_queue_stack) const struct bpf_map_ops queue_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -259,6 +267,7 @@ static int queue_stack_map_get_next_key(struct bpf_map *map, void *key, .map_pop_elem = queue_map_pop_elem, .map_peek_elem = queue_map_peek_elem, .map_get_next_key = queue_stack_map_get_next_key, + .map_mem_usage = queue_stack_map_mem_usage, .map_btf_id = &queue_map_btf_ids[0], }; @@ -274,5 +283,6 @@ static int queue_stack_map_get_next_key(struct bpf_map *map, void *key, .map_pop_elem = stack_map_pop_elem, .map_peek_elem = stack_map_peek_elem, .map_get_next_key = queue_stack_map_get_next_key, + .map_mem_usage = queue_stack_map_mem_usage, .map_btf_id = &queue_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153795 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25D70C64ED6 for ; Mon, 27 Feb 2023 15:21:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229720AbjB0PVg (ORCPT ); Mon, 27 Feb 2023 10:21:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230244AbjB0PVb (ORCPT ); Mon, 27 Feb 2023 10:21:31 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66E5422A17 for ; Mon, 27 Feb 2023 07:21:27 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id p6so6220261plf.0 for ; Mon, 27 Feb 2023 07:21:27 -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=X8bqcj+fQi+LPKTwP58l/gYDYaKLYdssShQrHrw6wI4=; b=dr/V9d8Xp6J4y4aT6dXDbtYUZw60I5ggq3da0gVZG+vYcilYZBGI7/GNpwkhmQ0Y0C hh6Pc+AdZYva4FCoeV9xhsRAiOlh8+Bl7XDp93hnAGKTgHWR57oCXHKWqeadrZ3B2LXV htkSH5SzHvYY9eCQZR4HXuyaO9+qNTEUWU7oY0MtFbVVVRmzhSZC4d/bN5up4BYXf6FF SzELOoibBmXFZINNHjdAyxOlFEB2ocX0Y0QTtO+dWs8gIUlJQN1jOVCjQzGjYW1SKX0a wfi2X5sngNWnzjDKmsmswroHDnu2o/q2/eeQozHtjiy/zI3Q1qb94SWyHvMnaCkE2jyU fq/g== 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=X8bqcj+fQi+LPKTwP58l/gYDYaKLYdssShQrHrw6wI4=; b=qOrHswWKXYHeVHMnhysHoakGVt1QTbF3f8vBd8UwMkMbab8VsKu6IJvl5w13E25oQx r6hzRsZQXDXuaUXSVIVDcYP3F869pQtir/n8uv7pWvEsvqT90agOMOA/2YwdHlyyYRz7 cFBJz4eaZ9H4M7+UuPaDJnyYDtCaylyL7lf6uca82MHpGaTyy0FB/G7sNa1srvXUpxth 4eCUw4OlUGksQD10I3EM/OJWkGyCtnh0AIw40WVq40gVIMGe+bZ/rwzu5PsD1v/ksPmY fxw5y4/6xdUnz9OM4A5Rr9eUVZ3H05x3mX3v8IbABxI8NgZA3AoYelkDPiXky3I2dxRv z6gA== X-Gm-Message-State: AO0yUKXE0zpDHPudljkSqzxdsN6xyKmUDVbQm1Dos66EGE0EYTonfzMm QRXBxnyWTOpaa/hNShkxTV4= X-Google-Smtp-Source: AK7set8thacgtVIpgYitQNwESehPrSgzEk0W9nkBld/GehwQsuzvIF8fXvAVRFU/vd5AxCVG1IO+hw== X-Received: by 2002:a05:6a20:7da2:b0:cc:8266:9951 with SMTP id v34-20020a056a207da200b000cc82669951mr12549815pzj.56.1677511286985; Mon, 27 Feb 2023 07:21:26 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:26 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 12/18] bpf: bpf_struct_ops memory usage Date: Mon, 27 Feb 2023 15:20:26 +0000 Message-Id: <20230227152032.12359-13-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to calculate bpf_struct_ops memory usage. The result as follows, - before 1: struct_ops name count_map flags 0x0 key 4B value 256B max_entries 1 memlock 4096B btf_id 73 - after 1: struct_ops name count_map flags 0x0 key 4B value 256B max_entries 1 memlock 5016B btf_id 73 Signed-off-by: Yafang Shao --- kernel/bpf/bpf_struct_ops.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index ece9870..38903fb 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -641,6 +641,21 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) return map; } +static u64 bpf_struct_ops_map_mem_usage(const struct bpf_map *map) +{ + struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map; + const struct bpf_struct_ops *st_ops = st_map->st_ops; + const struct btf_type *vt = st_ops->value_type; + u64 usage; + + usage = sizeof(*st_map) + + vt->size - sizeof(struct bpf_struct_ops_value); + usage += vt->size; + usage += btf_type_vlen(vt) * sizeof(struct bpf_links *); + usage += PAGE_SIZE; + return usage; +} + BTF_ID_LIST_SINGLE(bpf_struct_ops_map_btf_ids, struct, bpf_struct_ops_map) const struct bpf_map_ops bpf_struct_ops_map_ops = { .map_alloc_check = bpf_struct_ops_map_alloc_check, @@ -651,6 +666,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) .map_delete_elem = bpf_struct_ops_map_delete_elem, .map_update_elem = bpf_struct_ops_map_update_elem, .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem, + .map_mem_usage = bpf_struct_ops_map_mem_usage, .map_btf_id = &bpf_struct_ops_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153796 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08647C7EE23 for ; Mon, 27 Feb 2023 15:21:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230160AbjB0PVi (ORCPT ); Mon, 27 Feb 2023 10:21:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230132AbjB0PVc (ORCPT ); Mon, 27 Feb 2023 10:21:32 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED3FF22A37 for ; Mon, 27 Feb 2023 07:21:30 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id i5so5373216pla.2 for ; Mon, 27 Feb 2023 07:21:30 -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=ZrmMexioGuwPjZT+tUM26QmTb0IQCm4pElgfaJMzHrk=; b=INSzJ9kVnP6faYNpbGeh/sY8O0xTC5SlYha3vUD0FMZ26AeKORusNYliSTCXpu7W7g 3tEFlaTjyJuNCJRKuco2f6UddquR8Qk8BRiF2K0ktMe+fZGaFt98Z6uEtKMFLmuimizT ZenFt6xK60U0LjtPO8PRXbwKgdhHG/07oPEEEOz7S4bEwzuUZBuhnOisG+HqqJFriafb 1h+OSakifDrs9+TFRuO1zlxQ95ZDwEFEsIMOWiDhcskNzSFq00abTP4JOfJIcw5rV4HV mFds4JNyC3JISaCG3A0iXtwuoZIC52XHMhpPBfZyY8AUDxfThbl0xJ4b7Me2AxoNbHGM GoKA== 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=ZrmMexioGuwPjZT+tUM26QmTb0IQCm4pElgfaJMzHrk=; b=BFPhmMiJy6qpBw6gRu5u5P2iwcEjMXfB/lSgJMoGG2GnW4diX1IQjUASXkkUxOgvY6 vp36pgekRTFu5/8G6NNGT3JgYXegwJHtpmqp+wJKMY0iJCUurqKYiBjNb3JTq7jXRRe6 1wRjUjgi6IZMktBT4aSScZTbrtak8KucWXoQNEtlffdr3yHC5w/5Qmk1O0HEDlqGbMrk 11QOmnha9thiaQl2nFPA/hZN3WGtbyMedOMDmKqfP6S6NKzyyKMu52MS1Pipe9Qq7imt VY7jW1PFtWN9mc+ZfUPt4LwTY5/956hclAwf7UZEBv4yNoy1MMg/GgWzjPmIgoPJfWA8 URww== X-Gm-Message-State: AO0yUKUK2FTuCkQxp2MpnlBsrBr9DaMqu/7NKRKJxTdLzlHC/MHD40yM YYGYRZKkiZiOE6vOPvWtSwg= X-Google-Smtp-Source: AK7set9W+DE40EBzgn9kt+oxG6wIhXZNVip0zYH7/4jxdDHxqU3iAMKb1T0KH722GxNWJ6kdlx2eOw== X-Received: by 2002:a05:6a20:1610:b0:be:22c5:92df with SMTP id l16-20020a056a20161000b000be22c592dfmr23119061pzj.16.1677511290448; Mon, 27 Feb 2023 07:21:30 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:29 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 13/18] bpf: local_storage memory usage Date: Mon, 27 Feb 2023 15:20:27 +0000 Message-Id: <20230227152032.12359-14-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to calculate local_storage map memory usage. Currently the dynamically allocated elements are not counted, since it will take runtime overhead in the element update or delete path. So let's put it aside currently, and implement it in the future if the user really needs it. Signed-off-by: Yafang Shao --- kernel/bpf/local_storage.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/bpf/local_storage.c b/kernel/bpf/local_storage.c index e90d9f6..a993560 100644 --- a/kernel/bpf/local_storage.c +++ b/kernel/bpf/local_storage.c @@ -446,6 +446,12 @@ static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *key, rcu_read_unlock(); } +static u64 cgroup_storage_map_usage(const struct bpf_map *map) +{ + /* Currently the dynamically allocated elements are not counted. */ + return sizeof(struct bpf_cgroup_storage_map); +} + BTF_ID_LIST_SINGLE(cgroup_storage_map_btf_ids, struct, bpf_cgroup_storage_map) const struct bpf_map_ops cgroup_storage_map_ops = { @@ -457,6 +463,7 @@ static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *key, .map_delete_elem = cgroup_storage_delete_elem, .map_check_btf = cgroup_storage_check_btf, .map_seq_show_elem = cgroup_storage_seq_show_elem, + .map_mem_usage = cgroup_storage_map_usage, .map_btf_id = &cgroup_storage_map_btf_ids[0], }; From patchwork Mon Feb 27 15:20:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153797 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4732C64ED6 for ; Mon, 27 Feb 2023 15:21:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229760AbjB0PVj (ORCPT ); Mon, 27 Feb 2023 10:21:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230215AbjB0PVg (ORCPT ); Mon, 27 Feb 2023 10:21:36 -0500 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C45B22A00 for ; Mon, 27 Feb 2023 07:21:34 -0800 (PST) Received: by mail-pf1-x435.google.com with SMTP id n2so3715761pfo.12 for ; Mon, 27 Feb 2023 07:21:34 -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=JsHGBYYFhohyQndeZKCRssCyWfovKITpIREysK6LA6M=; b=b2tUuU19zAoiFdNL1rpmyfWLSSnq0DQ70VjNdC2uFGxQ/MUQEQQpgCLfalIA0QnI3X 7yYo6wtqAPiP/WQhdapiohEaUWSCMgzoR2SfGRHvXLPlwnoSM03zArIGeORN0or1Vhvf dqgIxIDrhm6mjw5Ugt8wxD0T2Mo2D66m0X2YrEXUAxw2XuuuXRo/qYWwi1ghGmR7NqDg PsvKoR3jne00lJD+S/NwxSMqSw3loDJobvamAML/yOgptw8wY9FHPh0r7NbaCAu1eK4J TT8RfNDwWRSH7lzLcaJp7YlLcLYnLLzmpXwfumm2w3fnZ+dnRZmsmbFx7Y7Vvqs4DGeB C8oA== 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=JsHGBYYFhohyQndeZKCRssCyWfovKITpIREysK6LA6M=; b=UkmqqCOSDwz0I4rKOdhRaFgJxFKFpBo4DLzoyz9lXJDoZktMdDNG6ioILIsG8Iqdek MkcnJNO62q6V7uWtL8fC3FXyERjgzKWdsr/9LciY+BMVwko2zdnOgs2yttiSXZo5y3zp 9yGrgFnxLoq9myswhRCXrynbrifv/uRVXr/IClZO7Ox0C2r3TiMA3zzx1IhgE7Q0hF4T G0Qp0ndZc9h3wbAjZhiZho+ITiyOwb/aMe56FT2sEZweDABX2Ko85vu+dfeOFi1CBJjU b7EOMA5ZqmSthCKBOtwENZ+q5YW0NSHWeeOp6YiomM8TEvdz1aHMtxipq/xZhkecZiXG BO/g== X-Gm-Message-State: AO0yUKUO8nrM3vt6Ke33+IFqm0ujsVhQ+7ktjeT52LyABjMqt5SzPCCc 8ylQxlnBzP5uSXamww5G0V4= X-Google-Smtp-Source: AK7set9hRuOgOcZ8KI6onLtqd3ZORSanLrKkr++V3BnzHtj7mqXY5ogT1BYJfy7nOicf/jy6ruE50w== X-Received: by 2002:a62:1ccb:0:b0:5a9:d5c7:199a with SMTP id c194-20020a621ccb000000b005a9d5c7199amr21227108pfc.8.1677511293867; Mon, 27 Feb 2023 07:21:33 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:33 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 14/18] bpf, net: bpf_local_storage memory usage Date: Mon, 27 Feb 2023 15:20:28 +0000 Message-Id: <20230227152032.12359-15-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced into bpf_local_storage map to calculate the memory usage. This helper is also used by other maps like bpf_cgrp_storage, bpf_inode_storage, bpf_task_storage and etc. Note that currently the dynamically allocated storage elements are not counted in the usage, since it will take extra runtime overhead in the elements update or delete path. So let's put it aside now, and implement it in the future when someone really need it. Signed-off-by: Yafang Shao --- include/linux/bpf_local_storage.h | 1 + kernel/bpf/bpf_cgrp_storage.c | 1 + kernel/bpf/bpf_inode_storage.c | 1 + kernel/bpf/bpf_local_storage.c | 10 ++++++++++ kernel/bpf/bpf_task_storage.c | 1 + net/core/bpf_sk_storage.c | 1 + 6 files changed, 15 insertions(+) diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index 6d37a40..d934248 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -164,5 +164,6 @@ struct bpf_local_storage_data * void *value, u64 map_flags, gfp_t gfp_flags); void bpf_local_storage_free_rcu(struct rcu_head *rcu); +u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map); #endif /* _BPF_LOCAL_STORAGE_H */ diff --git a/kernel/bpf/bpf_cgrp_storage.c b/kernel/bpf/bpf_cgrp_storage.c index 6cdf6d9..9ae07ae 100644 --- a/kernel/bpf/bpf_cgrp_storage.c +++ b/kernel/bpf/bpf_cgrp_storage.c @@ -221,6 +221,7 @@ static void cgroup_storage_map_free(struct bpf_map *map) .map_update_elem = bpf_cgrp_storage_update_elem, .map_delete_elem = bpf_cgrp_storage_delete_elem, .map_check_btf = bpf_local_storage_map_check_btf, + .map_mem_usage = bpf_local_storage_map_mem_usage, .map_btf_id = &bpf_local_storage_map_btf_id[0], .map_owner_storage_ptr = cgroup_storage_ptr, }; diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c index 05f4c66..43e2619c 100644 --- a/kernel/bpf/bpf_inode_storage.c +++ b/kernel/bpf/bpf_inode_storage.c @@ -223,6 +223,7 @@ static void inode_storage_map_free(struct bpf_map *map) .map_update_elem = bpf_fd_inode_storage_update_elem, .map_delete_elem = bpf_fd_inode_storage_delete_elem, .map_check_btf = bpf_local_storage_map_check_btf, + .map_mem_usage = bpf_local_storage_map_mem_usage, .map_btf_id = &bpf_local_storage_map_btf_id[0], .map_owner_storage_ptr = inode_storage_ptr, }; diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index 58da17a..0d4d108 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -646,6 +646,16 @@ bool bpf_local_storage_unlink_nolock(struct bpf_local_storage *local_storage) return free_storage; } +u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map) +{ + struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map; + u64 usage = sizeof(*smap); + + /* The dynamically callocated selems are not counted currently. */ + usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log); + return usage; +} + struct bpf_map * bpf_local_storage_map_alloc(union bpf_attr *attr, struct bpf_local_storage_cache *cache) diff --git a/kernel/bpf/bpf_task_storage.c b/kernel/bpf/bpf_task_storage.c index 1e48605..20f9422 100644 --- a/kernel/bpf/bpf_task_storage.c +++ b/kernel/bpf/bpf_task_storage.c @@ -335,6 +335,7 @@ static void task_storage_map_free(struct bpf_map *map) .map_update_elem = bpf_pid_task_storage_update_elem, .map_delete_elem = bpf_pid_task_storage_delete_elem, .map_check_btf = bpf_local_storage_map_check_btf, + .map_mem_usage = bpf_local_storage_map_mem_usage, .map_btf_id = &bpf_local_storage_map_btf_id[0], .map_owner_storage_ptr = task_storage_ptr, }; diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index bb378c3..7a36353 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -324,6 +324,7 @@ static void bpf_sk_storage_uncharge(struct bpf_local_storage_map *smap, .map_local_storage_charge = bpf_sk_storage_charge, .map_local_storage_uncharge = bpf_sk_storage_uncharge, .map_owner_storage_ptr = bpf_sk_storage_ptr, + .map_mem_usage = bpf_local_storage_map_mem_usage, }; const struct bpf_func_proto bpf_sk_storage_get_proto = { From patchwork Mon Feb 27 15:20:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153798 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3E10C64ED8 for ; Mon, 27 Feb 2023 15:21:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230009AbjB0PVl (ORCPT ); Mon, 27 Feb 2023 10:21:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230174AbjB0PVi (ORCPT ); Mon, 27 Feb 2023 10:21:38 -0500 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C91FD1C313 for ; Mon, 27 Feb 2023 07:21:37 -0800 (PST) Received: by mail-pl1-x62a.google.com with SMTP id v11so3535286plz.8 for ; Mon, 27 Feb 2023 07:21:37 -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=Q9yAL066kb5/fOCVw5E7uXU6m1iQadFMqnUoLomFIFc=; b=JJnyhmL0ArCl0KtUokgLLK1cP4VbM887cXlQeeFqAevk2RUSevj+N+2Dr/ZTNBtqAo B8/UD+QBy+aejiVZ4MPr9/b1EX4nw/wBf/yCJJIugPW4i/hIDZMrwvTc8KfaPSOFWxkl lVRhtEhNLubNqCUjjQg1kzCA4aQVjt3NUNR8rItJmY1dm8uJCM7lvKdzD86YM8zoul7M +3lk1LLu0OZjdDDfRI45pK9qXM7E3pM/wgknyjYRuHOUToRgXaTfYrh1JmuQmjG5lxxn yJ3h32Vc+95Zx4XqxHv4GeyeflBducCkWV2ORXfdNBSKk64JDC5DJP2Y+Y0P9mtyPXyq Qo/A== 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=Q9yAL066kb5/fOCVw5E7uXU6m1iQadFMqnUoLomFIFc=; b=TPHjpbOAf2km/ioBYSWCa0JwbJQoWMpKXaxxf1nChlevlYwubHSOVKgkESeSt0PUsN EfH+hG9BeCCBpl2dkb6f756U3Uud+qnlMmpL2azP8xlBsYNfG+tzPtC62Uv8hpZDNCfI m15ilyMDZqrj4LVj3m+DvwOqJ/h0GEx9GbkDjXudvV9ZVaOyYqQrQcfLBr6U1shVjdmp 2jdIL5SzlD5+XU3liN9WIERZ9EFRAVJ3WAkmTFqqzEwKzeNcNV+U4Nllrc/UNzPjFIqP vRtaZpK77KdqGjOTbopABN3k6hNKeXc9z2kWbEBqqOHH1wP7py2BvcY1kKxMYOIwa1LG P3tw== X-Gm-Message-State: AO0yUKWh4GTaRNGQnFSdO6SwCYIL6j3yMgCGRENm0zD26fK9aRNkCJQL aXnOl9KZTeek74JdTjPONZo= X-Google-Smtp-Source: AK7set/8SHrzBsvj0xRZOw6FRftIOZqSKkp/EflQbKsQ8UzGe6NeKu2w1XEVbt4GMWjV9Y+nib5zSg== X-Received: by 2002:a05:6a20:440a:b0:cd:7d01:7671 with SMTP id ce10-20020a056a20440a00b000cd7d017671mr2728956pzb.22.1677511297301; Mon, 27 Feb 2023 07:21:37 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:36 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 15/18] bpf, net: sock_map memory usage Date: Mon, 27 Feb 2023 15:20:29 +0000 Message-Id: <20230227152032.12359-16-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net sockmap and sockhash don't have something in common in allocation, so let's introduce different helpers to calculate their memory usage. The reuslt as follows, - before 28: sockmap name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524288B 29: sockhash name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524288B - after 28: sockmap name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524608B 29: sockhash name count_map flags 0x0 <<<< no updated elements key 4B value 4B max_entries 65536 memlock 1048896B Signed-off-by: Yafang Shao --- net/core/sock_map.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/net/core/sock_map.c b/net/core/sock_map.c index a68a729..9b854e2 100644 --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -797,6 +797,14 @@ static void sock_map_fini_seq_private(void *priv_data) bpf_map_put_with_uref(info->map); } +static u64 sock_map_mem_usage(const struct bpf_map *map) +{ + u64 usage = sizeof(struct bpf_stab); + + usage += (u64)map->max_entries * sizeof(struct sock *); + return usage; +} + static const struct bpf_iter_seq_info sock_map_iter_seq_info = { .seq_ops = &sock_map_seq_ops, .init_seq_private = sock_map_init_seq_private, @@ -816,6 +824,7 @@ static void sock_map_fini_seq_private(void *priv_data) .map_lookup_elem = sock_map_lookup, .map_release_uref = sock_map_release_progs, .map_check_btf = map_check_no_btf, + .map_mem_usage = sock_map_mem_usage, .map_btf_id = &sock_map_btf_ids[0], .iter_seq_info = &sock_map_iter_seq_info, }; @@ -1397,6 +1406,16 @@ static void sock_hash_fini_seq_private(void *priv_data) bpf_map_put_with_uref(info->map); } +static u64 sock_hash_mem_usage(const struct bpf_map *map) +{ + struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); + u64 usage = sizeof(*htab); + + usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket); + usage += atomic_read(&htab->count) * (u64)htab->elem_size; + return usage; +} + static const struct bpf_iter_seq_info sock_hash_iter_seq_info = { .seq_ops = &sock_hash_seq_ops, .init_seq_private = sock_hash_init_seq_private, @@ -1416,6 +1435,7 @@ static void sock_hash_fini_seq_private(void *priv_data) .map_lookup_elem_sys_only = sock_hash_lookup_sys, .map_release_uref = sock_hash_release_progs, .map_check_btf = map_check_no_btf, + .map_mem_usage = sock_hash_mem_usage, .map_btf_id = &sock_hash_map_btf_ids[0], .iter_seq_info = &sock_hash_iter_seq_info, }; From patchwork Mon Feb 27 15:20:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153799 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E98FC64ED8 for ; Mon, 27 Feb 2023 15:21:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230116AbjB0PVp (ORCPT ); Mon, 27 Feb 2023 10:21:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229900AbjB0PVo (ORCPT ); Mon, 27 Feb 2023 10:21:44 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AD011E1E3 for ; Mon, 27 Feb 2023 07:21:41 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id k21-20020a17090aaa1500b002376652e160so6544709pjq.0 for ; Mon, 27 Feb 2023 07:21:41 -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=yEqHGSzh4ebrLjbpD441xY6idCWwz84uueSdrYMosCU=; b=WTVuUqHA9nDmp5UBiufbO5/RpcybHmWsCD5kgmWw+48sIluAS2EuXQy5qlZq7XvAL2 29zpK5/HZAVN4o38za1ifi7glqjwiiNQML6VMk3zaCr8EOPk//VRscEoEsG4HY80T/+q 1o6UgbHbf2AWpMZgLMaZ8u1EA3DEmsKXOgYyExoKBTuNC9c/1CgOdjZSHXCFqpdoSka3 nDfflWrUBOGqDYpgw8BS5iI9HxvuFR6MA5QkCie9A6sRMcTbHMtcfrpFBkf4oEV8vRJG qPYo+oTwmo9/4jPIH5RHrXITSDdn5o9kbND2cA5C0LwmyMgT6sSah/vsoftQ/vNsb/g+ Z4Rw== 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=yEqHGSzh4ebrLjbpD441xY6idCWwz84uueSdrYMosCU=; b=BgpX4flmOA2hHCVDNk2RsO/IgdLrHSxsOFOvJ6OcjRnmfpfK2Z1ZrgEt6HGskic7/C XA5etQWdWNCwXxt7knZXGnahF1d/dvO/JoyRNWn6oiPFR6/o+6QRfWh83H+nInPtd4x2 mMDUeYFRKJgI+mW1GrMFakT6c23kq9Bzi7pksrf3u65Cqj+YRK4Pkx5nZmeO9dFkSYUl ucqYRQVOA84B19HVfq27II+XUxxTAsM6K6ZIxQJvyIB6dGndzoER+QVGyXv7ZbZUVp1S VOPPONjC6pSLTzsMGiPZYxFpodiT96oM53FE07VylvxhXcjdCmtNROA+pKsaVLkyT3t3 J23Q== X-Gm-Message-State: AO0yUKWQfPTbijQGLXopGtjh0DALjv9UROiZdzd/IzQHhd2oLReV3ROQ IHYIpsnYaPnycI3UxVEogyc= X-Google-Smtp-Source: AK7set+t5Mn3pHM+HlZ5y8OvC3sMphhSd8L8VWbG/voPAK4hfYGmtLyoSOFFhAiG+4Ra1kB94AnQGw== X-Received: by 2002:a05:6a20:12c4:b0:be:d4be:50a2 with SMTP id v4-20020a056a2012c400b000bed4be50a2mr32218535pzg.32.1677511300908; Mon, 27 Feb 2023 07:21:40 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:40 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 16/18] bpf, net: xskmap memory usage Date: Mon, 27 Feb 2023 15:20:30 +0000 Message-Id: <20230227152032.12359-17-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to calculate xskmap memory usage. The xfsmap memory usage can be dynamically changed when we add or remove a xsk_map_node. Hence we need to track the count of xsk_map_node to get its memory usage. The result as follows, - before 10: xskmap name count_map flags 0x0 key 4B value 4B max_entries 65536 memlock 524288B - after 10: xskmap name count_map flags 0x0 <<< no elements case key 4B value 4B max_entries 65536 memlock 524608B Signed-off-by: Yafang Shao --- include/net/xdp_sock.h | 1 + net/xdp/xskmap.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/net/xdp_sock.h b/include/net/xdp_sock.h index 3057e1a..e96a115 100644 --- a/include/net/xdp_sock.h +++ b/include/net/xdp_sock.h @@ -38,6 +38,7 @@ struct xdp_umem { struct xsk_map { struct bpf_map map; spinlock_t lock; /* Synchronize map updates */ + atomic_t count; struct xdp_sock __rcu *xsk_map[]; }; diff --git a/net/xdp/xskmap.c b/net/xdp/xskmap.c index 771d0fa..0c38d71 100644 --- a/net/xdp/xskmap.c +++ b/net/xdp/xskmap.c @@ -24,6 +24,7 @@ static struct xsk_map_node *xsk_map_node_alloc(struct xsk_map *map, return ERR_PTR(-ENOMEM); bpf_map_inc(&map->map); + atomic_inc(&map->count); node->map = map; node->map_entry = map_entry; @@ -32,8 +33,11 @@ static struct xsk_map_node *xsk_map_node_alloc(struct xsk_map *map, static void xsk_map_node_free(struct xsk_map_node *node) { + struct xsk_map *map = node->map; + bpf_map_put(&node->map->map); kfree(node); + atomic_dec(&map->count); } static void xsk_map_sock_add(struct xdp_sock *xs, struct xsk_map_node *node) @@ -85,6 +89,14 @@ static struct bpf_map *xsk_map_alloc(union bpf_attr *attr) return &m->map; } +static u64 xsk_map_mem_usage(const struct bpf_map *map) +{ + struct xsk_map *m = container_of(map, struct xsk_map, map); + + return struct_size(m, xsk_map, map->max_entries) + + (u64)atomic_read(&m->count) * sizeof(struct xsk_map_node); +} + static void xsk_map_free(struct bpf_map *map) { struct xsk_map *m = container_of(map, struct xsk_map, map); @@ -267,6 +279,7 @@ static bool xsk_map_meta_equal(const struct bpf_map *meta0, .map_update_elem = xsk_map_update_elem, .map_delete_elem = xsk_map_delete_elem, .map_check_btf = map_check_no_btf, + .map_mem_usage = xsk_map_mem_usage, .map_btf_id = &xsk_map_btf_ids[0], .map_redirect = xsk_map_redirect, }; From patchwork Mon Feb 27 15:20:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153800 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20D1BC64ED8 for ; Mon, 27 Feb 2023 15:21:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229629AbjB0PVs (ORCPT ); Mon, 27 Feb 2023 10:21:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230124AbjB0PVq (ORCPT ); Mon, 27 Feb 2023 10:21:46 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEACF21A11 for ; Mon, 27 Feb 2023 07:21:44 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id h8so3763601plf.10 for ; Mon, 27 Feb 2023 07:21:44 -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=Q9Z5DKhfz6PQXivX8D2qLY+IJ9SYZdE33NA/HYP26PU=; b=YeHoBBXZUnjCKBS3Pdl9+IPNtAUJLLCiGExuFVntMiCIGvFXr1et5QKFYcnTquLHsY DtL41lbDIV2LUo6P7Pwhz0n5FoHVY6HNrNJrUDAZUmMe2ENhIbdSrZVO2tPkmcrQ5c9Q fPH7slIDkU8ehlddDh+0g5mfB47QEfxlhJIHIRWyOH+5Kmz/+tda0vgVemsn/T7al/Rf NC5y3Lag4B/dkMVWWHIcwaPnYWZ9qXpDE5wE2DVEPpKfT8mRuFeeUZrni/7z/gSbb4vW p5SjLYjwCrO5JHwDNdn8t7ZBNyqHX2eKjdX3p8Hv4VccHGZauJbp4DVQ9hdg/Tm+MKdu 90Fw== 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=Q9Z5DKhfz6PQXivX8D2qLY+IJ9SYZdE33NA/HYP26PU=; b=JhQUE3EhFvGdlLvZHaN0vt9fuv5rlRtPsf/hAtI7ygjBBMTncXXzjdPa3XyzmfUddn Gg2PYaghAIS6V4JWtXbyqhxX4760L0XZbQNk+Nl8I7zLJB9AwURo6k2f5Ulu6vpUwOqw 5dkF17enR+VdMoe9r88/5dI68gxZYkPpyQzvbeiUk1hXY/pbVdWUmOJ7ExW3lJnBXw7L Z926/Nq+qQa+iYBQgUeo+qTE+vV3i6W1QudBRfS3RK1z5UFU1+1dZZbxGZaP8+mv6G2t UMxolvYFuCfRVPU6NzaGgv5ssV/IKKhg68uvzPL1HFf7M5mwDu7am5Xuexp7zOlED1vn 030Q== X-Gm-Message-State: AO0yUKVITpLqZm9pdKZYAdXq2871O8W+7+2UrK7AD6ADdNYLadKAZuT5 6TRtLPoKUye19ZX1jH73zD8= X-Google-Smtp-Source: AK7set9vru8Y31spVr9dxRvBxxFytTCIMF5tXv6u63jwB8o+V3wrstKE7iSyX268UzXjdaEgNFRarg== X-Received: by 2002:a05:6a20:6982:b0:bb:9d1c:ede5 with SMTP id t2-20020a056a20698200b000bb9d1cede5mr12674961pzk.19.1677511304274; Mon, 27 Feb 2023 07:21:44 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:43 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 17/18] bpf: offload map memory usage Date: Mon, 27 Feb 2023 15:20:31 +0000 Message-Id: <20230227152032.12359-18-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A new helper is introduced to calculate offload map memory usage. But currently the memory dynamically allocated in netdev dev_ops, like nsim_map_update_elem, is not counted. Let's just put it aside now. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 6 ++++++ kernel/bpf/offload.c | 6 ++++++ kernel/bpf/syscall.c | 1 + 3 files changed, 13 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index bca0963..341e8df 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2568,6 +2568,7 @@ static inline bool bpf_map_is_offloaded(struct bpf_map *map) struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr); void bpf_map_offload_map_free(struct bpf_map *map); +u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map); int bpf_prog_test_run_syscall(struct bpf_prog *prog, const union bpf_attr *kattr, union bpf_attr __user *uattr); @@ -2639,6 +2640,11 @@ static inline void bpf_map_offload_map_free(struct bpf_map *map) { } +static inline u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map) +{ + return 0; +} + static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog, const union bpf_attr *kattr, union bpf_attr __user *uattr) diff --git a/kernel/bpf/offload.c b/kernel/bpf/offload.c index 0c85e06..d9c9f45 100644 --- a/kernel/bpf/offload.c +++ b/kernel/bpf/offload.c @@ -563,6 +563,12 @@ void bpf_map_offload_map_free(struct bpf_map *map) bpf_map_area_free(offmap); } +u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map) +{ + /* The memory dynamically allocated in netdev dev_ops is not counted */ + return sizeof(struct bpf_offloaded_map); +} + int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value) { struct bpf_offloaded_map *offmap = map_to_offmap(map); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 8333aa0..e12b03e 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -105,6 +105,7 @@ int bpf_check_uarg_tail_zero(bpfptr_t uaddr, .map_alloc = bpf_map_offload_map_alloc, .map_free = bpf_map_offload_map_free, .map_check_btf = map_check_no_btf, + .map_mem_usage = bpf_map_offload_map_mem_usage, }; static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) From patchwork Mon Feb 27 15:20:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13153801 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1B25C7EE23 for ; Mon, 27 Feb 2023 15:21:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229972AbjB0PVt (ORCPT ); Mon, 27 Feb 2023 10:21:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229696AbjB0PVt (ORCPT ); Mon, 27 Feb 2023 10:21:49 -0500 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23D0621949 for ; Mon, 27 Feb 2023 07:21:48 -0800 (PST) Received: by mail-pg1-x532.google.com with SMTP id z10so3753453pgr.8 for ; Mon, 27 Feb 2023 07:21:48 -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=C2WET1805ss0lLQGWaW2KlvdzMS5ESAXwecQVAlAkQw=; b=ZtTkQdamdKAtOOEYrLn8LYY6/2UIZqrLPtp9xLbiFGGJTDG1WpdmeDKA4AcGsOhULa suJcOz8MKjkz5WTSzuNUwwleDEmt304Avr+bAxX1AvSspONOJAQE/0k8B2u/E5F7TfXP lftkq+Hun7ZfhkyhhdWqYjctV+KhgEutRwGIuvVLddtM2T3Beb092+XvZ8HjqJ65Pbx2 LIU2LYynwDY+2fTdSFMqN3LLYFFF+5TQKIG+wGuSeC5HtbseoqHvGp1ieQ1xgX19FBY+ TzFL6KvA/B3Vlh1JUUHZ91NAmvElxY99SBen37yJ5rYbi1TLgDSmXJnXfzS/c2Aw26su tN2g== 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=C2WET1805ss0lLQGWaW2KlvdzMS5ESAXwecQVAlAkQw=; b=rCEwrPb7FQB6nEMs0L/rgLCaxf+jV8rDL4rQoHmjBVE9j+MLM8q2uGmvVojJMj589E s09jV1xo0eu5XkyJOs4gFlWA53KCuyT+Z1EAy0Q7++4qA+gtXiH4JHsJR1XU4IE500GI Hbmxy58TIXakT2A2eg/UsWiM/VP7/t/XVOHE7yvimH2VdUZbdPNACbkVgMFpz95rxWMg xXzxeQ7d9OPPFqEBHXoNSQehx5z9LRHiDDJHco2suiLsiPCIwsVErrzy23bod2bG1vrm KmFZVuAFILX2SSRDpbOeh9eWZH4qrgI+MNAMeU9VUTPUsR4FvTJA6Kf2Xc6WYBSt11Iq ZcmQ== X-Gm-Message-State: AO0yUKVB9gu3rcpo4oQdjjzUhm9jfz2k2fdPNIErjqM7uzO/xXfHDkuQ cejd/pz7cjV50aK2vKvQTaQ= X-Google-Smtp-Source: AK7set/vm/m8K9DddpbtBNwc8UvE5zlH1JQpWsoJ7w4jCTTK9uzPAKCwv+Y4z6mXk5FqGXEej6U/oA== X-Received: by 2002:aa7:98de:0:b0:598:b178:a3a9 with SMTP id e30-20020aa798de000000b00598b178a3a9mr22878616pfm.6.1677511307669; Mon, 27 Feb 2023 07:21:47 -0800 (PST) Received: from vultr.guest ([2401:c080:1000:4a6a:5400:4ff:fe53:1982]) by smtp.gmail.com with ESMTPSA id n2-20020a62e502000000b00589a7824703sm4326825pff.194.2023.02.27.07.21.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 07:21:47 -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, horenc@vt.edu, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v3 18/18] bpf: enforce all maps having memory usage callback Date: Mon, 27 Feb 2023 15:20:32 +0000 Message-Id: <20230227152032.12359-19-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230227152032.12359-1-laoar.shao@gmail.com> References: <20230227152032.12359-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net We have implemented memory usage callback for all maps, and we enforce any newly added map having a callback as well. We will check the callback at map creation time. If a map doesn't have the callback, it will return EINVAL. Signed-off-by: Yafang Shao --- kernel/bpf/syscall.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index e12b03e..cbe7ff3 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -129,6 +129,8 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) } if (attr->map_ifindex) ops = &bpf_map_offload_ops; + if (!ops->map_mem_usage) + return ERR_PTR(-EINVAL); map = ops->map_alloc(attr); if (IS_ERR(map)) return map; @@ -775,13 +777,7 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) /* Show the memory usage of a bpf map */ static u64 bpf_map_memory_usage(const struct bpf_map *map) { - unsigned long size; - - 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); + return map->ops->map_mem_usage(map); } static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)