From patchwork Fri Mar 1 17:07:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13578810 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 DAEE6C5478C for ; Fri, 1 Mar 2024 17:07:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D8AC6B009B; Fri, 1 Mar 2024 12:07:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 586386B009D; Fri, 1 Mar 2024 12:07:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AEBB6B009C; Fri, 1 Mar 2024 12:07:16 -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 EEC15940010 for ; Fri, 1 Mar 2024 12:07:15 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A1E37A1B79 for ; Fri, 1 Mar 2024 17:07:15 +0000 (UTC) X-FDA: 81849100830.25.762DC39 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf13.hostedemail.com (Postfix) with ESMTP id 7D4EF20028 for ; Fri, 1 Mar 2024 17:07:12 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf13.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709312832; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ejITEmwOvuELk0FkAjL3jJy28ouM8QRgbljo3Xli1AE=; b=tjjZ9/jVM+io4iKdxdW1tHjVMBM4ODT1xzk/gJzzmAsWARxuKULDpTZEoyzDwW0/9yk+nj gzs+s6OSHKPs9NJyKLIRAPsrcIz1tIiq0kSK0AzC3a5Zy+hpeZ2BVnYk+sTnnT50iY8tma 5KVPt/EQVUvpJ+H/GuZDUKfIgdbb5es= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf13.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709312832; a=rsa-sha256; cv=none; b=s2FNrLKivXn7xfmyoSzi3T2a3eZVqotZdzu36atkugr+99Xa9jzkBCNmRRqNYLHvpfdnWc LJBunXzp8bW3I5WEgVK/PR2mw+xW3JwQl5HHLzSvmm1I1pXIGEJCjuaOqgPuhaZC/86cPt LrYlRITGHW38HZYMnKTIfMK1XSasuP4= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id E827E33D2B; Fri, 1 Mar 2024 17:07:10 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id C31ED13A59; Fri, 1 Mar 2024 17:07:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id AAZcLz4L4mUcGQAAD6G6ig (envelope-from ); Fri, 01 Mar 2024 17:07:10 +0000 From: Vlastimil Babka Date: Fri, 01 Mar 2024 18:07:10 +0100 Subject: [PATCH RFC 3/4] mm, slab: introduce kmem_cache_charge() MIME-Version: 1.0 Message-Id: <20240301-slab-memcg-v1-3-359328a46596@suse.cz> References: <20240301-slab-memcg-v1-0-359328a46596@suse.cz> In-Reply-To: <20240301-slab-memcg-v1-0-359328a46596@suse.cz> To: Linus Torvalds , Josh Poimboeuf , Jeff Layton , Chuck Lever , Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Alexander Viro , Christian Brauner , Jan Kara Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, Vlastimil Babka X-Mailer: b4 0.13.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7D4EF20028 X-Stat-Signature: omdp8kg8cc3yd5u5p37wtjizmomywy1u X-Rspam-User: X-HE-Tag: 1709312832-804189 X-HE-Meta: U2FsdGVkX1+i5QKHSYXk88ws4PkWCP+SWv2lgJORAgfOJcUTwO1MdpnXGgQx//dMXYyxCTHgz77v6S/dZHWi97dxjM9Ikca7RFM4cpo8fIB7SWlkxE8Hw3z6zS0M2gNsn18cN4gKTowpEQAkoA3PpixjY7haOtJUA9GwqJ/5LaICTdXfvllHqCRvgF2NAh5Yv6ErumgzFKn1TxutulXOacYRFCHUabwd9gZaOhY3/uvvSpR8RJLyDohLoqF3kdQzcwkk5LHYxWh+5UvOYeezar2v/uw+w+VUP/IwGhxUaeaD01SXuneInaAoz+FvENrqKr+CWwtKGDt/KPd+3ZIoWM1EASJeA7xl+2TDSSX3PPvYCRW0K3nWYx9B3XZM0umrtIsZU7wZxrt2fSlsAs2MnLVTET9C5XZZhYhvqak7QJItM1e2qzGe76ud6jfnRDxfKLSkrQlE24Qi7bGs15hurJAAFRRmYdgGQVkDXpRTYrtT8S5WwyV3th5XdY5xeiKq45Tm0ACib3Who6GJVhMr+9NL+/Ps0agSPRCn93hrisvMK22q9rD1GCcBytjGyAu2CM9hXsi4lKf0c6DTmeDIpiJuZUEMK/uSn2jTDAvDTBOWyYj8JC7gES+nV2UQkjx0Kxv0hDL1XItJAGCQ8FDyvn3aTYUqyYonAXQE0PHAn9npYL57Sxpx766xM+ZJHweeefyGKFBfdiPCGi6jb5VNhZs3HqMqBw3IcUsgyGMW7f9cm77hg+dPiARTyq5/zTaADb/yuYUOgd2Sb22eIERAIrqzZSDEUMMQ0LhXjR4khbKgyZAClAifjb5LHgEfvHAlTse6Wn0Xxm9LuB/qeBjb42VH4tTtpcrbwdYurCWc9KD14r7a6T5W8ZUBwQugepAAhwMQ68UgE7NzvctDsKPveuKMrEkicHk56IYFKAQwFkz2kX0cCXDCdQpoX0XXlatyTme0HGoX1guJjzSG/Q4 SEDbf0OH 5lUzAjOAgpufJUNJC2d93qA9WVL62n6Hv9IDFTwWjPgBEpimAVS8asQczpMHQF9DexkGHRlMh3Eh+8FTJYUUYF6hF1FOml45nsI7lcfXFLiB6S1aFTDHbxs3wwdNg6qW8v9uQLEIfz82kYeSTH+RsDTlX86YSTPwUdEd9JNN/l5590ksUyxkHAVyZ0oyKT1ujRcCh4r99MmXYTbZMrP5xZPM7TYB8uDWbSG0cGOF8y1WwpoDElZb6kPhTnNBAQc+dZbH9hMmkSar7BJRBHSgfZIiqt9KIdErgsDiKkBi1HdgI9zoYwRlBlV9yAFya23r/btuQgN8bn1LRldZ2ULD4epzJTxBuaQsloHy2iuCg02SUQQ4sTULjn3jH/Q== 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: List-Subscribe: List-Unsubscribe: As suggested by Linus, introduce a slab API function to memcg-charge a an object that was previously allocated without __GFP_ACCOUNT and from a cache that's not SLAB_ACCOUNT. This may be useful when it's likely the object is to be freed soon, and thus the charging/uncharging overhead can be avoided. In case kmem_cache_charge() is called on an already-charged object, it's a no-op. Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=whYOOdM7jWy5jdrAm8LxcgCMFyk2bt8fYYvZzM4U-zAQA@mail.gmail.com/ Signed-off-by: Vlastimil Babka --- include/linux/slab.h | 10 ++++++++++ mm/slub.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/linux/slab.h b/include/linux/slab.h index b5f5ee8308d0..0c3acb2fa3e6 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -491,6 +491,16 @@ void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags) __assume_slab_ali void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags) __assume_slab_alignment __malloc; void kmem_cache_free(struct kmem_cache *s, void *objp); +#ifdef CONFIG_MEMCG_KMEM +int kmem_cache_charge(struct kmem_cache *s, gfp_t flags, void *objp); +#else +static inline int +kmem_cache_charge(struct kmem_cache *s, gfp_t flags, void *objp) +{ + return 0; +} +#endif + /* * Bulk allocation and freeing operations. These are accelerated in an diff --git a/mm/slub.c b/mm/slub.c index 64da169d672a..72b61b379ba1 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -4241,6 +4241,35 @@ void kmem_cache_free(struct kmem_cache *s, void *x) } EXPORT_SYMBOL(kmem_cache_free); +#ifdef CONFIG_MEMCG_KMEM +int kmem_cache_charge(struct kmem_cache *s, gfp_t flags, void *x) +{ + struct obj_cgroup ** objcg; + struct slab *slab; + + s = cache_from_obj(s, x); + if (!s) + return -EINVAL; + + if (likely(!memcg_kmem_online())) + return 0; + + /* was it already accounted? */ + slab = virt_to_slab(x); + if ((objcg = slab_objcgs(slab))) { + unsigned int off = obj_to_index(s, slab, x); + + if (objcg[off]) + return 0; + } + + if (!memcg_slab_post_alloc_hook(s, NULL, flags, 1, &x)) + return -ENOMEM; + + return 0; +} +#endif + static void free_large_kmalloc(struct folio *folio, void *object) { unsigned int order = folio_order(folio);