From patchwork Fri Nov 22 15:40:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13883308 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 DA7E8E6916D for ; Fri, 22 Nov 2024 15:41:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61BB88D000D; Fri, 22 Nov 2024 10:41:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5CA6E8D0007; Fri, 22 Nov 2024 10:41:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41D888D000D; Fri, 22 Nov 2024 10:41:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1D0878D0007 for ; Fri, 22 Nov 2024 10:41:23 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D93C58181E for ; Fri, 22 Nov 2024 15:41:22 +0000 (UTC) X-FDA: 82814143902.15.3DF68A3 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf12.hostedemail.com (Postfix) with ESMTP id B7EDA4001E for ; Fri, 22 Nov 2024 15:40:55 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="mMMJp3i/"; spf=pass (imf12.hostedemail.com: domain of 3HqZAZwkKCK0NYVPRelUYTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3HqZAZwkKCK0NYVPRelUYTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--aliceryhl.bounces.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=1732290017; 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=Jn+jmdJoGY3YsjVzLAxhBr/vpe9LejwdH758cgsIixQ=; b=PECZot7ZGqKKkz3mIpviZ+pAODBszaZLAuyAmPpnklAgyK3KxWcQD7OdoUxOgKtE48f3xP XuXRhT4Q5NELMnYdfumK97StrQELr8GWCfvTGF0yJjg/px/aHfnFDwaJGVyle9C2a0pV1P /diLaYtqrDDziQ1xq4MY6XAgPA8d3oc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732290017; a=rsa-sha256; cv=none; b=iuE+1MVZb+tYOof0FIDKqmy+sfHSW1KltXgSFazQFyvp6oi/0e7WFEC+b9wCAlY9c9kQ7l 7DhFZVRPWvF7Kp4Bo7D3FbU8R5Bc90666fvQOaKxsxJqbXQFtFJ6U+KJukexxKvtCJ3CIb As4LCUublx8kUnPJ5iPikzun9JDyxtM= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="mMMJp3i/"; spf=pass (imf12.hostedemail.com: domain of 3HqZAZwkKCK0NYVPRelUYTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3HqZAZwkKCK0NYVPRelUYTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-382357b294aso1110076f8f.3 for ; Fri, 22 Nov 2024 07:41:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732290078; x=1732894878; 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=Jn+jmdJoGY3YsjVzLAxhBr/vpe9LejwdH758cgsIixQ=; b=mMMJp3i/f2JX8NLT6Vc8MOHXtqE/9J5MZ1DUH+C264BQ1XWa9UnDqfx5Dj53xrKueo 4xVnTW2IPfsXelQbK95zTEFTWfvxjLz/xk6tjbVRBcKk9QR0NeyqGrCzD3Y/Xmuhppzo 9wzk7ogr5tk6QS/RazNIE9ojyLDzp0CyHzDGTmZLkjG0JYw4t4wt0dWoVjKXJgGjssLK rbz7CfPNrNN9UBpLlSGo/F44VSIHocVEy8I4MQ+GhnnH8FTP6iAEvjORXPBX55xwSAXN PTInVPgXC88ibcTqgbPu3Yqpsh+l6UnzfLhZD1CdH55GhSEKHHef2n95ZX0aIhffUtzw JpZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732290078; x=1732894878; 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=Jn+jmdJoGY3YsjVzLAxhBr/vpe9LejwdH758cgsIixQ=; b=crTYFviPXlb7bVQkbfKk3Hi4772yfCx5CeDDyOZiNYeShP4KQ/b1GalL95USi5QmCN Le6enSrDGbhpdcXznDfX2W3ITkZ1GlCJ5NbWoXISOOABG64gmCsnFcnE/xd0kw7rmRUZ gtSjp7ILj1mrOLzlHsPNSRZejpndTClhIwdBQUcNk4k5FvhTPeC02eGvmbJ5tHG3kHfV KcRzlO+zLmumOK78dOQUYGOp3zhNQt6gry+o5P0M0yPlJGGYyCSDh4qOE8XxzMCWaeIh Ci2fopBbr5VSAWgc/AM1T8zrKRCuyUw0aO988aUFGRmzCaCrtlhpwe2yjwRw/PQR25Dl tQ9A== X-Forwarded-Encrypted: i=1; AJvYcCUcAetIiaQxJFK0uhPpKZrrfbEOGqlQZzEoKDBR1AnSe0c8Ez1Z9b/dQB1m91NeSGKohvaCJrBUmQ==@kvack.org X-Gm-Message-State: AOJu0YydBvHzR7iYNPZYCV6RAMBlagX/BpFHMugOYoTdmP9MICHefZfG zXcD7u1QXLkjnUXVj7LUJZxM0NYYDKP6puTn+HmhKlg8QskYkBnKpcIxescIStALABaWSI/g6iN OtiSTsFoKIPMZsw== X-Google-Smtp-Source: AGHT+IEDL6wyaXq4KVeCa+5cSaT1eT6HwgjeW/qH+ytywyB3jjXDr+R/rDF3sLSLr/FXsbnoKOvq0/msM05k6g4= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a5d:468d:0:b0:374:c0b5:565 with SMTP id ffacd0b85a97d-38260bdfb03mr927f8f.9.1732290078524; Fri, 22 Nov 2024 07:41:18 -0800 (PST) Date: Fri, 22 Nov 2024 15:40:32 +0000 In-Reply-To: <20241122-vma-v9-0-7127bfcdd54e@google.com> Mime-Version: 1.0 References: <20241122-vma-v9-0-7127bfcdd54e@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2821; i=aliceryhl@google.com; h=from:subject:message-id; bh=CackrY4N4HrwWKJd4mh44KeNaf6Y+uJ77q1hA2gtWCE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnQKYKX/KFzHUB/ScZLT3bwuduBq3gubqN9dGbb zhWJO4uzlqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ0CmCgAKCRAEWL7uWMY5 RmMND/9qq+Sg908pb5iHFBsP7RY9cC5qNfZFzfWuBMKlhj48SWuaTddtGzGW+uv7CCplsCcgeHt HAc286bLeHj6SasQPWO5wZ1yfZ9lc/TEdx2iQTqII9TNxR36DXtL5SZprMoBXYVjQ2Tywws7lHY xlQMpm1oy10QRFyuA3mbbrIGpz9LxAx+SNJ3AXDcKnehoHsIxXQiGSTN2db7DA+AuFl2jYjV5lB VIMg44+BKCwfbBhCZtkpUj0U/EHsDy7A5LhsBKv1rFUGccDleWk7uPBbj/LXktuYJ6nYsCBxS9s h5j4yvM/qN4whXwUmWP0Go6PcPTEATyx+1v5ekMf+mhLwWf3qCcpStayFbm0BNFi07zT+4nsZvM E228lqBxIZHtoO2z91VI8fJpZPMSi41fki2pzwndXccNGEL+EyHY7OyKAhtunktx6JOtW+TYRCP mWZ7+rFxasmzyRndH3DcUNWreRDBKAAr2m0cX5l/2Mx2kQRt+ZbWLBDlSp1fcGwHy9p15TDDLPD SEPyL8FilnUErOT3rB+EoH3cywn1kC5I4rTeacyutzbqiCk6fcOTZ0bA8ZjX1uo5vgrXQrfcHqt BBIvXbcN36yINaxKYxh30yS6jPryfIoudkn5gxuUKV7Oo3BewdDxanK/Wgrg2EZeqqo5PeQTEMl mqmg/Y0+Mk6IEMA== X-Mailer: b4 0.13.0 Message-ID: <20241122-vma-v9-7-7127bfcdd54e@google.com> Subject: [PATCH v9 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 , Christian Brauner , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Stat-Signature: iwsfiwm83wrn6qfax699mgchnbs198ge X-Rspam-User: X-Rspamd-Queue-Id: B7EDA4001E X-Rspamd-Server: rspam02 X-HE-Tag: 1732290055-659232 X-HE-Meta: U2FsdGVkX18d/sJCgTfKZeGJMUMF0gFQFa2A4aQ+ZTBO1egaQ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wngc0r2Z0hO/TE59m4IM5cHUuPCtaWrbcbXJSrrbaxVKz77WgtgEHRJ9XWW7AtQFUiuDR4t8splEynbZibBISzXW8qzqdrqqfA5TcKvkjal7MzDywWldVn4xEm/Wzn1WngChkOb+WHef0SsRFxDlOkHAVRxJBaFlUBrOc7VnI3Il8q1lmCEkHkfh4/dKCTRaI8Z8Ibu1QUQxOHf+91NJqz7eVdxihYqrDaqltUaj6oGNWo4pCXTZMZ3ryF20rLNzjgH61n3KA2QyRitm8b5d107r3YG+FMni/DI/LLFDRvVBNJH2D2wclgk4zAbuCPmoyJX//o0f6bYDqfA7FC76HjwsI5kqR+ybYK42ZyAbsFwYfT7gpT+NG2zUHPRzgHXKrrcUp4mhAbR66T6Z7AGpJjLFkxvkkcOx9KoUUgFxlt+GBCWeR+fBpFeZzY63kdMoTWT648RgLy2BvqZ+3M8yu9a5zhq73zjQqD3GdfmbelCtW8/mDnVEsFZT4KUXSiG0UfhEQuiyhV/0USV 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: 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 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 7e2a79b3ae26..4e4b9476e092 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -11,6 +11,7 @@ use crate::{ bindings, error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, + mm::virt::VmAreaNew, prelude::*, str::CStr, types::{ForeignOwnable, Opaque}, @@ -110,6 +111,11 @@ fn release(device: Self::Ptr) { drop(device); } + /// Handle for mmap. + fn mmap(_device: ::Borrowed<'_>, _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 +162,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 +223,27 @@ 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: Mmap calls can borrow the private data of the file. + let device = unsafe { ::borrow(private) }; + // SAFETY: The caller provides a vma that is undergoing initial VMA setup. + let area = unsafe { VmAreaNew::from_raw(vma) }; + + match T::mmap(device, area) { + Ok(()) => 0, + Err(err) => err.to_errno() as c_int, + } +} + /// # Safety /// /// `file` must be a valid file that is associated with a `MiscDeviceRegistration`.