From patchwork Thu Jan 12 15:53:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13098269 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 6FFC2C54EBD for ; Thu, 12 Jan 2023 15:53:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8E498E0007; Thu, 12 Jan 2023 10:53:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B1A188E0001; Thu, 12 Jan 2023 10:53:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96B0E8E0007; Thu, 12 Jan 2023 10:53:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 78C798E0001 for ; Thu, 12 Jan 2023 10:53:41 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3C619140616 for ; Thu, 12 Jan 2023 15:53:41 +0000 (UTC) X-FDA: 80346592242.26.E9B0392 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) by imf27.hostedemail.com (Postfix) with ESMTP id 9D2084001B for ; Thu, 12 Jan 2023 15:53:39 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=mN5ObGrh; spf=pass (imf27.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.160.176 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=1673538819; 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=1PdPmTP/6nNwfuUbfoIGYl12UKrMMnWaGWGE68CKK00=; b=e/X9g8rv6luzZQbG2V/+UaPA/6FMJoXZss6s/zVsZPxChxjAe5DOjC0Zz3hl55EevZDJvJ pT4CwnDxW8D0dt44oPr2Wg7KZ8RXwp40zD2hfrGxbfU0S9q3q4X8eXruZgZigpasp3LXoJ 8RmUPU1R9qeZ0D/uj/GUUuO+qhcgke0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=mN5ObGrh; spf=pass (imf27.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.160.176 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=1673538819; a=rsa-sha256; cv=none; b=U4LtMHpiqAQ/DmnVs2saTm0B1yGFNHUHRnKSnaA+Bi/9mPWqBWuwCjV+slHwmtFXiqeVRO nTWP15tOtT9lVbgH67V6m/FQDlU/QfzTZ/TwjwH3ykFC0557rcZis8ogPA67DzhBAarn5K q3/RilTnha7JOl/5mPTXuHirwR7h994= Received: by mail-qt1-f176.google.com with SMTP id a25so9626982qto.10 for ; Thu, 12 Jan 2023 07:53:39 -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=1PdPmTP/6nNwfuUbfoIGYl12UKrMMnWaGWGE68CKK00=; b=mN5ObGrhWrxMJkdoooArLIKC8XD0Tvow9VTzY+Dt+bM8q/RM6rw039QTNBaDeGeO3W jc5ev4ZPmIqrRhwhGdLdkzTzN8gZKgSqZNiEFBB8OAHgukMGMx+f/InoklehDY6tX4bB CUnIWUqLRKGvCWlsJSI5IP1MkFCBw1ZTcSf2C/sDXTI6RYUmopTDbtNDJP+JyheZzkFf TzLt3sLAhYi+rdZI7qQ1UV42ayBjgKZFUszSk4A+zhvgOxP4u/jn2S3xHtTb2g+P1y58 CnyYcLq2SXGRAvEUlWv58eL9NelCvX7CcHE8aby/6geeLc5bxLV+FACVmTuHiL8tHB3/ 36HA== 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=1PdPmTP/6nNwfuUbfoIGYl12UKrMMnWaGWGE68CKK00=; b=ZcclO9lud6g2TNySSaDnrOd54Q0kBgCMLWrtkcE7R93iF0rITa9C7yynQYhpVqV3Av QUcHRtrEeRd4COLHoRFxlheDdqiHJBv2BqiMENASbh+DLSTc6o11zjUn7udK2bkaXiar sDnNlxHEULbiniPB+9NFA5W+czStozwrXGO2d7x8uNVBnYuS5ZHohqJL8/5PevKjSfdQ 0cG6voK3IDaK8FZPSORgxpKHbkPVLY5eM0cBKBxTaR1cycDL2RV55xtnNPsdc+xfF45x 2Tl8zH7EVTqaUwu1xAfPGcYX8Tm9ntnWX7QlAtkrlMTga1sWmfPBDJ7J1qhsVJiYB39P jseQ== X-Gm-Message-State: AFqh2kq+YcRj6SbhN7snyp+NdDWgbvUVG4x5v7JQucv8Rza8m3bwM4ct HwGq0/3YUfh6VB3akdpbzDA= X-Google-Smtp-Source: AMrXdXvcvv5QnOw+ucO2xICHIrhkKnumytKrPNexoUtZMl0/bJueZhRxVfPyCqg8K84Xgk088KoBmg== X-Received: by 2002:a05:622a:1741:b0:3a8:2716:ac2d with SMTP id l1-20020a05622a174100b003a82716ac2dmr149575319qtk.56.1673538818785; Thu, 12 Jan 2023 07:53:38 -0800 (PST) Received: from vultr.guest ([173.199.122.241]) by smtp.gmail.com with ESMTPSA id l17-20020ac848d1000000b003ab43dabfb1sm9280836qtr.55.2023.01.12.07.53.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 07:53:38 -0800 (PST) From: Yafang Shao To: 42.hyeyoo@gmail.com, vbabka@suse.cz, 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 Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 04/11] mm: slab: introduce ksize_full() Date: Thu, 12 Jan 2023 15:53:19 +0000 Message-Id: <20230112155326.26902-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230112155326.26902-1-laoar.shao@gmail.com> References: <20230112155326.26902-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 9D2084001B X-Stat-Signature: 83ds3ntiqqgwrc1sz8zwib6t7gpp5knb X-Rspam-User: X-HE-Tag: 1673538819-810261 X-HE-Meta: U2FsdGVkX1+YLBsT4gacMGnyI91VERmlxc6QnlZtYZb+sVYrtONSCEmi2D7RgjNrnoQ/C/p3jsbwR+uSvPS+whbG49ImhlC8+kOm992PHC8DszEu40ZO7imDqjJJumu6AxFCjScfB7+2wI0oL31rIusnoCONnVao3qTJugspzcelibGfVw03LyFWLiKCBr4DKU38s1nV5QuMlYw4DZkiH94Kd77cNIsKC1gRw91/TZ/FJ6bvotD1D4B0J2j3xq17MTOgPCO/eHvKPV73V4s45ewJdEG6OkWGB8xVs0aARi1yWr55N876u4VRRRRW09/5IH4jKKAq13sWBKwDrf3ElA4Ac6F7T5h/Dmgnv5YmNqwnOgejtMVNHZ8GCbqt4K+zrB0e8e+pDiQMabNqcqxVsDVYh+WmWG18FtEtETo/OVaUpxPtZ08RtOguj/7mjPKmroRmX35KKkbeyRzf0D93vKKTLiuc/rZRg5dCFin9UNh1QWNhObnlTPhxSLixDVI4AOJdRUbV159jcNFYC3bByDGdcBUZA8VZf5z1+TpGnedRqp1oBqNhC/flFXYzaAgM0LdyTGpk2SybCx/oAXKL29upub1YgvhzSLscf5jd/q+ql4XgMCSmZbBFxsZonjrYoZLvGW+WyQEtG5e+vrEBFuNhy75PiNPEpKoK4dt8B59ZV+T1xepg+TJiKS18FdqTbYy9eSkJwMgA3UfXgmfuuenrveEM5cCxK60GxcW/VJ9aHT6CNkc/4tGEcaBXL866uLm11aB+S0v52KR7spiswkOFkahRmGQ3LffSuF5+t4og9CRO/4CQ8jpX/ptn7122m8ni50Jygd1Rzzb6iLEA6TVM7nb6HtIsOMf8cPf2tUCacNAsI2Tdf0q4EFwxu1VyXVilbMT6YZ0XLLYfXCnBHrjMSaYa8El1eWbnbeysPQIZIAscEYCoFe/IoWvNQ30Y9EUQ85Rss3OZg5tVk/e kWCogU9E kwstrUzWikjzjJsKimxjQ/lRD7T2xDMxIIh2dhqj58GhAHBLGq+rZc/4tzhkNQDLTM8QrLw1+YBEzBaM/B441peJ9B+5ZWE+iK2m8b3JLMkRjPJeViMYhu3DGvvwnqLRR6rTTH+QS67W9nrHwgzrMhe2xh2Iu6/fN9WSr7Ke5t4AUTxN7iHS019bvtNiN1CUjckHNR6ZTIJfknMr9J2LM+wfaUcs6SlPgqL6IBZ4WJcNyGtxmzjBQagRPsKNVnbkvU4O6MAVhajlhXo2cnghVV9GKqIVO9grQ9pyC 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: When the object is charged to kmemcg, it will alloc extra memory to store the kmemcg ownership, so introduce a new helper to include this memory size. The reason we introduce a new helper other than changing the current helper ksize() is that the allocation of the kmemcg ownership is a nested allocation, which is independent of the original allocation. Some user may relays on ksize() to get the layout of this slab, so we'd better not changing it. Signed-off-by: Yafang Shao --- mm/slab.h | 2 +- mm/slab_common.c | 52 ++++++++++++++++++++++++++++++++++++---------------- mm/slob.c | 2 +- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/mm/slab.h b/mm/slab.h index 35e0b3b..e07ae90 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -681,7 +681,7 @@ static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x) #endif /* CONFIG_SLOB */ -size_t __ksize(const void *objp); +size_t ___ksize(const void *objp, bool full); static inline size_t slab_ksize(const struct kmem_cache *s) { diff --git a/mm/slab_common.c b/mm/slab_common.c index 1cba98a..4f1e2bc 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1021,21 +1021,11 @@ void kfree(const void *object) } EXPORT_SYMBOL(kfree); -/** - * __ksize -- Report full size of underlying allocation - * @object: pointer to the object - * - * This should only be used internally to query the true size of allocations. - * It is not meant to be a way to discover the usable size of an allocation - * after the fact. Instead, use kmalloc_size_roundup(). Using memory beyond - * the originally requested allocation size may trigger KASAN, UBSAN_BOUNDS, - * and/or FORTIFY_SOURCE. - * - * Return: size of the actual memory used by @object in bytes - */ -size_t __ksize(const void *object) +size_t ___ksize(const void *object, bool full) { + size_t kmemcg_size = 0; struct folio *folio; + struct slab *slab; if (unlikely(object == ZERO_SIZE_PTR)) return 0; @@ -1054,7 +1044,27 @@ size_t __ksize(const void *object) skip_orig_size_check(folio_slab(folio)->slab_cache, object); #endif - return slab_ksize(folio_slab(folio)->slab_cache); + slab = folio_slab(folio); + if (memcg_kmem_enabled() && full && slab_objcgs(slab)) + kmemcg_size = sizeof(struct obj_cgroup *); + return slab_ksize(slab->slab_cache) + kmemcg_size; +} + +/** + * __ksize -- Report full size of underlying allocation + * @object: pointer to the object + * + * This should only be used internally to query the true size of allocations. + * It is not meant to be a way to discover the usable size of an allocation + * after the fact. Instead, use kmalloc_size_roundup(). Using memory beyond + * the originally requested allocation size may trigger KASAN, UBSAN_BOUNDS, + * and/or FORTIFY_SOURCE. + * + * Return: size of the actual memory used by @object in bytes + */ +size_t __ksize(const void *object) +{ + return ___ksize(object, false); } void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) @@ -1428,7 +1438,7 @@ void kfree_sensitive(const void *p) } EXPORT_SYMBOL(kfree_sensitive); -size_t ksize(const void *objp) +size_t _ksize(const void *objp, bool full) { /* * We need to first check that the pointer to the object is valid. @@ -1448,10 +1458,20 @@ size_t ksize(const void *objp) if (unlikely(ZERO_OR_NULL_PTR(objp)) || !kasan_check_byte(objp)) return 0; - return kfence_ksize(objp) ?: __ksize(objp); + return kfence_ksize(objp) ?: ___ksize(objp, full); } EXPORT_SYMBOL(ksize); +size_t ksize(const void *objp) +{ + return _ksize(objp, false); +} + +size_t ksize_full(const void *objp) +{ + return _ksize(objp, true); +} + /* Tracepoints definitions. */ EXPORT_TRACEPOINT_SYMBOL(kmalloc); EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); diff --git a/mm/slob.c b/mm/slob.c index fe567fcf..8c46bdc 100644 --- a/mm/slob.c +++ b/mm/slob.c @@ -579,7 +579,7 @@ size_t kmalloc_size_roundup(size_t size) EXPORT_SYMBOL(kmalloc_size_roundup); /* can't use ksize for kmem_cache_alloc memory, only kmalloc */ -size_t __ksize(const void *block) +size_t ___ksize(const void *block, bool full) { struct folio *folio; unsigned int align;