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 2D191C7112B for ; Wed, 28 Aug 2024 20:16:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFA0A6B0089; Wed, 28 Aug 2024 16:16:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B5CC66B008A; Wed, 28 Aug 2024 16:16:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FD486B008C; Wed, 28 Aug 2024 16:16:28 -0400 (EDT) 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 76AC06B0089 for ; Wed, 28 Aug 2024 16:16:28 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3354D1A07D3 for ; Wed, 28 Aug 2024 20:16:28 +0000 (UTC) X-FDA: 82502761656.14.04DD0C3 Received: from out-175.mta0.migadu.com (out-175.mta0.migadu.com [91.218.175.175]) by imf25.hostedemail.com (Postfix) with ESMTP id 2BC51A0006 for ; Wed, 28 Aug 2024 20:16:25 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=UA8NPRLH; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf25.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.175 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724876166; a=rsa-sha256; cv=none; b=TH/VXEpR06f2i65QUvSCRvpQ6Hp8O2ladosKo8+qHpvlA53r35zoYQA1PHKVC8tXiJhEeM 2jNb1su+WjDlpwDJ0DYgW1NbercBgMDkU3EGSJ/JxJSmvq8agXlYA6yqP9JjDTNgBta+nv se3NmXbU/xdmtfZ5AeY4e+3sVu/Z52c= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=UA8NPRLH; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf25.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.175 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724876166; 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:dkim-signature; bh=TcFJMtnoZx/3smatfAG72v8CqCQxeneyOqoZqcBXEJ4=; b=l0YqXDhhgpGLdJYI5i/oiPABkWDlVDsVmg+efnhFFp5p1bMCWzoHz2YSWrJ/hZA6NwWhdL EBUidfHuqg79h/dTsC8o9thB19KC0NJfo7N1FHK+moDywC/z8iMukAYdNhvY/ojeAhNsTy ipQnwznFCgkW4uLo5nROr5EL3LzXFvo= Date: Wed, 28 Aug 2024 13:16:14 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1724876183; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TcFJMtnoZx/3smatfAG72v8CqCQxeneyOqoZqcBXEJ4=; b=UA8NPRLHJk8sJ2BgEWshSbomfpPfoUBD1kFcgir+M5ur3KcuRfOPjB7A2f2BlRBseINxqv HhHMR6llKGwdYydRhGqyBns7E2u9qHvc/4pRMbhiEmxIwHvQJA4leZgR+BGuxL9hhusIOD 4baQO6VbhPbCE3d6lu4DwEUEd1ErM1U= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Shakeel Butt To: Yosry Ahmed Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Vlastimil Babka , 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 v2] memcg: add charging of already allocated slab objects Message-ID: References: <20240827235228.1591842-1-shakeel.butt@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Queue-Id: 2BC51A0006 X-Rspamd-Server: rspam01 X-Stat-Signature: 1ei55gqniix8wz35yiar64wztdojnpwi X-HE-Tag: 1724876185-938046 X-HE-Meta: U2FsdGVkX180xseFjLUs5ncg9vBZ9BDpqjXRjqExrKhnKPLbw4ZWgIdwmshL2OCVkUvlPmFShR0iU+uVtPpE8RzVfgaqRyjkvrPl1c61NslKwQ0L72OSOazDyqYJUrLlIVEltpan6hGczLZtQ2Y9LFmo1XLZ1WaLU3RnQGDDeMSBWKWLBjTOM4KoGG7ss7WDtiKH1ttRijvKTc7tDxeaRsvD5IYNiRGFUYdYgUCQhiL5fEWzOtXMwt0zOOPILB+wMJIQCYxoSJuILkDCrDRJsBA2iLFv+vRZcWaVXiu63act6is2BGiUYK2p4FvzvFdriVIJ92shTeJZ/4IoRiKlejhYHNX17vWakIElu2Y0TIfU7vA0lejqYNXhKv3b4/FB7W4NMCSo41YOOExO0omTTUmXjEoYZukdDXlny/iSanOw9qHIIoUPprvRCm+pLkAysG0lo4eFhBfHChKN0ddeESNmOWU52pyTAjbWnM4+bjXOBBp62chr1xY84k2TPgG6fHi7iNV1aH+BKgL8VccYbUdhIc59MmslRxmH4kMJ2YagGidoUmzzxRricGfylsxwJrdEiuHvKF5g7lkdWY0op5Q5JZEt/jQUCPt6WxJGFWh5yzJtWShHIrEcqvAreGb5m3as+Zj5R+XHVl8xYgNsa+tTvZsEkHmyp/duOJ35/Sn/IK8BZpspeB9rl2LgYQaBKqdF0aa7CZuxcu0PQG7skF6Prlrv4eUFDHszDfjs7bsUfbCQro1vXXfDFObb5RACHK8jDOkgP8VoiWJMU+xpzKq7dsz3gM2qp435RVe5TxnhVrdUFUhqP9FX9RWmr9QcTfm8IWLPkQtQpV8vbgu1TLXIQ2lUa0N7hey5xQM+7pJg6eYK9wiiXOqhXibPeVtCSHG9gNconTuM+EwHyUQ/3oOoCY0TLfQ8nq3GlpiKxA8WF+EChY+JNM9YkBPCMT66G10N4VqgrGm3HFN//fX I6R62zJo k1rciZWQHY9JQJ8jLRT1ip7MUDwDSuOEEB1YQJnp/gWWXTHjwzhv91eIkyjP02QUxRYeVxIRPDTQ9IEsH4AxYUD5ZHZkYt5xbxo22I26VvWZ+7dj1dJDdGe1o7doGjhG1wGXdW5/B6yA3Ylos86FaJHgBSOK1EPmJV/R2q3Q8lC2RPI0Kp8+XGBesBfS28fGr7rYAiaTQ1UEF+KlwNDGBcOTNXAJ7uZUR72127HD2fdUpr4M6AQO9crQ2VA== 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 Wed, Aug 28, 2024 at 12:42:02PM GMT, Yosry Ahmed wrote: > On Wed, Aug 28, 2024 at 12:14 PM Shakeel Butt wrote: > > > > On Tue, Aug 27, 2024 at 05:34:24PM GMT, Yosry Ahmed wrote: > > > On Tue, Aug 27, 2024 at 4:52 PM Shakeel Butt wrote: > > [...] > > > > + > > > > +#define KMALLOC_TYPE (SLAB_KMALLOC | SLAB_CACHE_DMA | \ > > > > + SLAB_ACCOUNT | SLAB_RECLAIM_ACCOUNT) > > > > + > > > > +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 __memcg_kmem_charge_page(folio_page(folio, 0), flags, > > > > + folio_order(folio)) == 0; > > > > > > Will this charge the folio again if it was already charged? It seems > > > like we avoid this for already charged slab objects below but not > > > here. > > > > > > > Thanks for catchig this. It's an easy fix and will do in v3. > > > > > > + } > > > > + > > > > + slab = folio_slab(folio); > > > > + s = slab->slab_cache; > > > > + > > > > + /* Ignore KMALLOC_NORMAL cache to avoid circular dependency. */ > > > > + if ((s->flags & KMALLOC_TYPE) == SLAB_KMALLOC) > > > > + return true; > > > > > > Would it be clearer to check if the slab cache is one of > > > kmalloc_caches[KMALLOC_NORMAL]? This should be doable by comparing the > > > address of the slab cache with the addresses of > > > kmalloc_cache[KMALLOC_NORMAL] (perhaps in a helper). I need to refer > > > to your reply to Roman to understand why this works. > > > > > > > Do you mean looping over kmalloc_caches[KMALLOC_NORMAL] and comparing > > the given slab cache address? Nah man why do long loop of pointer > > comparisons when we can simply check the flag of the given kmem cache. > > Also this array will increase with the recent proposed random kmalloc > > caches. > > Oh I thought kmalloc_caches[KMALLOC_NORMAL] is an array of the actual > struct kmem_cache objects, so I thought we can just check if: > s >= kmalloc_caches[KMALLOC_NORMAL][0] && > s >= kmalloc_caches[KMALLOC_NORMAL][LAST_INDEX] > > I just realized it's an array of pointers, so we would need to loop > and compare them. > > I still find the flags comparisons unclear and not very future-proof > tbh. I think we can just store the type in struct kmem_cache? I think > there are multiple holes there. Do you mean adding a new SLAB_KMALLOC_NORMAL? I will wait for SLAB maintainers for their opinion on that. BTW this kind of checks are in the kernel particularly for gfp flags.