From patchwork Mon Aug 12 22:34:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13761077 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 8128DC3DA7F for ; Mon, 12 Aug 2024 22:37:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0DC76B008A; Mon, 12 Aug 2024 18:37:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B95416B008C; Mon, 12 Aug 2024 18:37:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A36056B0095; Mon, 12 Aug 2024 18:37:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8523C6B008A for ; Mon, 12 Aug 2024 18:37:20 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2CB0D8044E for ; Mon, 12 Aug 2024 22:37:20 +0000 (UTC) X-FDA: 82445055840.23.C9130E1 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf03.hostedemail.com (Postfix) with ESMTP id 8E9582002E for ; Mon, 12 Aug 2024 22:37:18 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=u0Zlwqli; spf=pass (imf03.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=1723502204; 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=zRqKRwp0zUaBeWR4IFGz7bZdlmHTSRAuhYtPSziafe8=; b=7nG8TY8Vrwa4MsuzPH7fHAoE3AO5UkfPefC2L+YoL96H6U2pJxmm+QMMW7tfiCq51pBye6 qIY0WVb+OqUj55g0zQ+OTX96gSmGVRMre6tpGuwAvmFJ3F8q1ocYmrlDvpylmL0nTYNojN knSQL9CRPf6gFprOgYIhBNWEhx/Ehk0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=u0Zlwqli; spf=pass (imf03.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723502204; a=rsa-sha256; cv=none; b=fypgk2RJeJM7JtlSlgwJ1LRMXLWn4HZSw/yKlm7V/6gJml6gjwEtOt2m02PGiECsGF0cPu ZSpoD4pvcDioLsg/uZX6/JPa0AMVYppSce83gmIbcbwuZp2b+pyGcbi3Eo4PCxWv4fbhW4 J0wXG9sW1FdfzaGJBIMHpNSfCGBM7GQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 6E715614D9; Mon, 12 Aug 2024 22:37:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2223C4AF0E; Mon, 12 Aug 2024 22:37:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723502237; bh=7wzGtViqpVZzuhPvzkBp87xBfZgW05gIDV+YZ+kbWBs=; h=From:To:Cc:Subject:Date:From; b=u0ZlwqliEa4GkN26rqrReNTi8sLMWHeLTMcZTdnYeLxiOlM34WlgqIYJctY8xbBMt vc5oIwzc3PrtHD3K1Pr6zaBgOfMoL82RbGY+yoFRIXuMh+J63DXFsXZxaoloi3PEiM y2GrHd8ORrVDDyjdaiMXrEn0ZEQBZj/LTq8pFgOfPnKcCjZ1Y7UIwydNngQAj1KQ9K 6csANk59USYfuAeGekJH2daB7B5fx1XpyoWghfiOQIlAeB6yiJvCZ41LlraJZE3L/F Y3niujXL7/EbpqXKuZlWY/wQXdcnDyHKPLCW9/KNcUDBK9r2Dkz1oFiTCLcOiE9gae Xq9Q1D+2J7UrQ== 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 1/2] mm: krealloc: consider spare memory for __GFP_ZERO Date: Tue, 13 Aug 2024 00:34:34 +0200 Message-ID: <20240812223707.32049-1-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 8E9582002E X-Stat-Signature: 4r4ckg8ft83qhkyf6jtaho5ix4r9bbth X-HE-Tag: 1723502238-742983 X-HE-Meta: U2FsdGVkX19pjMD7hfrzJgWtddd/zYCm/YTHWjJqXlwWzHsP5m/TbMzP9McAV8henD+JEyOnJTULe/sndTJ5NMb8ClWd39JTlGeV+vfzOwkYUs/kWoG/3bnuBkk04xhAkde580FR/jkpMUF+oA11GGXt1upVXVRqvVjjwQ/Z3JLemsxnhb2+69d+1gLML49i5USAzsXn8MUl9SR+c6R0xNKXEhq8U8gPlh/X+hiC9YExMj5HhrS8VptGvOMKIDilJIlzjXxLzfobi8SbmVfl8A9oujPmJq/Vlf8LwKrbB4m9wkwrR0C88BG3feb/6OL9bd/HTFhOlTJuuG2wbZM50RNU3RQgK0TEsobUhnDFFe2pKRuOG/v2O2ICR1D3yg42BoFYNw7YLETH8COB3Um7ZFlb588M1acs0C8B+gsQGgMgc9PDfTDsvDGatSeDBI0MK36R+RCu2L4XKfEjXr1DRmK0QtN50TNQ64mJB5COLNb0yR/20aBAgxaRrQm5fvwzK+FQ6MB5JTrYKwNFOBaHkkYV49f9UiM4dPWjEf4IvUzGmLoaCltC2i/w1pAWwLoKx9iwKvJaPLm9xEFas6PBwtA50vujstlWyGCcua4h7pYmDXDHJaKiGjvuCP5WovTn1Vno6TjXeb5GpOnR9AeLZLyRCCeQmlCaUFaI/WNM459dkri0mwN4EfSeXkj2hLj95M6HI3jRQ53+2HF6a79DwCJmtfcSxTPZmdnrEERBgwUNeVPD9elqHMn5Euybf9flRSn0YrnARiRVmK+JtWmau0a3iROG1glZGpYzhf7llTgH6oirK8zSlAO4OejLKI1jsGJaKdop+rWPIchB7EMiY1CAqsROnqGintKQPVE6FnfI0y4YOTSJLxq2Vd14wbPoPBv4CPt+hKVlK0/peQ+Vg/i3OZDGUWbSLnWICn3ft28xtLBC9fimzj8z+B+oZpg1iURlHcO0F1guGmRQ9Fj sFail9SW 5FH2TdNt5yKSnMVAW/4nGcpzN22o/SgkW3q1jjgnQjkPIfxRKT/ojWZHzqT+tYFQLYVeWuiik7Px1vBMSBAG7WRhJvlnUkJ3jbEO8W5/Dry2NLKpzkqRKynzE/UimNFO/Qnve82zLf5zK6Dq5vnNkDAv1sY89IBwvaMduOy+oro/olDnKRVuFEpattlHRhLK/jxFXKuEjeFvGJxYTzDdXhFZHRcfFT7yAYOv2I4BuXUq6UVKr/Ph1uYb4gGzVhivKJStA0p/YsKiCKlZaJRzdrGvMbdMAGyBln6woDl2Bn/2aEVAQ23QDEYLHvuvD4V9q21yabTmiIsT3TPX5bRSMUNanQLeMCW/qwM0u 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: As long as krealloc() is called with __GFP_ZERO consistently, starting with the initial memory allocation, __GFP_ZERO should be fully honored. However, if for an existing allocation krealloc() is called with a decreased size, it is not ensured that the spare portion the allocation is zeroed. Thus, if krealloc() is subsequently called with a larger size again, __GFP_ZERO can't be fully honored, since we don't know the previous size, but only the bucket size. Example: buf = kzalloc(64, GFP_KERNEL); memset(buf, 0xff, 64); buf = krealloc(buf, 48, GFP_KERNEL | __GFP_ZERO); /* After this call the last 16 bytes are still 0xff. */ buf = krealloc(buf, 64, GFP_KERNEL | __GFP_ZERO); Fix this, by explicitly setting spare memory to zero, when shrinking an allocation with __GFP_ZERO flag set or init_on_alloc enabled. Acked-by: Vlastimil Babka Signed-off-by: Danilo Krummrich Acked-by: David Rientjes --- mm/slab_common.c | 7 +++++++ 1 file changed, 7 insertions(+) base-commit: b8dbbb7fe1db26c450a9d2c3302013154b3431df diff --git a/mm/slab_common.c b/mm/slab_common.c index 40b582a014b8..cff602cedf8e 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1273,6 +1273,13 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags) /* If the object still fits, repoison it precisely. */ if (ks >= new_size) { + /* Zero out spare memory. */ + if (want_init_on_alloc(flags)) { + kasan_disable_current(); + memset((void *)p + new_size, 0, ks - new_size); + kasan_enable_current(); + } + p = kasan_krealloc((void *)p, new_size, flags); return (void *)p; }