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 5CB11C3DA49 for ; Tue, 30 Jul 2024 20:35:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B02956B007B; Tue, 30 Jul 2024 16:35:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB2B16B0082; Tue, 30 Jul 2024 16:35:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97A816B0089; Tue, 30 Jul 2024 16:35:45 -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 79BB16B007B for ; Tue, 30 Jul 2024 16:35:45 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E2D41A0237 for ; Tue, 30 Jul 2024 20:35:44 +0000 (UTC) X-FDA: 82397575008.30.10F51B5 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id 2371BC002A for ; Tue, 30 Jul 2024 20:35:42 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=LoKwtyZC; dmarc=none; spf=pass (imf10.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722371698; a=rsa-sha256; cv=none; b=AqyLdmljj4VuAx4P7UbUItKaJf9Hmx5Y9EaKvM3jM4VtmJFB4Vgx3wbCoKUTVkSJ/svv5n LrwsMMxkK3gNPckHywctWBWmGooRjfahVkFOZEbWh1PneBf06FjBlGaSQnYiTVUR7zUMIM +1iaL9g22q0e07v2vShxHSGRNx6AF6c= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=LoKwtyZC; dmarc=none; spf=pass (imf10.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722371698; 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=7Gwl/x3II+iKCtXRm4AxdXLN8PpsgsnC8AQBy3wzkOo=; b=dNz31y7Lm0o3aWuUaZ7GlxyGsmWeMfsiIYyrRHgiaAQ63mCM/03JLpXK0QIos5VuK4KIbe eHoWL69T3aAcjtsFhz4sWvgrnniulSZK5t6K1wSFKJ4cYPFOD75lDIE3wD+HKsPFbB2Utt 6U5AG1tKi05VTUn9ZQ071guR11KezhQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 3CFE561F06; Tue, 30 Jul 2024 20:35:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6802DC32782; Tue, 30 Jul 2024 20:35:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1722371741; bh=WZ48PPmVTj+kJ37rNAFT+iZMowNbWwVK6vyYDSGVsDI=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=LoKwtyZCWE2yjnIRT5ga53Sub0TuDnNiKxE56ul46UQs1lenSt7iHzfQn8VVmfrat P2b8NUhXmehN7v0LCy3P6BGqT6Vz3qXppkaR3Q/taSqH2nPyi523OHKKOcORTiG8Yo pUhiDHe/CkmVEyPc2vROrZVUusQX7uW6zY5qwsBk= Date: Tue, 30 Jul 2024 13:35:40 -0700 From: Andrew Morton To: Danilo Krummrich Cc: cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH 2/2] mm: krealloc: clarify valid usage of __GFP_ZERO Message-Id: <20240730133540.66e215082a513509c0d8649c@linux-foundation.org> In-Reply-To: <20240730194214.31483-2-dakr@kernel.org> References: <20240730194214.31483-1-dakr@kernel.org> <20240730194214.31483-2-dakr@kernel.org> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2371BC002A X-Stat-Signature: huectkjobs3x7ua735fp8gk7m866w4dh X-Rspam-User: X-HE-Tag: 1722371742-10324 X-HE-Meta: U2FsdGVkX1/mPCmZkcThV1OyxtEYsNZp7nvt9Brwgn7ybN9cO2RO2Ahmjd/ASfV++HpYBrO17cL48g0DtEkX5ckmSn1IMJuxZgh7udFiPRUgZ6+cL4jCupCYxyHEPYLANN+7djoxC3iqKrhate/2pLiHE6SJBTcQVA4nYx3nzv6NCm/3P0VC9Bm/seWN0NqZ4SR5pHvxDwM0ZmKgukMZB6PrMVOM0pfDm2WAjaIl0o2eyqMwIn/KFGZa1035hLuYcKB4dM+BtBADAKMCEWx474MYTDiwWNoNhEyyrHsasd3bts0EuSY1eVLO+ugWnl9xbJpgDzA6vB9+oYysLbrrp1iSwUpp1fH5r7pIEuq1JKR6/ddcLfvzfgo4mf2LhrF5WMIt6XCmzQpQxLMJAQWN/92WhLN8xo0WUDKeLSiRnO+d/qqtd3kKMsDLQZ7IlONVv3XrOYDAttdTf25etKssqyWMrgdAExI7Aq5KX/VFaZZICB2tote85IYpJYfRxltoCLsbhvvzfxWMFC0rJwnr8XDKkAWMjiHe/UDaqpjzcYTAfgOCmvs+MF1WkKWaWqGIAfXcZCsT6j0pwZoE2wt3K+P9rYyakHPpNeS3opsvqigzPmXmFIwBv2BUdEDJXBHwnxT339vasg3rddz4JqehwqPRlGOfqsd1+OWmzB3ukPw0O9b6eaIQlgUWf7mhFv7AprMVFt5ci7mRTt4kVMyWeIWU07eHIZJ2i/tJp+EDFkcjKzbWo+U4j5u1cz+4javdAUnQSPwZz4aFNWPYBbgn7j4XPF8MfvtrRQ3wLORlX4xXgVAxFPtNfB3xqEEFz+60Ptx7SJonPOjEjdkX85hsKBZCzwHIIgE2gX92ZJYsI55tf5o332EntLMwslJ5CW8+TPZuNSICcHvzUX6HjJJ/wlNVuTId6dpoGzk1HlSXzAu/Bax2GBV2AK/S2JnSTJnbzRizvFNzenYTrgS2nzz sM39AwAp AHTZNs56FRleZUAgpuUlYPpVspgHLKPwn1Bx7fH2yc+1uxNxhgAHuSoAqSupIn47qJVovDULV71lqxvOHyEfMhrf+ap+0gtL9uUBeODyJNZz44OsmXRw8wZvIjxW6kXQig8KV8+GE34ws1N+XXQ6jWDzEGOHqG6duEIB7LNJhwuWzJk+QI97Ni0Y9bybAuCWhfhbJxXtPwEb1erHIalKgh7dNWqXhg+U2qDcmhaUwSj1F4DGcK6KWU7VVQY/8ztNFPuKdDd5l1crwVu1XSXvM4GQgAUzrGbUVmrtBq5c+L7PsxSo95poCrwO2s4W9vNkxV7lu09Ib3mBi7TsrO4H9L05fEvqjvdtMnB0Uzpm+YeuQTnqpPKJqGZl3/btBPwaPPxEPkKWMSOgKts/w4jc1ifSCHA== 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 Tue, 30 Jul 2024 21:42:06 +0200 Danilo Krummrich wrote: > 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. > > ... > > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -733,6 +733,14 @@ 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. > + * > + * 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..faa13f42b111 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -1301,11 +1301,17 @@ __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. > + * > + * 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) In both cases, we're saying "callers should do X". I think it would be better to say "this implementation does A, hence callers should do X". Tell people what's going on. eg, "if krealloc is expanding an existing allocation, the newly-added memory will be uninitialized unless the caller used __GFP_ZERO". Or something like that. I assume that if the caller actually touches the uninitialized memory, KASAN will warn?