From patchwork Mon Oct 14 09:29:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13834513 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 C97B3CFC52C for ; Mon, 14 Oct 2024 09:30:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 38CA96B008C; Mon, 14 Oct 2024 05:30:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 36D336B0092; Mon, 14 Oct 2024 05:30:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DEA36B0093; Mon, 14 Oct 2024 05:30:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E1D096B008C for ; Mon, 14 Oct 2024 05:30:19 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 0A63D1C4FDA for ; Mon, 14 Oct 2024 09:30:11 +0000 (UTC) X-FDA: 82671686706.01.6E4BF59 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf14.hostedemail.com (Postfix) with ESMTP id B4BAC10000D for ; Mon, 14 Oct 2024 09:30:10 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=VyGOkMzC; spf=pass (imf14.hostedemail.com: domain of 3qOQMZwkKCOMFQNHJWdMQLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--aliceryhl.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3qOQMZwkKCOMFQNHJWdMQLTTLQJ.HTRQNSZc-RRPaFHP.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=1728898171; 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=syzQ0YIA7hiuVBWPGiYek/r3xbiBERARR9zpu8XM3Ak=; b=YRRjGmj/FqhD8kKNG6lvqc1KukTOLaBHNg1cc1PmjILj0WUdY59LlF6M8fAIHRinjf8JNt OyNuntySyw7EytlNqsMg+Si6GMnnz0fpZMjPzS+OkVmQnFey99tkOTs12WLswRwN/+FUox hoOY87232SFjKo1vYyCUSWvDom1we2E= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=VyGOkMzC; spf=pass (imf14.hostedemail.com: domain of 3qOQMZwkKCOMFQNHJWdMQLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--aliceryhl.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3qOQMZwkKCOMFQNHJWdMQLTTLQJ.HTRQNSZc-RRPaFHP.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=1728898171; a=rsa-sha256; cv=none; b=Efkgh3g2McW0rh7a8NZAeestiQC5gs9H65RnT6UxgwIOUL2QyLZu7lzlESFqa92oUvHuk2 2HBzcMw5r6ZPOdLqkHsZPMk8QHWKiVDfMPfHD/4npA2NT/o5pzav1f3TFNL2awu99vsNwu EWrM4wg1M+oUM47MbasIfDERIFDThP4= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e28fdb4f35fso6255308276.0 for ; Mon, 14 Oct 2024 02:30:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728898216; x=1729503016; 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=syzQ0YIA7hiuVBWPGiYek/r3xbiBERARR9zpu8XM3Ak=; b=VyGOkMzCWNdQRADStczIXPCYviwX8/7VmRNLn462nIHikAQw7ExYPt9NjEq0Wy1fAW V23Q2RvOthYPc1PGSAncH6t53Y10zSncMR2bf3vx1BWZQwRvEE8C1Uiyowlls1BLrLKe tWjPLK2Uf1R+nDI7NQAZtqjh87KKyrn62i9HizQPAIoVISaLL3hHh3FmHpbvNRmEwJu6 PSZlNBVLSmjHHLtQgSGcY9iYmoQvIDSaCLs5hStZ3Z1MoR2CnVvXImmqifSPAz3C+74o vpOAbdmGucOdzoGvTSaFiF4LxfAiT/OvPt5c/c3cKHJGv5a/UF9cWNK+qJyViDPPOOeI p5Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728898216; x=1729503016; 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=syzQ0YIA7hiuVBWPGiYek/r3xbiBERARR9zpu8XM3Ak=; b=Ht5o7l/I5FjUtOW5bmRh3jDfIjmA88KGfYd9rlDDvj4pAr+W5HMr6KbUWLlTRX3p00 AQTlbLh6Ez8hy28haC+j+sUC675VPCrHVk59J9T52CS+9mIyRmFuLDBf/gp2Ag5xmG+M TmPEeVYDweCGz9iYw8ZZqqSM8HXeHytImKlQp2k2Vks2QwuINPUVEAh/XphAp6yy4Zyp lXx3DeIGc8tTgO87xhgMV1Ihws4J9w2fhazOJcFP5YWykzHSLwO7nobM+nhpg2w2eizC 3JaygYVDn45mu0yHYBowbslPRi5+pHxsbANNBkomvYFr46FG6f8kKjCBjK9zEoDZj/aM pdGw== X-Forwarded-Encrypted: i=1; AJvYcCVlbpCJFwNBQnDR1WUailtT+F+gExrd81hDq4bHFrRy3ja+MzyKu6MyJ7cuxtWnxo95CJ1XknFOVw==@kvack.org X-Gm-Message-State: AOJu0YwQavXBOCEHZS1o1/Pwh+alMkz6yiFS3cybbg+uxSO6YnJF2CW7 S95GNaVkv4o7TDBFZq2geGVravVcwJ5+RZW8i8y6uCujmsIMcUlyTZpm6uCUTpL1FOEd7qSD2iC cfSvtzEx66rahmA== X-Google-Smtp-Source: AGHT+IFP0Yhol+IMXDZsu5DN19fHDcashAEaCkOGsyT7YnCVDy2E1d/hSKxj0g0oPqksGM1biSgJAFHs+8wjIqI= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a25:6403:0:b0:e13:e11c:507 with SMTP id 3f1490d57ef6-e29506e5290mr2521276.3.1728898216460; Mon, 14 Oct 2024 02:30:16 -0700 (PDT) Date: Mon, 14 Oct 2024 09:29:52 +0000 In-Reply-To: <20241014-vma-v7-0-01e32f861195@google.com> Mime-Version: 1.0 References: <20241014-vma-v7-0-01e32f861195@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2493; i=aliceryhl@google.com; h=from:subject:message-id; bh=2Enhd6cmnVgHNoEaruv8nJGUDXUY2CG66Bw7ifA/0Mw=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnDOSgpJa1Paui3iee9sSjZR4NEdc4yfCvkJU1x YJUtF++TbCJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZwzkoAAKCRAEWL7uWMY5 RrWmD/9uUc7/jFNoMvSWwLQAbbqfprAp4SkfBDb1GaQjPkU7kASucap/tIcLMDy8BoZAAoj9LYu pC4iGkCqTkMk5yRVXVlRxP5+qmaSROczJENdn9zZLkrGqNEO4KGaOdwqe42053x9tvFk37d7TFp /3xALRF7ln1OzIuP8Afd12JVhjCo7QIeb2lOnegG/KqD7aVr8XZnC42SDzFMe1NZeFuD4Vivgzt +v/k+1q3DfbFXcDOD0kjiytiJEVE8s2StSX8ghmHUpBDjfPpvidTfuoYRYwxuxgNl26WlXXal1P HRYxmh5qmHZCTBd7RylTXzkY3FlmQFP1C9N2NxrlTUuES38WIscX3o2MTBh7QRn4JP7zMUqd8s7 DknHT7HA39J2w1k5cpxgb0i+SsIb0ndBiwvNj48BHwsxWFAv0KkKQ+C9lYw2lrk0ormwrsYJYS4 x4rxnMQ1x1OdvN+jQQ1fSSYIS7zEvayfJZrVXDuTnNg1KHEhGtihtu9X88Al2Ubp6/IheTevnDV Mf/J3GH6DwWCNmzsT9T5DXA6VGU43QLi0KMmnGAyHXFSQmYJRposkrzhoo5l+ZkoRhl3loX5flq XicoyN3dqVDFHZdLCqh6lPyNdrwLa1t1cwf1qKy8mKRWM0M1Rt/ZXRnQAKsbcnlOZVUx2A6ZuXA nmUACYPEoAAGHzg== X-Mailer: b4 0.13.0 Message-ID: <20241014-vma-v7-2-01e32f861195@google.com> Subject: [PATCH v7 2/2] 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 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-Rspam-User: X-Stat-Signature: fx3ds5zk8gkdtjt9mew6cgqtur5tosmm X-Rspamd-Queue-Id: B4BAC10000D X-Rspamd-Server: rspam11 X-HE-Tag: 1728898210-249575 X-HE-Meta: U2FsdGVkX1+ZQ6XkK1rKjfDoNYywx+ltroL1s41ssikqKKCCBR54alytGKNUHRLw3+qKVFWPrXuEvcMy1LC6XR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gOiJfOllFcBXftdQTvvLPT+GtG16mlJBLSZGIbVt5YhHxhfCcKGhCDLJXgcXBdCWwb2nyuvRDQpBpWRBf7CqpLNaL4OEsWwq2GV3QBJ5UutF9CUFaU7BfrvInsAvGmZ5xqpTh9QvSkH/DkK3Ew4GGFtXbolkGVQa8lwQCh6mPUHh9yFXE6UjD7GWqjjxLub7rltnrjEls7JrBoqTmPAagcIDOw789fj5L61wNOtAw9ZMoEVVgL3oPzO48Gr2pcXKWSkilcGAzeX0HZIhkdTPavTnIbdmPb+oxAkRfnZWoVss/++60+HDFF7rUwoZMi0nV2Tkk4ESXU4xpipC38ui9Pnjrjc07KS5g6R1FxhdRAZswcL3keHNbW5mZMKB2N3xpcGXq138sE+0asY8H0rDKLX6al9LOIpRhZhcHN1g5GNytfaFN5GTVjZowSlNwK5370JZvXRi6KMDGxZpo66hcWEmE+HMNAyRBF7HDhXuvH3EA/z2P9g4= 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: Using the vma support introduced by the previous commit, introduce mmap support for miscdevices. The mmap call is given a vma that is undergoing initial setup, so the VmAreaNew type is used. Signed-off-by: Alice Ryhl Acked-by: Boqun Feng --- rust/kernel/miscdevice.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index cbd5249b5b45..c9428a6154e3 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 { @@ -207,6 +214,23 @@ impl VtableHelper { 0 } +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 { kernel::mm::virt::VmAreaNew::from_raw(vma) }; + + match T::mmap(device, area) { + Ok(()) => 0, + Err(err) => err.to_errno() as c_int, + } +} + unsafe extern "C" fn fops_ioctl( file: *mut bindings::file, cmd: c_uint,