From nobody Fri Jun 19 07:48:47 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 188AE1A9F90 for ; Fri, 24 Apr 2026 05:30:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777008612; cv=none; b=SjtbBRllO0Ufm5kgWUJmOapoY3Ea6nqbmK2HMbX621xQbKb+AGdtaAM1CafhHrkdaYPLrVp9NKUloath6N4jj4tHhmkrYva0m8K4RH13vxXFhVnE9UY1CO7Hpz0VazP7vMX9//4QHThdsWLXRrUhGTv3/N1SJbiAv2DNsf/nsu8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777008612; c=relaxed/simple; bh=r/alvrJIilJ8lBwi8xOW/gbxfhO6uFDacXHOGag8+WY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=NzBhb+geuKCL6QO18tiLcMJYTXDE/n5jc26324KuhTsI3Oq2sDuVUd/N/czZ3YE0rE3p1nNDQGrPGY85cEaQC9wO5VaTBTTPAWYx4focQftQwILMGU5Sq/lt2Q3f8tqNidxTJoqh0rpCoso4zOmachMghyBXFUiFijn6Ymz5r8g= 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=X9SoK+CU; arc=none smtp.client-ip=209.85.214.179 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="X9SoK+CU" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2b24fcc2b5dso47280935ad.1 for ; Thu, 23 Apr 2026 22:30:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777008610; x=1777613410; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=kKEdQrvB8ytWMvKSrM0F6/GX8zuGT5l4i061TWozc+c=; b=X9SoK+CUOl0sb8fBP1xR7M79oujA3Namy+FJNRjSpgtF2HSOu0uQro3XG9UjsQE1NV UDFTKAb6xWvqaUB/+XCkGs2g1blJOo9o/qNThh4Mw5Q+bfp565Q1D2XJ5Ix/JgXRwGRA /Br4TpCf+0PcbatheNBUP9VAxLGIF9ly3H4HaMWUUEVkRuNntm07Qz3PdGq9zSsDQEur AuyRHRgi2hKKRg/7LX2iS6vOsoCKZwBA3fiNidXt6v1eT2AMkRijHgiEI++a37f9u3vr CQ4zSg6u8spAa409vKL6+K/sVCFRBfcVEVssYXsj5YVpesvGE3ej3mHLBMBQ152uMjv8 TFDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777008610; x=1777613410; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=kKEdQrvB8ytWMvKSrM0F6/GX8zuGT5l4i061TWozc+c=; b=Wkz9Obd8gTCJA9G9niik7kuCzPSDZHQo5SEiL1E5uQOwrRswGPItCXeM2xHrh9I9ne v1yd44A1xp1d14wteKiHlR2TZRN2sQ5JZ8bSLsBw20CxSN5oQWvOcbFh8M7rogC95lUN b53n08cT0xH7CZMFhu9aM8PJi9tnq7Mn7mUaS8azGQVUBVpuT83gSpNxTUdbZslwYTJX J4o670fWkIMnnpaTYwDXpwOuC1tkIn6s/KIdSmN7N6ip2RqzLiQKNZG1rxLb0H1kF+9G frbdSkAv+u7UzJpTtXtC3ypss/59KS7pDmGFcfUcwasDyshvY6/CQJ0TIJKcVU6Gd2Ye 48Ng== X-Forwarded-Encrypted: i=1; AFNElJ9aox7PMHntyhVnIYpas2Ni4UXtj+Efva6v1NFq/f8fMQ69ipqvtq6pg6AstcBGzquit3KnBsxdxFxDzik=@vger.kernel.org X-Gm-Message-State: AOJu0YzeZTFRCAcqXdYbZoGP1iBJFZxRxVtLiUy7gJ/kQc3puVtGzSTi 3doGtjpFMPOrceHVncVST2WYP+EKx13xolozK0qEldVmDD0AdBjrkSvW X-Gm-Gg: AeBDieuzERjMJIA6AN8JReCPZXslNtplfOW/r7NOKJS23Ih8f8u1slbslVJuUw3POEY Uvx7bo8h7JexXpC2t95/sI5eqwjnmjNF0ang1ShdkyGnSPKhvJkuu7R90TkoXEz69dIWy/ZZ3Er Iso7sAshLM/GHU0bD6R2KQdJevojFsKFqhLqF2xzdYHMYsfo+RR5SR0T+5oZ/smoP5EbCV98jzx SNI7DIap8A8VGHUep5OERVVikUzoGOwt4SUrSGUFSMhlZqsNPJZ3Mb9I3uHoFnhgrXpkTAWQFIG Tad9PZVlvOXaxA0w7+9RG7F3oOxz1HRFq2LzTxLOR8Sli7F1H0acWRdIgCMBmWgXmqZzHocdsWG LxWzbRKNMfRTFZOK56sFJO0dDtPocJI9gOFmug+Vy69592Zly+KRKctJ2KoZg14CqMBpNYcnW61 HphIH013pDJBl7p7hCxhzUKXZ72mQC3FeedZDCt+caKstPt2dqCftzvnX25A== X-Received: by 2002:a17:903:3c47:b0:2b0:afb4:7d41 with SMTP id d9443c01a7336-2b5f9ea4989mr306875585ad.10.1777008610413; Thu, 23 Apr 2026 22:30:10 -0700 (PDT) Received: from localhost.localdomain ([49.213.140.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab0cbaasm215188165ad.54.2026.04.23.22.30.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 22:30:09 -0700 (PDT) From: Hsiu Che Yu To: Danilo Krummrich Cc: Hsiu Che Yu , Lorenzo Stoakes , Vlastimil Babka , "Liam R. Howlett" , Uladzislau Rezki , Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] rust: alloc: add test for realloc with zero size acting as free Date: Fri, 24 Apr 2026 13:29:36 +0800 Message-ID: <20260424052936.250238-1-yu.whisper.personal@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The `Allocator` trait documents that reallocating with a target size of zero must be equivalent to freeing the memory: "If the requested size is zero, `realloc` behaves equivalent to `free`." Add unit test to verify that `KMalloc`, `Vmalloc`, and `KVmalloc` all conform to this contract. Signed-off-by: Hsiu Che Yu --- rust/kernel/alloc/allocator.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 63bfb91b3671..6120587db205 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -305,4 +305,23 @@ fn is_aligned_to(&self, align: usize) -> bool { =20 Ok(()) } + + #[test] + fn test_realloc_zero_size_is_free() -> Result { + const INIT_SIZE: usize =3D 64; + + let old =3D Layout::from_size_align(INIT_SIZE, 1).unwrap(); + let new =3D Layout::from_size_align(0, 1).unwrap(); + + let ptr =3D unsafe { Kmalloc::realloc(None, new, old, GFP_KERNEL, = NumaNode::NO_NODE)? }; + assert_eq!(ptr.len(), 0); + + let ptr =3D unsafe { Vmalloc::realloc(None, new, old, GFP_KERNEL, = NumaNode::NO_NODE)? }; + assert_eq!(ptr.len(), 0); + + let ptr =3D unsafe { KVmalloc::realloc(None, new, old, GFP_KERNEL,= NumaNode::NO_NODE)? }; + assert_eq!(ptr.len(), 0); + + Ok(()) + } } --=20 2.43.0