From patchwork Wed Aug 2 15:14:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13338356 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 D7A61C04E69 for ; Wed, 2 Aug 2023 15:14:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CC3B2801A5; Wed, 2 Aug 2023 11:14:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 03F0F2801A1; Wed, 2 Aug 2023 11:14:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B13F22801A2; Wed, 2 Aug 2023 11:14:19 -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 9ABAD2801A1 for ; Wed, 2 Aug 2023 11:14:19 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 716861A0742 for ; Wed, 2 Aug 2023 15:14:19 +0000 (UTC) X-FDA: 81079510638.24.32F5CDF Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf23.hostedemail.com (Postfix) with ESMTP id B4402140008 for ; Wed, 2 Aug 2023 15:14:17 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=fqp32luI; dmarc=none; spf=none (imf23.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690989257; 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=TTmj8LRFmGI/uufNCg5PeNzaUBwTamXDfoKIZevsRLE=; b=aZAZHX2/Pc3LZWFrtlP2VP6UeHTVxq2YodfCa2pFRVw/k90Rv5KYOogaggMeswVqwvx21o l57KL33dTOlIcDHYbACevQcsEj64TcwPjsd0DhhnHFieGe6Wh+vPOW00vmcTh5F+4pEEUA CQG6p7rXk7juR7yuqbzWHFq+5bF7C4s= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=fqp32luI; dmarc=none; spf=none (imf23.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690989257; a=rsa-sha256; cv=none; b=XJszUXMcdU3l/KylvTdmMPDj2H+p/SB1GsarpTi7i+cUvf6FIvubM4x66AX24NjnbQZE+7 LhwrwYQNKtrtHUyNZGW/hgnHVQrU6d/uStVQ5Ng/OAI9REl/H5nsrsOJZv4vWf8/WwS4yq lDwPkDEXdbQkTFIYR3LuLcB6UJmff5I= 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=TTmj8LRFmGI/uufNCg5PeNzaUBwTamXDfoKIZevsRLE=; b=fqp32luIzS6X7O0ptXt5fgXfa9 8roNt9rAFD/QNNKxuvYv11j1FF/BhhoyPBedPkQtsa8n0XNTX/iLrbFDzn1cioJhRZlekE/owKL70 1ZXv2YLU+ZPKfmz7lNy1g4rdql3K0zc+A8FdzcS74g9PBGbnzBXuXYV8/W8v9r2NdJPywgHIkWeKu XU6eHVdgmuzQg0BQKb6dAirb/IxCsolv/9SY1CvENW9tEtosxynBgMwHWePOqxKDanO/6B9p47Qn/ mNsBMUJ0eYNf/XQvY3cRelcnvlVzBek7ScUzGxtAsXqH8pcwZLo+CiSXPKdHeG5/j8QBmZ0x7i6kM WfyhgZ6w==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qRDYB-00Ffll-Us; Wed, 02 Aug 2023 15:14:11 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: Yin Fengwei , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Matthew Wilcox Subject: [PATCH v6 34/38] filemap: Add filemap_map_folio_range() Date: Wed, 2 Aug 2023 16:14:02 +0100 Message-Id: <20230802151406.3735276-35-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230802151406.3735276-1-willy@infradead.org> References: <20230802151406.3735276-1-willy@infradead.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: B4402140008 X-Stat-Signature: wckahgndktp4ho7ida3a85rghjbg41b7 X-HE-Tag: 1690989257-921349 X-HE-Meta: U2FsdGVkX1/vjDrJMkDp9cQhoeXZfZzQAVMUCNq0kMzsVQU8oEnlUXxandEIIXlMR4dvYFnEtfb27aPXt5Pr3Mhu8PEHX6SMUs1NzUAAS2UXG+PLGbaPaRqqBQS1vx0W5lJJATKVIuBfM54Ax6/6eMN6cJvpzWphoTK+0Y0erEPPibmEPOSCKaWN3Xl3a+V1HEFzBDdhHWw2uP5DzbIMjZolAgrozGg7E+ea74ALJWkqxe2Nbr2fOja66cHZNUWNpQxGDc6oI2zhHHFOlloq5G1C/hBR9C82pWSLwsupJrTINcyZZtiG2tACMayKWijwI+igO3cHS1mNR0T/hwSjfJH4xnllkAjTXLJhIzJd92CUXTte42xhAYG3BYQvc2jT8fmUhLbSbIzF53cROSZ068wLsnqKYrDAt06o0B+6Y8wdId27UNmbLoKoyF1yKsDcUzrhIl+gw0EyNvRy4V6/gUOFmRVQz4X6FBKRQ3J9+i6Sha0wO0nJWNrSsI4g4VEWrcvgTjTvWjA2zoUqiYjTUrcbj8XtbaRI0uJQr7yD54pE/RLfika8EV2F5cVOlA8S8ZZ1Ltp3E+4ujkfbfPHxOvOCaSnXvojni6G2jM5EniKc/eNwyiMAen+K9fEoDT69E+sKfidZcK0n1AgjgQfFjQaBXRDFcoWH+kv9qu33CMf/0tvRqaPV0e5fXF/zs3vu0LQwzKW6KgCLt91poaVgwVBsxltlgr/RZxd/POX5HAumPZf6SgEJpro/ohsLe4SHz6z65iNLkYg2tGE6U/MqBLIT/wd0tPEul16kSjUSCpnFzloED4kKFJasiCLC8P82OoPN/4iocjPsv0k6QgQ2Q7/XEKdBMems5F6H2vdUISUrz3sDJ5nEA05YOvhEHnPQ9YV6iaD7aJCo0DatQNHCsde6hnxlnjZ7TDqcP8Q9iIowUEEgKn+yU/bdSOHmy6bOoWKs3AucnFaDTkkqB0Y bvTtuqLt QMJD8nD8XFisje4zep4JDWI5HxRwWzWJpbt3aYx701mCHMO36QFdjm0kWh51Evi13plf5WcNroNN2x4k8kjZP600bfQQIf6lhNJ+tiPMCsigxfqbEXzfJWsDmPtI+sx3mKxDjjVcKWmcFVdZehSytUeOdFtsg/hYmsIeaSQKgloUw80z9rY15WE4skMNmrDduxiRoeNoOPHQe14nRtMW8PLwfIAEwgAH0xkWBTbksLMEblsdjWjMxHc5LVJXttF5ZRHDSuPm4Zb+Jat0= 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: From: Yin Fengwei filemap_map_folio_range() maps partial/full folio. Comparing to original filemap_map_pages(), it updates refcount once per folio instead of per page and gets minor performance improvement for large folio. With a will-it-scale.page_fault3 like app (change file write fault testing to read fault testing. Trying to upstream it to will-it-scale at [1]), got 2% performance gain on a 48C/96T Cascade Lake test box with 96 processes running against xfs. [1]: https://github.com/antonblanchard/will-it-scale/pull/37 Signed-off-by: Yin Fengwei Signed-off-by: Matthew Wilcox (Oracle) --- mm/filemap.c | 109 ++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 54 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 4b23c8dc993c..9dc15af7ab5b 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2173,16 +2173,6 @@ unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start, } EXPORT_SYMBOL(filemap_get_folios); -static inline -bool folio_more_pages(struct folio *folio, pgoff_t index, pgoff_t max) -{ - if (!folio_test_large(folio) || folio_test_hugetlb(folio)) - return false; - if (index >= max) - return false; - return index < folio_next_index(folio) - 1; -} - /** * filemap_get_folios_contig - Get a batch of contiguous folios * @mapping: The address_space to search @@ -3441,10 +3431,10 @@ static bool filemap_map_pmd(struct vm_fault *vmf, struct folio *folio, return false; } -static struct folio *next_uptodate_page(struct folio *folio, - struct address_space *mapping, - struct xa_state *xas, pgoff_t end_pgoff) +static struct folio *next_uptodate_folio(struct xa_state *xas, + struct address_space *mapping, pgoff_t end_pgoff) { + struct folio *folio = xas_next_entry(xas, end_pgoff); unsigned long max_idx; do { @@ -3482,20 +3472,51 @@ static struct folio *next_uptodate_page(struct folio *folio, return NULL; } -static inline struct folio *first_map_page(struct address_space *mapping, - struct xa_state *xas, - pgoff_t end_pgoff) +/* + * Map page range [start_page, start_page + nr_pages) of folio. + * start_page is gotten from start by folio_page(folio, start) + */ +static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, + struct folio *folio, unsigned long start, + unsigned long addr, unsigned int nr_pages) { - return next_uptodate_page(xas_find(xas, end_pgoff), - mapping, xas, end_pgoff); -} + vm_fault_t ret = 0; + struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; + struct page *page = folio_page(folio, start); + unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss); + unsigned int ref_count = 0, count = 0; -static inline struct folio *next_map_page(struct address_space *mapping, - struct xa_state *xas, - pgoff_t end_pgoff) -{ - return next_uptodate_page(xas_next_entry(xas, end_pgoff), - mapping, xas, end_pgoff); + do { + if (PageHWPoison(page)) + continue; + + if (mmap_miss > 0) + mmap_miss--; + + /* + * NOTE: If there're PTE markers, we'll leave them to be + * handled in the specific fault path, and it'll prohibit the + * fault-around logic. + */ + if (!pte_none(*vmf->pte)) + continue; + + if (vmf->address == addr) + ret = VM_FAULT_NOPAGE; + + ref_count++; + do_set_pte(vmf, page, addr); + update_mmu_cache(vma, addr, vmf->pte); + } while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages); + + /* Restore the vmf->pte */ + vmf->pte -= nr_pages; + + folio_ref_add(folio, ref_count); + WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss); + + return ret; } vm_fault_t filemap_map_pages(struct vm_fault *vmf, @@ -3508,12 +3529,11 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, unsigned long addr; XA_STATE(xas, &mapping->i_pages, start_pgoff); struct folio *folio; - struct page *page; - unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss); vm_fault_t ret = 0; + int nr_pages = 0; rcu_read_lock(); - folio = first_map_page(mapping, &xas, end_pgoff); + folio = next_uptodate_folio(&xas, mapping, end_pgoff); if (!folio) goto out; @@ -3530,17 +3550,13 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, goto out; } do { -again: - page = folio_file_page(folio, xas.xa_index); - if (PageHWPoison(page)) - goto unlock; - - if (mmap_miss > 0) - mmap_miss--; + unsigned long end; addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT; vmf->pte += xas.xa_index - last_pgoff; last_pgoff = xas.xa_index; + end = folio->index + folio_nr_pages(folio) - 1; + nr_pages = min(end, end_pgoff) - xas.xa_index + 1; /* * NOTE: If there're PTE markers, we'll leave them to be @@ -3550,32 +3566,17 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, if (!pte_none(ptep_get(vmf->pte))) goto unlock; - /* We're about to handle the fault */ - if (vmf->address == addr) - ret = VM_FAULT_NOPAGE; + ret |= filemap_map_folio_range(vmf, folio, + xas.xa_index - folio->index, addr, nr_pages); - do_set_pte(vmf, page, addr); - /* no need to invalidate: a not-present page won't be cached */ - update_mmu_cache(vma, addr, vmf->pte); - if (folio_more_pages(folio, xas.xa_index, end_pgoff)) { - xas.xa_index++; - folio_ref_inc(folio); - goto again; - } - folio_unlock(folio); - continue; unlock: - if (folio_more_pages(folio, xas.xa_index, end_pgoff)) { - xas.xa_index++; - goto again; - } folio_unlock(folio); folio_put(folio); - } while ((folio = next_map_page(mapping, &xas, end_pgoff)) != NULL); + folio = next_uptodate_folio(&xas, mapping, end_pgoff); + } while (folio); pte_unmap_unlock(vmf->pte, vmf->ptl); out: rcu_read_unlock(); - WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss); return ret; } EXPORT_SYMBOL(filemap_map_pages);