From patchwork Thu Aug 29 17:53:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 13783528 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 B4C72C8303E for ; Thu, 29 Aug 2024 17:53:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 486696B00BF; Thu, 29 Aug 2024 13:53:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4360C6B00C0; Thu, 29 Aug 2024 13:53:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34BD76B00C1; Thu, 29 Aug 2024 13:53:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 115626B00BF for ; Thu, 29 Aug 2024 13:53:49 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9DF811A01F6 for ; Thu, 29 Aug 2024 17:53:48 +0000 (UTC) X-FDA: 82506030936.19.7737191 Received: from out-184.mta1.migadu.com (out-184.mta1.migadu.com [95.215.58.184]) by imf13.hostedemail.com (Postfix) with ESMTP id BEF8E2000F for ; Thu, 29 Aug 2024 17:53:46 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=xnGaMooO; spf=pass (imf13.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.184 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724954006; 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:references:dkim-signature; bh=zAYUC2lnpLysuA25o2WG5d1H9XkayuvnH+zN7Gauimg=; b=NX78BNk4JAaVCi9rcOfNaZIFNf5q82xjO5W5dOxZ6WYvmxZS/IzLc9qGOeuV1xPUSAeZ5k CpuRhBpIv2fos5eFBWy3D+GaQgQqK5kSzOJ33+7sftmczI2OVDyBbSDUnSDHDS1q//CGoe McuTDVTSevljBQm8DR1qPq1WBMmfmUw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=xnGaMooO; spf=pass (imf13.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.184 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724954006; a=rsa-sha256; cv=none; b=m+GlPlbT7CiPC0S35G5yhOjW5LSExheDygyOMe9pQUcjImkXpuybj5WpSVxqMuXr8GYt62 7yuAMoMIHaK8lLNyWHDHpRk+fjf4HHnqQqXIdmSGWy8jjzm1igMKm/65p1ieX9EZGMqqXN bXqor4Jyy6fL59b5RmqiRK9f1VBWZOk= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1724954024; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=zAYUC2lnpLysuA25o2WG5d1H9XkayuvnH+zN7Gauimg=; b=xnGaMooOtGo+/Gf22x5GLoM7oFgJOtusog3ARs6jMMuw7loqsn9SgPe+Skq51XRLRRYxBw sHFE3X4VXWuOnWfCLo2TOgFWZz9wcDK1JV+7CRuYGd2bsIDlXMfA2pog3H31X49jHv/I2M Is9DoFBJinpXA/InWhoo/wkCzG81jiw= From: Shakeel Butt To: Andrew Morton , Vlastimil Babka Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , David Rientjes , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Eric Dumazet , "David S . Miller" , Jakub Kicinski , Paolo Abeni , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Meta kernel team , cgroups@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v3] memcg: add charging of already allocated slab objects Date: Thu, 29 Aug 2024 10:53:39 -0700 Message-ID: <20240829175339.2424521-1-shakeel.butt@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: ernqt7hd3syoa4np1k4qu1yq4rwzfwk6 X-Rspamd-Queue-Id: BEF8E2000F X-Rspamd-Server: rspam11 X-HE-Tag: 1724954026-43772 X-HE-Meta: U2FsdGVkX183CgZiDUIyY7f3mlqBRA92Yo/VKyvhzXmbO+bGK+pgbWtmB6aykXINQNgBH/ANdAfg+XiR6qntS6i8b+lQEe8xSQqha76stLi3w6hl6+Fhvfv8sOEzcDYP07wsGh6wCh8Ghh1Eq0Q1WkWXmNK4N1leNS0fbezCzEVEd4jN2+/yd2tVNAR8mm0vli0xAsl8stTfCKBPIXAY+Z18DlnxfDB0joeyNfv7EHVCFicS7VjBGzCb2VZEUySFIB9BVmaxHuYU9lrAN2G7WO+6km+rCe5RcizKF+3lHchSJBDZuOQBRiiabSk8F5fuxFyRL1xcKkqTlSYN1zRXCcDlKG56DEdxBZe9Sr0UHwZukFzhZhdeP0Th2Wh6X0oYIUpx2pwmigMJy7XgeRCcRUNKptdHjG+jQi/SoSHr3uo7nnapxZxM0ZRTBFRSS/vuUYnm7g/KyAK2OKt9006s8oxSw03eb1i7TwzUPzqVbkJhe0xZnrXeAT97nc/zcAPAEG2fc+6JNQDeBQPhZaHn7fkUZA9DwGhJETplzF9wthi4RYqFQ6LMfy8V/c58FJbMhS1hTQ7Bq7bJTq07tJARM5/5s5wvj2XyAyb7XNinUlOFX3rfNPMX5GWK6HrdzirbLt6puHW4yw6ds78HIgeYXMKFlNKWNLhd0o79M0pLrehUhOkUD5qzkP80RfkGi391O7BIWgdnJqCZ5SEDHKirGHeuqehyOtODxFZ48RIzCR+D7C9uyIDJ48OoGWf/Yt9U4w0h3b2d/aYjHFSe2HeoGNJAjjlu+OAvmCZiK/a2/1YehMhFY04ByXW2vgNP9nyklMgBAWOiEeuYTQCIK6ltQ+EgxJTLWnOMcq9bdov7waXJTXfpEh2VgxOdl1mUfp8IhNMIkoFUZtvv7jPmSvCP+GPXYzwEpI5R8tgqyKdIntIF/VAtuSzfgjxl1/Ct9caSpaLdS8sukXZAyTMR8IA u6QBWJz2 0FH5OZ4FiCxgOjtxIS5eUGa9mpLCKL9FLXmzTU3a0E0/Z3IfBCcNec1nDF5SqWEZDxWsBllH4WcyVF9/3f/qtwfTbIj+SnnWWumKQZQp8MJSEXSHzGW4i69CON+CnEpSRjd364oryRJdok9CRW7bPrLe0XSgf1/vLt6ZfNHaIvBJ56mnDnkB6s57CObJJq02PHhAfHUFA732s/4CW+CLTPjVYFyIQObFlAL67KbwDTE4V4ygR00xm0YJOzBW7LLNQ95Ed 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: At the moment, the slab objects are charged to the memcg at the allocation time. However there are cases where slab objects are allocated at the time where the right target memcg to charge it to is not known. One such case is the network sockets for the incoming connection which are allocated in the softirq context. Couple hundred thousand connections are very normal on large loaded server and almost all of those sockets underlying those connections get allocated in the softirq context and thus not charged to any memcg. However later at the accept() time we know the right target memcg to charge. Let's add new API to charge already allocated objects, so we can have better accounting of the memory usage. To measure the performance impact of this change, tcp_crr is used from the neper [1] performance suite. Basically it is a network ping pong test with new connection for each ping pong. The server and the client are run inside 3 level of cgroup hierarchy using the following commands: Server: $ tcp_crr -6 Client: $ tcp_crr -6 -c -H ${server_ip} If the client and server run on different machines with 50 GBPS NIC, there is no visible impact of the change. For the same machine experiment with v6.11-rc5 as base. base (throughput) with-patch tcp_crr 14545 (+- 80) 14463 (+- 56) It seems like the performance impact is within the noise. Link: https://github.com/google/neper [1] Signed-off-by: Shakeel Butt --- v2: https://lore.kernel.org/all/20240827235228.1591842-1-shakeel.butt@linux.dev/ Change since v2: - Add handling of already charged large kmalloc objects. - Move the normal kmalloc cache check into a function. v1: https://lore.kernel.org/all/20240826232908.4076417-1-shakeel.butt@linux.dev/ Changes since v1: - Correctly handle large allocations which bypass slab - Rearrange code to avoid compilation errors for !CONFIG_MEMCG builds RFC: https://lore.kernel.org/all/20240824010139.1293051-1-shakeel.butt@linux.dev/ Changes since the RFC: - Added check for already charged slab objects. - Added performance results from neper's tcp_crr include/linux/slab.h | 1 + mm/slab.h | 7 +++++ mm/slub.c | 49 +++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 5 ++-- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index eb2bf4629157..05cfab107c72 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -547,6 +547,7 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags) __assume_slab_alignment __malloc; #define kmem_cache_alloc_lru(...) alloc_hooks(kmem_cache_alloc_lru_noprof(__VA_ARGS__)) +bool kmem_cache_charge(void *objp, gfp_t gfpflags); void kmem_cache_free(struct kmem_cache *s, void *objp); kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, diff --git a/mm/slab.h b/mm/slab.h index dcdb56b8e7f5..9f907e930609 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -443,6 +443,13 @@ static inline bool is_kmalloc_cache(struct kmem_cache *s) return (s->flags & SLAB_KMALLOC); } +static inline bool is_kmalloc_normal(struct kmem_cache *s) +{ + if (!is_kmalloc_cache(s)) + return false; + return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT)); +} + /* Legal flag mask for kmem_cache_create(), for various configurations */ #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \ SLAB_CACHE_DMA32 | SLAB_PANIC | \ diff --git a/mm/slub.c b/mm/slub.c index c9d8a2497fd6..3f2a89f7a23a 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2185,6 +2185,41 @@ void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, __memcg_slab_free_hook(s, slab, p, objects, obj_exts); } + +static __fastpath_inline +bool memcg_slab_post_charge(void *p, gfp_t flags) +{ + struct slabobj_ext *slab_exts; + struct kmem_cache *s; + struct folio *folio; + struct slab *slab; + unsigned long off; + + folio = virt_to_folio(p); + if (!folio_test_slab(folio)) { + return folio_memcg_kmem(folio) || + (__memcg_kmem_charge_page(folio_page(folio, 0), flags, + folio_order(folio)) == 0); + } + + slab = folio_slab(folio); + s = slab->slab_cache; + + /* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ + if (is_kmalloc_normal(s)) + return true; + + /* Ignore already charged objects. */ + slab_exts = slab_obj_exts(slab); + if (slab_exts) { + off = obj_to_index(s, slab, p); + if (unlikely(slab_exts[off].objcg)) + return true; + } + + return __memcg_slab_post_alloc_hook(s, NULL, flags, 1, &p); +} + #else /* CONFIG_MEMCG */ static inline bool memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru, @@ -2198,6 +2233,11 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, void **p, int objects) { } + +static inline bool memcg_slab_post_charge(void *p, gfp_t flags) +{ + return true; +} #endif /* CONFIG_MEMCG */ /* @@ -4062,6 +4102,15 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, } EXPORT_SYMBOL(kmem_cache_alloc_lru_noprof); +bool kmem_cache_charge(void *objp, gfp_t gfpflags) +{ + if (!memcg_kmem_online()) + return true; + + return memcg_slab_post_charge(objp, gfpflags); +} +EXPORT_SYMBOL(kmem_cache_charge); + /** * kmem_cache_alloc_node - Allocate an object on the specified node * @s: The cache to allocate from. diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index 64d07b842e73..3c13ca8c11fb 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -715,6 +715,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg) release_sock(sk); if (newsk && mem_cgroup_sockets_enabled) { int amt = 0; + gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL; /* atomically get the memory usage, set and charge the * newsk->sk_memcg. @@ -731,8 +732,8 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg) } if (amt) - mem_cgroup_charge_skmem(newsk->sk_memcg, amt, - GFP_KERNEL | __GFP_NOFAIL); + mem_cgroup_charge_skmem(newsk->sk_memcg, amt, gfp); + kmem_cache_charge(newsk, gfp); release_sock(newsk); }