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 60262C3DA7F for ; Wed, 31 Jul 2024 00:02:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD5E16B0085; Tue, 30 Jul 2024 20:02:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C866D6B0088; Tue, 30 Jul 2024 20:02:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFE586B0089; Tue, 30 Jul 2024 20:02:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8AE116B0085 for ; Tue, 30 Jul 2024 20:02:51 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 43FE3160145 for ; Wed, 31 Jul 2024 00:02:51 +0000 (UTC) X-FDA: 82398096942.24.C9D13AB Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf12.hostedemail.com (Postfix) with ESMTP id F095740024 for ; Wed, 31 Jul 2024 00:02:48 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fJL15IV3; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf12.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=1722384165; a=rsa-sha256; cv=none; b=nWk8OR5hornJVNfQlZEsCcxbhrvLLMdvqxvvk0GRmRMVIkz0aAtYQmJ8XB4XvA33RYdg3i sA2922jNUUT3lJk2qctomgDpGV6qEGgeGpqluDGjoZJIFipQTeRjiEWeUOOUYNSi5PpexW SBQbvEUl+4pnRQl9dDYK4FV0cNOguTI= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fJL15IV3; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf12.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=1722384165; 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=/g2GXZhrkoVHVb2AvbTiBPwDsocwGTUFjhgz6CLCLnY=; b=wWDeXY8SBHmAyLjphRnpFyimFPwp35+A+i3JB7NVEQa8Numnv56t3N2DD+60LOY0VeNI8g Et9fGzVZ+iHjMvVo6KO/+5ahr8wqSyQi6q9BvSVVqKQpR94scWAuBEw5ViZLXmbpta61Tp HitLk4jTXvBMCs9qzncujGafUpSb9qs= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 24EA7CE10C0; Wed, 31 Jul 2024 00:02:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 492CDC32782; Wed, 31 Jul 2024 00:02:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722384163; bh=MZ84WBMb67FnZkuouahXmXGFPZ8pi0oJbpj2ztROqaU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=fJL15IV3A2dLzdPyRzR5y4VCQeSSN+1p787hRsswNZ8EVKwUs+DqjQdV7hgYcr+gb 6OOwlGbYrHIhtYbFL6Xy55WCXEwj+OqIBqikxd4O/ekYFNhK4k22qgv/QLf/yHCxaS 83R41+ByGeihfOfAaQ7xfq/VSU6+nt86wFVBVtBqSbQvpJ0I7nTMulXF+IruZbn3se DzsoqzTq2ObuiZADby0JYnDOEfi2yqfV0cTTjNot+vVvlzaWrtpipPLpgvMopipxKY OpYqfcMJttTIUbGo2/1KtGlwZ5mnM5IKMVboWV4AHUdwMpiypK/T37wKt71Sdrg9NS 2DnG9nbKn+SCA== Date: Wed, 31 Jul 2024 02:02:39 +0200 From: Danilo Krummrich To: Andrew Morton 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: References: <20240730194214.31483-1-dakr@kernel.org> <20240730194214.31483-2-dakr@kernel.org> <20240730133540.66e215082a513509c0d8649c@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240730133540.66e215082a513509c0d8649c@linux-foundation.org> X-Rspam-User: X-Rspamd-Queue-Id: F095740024 X-Rspamd-Server: rspam01 X-Stat-Signature: 9swcjktwpzeirmqnja1h6jwhkojt86xb X-HE-Tag: 1722384168-969626 X-HE-Meta: U2FsdGVkX19ITl9QjayhFX9M/5XExk6B0HMINLHEZTJdKOpdqGYpItl+SRsSavU7FtL+db6W1xaBgaZ6r+qzerw++PtqSY99rbXCXvUjUZh9kpA6ScFECpY68fb9ncwSzrwY86Nu19kH3Wuj/DTa0DsPnWKSM4dNNdcOYhiFOUaSlupBoBt/cCihtm0MNG4Y6i5K1LqvwxBIsbfjS5q65VmFg/6ZGedvU3J3z/9HSdBHB7RX8l8R2wo8EwjIZrjOW/TFrqZnTmR8uIfagnYj1SQJ4nJoaIkzbZuL3I7mr/HVC2lP70upYix/at+KG7jwF+JdnAjr6blVep/Ogm3M3tJlNbxNNJ/AT+dL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precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Tue, Jul 30, 2024 at 01:35:40PM -0700, Andrew Morton wrote: > 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. Sounds reasonable, I'll add an explanation here and in the fixup series for vrealloc() / kvrealloc(). > > 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? For the case that is fixed in patch 1 of this series, no. KASAN can't detect this. As you say, the memory is just uninitialized (not poisoned), where it should have been zeroed instead.