From patchwork Thu Feb 2 01:41:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13125202 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 47ACEC05027 for ; Thu, 2 Feb 2023 01:42:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 828D46B0078; Wed, 1 Feb 2023 20:42:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D8BA6B007B; Wed, 1 Feb 2023 20:42:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A0AE6B007D; Wed, 1 Feb 2023 20:42:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5ABCC6B0078 for ; Wed, 1 Feb 2023 20:42:20 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2DA18140DC1 for ; Thu, 2 Feb 2023 01:42:20 +0000 (UTC) X-FDA: 80420651640.08.1AC3F87 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf09.hostedemail.com (Postfix) with ESMTP id 5BF07140008 for ; Thu, 2 Feb 2023 01:42:18 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=o9C1yU0q; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.51 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=1675302138; 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=a7K+pfryiakj921NmwDDKHUHNMkRDDlwHdec1/AfuAw=; b=zLB8BotWR7j6oHfXELk2hpxeXLoVqXKreDIf3Jt3sAvnvkMbi1Eq5QRBMj7wf8i4XqD25I M8Wf0SXBib18+RFu1sOpos3r2IBFWRAm+05NY0SJlq+T5TAEYjTLF2MJ8UEWw3YTjSR61J w+JyaJjmy9G4Ap84WIY0UjPvDclvfcU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=o9C1yU0q; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.51 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=1675302138; a=rsa-sha256; cv=none; b=NoNsk6jVcle+EGriftka2B4kg6cj+Zg60id/RUOqgGFNvPA59cTpgFr3wl41ZpyJdeNb2M yAbs5fX4Yvt+AN7pr2WVpjLa49I1fYq/ZNrxn55Zv0wu/GtmM7+fJkGNOAp0H2mGXUtaIX fkhJGkaesOSST5/U54g86UNRMWG74tE= Received: by mail-pj1-f51.google.com with SMTP id 88so505487pjo.3 for ; Wed, 01 Feb 2023 17:42:18 -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=a7K+pfryiakj921NmwDDKHUHNMkRDDlwHdec1/AfuAw=; b=o9C1yU0qRyxMb+8TjyXQNQmbHix8uSyQey7fMxpn7WB3cyhyXbe0esxvoUn/cYcC7l sRqr6UOS4MgqlhVNJRLusBxIqf2SRvOxbmza/2MFH/T3TF2WgsTf6ulZlLcAwUF6noqE LxckguhCFANPYNBI/kmrCtTki8+ZuXh6cl7LgJfEj+IgzOCGCsl3vAxhecbvBrHF5Xr4 gJ83wFUazvetJ8aKBIL9iUUqPe4ISRtDM3HOpIrVKZ5J6qbBRaYs8m95ZQHH06AR6Frc N5t/S45+9qe+JRQr/EubCBbF+2wBXVShFKEz7oMxUh6Hu6SELFnKpfc5AkwS+THIeB6m AALA== 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=a7K+pfryiakj921NmwDDKHUHNMkRDDlwHdec1/AfuAw=; b=TJev+hCrnjKQ+vEUXd6EeEYLBthb8TUUPW3ZEcmVIOzc2oqrku7e13vigu21lMWk6c lRqrwUT+eP9YgYEmXbRPv+jzVwLMDO6VKzq31dXSMTIyfwZVVTzF8YkeK+/VFXmLFtlo wJf7gJubRnbu+LyN8mylJBSMIrS3+WrwiaZmxpkFZVUpmqMo3mTx4xhGfUS7mBSeiHuZ 7Wa/CnDethf8J80EQUsPfp726/HxNYXE7Gy0MwTtaTZWEsyCWQJWn/Rxn76Xl2OuzLhp WKc76iZsXW2WUGXC17IlT3T/0R54yqLXEAGPUyhGcDJc4wky7lKVXJD4QkBBaprpqQaN iT5A== X-Gm-Message-State: AO0yUKXjM2T2xWlsy/2UC+mvc+3GGCc4QNE6r4d8VPPdBGFNhnKMYMe2 ytPW8U1iygqYHgB7cUaGiIo= X-Google-Smtp-Source: AK7set/GHLWgQK5/vzlqcUo2kY9V/MdT1dT0WTXwTEy8hgxP1zeDjhfySKsD37XrNnwjv0OVSqcuSg== X-Received: by 2002:a05:6a20:1605:b0:bf:40b:2db with SMTP id l5-20020a056a20160500b000bf040b02dbmr5874632pzj.22.1675302137265; Wed, 01 Feb 2023 17:42:17 -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.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42:16 -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 , Vasily Averin Subject: [PATCH bpf-next 1/7] mm: percpu: fix incorrect size in pcpu_obj_full_size() Date: Thu, 2 Feb 2023 01:41:52 +0000 Message-Id: <20230202014158.19616-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230202014158.19616-1-laoar.shao@gmail.com> References: <20230202014158.19616-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 5BF07140008 X-Stat-Signature: qbrkrn6b4mjk7x473taw7tsmjm1xw6wj X-Rspam-User: X-HE-Tag: 1675302138-605915 X-HE-Meta: U2FsdGVkX18qboEQejD3gkNF3RC/bq4tWAhOD2gNcrUJiMpy0IroBn4+qQkqnkGrx79svQJuu48fAZhybx3Lhjeioln8D/3697T5auhNtmxSbdKrp2NHoWNlbRaZxDPv2ItDc8qPL/mm94J8Bt93I0ozA+hKnVfrRHX0kAsRpNelWe7fWWa4ntRya9X98OH3IwsJzr8ww6EA4i/A0eBqTazZmrmrv9SMgK+WENgNId9ngwbpSr8US8CfRU2znOD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xiZV2UmIOTT69+4yP4GZYS58qqxeC/xaN1a1LDYzp6LrWQf17J6WyN0TS4tTCWUzn4CXnSf2OyzSaunkZev7y7MEx8S83P1i5Wl0p6nxGZH2LUCqrUZjjy3XAodlAdyztKf03VY3K0Uy5XkGheANas/IAmMj/xBdHLvehUmyOxJT5C4Z8XWduPlJcaQVeri8/627xtYcCehvkgszsSf9fDsQZsAp+A0GYDUVph/YsdqTY63zVwlncEuh5D/lUNDX+lHvRTXttr/HgOs+BKiALUnVm7u5q6zXaSWuPXX4bFdrWhIntC9G7Ykl9+1JvzQxn3ofUQAlVkhAHRrHS7Qk14DvvwO4eLwy3OVweK7agrxMJTfrRmmaWsDFDGFpQ1bOMPvg3fAKofQOjkMlBJK96IPWcdrC7OwByQrbtkXRgCZVpuisE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime. This helper is also used in non-memcg code, for example the tracepoint, so we should fix it. It is found by code review. No real issue happens in production environment. Signed-off-by: Yafang Shao Cc: Dennis Zhou Cc: Tejun Heo Cc: Christoph Lameter Cc: Roman Gushchin Cc: Vasily Averin --- mm/percpu-internal.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/percpu-internal.h b/mm/percpu-internal.h index 70b1ea2..2a95b1f 100644 --- a/mm/percpu-internal.h +++ b/mm/percpu-internal.h @@ -4,6 +4,7 @@ #include #include +#include /* * pcpu_block_md is the metadata block struct. @@ -125,7 +126,8 @@ static inline size_t pcpu_obj_full_size(size_t size) size_t extra_size = 0; #ifdef CONFIG_MEMCG_KMEM - extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *); + if (!mem_cgroup_kmem_disabled()) + extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *); #endif return size * num_possible_cpus() + extra_size; From patchwork Thu Feb 2 01:41:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13125203 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 001ECC636D3 for ; Thu, 2 Feb 2023 01:42:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8EC416B007D; Wed, 1 Feb 2023 20:42:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 89BC56B007E; Wed, 1 Feb 2023 20:42:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 73CA36B0080; Wed, 1 Feb 2023 20:42:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6099F6B007D for ; Wed, 1 Feb 2023 20:42:25 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2F36D140DC1 for ; Thu, 2 Feb 2023 01:42:25 +0000 (UTC) X-FDA: 80420651850.07.F421770 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf16.hostedemail.com (Postfix) with ESMTP id 60646180005 for ; Thu, 2 Feb 2023 01:42:23 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=XgJCNvo3; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.49 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=1675302143; 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=HRKKHzUPeYRgy9sp4sc6HU9jmgQ/kndp4UTR0lBuLfE=; b=kbsvRuQshkNh5vW7k6Iur2aFj7wIpieQspSK4lpea4z1Fca/i6/jau4ZvOf8AnhH96PXTB Mv23+mHjy12JfLdz/YfXwasxL/Eyd64jzkuR/6GUjLo0y2zfUd5BuPcFTvIB86RYyJ6Wlp KKB4sZ7adpl1ch2SQeTOr6vprGMwDeQ= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=XgJCNvo3; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.49 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=1675302143; a=rsa-sha256; cv=none; b=BVUHmw4wvqw61fB1inwOq+hUY9LfMA67bZYs9SRbBXqQA/LP5sNwENO91Co5rBEkqVndcs dqVFQYQvNm2FSg6ObJLCNI+8HpO4tLIBwhcsSBOgDiEzjxZi1D4sEggbHJEXPOtE+7A7bC 1dZAw6to8AoO0jL2tIJ2ik+enNjSj6Q= Received: by mail-pj1-f49.google.com with SMTP id mi9so502731pjb.4 for ; Wed, 01 Feb 2023 17:42:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HRKKHzUPeYRgy9sp4sc6HU9jmgQ/kndp4UTR0lBuLfE=; b=XgJCNvo3Mxgtg5Cm1ISnzALszmieUGA5g+mJmIl1lai7/r9rTtYkS3lVzxT8o3lN9P M07KKCtnz0xTxGyblruhALIg8QtCdiv0QIEPRfGk63fKr+7soH35NVOt9nlCiG6uZMI1 6C8k8MP6uMuCdf3W9QcjepEoOVtGmdI8Wp/s/Q9Z2mWDgX2AAC4YUXmgK4/NU1E5OFtY bm1PQVqxf9D4x3Wiski2pMGKHx1GH+/fEne7NjldfB6ly5ft+PiCb2uiJJ1G/VxOcJee wrj6uM4nSHqYrDuB9s+QK7xx9U+P75pCWOuCwgfEHRqrLxuxOo0FpP9kShHIcslp/3gK +WAg== 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=HRKKHzUPeYRgy9sp4sc6HU9jmgQ/kndp4UTR0lBuLfE=; b=8HgoDtbSbvMgFjXr0VaK2wHbN14h4KXpeN6nJgb+9Xapq9UrvlHn679oFVfGzeHf8y uBMLDnRa1G4uwGzuHIy2wuPWwGpBmoe5BF5eoCr7CsPcBCgDRtC0X9odZm7rtEC0tl8R p3n85RPEPUmMwYWd8ts5RMPYgoBYpwz6QxQH03/jkqanCrQpUwkCRex+6KYGwHJ1uuGl X2L5fXrtIAhqL9vo9VCxNDq5QgiTyex5OWVuSaqEwhucYgANYe25MBnViKLZJwW4HoY7 zy14zB19Il3OvA/573J4JhSRgDZbelES0E1rax3l1fyhVa12h8cNYG/z9qQ4l7SDZnLW zJDQ== X-Gm-Message-State: AO0yUKUx25ctkFCwbCcOWKp8E/569/FWLrsf6PW+GLGPUB9RxTZvLg5P xcFY0M3OkY1vyyJR+1NKM0E= X-Google-Smtp-Source: AK7set9GF2+wbpIKssdijZ0MvTlGQkES0Y8qB6dhHwsPA23hO7kBC462w0w9sb2JVm95BPkSBvmTuQ== X-Received: by 2002:a17:90a:1997:b0:230:3af9:177 with SMTP id 23-20020a17090a199700b002303af90177mr693628pji.8.1675302142358; Wed, 01 Feb 2023 17:42:22 -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.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42:22 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, 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 2/7] mm: percpu: introduce percpu_size() Date: Thu, 2 Feb 2023 01:41:53 +0000 Message-Id: <20230202014158.19616-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230202014158.19616-1-laoar.shao@gmail.com> References: <20230202014158.19616-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 60646180005 X-Stat-Signature: 9tte9pme3bq8mtfm69nryx9d7m5r7ain X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1675302143-641582 X-HE-Meta: U2FsdGVkX18B3nchwGrf0VRPZzEB6YJty8LwF7TJVMZOUFyI/8zCdz77Z3yKeIwJOA2EJGiqMNuqr03nlR3RggGPql3w8nFxyH19McqUI/3D014DgJQ8uSOoKIfYX7jd4PzSu3zpTd7G8+kqjZRkUEX/yhLyqx+SuszrihDylmAn2wCJvzo+XTixmfmuVT1tPr7MRQRULx7IsGMBuAwrGNJe0NmQ+qDSAVa9hvFEC0DS4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T8morIqRh4MqmAt32JLWfFFa7bkCCayzKHb42zMSf/mC1Eyu+knSshTMZV9fnUZ3MeCcvKe0sa8E7IN4usRqfT4pg1Fi7PizuM1vNrY5TW8vGe98kwlWbOO5Mx/Yw6d0/mA4+b6LKHUqmcH1pzyqST1WZSEqQtv2ijjglL5S1MdWRSSTnF7YbTU+B805Czucy+j6FhtLG1Dhim0sNVsELlxzXQ87Ep+pXQHImF0K3LHsj0K6+M7dqRX8s0E8hdwHA3qRKtd/KPuXxi1kdq9ZY8bVEN2qRJ5ku/HopTxII3wnS4W2b9gtpcTowXbq1kMihCYMoJ/Lj0sgZ5uKEvFGTUP0PJuboRSKMMsaA2lfqTBepWACKCWVdFj8fd7BFsK7OwUJr0yZZhj78C9a4ovc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce a new helper percpu_size() to report full size of underlying allocation of a percpu address. Signed-off-by: Yafang Shao Cc: Dennis Zhou Cc: Tejun Heo Cc: Christoph Lameter --- include/linux/percpu.h | 1 + mm/percpu.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/percpu.h b/include/linux/percpu.h index 1338ea2..7be4234 100644 --- a/include/linux/percpu.h +++ b/include/linux/percpu.h @@ -137,5 +137,6 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size, __alignof__(type)) extern unsigned long pcpu_nr_pages(void); +extern size_t percpu_size(void __percpu *ptr); #endif /* __LINUX_PERCPU_H */ diff --git a/mm/percpu.c b/mm/percpu.c index acd78da..5580688 100644 --- a/mm/percpu.c +++ b/mm/percpu.c @@ -2302,6 +2302,41 @@ void free_percpu(void __percpu *ptr) } EXPORT_SYMBOL_GPL(free_percpu); +/** + * percpu_size - report full size of underlying allocation of percpu addr + * @ptr: pointer to percpu area + * + * CONTEXT: + * Can be called from atomic context. + */ +size_t percpu_size(void __percpu *ptr) +{ + int bit_off, bits, end, off, size; + struct pcpu_chunk *chunk; + unsigned long flags; + void *addr; + + if (!ptr) + return 0; + + addr = __pcpu_ptr_to_addr(ptr); + + spin_lock_irqsave(&pcpu_lock, flags); + chunk = pcpu_chunk_addr_search(addr); + off = addr - chunk->base_addr; + bit_off = off / PCPU_MIN_ALLOC_SIZE; + + /* find end index */ + end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), + bit_off + 1); + spin_unlock_irqrestore(&pcpu_lock, flags); + + bits = end - bit_off; + size = bits * PCPU_MIN_ALLOC_SIZE; + + return pcpu_obj_full_size(size); +} + bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr) { #ifdef CONFIG_SMP From patchwork Thu Feb 2 01:41:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13125204 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 450E4C63797 for ; Thu, 2 Feb 2023 01:42:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA8586B007E; Wed, 1 Feb 2023 20:42:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D56AA6B0080; Wed, 1 Feb 2023 20:42:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF7B26B0081; Wed, 1 Feb 2023 20:42:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AECFA6B007E for ; Wed, 1 Feb 2023 20:42:31 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 812CBAB51C for ; Thu, 2 Feb 2023 01:42:31 +0000 (UTC) X-FDA: 80420652102.19.573BF33 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf12.hostedemail.com (Postfix) with ESMTP id B316D4000F for ; Thu, 2 Feb 2023 01:42:28 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=iv03dHdB; spf=pass (imf12.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.170 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=1675302148; a=rsa-sha256; cv=none; b=mnyvzRP7avM8gWrXOi8Ii1h40qNttszdz1pKpsohJCKYw+MJFzicJa8yAh6nHunWmgKJWX A8FBEmIdstrfP4n288kUv3W4/QQTTqhokEsnD4WrxhDZelTAAXuksTHMmOhfW+td5QVjLu dUaXdNX7t1Z2K6irv6Z9CAjdK+HhFzU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=iv03dHdB; spf=pass (imf12.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.170 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=1675302148; 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=JYjNCZTedk1AJoVE/LTBpwn60v8SrzPue4uHPDh5wcE=; b=oGuNXhYYVixSyfycho0YAEj1J7rXXp9nsz7hyBnH7tM5IL5HGw0NBk7noqZtv8DPVetG2M akshh/eaWMxuks8gVcINVDR/h1lGP0e1ivRa5Q0wFa/WpLxeQFRpSZoPhfiefzxEXmjjuU zEOjv7/0miVTktdzXeHVux+feVKE5ng= Received: by mail-pl1-f170.google.com with SMTP id n13so361300plf.11 for ; Wed, 01 Feb 2023 17:42:28 -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=JYjNCZTedk1AJoVE/LTBpwn60v8SrzPue4uHPDh5wcE=; b=iv03dHdBcW/AZOHUDlopVCkZMKFM5RQoGCY65iwL3v1XSSn6aFyN06IJDrI7/Y1dPh CaFdxSXVVjpIPBxknyOfU6wiAXoOpTQA7lbpm7JbzsU7X3rmtw9j/uDnDsZC8p37KAOl T/651l6xryaOqGzHBqD/t2tGccpWOjcNcUV+37GFHt59GhVhWtoCFFWYkEDTdq+EfD1B nVdqLRlenGwkWXLqu5fy40pZ2DGJG04NOPHli4XhXuvWujUoeJyQU9rSdmhiMvX2tS3i jVmpex4Sk2c0aoGC+RK06Pwp+M0a7xsu2yZ4brDKq+6NUq/LJ1nCbzhjhvPG43MnxO94 5Pvg== 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=JYjNCZTedk1AJoVE/LTBpwn60v8SrzPue4uHPDh5wcE=; b=uHe6GOcSBfmWAMT1KWroc7iBY+kUX0r5camWlujq5pBCFIL/zEI7GtsBtva6DSHafR tUhMPF7xG+jRtugAesHymAG7OiKOb4cXWrLggjcyHhGoR6uCdprNsSbmuQmFjJzgjH4e 3JZa6+wtKrJOJqrJNcYi0k0N3Qfk+yhMoiwRnYhtbfCboel6R/DthOJc0KYllkJOjjBE 9r/25CwTbpi3Yq/r8jftvWmqBUDEk9W2hc7KTBI2s8DWyzyIHGTql376IjL/cFwt9soJ I83mVMwGXdWPnNN410hO7wFgz0w/KoYoxDLWIr4WRoPTQZ8gK+9Pi3GDTVP/BLNGP5T9 UCNQ== X-Gm-Message-State: AO0yUKUaa7Jb15UrJma9WIRsCcZ6VUKo3dHLHu78BE7Ps00+P5FaRe+a Z08jV/7zKNlL0f+QI6YNQYE= X-Google-Smtp-Source: AK7set9JBuOshDn5GHDps2tnszs7MZJQNL/kb7SOm046DT8cAbZupwyNErHhHuomwxoErVIBH3XDkA== X-Received: by 2002:a05:6a21:33aa:b0:bc:fe7e:cfd9 with SMTP id yy42-20020a056a2133aa00b000bcfe7ecfd9mr6068166pzb.18.1675302147552; Wed, 01 Feb 2023 17:42:27 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42: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, 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 , Christoph Hellwig Subject: [PATCH bpf-next 3/7] mm: vmalloc: introduce vsize() Date: Thu, 2 Feb 2023 01:41:54 +0000 Message-Id: <20230202014158.19616-4-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-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c+Zobb aYNjfmeR Yq+6WSam+A4C60hPIofzd4DBqH4cXprwHujkEQSMWphx3v6HI1pwDrW64dSj5oG6vA1alGkhO5Du3yx4JqWw+ZJy7sKJ1qWig2OusfxwRJ+4GDOm5ovtPqATP+T69NsyGAV0gw2EF4lMUXTZj34NQ/wnzeo0DTVR+OsHuyQkQjL6RTZ/9yD/TbHM9TdIS9jTULinYxfMrBKPdwezvSx7spQtaokfGgB04Mm/SZ3qvAvu99R1dsw4DbRxGTb7dzHx8OlnWH9a3uP6dOcImI8iox3E1hba4meobTI40jm+xhqjJxDzvmuioAWplphTnf875/lhcFFxFuUKiKTUSUywiY3bgV00+PKeDRrS30ZeRFXjE6DE00pi1qhZl20oFifMvSwqQIZ1EiZZrSlF1dpwZ6DrSz7kJZO8ISHpsMGRnBiUQG2iehUAafIzkiosQmMVUJ3yzzGU/cLpJLICllBrTEwBBu0Leo26Hb0qWwDdSFYw9ELJNtn0KN+kkRTNXjO343KBjJqaQOweJUOHxVprNdknkQxaxBjHbN/qixJJIlgO/cGxB/1aomHKUwv+9oCqLbEA/mhKy+joPfInE3dswKxiKMSZrcwLLfdZPVz+GsLNTpKM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce a helper to report full size of underlying allocation of a vmalloc'ed address. Suggested-by: Uladzislau Rezki Signed-off-by: Yafang Shao Cc: Uladzislau Rezki Cc: Christoph Hellwig --- include/linux/vmalloc.h | 1 + mm/vmalloc.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 096d48a..7fbd390 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -297,4 +297,5 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, static inline bool vmalloc_dump_obj(void *object) { return false; } #endif +size_t vsize(void *addr); #endif /* _LINUX_VMALLOC_H */ diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ca71de7..8499eba 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4057,6 +4057,23 @@ bool vmalloc_dump_obj(void *object) } #endif +/* Report full size of underlying allocation of a vmalloc'ed addr */ +size_t vsize(void *addr) +{ + struct vmap_area *va; + size_t va_size = 0; + + if (!addr) + return 0; + + spin_lock(&vmap_area_lock); + va = __find_vmap_area((unsigned long)addr, &vmap_area_root); + if (va && va->vm) + va_size = va->vm->size; + spin_unlock(&vmap_area_lock); + return va_size; +} + #ifdef CONFIG_PROC_FS static void *s_start(struct seq_file *m, loff_t *pos) __acquires(&vmap_purge_lock) From patchwork Thu Feb 2 01:41:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13125205 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 35CD3C05027 for ; Thu, 2 Feb 2023 01:42:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B83A36B0081; Wed, 1 Feb 2023 20:42:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B34D06B0082; Wed, 1 Feb 2023 20:42:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FCDD6B0083; Wed, 1 Feb 2023 20:42:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 90E676B0081 for ; Wed, 1 Feb 2023 20:42:35 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6D741140DB5 for ; Thu, 2 Feb 2023 01:42:35 +0000 (UTC) X-FDA: 80420652270.21.9DB21FE Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf04.hostedemail.com (Postfix) with ESMTP id 940FD4000C for ; Thu, 2 Feb 2023 01:42:33 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=eXFJpIeX; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675302153; 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=n3aayDdIilVnW+ECMQQHHSm0J0IUJbFKBuiVmS/NUpY=; b=A2AIgUzqgKJzNxNHqGftIgD+3bccg0ps4BaAxWiRkS2KhVVkeRMKs5GJVmyg+hd8Dv5Yj6 NzkXFmIsVG20MAWJYs/m+pHKBEAgQP9ztglUq0U9HzcFrOqFk38f4Qy+0XGQupza5hgJ0u LEp8/BDfgsaceJW+HEQ/9MQUE5qmKqk= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=eXFJpIeX; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675302153; a=rsa-sha256; cv=none; b=bdCxvCkkzxh4Cmr6ztphqVaEr949ZH8hFuLup+2AbFH604yc3kzbhUPBabAj3nzva8iTmY neGOoSuNUnd+w8BThWJEWqdTvhZPW99WA4uhraEJTBs8IHxT8dNwwwA3LPjI91VexobPMB suJKl/O+MhuA/pU6x//mD4vJ2yOYpkA= Received: by mail-pj1-f49.google.com with SMTP id 88so505938pjo.3 for ; Wed, 01 Feb 2023 17:42:33 -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=n3aayDdIilVnW+ECMQQHHSm0J0IUJbFKBuiVmS/NUpY=; b=eXFJpIeXYVUSPivkytSVbl5Cx+bVouw4PGsZHvBMRjAggVZwRnnFxImsPUSa20NFrE iQaQCSoHR1+JgPHA480wcfU17dzZ6o6TjbyiXSVZ5mNF78+C+MWigkULQtnu04SPA1+u cg6adFij+OS202omImD6F+tFUMNBlxcb9Rp9mqPPGl+5JGMzZO6SjgnQWEHtHToUrhWn jIGii/ALcIlvUnzD0Exx3izAAq1GKvujASyQhprQEQbd8Rk8OCjePs9KmtHRSEvOtp+e jU+hziToxxaaSZYcyDEDGzDbL20GWelRB54kK2WL+32kFgH54EfHI751dKUP6YwQSVl5 nrRA== 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=n3aayDdIilVnW+ECMQQHHSm0J0IUJbFKBuiVmS/NUpY=; b=FkqpBteqA8y25uFlD++yY00r7z/WYT0rvHa0l5caB9fizuLXQw6WKJ/YafBWq1Ga8/ Uup0bn/EMVdwFn2zSiWhKxJEKml1N0lX7vrXgM4QtmYU44hTLw3p9pApsOsuIxTuORK8 f+IjFQ60tUghmfccjgJLroVg2TIxIlaAJE/xGG9ZcK5xsyIsnwu5dDsUq386h22ZIzQc XoJ+GNp/xBo7l9A5AU/B+z+Q+sgPtabiYAP7XNGzwpEXPxCKI9K41pN3cB+LqXG8oqQY x5lFrBNNKyJr6hg7FEzoMaP9fvphyLQvYGIExS0Reb6Q3xvENXxuEbUuhhj3ydAearva CyLA== X-Gm-Message-State: AO0yUKWCyHoenVZmDkA2MGetr/ttU4cMPSj6Wk3FBHIeGXC5CBc2iVpp hPp4//QAbTrzuLy8eEw3EfQ= X-Google-Smtp-Source: AK7set+dNLy+KLgiWVd1XMiLsrgHNgux7c0+iUY8ZD/JtU9eRdmRWRIuCrfznCo3Dy7IZq+ZnnpkmA== X-Received: by 2002:a05:6a21:3990:b0:bf:8840:ffd0 with SMTP id ad16-20020a056a21399000b000bf8840ffd0mr1013419pzc.29.1675302152581; Wed, 01 Feb 2023 17:42:32 -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.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42: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, 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 4/7] mm: util: introduce kvsize() Date: Thu, 2 Feb 2023 01:41:55 +0000 Message-Id: <20230202014158.19616-5-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-Server: rspam02 X-Rspamd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gU42yP1uIAdxoLUax00+ccmC5RjOnMBONZ0P0IK+dNp1Sqrurhn4EjqpJ6i9w9o6O1jKIotk0b7oEFtgvLnnPePszjeWlnIqyBOztcyqkD9MHJ0u6J1KleLAVk1tLPPdbjmca0NgaXh7gG0vrHQmgMxPizw4mcn9IZr8wB3YkZwuoVBqI+STYEHQa/v2as9rtVv/8U5iDYBmt21ti2PYHO8i6zc4XAuDWCFh8SF2osLKDi9b8vEAx3IQPibGnvdvEa5tne83c4QQ8jLTu5Ec56s7ssXW6coYqoW4bk1xofkelbAS3zxHGFT1ol4FjuerXgUg0FbPFhz9P1wAUgawdzVfnNgnFvodq5ojcy2D2ukbJSx1hskTgWrz7ZIhM5ypIjrRehog0OgxzcDKn2smlM1dU/aAqzkuwNvspTfkCa4XxGpvTK8+lqPxUYK0vYJzFqXgfS6AJ/28dJC34acmKNcjC0YRJvBasywCBQQgdERAUB6Xq5nqqqztcnwL22UHFj28KF6mtwJClT1VOFV0rnSTrKaBR8EVsi1EJeLlXpQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce a new help kvsize() to report full size of underlying allocation of a kmalloc'ed addr or vmalloc'ed addr. Signed-off-by: Yafang Shao --- include/linux/slab.h | 1 + mm/util.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/linux/slab.h b/include/linux/slab.h index 45af703..cccb4d8 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -762,6 +762,7 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) __realloc_size(3); +extern size_t kvsize(void *addr); extern void kvfree(const void *addr); extern void kvfree_sensitive(const void *addr, size_t len); diff --git a/mm/util.c b/mm/util.c index b56c92f..f77d0cc 100644 --- a/mm/util.c +++ b/mm/util.c @@ -610,6 +610,21 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node) EXPORT_SYMBOL(kvmalloc_node); /** + * kvsize() - Report full size of underlying allocation of adddr + * @addr: Pointer to kmalloc'ed or vmalloc'ed memory + * + * kvsize reports full size of underlying allocation of a kmalloc'ed addr + * or a vmalloc'ed addr. + */ +size_t kvsize(void *addr) +{ + if (is_vmalloc_addr(addr)) + return vsize(addr); + + return ksize(addr); +} + +/** * kvfree() - Free memory. * @addr: Pointer to allocated memory. * From patchwork Thu Feb 2 01:41:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13125206 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4EDBEC05027 for ; Thu, 2 Feb 2023 01:42:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC09E6B0083; Wed, 1 Feb 2023 20:42:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A70716B0085; Wed, 1 Feb 2023 20:42:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9380C6B0087; Wed, 1 Feb 2023 20:42:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 86C4F6B0083 for ; Wed, 1 Feb 2023 20:42:40 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 63363C0D88 for ; Thu, 2 Feb 2023 01:42:40 +0000 (UTC) X-FDA: 80420652480.15.A8D0B8C Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf14.hostedemail.com (Postfix) with ESMTP id 8F38F100018 for ; Thu, 2 Feb 2023 01:42:38 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=l3QJYqqB; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675302158; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lKQM5vml6SlvXVBphY+FPvXDlacnwZjaLK4//iP66Rc=; b=5P0IVFr9iIJ1yPtLmvrwkrhIu6WariSnPmBxk7y91AQBDX7ZVTKFcSQAkItb+FbnnPOCgm EWoQyA1Ft+Ahemms6Mxdl9UTdCH5v3tzzmWz7cnbJa0i4dp4zL+N109ka4+Bjatdek2yHG EI0exZzWD1XVKQndqalewITsu/KxLac= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=l3QJYqqB; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675302158; a=rsa-sha256; cv=none; b=3o/w0Rs9TfDAca9zbKTH2jPS9ZJxbwO65XC8/k7DgaOZM4m+2xhfQZSAnRkx31pZAQQoSb JHf+UqwYzW4JjBEUgpD9s7NBvwRrYZT1RmjrKY3S5uf69sCEhGv0MzibINnWFayUV3HKNR 9Foe7mMirIzweUCUj3UoMTglr84M4qk= Received: by mail-pl1-f171.google.com with SMTP id k13so431046plg.0 for ; Wed, 01 Feb 2023 17:42:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lKQM5vml6SlvXVBphY+FPvXDlacnwZjaLK4//iP66Rc=; b=l3QJYqqBg/FLpgLF3w9RJzT6LCxhxV+XkCfXCmvbcqX9dmtvv9gqm2MxgFHNi3KsIH L+1/Jzgydg9Pvnr7vXfwlJEXyt4Oe1rE4kn05ybn6xcyMfcQMRzN4eG0/xL7461EBLbU vf8jElG3ItPsPUFX6RfXXViWPo74PSCLHWNs+XuIEBTlGbqhkMQOkJwZ0PF3jG4JRuIe i7wvYJRzchy9EPmYoVbqXKwf7JSxNNIoB43hXNxIn/Q4QNrIUw3Vxkf18pSPBnzibW3l 6u0M80ydQuUtrqXBFb2K8aWPsqAn+/XoHwWw7L3Uk3NbQoeBfk+DJU4Lww1vd2yFr3da kx9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lKQM5vml6SlvXVBphY+FPvXDlacnwZjaLK4//iP66Rc=; b=c7Z/i461o4KVtMRSiVdZj8qHZhKqMZa3gBA0Tw+WZVkXMP93frGfBXEltbo8uJo7gB 8nL2K/ZD/F0GzZFd32vcLfzLOBxnBnGLhS644/58qjwD8CRd8t1zIbK6C3HBD/VyTFTN X2ljXm9n6NQB8LNxrZ1vZW1/OSi17e/hUA4ay2DqjynD98gty6H9Geg1gN0u5gUHI1wy e/51vy1ghM23Ux3mzPveSo1246/gVQ4JLWZvTP730eF13zJhjgQUiFWxhPryloz7mWOX q7oTyL/8dq5j/5pZAyFptgFVQMLKQKuRSMI3QFLpCW9PBxuEj6xsSLTALliWdYGfNpyK losg== X-Gm-Message-State: AO0yUKXWRCKFvfm6aWqer4pQDpWfq/I5PF1xWRq93sczRSIn5dVBZghz UwM79jdad3jXGiz6DpqMAms= X-Google-Smtp-Source: AK7set9ELt2JOyjgVr4+8gniE1xArw7bOYKUK5RSQApxYJDzJ0/kzRDbUTtaVk55GHthACef4ci2vQ== X-Received: by 2002:a05:6a20:1603:b0:bc:2665:cbe6 with SMTP id l3-20020a056a20160300b000bc2665cbe6mr5916806pzj.5.1675302157573; Wed, 01 Feb 2023 17:42:37 -0800 (PST) Received: from vultr.guest ([2001:19f0:7001:3f48:5400:4ff:fe4a:8c8b]) by smtp.gmail.com with ESMTPSA id t191-20020a6381c8000000b004e8f7f23c4bsm6594205pgd.76.2023.02.01.17.42.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42:37 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, dennis@kernel.org, cl@linux.com, akpm@linux-foundation.org, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, vbabka@suse.cz, urezki@gmail.com Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 5/7] bpf: add new map ops ->map_mem_usage Date: Thu, 2 Feb 2023 01:41:56 +0000 Message-Id: <20230202014158.19616-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230202014158.19616-1-laoar.shao@gmail.com> References: <20230202014158.19616-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8F38F100018 X-Stat-Signature: gat3acq16metbg6bydtzwgb6kjbr9tjs X-Rspam-User: X-HE-Tag: 1675302158-845551 X-HE-Meta: U2FsdGVkX1/sNEIfH6u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zIvqUGwxIFlNEhuGpVk6C33yDlkhH0LyoHzu7hRQmyeBx03O/NLN7EwiTLA0l94HCB8G6kC/eFZ9Jpji2cCmfNt9jVqEivXLfdup6U7ln1CWyZUpvON3mtPRW5Z5kGwckLQd+RpgMgT5spwpMkTJDRkou5pyZ0rtccgJrog3+E2IlMY0GSSYqJP0qF9I270fuRmx6DAx9nyredyiDmgsuaHCWvZVKEO3629QdbkOCmhvaEENLUpBhO1KHJXepkSYXp3AKDS6N0gOFfrZYTC1oHk6pZMGY9nIw1NeoHfyz2dHg8+QsXoVfuM4/rua59Mzgv1VsPLQuDWHpf0BogXZ+xoEAd/Jdyu+5XdbTigo2HmtD+30oAhXOjfSMnJhhyQDwspPZRTgB2hta5JihvIuP4eMlvGnhD5KX82boZGYxTzfDzwnNDLDEj6LDsmUWxr8jWQgMo9dmrSBecu9gCQHi75uYz/1edtZ4Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add a new map ops ->map_mem_usage to print the memory usage of a bpf map. ->map_mem_usage will get the map memory usage from the pointers which will be freed in ->map_free. So it is very similar to ->map_free except that it only get the underlaying memory size from the pointers rather than freeing them. We just need to keep the pointers used in ->map_mem_usage in sync with the pointers in ->map_free. This is a preparation for the followup change. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index e11db75..10eb8e9 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -160,6 +160,8 @@ struct bpf_map_ops { bpf_callback_t callback_fn, void *callback_ctx, u64 flags); + unsigned long (*map_mem_usage)(const struct bpf_map *map); + /* BTF id of struct allocated by map_alloc */ int *map_btf_id; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 99417b3..df52853 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -758,16 +758,20 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) } #ifdef CONFIG_PROC_FS -/* Provides an approximation of the map's memory footprint. - * Used only to provide a backward compatibility and display - * a reasonable "memlock" info. - */ -static unsigned long bpf_map_memory_footprint(const struct bpf_map *map) +/* Show the memory usage of a bpf map */ +static unsigned long bpf_map_memory_usage(const struct bpf_map *map) { unsigned long size; - size = round_up(map->key_size + bpf_map_value_size(map), 8); + /* ->map_mem_usage will get the map memory size from the pointers + * which will be freed in ->map_free. So it is very similar to + * ->map_free except that it only get the underlaying memory size + * from the pointers rather than freeing them. + */ + if (map->ops->map_mem_usage) + return map->ops->map_mem_usage(map); + size = round_up(map->key_size + bpf_map_value_size(map), 8); return round_up(map->max_entries * size, PAGE_SIZE); } @@ -799,7 +803,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) map->max_entries, map->map_flags, (unsigned long long)map->map_extra, - bpf_map_memory_footprint(map), + bpf_map_memory_usage(map), map->id, READ_ONCE(map->frozen)); if (type) { 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/hXAwjmF6R9NvlmIsxh5eAfqP2blc4j9BkCqmc3UAbSCJRaR6tI6Pp6J9RCOIcjR+xsEC2LUsBLzLlWXM6oMPsIgYoJzYNBeZuokkwIr6Syd9MlmVfhw3533/lJ4izfzBIEADvMBz1e8sulytCoAuRs2rV9UR2DNArKMnb2uCCuRsFRMTMgDCbtRd4tSQCLVa1mRSyN5iUnsDU46DaZ8iMzDnb6LWGvP1WN49hTNxLWQNbcPdvrjHOW29A0iHMV4jmn7NyvuyUU0LY3pNZrSuKg2+01QORsBIlXX6dnS2Bz7wfr7SoHzGGSu7KO0Nmqwjep28pP0/Q+hsEWFKrG2C9MP/CQFheL7qyfjEtvSRTHPYjm9nBMiEg8uejDq3c4/wA3cD2q8V3UR/0xNE7rqKOgx+omVenWALUsDRs+PqZbQ8/7jVtlHVLh4BpkbR6hm8U3ZkLayIPdO5E97aMvLS560xYDtbDtjBKfhgCHmg3+a1qHzUbGNfoBPgPSqabrkO/zDV2+vRhk61V4duX5RcFl+OaFte82alJPMsHdL2u2jcB/odveZEcpVkwjiYMjdZ3nUbir0VF5itQ0gWRORfJ2vATK7QUmPMRcOBbM5N8FrIynVG7Bzr8krTjda9kVCRCtWOsmSq1pxJ4Vxd0xDPjphdVGhMhm+EXFrHahSM1vJUgjZz39UuFZAhRQ/+RQkLVVGCwd7T2woxOHoTUqOgKH17sAC u5PQiUY2 tZSy4z3j2fF43x/DAYw2JX/28Cmh1D8SwXSHQzrgWqmfXKLbWciRuqbUUO7/j+1XIsCwL4qmbTPj+yf2dtV0IRTB5HPnm9ZW2A+A9YkKvvMgq/Ksgt4m+ejcAh+3LcIdhXzcjPYROvxC+D2AEezJPznIrZdf+K8u9MzznPpqPJUes6xfLGHhUaccmctssuwhG+VzUdMOZCVn7BnQmygGs3fMF/f3pD+5CLDBC0tlBNRGLq8g3MKmUndnPQGid/VnUMqMov8bMqWBMv0xmITCFY2fHJz05Exy+WkNDLWKg7SPwmLOrFoBADJsKXMFa31iAsxBaRHuTHSywxha2LgTNL9k/ZiHH5sWLHifXrC6OIQzhCKpKbG8thrcatmdooYWlsKGuagcEUFB9fXQ4yCUwmniITKZkroxZNuVB7vV2IkQvpVu+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; +} From patchwork Thu Feb 2 01:41: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: 13125208 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 44200C636CD for ; Thu, 2 Feb 2023 01:42:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCD056B007D; Wed, 1 Feb 2023 20:42:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D56946B0088; Wed, 1 Feb 2023 20:42:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B82A66B0089; Wed, 1 Feb 2023 20:42:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A770D6B007D for ; Wed, 1 Feb 2023 20:42:50 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 837291A0C31 for ; Thu, 2 Feb 2023 01:42:50 +0000 (UTC) X-FDA: 80420652900.06.36FB999 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf07.hostedemail.com (Postfix) with ESMTP id BAF8740007 for ; Thu, 2 Feb 2023 01:42:48 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=qw4zWHEG; spf=pass (imf07.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.42 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=1675302168; 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=IlsPDQQrn+oExLAwKU+9a981T51GRXTZu9kwUUI2F68=; b=FMkIt/G9nM+M8qV2mu9OIPAZ91t9MILCqBWFd6AtiQzQkm5YvZCGkSWpnet8RptOF9KyNl 9O/wnNjWo+I2+uoqoDJmbjp1kIfACdkul+NSPVxaaWlQ6VU5aRsMjECGDpYa7aMxBixD4o xG8Pf44K7jYsbpYaDPH5a0deImDJcFM= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=qw4zWHEG; spf=pass (imf07.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.42 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=1675302168; a=rsa-sha256; cv=none; b=rhinQce3QgmsR37xyhIJu/TYqyfinJ9TkawikIUjP2lm6KKxoJRuO9RuQRMQCZCJvi7YGr WhiHDLkSdxuxJ+NYpvqQexYn2fUIIOwFComl/4J8/rZVMvX4FHQcmeZkxaM+iKxzYijLId OkMsbeV91cMluJ2DDKJ+OVR9SyKq8YU= Received: by mail-pj1-f42.google.com with SMTP id t12-20020a17090aae0c00b00229f4cff534so4600159pjq.1 for ; Wed, 01 Feb 2023 17:42:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IlsPDQQrn+oExLAwKU+9a981T51GRXTZu9kwUUI2F68=; b=qw4zWHEG5rlxLuRA2Mk2zQxmQoWx+8vAJfsjWhcoXTLwNOskwzmVzveBoO/Ik8mj/K jYB40qB1HcmjfgW4nXG7OjV/04e3FIRK51MW/KIInIHJEk0gTzEruSbVfzBqCj0+BiUm A4jK+3jrJFm1TCQvZF4dDc0Hsr3bf+i2XLlq1IWjcxdzswxaf3xPKKRBt1mT5zyO29ep 0rbTQVBit/K5XjnUmlEiG9J8i37h23wW7+oTDwbuVwt7QV8z4O43KsfzUKSHPq/6HiHS Ug6SgpdJ17MIdoCo8XPN3c51YsQgozW6lYKoWgLSp2jmPM9kcpakKcbGoAantIgGh4uQ iC3A== 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=IlsPDQQrn+oExLAwKU+9a981T51GRXTZu9kwUUI2F68=; b=ond/aud/rO4xqnFOKTEd/Vue7NndkXL9cVvAzSbCxFKd5a5HvyOwuRFIqsYkuYNUdL qRFv6cfvLFvZw1TmO1DJ9kiQnZdeW/BniqA2NYUI1g/evaW6RyS27NPqUksDlEAjDaRB 21RGXCgrBw78QLlKmg30FqmCtsLCz9C50BTZG0sf1ZI8U07jrsLS2qIeGt/+gPzzKEr5 Vqp7YLkH4Y68vNsNxK8bpgMvuoyt/y2tgbfrq90FijlyxuysLs6/MUqfyfrLt6H/dFrB EM4Xuf8+KHruTnL68qORgpkkIS95NAlJ1RGebo/5+TBwi08LRSYhTEpNh89FzgIkteGg 3IfA== X-Gm-Message-State: AO0yUKVFTdd4VSdPl1yChOmoqjdjdWsJ+Awy81LbeMkMXC08UbQ1stte zUpJdXBFhiEX1bnhct1rTCk= X-Google-Smtp-Source: AK7set/OuunuF82xfnkKr4JO7JphAerYxqJCUhDt5Rs18OlwYb7xCNJ/ZlBTM0ydmhVnZGVpcgln1Q== X-Received: by 2002:a05:6a20:158e:b0:b8:54a2:b8d1 with SMTP id h14-20020a056a20158e00b000b854a2b8d1mr5750054pzj.57.1675302167711; Wed, 01 Feb 2023 17:42:47 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 17:42:47 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, 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 7/7] bpf: hashtab memory usage Date: Thu, 2 Feb 2023 01:41:58 +0000 Message-Id: <20230202014158.19616-8-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-Stat-Signature: 8zz5cj16s1btpnbdq9i1fz6nae4pmdux X-Rspam-User: X-Rspamd-Queue-Id: BAF8740007 X-Rspamd-Server: rspam06 X-HE-Tag: 1675302168-505164 X-HE-Meta: U2FsdGVkX1+rcuLdtgsouEx9Qs/WIwfnI6crvodnQf+AvT73mRdCh/yBnazafO1wMgW6JuZPbGUjB7deHEDNrfHWT0E7uJdyGGX8sbJat8JV/4kVvlYlHZuJk1yPDu9I1AnD13TLC6BIYZe2VtrrZRHLd26uFF0q8b4ug2RjIXllqAlsn2q7g8egEbL+lDhUzZ4iBEvzji9GbxsmGuOjU8XKFgDdrdz3sIq4Yt9uRBYf0rB4BOSuH2DHS7gIyWjMSPZ2e9GMFnCcaBDQ8/Uuq85vdIXVCztC0BpSw0MU+Ia6kzcsIegFz7pw3UTZ/6Srk8DYd/Vf75fs4417UJ2lvFZ9jPEghwG2D1jt563HrtsVpWj59ASZjUJMMfz+w2YvPXfRpyHXSNX5+LTk7L/5Cn+IR4IfjY+kVWrzkTA0Pq/ZFoZ8OuI8SqUqfjOrj4Vs4f2qFAxsVshpiPxEGUtXXihswlC39JaZrUu3yZHVFPZ+1tD05Fl40S1Fs6ao+Rx6NVF2e2lyF7xpjoyL+qo+zcFvqOBYu71+XrE48bR+mzPHvH0Fxrt1Hu9VggEwWSFIp4XDS5f6T7kJ1S2tQS6vn53LZM1uMKPCqrLdPOoWRHqMg/4UMedraIvc27PXnxLWXKwEVRcHsp5ZNWcqwKOrLmsypUC+EoKEPjD9NBmdc0k6OA5F7SpdN5LTLVhrfVaCqGvpqMEBYjb+Yxom3IQpPIRp3VunfWtSlHvOiN72ZjifLgwDzGiFrN46Rp7B4FJmTvW1JJqWwTXRYxuLZCzlucTlxoYNGCbATVsn1d61I2BoU3gVJVE6BQ9tnTSjGGJ9eSsSyVK7i9wn8XjCwPRpY/wKO6kDMxxKmiF/QiLTiiRY/+Y4dxor41sKXrA1BvD4N6UA3FVL98Or3iXOMkpsv/B5IBlTEqLqVHVV1HE4sgp20Wnz/O38fmJC+dRPvZ8coIYVzXoZZ3D+RnJE8cV l1FBccFs DW7RsqZq7kpfakLH7Im4w9DD52jLKyiY8iqPOfz5PO7UsDqffDzPCS7Nr/FfNkiorpONHfbifq89s7rcjSuzzeTbbeerLvRuMSbud30U+qQBHiyNfoPX7EUbfgszjimu162gY8CeobeZ4sZ3ZBUM59OGIsnfa035+FmqELvdL/AE0LR5zdyczI/3PEa/nh29JO8UjR74UBgjC2SSDEdnL9CYcvH1dRyosjdQCElia8VXXE6hIRzu8JJKJUWwn3FnIpluF2YiHAb70+UEwOlVu9YMU5mmcal1TPdJf2+gghPKKllIyGS546toq0rICeSDSuP79H1wd83uy45bKnSXLMrbAwmIU9BfoL4rAKHBJWIxPai8W0d3H+BZ3/heVwTkc4bs/XJMhWlm+szGMjQ5X8+nVKsszDYiyD6wQwqzdqb0vXvOL7jAIAnwt4fx2/btAPWZUh6edGEzK3rtT0AuOlvv2qkWwBMizP+cB+wDPwHqjfCfF4snB0MAlRmaCeI7Xsil5A9mT7JBHdKiUisnxpIxofhvm8tqYA6Pu6+BMqK6ugrw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Get htab memory usage from the htab pointers we have allocated. Some small pointers are ignored as their size are quite small compared with the total size. The result as follows, - before this change 1: hash name count_map flags 0x0 <<<< prealloc key 16B value 24B max_entries 1048576 memlock 41943040B 2: hash name count_map flags 0x1 <<<< non prealloc, fully set key 16B value 24B max_entries 1048576 memlock 41943040B 3: hash name count_map flags 0x1 <<<< non prealloc, non set key 16B value 24B max_entries 1048576 memlock 41943040B The memlock is always a fixed number whatever it is preallocated or not, and whatever the allocated elements number is. - after this change 1: hash name count_map flags 0x0 <<<< prealloc key 16B value 24B max_entries 1048576 memlock 109064464B 2: hash name count_map flags 0x1 <<<< non prealloc, fully set key 16B value 24B max_entries 1048576 memlock 117464320B 3: hash name count_map flags 0x1 <<<< non prealloc, non set key 16B value 24B max_entries 1048576 memlock 16797952B The memlock now is hashtab actually allocated. At worst, the difference can be 10x, for example, - before this change 4: hash name count_map flags 0x0 key 4B value 4B max_entries 1048576 memlock 8388608B - after this change 4: hash name count_map flags 0x0 key 4B value 4B max_entries 1048576 memlock 83898640B Signed-off-by: Yafang Shao --- kernel/bpf/hashtab.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 66bded1..cba540b 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -273,6 +273,25 @@ static void htab_free_elems(struct bpf_htab *htab) bpf_map_area_free(htab->elems); } +static unsigned long htab_prealloc_elems_size(struct bpf_htab *htab) +{ + unsigned long size = 0; + int i; + + if (!htab_is_percpu(htab)) + return kvsize(htab->elems); + + for (i = 0; i < htab->map.max_entries; i++) { + void __percpu *pptr; + + pptr = htab_elem_get_ptr(get_htab_elem(htab, i), + htab->map.key_size); + size += percpu_size(pptr); + } + size += kvsize(htab->elems); + return size; +} + /* The LRU list has a lock (lru_lock). Each htab bucket has a lock * (bucket_lock). If both locks need to be acquired together, the lock * order is always lru_lock -> bucket_lock and this only happens in @@ -864,6 +883,16 @@ static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l) bpf_mem_cache_free(&htab->ma, l); } +static unsigned long htab_elem_size(struct bpf_htab *htab, struct htab_elem *l) +{ + unsigned long size = 0; + + if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH) + size += bpf_mem_cache_elem_size(&htab->pcpu_ma, l->ptr_to_pptr); + + return size + bpf_mem_cache_elem_size(&htab->ma, l); +} + static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l) { struct bpf_map *map = &htab->map; @@ -899,7 +928,6 @@ static void dec_elem_count(struct bpf_htab *htab) atomic_dec(&htab->count); } - static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l) { htab_put_fd_value(htab, l); @@ -1457,6 +1485,31 @@ static void delete_all_elements(struct bpf_htab *htab) migrate_enable(); } +static unsigned long htab_non_prealloc_elems_size(struct bpf_htab *htab) +{ + unsigned long size = 0; + unsigned long count; + int i; + + rcu_read_lock(); + for (i = 0; i < htab->n_buckets; i++) { + struct hlist_nulls_head *head = select_bucket(htab, i); + struct hlist_nulls_node *n; + struct htab_elem *l; + + hlist_nulls_for_each_entry(l, n, head, hash_node) { + size = htab_elem_size(htab, l); + goto out; + } + } +out: + rcu_read_unlock(); + count = htab->use_percpu_counter ? percpu_counter_sum(&htab->pcount) : + atomic_read(&htab->count); + + return size * count; +} + static void htab_free_malloced_timers(struct bpf_htab *htab) { int i; @@ -1523,6 +1576,26 @@ static void htab_map_free(struct bpf_map *map) bpf_map_area_free(htab); } +/* Get the htab memory usage from pointers we have already allocated. + * Some minor pointers are igored as their size are quite small compared + * with the total size. + */ +static unsigned long htab_mem_usage(const struct bpf_map *map) +{ + struct bpf_htab *htab = container_of(map, struct bpf_htab, map); + unsigned long size = 0; + + if (!htab_is_prealloc(htab)) + size += htab_non_prealloc_elems_size(htab); + else + size += htab_prealloc_elems_size(htab); + size += percpu_size(htab->extra_elems); + size += kvsize(htab->buckets); + size += bpf_mem_alloc_size(&htab->pcpu_ma); + size += bpf_mem_alloc_size(&htab->ma); + return size; +} + static void htab_map_seq_show_elem(struct bpf_map *map, void *key, struct seq_file *m) { @@ -2191,6 +2264,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_mem_usage, BATCH_OPS(htab), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2212,6 +2286,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_mem_usage, BATCH_OPS(htab_lru), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2363,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_mem_usage, BATCH_OPS(htab_percpu), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2382,6 +2458,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_mem_usage, BATCH_OPS(htab_lru_percpu), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2519,6 +2596,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_mem_usage, BATCH_OPS(htab), .map_btf_id = &htab_map_btf_ids[0], };