From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 751F6448393 for ; Tue, 11 Aug 2026 14:12:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786457575; cv=none; b=ZdsJpoPuf/RQYvwjiecqR1Ijqb/CTde3dZ3VZX4agHPWKw4tDYvfE3fivfQetKNry5DS/34BNK/mcSkFiKFNT8RIK+i8atqib+rob5jBPkYl07esPQ0siw16hPZR9nNCxRkHup/wpuRupp779kF4P50bIYNf44HYJ0zxJ74scJg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786457575; c=relaxed/simple; bh=LjIubD3Zg+wcYFo41RfpwdNGP6TjBzaPkrqdHz0k0BI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N6KPsZOTKgfTqpbW3W2Ia9EhlKDCaIad1RTztRn4THoxs/KmB/Vh5WnMVWSrstjkhQHFsL/mz3zt85Ltpr/nJJIaAGTJV8ed/0buUivaGOcs0HKBO55DNAD6XuvFJT1u7Pnz0KPdaNb/vnWx4nhvh2KCFuOvBkMw0gLle5MB55c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PYL6kqQ4; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PYL6kqQ4" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-49802c418b5so30638205e9.1 for ; Tue, 11 Aug 2026 07:12:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786457572; x=1787062372; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=o80QnEVKUcuUdd3OZFAdxnODo4B1zikCcE/tvO9NxHk=; b=PYL6kqQ4cap3W/M0taPR1P8+0VSxhpAFQ082G8vEueVauFYcRZwN6eM49gXJJ8jQLA CZxBeXlPvkzBbRSFWkm7NwhKwlhlNvowEfjLuC9/wYQwt0x3t3DZMBRpoPnRPPfa5DV9 KIkZs8C9qwZBluNiEUQ/Vvn60He2lhn7V1TIlAK/sM2+TKmevgb3lJJvbdcOlBfzMxfR RD52Y6D7mriNNHzGzkxUQzfPF+oVzaqmDq/n4VnGMmwtfaktpVYMOYSPB4yFuim33aOg B3on0epkL9FF1H24jBeuxt0c5omPBT71hS5Czqn9YgJtxPuMyM+/tjE506tlAJRbveW2 UnlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786457572; x=1787062372; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=o80QnEVKUcuUdd3OZFAdxnODo4B1zikCcE/tvO9NxHk=; b=AgwDFh6VkkjVHyAC7GmeNFYkltUpSSNGnf7MyBii/hspC8LQbUkzo7K/ohhS2YISFN vci77aTOCvu1Pl3eBKrZSoURwC4zCxjawDlKSc6nUK1C81ZWvbX45aBbxpQPT0iEwnxx IOqp2yrw86kCMwcjWpcBjIQQ+BFOXNqthjEpFNdaL6unxyc61Qzowgj8BLoyTho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X-Received: by 2002:a05:600c:4709:b0:495:4d88:e630 with SMTP id 5b1f17b1804b1-4997844c55emr61209035e9.10.1786457571515; Tue, 11 Aug 2026 07:12:51 -0700 (PDT) Received: from localhost.localdomain (dynamic-2a02-3100-a133-e401-bc8e-d36c-2d08-c5e0.310.pool.telefonica.de. [2a02:3100:a133:e401:bc8e:d36c:2d08:c5e0]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4814a5be29fsm4464099f8f.12.2026.08.11.07.12.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 11 Aug 2026 07:12:51 -0700 (PDT) From: Karl Mehltretter To: Vlastimil Babka , Harry Yoo , Andrew Morton Cc: Karl Mehltretter , Rasmus Villemoes , Hao Li , Christoph Lameter , David Rientjes , Roman Gushchin , Catalin Marinas , Kees Cook , "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v2 3/3] slab: test zero-size allocations in slub_kunit Date: Tue, 11 Aug 2026 16:12:40 +0200 Message-Id: <20260811141240.62519-4-kmehltretter@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20260811141240.62519-1-kmehltretter@gmail.com> References: <20260811141240.62519-1-kmehltretter@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add KUnit coverage for the zero-size allocation contract, including ZERO_SIZE_PTR alignment and exact ZERO_OR_NULL_PTR() matching. Assisted-by: Claude:claude-fable-5 Signed-off-by: Karl Mehltretter --- lib/tests/slub_kunit.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c index fa6d31dbca166..a02fc6b401e3e 100644 --- a/lib/tests/slub_kunit.c +++ b/lib/tests/slub_kunit.c @@ -380,6 +380,48 @@ static void test_kmalloc_kfree_nolock(struct kunit *test) } #endif +static void test_zero_size_alloc(struct kunit *test) +{ + unsigned long zsp = (unsigned long)ZERO_SIZE_PTR; + void *p, *r; + + static_assert((unsigned long)ZERO_SIZE_PTR % ARCH_KMALLOC_MINALIGN == 0); + + p = kmalloc(0, GFP_KERNEL); + KUNIT_EXPECT_PTR_EQ(test, p, ZERO_SIZE_PTR); + KUNIT_EXPECT_EQ(test, ksize(p), 0); + kfree(p); + + KUNIT_EXPECT_PTR_EQ(test, kzalloc(0, GFP_KERNEL), ZERO_SIZE_PTR); + KUNIT_EXPECT_PTR_EQ(test, kmalloc_array(0, 8, GFP_KERNEL), ZERO_SIZE_PTR); + KUNIT_EXPECT_PTR_EQ(test, kcalloc(4, 0, GFP_KERNEL), ZERO_SIZE_PTR); + + p = kvmalloc(0, GFP_KERNEL); + KUNIT_EXPECT_PTR_EQ(test, p, ZERO_SIZE_PTR); + kvfree(p); + + p = krealloc(NULL, 0, GFP_KERNEL); + KUNIT_EXPECT_PTR_EQ(test, p, ZERO_SIZE_PTR); + r = krealloc(p, 64, GFP_KERNEL); + KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR(r)); + p = krealloc(r, 0, GFP_KERNEL); + KUNIT_EXPECT_PTR_EQ(test, p, ZERO_SIZE_PTR); + kfree(p); + + /* Only NULL and the zero-size sentinel match. */ + KUNIT_EXPECT_TRUE(test, ZERO_OR_NULL_PTR(NULL)); + KUNIT_EXPECT_TRUE(test, ZERO_OR_NULL_PTR(ZERO_SIZE_PTR)); + KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR((void *)1)); + KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR((void *)(zsp - 1))); + KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR((void *)(zsp + 1))); + KUNIT_EXPECT_FALSE(test, ZERO_OR_NULL_PTR((void *)(zsp * 2))); + + /* freeing the sentinel must stay a no-op */ + kfree(ZERO_SIZE_PTR); + kfree_sensitive(ZERO_SIZE_PTR); + kvfree(ZERO_SIZE_PTR); +} + static int test_init(struct kunit *test) { slab_errors = 0; @@ -404,6 +446,7 @@ static struct kunit_case test_cases[] = { KUNIT_CASE(test_kfree_rcu_wq_destroy), KUNIT_CASE(test_leak_destroy), KUNIT_CASE(test_krealloc_redzone_zeroing), + KUNIT_CASE(test_zero_size_alloc), #ifdef CONFIG_PERF_EVENTS KUNIT_CASE_SLOW(test_kmalloc_kfree_nolock), #endif -- 2.53.0