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 AE6BCC02192 for ; Mon, 3 Feb 2025 09:58:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E496628000F; Mon, 3 Feb 2025 04:58:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DD01328000E; Mon, 3 Feb 2025 04:58:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C210028000F; Mon, 3 Feb 2025 04:58:19 -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 A12C728000E for ; Mon, 3 Feb 2025 04:58:19 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8A684142499 for ; Mon, 3 Feb 2025 09:58:11 +0000 (UTC) X-FDA: 83078182782.27.C95F32C Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) by imf18.hostedemail.com (Postfix) with ESMTP id 7B3A01C0004 for ; Mon, 3 Feb 2025 09:58:09 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=ffwll.ch header.s=google header.b=D3CGSBnU; spf=none (imf18.hostedemail.com: domain of simona.vetter@ffwll.ch has no SPF policy when checking 209.85.128.43) smtp.mailfrom=simona.vetter@ffwll.ch; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738576689; 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=GeD4zer0SwhO1vY20QVHV1mXu+FN5WI4OZDMZzjZdsA=; b=fxX6vtSBsgAzKZoCf4O1ZW4dMJjU2churDHSc5+SDDhDlBxGiGE/S37hBgOG1sHcDPmijd MBCyzsCCCSIdo/xLAribUZpkGjI0bKpsvU2s8ZqINP79Yx+hU+DK4tCDKcgZ4c3ryovjm6 t3QMtQUEBw+tXasn7OxCsUiwngVZRQM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=ffwll.ch header.s=google header.b=D3CGSBnU; spf=none (imf18.hostedemail.com: domain of simona.vetter@ffwll.ch has no SPF policy when checking 209.85.128.43) smtp.mailfrom=simona.vetter@ffwll.ch; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738576689; a=rsa-sha256; cv=none; b=xwIscUYncBR/C3QZOwwmppTJjJz1vWddSGF2W+GW8jfwd74yeei29p7tarnW46G16wl0kr 1zBhfEreOUv+lDv8S1yCUtWAGiC9ODPzt6nopfYBxyfEem3XImcOgQs38JAYta1u6WcYuU K06zRphFR9IQKVPEianzdmBtqu+EhFs= Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4368a293339so49009585e9.3 for ; Mon, 03 Feb 2025 01:58:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; t=1738576688; x=1739181488; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date:from:to:cc :subject:date:message-id:reply-to; bh=GeD4zer0SwhO1vY20QVHV1mXu+FN5WI4OZDMZzjZdsA=; b=D3CGSBnUluuchVkTka2zgH0IySLiQZOJqHQ70+HUr+qrbpcl8py/Q+tkezKpP2Ixrj O6VKofwzyioq5NyRgOTcMHCqvgOZabtHZ9rK2E93sm2cIpOAK9HBhmbEQ8u89fjAGscP EN9bwwtJaeMS4EiQnIdhnUvWnAuSgCu8hbdwY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738576688; x=1739181488; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GeD4zer0SwhO1vY20QVHV1mXu+FN5WI4OZDMZzjZdsA=; b=pRCgY0zlpYPGE06sPRqAL9bRyU55GedT/sIQiq/uHyGrQSbXHW4g7XIEu6rLfXVgDa wIqELjFCes1X3pPNWDE+jwcXw/0jPVFCWPpxG0kFC/6WLkxWgr5vdFU5rumRgkpXV44u aHZty9dOKYRORXNwCTkWjtb2DH0bKZHNRHMKVSm+BdTKWkobAHKbOIjEHvppEGXlzPGL JhUnwddPVYaaDUR0l8iT5uaf7HLfvVpYANyRWMEVujf/OCRAtdVshEL35Kk1/0ARiYS5 B0bAullZjW63FVmupoeTPid5y+WMXB+bePo8uZK5oyLcAed9YelliyR0wtBUUctGEqef g2/g== X-Forwarded-Encrypted: i=1; AJvYcCUVesJdxVFYwBfSioMWuNh2BhPIZW0nzIrKD4w+mdKScgn7HHBtKxyFeHbc2nXi80h5Qr6KmaG77g==@kvack.org X-Gm-Message-State: AOJu0YypQgy4c5q167WOUAIqZWkd+ojgkke/myCt/TioOgGHbh8zodYC b80nFcgUgermc3azD14vRLfWYVm/IUP9EAkQwf30yg8Na+euZxLMOG471mTNuXs= X-Gm-Gg: ASbGncvgmV4NAv50pxvJ0p7nBt91Ut8IrpxWcm/BFZGbn9fkummg0j+Jx2fMoZQkOxf 4PWw+6X7upBtMcpzRtYliyW71TIUiJRMS6kQLB8w8vCtxveg7ekxkEdGWXx/VvG5gHoV3CfuVW/ 1fdhqpWZq8Il4tHwCUF6JBSmkYOwe5YTAzxJ7LedUyN6UeceAUcEg22CsYhXuKwlgDst1AH1JgM tACTTx95jgUutxDdShOLJZyKvt5eWGsgNxyWrkiYCimfK918Nr9D5X5DeYG9wHizGuN77U70Jz9 m6MUkD7d0kWv6VtLtjBAlGltnKs= X-Google-Smtp-Source: AGHT+IGorkrzHjX4R3pZdGwk+hHms7tlUcrC4qehkpQJ03KFCFxuLmGP8ZR27Vt03TxvRXb7uHx+vw== X-Received: by 2002:a05:600c:4594:b0:434:f9e1:5cf8 with SMTP id 5b1f17b1804b1-438dc435389mr183716805e9.31.1738576687704; Mon, 03 Feb 2025 01:58:07 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:57f4:0:5485:d4b2:c087:b497]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-438dcc2ede0sm185941355e9.21.2025.02.03.01.58.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Feb 2025 01:58:07 -0800 (PST) Date: Mon, 3 Feb 2025 10:58:04 +0100 From: Simona Vetter To: Asahi Lina Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , 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 Subject: Re: [PATCH 0/6] rust: page: Support borrowing `struct page` and physaddr conversion Message-ID: Mail-Followup-To: Asahi Lina , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , 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 References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> X-Operating-System: Linux phenom 6.12.11-amd64 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 7B3A01C0004 X-Stat-Signature: 5uqxa79xsx5qzmpfhs4iskxzwz1emm9w X-Rspam-User: X-HE-Tag: 1738576689-500302 X-HE-Meta: U2FsdGVkX1/t7z1hX8T5JrFQhzu5RVl5p0YmUEkL9/dmvV0gX+DLB+YWBHb0RsyKuqEo+ikeBhb3riQD6mOyB6wV/dYklcECdt7ecKi2fY/fmwEgcxEyA3zK2DuYU93zccX06asu/zrbXcG4glfu/QIA2M6JlmGQPFpMLYO8vh3TguAOVqia/dtJzRgJaaktThkrzKkY7DLT2xMYexZYTq/51RhBg3RyQGw1KBFoStF2dnF9aXVmxZliAwSfUNL/w/ADqjgK6YwEnXqwBiYdnbZBlrUUvLXIYKBJw862Qt+4U2GX+2qVnQrZHiESuzcpU8oV0CTgrD6JJcX229q5UpAeAvDTZENFhb2/RhkYtA59y1MzX/xM0Rp/DTMuCjZUnl12X1b69USxDYA5Vi/kkrKseEL4czsLC9PGSqdygVt5XuzMYEnTV05OOdeKsKJ9mFaey8xR+xZgQpP47GF5E27Wj/Ws4V/kmdx89PbcI0hNWHz1X+Ub9HekfiRJng2WHnIbtLGhP8OJ+d8Oy5tfFS9SkeVSF/SqIYEvP9SVbXQ1GizlJgnPCoQO1t3yjC9Vj5vhkpSm5tfDYEMH+UDydvPLHJccthsw7MMp1tBVx6aMzEM844WaWJXX0aFb7h2WEI+EOpz1i+1MLPkFtMI1MennQNeZxT9j8EfVOZMldowgvFiZKKEQ87yesrPvevbpb3N1ID+9fotileWzNEBDOvlnboHfYVsKp6KuiRM1/cmHNagFeDxS6GuvsuD7XGLLZJJhXHG7J+j+6SKw9GSTrTDHytK65SBrKGG0MyEBL3jNoay2zFm57jpIVUmoTPH8v06P1EMOIxedXL+4js/GFjmdqCNRSVcUAiyMs2wDifEZDtV9lA6GV88cbaDhkyU8Pd9aQlZmtN+dNjQ3XZKnOB8WaPbUe5hdQkuwTP16eWECSmUggZEi/Gm5Wz+icvga3gPjYjT9PSaTUOp82we QYrDuVl3 33mesKLvBL7Gkj1V7mv+/txJ7Tlg1HwRaYcQ3cM+dFyeTw6kmcYor4AvTq9+aE8ZdvRDnPtw3jx5yMnvaF20JpKefwBY2IBsi5nZrN9GhoMFGp2Af4PY/Md8umAEQTzpwX85F13d+Lc1IKdnBijfXcpIKHPPi3aOTBc/mIxWKd95P/SGoanHZYumBtaRR56dAgN5pCy34K8NSa/6RVkRAPxDNtS/DLEGl81wgTZ+BD+jCIBDfVEQEVc5Tglx+NulXBSqERWdVOp94qhLJMqLW18fuCMo3lIqdEi6tHFRT9jKlSAzb7/xW86Pr+GWGW+bL7JDx2LWZbBRnNCqjl6d7qpPU6EvBPhQM74oa81eRw9hSvywXBVRgkX0ibPOx2bJ5HH7VXOlj1E//0Mj4iPRYnwpW+cN5xXf6BupJ0fMNi7EcNMYQRBQoULIhm4/bqbImqFO0D42ltgW+gGzSTktZF/HuiRRu0kiJwVWmyQDUNrYUmqi+i17eey2kyaZN+aszGO7r+f3OJEYiTeJ5C0CaFQ3grg== X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499976, 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 Sun, Feb 02, 2025 at 10:05:42PM +0900, Asahi Lina wrote: > This series refactors the existing Page wrapper to support borrowing > `struct page` objects without ownership on the Rust side, and converting > page references to/from physical memory addresses. > > The series overlaps with the earlier submission in [1] and follows a > different approach, based on the discussion that happened there. > > The primary use case for this is implementing IOMMU-style page table > management in Rust. This allows drivers for IOMMUs and MMU-containing > SoC devices to be written in Rust (such as embedded GPUs). The intended > logic is similar to how ARM SMMU page tables are managed in the > drivers/iommu tree. > > First, introduce a concept of Owned and an Ownable trait. These are > similar to ARef and AlwaysRefCounted, but are used for types which > are not ref counted but rather have a single intended owner. > > Then, refactor the existing Page support to use the new mechanism. Pages > returned from the page allocator are not intended to be ref counted by > consumers (see previous discussion in [1]), so this keeps Rust's view of > page ownership as a simple "owned or not". Of course, this is still > composable as Arc> if Rust code needs to reference count its > own Page allocations for whatever reason. I think there's a bit a potential mess here because the conversion to folios isn't far enough yet that we can entirely ignore page refcounts and just use folio refcounts. But I guess we can deal with that oddity if we hit it (maybe folio conversion moves fast enough), since this only really starts to become relevant for hmm/svm gpu stuff. iow I think anticipating the future where struct page really doesn't have a refcount is the right move. Aside from that it's really not a refcount that works in the rust ARef sense, since struct page cannot disappear for system memory, and for dev_pagemap memory it's an entirely different reference you need (and then there's a few more special cases). For dma/iommu stuff there's also a push to move towards pfn + metadata model, so that p2pdma doesn't need struct page. But I haven't looked into that much yet. Cheers, Sima > Then, make some existing private methods public, since this is needed to > reasonably use allocated pages as IOMMU page tables. > > Along the way we also add a small module to represent a core kernel > address types (PhysicalAddr, DmaAddr, ResourceSize, Pfn). In the future, > this might grow with helpers to make address math safer and more > Rust-like. > > Finally, add methods to: > - Get a page's physical address > - Convert an owned Page into its physical address > - Convert a physical address back to its owned Page > - Borrow a Page from a physical address, in both checked (with checks > that a struct page exists and is accessible as regular RAM) and not > checked forms (useful when the user knows the physaddr is valid, > for example because it got it from Page::into_phys()). > > Of course, all but the first two have to be `unsafe` by nature, but that > comes with the territory of writing low level memory management code. > > These methods allow page table code to know the physical address of > pages (needed to build intermediate level PTEs) and to essentially > transfer ownership of the pages into the page table structure itself, > and back into Page objects when freeing page tables. Without that, the > code would have to keep track of page allocations in duplicate, once in > Rust code and once in the page table structure itself, which is less > desirable. > > For Apple GPUs, the address space shared between firmware and the driver > is actually pre-allocated by the bootloader, with the top level page > table already pre-allocated, and the firmware owning some PTEs within it > while the kernel populates others. This cooperation works well when the > kernel can reference this top level page table by physical address. The > only thing the driver needs to ensure is that it never attempts to free > it in this case, nor the page tables corresponding to virtual address > ranges it doesn't own. Without the ability to just borrow the > pre-allocated top level page and access it, the driver would have to > special-case this and manually manage the top level PTEs outside the > main page table code, as well as introduce different page table > configurations with different numbers of levels so the kernel's view is > one lever shallower. > > The physical address borrow feature is also useful to generate virtual > address space dumps for crash dumps, including firmware pages. The > intent is that firmware pages are configured in the Device Tree as > reserved System RAM (without no-map), which creates struct page objects > for them and makes them available in the kernel's direct map. Then the > driver's page table code can walk the page tables and make a snapshot of > the entire address space, including firmware code and data pages, > pre-allocated shared segments, and driver-allocated objects (which are > GEM objects), again without special casing anything. The checks in > `Page::borrow_phys()` should ensure that the page is safe to access as > RAM, so this will skip MMIO pages and anything that wasn't declared to > the kernel in the DT. > > Example usage: > https://github.com/AsahiLinux/linux/blob/gpu/rust-wip/drivers/gpu/drm/asahi/pgtable.rs > > The last patch is a minor cleanup to the Page abstraction noticed while > preparing this series. > > [1] https://lore.kernel.org/lkml/20241119112408.779243-1-abdiel.janulgue@gmail.com/T/#u > > Signed-off-by: Asahi Lina > --- > Asahi Lina (6): > rust: types: Add Ownable/Owned types > rust: page: Convert to Ownable > rust: page: Make with_page_mapped() and with_pointer_into_page() public > rust: addr: Add a module to declare core address types > rust: page: Add physical address conversion functions > rust: page: Make Page::as_ptr() pub(crate) > > rust/helpers/page.c | 26 ++++++++++++ > rust/kernel/addr.rs | 15 +++++++ > rust/kernel/lib.rs | 1 + > rust/kernel/page.rs | 101 ++++++++++++++++++++++++++++++++++++++-------- > rust/kernel/types.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ > 5 files changed, 236 insertions(+), 17 deletions(-) > --- > base-commit: ffd294d346d185b70e28b1a28abe367bbfe53c04 > change-id: 20250202-rust-page-80892069fc78 > > Cheers, > ~~ Lina > -- Simona Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch