From patchwork Sun Mar 5 12:45:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160102 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 90534C6FA8E for ; Sun, 5 Mar 2023 12:46:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229557AbjCEMq0 (ORCPT ); Sun, 5 Mar 2023 07:46:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjCEMqZ (ORCPT ); Sun, 5 Mar 2023 07:46:25 -0500 Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5E61E057 for ; Sun, 5 Mar 2023 04:46:24 -0800 (PST) Received: by mail-il1-x129.google.com with SMTP id l2so4799183ilg.7 for ; Sun, 05 Mar 2023 04:46:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020384; 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=MafW7bXPA88fHr/WGC7tIi2znNqlfrWVpdbFx7QF1LE=; b=V8YuuQhIY0hw+e4AFuP0jQwEV+lVJpKyGATSgl333vPw96jO6aV4jvc7d9YK2My5HR vgqAAxGvllpypoicDVp1rEsYGdwymbQ/LchzzWAEaNCAZ6wc+cAwlBhRzDJdfJtOqaYG fJvyIUIZdfaGY5naFrUFAX4ybtdYjicVgEYfQ5TFihyBc5g7uZiS5xTwYvJ4ZYYyT+1k bkMiqf2sytlMF5r4n3x+Eh6k0ybwI2YZPdOOIJ/uFvOqSp4TH3GI+jAfBETxTxZJZbvi skgyZb3Mysp2L1BqcWTAOBAL3idiAKniIXqhqVgBgGm8x0seNF2cCTpbSOxnGggEJu/f o2nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020384; 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=MafW7bXPA88fHr/WGC7tIi2znNqlfrWVpdbFx7QF1LE=; b=XnJOYVYzrmHaQ953zHN6gxhP7PHaxa3Qb2OvmfaKcwcK4DWHq2uE2C/pulTBh30O2t 6cj1IfhdScGuDpE0qaxaY/JrdjLIKJFjs3jqu4hII9A0P8xoY8v01DI0f/ODI9kBuzoq jxAiO82tjaYwmT6RQhjVjfnpmWg2unNbwCAwawXPpV2l9uFAUKBXfc88FA3HHZ0ZHu1J Rbu0JuSEQ05CgIZLUCl5lJ4uts1qegXs8Ds4t+S6jdetMf0tOPLmhA4NPAnFY5mhf9DY 3SiPRLcVO2Xg0mYthBURmwXjVlLlUGl7XnqnkOoHb7+FguCzNznx8CAJ6zNBU9AvbegZ 5kHg== X-Gm-Message-State: AO0yUKVdXhGcygSR3XnKmCkquoyuDRjjaOVX6dItPRSF+UI0EFTN04tO q/c4elx/fBBgz81ImocBMyo= X-Google-Smtp-Source: AK7set9X+oGneEQ0wWXIrHjNSZ8t+BZ8R7PGyWDJK99ZF2w0E9Vl4Z4hvJ64akRxRfA5NiAtwBeKBg== X-Received: by 2002:a05:6e02:811:b0:314:27c:a730 with SMTP id u17-20020a056e02081100b00314027ca730mr7423321ilm.0.1678020384035; Sun, 05 Mar 2023 04:46:24 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:23 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 01/18] bpf: add new map ops ->map_mem_usage Date: Sun, 5 Mar 2023 12:45:58 +0000 Message-Id: <20230305124615.12358-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 d345680..9059520 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 eb50025..073957c 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 Sun Mar 5 12:45:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160103 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 B9D97C678DB for ; Sun, 5 Mar 2023 12:46:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229437AbjCEMq1 (ORCPT ); Sun, 5 Mar 2023 07:46:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbjCEMq0 (ORCPT ); Sun, 5 Mar 2023 07:46:26 -0500 Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com [IPv6:2607:f8b0:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D40C2CA0F for ; Sun, 5 Mar 2023 04:46:25 -0800 (PST) Received: by mail-il1-x12b.google.com with SMTP id p13so4783309ilp.11 for ; Sun, 05 Mar 2023 04:46:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020385; 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=3ficowSGEUkQCN6NzuyEybzi5V6w1MYg0SyfvaQz1rQ=; b=Gf/ZCzbPbyRC/m4885/YtvHioATjlZrw7a6erIpu+0Yqh6rVHFsPUwG5vXFLUGWOPq cmSRVKreqUmpFIS9KL41UxQEco55kGbebrmROBPpes8aichsEAR76Th1YHv3CFeEOoET YJcFfD0BHvmN+N6dc1MggWnSla9Y9PxuOLnGvKo7bI7d5FzB4RaJMjCeYdzaz5Xcxl10 cpgt1JKdY7cz3xsr0M7fAtg2BmyMDt7unDUpcCscy86B4ynTrds/MO59oTE2h9lfDqJM BMcAlSgOu6L/b05tN8wE+lINLhWypwVgKY1EJwrFnBG7zw92s9RlmpO6MVGB6Pt2OSDL Owww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020385; 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=3ficowSGEUkQCN6NzuyEybzi5V6w1MYg0SyfvaQz1rQ=; b=1wLjiYXpUXKvX1jovZXs5idJpSnj81Q2+zzu9wGQAg2/NrhgdE7SIYzOfqsAzG+UIi mY9cbie4BKQ+y9VxxwACtT0BegZdCQ1fzCL+d9qe6D+2wxIKtynzE4v8LCW+6/D5LOhL BNbJQVA9qmffFfbIShDghi11vCAQpI2/moJQ+OjiRdL3qgNKTKxpF82sfMECVG+fZoE1 ebT8F7bb3ffxgSFvUi/MLaTtdOUkD+MRvhp0+zGK1xcbShfMPNNtdyGrRUS2I7fyRcQM W+lnJ1IO3tpYapHsQpnfjSLTPo+odbEKEjCDNlxvtjwdJbrA+xgcF8kdg/Myo9oCmc3r dC+w== X-Gm-Message-State: AO0yUKXDcKxzyWDFeMkoDRHJ+GHEzsS0DCqcfegnPcOBuJy86keLKELa abSc9hyFiWFuYJVAqD1D2WM= X-Google-Smtp-Source: AK7set9dmPaOCLfh+5b9MW57tKq99pbxeQG9ccKZ+6XHJMV43i+OiVJsPq+X5AqokPC0A7LTsKxodw== X-Received: by 2002:a05:6e02:1ba8:b0:313:ce4b:a435 with SMTP id n8-20020a056e021ba800b00313ce4ba435mr7200818ili.25.1678020385177; Sun, 05 Mar 2023 04:46:25 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:24 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 02/18] bpf: lpm_trie memory usage Date: Sun, 5 Mar 2023 12:45:59 +0000 Message-Id: <20230305124615.12358-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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..dc23f2a 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 * READ_ONCE(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 Sun Mar 5 12:46:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160104 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 C7D89C6FD19 for ; Sun, 5 Mar 2023 12:46:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229559AbjCEMq2 (ORCPT ); Sun, 5 Mar 2023 07:46:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbjCEMq2 (ORCPT ); Sun, 5 Mar 2023 07:46:28 -0500 Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C167CE057 for ; Sun, 5 Mar 2023 04:46:26 -0800 (PST) Received: by mail-il1-x135.google.com with SMTP id w4so582084ilv.0 for ; Sun, 05 Mar 2023 04:46:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020386; 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=cu/vAfEmHhCsb3IwcGAX6XTmUnTtW1Rb4D+n9EPDM6I=; b=VlouMaOt5FtXxeFx/Pf/5zqJZJkim3g3Rx4CyNYrKvruDjc5EuZqaUw+7GQ9gfHXJc NFvZpS6E/7U6kdfmi3vtImRRtB6fFw7SPqo47Ye0W35NDO4h8+42hrTHzmca/Q1xC7+0 /j7O2uElQmpWRVgoug7hEA0fMNYrgEsIs/SdRRTxNBLla4zPf14gXGJKUtCJDDuKDPMV yMDwo3R0Ma+AKfvwdJ3r3u9BHiy0iAh8Vbk2R/y0563vLX+C2l0Ee7k/sKTZq/4STMfT zylvEIwgHbztJ3zpGwD2rmKIvU6nn82S+wBExqE/at+MkfSuQPT9Qc2BajBCDQk0dRoH gWBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020386; 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=cu/vAfEmHhCsb3IwcGAX6XTmUnTtW1Rb4D+n9EPDM6I=; b=ngQ5rrTZBs7MOIzWEII0PpMrIh9UxnVIQCXCLWCdsdsMDftlrZCpTKruQBx3ixDsI0 eElNGpcOpz29nHmlRPIibcx1vbYMpLGAv8VkIyaFCiFwgGzuHEjIFH9xtEMSg6trR13/ 5KOUJVgNOiZD0rpy4YXC4CL/FPEF9ztt6Sec2nF3m0ZEG27ihgxjjFGQV0xiBfqJctsX qQd1en2RN7wdHFVT0CwXSq8i3Ac1VaVaDu7mLDwYLPFIuF80RRuNN3IIeX4TMyEVIrgV HzAjibhLElxtUSzVOc5L3eZw8GlaL8blvzPMQUnsPu6JT89wounbOw2MYKeSlMW7YaNL VbVg== X-Gm-Message-State: AO0yUKX0vUwoo2bZTKJtyOmASwCFEAj9AO+NhWg6JE+s3tMY4GVMx4rM x0TDdIXSEKxfHPgJancXnK8= X-Google-Smtp-Source: AK7set9jjzYHMglSlYKnEnQa4zB/ftputBAb4od70rbfJX1u/vTKcgr0Qq0wRKFWiWnPg4bxPJ1YQQ== X-Received: by 2002:a05:6e02:669:b0:313:d82a:9fdd with SMTP id l9-20020a056e02066900b00313d82a9fddmr5765356ilt.29.1678020386497; Sun, 05 Mar 2023 04:46:26 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:25 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 03/18] bpf: hashtab memory usage Date: Sun, 5 Mar 2023 12:46:00 +0000 Message-Id: <20230305124615.12358-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 653aeb4..0df4b0c 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -2190,6 +2190,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, @@ -2206,6 +2244,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, @@ -2227,6 +2266,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, @@ -2378,6 +2418,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, @@ -2397,6 +2438,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, @@ -2534,6 +2576,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 Sun Mar 5 12:46:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160105 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 19C8BC61DA4 for ; Sun, 5 Mar 2023 12:46:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229605AbjCEMqa (ORCPT ); Sun, 5 Mar 2023 07:46:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbjCEMq3 (ORCPT ); Sun, 5 Mar 2023 07:46:29 -0500 Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C4ABEF96 for ; Sun, 5 Mar 2023 04:46:28 -0800 (PST) Received: by mail-il1-x130.google.com with SMTP id y9so3768210ill.3 for ; Sun, 05 Mar 2023 04:46:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020387; 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=pSUWvfMvqIxr/QevpAQochdcAe51AKsPLyT1A/GKDY9pfWLzl/b3MHvgkzDMkeTZLu hz7OMkYBTSTkX1H9Za8sytFAsNPuHUk+pvlsiyl0XisffLrAjlUs/dW1Vtubhm6kwDE3 krm5ckfv/rbdvyhoe6FLNdnj0bZm24iKlRhLYJ6JIPoj1FEGA18Zv83snlBIt7zhjWss AQhwpdoK8rSjarWVOV+luH+DMTaIcBQ8UEyVxYW+qJkSQnOUWZDDYReqCmNmFg6wYkD9 DQ0ZjsFf5UAuEkZZcbP4YC9X3ecDVad6FeKuh97985G8uefnBh6Mjb3lifoXfBj3ZUvr zJtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020387; 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=IdFguDcZGRYVmKPAdcbTyCCHVUb29Ga6ONnefW7yfY0NQHg2EpixFgl6dXWT8mc6kb bwm7xnMtzHPVBvBG/tlyEEYMF1lzgKtBsjWcjW+0h/CWojMt/THxez/mgbRa0jRHbrox XOFfw0ZCGUuPT1Ys6PJmBgQ9tsP23XFujlygB2X4W8Jg3XkX0J1duWxSmZ8Qx+q8FDt1 RO1IyD21Jo3Ax1Jz96hVgCJGETDcFGz7rEahEtQItd3ckgS0VfFvYWQ7XqUgvy27bYTi EKGkcvOBjnGv0Fawj065PvLP/Wga+7IcnFOFBV1Ik4V76JakFiE3y+70SRUEGOR3eZo+ 1Raw== X-Gm-Message-State: AO0yUKXQpRZ2l5qUWXnsINugfNlhpyM59tiUv0tsBNreT3e1z8SlDFQf /+Ei5NMgAlRmZt1Z1R5aFU0= X-Google-Smtp-Source: AK7set8hfAhPhe7dlRkTNO1pibztKzIMwVYxlwf0rkLj/lHjIc4Dh6Yon5cGSRvtpLb8+uTH3z9wRQ== X-Received: by 2002:a05:6e02:12ed:b0:314:f7f:a369 with SMTP id l13-20020a056e0212ed00b003140f7fa369mr8272413iln.11.1678020387665; Sun, 05 Mar 2023 04:46:27 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:27 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 04/18] bpf: arraymap memory usage Date: Sun, 5 Mar 2023 12:46:01 +0000 Message-Id: <20230305124615.12358-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160106 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 AE791C678DB for ; Sun, 5 Mar 2023 12:46:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229379AbjCEMqb (ORCPT ); Sun, 5 Mar 2023 07:46:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229633AbjCEMqa (ORCPT ); Sun, 5 Mar 2023 07:46:30 -0500 Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 841DAF741 for ; Sun, 5 Mar 2023 04:46:29 -0800 (PST) Received: by mail-il1-x12a.google.com with SMTP id k9so4289664ilu.13 for ; Sun, 05 Mar 2023 04:46:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020389; 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=PqOZJnFrDB86cB7btPiL6nolTzkufEP4VayhsRlUwPdZHZBcpwwLc5HZ/6Q3Hxl9C/ buLvHBYpOmg8Fed4RA+oKLNqje54kJ8S8fQMmC6FUcCr2RWn9TRSjbLKG388agJDCy68 zg1fjYc+Vk4sJujxTgNSaLHoJt6qkeqhyvpYC0ZFzPAan0K8Crpy95y7YvL/7f+jlrol NnrJaStC0DH4lV5Cmkuj//v5HPJAHWyvIbuhFdHPEvV7sIacyPIq4zowl4pr5s21wT+v cHoMW8MET5x2o/mi4n28Kjtf3gZL7VJnPS0VAwZDV8oMq8RONLXP7LuRBSc1SsfwCGO4 9iXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020389; 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=GMG3JKuVzLYQxXrCw6/0vpU8NskTJcWppLOZz8DUUvB73pTyQJNARFdTh+MI3ouvfz dTY2lVK7bQkcdgTYR6Zx0AhBjFS8Zsol6CVFab8ZuBYlMr26anwGJq430FwX9ETsXY6Y kFgWZ3CQOq/8Xcwpg1VySJq2qcGmq8EvS+BQRJYrzX+dqE3FfjrkS4cPCv2eWI8RksBw xQxzeVEnq2XjxJzpehz5Bum2a4/wmMjrdVIyOENgHOPo+fLKcnPvwwcT9mcEXWl4f4l6 18H/RF/2d/FTL7GTSXTNfgJuL5wdsJdkppCmVvo/zSRRioS6wrzJqn5FkDGAk+mhlmvX Z+ew== X-Gm-Message-State: AO0yUKVycnGamD0uOUFIwWmUPrA8VespGPXQ1aQKyTUtQwEQag1UGHiw OwycH4K66NHyop/oZqzHOqs= X-Google-Smtp-Source: AK7set9pGyP7wOtEyM+cVf+Zvo9fNmNxQ1bZjs5an1Oc+ILBlslGoZMqxE/dZSX29W0QL2IffzdJ8w== X-Received: by 2002:a92:b509:0:b0:317:9d16:e6bc with SMTP id f9-20020a92b509000000b003179d16e6bcmr5873233ile.5.1678020388893; Sun, 05 Mar 2023 04:46:28 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:28 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 05/18] bpf: stackmap memory usage Date: Sun, 5 Mar 2023 12:46:02 +0000 Message-Id: <20230305124615.12358-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160107 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 47367C6FA8E for ; Sun, 5 Mar 2023 12:46:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229590AbjCEMqc (ORCPT ); Sun, 5 Mar 2023 07:46:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229636AbjCEMqb (ORCPT ); Sun, 5 Mar 2023 07:46:31 -0500 Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C281F74C for ; Sun, 5 Mar 2023 04:46:30 -0800 (PST) Received: by mail-il1-x12e.google.com with SMTP id r4so4824383ila.2 for ; Sun, 05 Mar 2023 04:46:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020390; 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=m0SUc1/ZbIpfsNFM9e8SiSTidFUB7dKzzulYlEUp/e13nIQ0zN108kyGuGh/Cc3JBD 6eLQ0SIJ6zW9KEbcTfwSxA3cIiEcgzsfAo1rJ59aNpQ1NHzY8/DMrXTCVG1tmJ+uwsfS 0tTshksuZnc1p+yHMLFUz7VqtAyYmulbY0kmS7sB50XgDNzY6LokJW8UqAphpvbw+4O0 bNbyvA0D5yzAuolZio4BvAZ4FQiCPeMTztIb26yK3RdNOlcvWNQPLQ0gIdxeBxgQ6YXg 0U0l1/caJ6Teym0wn6uygMz60+oJQzHlAn3cD5qbAdJklqZdRBSEiZp3tLRGbmrzfT7O X45Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020390; 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=givITCPoFx+AX4Vv+Saz/HsLYqdcpgCdP36/uSEiS4zlViuNZ+RJmk12PvLmZLHoYO EalOwynlq1QrnAksDXY6lQA7Vk8Ww1aW+nrdbu3n/+ThbsXr7rr9CRqxwS5gHLqWVLaD if50YucHl/0p385ACPh//IZ5LSZC+s9R6pLFr6Emx0TK7qBFM7fcVagQq3geseAvj58Y 1fSJVyeMA1+j1Y9R7aPyBTzbYbKxPs1SrT+BFDv+ZP6CO5tENGFNmfGaj908RnSDEQkf RJAuYv9cigXCZ9hyUfsp+vK4zJA3vzCXacEkY7X4HvQedc0QQNKkfo+xjDrFZJUTVyz7 YzBg== X-Gm-Message-State: AO0yUKVkYsBDvefPA24/KY7nQqGgr6jfFx60vps9CkxfRLZ7WNX5GIlb Ie8jnmLGE4JydYgMvGGDQmk= X-Google-Smtp-Source: AK7set8vpt9Lfg4vcQlD+moY5p5C+AF3JDwmTTLbJuNidEi2xTTwmIh3ykvyA0nUOq1aV1lmVrwFAQ== X-Received: by 2002:a05:6e02:1945:b0:31a:1554:b0a with SMTP id x5-20020a056e02194500b0031a15540b0amr6468417ilu.10.1678020389889; Sun, 05 Mar 2023 04:46:29 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46: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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 06/18] bpf: reuseport_array memory usage Date: Sun, 5 Mar 2023 12:46:03 +0000 Message-Id: <20230305124615.12358-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160108 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 74D17C61DA4 for ; Sun, 5 Mar 2023 12:46:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229562AbjCEMqe (ORCPT ); Sun, 5 Mar 2023 07:46:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229677AbjCEMqd (ORCPT ); Sun, 5 Mar 2023 07:46:33 -0500 Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46DB4EF91 for ; Sun, 5 Mar 2023 04:46:31 -0800 (PST) Received: by mail-il1-x130.google.com with SMTP id y9so3768254ill.3 for ; Sun, 05 Mar 2023 04:46:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020391; 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=T8UDH+wlPN8uSpySWEpYYit/mpYRRJI5xeMgR3o6Mx4=; b=QBgbiAgLpxSYBD5r/n6S0ASsKMv7ibET5mUUjhzDSSBiwQWICyrtPDto0lH83OgoM3 SfgNJsr2xh3pvsjFnEXzOJkTOsm4B8PJ2EV7UKteKiEctld3Xk/JcPk+T7PkCdYV2gFN kPn5bcLhuCfIejZlv9Cdp4Y0O4fLnNmzfaTGFSd6FywVv6Km398QE3hikKC4LyQnfE9F Fc0MTy+W6dbX92oU6PkM82kqNSVKuQL353RjdGBfkzcdKpkiCkbUFciNStIzABjU5gnF Ytjs31g2ZgfXg+UCyXO9IIgZ92ZqBU9iqwjOGsFuWoiAakB1gus2O68OAvccZckUG9tb FspQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020391; 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=T8UDH+wlPN8uSpySWEpYYit/mpYRRJI5xeMgR3o6Mx4=; b=XdaWZyBq+oCRhC0xK1jOx/LcBoEUAht0V0pgHke8CZAl49SwYKUTLv47Z8+vYivPbV BEYVblqEf+1H11H66jEeAY2ZLmmiZqvUg2/VVjXDyXYkql/VSqwdUZRHOFgDDm18a3Cg Jbn42fY+I1WTve1eJlCcrZGYAt3svMqJHjYM6yHayF21ADEBl2sr2DRRsMLGdIPsndTt sy6zSSuAdUandc31k8/HSv2oMm+VYAQR9U75r/uX7sp/m5H0GWnR3Q8ydkdpzoalW+Aa LCbcGywbDa5UYyuIziEOY+DxXrTR/SbuX9RlGrR2QjJUVxRWhIHm7rT3MadoX5pJ+ujw VMDQ== X-Gm-Message-State: AO0yUKWaWABDqjSp+UHx8Fozq2cmu6FOsDgmo/8EXdNiGKDF6B7GUWK1 IqYSvC3d5TB7QQgvELn8qZI= X-Google-Smtp-Source: AK7set8VGtH+41mg5WJip6DxKG5bCZBe4x1pKCf1mw1TPn6h1lJrX6I6fdLKzwvWH/A9JpSA3hYs3g== X-Received: by 2002:a05:6e02:1b09:b0:315:34b8:4c6d with SMTP id i9-20020a056e021b0900b0031534b84c6dmr6666816ilv.17.1678020391001; Sun, 05 Mar 2023 04:46:31 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:30 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 07/18] bpf: ringbuf memory usage Date: Sun, 5 Mar 2023 12:46:04 +0000 Message-Id: <20230305124615.12358-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index 80f4b4d..57b8896 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -19,6 +19,7 @@ (offsetof(struct bpf_ringbuf, consumer_pos) >> PAGE_SHIFT) /* consumer page and producer page */ #define RINGBUF_POS_PAGES 2 +#define RINGBUF_NR_META_PAGES (RINGBUF_PGOFF + RINGBUF_POS_PAGES) #define RINGBUF_MAX_RECORD_SZ (UINT_MAX/4) @@ -96,7 +97,7 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node) { const gfp_t flags = GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL | __GFP_NOWARN | __GFP_ZERO; - int nr_meta_pages = RINGBUF_PGOFF + RINGBUF_POS_PAGES; + int nr_meta_pages = RINGBUF_NR_META_PAGES; int nr_data_pages = data_sz >> PAGE_SHIFT; int nr_pages = nr_meta_pages + nr_data_pages; struct page **pages, *page; @@ -336,6 +337,21 @@ 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 *rb; + int nr_data_pages; + int nr_meta_pages; + u64 usage = sizeof(struct bpf_ringbuf_map); + + rb = container_of(map, struct bpf_ringbuf_map, map)->rb; + usage += (u64)rb->nr_pages << PAGE_SHIFT; + nr_meta_pages = RINGBUF_NR_META_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 +363,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 +378,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 Sun Mar 5 12:46:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160109 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 5E3EBC6FA8E for ; Sun, 5 Mar 2023 12:46:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229681AbjCEMqh (ORCPT ); Sun, 5 Mar 2023 07:46:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229636AbjCEMqf (ORCPT ); Sun, 5 Mar 2023 07:46:35 -0500 Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5B47EF8B for ; Sun, 5 Mar 2023 04:46:32 -0800 (PST) Received: by mail-il1-x12d.google.com with SMTP id t1so942742iln.8 for ; Sun, 05 Mar 2023 04:46:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020392; 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=ATIpOr56mhb/z3FLbKDR+xBZbPICjiHqRbRiepxOl1feNlA46C6N/P6wQv5FZhath+ 6n+6j5+De2toxR7gV/TbYZ3hcJmA8wnanlMdNZgzplrmDnFMNRAgGnMQAlXuYMAz1fYo ebSoy7wz3p4KqqfFB2lWlYusr2dKlw3KfLPlQx194/LQU4orayAivl+c9iWmhnstH1NO MPAJT7O5ipAYhQd16Yg3RMSSm2UbsYZMgvIPDYi00/+WBFHLLQG9MSxzekxRtok9w+38 jBU4esGaYhja4d/Lskj8PldyNj3oA1jKnK2gruAZMG52aJuhtHDtTgTzPK4oO2QtmI8H cPag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020392; 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=RzepgGV5p7n7wQvRedIz0IDbr6k96V2UJi3PhbCK/aV25aF9D197bX80yLyE4FvwLq 52+K3W7O0cOZJ3g7dblxpDEe8436RHxODrcJkfXFrynoNvoct4+Cof/JWaokSiOjqxWX VNfvJE1oitQS93XMOBMVxg7N4XcXbZRr6DvhoKhNf2VuJ/OSl2b5G7EFUBCifHltMOls FfcwFOl/iIflkHPW9f1/k3v59URp0cz3Sw7OOQZ+mMP0WqIbr8du23rcFhhvHP9YBrjo gCOfLeVln/iE737azq9GNYWzTm1HIKHirXKUuiEQzrPDdOWkzPH95AVTiIGHtSOFze+8 gvDw== X-Gm-Message-State: AO0yUKUM1H/h132Bq7e95rzw+BIf/MywCcJnL/QOzizkZU8YQatR38J6 svNZfCufbhd+uqehffSECeV9csK+X8OEcTzPj5M= X-Google-Smtp-Source: AK7set+jgc7+A5B6g4YFuVCPJLQmgwq1D0xZu11bUXbA7vXLMv9mX3SEBculCIdqqQUIhzqW3Nhzdg== X-Received: by 2002:a05:6e02:1569:b0:316:dc3a:fe80 with SMTP id k9-20020a056e02156900b00316dc3afe80mr7151918ilu.0.1678020392048; Sun, 05 Mar 2023 04:46:32 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:31 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 08/18] bpf: bloom_filter memory usage Date: Sun, 5 Mar 2023 12:46:05 +0000 Message-Id: <20230305124615.12358-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160110 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 4B2BDC678DB for ; Sun, 5 Mar 2023 12:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229636AbjCEMqh (ORCPT ); Sun, 5 Mar 2023 07:46:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229677AbjCEMqg (ORCPT ); Sun, 5 Mar 2023 07:46:36 -0500 Received: from mail-il1-x136.google.com (mail-il1-x136.google.com [IPv6:2607:f8b0:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFBE1C649 for ; Sun, 5 Mar 2023 04:46:33 -0800 (PST) Received: by mail-il1-x136.google.com with SMTP id 4so4805794ilz.6 for ; Sun, 05 Mar 2023 04:46:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020393; 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=pTRWptfZGDb0EQ+CVKa8mjKJBZd3QAf6yoZB4LXli3GRdXMrKuANSjBhJZMe8ootVv 8dlDX6FuQcnfAr6ZRGTm6JNZBi1vBjEwkek99LciRsDhWEBTmHUu9U9jE0+HNKpLVVPw gwVIPP3lq8bd3RjDk9aLxe3kQMv78vPgRrR1mxeH3c24vxlf37pXtlo5Qgi67P3jyLOu kRFs1yKW7+rECnEBSlNBbsSw5yjd50MCjPYjbiQ9GoRCfLxKgNf1EZthpJvu2r0t7O/1 3633jE/9wpA4dc0qneRhYcpoY1kzv8YEeKt+OCkuLHMj+/Ig9Qg4QjoiqU2cOxV2n2eJ 1uJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020393; 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=x5TLeCyzLPb3224lG2pQoCeSVDafo+1NtfCm2dZLbOj7VMJv/qHJlAoGyN9mynnBuk OE1kl4X5nUsMJxjw8seuZlR/hrLUz8ghFrGd6d8ojn5LdeUNHnHBuo6Gj7YLcZBKX58q dcIucdekOmtXKR3MJr0Umd2+ELeMJDUhL30VfyoQ8h8nGa2lyba1oAC6Cv9Gcnw4E6hM VRxNr3F16qNzhw3e2WY/bp3zJncu+p7tv9qbsp36Xq8NOZDFracO1TmoV/L6VzecE8Kj sL4aEkepnBv0Hps+bPD88ZOLEF+i1Xj+0kYxdEzJmkJbwhN+42n7RLn4xz4+6DVut58F ftJw== X-Gm-Message-State: AO0yUKWtvFK/JK3OrslwNz37O+2hxAHjIVZN61f9ELW2k+XwG+1etP6r Z9d8LZN6q64oev0CuSix2a8= X-Google-Smtp-Source: AK7set9ES5Kqsl3YdAyhf5E5o2RnX2jTAwwRiwlMer5NkTQAsGcEBKXwrXyPhLw7+joigCmrLo9qaw== X-Received: by 2002:a05:6e02:1705:b0:315:3421:ef2a with SMTP id u5-20020a056e02170500b003153421ef2amr8579182ill.25.1678020393075; Sun, 05 Mar 2023 04:46:33 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:32 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 09/18] bpf: cpumap memory usage Date: Sun, 5 Mar 2023 12:46:06 +0000 Message-Id: <20230305124615.12358-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160111 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 ED41EC6FA9E for ; Sun, 5 Mar 2023 12:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229677AbjCEMqi (ORCPT ); Sun, 5 Mar 2023 07:46:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229686AbjCEMqg (ORCPT ); Sun, 5 Mar 2023 07:46:36 -0500 Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFD59EF99 for ; Sun, 5 Mar 2023 04:46:34 -0800 (PST) Received: by mail-il1-x12c.google.com with SMTP id b12so1202974ilf.9 for ; Sun, 05 Mar 2023 04:46:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020394; 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=nYbNcXqLx0Xnu+OZfznwuKJ9mvUo/P7jOqrHJQHSbOfxMePf82QxBb9RGYX26mZyEC ogIB7N8LQ1OhxK/GPC5Jr5DdnfR+JDppNnFNZ0QDQ/TQrfvdGXMKQouBzciULwaetgRZ QwKxcjOJr9uxR2w25QPqN8hQr5qOF41lQyQILqvhBgLxvj5tcAy4eddVUJinymGSJpeG 5MSfU4QTp5QtuR7fhHD84xhKaNzfGqyOezXXvqcZughG1zypQW7aFz1M4nDcr1mclDqK eyWIAQP8+koC1790J1iZTfiwYZkDGmYOnNNQe9Rd6o7wZ0UCOH+Sy+lkQdhlhGaH5V/e GX2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020394; 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=NptSa/egR10N6VGjcggICEqmc1mJEWGZTtbguovf9PfSqRUe1x0zA6SreZh2NE6zhw sJeB4W4PeuH9u/5xSoVZ8L8THSS07W9fJTGnbzAoeGuKxF5xxfqkZlyLc8TGmh8FC8g4 xQR6fanl3ZXtz5aMFRF+MXAZDQcjRBPl0TweNVWwvWOZtBX5OsNIpiPP6OqmHl1USXTx NZ0DhyW/Vink6P2iZB5ISnR26cURIUzOh5FyAp6XqtWwiChnMkdSEwtiukllahH1Gp/2 bNRLsw/5WUX3ZPRsY3ysso+CC00d/Wri2tVQIel0fN2nkFGYoq5ZhAfQ0S61B5FnHFRb tQLQ== X-Gm-Message-State: AO0yUKXsO/bivt7CxRZa/fJbCJUCXcFGFf10CM6p5RD6oiuz83okJWsF 8ye2lc5Hag+JfVbRFbC4bUY= X-Google-Smtp-Source: AK7set8i9U5rpHHcpoQbN7VRT7WGelKch3Ws10RR8TRmneqhYegnX3HE2qxppMxd+gzfflgC3XsZmQ== X-Received: by 2002:a92:1a43:0:b0:315:51c3:2ad9 with SMTP id z3-20020a921a43000000b0031551c32ad9mr5571033ill.21.1678020394060; Sun, 05 Mar 2023 04:46:34 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46: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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 10/18] bpf: devmap memory usage Date: Sun, 5 Mar 2023 12:46:07 +0000 Message-Id: <20230305124615.12358-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160112 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 B70A7C6FA8E for ; Sun, 5 Mar 2023 12:46:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229684AbjCEMqj (ORCPT ); Sun, 5 Mar 2023 07:46:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbjCEMqg (ORCPT ); Sun, 5 Mar 2023 07:46:36 -0500 Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CF9EEF8E for ; Sun, 5 Mar 2023 04:46:35 -0800 (PST) Received: by mail-il1-x12a.google.com with SMTP id k9so4289743ilu.13 for ; Sun, 05 Mar 2023 04:46:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020395; 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=SHPrPRT4voAq43xpTpn2L5Q1SUzjYpPKxudUGfUaXTLCXkvRwVXRCMpyQ6zEniCVg2 ZsNO3cjLtErspBbfl3ZLQeMj6kcSzmgjwLr1EoEkzdJJqokiTV3Cwjleg8NSsa9hAobn +u8w09kLp/ooMebkv7GRZuaDwvFFOyvsbG6kClqaJDIucm1S3qOQ40H/5/jg2fZUitb1 wAs1ppTw/9XSe/ee4NuYiWXNS4xI6zAmheGDmUwK1KpzKkCDKYmdjK/j4dM5VrIK1b72 76/7Fa8gNx1Y1LljNVPHX+K3FMt2w5+tfeebQ0MuW6XGS2alCcfYjekKVUHiBOEmiXsC VuJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020395; 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=YiViC6VSDOtSEMP7n/wFeFA9yHLXFxATWQ6Tr5oGxAlsmJ4+Sza7T/U6Nm8FOgXcF2 yAQXIa9wlihtIDfTUXhWkvSDMfoxq/xyRl0f1aI0FDeH6X7yr3OgLSQ3/I+PZGynEnl5 TEUBpkefs2HN1XrpXtS8aoclcLyRDf088e3shCsOMq9WibnN9nZbNlROi7Epy1bAh/RS qyEE53ITHLUtxwFVGkBoDH2AxsPS0u8UiKGRbu9lH56GXVQ58YuFKqsnLmyUjMp+i6WU fwUl6IMenzP3iYPeTeztBmzlkqQ3346/E4zHLC51AvGRuQjj+tT5uL8Jw735ltibQ15s 478Q== X-Gm-Message-State: AO0yUKWWYVE/bkIRt0NdpcqZZrixCfs3cYyobqTOVHdn4t9Rl2BMqrJ+ DaKOik9LZhBo31VbeT4H3ss= X-Google-Smtp-Source: AK7set+Pqijc+AxjzacF3hDyHbhDcUV4GSbsOrPN06R3f9rui7epRrJUzB5So4pEgVkCuzCO+ir57Q== X-Received: by 2002:a05:6e02:e13:b0:315:ac3e:9639 with SMTP id a19-20020a056e020e1300b00315ac3e9639mr8035909ilk.4.1678020395032; Sun, 05 Mar 2023 04:46:35 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:34 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 11/18] bpf: queue_stack_maps memory usage Date: Sun, 5 Mar 2023 12:46:08 +0000 Message-Id: <20230305124615.12358-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160113 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 5B174C678DB for ; Sun, 5 Mar 2023 12:46:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229633AbjCEMqk (ORCPT ); Sun, 5 Mar 2023 07:46:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229689AbjCEMqh (ORCPT ); Sun, 5 Mar 2023 07:46:37 -0500 Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69479EF8B for ; Sun, 5 Mar 2023 04:46:36 -0800 (PST) Received: by mail-il1-x130.google.com with SMTP id y9so3768319ill.3 for ; Sun, 05 Mar 2023 04:46:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020396; 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=enkl5ENiyCDGmgWozJha2dGyDH4IeEnZ5mmxA58gDcAjtquO7kQPWkVkd8QbpsE/5R J/Y9UWkuN6Mo5V1cvbc+bcD99rLYCfZALjOaJH41ilkktUg+KE/47sq6d6+ol9jo0JHz xVJQai7VeSCRXztqpZHC/6tePvZa8bZRNajRmat+uwf3GvvCbyBQiG49b1s3BqSty+ut 0h+PS1VTqsaxs/hm0s/j6tucWtgbmVvk7LxhlBUcpImG9QQ4Eu73z0KuFfaCV+KGlJLZ iDX4sgGh1lkY5sRm5cQxxWoNMrj/gfY1rN31CULvsoPYz1FMuvD87stKsKYsiUr4EBbE UQjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020396; 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=ONz6B/55l6861B2rMj2u/HsTzXEbiXdp1OT7nJxmbBBSBbS2Me27YoAiPyv55/a2WD chJayHIV3ZH1WfmfjWg4NWnrB0W/tqut+omKU0eEmFiyJveqxqR4JDjgZB9EntwrLavW XUiO3cmPKxg4cjbdKg3Fs5vg1RWrH1SdIjwgJWh7MkxYl8nQe387E4cs+nTBqr4I8TxU nx6QjsdHtA++aE+l3kDqsg6nQwrPeiBDShuLe5dgHRNOcEC8RFQTiOAZYwXqBKO3GG9D K7bT9FsGp/UokhSqwZCPLejxITVVOk5yrWxtlmJbfQs+41Lu4el+j27zt0DJq8jUGnAy A4LA== X-Gm-Message-State: AO0yUKXGhyHTWgceXmRKcjus+1vg5qviiKMPrjY+gBA4GUfchhG19HY6 FX4NyXjB2n0ve8dGvCs0gk4= X-Google-Smtp-Source: AK7set/TR1tFS7bzgwkJGxHuTU+WWw4mzvSi4vbtqeT2iivhnmAmPJeChi5paT0lENRazWBw+t2jrw== X-Received: by 2002:a05:6e02:1d83:b0:317:b8a5:6d2d with SMTP id h3-20020a056e021d8300b00317b8a56d2dmr7266301ila.17.1678020396102; Sun, 05 Mar 2023 04:46:36 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:35 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 12/18] bpf: bpf_struct_ops memory usage Date: Sun, 5 Mar 2023 12:46:09 +0000 Message-Id: <20230305124615.12358-13-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160114 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 BA6B8C6FD19 for ; Sun, 5 Mar 2023 12:46:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229643AbjCEMqk (ORCPT ); Sun, 5 Mar 2023 07:46:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229649AbjCEMqi (ORCPT ); Sun, 5 Mar 2023 07:46:38 -0500 Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74CC4F76F for ; Sun, 5 Mar 2023 04:46:37 -0800 (PST) Received: by mail-il1-x12c.google.com with SMTP id b12so1203014ilf.9 for ; Sun, 05 Mar 2023 04:46:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020397; 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=SpgrIyZOj4mo5eTEmeGr8zWeeteATtN+L6UP9OW3VaBEaC8K7fh0htxidOGPXikbmW ZJUWFItHN8RQ55mKJIvxjouHdF/DxqNm/aBiyWqTL5myC9fWwfI3J0oDEV9tOSrHVtzl /0+2Z+toYPR1iHVcAoF00iGG9SD8AOhKdr6U/U4dAZT4rj3+0T918o7G2MOS06DGr7Kf bVbK7g8I9nkE43s2afwtgRAYmsErv2CvH6W0p2DxLMFc4lZUwMwWHJU3kx0+Q82uayAA QrXlag+MYfsrVfi+AdBsGJcMm0f/nMsaEDP4+Us32argvX6PXAiVp7w6PVt5t+kum9Sb YLXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020397; 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=ZHuwYmBVEHeNo/1ij3/iTA4EyvS2FErzVV57t9P6T+5FSoD8IbtEOSHmUiDMz2JZv+ 80FbcZpvYQuxOvCcPYIYYl9tX4/0MP1FArqc2CM4Fg5IBg+lmci6JVLFUNriQRoNhdkh dFgKwwhtC8PW9t7J3dWTVyMPlSWsIUQ/9R0Tk7GJdQefbdpjj7RirMKCaKQ116ho0lm+ zhNEKU7a8F6u/BIcNArB0VCFf9LCT72RgzmDKE3H88WKEYQd7LLy4u5w+IrTgz6lAAGV 1utozM01fHJc5Y5OM7t0FWdw6qR2HX1k2x/TaT5NS6pZiuyI9HyHQdzWPp+2G5julEuP J3wg== X-Gm-Message-State: AO0yUKXe1s8MxfcFXI0kuNvnpZRZl5RqElDf/SweNv6gCc+3BjvG5z15 o544Rx0XQa4jcBwx3VMs+Y6eq6it+e2OJxDtkYk= X-Google-Smtp-Source: AK7set9E+epzKirGcZEhK/9LXGqftD6EfDp2HvnR5Pfx+biu2Yn+WGTJOpbKSlVsttm5NhxaMqfK+w== X-Received: by 2002:a05:6e02:20e6:b0:316:e39f:13f2 with SMTP id q6-20020a056e0220e600b00316e39f13f2mr6843502ilv.12.1678020397219; Sun, 05 Mar 2023 04:46:37 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46: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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 13/18] bpf: local_storage memory usage Date: Sun, 5 Mar 2023 12:46:10 +0000 Message-Id: <20230305124615.12358-14-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160115 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 37F82C6FD1A for ; Sun, 5 Mar 2023 12:46:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229649AbjCEMql (ORCPT ); Sun, 5 Mar 2023 07:46:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbjCEMqk (ORCPT ); Sun, 5 Mar 2023 07:46:40 -0500 Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5D76F748 for ; Sun, 5 Mar 2023 04:46:38 -0800 (PST) Received: by mail-il1-x131.google.com with SMTP id i4so4817555ils.1 for ; Sun, 05 Mar 2023 04:46:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020398; 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=j7v/ESz869l8hOPmt9s29UAAocP8YuswFSiq16NrLEg=; b=o8hwnC7EcJ97IuM6vVHKWdaXAXcXAaaRDGpAaUFZxKVMrH2wkqVBva4AMAC4ls72e8 7z2w9/oqtS/ZlrDwUNffzYWl35nonmwN2Kx0NBWNAh4ub0PsLmqZkIfUDlNUAtvbPr2I Cs65YyY6MUc85+uZYyrOgavz3QaHNI0zIHYD4Foltb99VUxHQgmAv1YZlxN3oAJc2WN2 Biu93jrZBHmZvCg6YSvaiyOtj4TwbuPG9HTkF+thJST1EGTRNi7qjpuIsQGd7HXvh7ce adDNC5VxkpgtvRszhEN7YKHdHOqKNR7DLMfuxJX97eOzOtRTn5V8S2cPcdrEEPIiYtQx uCDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020398; 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=j7v/ESz869l8hOPmt9s29UAAocP8YuswFSiq16NrLEg=; b=XN6BN7XMKMmRCXmzq97YOGYDwPnk2ZBDDOtDWKUIjl4JjYDAB9oeJz39ziNzlKbZXS Zk0un6uTIunX2oMxKocKZXojEO+IvvESImxP/uM4XdwOS0J0qCkIq/nGLjeuBQa9Houj W33thaDafdgKJ3oGt++ejbCgLnux/xL6SjJZAQ8W5kKqyC0ozyZ8wlOx1AUD+dO+/c9f eu332WJYH0qpLrqRQMFDlQbBFp3/F+zg6Ihk6xly1ZZqSk3iWPFFp5skkTsDi7Ruw+nU SbPTTxw3H8h+Y5QhZAjXqFXLWYP0ytfzYWOVnOyDTeLH4tdHmRZde88OEfRR52U80rsb f6vQ== X-Gm-Message-State: AO0yUKXH05kL7atPHMM1erSAViU1qlrcLHQNZgytJc7+hECfoePby5L4 1I9SF1EWgu6CkguVKBYI7qI= X-Google-Smtp-Source: AK7set+MvXL4vlzkKP++Ylq1XblB8jqnmcCxYNLKIppjfhs45pckWpXNm6UZNgqA0tgeLQf7yJe4Jg== X-Received: by 2002:a92:c54a:0:b0:313:bf44:b4a5 with SMTP id a10-20020a92c54a000000b00313bf44b4a5mr6838558ilj.13.1678020398192; Sun, 05 Mar 2023 04:46:38 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:37 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, horenc@vt.edu, xiyou.wangcong@gmail.com, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 14/18] bpf, net: bpf_local_storage memory usage Date: Sun, 5 Mar 2023 12:46:11 +0000 Message-Id: <20230305124615.12358-15-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 3d32039..d3ba3f2 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -685,6 +685,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 Sun Mar 5 12:46:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160116 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 DD353C6FA9E for ; Sun, 5 Mar 2023 12:46:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229558AbjCEMql (ORCPT ); Sun, 5 Mar 2023 07:46:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229686AbjCEMqk (ORCPT ); Sun, 5 Mar 2023 07:46:40 -0500 Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C39FEFBF for ; Sun, 5 Mar 2023 04:46:39 -0800 (PST) Received: by mail-il1-x129.google.com with SMTP id l2so4799369ilg.7 for ; Sun, 05 Mar 2023 04:46:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020399; 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=iIyQvJUaPeTbJt3LUNrdSyA+Oi2qf0dtbMoVwoFym4qSxucjkGPBerV6qkdweaHLxU FJBbe4L/HXI6k7HBlN1BgV6jZko4GxrfC9A5BK8lCtyPL2kEzoKno4lZ6pf+mufxVspV Swjf//RhEqm4dtMgilvxz+VB+EleohOENZH02KF1ssl+EupIwAabcNOdigDXpQVYJ91l uhW21zZb6WL6dsaSa3GNT5iQ2OCo09q7qhoUCC1RSL1Y2YN7hWrO9d4061zGnaNfzvuU iRYcOYd5uRsW4vUWrnFk6tCdznPC5YOplRkoTQUrZcGi1JBN5HTnroH8rln+ZuDPJEtA U4hQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020399; 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=BFzD2aME65CXJRMSHmnxvcLRBhQdUTDrn2PUDmobZXVcyUQZJ5f6b230qQo4FP1vwn wV2RgLKm/Ds7HcDHTU4f2eAVM5RsX1IZVm6Kt+623UDgXg8NLo9VDdWSsV/0FAXgM9pA 7+dbh53jvIzLozAeBufwFdYXUmfVieduI6oNLrc8nMnTQvmAzwHH8WzlnM1ReYNbiFSK Mn5jHwnVEoduY0DGrthEJSXfLpvTaMeqmkr4ufleSUxvwpE7od27c49NdJlJM/gnRMO+ a7QODBquQw36PPhtQx4I6qG9Xcgi+tPb8Ji+HX7fQlj/5wPeIunfk/dDPdJXovlYfbZR BKfw== X-Gm-Message-State: AO0yUKUJs1poyyI5RIMLTczeyQyrsslXH+FFamKDmqKet9vy8lnNAglH GgogzQJvr46wAx8buBERWrg= X-Google-Smtp-Source: AK7set/PWmr8MYw5pwBrQ00bcq5m+KZEilTBIcsh4nsOzHGI9rQv3KXAek7B+sRp9TjfMahpaX6G4w== X-Received: by 2002:a05:6e02:16ca:b0:316:ecbf:5573 with SMTP id 10-20020a056e0216ca00b00316ecbf5573mr7894243ilx.12.1678020399122; Sun, 05 Mar 2023 04:46:39 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:38 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 15/18] bpf, net: sock_map memory usage Date: Sun, 5 Mar 2023 12:46:12 +0000 Message-Id: <20230305124615.12358-16-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160117 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 B28E5C61DA4 for ; Sun, 5 Mar 2023 12:46:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229581AbjCEMqm (ORCPT ); Sun, 5 Mar 2023 07:46:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229687AbjCEMql (ORCPT ); Sun, 5 Mar 2023 07:46:41 -0500 Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com [IPv6:2607:f8b0:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58738FF2B for ; Sun, 5 Mar 2023 04:46:40 -0800 (PST) Received: by mail-il1-x12b.google.com with SMTP id p13so4783487ilp.11 for ; Sun, 05 Mar 2023 04:46:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020400; 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=dDzEbLwfjit3gopVaJUO01+sxRx7VlFyGGRjxrQouHboSDzYXl7kcCMlc4o7PJfw+e qBFQ9kPtrlKU6zIAI3vXRzQ6/iHjDGPkU8ZgSGXy9tNvp+OFKPqgGU90WvnrhrETogMA jPbJRw8bioyaHFQBhKbsG42a2yCssT0f9jTiVjq/cxSxSF2zQSLraIirfVXdWE4qf0j7 q6H1uccpAfGyyTg8OqS5U42waZL9M8yZ9qIMu4tXgd9S78U9/3YjRbI8+RO7TmHBHYBD MROyb3wdTyi2r+9Ec5twIJqgQsm47Auio3Ww4ETL8/05VHmxmx0b5T7vvj8ZdqEvzp80 9JXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020400; 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=DgbvNovK7mh4Vshe240I3GpZ3zKR2QpKkCp0MguQrEHYxK21Lay85Qf9gBe6etFgZA jd5xZ8bA97Q/3UDZN+24+HKxIpc1u3Iw4/UQikbPw+Q4Xor2Z/s/QaXGd3reWIUsKBY7 bOdX5W7gzVRtqhjPn08yMzQyXOyiEEy4N+1pXOvB4ZfafhQ6lnbRV4hCEJ6X6wVl8uds 8sr5W5gBjzQkhC0ckSkXzAkw/SIUpJ5RUQPhtoT+RGMsdd/f2m/mHsTvcheT4uiMI6YU SfOe20n+u/B0w7GKKsVuCaRWb4n/2RtInj9Qd5Q3KlbaST46qF5GdqV+VWPSZ3iLxR3B G4Jg== X-Gm-Message-State: AO0yUKVyhmFDD1gu2Yxg6jRj5ranWKpaKSYbzeLCUFwvzBY/VYFGygs/ Sdr2Br5kqCnQeHUKntJYFXM= X-Google-Smtp-Source: AK7set9dQlfwanyXZ0TZv9cPZNQruxxPgJdbzE+prAM7280kNKWMmabBtaXdMDVGUQYc/h/QDJs3aQ== X-Received: by 2002:a05:6e02:12ca:b0:315:8589:f598 with SMTP id i10-20020a056e0212ca00b003158589f598mr7534942ilm.6.1678020400126; Sun, 05 Mar 2023 04:46:40 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:39 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 16/18] bpf, net: xskmap memory usage Date: Sun, 5 Mar 2023 12:46:13 +0000 Message-Id: <20230305124615.12358-17-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 Sun Mar 5 12:46:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13160118 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 7CB61C6FA8E for ; Sun, 5 Mar 2023 12:46:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229687AbjCEMqn (ORCPT ); Sun, 5 Mar 2023 07:46:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229688AbjCEMqm (ORCPT ); Sun, 5 Mar 2023 07:46:42 -0500 Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 648FCEFBB for ; Sun, 5 Mar 2023 04:46:41 -0800 (PST) Received: by mail-il1-x12c.google.com with SMTP id b12so1203063ilf.9 for ; Sun, 05 Mar 2023 04:46:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020401; 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=8HN2rAcYUn7jZvakDvBEUWKRCMQqKtSQ/uWq6kAxCHY=; b=aObpSZED69kq8e+/TytOEfo/js+sfMmz3wuTKx4u7b0ZipF9D2dNieHXL7wqWHAWHE xsxIorPwnRTAzoqdsNUMHNHBQRZzz0CEtZBBqQsRpbOqnGoKXnfvuafJgbT+dgVOROTg 2MVQ0RW4/7KbFF2/xn2pyZ2oJo+n4WcU3C7FFl4U31M088AaRaIHdKkqOM/kL9Py6VY3 dBAVcG4s45JX5Ce5qwx9kCNRnrCErcg554EnHOYauXAUsEO3pL00MrIbxsAN1L/VjMKw 4etkDRGH3Cj8x7rb3k5QNWLLki+iHfSaYb0CoriaYW3A74jHYhxBGDcl0BtLwFxRBBh5 P4bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020401; 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=8HN2rAcYUn7jZvakDvBEUWKRCMQqKtSQ/uWq6kAxCHY=; b=Qw6FM+Vd85SycvIIc9ZA7GVpC4juu7qqkQQWQD6n6cVY36faHEqlBxy4QCkLIgDzeC osvUhEBDebZ7frCP4LY3tZnvEwYakUOkUypoxAdOUnfwaz8hz2dM/LazTriX/Mpd8qOV wtOZHoCBFMBs6NrVI56Mfow69KzIF8mPKJ/TX2EQ0fCXwHhPx9JWSVsxXf4L2ydEGHr2 y5BVBiNFkJll8AzBTUGwzMccvp/swLW/b7F0nrqz0Rd682EGmhvGWG7yQIRvZwyLMkqQ RgXumeoHpXO9U1aOJ2bbYEZD2/jbWbDrlfZ3EBXMa5y37kFlW73fFASwqyXtscfztQvr o7Ww== X-Gm-Message-State: AO0yUKVIidJ9pteKblRKgJeXIab0ZJ77KJUl6lUNXN5FZF+7oEufluci 9d5B8tN+zuT1Sq5bxHPV5i2Nh3xOXaAmzfhHF94= X-Google-Smtp-Source: AK7set/07EKa7Ig4p6YQHWgNBdU/NlsFrTDIV3ELmaJ2jLGO0c+vehs3gARJksK8LqWkJnR9niyzqw== X-Received: by 2002:a05:6e02:18ce:b0:315:9b3a:2f56 with SMTP id s14-20020a056e0218ce00b003159b3a2f56mr6291223ilu.28.1678020401085; Sun, 05 Mar 2023 04:46:41 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46: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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 17/18] bpf: offload map memory usage Date: Sun, 5 Mar 2023 12:46:14 +0000 Message-Id: <20230305124615.12358-18-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 9059520..6792a79 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2624,6 +2624,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); @@ -2695,6 +2696,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 073957c..3532c1e 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 Sun Mar 5 12:46: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: 13160119 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 C4B9DC6FA9E for ; Sun, 5 Mar 2023 12:46:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229545AbjCEMqo (ORCPT ); Sun, 5 Mar 2023 07:46:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229691AbjCEMqn (ORCPT ); Sun, 5 Mar 2023 07:46:43 -0500 Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 516DCEF8F for ; Sun, 5 Mar 2023 04:46:42 -0800 (PST) Received: by mail-il1-x12d.google.com with SMTP id t1so942878iln.8 for ; Sun, 05 Mar 2023 04:46:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678020402; 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=2IX58MH2QLoXhpmV/zJOVM+Ywg98JYzy0v23r4fWWAo=; b=U7ZVKK79PWpLTOb8M9fifa50phlLR/LB6xE1iqGAemhL1PICF5TN9AD1PQMHz+/SGS 2msGJIZ6vkZ2tyEi5VyMtGCTn+GL2a7I8mCpXKpb8U5AFEnZpWQs0dDxH/7FG8lbSYMN 8V3iyM4MxdEHofFC/FyN5WiH3rVS1RL7u3XTyPrSxlw/+TuhR91BTdUKyhKEJE74Zd4G kVe7z7HnJlmqkjfcan8G95fpFCs0R8knPvogd4KLRiHBxHJ0BIH5mZY712/3If1Re1ql 8c/vi/hK2e06T5Il7igOh5FJz7vkzWjRUwEhm4UCpQ5AnnlzAisJDBccwhB4ZciymXNz 3HdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678020402; 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=2IX58MH2QLoXhpmV/zJOVM+Ywg98JYzy0v23r4fWWAo=; b=ZHI7xPapsjn8ovLSf9w/MM7HxfhjtMo9swFvElyhqJojvtU6d34/vm/3L2eQVgNzSP jGEkhOoJ1gnGQB8PrX07KAGn/ZYdJCdbj5pyOLZWyN7RJF08FvQZGOy3H2YhX9ZByZF1 XKoeAIaIG7IFPbH4HNAgrRV2HUBr3lx8lQrcRGFvDOGjIQ5Kgh9l0Iz+X/gBTBatNF4b AP0ofAmtWX9sSqB/6hz/rnNT6M4pDqzGo29pi/KAmfnAs47NxCyukyi3oWIwwB3D0CPO nDGT76vWsG84Ds2AMczLShpL/lxtAWdV4ImNa001RUR/wpnsZPUkhRtdYvcWR7tdtz5q Dy1w== X-Gm-Message-State: AO0yUKVssFyqH9PMDNIZ0b1jZxbnEsILJCepbQMZAW/AnS/bBo3aAFe9 9bX+0EgCmNeQ6rOUY/FLjF36rDf2TbBVt62LexM= X-Google-Smtp-Source: AK7set9Yp3QuAqdeVlhRpreVDVq84HYzC++mxeM8a4bkdjAMoMnwrqnNB7xqMI967mKHoMVwJYTh7A== X-Received: by 2002:a92:1a43:0:b0:315:40d1:62a3 with SMTP id z3-20020a921a43000000b0031540d162a3mr5767228ill.24.1678020402041; Sun, 05 Mar 2023 04:46:42 -0800 (PST) Received: from vultr.guest ([107.191.51.243]) by smtp.gmail.com with ESMTPSA id v6-20020a02b906000000b003c4f6400c78sm2269629jan.33.2023.03.05.04.46.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Mar 2023 04:46:41 -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, houtao1@huawei.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v4 18/18] bpf: enforce all maps having memory usage callback Date: Sun, 5 Mar 2023 12:46:15 +0000 Message-Id: <20230305124615.12358-19-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230305124615.12358-1-laoar.shao@gmail.com> References: <20230305124615.12358-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 check this callback at map creation time. If it doesn't have the callback, we 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 3532c1e..da1e762 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)