From patchwork Wed Nov 20 14:50:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881318 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 A083FD711A7 for ; Wed, 20 Nov 2024 14:56:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 923606B00A0; Wed, 20 Nov 2024 09:56:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D1316B00A1; Wed, 20 Nov 2024 09:56:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74B216B00A2; Wed, 20 Nov 2024 09:56:36 -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 53E086B00A0 for ; Wed, 20 Nov 2024 09:56:36 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 16E5280D8E for ; Wed, 20 Nov 2024 14:56:36 +0000 (UTC) X-FDA: 82806772524.27.C62BA90 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 4FDD71C0006 for ; Wed, 20 Nov 2024 14:54:56 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=euVXFQDj; spf=pass (imf21.hostedemail.com: domain of 3oPg9ZwkKCMkp0xrt6Dw0v33v0t.r310x29C-11zAprz.36v@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3oPg9ZwkKCMkp0xrt6Dw0v33v0t.r310x29C-11zAprz.36v@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=1732114533; 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=aBJMJ0qYrky91mwQ0u5syokhvobq81rvT/hL+9f8jt4=; b=7YfCnYkrAobjt+4m5TPd0ULAq5lZP1V58vAEZs1Vmf6AfeZqWiBgOi2OktvjYaDnHZP0fo eGwO7piAso+5ggw07oqiqLZ1gAukzO2YfW4tTJ0zMVc3CrTCvcIJ5KO5MxyWUSy6M+l7mV rbzhloqOJ7d1u3xpewQQMqnuckJGtoY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114533; a=rsa-sha256; cv=none; b=y0SZBEkjOZ8Z2CQIkpLP2s4eudxc2Wju1KLW7RdCa+m3ZJ2hoDm/BewGZZ/WUFBbwONJ7n v8R5jcPII/mvK29wnlvPN+DhxvQqXDVERjKIEPwxfdaqsscIZoJoIBv+ujcaE9EiLD8pYT oxq6wuPaf78talz1VcDKbQeLuiTuNzQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=euVXFQDj; spf=pass (imf21.hostedemail.com: domain of 3oPg9ZwkKCMkp0xrt6Dw0v33v0t.r310x29C-11zAprz.36v@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3oPg9ZwkKCMkp0xrt6Dw0v33v0t.r310x29C-11zAprz.36v@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-382412f3e62so2043041f8f.3 for ; Wed, 20 Nov 2024 06:56:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114593; x=1732719393; 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=aBJMJ0qYrky91mwQ0u5syokhvobq81rvT/hL+9f8jt4=; b=euVXFQDjH6aVhUtIm8FkzXBu9ific+pCNlUKdz3rN+V7GrucQUSTRB7YAaeByBzF4l QTy7bbGBJ33pm+MQCv58vOOfUtlq7tf/cJzfvnEsJOuhF1sN5JhSjCJnEgNuWqtQ27UG bP32/io8dqMoseVz8mYfT8Q9Y3sfo/AD1IhPDHUs/WLSny8UyB/HA8K/4XUXsp22jiMT tOHfEqQo70Fo04mp46gLPH/Rg996shlhvFZWFB54XepPuc+JnMmTUafezKKyiAhMmajf zB6hN0q283S/TTQOJNzl/RpxBhT1AW7wtLpj0F5zygSN+Vuosk0h+/hghy1tGC5ZegQ9 gtGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114593; x=1732719393; 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=aBJMJ0qYrky91mwQ0u5syokhvobq81rvT/hL+9f8jt4=; b=KHLfrfeMrkF1RU8m7wbhNSnQ0etnrepJmOmVg4i62eNGJiUzJroEt5/2wEvPRTityY ppDQRkuWhAwNbYhxEPYF9lEm28MgnuP0mbaXkXtJ0G82r6hHlg5fO3/IP7XEtWMlVPUn 6czcFCFAKVoJfMsnvhRHDNCZi/2UAHcbDNstYONCyW+UNMR7QpqYnh96EkAHs8NC3uNP +hy/4QdeR2WkGREAG40js8dUuobG7JekESuoQ8oUqeIYUl5cOmK4KOj7xpGkImboMKG6 oibo8zmMW+kLfztxRpJLH6b3crqN14GxLHGsAH7bg0Gz+XQrVojrSmY8egiO04DPZAC8 S+Mw== X-Forwarded-Encrypted: i=1; AJvYcCXddQPieEIfpaQfFmFzeFC83pu+KN+PwEchoHZ7qvqm82EPZ7afWJs48WDUB2B5TB00z1yfwzp0uw==@kvack.org X-Gm-Message-State: AOJu0Yw2uQx0QklfH1SeHJAi14X1tJWLe3jlylzdEZZAjNqvO5+IfHab hBHjKSGkMzYv6aGrMMgNFsOqDHHVykW4Sbk9DPLaMvWSQHuOkOIIXFDMMCzvirotrPzcRCapYY9 xStZGZiRPaTpZ5A== X-Google-Smtp-Source: AGHT+IHg6gC2fHXTfoa8HybdbCejL4Hw2wCXFWVyfLSy/FMj3wKsyFBkjC9iLS8+VOnYGq57+fL7fZy03dC8naI= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a05:6000:148:b0:382:3745:e843 with SMTP id ffacd0b85a97d-38254a8593dmr859f8f.0.1732114592756; Wed, 20 Nov 2024 06:56:32 -0800 (PST) Date: Wed, 20 Nov 2024 14:50:01 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2667; i=aliceryhl@google.com; h=from:subject:message-id; bh=GSw09bqyA6K1PcCK8gagOT2XwF7uH8Q/0z1wyixcHDs=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiM46FQ43oKCfz0YbqQO2Vr66Kdgk66Jo5tx JPUulsyfy+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34jAAKCRAEWL7uWMY5 RkXqEACEB7yn2mFW0lgsKxYBQ4qe2+54UutRvkGiIwc8wfu0WgQ+1KUd04XtaEmpyHADZ+itLu4 w1PUr+Qf3HccuAeWtsE12ckyvdIacsnpDEl73ajj1jGwJsZwDgM27DJB3Pe9OaB8FTOZAPbaLEZ JOmLqMLXCULYc26n3q9/Yb/FMwV41XYcxFPovNlq16vpbsQ0PTY9A//QKAhhINkxA+HD6etiNf+ bMN7aL4D5wJW9KHNeA+ubufEouX8yDKZF21ft/+ZyGHSs2bL6qlhwG2ZUly0H8wtI6G0XHciaNh xe2agSElixu2sn+THcpulk9QebWT0nAW3alVucVH8TVZPzXB04MQdA1cMzSxg8yHCSlRdWdCtAi jJfO979h+KcrnKE2+KMi6YV1lr9SEcLHt8q12IeUWIZDUD7Bb8JAC9zi5B67xB11hl2HA4kfdZm jo5jJp9K3lAgAmFrxIhr5PhOpPUauxswnhtFQLl40gmlHRUFh6eOngHO/oMoCLRqKY3hRlw8nv2 MIK9KFyNCYT2SFWifeBNJ575bVLcsnE9ESTUpSgsQU1V1SfWI+UENNWsPrxUd+sIY4rYkGrSTn4 1ptpfZuhqYA6ZNOY9B2j2hhTh0rf1jOBrWFkm0AuSio6VJCdcqB3J4dmzMR4zSY6VslhXm/+yV+ q10Bd9/rJyXui7A== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-7-eb31425da66b@google.com> Subject: [PATCH v8 7/7] 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 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: s47bgz7ypgkhim1q1hsrbrfjq5wgdf5a X-Rspam-User: X-Rspamd-Queue-Id: 4FDD71C0006 X-Rspamd-Server: rspam02 X-HE-Tag: 1732114496-524807 X-HE-Meta: U2FsdGVkX18fhLISGth+l27D1UbVMKiCpe129NoIR5gxOK9f3oFycylrDMXf9KxhSsE1XGKTRLn3tOga+l+XU15tgx12AKsysGfEm/LLsQU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m3jvUnCheN/tbkpA4VCR20RFjZaMp7LaKrzOfOqvE0QeOc+UmwmrRKzD6qkrkBOlA6zgIrxbTCaL2uK6VACSnjxu84Lb99AOGvbr1A/QoYSBMZ2URPjEw1uP6qdQ2uPgo+q1lEXWTTmLOas0h9CYnA3dEZB2sVdbKhCTm5WB1dPmfJjWd0QT54DqiFB/9K+gVv6C7ZYJGn8ZYNs2Cf3djQNccPBs3jqp0B+sY2wr+jWpck/JQESIzUBItKQ9rTOnrXn9xmMimjTohyP5pWvXycZqu/13xvTgOvGioNGFCo22D7hNUUXanxog9gqRhiHu7qiX3pUxkq9bgdqk5re4yEdWhLRF5xxze7ae5b3WYwNeRBAysO7J1s2IVnTa+/+N97B+5/dme+vW7hIWpPYooSNhvFNgrxdX1MENHKGJZKZcKEtm3yfCba46fk+ICMk61HNHl20DkWXSY+9fwV60apsuXmhyOCd1d7VBLy4M= 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 --- 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`.