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+AnT4CfRDKyd0CaGmTDigiWzFBE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Q++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)