From patchwork Mon Sep 9 01:29:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Tang X-Patchwork-Id: 13795733 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 51217ECE577 for ; Mon, 9 Sep 2024 01:30:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D5D526B0102; Sun, 8 Sep 2024 21:30:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE5376B0104; Sun, 8 Sep 2024 21:30:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B36B26B0105; Sun, 8 Sep 2024 21:30:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 910266B0102 for ; Sun, 8 Sep 2024 21:30:26 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4F9C5C0942 for ; Mon, 9 Sep 2024 01:30:26 +0000 (UTC) X-FDA: 82543469652.23.43E5809 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) by imf30.hostedemail.com (Postfix) with ESMTP id 3C64380016 for ; Mon, 9 Sep 2024 01:30:24 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MLiaaWnj; spf=pass (imf30.hostedemail.com: domain of feng.tang@intel.com designates 198.175.65.15 as permitted sender) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725845290; 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=TdrgPZ3kYW9y9DC7KkVYudRyE6G4mFvxFIKatTay83o=; b=gYFkvHD/YgF3/UM6jmjYTb6Hac0A7slIykkp29Sc3eInbIDQIKCZNYalm+UcASGrRPbOXX XMxj4jO8zBy0kNb+kSDKjmelcX4jM9zXAcAGFZN9QE0NYkKZwjcjFoy0g13HtWJ/qj7ucE 0O1YoyFFBvsRAHZhjaF2pu/lAnvKTzA= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MLiaaWnj; spf=pass (imf30.hostedemail.com: domain of feng.tang@intel.com designates 198.175.65.15 as permitted sender) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725845290; a=rsa-sha256; cv=none; b=7XkC6JELBKf8OqqmNTzFhhK5EBu7iC+d9zba4vzmuCiuU6vQiWCop8DCmQtLHQT1sCxbgY ZEOufcAO52ZHwlnXTdKXJ1KZT3dVdsCJamocfiE3ijN7Gs3eIo1xOa24mKFJULRI2gO7iC 4wQfh0T6/tsb3+0xVak7EG+8YrlZlLU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725845424; x=1757381424; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ol5TUqWkU7uvIyG026mGuFtpsR/rEt0g9LWcDZDc0MM=; b=MLiaaWnjz/qlMjstOwedT7w+GHAVqaxijuYmEZFoZ6NTFqpRG3Ux7alx +QlOJv7lWICUPNMowS43ZEY2gYQREob2TGAUyKjmzH/K4m5yo0YFq/EXR BvvKEcTcHm53x3CNZheSzcLYEw9/5BqnDu7qtB4KXGrQB/sZCGvNRVW3b B0+KJ+jJE67GWCPOGRZ7MycyFFH55cI82Csw+HoY50t2M5qtBlc6V29JT sugZakY7Qwo+iUIJh7eXWubLHjWod8w4qPXcBhQfHhsC9w2KbUIJdDEEz yD/HhVXSIubM6f9SvShySuQ+f1Ri6e80izT0finuReRIFANcnuJ2Lbxno Q==; X-CSE-ConnectionGUID: WLA+Nq/ZRriPFICWhsKQcQ== X-CSE-MsgGUID: lvyAQlHrQIWDoT+sOKS2xA== X-IronPort-AV: E=McAfee;i="6700,10204,11189"; a="28258163" X-IronPort-AV: E=Sophos;i="6.10,213,1719903600"; d="scan'208";a="28258163" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2024 18:30:24 -0700 X-CSE-ConnectionGUID: ROdRq+r4T9WF2KAQcT/Xlg== X-CSE-MsgGUID: DV/YFBNgQQa1SpiY83zezA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,213,1719903600"; d="scan'208";a="66486541" Received: from feng-clx.sh.intel.com ([10.239.159.50]) by orviesa009.jf.intel.com with ESMTP; 08 Sep 2024 18:30:19 -0700 From: Feng Tang To: Vlastimil Babka , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Andrey Konovalov , Marco Elver , Shuah Khan , David Gow , Danilo Krummrich Cc: linux-mm@kvack.org, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, Feng Tang Subject: [PATCH 5/5] mm/slub, kunit: Add testcase for krealloc redzone and zeroing Date: Mon, 9 Sep 2024 09:29:58 +0800 Message-Id: <20240909012958.913438-6-feng.tang@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240909012958.913438-1-feng.tang@intel.com> References: <20240909012958.913438-1-feng.tang@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id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dx24bR4vUP97f/tHQ8gJLBWW6zQAMS7R5+TasJ3kync5ErvwW3C7D0sIeui+ZL8LqjAUbr20V0+ySEJG3TEHJoljkrCzoPl26ZWNbIr2+H16ZQfR9iF5/EIX8UBSfvDYj4sUzcXru4HU2FxhyOe41PYMEwRzXF1J47SnY8ItXh5xymN+w8MKQikj4tjOQzRFJu96Hmw5g1jwt2XnDpp2+GGzyPu7Que9z17IcizoqpYilQSz3aK4Ep8SIwY5ayDXcWvuvLm47pDZw== 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: Danilo Krummrich raised issue about krealloc+GFP_ZERO [1], and Vlastimil suggested to add some test case which can sanity test the kmalloc-redzone and zeroing by utilizing the kmalloc's 'orig_size' debug feature. It covers the grow and shrink case of krealloc() re-using current kmalloc object, and the case of re-allocating a new bigger object. User can add "slub_debug" kernel cmdline parameter to test it. [1]. https://lore.kernel.org/lkml/20240812223707.32049-1-dakr@kernel.org/ Suggested-by: Vlastimil Babka Signed-off-by: Feng Tang Reviewed-by: Danilo Krummrich --- lib/slub_kunit.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/slub_kunit.c b/lib/slub_kunit.c index 6e3a1e5a7142..03e0089149ad 100644 --- a/lib/slub_kunit.c +++ b/lib/slub_kunit.c @@ -186,6 +186,51 @@ static void test_leak_destroy(struct kunit *test) KUNIT_EXPECT_EQ(test, 1, slab_errors); } +static void test_krealloc_redzone_zeroing(struct kunit *test) +{ + char *p; + int i; + + KUNIT_TEST_REQUIRES(test, __slub_debug_enabled()); + + /* Allocate a 64B kmalloc object */ + p = kzalloc(48, GFP_KERNEL); + if (unlikely(is_kfence_address(p))) { + kfree(p); + return; + } + memset(p, 0xff, 48); + + kasan_disable_current(); + OPTIMIZER_HIDE_VAR(p); + + /* Test shrink */ + p = krealloc(p, 40, GFP_KERNEL | __GFP_ZERO); + for (i = 40; i < 64; i++) + KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE); + + /* Test grow within the same 64B kmalloc object */ + p = krealloc(p, 56, GFP_KERNEL | __GFP_ZERO); + for (i = 40; i < 56; i++) + KUNIT_EXPECT_EQ(test, p[i], 0); + for (i = 56; i < 64; i++) + KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE); + + /* Test grow with allocating a bigger 128B object */ + p = krealloc(p, 112, GFP_KERNEL | __GFP_ZERO); + if (unlikely(is_kfence_address(p))) + goto exit; + + for (i = 56; i < 112; i++) + KUNIT_EXPECT_EQ(test, p[i], 0); + for (i = 112; i < 128; i++) + KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE); + +exit: + kfree(p); + kasan_enable_current(); +} + static int test_init(struct kunit *test) { slab_errors = 0; @@ -196,6 +241,7 @@ static int test_init(struct kunit *test) } static struct kunit_case test_cases[] = { + KUNIT_CASE(test_krealloc_redzone_zeroing), KUNIT_CASE(test_clobber_zone), #ifndef CONFIG_KASAN