From patchwork Tue Jul 30 19:42:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13747821 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 2D76DC3DA70 for ; Tue, 30 Jul 2024 19:42:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3F066B007B; Tue, 30 Jul 2024 15:42:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9EF306B0082; Tue, 30 Jul 2024 15:42:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B75A6B0083; Tue, 30 Jul 2024 15:42:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6CC7C6B007B for ; Tue, 30 Jul 2024 15:42:27 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 10EA7C048F for ; Tue, 30 Jul 2024 19:42:27 +0000 (UTC) X-FDA: 82397440734.02.8B2FFE9 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf17.hostedemail.com (Postfix) with ESMTP id F3F1A40013 for ; Tue, 30 Jul 2024 19:42:24 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qWfirlZU; spf=pass (imf17.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1722368518; 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=/bWAIu40nTHf4GuUtyXHloV34XIazGbVHKXM4aLzzAs=; b=UpVDgOz3IhQgT8E46/M6agRfKR6F7P0s55lV4Uu8btNvm3bNtUHbFiWPI0KfmdODzv72Q3 fmsrxCOigou07hxOfVD2jgJhrO2QzG7nkcjMDfIXBTswNZAgYKWKGaVn1HWNNzxvcgEViq cxcbxGoulrO++kmi3+7jxV3+vC6JRS0= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qWfirlZU; spf=pass (imf17.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1722368518; a=rsa-sha256; cv=none; b=EWSPEPLpAiQ+eYX8868OOGcrlQu/gFcx4IHhoK7gjg6WcjGtAlTfX3IALGXcTcUFxDsO+L VLiDpmTBgai9a7TRobxy90fiErwhuXwtSbS3XYBytXZFUwjs9W4LsCrKiY5gu6IIsn71YG CBgbBfRjC3Ryf8F8g1qcC2OnfyLvgbA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id A219DCE114E; Tue, 30 Jul 2024 19:42:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88303C4AF0B; Tue, 30 Jul 2024 19:42:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722368539; bh=2LdP+yaGyLvvhYc3Idvuj/mK+BX++D9CaCktI4AVghc=; h=From:To:Cc:Subject:Date:From; b=qWfirlZUmxvQU4E3uad68ZRUdQdYOQoS2Dlu3zOu90XNN3UVB2SfIEI1u93uByfs4 /nAnsk9fzX+GOzuQElEDngL2EzwpGr0fiGlyhKKKFUb6OqXA1+DCWFF017EAIblUm8 eYn5+orLLFewFDwnymqLVrwTZprZc80wHvOwiMf1K2JgSbyQvH9XGxYBfMiFzyy+Z6 YQlg1kLbrVVjlfdAabf00WPulMXYXIsOQo/sLuLvVfl+E4UsXmlVVnnW44B/lvOFcR eiqCk1VZ2UunbCoVl67eqd356b126hCOuEgoR0J+ec1T4aps1lvp9qv82mHz4yKh6z 2v71OZIVs64Iw== 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 1/2] mm: krealloc: consider spare memory for __GFP_ZERO Date: Tue, 30 Jul 2024 21:42:05 +0200 Message-ID: <20240730194214.31483-1-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: F3F1A40013 X-Stat-Signature: woe5hynq5mpwb7k3d6e5tu5819wm91pc X-HE-Tag: 1722368544-277248 X-HE-Meta: U2FsdGVkX198kRBCSPjyh1VtXz3DJXWad0tf8nwyXQ9yYhFT+fIkCRlKwelrIWOYiXlRWOgptsz8613XeRq2SaQg76+LMQDMENpgUoNh1W+p3SJKi0iBMPzQmZZ6Ctyxy5paoE+cd/5cisxRLIww32OTfd6XGBl39NnCdu1kVbINLDTBgve6MQqFcJ9jerCTvCIpIB4CCPM59A4EWCoL6ySqp/axBcoOQaMA8o2XkIGHXFZJRlGbLNksvQ7RNu3dS9zgoPZd+vOcC0sqVJeoeJ1BYmCDc40pij0Q20465ockMXL7wHFiao18afYNM+bncEJHscVYUQhhWpZQfUEmzlo6kLFHat5OzB7Eaq6zySJQ1/oBYg5wHiFlY636iY1Mx9R2UBbUVnevJ1JKP4IW1lO4enbVJeubR4Wt0po7NHsyOka4lxMzEQvUWNnLZU1wfagkXmkZRPL0f9EOfGc+u0PYy7kib4vqly60UJVJWxH7UoMPdLVNt+LgzeYsCDI0g9R1xy2e0/fOowGnYNeG3i84U4rAA92v0OEp6Pagq7QTNVqWGmHVPi9l9br8yPVHkOgpHZRR8P1BdVlPZvKBuEVByhsMuwqGUKG8fPnIvjRYtzDG9d9jN7JfJAT/PpUTbav/MZiO2Rcu/5FPxSxyPZQ8IODSA3lV7xE4uQ0jrHgpwTdq432Kl8J0+oBSENY3xYlYkK0KHNLdnJLJcKSimrgkCpSrMrhL6MARXFJljIolVuVPmpl1+qUkMSp63DSr0vACOn4umdupGsz0dD7RoTTirvLzVO/ioH9kdDrmq7RQFFsSheTL+imblVntWXgftZe+/JSfv3bwkfdY/4e0n+ulIPoErdFbdsreedFzXQ4kSK1OD6+evYFX+4L7OLXPPJWZFg+/t/jroPOQ6fDKGl6YiUQjvCfdS6SMhRJ/Dy7Qhwk0bNwFn9JiaJKvExvON8RMAoSPbRKA2+4pcub Vqliv448 rAc1rpMJDtf0jj/Pf5HQ8hc4McLVq3sSxJuCXza9Z5jb4uc5iExvKqFqDBNhi4lSp9FPJZ//fCvoaa/KS89YAC5VVsFTgVVHl2WFwko052fzXPKvBN1mimHLXMeKPrgNwUo00Hc9Dvpm4ODZXXeKzlzeucm91wbnY6ZUoQxtgKD076fs0F9AYvIqSodjdd1JkFwkVWh18KVw9cuPb2gNWiSca7ECoE2lrjGBIIUl4YMLC/6Cfn5S53/rO2MnPDDfoZcI6Fb9NyErgWUShrnYPSZFFFTWjiipqMty42PN+4KjobM2COm5CapNZeID4yVMH5lblNJ4WeATRdM/7GxyygtrHr4k4T/Yn4N// 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. Signed-off-by: Danilo Krummrich Acked-by: Vlastimil Babka --- mm/slab_common.c | 7 +++++++ 1 file changed, 7 insertions(+) base-commit: 7c3dd6d99f2df6a9d7944ee8505b195ba51c9b68 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; }