Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2536871rdb; Mon, 5 Feb 2024 09:25:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IF1idf8s9UqoIFBct2CIN26DVuB9OZDvZ9e0h6vNcipIXHhYW06zRRG16O3ZMH0eiG2wVrW X-Received: by 2002:a05:6214:f08:b0:68c:4306:b8a7 with SMTP id gw8-20020a0562140f0800b0068c4306b8a7mr44038qvb.9.1707153917853; Mon, 05 Feb 2024 09:25:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707153917; cv=pass; d=google.com; s=arc-20160816; b=RKWPEw/Z15xXCgdA0bykyaC6OLK9rgrhUp1GDvfgKuJVSE8JskhbJNbDUCCXhpUlkB 45fQUupysaYUCUBSLhHkVenAJaPM1MX1lXAyJjpqYbGCrvQCtx4jGVR3wXn/NoZTKnxU XlGSzl9PiDbFpdr/3SpBOEz8U7CCcEkzMF0m6nIEECMMqjwrVBhkhODzduL/rl4fxJZh EYRTQC4d/KBaVpOjJRMPKmP0r52YOlWrf7z0TCsDFcElxeix5lgfOhmI+pEOACX4kChY kSFsiT9FsPgnb7JVUtBvOI6H1XtP6HRh+/OYsRfGKf/7wM+Jw99D4YqE8xytkbPF2wMa nX2g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature; bh=K+cat8wRiFE8ayUEL9AAC8rZ9xDFXFmCN5IZ9qalElU=; fh=Q7IKnf8ZBFLYgtI8hHNaWYZYWlYrZJQGXhE108cwU/8=; b=tBqcGihnhhuCFJOm2bJfaKMj8Rbw4FUBIUnxDtpfyfd0ZnXZglXyMJfjEYUe3FgUbO k325meJm6AS7y2z2yfBiOPaNPA1ycnQdFRWSQggsAmVwYptCCQ4ct3rn8xyT6odiyxNs VkKwXuzw20pYj0drmNuFJ+TrLYh7nL5VGzQKd7M0xEWJPJveUf8lNUkuwknllb0a3WyF ZIkQH3lZNOIhjzaHtv+o5eYZEQySnGqoyGjRTYCK0o9Z1paa4QJl7jzKS9XDxZ4ZKg2N bOFGnaEBRFGgKBRAXEALVBQ6IU9c38KMhTmTuWZ4mujVQYaDowVAZ53KQGt4ZYH07KrQ ZMvw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Y5njpc12; 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-53097-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53097-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com X-Forwarded-Encrypted: i=1; AJvYcCVFBCPFXFqAhcg8JWtYp7TEBSYJEuOoQibmdMqErCDlUJzbKB6JhYuzdXW0l7DdYZBffcT+Gw2Nz2sqtcEz+pDlPVLlpK3AO0OCOSwOfQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id jx6-20020a0562142b0600b0068198aa2c65si408700qvb.91.2024.02.05.09.25.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 09:25:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-53097-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Y5njpc12; 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-53097-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53097-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8E8321C21875 for ; Mon, 5 Feb 2024 17:25:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 852B644C65; Mon, 5 Feb 2024 17:25:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Y5njpc12" Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (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 D5B9B44C60; Mon, 5 Feb 2024 17:25:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707153909; cv=none; b=dfEfcg1pzC0dUETlOovXKBSDIDsi9vMLtVOiVDXR0veS04kq16xHNgY/dGOO/fs9wyBVCuqHIzOhMTiMEvJ0H9eURB9F+rusMIPu0No4iQFHbEuCB1gxVWx1FV3XYGtV6BlHtnPFrCMqm2UAzEBn2dM/br9FlOSKn6Jf3U6gSSw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707153909; c=relaxed/simple; bh=d19VFgDS03vvtjMyufp38CqLNpLc+fMbtV3FQXMkhhc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ahYNnLflOEXgbGYPVe80xxqGvQcd5+8S/Dj919wKec2sVCLfkAsUtysf1OIteNVKL71ozWUd0N81OVy7GrcuzxwP3f5yujDhG267yC/8//AopTb3Wa/wST78n6jRVGIiBEEVfs976PUgdTxYamop1wSc3C1W93BTx4fs1siBI84= 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=Y5njpc12; arc=none smtp.client-ip=209.85.210.44 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-ot1-f44.google.com with SMTP id 46e09a7af769-6e12b917df2so2918475a34.1; Mon, 05 Feb 2024 09:25:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707153907; x=1707758707; darn=vger.kernel.org; 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=K+cat8wRiFE8ayUEL9AAC8rZ9xDFXFmCN5IZ9qalElU=; b=Y5njpc125ADGWk6778kSHCR7Iiiev2bpgP3hPOQ8NuAaaBqJYeoFxtL8qGh6rEv39o TUSgudoeRM4Xrbx39/2mzyIrzckXN+m23G6hThXhA/O1G8G6QrJK8oyyuq3TB7+cGI7Y i2mnwTKwjEejVhAAaSkBQGErruzbKgCuU6N8ycXBrAiPLVVKNz3GktHq5FGGbKJ9ii0L d1MRikkiy40vAV9TIk2DZ54uueIIgxtc0l6sKVPAbBIx8p+N16Z6OMROGa2RnYr34dp9 DJdyvzjkKFnOEsBe5iOmDxihOnGWMvw94/UWOrwuEdsxcbDd/rVhTBKdplbDADQyqOG+ iQxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707153907; x=1707758707; 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=K+cat8wRiFE8ayUEL9AAC8rZ9xDFXFmCN5IZ9qalElU=; b=n0yulka1CnHDWe/xbxPBKFrHngqX0zgFM7bVRLIbIYOf3Th4BMe7wKQMWGYKWKKqa9 uRIL/MOdG1f4RAp5Ic9pIyPsYhTtGCd0zcdRYTmEX9ZMmkdktYwdakMXeMOzcVHVvwtN oUMBpiy9r+iqaUfeWDgZJbzr6Ju7ECu48u2IBwCvF++J4lJNraUJfBZNl/T4FqRs1ych +FBEMbKy0coiWF/6e8lMqC33ddNKxLgYgynTaxZ2pgGbb3SWUic4XvVw2OFjCHlAsR+C ERIraaoGycYXywXw2N5aaY136nyKpPrUTljLgPhE73eekWdofwjZRJXNhUTSJThcroTK 1mBw== X-Gm-Message-State: AOJu0YxSr3V/ViWrGSXbGwrK592GphedEV+x37bwZHc0/zesbQOxZTcd db7vjROcV0OK3MCFHcw5x9ARfc2sjfF6JmuqwB+IcGWXKqpnaqFL X-Received: by 2002:a05:6870:5692:b0:218:7643:26cf with SMTP id p18-20020a056870569200b00218764326cfmr397934oao.10.1707153906935; Mon, 05 Feb 2024 09:25:06 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVd1jCv1k+pmp2tP5dozwRgkWN3WSpCTmwZy8ZuZKKbPdDZGaiixHmFM8QfDNg1rmiefqgWfz4BFCBsd860M3PuUGMPZuZCRnzLZK0Lxq5YaSLZgIaCWUgciYZKI2W2G9yzbN19+wSwgnP/JYIdiqg3iHKGHOqJxja8v51QeOGU/9G7c3OwNsyxWCNpRNflOcqGCxSkJ+j77XDS7nqP+mGwclNRSkrdBAip300lOFg+04o69Aqy0ARALtyPMpzmh868LoL1AXfoEf5Mt6N/cEOJkIRVtgeANq/uEyjmJhGktrPqxHZvLLWd0gKFcLIpJKo8dn+W1CkIYxSrKmQpzx/KtZakZv8eq32RJlgZ46l8WugKBVG/BQTTh9vFCr8/bzvYllz4dLnk4W2H6uk0eNt2XGTrE3xMwduQbKrs/7hqazvE2f7UkcJ+g7igGxgRY6h5VylwtSQUDzZLkPInScxNk8dyawI81fCHpeZG9c9Mb4lYmWByhyRygmDf/5zTE/UtTr17BPwXLMeOeJG9VEvhV6J2Ge0rA28+5d8V/hHiMDiFAxSwp7yqmvaHuRpY4mkrenkuiHLllSKNgNotSh57+oMHUxVmkMC44D2lEl8o4gaVLOXOpnidKnZSM7+Pn4PSLVqvuoKLdUXJnBu39LcJ58e5epqfqTtyrL1H5cN5CTc2nzT6B7ZUoNKmvM5od0wEsF8ftbiEby98/1lUQdkC9LN9BZE+nOYg+GoU Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id b16-20020ad45190000000b0068ca40389besm162134qvp.101.2024.02.05.09.25.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 09:25:06 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id 9FAA527C0082; Mon, 5 Feb 2024 12:25:05 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 05 Feb 2024 12:25:05 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrfedvuddgtdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggugfgjsehtkeertddttdejnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepvefghfeuveekudetgfevudeuudejfeeltdfhgfehgeekkeeigfdukefh gfegleefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 5 Feb 2024 12:25:04 -0500 (EST) Date: Mon, 5 Feb 2024 09:23:46 -0800 From: Boqun Feng To: Trevor Gross Cc: Alice Ryhl , Andreas Hindborg , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , 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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Thu, Feb 01, 2024 at 01:50:53AM -0500, Trevor Gross wrote: > On Fri, Jan 26, 2024 at 1:28 PM Boqun Feng wrote: > > > > On Fri, Jan 26, 2024 at 01:33:46PM +0100, Alice Ryhl wrote: > > > On Fri, Jan 26, 2024 at 1:47 AM Boqun Feng wrote: > > > > > > > > On Wed, Jan 24, 2024 at 11:20:23AM +0000, Alice Ryhl wrote: > > > > > [...] > > > > > + /// 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? > > > > > > Ideally, we don't want data races with these methods to be UB. They > > > > I understand that, but in the current code, you can write: > > > > CPU 0 CPU 1 > > ===== ===== > > > > page.write(src1, 0, 8); > > page.write(src2, 0, 8); > > > > and it's a data race at kernel end. So my question is more how we can > > prevent the UB ;-) > > Hm. Would the following work? > > // Change existing functions to work with references, meaning they need an > // exclusive &mut self > pub fn with_page_mapped( > &mut self, > f: impl FnOnce(&mut [u8; PAGE_SIZE]) -> T > ) -> T > > pub fn with_pointer_into_page( > &mut self, > off: usize, > len: usize, > f: impl FnOnce(&mut [u8]) -> Result, > ) -> Result > > // writing methods now take &mut self > pub fn write(&mut self ...) > pub fn fill_zero(&mut self ...) > pub fn copy_into_page(&mut self ...) > > // Add two new functions that take &self, but return shared access > pub fn with_page_mapped_raw( > &self, > f: impl FnOnce(&UnsafeCell<[u8; PAGE_SIZE]>) -> T > ) -> T > > pub fn with_pointer_into_page_raw( > &self, > off: usize, > len: usize, > f: impl FnOnce(&[UnsafeCell]) -> Result, > ) -> Result > > This would mean that anyone who can obey rust's mutability rules can > use a page without any safety or race conditions to worry about, much > better for usability. > > But if you do need to allow the data to be shared and racy, such as > the userspace example, the `_raw` methods allow for that and you can > `.get()` a `*mut u8` from the UnsafeCell. This moves the interior > mutability only to the mapped data rather than the Page itself, which > I think is more accurate anyway. > Looks good to me ;-) Thanks! Regards, Boqun > Leveraging UnsafeCell would also make some things with UserSlicePtr > more clear too. > > - Trevor > > > Regards, > > Boqun > > > > > could be mapped into the address space of a userspace process. > > > > > > Alice > > >