From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE8A32ED163 for ; Fri, 11 Jul 2025 15:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752248600; cv=none; b=OG6v5eDM2dvCbppcgFqrlvfV6MN9tUYJJskgGnZRg6jOkyNy2i1lzty4tsNtxwqxETgKF+sxKPmzWDmXLRaYWp2FXgxXq4iBk05XG45ppdeVfjXvUlSs6k25W+bT5u41zZmNHs5XyBRok9eCaO36607NN46/K9EqFM1M1wxFKfA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752248600; c=relaxed/simple; bh=X4OMalfak3+mVNJ6hPZvojzo4/zMhqoNofhoFoNgwK8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=TnIrbflqpbV4mVggtQ4G0czxw91BQMpkKpeLHWg8RDeU5ZjLOGYw5Q0588p9h5c6tMhbJKgMJl/0+ngP4GLWYNntaFlFYwIwcUcRawVwx6wKKN4w2qKDUhlJZIWMAVO2ehuORpKDNdFmjim5VhW4GFeApnbrDIkqRvKFeunqbxM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=buVz6wBK; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=lrfIPlZn; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=buVz6wBK; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=lrfIPlZn; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="buVz6wBK"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="lrfIPlZn"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="buVz6wBK"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="lrfIPlZn" 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-out2.suse.de (Postfix) with ESMTPS id C8F1B1F451; Fri, 11 Jul 2025 15:43:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1752248596; 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=QX0qRN0p/QGgY/SoU9mRpHTPekrDiV+O8fwwSQc0kC0=; b=buVz6wBKiiIiyZM6vaFmOTWIxruB5hgLycrzUnFIP3i05lNHYTeXKflgPgUvyQkE5sBN7/ 5egUzgt10/7IGx6jvx3sEv7Z2dsjcu03b49CnR6FNEFnhWXKtum5YJfwkLtR0kMbhzHbme mWkaIPjCBZFp2bnXjMUdJy00Y4uBPGQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1752248596; 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=QX0qRN0p/QGgY/SoU9mRpHTPekrDiV+O8fwwSQc0kC0=; b=lrfIPlZnuLcoU0b08zNg2VqqtM69xC64NHT8F4dxTn1rduqBgJcfhnotwiM4FFB1Y8kIWw 1s4eBVHL3UJ9mTAg== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1752248596; 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=QX0qRN0p/QGgY/SoU9mRpHTPekrDiV+O8fwwSQc0kC0=; b=buVz6wBKiiIiyZM6vaFmOTWIxruB5hgLycrzUnFIP3i05lNHYTeXKflgPgUvyQkE5sBN7/ 5egUzgt10/7IGx6jvx3sEv7Z2dsjcu03b49CnR6FNEFnhWXKtum5YJfwkLtR0kMbhzHbme mWkaIPjCBZFp2bnXjMUdJy00Y4uBPGQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1752248596; 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=QX0qRN0p/QGgY/SoU9mRpHTPekrDiV+O8fwwSQc0kC0=; b=lrfIPlZnuLcoU0b08zNg2VqqtM69xC64NHT8F4dxTn1rduqBgJcfhnotwiM4FFB1Y8kIWw 1s4eBVHL3UJ9mTAg== 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 A3C0B1388B; Fri, 11 Jul 2025 15:43:16 +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 VtBYJxQxcWhtawAAD6G6ig (envelope-from ); Fri, 11 Jul 2025 15:43:16 +0000 Message-ID: <5bc89531-ab09-4690-aae4-a44f9ddb4a68@suse.cz> Date: Fri, 11 Jul 2025 17:43:16 +0200 Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v12 2/4] mm/slub: allow to set node and align in k[v]realloc Content-Language: en-US To: Harry Yoo , Vitaly Wool Cc: linux-mm@kvack.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, Uladzislau Rezki , Danilo Krummrich , Alice Ryhl , rust-for-linux@vger.kernel.org, Lorenzo Stoakes , "Liam R . Howlett" , Kent Overstreet , linux-bcachefs@vger.kernel.org, bpf@vger.kernel.org, Herbert Xu , Jann Horn , Pedro Falcato References: <20250709172345.1031907-1-vitaly.wool@konsulko.se> <20250709172441.1032006-1-vitaly.wool@konsulko.se> From: Vlastimil Babka Autocrypt: addr=vbabka@suse.cz; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9B75qW2plBs+7+YB 87tMFA+u+L4Z5xAzIimfLD5EKC56kJ1CsXlM8S/LHcmdD9Ctkn3trYDNnat0eoAcfPIP2OZ+ 9oe9IF/R28zmh0ifLXyJQQz5ofdj4bPf8ecEW0rhcqHfTD8k4yK0xxt3xW+6Exqp9n9bydiy tcSAw/TahjW6yrA+6JhSBv1v2tIm+itQc073zjSX8OFL51qQVzRFr7H2UQG33lw2QrvHRXqD Ot7ViKam7v0Ho9wEWiQOOZlHItOOXFphWb2yq3nzrKe45oWoSgkxKb97MVsQ+q2SYjJ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RrZE/v5YFHbayRD dJKXobXTtCBYpLJM4ZYRpGZXne/FAtWNe4KbNJJqxMvrTOrnIatPj8NhBVI0RSJRsbilh6TE m6M14QORSWTLRg== In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Level: X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; MIME_TRACE(0.00)[0:+]; RCPT_COUNT_TWELVE(0.00)[17]; RCVD_TLS_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[kvack.org,linux-foundation.org,vger.kernel.org,gmail.com,kernel.org,google.com,oracle.com,linux.dev,gondor.apana.org.au,suse.de]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.cz:mid] X-Spam-Flag: NO X-Spam-Score: -4.30 On 7/11/25 10:58, Harry Yoo wrote: > On Wed, Jul 09, 2025 at 07:24:41PM +0200, Vitaly Wool wrote: >> Reimplement k[v]realloc_node() to be able to set node and >> alignment should a user need to do so. In order to do that while >> retaining the maximal backward compatibility, add >> k[v]realloc_node_align() functions and redefine the rest of API >> using these new ones. >> >> While doing that, we also keep the number of _noprof variants to a >> minimum, which implies some changes to the existing users of older >> _noprof functions, that basically being bcachefs. >> >> With that change we also provide the ability for the Rust part of >> the kernel to set node and alignment in its K[v]xxx >> [re]allocations. >> >> Signed-off-by: Vitaly Wool >> --- >> fs/bcachefs/darray.c | 2 +- >> fs/bcachefs/util.h | 2 +- >> include/linux/bpfptr.h | 2 +- >> include/linux/slab.h | 38 +++++++++++++++---------- >> lib/rhashtable.c | 4 +-- >> mm/slub.c | 64 +++++++++++++++++++++++++++++------------- >> 6 files changed, 72 insertions(+), 40 deletions(-) > >> diff --git a/mm/slub.c b/mm/slub.c >> index c4b64821e680..6fad4cdea6c4 100644 >> --- a/mm/slub.c >> +++ b/mm/slub.c >> @@ -4845,7 +4845,7 @@ void kfree(const void *object) >> EXPORT_SYMBOL(kfree); >> >> static __always_inline __realloc_size(2) void * >> -__do_krealloc(const void *p, size_t new_size, gfp_t flags) >> +__do_krealloc(const void *p, size_t new_size, unsigned long align, gfp_t flags, int nid) >> { >> void *ret; >> size_t ks = 0; >> @@ -4859,6 +4859,20 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags) >> if (!kasan_check_byte(p)) >> return NULL; >> >> + /* refuse to proceed if alignment is bigger than what kmalloc() provides */ >> + if (!IS_ALIGNED((unsigned long)p, align) || new_size < align) >> + return NULL; > > Hmm but what happens if `p` is aligned to `align`, but the new object is not? > > For example, what will happen if we allocate object with size=64, align=64 > and then do krealloc with size=96, align=64... > > Or am I missing something? Good point. We extended the alignment guarantees in commit ad59baa31695 ("slab, rust: extend kmalloc() alignment guarantees to remove Rust padding") for rust in a way that size 96 gives you alignment of 32. It assumes that rust side will ask for alignments that are power-of-two and sizes that are multiples of alignment. I think if that assumption is still honored than this will keep working, but the check added above (is it just a sanity check or something the rust side relies on?) doesn't seem correct? >> + /* >> + * If reallocation is not necessary (e. g. the new size is less >> + * than the current allocated size), the current allocation will be >> + * preserved unless __GFP_THISNODE is set. In the latter case a new >> + * allocation on the requested node will be attempted. >> + */ >> + if (unlikely(flags & __GFP_THISNODE) && nid != NUMA_NO_NODE && >> + nid != page_to_nid(virt_to_page(p))) >> + goto alloc_new; >> + >> if (is_kfence_address(p)) { >> ks = orig_size = kfence_ksize(p); >> } else { >