From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (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 8CC8F4A1A for ; Mon, 25 Nov 2024 16:20:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732551625; cv=none; b=T7CrpTWLULcbuJtqzwvTEQnKHjCRnthutw2vZPdceYCQ92FikYDDPzYlflLaGyfSfSuLDzHgO2Y5PBzbis8joXYsDRSYK9ZZt0IHiQgMAn+FiRFP/EJJsSqMSLywGELaZgWAPPfdiai1q5id00u2DurSqi2ATsaSQrU15DxXuRI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732551625; c=relaxed/simple; bh=wiJO8EafWrL3t6qlIiyV2F5okSL6L2pFYmTGRurbNkY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=A/e+jGioaxEuSiUzV7fYlGSTwg0VRXr1sz9rEDrFw1RbzACoBaLtsAtzketjIn9hR2CmTEVGMwntpdqXnFhzXORog0e17Z1dlZSOi0zxCKEkFIYbqrBE1MFJNLgEtW91Z41GQ4Trno8AlgD4FvY510WDv5ea9EWM7XxUwo2UuLE= 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=k45fQzAo; arc=none smtp.client-ip=209.85.219.49 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="k45fQzAo" Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-6d4241457b7so30264836d6.2 for ; Mon, 25 Nov 2024 08:20:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732551622; x=1733156422; darn=lists.linux.dev; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=kGAJxqXAxllg/W2VuJ5ECjZPkQ6tCWdsZghimeCyrgk=; b=k45fQzAodlQF0ny6oU0bg1nfFT7qs67dgNq4GZWZm1k1GlBUy+HITxlpCZLCiriuwg Zor+netV51MqW6hP9fKeeu/WCRgyirwF4OGDAVFWfek+S5aGSA0DqrwORu1oBsRDxYrl LbCtzpoXawri7mCKAoVyCw17sZRhz0w0gkXv0A7GdOy2PjoqSgOOdFDtV1WRMO1L6XMY VvvMuQuWXyIuYd8YDr+EyyFsEt8uaBBiA29lOC8C2X6NWY96FEx95TTecnkjPLOWj4w2 ek8QSJA54V/PqvWnm7kwVDpUo/+75eCJKUryaiFhvSD0Pxy0ktEZUcQ4xnpknSZpqdWp kbcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732551622; x=1733156422; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kGAJxqXAxllg/W2VuJ5ECjZPkQ6tCWdsZghimeCyrgk=; b=F4snSRUv2mKcERCVVz+ICu9DD8OTHTs9bYgiruXJuh+WtC6T3inxM9evuW/XrO7N4c StCd/2fYXPATvgySuF4qPAl+pP74dhs0FXPwHToLOqusgzfXo0ZPSRairUtRgkq+hOzv 7dXY1C42adscSEoc51/V8N7ue8UEMb1Mk5ubmxe1G+vAjbzTCtbkThduLZd7Ku3MjqMC Aa1p+jwDV6jKRdpp68WavnlZ1UM/Jr3NXwO/DrxkpQn6+BqIiTfWb0982giNfysjGBIf tUfi/xhNl16/jjHOjVfv/fKYKly7YmhlZrStjAXp5h9zbDvjd3oqlZDyauNZJ4+iIR+K 2OFQ== X-Forwarded-Encrypted: i=1; AJvYcCV5+xXizWON9I9KurGmtoPGjQ3je0iZRLFY2/AuRnl0xmOqgi72F26TdzRjmlxYJYz1t22/HQ==@lists.linux.dev X-Gm-Message-State: AOJu0Ywq4V1BXRDYbM+5ZsyVeBZ234WZtcY7fWDQmFgvISRS7LBOesHP Uq/uvZcftUtR8SNj/WhFmzFpVJjOqyMmzozHvhl5hdTAzl5nglfC X-Gm-Gg: ASbGncv26llFPVedu7ZMlVEbHe03pC22Ww0nmn0ELU4rf5T3O49Q2InNnZ8UtC11tLP 58zXmUFPa0H4FnzlEt10hUpmeH4mbSjRjBe6yeHXge72f6BaC1Xd+kFwlOtV7519XdaUgJDQse4 +6cDCw2GCvPsXCBX05zj0pf8sxacME6BBR9ZIwRW92n9GNYh2FxT7b4Q/utySe5PZMj8jhXF8fs OhqtXcvU5Ej+Bm+kLHStrWE5Ka6GDTTGGhyLhbe6+915GEGInOkaBJ9v26ZSrLAj1hlnGWWqdAp kNnyk7qCoVgT5wAp3mBq99F6g3JlwsBB1oNLIwjd X-Google-Smtp-Source: AGHT+IGPkw8t4N9gDzf9lwkJ1qKoELH7ZpcCc2tznTmePFzUIMnxy8viKSeF32UNYG2rISDhl961FA== X-Received: by 2002:a05:6214:508e:b0:6d4:287d:b8d6 with SMTP id 6a1803df08f44-6d450e7c2dfmr232620986d6.18.1732551622507; Mon, 25 Nov 2024 08:20:22 -0800 (PST) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d451ab6198sm44259746d6.68.2024.11.25.08.20.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Nov 2024 08:20:22 -0800 (PST) Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfauth.phl.internal (Postfix) with ESMTP id 73B0B1200043; Mon, 25 Nov 2024 11:20:21 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Mon, 25 Nov 2024 11:20:21 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrgeehgdekhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpeffhffvvefukfhfgggtugfgjgesthekredttddtjeen ucfhrhhomhepuehoqhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrd gtohhmqeenucggtffrrghtthgvrhhnpeevgffhueevkedutefgveduuedujeefledthffg heegkeekiefgudekhffggeelfeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhi thihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmh grihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohepudeipdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopehmihhguhgvlhdrohhjvggurgdrshgrnhguoh hnihhssehgmhgrihhlrdgtohhmpdhrtghpthhtoheplhhinhgrsegrshgrhhhilhhinhgr rdhnvghtpdhrtghpthhtohepuggrkhhrsehkvghrnhgvlhdrohhrghdprhgtphhtthhope hojhgvuggrsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegrlhgvgidrghgrhihnohhr sehgmhgrihhlrdgtohhmpdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpd hrtghpthhtohepsghjohhrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhmpdhrtghp thhtohepsggvnhhnohdrlhhoshhsihhnsehprhhothhonhdrmhgvpdhrtghpthhtoheprg drhhhinhgusghorhhgsehkvghrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 25 Nov 2024 11:20:20 -0500 (EST) Date: Mon, 25 Nov 2024 08:20:13 -0800 From: Boqun Feng To: Miguel Ojeda Cc: Asahi Lina , Danilo Krummrich , Miguel Ojeda , Alex Gaynor , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Janne Grunau , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev Subject: Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations Message-ID: References: <20241123-rust-fix-arraylayout-v1-1-197e64c95bd4@asahilina.net> Precedence: bulk X-Mailing-List: asahi@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Sat, Nov 23, 2024 at 06:39:23PM +0100, Miguel Ojeda wrote: > On Sat, Nov 23, 2024 at 11:30 AM Asahi Lina wrote: > > > > We were accidentally allocating a layout for the *square* of the object > > size due to a variable shadowing mishap. > > Good catch, thanks! (Square?) > While we are at it, I think it'll be good to add some example/tests for those functions of ArrayLayout, for example, the below will catch this: I will open a good-first-issue. Regards, Boqun ------------------------>8 diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs index 7e0c2f46157b..bb3ce3b2218b 100644 --- a/rust/kernel/alloc/layout.rs +++ b/rust/kernel/alloc/layout.rs @@ -7,6 +7,7 @@ use core::{alloc::Layout, marker::PhantomData}; /// Error when constructing an [`ArrayLayout`]. +#[derive(Debug)] pub struct LayoutError; /// A layout for an array `[T; n]`. @@ -43,6 +44,20 @@ pub const fn empty() -> Self { /// # Errors /// /// When `len * size_of::()` overflows or when `len * size_of::() > isize::MAX`. + /// + /// # Examples + /// + /// ```rust + /// use kernel::alloc::layout::ArrayLayout; + /// + /// // No overflow. + /// let layout = ArrayLayout::::new(12); + /// assert_eq!(layout.expect("sizeof(i32) * 12 is 48, not overflow").len(), 12); + /// + /// // Overflow, should return `Err`. + /// let layout = ArrayLayout::::new(isize::MAX as usize); + /// assert!(layout.is_err()); + /// ``` pub const fn new(len: usize) -> Result { match len.checked_mul(core::mem::size_of::()) { Some(len) if len <= ISIZE_MAX => {