From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 0755C2F747D for ; Sat, 20 Dec 2025 08:41:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766220087; cv=none; b=TsOM43ZFdy87+9wnQe/v/KapzXLBjds0CRclgteg2skHZCPSXq2RDjSFlMuIKJRNTsLtwoGuD2UOLb2wrKWkwZnwbADXIaW2RH4yQ0RrcUuqjVmRUcsjBnKsL/SvX19EJQbjB3/L2O4qjxSFs6IbpbMk/QvdrUud0LnHHgI0TAI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766220087; c=relaxed/simple; bh=jA8x3+Ev/73DySJ69fZiQRQYow8d0Sy2QpZdncY5jnQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=tKOEg5N90S671CrqgAG2sch23ra8Wi+aWuKHhyp3orRGSwAtLuhWHDwMYRMWvOZNFGNsDZOIDDKvQsPddwAwganTQQLPbfIafxvLXQz0FtiivvM9e2HhBGCr70JoKFz/IvaMn9QWCDqYUKIepB2XT7uDB3cwjbZDDfxX8kPhzis= 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=Ic3kwImJ; arc=none smtp.client-ip=209.85.218.42 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="Ic3kwImJ" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-b7a6e56193cso425185066b.3 for ; Sat, 20 Dec 2025 00:41:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766220084; x=1766824884; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=bWMu34v/hHkSDoTf1KqyJ/0RKvtfdSHBb4H28JDtcl0=; b=Ic3kwImJDhqRDpx+YALzM2Glgmuybn1xQ3tmzQyx5BWcI/c1wSmIf2n5YlVHkotsYi wIvPTUVOrn6LZ6mCg1EziwsGhJJmEXu9j3hCxAxZYKk6H1yTQO5KKVjcHNsDRl6cG1VQ ATjk1Ygnm/cQrTQu7wMmKMNXgsZ9zEhvvxxyydLylQVEHCbNkA0bvdLY0QvCFFiFWbXT DLU2xPKbDEihkOK3aycrgYTlsCUNPav+W6KAXiWaeNB8VlcjTbHAhwiFcJ7i8DUouZlP z4HM9gWzNC3dqkse9pZIEwJT0+9NVqEIcxZAKYik0aRy4KWc9gbzoyq1gn2UUS4NnZFF u6kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766220084; x=1766824884; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=bWMu34v/hHkSDoTf1KqyJ/0RKvtfdSHBb4H28JDtcl0=; b=PxuDCIhpd9vOt4LHz5wiAhR5LvOL8wpxZpl7nI4dXvuRc59GdvUt/kpjWHtPMWZOGJ cp6KpzmQ+hVShC1pMztus1aykRDF5aMihBr74qFLBj4rUBhv3ufQG0C0m1L4ad2Taud1 A6qojqYcTwStx5KHjLy6SUYlV+cF4OqerBVgUMOJxgn8dANlcEgwclQLwxBX+TDQ46ED gcnBLu/eGa0mMTgGHbs5BTIfDcDUVL0uouQZcfzSLxO1ls7Bv5ytcvaY7c/KsZiETAzJ DPvclq0JCsPrYzl8BJZctfLcTZhS1ijnkna5bxIQfiMgFWOOKEY9pbLxhVhqgH85Q0zy sywA== X-Forwarded-Encrypted: i=1; AJvYcCWhKOAJ4vylVx7x6CCuWvs53+YiV3+IHED9S0PBLWgCOv/jxDNiq1ZIwcqjfupUAkvwaTPw+/QL1FQCkv0=@vger.kernel.org X-Gm-Message-State: AOJu0YwvrLsEF6qFYqiIxplicb8MoGuzp67o8vu249nZae7vF2/hPY/w u4pruD3ltKdRfsa68fjHwpsyjYkXinxQlHr9swuFJX1muNrCyaUzwvSP X-Gm-Gg: AY/fxX7IsFjftmMINWrncILDK3RHMa+r7UE3qt8E098VU7nT+cdj7XoY+CXtLHY0tvQ 94/48jLJDubEfwCr2YtJaOBDt0l456iNYxP9e6nr7To1WL2DJGb+oT8wCF0s+tWKsW4F2LQDRpL rvjKShd+Z3XoW/2bMwnb6oRMagNjrM5/BibJ8JZh6XSDHozsPXyhgvq7CIUegW34nta61SwyCoJ Akun1dCTnZTnfBS8zqK6tZzkphxfgOZAwCvZW0Wuqga1h0G9T5gm1coIBYhfuJJqBtWUs8kxL53 yB446jJ3zaB7Xut+2SXeULS3/Burf53hMCYACDkcwZfiNI6hp++dloXsOrFcqjGefxn/AUkQFc2 wezQY2O3IasUbKiRbcbuP1C0UoP786UmsNus9tw19L6vCELwpOreoea0QOSlnThszQVUzl471El 8nA3pFdaJj25b7Yz0rOYykvOBLa1YmKs6WxBeyz+wyaTthKBWqac+XDFMOuk+8sSUTLNGR608aD Il4Iyjr+wr/NU6RH4eOENguDxaJw1auTPpyxtbZ1RoSSg== X-Google-Smtp-Source: AGHT+IHZzjoS9V14RENmLKlf+khQqIifVYxxHt4K4jB0VUyp6GWo7+1rwbjQADCiEESqZ3CgtTPG4w== X-Received: by 2002:a17:907:960c:b0:b80:b7f:aa32 with SMTP id a640c23a62f3a-b80371da8f5mr551727066b.41.1766220084018; Sat, 20 Dec 2025 00:41:24 -0800 (PST) Received: from ?IPV6:2003:df:bf2d:e300:6a92:2e3e:53e0:d832? (p200300dfbf2de3006a922e3e53e0d832.dip0.t-ipconnect.de. [2003:df:bf2d:e300:6a92:2e3e:53e0:d832]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b8037de0de1sm452672966b.40.2025.12.20.00.41.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 20 Dec 2025 00:41:23 -0800 (PST) Message-ID: <9efae3dc-05d5-475b-bc69-250c2186dd70@gmail.com> Date: Sat, 20 Dec 2025 09:41:22 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] rust: page: Simplify overflow check using checked_add() To: Kari Argillander , Alice Ryhl , Lorenzo Stoakes , "Liam R. Howlett" , Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich Cc: linux-mm@kvack.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org References: <20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com> From: Dirk Behme In-Reply-To: <20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 19.12.25 22:29, Kari Argillander wrote: > Replace the explicit bounds comparisons with a single checked_add()-based > range check. This avoids redundant comparisons, makes the overflow case > explicit, and results in simpler generated code (checked with godbolt > for x86). > > Option::is_none_or() would be nicer, but it requires Rust 1.82; the > kernel currently targets 1.78. > > No functional change intended. > > Signed-off-by: Kari Argillander > --- > rust/kernel/page.rs | 18 ++++++++---------- > 1 file changed, 8 insertions(+), 10 deletions(-) > > diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs > index 432fc0297d4a..a07e6d256860 100644 > --- a/rust/kernel/page.rs > +++ b/rust/kernel/page.rs > @@ -239,17 +239,15 @@ fn with_pointer_into_page( > len: usize, > f: impl FnOnce(*mut u8) -> Result, > ) -> Result { > - let bounds_ok = off <= PAGE_SIZE && len <= PAGE_SIZE && (off + len) <= PAGE_SIZE; > - > - if bounds_ok { > - self.with_page_mapped(move |page_addr| { > - // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will > - // result in a pointer that is in bounds or one off the end of the page. > - f(unsafe { page_addr.add(off) }) > - }) > - } else { > - Err(EINVAL) > + if off.checked_add(len).map_or(true, |end| end > PAGE_SIZE) { > + return Err(EINVAL); > } > + > + self.with_page_mapped(move |page_addr| { > + // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will > + // result in a pointer that is in bounds or one off the end of the page. > + f(unsafe { page_addr.add(off) }) > + }) > } > > /// Maps the page and reads from it into the given buffer. > > --- > base-commit: cc3aa43b44bdb43dfbac0fcb51c56594a11338a8 > change-id: 20251219-rust-page-check-819ccc39c53a Thanks, this switches to the early return style we recently discussed in [1]. I just wonder if we would like to add a comment like // TODO: replace `map_or` with `is_none_or` once the MSRV is >= 1.82. like we do in e.g. transmute.rs [2]? Anyway Reviewed-by: Dirk Behme Thanks Dirk [1] https://lore.kernel.org/rust-for-linux/CANiq72kiscT5euAUjcSzvxMzM9Hdj8aQGeUN_pVF-vHf3DhBuQ@mail.gmail.com/ [2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/transmute.rs#n75