From patchwork Wed Jan 29 11:54:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13953682 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 8F3AAC0218D for ; Wed, 29 Jan 2025 11:55:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 17E2328002E; Wed, 29 Jan 2025 06:55:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 12C82280025; Wed, 29 Jan 2025 06:55:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE77028002E; Wed, 29 Jan 2025 06:55:01 -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 C753D280025 for ; Wed, 29 Jan 2025 06:55:01 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D4DF7120DA6 for ; Wed, 29 Jan 2025 11:54:29 +0000 (UTC) X-FDA: 83060331858.04.1D4961F 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 8BC094000F for ; Wed, 29 Jan 2025 11:54:27 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=N1nw3rxa; spf=pass (imf01.hostedemail.com: domain of dhildenb@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhildenb@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=1738151667; 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=FbpRkzZA4TilYwHsov5kO/PS96AeznJfw9pJaA8wpMg=; b=BBczGH5Sq+SJ5HGBBcruTCeyziaF3JNRZU8uMyPSmnekcFv4iOP2zmiqMeHwlOhblXYUyo 3XNuEZ7HxFHgi50nY2AlWbq+4yOQGlucz1QVOBuCQAQf0zMh2JfnwlyAWsvMcmU8rl9fif HVcvUJoj/+jO02Wdb90hdOQ6frO21w4= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=N1nw3rxa; spf=pass (imf01.hostedemail.com: domain of dhildenb@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhildenb@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738151667; a=rsa-sha256; cv=none; b=CNRy5SaD11CAGQFE8Oinmen75ZKQNcx1vnaUzpWOm3PNcgLlGBeOrGxLOvnU0DJIJow6+/ cqPtnc/pDQOX7C/JfYc0EW7CSevL8yVNNQyGb8BsG8FuF4q3oIwvFScS2B6TvMkDE4ctS/ 9ByZmTOKF3r1yzTmuth1gCTb+YpM4VM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738151666; 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=FbpRkzZA4TilYwHsov5kO/PS96AeznJfw9pJaA8wpMg=; b=N1nw3rxaCjaMMKo5m7OvbxQYP1a+kPUcfXjWPQguOW9x+z6acMCIdpuCUDGFiMnF3DGQsJ CPlQmBO7pRLrIobPOAkbWVxfgXhn+62N5OU0GCgt2Spf7FeiPGA6mHVOlSEljcY5zt9D0n BqTXXfoYGnJoY1saCfLlCzptlmjBzoo= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-115-HZZAvdDPMMme004xLJow8g-1; Wed, 29 Jan 2025 06:54:25 -0500 X-MC-Unique: HZZAvdDPMMme004xLJow8g-1 X-Mimecast-MFC-AGG-ID: HZZAvdDPMMme004xLJow8g Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-38bee9ae3b7so4420936f8f.1 for ; Wed, 29 Jan 2025 03:54:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738151664; x=1738756464; 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=FbpRkzZA4TilYwHsov5kO/PS96AeznJfw9pJaA8wpMg=; b=CmdIEGf0Abqupw3jFSJmDeHVxHkwtpqm90i3Q4hHqSil+auEK37W27ZfJ7ZcUnuUIV EuZ9VoxsoY2YhVk5Wl06fvcu4QJgYdGOh4M1gXwtcYYdSNMX5ik5zueVXiP51vokIjy7 V/zvCbp7Nkxd/DCtslfmz2w2pKsR1PCJfS49joTTFVoy5ONWsXBCpBOgrRMDiXt7y0l9 kHh4GzBJzRu5v3NLUlAZotSNfnug1tXiMF0SY9mxheK4rzNt5rgWdAturxFIHTMPe6at N+aiC7yAAyDQpCG/nxx6RfqArfhZdLt2bPCHoXbuGcBN+zjleXahKqXJRPvwtRIU8z4N Sraw== X-Forwarded-Encrypted: i=1; AJvYcCXH83hvTIczZXouNKXQVcYr9t05OsNnZaO7k+9GXnHC6o0IQbmzk0LbQHedNLsrZCmGZub2O/jTLg==@kvack.org X-Gm-Message-State: AOJu0YyIzd29WnzEAOoaJo5YBpOagf9jm69oVQKUUCftrL6BGoZsoNIb Wc+NHdr8ZzNhHPaw3gpoOOeKoO3sQQqxdNe7DoFTUIixuzk9awxDqeL0dGrgsWSoXajfitMdz8f YDgjVE75F8SFPSjD+P8NNEXAuo4bhwvMTdeIQMxA5krhoqmNu X-Gm-Gg: ASbGncsIxiG1GB6B+CKDUJeo5LASiY19WrF+YZLzX15dXu7Io/A+jj/z4E/IuicEsvE WmIK7Un93aduu3rqY//9qoGzVejnXPcY/DYfo+bBHCihCu/F+hSxy7en2UWXUt201/f1+g/CzBR KTqkkgquWVB8yYTv/OCHN9UNXrhjkUsgip352du7hdkElzAsVIc+WtxQ7LLjRIsleHwN6mxT95C gcBxADpQc8lcnk83FQhwCjG3pXK8u16poB3n4xilum6r8AeHXJ3zqlaoxRqr859pLdAZJ5r3uGl HSwOrsSrh7r5CpWE2bxY17w9bVczIZu40152EcAiMDdnHQcBbv4ka5+h8GkTWsqyDg== X-Received: by 2002:a5d:64c3:0:b0:385:e5d8:2bea with SMTP id ffacd0b85a97d-38c519460aemr2346430f8f.20.1738151663853; Wed, 29 Jan 2025 03:54:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IECXzKNlO/KxhDgtZSrbOQmu7i91GFToVYgJKpoIoUvo8DlvKFDTSa6/lsKfWQw3fetIFbvYQ== X-Received: by 2002:a5d:64c3:0:b0:385:e5d8:2bea with SMTP id ffacd0b85a97d-38c519460aemr2346391f8f.20.1738151663351; Wed, 29 Jan 2025 03:54:23 -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-438dcc27130sm20111455e9.16.2025.01.29.03.54.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Jan 2025 03:54:22 -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 03/12] mm/rmap: convert make_device_exclusive_range() to make_device_exclusive() Date: Wed, 29 Jan 2025 12:54:01 +0100 Message-ID: <20250129115411.2077152-4-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250129115411.2077152-1-david@redhat.com> References: <20250129115411.2077152-1-david@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: yRj55y7_mbWhT0SZN8GVgeHk5fQ5TXeiQl8dYSc00g0_1738151664 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8BC094000F X-Stat-Signature: 15zuh8n41z7n9ga6aky8r8t7x9998xhi X-Rspam-User: X-HE-Tag: 1738151667-977916 X-HE-Meta: U2FsdGVkX1/5iY4jYl8+bMERy9msFjsft1zxr5sGgYHhonjIZHJYc8LAoTCyzJRbiVhsNW0Pd0jgtl25xJRYLFoWeY0vxooLamPoRMzW6T8w5s6WkOueLrdaEPKSY7jaLMS+R3naoJTGoD6lxgZ3kjZUb5DOoTa50131a5w/sW+hoc3wd7errYab30kvJexczwGKqdfqvTZPJdXMo17P6/ja1p+QF9luIKKMoM76d0f65qjCK0Tdz6b14tWTqozvJxPjgJpMkwdJsSdqsLCTheWrVDpIJQyP/4QZLXFe6h38YBKgG2eT+oTaYpVaE1KKhfKlSILWBLQb+e3xDP44l9ZuuyLWdrW/V5yqe707GhMTOKHTZ76Cw9DbPBQVR85wt7VkobyJwKemTEpPt6s+UeKSRsBD47RUOgLXssqHZ96K6+b6qsBrW7rHSXS1DMGf8Z4hEbifzDmex7tkaC3HSwGgwuM8BL5ZG6Q2NIGzPQOrvY9cUe+IhTx8ZvBgYLslrwQ8YbCmtMFPixxAWHdByp8/PDupf3g4Lrwr7AM2afp5KNUr0UqAYeFmuTW9qXKaWvB3FqnVjzo3V40JO9W0hb5GDAp9RSRLPYnAZfTqimHRO2OFyKMwur9K0ltzv90OBQDECEJhlo0BW3GzA576h3ZmVr6q+UYiL5P4Ajp29maYkG0P1bfsHBpW9pVmQpVZ1V6EGvZXx9u9NlGd7yEKHhozVR8HnTyEisFyFoLUJct8Z/o/eWztY4GeIhRbHpym5wskR9MnW9/kVYOM291LVf6aPrJfPSIDzDVP1JJeNNCfEYHBoyYYKTLVqyT3J6L502CdvCEvnZINLz22HQp6SoOo+DJfXIxTmw6ojo46B4MWGd3nV/Bb8AQflRGA7k9NhjAWXwEIxdxtPFiIQcYl+MvYoCgBz/5hYVTcFihIKSUQPnecQdg5RhdQnkNbf9Hk0OwJAO3VuLC1dXVJ5RD xoyUDbUi WSTzEMh0IZ0QuSUvpb75UOc4Cud6mJHGBzbHRVvsavyw9f1zElLeUtaPt1kIftkvE2zzz4mrZdYsrmJdtrqQboVO9Zo891OlaiAfjTWj+pHY9iIQQUVyZ+694Vdq9wQEetvIgKQ8gFaZo9ya3SKa46jLeGC1yzNK6yIQ0WNrjFb18ZdjoFQbpllCSpiGjpPoKD3owFi5M+3l82SldT/bEdJn3w9Z8qrgbgPkT0glVkR1OLl2LMMN5h3pj83sB0h2tn7kyOaMomXjZPS1ZiOzP0FLagmqxV8CSw7gpN4MpCFJpPMj8rcHrNkoBvxb5jwXAllJhSG47pT4ue+77Jd13tiXHVtXe6cQ1ImEav3+MI0qRS3Xcv38BUioD5gd2mHbshZMIDr4tKrwm0kiH3MgSjeQ9nJPciYmLo386AeG6c2yeI7pnbF/u/xQLw0XrU8eL7ru4OFAH2tM8AvA+i8hm7od7HwMcLXqIYaUKqKKapH1HqdVtOlCu51UAh+UvS6GGY3idg9fsCFstWdiu9nOBWoji5CumZjMUItxNOk76q0zT4l1y2eIps1IdJA== 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: The single "real" user in the tree of make_device_exclusive_range() always requests making only a single address exclusive. The current implementation is hard to fix for properly supporting anonymous THP / large folios and for avoiding messing with rmap walks in weird ways. So let's always process a single address/page and return folio + page to minimize page -> folio lookups. This is a preparation for further changes. Reject any non-anonymous or hugetlb folios early, directly after GUP. Signed-off-by: David Hildenbrand Reviewed-by: Alistair Popple Acked-by: Simona Vetter --- Documentation/mm/hmm.rst | 2 +- Documentation/translations/zh_CN/mm/hmm.rst | 2 +- drivers/gpu/drm/nouveau/nouveau_svm.c | 5 +- include/linux/mmu_notifier.h | 2 +- include/linux/rmap.h | 5 +- lib/test_hmm.c | 45 +++++------ mm/rmap.c | 90 +++++++++++---------- 7 files changed, 75 insertions(+), 76 deletions(-) diff --git a/Documentation/mm/hmm.rst b/Documentation/mm/hmm.rst index f6d53c37a2ca..7d61b7a8b65b 100644 --- a/Documentation/mm/hmm.rst +++ b/Documentation/mm/hmm.rst @@ -400,7 +400,7 @@ Exclusive access memory Some devices have features such as atomic PTE bits that can be used to implement atomic access to system memory. To support atomic operations to a shared virtual memory page such a device needs access to that page which is exclusive of any -userspace access from the CPU. The ``make_device_exclusive_range()`` function +userspace access from the CPU. The ``make_device_exclusive()`` function can be used to make a memory range inaccessible from userspace. This replaces all mappings for pages in the given range with special swap diff --git a/Documentation/translations/zh_CN/mm/hmm.rst b/Documentation/translations/zh_CN/mm/hmm.rst index 0669f947d0bc..22c210f4e94f 100644 --- a/Documentation/translations/zh_CN/mm/hmm.rst +++ b/Documentation/translations/zh_CN/mm/hmm.rst @@ -326,7 +326,7 @@ devm_memunmap_pages() 和 devm_release_mem_region() 当资源可以绑定到 ``s 一些设备具有诸如原子PTE位的功能,可以用来实现对系统内存的原子访问。为了支持对一 个共享的虚拟内存页的原子操作,这样的设备需要对该页的访问是排他的,而不是来自CPU -的任何用户空间访问。 ``make_device_exclusive_range()`` 函数可以用来使一 +的任何用户空间访问。 ``make_device_exclusive()`` 函数可以用来使一 个内存范围不能从用户空间访问。 这将用特殊的交换条目替换给定范围内的所有页的映射。任何试图访问交换条目的行为都会 diff --git a/drivers/gpu/drm/nouveau/nouveau_svm.c b/drivers/gpu/drm/nouveau/nouveau_svm.c index b4da82ddbb6b..39e3740980bb 100644 --- a/drivers/gpu/drm/nouveau/nouveau_svm.c +++ b/drivers/gpu/drm/nouveau/nouveau_svm.c @@ -609,10 +609,9 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm, notifier_seq = mmu_interval_read_begin(¬ifier->notifier); mmap_read_lock(mm); - ret = make_device_exclusive_range(mm, start, start + PAGE_SIZE, - &page, drm->dev); + page = make_device_exclusive(mm, start, drm->dev, &folio); mmap_read_unlock(mm); - if (ret <= 0 || !page) { + if (IS_ERR(page)) { ret = -EINVAL; goto out; } diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h index e2dd57ca368b..d4e714661826 100644 --- a/include/linux/mmu_notifier.h +++ b/include/linux/mmu_notifier.h @@ -46,7 +46,7 @@ struct mmu_interval_notifier; * @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_range(), otherwise the owner will be NULL. + * caller of make_device_exclusive(), otherwise the owner will be NULL. */ enum mmu_notifier_event { MMU_NOTIFY_UNMAP = 0, diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 683a04088f3f..86425d42c1a9 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -663,9 +663,8 @@ int folio_referenced(struct folio *, int is_locked, void try_to_migrate(struct folio *folio, enum ttu_flags flags); void try_to_unmap(struct folio *, enum ttu_flags flags); -int make_device_exclusive_range(struct mm_struct *mm, unsigned long start, - unsigned long end, struct page **pages, - void *arg); +struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr, + void *owner, struct folio **foliop); /* Avoid racy checks */ #define PVMW_SYNC (1 << 0) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 056f2e411d7b..9e1b07a227a3 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -780,10 +780,8 @@ static int dmirror_exclusive(struct dmirror *dmirror, unsigned long start, end, addr; unsigned long size = cmd->npages << PAGE_SHIFT; struct mm_struct *mm = dmirror->notifier.mm; - struct page *pages[64]; struct dmirror_bounce bounce; - unsigned long next; - int ret; + int ret = 0; start = cmd->addr; end = start + size; @@ -795,36 +793,31 @@ static int dmirror_exclusive(struct dmirror *dmirror, return -EINVAL; mmap_read_lock(mm); - for (addr = start; addr < end; addr = next) { - unsigned long mapped = 0; - int i; - - next = min(end, addr + (ARRAY_SIZE(pages) << PAGE_SHIFT)); + for (addr = start; addr < end; addr += PAGE_SIZE) { + struct folio *folio; + struct page *page; - ret = make_device_exclusive_range(mm, addr, next, pages, NULL); - /* - * Do dmirror_atomic_map() iff all pages are marked for - * exclusive access to avoid accessing uninitialized - * fields of pages. - */ - if (ret == (next - addr) >> PAGE_SHIFT) - mapped = dmirror_atomic_map(addr, next, pages, dmirror); - for (i = 0; i < ret; i++) { - if (pages[i]) { - unlock_page(pages[i]); - put_page(pages[i]); - } + page = make_device_exclusive(mm, addr, &folio, NULL); + if (IS_ERR(page)) { + ret = PTR_ERR(page); + break; } - if (addr + (mapped << PAGE_SHIFT) < next) { - mmap_read_unlock(mm); - mmput(mm); - return -EBUSY; - } + ret = dmirror_atomic_map(addr, addr + PAGE_SIZE, &page, dmirror); + if (!ret) + ret = -EBUSY; + folio_unlock(folio); + folio_put(folio); + + if (ret) + break; } mmap_read_unlock(mm); mmput(mm); + if (ret) + return -EBUSY; + /* Return the migrated data for verification. */ ret = dmirror_bounce_init(&bounce, start, size); if (ret) diff --git a/mm/rmap.c b/mm/rmap.c index 17fbfa61f7ef..676df4fba5b0 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2495,70 +2495,78 @@ static bool folio_make_device_exclusive(struct folio *folio, .arg = &args, }; - /* - * Restrict to anonymous folios for now to avoid potential writeback - * issues. - */ - if (!folio_test_anon(folio) || folio_test_hugetlb(folio)) - return false; - rmap_walk(folio, &rwc); return args.valid && !folio_mapcount(folio); } /** - * make_device_exclusive_range() - Mark a range for exclusive use by a device + * make_device_exclusive() - Mark an address for exclusive use by a device * @mm: mm_struct of associated target process - * @start: start of the region to mark for exclusive device access - * @end: end address of region - * @pages: returns the pages which were successfully marked for exclusive access + * @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 + * folio reference, locks the folio and replaces the PTE with special + * device-exclusive non-swap entry, preventing userspace CPU access. The + * function will return with the folio locked and referenced. * - * Returns: number of pages found in the range by GUP. A page is marked for - * exclusive access only if the page pointer is non-NULL. + * On fault these special device-exclusive entries are replaced with the + * original PTE under folio lock, after calling MMU notifiers. * - * This function finds ptes mapping page(s) to the given address range, locks - * them and replaces mappings with special swap entries preventing userspace CPU - * access. On fault these entries are replaced with the original mapping after - * calling MMU notifiers. + * Only anonymous non-hugetlb folios are supported and the VMA must have + * write permissions such that we can fault in the anonymous page writable + * in order to mark it exclusive. The caller must hold the mmap_lock in read + * mode. * * A driver using this to program access from a device must use a mmu notifier * critical section to hold a device specific lock during programming. Once * programming is complete it should drop the page lock and reference after * which point CPU access to the page will revoke the exclusive access. + * + * Returns: pointer to mapped page on success, otherwise a negative error. */ -int make_device_exclusive_range(struct mm_struct *mm, unsigned long start, - unsigned long end, struct page **pages, - void *owner) +struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr, + void *owner, struct folio **foliop) { - long npages = (end - start) >> PAGE_SHIFT; - long i; + struct folio *folio; + struct page *page; + long npages; + + mmap_assert_locked(mm); - npages = get_user_pages_remote(mm, start, npages, + /* + * Fault in the page writable and try to lock it; note that if the + * address would already be marked for exclusive use by the device, + * the GUP call would undo that first by triggering a fault. + */ + npages = get_user_pages_remote(mm, addr, 1, FOLL_GET | FOLL_WRITE | FOLL_SPLIT_PMD, - pages, NULL); - if (npages < 0) - return npages; - - for (i = 0; i < npages; i++, start += PAGE_SIZE) { - struct folio *folio = page_folio(pages[i]); - if (PageTail(pages[i]) || !folio_trylock(folio)) { - folio_put(folio); - pages[i] = NULL; - continue; - } + &page, NULL); + if (npages != 1) + return ERR_PTR(npages); + folio = page_folio(page); - if (!folio_make_device_exclusive(folio, mm, start, owner)) { - folio_unlock(folio); - folio_put(folio); - pages[i] = NULL; - } + if (!folio_test_anon(folio) || folio_test_hugetlb(folio)) { + folio_put(folio); + return ERR_PTR(-EOPNOTSUPP); + } + + if (!folio_trylock(folio)) { + folio_put(folio); + return ERR_PTR(-EBUSY); } - return npages; + if (!folio_make_device_exclusive(folio, mm, addr, owner)) { + folio_unlock(folio); + folio_put(folio); + return ERR_PTR(-EBUSY); + } + *foliop = folio; + return page; } -EXPORT_SYMBOL_GPL(make_device_exclusive_range); +EXPORT_SYMBOL_GPL(make_device_exclusive); #endif void __put_anon_vma(struct anon_vma *anon_vma)