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 5C9A9C02193 for ; Thu, 30 Jan 2025 08:55:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D429A280075; Thu, 30 Jan 2025 03:55:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D19A5280074; Thu, 30 Jan 2025 03:55:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE14B280075; Thu, 30 Jan 2025 03:55:34 -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 A157F280074 for ; Thu, 30 Jan 2025 03:55:34 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 38DC0120B41 for ; Thu, 30 Jan 2025 08:55:34 +0000 (UTC) X-FDA: 83063509788.17.476F1C0 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) by imf16.hostedemail.com (Postfix) with ESMTP id 2EE9E18000E for ; Thu, 30 Jan 2025 08:55:31 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=ffwll.ch header.s=google header.b="GwEoE5/2"; spf=none (imf16.hostedemail.com: domain of simona.vetter@ffwll.ch has no SPF policy when checking 209.85.128.51) 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=1738227332; 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=hIslw6ufQaCzkTDQo2Y7rnWGVXJFTGegVErKgzJce74=; b=oli2TTPYZqQVNzMiMjyHpuxqpAbHukSsll8Ityc1AmgTBYWFFMrZlmQwzuVrrXLgyb1AWy fjOv93u6G2zdwV4QrPtnK6LiYKuq3iEdW2zomYIBJPgc+IjXxh25xlebzkwHSu85oLe3dX wc4vAS0cG0N3fsvTuwqLRQIHX8f7FF8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=ffwll.ch header.s=google header.b="GwEoE5/2"; spf=none (imf16.hostedemail.com: domain of simona.vetter@ffwll.ch has no SPF policy when checking 209.85.128.51) smtp.mailfrom=simona.vetter@ffwll.ch; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738227332; a=rsa-sha256; cv=none; b=PGq3tjsEw0PUDc3uH3NHTkaSGk2Ol40njtZaLjkvHMiQfX0oLveaDlpUhUZx2WHa8s8gNx aoCLc8jJos+WTxk4/SUOJJ2zHb+pLiXA4UNj7+eA8IgzLDS59DIH7j9Q7eX9KcJk2+LjvB 6xLguPFEayisNNNEW3deonX6B6arPbI= Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4361c705434so3413195e9.3 for ; Thu, 30 Jan 2025 00:55:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; t=1738227330; x=1738832130; 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=hIslw6ufQaCzkTDQo2Y7rnWGVXJFTGegVErKgzJce74=; b=GwEoE5/2HLqzv3pymKVxohJDPdYpiy8h5eZAWNFiORRw1SHcWF261q1ONGD590quii FdEZGKoayGWsGEim8ttOdkj3xtaiCfmU6rJO3VatDUzLkWg7D7P4CDVY4ZQ+nTWgruou mS17sNXYBwG7Hvn3wrbtPkWM5R6bmH2xI+wCA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738227330; x=1738832130; 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=hIslw6ufQaCzkTDQo2Y7rnWGVXJFTGegVErKgzJce74=; b=RYP+NPaKhIjljCRvbGrYsuui1TsKQ9ymDMQlI5U3Np4AItpt3i1tUi0Na2fQL7+Stc jLmh01+D/a96rwkYwxYhUp3L6e43ueDATlnkinbY6mSQ1uPQxH3HfFK0SsrONiKW16ao lRH7G40c+h3svi1csSlvP0alMmbNzHy++uXl1ZlXtc0CNauusmoCt5UAVweIbXfyAIx0 JUcmyVxV+FrHmuOBTtyObdOYK7pnQqg640WIehVvKbLKzJu950K85haAY73VwADM2CXi D75BBLA+D3uf3jvq9QHmxl4/Kk0dNit1P8ouJ9XVCsgj1U4dsYiO7XhpgVkfmQbrGih6 TEDQ== X-Forwarded-Encrypted: i=1; AJvYcCWRfk5nXIckOgEoNnNAFSosGOzUyDNuBozkeVEDUD1XdbU/smR6mWvd6a8o/dpmXn14y2KtvrJ0hA==@kvack.org X-Gm-Message-State: AOJu0YxE0OGtdfFSM6hs3hkhnykI+PMIGDmj6NbLjaYPngHCHusEgEPo KTxP/EZ0eHwjDrIuKpVHWPPexySMFzz1muclM1zFREBxB8vPOdhtnOHV+/nf9Y4= X-Gm-Gg: ASbGncuG9fFG6EZl9mWoBoPBWj4gJqwnoJeHWMd6HM/nOtONZvmc3K6M7zV47FwLP/J SuaXptDwC6+PL3w3yvK1XnTljzthZHPQ7zNNGfgKn8rKOjyuuO+THRXm+bsnev16EfM4X+pEKqY T7ogDXWSoF4Nq7dcRp+DmguW4yvG4tBz/qjQ5Q34egPM+n/2asYNRM8yyLX2Wk/Lo2hbxin4knZ 4ASwtHbJvw5PDMUKmbUI+gJ4pX5TLKIFWmEhZs0bEihzSUMePVCvB+DmC7lIJ65nscMiRRjTQce 3x0T75lLFIjo5lPEU7TbinApalY= X-Google-Smtp-Source: AGHT+IFyLToZ0gdk5jIE+m1EP+tgN9TteTZIvDJwNmgzXNNu1UsMwvchAhqyIMVoSY01MWwiaZ6shA== X-Received: by 2002:a05:600c:138f:b0:434:f1e9:afae with SMTP id 5b1f17b1804b1-438dc3aa785mr46269975e9.1.1738227330367; Thu, 30 Jan 2025 00:55:30 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:57f4:0:5485:d4b2:c087:b497]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-438e244f0d2sm15613675e9.30.2025.01.30.00.55.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2025 00:55:29 -0800 (PST) Date: Thu, 30 Jan 2025 09:55:27 +0100 From: Simona Vetter To: David Hildenbrand Cc: Alistair Popple , "linux-mm@kvack.org" , John Hubbard , nouveau@lists.freedesktop.org, Jason Gunthorpe , DRI Development , Karol Herbst , Lyude Paul , Danilo Krummrich Subject: Re: [Question] Are "device exclusive non-swap entries" / "SVM atomics in Nouveau" still getting used in practice? Message-ID: Mail-Followup-To: David Hildenbrand , Alistair Popple , "linux-mm@kvack.org" , John Hubbard , nouveau@lists.freedesktop.org, Jason Gunthorpe , DRI Development , Karol Herbst , Lyude Paul , Danilo Krummrich References: <8dfe1dd5-f4a7-4b8a-92c7-278519ee4c58@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8dfe1dd5-f4a7-4b8a-92c7-278519ee4c58@redhat.com> X-Operating-System: Linux phenom 6.12.11-amd64 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 2EE9E18000E X-Stat-Signature: b1he4xusoiuigx7qge6698o1ajs47jpo X-HE-Tag: 1738227331-524189 X-HE-Meta: U2FsdGVkX1+GuqewTZ77E33Ua6q/QLo7UDe9z1yDAFh2SlOahF46C/5gdX9Xn5npjgIe4S5kF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Ns5uCiRzZJYwiNrahJaIs+JLuMdQXmlgMCXRv8FV2eo53O5JUUETNUhUSVTgUBhSH9MrAii8fbEDvsNvk9sLX3T5LiMGyTnaYGp03FUyZzBbAq8L8fx+12YQJblKx7WddIyLowFtvDGhGz5XNPBUBj3lmlgxvldnsBs2CX767fxJAz5cXG6QalzWq6R362/WDLS4YvQr/JejutTrTu6fj4OY/n6q/u5w4KsqkVETcyg4ZeF6p1uniw== 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 Wed, Jan 29, 2025 at 05:13:41PM +0100, David Hildenbrand wrote: > 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. At least as a fallback you need a notifier for exactly only the fault, to make sure you don't retry due to spurious invalidations. Otherwise if userspace is hammering an adjacent page from the cpu we would not make forward progress, and that's a correctness issue. Now always inserting a new notifier for every fault in all cases isn't great because it incurs a stall. But I discussed this issue with Thomas Hellstrom a while back and there's a bunch of tricks we could deploy if this ever becomes a performance issue. So this all looks ok to me. -Sima -- Simona Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch