From nobody Tue Nov 24 11:35:52 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463787; b=zABaMuBZ9NjENyqAOA7lezSw9G/pLLCwn3PdhKHMQMOEo+sNAw+dmZ7uifiPfd+3FGgd NuWPCh7hCwb6Vj5NhId75ZSniNBGvnNGAbsWVdm2YX/E1NHsVY+ei+UQ4CBtMLMYwGhEw Ulx+3K+qlOSBcMQwerOqLyBIodImhfYubSCmX4LhcmF26FCEe+ViigXKs3eDdiwtkUSXN Ieii84P/fGah0T5L7qC4jgs72dKXQcX9S86aj79oulsFX/uUEaw1dnfC9zjR/CN97ehnS piYmY2SWDmhwuQr/Hyk28eVpn+E8+k/bU2joJPLxvg3L7CB8l+eaX1HnrH1PPFndkOw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463787; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=QebCm/HdUJNQJhaYSYkTX5nopMX74EmzXUpwCg5l4wY=; b=0Xe3pzVuq2EmIHUBLzBb780IFAeSmKXUN2hoOxuQs16kYdmcvtLl3QdgApCwzjWuiiWE 3PiCqvpXq08TBKZSTUMsV9/Kn11VoycVlkZ8OuW8xSn7W1r3OyI66t6d/DZ/uxoXbNG/4 rkgKbWBC0ATtJgngYZqk+0SFytO0GKYstfRviZt+yttwY1WyU4RFhhSa8Vew2S6+/sYxu AHQ3ZEb6b4DFHlUNObeYFvD4jXqU4cdwNsIkX7noFZl0CjPpVd10/4q9jxsaAJ0W88J7k yqWFHTTlhCZMoNx6augNdr5sWSTIlxR7QrKF0CVrJqbwRELIlLLbFF16g1cWPo6NyhA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=bkafn7af; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2B84606A335 for ; Mon, 23 Nov 2026 19:56:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2B84606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=bkafn7af DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=QebCm/HdUJNQJhaYSYkTX5nopMX74EmzXUpwCg5l4wY=; b=bkafn7 af9RQSHz5YgQbHnMTZD9q0cKlfKPKAnwgQ471p68km6500ZobrP4S414IUZh+r27gnbglITiixfw8 mgYwUoAYj/QiovpNkNf0Mm2WN1qvSHWCCZKhQNlecy/Xg9bLL5Hx21V2jfKYAWM4uqviFbj2XVcSE 9U4mS19uPDjWHvwOkFuqhXmDzsHmMjdBnI7Bp0h4RMDIAb65eBm3YsIAvjHD4Gt78LIayOJGNP2ao p0xpq6OflBp0V8fEmFL3rdfVPnRVr//vYZugZhhlHf6qSdqJogzX/VWevIfwobtIkQfX2nt1dQLz5 8ZHk+ncKqGWhRAegzfXO/0ebBqhQ==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-1Cs1; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 1/9] mm/gup: break out gup_fill_pages() helper Date: Mon, 23 Nov 2026 14:56:01 -0500 Message-ID: <20261123195609.4115244-2-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __get_user_pages() fills pages[] in an open-coded loop, and the GUP-fast PTE, PMD and PUD leaf paths each have their own copy of the same loop. Move the store into a gup_fill_pages() helper, shared by the slow and the fast paths. The helper keeps a pages=3D=3DNULL check, so the fault-in users that pass no pages array (faultin_page_range(), populate_vma_page_range()) need no conditionals at their call sites. Cache flushing stays open-coded for now; it moves into its own helper in the next patch. No functional changes intended. Suggested-by: David Hildenbrand Signed-off-by: Rik van Riel --- mm/gup.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index b0a0019de80a..e0698d2075b4 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -710,6 +710,25 @@ static inline bool gup_must_unshare(struct vm_area_str= uct *vma, return gup_pages_not_requiring_unshare(vma, flags, folio, page, 1) !=3D 1; } =20 +/* + * Store @nr_pages consecutive pages starting at @page into @pages. + * The caller must hold at least one reference on the folio, so it + * cannot go away, or get split while the array is filled. + * + * Some GUP users only fault pages in and pass no pages array. + */ +static void gup_fill_pages(struct page **pages, struct page *page, + unsigned long nr_pages) +{ + unsigned long i; + + if (!pages) + return; + + for (i =3D 0; i < nr_pages; i++) + pages[i] =3D page + i; +} + #ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES /* FOLL_FORCE can write to even unwritable PUDs in COW mappings. */ static inline bool can_follow_write_pud(pud_t pud, struct page *page, @@ -1579,9 +1598,9 @@ static long __get_user_pages(struct mm_struct *mm, } } =20 + gup_fill_pages(pages + i, page, page_increm); for (j =3D 0; j < page_increm; j++) { subpage =3D page + j; - pages[i + j] =3D subpage; flush_anon_page(vma, subpage, start + j * PAGE_SIZE); flush_dcache_page(subpage); } @@ -2946,7 +2965,6 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pm= d_t *pmdp, do { pte_t pte =3D ptep_get_lockless(ptep); struct page *page; - unsigned long i; =20 nr_batch =3D 1; =20 @@ -3017,8 +3035,7 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pm= d_t *pmdp, nr_batch =3D 1; =20 folio_set_referenced(folio); - for (i =3D 0; i < nr_batch; i++) - pages[nr_pages + i] =3D page + i; + gup_fill_pages(pages + nr_pages, page, nr_batch); nr_pages +=3D nr_batch; folio =3D NULL; } while (ptep +=3D nr_batch, addr +=3D nr_batch * PAGE_SIZE, addr !=3D en= d); @@ -3054,7 +3071,7 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pm= d_t *pmdp, { struct page *mapped_page, *page; struct folio *folio; - unsigned long nr_pages, i; + unsigned long nr_pages; =20 /* See gup_fast_pte_range() */ if (pmd_protnone(orig)) @@ -3089,8 +3106,7 @@ static unsigned long gup_fast_pmd_leaf(pmd_t orig, pm= d_t *pmdp, return 0; } =20 - for (i =3D 0; i < nr_pages; i++) - *(pages++) =3D page++; + gup_fill_pages(pages, page, nr_pages); folio_set_referenced(folio); return nr_pages; } @@ -3101,7 +3117,7 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pu= d_t *pudp, { struct page *mapped_page, *page; struct folio *folio; - unsigned long nr_pages, i; + unsigned long nr_pages; =20 if (!pud_access_permitted(orig, flags & FOLL_WRITE)) return 0; @@ -3133,8 +3149,7 @@ static unsigned long gup_fast_pud_leaf(pud_t orig, pu= d_t *pudp, return 0; } =20 - for (i =3D 0; i < nr_pages; i++) - *(pages++) =3D page++; + gup_fill_pages(pages, page, nr_pages); folio_set_referenced(folio); return nr_pages; } --=20 2.56.0 From nobody Tue Nov 24 11:35:52 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463786; b=x9x+yF3fle7+Ld1VANUWX5T3VWE2f8btHAFJIWu9n2G7FZDxStaMF6UL29zVUbgHybd0 FdZAXhMca+KDuHTiAnPUTLJPT9aQk2DVHU3hLwUrUwbOjC1Ubryk0ZyG1WGHXDRzf0k3O 5vB3BhEAw094tTKhLTaNOXuJjQIELSRuwf4uFqT/07HpJwK7527eZf4zICEdCDcTgj99N LpkD3ccvFqgL5TAO6m5nX+bO/C1p7CgjtdErccsQvR2hzRvuBYx1i0k5u0Q3V5v3qIlP3 I/kW3NkojC3Ui9AxkihslM4X8CG1E7p4a3Do4FBQ5QfYu++uWLxkSGWR1K8KlvWDmkw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463786; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=SNg/eN30d+0BFvF9t/QYVmNoDb1lzvAqSarbFDg/MKE=; b=IX9850k45pi9tZYpllIrYUgJvc+65dPzY5Zo4xrKmU4pCm7ku0xUM79BQEbEubgS6xfx 9RNTlYCPJpGHiSK33wnyZxvXo+wzB8YE/mMDbvTdz7tjLRvCFgIGXL8ccdzLr63rPiKko wO8FJNMhkmvVlwOJV9iT/Wh9pqCT5bmj/c8fgCKTnnpwPwLO4ERQFNp779Ss7sjs8FgF9 UTW+P2w/kU67v9ewMN/o1WQwTGwyq7wvJQwX/clWTQCSUyC8yXtpJuo9CPAHRsIMuLseC 9zzyCJGgUDyOC9Ad0MyL7k1vAcgxbuXRUWBpSBMdOJCRKAM9g9qJyHyVNp8TpMrh5rA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b="TWxZm/dZ"; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E7B8606A332 for ; Mon, 23 Nov 2026 19:56:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E7B8606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=TWxZm/dZ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=SNg/eN30d+0BFvF9t/QYVmNoDb1lzvAqSarbFDg/MKE=; b=TWxZm/ dZ0v63E06eHlklmT+WDHwGP1hIkYnKx0guN4mDEWWcSeIAqqxdy5PUKUA9Rg8yUTHZDQm2BzNDC8S OSA5I2Ujbl8238wuBeAokM++MnPwzvB+l67E2hBhRQ3GGAYkmmkcfh55yLghR9moWtAtPVtJ0fai3 yQzzeED5w41VzXmZnFsHjRCtCiMDQaM/vO+GWnkqda5iAX3sSszBvCISQmwG7iBHynple/qEcKSFM rgH5RM2FRXEP7ungdCDhsQTCMtE5QxMljgMU6RBY85sGan9KKUTmVbZ98NO8BPdf4zaLRbEmp8UCC ZxkhoaHtZyWr39sOgEg7c1KpH7nA==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-1JJ4; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 2/9] mm/gup: break out gup_flush_pages() helper Date: Mon, 23 Nov 2026 14:56:02 -0500 Message-ID: <20261123195609.4115244-3-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __get_user_pages() flushes each page's caches in an open-coded loop right after filling the array. Move it into a gup_flush_pages() helper. No functional changes intended. Suggested-by: David Hildenbrand Signed-off-by: Rik van Riel --- mm/gup.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index e0698d2075b4..d35fedbce7e1 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -729,6 +729,24 @@ static void gup_fill_pages(struct page **pages, struct= page *page, pages[i] =3D page + i; } =20 +/* + * Flush the caches for @nr_pages pages stored in @pages. The caller + * already holds folio references on the pages. + */ +static void gup_flush_pages(struct vm_area_struct *vma, unsigned long addr= ess, + struct page **pages, unsigned long nr_pages) +{ + unsigned long i; + + if (!pages) + return; + + for (i =3D 0; i < nr_pages; i++) { + flush_anon_page(vma, pages[i], address + i * PAGE_SIZE); + flush_dcache_page(pages[i]); + } +} + #ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES /* FOLL_FORCE can write to even unwritable PUDs in COW mappings. */ static inline bool can_follow_write_pud(pud_t pud, struct page *page, @@ -1566,9 +1584,6 @@ static long __get_user_pages(struct mm_struct *mm, page_increm =3D nr_pages; =20 if (pages) { - struct page *subpage; - unsigned int j; - /* * This must be a large folio (and doesn't need to * be the whole folio; it can be part of it), do @@ -1599,11 +1614,7 @@ static long __get_user_pages(struct mm_struct *mm, } =20 gup_fill_pages(pages + i, page, page_increm); - for (j =3D 0; j < page_increm; j++) { - subpage =3D page + j; - flush_anon_page(vma, subpage, start + j * PAGE_SIZE); - flush_dcache_page(subpage); - } + gup_flush_pages(vma, start, pages + i, page_increm); } =20 i +=3D page_increm; --=20 2.56.0 From nobody Tue Nov 24 11:35:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463786; b=yKcNUkggpKxCdI5iU712FkOG6Iyxkdr9RRk4pdGMCFMhZkwUL/Azmph8GOV+WFgtl2I2 E5kTdRu0nCCsbOQ8mb8RdJwjZ6dZoPc3MlMQ/hyQy0APnRI+OEXEthbLuqIQ8l1kvwnQF ITKKgXLU6EX0Y+dNB7pGoXsFABAFAMOlI02miomq7TSQ3S8nABALSirwivFM5+ZP7AVkE D5E98KKR+LS5tq2trUsRNm/bGMOZbhs9WXTy0SBmd+L9Lfv6Qei5pWKsKC0LhPxgR8Ulq b8L3J/Fl8meK82OaAwtp4qCgnNfgN/h4IJdil+MchHEV/WtQHlXNCb3RnP2/1hsPrVg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463786; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=psRfc0KHheIriVsHfc4tEURoTeOTCGIfo9MPK7BM01o=; b=foWykTj0hPIVXwnFpbzIDskZCmRTdSuMtIjyMtPjl731zNawe3ZTq2hFpo7tALXklI/l LfPhl0+7fzWzty7x/Gejn81+BmSMOR/hYTwHXQerwKXwAbqNRut9GdzpFR19jZG5x6acX a+EOBx4lt+u7OOdOpsHfwrqA9hIDwXGvob0Ac2XNv3sEF6T2MmNMcdCVuJpLR1SV2Bzal sA66vfDt8W0xN0yzExF5i/QNQEszxb7fgWM0AKKrtSCHlm2z7inoyVgzdVVvA2HMSeHx/ u7vHw0yXjgw/kPx3vGZkGPtDhjjktMFPAgShQyZOGVnmqGsX4IclAzGp2IlAOL+5lew== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=WMHCDNSZ; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E6FB606A32C for ; Mon, 23 Nov 2026 19:56:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E6FB606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=WMHCDNSZ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=psRfc0KHheIriVsHfc4tEURoTeOTCGIfo9MPK7BM01o=; b=WMHCDN SZMjBzZPPYC6sFRzkkaLym/MIvixrdAnn4T1oTUKOtKoCY2MlTJy+XODOUHzRNaoGUmN7WB/uC9sA BoilXj99ZhUn205mvvc0SVnkZPoGsbGB8iljRzCjbXY77KH+KZ5bBM/f7Dq7f95JZwthppQI1zg66 fxUraWqzTG1RRRRjxBa6QyBBuIUqfMPBzjYZj9c5khQOSF+dp+SswIeqwpwjTCKH1GS9dOncuUPEF 2K7s5X5zvz8XXMfjshWj4hz1Y13nk1L3hIlusyiLG9Dzg4XTtNhN9WDgMuFPlZoPNOFhbxi5tII9x 3MQg61tFwwl6HNo21KJrYh2HvBgg==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-1RMz; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 3/9] mm/gup: convert follow_page_mask() to return a long Date: Mon, 23 Nov 2026 14:56:03 -0500 Message-ID: <20261123195609.4115244-4-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" follow_page_mask() and its helpers return a struct page pointer: NULL, ERR_PTR(), or the page found. Change the return type to long instead: 0, a negative errno, or 1 with the page stored in a new pages[0] slot. This lets the return type carry a page count rather than a single struct page pointer. follow_huge_pud(), follow_huge_pmd() and follow_page_pte() now store the found page in a pages[0] slot. __get_user_pages() reads that page back from pages[i], fills the rest of a large folio's run itself, and flushes the whole run's caches with gup_flush_pages(). The follow path itself never flushes: the caches are always flushed from __get_user_pages(), without any page table lock held. The vsyscall gate area bypasses follow_page_mask() and fills and flushes its own slot the same way. The gate handles a single page, so it initializes the count to 1. *page_mask and __get_user_pages()'s handling of a large folio's remaining subpages are untouched, and mm/gup_test.c (PIN_LONGTERM_BENCHMARK) shows no measurable difference for 4 kB, 64 kB mTHP, or 2 MB THP. No functional changes intended. Suggested-by: David Hildenbrand Signed-off-by: Rik van Riel --- mm/gup.c | 258 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 141 insertions(+), 117 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index d35fedbce7e1..963c09b955d9 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -608,15 +608,15 @@ static inline bool can_follow_write_common(struct pag= e *page, return page && PageAnon(page) && PageAnonExclusive(page); } =20 -static struct page *no_page_table(struct vm_area_struct *vma, - unsigned int flags, unsigned long address) +static long no_page_table(struct vm_area_struct *vma, + unsigned int flags, unsigned long address) { if (!(flags & FOLL_DUMP)) - return NULL; + return 0; =20 /* * When core dumping, we don't want to allocate unnecessary pages or - * page tables. Return error instead of NULL to skip handle_mm_fault, + * page tables. Return error instead of 0 to skip handle_mm_fault, * then get_dump_page() will return NULL to leave a hole in the dump. * But we can only make this optimization where a hole would surely * be zero-filled if handle_mm_fault() actually did handle it. @@ -625,12 +625,12 @@ static struct page *no_page_table(struct vm_area_stru= ct *vma, struct hstate *h =3D hstate_vma(vma); =20 if (!hugetlbfs_pagecache_present(h, vma, address)) - return ERR_PTR(-EFAULT); + return -EFAULT; } else if ((vma_is_anonymous(vma) || !vma->vm_ops->fault)) { - return ERR_PTR(-EFAULT); + return -EFAULT; } =20 - return NULL; + return 0; } =20 static unsigned long gup_pages_not_requiring_unshare( @@ -760,9 +760,10 @@ static inline bool can_follow_write_pud(pud_t pud, str= uct page *page, return can_follow_write_common(page, vma, flags); } =20 -static struct page *follow_huge_pud(struct vm_area_struct *vma, - unsigned long addr, pud_t *pudp, - int flags, unsigned long *page_mask) +static long follow_huge_pud(struct vm_area_struct *vma, + unsigned long addr, pud_t *pudp, + unsigned int flags, unsigned long *page_mask, + struct page **pages) { struct mm_struct *mm =3D vma->vm_mm; struct page *page; @@ -775,22 +776,24 @@ static struct page *follow_huge_pud(struct vm_area_st= ruct *vma, =20 if ((flags & FOLL_WRITE) && !can_follow_write_pud(pud, pfn_to_page(pfn), vma, flags)) - return NULL; + return 0; =20 pfn +=3D (addr & ~PUD_MASK) >> PAGE_SHIFT; page =3D pfn_to_page(pfn); folio =3D page_folio(page); =20 if (!pud_write(pud) && gup_must_unshare(vma, flags, folio, page)) - return ERR_PTR(-EMLINK); + return -EMLINK; =20 ret =3D try_grab_folio(folio, 1, flags); if (ret) - page =3D ERR_PTR(ret); - else - *page_mask =3D HPAGE_PUD_NR - 1; + return ret; + + *page_mask =3D HPAGE_PUD_NR - 1; + + gup_fill_pages(pages, page, 1); =20 - return page; + return 1; } =20 /* FOLL_FORCE can write to even unwritable PMDs in COW mappings. */ @@ -811,10 +814,10 @@ static inline bool can_follow_write_pmd(pmd_t pmd, st= ruct page *page, return !userfaultfd_huge_pmd_wp(vma, pmd); } =20 -static struct page *follow_huge_pmd(struct vm_area_struct *vma, - unsigned long addr, pmd_t *pmd, - unsigned int flags, - unsigned long *page_mask) +static long follow_huge_pmd(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmd, + unsigned int flags, unsigned long *page_mask, + struct page **pages) { struct mm_struct *mm =3D vma->vm_mm; pmd_t pmdval =3D *pmd; @@ -829,24 +832,24 @@ static struct page *follow_huge_pmd(struct vm_area_st= ruct *vma, =20 if ((flags & FOLL_WRITE) && !can_follow_write_pmd(pmdval, page, vma, flags)) - return NULL; + return 0; =20 /* Avoid dumping huge zero page */ if ((flags & FOLL_DUMP) && is_huge_zero_pmd(pmdval)) - return ERR_PTR(-EFAULT); + return -EFAULT; =20 if (pmd_protnone(*pmd) && !gup_can_follow_protnone(vma, flags)) - return NULL; + return 0; =20 if (!pmd_write(pmdval) && gup_must_unshare(vma, flags, folio, page)) - return ERR_PTR(-EMLINK); + return -EMLINK; =20 VM_WARN_ON_ONCE_PAGE((flags & FOLL_PIN) && PageAnon(page) && !PageAnonExclusive(page), page); =20 ret =3D try_grab_folio(folio, 1, flags); if (ret) - return ERR_PTR(ret); + return ret; =20 #ifdef CONFIG_TRANSPARENT_HUGEPAGE if (pmd_trans_huge(pmdval) && (flags & FOLL_TOUCH)) @@ -856,23 +859,26 @@ static struct page *follow_huge_pmd(struct vm_area_st= ruct *vma, page +=3D (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; *page_mask =3D HPAGE_PMD_NR - 1; =20 - return page; + gup_fill_pages(pages, page, 1); + + return 1; } =20 #else /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ -static struct page *follow_huge_pud(struct vm_area_struct *vma, - unsigned long addr, pud_t *pudp, - int flags, unsigned long *page_mask) +static long follow_huge_pud(struct vm_area_struct *vma, + unsigned long addr, pud_t *pudp, + unsigned int flags, unsigned long *page_mask, + struct page **pages) { - return NULL; + return 0; } =20 -static struct page *follow_huge_pmd(struct vm_area_struct *vma, - unsigned long addr, pmd_t *pmd, - unsigned int flags, - unsigned long *page_mask) +static long follow_huge_pmd(struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmd, + unsigned int flags, unsigned long *page_mask, + struct page **pages) { - return NULL; + return 0; } #endif /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ =20 @@ -915,15 +921,16 @@ static inline bool can_follow_write_pte(pte_t pte, st= ruct page *page, return !userfaultfd_pte_wp(vma, pte); } =20 -static struct page *follow_page_pte(struct vm_area_struct *vma, - unsigned long address, pmd_t *pmd, unsigned int flags) +static long follow_page_pte(struct vm_area_struct *vma, + unsigned long address, pmd_t *pmd, unsigned int flags, + struct page **pages) { struct mm_struct *mm =3D vma->vm_mm; struct folio *folio; struct page *page; spinlock_t *ptl; pte_t *ptep, pte; - int ret; + long ret; =20 ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); if (!ptep) @@ -941,14 +948,14 @@ static struct page *follow_page_pte(struct vm_area_st= ruct *vma, */ if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, page, vma, flags)) { - page =3D NULL; + ret =3D 0; goto out; } =20 if (unlikely(!page)) { if (flags & FOLL_DUMP) { /* Avoid special (like zero) pages in core dumps */ - page =3D ERR_PTR(-EFAULT); + ret =3D -EFAULT; goto out; } =20 @@ -956,14 +963,13 @@ static struct page *follow_page_pte(struct vm_area_st= ruct *vma, page =3D pte_page(pte); } else { ret =3D follow_pfn_pte(vma, address, ptep, flags); - page =3D ERR_PTR(ret); goto out; } } folio =3D page_folio(page); =20 if (!pte_write(pte) && gup_must_unshare(vma, flags, folio, page)) { - page =3D ERR_PTR(-EMLINK); + ret =3D -EMLINK; goto out; } =20 @@ -972,10 +978,8 @@ static struct page *follow_page_pte(struct vm_area_str= uct *vma, =20 /* try_grab_folio() does nothing unless FOLL_GET or FOLL_PIN is set. */ ret =3D try_grab_folio(folio, 1, flags); - if (unlikely(ret)) { - page =3D ERR_PTR(ret); + if (unlikely(ret)) goto out; - } =20 /* * We need to make the page accessible if and only if we are going @@ -985,8 +989,7 @@ static struct page *follow_page_pte(struct vm_area_stru= ct *vma, if (flags & FOLL_PIN) { ret =3D arch_make_folio_accessible(folio); if (ret) { - unpin_user_page(page); - page =3D ERR_PTR(ret); + gup_put_folio(folio, 1, flags); goto out; } } @@ -1001,23 +1004,27 @@ static struct page *follow_page_pte(struct vm_area_= struct *vma, */ folio_mark_accessed(folio); } + + gup_fill_pages(pages, page, 1); + ret =3D 1; out: pte_unmap_unlock(ptep, ptl); - return page; + return ret; no_page: pte_unmap_unlock(ptep, ptl); if (!pte_none(pte)) - return NULL; + return 0; return no_page_table(vma, flags, address); } =20 -static struct page *follow_pmd_mask(struct vm_area_struct *vma, +static long follow_pmd_mask(struct vm_area_struct *vma, unsigned long address, pud_t *pudp, pud_t pud, - unsigned int flags, unsigned long *page_mask) + unsigned int flags, unsigned long *page_mask, + struct page **pages) { pmd_t *pmd, pmdval; spinlock_t *ptl; - struct page *page; + long ret; struct mm_struct *mm =3D vma->vm_mm; =20 if (unlikely(pud_bad(pud))) @@ -1030,7 +1037,7 @@ static struct page *follow_pmd_mask(struct vm_area_st= ruct *vma, if (!pmd_present(pmdval)) return no_page_table(vma, flags, address); if (likely(!pmd_leaf(pmdval))) - return follow_page_pte(vma, address, pmd, flags); + return follow_page_pte(vma, address, pmd, flags, pages); =20 if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags)) return no_page_table(vma, flags, address); @@ -1043,28 +1050,28 @@ static struct page *follow_pmd_mask(struct vm_area_= struct *vma, } if (unlikely(!pmd_leaf(pmdval))) { spin_unlock(ptl); - return follow_page_pte(vma, address, pmd, flags); + return follow_page_pte(vma, address, pmd, flags, pages); } if (pmd_trans_huge(pmdval) && (flags & FOLL_SPLIT_PMD)) { spin_unlock(ptl); split_huge_pmd(vma, pmd, address); /* If pmd was left empty, stuff a page table in there quickly */ - return pte_alloc(mm, pmd) ? ERR_PTR(-ENOMEM) : - follow_page_pte(vma, address, pmd, flags); + return pte_alloc(mm, pmd) ? -ENOMEM : + follow_page_pte(vma, address, pmd, flags, pages); } - page =3D follow_huge_pmd(vma, address, pmd, flags, page_mask); + ret =3D follow_huge_pmd(vma, address, pmd, flags, page_mask, pages); spin_unlock(ptl); - return page; + return ret; } =20 -static struct page *follow_pud_mask(struct vm_area_struct *vma, - unsigned long address, p4d_t *p4dp, - unsigned int flags, - unsigned long *page_mask) +static long follow_pud_mask(struct vm_area_struct *vma, + unsigned long address, p4d_t *p4dp, + unsigned int flags, unsigned long *page_mask, + struct page **pages) { pud_t *pudp, pud; spinlock_t *ptl; - struct page *page; + long ret; struct mm_struct *mm =3D vma->vm_mm; =20 pudp =3D pud_offset(p4dp, address); @@ -1072,7 +1079,8 @@ static struct page *follow_pud_mask(struct vm_area_st= ruct *vma, if (!pud_present(pud)) return no_page_table(vma, flags, address); if (!pud_leaf(pud)) - return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask); + return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask, + pages); =20 ptl =3D pud_lock(mm, pudp); pud =3D *pudp; @@ -1082,18 +1090,19 @@ static struct page *follow_pud_mask(struct vm_area_= struct *vma, } if (unlikely(!pud_leaf(pud))) { spin_unlock(ptl); - return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask); + return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask, + pages); } =20 - page =3D follow_huge_pud(vma, address, pudp, flags, page_mask); + ret =3D follow_huge_pud(vma, address, pudp, flags, page_mask, pages); spin_unlock(ptl); - return page; + return ret; } =20 -static struct page *follow_p4d_mask(struct vm_area_struct *vma, - unsigned long address, pgd_t *pgdp, - unsigned int flags, - unsigned long *page_mask) +static long follow_p4d_mask(struct vm_area_struct *vma, + unsigned long address, pgd_t *pgdp, + unsigned int flags, unsigned long *page_mask, + struct page **pages) { p4d_t *p4dp, p4d; =20 @@ -1104,7 +1113,7 @@ static struct page *follow_p4d_mask(struct vm_area_st= ruct *vma, if (!p4d_present(p4d) || p4d_bad(p4d)) return no_page_table(vma, flags, address); =20 - return follow_pud_mask(vma, address, p4dp, flags, page_mask); + return follow_pud_mask(vma, address, p4dp, flags, page_mask, pages); } =20 /** @@ -1113,6 +1122,9 @@ static struct page *follow_p4d_mask(struct vm_area_st= ruct *vma, * @address: virtual address to look up * @flags: flags modifying lookup behaviour * @page_mask: a pointer to output page_mask + * @pages: array to receive the page found, refcounted per @flags, or NULL + * to walk the page tables (e.g. to fault pages in) without + * collecting or refcounting them * * @flags can have FOLL_ flags set, defined in * @@ -1123,17 +1135,17 @@ static struct page *follow_p4d_mask(struct vm_area_= struct *vma, * * On output, @page_mask is set according to the size of the page. * - * Return: the mapped (struct page *), %NULL if no mapping exists, or - * an error pointer if there is a mapping to something not represented - * by a page descriptor (see also vm_normal_page()). + * Return: 1 with @pages[0] filled in if a page was found, 0 if no mapping + * exists at @address, or a negative errno for a mapping to something not + * represented by a page descriptor (see also vm_normal_page()). */ -static struct page *follow_page_mask(struct vm_area_struct *vma, - unsigned long address, unsigned int flags, - unsigned long *page_mask) +static long follow_page_mask(struct vm_area_struct *vma, + unsigned long address, unsigned int flags, + unsigned long *page_mask, struct page **pages) { pgd_t *pgd; struct mm_struct *mm =3D vma->vm_mm; - struct page *page; + long ret; =20 vma_pgtable_walk_begin(vma); =20 @@ -1141,13 +1153,13 @@ static struct page *follow_page_mask(struct vm_area= _struct *vma, pgd =3D pgd_offset(mm, address); =20 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) - page =3D no_page_table(vma, flags, address); + ret =3D no_page_table(vma, flags, address); else - page =3D follow_p4d_mask(vma, address, pgd, flags, page_mask); + ret =3D follow_p4d_mask(vma, address, pgd, flags, page_mask, pages); =20 vma_pgtable_walk_end(vma); =20 - return page; + return ret; } =20 static int get_gate_page(struct mm_struct *mm, unsigned long address, @@ -1497,6 +1509,7 @@ static long __get_user_pages(struct mm_struct *mm, do { struct page *page; unsigned int page_increm; + long nr; =20 /* first iteration or cross vma bound */ if (!vma || start >=3D vma->vm_end) { @@ -1523,8 +1536,11 @@ static long __get_user_pages(struct mm_struct *mm, pages ? &page : NULL); if (ret) goto out; - page_mask =3D 0; - goto next_page; + if (pages) + pages[i] =3D page; + nr =3D 1; + page_increm =3D 1; + goto fill_flush; } =20 if (!vma) { @@ -1546,10 +1562,11 @@ static long __get_user_pages(struct mm_struct *mm, } cond_resched(); =20 - page =3D follow_page_mask(vma, start, gup_flags, &page_mask); - if (!page || PTR_ERR(page) =3D=3D -EMLINK) { + nr =3D follow_page_mask(vma, start, gup_flags, &page_mask, + pages ? &pages[i] : NULL); + if (!nr || nr =3D=3D -EMLINK) { ret =3D faultin_page(vma, start, gup_flags, - PTR_ERR(page) =3D=3D -EMLINK, locked); + nr =3D=3D -EMLINK, locked); switch (ret) { case 0: goto retry; @@ -1563,7 +1580,7 @@ static long __get_user_pages(struct mm_struct *mm, goto out; } BUG(); - } else if (PTR_ERR(page) =3D=3D -EEXIST) { + } else if (nr =3D=3D -EEXIST) { /* * Proper page table entry exists, but no corresponding * struct page. If the caller expects **pages to be @@ -1571,49 +1588,56 @@ static long __get_user_pages(struct mm_struct *mm, * for this page. */ if (pages) { - ret =3D PTR_ERR(page); + ret =3D nr; goto out; } - } else if (IS_ERR(page)) { - ret =3D PTR_ERR(page); + } else if (nr < 0) { + ret =3D nr; goto out; } -next_page: + page_increm =3D 1 + (~(start >> PAGE_SHIFT) & page_mask); if (page_increm > nr_pages) page_increm =3D nr_pages; =20 - if (pages) { + /* + * This must be a large folio (and doesn't need to + * be the whole folio; it can be part of it), do + * the refcount work for the remaining pages. + * + * NOTE: here the page may not be the head page + * e.g. when start addr is not thp-size aligned. + * try_grab_folio() should have taken care of tail + * pages. + */ + if (pages && page_increm > 1) { + struct folio *folio =3D page_folio(pages[i]); + /* - * This must be a large folio (and doesn't need to - * be the whole folio; it can be part of it), do - * the refcount work for all the subpages too. - * - * NOTE: here the page may not be the head page - * e.g. when start addr is not thp-size aligned. - * try_grab_folio() should have taken care of tail - * pages. + * Since we already hold refcount on the + * large folio, this should never fail. */ - if (page_increm > 1) { - struct folio *folio =3D page_folio(page); - + if (try_grab_folio(folio, page_increm - 1, + gup_flags)) { /* - * Since we already hold refcount on the - * large folio, this should never fail. + * Release the 1st page ref if the + * folio is problematic, fail hard. */ - if (try_grab_folio(folio, page_increm - 1, - gup_flags)) { - /* - * Release the 1st page ref if the - * folio is problematic, fail hard. - */ - gup_put_folio(folio, 1, gup_flags); - ret =3D -EFAULT; - goto out; - } + gup_put_folio(folio, 1, gup_flags); + ret =3D -EFAULT; + goto out; } =20 - gup_fill_pages(pages + i, page, page_increm); + } + + fill_flush: + /* + * The first page is already stored and references on the + * whole run are held; fill the rest of the array and flush + * the caches without any page table lock held. + */ + if (pages) { + gup_fill_pages(pages + i, pages[i], page_increm); gup_flush_pages(vma, start, pages + i, page_increm); } =20 --=20 2.56.0 From nobody Tue Nov 24 11:35:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463787; b=NzweJAK3ynuGa7BSZ0AhP0ET0B9bZiSIEVora+nNarESpcibuBMJZr7nZDaMycfdMp5r 8Vti5COnCpEKjHiVcOQjXy5uGkWFFdoP8apO68DxJYneUH/jOJ162lG64QPV6FOJT4lA1 3KG0bVBcarTuNlXwxr56to+hZj3+2GbEiRVq63Ct5lkmYgCODvisMPPm6DbUA3yLQAj3v Zg3A6kZxFS//CxYvsmPxJHQ0LomGl3Epp7x2P+B4q0+R541+obLhH1jQalmktfXoDUG06 Zb/njjTOtH2MDaMkaPobiqznISv7GZegIEWiHvb4vsAt01MjoqJBIVr9NXFI1Fgaq3A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463787; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=IiLEpxFdp/GdWWLhE2SRceq2aNaPLrmH4fAKh/8siQw=; b=E4tbEGo07ToqgLagrhPOmauA3hvnksfz2GIcrvZQ7NrAhtdWSPphkCbdymxuj3xtfzlf Ar6Q2f0p5PxEo/xXWagfSieCfUyDiumkPx2epN/FPNBcMYBxbpEpC9nEYmHQO0PTGk1Fp Fy92J13M5/n4p8oCEzjA2Og3MKGBj4WzVfpn9MLpSvsUlLcI7trTWmc4NBfdx+0JsWX9X zTQwNe1Ke8MQ9TYM8SMBsobshGC/g2Ay1hGzRATFyS4A4CWN/HqkomJ4sL+PvxQTrKMj9 NbDM08PM1GoDbS2vnlZzBMo8ZMKXakTIGRIdkpEmPojMh2ASzRPUpBmo+OB9rz92RRQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=dW5Cdkk4; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2C72606A338 for ; Mon, 23 Nov 2026 19:56:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2C72606A338 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=dW5Cdkk4 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=IiLEpxFdp/GdWWLhE2SRceq2aNaPLrmH4fAKh/8siQw=; b=dW5Cdk k4L9Y2JmljporTezPw+aHpEUtoacoF7UfrA6w4xnXXKKfOKtaqurjnUfV4qUaSCulnTbfxhVMT7Qc Yz59Ao45rw+bHu+b0IS/Q70xoZBfxsgBxNQKGamwYNt9nlYYNRv0F/Ak/Lromoa3CugZN7tQHo7MA bdebbrRHXNiBALSwLRI7b7ctpAeMI9ylA3ycftgdtpObfVG4dPSla7HUEvAXIPce7UL4oMTi6EGni q1RJODhMyPqFPmZAOeLQfuJRjGsd0rDkgy6WGWBzfuQjB6XYztkstieGzn7mtc6LkIkGYax20nnGz AXHMdJZ50tEcn7qVZSaX5p6UIRrA==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-1aVr; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 4/9] mm/gup: split out folio grab-and-store helper Date: Mon, 23 Nov 2026 14:56:04 -0500 Message-ID: <20261123195609.4115244-5-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" follow_page_pte() does two things once it has resolved a present PTE to a page: run the per-PTE safety checks (write-fault, unshare), then grab a folio reference and hand the page back: fault it in if pinning, mark it dirty/accessed, and store the caller's array slot. Split the second part into gup_grab_fill_folio_pages(), which stores the pages with gup_fill_pages(). The cache flushes stay with the caller in __get_user_pages(), which runs gup_flush_pages() without any page table lock held. Storing one slot under the lock is cheap; flushing is not, and needs no lock once the folio reference is held. No functional changes intended. Suggested-by: David Hildenbrand Signed-off-by: Rik van Riel --- mm/gup.c | 76 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 963c09b955d9..d2097ecd1576 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -921,6 +921,50 @@ static inline bool can_follow_write_pte(pte_t pte, str= uct page *page, return !userfaultfd_pte_wp(vma, pte); } =20 +/* + * Grab a folio reference for @nr pages starting at @page, mark the folio + * accessed (and dirty for FOLL_WRITE), and store the pages for the caller. + * The caller (__get_user_pages()) flushes the caches with gup_flush_pages= (). + */ +static long gup_grab_fill_folio_pages(struct folio *folio, + struct page *page, unsigned long nr, bool dirty, + unsigned int flags, struct page **pages) +{ + long ret; + + /* try_grab_folio() does nothing unless FOLL_GET or FOLL_PIN is set. */ + ret =3D try_grab_folio(folio, nr, flags); + if (unlikely(ret)) + return ret; + + /* + * We need to make the page accessible if and only if we are going + * to access its content (the FOLL_PIN case). Please see + * Documentation/core-api/pin_user_pages.rst for details. + */ + if (flags & FOLL_PIN) { + ret =3D arch_make_folio_accessible(folio); + if (ret) { + gup_put_folio(folio, nr, flags); + return ret; + } + } + if (flags & FOLL_TOUCH) { + if ((flags & FOLL_WRITE) && !dirty && !folio_test_dirty(folio)) + folio_mark_dirty(folio); + /* + * pte_mkyoung() would be more correct here, but atomic care + * is needed to avoid losing the dirty bit: it is easier to use + * folio_mark_accessed(). + */ + folio_mark_accessed(folio); + } + + gup_fill_pages(pages, page, nr); + + return 0; +} + static long follow_page_pte(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, unsigned int flags, struct page **pages) @@ -976,36 +1020,10 @@ static long follow_page_pte(struct vm_area_struct *v= ma, VM_WARN_ON_ONCE_PAGE((flags & FOLL_PIN) && PageAnon(page) && !PageAnonExclusive(page), page); =20 - /* try_grab_folio() does nothing unless FOLL_GET or FOLL_PIN is set. */ - ret =3D try_grab_folio(folio, 1, flags); - if (unlikely(ret)) + ret =3D gup_grab_fill_folio_pages(folio, page, 1, + pte_dirty(pte), flags, pages); + if (ret) goto out; - - /* - * We need to make the page accessible if and only if we are going - * to access its content (the FOLL_PIN case). Please see - * Documentation/core-api/pin_user_pages.rst for details. - */ - if (flags & FOLL_PIN) { - ret =3D arch_make_folio_accessible(folio); - if (ret) { - gup_put_folio(folio, 1, flags); - goto out; - } - } - if (flags & FOLL_TOUCH) { - if ((flags & FOLL_WRITE) && - !pte_dirty(pte) && !folio_test_dirty(folio)) - folio_mark_dirty(folio); - /* - * pte_mkyoung() would be more correct here, but atomic care - * is needed to avoid losing the dirty bit: it is easier to use - * folio_mark_accessed(). - */ - folio_mark_accessed(folio); - } - - gup_fill_pages(pages, page, 1); ret =3D 1; out: pte_unmap_unlock(ptep, ptl); --=20 2.56.0 From nobody Tue Nov 24 11:35:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463787; b=WUTC6yxCSbb8c7zXd7BdDVfxLwsEY6JaPBbxDJigSMIL7GOXd1WA/HMuh8xNdnzsRueT I3lCx5s3mycUBvi3DkWxggKVTUmlYXfS47juToH9uTAVnSdyG81VhsCy3H2BAz57ehwSh 0xhEU72zhVg56LJg3UiDDkp5RlOwfRw2SlSqXad1xW4pTRVq9te3QStp2cve1JtUwho3v 5Wncdg4xVUYEflPquJRl3DPX/qEhK2g14rixYPjova0mKSU9JtpYXtkX/axgRMjeC7oNU sD7HXtmXNb3oigZ+pHy6JTZ5hLPRiJjHQnxa8XGj9Xe4rQe8tae0Xh8zCoMFcsdufrg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463787; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=N3nyf5W47UgYpP4B9F86Yz8ZQ6TUVBJoCHj4TQFdYoU=; b=CKgqimx39efKyLRXq4/aV3n3KcqvhffJ3EYEK7O5pvO4l2WasNLbbd1aas+oUVn5OpSD pXacnQh1Q2xsHFoK2uhTqLxeGcBMuoYNhyYkAQKWAZFzzeIm4j1oT7WCARu6VbCKuGAf4 ZRb3bTvW0S5eLequp576LMNEyvsNvkJCxufSlTlUn3HQwY94twSetABe1/ExPI4UxMluM 7UqDILkhPTKSu+BqoRYP1u1BqU/58tLQEdGlDekNxr/XbrEYs5w3hCHtZhvR/npc/1XRn J8c1PDGfIIXwybAcsf3fHgMiVGBkLV4ty2wd8BydOecyjpf1BDzJOH16g6OsXibOG3A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b="O/Cr/Jpc"; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2BB7606A336 for ; Mon, 23 Nov 2026 19:56:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2BB7606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=O/Cr/Jpc DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=N3nyf5W47UgYpP4B9F86Yz8ZQ6TUVBJoCHj4TQFdYoU=; b=O/Cr/J pcnrcCOeJU4KglvusKc3MxzjYXyI2+j2R+hIPxRRd45J/Xozq6YBmtZmjyrnVMjfCt/k64Jf5+1lF hyMI7GoHv7FEyf4uKFXV20bGK73x0gN/PHAI9m3nQdkSvjJlV6LZcpexVuOxuxxaGwDG2e/l089dJ FAZ7f5nr3cJgr2uG6fWzz9vEGRUGuE2lmOdsf076j6rsexZNVGer9sDbymGyGHHEi9aZEpq12z/96 JjgOMQsHcRfsSTAcsx0d71OVlC01X9QExgnCTgDoByPt4UvG0ZtclHHp+cXP41TxOa+AIBZ1qaZon QYriNr74Pjv1WhtjMHNeBVpx/0qA==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-1i1D; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 5/9] mm/gup: break out gup_lookup_pte() helper Date: Mon, 23 Nov 2026 14:56:05 -0500 Message-ID: <20261123195609.4115244-6-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" follow_page_pte() does two things: resolve one present PTE to the page it maps, then grab a folio reference and hand the page back. Split the first part into gup_lookup_pte(), which returns no page and no error when the PTE cannot be followed but the caller may fault it in, and a negative errno when the caller must report the failure. A none PTE in a hugetlbfs VMA can only be resolved to the page cache outside the PTE lock, so it is deferred until after unlock. No functional changes intended. Suggested-by: David Hildenbrand Signed-off-by: Rik van Riel --- mm/gup.c | 102 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index d2097ecd1576..54a710f1a843 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -965,74 +965,88 @@ static long gup_grab_fill_folio_pages(struct folio *f= olio, return 0; } =20 -static long follow_page_pte(struct vm_area_struct *vma, - unsigned long address, pmd_t *pmd, unsigned int flags, - struct page **pages) +/* + * Resolve one present PTE to the page it maps. Returns no page and no err= or + * when the PTE cannot be followed but the caller may fault it in, and a + * negative errno when the caller must report the failure. + */ +static long gup_lookup_pte(struct vm_area_struct *vma, unsigned long addre= ss, + pte_t *ptep, pte_t pte, unsigned int flags, struct page **pagep) { - struct mm_struct *mm =3D vma->vm_mm; - struct folio *folio; struct page *page; - spinlock_t *ptl; - pte_t *ptep, pte; - long ret; =20 - ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); - if (!ptep) - return no_page_table(vma, flags, address); - pte =3D ptep_get(ptep); + *pagep =3D NULL; + if (!pte_present(pte)) - goto no_page; + return 0; if (pte_protnone(pte) && !gup_can_follow_protnone(vma, flags)) - goto no_page; + return 0; =20 page =3D vm_normal_page(vma, address, pte); =20 /* * We only care about anon pages in can_follow_write_pte(). */ - if ((flags & FOLL_WRITE) && - !can_follow_write_pte(pte, page, vma, flags)) { - ret =3D 0; - goto out; - } + if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, page, vma, flags)) + return 0; =20 if (unlikely(!page)) { if (flags & FOLL_DUMP) { /* Avoid special (like zero) pages in core dumps */ - ret =3D -EFAULT; - goto out; - } - - if (is_zero_pfn(pte_pfn(pte))) { - page =3D pte_page(pte); - } else { - ret =3D follow_pfn_pte(vma, address, ptep, flags); - goto out; + return -EFAULT; } + if (!is_zero_pfn(pte_pfn(pte))) + return follow_pfn_pte(vma, address, ptep, flags); + page =3D pte_page(pte); } - folio =3D page_folio(page); =20 - if (!pte_write(pte) && gup_must_unshare(vma, flags, folio, page)) { - ret =3D -EMLINK; - goto out; - } + if (!pte_write(pte) && + gup_must_unshare(vma, flags, page_folio(page), page)) + return -EMLINK; =20 VM_WARN_ON_ONCE_PAGE((flags & FOLL_PIN) && PageAnon(page) && !PageAnonExclusive(page), page); =20 - ret =3D gup_grab_fill_folio_pages(folio, page, 1, - pte_dirty(pte), flags, pages); - if (ret) - goto out; - ret =3D 1; -out: + *pagep =3D page; + return 0; +} + +static long follow_page_pte(struct vm_area_struct *vma, + unsigned long address, pmd_t *pmd, unsigned int flags, + struct page **pages) +{ + struct mm_struct *mm =3D vma->vm_mm; + struct page *page; + spinlock_t *ptl; + pte_t *ptep, pte; + long ret; + + ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); + /* The PMD entry is empty. */ + if (!ptep) + return no_page_table(vma, flags, address); + pte =3D ptep_get(ptep); + + /* + * no_page_table() may access the page cache for a hugetlb VMA, + * which cannot be done under the PTL, so defer it until the + * lock is dropped. + */ + if (pte_none(pte)) { + pte_unmap_unlock(ptep, ptl); + return no_page_table(vma, flags, address); + } + + ret =3D gup_lookup_pte(vma, address, ptep, pte, flags, &page); + if (!ret && page) { + ret =3D gup_grab_fill_folio_pages(page_folio(page), page, + 1, pte_dirty(pte), flags, pages); + if (!ret) + ret =3D 1; + } + pte_unmap_unlock(ptep, ptl); return ret; -no_page: - pte_unmap_unlock(ptep, ptl); - if (!pte_none(pte)) - return 0; - return no_page_table(vma, flags, address); } =20 static long follow_pmd_mask(struct vm_area_struct *vma, --=20 2.56.0 From nobody Tue Nov 24 11:35:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463787; b=iFYfQCtMCMVWTVa4/ZTsLgDLo3iFlIEerRjt4GbcG7GOdPC9ZPRAz130nqkCpAX+XKRD O0CzeJF1IAhK7vAuvT1qpPm11DqyY8SRboVfc9l5xiQeY/AUUwoTiE2VwvwVbkeFTn11i oYVApi75E+07vBeMH9SNp2xVXsedc5Snh8fCn+Sp4MIsKV/KegmMy9MhzpjR/Lk3keQiG GfWKdx6hgINfePHtX/EuPl4Osbxmb0bClkDNIpAoAF9MadLf9e5wuHQ+mkGax6fY4/Pzy XE5nMrR1UDfuPgY77e7coPonJr62nrz62v0OxAdhbhzTGDz5Tg/qwkk28DlB5Oh3yoQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463787; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=iBE/HJPHC78fW1HAKwGMiqdRMq4UB3OsvluU9x/JAOc=; b=kDcGUCa154v2JZAeqOwm3adYpA/Exdv2OMzD5K1wI2F7hc0rnGwnVJKVOfRAuXcEHjBW rWZCK525CAndqTCNKun4nO8bbY6a09bw5/tqQw4weAu4AyU1fdkBNuAlaf8npsXEHn7Q5 J5aVF1EMZde9p9N/mD5OU7FuPuezbKtzlY8aqwPblwjy+tg8F4jgQroB9yy65kmxaSclf bX5MT5tNAyRDloiNYSJmOSp+KwtlCQVuH4I9+CXNoc5DS7AP7q7dop1W9I5pY5RUfYhQz zg5r6tO7xaEQKaGT9vipo2zX/YGR7Vnnzy4hRKbGy+uzA7ZmoVyhC8iANAdflMEKJow== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=kkyc95kg; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2BD6606A337 for ; Mon, 23 Nov 2026 19:56:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2BD6606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=kkyc95kg DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=iBE/HJPHC78fW1HAKwGMiqdRMq4UB3OsvluU9x/JAOc=; b=kkyc95 kg+K0b2BGfbuM3OOPT22M1F7b7o1ElV2VuTdsAUagK14WKzdeyOD7oSjxi1VNRN/sr98OniNFoIR+ mu512LnU/j56nsMJy9efVunDyBBBsneQSUA7/JohdHNsNCy6peFAN8R6yDnJODKXDCMDjIhS9pJgQ gTBfRxdMVxD0uMQ3Hqbqb5p1FXmk1MtiPAwpTZizjkvH6aIe+AdipHOqsw+xFCMQS7y6oNg2F+VyZ +tKozVLkd/h+/UXzL02oNwsjnKEEDPBRQ7PsZyZyBAwONMJ9SD4Z20x8uxZ1eQJo2Z2luGQ7xlhIp erOEthmTwhw0ap9nW+W/f52DhiiQ==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-1rD9; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 6/9] mm/gup: return a huge page's full count from follow_page_mask() Date: Mon, 23 Nov 2026 14:56:06 -0500 Message-ID: <20261123195609.4115244-7-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" follow_huge_pud()/follow_huge_pmd() already know the huge page's full size but report it via a separate *page_mask output; __get_user_pages() does a second try_grab_folio() call and subpage loop for everything past the first page. Have the huge paths report their count as the return value instead, clamped to the huge page's size and @end. The merged grab returns whatever error try_grab_folio() gives, instead of forcing -EFAULT on the second call's failure. *page_mask and __get_user_pages()'s second-grab/subpage loop are now dead; remove them. The old silent page_increm clamp becomes a VM_WARN_ON_ONCE, since @end already bounds the count and refs/pages[] are already committed by the time the caller sees it -- truncating here would leak references, not just waste a comparison. follow_page_pte() is unaffected, still returning at most 1 page. The -EEXIST path needs an explicit nr =3D 1 when pages =3D=3D NULL: it used= to get that from page_mask staying 0 for a PFN-special PTE, but nr holds -EEXIST there, which would grow nr_pages instead of shrinking it. mm/gup_test.c (PIN_LONGTERM_BENCHMARK) shows no measurable change; the lock-hold-time improvement is by inspection, not measurement. Suggested-by: David Hildenbrand Signed-off-by: Rik van Riel --- mm/gup.c | 153 +++++++++++++++++++++---------------------------------- 1 file changed, 57 insertions(+), 96 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 54a710f1a843..57faf0165385 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -761,15 +761,15 @@ static inline bool can_follow_write_pud(pud_t pud, st= ruct page *page, } =20 static long follow_huge_pud(struct vm_area_struct *vma, - unsigned long addr, pud_t *pudp, - unsigned int flags, unsigned long *page_mask, - struct page **pages) + unsigned long addr, unsigned long end, pud_t *pudp, + unsigned int flags, struct page **pages) { struct mm_struct *mm =3D vma->vm_mm; struct page *page; struct folio *folio; pud_t pud =3D *pudp; unsigned long pfn =3D pud_pfn(pud); + unsigned long off, nr; int ret; =20 assert_spin_locked(pud_lockptr(mm, pudp)); @@ -778,22 +778,22 @@ static long follow_huge_pud(struct vm_area_struct *vm= a, !can_follow_write_pud(pud, pfn_to_page(pfn), vma, flags)) return 0; =20 - pfn +=3D (addr & ~PUD_MASK) >> PAGE_SHIFT; + off =3D PFN_DOWN(addr & ~PUD_MASK); + pfn +=3D off; page =3D pfn_to_page(pfn); folio =3D page_folio(page); =20 if (!pud_write(pud) && gup_must_unshare(vma, flags, folio, page)) return -EMLINK; =20 - ret =3D try_grab_folio(folio, 1, flags); + nr =3D min(HPAGE_PUD_NR - off, PFN_DOWN(end - addr)); + + ret =3D try_grab_folio(folio, nr, flags); if (ret) return ret; =20 - *page_mask =3D HPAGE_PUD_NR - 1; - gup_fill_pages(pages, page, 1); - - return 1; + return nr; } =20 /* FOLL_FORCE can write to even unwritable PMDs in COW mappings. */ @@ -815,14 +815,14 @@ static inline bool can_follow_write_pmd(pmd_t pmd, st= ruct page *page, } =20 static long follow_huge_pmd(struct vm_area_struct *vma, - unsigned long addr, pmd_t *pmd, - unsigned int flags, unsigned long *page_mask, - struct page **pages) + unsigned long addr, unsigned long end, pmd_t *pmd, + unsigned int flags, struct page **pages) { struct mm_struct *mm =3D vma->vm_mm; pmd_t pmdval =3D *pmd; struct page *page; struct folio *folio; + unsigned long off, nr; int ret; =20 assert_spin_locked(pmd_lockptr(mm, pmd)); @@ -847,7 +847,10 @@ static long follow_huge_pmd(struct vm_area_struct *vma, VM_WARN_ON_ONCE_PAGE((flags & FOLL_PIN) && PageAnon(page) && !PageAnonExclusive(page), page); =20 - ret =3D try_grab_folio(folio, 1, flags); + off =3D PFN_DOWN(addr & ~HPAGE_PMD_MASK); + nr =3D min(HPAGE_PMD_NR - off, PFN_DOWN(end - addr)); + + ret =3D try_grab_folio(folio, nr, flags); if (ret) return ret; =20 @@ -856,27 +859,24 @@ static long follow_huge_pmd(struct vm_area_struct *vm= a, touch_pmd(vma, addr, pmd, flags & FOLL_WRITE); #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 - page +=3D (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; - *page_mask =3D HPAGE_PMD_NR - 1; + page +=3D off; =20 gup_fill_pages(pages, page, 1); =20 - return 1; + return nr; } =20 #else /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ static long follow_huge_pud(struct vm_area_struct *vma, - unsigned long addr, pud_t *pudp, - unsigned int flags, unsigned long *page_mask, - struct page **pages) + unsigned long addr, unsigned long end, pud_t *pudp, + unsigned int flags, struct page **pages) { return 0; } =20 static long follow_huge_pmd(struct vm_area_struct *vma, - unsigned long addr, pmd_t *pmd, - unsigned int flags, unsigned long *page_mask, - struct page **pages) + unsigned long addr, unsigned long end, pmd_t *pmd, + unsigned int flags, struct page **pages) { return 0; } @@ -1050,9 +1050,8 @@ static long follow_page_pte(struct vm_area_struct *vm= a, } =20 static long follow_pmd_mask(struct vm_area_struct *vma, - unsigned long address, pud_t *pudp, pud_t pud, - unsigned int flags, unsigned long *page_mask, - struct page **pages) + unsigned long address, unsigned long end, pud_t *pudp, pud_t pud, + unsigned int flags, struct page **pages) { pmd_t *pmd, pmdval; spinlock_t *ptl; @@ -1091,15 +1090,14 @@ static long follow_pmd_mask(struct vm_area_struct *= vma, return pte_alloc(mm, pmd) ? -ENOMEM : follow_page_pte(vma, address, pmd, flags, pages); } - ret =3D follow_huge_pmd(vma, address, pmd, flags, page_mask, pages); + ret =3D follow_huge_pmd(vma, address, end, pmd, flags, pages); spin_unlock(ptl); return ret; } =20 static long follow_pud_mask(struct vm_area_struct *vma, - unsigned long address, p4d_t *p4dp, - unsigned int flags, unsigned long *page_mask, - struct page **pages) + unsigned long address, unsigned long end, p4d_t *p4dp, + unsigned int flags, struct page **pages) { pud_t *pudp, pud; spinlock_t *ptl; @@ -1111,8 +1109,7 @@ static long follow_pud_mask(struct vm_area_struct *vm= a, if (!pud_present(pud)) return no_page_table(vma, flags, address); if (!pud_leaf(pud)) - return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask, - pages); + return follow_pmd_mask(vma, address, end, pudp, pud, flags, pages); =20 ptl =3D pud_lock(mm, pudp); pud =3D *pudp; @@ -1122,19 +1119,17 @@ static long follow_pud_mask(struct vm_area_struct *= vma, } if (unlikely(!pud_leaf(pud))) { spin_unlock(ptl); - return follow_pmd_mask(vma, address, pudp, pud, flags, page_mask, - pages); + return follow_pmd_mask(vma, address, end, pudp, pud, flags, pages); } =20 - ret =3D follow_huge_pud(vma, address, pudp, flags, page_mask, pages); + ret =3D follow_huge_pud(vma, address, end, pudp, flags, pages); spin_unlock(ptl); return ret; } =20 static long follow_p4d_mask(struct vm_area_struct *vma, - unsigned long address, pgd_t *pgdp, - unsigned int flags, unsigned long *page_mask, - struct page **pages) + unsigned long address, unsigned long end, pgd_t *pgdp, + unsigned int flags, struct page **pages) { p4d_t *p4dp, p4d; =20 @@ -1145,18 +1140,18 @@ static long follow_p4d_mask(struct vm_area_struct *= vma, if (!p4d_present(p4d) || p4d_bad(p4d)) return no_page_table(vma, flags, address); =20 - return follow_pud_mask(vma, address, p4dp, flags, page_mask, pages); + return follow_pud_mask(vma, address, end, p4dp, flags, pages); } =20 /** - * follow_page_mask - look up a page descriptor from a user-virtual address + * follow_page_mask - look up pages at a user-virtual address * @vma: vm_area_struct mapping @address * @address: virtual address to look up + * @end: virtual address at which to stop batching contiguous pages * @flags: flags modifying lookup behaviour - * @page_mask: a pointer to output page_mask - * @pages: array to receive the page found, refcounted per @flags, or NULL - * to walk the page tables (e.g. to fault pages in) without - * collecting or refcounting them + * @pages: array to receive the pages, refcounted per @flags, or NULL to + * walk the page tables (e.g. to fault pages in) without collecting + * or refcounting them * * @flags can have FOLL_ flags set, defined in * @@ -1165,15 +1160,15 @@ static long follow_p4d_mask(struct vm_area_struct *= vma, * trigger a fault with FAULT_FLAG_UNSHARE set. Note that unsharing is only * relevant with FOLL_PIN and !FOLL_WRITE. * - * On output, @page_mask is set according to the size of the page. - * - * Return: 1 with @pages[0] filled in if a page was found, 0 if no mapping - * exists at @address, or a negative errno for a mapping to something not - * represented by a page descriptor (see also vm_normal_page()). + * Return: the number of contiguous pages starting at @address that were + * placed into @pages (if non-NULL), which may be fewer than the pages + * requested via @end; 0 if no mapping exists at @address; or a negative + * errno for a mapping to something not represented by a page descriptor + * (see also vm_normal_page()). */ static long follow_page_mask(struct vm_area_struct *vma, - unsigned long address, unsigned int flags, - unsigned long *page_mask, struct page **pages) + unsigned long address, unsigned long end, + unsigned int flags, struct page **pages) { pgd_t *pgd; struct mm_struct *mm =3D vma->vm_mm; @@ -1181,13 +1176,12 @@ static long follow_page_mask(struct vm_area_struct = *vma, =20 vma_pgtable_walk_begin(vma); =20 - *page_mask =3D 0; pgd =3D pgd_offset(mm, address); =20 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) ret =3D no_page_table(vma, flags, address); else - ret =3D follow_p4d_mask(vma, address, pgd, flags, page_mask, pages); + ret =3D follow_p4d_mask(vma, address, end, pgd, flags, pages); =20 vma_pgtable_walk_end(vma); =20 @@ -1525,7 +1519,6 @@ static long __get_user_pages(struct mm_struct *mm, { long ret =3D 0, i =3D 0; struct vm_area_struct *vma =3D NULL; - unsigned long page_mask =3D 0; =20 if (!nr_pages) return 0; @@ -1540,7 +1533,6 @@ static long __get_user_pages(struct mm_struct *mm, =20 do { struct page *page; - unsigned int page_increm; long nr; =20 /* first iteration or cross vma bound */ @@ -1571,7 +1563,6 @@ static long __get_user_pages(struct mm_struct *mm, if (pages) pages[i] =3D page; nr =3D 1; - page_increm =3D 1; goto fill_flush; } =20 @@ -1594,8 +1585,8 @@ static long __get_user_pages(struct mm_struct *mm, } cond_resched(); =20 - nr =3D follow_page_mask(vma, start, gup_flags, &page_mask, - pages ? &pages[i] : NULL); + nr =3D follow_page_mask(vma, start, start + nr_pages * PAGE_SIZE, + gup_flags, pages ? &pages[i] : NULL); if (!nr || nr =3D=3D -EMLINK) { ret =3D faultin_page(vma, start, gup_flags, nr =3D=3D -EMLINK, locked); @@ -1617,50 +1608,20 @@ static long __get_user_pages(struct mm_struct *mm, * Proper page table entry exists, but no corresponding * struct page. If the caller expects **pages to be * filled in, bail out now, because that can't be done - * for this page. + * for this page. Otherwise, skip to the next page. */ if (pages) { ret =3D nr; goto out; } + nr =3D 1; } else if (nr < 0) { ret =3D nr; goto out; } =20 - page_increm =3D 1 + (~(start >> PAGE_SHIFT) & page_mask); - if (page_increm > nr_pages) - page_increm =3D nr_pages; - - /* - * This must be a large folio (and doesn't need to - * be the whole folio; it can be part of it), do - * the refcount work for the remaining pages. - * - * NOTE: here the page may not be the head page - * e.g. when start addr is not thp-size aligned. - * try_grab_folio() should have taken care of tail - * pages. - */ - if (pages && page_increm > 1) { - struct folio *folio =3D page_folio(pages[i]); - - /* - * Since we already hold refcount on the - * large folio, this should never fail. - */ - if (try_grab_folio(folio, page_increm - 1, - gup_flags)) { - /* - * Release the 1st page ref if the - * folio is problematic, fail hard. - */ - gup_put_folio(folio, 1, gup_flags); - ret =3D -EFAULT; - goto out; - } - - } + /* Check that we didn't pin more pages than the caller will free. */ + VM_WARN_ON_ONCE(nr > nr_pages); =20 fill_flush: /* @@ -1669,13 +1630,13 @@ static long __get_user_pages(struct mm_struct *mm, * the caches without any page table lock held. */ if (pages) { - gup_fill_pages(pages + i, pages[i], page_increm); - gup_flush_pages(vma, start, pages + i, page_increm); + gup_fill_pages(pages + i, pages[i], nr); + gup_flush_pages(vma, start, pages + i, nr); } =20 - i +=3D page_increm; - start +=3D page_increm * PAGE_SIZE; - nr_pages -=3D page_increm; + i +=3D nr; + start +=3D nr * PAGE_SIZE; + nr_pages -=3D nr; } while (nr_pages); out: return i ? i : ret; --=20 2.56.0 From nobody Tue Nov 24 11:35:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463787; b=2G2PK3T8E59v604OTP+xD9u9B337VyN0Altp56GSTS24I+X/VeRxMOthJP94I/vn8EsH OUqqUQilc0u9s2Osmv6tAX6go+EoQdaj9z/+D+o6klhvrs7i2WXYeryLXqb5ZuuxOqQd2 ydW+KEIlwtq/eQmfGzWpZsqdXCQ6XecdxjV8IxjV2+ex/M4LN671/c0n2vV8EpXfzvOJ4 gwS/rPEQkT7AszPX25y+pXZQ/0DEpyU1Xu0Q2LKj99Q/32mysDVs5aoZSI46aX5u0JWhp HUgiBQe5Mlbx90zu/ITqGC84pvnT26P+Tp1h/PDeWD95tv+m3vgdrsb7WwSJdan0iRw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463787; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=09Qoe43PT4Iegq/2tV6EDeu/7nFFELs7zlPH6GSP538=; b=xnXpBWtxsEj/ruKV8BUUb52NyH2PnZQEMbJgbiWL2GWRWJRlUcHvMCVcjC3vXo2g4fCR 7DPW15gLtN1hv8X93lrOKKPfJVZOUflB9/owGO9PW/rV1RcJRsD7zyJWVTqXEZ5wGzUTM 8M5Dls0ZGRnSZkLY7HwtZkbG/xmAtJKY2/l4RuJe+CmGjj19amErn4ZbW52BoknXFqnOn khtoBkdYyPKX3LVpcI4yqylup0OFbUWFwEHeqnnHfu/ZB6MGa5LJA13J0vxL1zPfm7ESn 5cZadNyTJeTO8OuSmIfzFU0xYdvW5Imq7gdmrDV0dX5lZ4+ZZls9AELdaedi/D4noEw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=QkrF3T6H; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E78C606A331 for ; Mon, 23 Nov 2026 19:56:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E78C606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=QkrF3T6H DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=09Qoe43PT4Iegq/2tV6EDeu/7nFFELs7zlPH6GSP538=; b=QkrF3T 6HtDiNETIOIWuNnNM+k2VV+lrscYx8EBpB8UbzgK2LkJMNLscxpwc8shZjJGh6FqgwRd3e4UggVET L/ferf0OlEwqveWg/SHG6gHVp3nSbUSn85IEr4m7B2uITaWwVCGbpUIXOEb/NnOQY7/g94VTz8WDZ ARCr6/+FPy7P6eh44Nj3swzrIEJH6Z88KV+wnxq4fnQFYrbVIBSZ6C/qmtia34ZAkH/rRlKEmZ7hV 83oF7bV1jyR/6HhTZFNKVCBnkGKeE6Vo2y0nul6jx+mNwMc03p9YoYPzj1FqDPGUXJY7+dlgENgsy JPgtnHM3PVw5kvFWwbACoFuJrV4A==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-1zAH; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 7/9] mm/gup: walk multiple PTEs per follow_page_pte() call Date: Mon, 23 Nov 2026 14:56:07 -0500 Message-ID: <20261123195609.4115244-8-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Slow GUP resolves and commits one PTE per follow_page_pte() call, so __get_user_pages() restarts the pgd/p4d/pud/pmd descent and retakes the PTE lock for every page. Walk the PTEs from the start address to the end of this page table or VMA, whichever comes first, and commit every page found. The walk end is computed from the inclusive end like folio_pte_range() does, not via pmd_addr_end(), which does not clamp on 2-level page tables and can wrap on unsigned-long overflow. A none PTE in a hugetlbfs VMA can only be resolved to the page cache outside the PTE lock, so it is deferred until after unlock. A failure on the first PTE is returned to the caller. A failure after that is a short read; __get_user_pages() retrying the read gets the error. Suggested-by: David Hildenbrand Signed-off-by: Rik van Riel --- mm/gup.c | 95 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 34 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 57faf0165385..b5747f2c2ca3 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -792,7 +792,7 @@ static long follow_huge_pud(struct vm_area_struct *vma, if (ret) return ret; =20 - gup_fill_pages(pages, page, 1); + gup_fill_pages(pages, page, nr); return nr; } =20 @@ -861,7 +861,7 @@ static long follow_huge_pmd(struct vm_area_struct *vma, =20 page +=3D off; =20 - gup_fill_pages(pages, page, 1); + gup_fill_pages(pages, page, nr); =20 return nr; } @@ -1011,41 +1011,69 @@ static long gup_lookup_pte(struct vm_area_struct *v= ma, unsigned long address, return 0; } =20 +/* + * Walk the PTEs from the start address to the end of this page table or V= MA, + * whichever comes first, and commit every page found. + * + * A failure on the first PTE is returned to the caller. A failure after t= hat + * is a short read; __get_user_pages() retrying the read will get the erro= r. + * + * Return: the number of pages stored in @pages, 0 when no mapping exists = at + * the start address, or a negative errno for a failure on the first PTE. + */ static long follow_page_pte(struct vm_area_struct *vma, - unsigned long address, pmd_t *pmd, unsigned int flags, - struct page **pages) + unsigned long address, unsigned long end, pmd_t *pmd, + unsigned int flags, struct page **pages) { struct mm_struct *mm =3D vma->vm_mm; - struct page *page; + bool pte_is_none =3D false; + pte_t *ptep, *orig_ptep; + unsigned long walk_end; + unsigned long nr =3D 0; spinlock_t *ptl; - pte_t *ptep, pte; - long ret; + long ret =3D 0; =20 - ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); + orig_ptep =3D ptep =3D pte_offset_map_lock(mm, pmd, address, &ptl); /* The PMD entry is empty. */ if (!ptep) return no_page_table(vma, flags, address); - pte =3D ptep_get(ptep); =20 - /* - * no_page_table() may access the page cache for a hugetlb VMA, - * which cannot be done under the PTL, so defer it until the - * lock is dropped. - */ - if (pte_none(pte)) { - pte_unmap_unlock(ptep, ptl); - return no_page_table(vma, flags, address); - } + /* Clamp the walk to the VMA end or PMD boundary. */ + walk_end =3D min((address & PMD_MASK) + PTRS_PER_PTE * PAGE_SIZE, + vma->vm_end); + if (end > address && walk_end > end) + walk_end =3D end; + + for (; address < walk_end; address +=3D PAGE_SIZE, ptep++) { + pte_t pte =3D ptep_get(ptep); + struct page *page; =20 - ret =3D gup_lookup_pte(vma, address, ptep, pte, flags, &page); - if (!ret && page) { - ret =3D gup_grab_fill_folio_pages(page_folio(page), page, - 1, pte_dirty(pte), flags, pages); - if (!ret) - ret =3D 1; + /* + * no_page_table() may access the page cache for a + * hugetlb VMA, which cannot be done under the PTL, + * so defer it until the lock is dropped. + */ + if (pte_none(pte)) { + pte_is_none =3D true; + break; + } + + ret =3D gup_lookup_pte(vma, address, ptep, pte, flags, &page); + if (ret || !page) + break; + ret =3D gup_grab_fill_folio_pages(page_folio(page), page, 1, + pte_dirty(pte), flags, + pages ? pages + nr : NULL); + if (ret) + break; + nr++; } + pte_unmap_unlock(orig_ptep, ptl); =20 - pte_unmap_unlock(ptep, ptl); + if (nr) + return nr; + if (pte_is_none) + return no_page_table(vma, flags, address); return ret; } =20 @@ -1068,7 +1096,7 @@ static long follow_pmd_mask(struct vm_area_struct *vm= a, if (!pmd_present(pmdval)) return no_page_table(vma, flags, address); if (likely(!pmd_leaf(pmdval))) - return follow_page_pte(vma, address, pmd, flags, pages); + return follow_page_pte(vma, address, end, pmd, flags, pages); =20 if (pmd_protnone(pmdval) && !gup_can_follow_protnone(vma, flags)) return no_page_table(vma, flags, address); @@ -1081,14 +1109,14 @@ static long follow_pmd_mask(struct vm_area_struct *= vma, } if (unlikely(!pmd_leaf(pmdval))) { spin_unlock(ptl); - return follow_page_pte(vma, address, pmd, flags, pages); + return follow_page_pte(vma, address, end, pmd, flags, pages); } if (pmd_trans_huge(pmdval) && (flags & FOLL_SPLIT_PMD)) { spin_unlock(ptl); split_huge_pmd(vma, pmd, address); /* If pmd was left empty, stuff a page table in there quickly */ return pte_alloc(mm, pmd) ? -ENOMEM : - follow_page_pte(vma, address, pmd, flags, pages); + follow_page_pte(vma, address, end, pmd, flags, pages); } ret =3D follow_huge_pmd(vma, address, end, pmd, flags, pages); spin_unlock(ptl); @@ -1625,14 +1653,13 @@ static long __get_user_pages(struct mm_struct *mm, =20 fill_flush: /* - * The first page is already stored and references on the - * whole run are held; fill the rest of the array and flush - * the caches without any page table lock held. + * The run is already stored and references on all of it are + * held; the array may span several folios, so it cannot be + * re-derived from the first page here. Only flush the caches + * without any page table lock held. */ - if (pages) { - gup_fill_pages(pages + i, pages[i], nr); + if (pages) gup_flush_pages(vma, start, pages + i, nr); - } =20 i +=3D nr; start +=3D nr * PAGE_SIZE; --=20 2.56.0 From nobody Tue Nov 24 11:35:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463786; b=Iig4OQA1Akxi3sVHkWT7kgv/V5z6IM4LzbITYGypxtAVeif1oLCyQZEgT6a7SC6EYfXt NFgRZTvdDQcbyC/KGzOTnu22FNn+K/mC05OFVuIXeeFdbWAM5mdEwh8qmEYBezJC4bUAh G+J4+lIFr20q1lJJcpSNFltMQCBPuNdHlKpKb6Bsvq4xGW+wT1lFZJuJCCdyOIJjBrdW9 IV9PWVKI9udiOti4h0eJh3DnHO/8Qcv18n1LLj8+g+PNd4V+aqWsl5qvfZ4IIZIsS4Sx1 LJx5nVN9+D/mBqd0DKeAI9jIGdNmVEDx9vbHh0DXQrs4d+MNHC4itMA2WPejFqHe5Xw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463786; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Can82GwJLZ6xNFloN0UvipHW26+kIvgtH3oyrupC710=; b=qDJkX9Nz+rcRvPC2dj6zklHfDTlXFPmd1nncydfqOisDSuWSbmuw//a4Y0xxE2OwUv2S 9M3HK5/JFdip1tHmHWq0Ir0MSbZ2IsXztKEiqdrwPIhkl7JVNODNvSp6FALRld1KAhmoX tnSPdX45JAKjubJ1ou5KnOeqTbe+aaqdGNZ1BU+m42IJhyRZdB8vZeDFaDyNR+r4rC4t/ 46R8foVg5C/uyKAo6z+skjQCSznx/pEa91rKPaoxLHslRnJNwx0HUh0jDICAQKT+8VQIv LzYE7uDFKDzU2+uNHk5mMBtXI6Z46vjZcJfhYka/ryJB7n9uEr8zmKIxT4/gkuubD7A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=GyCi9MXd; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E6D1606A32A for ; Mon, 23 Nov 2026 19:56:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E6D1606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=GyCi9MXd DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=Can82GwJLZ6xNFloN0UvipHW26+kIvgtH3oyrupC710=; b=GyCi9M XdMGJPLYKb681Mul/PozoVN4IhEIQlzk2y4fhyibojn2D4rnbJztGxsCKbCamUO2H0/aBjW13Af+R QGtBTPfimG+/LUIug75QleEtpH5Cg9MfRHuWX1Zn3eYjBEwiIQMfAXji5jZ2b26Py2H/AlKMJ+NDS UUhuoSPpmM8jImd+IMld6fQUGG7MHXTgolYiF56a/qAaqz5x2iV199RbmOayWnco7zhJbUsfLsA5p k2lYz08oYTnxS3p5CIbBAUMNM24gCv/TdIGhPLJ0RzmSj/i6MkBS3msbIqqCv5rPsJM2XKMBNdUTT IeLUsWP1pOaImNhHX/YTPMXEgwKg==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-25zi; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 8/9] mm/gup: batch contiguous same-folio PTEs into one refcount grab Date: Mon, 23 Nov 2026 14:56:08 -0500 Message-ID: <20261123195609.4115244-9-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" follow_page_pte() now walks a whole page table in one call, but still resolves and commits each PTE on its own, so a PTE-mapped large folio (mTHP) pays one try_grab_folio() per subpage. Add gup_pte_batch_len() and give gup_grab_folio_pages() a run length. Each run starts with a full per-PTE resolve, then gup_pte_batch_len() finds how many more PTEs extend it with a pte_same() scan rather than a re-derivation of each page, and the run is committed with one refcount grab. Runs stop at folio boundaries, so plain base pages are unaffected. Gather the dirty bits from all the PTEs in a batch, in order to mark the folio dirty for FOLL_WRITE. Same benchmark as the previous change, with before again taken at the base of the series: gup_test -L -m 256 -n 65536 -r 16 -t before after 64 kB mTHP 2946 us 231 us (12.8x) 4 kB base (control) 2753 us 1198 us (2.3x) 2 MB THP (control) 72 us 71 us The 4 kB column is the previous change's 2.3x, unchanged: base pages share no folio, so there is nothing to batch. 64 kB mTHP goes from that change's 2.2x to 12.8x, a further 5.9x from committing a run with one refcount grab instead of one per subpage. With FOLL_TOUCH, folio_mark_accessed() runs once per run instead of once per page: 512 calls before and 32 after over 2 MB of 64 kB folios, leaving them referenced but inactive after the first call. That matches the huge-page and fast paths, which mark once per folio. (Measured by Yuan-Hao Hsu.) Suggested-by: David Hildenbrand Signed-off-by: Rik van Riel --- mm/gup.c | 95 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 19 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index b5747f2c2ca3..886a413b8233 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1011,6 +1011,46 @@ static long gup_lookup_pte(struct vm_area_struct *vm= a, unsigned long address, return 0; } =20 +/* + * Return how many PTEs map consecutive pages of the same folio and can be + * committed as one run. Always at least 1. + * + * The write-fault and unshare checks in gup_lookup_pte() are per PTE, but= a + * writable run needs no repeat: a writable anon page is exclusive. A + * read-only run under FOLL_WRITE or FOLL_PIN does need the per-page check, + * so it stays one page at a time. + * + * @fpb_flags selects the scan: slow GUP merges young/dirty so + * folio_mark_dirty() sees any dirty PTE in the run; fast GUP scans + * locklessly and revalidates the run after grabbing it. @vma is only + * needed for FPB_MERGE_WRITE and may be NULL otherwise. + */ +static unsigned long gup_pte_batch_len(struct vm_area_struct *vma, + unsigned long address, unsigned long walk_end, + struct folio *folio, pte_t *ptep, pte_t *batch_pte, + unsigned int flags, fpb_t fpb_flags) +{ + unsigned long max, nr =3D 1; + + if (folio_test_large(folio)) { + max =3D (walk_end - address) >> PAGE_SHIFT; + if (max > 1) + nr =3D folio_pte_batch_flags(folio, vma, ptep, + batch_pte, max, fpb_flags); + } + + /* + * Read-only runs are trimmed to the leading pages that need no + * unsharing; a zero return means even the head page needs + * unsharing, and the caller must fault it in instead. Writable + * runs need no repeat: a writable anon page is exclusive. + */ + if (!pte_write(*batch_pte)) + nr =3D gup_pages_not_requiring_unshare(vma, flags, folio, + pte_page(*batch_pte), nr); + return nr; +} + /* * Walk the PTEs from the start address to the end of this page table or V= MA, * whichever comes first, and commit every page found. @@ -1046,7 +1086,10 @@ static long follow_page_pte(struct vm_area_struct *v= ma, =20 for (; address < walk_end; address +=3D PAGE_SIZE, ptep++) { pte_t pte =3D ptep_get(ptep); + struct folio *folio; struct page *page; + unsigned long batch; + pte_t batch_pte; =20 /* * no_page_table() may access the page cache for a @@ -1061,12 +1104,33 @@ static long follow_page_pte(struct vm_area_struct *= vma, ret =3D gup_lookup_pte(vma, address, ptep, pte, flags, &page); if (ret || !page) break; - ret =3D gup_grab_fill_folio_pages(page_folio(page), page, 1, - pte_dirty(pte), flags, + folio =3D page_folio(page); + batch_pte =3D pte; + + batch =3D gup_pte_batch_len(vma, address, walk_end, folio, + ptep, &batch_pte, flags, + FPB_RESPECT_WRITE | + FPB_MERGE_YOUNG_DIRTY); + /* + * Break out if the helper says the pages are not + * usable as-is. + */ + if (!batch) { + ret =3D 0; + break; + } + ret =3D gup_grab_fill_folio_pages(folio, page, batch, + pte_dirty(batch_pte), flags, pages ? pages + nr : NULL); if (ret) break; - nr++; + nr +=3D batch; + /* + * The loop's own increment covers one PTE; skip + * the rest of the batch. + */ + ptep +=3D batch - 1; + address +=3D (batch - 1) * PAGE_SIZE; } pte_unmap_unlock(orig_ptep, ptl); =20 @@ -3021,8 +3085,6 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pm= d_t *pmdp, pte_t pte =3D ptep_get_lockless(ptep); struct page *page; =20 - nr_batch =3D 1; - /* * Always fallback to ordinary GUP on PROT_NONE-mapped pages: * pte_access_permitted() better should reject these pages @@ -3062,20 +3124,15 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, = pmd_t *pmdp, if (!gup_fast_folio_allowed(folio, flags)) goto out; =20 - if (folio_test_large(folio)) - /* - * The reference acquired above keeps the folio stable - * for the following checks. - */ - nr_batch =3D folio_pte_batch_flags(folio, NULL, ptep, &pte, - (end - addr) >> PAGE_SHIFT, - FPB_LOCKLESS | FPB_RESPECT_WRITE); - - if (!pte_write(pte)) { - nr_batch =3D gup_pages_not_requiring_unshare(NULL, flags, folio, page, = nr_batch); - if (!nr_batch) - goto out; - } + /* + * The reference acquired above keeps the folio stable + * for the following checks. + */ + nr_batch =3D gup_pte_batch_len(NULL, addr, end, folio, + ptep, &pte, flags, + FPB_LOCKLESS | FPB_RESPECT_WRITE); + if (!nr_batch) + goto out; =20 /* * We need to make the page accessible if and only if we are --=20 2.56.0 From nobody Tue Nov 24 11:35:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=96.67.55.147 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795463788; b=wKPoF/rF+q0CGzC6eQEHTZoa0BKkMd5EiUPl2zRa5Yq2FGQK/UcEdlB0X6UBanEeoBGn aojvH+MlmfQbQLqWUQaYU2jnIjln1O8yaH6vMIT2oUWztPu9pSdQIkp7JPKbZpRDz9jAi ZmFu5847qi4MFVDp4w4M7S3Dm/Y1rS5t8BeojnvESc+sOHK5pbWuKCWcJ1Es7jAdK05ad clAs4t0M3bFzPrN3GiQMCGej6ihq1P+EaUzoYRZo1MScrLk9vayamRM7FXSXhvRGX8ITE N37w0PnkSXO88ds6hNF4NtFmx/VtT5aasqF2JdVU1Fzsb2oLTtx6K5jwRMVqqr6+UTg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795463788; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=FIdriMJqvNi8Sms/WZB3l4qxJPg/qzey4m5IhEpr/rA=; b=iJjfZWKfV+yoGc9ULJAXC4e+sv46q+8T/zBXrWLyGcDL/9ThaW6qykFraXCgL9Fg1Dba eAtU1yEvknDBbIDjcE7RIsid7IhMB7GQGaMWeMyZ11ryXc5gnuWNiiXqCi5vwOQaJy6tv U9BzpBuzKSUVN0z2Oqi3AsYklQR3qWGY201cC8RJd/PpEyfVbgfvL0A3j3DEVQakIkQD7 WVA53TXAy81YMOVqW61l92NHrpUzzn8AuMBoy1Zd0DJ1gW4YxnPTqi2+wrXPLjA8DNQVn x78jKKx9rO4BOpd5dBv1+hGNQSLxM2F3bfnUQq+IV/LNwvDIPttf8wK5+g7RMn8ZkBg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=lAfq26yC; arc=none smtp.remote-ip=96.67.55.147 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2C90606A339 for ; Mon, 23 Nov 2026 19:56:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2C90606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.a=rsa-sha256 header.s=mail header.b=lAfq26yC DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID; bh=FIdriMJqvNi8Sms/WZB3l4qxJPg/qzey4m5IhEpr/rA=; b=lAfq26 yCLesStGS1Da8QiWVoFQx7z2GofwOGGAdcZFuVH3KpQb0KJlcM2KQQr2NHgjFqrzfXsslrYYvYoNS zGnJF6RqKpI7ED0DiP1Vgf/yT/eWsmvzAPrOQr0y0oTOw5ZAVOV+etuWGzry9K+mGkqKHcLzv9oR2 SI4tZPrK21c10BjhTj31f88xs9qDsX5pQpWHLgqwUP1Dx1J386vebspdL2IgMPcj1TiqoY/oN9tMB 7ouK6H89vDnLumiApdqNKzeeCWjtY7btqnt/7wrBu3wXMgyiXY+7JW//hl9V1p0TnT9AmLfLg2uK1 dOjeOhgL2TJFfgX9YZLAOa4K1XSQ==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1xVa99-00000006IU7-2C0i; Mon, 23 Nov 2026 19:56:15 +0000 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Suren Baghdasaryan , Lorenzo Stoakes , Christoph Hellwig , Yuan-Hao Hsu , John Hubbard , Jason Gunthorpe , Peter Xu , Rik van Riel Subject: [PATCH 9/9] mm/gup: unshare-check every page of a pte batch Date: Mon, 23 Nov 2026 14:56:09 -0500 Message-ID: <20261123195609.4115244-10-riel@surriel.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123195609.4115244-1-riel@surriel.com> References: <20261123195609.4115244-1-riel@surriel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A slow GUP write pin over a PTE batch can hand back pages that a later write fault replaces under the pin holder: the process writes to the copy, the holder keeps the old page: pin holder process (mm owner) pin all 16 for write batch passes unchecked write to a page fault copies it elsewhere read pinned page -> stale With GUP-fast, FOLL_WRITE | FOLL_PIN can never reach gup_pages_not_requiring_unshare, but slow GUP can reach it with a read-only head PTE. Only pure reads skip the per-page check now; anything else is vetted page by page, so GUP pins the pages the process owns exclusively and faults in the rest one by one. Without this fix, the pin holder can see stale page content, with it the pin and program address map the same page. Tested by faulting a 64K mTHP, shaping it (fork+exit, head write, mprotect read-only), then pinning all 16 with FOLL_FORCE|FOLL_WRITE: before, the pin takes the whole batch with no faults but the program's writes can COW the pages later; with the fix, the pin unshares every page first and the writes land in place. Suggested-by: David Hildenbrand --- mm/gup.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 886a413b8233..ec39e489e131 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -640,11 +640,12 @@ static unsigned long gup_pages_not_requiring_unshare( unsigned int i; =20 /* - * FOLL_WRITE is implicitly handled correctly as the page table entry - * has to be writable -- and if it references (part of) an anonymous - * folio, that part is required to be marked exclusive. + * A read-only PTE can map a shared anonymous page. Pinning + * one without unsharing it first leaves the pin holder behind on + * the old page once a later write fault copies it elsewhere. + * Only short term read accesses can skip the per-page check below. */ - if ((flags & (FOLL_WRITE | FOLL_PIN)) !=3D FOLL_PIN) + if (!(flags & (FOLL_WRITE | FOLL_PIN))) return nr_pages; =20 /* folio_test_anon(folio) is stable until folio gets freed. */ @@ -1015,10 +1016,11 @@ static long gup_lookup_pte(struct vm_area_struct *v= ma, unsigned long address, * Return how many PTEs map consecutive pages of the same folio and can be * committed as one run. Always at least 1. * - * The write-fault and unshare checks in gup_lookup_pte() are per PTE, but= a - * writable run needs no repeat: a writable anon page is exclusive. A - * read-only run under FOLL_WRITE or FOLL_PIN does need the per-page check, - * so it stays one page at a time. + * The scan covers PTE bits only; a read-only run is then trimmed to the + * leading pages that need no unsharing, so a mixed-exclusivity batch + * commits its exclusive prefix and the fault path unshares each + * remaining follower in turn. A zero return means even the head page + * needs unsharing, and the caller must fault it in instead. * * @fpb_flags selects the scan: slow GUP merges young/dirty so * folio_mark_dirty() sees any dirty PTE in the run; fast GUP scans --=20 2.56.0