From nobody Mon Dec 15 21:47:19 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 A41CD28EC73 for ; Wed, 15 Jan 2025 13:36:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736948183; cv=none; b=lDyf+f87kCiQoSVf/jVlsJTW2V3YHNwdShUxw5Wk4bwdAWhcYtoetzpAL+l9kaGY8yyT40vXRwaMDcFtNgg/TjvJLdiwcprTnCHFDBMBMynFUKSVw7HVurt2ugTIdSVSPO/1gPI4N5D0soTFd416dhRdvA+HcVZTECCtoODNGLI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736948183; c=relaxed/simple; bh=YABlKOR4s3jcHAdrd+4knVVhbkpmpWt8xx350u7Y5JI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YZ0wjIdq7G15QUYxHRaw9ACIyDg3drJomgLBo4yw0oMfWxlZBw4/cZj5fmDvo2uhJjuESydjfEsBwPM2fg7PCfyDfuyA8PFHJFxU6FGKHIcxvuIlzRuoOgKuAz4zclyb4V11b/YH5Gd8sfd9t00CgYkW6AlqJBrMsLH+iXl9y6A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4dIIkpTj; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4dIIkpTj" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43631d8d9c7so4407975e9.1 for ; Wed, 15 Jan 2025 05:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736948180; x=1737552980; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=81ZLKD3ogzg79yEJn00+1Q3wr2cvrl13TETNOCjT2u4=; b=4dIIkpTjJGO6LYcPZjpxsUmCfvxeqfS5x9BnJ3PTkDkpktvuMijnlCgWyLta44O1SA bWXfDKi1Sz+Mae01aT9NXKNqExObxyb4ngE8oKkDsHsb78Zyw0dfxaXLyIDXGRxNFHUA faCnO/VbrhpgE6JOPNbvPi9TQmp27dazl47Eqc9ldNCOibVKbCuExnoJjH9a24BUcW4m lWIySeUgG0OuaSt2SNSP8MsKs9pxffWGZlGTG7az1PkWhF9Q9LWrxYlBbD39RyVyAdkh 6BdMmvxUwyu16UjXnDsiHJEg37yIPV3eIZLvaB0anhG8KSfjR+PDRTBD0xJAPWWxsY1q asTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736948180; x=1737552980; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=81ZLKD3ogzg79yEJn00+1Q3wr2cvrl13TETNOCjT2u4=; b=Ug4Qs5vlg32s2UPYQK7pB7m8idKSS/3jK1QqQtcamyNYGoOyXYKajtoQqglfzY5W6d mOtGPCeVEkjEw8J1iOsJeMEn6N+vSCA0ETtJ58kRDpBwRM/VBibSQtnIBXNzhXKoF2Mn pJn6foHGbotawaYz+9lEdclcb45MHO/pNv4+b4Z57YIszQM9omKF0+xrwh9ILnMWHdXO GGmGndBNZe8hqjrV5VoAgN4TkCLVkh6+qyUI3AWw4w8dBfjem8MHdMh+h9rJBURAltKL Jaqh3N4vrJI7NvGxYoURE4jIPG0u4Em6l+cAwlregqpgzlUo8C6bc+zy0VQamAliNjaD /pZA== X-Forwarded-Encrypted: i=1; AJvYcCUDeksGCh9OlbGxcyUg0gE6hwc7OlUV0Oyzi20c3UhCqLoEZN0s6szs6BUHrlGQrDxK1t7mDHQre/RFtF4=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0sTBXtzi4kxT33CTEEa2pi0CO8rAjg1xtw5iPNSGUQBQA6jhN FMosXit1FNQWYn6FghmIGlINreFPIddj7ZZUETZbz9VMacFrM+CDqDwisAuuOu1engvCEAic+ss yN+B3IWsjVY39+Q== X-Google-Smtp-Source: AGHT+IHgYgEKSPDdi2P+oyelNydwykWpCSvtY+CbLbji5X9hWCVjK6MNYOAiA3I1gSkw4AjTEKFL2AALGCdOpWg= X-Received: from wmpz18.prod.google.com ([2002:a05:600c:a12:b0:434:f299:5633]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1e29:b0:434:e892:1033 with SMTP id 5b1f17b1804b1-437c6b7b184mr26161955e9.2.1736948180099; Wed, 15 Jan 2025 05:36:20 -0800 (PST) Date: Wed, 15 Jan 2025 13:35:10 +0000 In-Reply-To: <20250115-vma-v12-0-375099ae017a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250115-vma-v12-0-375099ae017a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3771; i=aliceryhl@google.com; h=from:subject:message-id; bh=YABlKOR4s3jcHAdrd+4knVVhbkpmpWt8xx350u7Y5JI=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnh7nAleiDJcaQtJuPa+I8hS1lU1tl1JfuAuv6C fM9qXyv8SiJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ4e5wAAKCRAEWL7uWMY5 RjKWD/963spEQDrIssZkZ3POnDl7uN2xDsLrbFE7PWw0knySvUIAaNYgn/o5eNYUfpxiQLaCKkD CXGxsKIuf60CrxosESAo+b4ZUrA3IUYF+nX7NShS9KEgo62sv6MnBwZclUyx1Gj55nj3aNveugE fy0OodhenOJTHWSU7N4gbqJlMKsNFxFMoLCMphn2Mle3wqHMGd4jGi9IRXspxNU3pWa6ccMfYo3 LqkkBCs65pX3GBKgCt9OByOiUNBtC84SEfpNjTar6ZqPx1wR+7nJO6U8hxO+jc1KdErXJ1KwxZS mSQXRQDKLpBln8f9ZazAF73LtchsKtETNLrTD18PNCkfZkh5wxnb7cHNkK8CC9ULX6quxyPzmCH QO0rDKnCD2fPfoyJWdf1xnjS4Z4PKm+TesBB3ihfWES2VoSsoD34cnbq6uE/opRmhvNYUhQvfAu CV1nQnnDyugFYDTO4gGC3FmJ8dWJNPy2DYEtQSMhbyHFqI/pGeTc2+CLwWlrsBob7NPgw+KPJR1 vwl2jg1bbS0MmLM0UKpUFO+RYxBg6HaNBwdr0R5RLmkmuJkqWdEk3wOajCyuMxCP1IWmmLymZya qtAJCN8TF+CkyYnG99EOWqEe/MlfaHTX38XNA0sreMyoYwYAsBgayLu4dFvPIME3w8Cko/T6+o8 +ejB8bK+Cslj+dw== X-Mailer: b4 0.13.0 Message-ID: <20250115-vma-v12-7-375099ae017a@google.com> Subject: [PATCH v12 7/8] rust: miscdevice: add mmap support From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add the ability to write a file_operations->mmap hook in Rust when using the miscdevice abstraction. The `vma` argument to the `mmap` hook uses the `VmAreaNew` type from the previous commit; this type provides the correct set of operations for a file_operations->mmap hook. Acked-by: Lorenzo Stoakes (for mm bits) Signed-off-by: Alice Ryhl --- rust/kernel/miscdevice.rs | 45 +++++++++++++++++++++++++++++++++++++++++++= ++ 1 file changed, 45 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 7e2a79b3ae26..0a0ef172304c 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -11,6 +11,8 @@ use crate::{ bindings, error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, + fs::File, + mm::virt::VmAreaNew, prelude::*, str::CStr, types::{ForeignOwnable, Opaque}, @@ -110,6 +112,22 @@ fn release(device: Self::Ptr) { drop(device); } =20 + /// Handle for mmap. + /// + /// This function is invoked when a user space process invokes the `mm= ap` system call on + /// `file`. The function is a callback that is part of the VMA initial= izer. The kernel will do + /// initial setup of the VMA before calling this function. The functio= n can then interact with + /// the VMA initialization by calling methods of `vma`. If the functio= n does not return an + /// error, the kernel will complete initialization of the VMA accordin= g to the properties of + /// `vma`. + fn mmap( + _device: ::Borrowed<'_>, + _file: &File, + _vma: &VmAreaNew, + ) -> Result { + kernel::build_error!(VTABLE_DEFAULT_ERROR) + } + /// Handler for ioctls. /// /// The `cmd` argument is usually manipulated using the utilties in [`= kernel::ioctl`]. @@ -156,6 +174,7 @@ impl VtableHelper { const VTABLE: bindings::file_operations =3D bindings::file_operati= ons { open: Some(fops_open::), release: Some(fops_release::), + mmap: maybe_fn(T::HAS_MMAP, fops_mmap::), unlocked_ioctl: maybe_fn(T::HAS_IOCTL, fops_ioctl::), #[cfg(CONFIG_COMPAT)] compat_ioctl: if T::HAS_COMPAT_IOCTL { @@ -216,6 +235,32 @@ impl VtableHelper { 0 } =20 +/// # Safety +/// +/// `file` must be a valid file that is associated with a `MiscDeviceRegis= tration`. +/// `vma` must be a vma that is currently being mmap'ed with this file. +unsafe extern "C" fn fops_mmap( + file: *mut bindings::file, + vma: *mut bindings::vm_area_struct, +) -> c_int { + // SAFETY: The mmap call of a file can access the private data. + let private =3D unsafe { (*file).private_data }; + // SAFETY: This is a Rust Miscdevice, so we call `into_foreign` in `op= en` and `from_foreign` in + // `release`, and `fops_mmap` is guaranteed to be called between those= two operations. + let device =3D unsafe { ::borrow(private) }; + // SAFETY: The caller provides a vma that is undergoing initial VMA se= tup. + let area =3D unsafe { VmAreaNew::from_raw(vma) }; + // SAFETY: + // * The file is valid for the duration of this call. + // * There is no active fdget_pos region on the file on this thread. + let file =3D unsafe { File::from_raw_file(file) }; + + match T::mmap(device, file, area) { + Ok(()) =3D> 0, + Err(err) =3D> err.to_errno() as c_int, + } +} + /// # Safety /// /// `file` must be a valid file that is associated with a `MiscDeviceRegis= tration`. --=20 2.48.0.rc2.279.g1de40edade-goog