From patchwork Fri Feb 3 13:16:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13127502 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 10084C05027 for ; Fri, 3 Feb 2023 13:14:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9479C6B0075; Fri, 3 Feb 2023 08:14:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8CFFA6B0078; Fri, 3 Feb 2023 08:14:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 749A26B007B; Fri, 3 Feb 2023 08:14:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6788D6B0075 for ; Fri, 3 Feb 2023 08:14:50 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 42DBD804D6 for ; Fri, 3 Feb 2023 13:14:50 +0000 (UTC) X-FDA: 80426025540.26.7A9969E Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf18.hostedemail.com (Postfix) with ESMTP id 36BC31C0009 for ; Fri, 3 Feb 2023 13:14:47 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DZA+aYuD; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf18.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675430088; 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=PkN3M8JdvU4YbbZ+vqRS95i4OQTeEU6UDDCzY0xrouU=; b=Iqv62jwT3Zy/gLBiiD7swoOEl4ti2SYoubNbNqhxlvwZdI5JNPpVA9kcog9jcD7glmFZ2D Po34ILhyXQyBgWES0ktUFUP3rCIpltUaYLfKJ8RY2jS8zpaTkNjRDANH01jjsUkUreSyJk LSN62Y0N0nkCV63KszFmkdS1IFOMH10= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DZA+aYuD; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf18.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675430088; a=rsa-sha256; cv=none; b=WJi3FrGcZu3rgWV58zgyYhAUnHs127hpm9jfmq5Ay68MaLbFcM7LqAGSPgC9QwkditW62q WfWyhP0IWHWtnnhVpmKDmROB+1gTBf4XdUT7dXi7Y8g92kd94TWiePV3j1DZ3aQRltea4o dLggdbxXxaWERnMzEwqyx3D6Y+ihanQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675430088; x=1706966088; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3bf4l263P9fVufjhv5VLZbFxEerrnvzLFxKFmK42Ng0=; b=DZA+aYuD3EXHWodyWe3BJcjhQzy/jpyuRef/Rtw79XH3LxJKAdEyCCMS VTe23WTsdz1Xh+nmJ0dL0MlcnLyeWmHbibuMLQ0POL8IZL3Km1amHeHiD Dj0FWw+uSGLtHVnQP+hp2/tOhplbOCl9SzwH2RrPj14SvL4jJUDB71LyJ KLRrcX7/nMf+0HOlTrJ0uzraE9BpmzYFgoNx3kvzguPNV0gsFnR5K6mLF z/R+cArLWn5BTEWmIow0TrFNbEvmOkwekE//kZCi1cDWdpK4udOIAwgb0 EiqKkoAwgvwzkwD2ZdcvvR2GJVLfidQiclCnZUweivbk/1a1uRgl2Ke4K g==; X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="326441885" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="326441885" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2023 05:14:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="667641943" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="667641943" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga007.fm.intel.com with ESMTP; 03 Feb 2023 05:14:45 -0800 From: Yin Fengwei To: willy@infradead.org, david@redhat.com, linux-mm@kvack.org Cc: dave.hansen@intel.com, tim.c.chen@intel.com, ying.huang@intel.com, fengwei.yin@intel.com Subject: [RFC PATCH v3 1/4] filemap: add function filemap_map_folio_range() Date: Fri, 3 Feb 2023 21:16:33 +0800 Message-Id: <20230203131636.1648662-2-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230203131636.1648662-1-fengwei.yin@intel.com> References: <20230203131636.1648662-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 36BC31C0009 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 7i1bfgfsadhu384yh1w3jndxnmjkkquk X-HE-Tag: 1675430087-648291 X-HE-Meta: U2FsdGVkX1/uqq5QoBX/O9TvvxFAuAiNE5oZCVNIvV1kYcpdrRVQb4yIbNtD1BIA29SEUNoygveQQi1P3hx04Y5K28GSwL55U7AWlkL4A4HGlnCcj6jjFIULYoMvE2YB7rPYQebuYteOBpI9z42M61OAFnWwfDxU6sEm1bU5+0N829M5FcZb3hKmnGV5PXLz7EsgW9GUe70qRZ/pEKsmJDCRpd7nTSqwl9caBC16BqQB3XqXD9GLiQRp/XTrrU1dz7yRAe7wFk9Z4JJFCD/hq7SOtyI9zMGNYooHx4C6UKb4VLoE46MmXCwlLVInOwXIFaqr5dzRFP480fYjzpihd7vcreig22+VoVo9WlgkBWnWTRvxGBC3S2vY7q45mCbxJRk8Lo4+eTuslXr0x9Q28+soEdlQPTx7LwdwN/Jh0HQx6pay2wl0xgOQOnOhahIWQBe3SHUxBKTgOBb+T7EbrVdgb4o4sVUDuI2eR2IKsk+Yg/Mf9RvDW93DzWpPh1UvBXS37aKFCBTQtRHwzlg43b3lpLVfIQ06DxJEPz3hFAadOFjf3gUXb1fH1gXSBeYKWGAeKw8+csxKV1wIQ7AxqZ2JAUZ9IqQ8uIf/dWX27kqCP5jnuVik3SGnls/muuzPtb7qTANbOMeYXfmu7cwIPMK8QMCVFZCiZoLBXjByS938LMqCkwJir96ENrsq5uuRb9Ec0GuKxncI6wtLPk7eyCx4TGeYmvRZykmVa0S85J7Pb1yGt1A2mipuL27urLRUM9yoZYipOvzh3LO4yukKCLfG9e8FID/O7F4rzuaSzn3AC7JKdlgAoFW9OO+JPiJYnoPi6YbsMG27B3ZVFmswOa9ZpH75XXB0Dwvk2s/uwgkVm5Tz8rCePR25eYqYLivrKWL8WG8wnd1GjY39jV0OKTp+AKLZSf6OWDQid+ofPzz9oembCtWFvJBxZJmFzHfOUWFcExpRfHoWpQTTT4i 3q9KpPlv F/4N6k+/JsB9K3zR23o6cU5piOnBJVDcaAv71O1LAkN5PUkVot9YfO0pHORckQZkWGwxFlQYFbM2UfqjVr1zz/vaiDUJlX5NKRCMeQuSLXYzEw7O+7Pk6g1obVw32lDObPgAfqhzBsvAhNFATDTmuicBQHTKeYv/AgR7/6VhI1rP3OacCPcPQgkluXj453CBh6mwVOVYlZZPR0aWkPtRcZi/LsQ== 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: filemap_map_folio_range() maps partial/full folio. Comparing to original filemap_map_pages(), it batched updates refcount and get minor performance improvement for large folio. a self cooked will-it-scale.page_fault3 like app (change file write fault to read fault) with xfs filesystem got 2% performance gain. Signed-off-by: Yin Fengwei --- mm/filemap.c | 88 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 992554c18f1f..f444684db9f2 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3351,6 +3351,53 @@ static inline struct folio *next_map_page(struct address_space *mapping, mapping, xas, end_pgoff); } +/* + * Map sub-pages 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) { @@ -3361,9 +3408,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); @@ -3378,45 +3425,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; - - /* 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); + xas.xa_index = end; - 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); From patchwork Fri Feb 3 13:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13127503 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 C557AC636D4 for ; Fri, 3 Feb 2023 13:14:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0206D6B0078; Fri, 3 Feb 2023 08:14:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F129E6B007B; Fri, 3 Feb 2023 08:14:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB2716B007D; Fri, 3 Feb 2023 08:14:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CA4766B0078 for ; Fri, 3 Feb 2023 08:14:51 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 97C18AB7D8 for ; Fri, 3 Feb 2023 13:14:51 +0000 (UTC) X-FDA: 80426025582.13.9E576FA Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf23.hostedemail.com (Postfix) with ESMTP id 64D9F14001D for ; Fri, 3 Feb 2023 13:14:49 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GA45Jrjm; spf=pass (imf23.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675430089; 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=NEgBocWJZ6EgKIGVcRxRsqUmjXNgdcctNcLq4HpzGtA=; b=Hw9BlQBqKGVF83t0FnIkUOiYTS95gGUpkvwq0SdGUTHPypF6Qr0nJbJ1P1/kHkaWMwBvVf F5M1T/M2bt9b0BEEXCZhP89+uS7FSXWAszVFt6PtRooEMdigwjdWXPHkIoXG9KzbP8/c/d ixdjIr/DJkQPyiBXx7acToaZSoHt3mo= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GA45Jrjm; spf=pass (imf23.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675430089; a=rsa-sha256; cv=none; b=PxdM0JOZ5SFHw+nb5eTWzPqgp0EYaPYM7A4ubn4S+Wj1nlHhcGI7zpGzIgAVYv4/aVg4Jx jGMu4a/uWwbzmckmdelFqKxCCs8HOqlZDWwT2yO/aIai2HnMYm8HjrjnajYR+wMJB0aDeb MWarbF7UFKilYR+BPV+KJljmNz6UAyc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675430089; x=1706966089; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=d9C+zDfVUIiVtgWbOtTmri9i/MfcV8BTMrFKykwN2Wo=; b=GA45JrjmEisvzRzXMGOJVAI6IOJM6rhhxE5jy6jrXme/FXYvlJe3IY9f LT4MBbAW2OWw5lpF/cD219Sy4HGiy0N8dcgl9nagP7W7smi++gCYjkfDc FcSMoJJi0M6CkS2+HWxLLHUCaqrAOuf+NWbnYWLJiduysIMHbjsNHhyNd 1QM4HOL+XAU+Br9deIZhgS2pL4IdHBiOo7890h4+t63QyvZl6vQm9Z1o9 uykXVn+4nmX31voGQLlVCken5y18+8qZl36iZBnI7nMxHMGqT1ANgKeZp 8due8Bq+MHwCyndEgXRBSjVlX/+Hm+egKwtqHX9KPcfaWt2fsTpAWjxWG w==; X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="326441888" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="326441888" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2023 05:14:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="667641946" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="667641946" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga007.fm.intel.com with ESMTP; 03 Feb 2023 05:14:46 -0800 From: Yin Fengwei To: willy@infradead.org, david@redhat.com, linux-mm@kvack.org Cc: dave.hansen@intel.com, tim.c.chen@intel.com, ying.huang@intel.com, fengwei.yin@intel.com Subject: [RFC PATCH v3 2/4] rmap: add folio_add_file_rmap_range() Date: Fri, 3 Feb 2023 21:16:34 +0800 Message-Id: <20230203131636.1648662-3-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230203131636.1648662-1-fengwei.yin@intel.com> References: <20230203131636.1648662-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Stat-Signature: s9o9wsgbotbmhdsmcxnw8p3tqiunk8zd X-Rspam-User: X-Rspamd-Queue-Id: 64D9F14001D X-Rspamd-Server: rspam06 X-HE-Tag: 1675430089-502871 X-HE-Meta: U2FsdGVkX19UDRCJRppFn/clwXqAoKP/HYOamS4bov0nzTpsH/5wvVlrtirud07EW30maOlyFOP2jP38DtcZot430w6yEvThvRQOQFahWNEkTn/7htUCBbXaga0NMXXAhmYTPxN5aNbToNmaIUYx25XkqCC1rdJ5cM9g/8UEdFNbSTjQTZyqnilFfcmKiM5zejXLDlg23zKcKDu17146Ux9zgdk/jqYbGKmujwLgkicvMXRrTx8yd5cTLzbhuxhL6csTHvjvQQF9ley/lXKP1hmJ+PAqqQQGh6Yjqxi7uE0VKgzMxnBrkPZjc8JLNLpVFcrC/TOV9ASbZN1EUQZfuLAzg9EKN15DSwG+ANhtzNnosw/TapUbuFoDU4PEwRDfap9Imk6Ve6KrrOBNT0MH+phas7ePRwJvr2467J1fzQG1SrlUEs5xP7cAzqUat/jfYwRBZ7L7UmssSwuhP7zI2Ghal7SG+DkFw3JzUl0Z8Ok6Mxex2xsG1Aa94bX43GRajFXcfD9VWdz2npC3U3KyEOQ2R911gJPZ/2JIIeWD7OK/T1n6pf71mFckX/8x7sAJW+fiZ2LGiXdKQ8fFUVtTTEnJ/5s2GCOks1snJ6yYC33gU6kSLz6OXhmwDFMsWRzVrDUzae6XVaoBIK4A7/oj40KqeThXh0dt9ILxvCPQ7jy7kgN1jAB7aG3djF8PI+ThlN402IjCYdjqZZBEcL33JRYmW1za6Xogn8AVbORQfU5ksYr0t3+PO51VRQMgw4ApXej2Nj2o3KAJ9eB2lgLBf9UlAlRwbsSv+yIMeYrPdZxzV0AMyVxJFiRwlPSaUAD1YSJiPNLlyDmLFg8ta7BE3Jb7G2TajvpXUyGbvMmKGg/47Eeu2GECr0GwMlWfkpU75DW7a5fusWm4LQkqWzgZHlzFUccilA/hSJxCy04c/fmcVNbUi978gy6UChjwYfBak2kQnIRPZJz6zCG7lqi TEYjZ3IG 5eqEkeypamsgHBEEnNtQC5mWYyKcb57Ba8h2Ky4ZncnIBWcGXCC/q8iQJ4k6BnkFLvN9XMuOk4qUo0E58gWsHPAa1Y5UoG5J7m/3EuCEvxt5x4cvLBTaSVMKAkOlRmRML1cKJaN2mbDtDP+pfVF/Mvz8W4IqBKdW6Sfr3dEhJfmlmntHqFI3KwsTcgnvsBDVcw0LFeDrIZGUr32qLPKVP/HOkB+2OuUtP9Gl/3zL7zbDCQ+fpABcbxnKEynNPfgJRG93U 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: folio_add_file_rmap_range() allows to add pte mapping to a specific range of file folio. Comparing to page_add_file_rmap(),it batched updates __lruvec_stat for large folio. Signed-off-by: Yin Fengwei --- include/linux/rmap.h | 2 ++ mm/rmap.c | 66 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index a4570da03e58..974124b41fee 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -198,6 +198,8 @@ void folio_add_new_anon_rmap(struct folio *, struct vm_area_struct *, unsigned long address); void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound); +void folio_add_file_rmap_range(struct folio *, unsigned long start, + unsigned int nr_pages, struct vm_area_struct *, bool compound); void page_remove_rmap(struct page *, struct vm_area_struct *, bool compound); diff --git a/mm/rmap.c b/mm/rmap.c index 8287f2cc327d..f8861d832468 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1303,31 +1303,44 @@ void folio_add_new_anon_rmap(struct folio *folio, struct vm_area_struct *vma, } /** - * page_add_file_rmap - add pte mapping to a file page - * @page: the page to add the mapping to + * flio_add_file_rmap_range - add pte mapping to a sub page range of a folio + * @folio: The filio to add the mapping to + * @start: The first page index in folio + * @nr_pages: The number of pages for pte mapping adding * @vma: the vm area in which the mapping is added * @compound: charge the page as compound or small page * + * The page range of folio is defined by [first_page, first_page + nr_pages) + * * The caller needs to hold the pte lock. */ -void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, - bool compound) +void folio_add_file_rmap_range(struct folio *folio, unsigned long start, + unsigned int nr_pages, struct vm_area_struct *vma, + bool compound) { - struct folio *folio = page_folio(page); atomic_t *mapped = &folio->_nr_pages_mapped; - int nr = 0, nr_pmdmapped = 0; - bool first; + unsigned int nr_pmdmapped = 0, first; + int nr = 0; - VM_BUG_ON_PAGE(compound && !PageTransHuge(page), page); + VM_WARN_ON_FOLIO(compound && !folio_test_pmd_mappable(folio), folio); /* Is page being mapped by PTE? Is this its first map to be added? */ if (likely(!compound)) { - first = atomic_inc_and_test(&page->_mapcount); - nr = first; - if (first && folio_test_large(folio)) { - nr = atomic_inc_return_relaxed(mapped); - nr = (nr < COMPOUND_MAPPED); - } + struct page *page = folio_page(folio, start); + + nr_pages = min_t(unsigned int, nr_pages, + folio_nr_pages(folio) - start); + + do { + first = atomic_inc_and_test(&page->_mapcount); + if (first && folio_test_large(folio)) { + first = atomic_inc_return_relaxed(mapped); + first = (nr < COMPOUND_MAPPED); + } + + if (first) + nr++; + } while (page++, --nr_pages > 0); } else if (folio_test_pmd_mappable(folio)) { /* That test is redundant: it's for safety or to optimize out */ @@ -1356,6 +1369,31 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, mlock_vma_folio(folio, vma, compound); } +/** + * page_add_file_rmap - add pte mapping to a file page + * @page: the page to add the mapping to + * @vma: the vm area in which the mapping is added + * @compound: charge the page as compound or small page + * + * The caller needs to hold the pte lock. + */ +void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, + bool compound) +{ + struct folio *folio = page_folio(page); + unsigned int nr_pages; + + VM_WARN_ON_ONCE_PAGE(compound && !PageTransHuge(page), page); + + if (likely(!compound)) + nr_pages = 1; + else + nr_pages = folio_nr_pages(folio); + + folio_add_file_rmap_range(folio, folio_page_idx(folio, page), + nr_pages, vma, compound); +} + /** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from From patchwork Fri Feb 3 13:16:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13127504 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 CF914C05027 for ; Fri, 3 Feb 2023 13:14:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D6B76B007B; Fri, 3 Feb 2023 08:14:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 689556B007D; Fri, 3 Feb 2023 08:14:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5017D6B007E; Fri, 3 Feb 2023 08:14:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 404896B007B for ; Fri, 3 Feb 2023 08:14:54 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0C809C0320 for ; Fri, 3 Feb 2023 13:14:54 +0000 (UTC) X-FDA: 80426025708.14.C9263A1 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf23.hostedemail.com (Postfix) with ESMTP id E4EFD140008 for ; Fri, 3 Feb 2023 13:14:51 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lc0oBJb6; spf=pass (imf23.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675430092; 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=r8as/7WUg8SJEFL5i5iWeYfV/HZE3qGURoLJ0Rfb5RA=; b=GsQi7P1qaxe7AL0oGX1g6ykXiZts6f0B+k3yEiGtpxhOGEz3zXhPBoMSp9UNQaGtDaMwRG vms7st2h5VPJHOgmSVSB9abHKOJdHEfB7FNvTDjq6tf5aBQaNkeXP7DfZAbZb0TnfutiSd MwmwP9K6bNKsRR2FCJyK4xz9Kj40mPA= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lc0oBJb6; spf=pass (imf23.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675430092; a=rsa-sha256; cv=none; b=ElX4/ThWj+GgNBTkV11Ydl1cc0A/Iqegz3IWnLH+ZXRZspAUPl9+mKruC6T4SfRMr1e7oB olCdriYmiHM1xq7Kqw2eDpRsgI2dAmlHf23/TujD0gkKl5NoGo3Er4rz0YqqdqBLdPcy+7 9l5FGe4+s+tVVqbFVMVrAg80itvp/fk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675430092; x=1706966092; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=87aulzXqZlhIUbrzeU7vi/mx61DwZU61Oxk0IZoKp2I=; b=lc0oBJb6/7DyN9fpifuA83/ikqYaNxWQY0B+fUYG7E+djAH9OXv21cPs sOEuDukQqOutKtuS+lqspXAxHusY4bWY7M4uz3SFCdCzLkqmcGbYf2Z8q 1yezMFaFzsGBBU/XIW0pbalrd+girRmPaQSyKa1dMxahWNxN6OitYmznk MkBATVl9vrWy4r/aXlv5k5GL2jw+FroP8KSKF1OSfy2HE3X+ZtlcJdI+a ebsSb+8t6/zhN0ZF7BaS3E5R2Vg2lMAk+xKx/OSbIidX9JBa5jX84Hjkt SSb3k7RBLclDh9ibbnJ9KPt/brR2ViZmbxNNQTc/eRM9cetvV+epuglfY Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="326441893" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="326441893" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2023 05:14:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="667641950" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="667641950" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga007.fm.intel.com with ESMTP; 03 Feb 2023 05:14:48 -0800 From: Yin Fengwei To: willy@infradead.org, david@redhat.com, linux-mm@kvack.org Cc: dave.hansen@intel.com, tim.c.chen@intel.com, ying.huang@intel.com, fengwei.yin@intel.com Subject: [RFC PATCH v3 3/4] mm: add do_set_pte_range() Date: Fri, 3 Feb 2023 21:16:35 +0800 Message-Id: <20230203131636.1648662-4-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230203131636.1648662-1-fengwei.yin@intel.com> References: <20230203131636.1648662-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Stat-Signature: cs9fde7wp4mrndbwq5fh3849e3jnpwi5 X-Rspam-User: X-Rspamd-Queue-Id: E4EFD140008 X-Rspamd-Server: rspam06 X-HE-Tag: 1675430091-10528 X-HE-Meta: U2FsdGVkX1/qj1jlG2JlLOg5yJDsm9tuz4b43stPggdyt16QM3hZ48tUPXMvjKzEqBi1bkk0KchHaRLch6bWLwC6CV5V/RdNddrsY1b6tq+qrV6wEgiEvpRSchVJ3P8WarcWfptQuMXHtlsSGxNEje36Yeh0arv1myVmr7VXmIU2fGlVovGUcMG/9tcs/5YHr0V5QBwoYGxjF+2bWertEmh5lGq7JMR9jQ0j63/BaY+vys0z87kWcKmKXWSOsjjloGskr9fOqEj4fHZsjfummsmjY7nu3syaTg/v8kW87BLiXz0z9PcMG6UTxz8upj/FrBkfkJdLTt7ykfO1YTivELCcet4MrLH0QfaCT12oWXeGsWhIUgnqTyeAvh2cuLBJX/vZrBWsxMnUmgfRFRa4raKCnkYe6U/DRUaTHRqo015EF1usvvHcg0gRq5MuBCnCP6KrA6rJvNXdkXKNr5S8E11SOyvunY1VYCUOnvIi84BOx3OeBN0lnfwmnuzY+38T0uX+Y2pSZfSNPxzpMmjhH5dnJPalASwdNYA1SOIZNbYTMfUmrMLo1y/QyedWgueTlu7qIGWbXU0eDrC6Nt/3F+cDlew7GkHTYZF2nvxl2169UxZ0DPLU5TndXO2h5tSmfnn2RK2E2eeqXWhJcarb031UdvxPkwzfhrFD0iFALV7cVtdpODzUgl4QicQxDHGCnUrwN7J+eCg6N/N0O6BK8jMOYv7E3Upbq6AZcqlPjhATfWnQNIyLyJM51wIQl5omPAIX5ls5kuMd0xaJpRr4cOxLfPJKMX+c4Ubzw3X+SIY+epzs40ZZswvYVz1hT0CQJoi7ieuJe5asQugUdOmmvV3esb7s9nLR2S9BGxMlCpTCySsRdU0MoW8Dd71fqqjN84MiTuGBs2JFcl9XtCEqDnLnPNVRNIdkxDhJRyMZURMw7LTLQpX/ceyp2LsRHa4s8DzJ86fHiULyUdUY29V MyD355e6 //NpLLuLg5Z30f5hEdZUEGFwcfq8aYQJ1AIx1cnglCaSQJ133mtpdZd+x19gGg6hzJT01V6irnCwWjxp9DzzV+/2RG2K16zx22W6jF3T2cxL5sCYYfhwQSp1NV4x6gaaSxmYO772eFYZfBNJJOgQZFNAIqtMqrDf2A9+KnsrlGK9nCPpbTUFvMo7nHlMHxLSYOIHdDoxa0DIfmzQEVB2I++3Epg== 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: do_set_pte_range() allows to setup page table entries for a specific range. It calls folio_add_file_rmap_range() to take advantage of batched rmap update for large folio. Signed-off-by: Yin Fengwei --- include/linux/mm.h | 3 +++ mm/filemap.c | 1 - mm/memory.c | 59 ++++++++++++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d6f8f41514cc..93192f04b276 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1162,6 +1162,9 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page); void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr); +void do_set_pte_range(struct vm_fault *vmf, struct folio *folio, + unsigned long addr, pte_t *pte, + unsigned long start, unsigned int nr); vm_fault_t finish_fault(struct vm_fault *vmf); vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf); diff --git a/mm/filemap.c b/mm/filemap.c index f444684db9f2..74046a3a0ff5 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3386,7 +3386,6 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, 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 */ diff --git a/mm/memory.c b/mm/memory.c index 7a04a1130ec1..3754b2ef166a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4257,36 +4257,58 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) } #endif -void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) +void do_set_pte_range(struct vm_fault *vmf, struct folio *folio, + unsigned long addr, pte_t *pte, + unsigned long start, unsigned int nr) { struct vm_area_struct *vma = vmf->vma; bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte); bool write = vmf->flags & FAULT_FLAG_WRITE; + bool cow = write && !(vma->vm_flags & VM_SHARED); bool prefault = vmf->address != addr; + struct page *page = folio_page(folio, start); pte_t entry; - flush_icache_page(vma, page); - entry = mk_pte(page, vma->vm_page_prot); + if (!cow) { + folio_add_file_rmap_range(folio, start, nr, vma, false); + add_mm_counter(vma->vm_mm, mm_counter_file(page), nr); + } - if (prefault && arch_wants_old_prefaulted_pte()) - entry = pte_mkold(entry); - else - entry = pte_sw_mkyoung(entry); + do { + flush_icache_page(vma, page); + entry = mk_pte(page, vma->vm_page_prot); - if (write) - entry = maybe_mkwrite(pte_mkdirty(entry), vma); - if (unlikely(uffd_wp)) - entry = pte_mkuffd_wp(entry); - /* copy-on-write page */ - if (write && !(vma->vm_flags & VM_SHARED)) { + if (prefault && arch_wants_old_prefaulted_pte()) + entry = pte_mkold(entry); + else + entry = pte_sw_mkyoung(entry); + + if (write) + entry = maybe_mkwrite(pte_mkdirty(entry), vma); + if (unlikely(uffd_wp)) + entry = pte_mkuffd_wp(entry); + set_pte_at(vma->vm_mm, addr, pte, entry); + + /* no need to invalidate: a not-present page won't be cached */ + update_mmu_cache(vma, addr, pte); + } while (pte++, page++, addr += PAGE_SIZE, --nr > 0); +} + +void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) +{ + struct folio *folio = page_folio(page); + struct vm_area_struct *vma = vmf->vma; + bool cow = (vmf->flags & FAULT_FLAG_WRITE) && + !(vma->vm_flags & VM_SHARED); + + if (cow) { inc_mm_counter(vma->vm_mm, MM_ANONPAGES); page_add_new_anon_rmap(page, vma, addr); lru_cache_add_inactive_or_unevictable(page, vma); - } else { - inc_mm_counter(vma->vm_mm, mm_counter_file(page)); - page_add_file_rmap(page, vma, false); } - set_pte_at(vma->vm_mm, addr, vmf->pte, entry); + + do_set_pte_range(vmf, folio, addr, vmf->pte, + folio_page_idx(folio, page), 1); } static bool vmf_pte_changed(struct vm_fault *vmf) @@ -4361,9 +4383,6 @@ vm_fault_t finish_fault(struct vm_fault *vmf) if (likely(!vmf_pte_changed(vmf))) { do_set_pte(vmf, page, vmf->address); - /* no need to invalidate: a not-present page won't be cached */ - update_mmu_cache(vma, vmf->address, vmf->pte); - ret = 0; } else { update_mmu_tlb(vma, vmf->address, vmf->pte); From patchwork Fri Feb 3 13:16:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13127505 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 5108AC636D4 for ; Fri, 3 Feb 2023 13:14:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E29CC6B007D; Fri, 3 Feb 2023 08:14:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DB2E46B007E; Fri, 3 Feb 2023 08:14:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2C656B0080; Fri, 3 Feb 2023 08:14:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B0A076B007D for ; Fri, 3 Feb 2023 08:14:55 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 74BD5A0740 for ; Fri, 3 Feb 2023 13:14:55 +0000 (UTC) X-FDA: 80426025750.26.E24A05B Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf10.hostedemail.com (Postfix) with ESMTP id 4A8FEC0015 for ; Fri, 3 Feb 2023 13:14:53 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=L4rDe16L; spf=pass (imf10.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675430093; 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=UTcTiYEDWZ/mD11MQF2nw4B+pzOsZZdmCp0kmgLs/rE=; b=EHLZmGhNvOdEd1XII60a3pY4unHIYmRr7fDACe9pCAyILjT8wxNk+JYgcPbg1FE8D6Aga3 YU6Ob1lhvS/7EsaxHlkuDy2GktXgJ+2LG5IJOaEhX4wZB6Uw5KJuzeGnf+FwwSU9nIqQzy fhWZKsiApRWvJYCndXons/+zmDfDV+s= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=L4rDe16L; spf=pass (imf10.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675430093; a=rsa-sha256; cv=none; b=O3TqnIEdlbOohXFlKD8Lw7DxAWY9tKjaiMSGgbYy7keBH7zMV4fm16+EJTRBxFO693ome1 96XOV24+9FRwdH2egJfph+n6j5Cjwl9+8yby+lk7uCgKmIfOcPFjygP0tGwfqu+uIzBTRh MxBajDkXCuce8wYg8oq5ovtrZh8fQsU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675430093; x=1706966093; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ej/z7Kbe67ZMdA6zpK1CWP2O1oj7nyZh2d/x7rAyAbA=; b=L4rDe16LHM4FqZDmMYCZ+T91Zok0/xRLV9btG/Fff8jQxlmP9g5J70dU ZIjRbReEQOflaQ4qA2OyZxueAXxGLAvbg88YwSXs2B/T+txyDfQO+PRzc 4Tx3OTk8+v30arnuD2B+x3KjNWoqEfpTRtWFRL6l8cuiElJj79Q2fYyvY MLyaAKmm++jFpYPoFRb7zn0klu/qCSrecF/eVyeTtLb0106KHLoKIcYBs 6nCv4BnQFenKStga80sIkR1FRdXO7ZSEfSFWOQculSNFOS5Tnjbmq28qw hvwtI/7FZNdCq9/mI48CY6GpZqnT1XUEwHCXGjKZsE0pZXO6j1l0XHI0Q w==; X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="326441899" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="326441899" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2023 05:14:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="667641953" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="667641953" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga007.fm.intel.com with ESMTP; 03 Feb 2023 05:14:50 -0800 From: Yin Fengwei To: willy@infradead.org, david@redhat.com, linux-mm@kvack.org Cc: dave.hansen@intel.com, tim.c.chen@intel.com, ying.huang@intel.com, fengwei.yin@intel.com Subject: [RFC PATCH v3 4/4] filemap: batched update mm counter,rmap when map file folio Date: Fri, 3 Feb 2023 21:16:36 +0800 Message-Id: <20230203131636.1648662-5-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230203131636.1648662-1-fengwei.yin@intel.com> References: <20230203131636.1648662-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 6zroo55aw1t9upynk69gzxcd1gtcckwx X-Rspamd-Queue-Id: 4A8FEC0015 X-HE-Tag: 1675430093-784812 X-HE-Meta: U2FsdGVkX1+MqKKm6NqpGGysEwLqmCPAP2zVX484Jhw/2nkab14ehECvtzTKThFK5nqcpLP6xxdjg7Jwtd8byBrE5i7AqFXagghbrS8+KXVvjbG63yUaonz5ht+Qo1zcmgkhFyqY6XYvB76LMcp7tDyburWRTkNPLAYxwJOQa+V3t7oJTfXv2yVu20NgxlXNIaAW77xbPkO3EBXFp+g9M5VBggLEY6uzxeG8We+69reljp6UcC/LftCX81JR4x/uTh67LpU0G6ju+Q3V68ksam6vUivEztsZ5g/o8hh04J27XxUxNUOhvm0A7Lp1pT0d9VmdX4m8F908dm7ZZUXVKbtkqjwbH694EpVuJhnk/9U2tZPINkTvdunGxPMk9K2Tm+Kv1pgch9KrRbaGnQpRGBXd9I1bhXyUlNFpM7i003PoyMcaecSXGZ5BggEWLC+84rJpG1qBCdf3/CePDEhBzrWAK5yN9spYuHtod4IplolSVqRSdOENbW8hhyD9EZxR6aTtYfDOCPFqdo2Jgy2IM4RCaqWtHZC87BIEvvzU7wQ3O5iM4/6AgaTtYugO8Xwf5xRxthO4/mpeMKN/MXVlUuScPOSbtE2Ut6GrVSOAOpnUH0SbZ4QlE/OsVSAwPa+gZLjoCLbaRq1ES7RO4XZqmqiGQDs9PfEk6dCB0wIc+n0f9da7GNCQAf2LKipXwARSdhxa6gzL6RYhbau9KzSU5o+9sOwgM0FGm2ACTMTCi8N7D126IQwG3QLridDVNfzSrYkxUFBrdcAVzFW7G8fmJ9EFu0oHhADOA/JPJNa4n7Tnj0pErk4wEAf4y3JwZN0VaeydaYoxweB+siX4G9vldSLEprbk1XqG2QhXTX+1mEbmUvtCT3ciEZMKBUN36QvLFBcZ38vYdoomrYkVFCE9couO4jkLOty4NJGhUA2XdfES1ydWEyU1ihFD5p8qhtO589SrHRedeEy6bVNdP76 738zwL1f xVqAlWbvw4P9Cu8fQOMqPhPnJXGlzOHnvfxNhuswNNdKSuaxS+QZL7VWTW8ovA0oBNnvjywRvYtceSIzz3SO80lsyxZ7DuCre7LD6i9YJvYL34ylKtIG+lMgXdS5t4YNBlbKq3ZL/httadH5OkC6XmJEHVlb8d8vdW+1G7AKcLWqaoD+CjpNNKfv/16+NPHU2QKwH 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: Use do_set_pte_range() in filemap_map_folio_range(). Which batched updates mm counter, rmap 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 15% performance gain. Perf data collected before/after the change: 18.73%--page_add_file_rmap | --11.60%--__mod_lruvec_page_state | |--7.40%--__mod_memcg_lruvec_state | | | --5.58%--cgroup_rstat_updated | --2.53%--__mod_lruvec_state | --1.48%--__mod_node_page_state 9.93%--page_add_file_rmap_range | --2.67%--__mod_lruvec_page_state | |--1.95%--__mod_memcg_lruvec_state | | | --1.57%--cgroup_rstat_updated | --0.61%--__mod_lruvec_state | --0.54%--__mod_node_page_state The running time of __mode_lruvec_page_state() is reduced about 9%. [1]: https://github.com/antonblanchard/will-it-scale/pull/37 Signed-off-by: Yin Fengwei --- mm/filemap.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 74046a3a0ff5..be75352050fe 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3364,11 +3364,12 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, 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; + unsigned int mapped = 0; + pte_t *pte = vmf->pte; do { if (PageHWPoison(page)) - continue; + goto map; if (mmap_miss > 0) mmap_miss--; @@ -3378,20 +3379,33 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, * handled in the specific fault path, and it'll prohibit the * fault-around logic. */ - if (!pte_none(*vmf->pte)) - continue; + if (!pte_none(pte[mapped])) + goto map; if (vmf->address == addr) ret = VM_FAULT_NOPAGE; - ref_count++; - do_set_pte(vmf, page, addr); - } while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages); + mapped++; + continue; - /* Restore the vmf->pte */ - vmf->pte -= nr_pages; +map: + if (mapped) { + do_set_pte_range(vmf, folio, addr, pte, start, mapped); + folio_ref_add(folio, mapped); + } + + /* advance 1 to jump the HWPoison or !pte_none entry */ + start += mapped + 1; + pte += mapped + 1; + addr += (mapped + 1) * PAGE_SIZE; + mapped = 0; + } while (page++, --nr_pages > 0); + + if (mapped) { + do_set_pte_range(vmf, folio, addr, pte, start, mapped); + folio_ref_add(folio, mapped); + } - folio_ref_add(folio, ref_count); WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss); return ret;