From patchwork Mon Jul 10 20:43:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13307625 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 AB1D5C001B0 for ; Mon, 10 Jul 2023 20:44:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D73608E0017; Mon, 10 Jul 2023 16:43:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDDD58E001B; Mon, 10 Jul 2023 16:43:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9A3A8E0017; Mon, 10 Jul 2023 16:43:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9FA288E001B for ; Mon, 10 Jul 2023 16:43:53 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7C0CA1A021C for ; Mon, 10 Jul 2023 20:43:53 +0000 (UTC) X-FDA: 80996878746.02.278F14C Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf16.hostedemail.com (Postfix) with ESMTP id 2F1FF180014 for ; Mon, 10 Jul 2023 20:43:50 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=UZGRwuNv; dmarc=none; spf=none (imf16.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=1689021831; 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=LZXcBnkSopupOjRiXEH3IjaRoI8hYxfLxhTdnrJE7/E=; b=gzeqAWaafYCvkVtzXbrGDTOtYcJt+NXX9AnvbitmXPCHgUokJ+Bbvwj6EJU3cB/x93F/kW p/cmYzGkXj1PtgEEY1LJzAYBstlDwRbnfHdrXkjDm3zYYRAw3aNvPTd/bUJFfUXM3x9hjc Rfiyb25G2/Sa5reWoHY4zqPqjrFf1sQ= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=UZGRwuNv; dmarc=none; spf=none (imf16.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=1689021831; a=rsa-sha256; cv=none; b=SYeDKwHmKzgqMlxGUN/RRlIWvto92TIxXSpOBpKJgfIGemXrkXs/DEmvCasHciBhXUwBjr Uwo82bzj3FwdULr4cmj/GSXHedLOWCgUH5eLygg7UoGi77qk1GdiBFJmcl3Sg24pqzn64S 1eqw3RdINuaxZSiMC2lH4a40igOx598= 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=LZXcBnkSopupOjRiXEH3IjaRoI8hYxfLxhTdnrJE7/E=; b=UZGRwuNvpDEO6LgiTtVda6Le3s 8V37WDV4IfgqmT9LGcaQms1+Yk3cLoG2A9KbAfSe7Zu8NeuBDEN6ErcwCrCkw9dcBn4cYyRvuPRZr Jo9VtQkdKqShHWEOW0l06/c0HvvzKnZATErnunpUS8LoBMVlHlTOTIG2ZyBlqqylUDkogeF1xJwCf OlKJu7iSmyX7JGks9C0DDMHv1rEL48wUH5KRJcKgpx6pC2RbyT2o+ycJsWUwUKjbOaleBzWKenZ6u 7PWFgAZCI1jLFzp9FbolUcv/FaJP8Jf9qxtRccd6BjYahFeP0VEV1rHYVQh6pa/NU/900MqLtqgCv jvEi83Ew==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qIxjV-00EurD-0w; Mon, 10 Jul 2023 20:43:45 +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 v5 34/38] filemap: Add filemap_map_folio_range() Date: Mon, 10 Jul 2023 21:43:35 +0100 Message-Id: <20230710204339.3554919-35-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230710204339.3554919-1-willy@infradead.org> References: <20230710204339.3554919-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2F1FF180014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: tbwtmt9zjtusqbqagws7czq8wezzdhx8 X-HE-Tag: 1689021830-883662 X-HE-Meta: U2FsdGVkX18Sad83RH6nLGiKDKAFFgKSq6mUO3KpVjz9KerAd7QxFlzZEy/Wet9Uz3cp3L0jX8KaC5iTnbekLkqW0hBN+C3/FJuRZtfqxyU6jQRYFwGTD0VqQurQHyFFDNfk105tNGPdIzfl+/ApRNDLTWrmNwONaTMzvrwmYareF1+8/RK/Lltxa4csN+PLIIZ7pVvEtZ5WN7HE0bBWN2xh0HulYLnmK5r6LNjTX7m0XhwuA7SVTEP24feZN38hA/C4YMLNXoYaL9MIYdFVzW3fc+OxkM70+Aau2ncx8XWglMzUAX44Ba1D2UqALFNUgRI9aZ/gtN6aTL7c6JpXu+9nBCrELaJrnZfUz3jweXNWRz0lBKg3BK4DZtcO6dNIsbdkJSbAzBndldwOo5gPqJXsblXhX4Zskj9esFmdBzDJiT3+exejMbN9Jpavv3o+HPQeebaQ4qOQnDD5gxzORe6yoadqE/DC/bGTA/8MXTftWHcz9OT3D2+p3lEQuAVOm428misHB8j0w/VLwMpYh6yIDcjVPsVZlx6eOmoEhXeFeLl4ADrXrsAdZd9X4bd1HKLXDrlhxqYAEwfmMEeTG7rbgEk/4V04v8xFornuFcGlVjMAGWGXvOkRfmYn1xXOoF/X6BePGLMUuDuwk50oSo75fsmrC9Y0f1W4333zdh+gCobm/EYi5i1c+Waav9m7ST6cMTABvABnJa09U/zuNU6gE7JrPSRc/2fQtBgrQBm0yLw6AxpoN6YDiKbpFr6WabyQUl9TZxIQV5ISLM4bI1SjDGfzuJ58etItpmVY1YSjLvxHRFJ8Rf+a/3Hla+bKBB3M26j9TragleFSGl1nIjjgPA32swveKdsuIrytl7u3u1BpcShWW90F/NmWI14W6dunR6dS/CIaf/5Wgo490nz4wBeKzzKnwQMdtBZeTw9y3bqqNdnYgCQKrGKqiXLZ4QxF6ecOz9wLiQsLhxA k3b96TBo aCq9KLMP5WO6lpbUdvp3opAqgWl9vibvmwd+tv6OSm+dy0Pr7vmJdtEHxz8YDq8AgpsD/FMvrfmIuDuD2SYVG071asgHRFtsbnqjlQqqyhlZl5adVC70mdqeCEjjC2CYcHzrORy2nXqsSECvuo90SaNeAlM885WZx6bTcN1BjqhuE7Ivur//j48rQjGBMYAUb+2FCHYCT4UFIfwDcj1vuf/F2pa10kkok6QC3uuROlGl3DIoLkFu5ZrbP4AsCGY48cL8x9zW3dqHie4Q= 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 8040545954bc..bdc1e0b811bf 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2168,16 +2168,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 @@ -3436,10 +3426,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 { @@ -3477,20 +3467,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, @@ -3503,12 +3524,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; @@ -3525,17 +3545,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 @@ -3545,32 +3561,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);