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 00E93C02196 for ; Tue, 4 Feb 2025 14:38:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 502D76B0083; Tue, 4 Feb 2025 09:38:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B33B6B0088; Tue, 4 Feb 2025 09:38:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32C9F280001; Tue, 4 Feb 2025 09:38:29 -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 128876B0083 for ; Tue, 4 Feb 2025 09:38:29 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 39F9F160A62 for ; Tue, 4 Feb 2025 14:38:28 +0000 (UTC) X-FDA: 83082517896.08.E42B932 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 3269540010 for ; Tue, 4 Feb 2025 14:38:24 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cEkGbTz6; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf01.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738679905; a=rsa-sha256; cv=none; b=tG6TbM1YJMe509GUBQFM0WTYwYqvGtDlA9AWXdjl4Tx7kD4PbdHWUcidzFZaya55iKx+Im kBR+LxpQlACJ8wjIhPijU+w0+rEuLnskATirhSYASRTOWY2YQS2WGdcql3SFAuyW7D/mWW zECukO7lbPivCrPYOMbPJR6F2i3edEk= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cEkGbTz6; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf01.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738679905; 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=0e0mruGnMwGPmc/NrxBHwwuLHVLUaZKdPzjoEYXvcj0=; b=JcwOPknLCPawLYtx/lD18Zv+iZ36UcXpYhDvVnrjDnrWT8IJmyWgMRqFm8tOVRXtlBX6RU QtmiGq7LchQM9r91dAvp/5jXYtSJCHeL5wUjLGpyzMumjbBAOtM+n4Ptr/ksX6xE2YA8jt JC1auc4k4ySx+Y4mrkygwwpFIos75Vc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738679903; h=from:from: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:autocrypt:autocrypt; bh=0e0mruGnMwGPmc/NrxBHwwuLHVLUaZKdPzjoEYXvcj0=; b=cEkGbTz6ZBf+V8+DYveyec1GXpYE6eixOIlkYzggD6pdsgAQ341TDFeg83MdU9+E0nxYrN JNxcvev8AtK68v/tjxV0Id8o+yHWvyQWzy7vQARggPgK1Djdh19HTCp1Agz1UjtM/RauT7 Zrj+nZTbzmM2tzauxrjbbotu1O0hj+s= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-569-XSwnZdTQOBqMfyhwiaUAHA-1; Tue, 04 Feb 2025 09:38:22 -0500 X-MC-Unique: XSwnZdTQOBqMfyhwiaUAHA-1 X-Mimecast-MFC-AGG-ID: XSwnZdTQOBqMfyhwiaUAHA Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-43626224274so29751895e9.0 for ; Tue, 04 Feb 2025 06:38:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738679901; x=1739284701; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=0e0mruGnMwGPmc/NrxBHwwuLHVLUaZKdPzjoEYXvcj0=; b=LrTL747k/qpIXQMxd7GMlvk+COblaZ1W+25JOVOgXTLS8Hd8JaRc2OggYtX2YV8Oco 5JwehQExn2UzpAwwUpK82hy6F7DqIxMxi8yxm4nSF7awfvvY6WlVFDvVieg/bf9qgRkY C/u2y9jSErZ3k9iUEx+A0kIoX8GT8yfO7l+/u86dnGltghH916p0HQ2PxCMdRG6L3Xhs t8PMtwZ8ANGaIEGnU1zhhmq6HSghMRmheoCo4r1H4ahLqhlhyRQHrPY7UGVLuGcWPD7x kDaCXBV8lpdBhJdp0bVQkgIvixqxiXINHWPFbpWqBlAnrbUUeN4GXOXjUd+6JIawzSbV Ja3w== X-Forwarded-Encrypted: i=1; AJvYcCX7dCadLNHVa24pWwty0RW3lyM5PbeJTOjfPEojYFA3xASaBp+k59dphLRxw/jUwqf0zANrt6gPaQ==@kvack.org X-Gm-Message-State: AOJu0Yys0OgUbe/G+iRNQBBbmnco1oDs1lNqx0M1fxkPUKa8zKxv3kr6 WgPMglgCo9PLaT+Q+RRQD2K25fazg6CvukLXSx/zrcjcXeh6gX1X8nv8ywkcS53ifoppS6/LW2e Vs5CJ20XSRu/jQIXPvspNseBszEd4lEzraHGty6oOpMctW+Fe X-Gm-Gg: ASbGnct7qbz5HJwN9h6VrK7Dp672PioGp46xe79Xsr89oqlOh0qaFdQxEUNtC4zu0c/ DdOGYhmm1uncf8lAC5hbu48nMMf99cxEWTey8h39jfAeFBCx6Cn+Vro0dDQ6Rl1Uj8E2uTibEkF tgdBea1zmZslkpVB7Mi792tdVcm3Mtog2/2r9m4DkPaQF+rhUQagCHhUqf89xjGZNI6L9b8JV1H L1+fRrRM1N2X1R0D4wKd9523f0B1wB5hvOEENdMwpET/Oh8FYNTd4FN8rYoNdbcuksMkrpz+qMo Gj1rr44VC22CO/4XyvwILeF1iN4TE4IJIXOHzsVBm3tHbkrhyiJmql669YCD3xTbFilNt9GeOT2 mhv7sAOv648OoS6nk4M5cvBxxQ/0= X-Received: by 2002:a5d:5988:0:b0:38b:ed1c:a70d with SMTP id ffacd0b85a97d-38c5167b477mr23236920f8f.0.1738679901130; Tue, 04 Feb 2025 06:38:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IHI4IGrCFQbb7RqQRviN6FtHZtDEVfjbcBsjFUO9cGnzjF7imq8c25U7i1e04iqyZI5RcWbxw== X-Received: by 2002:a5d:5988:0:b0:38b:ed1c:a70d with SMTP id ffacd0b85a97d-38c5167b477mr23236872f8f.0.1738679900227; Tue, 04 Feb 2025 06:38:20 -0800 (PST) Received: from ?IPV6:2003:cb:c70a:300:3ae1:c3c0:cef:8413? (p200300cbc70a03003ae1c3c00cef8413.dip0.t-ipconnect.de. [2003:cb:c70a:300:3ae1:c3c0:cef:8413]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c5c1028d0sm15707943f8f.25.2025.02.04.06.38.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Feb 2025 06:38:19 -0800 (PST) Message-ID: <026c1a0c-e53a-4a5e-92da-6e4f18ce0fee@redhat.com> Date: Tue, 4 Feb 2025 15:38:17 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 0/6] rust: page: Support borrowing `struct page` and physaddr conversion To: Asahi Lina , Zi Yan Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_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, Oscar Salvador , Muchun Song References: <20250202-rust-page-v1-0-e3170d7fe55e@asahilina.net> <41ca3445-80cd-43c1-8f9e-634c195c9187@asahilina.net> <37A0729B-A711-4D45-B9F0-328FDB9ADD28@nvidia.com> <0e19e1c3-293b-4740-93f3-2c410893288b@redhat.com> <82047858-480a-45e3-b826-3a46fbebe842@asahilina.net> <1e9ae833-4293-4e48-83b2-c0af36cb3fdc@asahilina.net> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKD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d9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <1e9ae833-4293-4e48-83b2-c0af36cb3fdc@asahilina.net> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: A9-s2vkd8Sg1_LfeN5OZs5epcuermOtIHbpTAzFG6HE_1738679901 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3269540010 X-Stat-Signature: o4aqnrjfzf1d9jy89ghasn4ew9mexwus X-Rspam-User: X-HE-Tag: 1738679904-567716 X-HE-Meta: U2FsdGVkX1/UZ6jdr+vfIpju08X63Nbc5tOpI5jKK9Y9iIe07k/Mfw9euWlUxuFcLdBEBNI4lt/OlEnu6ZHw3JeFYwJ6E1QaXw9/0CBOYrjZ0YmCFh36Or6Ws5B1R1098geV2DpcXfqvVI+F3YgKg+A9hmlZJ2EBokSwGR7xmE4ARY+rVXkHMvOXS3vRjMD/Abo5yE4CAGZVK4MVrijHpfGtFIAUE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UnSLyXzHY8n/ZhGTQe4qC+w4qvv8IUw/nkB/kx3tnLLExhJGNsKWoiAVwG9t/Kwei3cnvX4JDrHVP7T+wOwUi/+uysz3FLlI2tFbxxU6NDMr/a43RaibbBi/5H37tO3Ua0EAl+G0dY3dg+pdGElc1RwkvZz1CvHvj11PJb0wtDZkBkltUE1npWxew8urYhHR8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.432073, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: >> It can still race with memory offlining, and it refuses ZONE_DEVICE >> pages. For the latter, we have a different way to check validity. See >> memory_failure() that first calls pfn_to_online_page() to then check >> get_dev_pagemap(). > > I'll give it a shot with these functions. If they work for my use case, > then it's good to have extra checks and I'll add them for v2. Thanks! Let me know if you run into any issues. > >> >>> >>> If the answer is "no" then that's fine. It's still an unsafe function >>> and we need to document in the safety section that it should only be >>> used for memory that is either known to be allocated and pinned and will >>> not be freed while the `struct page` is borrowed, or memory that is >>> reserved and not owned by the buddy allocator, so in practice correct >>> use would not be racy with memory hot-remove anyway. >>> >>> This is already the case for the drm/asahi use case, where the pfns >>> looked up will only ever be one of: >>> >>> - GEM objects that are mapped to the GPU and whose physical pages are >>> therefore pinned (and the VM is locked while this happens so the objects >>> cannot become unpinned out from under the running code), >> >> How exactly are these pages pinned/obtained? > > Under the hood it's shmem. For pinning, it winds up at > `drm_gem_get_pages()`, which I think does a `shmem_read_folio_gfp()` on > a mapping set as unevictable. Thanks. So we grab another folio reference via shmem_read_folio_gfp()->shmem_get_folio_gfp(). Hm, I wonder if we might end up holding folios residing in ZONE_MOVABLE/MIGRATE_CMA longer than we should. Compared to memfd_pin_folios(), which simulates FOLL_LONGTERM and makes sure to migrate pages out of ZONE_MOVABLE/MIGRATE_CMA. But that's a different discussion, just pointing it out, maybe I'm missing something :) > > I'm not very familiar with the innards of that codepath, but it's > definitely an invariant that GEM objects have to be pinned while they > are mapped in GPU page tables (otherwise the GPU would end up accessing > freed memory). Right, there must be a raised reference. > > Since the code that walks the PT to dump pages is part of the same PT > object and takes a mutable reference, the Rust guarantees mean it's > impossible for the PT to be concurrently mutated or anything like that. > So if one of these objects *were* unpinned/freed somehow while the dump > code is running, that would be a major bug somewhere else, since there > would be dangling PTEs left over. > > In practice, there's a big lock around each PT/VM at a higher level of > the driver, so any attempts to unmap/free any of those objects will be > stuck waiting for the lock on the VM they are mapped into. Understood, thanks. [...] >>>>> >>>>> Another case struct page can be freed is when hugetlb vmemmap >>>>> optimization >>>>> is used. Muchun (cc'd) is the maintainer of hugetlbfs. >>>> >>>> Here, the "struct page" remains valid though; it can still be accessed, >>>> although we disallow writes (which would be wrong). >>>> >>>> If you only allocate a page and free it later, there is no need to worry >>>> about either on the rust side. >>> >>> This is what the safe API does. (Also the unsafe physaddr APIs if all >>> you ever do is convert an allocated page to a physaddr and back, which >>> is the only thing the GPU page table code does during normal use. The >>> walking leaf PFNs story is only for GPU device coredumps when the >>> firmware crashes.) >> >> I would hope that we can lock down this interface as much as possible. > > Right, that's why the safe API never does any of the weird pfn->page > stuff. Rust driver code has to use unsafe {} to access the raw pfn->page > interface, which requires a // SAFETY comment explaining why what it's > doing is safe, and then we need to document in the function signature > what the safety requirements are so those comments can be reviewed. > >> Ideally, we would never go from pfn->page, unless >> >> (a) we remember somehow that we came from page->pfn. E.g., we allocated >>     these pages or someone else provided us with these pages. The memmap >>     cannot go away. I know it's hard. > > This is the common case for the page tables. 99% of the time this is > what the driver will be doing, with a single exception (the root page > table of the firmware/privileged VM is a system reserved memory region, > and falls under (b). It's one single page globally in the system.). Makes sense. > > The driver actually uses the completely unchecked interface in this > case, since it knows the pfns are definitely OK. I do a single check > with the checked interface at probe time for that one special-case pfn > so it can fail gracefully instead of oops if the DT config is > unusable/wrong. > >> (b) the pages are flagged as being special, similar to >>     __ioremap_check_ram(). > > This only ever happens during firmware crash dumps (plus the one > exception above). > > The missing (c) case is the kernel/firmware shared memory GEM objects > during crash dumps. If it's only for crash dumps etc. that might even be opt-in, it makes the whole thing a lot less scary. Maybe this could be opt-in somewhere, to "unlock" this interface? Just an idea. > But I really need those to diagnose firmware > crashes. Of course, I could dump them separately through other APIs in > principle, but that would complicate the crashdump code quite a bit > since I'd have to go through all the kernel GPU memory allocators and > dig out all their backing GEM objects and copy the memory through their > vmap (they are all vmapped, which is yet another reason in practice the > pages are pinned) and merge it into the coredump file. I also wouldn't > have easy direct access to the matching GPU PTEs if I do that (I store > the PTE permission/caching bits in the coredump file, since those are > actually kind of critical to diagnose exactly what happened, as caching > issues are one major cause of firmware problems). Since I need the page > table walker code to grab the firmware pages anyway, I hope I can avoid > having to go through a completely different codepath for the kernel GEM > objects... Makes sense. -- Cheers, David / dhildenb