From patchwork Mon Jul 22 16:29:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13738938 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 A0710C3DA59 for ; Mon, 22 Jul 2024 16:31:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C45776B0085; Mon, 22 Jul 2024 12:31:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCE5C6B0088; Mon, 22 Jul 2024 12:31:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6EC86B0089; Mon, 22 Jul 2024 12:31:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8646C6B0085 for ; Mon, 22 Jul 2024 12:31:47 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F25AF1C394A for ; Mon, 22 Jul 2024 16:31:46 +0000 (UTC) X-FDA: 82367929812.30.B4A82DF Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 5AA2640002 for ; Mon, 22 Jul 2024 16:31:44 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QhrdBaZI; spf=pass (imf04.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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=1721665859; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=8viO8FBna7Vk0uL4IYRcdWheISQVCuU1t/YhjO7JoUE=; b=mk7ZxwQ6dIZRkKbxCBZl9eRrqL9kfhIXRQaggjVzx5uDBMR7h+QFTV9Oz634xRf7TDU8fA 4Q5Gv/9bANPnNIGZc3osCk8mg4L1oxvOzJtT1dviHX19uKsA8TKA2+uXR2x4en5L//hain u7snF6OAA4gvZQ6ukY8pGaBaK4iZ2AY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721665859; a=rsa-sha256; cv=none; b=vBtI/nB2Zl6Tao0WOBoOYhGq5vLqG0ip3qAxpC5f3F9gCWHEOYEf+7uLfP7GtYsFQgBpFU 7YU34KaGfO2xx2lPsoUkbo0yvq+EjomcBlcek6vJ6kIgOT5fzvX9d8A4wSVghrIlGJE/Cx 0Ws3DcvnaVRgEn3kYpTKmlO9p9Yy26U= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QhrdBaZI; spf=pass (imf04.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 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 dfw.source.kernel.org (Postfix) with ESMTP id 2999360B6A; Mon, 22 Jul 2024 16:31:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C6ECC116B1; Mon, 22 Jul 2024 16:31:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721665902; bh=CkDc4sr4ZxAi/U/UIXbOEimKRnQn1vJDruoUwJilY5E=; h=From:To:Cc:Subject:Date:From; b=QhrdBaZIfTLINR2GFGCAUAZHlmj3VghRtvcAZr6ak8+xjsLM9/4WV8cW7lKrhI3Iu +hZ4dQkGdUKt251MhyyuQQBPOcEBF8FZHkIs4luk1CdYg4fuaNTyOsSD2DUO/2vqyZ FKiMpdfd/sW5ltp3BCjcZiDzhNyfHB0f1mgK0WYk5AuKhn+Pywnc7BCaZyi4NQ3V3x 5qqqtr1/3/Uz5mQNWdNvfT20OdRyhiAqUaSD5hTPCK6NlbTM6L1J1l1gc7GgsUnk4s u51wSKTLz6pp90GldWhJSFEM8k+Ut4eg+nhEKtXssCAbzWFTpAmVMTXT82IwFO0wr4 Es6ICwRpaQI3w== From: Danilo Krummrich To: cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, vbabka@suse.cz, 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 Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 0/2] Align kvrealloc() with krealloc() Date: Mon, 22 Jul 2024 18:29:22 +0200 Message-ID: <20240722163111.4766-1-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Rspamd-Queue-Id: 5AA2640002 X-Stat-Signature: iicm8g65pscrbfi94mekd5waofb7uxie X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1721665904-417435 X-HE-Meta: U2FsdGVkX1+9F2ryeq0uAD8YyJ8wHITgZVlr11IEZxUJVRMr9mNvkPqE7tmaeaf0cvqsCojCi9ZOQsTSPOxvGsXo+YcXs2Yev0vJRGDGQNDUpGrf7GpFgRvvpOH+zddTEOj6QJ4kd6btswCiiqgZOv2gFAcrYl0KXBegBUE+Yu6upXlQPoCZRUSXpAlhG70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pCUO6k67EUsu8/S0TK52vh4aHZdMHhs5+PQ3UzNiLd94ZjoLwWZSsXwxNKI= 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: Hi, Besides the obvious (and desired) difference between krealloc() and kvrealloc(), there is some inconsistency in their function signatures and behavior: - krealloc() frees the memory when the requested size is zero, whereas kvrealloc() simply returns a pointer to the existing allocation. - krealloc() behaves like kmalloc() if a NULL pointer is passed, whereas kvrealloc() does not accept a NULL pointer at all and, if passed, would fault instead. - krealloc() is self-contained, whereas kvrealloc() relies on the caller to provide the size of the previous allocation. Inconsistent behavior throughout allocation APIs is error prone, hence make kvrealloc() behave like krealloc(), which seems superior in all mentioned aspects. In order to be able to get rid of kvrealloc()'s oldsize parameter, introduce vrealloc() and make use of it in kvrealloc(). Making use of vrealloc() in kvrealloc() also provides oppertunities to grow (and shrink) allocations more efficiently. For instance, vrealloc() can be optimized to allocate and map additional pages to grow the allocation or unmap and free unused pages to shrink the allocation. Besides the above, those functions are required by Rust's allocator abstractons [1] (rework based on this series in [2]). With `Vec` or `KVec` respectively, potentially growing (and shrinking) data structures are rather common. The patches of this series can also be found in [3]. [1] https://lore.kernel.org/lkml/20240704170738.3621-1-dakr@redhat.com/ [2] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/mm [3] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=mm/krealloc Changes in v2: - remove unnecessary extern and move __realloc_size to a new line for vrealloc_noprof() and kvrealloc_noprof() - drop EXPORT_SYMBOL for vrealloc_noprof() - rename to_kmalloc_flags() to kmalloc_gfp_adjust() - fix missing NULL check in vrealloc_noprof() - rephrase TODO comments in vrealloc_noprof() Danilo Krummrich (2): mm: vmalloc: implement vrealloc() mm: kvmalloc: align kvrealloc() with krealloc() arch/arm64/kvm/nested.c | 1 - arch/powerpc/platforms/pseries/papr-vpd.c | 5 +- drivers/gpu/drm/drm_exec.c | 3 +- fs/xfs/xfs_log_recover.c | 2 +- include/linux/slab.h | 4 +- include/linux/vmalloc.h | 4 + kernel/resource.c | 3 +- lib/fortify_kunit.c | 3 +- mm/util.c | 89 +++++++++++++++-------- mm/vmalloc.c | 59 +++++++++++++++ 10 files changed, 129 insertions(+), 44 deletions(-) base-commit: 933069701c1b507825b514317d4edd5d3fd9d417 Acked-by: Michal Hocko