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 64CCCC3DA49 for ; Tue, 30 Jul 2024 10:58:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF8B26B007B; Tue, 30 Jul 2024 06:57:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA8BC6B0083; Tue, 30 Jul 2024 06:57:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96FB36B0085; Tue, 30 Jul 2024 06:57:59 -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 797FB6B007B for ; Tue, 30 Jul 2024 06:57:59 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2286B140235 for ; Tue, 30 Jul 2024 10:57:59 +0000 (UTC) X-FDA: 82396119078.06.795F1AB Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by imf10.hostedemail.com (Postfix) with ESMTP id 46481C0006 for ; Tue, 30 Jul 2024 10:57:57 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tjO0VQWn; spf=pass (imf10.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.53 as permitted sender) smtp.mailfrom=aliceryhl@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722337049; a=rsa-sha256; cv=none; b=1LvJjDwqlJWIiDKSkN4yRonGLYAdmZlPWq1LIMSOXp/7XA+FGHrqIyMJrhSe9tJUVnQKbV e1d9eH5jBWtkejoZRe8t+E2WBQLzpB0Q/NHBk4C8JPz3IC8GZKTlliwSWSZjekkU7cPQvf 1K0wJWPmK1roZkvrfQdMjyX4nMC+9EM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tjO0VQWn; spf=pass (imf10.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.53 as permitted sender) smtp.mailfrom=aliceryhl@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722337049; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Byx/+NsYax4XTwzR5o0dMoTnWWs//cW6U9789awaLpM=; b=fheqLAV4OwWcFfBJHprNqZt4PaRrtXSNF0exVBfQrdZzLCFiHEHtTLOQu5UMb/8aPvVLEV c7djXtqIGGQtua3R5wllbPFruMkmsRKjvtJ8zB5cUU9EFJQd3G25NZPjcV5NMgSEJutrFa 18/8zfJwAKMwDiV39D0wgZlKcfUzlkY= Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-368440b073bso1795579f8f.0 for ; Tue, 30 Jul 2024 03:57:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722337076; x=1722941876; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Byx/+NsYax4XTwzR5o0dMoTnWWs//cW6U9789awaLpM=; b=tjO0VQWnFgV3Y3+FplFa9TYIYsBt3waJAI3Vmm6Op1OtKMkCWRJ0bWSquueZPHaj7e aGbxCo+rn+YiLbF9EAAMCCxH3BINceoWVsohsDwQd9p9QH6FtVRXwFRK21EJlh4oEBMX dbhlQ/f1mGls1qrp1FuwXUEZU4zWejCAQQoTkmpP/TuEfatoGmj5rlMaWyJ0pkKTB71+ ENrGe+t4ipSpuONTKGo116rTcEnFetFjZm2+5zJtcI+hRxXp5dvmIR7RfOOB/LGUhIoV gp72bjBcmoGF6wQ+H93XhmLOSYGfnjtbT57aVBTellr5FDo+IYezczrz2Qu4cgrBjESo 9EKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722337076; x=1722941876; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Byx/+NsYax4XTwzR5o0dMoTnWWs//cW6U9789awaLpM=; b=Hz/AhkuBfyQz1W3jzS7ECQ+2r5cmkCmagSyKxzeuw+LQugMnISpiadx1N5Eo45FHuW Y1Plf+WqywX//j1TbtBy24ma+i3Z6N0/c55T0toImnS4U60+Tx/UcORP5q9apjsJD6Pv r0lJC0gZUN8Nf9wd5+92TziInkgLPFrQovle9n+9v6ktb67BjO1nwCm1RO6904Ym7pVI n2NbxTCAl1FxRGk0bZqbnVVzUgQu+YyWSnkYhangoP7c3cbpyRsxUG5YXDDLc913g9Wa DiH5idMCBn23SLPmUrekgbi/ovX7kzaW0+5bgkDni4pKJE361t8+s2K5UHc3DoRHHhzB RtlQ== X-Forwarded-Encrypted: i=1; AJvYcCU1ZlDbjAl8Cd8xlTAzrOtmAUMHXDVatmMjpqt4+497wGtf4enDcILr3H9sqjfc04LJz6eSTSrTVi0RsTI+CSZGrn8= X-Gm-Message-State: AOJu0YxIOQ2cCWc+syT/ygak7cQJ2L2HzUzH7nkMLOUTSItbqt+getcd QhVXq75yEAYULNUStpJvcgaK+wbm/eXGrKTaUMKGiC49LGhBqkXn//h3jjLDsLEkprmoBIR/94Z c25WMrybOXKWSWeEGXMoCHAfbljMjz0Bj62La X-Google-Smtp-Source: AGHT+IHBKvdpwpeX66stBMzQBehm8htfa32lEV9TetSlhZ2IXJcx0jHxCW463ZOp5gjW96fEFOWtq7k3XkupvLgGB/k= X-Received: by 2002:a5d:46c9:0:b0:368:374d:81ec with SMTP id ffacd0b85a97d-36b8c8df3cemr1018349f8f.10.1722337075385; Tue, 30 Jul 2024 03:57:55 -0700 (PDT) MIME-Version: 1.0 References: <20240727-vma-v2-1-ab3e5927dc3a@google.com> <3ffd4742-7a84-434d-ad0d-962f302b977a@proton.me> In-Reply-To: <3ffd4742-7a84-434d-ad0d-962f302b977a@proton.me> From: Alice Ryhl Date: Tue, 30 Jul 2024 12:57:43 +0200 Message-ID: Subject: Re: [PATCH v2] rust: mm: add abstractions for mm_struct and vm_area_struct To: Benno Lossin Cc: Miguel Ojeda , Andrew Morton , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: rjgjqwgjqwy8u93zhauc614zfidghbms X-Rspamd-Queue-Id: 46481C0006 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722337077-239884 X-HE-Meta: U2FsdGVkX19rlDjaxjcnQAVN5a8d2Dj6nqsWOBFwIGmPtFtxwW4QZ9n73smGYu5mk00A/DCpFO6UYTlrpUFCfhY/ADxG+sjhsYNr5uK18CKNiCS0o069fIYao8Bjqi656rG41yJa/11dPnVgXM2/9ErGfCckJdXopmjJvb3gmxjstjVnJ87oGAFLpz8Laqu9eVR+leRs0SpEt1/kQBX2p/jtwnQ78KrORg8+E3DCLRRfEok9bczSs+rAeLfByB2d1qiSpXAHTICb0y6AY2lJBxPhjFtI5fAr/t3F6ykQvhGhNRvJIPOwz7ZQrGZuffHJWvFA0lVZ0MxHbSGeUebQWEk/G17RGHQlGM0aRTvz9t9dJ3WdbDRK1d2/SVkshMarsFQ60fBF9gtrAJLSznsYckB5njIr/8JbUzo47dY5hDycNGuOKcMl1SWN7iVb3BSbyKG5HhrKdowHK0qqIWIh7Sl1n7ozovgse/LBYkjOyq3tExQMak4mOmXeUFkIN+nExm4sVuSzFD5Rouqk7PsHzEWalF05Cmu2H87q7xB60BT1A0b4jqnnokM33/ue09T193qZFhbwe3T8QJMHACcfGKgQMUQOf6yJrBYNUt5M/v69WXKWXSof1wr6U9N6T4Ewb1oDJCeYxzg6QEDqGE8FhNim51xSDn3jZQ2l3sSorxCYPjEddQwWjByAeJtMl5pknDcy+bCssBq30h87uzNGFVneH6MvfCZxWx9835YCb1qFmAUAbZEUIS7RIBbwf4bXet2dv97ly88xCUQbh3rVms6bl4Boh9ociPcXS0WgK7sWrkVLCCTRhMDViRvArIM8wr7cmSEmlqDlzK/EPHAEmHFGrBoGJ0v2iMPvciZORZYR1szkp1o2OmrlUYB5nUgslIxJz9/XB5Hlu7XepMWDWO8gqjZPS233rhf/6kM5WCL7c8Ql1s03VKxPysAtjnlW9KODz6tvp6kmx3oo8SO kkOaKpgC F+m3d5T2rjaW/Y8Ue8j0+h5K+AkZGrmx/tOEelipXTiAfT2svrkDqOqHCnQbYolXbpuvlheUiRUuhmfUTO5SVqkun2cxhKU4f27AyoPlgSxoRxeHuOResOwnEY1kkR108CY13FP3yCutTZ396hWOE8yCPRIW5SZE/ZMmiTMg80lK9qOLrDoJWgtoqY2zYM2I0bbS1raZPpUnQAwb5w1rhHtihexeoTHIj38YY5SMLm6fV4rr7AyrL3WINJRka/dYA4x9AoIIfzqxklxJOjNxIaJVHbZJGzxitjOZI X-Bogosity: Ham, tests=bogofilter, spamicity=0.000888, 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 Mon, Jul 29, 2024 at 6:13=E2=80=AFPM Benno Lossin wrote: > > On 27.07.24 11:03, Alice Ryhl wrote: > > +/// A wrapper for the kernel's `struct mm_struct`. > > +/// > > +/// Since `mm_users` may be zero, the associated address space may not= exist anymore. You must use > > +/// [`mmget_not_zero`] before accessing the address space. > > +/// > > +/// The `ARef` smart pointer holds an `mmgrab` refcount. Its destr= uctor may sleep. > > +/// > > +/// # Invariants > > +/// > > +/// Values of this type are always refcounted. > > +/// > > +/// [`mmget_not_zero`]: Mm::mmget_not_zero > > +pub struct Mm { > > + mm: Opaque, > > +} > > + > > +/// A wrapper for the kernel's `struct mm_struct`. > > +/// > > +/// This type is used only when `mm_users` is known to be non-zero at = compile-time. It can be used > > +/// to access the associated address space. > > +/// > > +/// The `ARef` smart pointer holds an `mmget` refcount. It= s destructor may sleep. > > +/// > > +/// # Invariants > > +/// > > +/// Values of this type are always refcounted. The value of `mm_users`= is non-zero. > > +#[repr(transparent)] > > +pub struct MmWithUser { > > + mm: Mm, > > +} > > I personally wouldn't sort it this way (so struct decls, methods and > then AlwaysRefCounted impl), but I would sort it first by the struct. > I find it helpful to have the `AlwaysRefCounted` impl close to the > struct declaration (similarly for `Drop`). But that might just be me. I can reorder for next version. > > + > > +/// Equivalent to `ARef` but uses `mmput_async` in destruc= tor. > > +/// > > +/// The destructor of this type will never sleep. > > +/// > > +/// # Invariants > > +/// > > +/// `inner` points to a valid `mm_struct` and the `ARefMmWithUserAsync= ` owns an `mmget` refcount. > > +pub struct ARefMmWithUserAsync { > > + inner: NonNull, > > I am confused, why doesn't `mm: MM` work here? I.e. also allow usage of > `ARef`. We could do that, but I don't know how much sense it makes. With Mm and MmWithUser there's a legitimate distinction between them that makes sense regardless of whether it's behind an ARef or &. But with the `mmput_async` case, the distinction only makes sense for ARef pointers, and &MmWithUser and &MmWithUserAsync would be 100% interchangeable. That is to say, this is a property of the pointer, not the pointee. I don't think it makes sense semantically to have it be a wrapper around MmWithUser. > Another approach might be to have the function on `MmWithUser`: > > fn put_async(this: ARef) > > Or do you need it to be done on drop? This needs to happen in drop so that use of the question mark operation doesn't suddenly result in sleep-in-atomic-ctx bugs. > > +} > > + > > +// Make all `Mm` methods available on `MmWithUser`. > > +impl Deref for MmWithUser { > > + type Target =3D Mm; > > + > > + #[inline] > > + fn deref(&self) -> &Mm { > > + &self.mm > > + } > > Does it really make sense to expose every function? E.g. > `mmget_not_zero` would always succeed, right? I don't think it's a problem. Right now it exposes mmget_not_zero, is_same_mm, and as_raw. The only one where it doesn't make much sense is mmget_not_zero, but I don't think it hurts either. > > +} > > + > > +// These methods are safe to call even if `mm_users` is zero. > > [...] > > > diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs > > new file mode 100644 > > index 000000000000..2e97ef1eac58 > > --- /dev/null > > +++ b/rust/kernel/mm/virt.rs > > @@ -0,0 +1,199 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > + > > +// Copyright (C) 2024 Google LLC. > > + > > +//! Virtual memory. > > + > > +use crate::{ > > + bindings, > > + error::{to_result, Result}, > > + page::Page, > > + types::Opaque, > > +}; > > + > > +/// A wrapper for the kernel's `struct vm_area_struct`. > > +/// > > +/// It represents an area of virtual memory. > > +#[repr(transparent)] > > +pub struct VmArea { > > + vma: Opaque, > > +} > > + > > +impl VmArea { > > + /// Access a virtual memory area given a raw pointer. > > + /// > > + /// # Safety > > + /// > > + /// Callers must ensure that `vma` is valid for the duration of 'a= , with shared access. The > > + /// caller must ensure that using the pointer for immutable operat= ions is okay. > > Nothing here states that the pointee is not allowed to be changed, > unless you mean that by "shared access" which would not match my > definition. How about this? Callers must ensure that: * `vma` is valid for the duration of 'a. * the caller holds the mmap read lock for 'a. And `from_raw_vma_mut` would instead require the caller to hold the mmap write lock. > > + #[inline] > > + pub unsafe fn from_raw_vma<'a>(vma: *const bindings::vm_area_struc= t) -> &'a Self { > > + // SAFETY: The caller ensures that the pointer is valid. > > + unsafe { &*vma.cast() } > > + } > > + > > + /// Access a virtual memory area given a raw pointer. > > + /// > > + /// # Safety > > + /// > > + /// Callers must ensure that `vma` is valid for the duration of 'a= , with exclusive access. The > > + /// caller must ensure that using the pointer for immutable and mu= table operations is okay. > > + #[inline] > > + pub unsafe fn from_raw_vma_mut<'a>(vma: *mut bindings::vm_area_str= uct) -> &'a mut Self { > > + // SAFETY: The caller ensures that the pointer is valid. > > + unsafe { &mut *vma.cast() } > > + } > > + > > + /// Returns a raw pointer to this area. > > + #[inline] > > + pub fn as_ptr(&self) -> *mut bindings::vm_area_struct { > > + self.vma.get() > > + } > > + > > + /// Returns the flags associated with the virtual memory area. > > + /// > > + /// The possible flags are a combination of the constants in [`fla= gs`]. > > + #[inline] > > + pub fn flags(&self) -> usize { > > + // SAFETY: The pointer is valid since self is a reference. The= field is valid for reading > > + // given a shared reference. > > Why is the field not changed from the C side? Is this part readonly? Because we hold the mmap read lock. (or the write lock) Alice