From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.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 4FB3233AD89 for ; Tue, 23 Dec 2025 12:51:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766494266; cv=none; b=dv7UHpwzeuPmoR+Eu4Zy2iG/8mIXNBr/9JH4dkfLzVxxRB4zMc4lDUu8FZnTVAZFmEVnb0Pvj5fc1ogATUug8UjuYE53XAMwYkZCZDhGJ33gvbqPJjWRU8tENGplnW/OahfaOOWJASU9WSZ526wk/bwsKNCSZNMxTIRk4usF3qk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766494266; c=relaxed/simple; bh=mBkbJS2J+y697LV76VdrD/L4zDga0KCQkfpOno+j3iM=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=gNnOgB6GTwS064XkUvPnZh2gN8GScHMmcEMU5f5leAIxymar0k+wD8tREH59NP1WUbo6UKRgoLoifNuLDtA71EzF6R93TV1LAV4Gocz/abpWjUvVgZeBWqFv2x/1TkzpF9XDApbzdMZ+T+BbIgK+YFLnJ65cT6ujVlslAZ4s5YQ= 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=SjOrpq0Q; arc=none smtp.client-ip=209.85.216.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="SjOrpq0Q" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-34c277ea011so4941818a91.1 for ; Tue, 23 Dec 2025 04:51:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766494263; x=1767099063; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=94oLudekiif5HeilrLZvUbicmHfgUxsRz7xoRTDax8c=; b=SjOrpq0QeNfpaQrlLpfIBgFqSSEG2kbaZitM3W90av2bfg1QcnXeXZM20ra4zS7ixN OkAqgDcq0Hk7ev90nZkawY+vZhdMAIrcN7mD2nLtVR480fcBaX1ordntQjjZpvt1FDJ8 wGZwJ/fcyJMAD53X7EWheXRihQCKRVGTegQo0dmiNMqd1lN+OFlqYxelWfI+3j386KI8 c6nSxeHdJExdKYJ3u/xSOSuBiOxE4pKq9L6r2nWWHA/dU67qWYhMbsa9lI/quFaJ8MDp JKdDUiLJjV45gzo2SKYkSt4+RkmiH0j+NAoN+Z1q02pWQ+4iC75wBch2nm8LwxkA6BxB p6mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766494263; x=1767099063; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=94oLudekiif5HeilrLZvUbicmHfgUxsRz7xoRTDax8c=; b=m3Dtg2MxQMOZTd2uqtWmsbeZ1O8IMT5cla4I/Nq97+M3uCdX0RLhN7IQAXaLI0sNiQ Y4jgwhrx3puV9/wREzKyHECBIiTAK/dxQm4UIuuadCeaFQDooHitoftqIBLQLXoFqp3Z iAXcWokfskPc3UNe3fFmogmVHgWuL0a6LETV6Kk7rO6OEs6IuViCw6ynF8gCFWTJFgAK 90aCkKeJgtog6MKP4rnWOIPkLaiiEzM1BtFUdgw1iYUzJ/u8ah0Dz1ivnHJxRrOSnc/q ymav06yXgAh9aqGoyUw2FtBO8G8Nx8+woovhF2bGbeTpwDIw9iwpbbpnFOB4K/YtvmCb yJDQ== X-Forwarded-Encrypted: i=1; AJvYcCWnMzLIxBmAvBQjtIVDEXePNJWBvVLjBOacAeBrqvCt0MHd/aheXpFoD+C32mfxmASje4tpFyBNqiqlp9k=@vger.kernel.org X-Gm-Message-State: AOJu0Yx676JMI/MYNJrUG2Hq56nWc08olQwLjlq4yuqil5Amj2sPQElD 1UKAqRBOJhSL+EhyJmwwuSaML9Wak7nMhMEiUVln55sFXbAOmOrV/meCfwccK4cAdo7ncbMpB6b e8g2NpAvXWqx7tQ8gfDwY5CFZbjrNh2w= X-Gm-Gg: AY/fxX7kpMj9grWk1FKC5q6RJCeur7HbeWYEkIBq9LTdfTKApybWUF9MESyz+94GYMH +eO1mSVM7VGNK2KhEK5hN55KFIKRAnKtqo9NsYXM5mwnk/++kvWDy/1lA5CRqYVkn62r2R2txBQ tTcTSVCX3XeGjv0H8JGBQS/zOGLO911pXXIu04zD5ir8wNxyFlY3cvkk/L0+AZJBmef10O1gX6C wB/yr9GAYkm1i9INTc7ywAomfWAQ0HMkHVme+5XVo0oz3e8BE7SsD90R17N12l7VoQLqRJmPlJE KFLfA0iUXW08voysjVB3fm0q6tdUgQRlLQE8lw== X-Google-Smtp-Source: AGHT+IEyXen1C1B8ovK20y6lttFcVNSX1HJ8t8g6zVHxBqNhBj98DsBKXvaXLLqY6Msx/fJ20+CpJ/vT7PMw8NgCuG4= X-Received: by 2002:a17:90b:524b:b0:343:6a79:6c75 with SMTP id 98e67ed59e1d1-34e921e05bcmr12664658a91.29.1766494263489; Tue, 23 Dec 2025 04:51:03 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20251223-rust-page-check-v2-1-fba3dbdde1d8@gmail.com> <20251223115726.621bfa38.gary@garyguo.net> <46e188a4-4b74-4206-ba18-f107f45bb98d@gmail.com> In-Reply-To: <46e188a4-4b74-4206-ba18-f107f45bb98d@gmail.com> From: Kari Argillander Date: Tue, 23 Dec 2025 14:50:46 +0200 X-Gm-Features: AQt7F2q8qcvJUN3cNbmkB3zGBaUJ3rux4__s9gM96stJQSLixTZEWUtt842AGQI Message-ID: Subject: Re: [PATCH v2] rust: page: Simplify overflow check using checked_add() To: Dirk Behme Cc: Gary Guo , Alice Ryhl , Dirk Behme , Alexandre Courbot , Lorenzo Stoakes , "Liam R. Howlett" , Miguel Ojeda , Boqun Feng , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , linux-mm@kvack.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" On 23.12.2025 14.29 +0200 Dirk Behme (dirk.behme@gmail.com) wrote: > > On 23.12.25 12:57, Gary Guo wrote: > > On Tue, 23 Dec 2025 12:06:17 +0200 > > 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). > >> > >> No functional change intended. > >> > >> Reviewed-by: Dirk Behme > >> Reviewed-by: Alexandre Courbot > >> Signed-off-by: Kari Argillander > >> --- > >> Changes in v2: > >> - Added MSVR todo (Dirk Behme) > >> - Link to v1: https://lore.kernel.org/r/20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com > >> --- > >> rust/kernel/page.rs | 19 +++++++++---------- > >> 1 file changed, 9 insertions(+), 10 deletions(-) > >> > >> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs > >> index 432fc0297d4a..cd2af7e4c357 100644 > >> --- a/rust/kernel/page.rs > >> +++ b/rust/kernel/page.rs > >> @@ -239,17 +239,16 @@ 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) > >> + // TODO: Replace `map_or` with `is_none_or` once the MSRV is >= 1.82. > > > > I was about to suggest just enable the feature gate, but turns out it was > > only added in 1.81. That's a fast one to stabilize! > > > With Alexandre's `matches!` proposal I was thinking that we could drop > the TODO comment and we won't need to touch this file again once MSRV > is >= 1.82. Opinions? Personally I like `is_none_or` more than `matches!` but then thinking again I agree that it might be better because we do not have to leave TODO here. I will respin again tomorrow. I also checked that output for at least x86 is identical. > Cheers > > Dirk > > > > Reviewed-by: Gary Guo > > > > Best, > > Gary > > > >> + 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 > >> > >> Best regards, > > > > >