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 C7102C3DA49 for ; Tue, 30 Jul 2024 13:56:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E5716B0082; Tue, 30 Jul 2024 09:56:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 16F576B0085; Tue, 30 Jul 2024 09:56:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F03856B0089; Tue, 30 Jul 2024 09:56:51 -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 CECDD6B0082 for ; Tue, 30 Jul 2024 09:56:51 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5190616026C for ; Tue, 30 Jul 2024 13:56:51 +0000 (UTC) X-FDA: 82396569822.23.F16F59B Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf29.hostedemail.com (Postfix) with ESMTP id 1FEA7120025 for ; Tue, 30 Jul 2024 13:56:48 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=yzGxhFWz; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=cRVQ0sHw; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=yzGxhFWz; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=cRVQ0sHw; spf=pass (imf29.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 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=1722347782; 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=tTjsllBsZ0Sc2hQb4mXDp4REPh1mWvWiYq8KYR/uMy4=; b=kpFVbSiPXpECnwGq2uKYsa0kao7Liwv6bp2cjmmfqhhD65RN99sTGAymodRIemWEWcWRZd WzZTH8PtCcOVtgk6QvVAkPLjO5aaHHOKHLyiTzR2LUKfP/PsLfigJr2AxE5Ksl3BLnV1wl qUXfYq6M9WYvGnOfh5b8Wr3LM5lUPfU= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=yzGxhFWz; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=cRVQ0sHw; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=yzGxhFWz; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=cRVQ0sHw; spf=pass (imf29.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722347782; a=rsa-sha256; cv=none; b=aUvyOTXgOXlgqT9gsz7GjuwrAzpqnIYrx3ZNUOQ9QgNQ2ZS3x3hvakeU2Y7iugk7MhJmJp jW4yFhqewuCTqziD+7OdrLtYku0qj6hSZ1wqd5pkuGE9dfCJQwMAvuZCXLaqGURLZYn/h8 pK8eC8PuFAeIonyl5q6SDPdShkrCUHo= 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 6C6D721B2B; Tue, 30 Jul 2024 13:56:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1722347807; 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; bh=tTjsllBsZ0Sc2hQb4mXDp4REPh1mWvWiYq8KYR/uMy4=; b=yzGxhFWzz7dOIJzAQLsBT9HLwvB2Ln77ZvsoCk3l6nznhzgyjxXNqe+4aHSlpGMGWdMjva Ze7hzU78gGJHYYN/aH/4B++16zYpx1vQl8yMgDrp2uA0JhEyNpf1gDGqE1DrTHMtO31RC5 8BIorIKYcIFAkCLJQwlraozEGhDIjyU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1722347807; 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; bh=tTjsllBsZ0Sc2hQb4mXDp4REPh1mWvWiYq8KYR/uMy4=; b=cRVQ0sHwaJRCVsq3QLFGsWcx3dI7RemwmM2tLJ+cFFC+uAdWif4pdAWLYYqKQlM5ufaiTw m2nToJbgBO6RAHBQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1722347807; 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; bh=tTjsllBsZ0Sc2hQb4mXDp4REPh1mWvWiYq8KYR/uMy4=; b=yzGxhFWzz7dOIJzAQLsBT9HLwvB2Ln77ZvsoCk3l6nznhzgyjxXNqe+4aHSlpGMGWdMjva Ze7hzU78gGJHYYN/aH/4B++16zYpx1vQl8yMgDrp2uA0JhEyNpf1gDGqE1DrTHMtO31RC5 8BIorIKYcIFAkCLJQwlraozEGhDIjyU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1722347807; 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; bh=tTjsllBsZ0Sc2hQb4mXDp4REPh1mWvWiYq8KYR/uMy4=; b=cRVQ0sHwaJRCVsq3QLFGsWcx3dI7RemwmM2tLJ+cFFC+uAdWif4pdAWLYYqKQlM5ufaiTw m2nToJbgBO6RAHBQ== 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 AE61813983; Tue, 30 Jul 2024 13:56:46 +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 tlP5KR7xqGYoYwAAD6G6ig (envelope-from ); Tue, 30 Jul 2024 13:56:46 +0000 Message-ID: Date: Tue, 30 Jul 2024 15:58:25 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 1/2] mm: vmalloc: implement vrealloc() To: Danilo Krummrich Cc: cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, urezki@gmail.com, hch@infradead.org, kees@kernel.org, ojeda@kernel.org, wedsonaf@gmail.com, mhocko@kernel.org, mpe@ellerman.id.au, chandan.babu@oracle.com, christian.koenig@amd.com, maz@kernel.org, oliver.upton@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Feng Tang , kasan-dev References: <20240722163111.4766-1-dakr@kernel.org> <20240722163111.4766-2-dakr@kernel.org> <07491799-9753-4fc9-b642-6d7d7d9575aa@suse.cz> <44fa564b-9c8f-4ac2-bce3-f6d2c99b73b7@suse.cz> From: Vlastimil Babka Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 1FEA7120025 X-Stat-Signature: nrsp87xkx7cbbnoey8xe3p64xgbgqrmk X-HE-Tag: 1722347808-414862 X-HE-Meta: U2FsdGVkX19ZtEjiTXU96TgW+3ZqVKaxFAJ94/nYzQJjUgb8EtvjDWwiMmF2jE7lxrn/zuQJf/nL/YVEKDMmcXa6rPLhvY+uxofpq839edXlq75S8keZbiFClNLqi9a9HmFiyRGtvPi758wHs622TmgL8vgb+u6NFftZce7ddF0iKBud6CJRc1WVpelo0IvVBJOnU4gs3dZlGNuhzrttyX8l7lXRcyXeJtHV7vDrjqxZB32cPopjZDLuw9GlPwhYVWPkEvgFcP1mRniQot0hstT5OBINH6CNOIlfypAmoPeVCTH1GqbX803nyLgU4NRIX7AP6OqG5nACF8N9WjJ9YLClxLxN2mwXDuKoN51kMw293Kd2jj54KZ2vmUVMd4douINwOhmioYMA/tjeGlblFlHolP+NWPxDI25N3Rc5z0+ph22P4IATpXoRMwFwtDvPQ/LQIrLCgR2eyEDxBaQ51aXLxJJTlDJsiqOhz7EoDZcbZk3WPH4q+fDP+jIoH7VlDM+1b4gYJSNiybj2PeP+RtQcl8alE1X8py7oaJGKLt9EQfMGGzBaJ2FsbUoei5E+6Ci7jQyEjUxR4fUggZ0kwO5M4oT2icFLO1L9tnB6wiX5d6GFaXJlqtkoVBo1Ph0dQYGHpXH28idSnvHczr2pTcMBcsSU2K+Wos145VRyUFhQhDad3GHXuoTjJtYPO9ri2hbxJSSYBdGBdnrpXT9GRyAnMn9MteXUyCFJ0L+I46tVyeBme2+1g0ac0i4DcxjrdaQuHd3cATqup5KPreiwOZc4UvslSE9xj4xlTeBAi5Cb+LyQF9MTS9t4uRLZc2oOjhqe3TlwOrD3bwis8qo1a64xm2BL1qlIW74+lOzooxqTAHlXzKY7lWlL4MCAOpM69JVZb7hPD3CQc29/cr9zHQiFua+dABPk9m2KsMge7f6Yec/i+1kyx7qYWP4Uw8gzgyd9QcS3nfQ3usiuxit tl4bzMIx BRaJoO3H+rH4hygXRJFP7xz44PyZD0icbg+OFFyOl4yNv9dD1L5JjWQvkUwB9MS5WUH7GfuGaOHGgdRyDmQIx78tD+0voAqOQRtksWQ/1LLShbPkro3myj9VxlC5hGN70XFYPULLkOJLfPHNYE2DpXKFT2Bx1OUDKYvCpL7mxa6idl3yMTz7Mwh4FG7Z4LHRs/48+fYV56b7S1UrnHMq0xQy5JWVjH/vFiANWbdMiKWbNtOmva6Q1CcEzBernGEfJofnFrbyEwRRrO6J9lha8apjo9mCuvV9tx+U5+XyWZF3go6ZADzjCfwY0xIQiWxAnt/6OcqwayL2KGXuJE++x5+SSzgB5pm8wwV+ogeprzM8H2IS5pVkOcTSVAugSdcId8uR+A9O1YTpsvlg= 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/30/24 3:14 PM, Danilo Krummrich wrote: > On Tue, Jul 30, 2024 at 02:15:34PM +0200, Vlastimil Babka wrote: >> On 7/30/24 3:35 AM, Danilo Krummrich wrote: >>> On Mon, Jul 29, 2024 at 09:08:16PM +0200, Danilo Krummrich wrote: >>>> On Fri, Jul 26, 2024 at 10:05:47PM +0200, Danilo Krummrich wrote: >>>>> On Fri, Jul 26, 2024 at 04:37:43PM +0200, Vlastimil Babka wrote: >>>>>> On 7/22/24 6:29 PM, Danilo Krummrich wrote: >>>>>>> Implement vrealloc() analogous to krealloc(). >>>>>>> >>>>>>> Currently, krealloc() requires the caller to pass the size of the >>>>>>> previous memory allocation, which, instead, should be self-contained. >>>>>>> >>>>>>> We attempt to fix this in a subsequent patch which, in order to do so, >>>>>>> requires vrealloc(). >>>>>>> >>>>>>> Besides that, we need realloc() functions for kernel allocators in Rust >>>>>>> too. With `Vec` or `KVec` respectively, potentially growing (and >>>>>>> shrinking) data structures are rather common. >>>>>>> >>>>>>> Signed-off-by: Danilo Krummrich >>>>>> >>>>>> Acked-by: Vlastimil Babka >>>>>> >>>>>>> --- a/mm/vmalloc.c >>>>>>> +++ b/mm/vmalloc.c >>>>>>> @@ -4037,6 +4037,65 @@ void *vzalloc_node_noprof(unsigned long size, int node) >>>>>>> } >>>>>>> EXPORT_SYMBOL(vzalloc_node_noprof); >>>>>>> >>>>>>> +/** >>>>>>> + * vrealloc - reallocate virtually contiguous memory; contents remain unchanged >>>>>>> + * @p: object to reallocate memory for >>>>>>> + * @size: the size to reallocate >>>>>>> + * @flags: the flags for the page level allocator >>>>>>> + * >>>>>>> + * The contents of the object pointed to are preserved up to the lesser of the >>>>>>> + * new and old size (__GFP_ZERO flag is effectively ignored). >>>>>> >>>>>> Well, technically not correct as we don't shrink. Get 8 pages, kvrealloc to >>>>>> 4 pages, kvrealloc back to 8 and the last 4 are not zeroed. But it's not >>>>>> new, kvrealloc() did the same before patch 2/2. >>>>> >>>>> Taking it (too) literal, it's not wrong. The contents of the object pointed to >>>>> are indeed preserved up to the lesser of the new and old size. It's just that >>>>> the rest may be "preserved" as well. >>>>> >>>>> I work on implementing shrink and grow for vrealloc(). In the meantime I think >>>>> we could probably just memset() spare memory to zero. >>>> >>>> Probably, this was a bad idea. Even with shrinking implemented we'd need to >>>> memset() potential spare memory of the last page to zero, when new_size < >>>> old_size. >>>> >>>> Analogously, the same would be true for krealloc() buckets. That's probably not >>>> worth it. >> >> I think it could remove unexpected bad surprises with the API so why not >> do it. > > We'd either need to do it *every* time we shrink an allocation on spec, or we > only do it when shrinking with __GFP_ZERO flag set, which might be a bit > counter-intuitive. I don't think it is that much counterintuitive. > If we do it, I'd probably vote for the latter semantics. While it sounds more > error prone, it's less wasteful and enough to cover the most common case where > the actual *realloc() call is always with the same parameters, but a changing > size. Yeah. Or with hardening enabled (init_on_alloc) it could be done always.