From patchwork Wed Feb 1 08:17: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: 13123909 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 5ED6BC05027 for ; Wed, 1 Feb 2023 08:15:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D37556B0072; Wed, 1 Feb 2023 03:15:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CC0ED6B0078; Wed, 1 Feb 2023 03:15:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B396B6B007B; Wed, 1 Feb 2023 03:15:57 -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 A332D6B0072 for ; Wed, 1 Feb 2023 03:15:57 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 8B6101C652E for ; Wed, 1 Feb 2023 08:15:57 +0000 (UTC) X-FDA: 80418014754.18.2E77179 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf02.hostedemail.com (Postfix) with ESMTP id 783C680009 for ; Wed, 1 Feb 2023 08:15:55 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Yk90htaX; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf02.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1675239355; 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=yfqeLOXHYvN5fcwbZ9XofDXQxkaJPcCjRUHM4TDaFeQ=; b=1HPjp84jQIvhSejhDx1PJI5afxZb6O4AQfuyBo8CMY1+Kb/CtLMIQ/cWj+fB/FcSz4B1nh v53LTWTqJD+DjQk3g28/nqkU9fTw0SFBwFjCyyvCaY6PNCEbvwngsn8b/zvCEH0C7TsLsb TdzlZzzrK6Sev87PGEQMWHdaD4ZdEJs= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Yk90htaX; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf02.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675239355; a=rsa-sha256; cv=none; b=7z2V7aFzJnQ89w8vjQ4aNSoscTy+kNHIu5x/MZvEtEnECdNk6DpJPqDrduj1DEN0dI0J5x wy9bBf2IpPObRlq4XL3zR1Y79dmhY+ho8dpO5SI3zwmwMYuZBxqDi4nq66fj211MDCLcwC 1cN5Fzzrlyq1zxeFW7YJKuNlkxYk+GA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675239355; x=1706775355; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=j54FK8ufy2534q2LDgpNKoMNxEN51fsbb9NuCT+dCvw=; b=Yk90htaXGDN7gn/bi334G/1Eaa7g6ypkF/zVGCmpNM2O4oIFgXLhEvwi tb4PZPvsYEz+mXodnGtn3f52QoUDU8OrT7rBf7wkUrZOvf14F/cb+fd9k ugWaTfK4VfD/UEK7Ggu2RF1vrrght9OECLhUVxmLanMp7EiRiQ3TXOZ8+ W4tZQML3ozYf6XFFSRTh3UBtPAILKUDoHqZj2Hjcn7RQRzHelhyKBBt8t xw3d70XqROHhmk7sUkVHbV7n6q17ZgULDpA2YQulnjL6P5V5z+B7XLZ9j nwSOBgJdQIlXJvITWG9oiFpPxf0LkpzXbCX8lCb/9mMWSIdcfZzkD420w g==; X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="390471670" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="390471670" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 00:15:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="753580320" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="753580320" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by FMSMGA003.fm.intel.com with ESMTP; 01 Feb 2023 00:15: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 v2 1/5] mm: Enable fault around for shared file page fault Date: Wed, 1 Feb 2023 16:17:33 +0800 Message-Id: <20230201081737.2330141-2-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201081737.2330141-1-fengwei.yin@intel.com> References: <20230201081737.2330141-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 783C680009 X-Stat-Signature: 5dce1t4yof4n5xwgh7a3yxm9rej3mr6c X-HE-Tag: 1675239355-561498 X-HE-Meta: U2FsdGVkX1+Sq/qSzqkXyhfxk3+15euMTFoaUsx93xYm1UgafVoP5yuIu5OnPfJWBGfdM57AeLksRpUlQgSuxeyf0VF9rvDRlqUNSLRH3g+LqDkE2YLPbw+f9PeJ7AcrsssaTjcaXK4x6hRZor73vqVy6Hp4zkJZsYCpxs2XyeTgJmIS1GDrPYymA7YP0AnooaXOA7e7/0sw9nhbDSIwrWQf3EIwXYoKRaJDZEEXobxhJLedQiwmaLR4o/T0erJfVsLEdssz1dtHVSdeNtNsHMwmrBSylHr8P5ZCAXsk3SIHSN24qzi1o4LI2DiGkXtiRnssZwkkaLHXqYOdsqVqVVjyhfGsa/QtL5mgWNJdu6sp1CqCMBqMgzGiqKjH9Fkh57Vlr+/8pSWiEFw/pHkThhU097Ss6ovr5N6jT1LGXWMZ48i5bzQblpxP+vniov5NgmD+trGxQ0iui2wpJ3LkMBGk27oBKwZBwjc9dR6HwvxVliHc9vEO2PrGKOxkHFkV7qkwx4eBSWsqaR+QtlWbWwVn8X3BGRurNrTQsBv342cZX8D+bLzzR/AjKzfPrNyxPTEMhBKgCCEfROeXauZThncv5pDPL8VK+37IuWozbAAzWl+zzQAqhTwfE77VpIDfsrsPtGQohliXtm03oOCFM8cpddZrOrzV611SDEzED0u/PddpYXnP01q4q0tKDnUCezDKuc3lzgSkz0pkbLBFqUNhyStsYnHy6GXFG50lz3ujiAHAQWxND/HrXa6GuMQ9zUiqDLT8mHsgldUfnEVEWLtw9hbMbOIVUpphLa5gJfYcBZaR8eQBr8lRdc2aZnjQ74Ok0nlTnurXCxDkbPy/KOYoX69PKDzrJKjRBaN29Lp62lTeZCnAW5PwXPegZX4hLZla/2FqrNz7U2eT4zVJgPhxsofaqlv1WV5g/eUp/uytf2AnbO2ASzLSUu/U0PDIPCLDRjfW1hFMFEulx9v /xqs8I8Z Qqh3Gqv5RsAB+1x3wU9P7iRMBiKoPTi921JdZLhfx/Z57LwmUilsRsIs7jniwZrkouiBiD5OPE/XpvqCrkicv1I8unaG0BA2kgZ+lVP3/K2JSLJdJreAKvpTPcrgRum6UEdDv3ot+tSFNAnP9Q14YtHTYUbSp1d4V76o5kA34rR55Ctqn4/VOoZ1e4a9Zp2G/xQdojlYqs6Szpfwsx6C0c2iqz1MAvnzDMODYIxq8K2+GfmU= 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: The shared fault handler can also benefit from fault-around. While it is uncommon to write to MAP_SHARED files, applications that do will see a huge benefit with will-it-scale:page_fault3 (shared file write fault) improving by 375%. Signed-off-by: Yin Fengwei Reviewed-by: Matthew Wilcox (Oracle) --- mm/memory.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index 7a04a1130ec1..51c04bb60724 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4546,6 +4546,17 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf) struct vm_area_struct *vma = vmf->vma; vm_fault_t ret, tmp; + /* + * Let's call ->map_pages() first and use ->fault() as fallback + * if page by the offset is not ready to be mapped (cold cache or + * something). + */ + if (should_fault_around(vmf)) { + ret = do_fault_around(vmf); + if (ret) + return ret; + } + ret = __do_fault(vmf); if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) return ret; From patchwork Wed Feb 1 08:17: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: 13123910 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 41DABC38142 for ; Wed, 1 Feb 2023 08:15:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A528E6B0078; Wed, 1 Feb 2023 03:15:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 98D416B007B; Wed, 1 Feb 2023 03:15:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E0BD6B007D; Wed, 1 Feb 2023 03:15:58 -0500 (EST) 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 619D76B0078 for ; Wed, 1 Feb 2023 03:15:58 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2131B1A0498 for ; Wed, 1 Feb 2023 08:15:58 +0000 (UTC) X-FDA: 80418014796.11.93EB691 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf17.hostedemail.com (Postfix) with ESMTP id CC80140015 for ; Wed, 1 Feb 2023 08:15:55 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="Ao3bG/LJ"; spf=pass (imf17.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1675239356; 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=UJUMh6py4O0Tr+jNGUObiyCKiauCrv4jrbrkHaA7X3Y=; b=hZwqIfOp04+DI3xlk3vSvYxQlfvskJdKyZ5Oj+gZ61BdHrYcQMt2rxzSp8XfrD0xLYjKFf nwYaL7uN5RBuwt0KSpZBPYBqdPDtl9uj1bp6XHn0DWyEfOU4ZU+Rg6WcavQtwfx4qNBcVF E8zyXrghvPziUkY9PaA9GO9cAVwq5XA= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="Ao3bG/LJ"; spf=pass (imf17.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1675239356; a=rsa-sha256; cv=none; b=A7xzk4seNB2XqhYiJMJV96CmNOLzOrf2K7dAnFYr3Bwf5/zZAYojG988ED0LisWNF7+v4z iti/TQqnPPvzQ9wZY03wueGFmH5otbGufYeEGfBxWX3IrKas8JOkpCqgmGoJ/0MriHW4jT RclOtncrEzJuxSBkCNUIz+6mpRWCjmg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675239356; x=1706775356; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Gq3LL1aqVMDzRz4yoac8QrMuaWFnU+iYFwQ79VrezwE=; b=Ao3bG/LJno+J/oKZKSth2wVk0Sx52ES2Ac5tIGPEfrHV/suNjWGas4ek mrF92FhOeHT0Bdzu0uwu6M53eDzd7BlisglMfi+oPwYojWOJi/DWncKeA yM+4LSlNx0inX39dlDdwQJX97xEILEuu6ebGY87RYINjDhhHfZX85wRAh zEJffCT0Ah5ALnMJx+Pz8pD0wCflvdbacBwNgLbXPHX0ITPqElf792EjU tCbHaEuNASy9cLou6xUTtDdS5gAWEezyrr0R/V3UjJp451BNNIGM9641P wIK4x+MSgLMRieb5+3tiFiuD/VT1QCsuKSvC/vr43DY5FQK57Q18RWygc A==; X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="390471685" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="390471685" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 00:15:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="753580331" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="753580331" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by FMSMGA003.fm.intel.com with ESMTP; 01 Feb 2023 00:15: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 v2 2/5] filemap: add function filemap_map_folio_range() Date: Wed, 1 Feb 2023 16:17:34 +0800 Message-Id: <20230201081737.2330141-3-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201081737.2330141-1-fengwei.yin@intel.com> References: <20230201081737.2330141-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: boibibnponsnwunx7a3bit6itykx634f X-Rspamd-Queue-Id: CC80140015 X-HE-Tag: 1675239355-848600 X-HE-Meta: U2FsdGVkX18c7rAkPklsytO5OBmvU6oBuaIwOqh9SyIxA7HTI3drBS4RdNkfmhEXnSPVar5JcU2Im47AdcyOlmb2a9jHY3+YTkKIimPYiv4RYw+e2oA8ekX567op6cLV1kzfyi7bUq6Q88zLcnAKtsxKpjkTdxbSlWvhNUypSyVFNouroAnp9wE06yMz3/8pXr9OmnNmo7rklGGDZAwAOlWX36oKi2t8Euohpktvvya7YOBUl7lvdGw42IkdpB4JKPGn8VvrUa3it6x8pmITdX/MlOgo0jBZFSVg2R7AazvKJTd9ibqyPhnsao212M5whrSV/ClJk7OfpIlTynEWmAIEHJHkQygGJ2BtawKJTOjqXZfrF/766RZ/vfQ5DNVTQN/z/yLBLWxM9OisUNV2fEPnexsGxjoO4KVEXQkQ0U1FG+hLppqlcYDKZCw7iiwSBEkaJp5Fah9XnAJNpFWVJyWvcZ+/XAaZaqGjQ9I2+iv1NKqLT8QkJCPJGkpfpgn58W6/Rn/A6fKCN8hDT33YzGA3IQHA6RKYsXCvClUdZm7iA1naKkPJ68R7NrmSuAPztKnzzGFbEToabNokZ2nrE6UwJo4Xu7J2B6EKuIGkI11Uz6vzE8jKu2SItiwwFD6kycR+Y98Ns+YMj/UkjjdlWEU8/ef8OIdTtQRJOcreJ+mOClph+ygWODNqqEsY/CSyl/q9GVp1k/Ra4l2/NY81oEn2precSiuznDxY4XU5F1980I5hRYCPOb7CQDZndF4uiaV5c4reQr2aqZgiHfLYZdlYC63XSq0w6kemIuwFHTsuJ2xYE5IVS2JypSHNHxweeE7TmIWsVYwnopw/EzbpbXI3uCpk25bluQbwqnDVfvQsDy3v5ZDlGx1dnjV14f2LtkuIAzerTJAv2IDlOzU739DWFEiTSBR4bdnNTX1yy+EBrOa2FAD7B3XbodTzgaWiqgEkNXCJlb63mFOd3Zm oEm3fYy7 mpStDPpDry06TAvtSynUqaotIWpXmhTTkaCp0WIZLdzDpFl+tzVdV6mbn6zKPV42a8HRHoHgz+tUoNiE+MXJXZamjK/qvnUGxXdUHyY/ITWIneQ1yO4y6RBbEK3MPE3n1LnL9QMwgAqcEQ5TrrsEFclUshurP0Mtan8uz1c7I3QMj51d8/AEmBnPM+ajOCAosDc7KeZZESctP8rs13NNiSnBq5g== 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 | 91 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 992554c18f1f..9cc5edd8f998 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3351,6 +3351,56 @@ 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. Otherwise, it's possible vmf->pte point + * to next page table entry if the last sub page in the range is + * mapped to the last entry of page table. + */ + 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 +3411,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 +3428,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 Wed Feb 1 08:17: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: 13123911 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 BD04BC636D6 for ; Wed, 1 Feb 2023 08:16:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2FE0A6B007B; Wed, 1 Feb 2023 03:16:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2619F6B007D; Wed, 1 Feb 2023 03:16:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B2D76B007E; Wed, 1 Feb 2023 03:16:00 -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 EAD586B007B for ; Wed, 1 Feb 2023 03:15:59 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C1C051C6511 for ; Wed, 1 Feb 2023 08:15:59 +0000 (UTC) X-FDA: 80418014838.25.C9CA551 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf02.hostedemail.com (Postfix) with ESMTP id B8F2A80003 for ; Wed, 1 Feb 2023 08:15:57 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EBm14QwP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf02.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1675239357; 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=3v020W80207K5p7bwR9NFT8ToRSjhAW25L7AUqUD7gs=; b=Y4+t5nTZxLZEltRcDPp0W/OFzFtwbCXjirf2uN5Xh28+tOC80sMgNakvoJFnE6qEgdxVHJ nu1RJ8I56nqoUPaY+DOU/LgjS505iFTwDBOMsUpWgoUUHYc3nQyQZLc+9o4pFuBD3neuSj h8gkFw2guPfXx5h+pd7cqWPoJlvcyP8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EBm14QwP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf02.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675239358; a=rsa-sha256; cv=none; b=6LUH/GdAYWFC+OtpDvasnCqiEjKqeyjMCde54CnexEQWc7TdXeIszz7PgmTqFH5GJZ19lR KndisxHwgYGD0i5TtnVVbD5c0RX7XNKMrCUEcEVzKsOdzqmhNnb14HDU8YZgE1I5AF926w jYn2gOH+IIRCMbzngsYSsDyUoREN/n4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675239357; x=1706775357; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/JXq38FVFetnXKwqqjKvNYEq6660noLrsCCrhSHJtHk=; b=EBm14QwPeBud+j/4VWoxB3TrW8b5NDEAlHckHrPNFJZmQuBNSG/G4une C9L/o24XCaX8kAH2u/5vo8gz/aJCjNTsphOPp8DNuATbRR/YVb4ZPz7FT 0PZEdVgxLzzyT9Vaf4YWZCX/4Rb4mBxStHmwcjEsdpjDSlgbkTAy8Ru3a 4QdvSqnEXcdy2SZtDk0RQ7JBZ8vUmiPjXHZ7L7sxrJy2heC82lj4pTUZK tuO4XcLYtM+LtqqMiSdXGWpoXZzeD7coJOq923ESicuqR6BeGaOcU64K+ ch2mZvPoFAYA6NBHFPPCsgmcIKjNmeeugEN2QFeZdW4cSAxj5mM3iXMrV g==; X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="390471694" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="390471694" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 00:15:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="753580339" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="753580339" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by FMSMGA003.fm.intel.com with ESMTP; 01 Feb 2023 00:15:52 -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 v2 3/5] rmap: add page_add_file_rmap_range() Date: Wed, 1 Feb 2023 16:17:35 +0800 Message-Id: <20230201081737.2330141-4-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201081737.2330141-1-fengwei.yin@intel.com> References: <20230201081737.2330141-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: B8F2A80003 X-Stat-Signature: jjk8qeeacftjtf1yttzbcejutwmgfta8 X-HE-Tag: 1675239357-252113 X-HE-Meta: U2FsdGVkX19D456xSM2dRGWfLkf34IoydD53CLDNiQ9S4JZhsgNwyJKZdUwRqvHYTbv1sxetFNFkNm7PQccSXlkyMOrbw9Q2CcE1c3JDjuOtw1G/yn6uuqaWtFbpU6JOrrTioxr6guLOACuqnCgIC1fWDHEth6rOo/dTdKfX7beFsiMoAY9Ii+ienl3mUrFF1d0zAAVJcoKJsYlPif6q/P3gCafcM+uvbMa2KsRC+Wc1KZWoJ334xEtSuu7Si7Lti0qaaFxQpQ/eSFr7AIE2Onba/2w72dsW+SQK6eiKPCtpLzkcXrUf9y7XG+99AzVs/VQsr5xDVwh9bkBAl/ZD6+E5OxVSydIa2pfwoKDgTSbVeFcRJf0MxUReyvGJ5MYHDKWp5GZnr/qFBpgvlv2QRv7TJIgmfFsBsmJZn3hUChMypcCGuVT2XrvoAShHv1x2PSw/h3o0CkrdNmZwHNatCA94y9V+rXjJwF4KgyNHtwio4uPMXxVspsF8oH22Ix1a+xyh4CfqP0ulIXcoxp7lawzrbZ8Zpw22YLo4AHt63fOa53vGie9AovWnZgEYRlUIDTvDFL/GH+vYAdpH7CBIfFPwiFF96FX2TTPXce6s+izdrLe5CDaDMnMfK2J66cXhmUZkezG/rveDycq/ml9w7YMiD9tU4HNvrgRAq/vvpJaVDAKDjFyCykv3zsTXAtwAJts8uO+2DFC17ePGiBBxuD7Y0IW5lPO2LQgG5Ftqdn+wBbUlFFllRW/GBXovJpmU6gsfxoKi3+lXGmUjqIb8nGmsVABTKB43kqiRvDkEPk9E6sGK+25Yd9nYa4cQQEwcy1Mw3XyFT60uUZ4EPSqee2Aibf6DHn/CUpXpBAk5YL55fnGKb/FGzRUJowPwr2RRixivg8Dqcf2WYwF9MjzuTv9u4r2PlTcN80/AOqZP3NRIxNCL5JsS+2Gkpp4v9HAxclAlkxGdYxx/JpFxKOd H0h4h0lp pYg2G90OZtZLhsa0C3SAfvA4JJ9Cc2lbMOohA2V5k1xzSQZ+RxNpMGH/aOKW4duN4Ptv3N71cJYwCmsBEYll8+8VTMZmmox0N2MYYDkdy7pqALO1TcwKqy3lz4JhqhCPpTS/zZ9IgHWZJeBzR3Mif/fzIZMUwL1Z4LLNQIf9EhQWtLyAMyA+pBx7PK9Es6Gf8z/43JczdTRCRTxnfL9Ab150lzlWTf5NMmFEy1rQb4dNaRy5u5Aj+5wyCJYr2jjlGBy4K 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: page_add_file_rmap_range() allows to add pte mapping to a specific range of file folio. Comparing to original 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..9631a3701504 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 page_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 15ae24585fc4..090de52e1a9a 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 + * page_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 sub page index in folio + * @nr_pages: The number of sub pages from the first page * @vma: the vm area in which the mapping is added * @compound: charge the page as compound or small page * + * The sub page range of folio is defined by + * [first_sub_page, first_sub_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 page_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 = 0, nr_pmdmapped = 0, first; - 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); + + page_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 Wed Feb 1 08:17: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: 13123912 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 203B1C05027 for ; Wed, 1 Feb 2023 08:16:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 918956B007D; Wed, 1 Feb 2023 03:16:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A3BF6B007E; Wed, 1 Feb 2023 03:16:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60B276B0080; Wed, 1 Feb 2023 03:16:00 -0500 (EST) 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 2D1156B007E for ; Wed, 1 Feb 2023 03:16:00 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0D1FD120D6A for ; Wed, 1 Feb 2023 08:16:00 +0000 (UTC) X-FDA: 80418014880.26.CE03422 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf17.hostedemail.com (Postfix) with ESMTP id 07D984001F for ; Wed, 1 Feb 2023 08:15:57 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZBq1DstY; spf=pass (imf17.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1675239358; 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=fDcWKoFwFljsPK3KsT6Yo+8RTqtHxchd2WBzZ7mkuNA=; b=k/xadlq4KCZ2Q3t3jIBYlG03r0pmBiiAhMQL70oTLplAXwpio7q4ub2PWZ6eiKEfwOPP5l z64zOHP/W0XEBLcqpuTlZgJz7o0w1XNN2gntffWcgBKQJDKb+bUZUwT5Ld5cheeNFVMDvx pcjY3V4QsEKG0Gmz6rLHG/dsPNjkPDY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZBq1DstY; spf=pass (imf17.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1675239358; a=rsa-sha256; cv=none; b=lyMtBp0mE+IACCiL2AmDZvGQgimQA6xCynfDyVNa+GAxg0xXNpQbcncqj2mFKEJokytAMy Ni/3nypJxBqTTWzeIrfuZXOm4uDPm2Ttpa9sqjlRDmeuVOPDO7d0umSA/fG+g4jckvlLAl G4GFOx/911ntaxHvRLilPdkSLOTQK0A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675239358; x=1706775358; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Clv/GELshdHOgJDdTOOm5URWceoffx0lE1daZT1k8p0=; b=ZBq1DstYgpVMkYmWmDSiiYBKV06NYBor0ajzLlabET4zgPhvI+HpKLQZ kWihZmXjr3WiDP8xIHja2RkW4goFNtO9DJFjYVkDFoHTKCgLGezOlvVnW dWagyNA4kD+eygUUzAHsBGP5EyEVwpfH4DluENnnik9dxDM9av1fDBWJM WYdcYSXgqR9n8HIh2tLfPgdNftDWXhgR1R87YifqzZ6rGsD9yswN6J6Kc 297iTanJ1ovHfLgY11pESkSS4ztEJL1x9PM92zio8n0dhkRgsY8leV2s6 QgcC9BFAYwXI06ccCQrhyoDuuLo87+80fT7Ml0A5LoLOAipQlm80+apsz w==; X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="390471699" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="390471699" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 00:15:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="753580356" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="753580356" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by FMSMGA003.fm.intel.com with ESMTP; 01 Feb 2023 00:15:54 -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 v2 4/5] mm: add do_set_pte_range() Date: Wed, 1 Feb 2023 16:17:36 +0800 Message-Id: <20230201081737.2330141-5-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201081737.2330141-1-fengwei.yin@intel.com> References: <20230201081737.2330141-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: apq8zzz191judynsqfowe3onz4qm55qy X-Rspamd-Queue-Id: 07D984001F X-HE-Tag: 1675239357-236278 X-HE-Meta: U2FsdGVkX1/4b2qbs2y2uAgwssTd+0kgNUOdU8E38I1puyjtdiJMaaaVGhw4QwmONAYS3U+yBOrSfdXXwHjNqNND0ZMgVdgn6my5MlJgXOJVeIfqznOMYUfh+vUzMxuJTiuxciiR1KbZyiSE/gICdZASZ89DlhbhKPqCt5xKM9YkGvx6efyqcmoFCCglGQjGXeF4Ui/9jrpBSqXE9lagX+9DPzlDxg7c2EHOj6rIRq8stlVrrehdp5q7aJQmX07KyxlyXJicsK6iD5GY8k/h4s3n5tsOzOjX8xLD8EQ+GR8Lp9VD5om3TKA7aWOtrwSQRY5S/m1zvGqspLpYvcmNvk3pzngyHS2Of3+dq1eZQa+SCbgtnfHITICGCgYvbLnTp+tOF4VlkfXDzbljASzI/4s2Spias8v2ne8Q3KGMVDgalZwPpkEe82bQnMmm+xlIErFcJtxC7fc276lrtAUO7Aw6TZsnbGStilg1r8qMCZnF0UMH6pcffJ7uhXLFqYP6v4l7dV1f3ikUtAU8jxEIJ81bRwx6GKUtcAUTfHZJAD2aKsgrZcg14xCCVOjZtUlQJ9IanO8KQtxQm+TJZ9O+0VA4yocd5yEadIDfYxCNEqxwYFxiaapwLKMKvRphPsPy9oJDZhccp2hHVPDHfSEIE7WCus3uH437/MJcVglJJ+KbKhqTzctu2MvKKads09mznN2uEAtf4jRR607YGGJ+J2E43aVG4deoh0ti+GIQkdXhBDO1D9s5DY54nMW+RktxE21Fn08HAVA6pFUaZw+4/FeWyDoQAjKWHN6laO39oBtbF1XpNJmnkHwN4zLxaRvFebTI+u4YHbKR8AUBMGBgGNh9Go4iYBYNAULMZf+U7GemT+A/7tocDfrYfkaN6j9CnyFLYTCe6mNAc1ljcEsyIEUmcE5IukyRp2cy+38I3yLsESQXLlKKBXh/vxJ6SDh3yV3sEE+4yJuf6TtuSLQ Rmjuqq/q 5SW/HsG0L7C+h9aXCTgjTEXrQiuic/MxHawXh8H7Sfn9AXXrRAwujg5NpTDe1Npt+t3pnOGUBgQz65+QYvPGiY0XidhFzo/Ox63LPNuyx+rO57w2k5Hjp2h3xa+UkMNa4nc4f9L4yEaP7q1Hptn/HviGLYGkcaEJvcTzz6R5Wod5pG/Sflqvkn3oid/Q2p5RTJFkm0+NKpT0ArSmkpieBOl9Dfw== 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 page_add_file_rmap_range() to take advantage of batched rmap update for large folio. Signed-off-by: Yin Fengwei --- include/linux/mm.h | 2 ++ mm/filemap.c | 1 - mm/memory.c | 60 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d6f8f41514cc..96e08fcdce24 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1162,6 +1162,8 @@ 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 start, unsigned long addr, 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 9cc5edd8f998..95f634d11581 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); /* diff --git a/mm/memory.c b/mm/memory.c index 51c04bb60724..7e41142e1e4f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4257,7 +4257,8 @@ 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) +static void do_set_pte_entry(struct vm_fault *vmf, struct page *page, + unsigned long addr) { struct vm_area_struct *vma = vmf->vma; bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte); @@ -4277,16 +4278,52 @@ void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) 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)) { - 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); + + /* no need to invalidate: a not-present page won't be cached */ + update_mmu_cache(vma, addr, vmf->pte); +} + +void do_set_pte_range(struct vm_fault *vmf, struct folio *folio, + unsigned long start, unsigned long addr, unsigned int nr) +{ + unsigned int i = 0; + struct page *page = folio_page(folio, start); + struct vm_area_struct *vma = vmf->vma; + bool cow = (vmf->flags & FAULT_FLAG_WRITE) && + !(vma->vm_flags & VM_SHARED); + + /* + * file page: batched update rmap, mm counter. + * copy-on-write page: batched update mm counter. + */ + if (!cow) { + page_add_file_rmap_range(folio, start, nr, vma, false); + add_mm_counter(vma->vm_mm, mm_counter_file(page), nr); + } else + add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr); + + do { + if (cow) { + page_add_new_anon_rmap(page, vma, addr); + lru_cache_add_inactive_or_unevictable(page, vma); + } + + do_set_pte_entry(vmf, page, addr); + } while (vmf->pte++, page++, addr += PAGE_SIZE, ++i < nr); +} + +void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) +{ + struct folio *folio = page_folio(page); + + do_set_pte_range(vmf, folio, folio_page_idx(folio, page), addr, 1); + + /* + * do_set_pte_range changes vmf->pte. Restore it back as + * do_set_pte doesn't expect the change of vmf->pte. + */ + vmf->pte--; } static bool vmf_pte_changed(struct vm_fault *vmf) @@ -4361,9 +4398,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 Wed Feb 1 08:17:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13123913 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 81E12C38142 for ; Wed, 1 Feb 2023 08:16:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 76B646B007E; Wed, 1 Feb 2023 03:16:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F79C6B0080; Wed, 1 Feb 2023 03:16:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 40F036B0081; Wed, 1 Feb 2023 03:16:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 27CA36B007E for ; Wed, 1 Feb 2023 03:16:02 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 01ACB1408F4 for ; Wed, 1 Feb 2023 08:16:01 +0000 (UTC) X-FDA: 80418014964.02.BECCEB8 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf15.hostedemail.com (Postfix) with ESMTP id C606AA001D for ; Wed, 1 Feb 2023 08:15:59 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=D5JsGAfK; spf=pass (imf15.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1675239360; 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=A8TUmg8Ng3w8EXE+p8s+IzBj1XvOj0ff6BNpqb3Ep6Q=; b=qxS9sNSXCbGlvr9xNoZKSbhuqS+tRTW5cpQw6/ZjugKpR0NVBH+Tsuq8XNx5k1WzdWTq+G ajDJWJ6MxCdFLrGooZ0RpKeHkU6mX27TiXdcByv8y1sDSQtpLg87dq+1xWdFmpdVHs0aPt DYTKKvaHT6JlKRIuPXm0T+okuAsQwrc= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=D5JsGAfK; spf=pass (imf15.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1675239360; a=rsa-sha256; cv=none; b=Y6zTGllwkuBykSybesH9njPI/YXA97RAmz6akYkdGDSCD0GYkKDe+MqElOsITIJKC0dehP PE7DIjx0mdysPvhWAvupSKiEqiI/9ompaW9zH1+8i7k/YZCR1ggwos23jT3zZuCBPlx8KT z4JrCzx5kZsAMATb/F3A9kvccGSQRto= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675239359; x=1706775359; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FELofmlHKe7JG0HLem2mmDqUAOxt0yESMF6m7PRi5mg=; b=D5JsGAfKDIYaG5tuwKLK95+b2UT7nkchAH+TnwR+seRg9PhGSawGdLOG MGW4aqLp7y+rLrwdzBh3dXzJbL5iLYjxqG0LRfn+sIK5GTduLkVs365hw i2uymqVWA6i9BQDINpbCV7VPJXblMW6x8sdy/LnFn7jsv5QgI7yHsJejc yjOx++SXAZf+e3liuBC09730cWXSnuZHKEKCvLykutvfdskToKe5pkJ3M dCkew558aovJFHQPHeIMyaH/udDP1vUZt3ueMTYtqTLqII/gIgf6lZFBR L+NzVNdRyVX/La+8zNvc9FN7LRLc5BHo7RizB0kBYVqN2bdFKc6qs3bQq Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="390471713" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="390471713" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 00:15:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="753580383" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="753580383" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by FMSMGA003.fm.intel.com with ESMTP; 01 Feb 2023 00:15:57 -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 v2 5/5] filemap: batched update mm counter,rmap when map file folio Date: Wed, 1 Feb 2023 16:17:37 +0800 Message-Id: <20230201081737.2330141-6-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201081737.2330141-1-fengwei.yin@intel.com> References: <20230201081737.2330141-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Stat-Signature: 6uuiar6hapwqdomr78jx4ckisgkiuexa X-Rspam-User: X-Rspamd-Queue-Id: C606AA001D X-Rspamd-Server: rspam06 X-HE-Tag: 1675239359-586800 X-HE-Meta: U2FsdGVkX1+/U7w8URMEOZ8uoZoM2E+7cFWXG1K78/KZ0P3URVHGTCII9tWHtFxmP3zm+/yzOXzs6bnlP4f1fDyjIuV0Lt6oSNXWqhgX5P/7SaftB+32NRG+4ptw4j8+Gd0Lcu8Fhzyp95aKjkQ/QGTD1ph6gBM3+USslb/7Rmto5Kplud9uzXAh5++NErwepFh25BHD6UjUkxO1qXo3xWi/F3zYCcXFmJoMkkOcoqE4/3WSoBtKjTWnbOKmYzzteiQaYiGZJEfIZqeVxExMX9mk1DaSlYHyJnyEPk5l+ax73FsjSTFFZ5JHhJVQJZSvsmbjffDOuVENR6MHUjTvKEUvdyKpGHDjWlSEUNnoiJZzgSEXV8m9hiciMMIIQmSPBf4E0umYW3q8PFfxxhegKs4EsHesY48ydcr0bMElw4D6kvymfp0CPx/5RRBa0B5arOAVO8ES+/XY0+4PrRv3QdzrhL4y6+qCD+rEoMIlaVi7dWwcdUZ72LHQaw/GmM6qzqk1VxTJVwZbLq443imAK/V58uHDX80vDBI1U5c/qObe15vC6qBYZvTyYYIeDUT5U6P+VGzYCYYNC9a0HsrKtOeXEcHM+cwxhR4WW8iOv3NC906vMGFKWb9iVXNSZSbgttsg7I1vFRGlowW2j+RWuXk8FaH/UCvq2/H98+2ar91s1fYZHawxE7WWzouN4qe4nuHMM5Qrut0j3+Q7kXxXEJThHb6/ILZbcIe+T/8J/MYM/6VKanhIesgEhDO8OOIUk/AT5xz17Xn17dOlaeMuUuRVxQEE620BVj8Ljs37CE6Fw82mmUxqIjqcKsxTwTMtSkXXhH/p9j/WqzVbM+0OQu+TSOgKY90nyN4yRpqYRABr/EkYYwYGKNCtuII5MCk/37ARhP1Jl3GvUsq6uwfr8GpE+hsytqqHIXNAZFa8Eo6SSIOrbB1sMTLdhz4WF3th7+EdB9t6h/6HsFt4iWM zbRL3yQY dRbGiSi4jDU5iaPd/gD0nO531ausgFwmh7KvxNA+Au2JuzINLSlX80SMO17WzRt5V4jLam6dYPc34WVTVYAP7kLvBgJtygguuV0TJVUDjfs67XLz6ocnPEGh64wJcVdHZ85y3cT/1FcJrhdj/44MTeMnq3bjK/GYMSSvltLa17hCFdJQ= 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. With a self cooked will-it-scale.page_fault3 like app (change file write fault to read fault) 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 a lot. Signed-off-by: Yin Fengwei --- mm/filemap.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 95f634d11581..b14f077d1c55 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3364,11 +3364,22 @@ 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 ref_count = 0, count = 0, nr_mapped = 0; do { - if (PageHWPoison(page)) + if (PageHWPoison(page)) { + if (nr_mapped) { + vmf->pte -= nr_mapped; + do_set_pte_range(vmf, folio, + start + count - nr_mapped, + addr - nr_mapped * PAGE_SIZE, + nr_mapped); + + } + + nr_mapped = 0; continue; + } if (mmap_miss > 0) mmap_miss--; @@ -3378,16 +3389,34 @@ 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)) + if (!pte_none(*vmf->pte)) { + if (nr_mapped) { + vmf->pte -= nr_mapped; + do_set_pte_range(vmf, folio, + start + count - nr_mapped, + addr - nr_mapped * PAGE_SIZE, + nr_mapped); + + } + + nr_mapped = 0; + continue; + } if (vmf->address == addr) ret = VM_FAULT_NOPAGE; ref_count++; - do_set_pte(vmf, page, addr); + nr_mapped++; } while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages); + if (nr_mapped) { + vmf->pte -= nr_mapped; + do_set_pte_range(vmf, folio, start + count - nr_mapped, + addr - nr_mapped * PAGE_SIZE, nr_mapped); + } + /* * Restore the vmf->pte. Otherwise, it's possible vmf->pte point * to next page table entry if the last sub page in the range is