From mboxrd@z Thu Jan 1 00:00:00 1970 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 1831DD6E2A7 for ; Thu, 21 Nov 2024 10:08:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54CD56B007B; Thu, 21 Nov 2024 05:08:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FD956B0082; Thu, 21 Nov 2024 05:08:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C5346B0083; Thu, 21 Nov 2024 05:08:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 21F876B007B for ; Thu, 21 Nov 2024 05:08:21 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CF8E5140685 for ; Thu, 21 Nov 2024 10:08:20 +0000 (UTC) X-FDA: 82809674472.07.1BBC9CD Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by imf23.hostedemail.com (Postfix) with ESMTP id 6B6BB140003 for ; Thu, 21 Nov 2024 10:07:44 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AAh0KAtF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.49 as permitted sender) smtp.mailfrom=aliceryhl@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732183631; a=rsa-sha256; cv=none; b=qsle61XYQEcRKFCxaivkcJB/oMa7oB2ijW3G+spLgDomzsFaogoQOlFg9JRyIctC6CDVuW HsL2hncVQDDRQesQe56+wxYylN4CzjqicBUbbQSXDzIURj/NuDoJxaQDBAFIMPA2FhMLwo KbcJgSqsXkSpSDp+V0xp+3pDe9pUSMQ= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AAh0KAtF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.49 as permitted sender) smtp.mailfrom=aliceryhl@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732183631; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fuqAwFP3qXJbhgF+m94fzJxlivQXEpF5fDoudxVSxdg=; b=dQMBRgdRVed6TwHqZgQYWgizxJUSP0K+GZAMRtAYHvS3FQ/HsBqlxg2vsywWUlJ6bRUy22 9n9BGvsQt9ouw+Qrkf0UEfu98n+d3RNXdHcNksJHniq73MNyXTOHhL5Dk3aKmIFvBdyA5d LJRIuPDyDclGaefJcg04roUeN0Za60g= Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-3823194a879so448620f8f.0 for ; Thu, 21 Nov 2024 02:08:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732183697; x=1732788497; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fuqAwFP3qXJbhgF+m94fzJxlivQXEpF5fDoudxVSxdg=; b=AAh0KAtFlLcUfqTBk1o5kgzAJuSqD7ukM4EYhZvomP2ElKvlSRknn9rLnS+pPZMwIR pxmLIv5I4BPHnaobWfPgpJGgemdvapvX1vteqFPZa4hMx6niU3Vvetlybvyyl1IUo/jU Q0cFBxi3bfXZTUfG6Q0Vs+hp6rxAMtQ+zh3P1HZ4RG9HXTKwxiUCmHFkY87H27SYobc5 V7tk60eQRpsL3bxEnEFLK3ud0vwfxcq9QzCnHVgtz5ydz39MV/rpZAf5WXQ4Ig1UhVtP gWnP0wc80A9kkLgaomlRyVcS7ZJkGxVg5o1UTfZPqNOnQ41yxjA7Thd3DxvhOgEwaT9u v1kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732183697; x=1732788497; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fuqAwFP3qXJbhgF+m94fzJxlivQXEpF5fDoudxVSxdg=; b=Foq8Om3vmt4ZXa5g8kRuzl1mcD+O5Q/WqOe5VucNTbfHnNhqcXy4pZOi9gwa8XC51j 8+K+C2qbvvBTx468XO2IzrXYs1iq8ZCE61CJcdPJqagtKs6BCnc8RbTCwLNTQFHE6wGz 02KVOyaLCkexoC/+xIoLK6ELZInWdyE7jqf0XXJRGkIjdsJyGANP8Hbpph+o8fn9UR3X WWzYfR3Z/VAds9vdKEA7c5mwfZsXBOBwNgq2Sz2COC7BcHpp+/4lCtP2lTFKFG3WK5Mj XxdwTqgLR382BVF8Q9b1QBsJtjXayPuK9f6j0LTXhW+JaKbCSp+gqzKy9IRZribAUh1l 2Qow== X-Forwarded-Encrypted: i=1; AJvYcCXmzCUWNyAjZTP8oi0BR5T4Dlwvh4yzOTNL1l4ZYXOL5trAm6h4HCzm8t15kSGRf1ABMUauiZc8eg==@kvack.org X-Gm-Message-State: AOJu0YyKucBO+pLf+pVRJ/65hhEGzM6fehe9g7luGECWD8bidrJplGJx GR9vrw53K8oWumbHtP2pIerT4r8uZKtIGDzpTvbjdVRcCrADfesMosviXV8FCNvRe9D4XUMINlo hDypty+GbCs+mXTK7+tWQXLNZRD5QIX4QRtbAMjTiIvD4JKMrSChN/wc= X-Google-Smtp-Source: AGHT+IGfL/ASasP7Bf+YpdHopthl9ENVqGb52IIvzrlZxcW0+1CQTsIidejsTxMVAFwQUIeUIXHTNsPRRrhQUzoCXzM= X-Received: by 2002:a5d:64c2:0:b0:382:4b5c:418c with SMTP id ffacd0b85a97d-38254b20878mr5116380f8f.55.1732183697395; Thu, 21 Nov 2024 02:08:17 -0800 (PST) MIME-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> <20241120-vma-v8-7-eb31425da66b@google.com> In-Reply-To: From: Alice Ryhl Date: Thu, 21 Nov 2024 11:08:05 +0100 Message-ID: Subject: Re: [PATCH v8 7/7] rust: miscdevice: add mmap support To: Lorenzo Stoakes Cc: Miguel Ojeda , Matthew Wilcox , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Christian Brauner , 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, Andreas Hindborg Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Queue-Id: 6B6BB140003 X-Rspamd-Server: rspam01 X-Stat-Signature: y6xnqd13d865g3jjaktbg56a5bgp4343 X-HE-Tag: 1732183664-937166 X-HE-Meta: U2FsdGVkX18KWzPfWnIwE/qq1Y1s/6HY73KUxOZ8E2yqW6JIcRXslpuK94leDfHBZKlv9BTwkAsRpNCNFAsi14/CfOaI2D4rnnGW2Gf+i9tmUftTH/BmOpzBQQMdFBuBqSIlMeSnJPELL2so0xkVuaMuMdNrhhXa6eCMJO+4Nqt3inbGt/HQvz+AgG64Q02dTWdzyE6khOFOj3X52EjCa30iNZdv4os+usQRVyB9DAK5UG9Q3nE3eyM8853jEL4JRZDqqynaUUIfIBjTeTCjaeSV2X5GCF4hh02l035cR4wyF6KlofmpRCXwpaU2ne9zl3dXbBKe5gS8QnkAJ0oCkKw0Y+2asGvL6ifAupE+s7oTuZG61rg2yy2mMtqNQ3VV6ZFkYTs9usyuFUGdIPJgQwhJEqcVkSAHQu7vR/QZNvWI75QoRfMX3HdXJ6Ql+18A5YFzBHHq+10/mn3wHMSg7pEKGoJ2IdV/mOfLcL0VY1bcea5Cty1pIN6/vrP9vywHyslyPkMvJPaTyNnABxWcDFunmAl3DFct8kCe0oP6pgVaWD0jkXK9y+R6XPln6PNup4eTlLFgOurvhi2v+DzkYLgsat/k1smA7Fz9v/wpZblak3rhgEzMSi9kFX0kgMBWVubd/xodNw6SDwfQNgr3TqLfok+l3WFX9sMD2Z5X+D32yG9MVoLLBHssxN7Ll3lmnUOww7deW9RBuJdToN8sjhSTA4gMKWaK17od39mmnGt8XSmEr/jlkEOQWxBADgLu7I9cP07zk9e4wjczNZNtG4HHTods3FCoaFA4mp1d+pZ77VaaLbwOTGnOy3OJmIeQtZae1TvvWD7VDY7FYPXmJl6249zMGoqD6wxbxB7KyrZtHk4XRL5t1tHl2AMyG30vMKfeSQc9xqUQBn5SWGxfEebbZyFIqfM3T3B8SfdTDS2icJgm6F9ljZEO0E+U2qNItwwV42OLlJSB9UQxIsv 8qHuIXH7 qL7pg4vQoOzgv8op4pEsIsgJxWPVurwJUvq7TBRMBs8ZaytGoSJGUg6SjEWPnA/TrSoONDEmTN3VIEvNks4R/8WAeEJ+vTI4gsgcaw+09prpJfMDfJzKlDnt6HWAC84TZi3A7tn+F1+ssvN/+YxGLxMRKPWHBMQKjfZtey2SoDw+d8qDcCsZUjTOPfSigWvBuskBMWmrkA8V5twkdNAp8efvyTPhOR/VQ0SVGQ7eX25ZZY3hU5Ub2u4RRjQa+xB6FukRzw9TML7Tuzogan5ikNFfHjJHWRZ1lPc2TTa+pBAHl6X8yQCoXbJ4YHN9/1UhA00T3 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: On Wed, Nov 20, 2024 at 9:07=E2=80=AFPM Lorenzo Stoakes wrote: > > On Wed, Nov 20, 2024 at 02:50:01PM +0000, Alice Ryhl wrote: > > Using the vma support introduced by the previous commit, introduce mmap > > support for miscdevices. The mmap call is given a vma that is undergoin= g > > initial setup, so the VmAreaNew type is used. > > Again, I'd be explicit about the VMA being passed to a struct > file_operations->mmap() hook on mmap. Otherwise this seems super vague to > me! I wasn't sure if vmas could be constructed in any context other than mmap. Will reword. > > 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 { > > You will have to help me with this :) ForeignOwnable, Borrowed<'_>, _vma = I'm a > bit lost here! When you implement the Miscdevice for your own type, you write a block like this one: impl Miscdevice for MyType { type Ptr =3D Arc; ... } Here Arc is a pointer type (very similar to ARef) that represents ownership over a refcount to a refcounted value. In this case: * Self refers to MyType. * Self::Ptr refers to Arc * ::Borrowed<'_> refers to ArcBorrow The last step is resolved to ArcBorrow because of this impl block in rust/kernel/sync/arc.rs: impl ForeignOwnable for Arc { type Borrowed<'a> =3D ArcBorrow<'a, T>; ... } Note that Self::Ptr is short-hand for ::Ptr. > > + kernel::build_error(VTABLE_DEFAULT_ERROR) > > What is this? Is this not yet implemented or something, and this is a > placeholder or something? It's a build-time assertion that this function is dead-code eliminated. There are two cases: * Either the driver does not override mmap. In this case, we store a null pointer in the fops table. * Or the driver overrides mmap with their own implementation. In this case, we store a function pointer to whichever implementation they provided. In neither case is the above function present anywhere in the final executa= ble. Alice