From patchwork Thu May 25 22:39:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13255748 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 891DBC77B7E for ; Thu, 25 May 2023 22:40:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA026900004; Thu, 25 May 2023 18:40:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E520A900002; Thu, 25 May 2023 18:40:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CCB10900004; Thu, 25 May 2023 18:40:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B9857900002 for ; Thu, 25 May 2023 18:40:09 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 90981AE8AA for ; Thu, 25 May 2023 22:40:09 +0000 (UTC) X-FDA: 80830246938.04.8B0E367 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf15.hostedemail.com (Postfix) with ESMTP id C53E2A000D for ; Thu, 25 May 2023 22:40:06 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AEPqPzko; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf15.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685054406; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=FUrVwlJ5El5UIYEfnTZW9jIZIlAoV+8/20tkEUBC5dE=; b=XujPGrTbd4lUXeNHDqMqVMEKJGhPb1Q8GydL/gECpwnqQ9dROvVlRzhu6ceyuZlC3N+GBi Ye+7yyUXX5OuBVdbCyqHLWQ0gy4llZNMr5l6FudIwZn482+F77Vq/Qx/lPDrfPVdzEpKVT n2AhahKxofwCEWmfvtElDz6wQxoxj6M= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AEPqPzko; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf15.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685054406; a=rsa-sha256; cv=none; b=vM5DhV4OXphq67oIVj4mzwycs9AlFGTCxvxTXBc9O8bcXnSMG/qR+ddrtHE8oXft2fBuyN 1brOEV14uNYmuKaMD5/FflYM8GeSeY/yttRGCDvbKymYR2LZBP6DRoX7O6kZbhcnp+bCL4 VJqUkkGRYumxrUEOm8ktvI7tSCXhhgA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685054406; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FUrVwlJ5El5UIYEfnTZW9jIZIlAoV+8/20tkEUBC5dE=; b=AEPqPzkoCeggbPiRJJyPwyO69Ea+NsbZ0PJbo2K6i/CSKdZqvxAskeBHQLR6fQYCGZpk33 m7ibnjVaRzE45toCCMp+UzEHcitinRdBLwnR0h+IqvpzGcNs50H1PrlwObNaddpbGne6R5 +5YJyiR2FeYTUsjUwCGBBmZoudQD2YY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-628-fTcodurYMs6Ocu2Sqmj11g-1; Thu, 25 May 2023 18:40:02 -0400 X-MC-Unique: fTcodurYMs6Ocu2Sqmj11g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BC01485A5BD; Thu, 25 May 2023 22:40:01 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.39.192.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5EAFC1121314; Thu, 25 May 2023 22:39:59 +0000 (UTC) From: David Howells To: Christoph Hellwig , David Hildenbrand Cc: David Howells , Jens Axboe , Al Viro , Matthew Wilcox , Jan Kara , Jeff Layton , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , Christian Brauner , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton Subject: [RFC PATCH v2 1/3] mm: Don't pin ZERO_PAGE in pin_user_pages() Date: Thu, 25 May 2023 23:39:51 +0100 Message-Id: <20230525223953.225496-2-dhowells@redhat.com> In-Reply-To: <20230525223953.225496-1-dhowells@redhat.com> References: <20230525223953.225496-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Rspamd-Queue-Id: C53E2A000D X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: pxga74nths5p1ngn7313jb9c3muh8hcd X-HE-Tag: 1685054406-139024 X-HE-Meta: U2FsdGVkX1+AnT4CfRDKyd0CaGmTDigiWzFBEUUW+Qzi3Cr0+2yM40bxRPpp4MQ7sJg1hy3ux9FxtVyVJVlejnF/LbYP493SU0FGr9htCiZLFWvLFsaNI/g/ArokKuFTF1X/bRZ3dtx/8S8U2vuWk9IUfzCDexiQql3w0Dk7nw2rKeiNn8zcNMiE5xRhuJ04HCYIEnMfsatwmMabWyy4SSRObINtOYnFgawP4rkGFdmy6Pksj9IqNwsEeLv4131tBSCl10qqTOPciBTFpwr6XmtPoLvRDmZ/uXWwMGipmyJK71Ibw6Uq6PUbH2aU3tu69qJlpBDkQt124QPoEXyFQzCbsQTtdbVwvINd3KfsGJtHDjB8ZpMzdAxGnkVGMbev1GS0wziHWCbWfWdOuuKPSOILO+aMP0cIhNxIzq+ij3Gth4N9ApA3p0YO/sDcz4kv1QsUDB9TXs7xD8JEvG25NGS64iari6FZ2BvXMl2VNfIds6HQE6y9b5tQHp1hL6PeqFmMoFtB9OBgXwJK5xc4UhqlnhGmTlhLxkX4HveYGi1VDvMGK+RD+jElJ+hKmBypyVtKFjjQs5vh5zTxwDsJpJza49p4bWPn2ia6cZ/xT2IvCC0YjqIihZwGx+tfycCWYhSjcyUl/61LJQovWrXltywq8A5UksgDS0G6fRRsOKy1UKzKptVv1w0YgjFGQlCs4ri8la1dKNlC3gx54QMpAPASIdEESHoGCbyuBHTLf8JjURto5lfKRIU+0LJDyzbMbaSUfR5y/ekzahc8HaBkyBUk3IM+R4sJAWGSiLtL9EUQcl8FNUqTopFqdY/M0CcymtRHb6C8TMn/tPYH5hKnqW8Y9uY5ld1doJ3Bqr60vyDaJJfbuXcw788YqiuPRB2m5X0Rg/f6qvmwOBfdGGC2GvdVU4hKD6yo0ifdrruu5hzO2m904d/NCgycEuqz9OmeD73Gas1C+RMb90xOZjd WgJ2TMkb 8LxTh4P4u5cGiGIykdIyObiFOF1oCclbuVE3qR6qGw8pVQ4xJF0BqWLDLSf+NHnFcGiZZ5nuiYr2T8I5nR7/qIvpC0OCrwhWLJZeUdcmHNLqv89Uz/F5sm+yQ++xLU3GWHruaY2Nt5IItnA1Fz1+2ppA80fRca3JkCsY6cLrEZ61bfx+NR9edQP4dHth27BZiB9yKIV5wu0Jh3krSsIWri7v8AQCUOZoQglaaImj1wBTDGVo9bWpFGNTV4TYS8uX1O1YJlZ+/2jfnNnaa7uEUoytUZBUl+4Vn2ud07ygEVnDPjist/kYFF6HNDH1K62KVZtJUp4KOAnplwIOVH9VIvv/nRRDZphn0lNhoPee6U7bULGZ3rWZ8PKWi0I0JSHAY0LZ2B5eOtJb6LVSbU75jOUsjn7IeIJgC4CHxp+x7a1176RUsNtcwAAPLZdPYob1kYRdMOdxXQgs/6bWk8rQDfb/TtU/Ek2G3qHRBc/IvXr70/v3TnSACXUo8TxRojoObzi/JqGn16cDUIvQZd6W9PpLeHei/A6SzqKVnmaKrb0883aNURDBxUe8lR76pW/3GDQS+eIFUyVL7c0sZckiI/tR59jWD/aE1utFgN6LCIZ1Qki7Y2FV+kWqDnEl+/c5TIHMxSxrGZgdbTrLS08Xp9edrmliROJiuMx8RFXfkZjjfE4UGSFvO8aQGYmiINZh+4lYGGtPbl0spNgo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Make pin_user_pages*() leave a ZERO_PAGE unpinned if it extracts a pointer to it from the page tables and make unpin_user_page*() correspondingly ignore a ZERO_PAGE when unpinning. We don't want to risk overrunning a zero page's refcount as we're only allowed ~2 million pins on it - something that userspace can conceivably trigger. Add a pair of functions to test whether a page or a folio is a ZERO_PAGE. Signed-off-by: David Howells cc: Christoph Hellwig cc: David Hildenbrand cc: Andrew Morton cc: Jens Axboe cc: Al Viro cc: Matthew Wilcox cc: Jan Kara cc: Jeff Layton cc: Jason Gunthorpe cc: Logan Gunthorpe cc: Hillf Danton cc: Christian Brauner cc: Linus Torvalds cc: linux-fsdevel@vger.kernel.org cc: linux-block@vger.kernel.org cc: linux-kernel@vger.kernel.org cc: linux-mm@kvack.org Reviewed-by: Lorenzo Stoakes --- Notes: ver #2) - Fix use of ZERO_PAGE(). - Add is_zero_page() and is_zero_folio() wrappers. - Return the zero page obtained, not ZERO_PAGE(0) unconditionally. include/linux/pgtable.h | 10 ++++++++++ mm/gup.c | 25 ++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index c5a51481bbb9..2b0431a11de2 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1245,6 +1245,16 @@ static inline unsigned long my_zero_pfn(unsigned long addr) } #endif /* CONFIG_MMU */ +static inline bool is_zero_page(const struct page *page) +{ + return is_zero_pfn(page_to_pfn(page)); +} + +static inline bool is_zero_folio(const struct folio *folio) +{ + return is_zero_page(&folio->page); +} + #ifdef CONFIG_MMU #ifndef CONFIG_TRANSPARENT_HUGEPAGE diff --git a/mm/gup.c b/mm/gup.c index bbe416236593..69b002628f5d 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -51,7 +51,8 @@ static inline void sanity_check_pinned_pages(struct page **pages, struct page *page = *pages; struct folio *folio = page_folio(page); - if (!folio_test_anon(folio)) + if (is_zero_page(page) || + !folio_test_anon(folio)) continue; if (!folio_test_large(folio) || folio_test_hugetlb(folio)) VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page), page); @@ -131,6 +132,13 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) else if (flags & FOLL_PIN) { struct folio *folio; + /* + * Don't take a pin on the zero page - it's not going anywhere + * and it is used in a *lot* of places. + */ + if (is_zero_page(page)) + return page_folio(page); + /* * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a * right zone, so fail and let the caller fall back to the slow @@ -180,6 +188,8 @@ struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) static void gup_put_folio(struct folio *folio, int refs, unsigned int flags) { if (flags & FOLL_PIN) { + if (is_zero_folio(folio)) + return; node_stat_mod_folio(folio, NR_FOLL_PIN_RELEASED, refs); if (folio_test_large(folio)) atomic_sub(refs, &folio->_pincount); @@ -224,6 +234,13 @@ int __must_check try_grab_page(struct page *page, unsigned int flags) if (flags & FOLL_GET) folio_ref_inc(folio); else if (flags & FOLL_PIN) { + /* + * Don't take a pin on the zero page - it's not going anywhere + * and it is used in a *lot* of places. + */ + if (is_zero_page(page)) + return 0; + /* * Similar to try_grab_folio(): be sure to *also* * increment the normal page refcount field at least once, @@ -3079,6 +3096,9 @@ EXPORT_SYMBOL_GPL(get_user_pages_fast); * * FOLL_PIN means that the pages must be released via unpin_user_page(). Please * see Documentation/core-api/pin_user_pages.rst for further details. + * + * Note that if the zero_page is amongst the returned pages, it will not have + * pins in it and unpin_user_page() will not remove pins from it. */ int pin_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages) @@ -3161,6 +3181,9 @@ EXPORT_SYMBOL(pin_user_pages); * pin_user_pages_unlocked() is the FOLL_PIN variant of * get_user_pages_unlocked(). Behavior is the same, except that this one sets * FOLL_PIN and rejects FOLL_GET. + * + * Note that if the zero_page is amongst the returned pages, it will not have + * pins in it and unpin_user_page() will not remove pins from it. */ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, struct page **pages, unsigned int gup_flags) From patchwork Thu May 25 22:39:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13255749 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C505C7EE2F for ; Thu, 25 May 2023 22:40:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FC79900005; Thu, 25 May 2023 18:40:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 85C97900002; Thu, 25 May 2023 18:40:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FE89900005; Thu, 25 May 2023 18:40:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 626DC900002 for ; Thu, 25 May 2023 18:40:11 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A1E324081F for ; Thu, 25 May 2023 22:40:10 +0000 (UTC) X-FDA: 80830246980.28.5DBAFB4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf03.hostedemail.com (Postfix) with ESMTP id E81E620016 for ; Thu, 25 May 2023 22:40:08 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bVSP8N20; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf03.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685054409; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4LadGJQg30+UITevY/GGSgUqQRDMNZJwaw2G4qvwi6M=; b=J5dk7+NeVcnPWSg2/VMFLOTB59MV+POYDIUdp/4lFb6s1o/PfbTGyi88ksnrXZ8Tr3L/Yz YPJJYmBvs7N8RC8iL971+2s57CGpsS5h+zAD8FGsdiS3op8r4e1ZHXLnGVjQoM2CsrXbg1 MGe2pKHRFUlss8PUQTDYjFf+t+QnPGs= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bVSP8N20; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf03.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685054409; a=rsa-sha256; cv=none; b=kS3U5UqF+jgZLk9Fxy/2RoBYIStjsEdFnzd9IhSVHDavdHNlHUkEDmUiJGZHZyYO/HkvBR 1g+ARBsyap62YjDBS/jsweNJesOX8v7jWWFRo5Mry3OOgcNjqTiUATTSbpe17SmAhYm4Up +x5u1FgUGMnAZMEGFK9h+nJZO7p/Q9I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685054408; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4LadGJQg30+UITevY/GGSgUqQRDMNZJwaw2G4qvwi6M=; b=bVSP8N20MF6jnS3VAwOGm28+Y6yZUoeR03pIP5wXoY/zU0kzK6v+B/8Po61niUVS9p5sDj Ep65YtANR8NFPCuqjiuIHV83k0wbE0n8pteN3Nc5CjSleR4guz04n7AB1J6gVSP0Mvvb/U lyejXG470sgGU2cFOeOz0R8a4YIFtI4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-164-lyrjH9PRONCGKTu4lTu3Vg-1; Thu, 25 May 2023 18:40:05 -0400 X-MC-Unique: lyrjH9PRONCGKTu4lTu3Vg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DC30B1C05EA3; Thu, 25 May 2023 22:40:04 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.39.192.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C98540CFD45; Thu, 25 May 2023 22:40:02 +0000 (UTC) From: David Howells To: Christoph Hellwig , David Hildenbrand Cc: David Howells , Jens Axboe , Al Viro , Matthew Wilcox , Jan Kara , Jeff Layton , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , Christian Brauner , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton Subject: [RFC PATCH v2 2/3] mm: Provide a function to get an additional pin on a page Date: Thu, 25 May 2023 23:39:52 +0100 Message-Id: <20230525223953.225496-3-dhowells@redhat.com> In-Reply-To: <20230525223953.225496-1-dhowells@redhat.com> References: <20230525223953.225496-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Rspamd-Queue-Id: E81E620016 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: j9ok6j1wjzzy67zuzzkmhwk14jnrkgos X-HE-Tag: 1685054408-626650 X-HE-Meta: U2FsdGVkX19rCCju0ZQ2tZb+vjyc2LXGu004QiiU602vyy7r4aOpsTZ+9LYmLc+7UlnT1hadJ0UDbZfxp7ZyZsgpS9Nyg0zSCKyj2mvQ1iRjMjvrWKsVLd34f4QH+RzekkSI7K85rXa2EFTmzkQqMjQOw8+QmuKFgk9v7CzsWDT6pDdkm6wH/hnYlktHHFcf1kzhaTsl97Pm+V7l90Hf7C0T2fIqvwMsRNwz+7HSHS17MQTmiC4LYdcNDMb9h6VRjS8Z7CW7uG92NdLBBEoKYhX/9xtzUOjyc4BB0+urdG2HMY39udBzeZUUcEjhGKF6F7Q1cFBvF0LJCQyI/623uQi+DpWdIbaTpRydTwAQwXmstjQbc9Nb9F0PYHpnolctQi5HyubcpSRDkmWH86n8RB6ghJJsTodjhu2mOIGKR2dTgTb1at/86KOcwuxT2bZB6HdvhYlYPK+mIDEy/uKk4vMbWliClYSnOmrS8DXRUkrYJM7OFiwyn03Ia3JUaO1jEiHlzh7oH+XxxbdmO0SQRXJaSq4OogW2C2uv9sQm59HjcyuzVbX0PwhAwx0q+tWSY/zLoOc0+oqZgQWWecRUzYAwJSfnwNAzuiaHux5rdkMbhEk0o4D7i6apOFfXChSlK2lT2aXLfWh7QcKNr9zv4divioUW8Veh0ptNk5QqC702lyquGG/MsSM+AZZorFqcnCl9iztGgDkbtBm0mjyrNtySjtXc6rgwj9Y+oX9Lk6cIFid/4zJBX+ArzDYLm1EfzVpbx9t8DVVDBwecgcZEeP7yHWG3YYBBHQFBafjOnctW3jBF2MyjiwgpaE1lsL8l4zj8AadtcQCvYsO+KpzB+jYoaT1E/SzjETousMsd597o8xRbHCzW4Ss+bK2bqYKPQv27zfYQwoPMetHb3Tjc8dBxnaRPy+DwlnjtSNnuTMe4D+0YE4sgVotXeiEg1hsK0+T4TDxDXLnS4jBoS7H tBL7M8K+ fN4Jlr9FYF5EVwiyZn6xgzZX8rO3vdw/U/NhtDmTwrReZZhLMunRQaUIPFVGkS0vSzR9XJ+/apujKQCSuDrE5il1vpeok6drxCZBi5WpugICJqEr+6M9DStt9FTemw/EMg6oYqZfU+lhhKN0vgnhS0xdmnEaKnODDyFNP5ykDuSN5T2dgX6mqU/ZxxTcm+O89xkRa40xVj+BKyhc0aQJMOwQT/3X0YjdV9cGJH3Ax+o/uzmM5wMzgtaeReGQTxLt+9izhEupKZ3YRPEThL2fmD0IFjGwj8z2zGSdGWsHMR0O0HaGKVWGPkjz6jqH33y5ws5BmPXsLsBJwo3dAyLqlFLBF7RD5FSlZKFJ+sCa5dAgUhF3zyPk5C48a2feU7raXt0ZwzC0EAD4uwbXEzci86CqCiP1nnheVcXU86jvDQ6losAf9Y9XRsaTmHH5cXPljid4wySEGQhL7JPRfcr1d+xsO/rJjxrnCiXg8xyVYK1DTHXy6h6KQ9U2WvcH522viXB3mE78mu2Zk117cYFKZJu2KnRvIjRpffMiIuaZ+lol+S5IhxblwsJrPXhlqicYUlmg/+suA75Vn3GHMt38a4kS3U/OcUemTHuBQPCyf9XomywSxekbltb0hHWYseP7isQXWgED7Zbuiek0mXAmfn5cpIP0lUlqLp+2ZdrmceFwrPEw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Provide a function to get an additional pin on a page that we already have a pin on. This will be used in fs/direct-io.c when dispatching multiple bios to a page we've extracted from a user-backed iter rather than redoing the extraction. Signed-off-by: David Howells cc: Christoph Hellwig cc: David Hildenbrand cc: Andrew Morton cc: Jens Axboe cc: Al Viro cc: Matthew Wilcox cc: Jan Kara cc: Jeff Layton cc: Jason Gunthorpe cc: Logan Gunthorpe cc: Hillf Danton cc: Christian Brauner cc: Linus Torvalds cc: linux-fsdevel@vger.kernel.org cc: linux-block@vger.kernel.org cc: linux-kernel@vger.kernel.org cc: linux-mm@kvack.org --- include/linux/mm.h | 1 + mm/gup.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 27ce77080c79..931b75dae7ff 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2383,6 +2383,7 @@ int get_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); int pin_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); +void page_get_additional_pin(struct page *page); int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc); int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, diff --git a/mm/gup.c b/mm/gup.c index 69b002628f5d..4b4353a184ed 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -275,6 +275,35 @@ void unpin_user_page(struct page *page) } EXPORT_SYMBOL(unpin_user_page); +/** + * page_get_additional_pin - Try to get an additional pin on a pinned page + * @page: The page to be pinned + * + * Get an additional pin on a page we already have a pin on. Makes no change + * if the page is the zero_page. + */ +void page_get_additional_pin(struct page *page) +{ + struct folio *folio = page_folio(page); + + if (page == ZERO_PAGE(0)) + return; + + /* + * Similar to try_grab_folio(): be sure to *also* increment the normal + * page refcount field at least once, so that the page really is + * pinned. + */ + if (folio_test_large(folio)) { + WARN_ON_ONCE(atomic_read(&folio->_pincount) < 1); + folio_ref_add(folio, 1); + atomic_add(1, &folio->_pincount); + } else { + WARN_ON_ONCE(folio_ref_count(folio) < GUP_PIN_COUNTING_BIAS); + folio_ref_add(folio, GUP_PIN_COUNTING_BIAS); + } +} + static inline struct folio *gup_folio_range_next(struct page *start, unsigned long npages, unsigned long i, unsigned int *ntails) { From patchwork Thu May 25 22:39:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13255750 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8E0DC7EE29 for ; Thu, 25 May 2023 22:40:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 50366900006; Thu, 25 May 2023 18:40:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B31F900002; Thu, 25 May 2023 18:40:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 35426900006; Thu, 25 May 2023 18:40:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2725B900002 for ; Thu, 25 May 2023 18:40:15 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0627FAE8AA for ; Thu, 25 May 2023 22:40:15 +0000 (UTC) X-FDA: 80830247190.30.62B68AE Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf27.hostedemail.com (Postfix) with ESMTP id 26EDB40010 for ; Thu, 25 May 2023 22:40:12 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MDbCHnw5; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685054413; a=rsa-sha256; cv=none; b=PXUj49RWQHhVgyBHudey76A1lZOhDkvYz+ycd+3i7OtTIdUrX9F8Yo9Sbzg3Hl7o5oa/0g vBD9ovy6LczqSDYo/Ap2K1NE9w9Raz/dbVDyYGrFwR+diAVtgzGcS7nXLUo6BBtMRU4ZqP 28c5y1HW0eyNGzT/0PbAaDbvo4zad5I= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MDbCHnw5; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685054413; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PCC0sosk0Q9OchSk1Wd/PogJY3iRiwmEwElEkGvXYZE=; b=USxpS7hSx0dpqUHw0VGd/D7Ifgc4Q/LAZNGqyZWtucX/VGYWb5+Lb3+A7Kn1w0zXOjj3tp stCmfWv1y0jZ2cA+aPomq8A3WBejBV+qdPp24YERPKakIq/mTs697T9hnRgMRhOOpcAdBS pgwycplVJXUKNgaUvrXrKH+mptt6Umw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685054412; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PCC0sosk0Q9OchSk1Wd/PogJY3iRiwmEwElEkGvXYZE=; b=MDbCHnw5LrpRZAgYBMuKfJr+pKqkMYbfgTPPmlTAuUJ4GH2hsW44TP2L//t1SEGWEUXKG6 P9cy0WAi69TTvi8k9zmxPNa5epUrjxNMNadYe/1U2m+2RISIMdbbDpC53mki/oHquhfoD3 esJP+B0Vq85b5nFl1AC+vcvTJ/+RwSs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-344-_OlhGPMwOpeIMBefHaCpnA-1; Thu, 25 May 2023 18:40:08 -0400 X-MC-Unique: _OlhGPMwOpeIMBefHaCpnA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E96918007D9; Thu, 25 May 2023 22:40:07 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.39.192.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9049E7AF5; Thu, 25 May 2023 22:40:05 +0000 (UTC) From: David Howells To: Christoph Hellwig , David Hildenbrand Cc: David Howells , Jens Axboe , Al Viro , Matthew Wilcox , Jan Kara , Jeff Layton , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , Christian Brauner , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton Subject: [RFC PATCH v2 3/3] block: Use iov_iter_extract_pages() and page pinning in direct-io.c Date: Thu, 25 May 2023 23:39:53 +0100 Message-Id: <20230525223953.225496-4-dhowells@redhat.com> In-Reply-To: <20230525223953.225496-1-dhowells@redhat.com> References: <20230525223953.225496-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 26EDB40010 X-Stat-Signature: fbpp4ufp9n8ynz4kt8b6zyxzsuog5thn X-HE-Tag: 1685054412-112433 X-HE-Meta: U2FsdGVkX1+mD8V6FPoU9mY0ovCrjzBjeyvmUQxxvQz6PfCqpia7KTaJhgnXnnEeo2OVg9ZtyB8m/bA7LC6UXY14ZuhE6ClGaAJyjZXtnYUuCftritu/qMnaE5n+S6SY6DmP4KV7qoxUnbu2QmWTFmrCOfFyePU9Gn2HylNSS9bOFQTlRNhR92UdRzSR0itUreuQYt4MBab5dF/CmVkMozvsl4yNC3Yq+DK4MIOCINIKWXK5wiut2YOsEggIe2WHiEnqHITAx6N74BUm26XcDbB5KkbHq0S68yfCxIGWz3nIxaP9BlL5IjJY/oSmh2Rw+1/3kZlepzYHA2PN9MoVLWV5CJ+LS8REN7BP+COEzDVv/xoCdpdGiJqB3ztrCJrETqpOynmJ7VrzaDpDy162Kt8a7RGGm7IYKMkkMx2wJqO9/RmLYLv2H9ikam8W76VWJ71RcYFj+H5u+AJ64KYuAYmStGqJq77PnqWzpaMENm//JJiLCi77zyEuNF/B1AMIE1BdmpzCBKzE+ZmD7lydjUCSCbs6v7oX9ABdb1iXoGn5zRQV0gchZ9ZvC2B2cWO7I0A/p6v9luOoA3GVf21hVzTJyfHwMAyV88bZR3V8dHCNwaedpPbD32bGFauyNXRAmPYyPiJlGXiohour0uTA+ixRVLQ3byEW34HEsO9gH+NKGgFtQXzFt7JUPZdrnM0DaV5Au1ggTrHeTOvChuU4CPQTfoFPQt9dMwJs3bGb5AcpZ2zGYrwtXhiuWRZJvMIQiEmjNIKjAHmj6lGLIZ27kIqNL3lOg6481EIAf5eMxJYuYAZZ992Gy9ZxERToecg5ew7e6LL1leK9NrTDwmlWuk0rfwoqHvqQY0kcBneXRLbtcuvtPceYui+4sCqQBEFSjvPLlO85mpqFyzQbWlNbOJjKJ/PNQCl+EpFvhGWMXeHry9C/gT7ooX8WTRhU2DKGGIGcTpCKhRMZ+9qMkdY 574HggaU xbo7HyhKcnAfUWWDKoKqiiEBm/RRkrvko0kXJCjAbGBW3pa/ztrlWVCCaASq8IGYgURkh7v3rsGWm2oxNefmAxDOulJqJdssuaf7pRjQdQolCvguYbREAYqq/RKkMY2EaKG4JOe6ZIyTd4IDpyo7ole1ekjJ3yx2UAH8s7dIwdkof0ykjSLYA+J11BmdP6ZeZmZynAWgYS3DBRfMJsGMgfMj9b11YzF5kRuQm6LMR+UDeGJnRM+vlsvie8XVbcmnvNN+XsqPJjlAoatGDhmc9GghBiuyOVUksClURgyoiaq+fQvZk6diF2qIQzYpZAPmdhjUpVxYvlT/eauJpPNI/bpNPv690uP8PSj2f0BA/478cFQr/ieH0m//KtSQg+GppD2Xp5kYxj2vbVCxipTd+fI2e1ZCK9pLXT1XLDB1LYhu0vRJpuB9bQBDrTH74ta4qxBomXAS1B9no9tvJayQMAPBjWJ/WanhtcqPhYFWjAU6qCBF6bprscHCEGuG0qF9ZMYOuGoahrsX3gsPwvZZbGbwykcRaN+L8n74FvmmxNLICY71+A6+xeQMzIPx9E7XBSRLxYqGgLuerboEoXR7Tdh9QBCZ46FtoCnrCinQXDzHYM1afi13I/8gMCZwPpif9UGvM0YYV4iUi5zNXt8+wX8tjl3POdF94tGur X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Change the old block-based direct-I/O code to use iov_iter_extract_pages() to pin user pages or leave kernel pages unpinned rather than taking refs when submitting bios. This makes use of the preceding patches to not take pins on the zero page (thereby allowing insertion of zero pages in with pinned pages) and to get additional pins on pages, allowing an extracted page to be used in multiple bios without having to re-extract it. Signed-off-by: David Howells cc: Christoph Hellwig cc: David Hildenbrand cc: Andrew Morton cc: Jens Axboe cc: Al Viro cc: Matthew Wilcox cc: Jan Kara cc: Jeff Layton cc: Jason Gunthorpe cc: Logan Gunthorpe cc: Hillf Danton cc: Christian Brauner cc: Linus Torvalds cc: linux-fsdevel@vger.kernel.org cc: linux-block@vger.kernel.org cc: linux-kernel@vger.kernel.org cc: linux-mm@kvack.org --- Notes: ver #2) - Need to set BIO_PAGE_PINNED conditionally, not BIO_PAGE_REFFED. fs/direct-io.c | 72 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/fs/direct-io.c b/fs/direct-io.c index ad20f3428bab..5d4c5be0fb41 100644 --- a/fs/direct-io.c +++ b/fs/direct-io.c @@ -42,8 +42,8 @@ #include "internal.h" /* - * How many user pages to map in one call to get_user_pages(). This determines - * the size of a structure in the slab cache + * How many user pages to map in one call to iov_iter_extract_pages(). This + * determines the size of a structure in the slab cache */ #define DIO_PAGES 64 @@ -121,12 +121,13 @@ struct dio { struct inode *inode; loff_t i_size; /* i_size when submitted */ dio_iodone_t *end_io; /* IO completion function */ + bool need_unpin; /* T if we need to unpin the pages */ void *private; /* copy from map_bh.b_private */ /* BIO completion state */ spinlock_t bio_lock; /* protects BIO fields below */ - int page_errors; /* errno from get_user_pages() */ + int page_errors; /* err from iov_iter_extract_pages() */ int is_async; /* is IO async ? */ bool defer_completion; /* defer AIO completion to workqueue? */ bool should_dirty; /* if pages should be dirtied */ @@ -165,14 +166,14 @@ static inline unsigned dio_pages_present(struct dio_submit *sdio) */ static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio) { + struct page **pages = dio->pages; const enum req_op dio_op = dio->opf & REQ_OP_MASK; ssize_t ret; - ret = iov_iter_get_pages2(sdio->iter, dio->pages, LONG_MAX, DIO_PAGES, - &sdio->from); + ret = iov_iter_extract_pages(sdio->iter, &pages, LONG_MAX, + DIO_PAGES, 0, &sdio->from); if (ret < 0 && sdio->blocks_available && dio_op == REQ_OP_WRITE) { - struct page *page = ZERO_PAGE(0); /* * A memory fault, but the filesystem has some outstanding * mapped blocks. We need to use those blocks up to avoid @@ -180,8 +181,7 @@ static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio) */ if (dio->page_errors == 0) dio->page_errors = ret; - get_page(page); - dio->pages[0] = page; + dio->pages[0] = ZERO_PAGE(0); sdio->head = 0; sdio->tail = 1; sdio->from = 0; @@ -201,9 +201,9 @@ static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio) /* * Get another userspace page. Returns an ERR_PTR on error. Pages are - * buffered inside the dio so that we can call get_user_pages() against a - * decent number of pages, less frequently. To provide nicer use of the - * L1 cache. + * buffered inside the dio so that we can call iov_iter_extract_pages() + * against a decent number of pages, less frequently. To provide nicer use of + * the L1 cache. */ static inline struct page *dio_get_page(struct dio *dio, struct dio_submit *sdio) @@ -219,6 +219,18 @@ static inline struct page *dio_get_page(struct dio *dio, return dio->pages[sdio->head]; } +static void dio_pin_page(struct dio *dio, struct page *page) +{ + if (dio->need_unpin) + page_get_additional_pin(page); +} + +static void dio_unpin_page(struct dio *dio, struct page *page) +{ + if (dio->need_unpin) + unpin_user_page(page); +} + /* * dio_complete() - called when all DIO BIO I/O has been completed * @@ -402,8 +414,8 @@ dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, bio->bi_end_io = dio_bio_end_aio; else bio->bi_end_io = dio_bio_end_io; - /* for now require references for all pages */ - bio_set_flag(bio, BIO_PAGE_REFFED); + if (dio->need_unpin) + bio_set_flag(bio, BIO_PAGE_PINNED); sdio->bio = bio; sdio->logical_offset_in_bio = sdio->cur_page_fs_offset; } @@ -444,8 +456,9 @@ static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio) */ static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio) { - while (sdio->head < sdio->tail) - put_page(dio->pages[sdio->head++]); + if (dio->need_unpin) + unpin_user_pages(dio->pages + sdio->head, + sdio->tail - sdio->head); } /* @@ -676,7 +689,7 @@ static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio, * * Return zero on success. Non-zero means the caller needs to start a new BIO. */ -static inline int dio_bio_add_page(struct dio_submit *sdio) +static inline int dio_bio_add_page(struct dio *dio, struct dio_submit *sdio) { int ret; @@ -688,7 +701,7 @@ static inline int dio_bio_add_page(struct dio_submit *sdio) */ if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE) sdio->pages_in_io--; - get_page(sdio->cur_page); + dio_pin_page(dio, sdio->cur_page); sdio->final_block_in_bio = sdio->cur_page_block + (sdio->cur_page_len >> sdio->blkbits); ret = 0; @@ -743,11 +756,11 @@ static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio, goto out; } - if (dio_bio_add_page(sdio) != 0) { + if (dio_bio_add_page(dio, sdio) != 0) { dio_bio_submit(dio, sdio); ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh); if (ret == 0) { - ret = dio_bio_add_page(sdio); + ret = dio_bio_add_page(dio, sdio); BUG_ON(ret != 0); } } @@ -804,13 +817,13 @@ submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page, */ if (sdio->cur_page) { ret = dio_send_cur_page(dio, sdio, map_bh); - put_page(sdio->cur_page); + dio_unpin_page(dio, sdio->cur_page); sdio->cur_page = NULL; if (ret) return ret; } - get_page(page); /* It is in dio */ + dio_pin_page(dio, page); /* It is in dio */ sdio->cur_page = page; sdio->cur_page_offset = offset; sdio->cur_page_len = len; @@ -825,7 +838,7 @@ submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page, ret = dio_send_cur_page(dio, sdio, map_bh); if (sdio->bio) dio_bio_submit(dio, sdio); - put_page(sdio->cur_page); + dio_unpin_page(dio, sdio->cur_page); sdio->cur_page = NULL; } return ret; @@ -926,7 +939,7 @@ static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, ret = get_more_blocks(dio, sdio, map_bh); if (ret) { - put_page(page); + dio_unpin_page(dio, page); goto out; } if (!buffer_mapped(map_bh)) @@ -971,7 +984,7 @@ static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, /* AKPM: eargh, -ENOTBLK is a hack */ if (dio_op == REQ_OP_WRITE) { - put_page(page); + dio_unpin_page(dio, page); return -ENOTBLK; } @@ -984,7 +997,7 @@ static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, if (sdio->block_in_file >= i_size_aligned >> blkbits) { /* We hit eof */ - put_page(page); + dio_unpin_page(dio, page); goto out; } zero_user(page, from, 1 << blkbits); @@ -1024,7 +1037,7 @@ static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, sdio->next_block_for_io, map_bh); if (ret) { - put_page(page); + dio_unpin_page(dio, page); goto out; } sdio->next_block_for_io += this_chunk_blocks; @@ -1039,8 +1052,8 @@ static int do_direct_IO(struct dio *dio, struct dio_submit *sdio, break; } - /* Drop the ref which was taken in get_user_pages() */ - put_page(page); + /* Drop the pin which was taken in get_user_pages() */ + dio_unpin_page(dio, page); } out: return ret; @@ -1135,6 +1148,7 @@ ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, /* will be released by direct_io_worker */ inode_lock(inode); } + dio->need_unpin = iov_iter_extract_will_pin(iter); /* Once we sampled i_size check for reads beyond EOF */ dio->i_size = i_size_read(inode); @@ -1259,7 +1273,7 @@ ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, ret2 = dio_send_cur_page(dio, &sdio, &map_bh); if (retval == 0) retval = ret2; - put_page(sdio.cur_page); + dio_unpin_page(dio, sdio.cur_page); sdio.cur_page = NULL; } if (sdio.bio)