From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B741AC4345F for ; Thu, 18 Apr 2024 22:56:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EBF66B007B; Thu, 18 Apr 2024 18:56:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 09CD76B0083; Thu, 18 Apr 2024 18:56:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E58496B0092; Thu, 18 Apr 2024 18:56:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C85D06B007B for ; Thu, 18 Apr 2024 18:56:42 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D2089140174 for ; Thu, 18 Apr 2024 22:56:41 +0000 (UTC) X-FDA: 82024163802.24.03AB7B0 Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) by imf15.hostedemail.com (Postfix) with ESMTP id A9844A0003 for ; Thu, 18 Apr 2024 22:56:39 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OWAb1SLS; spf=pass (imf15.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.219.50 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713480999; a=rsa-sha256; cv=none; b=ubctCmpvQUaSsKzPFFuNXQdM5f6v47UU5uikIx8C1edrSkFCS6TF8jBGNnOWNauOSPM7MD VeiYAoA1tevGS2MAjxCa9YHaI1z7SFkXT5a1JvBqYF3VyGfbR2r3y6qe26o8QANWfFa+Uw U2tOQuMuKeYIacV4hRg1kDUtsOuKkkk= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OWAb1SLS; spf=pass (imf15.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.219.50 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713480999; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=a1ppV7Fh/1Iy/7RRfewnFGGAltFLGrqr5zJ2zdSeDXI=; b=KJAOhJlaSvqtP6P0GviUpFueNfp9wPOPuXV0+M3/QOcWBjNLnUXKSNURxVAAe5g7S1pMbF KF2Pv2YAMXC70vcGPBwhSuI9aJRQAKjQqBx9wg7zCE/IRsx91HELipc/ws1vUeHewvBBSh LIWHbg+STs92s420YJsZO/KNxcEfGuU= Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-6a05f376effso1682096d6.0 for ; Thu, 18 Apr 2024 15:56:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713480999; x=1714085799; darn=kvack.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=a1ppV7Fh/1Iy/7RRfewnFGGAltFLGrqr5zJ2zdSeDXI=; b=OWAb1SLS091pNogQRAt5eR78PhDM5El+53LkDRYl0LC5cKeBUUmmNmbeIllfwoDD64 V2Xj7WNDdcvkch8zMvBmR/fEbW3I8RC4DGx6H+QFNqBmXKACxaKnepDvoBq428wRq/GK q3Q7GQYRzET6Po8C1ThTio9uRWJmLF1P8GHA8iDemQ+TfDVPhLUUcS1y6lWlAbn+HgIP D1hDtefMd9Fwk02Qkbjg1I4chUQgpQAFjbKPbo8mC9euIb7/SN13YyDqro3drbDu2oXX owNMn/oA7xBBezxC38PQZglo4rel0g2pjKR6EJjXLXWV8rbACXkq42a0kGbBLoy9pqpJ 3woQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713480999; x=1714085799; 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=a1ppV7Fh/1Iy/7RRfewnFGGAltFLGrqr5zJ2zdSeDXI=; b=UsISx9L4Y++6Yj8WC8VV/JR032L3TJyHzFAIdAzkGtGd2motE/WpT4MwQZBbglemNe kEX1sI+vKUmNKY+yq4uXcG4EKn6QnkRzkLNw88+In/jTLJ1DIl8gl7hYXJY5NH3e+5vc YE9ZhhCOnzDOM6ppxGQMnhsH1I5c8kkNIic+RdymTTtKO9lSuSd79To2j7RkRi52jemW 2fgme6WhyCNAKWLGdO2a3w2WZRadNlgqYZS3+xVnBi7N6k1FI1WjrWePtLdSy+z4xY1G 0HESeDrumAAAXaUoGOxXEFZZziF06DhS+AXsAnJ/1cerPHqLP9Wg+hjwjqYlbCK89TQc 18Mw== X-Forwarded-Encrypted: i=1; AJvYcCWteDWM2KkDnLd83PrvRvrH/crG1h0ZADf7v6mLJWokePe59mIOyuoUD7xoKQT0Uitb/302qi4xVGtM+JgoP+Q3hcE= X-Gm-Message-State: AOJu0YzTzgMBQuJByHrPS9jTVWubtIfBL0M+3yPQaz5dryH/ncAOkCT0 sVQRGC722WNjegxl8RMbPPbaGs3ecUmomj6KVCU2urSjIubY1D7s X-Google-Smtp-Source: AGHT+IHJM9HcyMUvnziIjo9BnAimxWSPyNQZw63kKsHcZeY+F0D8c+NSU9CBu/ztB4ikC/Hh0gEFkA== X-Received: by 2002:a05:6214:a52:b0:699:1f58:398c with SMTP id ee18-20020a0562140a5200b006991f58398cmr447097qvb.5.1713480998697; Thu, 18 Apr 2024 15:56:38 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id a13-20020a0ce38d000000b0069b23dd33besm1042193qvl.126.2024.04.18.15.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 15:56:38 -0700 (PDT) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfauth.nyi.internal (Postfix) with ESMTP id 2BC041200032; Thu, 18 Apr 2024 18:56:37 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 18 Apr 2024 18:56:37 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudekuddgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne goufhprghmrfhhohhnvgdqohhuthculdehtddtmdenucfjughrpeffhffvvefukfhfgggt uggjsehttdertddttddvnecuhfhrohhmpeeuohhquhhnucfhvghnghcuoegsohhquhhnrd hfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvghrnhephedugfduffffteeu tddvheeuveelvdfhleelieevtdeguefhgeeuveeiudffiedvnecuufhprghmrfhhohhnvg epudektdegvdegvddthedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthi dqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghi lhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 18 Apr 2024 18:56:36 -0400 (EDT) Date: Thu, 18 Apr 2024 15:56:11 -0700 From: Boqun Feng To: Benno Lossin Cc: Alice Ryhl , Miguel Ojeda , Matthew Wilcox , Al Viro , Andrew Morton , Kees Cook , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Andreas Hindborg , Greg Kroah-Hartman , Arve =?iso-8859-1?B?SGr4bm5lduVn?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Arnd Bergmann , Trevor Gross , linux-mm@kvack.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Christian Brauner Subject: Re: [PATCH v6 4/4] rust: add abstraction for `struct page` Message-ID: References: <20240418-alice-mm-v6-0-cb8f3e5d688f@google.com> <20240418-alice-mm-v6-4-cb8f3e5d688f@google.com> <87dc4cdf-ccf6-4b08-8915-313aad313f93@proton.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87dc4cdf-ccf6-4b08-8915-313aad313f93@proton.me> X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A9844A0003 X-Stat-Signature: n8ye3rwk699nw84u4smad8m4hpuo9ouu X-Rspam-User: X-HE-Tag: 1713480999-999363 X-HE-Meta: U2FsdGVkX19EurIglxAzdUCj2vaETkRpFB8nYuxU9GnUSk3c25rifwBMQRSDYDdNr9c3agd7YjBHT6U4HrmqnmLPM67/yacEdCVO+fbvLsrmf1F2XRrTBCDxnGKrz5CI2ask0sH8iO/RPnfmOWM1Qs93XH3Z6SPdqlQX68We0W2tu3/JrchEdUgV/9KPprGFjy2wCG3qfV342zYtG3veTSE4lF5xYK287vO1J+hKkk69RK7YrCJC0CgoWtWP3m2tk3iZqW7f3NCSblHs9xe1ktZKC/y0y05gb7eJK+PsNz6H1QcxUb0ovZPAOOe1hPz+jrAqEyj1tf6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1++K7lLtg7CrlbwyJIsbnPMgJ4x+g+OM/EHOvU3Mchjw7VuEkWemHUuOdG4dhHa3Wl6lxDDnz+omNtE3WL/O6JNNTwl1yXTr8tjanu7lxGYqTrbLanNkM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Apr 18, 2024 at 10:08:40PM +0000, Benno Lossin wrote: > On 18.04.24 20:52, Boqun Feng wrote: > > On Thu, Apr 18, 2024 at 08:59:20AM +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`. > >> + /// > >> + /// # Using the raw pointer > >> + /// > >> + /// It is up to the caller to use the provided raw pointer correctly. The pointer is valid for > >> + /// `len` bytes and for the duration in which the closure is called. The pointer might only be > >> + /// mapped on the current thread, and when that is the case, dereferencing it on other threads > >> + /// is UB. Other than that, the usual rules for dereferencing a raw pointer apply: don't cause > >> + /// data races, the memory may be uninitialized, and so on. > >> + /// > >> + /// If multiple threads map the same page at the same time, then they may reference with > >> + /// different addresses. However, even if the addresses are different, the underlying memory is > >> + /// still the same for these purposes (e.g., it's still a data race if they both write to the > >> + /// same underlying byte at the same time). > >> + fn with_pointer_into_page( > >> + &self, > >> + off: usize, > >> + len: usize, > >> + f: impl FnOnce(*mut u8) -> Result, > > > > I wonder whether the way to go here is making this function signature: > > > > fn with_slice_in_page ( > > &self, > > off: usize, > > len: usize, > > f: iml FnOnce(&UnsafeCell<[u8]>) -> Result > > ) -> Result > > > > , because in this way, it makes a bit more clear that what memory that > > `f` can access, in other words, the users are less likely to use the > > pointer in a wrong way. > > > > But that depends on whether `&UnsafeCell<[u8]>` is the correct > > abstraction and the ecosystem around it: for example, I feel like these > > two functions: > > > > fn len(slice: &UnsafeCell<[u8]>) -> usize > > fn as_ptr(slice: &UnsafeCell<[u8]>) -> *mut u8 > > > > should be trivially safe, but I might be wrong. Again this is just for > > future discussion. > > I think the "better" type would be `&[UnsafeCell]`. Since there you > can always access the length. > Hmm.. here is the thing, having `&UnsafeCell<[u8]>` means having a `*mut [u8]>`, and it should always be safe to get a "length" of `*mut [u8]`, right? I haven't found any method doing that, but the length should be just a part of fat pointer, so I think getting that is a defined behavior. But maybe I'm missing something. > Another question would be if page allows for uninitialized bits, in that > case, we would need `&[Opaque]`. > Yes, or `&Opaque<[u8>]`. Regards, Boqun > But I don't remember how to get a valid raw pointer from > `&[UnsafeCell]`. > > -- > Cheers, > Benno >