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 4169FC3DA61 for ; Mon, 29 Jul 2024 19:08:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5C016B0083; Mon, 29 Jul 2024 15:08:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E4F26B0085; Mon, 29 Jul 2024 15:08:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 885576B0088; Mon, 29 Jul 2024 15:08:24 -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 690C36B0083 for ; Mon, 29 Jul 2024 15:08:24 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1327C16062F for ; Mon, 29 Jul 2024 19:08:24 +0000 (UTC) X-FDA: 82393726128.19.59E3735 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf04.hostedemail.com (Postfix) with ESMTP id A95C140007 for ; Mon, 29 Jul 2024 19:08:21 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=vLNuCjmZ; spf=pass (imf04.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722280048; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wPUBAvsq5MSmCL6yEr8FG7lTdDVJL5W9vrQQcmPYGN0=; b=lAjPQ6Ah4osbhnujiEV8X4OL7gZXuomVcevTOTTjq6c3x8S9XMztFmpGtse7ycvOOV6EHV j7mmtUjxj6NPL3xMw38gUl7feMuFCU5JfZCbQHXRUbJ92XHyuGkkg3fiSg1KVhy0Tzb0jV JChdzlX5NI8533wHh4YZan6NaAPIdz0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722280048; a=rsa-sha256; cv=none; b=VyR3NQWHD2BVr7u2dhB9wuMngmO2mqmMZoRdIzjhJckmXplqIBAqbAI7KcxumrMMBE4JqU O0ScmhIZvvVodinmReMj6uJmFT1350crFaVJbvh4ajh3s4OXVZN9hdqCKwp3l+33tsPhjX 0pHyIgHwMa/Gh/qcjoZiiu70y4phkNo= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=vLNuCjmZ; spf=pass (imf04.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id B3F09CE0BAB; Mon, 29 Jul 2024 19:08:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4295C32786; Mon, 29 Jul 2024 19:08:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722280096; bh=qDvC2tKPMuPdLTZF5Dg2OfAR2d+C5UOUdCHxbkrsbH4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=vLNuCjmZIzJq/yhSL4ej3IRwlABhGjNQVhVFq9bEmVDBLnXAJSQoEFXwAHJLTWeD6 owKDGYxGgq1Qmp4wfS6VChSn52IKfYbWlzrVBgoUtczTSVbfpuejosr17PLnXBuZb6 4e7CzYkOqf7NjVTlZLtekjZ3ayoqqRApRkC+YM7UQ+Fs6Pr1dHe5yGXvLjxNjPzNIN wWlqrB7vbmKkdOcssGJDEC9iYT/9jyHjpQWJiBHSxKe1Uld7R7uirSW7lRLWDpgBvU qsaeEWnkYA5TfVkV7bTY++5H0ochHFV7yvqYQVmlURHjNAE5Vahsz64Gb0cxXHNntg aEEqAitgZpgSw== Date: Mon, 29 Jul 2024 21:08:08 +0200 From: Danilo Krummrich To: Vlastimil Babka 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 Subject: Re: [PATCH v2 1/2] mm: vmalloc: implement vrealloc() Message-ID: References: <20240722163111.4766-1-dakr@kernel.org> <20240722163111.4766-2-dakr@kernel.org> <07491799-9753-4fc9-b642-6d7d7d9575aa@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: A95C140007 X-Stat-Signature: pnawwm6h3zmeiiqfjufg6adg8jcjjbp8 X-HE-Tag: 1722280101-718281 X-HE-Meta: U2FsdGVkX1+pZ6FstTOMN1zA9x2Vs4ZWQWkF1BL6u/pCIKLtSh66GlUlVppAySKGp2TO6TvG024HlJvBakja5C2bHu3DedJc52Bqk+ZK3xeq9/j3ilN1EiOCbnx15aCesLVlFdyc6qdX+KhWu4dEhyh2Rvq+3FwEFjBs0xySygmUzmiWbvsKBiudnBPQhykW9n6l57xUSp7yJYS2ytV1QgUSXFz+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U0ibnMrEkndSef22Gd4wEK6VCpODztZxSJX+CcI7vADd7GYb9//Jm3iN 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 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 we should indeed just document that __GFP_ZERO doesn't work for re-allocating memory and start to warn about it. As already mentioned, I think we should at least gurantee that *realloc(NULL, size, flags | __GFP_ZERO) is valid, i.e. WARN_ON(p && flags & __GFP_ZERO). > > nommu would still uses krealloc() though... > > > > > But it's also fundamentally not true for krealloc(), or kvrealloc() > > switching from a kmalloc to valloc. ksize() returns the size of the kmalloc > > bucket, we don't know what was the exact prior allocation size. > > Probably a stupid question, but can't we just zero the full bucket initially and > make sure to memset() spare memory in the bucket to zero when krealloc() is > called with new_size < ksize()? > > > Worse, we > > started poisoning the padding in debug configurations, so even a > > kmalloc(__GFP_ZERO) followed by krealloc(__GFP_ZERO) can give you unexpected > > poison now... > > As in writing magics directly to the spare memory in the bucket? Which would > then also be copied over to a new buffer in __do_krealloc()? > > > > > I guess we should just document __GFP_ZERO is not honored at all for > > realloc, and maybe start even warning :/ Hopefully nobody relies on that. > > I think it'd be great to make __GFP_ZERO work in all cases. However, if that's > really not possible, I'd prefer if we could at least gurantee that > *realloc(NULL, size, flags | __GFP_ZERO) is a valid call, i.e. > WARN_ON(p && flags & __GFP_ZERO). > > > > > > + * > > > + * If @p is %NULL, vrealloc() behaves exactly like vmalloc(). If @size is 0 and > > > + * @p is not a %NULL pointer, the object pointed to is freed. > > > + * > > > + * Return: pointer to the allocated memory; %NULL if @size is zero or in case of > > > + * failure > > > + */ > > > +void *vrealloc_noprof(const void *p, size_t size, gfp_t flags) > > > +{ > > > + size_t old_size = 0; > > > + void *n; > > > + > > > + if (!size) { > > > + vfree(p); > > > + return NULL; > > > + } > > > + > > > + if (p) { > > > + struct vm_struct *vm; > > > + > > > + vm = find_vm_area(p); > > > + if (unlikely(!vm)) { > > > + WARN(1, "Trying to vrealloc() nonexistent vm area (%p)\n", p); > > > + return NULL; > > > + } > > > + > > > + old_size = get_vm_area_size(vm); > > > + } > > > + > > > + if (size <= old_size) { > > > + /* > > > + * TODO: Shrink the vm_area, i.e. unmap and free unused pages. > > > + * What would be a good heuristic for when to shrink the > > > + * vm_area? > > > + */ > > > + return (void *)p; > > > + } > > > + > > > + /* TODO: Grow the vm_area, i.e. allocate and map additional pages. */ > > > + n = __vmalloc_noprof(size, flags); > > > + if (!n) > > > + return NULL; > > > + > > > + if (p) { > > > + memcpy(n, p, old_size); > > > + vfree(p); > > > + } > > > + > > > + return n; > > > +} > > > + > > > #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) > > > #define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL) > > > #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) > >