From patchwork Sun Jan 2 21:57:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthew Wilcox (Oracle)" X-Patchwork-Id: 12702366 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 4167DC433F5 for ; Sun, 2 Jan 2022 21:57:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 117156B0078; Sun, 2 Jan 2022 16:57:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 050C06B007B; Sun, 2 Jan 2022 16:57:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0B726B007D; Sun, 2 Jan 2022 16:57:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0072.hostedemail.com [216.40.44.72]) by kanga.kvack.org (Postfix) with ESMTP id CC5556B0078 for ; Sun, 2 Jan 2022 16:57:37 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 8F0538EBFC for ; Sun, 2 Jan 2022 21:57:37 +0000 (UTC) X-FDA: 78986709354.06.D89E398 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf21.hostedemail.com (Postfix) with ESMTP id 9B8B91C0004 for ; Sun, 2 Jan 2022 21:57:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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; bh=+rxdVzArFGjcrpa/qm5P7oEyraYzpZzNWOByz+nCGN4=; b=WeohFo8TJmNQ5QJjzg417utvPV pmGAAR+HgrFfulgux1puhU0iMEVPpe9MU0NrBXTbsFye8p/kNYqfv7bduV6uH0QIQbswPvEMNBYdC etLitxG4mmKYn4c5edSiBH4fSmOfr5JpHisjeq3OLpgETLWah561mv3VDT5fcO2BK6Ma5qtT/tzb4 ZAbl+VmsOurKImKimQlHKq8C8Mm3WnWGu/1p5OV7lYE46s81wxJmeMXUhSeTz0pLQYS6JYyQw3uUg blYhAViM9Ga9kXctkLQ79asLeQrLTGQXrZvvQ5D8pFAh9mgUZG1gR3BfDRJbSq14px6OcKGx6QWYc J+/ULPBg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1n48r6-00CLo8-6z; Sun, 02 Jan 2022 21:57:32 +0000 From: "Matthew Wilcox (Oracle)" To: linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , John Hubbard , Andrew Morton Subject: [PATCH 05/17] gup: Add try_get_folio() Date: Sun, 2 Jan 2022 21:57:17 +0000 Message-Id: <20220102215729.2943705-6-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220102215729.2943705-1-willy@infradead.org> References: <20220102215729.2943705-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9B8B91C0004 X-Stat-Signature: 5bnofg6bw6nskjf9nwpt1xddhpph8irb Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=WeohFo8T; dmarc=none; spf=none (imf21.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Rspamd-Server: rspam11 X-HE-Tag: 1641160650-407677 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: This replaces try_get_compound_head(). It includes a small optimisation for the race where a folio is split between being looked up from its tail page and the reference count being obtained. Before, it returned NULL, which presumably triggered a retry under the mmap_lock, whereas now it will retry without the lock. Finding a frozen page will still return NULL. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Christoph Hellwig --- mm/gup.c | 69 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 2c51e9748a6a..58e5cfaaa676 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -29,12 +29,11 @@ struct follow_page_context { unsigned int page_mask; }; -static void hpage_pincount_add(struct page *page, int refs) +static void folio_pincount_add(struct folio *folio, int refs) { - VM_BUG_ON_PAGE(!hpage_pincount_available(page), page); - VM_BUG_ON_PAGE(page != compound_head(page), page); + VM_BUG_ON_FOLIO(!folio_pincount_available(folio), folio); - atomic_add(refs, compound_pincount_ptr(page)); + atomic_add(refs, folio_pincount_ptr(folio)); } static void hpage_pincount_sub(struct page *page, int refs) @@ -63,33 +62,35 @@ static void put_page_refs(struct page *page, int refs) } /* - * Return the compound head page with ref appropriately incremented, + * Return the folio with ref appropriately incremented, * or NULL if that failed. */ -static inline struct page *try_get_compound_head(struct page *page, int refs) +static inline struct folio *try_get_folio(struct page *page, int refs) { - struct page *head = compound_head(page); + struct folio *folio; - if (WARN_ON_ONCE(page_ref_count(head) < 0)) +retry: + folio = page_folio(page); + if (WARN_ON_ONCE(folio_ref_count(folio) < 0)) return NULL; - if (unlikely(!page_cache_add_speculative(head, refs))) + if (unlikely(!folio_ref_try_add_rcu(folio, refs))) return NULL; /* - * At this point we have a stable reference to the head page; but it - * could be that between the compound_head() lookup and the refcount - * increment, the compound page was split, in which case we'd end up - * holding a reference on a page that has nothing to do with the page + * At this point we have a stable reference to the folio; but it + * could be that between calling page_folio() and the refcount + * increment, the folio was split, in which case we'd end up + * holding a reference on a folio that has nothing to do with the page * we were given anymore. - * So now that the head page is stable, recheck that the pages still - * belong together. + * So now that the folio is stable, recheck that the page still + * belongs to this folio. */ - if (unlikely(compound_head(page) != head)) { - put_page_refs(head, refs); - return NULL; + if (unlikely(page_folio(page) != folio)) { + folio_put_refs(folio, refs); + goto retry; } - return head; + return folio; } /** @@ -128,8 +129,10 @@ struct page *try_grab_compound_head(struct page *page, int refs, unsigned int flags) { if (flags & FOLL_GET) - return try_get_compound_head(page, refs); + return &try_get_folio(page, refs)->page; else if (flags & FOLL_PIN) { + struct folio *folio; + /* * 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 @@ -143,29 +146,29 @@ struct page *try_grab_compound_head(struct page *page, * CAUTION: Don't use compound_head() on the page before this * point, the result won't be stable. */ - page = try_get_compound_head(page, refs); - if (!page) + folio = try_get_folio(page, refs); + if (!folio) return NULL; /* - * When pinning a compound page of order > 1 (which is what + * When pinning a folio of order > 1 (which is what * hpage_pincount_available() checks for), use an exact count to - * track it, via hpage_pincount_add/_sub(). + * track it, via folio_pincount_add/_sub(). * - * However, be sure to *also* increment the normal page refcount - * field at least once, so that the page really is pinned. + * However, be sure to *also* increment the normal folio refcount + * field at least once, so that the folio really is pinned. * That's why the refcount from the earlier - * try_get_compound_head() is left intact. + * try_get_folio() is left intact. */ - if (hpage_pincount_available(page)) - hpage_pincount_add(page, refs); + if (folio_pincount_available(folio)) + folio_pincount_add(folio, refs); else - page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1)); + folio_ref_add(folio, + refs * (GUP_PIN_COUNTING_BIAS - 1)); - mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED, - refs); + node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs); - return page; + return &folio->page; } WARN_ON_ONCE(1);