From patchwork Wed Dec 11 10:37:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13903328 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 93A25E77180 for ; Wed, 11 Dec 2024 10:37:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D86E8D0017; Wed, 11 Dec 2024 05:37:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EE386B011F; Wed, 11 Dec 2024 05:37:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C9068D0017; Wed, 11 Dec 2024 05:37:51 -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 378326B011D for ; Wed, 11 Dec 2024 05:37:51 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E1B1A425E0 for ; Wed, 11 Dec 2024 10:37:50 +0000 (UTC) X-FDA: 82882325862.20.94FF9B6 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf04.hostedemail.com (Postfix) with ESMTP id 9E67C40007 for ; Wed, 11 Dec 2024 10:37:24 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Pm0ZeKNy; spf=pass (imf04.hostedemail.com: domain of 3e2tZZwkKCPgfqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3e2tZZwkKCPgfqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@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=1733913444; 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=rTGvDMTG6tS84iSkKnwUvzTY8tCVI3+QbIdPlF0cheo=; b=UKLNQhX8E2SAQrbJSZbKvINawtECNfGjlUzIMyhJNiYJg1vMr4n9Q9GB3nOd31mFYP8grO CjshsyFSIt4WEFgmXUrO9gvMQQ4PdE6eo3dZ6JXpk36tsz+/RJVX+2MpJ0EFi0QGfimfYO z/Pe8KeN2B849TcbIfdkqgJp/y6Y3ss= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Pm0ZeKNy; spf=pass (imf04.hostedemail.com: domain of 3e2tZZwkKCPgfqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3e2tZZwkKCPgfqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733913444; a=rsa-sha256; cv=none; b=tapCOfesbK5yh2CqQWt1lyOfPmLUQIlWejlEC0VP2+89gps2eEn747v0JaL5bacYORjnLW 2VN6DpOsQqKuV4HKWEKryasihFm33Af2eyUQZJKOr458Jq5eOECTWXyk7qa8RaJBWkIARM 41K2xOwJaZZPzQ4PLDutHY3IuEvFep0= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-434fe2b605eso16702975e9.2 for ; Wed, 11 Dec 2024 02:37:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733913467; x=1734518267; 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=rTGvDMTG6tS84iSkKnwUvzTY8tCVI3+QbIdPlF0cheo=; b=Pm0ZeKNy9RT8QxBCVZwxrTpLTTc0+2SmRY2jKpqBcZUD51jGWs90Znlgcjb2vvVBXW zAeyECZAxlIetwrIQnrYdiXWkVFKhC3t6tZjkiKrcZE3SMzp2fqwRvMidCyhYLqYOYqj VbQ2TW15hkFXYrxLLHjoyQcRe/oXjnglUs1BrAG5aiZbbaAn6DjBXhtX0ayVZ1XL7GhV SMnpoRaJlZehNgVCqJa6dfZpCYdMv67sRD4pXy53Kv9rzZp6wK8MVTpfyy0KiGqz/MI2 rkM4DDODEshaToZTki2/3Q7inw/NXj4N5PQlz08oPHxSrX/+257z0iDAp/XeD/ifkvIN oeGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733913467; x=1734518267; 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=rTGvDMTG6tS84iSkKnwUvzTY8tCVI3+QbIdPlF0cheo=; b=LqxVt/0hpmEdSXLxXemzoi0LGa40TqNv5lNv91Yvr7bDAJWZJqiujtSbR8ILaC4Rbw Gs76JhkV9g5FAmlX1PJUXY24T+hdbALfUu9R7XYpk1B187AdCWTfMUK+gU9cipr6EC+r i2y6AUZEXXTr7UhDMVagF3mHhmTDb4AzFza5Jtj8vw4vlhTvX76rS2i0pQdRqbUZct/i Xb2dhsrPlb7nTJri4FW+lECwd4HQ4OBuglpmY2SlFIu9qOBdqhjAMVxITCDzBpkJqkpV mv6Q+YCxt/DajvjfCcfq4bCjsp/UBfaS46HT+dUBeoHNyVrnnUZFZM7yV881BmYLLOi8 pzqQ== X-Forwarded-Encrypted: i=1; AJvYcCUtEcElZYsHfjwFoAVZEzD2eJINma98L6E41wZEBwfr2sIZlCtZEFzY6ijl+zaUdIo/mK50zAaalg==@kvack.org X-Gm-Message-State: AOJu0YxbylDU/ew0aADVUjSYX/GCW1vcOxQfcWgO0ENJL4QzdoFBIk8J PQ5gdCSbskhvW6LPtfznl9H487HFMQrXKFUhfWvjqwYtuwOgwjNUyAlgVC+5ERvXjEto1b+tbEs SfL3v1+yGiT/x1A== X-Google-Smtp-Source: AGHT+IFbQovoxVXUDOnrp4PlHMXfzU8u47wUa/LBd5eLin9mhsNX8CNYJjENnzGGaQ/unC/QIRj6aAvvCl/6L5c= X-Received: from wmbf11.prod.google.com ([2002:a05:600c:594b:b0:434:a98d:6a1c]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:83cf:b0:434:edcf:7464 with SMTP id 5b1f17b1804b1-4361c418431mr18973185e9.30.1733913467697; Wed, 11 Dec 2024 02:37:47 -0800 (PST) Date: Wed, 11 Dec 2024 10:37:11 +0000 In-Reply-To: <20241211-vma-v11-0-466640428fc3@google.com> Mime-Version: 1.0 References: <20241211-vma-v11-0-466640428fc3@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3113; i=aliceryhl@google.com; h=from:subject:message-id; bh=kzjS+ohxpZzNcxKXIXjmOFsrjiQU+XTutyXr8P3Ah4o=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnWWtphPlZQRQby61uOX0k3ecWUliVN/FZJFCoi bM4aIl7KVeJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ1lraQAKCRAEWL7uWMY5 RpGaD/wMi3M4it7Hk8rAcHr7ywdgfyToJ+g739ILqsktYqKVFQs/m+KIByJs+9mKBODgiyL10K5 PEZYsa0Ox8s8XKZPI69psKntnEhe3GjUUvu7px0f15cmt57bRZODDgo3vI59YfdTW5FZOESP9RD YhSPLC08wMnnoQBxpcm99TqMClCmmsWIlo4bXZfGvuzS2CJUipJ9sptfZzzrYVIKNO23klgaGPQ 8yZF6MBm59u1msMIvMt35PK1aBm/0EVWPzHCjgQnSqZTbQfrF0VdVZ5yjRfJiEzqf7NT36yIkz5 a89RzhZKbKMOqBVSMGJ7WHIpfsblZ/9wBgcrgDYLjQRb0FTgJQemp5aXCPWaXMNLS/rtMH/O0VQ eaYb9srV1MWgtGQOwbKFC8C0jIWhmSOiTdUabE96UYeBrzR4OkXqW1nPI/E6OaG6jx9o34JdWHQ nCIW7jvcdA2V19zfyaTQT+zOkrX6OJ3rfccVMAXlArE2lHiCUpKWFt7L7z1DlYoYj9PmjnUaTZn IIsFDF/0jEngVlAuF+Fw+EHPuOXm2I4uasrQubr3yOClmPU2OeM9hwv1p1GvOzU6QfgS0LZempt ng0nHj9CaNoclmMK2hvLhOPagmeyxUlGOVFA12epr2x5ASIL++FcelN0S6Uqpdlju+Q7hHbXi1C UQa8vdmTLIt2gug== X-Mailer: b4 0.13.0 Message-ID: <20241211-vma-v11-7-466640428fc3@google.com> Subject: [PATCH v11 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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9E67C40007 X-Rspam-User: X-Stat-Signature: 59wnsdxfum3grajrjxg9txe45razcs1n X-HE-Tag: 1733913444-701538 X-HE-Meta: U2FsdGVkX189JlWlr+AN/WQU5cFcL4niuffXAIkACTIVi4KbuAwWIfB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Wz0/xr9UQVhcznfaMMRnBvVH7zFNvQwW2uU1tQDrvz0Mq13FHwGqrYMJNISUHJcGnvfu0yIW1o4ECU4L6YG8MM/0lOZ73jDKWR1lY8YrhSZ3j21+6JnZNEXwf5cIVKOCsRuAmhPLQf2fh2xXnTaiea4gnBBDXzG00LVVlamRF4jbgkbLtpKRvfTPXQYoCOlEhatAQoVXv2q2Vs348o3xfkSYPFYcqqF8u/tCPanf0dF8Z2seYr57njV84vOO/K4oT6iKy8NQfmDuEfY7xBaw6mh9ORb2smlAln1k75aNNavrFsPgXQngzTX25rYlmqvlPA2L/5y3tV4Bt6mWgV6IL/Zc9XHqR+/B8dct/7gs5LQmtNRbLiv5ftRiDvnbv/7wug57xNbuUzdPlFQ+nbnPpBxSKiFBcg3ynhhy92ojqm8zkt5vK7LgGfkgSw+py/EmiLGZWZWGhC60er3GedPLff4PEHNI4Xbs8IQ5cE1CdrxXUK01GgMHAg+MOCYaws2uK7Q9OQrc/c/3mlfYxyNJsRpYTciP X-Bogosity: Unsure, tests=bogofilter, spamicity=0.496817, 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 | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 7e2a79b3ae26..e5366f9c6d7f 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,15 @@ fn release(device: Self::Ptr) { drop(device); } + /// Handle for mmap. + 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 +167,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 +228,31 @@ 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) }; + // 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`.