From patchwork Thu Feb 2 01:41:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13125207 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 30576C05027 for ; Thu, 2 Feb 2023 01:42:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6D8C6B0087; Wed, 1 Feb 2023 20:42:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C1E786B0088; Wed, 1 Feb 2023 20:42:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE5C66B0089; Wed, 1 Feb 2023 20:42:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9F2E06B0087 for ; Wed, 1 Feb 2023 20:42:45 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6D2CE120E53 for ; Thu, 2 Feb 2023 01:42:45 +0000 (UTC) X-FDA: 80420652690.07.24CA13A Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf08.hostedemail.com (Postfix) with ESMTP id A1C09160007 for ; Thu, 2 Feb 2023 01:42:43 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YLlTiQVi; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675302163; a=rsa-sha256; cv=none; b=kbJ/MyOfeNdQ0ihV1oGx3/8DBbESrUQPDOMnxew+JhjMSWJyPLpLif/+rvX8G1+mb/Iy8j bt0pQHZyi1QJw/ejkeQ1XpOPAHMGhEXSLXtvCm6Ln2GmhVKUvVKLbJVmFXAHbR5AbSTnYB VcriJClN6AtQXbQP/4APryEcBPg/GtM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YLlTiQVi; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675302163; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iXkRPNUKE8d5ftVBz5595yLHB869PZbpQON83mShRx4=; b=E1qhLP7ErArfQJEA3d9lAFcx1hW8lnrrS1siO+HbxYuhwGvkDNBTn1p479ZitAWZ1ByaVe rcT8/ZuyIAV5VdNgOz0qW24XbKHuR611N6xSKIEa/V6KL5lRsXM6EC7TICEysxWCrjBfNy rE4tHHYdKjNC2nxxix0HMI6PbTXpB9o= Received: by mail-pl1-f172.google.com with SMTP id b5so387435plz.5 for ; Wed, 01 Feb 2023 17:42:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iXkRPNUKE8d5ftVBz5595yLHB869PZbpQON83mShRx4=; b=YLlTiQVi1W6kOesI/itZUMFMXk0vDTecHCMSOuC1nRnzwzmVJkcWdwmEzp+h6Y0VGF 6bRLPxE9ercMM/AzdE83khyPtI6P6KjiTMBbOnPOroS+0coKScYO52fdo2es8e9hms3/ muEbfteiVwr3AlG94kSuXrL/2s9PFdcJM40hPW8VnKV67RoXbSDH99li4/gX7D5XNXxR yDPRvkgmp0UJCTbi65CFakuGydXukRwvdFeaUlQffBOvO6OvIGe2xt1Y4hTKY0fUbO3u 3qxbAi/x9otJLXP49/B+8xf7CThsGjYnhfm0D00n82xNGWGJPDGO4m+YtkXgJUaH8Ofv S2eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iXkRPNUKE8d5ftVBz5595yLHB869PZbpQON83mShRx4=; b=2DvOtSNfBZsMyLbiF9NKUaTYiHRKEuBFuA60d+BYZJO6AbCR8W2XsWQ829Y8NsgX5V 9uxMI1SL0EKKO0XBoVawryMEX98LcCMETZOJU35rs3x2VzJdcri4IuhDzvB0b9Ffbjod eZKqhdHtnQ2x7+xeK+4IhTcwfZwlxEu0mgSMP5eQhlbypldxikLJX169ItIT5l5H+IQf mrhBFLWcGTLYpF6xjnGvkvPEgshLehkb0dr0B7dnyS9m6Q5CoZXzUUP3KnUOcEIw78qF WOqa3ThFc6nQZl81BHfO59GkENN83hPPalSOU0pRNlbSRqc9/mXt99EG2hG9IARefyZj EyWw== X-Gm-Message-State: AO0yUKW7r3NcfA0eIFEFGvlLNHVzAfGe8dSiBa65woJUkV5nf89m7z3v 3LSQ9WwjhBCetGlHKqzqh+U= X-Google-Smtp-Source: AK7set/SrSm3u8TRTQ8rnsLayAXgTID2jYtlbM0umtSssmngdloviUV9lnMNXrqFT2mZKh/SiGuJFQ== X-Received: by 2002:a05:6a20:1aa5:b0:be:b878:6d71 with SMTP id ci37-20020a056a201aa500b000beb8786d71mr4543762pzb.7.1675302162662; Wed, 01 Feb 2023 17:42:42 -0800 (PST) Received: from vultr.guest ([2001:19f0:7001:3f48:5400:4ff:fe4a:8c8b]) by smtp.gmail.com with ESMTPSA id t191-20020a6381c8000000b004e8f7f23c4bsm6594205pgd.76.2023.02.01.17.42.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42:42 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, dennis@kernel.org, cl@linux.com, akpm@linux-foundation.org, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, vbabka@suse.cz, urezki@gmail.com Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 6/7] bpf: introduce bpf_mem_alloc_size() Date: Thu, 2 Feb 2023 01:41:57 +0000 Message-Id: <20230202014158.19616-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230202014158.19616-1-laoar.shao@gmail.com> References: <20230202014158.19616-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: A1C09160007 X-Rspamd-Server: rspam01 X-Stat-Signature: fhm9zoryctba947zmmtfnjcu7ju9qqbc X-HE-Tag: 1675302163-526778 X-HE-Meta: U2FsdGVkX1+A04vjlTKLIBqDHf6bB2L6v66HKXsha74HRZ1aKgjMEHQl2Y1gzufUZaBHpnD0czvtiRWc68n4OP9eZA2AV5Z40JDqiqoGOAkpvoFxIBtBUS6BTNKK2mZ8lV9gIIY1RNHCBtQanP4FntZQeM7xJRv0o4C7caKGCnsEzFcvsmz4vMRkTkVJ0RFznGAW75hGVk9vmlE4RY/W8RviwqJd9jeJ+PAFIJxBTTahsXD/CXYVdsE/hXAwjmF6R9NvlmIsxh5eAfqP2blc4j9BkCqmc3UA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8thrcatmdooYWlsKGuagcEUFB9fXQ4yCUwmniITKZkroxZNuVB7vV2IkQvpVu+LvaXfxJOISq/nMYg147r1xjcEHF0v/lYFMR2s3EbaMen3fI4s2OXgk+JYolWA/bEBYNRZL+SCH2XTfl2c4+tGPAJUMUwC7sKMgx6iWAxv1utiBtIAbGOCvbMt6hGnMGcZ9+KReGVnTEV8qBButigN1wXLOqpAqanb4PeenVP6A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000010, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce helpers to get the memory usage of bpf_mem_alloc, includes the bpf_mem_alloc pool and the in-use elements size. Note that we only count the free list size in the bpf_mem_alloc pool but don't count other lists, because there won't be too many elements on other lists. Ignoring other lists could make the code simple. Signed-off-by: Yafang Shao --- include/linux/bpf_mem_alloc.h | 2 ++ kernel/bpf/memalloc.c | 70 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/include/linux/bpf_mem_alloc.h b/include/linux/bpf_mem_alloc.h index 3e164b8..86d8dcf 100644 --- a/include/linux/bpf_mem_alloc.h +++ b/include/linux/bpf_mem_alloc.h @@ -24,5 +24,7 @@ struct bpf_mem_alloc { /* kmem_cache_alloc/free equivalent: */ void *bpf_mem_cache_alloc(struct bpf_mem_alloc *ma); void bpf_mem_cache_free(struct bpf_mem_alloc *ma, void *ptr); +unsigned long bpf_mem_alloc_size(struct bpf_mem_alloc *ma); +unsigned long bpf_mem_cache_elem_size(struct bpf_mem_alloc *ma, void *ptr); #endif /* _BPF_MEM_ALLOC_H */ diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index ebcc3dd..ebf8964 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -224,6 +224,22 @@ static void free_one(struct bpf_mem_cache *c, void *obj) kfree(obj); } +unsigned long bpf_mem_cache_size(struct bpf_mem_cache *c, void *obj) +{ + unsigned long size; + + if (!obj) + return 0; + + if (c->percpu_size) { + size = percpu_size(((void **)obj)[1]); + size += ksize(obj); + return size; + } + + return ksize(obj); +} + static void __free_rcu(struct rcu_head *head) { struct bpf_mem_cache *c = container_of(head, struct bpf_mem_cache, rcu); @@ -559,6 +575,41 @@ void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma) } } +/* We only account the elements on free list */ +static unsigned long bpf_mem_cache_free_size(struct bpf_mem_cache *c) +{ + return c->unit_size * c->free_cnt; +} + +/* Get the free list size of a bpf_mem_alloc pool. */ +unsigned long bpf_mem_alloc_size(struct bpf_mem_alloc *ma) +{ + struct bpf_mem_caches *cc; + struct bpf_mem_cache *c; + unsigned long size = 0; + int cpu, i; + + if (ma->cache) { + for_each_possible_cpu(cpu) { + c = per_cpu_ptr(ma->cache, cpu); + size += bpf_mem_cache_free_size(c); + } + size += percpu_size(ma->cache); + } + if (ma->caches) { + for_each_possible_cpu(cpu) { + cc = per_cpu_ptr(ma->caches, cpu); + for (i = 0; i < NUM_CACHES; i++) { + c = &cc->cache[i]; + size += bpf_mem_cache_free_size(c); + } + } + size += percpu_size(ma->caches); + } + + return size; +} + /* notrace is necessary here and in other functions to make sure * bpf programs cannot attach to them and cause llist corruptions. */ @@ -675,3 +726,22 @@ void notrace bpf_mem_cache_free(struct bpf_mem_alloc *ma, void *ptr) unit_free(this_cpu_ptr(ma->cache), ptr); } + +/* Get elemet size from the element pointer @ptr */ +unsigned long notrace bpf_mem_cache_elem_size(struct bpf_mem_alloc *ma, void *ptr) +{ + struct llist_node *llnode; + struct bpf_mem_cache *c; + unsigned long size; + + if (!ptr) + return 0; + + llnode = ptr - LLIST_NODE_SZ; + migrate_disable(); + c = this_cpu_ptr(ma->cache); + size = bpf_mem_cache_size(c, llnode); + migrate_enable(); + + return size; +}