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 704FCC3DA61 for ; Tue, 30 Jul 2024 01:35:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3BE16B007B; Mon, 29 Jul 2024 21:35:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BEC876B0083; Mon, 29 Jul 2024 21:35:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB3EA6B0085; Mon, 29 Jul 2024 21:35:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8DA586B007B for ; Mon, 29 Jul 2024 21:35:40 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 14029401B5 for ; Tue, 30 Jul 2024 01:35:40 +0000 (UTC) X-FDA: 82394702040.11.C978A7D Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf09.hostedemail.com (Postfix) with ESMTP id BD38F14000D for ; Tue, 30 Jul 2024 01:35:37 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="e/HVw/UI"; spf=pass (imf09.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=1722303284; 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=bNhXhXaIcjbCwFl4VHbdOIvjNHJPzOFGHg/tbvi3vow=; b=q0zRtsRiPVvbjFWDebbzQ/zvHbOJ3qtYpnjmihY7cBdpz/xkoggeMQP+DqX6fwPrWhgTl9 N44kM76Uml7rQ5XfrnL3n9zg7qQHGOVMzFY8UFhFxIWS+14ZbYHNp9JCsRgyusAY8m8/6d +spmpg9HueKUQsH5GK0b7k2GGwnLMKY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722303284; a=rsa-sha256; cv=none; b=nzKeINS+kkCVyUQh2aUu7teGMb6Rk2mKafmXQx9vccovMorYB/DrW+Hc36Euqfo8HMOzS5 ogihbRFaEjwyuZ+lrWZXSMC3RXaNFG4TCNZ6VLNg629IiGVU+NwETcH8x8aYsRr51WS6Ya O3JUiidk+52GLJoR1QNYd9lcy1rdIc8= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="e/HVw/UI"; spf=pass (imf09.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 60E05CE0D9A; Tue, 30 Jul 2024 01:35:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E21CC32786; Tue, 30 Jul 2024 01:35:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722303333; bh=tjRXVxEBxPBBVZ733r7YQOqR128QdPVyjbi3KAokkrY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=e/HVw/UIO02orHDQuNkXwBP8riFSZt0PcpC6BHynhd/Url8RSKNn47srp/9LillSP 4FQU8OaLpIF8eUI3Cf4bfcam5sW380/aZKuHWSxGfw8y0w9qhCUyvni513dLKZfYSy UGI9QhRFZ9mR24cdL+BOsnulPJftEg23SgZ8hCs4cwlua2PNTn5BmFNAu1MBFqGgxL eKAwrVFQG8c5CFLPDtMt6If+Buja+VwwAgcm527cMDlp69jQbcA1aybzAcD0FQnSvY hMZjTwHgpU4tm3mH39UqehLcIAT8iuMlP0lVmrLjN6aozYxPHnBqL3FZfPgVr+6VjO ChPx91wh5Wjmg== Date: Tue, 30 Jul 2024 03:35:26 +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-Stat-Signature: yjrw4hr5k4pxim4pzdqyqwwa9qr19tca X-Rspamd-Queue-Id: BD38F14000D X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722303337-124502 X-HE-Meta: U2FsdGVkX1/qpLBCyB7kw2ex6Y5sGA1CEFKVxlcwUhPFh+VeYvu0wJQWOUaoH4REaKxKzE+EWJhdv7O6DY+WsR9CVPws9YG3tho4eNs+tSo9G05UnDtN0qp1uOGCTw73WxSJROLV+DUpnRnHVB1PWiaE8JWwSuQ7HP5n+Mfaz/rx19C22cjQ6Cx2+0CFPtJTZ6xCUHJDP6HdSuGacyqEIG9ThikdQ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OdyL5P15T/syq4nG3thNu+Gip0LPkvG0QBW6Nt8jk4H7Somr/c 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 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 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). Maybe I spoke a bit to soon with this last paragraph. I think continuously gowing something with __GFP_ZERO is a legitimate use case. I just did a quick grep for users of krealloc() with __GFP_ZERO and found 18 matches. So, I think, at least for now, we should instead document that __GFP_ZERO is only fully honored when the buffer is grown continuously (without intermediate shrinking) and __GFP_ZERO is supplied in every iteration. In case I miss something here, and not even this case is safe, it looks like we have 18 broken users of krealloc(). > > > > > 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) > > >