From patchwork Sun Feb 2 13:05:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 13956522 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 0418CC02193 for ; Sun, 2 Feb 2025 13:06:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD30B280001; Sun, 2 Feb 2025 08:06:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D81B36B0093; Sun, 2 Feb 2025 08:06:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BFAD0280001; Sun, 2 Feb 2025 08:06:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9E9076B0092 for ; Sun, 2 Feb 2025 08:06:51 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 56F30AE7C5 for ; Sun, 2 Feb 2025 13:06:51 +0000 (UTC) X-FDA: 83075029422.14.955BE7B Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by imf09.hostedemail.com (Postfix) with ESMTP id 267CA140009 for ; Sun, 2 Feb 2025 13:06:48 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=asahilina.net header.s=default header.b=Q1d6zRTZ; spf=pass (imf09.hostedemail.com: domain of lina@asahilina.net designates 212.63.210.85 as permitted sender) smtp.mailfrom=lina@asahilina.net; dmarc=pass (policy=quarantine) header.from=asahilina.net ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738501609; 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=vyzCt0IDTC6cprzdZ6t4DBu1aNbP5XTeaYh7YF6GG9E=; b=SqWTwdquSP6sR1IGtGE2vVe2aJg2GGOScxw2VFb6asz9AaxXy3GditMmO5oEfwqVaVKFt3 CYVDz9j5o08zHGUGKKQ1zyW6UraDjwZACd/Hma7/sq3LpCuOT5228rywvhhQIi2zX2iyan n/INapvKaqzgYbBNi5ryFdHgbcdMG9k= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=asahilina.net header.s=default header.b=Q1d6zRTZ; spf=pass (imf09.hostedemail.com: domain of lina@asahilina.net designates 212.63.210.85 as permitted sender) smtp.mailfrom=lina@asahilina.net; dmarc=pass (policy=quarantine) header.from=asahilina.net ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738501609; a=rsa-sha256; cv=none; b=h2ja0qfJY9ow7YK+euYDHvtarEL9zh5XhKaAnPkX+TQh6+tsNaiMYR699XkUsegt8PKXPS QfLEIXFPknPOjQ5n8STPJeNdhwX3cJM9M6WZw6f/DXakBHs8XE8YM8Ow14xhkKW5ccBpzn LA3Gw97nsr0IHj+8ADb3gVSQ5oB+FJk= Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id C0B3242CC4; Sun, 2 Feb 2025 13:06:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1738501607; bh=/l/yqYUI25JHcLzcwMcBrWhtbhlMTOGAR488Z8bons4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Q1d6zRTZJ4O6vYmZeKYI/dMmURcPQcUkgzn7B0XvDog6QeDRPMPNjRMBp5Oh+dDWs fBKDBU6JMWUawsCrpr+SCfTXQnkcnfb5I3cwQbqLHu2mf+BRrHDcBcJiTDnDY9siL6 m6p+zuQZoIez31D62jvfEf0R9fnoa3LDPuhdrc18zVYA6dsnHDASD8vVK2finjxFXD 9+GkECLq4Vigayi1UGdSEgoy4ynEWax1Q/RkcUFIlmWVZMgVh23du/1cBc8QyRdqhD TMuB9MR8N2FLceLZtF6GbPWpGwVq+BarP2VPQSigR+sdaG+H5fFDHQ3bGFfzxgNb4L +8SDCfsLI9j6Q== From: Asahi Lina Date: Sun, 02 Feb 2025 22:05:47 +0900 Subject: [PATCH 5/6] rust: page: Add physical address conversion functions MIME-Version: 1.0 Message-Id: <20250202-rust-page-v1-5-e3170d7fe55e@asahilina.net> References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: 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, Asahi Lina X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1738501594; l=5914; i=lina@asahilina.net; s=20240902; h=from:subject:message-id; bh=/l/yqYUI25JHcLzcwMcBrWhtbhlMTOGAR488Z8bons4=; b=smOvr2ghPdeB8WGsuSZMnK+RdiZl/ggO1sjUJa9nF7n897PX4BZqpGtxrsCgRD0xgyLMFsN4C lONdvO0e8EiCCt7Wqof9tOCVkpiKbnePQ+jExk4ZKxJ5pM1CvRROa5X X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=tpv7cWfUnHNw5jwf6h4t0gGgglt3/xcwlfs0+A/uUu8= X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 267CA140009 X-Stat-Signature: wnsypufheeust9877eapphfyeprqixga X-Rspam-User: X-HE-Tag: 1738501608-387165 X-HE-Meta: U2FsdGVkX196shhY3uNv1z3YGvmxySq66g/PMYFNUqCKYnr6mUENoB0vFIjJ6ap0ZeNnDg+hmSiCSYZ/GF7qDpXBzwa1XA+jEYH45p1tOaK295GJ2V8h8nWmIG2IcexTZ5ulocsd4y8c4q6PrRh2rlSXIocIcwBCYZZFStqnm/tFV4DJNvnWY9YmzzNL5NVprb/64m7B7FonjqEHMYtS3qmmoTLBmr4V901RSKlUOZaABu7CkxMUG3VJKlo/MhH7XlA9NfRe+d43FdjVj/3pWxeS1hUzha6ASLYwkViPSs5GsHfwGYvzLth+WaMi2MXFmyhw5XuCjf7jI0MWnk9f8E7vqnFheUSRIxenGkVbjA47xFf/0JDrRuhffNoZnVuL/43JuE6VN7jx/jnQT4m1SNyXfFHbbD902ur6xEvxABPlJHhPHbmJaiy5Qupep5iP1iP51Ql3pe+E7Y+fH5yMLzULIvob2zeMHkJtQCpOLNUQVtDeJR8nCO+HAY9RN1ZMcKLKkDoD1xBXhg5nQ3hwFxvsT/S1a0sW3ojFdB1+kD1WUpE0QY5SVNgF+TP+43fixOSgVQ4e8bpg1cR5dJ6VNgXPYtFI3JTdI4fUL2bxEFmqpnYS+cUbLwMmh9AnCjOjEWsLKod5yHVzqWSasKLg2H0YFR7uIozelI7UkXwDC013eiQrJkBx5SVsElwoNiO4TIXwU5SyyDjsk58W5dDJ/vNZnQgfV7vQQRyKN+gzyGtYQEaCB38liJGkvffj1rbSzVcZJTVQMAhm0Gtl4z51iuEdzoaOcoidUDNQu+R57P2gFilZwzD+E7y0v1LN1yk2yYJYBGa4OWKs9vS+wfhXhOdPOvo1/cZoKRYRclksz++s9GCs6wIl7htS4YcHSSPL5TRcG14XNSA3/XqSNq3dZeLAZVck/xQflFcvv5d8ehQfN4VivdjH9CNpDltwO0qHkNIR8ISyBRIwcPq8OyE hfAPp6iQ b5qFc9in8Tt6VCmVRUU5OKQr9hz9ABVsqw3gIFTJbBV/M2Z6eSNB6bqw0TxVKDX+SfjXpMt6uwH6C9jUWvH/DJPtHbmzp4H+cI9ytV/LzVUyhJpAnWk/g/AV9AZD06a4ZdamMwvB2I6Ax98ZcAElypCxNhZ7XUrJjvlJPA3WI5pKnTX4vh/+62DSF9CM78Kxiog5cSU9k7zPmVRpkDQTa/ANKRSawJaG+iaOhoNr/nxUpDBPQ9XcZgCvrn5EWTvE599Dbua/htwHM6rvCLwwYZgSEz2iE/6+tUkdKeqIUY5Y1+rGgyqg//KbLgqJRL8yEQ0LXvp9zFpRl8hrZvYWjcRBHxvYYARJBHu5ovNo2CiE2FtD+W561Z8aw4azkJ5OeS51+nh2NqojU2gZwKrhMKpgllX3ExUdnfw3zm9jXkzK9LE8= X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499369, 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 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..1c3bd68818d77f7ce7806329b8f040a7d4205bb3 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..67cd7006fa63ab5aed4c4de2be639ed8e1fbc2ba 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(dst.cast(), len) }) }) } + + /// Returns the physical address of this page. + pub fn phys(&self) -> PhysicalAddr { + // SAFETY: `page` is valid due to the type invariants on `Page`. + unsafe { bindings::page_to_phys(self.as_ptr()) } + } + + /// Converts a Rust-owned Page into its physical address. + /// + /// The caller is responsible for calling [`Page::from_phys()`] to avoid leaking memory. + pub fn into_phys(this: Owned) -> PhysicalAddr { + ManuallyDrop::new(this).phys() + } + + /// Converts a physical address to a Rust-owned Page. + /// + /// # Safety + /// The caller must ensure that the physical address was previously returned by a call to + /// [`Page::into_phys()`], and that the physical address is no longer used after this call, + /// nor is [`Page::from_phys()`] called again on it. + pub unsafe fn from_phys(phys: PhysicalAddr) -> Owned { + // SAFETY: By the safety requirements, the physical address must be valid and + // have come from `into_phys()`, so phys_to_page() cannot fail and + // must return the original struct page pointer. + unsafe { Owned::from_raw(NonNull::new_unchecked(bindings::phys_to_page(phys)).cast()) } + } + + /// Borrows a Page from a physical address, without taking over ownership. + /// + /// If the physical address does not have a `struct page` entry or is not + /// part of a System RAM region, returns None. + /// + /// # Safety + /// The caller must ensure that the physical address, if it is backed by a `struct page`, + /// remains available for the duration of the borrowed lifetime. + pub unsafe fn borrow_phys(phys: &PhysicalAddr) -> Option<&Self> { + // SAFETY: This is always safe, as it is just arithmetic + let pfn = unsafe { bindings::phys_to_pfn(*phys) }; + // SAFETY: This function is safe to call with any pfn + if !unsafe { bindings::pfn_valid(pfn) && bindings::page_is_ram(pfn) != 0 } { + None + } else { + // SAFETY: We have just checked that the pfn is valid above, so it must + // have a corresponding struct page. By the safety requirements, we can + // return a borrowed reference to it. + Some(unsafe { &*(bindings::pfn_to_page(pfn) as *mut Self as *const Self) }) + } + } + + /// Borrows a Page from a physical address, without taking over ownership + /// nor checking for validity. + /// + /// # Safety + /// The caller must ensure that the physical address is backed by a `struct page` and + /// corresponds to System RAM. This is true when the address was returned by + /// [`Page::into_phys()`]. + pub unsafe fn borrow_phys_unchecked(phys: &PhysicalAddr) -> &Self { + // SAFETY: This is always safe, as it is just arithmetic + let pfn = unsafe { bindings::phys_to_pfn(*phys) }; + // SAFETY: The caller guarantees that the pfn is valid. By the safety + // requirements, we can return a borrowed reference to it. + unsafe { &*(bindings::pfn_to_page(pfn) as *mut Self as *const Self) } + } } // SAFETY: `Owned` objects returned by Page::alloc_page() follow the requirements of