From patchwork Tue Feb 28 21:37:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13155253 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 8A252C7EE31 for ; Tue, 28 Feb 2023 21:38:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3514F6B009D; Tue, 28 Feb 2023 16:37:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1EEF26B009C; Tue, 28 Feb 2023 16:37:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 08E326B009D; Tue, 28 Feb 2023 16:37:57 -0500 (EST) 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 EB9426B009A for ; Tue, 28 Feb 2023 16:37:56 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C815C1A0C72 for ; Tue, 28 Feb 2023 21:37:56 +0000 (UTC) X-FDA: 80518013352.01.2E62B95 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf14.hostedemail.com (Postfix) with ESMTP id 105E7100014 for ; Tue, 28 Feb 2023 21:37:54 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=Zu3izYdW; spf=none (imf14.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677620275; 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=i9ts6rBAYolQ1/FBCYccIPnlyslRrUlDUNhaTG0F47Y=; b=toy99/ZJAWbY1i+GJ0JDVa8x92/HQrBTqqzjSaUU2N7WKEv+M0L01XOUJyfCTI9FWh7FMU 2G91CR1Y0Q4hw25xde2/370PQqyO96rCm4c8qmq8SDWCmkNc6/R7RETZ4MQmRDkMfX2AqX 5WdTXH4wb8rX5kmaQWbp5pPm0qNOQBk= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=Zu3izYdW; spf=none (imf14.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677620275; a=rsa-sha256; cv=none; b=lfgM0kIIGac6ZgQvTlJwdPM7aC3s+8PsQ26i04pRyaLiDfJYgj3EeBXev6j7EOjBXJqKve GWQqP3QoJr7AwAocBVKiOHUTuxdzvSb8VabDyKFynf/bF99S9Ht4b9Yjy+TNxG8x5JOz15 ch7s4VfNlV7Wg8qLsHC4HQfhYaN/vm0= 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=i9ts6rBAYolQ1/FBCYccIPnlyslRrUlDUNhaTG0F47Y=; b=Zu3izYdWiUUrWNlblTIecgxVap vSbBWWBbGVf6F/mRhWkEatlIiQnHo0p87SmbzvwgHtTMhRqJ7iDgvDse5sB/S5t41WheVx2gII5C8 hBaIEcWUmNdss0vl3hGJmAep54TOAa3KmCoNQY+LnVxNkEkyaZnV/a4iADAxF7TcCPY5KSbsPG650 ADPBKhoPOPKcqaSgLW0NANyzDDwHvz3HXPOQJHU/aVC0a5QhSum4OYeiv1z77mW8yKu9D5p6/Lnrz da6ZPtIqG4XkeLCjYg0m3VLc2wKopty+hJW2xnBDcerJxOZQ7S2BK0qfWcEU3t1/OMfJ4ovzu+LDK 8Nda2UwQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pX7fL-0018r0-C5; Tue, 28 Feb 2023 21:37:43 +0000 From: "Matthew Wilcox (Oracle)" To: linux-mm@kvack.org, linux-arch@vger.kernel.org Cc: Yin Fengwei , linux-kernel@vger.kernel.org, Matthew Wilcox Subject: [PATCH v3 31/34] filemap: Add filemap_map_folio_range() Date: Tue, 28 Feb 2023 21:37:34 +0000 Message-Id: <20230228213738.272178-32-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230228213738.272178-1-willy@infradead.org> References: <20230228213738.272178-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 105E7100014 X-Stat-Signature: stqj7fd8rfh4r177by457ztiwp8xuw7z X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1677620274-614247 X-HE-Meta: U2FsdGVkX19mTVdVbJng9d5yv8NQlAyP4SX4MqjE/bZS7Fm9fwJ2I3ZN+6S0qYJsQlC2hehEpKeuE4Y7j2ju7ZlGhUZc3Q29g+pQ2rfhUUG+6hGMPCqWFrwtuuwwKu4m711DyfYhelEFPBAo82zhVjeLTDpZB/guu2A7h0Z+mJJ+A/7iiqSe1lefK+BtaS5NgHZLT+qOmXbKre/hubAXGIO5jvca7Bx7iHTTaheidqJT5yWqhLMKCY4Oo098vmJH7PwlbwGvmuVv/4E3K26voq9wBPPkpw3edS7LN6qgqe0BVkCYzWhZ9lK1kXi7dlC7aLHl8VHc49mQr5g5XEsKaL7NaLVQUe0MtwLcHxB6yPtF6YmjTN4KoF0e1EYPqU+FuRDNuhYiuJBzJAuzebLPq0muJk22CAiujx9G5DalIXn6W/P10Y3AewxRgJFoI0uy8fL0Di/Q+tC/sm0ycbK4Sztm0wRmkU1yurM/i86X9ysYcdUN4mVvw+chiCINrNwb1kv+Wl7wVALJTpJyW+W8jXF62m841kX4OYM/08O5TC60YLPbsDtxLwGqXhGkxIKCMawJn8hyRLsFI6ONVWBrkezKm6OCLU1PEPwx+w+r3gmY/riTadxSQEVc8Hnwet0EY7kgRpK9W0JdmGrvG0imbSpb01M6WXMeNj+XJky/m/O32TmH8XegQfHWG9V/nAbB9lJDGt/eNxv1SLxQ9/HarpTHF5dybEsBKnAi4/z7j4qFLICtFJhWoa1VTxNTJNzLldFiwoh0OlA1ecoFLgpdCQU5dVyPFJgoLAPK79X5+M8rc9NQUYz2swW4ZAeWiYDkHdrtVzxaE+/i9lhH0KhpouuPzJ+JfywdTc2diyh24/iYFdHwyLt/VS6WIH4scEfwellD+/w+++NFnPEGPlA9ORwY5ZrjbECODyA5DUvVvBcuQvcQLiyen74gzn6F/nIGejF5x89Y6ASW1ZxJ88A yw3OIrJp UzmfQyY33V7xbnxhdabrVjkdnk39r6+YHX+VZUw168CSCQhMrm2q4HDOn4ANhI6o5KfANF1zGk3/Zff62MxZ13FD7AAIzK+2uL6vyQfG/ChCmoTd/K4TG21RwS23L0OIYm7ezs0Crva2ec/vOW9IrkORZ6YC59/PuzCvzDMVXAWiRFUBHbmX2IAX/rtgrhWzUR8c8GrwFTlkkm0LQxJO/qT1hAnNmYDW3NY6j5eXuoAYuRPhaZv0GKYLh4eh6dBBapRPaLuIYBq7giv6KtMH778uMROgEQN1xdoJO 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 | 98 +++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 2723104cc06a..db86e459dde6 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2202,16 +2202,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->index + folio_nr_pages(folio) - 1; -} - /** * filemap_get_folios_contig - Get a batch of contiguous folios * @mapping: The address_space to search @@ -3483,6 +3473,53 @@ static inline struct folio *next_map_page(struct address_space *mapping, mapping, xas, 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) +{ + 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; + + 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, pgoff_t start_pgoff, pgoff_t end_pgoff) { @@ -3493,9 +3530,9 @@ 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); @@ -3510,45 +3547,18 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT); vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl); 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 - * handled in the specific fault path, and it'll prohibit the - * fault-around logic. - */ - if (!pte_none(*vmf->pte)) - goto unlock; + ret |= filemap_map_folio_range(vmf, folio, + xas.xa_index - folio->index, addr, nr_pages); + xas.xa_index += nr_pages; - /* We're about to handle the fault */ - if (vmf->address == addr) - ret = VM_FAULT_NOPAGE; - - 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);