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 E1D6EC87FD2 for ; Fri, 8 Aug 2025 14:44:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 680FA8E0005; Fri, 8 Aug 2025 10:44:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6593D8E0001; Fri, 8 Aug 2025 10:44:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5477E8E0005; Fri, 8 Aug 2025 10:44:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 41B3F8E0001 for ; Fri, 8 Aug 2025 10:44:37 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CB9FE140174 for ; Fri, 8 Aug 2025 14:44:36 +0000 (UTC) X-FDA: 83753861352.14.DC213C0 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf12.hostedemail.com (Postfix) with ESMTP id 557FC40007 for ; Fri, 8 Aug 2025 14:44:34 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=CJoEZC3q; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=JlVDAA6z; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=CJoEZC3q; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=JlVDAA6z; dmarc=none; spf=pass (imf12.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=1754664274; a=rsa-sha256; cv=none; b=rt6DMAamhANO41k8+yXFpIiH6YSS9F2fqBsYcCAPMuAnmc2pctswe/5ogSNlot8LtmeUwc M4yHbhK0AB1Ja6v8IXqcKC5MGsijlouWHEWVSeuWEyo7v0gLnBMGbturQhEWrGksrhWfwg UOa+6hYgqht1eVbU3NGzR/e+xDtyHHE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=CJoEZC3q; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=JlVDAA6z; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=CJoEZC3q; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=JlVDAA6z; dmarc=none; spf=pass (imf12.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=1754664274; 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=FZLIYAAzOqJrX0ZQHrg1ntpn3pRJDufkTOr2rwbrlqU=; b=mVQgtxxaS/ymr9PfLuqOgWXyQjOEQcM52DV2+hemvgzDRqZNKj05I+7o24giqac9h8QXsY XjSNNhzIq6uMyn8pSti+z/btIr+297kgS23/gpi3mey12mPYpCmgxFI9Joz/uRB+Phae8R yjRXk9+mi+OI7YtKIYVdnYXWh3SfEBE= Received: from imap1.dmz-prg2.suse.org (unknown [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 C9CEE33EAF; Fri, 8 Aug 2025 14:44:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1754664272; h=from:from:reply-to: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:autocrypt:autocrypt; bh=FZLIYAAzOqJrX0ZQHrg1ntpn3pRJDufkTOr2rwbrlqU=; b=CJoEZC3qM4RuZsGgyHwaUQJtzxnW5RRCEZq5OHZLqtBaoBGXsr6yUMSJ6oMbpqhrC5/AcE 5cW5vCmjSKhVjBuXTS/Q3ZEDu1olscN+QJc6t3pZDpwGyFWu8y6J88U49Mq/p+QgWfdkQ5 hmgiRjvAU6XaZ7ZWyurfggS0FUuqiQ0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1754664272; h=from:from:reply-to: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:autocrypt:autocrypt; bh=FZLIYAAzOqJrX0ZQHrg1ntpn3pRJDufkTOr2rwbrlqU=; b=JlVDAA6zzDmmC/vsVWsA2wtJ55XLTK0TEvocVucbGotZX/z6gmz/uM/kqfKtHsMR+vcUd8 DQ4wBPnhH2YtgQDQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1754664272; h=from:from:reply-to: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:autocrypt:autocrypt; bh=FZLIYAAzOqJrX0ZQHrg1ntpn3pRJDufkTOr2rwbrlqU=; b=CJoEZC3qM4RuZsGgyHwaUQJtzxnW5RRCEZq5OHZLqtBaoBGXsr6yUMSJ6oMbpqhrC5/AcE 5cW5vCmjSKhVjBuXTS/Q3ZEDu1olscN+QJc6t3pZDpwGyFWu8y6J88U49Mq/p+QgWfdkQ5 hmgiRjvAU6XaZ7ZWyurfggS0FUuqiQ0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1754664272; h=from:from:reply-to: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:autocrypt:autocrypt; bh=FZLIYAAzOqJrX0ZQHrg1ntpn3pRJDufkTOr2rwbrlqU=; b=JlVDAA6zzDmmC/vsVWsA2wtJ55XLTK0TEvocVucbGotZX/z6gmz/uM/kqfKtHsMR+vcUd8 DQ4wBPnhH2YtgQDQ== 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 9E98513A7E; Fri, 8 Aug 2025 14:44:32 +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 0gtbJlANlmiTOAAAD6G6ig (envelope-from ); Fri, 08 Aug 2025 14:44:32 +0000 Message-ID: Date: Fri, 8 Aug 2025 16:44:32 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH] mm/slab: save memory by allocating slabobj_ext array from leftover Content-Language: en-US To: Harry Yoo Cc: "Christoph Lameter (Ampere)" , David Rientjes , Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Suren Baghdasaryan , Kent Overstreet , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Yeoreum Yun , Vincenzo Frascino , linux-mm@kvack.org References: <20250613063336.5833-1-harry.yoo@oracle.com> <00e91300-c6e6-5330-f514-4628ae2ba696@gentwo.org> From: Vlastim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eVV1Jd9IjAoL+EqB0cbxoFXvnjkvP1foiiF5r73jCd4PR rD+GoX5BZ7AZmFYmuJYBm28STM2NA6LhT0X+2su16f/HtummENKcMwom0hNu3MBNPUOrujtW khQrWcJNAAsy4yMoJ2Lw51T/5X5Hc7jQ9da9fyqu+phqlVtn70qpPvgWy4HRhr25fCAEXZDp xG4RNmTm+pqorHOqhBkI7wA7P/nyPo7ZEc3L+ZkQ37u0nlOyrjbNUniPGxPxv1imVq8IyycG AN5FaFxtiELK22gvudghLJaDiRBhn8/AhXc642/Z/yIpizE2xG4KU4AXzb6C+o7LX/WmmsWP Ly6jamSg6tvrdo4/e87lUedEqCtrp2o1xpn5zongf6cQkaLZKQcBQnPmgHO5OG8+50u88D9I rywqgzTUhHFKKF6/9L/lYtrNcHU8Z6Y4Ju/MLUiNYkmtrGIMnkjKCiRqlRrZE/v5YFHbayRD dJKXobXTtCBYpLJM4ZYRpGZXne/FAtWNe4KbNJJqxMvrTOrnIatPj8NhBVI0RSJRsbilh6TE m6M14QORSWTLRg== In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 557FC40007 X-Stat-Signature: cyyi48hj8bqkc51yckagziuckn8nmfr8 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1754664274-156525 X-HE-Meta: U2FsdGVkX18TPU7Qx7FP7ObV/dYSskZc2F2vDhyHHt3Fhlnxv3+3r+DRVqA6TAaV4BDzag0b9pdOFbD3IABxRCDU0FkbUkR+fDJwXob+Ht0ClTlQPJsU7IJjBUnjgaCXikDP6oj5a2+ez6TU3dx73yU6Tk/fZVVzepcxDG83Lq59GeL2Jx8Bv+yfv9caFCLoU8FmLU2/uliu23A4B4MTKSfv1ap15ND8zOOHAK60mKuNSCYjfSb8inAHyDyIj11JY/8W5raGBhMzY7pvi0GylXhP2hAmEZxvaK1P+AJdK5mTIK+WMdW91qfxEEUqM520MacRTXu3H1L+nixTiuqsyxD8tiZHh8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amicity=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 8/5/25 13:57, Harry Yoo wrote: > On Thu, Jun 19, 2025 at 09:56:59AM +0200, Vlastimil Babka wrote: >> On 6/13/25 19:47, Harry Yoo wrote: >> > On Fri, Jun 13, 2025 at 09:04:34AM -0700, Christoph Lameter (Ampere) wrote: >> >> On Fri, 13 Jun 2025, Harry Yoo wrote: >> >> >> >> > Allocate slabobj_exts array from this unused space instead of using >> >> > kcalloc(), when it is large enough. >> >> >> >> How does slab debug work in this case? The object layout gets a bit >> >> complicated with other metadata there as well. >> > >> > Oh, the 'leftover' space I mentioned the cover letter refers to the >> > wasted space after the last object in a slab, not unused bytes within >> > objects. >> > >> > There is no per-object metadata stored there and SLUB simply poisons the area. >> > I taught slab_pad_check() to skip checking the slabobj_exts array. > > Apologies for the late reply. I was sidetracked with multiple things :( > > This is definitely worth optimizing, so let me make some progress > even if it's a bit slow. > >> I can imagine going further with this in case where leftover space in slab >> isn't enough. > > Right. > >> - indeed use per-object padding to store only single object's slabobj_ext, > > I think the most conservative approach is to not increase object_size > but use wasted area when ALIGN(size, align) is bigger than object_size. > > A good candidate for that is xfs inode cache. Yeah that's what I meant. >> if it doesn't lead to memory waste > > You mean increasing object size but without decreasing the number of > objects per slab? Just the ALIGN thing. It we can increase size without decreasing objects it means we can also just use leftover space for the array, IIUC? > ...or (maybe) reducing the number of objects but without increasing > the size of the remainder (same as calculated in calc_slab_order())? > >> - if not possible, but object size is small enough so there are many per >> slab, maybe have one less object per slab to store the array? > > If object size is small the array likely does not fit in one object... Hmm perhaps. >> - once we have struct slab decoupled from struct page, it could be part of >> struct slab directly (but it would mean struct slab isn't fixed size) > > It can be tried, but variable struct slab size may or may not work. > That'll depend on the implementation details of how we allocate struct slab > in the future. > >> Of course having multiple variants would risk slower code, so fast paths >> should not be affected > > I agree that affecting fastpath is not great. So we'll have to measure it. >> we could have pointer to the 0th slabobj_ext (we >> already have) and now also stride (to support the "per-object padding case" > >> - there's still space in struct slab right?) > > which space are you referring to, maybe lower 16 bits of page_type? Ugh I thought counters used only 32bit of 64... so the space exists only for 64bit kernels? It would be fine to limit the optimization to those only. >> and then the object alloc/free >> case could be oblivious to the storage method, with just a bit more >> arithmetic (stride). Slab folio alloc/free would be more complicated but are >> not fath path. > > So it would be something like (please correct if I misunderstood): > > index = obj_to_index(s, slab, object) > (the struct slabobj_ext pointer for the object at given index) > == slab->obj_exts + stride * index Yeah. > > slab->obj_exts, stride are determined depending on the case: > > - In the normal case (the array is allocated from kmalloc caches), > stride = sizeof(struct slabobj_ext) > slab->obj_exts = (the address of the buffer allocated from kmalloc) > > - In "the obj_exts array is stored in the leftover space" case, > stride = sizeof(struct slabobj_ext) > slab->obj_exts = (the start address of the leftover space) > > - In "per-object padding" case, > stride = s->size > slab->obj_exts = slab_address(slab) + s->red_left_pad + > (offset of slabobj_ext); > > Ok, I think it will work. Great idea! Yeah. > >> Also some variants would be wasteful if they need to be decided upfront (the >> 2nd and 3rd above) and then the array is unused > > Right. > >> so would be only applicable >> with SLAB_ACCOUNT caches (if kmemcg is active) or when memalloc profiling is >> active. > > Right. > >> Shouldn't be a big issue as ad-hoc __GFP_ACCOUNT is handled by >> different cache selection for kmalloc() and I don't know if anyone is >> actually doing ad-hoc __GFP_ACCOUNT on named caches. > > A while ago I was thinking of getting rid of ad-hoc __GFP_ACCOUNT usage > for slab allocations, but at least xarray cache appears to use it in > an ad-hoc manner. (See xas_nomem()). > > In that case, not all allocations to the same cache has > __GFP_ACCOUNT flag set. Hmm bummer.