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 CBE0AC02193 for ; Wed, 29 Jan 2025 16:13:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5F807280049; Wed, 29 Jan 2025 11:13:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A861280047; Wed, 29 Jan 2025 11:13:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3FA9B280049; Wed, 29 Jan 2025 11:13:51 -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 1831C280047 for ; Wed, 29 Jan 2025 11:13:51 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 76244C0EFE for ; Wed, 29 Jan 2025 16:13:50 +0000 (UTC) X-FDA: 83060985420.24.0275C44 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id D52CF140017 for ; Wed, 29 Jan 2025 16:13:47 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=R2QTyrAF; spf=pass (imf09.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=1738167228; 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=Kb3zTxBCC6THhW70RVY9Rp+026e8CWg8Ogw+Sa+ACUA=; b=oMyNircAZq3Nem2+tK292k5gAfZe89V8y2AnpS5uE9xL+8u0Y5O84Uc16hazSgcNw++rg2 VDc9GMncVLM+t7ZMUUIHux1T46EoBPvONCSrKK22B+3PGgXf1J0/lXlwioscYoZU4U9n1t zyy1w8msWv0ZFoA5Efrq/J8JyFN8R+o= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=R2QTyrAF; spf=pass (imf09.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=1738167228; a=rsa-sha256; cv=none; b=lqn6tXTpwFqDsYw2EYvkMdKykRNxlF8gp+ITpajiMna5Ic/UejWaa6CImd1pvAKyqyg8X7 qyBCHCL1BhmE3N9BrUBSMK3CHjFv+UbMj70nqiZX0U2iEfWiLNxxWe9sPl86epAFNlPepr ABjD049dZGlm0mNNGL4tocpAaZo2YtU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738167227; 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=Kb3zTxBCC6THhW70RVY9Rp+026e8CWg8Ogw+Sa+ACUA=; b=R2QTyrAFB3tW2vHVFPdGABuSWH+Fv7lcArAmf8M/Grv1pbg6qJJluGiBbmRZsDOxAfsDqL wKxxytohzkGcFOroykui7hpPChEOs+exx8K5Cze8Gsv4OEZB8g6NN+wkjGWH89H8yy6t0k y2EhVbYE//JIlzdJctIYzyElhEdu3tI= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-492-cAFahrIHPYyPSyLKZDPGZw-1; Wed, 29 Jan 2025 11:13:44 -0500 X-MC-Unique: cAFahrIHPYyPSyLKZDPGZw-1 X-Mimecast-MFC-AGG-ID: cAFahrIHPYyPSyLKZDPGZw Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-43619b135bcso34128625e9.1 for ; Wed, 29 Jan 2025 08:13:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738167223; x=1738772023; 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=Kb3zTxBCC6THhW70RVY9Rp+026e8CWg8Ogw+Sa+ACUA=; b=kPFi2QQLmJOYrr/TKWA0SLwBbkmDd1ibnVzojsp7hbruznIJytIjaKmlIMeGMknOdC Bq9TlwF5+4MoTkq5f9TOxRnj+zxIPK/x96aPsFhDfkMzGac1Y1tKD48BLGPciVqJ/x8s 69rleoqj06vJqGqNidJ7rLB/vUw7NOoXZ/v4A9kFyhAz1a9fs0wABnnXniVzIvsO58+d JuSNmjO27QTF10cv92Krcx5nGuAZ/XmKO5uabIijruyrAC5CGhV6DsrAV7mWvLZfag1O V1uq9A1DLr1TJSTcVDJ/7zPA42DxB4Uma+cnulR8KSm+lLuEU24vmHRl+y3vFxnAEoG0 YvzA== X-Forwarded-Encrypted: i=1; AJvYcCWDIM1SSOCRMP2jySQOJAygOzRgEqJXrdi79HYGNyxBkiR3Eu97IJw5Tu8ekJ8B8rsI5pIAkBBw8w==@kvack.org X-Gm-Message-State: AOJu0YxZ4ZEkd6Ev7yulKawF/ijsElJ361Soe5Bu1cgkC978G+gGAeug nXoGzszA78YCbTFrNSVwGoNPCndTC7xw64j6k5nF9InumEOWZkhXUzbq4FY7/T/tdurHFIvBQWD cpMhrlnBNW9sVw+SuscWKp2xbP4l5xNm2dktIIeeCUFMm91dt X-Gm-Gg: ASbGncuNh/g6CZRUVy3SOiZCcHd6VOUsntvubybZEVsVQQRCBc6D2NLjjOZ0x/TDOiU ny5kA8U/4O9efXHWi9pmCuBYF9u1XdZ8cDF2HEsv67zNzJUsER4x1DDhNlAC8F+AAQsQlLyCmuJ F8KC7VoM3ey4wwztVVV52UOuDljJfmPMnAvjhmbYlCbM0iKE4OGd/+q/fJhF/1rT+gzkHDpRcQz ZER+Tx/+JN6OmH4bcx4GpI0DXE8x0r2iB1cRZQRwMXqq+J7/ovgI4AdRIn8M+GCdzamOH/V8Mny xt4rwwbU228GWAZjL9bD2VZKZ8a7stkePukw1ne20OAKy4DgoL9OtlKcRnbu63+HKGFJSic2jcP HLK4ZaIVZ3/P8muzb8P91mwzMnbdo/mJb X-Received: by 2002:a05:600c:4e01:b0:435:136:75f6 with SMTP id 5b1f17b1804b1-438dc34b179mr35955305e9.0.1738167223426; Wed, 29 Jan 2025 08:13:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IERThuAB6HB630IBL71I6PXg4Ge8Wu2EWkINxaot7DRnv3BDYgceEpRtMXIjC4rFbds62QiZQ== X-Received: by 2002:a05:600c:4e01:b0:435:136:75f6 with SMTP id 5b1f17b1804b1-438dc34b179mr35954925e9.0.1738167222991; Wed, 29 Jan 2025 08:13:42 -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 5b1f17b1804b1-438d7aac93asm44293805e9.1.2025.01.29.08.13.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 08:13:42 -0800 (PST) Message-ID: <8dfe1dd5-f4a7-4b8a-92c7-278519ee4c58@redhat.com> Date: Wed, 29 Jan 2025 17:13:41 +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: <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 ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCBhUIAgkKCwQW AgMB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a77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 0snZu48xjo9iCxlnFjhucjuc-1qJ5ghir8PmjtMMvjU_1738167223 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D52CF140017 X-Stat-Signature: 5xwmpwu6ag9dct3ke8osaiapaw3figef X-HE-Tag: 1738167227-825097 X-HE-Meta: U2FsdGVkX18734bwVI8O80TJHjmZ3HzXPJStE9tToouqWlY2bzOe4+1OPnG8U7LDRanxmvSlRCAJc9z9UvV9nyNG008rqpPZ2nYboRnnnqPsj5sYmXfljO4PhKg8XtHb3jFd/kDyQcuUjqlThMo2ULigOXvFd+BMsNXRFp2NLyV9cEgbLcXUGA9CQhok9T7J39CZ6HSKeC7wVgvXKYumKNjinGX5vwhahkglGjNb19PODtSb/6HAdoFUjOSVtLKs8oP6E56FaEBaStvA109K2ftOCBXjA1/sKExXIwCOR2CypEakk95kvfSfZwF8wlKjW8++NMrB2OPHg0SXCHLPJEp4ntXtAbNPVogWZAy3rHO2hA2p+XDYbKUlCQ3CsnZZRu0Geb8WKVLxpYTXZ9ay8fMlDFMtGt4bWgGWcm7XdZm2SGACHDmJ9O+4K8L8hwfKVNn5atRXICjLZrlJN/LORL74vFruq7gxRxaG7ONzext9cWSvY2oQiZgVvbtHtWUSFwt5czDUwPYehN23IvnYk/vi7PAi/t+kf+30s4EGoJA3giTgF82XVxX+Or7HG2BbVZdYg/XWxZCyDbnZPuL/Oo0UMCP7RTuldyyZWutA0AylxYRoCn/pVqTNP9FzEUkAlRkBzAf3oIPjC71OOVXXOqTtb5xtTU2JJiy5wz3VXcmkECAjp8qOylRyRTm3S+6nHxAVoeYbvS9fVj32eZftnZ58Dj15xvTSIcpa/ibLMMPg6QtOYaXweUCKy2mmEXyg4SX5bNPlt6fs815m2xy1ZKvVTwlkP9iYrFrXTsJRJqR+H3iVSRj9ytqU4jt72Ie0c9EpvDoyK0RJD29rFEGl4qn1bdQR77WbyhukHkfSreoUn85sd//apcR/yLgGgzfLY/0QN+XvwRerMmPUhWHpNqLW9hab3xPq/Wyq6kYqxrAahROp7pSPdydGa54L1C9iBpPxeAJFj9kICcu30Ci 94qHc7DW VEfMDQdZd1xrZ99crl4K6MS1qhLiniMA/D4k24rmZoiMpJ3NBZpxf6Xv0kgEmDqGcIbRILtIb0W3TY1AkWCSCXRs/O3Oq0cjGXtcz6z+5qrqbKhUtOE1XG2OPZyPeHJpArHfdA1Wyp156wpHt20Xw4EeMHply+Dv3T2bO3w7sOdaUVSsAgtIHg168F8lbEzhNhYEDSPoZVpwpaRxZ64jfE9jz+O9f1JH9UUugel947CsqSHjeQyYoyve/xIn8eMLRE+rx2ZQrCAIZIjAV3TfDZgF+0pPjypauTOvmkejoPvoCjSB7GfdZTfRiylIXF9ifaucP+84l/t44TUNKqQlpS6kD16L+Hv/hC+SBOXhaOzvoY25W5Jaj0FLSq4bQWfo4yyqhkzxxxT+AFJn1Xf9SqZC0RaM8r8DOgYnFs07OnGJXu3jw/HMRIhWHQsYFQIaYF6/ncXv34lfWu+EZzPZPzMmd3lwqXN/5Sc4OHWiduOZ0T6FCe+GhYwWkDWF7MXnWiAeJ 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 15:05, Simona Vetter wrote: > On Wed, Jan 29, 2025 at 12:31:14PM +0100, David Hildenbrand wrote: >> 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 ... > > Hm ... also rethinking this all, we don't need a lot of guarantees here. > Anything userspace does that re-elevates the mapcount or otherwise could > starve out make_device_exclusive is I think a case of "don't do that". > > I think the only guarantee we need is that make_device_exclusive succeeds > against other kernel stuff like thp/migration/ksm and all those things, at > least reliably when you retry. And maybe also that concurrent > make_device_exclusive calls don't starve each another but eventually get > the job done (but only if it's the same owner). > >> 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. > > Hm yeah I was a bit confused why this would work at first. But since we > break cow with FOLL_WRITE there shouldn't be any other mappings around. > Therefore relying on the mmu notifier for that mm_struct is enough, and we > don't need to hold the folio_lock in callers. Right; the devil is in the detail here; I looked at the SVM code and it registers an MMU notifier around that code to catch any invalidations that are happening while mapping the page. And it has another one to handle invalidations in general. I did not completely digest why two notifiers are required ... there likely is a good reason. -- Cheers, David / dhildenb