From patchwork Mon Aug 12 22:34:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13761078 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 3DD4DC52D7C for ; Mon, 12 Aug 2024 22:37:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5CC66B0095; Mon, 12 Aug 2024 18:37:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE5B76B0098; Mon, 12 Aug 2024 18:37:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A856A6B009A; Mon, 12 Aug 2024 18:37:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 87F8E6B0095 for ; Mon, 12 Aug 2024 18:37:27 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2F0C5A045B for ; Mon, 12 Aug 2024 22:37:27 +0000 (UTC) X-FDA: 82445056134.13.791299B Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf09.hostedemail.com (Postfix) with ESMTP id CA56714000A for ; Mon, 12 Aug 2024 22:37:24 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NgwnhIBt; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723502234; a=rsa-sha256; cv=none; b=K852z4j2/+CuU1yHEuprG2/BWkpqigFbhiTNZtb8SiFvCPHpHvL4f7vGk4oG8RExMIhPL1 bxw/F3NJVivhJX92fGbcNh1qG7VJZHI9MB5xHc+fg3p65GVs9E5Q+LNM7ddyjmqH4p9jwZ axQWXeZ0fZFp213KWkzdaBsHaCTJ/uQ= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NgwnhIBt; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723502234; 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:in-reply-to:references:references:dkim-signature; bh=rvk+v7p7ZKPmOy3QyCUS451Ml16DqucCxi8dZF74SCo=; b=DFgc4iJxKLtaMVznQDiI7E8z+Cnl2CXkhvO54fFO/MowbHWSVjZpQ5aOEejD9KUF5lc50t HxCC5t408xS47/2feI6TtCxKhCqguvQmioCVHdODi2igx8Gc2NGb73MHdEGeIoTAm6YFl5 rOKoKBU2xWns38V+M3uoEM99O3Z4Ae4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id BFC51CE10FE; Mon, 12 Aug 2024 22:37:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91499C4AF0E; Mon, 12 Aug 2024 22:37:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723502239; bh=raeKwom20BEIx8mfvC/p6iMwG5L5vKHG/+gLhpcw2wU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NgwnhIBtrgdzhJrEEVGhm+SZ9S75g8uE4ZD/Z6YnWtAcDhIiTr3u2QUucxhDTE3Jx Fz2nqABIzpCgORqas1LsGv+vYKrG6ry8k45ZK26wbJN5Dn5vwxYmJ7q5IB9uatbb0J 9LoSAg5v0HBlXSzATbuZgGcqk7GCq+lKznAlJzg1tbDMz7cEntEkYeI1jDKLTaHi4V G4sSC4NJ5KZH3IqzMcJN8mmPXLE3OCTH2vR7SLnPZvjGu07lMPA1b+rqtffXV3A5Qw g9kKZYkTA4D/bHp/xSsJ67vNXacGLyqHE1Gv4zFKl1sTNZn3bKQJy3uosLpNhRz70X Av317cUR7y90Q== From: Danilo Krummrich To: akpm@linux-foundation.org, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Danilo Krummrich Subject: [PATCH v2 2/2] mm: krealloc: clarify valid usage of __GFP_ZERO Date: Tue, 13 Aug 2024 00:34:35 +0200 Message-ID: <20240812223707.32049-2-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240812223707.32049-1-dakr@kernel.org> References: <20240812223707.32049-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: CA56714000A X-Rspamd-Server: rspam01 X-Stat-Signature: 8dbwtnn6n3nfji4mo1ix4xm1xd57k56n X-HE-Tag: 1723502244-601808 X-HE-Meta: U2FsdGVkX19mE8KMouJzNuml2Q3XZ/3a26WObdeQwC8Vrfso82IJVxtHavBZFYHn8DlInPF+XmqF4CxvkqQ26nJoGNHHWlYFD4FByGgA2N+KFDw6fKRzrYhwYcL65vN8lf37ZxLml6t06t5CinaY7lKG7BwvkUJ0opDSeUqX/N88ow2rsPdnA6VoajYWrr5osjD0VwqOyKRArSXlKA9MLqfcZ88GqaG3iul0rUDkl9GnCRV6U95g16rU/NJLqn1h8KIPAQ24jfnvAV6f3egR2NW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ter, 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: Properly document that if __GFP_ZERO logic is requested, callers must ensure that, starting with the initial memory allocation, every subsequent call to this API for the same memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that __GFP_ZERO is not fully honored by this API. Signed-off-by: Danilo Krummrich Acked-by: David Rientjes --- Changes in v2: - additionally to what we require callers to do, briefly explain what the implementation does (Andrew) --- include/linux/slab.h | 10 ++++++++++ mm/slab_common.c | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index c9cb42203183..2282e67a01c7 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -733,6 +733,16 @@ static inline __alloc_size(1, 2) void *kmalloc_array_noprof(size_t n, size_t siz * @new_n: new number of elements to alloc * @new_size: new size of a single member of the array * @flags: the type of memory to allocate (see kmalloc) + * + * If __GFP_ZERO logic is requested, callers must ensure that, starting with the + * initial memory allocation, every subsequent call to this API for the same + * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that + * __GFP_ZERO is not fully honored by this API. + * + * See krealloc_noprof() for further details. + * + * In any case, the contents of the object pointed to are preserved up to the + * lesser of the new and old sizes. */ static inline __realloc_size(2, 3) void * __must_check krealloc_array_noprof(void *p, size_t new_n, diff --git a/mm/slab_common.c b/mm/slab_common.c index cff602cedf8e..1b380eb3b4f2 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1301,11 +1301,27 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags) * @new_size: how many bytes of memory are required. * @flags: the type of memory to allocate. * - * The contents of the object pointed to are preserved up to the - * lesser of the new and old sizes (__GFP_ZERO flag is effectively ignored). * If @p is %NULL, krealloc() behaves exactly like kmalloc(). If @new_size * is 0 and @p is not a %NULL pointer, the object pointed to is freed. * + * If __GFP_ZERO logic is requested, callers must ensure that, starting with the + * initial memory allocation, every subsequent call to this API for the same + * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that + * __GFP_ZERO is not fully honored by this API. + * + * This is the case, since krealloc() only knows about the bucket size of an + * allocation (but not the exact size it was allocated with) and hence + * implements the following semantics for shrinking and growing buffers with + * __GFP_ZERO. + * + * new bucket + * 0 size size + * |--------|----------------| + * | keep | zero | + * + * In any case, the contents of the object pointed to are preserved up to the + * lesser of the new and old sizes. + * * Return: pointer to the allocated memory or %NULL in case of error */ void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)