Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp310507rdb; Thu, 25 Jan 2024 16:47:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IEY0xuzQ2YXsXDluGEPHF5Gx3dz03wSTS8lKMrH+B51sTYjGQwReSI5q9ioZnjJiG3SCwfi X-Received: by 2002:a17:906:1685:b0:a2d:e9a3:ab1 with SMTP id s5-20020a170906168500b00a2de9a30ab1mr194659ejd.23.1706230036950; Thu, 25 Jan 2024 16:47:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706230036; cv=pass; d=google.com; s=arc-20160816; b=GNGvKgZwub27nSqlutbuClpH1a/yTRkWdR2HrompaxqVXZYk6sDw9l4UKMfhaI6RtU iqEGVrKUGeYuAXII4HgDOTSH1bm8cws9irXDERUv3OSN7fCzznzYIMdZmyAvCLrHxVVA L4uAi/DXdVt+NlhKFiz+09i+S2qX/Nlb58GG4wI9H7REb0JdFM3Ee1OAVaqXOSSUi4Gm 2aAym4ASzHSSO0wA4ZVjCMjbnOrjrfUuoTOAvC4/RKW7UyNXDxpitcPIxr8TiXeirfQ5 xdg3ujmiK8QJbpF9ZO5Emh54ZkjkLFi4guqFuPc6ApDOR0A67ObSa8n1hnjU/ycgx8Zv ftLQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=eb0k+mslh6p4v5lTPXQ4s3OPRdfCkOzWaYkI73LF69Y=; fh=QtLrAbjSgRZ96MkutsPOHkNM9qsf9vUPQY4fJ54Rc+E=; b=C8/f/mSgmWWpBvYl7HhFhOB5lBJNa10AO1wkdOl3mhIPhYqO71jOGMLMpQreLsz62d cIk/jLqSWy2KpBIEjV9WWnkk+0vPkHC0Qi+nxLcGD4++nvlPx4IvuUGkMbHIJVfA8zxn 00rBjPBsFGti7HT+ZuxWLYppAS/ORI+iC1rdWfCUYIPmySo/doyYpdehMTGOgwb6USMD DDqQVv1GugoqBZxosUIaeoT19OMgrpsW/+aHEoSOVdylO00oIQTOpKvFJVtLjMWMJrCp nfW8u2fvyKU4WB9WtVSC7MtqB95IoMj32mHq4EP/YUVqbH85jdTd3UIxA/ys3IFHENaK yiUA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=nrDb0ydF; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-39468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39468-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a25-20020a1709066d5900b00a2b61bbdc9fsi20618ejt.214.2024.01.25.16.47.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 16:47:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=nrDb0ydF; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-39468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39468-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 657E51F2579A for ; Fri, 26 Jan 2024 00:47:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F42B10FF; Fri, 26 Jan 2024 00:47:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nrDb0ydF" Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (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 0E877362; Fri, 26 Jan 2024 00:47:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706230026; cv=none; b=F4vpM/t1XVvIcwJOI9N5PUCs+UVCvy54qBA3ed1iHbzrwJeC7B+Tus/mfM6qio2Q4XM4feM9/Hebt4imVXMdUWv+LQ6NaMqfbTBisbq1Rz5b8GdDrJdWxY5C8BD3sNOWGkj4039yrVe+5snynK41/hNbaXYLCHH+XTXFIbwkkvA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706230026; c=relaxed/simple; bh=aZU67BcgtYdYWLFpivNFnPhz4A5DSRb9LWCNArdlDik=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=AifIpL+pKk83seIXxllOTP0dqHmf7RfZchYO213GV5A5h3Qp5p2Grw99fzKk4gn32Un7orkEManeERQ6sw3WEGzh23Yyb5x1GA9woCBNrrENOFgMFC1Q9wcM9E6vpX9RyXUyDbF81LsY8I+hyyLKUYBK8+XbPpLtlejVW9HRgA8= 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=nrDb0ydF; arc=none smtp.client-ip=209.85.222.174 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 Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-783cf58221fso1162385a.2; Thu, 25 Jan 2024 16:47:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706230024; x=1706834824; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=eb0k+mslh6p4v5lTPXQ4s3OPRdfCkOzWaYkI73LF69Y=; b=nrDb0ydFV9gB9/NgQQwP4cIfEYCQ+KIqvIxo//WYKGUiBihfsjyzGLbvySUDRyHVdN 0sVt468TdvIadHHwAeEeRsP/43NO1fdUmG+ENvzWY29GfJDvbMYYUyIf4dnwm8hbhfd9 nIp5sS1ThBU8UJRMTQomXbQ1tJzbSau1u4dW1YLT3ai4+LtnriTNDW7kE7xJyijmYOuK csOd7bNNiHBjX4PE68qVRaA3J8eSjs8Ch21VESboBV9IEdva/XOU7SdHCy/awONT0uNN VJjxx3Si80REQU2+WIgu2/8o8r/x8V07yKt42Z1bPDcJrPknkYbMH83obx9/3FZDemS8 Y3lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706230024; x=1706834824; h=in-reply-to: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=eb0k+mslh6p4v5lTPXQ4s3OPRdfCkOzWaYkI73LF69Y=; b=dw2JqNWbj8JBHx+cmtJRXN/x34kAVxHnazMFCmqH4ydiIqz3eiT3TAezZWWvGqm1B2 fdUz1+mbtSLBARtjE7ym5GtHb2TtCffg+Yj1rh1EdP68Y4t1LvTpxRzOXa8q9wczu9T6 XYTiO+8350w4bNdqHZXGYO83AbKCB8cpnQ3YFZP4qsXME9DAjD/s40uLV6BD1TGb7WUA jwJjKQYe+vVfdty5lEXPCyI0b+dslXJ8ao4eoZs2t+DckIspQoTXyx/3aqnzOfnUsovZ f/NM47d0KSqIzUfPzhH8uHcUSbhAZ6/uDZXI4KC7AmdszZh1MbctE226QKaVnZV6b3Aj j1Lg== X-Gm-Message-State: AOJu0YxKfyTpAMpvoJYCHgLM7h/MSNmG/oXjF/3oBhHgvByPAyPRTwDw nxoBvtEpBM8L7/OLDRT4sC/unmrbcXDQiHTEKuxX7Wht0fcIWGsN X-Received: by 2002:a37:ef15:0:b0:783:c869:8367 with SMTP id j21-20020a37ef15000000b00783c8698367mr562326qkk.139.1706230023816; Thu, 25 Jan 2024 16:47:03 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id m3-20020ae9e003000000b00781bdb17d8asm70244qkk.118.2024.01.25.16.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 16:47:03 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id ACDFC27C005B; Thu, 25 Jan 2024 19:47:02 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 25 Jan 2024 19:47:02 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdeliedgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleeivedtgeeuhfegueeviedu ffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 25 Jan 2024 19:47:01 -0500 (EST) Date: Thu, 25 Jan 2024 16:46:15 -0800 From: Boqun Feng To: Alice Ryhl Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Kees Cook , Al Viro , Andrew Morton , Greg Kroah-Hartman , Arve =?iso-8859-1?B?SGr4bm5lduVn?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Arnd Bergmann , linux-mm@kvack.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Christian Brauner Subject: Re: [PATCH 3/3] rust: add abstraction for `struct page` Message-ID: References: <20240124-alice-mm-v1-0-d1abcec83c44@google.com> <20240124-alice-mm-v1-3-d1abcec83c44@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240124-alice-mm-v1-3-d1abcec83c44@google.com> On Wed, Jan 24, 2024 at 11:20:23AM +0000, Alice Ryhl wrote: [...] > + /// Runs a piece of code with a raw pointer to a slice of this page, with > + /// bounds checking. > + /// > + /// If `f` is called, then it will be called with a pointer that points at > + /// `off` bytes into the page, and the pointer will be valid for at least > + /// `len` bytes. The pointer is only valid on this task, as this method uses > + /// a local mapping. > + /// > + /// If `off` and `len` refers to a region outside of this page, then this > + /// method returns `EINVAL` and does not call `f`. > + pub fn with_pointer_into_page( Name it as `with_slice_in_page` maybe? > + &self, > + off: usize, > + 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.cast::().add(off) }) > + }) > + } else { > + Err(EINVAL) > + } > + } > + > + /// Maps the page and reads from it into the given buffer. > + /// > + /// # Safety > + /// > + /// Callers must ensure that `dest` is valid for writing `len` bytes. > + pub unsafe fn read(&self, dest: *mut u8, offset: usize, len: usize) -> Result { > + self.with_pointer_into_page(offset, len, move |from_ptr| { > + // SAFETY: If `with_pointer_into_page` calls into this closure, then > + // it has performed a bounds check and guarantees that `from_ptr` is > + // valid for `len` bytes. > + unsafe { ptr::copy(from_ptr, dest, len) }; > + Ok(()) > + }) > + } > + > + /// Maps the page and writes into it from the given buffer. > + /// > + /// # Safety > + /// > + /// Callers must ensure that `src` is valid for reading `len` bytes. > + pub unsafe fn write(&self, src: *const u8, offset: usize, len: usize) -> Result { Use a slice like type as `src` maybe? Then the function can be safe: pub fn write>(&self, src: S, offset: usize) -> Result Besides, since `Page` impl `Sync`, shouldn't this `write` and the `fill_zero` be a `&mut self` function? Or make them both `unsafe` because of potential race and add some safety requirement? Regards, Boqun > + self.with_pointer_into_page(offset, len, move |to_ptr| { > + // SAFETY: If `with_pointer_into_page` calls into this closure, then > + // it has performed a bounds check and guarantees that `to_ptr` is > + // valid for `len` bytes. > + unsafe { ptr::copy(src, to_ptr, len) }; > + Ok(()) > + }) > + } > + > + /// Maps the page and zeroes the given slice. > + pub fn fill_zero(&self, offset: usize, len: usize) -> Result { > + self.with_pointer_into_page(offset, len, move |to_ptr| { > + // SAFETY: If `with_pointer_into_page` calls into this closure, then > + // it has performed a bounds check and guarantees that `to_ptr` is > + // valid for `len` bytes. > + unsafe { ptr::write_bytes(to_ptr, 0u8, len) }; > + Ok(()) > + }) > + } > + > + /// Copies data from userspace into this page. > + pub fn copy_into_page( > + &self, > + reader: &mut UserSlicePtrReader, > + offset: usize, > + len: usize, > + ) -> Result { > + self.with_pointer_into_page(offset, len, move |to_ptr| { > + // SAFETY: If `with_pointer_into_page` calls into this closure, then > + // it has performed a bounds check and guarantees that `to_ptr` is > + // valid for `len` bytes. > + unsafe { reader.read_raw(to_ptr, len) } > + }) > + } > +} > + > +impl Drop for Page { > + fn drop(&mut self) { > + // SAFETY: By the type invariants, we have ownership of the page and can > + // free it. > + unsafe { bindings::__free_pages(self.page.as_ptr(), 0) }; > + } > +} > > -- > 2.43.0.429.g432eaa2c6b-goog >