From patchwork Wed Jan 29 11:57:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13953705 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5B5C8C0218D for ; Wed, 29 Jan 2025 11:58:20 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 84F0F10E7F1; Wed, 29 Jan 2025 11:58:17 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="i4V+ikB2"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id A02BC10E7D9 for ; Wed, 29 Jan 2025 11:58:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151892; 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; bh=IvULs1yE7EOmGau/Ggivq4i0htACtoQXWVK4/nOi/M8=; b=i4V+ikB2Gk5mBT1ntGNVcNrB8GGm/iQdKYp3hzkcgqOAZoBvTu2NU4E7txnmdd1a5wJikT npgDh1aaT+RnJVMxs48IAG3iTGUqKhLf0ebcrJCMa+JksyRR/4TaW/0HCsiKoa4pgTTlkm 1s9BVr5u6oxTsLASo8Vsa++UtvJYUvw= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-8-mBQMddaePdSfIF50QVjJgw-1; Wed, 29 Jan 2025 06:58:11 -0500 X-MC-Unique: mBQMddaePdSfIF50QVjJgw-1 X-Mimecast-MFC-AGG-ID: mBQMddaePdSfIF50QVjJgw Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4361a8fc3bdso32545265e9.2 for ; Wed, 29 Jan 2025 03:58:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738151890; x=1738756690; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IvULs1yE7EOmGau/Ggivq4i0htACtoQXWVK4/nOi/M8=; b=AuQ/khZW56kIgG343AefVxTDiDLkWy+fYg4ffw9tH6golD0sabQXIMNT2qsyUdGL7J HIj02xJgCLzzSI8GkfB/Xxpf1lNy05KFHOfF8ric2qNGo/muC2T9aGX7w9U4CEicr7cu DcMnuMffXiXuqAygWy9UMx/53IwZAbwxBkNRnYhOQkF+S84C0K6vGDUUApqD2BhoE8Ie Unmzjn9q6kOgBHuk9TWSSHXeUikGCBTVvnGayXGLWAZRXgOQQ/K8gDKkoXVg5QbntFBm Bt9FD8MgMz/o+78uUwbTjltr3fa1mzc37PrYQnYzKNDAtBzBfpzrE/GV9jsj5FmXLK7R 9Abg== X-Forwarded-Encrypted: i=1; AJvYcCUMZlSRK0XFepgKLnMMtnGU+IFs4u+pd8Yf1WoIRpf4CIbkCOt/HsoLdw43weY24aCLu5P/tr3rUA4=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yy+wal2ntNblRbrNhJaQ9mr/ehtQMkmjI8YXwXDj7FhoSAVYruq 520yMt482bHjdwxnLKDgNebJMdAFhHsySOPC0lHVL6Ht/o/t1NxVU9cPxpUdHS2PFQotkSfroXR dSjZ3tGGlzEWtvHGC+Eu/ERU1Rz9YQmQ/FRIEJTI2LofOwROM/qpYJOvPNwlWYnZA4g== X-Gm-Gg: ASbGncvE1aOHOB7ShyNt25EL26bwa8UFHiEeyHwCKOOznWb+7o/PCxzsbtdHMVAEm1F ZgskhGDq5uGTfSioWxsrZiNT2KKbFQjJZiZK/8hcUVn0C3y19LDrQw9WvX54eZvAjrfQ+teKStw MGfkuz/PgMXqE7sNsT0abaf/3Tw1gLCrm9q9tv4z5dG3I/CSt5kwZws4JNKUSYKcDm4tDW221vn l8CdzLWqkRm1wP4NFXcTQkqRYti811lMmuxrSJOxOE3I7fHWf+KtSn6yMXk1xBho50FO47goyb3 dWT6L7qdYvuegk35s+dYf/DbpG4pJynuYT9mk8JWSE9hFT35tCqzStKl+5clXKVZCw== X-Received: by 2002:a05:600c:5252:b0:431:6153:a258 with SMTP id 5b1f17b1804b1-438dc3ca802mr23864075e9.13.1738151890432; Wed, 29 Jan 2025 03:58:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IEqM74m4ZV9IsBXW9SIKcsLoOPVNY/fxsU8Okkl94/EsEQHDya1iNUyBhUuUArF4/LgH5tlYg== X-Received: by 2002:a05:600c:5252:b0:431:6153:a258 with SMTP id 5b1f17b1804b1-438dc3ca802mr23863905e9.13.1738151890101; Wed, 29 Jan 2025 03:58:10 -0800 (PST) Received: from localhost (p200300cbc7053b0064b867195794bf13.dip0.t-ipconnect.de. [2003:cb:c705:3b00:64b8:6719:5794:bf13]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-438dcc12f88sm20420515e9.2.2025.01.29.03.58.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:58:08 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, nouveau@lists.freedesktop.org, David Hildenbrand , Andrew Morton , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Jonathan Corbet , Alex Shi , Yanteng Si , Karol Herbst , Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pasha Tatashin , Peter Xu , Alistair Popple , Jason Gunthorpe Subject: [PATCH v1 1/4] lib/test_hmm: make dmirror_atomic_map() consume a single page Date: Wed, 29 Jan 2025 12:57:59 +0100 Message-ID: <20250129115803.2084769-2-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250129115803.2084769-1-david@redhat.com> References: <20250129115803.2084769-1-david@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: hpmg-MlxmI1SfPJxbScNi_YpjVRbCF3ZNCkChkCI9Sg_1738151890 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The caller now always passes a single page; let's simplify, and return "0" on success. Signed-off-by: David Hildenbrand Reviewed-by: Alistair Popple --- lib/test_hmm.c | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 9e1b07a227a3..1c0a58279db9 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -706,34 +706,23 @@ static int dmirror_check_atomic(struct dmirror *dmirror, unsigned long start, return 0; } -static int dmirror_atomic_map(unsigned long start, unsigned long end, - struct page **pages, struct dmirror *dmirror) +static int dmirror_atomic_map(unsigned long addr, struct page *page, + struct dmirror *dmirror) { - unsigned long pfn, mapped = 0; - int i; + void *entry; /* Map the migrated pages into the device's page tables. */ mutex_lock(&dmirror->mutex); - for (i = 0, pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++, i++) { - void *entry; - - if (!pages[i]) - continue; - - entry = pages[i]; - entry = xa_tag_pointer(entry, DPT_XA_TAG_ATOMIC); - entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC); - if (xa_is_err(entry)) { - mutex_unlock(&dmirror->mutex); - return xa_err(entry); - } - - mapped++; + entry = xa_tag_pointer(page, DPT_XA_TAG_ATOMIC); + entry = xa_store(&dmirror->pt, addr >> PAGE_SHIFT, entry, GFP_ATOMIC); + if (xa_is_err(entry)) { + mutex_unlock(&dmirror->mutex); + return xa_err(entry); } mutex_unlock(&dmirror->mutex); - return mapped; + return 0; } static int dmirror_migrate_finalize_and_map(struct migrate_vma *args, @@ -803,9 +792,7 @@ static int dmirror_exclusive(struct dmirror *dmirror, break; } - ret = dmirror_atomic_map(addr, addr + PAGE_SIZE, &page, dmirror); - if (!ret) - ret = -EBUSY; + ret = dmirror_atomic_map(addr, page, dmirror); folio_unlock(folio); folio_put(folio); From patchwork Wed Jan 29 11:58:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13953706 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6D33AC02195 for ; Wed, 29 Jan 2025 11:58:21 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9F89810E7E3; Wed, 29 Jan 2025 11:58:17 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="gyDacMND"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1EE8410E7F0 for ; Wed, 29 Jan 2025 11:58:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151895; 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; bh=b7tz66Q1mh6b9biGlGMq1Dqed0aye0/ijzbhQ2yLKo0=; b=gyDacMND/LhZRFD7mSoTy71Jwlz0dNSoMnvaa/EL86MuK5chqkPYGpwIXNdXKbjCvuCovx Gkxy8DbQWMGeiLIeyZKNJv8UDEqBQnaJXh6QFMPoDG9RDuaek4AoOl3TqwGFsUzyQGXPVZ PLwp9lFZ8AGepAVyHKfDdJ7U0WR4fV0= 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-328-ux0plfAsPTmv1RfZJpxrKg-1; Wed, 29 Jan 2025 06:58:14 -0500 X-MC-Unique: ux0plfAsPTmv1RfZJpxrKg-1 X-Mimecast-MFC-AGG-ID: ux0plfAsPTmv1RfZJpxrKg Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-38bee9ae3b7so4422973f8f.1 for ; Wed, 29 Jan 2025 03:58:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738151892; x=1738756692; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=b7tz66Q1mh6b9biGlGMq1Dqed0aye0/ijzbhQ2yLKo0=; b=jY4bBc0oxPf24moC9Hkho0KSePQWrXZFezvgU9qqg+dXtHPNcHAqQD/77NnI/uU6pQ 7WKd3vRluxcV1lnR7wrTQgZhd2DJ/IOiJkTAra1DhspBmeYvi0Jpl1Prhw/lHtAvptbj LIsCF+lDVYgGUih+DWei2unEkOQgXEWPhyHAwKINbs9qmFlrb3zHa7USs1ViljzkD2Ns GiSTiisEGWATRPReOBJJGJ1yWN+4qljX9Tne2ziSQurAtM1BF5G43rPj4t8aw1RgB0x5 YtEN/wRDTt3Q1QMMThBDnVnKhjwwVy8EUhwyH/zfkY4vhFKmsLzB2Ma/cgXKezs9muNV /TIA== X-Forwarded-Encrypted: i=1; AJvYcCX+Q65qgjuVd9bPm3jP6Oj+f8kzwT/sBOwxEASdRHXl6DALJriuArdHEtuwJsWgDI+jWuyI0kr4i8c=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwFmbjnPPTTO+hWl72iP4CqpVW/u9SKLYvfoinodWXQ35cNRYiH 82/EX6fz+j7Ey1gLYYQOZJEOyIXbFiH23UURNH540avjNAq6+MLHcd64z1t79SIHeuQjbpqW+r3 X8ACEW3sjEKEKLa/JyjWSF9JHM7lhq5PghAmKzOeIWDzN+5KjGMkkE19UW1GPArxoKzinjTH6ge xW X-Gm-Gg: ASbGncubP7DzcH4Unu7SVBiVQr8QoP/IIq/itV2Hly43wOr1bmnCB6hNiHnp7WuwKeL /sL8TZi1jU0VaHbQzBI2jXwhk1vdocwi+9qFh4+vGW+JLr3VEDMG/t5uwPo+akD/pEoaGJ68Yf+ IUykouaMytVdtoIcdNKB+y+4lpX9H3A6BIwJ9VcObWqCm1w1uW6ow3nAIf6HRV8Zaz1xQXgnKJp DvJsEMAbwDKy6AGw4ltIeK4HF4lrji8LTgb43ET98JkxUG3eFQhgNBGE8/grnlXxwPsQxEH7hiL OyeDOR5XQZDUSV2ImGykpzoao6df7/fD/W78PYXIHTxaJPEjRjdg7kVWe2d9HDpS/A== X-Received: by 2002:a5d:5986:0:b0:38b:e1b3:16dc with SMTP id ffacd0b85a97d-38c520b0b02mr2139370f8f.50.1738151892500; Wed, 29 Jan 2025 03:58:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IFkkUWuvL/Qi4Q+ZXMjYaW+MERJXy55kjeOBJGiA5aHVhI3t0Ux4qagc7L275aZnwYNWB5ItA== X-Received: by 2002:a5d:5986:0:b0:38b:e1b3:16dc with SMTP id ffacd0b85a97d-38c520b0b02mr2139337f8f.50.1738151892123; Wed, 29 Jan 2025 03:58:12 -0800 (PST) Received: from localhost (p200300cbc7053b0064b867195794bf13.dip0.t-ipconnect.de. [2003:cb:c705:3b00:64b8:6719:5794:bf13]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38c2a1bb057sm17068560f8f.62.2025.01.29.03.58.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:58:11 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, nouveau@lists.freedesktop.org, David Hildenbrand , Andrew Morton , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Jonathan Corbet , Alex Shi , Yanteng Si , Karol Herbst , Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pasha Tatashin , Peter Xu , Alistair Popple , Jason Gunthorpe Subject: [PATCH v1 2/4] mm/mmu_notifier: drop owner from MMU_NOTIFY_EXCLUSIVE Date: Wed, 29 Jan 2025 12:58:00 +0100 Message-ID: <20250129115803.2084769-3-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250129115803.2084769-1-david@redhat.com> References: <20250129115803.2084769-1-david@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 7oJmHLDxwW5o6cKeZapaS8cq1oTYKo766ZXVOhGCLhg_1738151893 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" We no longer get a MMU_NOTIFY_EXCLUSIVE on conversion with the owner set that one has to filter out: if there already *is* a device-exclusive entry (e.g., other device, we don't have that information), GUP will convert it back to an ordinary PTE and notify via remove_device_exclusive_entry(). Signed-off-by: David Hildenbrand --- drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +----- include/linux/mmu_notifier.h | 4 +--- include/linux/rmap.h | 2 +- lib/test_hmm.c | 2 +- mm/rmap.c | 3 +-- 5 files changed, 5 insertions(+), 12 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_svm.c b/drivers/gpu/drm/nouveau/nouveau_svm.c index 39e3740980bb..4758fee182b4 100644 --- a/drivers/gpu/drm/nouveau/nouveau_svm.c +++ b/drivers/gpu/drm/nouveau/nouveau_svm.c @@ -510,10 +510,6 @@ static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni, struct svm_notifier *sn = container_of(mni, struct svm_notifier, notifier); - if (range->event == MMU_NOTIFY_EXCLUSIVE && - range->owner == sn->svmm->vmm->cli->drm->dev) - return true; - /* * serializes the update to mni->invalidate_seq done by caller and * prevents invalidation of the PTE from progressing while HW is being @@ -609,7 +605,7 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm, notifier_seq = mmu_interval_read_begin(¬ifier->notifier); mmap_read_lock(mm); - page = make_device_exclusive(mm, start, drm->dev, &folio); + page = make_device_exclusive(mm, start, &folio); mmap_read_unlock(mm); if (IS_ERR(page)) { ret = -EINVAL; diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h index d4e714661826..bac2385099dd 100644 --- a/include/linux/mmu_notifier.h +++ b/include/linux/mmu_notifier.h @@ -44,9 +44,7 @@ struct mmu_interval_notifier; * owner field matches the driver's device private pgmap owner. * * @MMU_NOTIFY_EXCLUSIVE: to signal a device driver that the device will no - * longer have exclusive access to the page. When sent during creation of an - * exclusive range the owner will be initialised to the value provided by the - * caller of make_device_exclusive(), otherwise the owner will be NULL. + * longer have exclusive access to the page. */ enum mmu_notifier_event { MMU_NOTIFY_UNMAP = 0, diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 86425d42c1a9..3b216b91d2e5 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -664,7 +664,7 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags); void try_to_unmap(struct folio *, enum ttu_flags flags); struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr, - void *owner, struct folio **foliop); + struct folio **foliop); /* Avoid racy checks */ #define PVMW_SYNC (1 << 0) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 1c0a58279db9..8520c1d1b21b 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -786,7 +786,7 @@ static int dmirror_exclusive(struct dmirror *dmirror, struct folio *folio; struct page *page; - page = make_device_exclusive(mm, addr, &folio, NULL); + page = make_device_exclusive(mm, addr, &folio); if (IS_ERR(page)) { ret = PTR_ERR(page); break; diff --git a/mm/rmap.c b/mm/rmap.c index 4acc9f6d743a..d99dbf59adc6 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2397,7 +2397,6 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) * make_device_exclusive() - Mark an address for exclusive use by a device * @mm: mm_struct of associated target process * @addr: the virtual address to mark for exclusive device access - * @owner: passed to MMU_NOTIFY_EXCLUSIVE range notifier to allow filtering * @foliop: folio pointer will be stored here on success. * * This function looks up the page mapped at the given address, grabs a @@ -2421,7 +2420,7 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) * Returns: pointer to mapped page on success, otherwise a negative error. */ struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr, - void *owner, struct folio **foliop) + struct folio **foliop) { struct folio *folio, *fw_folio; struct vm_area_struct *vma; From patchwork Wed Jan 29 11:58:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13953707 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ED9DFC02196 for ; Wed, 29 Jan 2025 11:58:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2DD9D10E7DB; Wed, 29 Jan 2025 11:58:22 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="AUfGPf6b"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id F276910E801 for ; Wed, 29 Jan 2025 11:58:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151898; 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; bh=n2/u4hMaC1Y/nn2Uh1F1xHtHTh10RqLxBEMK6rVGx5s=; b=AUfGPf6b3rHxYlgicVPzaQFk6ENa9AGqO/Z5gQPByNknmJbJBzywyyI02hCyAa13dmVie7 /nsTIxTRoQql2F/LUg2uOAn0HD4szoFxY1syboP904XOulbXhUUUBE5Knz2HcRjS3O2Fth tESeuMEMNzKErlIRXGfJfm0io6lg79A= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-459-AN5kZr62NCeJHU_K3iRPdQ-1; Wed, 29 Jan 2025 06:58:17 -0500 X-MC-Unique: AN5kZr62NCeJHU_K3iRPdQ-1 X-Mimecast-MFC-AGG-ID: AN5kZr62NCeJHU_K3iRPdQ Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-38634103b0dso3587122f8f.2 for ; Wed, 29 Jan 2025 03:58:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738151896; x=1738756696; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n2/u4hMaC1Y/nn2Uh1F1xHtHTh10RqLxBEMK6rVGx5s=; b=UTZFylbyWpJ3jHuSFyIrqBouQB9tiq1/PdoJ4nb8kyqLrmJaUuYrc3gbYFUFyQJ7Oi k/+7nL8r2ECbMelT8F7ftAO2nMOUzpl3x8y/vryCZ5F603G9i360NaPe6rZdG+F/IMyb 81ELuyQ/np5YbOF0f6xiX/z8C9bQHqmqUKKmyVkz/wMw2HPkWzyXu5KCQcdR/SfHcSO+ vjNpiwoZlOK+Sje1lpUN8QcuG0lRRhkNogX7u4GcLiF/lUxpabgVcrjhSDESGAEX20j9 cMxGaF75IlE6pv28QAJbf3c+QlCxwZbqbmd2m5Dnwvp185YwjlrHtcpjYYTLIjNjQk2M VG/g== X-Forwarded-Encrypted: i=1; AJvYcCV8iW6vy0vPyVwB0JIXg5zJML6z/H1JbQi/78EX+LI5iCc3SrOx3kS18TzdPvrPBdYxTpXSjR/b2XM=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yw+HT9pseUp1NwMCQpicgpqBbz4Ia2bekm3+wCbsaM6f6Qua6eW n+VMaEyPpH3CEwO4gyhJ1gTy14Sm3KC0ZVsOX/0RhS9CPOOYN0ZDvYoNV8oNB1ExWEWEhUdTJvB 8ZzxBLiCekutAmyT+zxGKUmd6hx/cLASCqKQd3DbEe1QZujZLEVh/3TWti8rJ5kR/rw== X-Gm-Gg: ASbGncuMx8L0FWgJ4br6/bvLo4ebgzWT6Vusk9l7R4mDDGdeD7Yc1TbmkLHETpqelMu GohY36yULWjmA8wuuOTNXGG0Pn0C3Tl6xsiOf5fi2QuaX/RD8OchYWa70jx1Ist0NKjyU0PdAfc 41RcNJMTm75hMLD9JCBQGOs+efcTj/yq+HGNpStKFOJN/Ap6qix6jINJ3v/9NlSx91qMB7TRsVU fZu7LXThZsdYu+x+tCkNB0mvAQh2hEi8L85TB59R6FCyWqtN2k1CBdHeaCZ+BnNOFNrPY5nwlON yuLn7jX4W5IvUOPNkRgOcNbWFUJGpGY+vlNAHb40nyCHR+3Rqd978v3QGi9tDg8NwQ== X-Received: by 2002:a05:6000:2c8:b0:38a:88f8:aac8 with SMTP id ffacd0b85a97d-38c52096289mr2025151f8f.54.1738151895827; Wed, 29 Jan 2025 03:58:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IEON5WkoaxoRH3qtkNX0vn2oqksdlJtc4nZs+NotAXeIJk4vQvUCQu3rl+CihtRV9gH+gpH8w== X-Received: by 2002:a05:6000:2c8:b0:38a:88f8:aac8 with SMTP id ffacd0b85a97d-38c52096289mr2025126f8f.54.1738151895421; Wed, 29 Jan 2025 03:58:15 -0800 (PST) Received: from localhost (p200300cbc7053b0064b867195794bf13.dip0.t-ipconnect.de. [2003:cb:c705:3b00:64b8:6719:5794:bf13]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38c2a1764e9sm17070487f8f.17.2025.01.29.03.58.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:58:14 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, nouveau@lists.freedesktop.org, David Hildenbrand , Andrew Morton , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Jonathan Corbet , Alex Shi , Yanteng Si , Karol Herbst , Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pasha Tatashin , Peter Xu , Alistair Popple , Jason Gunthorpe Subject: [PATCH v1 3/4] mm/memory: pass folio and pte to restore_exclusive_pte() Date: Wed, 29 Jan 2025 12:58:01 +0100 Message-ID: <20250129115803.2084769-4-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250129115803.2084769-1-david@redhat.com> References: <20250129115803.2084769-1-david@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Se36MIUerzsZ2ZlLbjvJA1tJPldjDsMzHzTeTj8F27A_1738151896 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Let's pass the folio and the pte to restore_exclusive_pte(), so we can avoid repeated page_folio() and ptep_get(). To do that, pass the pte to try_restore_exclusive_pte() and use a folio in there already. While at it, just avoid the "swp_entry_t entry" variable in try_restore_exclusive_pte() and add a folio-locked check to restore_exclusive_pte(). Signed-off-by: David Hildenbrand Reviewed-by: Alistair Popple --- mm/memory.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index cd689cd8a7c8..46956994aaff 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -719,14 +719,13 @@ struct folio *vm_normal_folio_pmd(struct vm_area_struct *vma, #endif static void restore_exclusive_pte(struct vm_area_struct *vma, - struct page *page, unsigned long address, - pte_t *ptep) + struct folio *folio, struct page *page, unsigned long address, + pte_t *ptep, pte_t orig_pte) { - struct folio *folio = page_folio(page); - pte_t orig_pte; pte_t pte; - orig_pte = ptep_get(ptep); + VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + pte = pte_mkold(mk_pte(page, READ_ONCE(vma->vm_page_prot))); if (pte_swp_soft_dirty(orig_pte)) pte = pte_mksoft_dirty(pte); @@ -756,16 +755,15 @@ static void restore_exclusive_pte(struct vm_area_struct *vma, * Tries to restore an exclusive pte if the page lock can be acquired without * sleeping. */ -static int -try_restore_exclusive_pte(pte_t *src_pte, struct vm_area_struct *vma, - unsigned long addr) +static int try_restore_exclusive_pte(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, pte_t orig_pte) { - swp_entry_t entry = pte_to_swp_entry(ptep_get(src_pte)); - struct page *page = pfn_swap_entry_to_page(entry); + struct page *page = pfn_swap_entry_to_page(pte_to_swp_entry(orig_pte)); + struct folio *folio = page_folio(page); - if (trylock_page(page)) { - restore_exclusive_pte(vma, page, addr, src_pte); - unlock_page(page); + if (folio_trylock(folio)) { + restore_exclusive_pte(vma, folio, page, addr, ptep, orig_pte); + folio_unlock(folio); return 0; } @@ -871,7 +869,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, * (ie. COW) mappings. */ VM_BUG_ON(!is_cow_mapping(src_vma->vm_flags)); - if (try_restore_exclusive_pte(src_pte, src_vma, addr)) + if (try_restore_exclusive_pte(src_vma, addr, src_pte, orig_pte)) return -EBUSY; return -ENOENT; } else if (is_pte_marker_entry(entry)) { @@ -3979,7 +3977,8 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf) vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl); if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) - restore_exclusive_pte(vma, vmf->page, vmf->address, vmf->pte); + restore_exclusive_pte(vma, folio, vmf->page, vmf->address, + vmf->pte, vmf->orig_pte); if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); From patchwork Wed Jan 29 11:58:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13953708 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 405E7C0218D for ; Wed, 29 Jan 2025 11:58:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B952E10E7D7; Wed, 29 Jan 2025 11:58:27 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="YAHcztfy"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7E99C10E7DB for ; Wed, 29 Jan 2025 11:58:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151900; 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; bh=FOqcQk5sbfQYMqiwFwVZu0o4Vgt4WnJhwl3OPDX2fpI=; b=YAHcztfyQgRI213DyDuuD9Xe8Wsi6bt2l8F4bXnlyLIb4gjzpLgy7LIykkuQUHTFPQIpVe WbVV+rkuABPb3UO8m5N4tPE1BfldGKngpC2EKrzxCvM1Bv62DFi0E8IY1OL+itK2kUm0F4 I69Q/AfokvqlKCxnEimsRq2L0rqonPI= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-680-MgIZOryTPIy_zJx77YrMCQ-1; Wed, 29 Jan 2025 06:58:19 -0500 X-MC-Unique: MgIZOryTPIy_zJx77YrMCQ-1 X-Mimecast-MFC-AGG-ID: MgIZOryTPIy_zJx77YrMCQ Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4388eee7073so3083205e9.0 for ; Wed, 29 Jan 2025 03:58:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738151898; x=1738756698; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FOqcQk5sbfQYMqiwFwVZu0o4Vgt4WnJhwl3OPDX2fpI=; b=kofpthOOtK6dMibtCdv+3eHk5iINbUz6/NqjXnb/gXAyWKILljVJcKoNPEnNKGRNTe S5zmqy+uywG/xssC68jmO26QRtcvMpVrLuQfwqF5VeRrTqfpdzCxZaSVG3xqvWwg+a4A heI5NfDGZb8CMGkLKUTegaDGaQKjupQsDFzKim45LLWWTnAt2BJY3Eo3hWc31SP0m9bK NnMiV9T8Xfg/V8LGTOQU14b7Sujsk1BfTayEELlRDyyCkOAq58pNZStgM0mL64as7l+Z 7HSPB6ZpZy3LtcrViNoGm0yVHxhqH+gI2LC/0X84h7g+xwBlpsIhd8BU5585GeavzNZ7 c/Zg== X-Forwarded-Encrypted: i=1; AJvYcCWHt2tpHyzKCp670d4RcQzNjugafliobYPRXDTV6w+uFFfRK+ew3rpmWZXQgJbh2/vm/xq/8jMFvmw=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwwFWr8GZbku7PVNz17EHSH+Vv61NsFUslGWSuyFJSdSIXKELl+ e4O23D5kTuOmkUX/QLJjAXQs326l1M2fAY6hCM8x4qX13Iv6fI0jM93SOe6htyNSbVwoiXKSRJ5 LlbxCbX+wZ1QFuuRjxpdf1JI+yvxPHEJ2WAkONTXOUXU/qHSYlj9T20zduh8faEcUDg== X-Gm-Gg: ASbGncvwGk1Xm7uXl13RPGSUOhXb+ef/WMwpPDSkbb1t283NN2dxEMGqFo1+vxwyNHa SK3eXZvGNRtAzWbNpoj4d4fVVPVsZWKt/7pmQNsEYPux2OmJfF2qYj4/aAAJjHewrpENE0lnhiq FBeJW6w/kHOmTZDIbBxqQbg7g3XzrANpVx07HMW3JXi9e2ttYe2qWn2VPQcNOzjs77IbfJ2s025 vAu/gAVFq+Ugi6ra3sX9Td2YqkmpE9ZUyzJNX/389FwEVmOFPUJQaCTk337OJJR8BSlXBy8byyy sbz+CXZbbQn9eCMl7kGLNCqBxznQGq57bdNcFxXlNaTQUY/zBpRh52Eg9b2r9gpIbA== X-Received: by 2002:a05:600c:3d87:b0:436:1b0b:2633 with SMTP id 5b1f17b1804b1-438dbe9a124mr27335865e9.9.1738151898334; Wed, 29 Jan 2025 03:58:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IEUOJXNPPQ6xLco9EGZUl9hRUezUN+y2Deyf0uuUDDMY6E4w4fKpt4O5zyjSgxt4LyQtnHPOg== X-Received: by 2002:a05:600c:3d87:b0:436:1b0b:2633 with SMTP id 5b1f17b1804b1-438dbe9a124mr27335535e9.9.1738151897999; Wed, 29 Jan 2025 03:58:17 -0800 (PST) Received: from localhost (p200300cbc7053b0064b867195794bf13.dip0.t-ipconnect.de. [2003:cb:c705:3b00:64b8:6719:5794:bf13]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-438dcc33e04sm20207605e9.29.2025.01.29.03.58.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:58:17 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, nouveau@lists.freedesktop.org, David Hildenbrand , Andrew Morton , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Jonathan Corbet , Alex Shi , Yanteng Si , Karol Herbst , Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pasha Tatashin , Peter Xu , Alistair Popple , Jason Gunthorpe Subject: [PATCH v1 4/4] mm/memory: document restore_exclusive_pte() Date: Wed, 29 Jan 2025 12:58:02 +0100 Message-ID: <20250129115803.2084769-5-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250129115803.2084769-1-david@redhat.com> References: <20250129115803.2084769-1-david@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: uSQng3L8DQ1adRViS5pVSq1FnwjTivzp3hvqlwG_8TI_1738151898 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 --- mm/memory.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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. + */ static void restore_exclusive_pte(struct vm_area_struct *vma, struct folio *folio, struct page *page, unsigned long address, pte_t *ptep, pte_t orig_pte)