From mboxrd@z Thu Jan 1 00:00:00 1970 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 C60C1E6FE2F for ; Fri, 6 Sep 2024 16:04:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F0C666B0089; Fri, 6 Sep 2024 12:04:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EBCB36B008A; Fri, 6 Sep 2024 12:04:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D84666B008C; Fri, 6 Sep 2024 12:04:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B992E6B0089 for ; Fri, 6 Sep 2024 12:04:03 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5A4931C44A8 for ; Fri, 6 Sep 2024 16:04:03 +0000 (UTC) X-FDA: 82534784766.28.A4313E5 Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf02.hostedemail.com (Postfix) with ESMTP id 52B0580003 for ; Fri, 6 Sep 2024 16:04:01 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=slP+PObl; spf=pass (imf02.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.175 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=1725638592; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dTskArZncKt0pUKIN2dkR1HoLoe1rwj7R2FFyS66ktk=; b=qiBhgH3YEEy9RGShSYHcaZLh3b8X65aUuagoIPzyLJ7YfsvItdeJ0jDL03Jj1Bph0uJ3Rn 2L3xmiRjXruv/Uj0+mCqTTIb0Eop/l5P7q0LYkUxCKuzzd9y8cO8uRfyorYF8LPq7DBwmQ EbWBXq9VSNadUQ4VcFxIU+CNoVnetf8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=slP+PObl; spf=pass (imf02.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.175 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=1725638592; a=rsa-sha256; cv=none; b=OS2tX6UXkzdgavuA5t8a5gba+sq/MMK3lcDwdNznjEL4YxBqRku03DzB42hMdWJDjpJuR1 qQmy6BtXPtqlzDKCSDZVG2Ll4bg+uC6qzvVlubOhYsPVZewdVwu/xlOgzO+elmHiDM/uiB U/4cFCX3yiIBr5HeYgLpqK0xY+BxLuM= Date: Fri, 6 Sep 2024 09:03:50 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1725638638; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=dTskArZncKt0pUKIN2dkR1HoLoe1rwj7R2FFyS66ktk=; b=slP+POblHpDGNl8D9VDtMWVWaV/xvR+Dsps19NpRz/soVWgyQss3p3S+poZsrB5Mq5O75K nzlMpEl2iuMDlnz8pwrf3rlgy4j2ffh0DLTD3MmD2nOuFVeDm0XwUtc4Pb2HFX4xo4bgSB 75tZ+or1hIcU9ftkSGJOxEqfK9sqXdg= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Shakeel Butt To: Vlastimil Babka Cc: Yosry Ahmed , Andrew Morton , 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: Re: [PATCH v4] memcg: add charging of already allocated slab objects Message-ID: References: <20240905173422.1565480-1-shakeel.butt@linux.dev> <572688a7-8719-4f94-a5cd-e726486c757d@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <572688a7-8719-4f94-a5cd-e726486c757d@suse.cz> X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 52B0580003 X-Stat-Signature: unzey4zwthx1sykjqz3wz48knrwc99pn X-HE-Tag: 1725638641-660707 X-HE-Meta: U2FsdGVkX19qbsLDbPJ21UzPsSg+em6qoNni6tzyfk+bNAGQBA1A2bzuEQw1Z59QmN4dNIwLrPNH+MEVEXywvNOMMvi1xIuYQbdez25v68mto6ESl/ZtTdYgyILjFKyxWJ8HoM2cISD84BkqEEo7Iun69LYfcCu2flHntJzEjwS2ZxwBGiOLuqjwN0+d3bbg3kfs+8UnFxdvRbuf+WjIz8ZEUxZjNYVPP+CFciCRvf+H32rpHEpsGByJLVJ21iJ9tUU1US+OU1IZxJeo16Cx0ll0tuWiPh9oGkRVr2YVY/iZCtl2ftp6LQELo9b6r2ipZpaFwyczzfW+tLtHhRPqGV6rxltcMtSN3f9VjYxNSODLkHktPpmZRarLT77P/0y0AaJGDnHf16/vLapZIonDrAJncmQ1pccXiZKCApB019GTRC1MBmkP9QeMz/Bb6wE3wZCsAADiuNACyCHtrKHXX8Zw3JDS7L2cvx1doSTvdC1LuiubvP3J7At8YJItC9E5ESLsszPW3jeu+565txYDDINwNm7Mx+pWG+F1KuFU+TkMTdQgsT2yJpQVed5NHaVwuBT6O+VjUAgrX1yAy99wMJp6ODORjciBc4Q8h8w80QOYYOindS3aDvOw9gtoeOKDz4xFKHwuBu8qGJzvbtbdJAnnXk6GfWoTcVZbtoXR3PTCpLsEwH1OcNSGVdyuaMfAgDg/pDlHcQ4bxIsWqFlIbGSICv5nRgi/KNxBG7AjlS0fSwgSxfhZva0Z7ay+nQ6SmqHBDEbcLy39uqX0H3+yxeri1UNbFEtQVh1/nIwXj5tN6rnkoMskjtYcdWQ8HalkLMhENuBcHfBgEe+xyeddLt8TPiucMLOwIalZIzM9VgAVii6YtK4Nsdi6rXsHoEbtZwJHrBcYe0AZt/VoEvkjqZERF00eImSBEtFMBST+5aDb6FgQacu22ZhcJe5tvTXPKvu0CflKTj4ltubX1TS vy9vk5n2 IRpWAZP1zjpzMcsk3+YlchP9/VncUECQLHzmpGxObFmxylRAFBp7wT71o5lgfWz1b8XAksk+rx6OSfrmv+XyMxxb9RLwygegF7vCrPsJ1O1mFihLHjH0P9YpdK0lWFsh41HcyZ87uo0aEDSnvZ9niGt9+Ag0+f7njyURux0IDrYtqackI/izBkH5DMbvIgPuDNFQYGNOyKKpTAasIfnbWnhUFVc9qj3pc6mieNLml45ofytVhNu0WX9nXiA== 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: On Fri, Sep 06, 2024 at 10:52:04AM GMT, Vlastimil Babka wrote: > On 9/5/24 20:48, Shakeel Butt wrote: > >> > --- > >> > v3: https://lore.kernel.org/all/20240829175339.2424521-1-shakeel.butt@linux.dev/ > >> > Changes since v3: > >> > - Add kernel doc for kmem_cache_charge. > >> > > >> > 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 | 20 ++++++++++++++ > >> > mm/slab.h | 7 +++++ > >> > mm/slub.c | 49 +++++++++++++++++++++++++++++++++ > >> > net/ipv4/inet_connection_sock.c | 5 ++-- > >> > 4 files changed, 79 insertions(+), 2 deletions(-) > >> > > >> > diff --git a/include/linux/slab.h b/include/linux/slab.h > >> > index eb2bf4629157..68789c79a530 100644 > >> > --- a/include/linux/slab.h > >> > +++ b/include/linux/slab.h > >> > @@ -547,6 +547,26 @@ 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__)) > >> > > >> > +/** > >> > + * kmem_cache_charge - memcg charge an already allocated slab memory > >> > + * @objp: address of the slab object to memcg charge. > >> > + * @gfpflags: describe the allocation context > >> > + * > >> > + * kmem_cache_charge is the normal method to charge a slab object to the current > > what is "normal method"? This is just a copy-paste from kmalloc() documentation. > > >> > + * memcg. The objp should be pointer returned by the slab allocator functions > >> > + * like kmalloc or kmem_cache_alloc. The memcg charge behavior can be controller > >> > >> s/controller/controlled > > > > Thanks. Vlastimil please fix this when you pick this up. > > I felt it could be improved more, so ended up with this. Thoughts? > > /** > * kmem_cache_charge - memcg charge an already allocated slab memory > * @objp: address of the slab object to memcg charge > * @gfpflags: describe the allocation context > * > * kmem_cache_charge allows charging a slab object to the current memcg, > * primarily in cases where charging at allocation time might not be possible > * because the target memcg is not known (i.e. softirq context) > * > * The objp should be pointer returned by the slab allocator functions like > * kmalloc (with __GFP_ACCOUNT in flags) or kmem_cache_alloc. The memcg charge > * behavior can be controlled through gfpflags parameter, which affects how the > * necessary internal metadata can be allocated. Including __GFP_NOFAIL denotes > * that overcharging is requested instead of failure, but is not applied for the > * internal metadata allocation. > * > * There are several cases where it will return true even if the charging was > * not done: > * More specifically: > * > * 1. For !CONFIG_MEMCG or cgroup_disable=memory systems. > * 2. Already charged slab objects. > * 3. For slab objects from KMALLOC_NORMAL caches - allocated by kmalloc() > * without __GFP_ACCOUNT > * 4. Allocating internal metadata has failed > * > * Return: true if charge was successful otherwise false. > */ > Yes, this is much better. > >> > + > >> > + /* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ > >> > >> Is it possible to point to the commit that has the explanation here? > >> The one you pointed me to before? Otherwise it's not really obvious > >> where the circular dependency comes from (at least to me). > >> > > > > Not sure about the commit reference. We can add more text here. > > Vlastimil, how much detail do you prefer? > > What about: > > /* > * Ignore KMALLOC_NORMAL cache to avoid possible circular dependency > * of slab_obj_exts being allocated from the same slab and thus the slab > * becoming effectively unfreeable. > */ > Looks great to me. thanks, Shakeel