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 90F20C83F17 for ; Mon, 14 Jul 2025 08:14:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 20DEF6B007B; Mon, 14 Jul 2025 04:14:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E56D6B008A; Mon, 14 Jul 2025 04:14:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0FB776B0096; Mon, 14 Jul 2025 04:14:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id F32566B007B for ; Mon, 14 Jul 2025 04:14:13 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4DE211DA34D for ; Mon, 14 Jul 2025 08:14:13 +0000 (UTC) X-FDA: 83662157586.04.BCA224B Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf17.hostedemail.com (Postfix) with ESMTP id BD2D140008 for ; Mon, 14 Jul 2025 08:14:10 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=a9k0+0DU; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=dpcnJWFj; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=mamSDHXH; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=gq58d71Y; spf=pass (imf17.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752480851; 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=oLZSSKrSwR5i5R+ZxG6wpsVx5R6CJ87qoC7NyByXS8Y=; b=O0CbguvW2EFOmnkp0CgPl7JqTSQ23Sv9yXz6tWrF1HricPchB8Ho37/S5Ehr5WtNfinkzJ uZRJ0tspmHPJW4kWTLsiH/ASGhr3bNYlXJVOH0Eq37HiJjqa8IDjIQVtN0nWSbSLLrGlYc YAVKC/91dxi1E5ilDGyi4y4f3ADVJj4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=a9k0+0DU; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=dpcnJWFj; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=mamSDHXH; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=gq58d71Y; spf=pass (imf17.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752480851; a=rsa-sha256; cv=none; b=VRP98VKHXQ1APclxDGXEndwb9pmQ/f+QG63LeTtbBL8d6Gf4H3hARDUC6I/mcqIiRllV8S GwZlVlnWxCXjnJYHcM9ECoC37Yr5JVo5go6hZGCMnrF/yOHWICZZqq7De38UgHlxpKZNnt nVBbwXJeO6+n4N3Xe+JWrmmf6JzaP9c= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104: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 CCB5B1F387; Mon, 14 Jul 2025 08:14:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1752480849; 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=oLZSSKrSwR5i5R+ZxG6wpsVx5R6CJ87qoC7NyByXS8Y=; b=a9k0+0DUgZ+2NNuTdMbJoo1Y5Q0zy6GMb/MkRjDMrstrGNWv5OkmiHnH6jNPucrCajF0Cx O9KlFRpmUGGcHFqpsALCmnYUv4VYBnkUz0AsfNnDA55b3HbCZJz4yqZFoeWPXxO5dLNr3C xg1YrMVXAeEqA9NBM+BuPyALea1S2/Q= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1752480849; 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=oLZSSKrSwR5i5R+ZxG6wpsVx5R6CJ87qoC7NyByXS8Y=; b=dpcnJWFjkB42gHX2Xh61tgF0ar4MVlp0HBxcHI73dYPj2xoJESeUSFOQJWO1dgliEG4ZPs ktFsgO9C0+bPBlCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1752480846; 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=oLZSSKrSwR5i5R+ZxG6wpsVx5R6CJ87qoC7NyByXS8Y=; b=mamSDHXHZWkLnGgbg19zG6gMVU+zJ57lV2t/JJ4e2ASq66TrP7V4YqtdyLunpYRSUV6xaU p4UYLAtB/ceAGan1kO959AkMtjhaG6WULOzR/xlMFP9RLDnunPIJgM18UqJcE8JYRuZWso TMtAUIwA4OumM5nn7ZBFAQXCkhokkFA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1752480846; 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=oLZSSKrSwR5i5R+ZxG6wpsVx5R6CJ87qoC7NyByXS8Y=; b=gq58d71YYaI0uMOCONBGIgtW5YJWbpuTP4grXzqc4SVuvE8YQZa5W6QcA5NRv/nyTCBgF8 ivA596bA9koOpJBw== 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 A8BD413306; Mon, 14 Jul 2025 08:14:06 +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 af6ZKE68dGhhKAAAD6G6ig (envelope-from ); Mon, 14 Jul 2025 08:14:06 +0000 Message-ID: <1dedcee0-c5a2-47b3-ae13-315ad437ae1a@suse.cz> Date: Mon, 14 Jul 2025 10:14:06 +0200 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: Vitaly Wool Cc: Harry Yoo , 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> <5bc89531-ab09-4690-aae4-a44f9ddb4a68@suse.cz> <3AD3F7B5-679F-4DC8-968F-9FE991B56A5C@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+q2SYjJRBBH4 8qKhphADYxkIP6yut/eaj9ImvRUZZRi0DTc8xfnvHGTjKbJzC2xpFcY0DQbZzuwsIZ8OPJCc LM4S7mT25NE5kUTG/TKQCk922vRdGVMoLA7dIQrgXnRXtyT61sg8PG4wcfOnuWf8577aXP1x 6mzw3/jh3F+oSBHb/GcLC7mvWreJifUL2gEdssGfXhGWBo6zLS3qhgtwjay0Jl+kza1lo+Cv BB2T79D4WGdDuVa4eOrQ02TxqGN7G0Biz5ZLRSFzQSQwLn8fbwARAQABzSBWbGFzdGltaWwg QmFia2EgPHZiYWJrYUBzdXNlLmN6PsLBlAQTAQoAPgIbAwULCQgHAwUVCgkICwUWAgMBAAIe AQIXgBYhBKlA1DSZLC6OmRA9UCJPp+fMgqZkBQJnyBr8BQka0IFQAAoJECJPp+fMgqZkqmMQ AIbGN95ptUMUvo6aAdhxaOCHXp1DfIBuIOK/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iT6fnzIKmZLY8D/9uo3Ut9yi2YCuASWxr7QQZ lJCViArjymbxYB5NdOeC50/0gnhK4pgdHlE2MdwF6o34x7TPFGpjNFvycZqccSQPJ/gibwNA zx3q9vJT4Vw+YbiyS53iSBLXMweeVV1Jd9IjAoL+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: <3AD3F7B5-679F-4DC8-968F-9FE991B56A5C@konsulko.se> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Action: no action X-Rspamd-Queue-Id: BD2D140008 X-Stat-Signature: ofr6zjgm5jaydai1tngyawim6sft6tpb X-Rspamd-Server: rspam02 X-Rspam-User: X-HE-Tag: 1752480850-90547 X-HE-Meta: U2FsdGVkX1+Iyq97vf51enyb5xY7JccaTIe64MWZ1bKkU7yZeefpozk9yTsfi5LMegvmthEBlZ3WGvzCm+WybEJ5FAa/TZZXnyNZ9vuoroKrwTJQbyynqW2RLuVsQbt0ioiR60nEw6vn3XdGDfJqFUIrtyQi6sbUNz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hQKThT/vO5K7CoyJSBgsSU/DBTIT8pYe4IzYXqgjIncTVexg+OMkaaQWtXnrD34B+cWiA== 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 7/12/25 14:43, Vitaly Wool wrote: > > >> On Jul 11, 2025, at 5:43 PM, Vlastimil Babka wrote: >> >> On 7/11/25 10:58, Harry Yoo wrote: >>> On Wed, Jul 09, 2025 at 07:24:41PM +0200, Vitaly Wool wrote: >>>> 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? >> > > It is a sanity check and it should have looked like this: > > if (!IS_ALIGNED((unsigned long)p, align) && new_size <= ks) > return NULL; > > and the reasoning for this is the following: if we don’t intend to reallocate (new size is not bigger than the original size), but the user requests a larger alignment, it’s a miss. Does that sound reasonable? So taking a step back indeed the align passed to krealloc is indeed used only for this check. If it's really just a sanity check, then I'd rather not add this parameter to krealloc functions at all - kmalloc() itself also doesn't have it, so it's inconsistent that krealloc() would have it - but only to return NULL and not e.g. try to reallocate for alignment. If it's not just a sanity check, it means it's expected that for some sequence of valid kvrealloc_node_align() calls it can return NULL and then rely on the fallback to vmalloc. That would be rather wasteful for the cases like going from 64 to 96 bytes etc. So in that case it would be better if krealloc did the reallocation, same as in cases when size increases. Of course it would still have to rely on the documented alignment guarantees only and not provide anything arbitrary. aligned_size() in rust/kernel/alloc/allocator.rs is responsible for that, AFAIK. And I think it's not a sanity check but the latter - if the following is a valid k(v)realloc sequence (from Rust POV). The individual size+align combinations AFAIK are, but if it's valid to make them follow one another them like this, I don't know. krealloc(size=96, align=32) -> can give object with 32 alignment only krealloc(size=64, align=64) -> doesn't increase size but wants alignment 64 > ~Vitaly >