From patchwork Wed Jan 15 13:35:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13940450 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 C3F89C02180 for ; Wed, 15 Jan 2025 13:36:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA128280003; Wed, 15 Jan 2025 08:36:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D2714280001; Wed, 15 Jan 2025 08:36:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B52B2280003; Wed, 15 Jan 2025 08:36:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8B7FA280001 for ; Wed, 15 Jan 2025 08:36:23 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 515AE812C4 for ; Wed, 15 Jan 2025 13:36:23 +0000 (UTC) X-FDA: 83009785446.13.11F497D Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf17.hostedemail.com (Postfix) with ESMTP id 5B05E40014 for ; Wed, 15 Jan 2025 13:36:21 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wAV5iyoq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 31LmHZwkKCKkJURLNahQUPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=31LmHZwkKCKkJURLNahQUPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736948181; 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=81ZLKD3ogzg79yEJn00+1Q3wr2cvrl13TETNOCjT2u4=; b=FVtRd95GykkDhjlTsGbolARX9ww8fM2IufBx4fY3OUsEMg/KvMaxZ9nqZjtRy3FoFCMuFu gIE0klMYtdjCEV5REEvtAJulvc7FTgA0BbbjKL4fRTrs/dXBV3OihQSDju+p+7VM0Bfqus QQYhkI1ZQ/32ooss0GvdZY9SNtGCwOY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736948181; a=rsa-sha256; cv=none; b=X8msz0C4I9L3eoIBd/aspIOsb8kZm2/ePFcEznf0h+a30iEd3eT9IUKr4C/3EgT0ekVbSi XVvx1W6xIxZWDYmoMkmqmUksRyDVNF0vM/n/ecwTnMk+ku5k+UVoHvjYuZ59q2C+058CrS IEmbFrT4hRjDDSNGR5J7gptBHKI2qOg= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wAV5iyoq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 31LmHZwkKCKkJURLNahQUPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=31LmHZwkKCKkJURLNahQUPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--aliceryhl.bounces.google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43624b08181so4454225e9.0 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=kvack.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=wAV5iyoqvhM1F/kLehQN3xM/7oNIW6ihPoqUxdTdQbqyJVS4LDiJdRCWK18DoYO2gS LVBtvCcLN6D0GzndvnvU67x5hLc3H0/NzEZWPzvBZOlPl4atk970esQd9erXVaUgkhu6 fawSOfos+ERe6mEWPvJyxhKIRn3QLVVxIqaADeoFFjGp8hEpf/ZZtLfR5FAXNbVnlUZ4 heLRPth922fXDqKpSOh0YIYXKXDP15/Xij+/+13uMO5nzXPLopeXsTx/s94J5qEorw1u EcIkZTNRz+ZB/54heh6dWDqSCkwXIEexxxBhjCKuHv+NW8fFQsUQjgJKYyDiDWHe3v5Q EGKQ== 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=FGQw2BCwV0GLNXV0yjKSyK5sGU930ivflaJCga11jytyRMyGMewJ64y7Sy6A9OhpVP VUwDzQa4x5P5v9u5vdqom/UkSUd5qMbjsfS9I//LlJau+LOKwgP+U6tf7TSvWbNT0RkP jzA02tDN5eO3Wpe6wGK55u8KWpaoW0zGzkyD/XHCtLpKxOmUMssZ0olIupxBlS6Yoafe qgaTEfekIUPolz8wK/wBveURJALaccCgRhNFNO0BiVovLfO9GeOFxZIZgYnU2nxyAkMO NJNBZoHuPzkkN4Cx980EEhDGsTDExRcON0XFXuYpK4Qy3iXnfSMtMK6wqfyQtTNdCug7 Ym4w== X-Forwarded-Encrypted: i=1; AJvYcCWPzRyWfuwyzJm9Zf/13srHG+D7QuAfAcnuwfRKBJaArDws6BbbjIiW67ItXMp6w/Y5YRkr9iypdg==@kvack.org X-Gm-Message-State: AOJu0YwmvzLqE3PYa6y+3EjMUF2L9aKK/mn6Mia/t61XBvCvApaqqwa1 6lZEp5gRw3a657RDrndC37bGjljAiSk+wMjsmodehGnEGc4YY5L1XGRrAiflzaUDoijXGBRaehd 0qfnpmMIcmVFeXw== 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> 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 X-Stat-Signature: 5sxdydzfeyjit7daqpw59r393yd6e6op X-Rspamd-Queue-Id: 5B05E40014 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736948181-581782 X-HE-Meta: U2FsdGVkX1+rWLVE5wi2OjWXvya/aElIJpGpOeOJJa7A4isWMfe3EC6OvE+/y/2NM8wb7DCAEEA3DkIPdLf0oLN27VoCk0kxP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ZZ4R7+XnRnsugOJ45Zdr4/Nmdygrk/s8EQ9X8VpX+gTsfFHMiMlpuycMm9X36nG8RuiSId3BR94JiEpLTLyOga7LAIwikui7vDnVoMOUY5SwXfiOK+KSs6yXXXeL/0hso4sb16kCcKpv2hKgbAw4BkmIr51/60aDvW0u0Op0rU3TxqMk+GtWTi1nlybAfuVi/cp9rH45heSI0lTbITmx+NIXeUzYHrhZyN68J6pn6jmCfKgkgAM0yH0L6LjxsO6/hjkr5Mq7rDhQE0s9nKayAWGTnS2UGXNttRYTCAFjzZ2wB152f1Sqpk3KHjmb9EKwYIsBCg9ISaE2DBx+BaonZ4R5ieFn9JOJWNd17Ay6Gg3sLQSsa7xsDoYSTWe9R2nJS46TIKHbGK9bZ/u9LIFmExDpqXT5FHWRSC4RSbdbNM1a/PaZ0+TxBY1E8uyS4+JVlbUYdzpLl8KgaYOBY26kBr8U2puTi1kihIup/9xF8 X-Bogosity: Ham, tests=bogofilter, spamicity=0.406004, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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); } + /// Handle for mmap. + /// + /// This function is invoked when a user space process invokes the `mmap` system call on + /// `file`. The function is a callback that is part of the VMA initializer. The kernel will do + /// initial setup of the VMA before calling this function. The function can then interact with + /// the VMA initialization by calling methods of `vma`. If the function does not return an + /// error, the kernel will complete initialization of the VMA according 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 = bindings::file_operations { 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 } +/// # Safety +/// +/// `file` must be a valid file that is associated with a `MiscDeviceRegistration`. +/// `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 = unsafe { (*file).private_data }; + // SAFETY: This is a Rust Miscdevice, so we call `into_foreign` in `open` and `from_foreign` in + // `release`, and `fops_mmap` is guaranteed to be called between those two operations. + let device = unsafe { ::borrow(private) }; + // SAFETY: The caller provides a vma that is undergoing initial VMA setup. + let area = 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 = unsafe { File::from_raw_file(file) }; + + match T::mmap(device, file, area) { + Ok(()) => 0, + Err(err) => err.to_errno() as c_int, + } +} + /// # Safety /// /// `file` must be a valid file that is associated with a `MiscDeviceRegistration`.