From patchwork Fri Nov 29 16:32:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13888847 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 881DFD729EC for ; Fri, 29 Nov 2024 16:33:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C4EB56B009C; Fri, 29 Nov 2024 11:33:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BD6C96B009E; Fri, 29 Nov 2024 11:33:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A272A6B009D; Fri, 29 Nov 2024 11:33:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7504C6B009B for ; Fri, 29 Nov 2024 11:33:10 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 39915C0212 for ; Fri, 29 Nov 2024 16:33:10 +0000 (UTC) X-FDA: 82839677172.11.674134A Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf06.hostedemail.com (Postfix) with ESMTP id 73445180019 for ; Fri, 29 Nov 2024 16:33:02 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fvJg5PBV; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3w-xJZwkKCAYgroikx4nrmuumrk.iusrot03-ssq1giq.uxm@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3w-xJZwkKCAYgroikx4nrmuumrk.iusrot03-ssq1giq.uxm@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732897984; a=rsa-sha256; cv=none; b=Bx8zNpyttSF3RVsKdsj76IvRTK8s3AZ/jVHy/bf73n/BRT6tEXT29QeM+QGFGZ+jMRkxoI nky1A5Qbc4ly8mpw1aGMUIOEjUPhC14epWh98qZUFPF0BElqHrouDvr+jtB2fpiorToZn0 5eKR1M2nXMMZYHiOJ5erWdir9PmsiCI= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fvJg5PBV; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3w-xJZwkKCAYgroikx4nrmuumrk.iusrot03-ssq1giq.uxm@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3w-xJZwkKCAYgroikx4nrmuumrk.iusrot03-ssq1giq.uxm@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732897984; 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=UnhyEorAiAZu+6K7VI1xMGROpYDgIW3AxkwRW5dHmBI=; b=jmjBgWfNXD3kJdfKbXGcKwhhxZuHvfUQ+n1spv1NJEqG5CL7h2q9P+G7oFd4pk+tIFa3e6 QXgyx/OIHvfkoxZnYquOs5nFUawzbM6PuUZc8AiOWdgSk4BQ1IZwIRYbVQ5nwprCZda71S hIusq2zGE+BHm7b+zIg5tWPb6fwPMFE= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43498af7937so16383005e9.1 for ; Fri, 29 Nov 2024 08:33:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732897987; x=1733502787; 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=UnhyEorAiAZu+6K7VI1xMGROpYDgIW3AxkwRW5dHmBI=; b=fvJg5PBVpIX3FdF5a0vaeSl41XIdgJOKRlqYiI9qd/MThcZfQZ2zHNAXzw65MgQE4O izH3SyCCY7eeIp5acS1PXDBKBHGa9WQiAiANfqQQlb3esrzY2JMePI7SXrD2pavTeapB aEPkSpcytsx11zjcoYGVyTX+d2eJXKzX5a4qc9xyoRdR+9X/wpEiu5HR7b5cAyjShG4Y VQJpCVpxImxDvuTVgj75T30G2v+g1a8rBsp12WbpXfHjjMf4BhjHz4RTUJdugN6jAdbN rBeuPEIUHkK7Jv1b2xImVHjihEYPyQrWF/PVZSa8/+h3BHFDoEdmnJ1ARbSgIlQsB40o q72A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732897987; x=1733502787; 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=UnhyEorAiAZu+6K7VI1xMGROpYDgIW3AxkwRW5dHmBI=; b=JGnUgaUyLNm7DdnkBL0iaqsBzTP4MRNiboBdGMtrk8W1wI1vI+rBuJYeDUZUjmwPAO WNf/qX192R5J+iqkL62FgQCSjPWnjwCRLy7niYf4Rk0Oe+UqvMunJf1+B+l84G+xAObp ziS2q7Im2N8YjkX+QaWQ14GhOWpH/EsT42Z1chsMEWgrgGkAU2cAUNpGLgrQ+sDAST11 7xmETEFsRP844YVJ1X9XFWNSuainsQnt0RLngHRX2YOtyiWhJfuqp169rNbdm/9RPsGR gnz1zvHTCAtJPrQ0tXeL6MdhGdC3LmEyL2mnBZhh+Eok2vo0Qcjp6LTJi35XXDL5+qwY fFkg== X-Forwarded-Encrypted: i=1; AJvYcCXf877g5jU/hCNgS3iVqWPAijvCxgdA9w1ZLbxtpRQ17ucAupPPrj70nN+M7vLyMSG0HlPIKxsT2g==@kvack.org X-Gm-Message-State: AOJu0YwnylJ/VpuUVX8u9LSpt/Jea2DWUZL2neRGdc9KXmYD9a7arQrn ywF9DiN2WALXl2i8nP9JolQeLx7MvbTUSNFo9PuyAzAgOhExYfXtKhUrZVdy0Rk04JW6LVLs5Vl dZP5Cxr3eKf0Vig== X-Google-Smtp-Source: AGHT+IEIkhciFv/SoLFFHF6Vxq9iGEQEsrBvyMo5yav6t96an9Wy9B+lJz1jJamwd+Gu4sliNPoz0+xpNLIZ01E= X-Received: from wmlv12.prod.google.com ([2002:a05:600c:214c:b0:434:9de6:413a]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3b24:b0:434:a754:661c with SMTP id 5b1f17b1804b1-434a9db83acmr109938695e9.7.1732897987022; Fri, 29 Nov 2024 08:33:07 -0800 (PST) Date: Fri, 29 Nov 2024 16:32:40 +0000 In-Reply-To: <20241129-vma-v10-0-4dfff05ba927@google.com> Mime-Version: 1.0 References: <20241129-vma-v10-0-4dfff05ba927@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2822; i=aliceryhl@google.com; h=from:subject:message-id; bh=IrZvUF0XzNLQMwbfZ0pN4UXzZJ4vVLLu+S9qv3H24zI=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnSeyx3XZetGy5dEQOShQaSbbMRUwJxGEEMjLw8 9RdWQXDUSaJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ0nssQAKCRAEWL7uWMY5 Ri8ZD/wLIEpXUXaIuGN7hRbJCy7O1J4CbcrlTxrSVyF9E7xofr8DGwiW2NYtspHF6JvEA9vEbKZ VEUdDED+X/G1IXI86Qv4eEGCrNHUL6+idMjPfMaYJw55Rci+8r0Ewi+h9QePeYJNaw53q74kjpT szaSueywBq6/KA0AMbekbkpvM/Mlz3sBAqf0pd4McgQOuj3tp0+ApxmJf6lh4+52AoMeKLT2HdV ceUq8pZjoh+KV4CJIX5olRuJzxOgYXDI3Dtg/eMH/XM8EjyEzWTBUmdFn2REX8ei8h4BqWYrX3A ztvAOHVQjzOO8cHP1ew5/ExWAXiOZGn5/ZYVpWMQngFnorTAyaag3wK17LHrOZUbkx7YffpqrBN +97cJ1Myn4/bRhJoAB1V1MYxrQG+8hsaoddXg0IjCtfQi2p1o1aLqkjw6QO3U3q+q5v3ONXSzV3 K65OLwOalr42aJNfyYeQJafHoTzHvpeJ56/O0bGWqaxqVVd59t/l4NjCAb3vHdZasnVmz4W6Ag1 pcPQWa5L+SzCdE2TxRKYKm5+WXtXUbtC30SMhq+MoPqmFStspXIk/CJ60nyzvxG0hgL8P+ji6Po U7yv2fwSlp1YrDsHbvR2mQe0lh39+/0PLppp8PGqkRDBRuKk8s3ccdhTh6/8QiMsuyTda/Hp5g4 rC86Q3qz2t7SDcA== X-Mailer: b4 0.13.0 Message-ID: <20241129-vma-v10-7-4dfff05ba927@google.com> Subject: [PATCH v10 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 , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 73445180019 X-Stat-Signature: 3mrrodmiuyh86q6y7g8fseszx56wsa4c X-HE-Tag: 1732897982-266769 X-HE-Meta: U2FsdGVkX19T4Hr5CWm4PJ3v/O0AQg7NKjEIx0oGUNU8XB70Ot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4SWkwRNPNX6UBBd1gtEDb+LEEattoIciwjcQjy/tiNOKzEFHSZ+Ljz9XEWoHGatrNSgkYL7nVFP6D5FtyQGrGBpxtcAi02rNLOi8HYnsVnH8lSQfBHBqbbEgldOJ2hiT82INoH20PH6XboLyUjo2xY5p7aSidCD82gUpJW2ZKMxznukXKA4L78Po4qUFY0YeNAbt89f7jWFedtMXz0SeeZPsqMzG2k5N0+Wv/4i0uCW0wEnH0R1cmtWzZ/PSMvWnG5gizYWpGirPDolG6/cczZreMTBFTwl/kX3SNFq1wuh6XqdKHIBxOAJjb/3IvCYWtrf/qQwjkoyLXvEOMLMyePxLmv28ShN66PvMimHKKUTbqkWl6t0KHIDSAEa2Klv1vO0hr+Ei1jGQBrbNKx/H29rR87XCyY8DviXGVYVcFCXHZi2JEPEjczFoHXLLYpFYyf/U+Rmsf22LfTagZEi22BGqslsj1fBULz5oCnL/L4bImW+eG+26jB/4/5yTj72U1O9gMckvhlrgjq48rtuzJW 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..99e076338f39 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`.