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 03898C02192 for ; Mon, 3 Feb 2025 10:01:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 87C56280010; Mon, 3 Feb 2025 05:01:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 82C3028000E; Mon, 3 Feb 2025 05:01:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F7FA280010; Mon, 3 Feb 2025 05:01:21 -0500 (EST) 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 5076F28000E for ; Mon, 3 Feb 2025 05:01:21 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EC6481A1F91 for ; Mon, 3 Feb 2025 10:01:20 +0000 (UTC) X-FDA: 83078190720.15.A74E1AE Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) by imf11.hostedemail.com (Postfix) with ESMTP id E730040019 for ; Mon, 3 Feb 2025 10:01:18 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1lLtH4IC; spf=pass (imf11.hostedemail.com: domain of aliceryhl@google.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=aliceryhl@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=1738576879; 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=dEGNgfB2S/8AjXy/uNgsGPvZS31C4zTjkg+WKJogdi8=; b=L73hPh02TtM1wLjNz+UOcqv3OuS/+WZp6K9HmvlhQKwEU5I08O/J7po2sQvVMiBEKRQBXA 6aLPPkEaPqdr6VZIj9eByRHMvgioeBOOfH+sR1OHCcTaIx7MsKxJ2eWIjb7chwiHuJL4tj JBCpI7j0zdgO/vFleQSw4sPJHPrWeGw= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1lLtH4IC; spf=pass (imf11.hostedemail.com: domain of aliceryhl@google.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=aliceryhl@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738576879; a=rsa-sha256; cv=none; b=2ZQD7/AhglIkMS8gVUj5muRsn/6OHPq7Zy5mNV6kIW/FLWLEWzrOvGj+Pxym8TLpIkTGpp 5w9KZ49zpSKZsztDnEuOx5l92bGJBvIZ5X0tuUh2KTtRaLGGbFj9S/LuxV0t7xGEGiE/Ti LDnb9PbrRJ8S1EEJw93o0hdQgbt1NmI= Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-436a39e4891so28057365e9.1 for ; Mon, 03 Feb 2025 02:01:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738576877; x=1739181677; 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=dEGNgfB2S/8AjXy/uNgsGPvZS31C4zTjkg+WKJogdi8=; b=1lLtH4ICSYSV0Z0W0TY/f6OMMIOH6jEhWk7E6qUcXakbUvlEmWLRos3oo8bN/oamlu Tc4ZwbMe6AjDRo0WuMSxGeHGg3u2m7jbt8RZkp/MLBfigerJTUeIq2BRk6R6DOUYliTd nHcjsFfxYV9zzm3SlSvnvGYR663chE5TD1+Nxi3ZH5jxHR4P0DyOUQzX8TKeHjtMjqqT +e2HvdKy8WSCpyQmMNDlGHyJlodPWVKlcGO9JyHzAyr81yo10UfTa5qhTV7S1m9lC9Kh Cg+MSKd7ZKw0Xx7h6bdcaTJGiVDJbGE9F/YTr7bxXV06Zl34OfFVorm4NgqMAcxjOqLn PxLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738576877; x=1739181677; 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=dEGNgfB2S/8AjXy/uNgsGPvZS31C4zTjkg+WKJogdi8=; b=eMeed7BOjf5JOp0hvhbg0x2K0IpAuMJpr/cfypFe3ePAE9jmDnHzYgZiv8mUnMefds 3vLVzcyR8BbT6UHUPD1ZrF7ycIZrvOLRC1qCwTfMdIV9iiIROEZaa1WQ027etBu+xiwC o0ityGinbwcJu8g/xO1aR/ArOzLh9suOgVJA5uGU14oP24cw9atalMIiBTT/D9p/yX/i vRdJs4Qz10blRtI6Gu/bzRFifPvzjk9xUQaQXq2dAxT33CHf0QErdXTH6flZDTQuZiF/ VMnswmVeEY/wEqkf/EcpMwHGGBTOmyQqRgT5EGNSyXmK7o8S3oYaPAdCZbJ69irxDZgK BUfw== X-Forwarded-Encrypted: i=1; AJvYcCW6jr2KyeeBMtFQGWaUTWeHG2U7zyuURnriiAY0QpuqlLcIpxDB3m1EQr+kBbSUQTIpt5cuY8FhGw==@kvack.org X-Gm-Message-State: AOJu0Yxjgz30cVap30eWvKMFcZuPc0g6O+6Rc+WtlDDGMQ813hEvotwT wrrmL5j5pcQlGrqF6wA5wJtIJLZIx9nV63trYZeHxQ/Yh+evy2pKX4P2X83UzOO+gmkl1bEOEKG 3KMG5iDIVeOuxWEvX+o3uzAmcqcqsCl7sNJpz X-Gm-Gg: ASbGnct405T0kvaATQeaWvtDQiJAqAEs+4y6tWn3LQ/35ZvESqGO2Vy85MgRH5jvhvv 7fRIj1mSk/WnQftJsD8t/othORf3DYQkQLt6gZYFe7ODm/qdjIhfpX16m3hT3H/oCC0WO+8tst1 B3XX/NfItrVyyrJcfUWfwjAJYm X-Google-Smtp-Source: AGHT+IH3g2mPZEl5GlZ3WLWpDP89LKo0b2KChSH5cl3Q6a9dl/Hpn/AlGzitcM3Z4OiZyJKWLS+CB0K0XadAjdKQxbo= X-Received: by 2002:a5d:6d02:0:b0:385:f092:e1a with SMTP id ffacd0b85a97d-38c5194c3b6mr16768265f8f.11.1738576877295; Mon, 03 Feb 2025 02:01:17 -0800 (PST) MIME-Version: 1.0 References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> <20250202-rust-page-v1-5-e3170d7fe55e@asahilina.net> <87jza74amo.fsf@kloenk.dev> In-Reply-To: <87jza74amo.fsf@kloenk.dev> From: Alice Ryhl Date: Mon, 3 Feb 2025 11:01:05 +0100 X-Gm-Features: AWEUYZndXPsUdFaaW3upqQnP0AEq9KZD2d2molkEULuOFcYuzceGr2VHgZKEEdM Message-ID: Subject: Re: [PATCH 5/6] rust: page: Add physical address conversion functions To: Fiona Behrens Cc: Asahi Lina , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Jann Horn , Matthew Wilcox , Paolo Bonzini , Danilo Krummrich , Wedson Almeida Filho , Valentin Obst , Andrew Morton , linux-mm@kvack.org, airlied@redhat.com, Abdiel Janulgue , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E730040019 X-Stat-Signature: iwfritn4c1siu41bbpuo5rypqqoiqhxf X-Rspam-User: X-HE-Tag: 1738576878-827691 X-HE-Meta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LLg1OqzNLLhF+SOkT3cEMj3SiF9O9p0d/9rQMrfyhohyzkkrgCW5ew8RhiIT5UlerLSUud6AzXM+R2QjHF01GAZKeGgGP7pva/IdpWHAEaVxp4biPBYWC9HEmNLnObXR+lyolAt0paUeCKVK5/h2c0TI= X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499625, 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 Mon, Feb 3, 2025 at 10:54=E2=80=AFAM Fiona Behrens wrote= : > > Asahi Lina writes: > > > Add methods to allow code using the Page type to obtain the physical > > address of a page, convert to and from an (owned) physical address, and > > borrow a Page from a physical address. Most of these operations are, as > > you might expect, unsafe. > > > > These primitives are useful to implement page table structures in Rust, > > and to implement arbitrary physical memory access (as needed to walk > > arbitrary page tables and dereference through them). These mechanisms > > are, of course, fraught with danger, and are only expected to be used > > for core memory management code (in e.g. drivers with their own device > > page table implementations) and for debug features such as crash dumps > > of device memory. > > > > Signed-off-by: Asahi Lina > > --- > > rust/helpers/page.c | 26 +++++++++++++++++++++ > > rust/kernel/page.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++= ++++++++ > > 2 files changed, 91 insertions(+) > > > > diff --git a/rust/helpers/page.c b/rust/helpers/page.c > > index b3f2b8fbf87fc9aa89cb1636736c52be16411301..1c3bd68818d77f7ce780632= 9b8f040a7d4205bb3 100644 > > --- a/rust/helpers/page.c > > +++ b/rust/helpers/page.c > > @@ -1,5 +1,6 @@ > > // SPDX-License-Identifier: GPL-2.0 > > > > +#include > > #include > > #include > > > > @@ -17,3 +18,28 @@ void rust_helper_kunmap_local(const void *addr) > > { > > kunmap_local(addr); > > } > > + > > +struct page *rust_helper_phys_to_page(phys_addr_t phys) > > +{ > > + return phys_to_page(phys); > > +} > > + > > +phys_addr_t rust_helper_page_to_phys(struct page *page) > > +{ > > + return page_to_phys(page); > > +} > > + > > +unsigned long rust_helper_phys_to_pfn(phys_addr_t phys) > > +{ > > + return __phys_to_pfn(phys); > > +} > > + > > +struct page *rust_helper_pfn_to_page(unsigned long pfn) > > +{ > > + return pfn_to_page(pfn); > > +} > > + > > +bool rust_helper_pfn_valid(unsigned long pfn) > > +{ > > + return pfn_valid(pfn); > > +} > > diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs > > index fe5f879f9d1a86083fd55c682fad9d52466f79a2..67cd7006fa63ab5aed4c4de= 2be639ed8e1fbc2ba 100644 > > --- a/rust/kernel/page.rs > > +++ b/rust/kernel/page.rs > > @@ -3,6 +3,7 @@ > > //! Kernel page allocation and management. > > > > use crate::{ > > + addr::*, > > alloc::{AllocError, Flags}, > > bindings, > > error::code::*, > > @@ -10,6 +11,7 @@ > > types::{Opaque, Ownable, Owned}, > > uaccess::UserSliceReader, > > }; > > +use core::mem::ManuallyDrop; > > use core::ptr::{self, NonNull}; > > > > /// A bitwise shift for the page size. > > @@ -249,6 +251,69 @@ pub unsafe fn copy_from_user_slice_raw( > > reader.read_raw(unsafe { core::slice::from_raw_parts_mut(d= st.cast(), len) }) > > }) > > } > > + > > + /// Returns the physical address of this page. > > + pub fn phys(&self) -> PhysicalAddr { > > Rust uses for similar references `as_*` so `as_phys`, would it make > sense to use the same naming format here? The as_ prefix is only used when returning a borrow, which is not the case here. I think not having a prefix is correct in this case. https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-f= ollow-as_-to_-into_-conventions-c-conv Alice