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 B5DE8C02193 for ; Wed, 29 Jan 2025 11:31:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C123280046; Wed, 29 Jan 2025 06:31:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 46FCC280044; Wed, 29 Jan 2025 06:31:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E91A280046; Wed, 29 Jan 2025 06:31:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1337F280044 for ; Wed, 29 Jan 2025 06:31:25 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 968B7120C2C for ; Wed, 29 Jan 2025 11:31:24 +0000 (UTC) X-FDA: 83060273688.03.4618243 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id E267D8001A for ; Wed, 29 Jan 2025 11:31:21 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UgrAnNk5; spf=pass (imf30.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738150282; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to: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=JXxKEWFa0kyjHsvEo16sNj1w52gUXHl7Gu/3LaFydsM=; b=lvmaL4Gqb+iMluxAlsGSDkp7ij546vyCvZ10t8IvWR6hK/NEMnoJkqAIj/wjLqI2WwdUAF UFafuQ/o8fYOECuQaYNi8n2PP2dWTYDMx3/NsQptuqtWqFicMetOXAqbldP580v7KVLsi5 QPmpscDA0XfMOnM5OKKGPK1syOMCUcY= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UgrAnNk5; spf=pass (imf30.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738150282; a=rsa-sha256; cv=none; b=TsAJOBonYPKhM/aoAQhpT/HKTDuH1OrZ3A9lhlVxuW8ztZSg6ifFdduCtlaiyCyjDfS6zw UZZme5E3P+2rfn7iDy3kSYijW2ZtJIzetPgSC86nUlGhhOEYc4AMmTPg8XmHd0+4IKWzWB k6f1J063aIuQLn9pac0usXWwZsMhOD8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738150281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to: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=JXxKEWFa0kyjHsvEo16sNj1w52gUXHl7Gu/3LaFydsM=; b=UgrAnNk52wmueLKuxNisWIqjO6f9yjSJbzxNY6vdBFS+7HNZOz/vH2Zd5hBJ92reVuyjBk BAFV3VnAAFla1gdXiVh472wgrbj837tC30RIRi0fYyd0FOJyRhmFBAmJqndhFp0YoRqN1I LgZWQnb+ypCRNLupKn+NYVEjx5PPfHU= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-616-m0wPUH-yNSG6JnKUlAiTKg-1; Wed, 29 Jan 2025 06:31:18 -0500 X-MC-Unique: m0wPUH-yNSG6JnKUlAiTKg-1 X-Mimecast-MFC-AGG-ID: m0wPUH-yNSG6JnKUlAiTKg Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-38634103b0dso3571864f8f.2 for ; Wed, 29 Jan 2025 03:31:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738150278; x=1738755078; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JXxKEWFa0kyjHsvEo16sNj1w52gUXHl7Gu/3LaFydsM=; b=w6d7UQ1EngdGdU1XVFyh2r+RacwYQWoHGGR8uavny2+JrTyW1AGQ4n2mFn5XsOA6Eb k2VlldGD0tL92i3G/wtUZX86loe5qwjPd9vORZ0a0koboTXxRVtckuOOCUgFcQJwOvT2 nHmwRYxPvidhmPbAzF1S2J5WmFZWaamZ+9T12YD2EGo4CBk8I0AFZ8igSvF7SyyZ6Pxe jsQD5xWHyDhZ3UQ4/tL3WHYVUss7HGJIAG38dFWZH40y5i72ubAjjyrn8g/biv6JfkD6 z+50gtYnrk4KBPGo3hMndFnjcfnUuS5rGHaE4YJPwMf5Nrwn7nkCtr2R+deUsVRYlIrS 3Ypg== X-Forwarded-Encrypted: i=1; AJvYcCX6jepmhp2r+uwxhPvsAo0U5HQAZXQL/VNdNAWa0ECJgravvmjPlUo68wmyGFdpFRBnhuDUroMAYw==@kvack.org X-Gm-Message-State: AOJu0Yy9JKT/LQK/xVqLWVAP3iXh6LHtI9Xe8S4atzxN1WqB9oJqzB0l AxF6x/ElEyzsXdQ+7XYuFsPSgtMksAIKCH2pN8zwPWPHQhYZYrYm++msqHtUG7MI060itfbLPIC pQIIhqmLYqZkyryp1SQ7Mo/J62oNuYfy3a5ZSd0jl8Q7f7ogG X-Gm-Gg: ASbGncvWmsb1m4hxMDhLzu8uMtA0Kteq+7Ttn7TGEpUpVY6GJ4CqOQfFlu3+a+m5bhK wHXwtzEitRCBFJN7qc++2XrWCmMvPKueuqOqtZJnmC7u9X9V+BwBGqBT/wtoQTuC62yKpXgDfXe rUJgG8HjLHXpmkbwayHLhJmu+0aqBWX5GthjK4UbkovjlGDzvZBv8zLB6M5TIWq90jSnmTzL6kY sEZFxmlDjojEjWoUFlEMhjP4dU8OVnWZvvhE0uz+QIaUWwl/h7axz1f79UbpkHJC6L5COcuPShI OmSHnD73EclZHokMhWLBlFj0fcj5XvaWKnRUivSHYH6oO9VtWm1nc4h3TnA9+1vwN77ZTsp74yH xf/7K8vZvOCaI30IqG+5UUa6/CyGky9yO X-Received: by 2002:a05:6000:1788:b0:386:459f:67e0 with SMTP id ffacd0b85a97d-38c5194d438mr2694288f8f.21.1738150277621; Wed, 29 Jan 2025 03:31:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IH+uB1GWH3/Vq0ObSfPbVqQL8MPtlhNJW3rwbsNG1FaGxyQH/zDRIQvN9QYr/JlswhT6uug5g== X-Received: by 2002:a05:6000:1788:b0:386:459f:67e0 with SMTP id ffacd0b85a97d-38c5194d438mr2694238f8f.21.1738150277083; Wed, 29 Jan 2025 03:31:17 -0800 (PST) Received: from ?IPV6:2003:cb:c705:3b00:64b8:6719:5794:bf13? (p200300cbc7053b0064b867195794bf13.dip0.t-ipconnect.de. [2003:cb:c705:3b00:64b8:6719:5794:bf13]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c2a1bbc8dsm16907601f8f.72.2025.01.29.03.31.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:31:15 -0800 (PST) Message-ID: Date: Wed, 29 Jan 2025 12:31:14 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [Question] Are "device exclusive non-swap entries" / "SVM atomics in Nouveau" still getting used in practice? To: Alistair Popple , "linux-mm@kvack.org" , John Hubbard , nouveau@lists.freedesktop.org, Jason Gunthorpe , DRI Development , Karol Herbst , Lyude Paul , Danilo Krummrich References: <346518a4-a090-4eaa-bc04-634388fd4ca3@redhat.com> <8c6f3838-f194-4a42-845d-10011192a234@redhat.com> 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//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCB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jKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: qn6wLyCDRCpuY6MflpFD7bMIaD7VxNS620TmDFuCB8U_1738150278 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E267D8001A X-Stat-Signature: 1i1e9ohhmmu4bqc97htew4y75k1jxcr4 X-Rspam-User: X-HE-Tag: 1738150281-415288 X-HE-Meta: U2FsdGVkX18KtIY5FBUlF7CliUUdwm8R49jaCGWZ8RpMgoPD5nAbcKuVoucg92oE6RxIY3sjlL4mbuC889LW0tPXpGBW7C7Xthqzw5hFSX1j5ljYfCBiZrUjTK1v2nlzmqINMNgLn2kbtnZJ9kvpQnMSu44sZHNRCV1RmBID7IapoI6AXQkrn/IIqeH+YKjLMYnQ8dhnciUD736D49vJG4IO/PkBbLVzzkhdTrhIZXcu/hJdAdBQJ32sC+ewh4j2v94LuGq+90GSX9ebFzbEtWJMMFYaJ/6xlbQe0zWuh2PQ7o+YWjXuNV+UL8owSqF725O6RmZHldaptmLOpP1fVOfaaU0FyUYBkag4mYltwYVRRYRP5D7y/tm+cHSoiZEl0nNTxhTnhlRUm+u91HVoZjsZKvJEewMoXLO9vlJN+CtFoqFFBjDfndyKSUigZ1kUr65H8a9EPGIB7DAp+MSyC7BL47OQ4BXaaTq+oNHx6PyH56tV7LjQalfuUzClSx9y+bJW/ZEmP47+1aGhAYDxml03WCjfFZ+tvzRVXXIXnC5e5V27EKUmd0ReizeaJi4kTQaQM8UEGW8PkkiY400dKwEuIoXckabwtdnKy7pOGM1u6bNa2OPW9qkfCXilkCg8pdtFPcBFLzWNO5EjQZJn1utlWSZY5JQgnZ+k6CSTUXACnFF8RUM43eH+RtqDK1xYVFObsX2hup7g24DIP2514uKiUep75EV2xfVUKh0yIbZENJxbgDUelSYQKPGfaILHppOXVjmqlGY+VlEbIOVnRFHwUO+dvatY8ql5ESwDLNsopUuCF4dmlZJBFi181lX2nZzrMqlfgxWzwtFDt+6y6b4dr8Aiv14fCeCSv7WnTd235wIcTulS5NWNywTdvVMVhvt5+9MIM/9TT9bVcZw4jmkmlR9HkMc+oES3BfB7W6mbx6Ct3bYEpnCpJovoUcPTnJLwjTQOnHxzj0M9Dqr JcXkif0w EGk0L8KEy/mwWJgMUiuffZ7+SGojfdNjN1uk/HCCu2mQnxGN3fI+L3Kvo7+OPsmPhxbdbar0j3onDC06Sbl77Xo/LdskuCBfwn3507AvvO5XGEM7jx3I/m+znxVShEVKvBazUXghWTG3dcqlLS1Iqp4VR7eADOsCN3DSDp3RESyyS3jPElvEfjd4n3xV0z9JM9twkP09PTtx7yyKoksuOA48EP9AeLxyRPKS1IU+KJ2aWZHXA1ARBzDu3EgLXqGCDAXZHDPaI8fldpr8ilY815Szg2E3EQ0WP8Nrf34WcPpFakevBZRKeZvj0zGPw/WbD41WQmp6mb3R40Y/mr2wvxLqtm99Gxg1a28mx0b10NLVw7xi3us2QunJNZWovpVjNAFIbBd96ykNaErlrplCiDX4Kml9GDLwPBE9+FCf9npZUSRikvKD+6mdSApDVmrf32bIWIUG2bUmpS0RKGiB5n9JTSjx72XAuCifzxemiap8GE5ypS43FHNR4Fg== 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 29.01.25 12:28, Simona Vetter wrote: > On Wed, Jan 29, 2025 at 11:48:03AM +0100, Simona Vetter wrote: >> On Tue, Jan 28, 2025 at 09:24:33PM +0100, David Hildenbrand wrote: >>> On 28.01.25 21:14, Simona Vetter wrote: >>>> On Tue, Jan 28, 2025 at 11:09:24AM +1100, Alistair Popple wrote: >>>>> On Fri, Jan 24, 2025 at 06:54:02PM +0100, David Hildenbrand wrote: >>>>>>>>> On integrated the gpu is tied into the coherency >>>>>>>>> fabric, so there it's not needed. >>>>>>>>> >>>>>>>>> I think the more fundamental question with both this function here and >>>>>>>>> with forced migration to device memory is that there's no guarantee it >>>>>>>>> will work out. >>>>>>>> >>>>>>>> Yes, in particular with device-exclusive, it doesn't really work with THP >>>>>>>> and is only limited to anonymous memory. I have patches to at least make it >>>>>>>> work reliably with THP. >>>>>>> >>>>>>> I should have crawled through the implementation first before replying. >>>>>>> Since it only looks at folio_mapcount() make_device_exclusive() should at >>>>>>> least in theory work reliably on anon memory, and not be impacted by >>>>>>> elevated refcounts due to migration/ksm/thp/whatever. >>>>>> >>>>>> Yes, there is -- in theory -- nothing blocking the conversion except the >>>>>> folio lock. That's different than page migration. >>>>> >>>>> Indeed - this was the entire motivation for make_device_exclusive() - that we >>>>> needed a way to reliably exclude CPU access that couldn't be blocked in the same >>>>> way page migration can (otherwise we could have just migrated to a device page, >>>>> even if that may have added unwanted overhead). >>>> >>>> The folio_trylock worries me a bit. I guess this is to avoid deadlocks >>>> when locking multiple folios, but I think at least on the first one we >>>> need an unconditional folio_lock to guarantee forward progress. >>> >>> At least on the hmm path I was able to trigger the EBUSY a couple of times >>> due to concurrent swapout. But the hmm-tests selftest fails immediately >>> instead of retrying. >> >> My worries with just retrying is that it's very hard to assess whether >> there's a livelock or whether the retry has a good chance of success. As >> an example the ->migrate_to_ram path has some trylocks, and the window >> where all other threads got halfway and then fail the trylock is big >> enough that once you pile up enough threads that spin through there, >> you're stuck forever. Which isn't great. >> >> So if we could convert at least the first folio_trylock into a plain lock >> then forward progress is obviously assured and there's no need to crawl >> through large chunks of mm/ code to hunt for corner cases where we could >> be too unlucky to ever win the race. >> >>>> Since >>>> atomics can't cross 4k boundaries (or the hw is just really broken) this >>>> should be enough to avoid being stuck in a livelock. I'm also not seeing >>>> any other reason why a folio_lock shouldn't work here, but then my >>>> understanding of mm/ stuff is really just scratching the surface. >>>> >>>> I did crawl through all the other code and it looks like everything else >>>> is unconditional locks. So looks all good and I didn't spot anything else >>>> that seemed problematic. >>>> >>>> Somewhat aside, I do wonder whether we really want to require callers to >>>> hold the mmap lock, or whether with all the work towards lockless fastpath >>>> that shouldn't instead just be an implementation detail. >>> >>> We might be able to use the VMA lock in the future, but that will require >>> GUP support and a bunch more. Until then, the mm_lock in read mode is >>> required. >> >> Yup. I also don't think we should try to improve before benchmarks show an >> actual need. It's more about future proofing and making sure mmap_lock >> doesn't leak into driver data structures that I'm worried about. Because >> I've seen some hmm/gpu rfc patches that heavily relied on mmap_lock to >> keep everything correct on the driver side, which is not a clean design. >> >>> I was not able to convince myself that we'll really need the folio lock, but >>> that's also a separate discussion. >> >> This is way above my pay understanding of mm/ unfortunately. > > I pondered this some more, and I think it's to make sure we get a stable > reading of folio_mapcount() and are not racing with new rmaps being > established. But I also got lost a few times in the maze ... That mapcount handling is all messed up and I'll remove that along with the rmap walk. Also, the folio lock does not stabilize the mapcount at all ... Here is my understanding: commit e2dca6db09186534c7e6082b77be6e17d8920f10 Author: David Hildenbrand Date: Tue Jan 28 15:25:37 2025 +0100 mm/memory: document restore_exclusive_pte() Let's document how this function is to be used, and why the requirement for the folio lock might maybe be dropped in the future. Signed-off-by: David Hildenbrand diff --git a/mm/memory.c b/mm/memory.c index 46956994aaff..caaae8df11a9 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -718,6 +718,31 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, } #endif +/** + * restore_exclusive_pte - Restore a device-exclusive entry + * @vma: VMA covering @address + * @folio: the mapped folio + * @page: the mapped folio page + * @address: the virtual address + * @ptep: PTE pointer into the locked page table mapping the folio page + * @orig_pte: PTE value at @ptep + * + * Restore a device-exclusive non-swap entry to an ordinary present PTE. + * + * The folio and the page table must be locked, and MMU notifiers must have + * been called to invalidate any (exclusive) device mappings. In case of + * fork(), MMU_NOTIFY_PROTECTION_PAGE is triggered, and in case of a page + * fault MMU_NOTIFY_EXCLUSIVE is triggered. + * + * Locking the folio makes sure that anybody who just converted the PTE to + * a device-private entry can map it into the device, before unlocking it; so + * the folio lock prevents concurrent conversion to device-exclusive. + * + * TODO: the folio lock does not protect against all cases of concurrent + * page table modifications (e.g., MADV_DONTNEED, mprotect), so device drivers + * must already use MMU notifiers to sync against any concurrent changes + * Maybe the requirement for the folio lock can be dropped in the future. + */ -- Cheers, David / dhildenb