From patchwork Wed Mar 6 08:38:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13583500 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 18491C54E41 for ; Wed, 6 Mar 2024 08:38:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8CD126B007E; Wed, 6 Mar 2024 03:38:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 87CCA6B0080; Wed, 6 Mar 2024 03:38:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 76B8A6B0081; Wed, 6 Mar 2024 03:38:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 683BB6B007E for ; Wed, 6 Mar 2024 03:38:23 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 12F2DC0473 for ; Wed, 6 Mar 2024 08:38:23 +0000 (UTC) X-FDA: 81865962486.30.B8A8393 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf24.hostedemail.com (Postfix) with ESMTP id 1D3EE180004 for ; Wed, 6 Mar 2024 08:38:19 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709714301; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=V4GsK/2pFGQ85HHnecmCAR/+8JUl0jm6qUN2x1dqCLk=; b=0wwNqT30tvkvHddeZBHWPk2K7uHqZQgnSw/qOzrHB13M4ZEnZpTlo30VKdIukNjujSH+LE bZiZYGf/ZMMhi0Xt/y+W4cSmLtarCedupVqwz5xtLVj6c7Jkba9iT1iaQI2V6IBGTsoexH bHywyFyERqJM8IHnqAh//66oBseMQvQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709714301; a=rsa-sha256; cv=none; b=0E0otcLTcN//19hSbvtdF7j1+HZEuwChoKuLx92uf22C/HrgHzDiLt4W7lUhrkD7oHGfMf 5z8d4ypQFtBwbGumhvtcufg++g5O0HKzUqFXABXYDg9cXTwmjo+OqpeVXs1y7GCDq2SBHo XFhKfkfudwoV+65uF9IiuU1ISpE7DdU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4TqQmv5mpzz1FLs6; Wed, 6 Mar 2024 16:38:07 +0800 (CST) Received: from kwepemm600020.china.huawei.com (unknown [7.193.23.147]) by mail.maildlp.com (Postfix) with ESMTPS id C980A1A0172; Wed, 6 Mar 2024 16:38:15 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by kwepemm600020.china.huawei.com (7.193.23.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 6 Mar 2024 16:38:14 +0800 From: Peng Zhang To: , CC: , , , , , , , , , , Subject: [PATCH v4] filemap: avoid unnecessary major faults in filemap_fault() Date: Wed, 6 Mar 2024 16:38:09 +0800 Message-ID: <20240306083809.1236634-1-zhangpeng362@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemm600020.china.huawei.com (7.193.23.147) X-Rspamd-Queue-Id: 1D3EE180004 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: bkmrxaf3rxcnupufsk5kuu6cdmusr17w X-HE-Tag: 1709714299-460443 X-HE-Meta: U2FsdGVkX1/3xWEdAYRCAyeUI52PTkNkA2jnw92efZY1C2vdxH/vP6j9EJxufyxVZuWgYyVV3MTLGEPlw6rHua65uhLaOS69v2PUJ8yO6BkIozy1pMAAber07MiUAxfSeO0xUhsThZqNG+kznpwc7Hi+msQ17WDBIChFA84xRDE0CDhpH+5MFuiZ0vqZttlsD/vfQ7T+UrdxhLIEdnVUZ/Nq5OGw0nelrWLgMdFiZN0HAGoy5tigR9ixsmiUfp7GsxIalcPgOjA5p+jp3TgiJ5gnc/TzvxFvkV/ksOh23JTf6Qci2ooAN1CtDPpEGBYYYaWxlPsJKZLPGOwYAaStT4imq4b6pjImcBv+ke43LcLim9aKh4WzQ4FTN+NP4H6K42tW/CA8rYh6+S5FLyDd02G9c5RMLyw6YJw+OQaGXtBfNFzGSQYxjN18qHQYADH4VOtZgiEeQuRGY3GYnUUxl+YZiFFuvpjDmk8CPUonIxL4kpyCJ+ZLsI0tiFpwmGXUYrAMK09OfmRLlChLLKmQWUYfDTxwxGLcrERjHFYxLbi13WpUdREuegQo2CAenCR6VXdIaIY3QDXHt+/q/GZYnmpQKxlT+up8tLgsKzh9EtjfJT/2vo+7tA/j8yt7z4g3lv4d6NIRv+SLCxkclsFtSEiiorh897yLN+Mp//mAG3UI2JRb6qq0O0UMt3PsG1sVtmuxZ99abR7KSa7MveGR/1MXSfHUEQkyonUOj/X94OdAJFuKzBHhUb32Z4AJjAe0UQCrO5833b5G5OwIzj+4uORQWL4V+l5FtgsvekJTuJzJ5aJhmrBx0nloI+1Bt5bJTtlWRvbgz64BiCto1DevvFGd6OntiX3KSO2imb/zRgrNa7RIsU76k/GXMWFHGiYYc2g9wGRah0w= 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: List-Subscribe: List-Unsubscribe: From: ZhangPeng The major fault occurred when using mlockall(MCL_CURRENT | MCL_FUTURE) in application, which leading to an unexpected issue[1]. This caused by temporarily cleared PTE during a read+clear/modify/write update of the PTE, eg, do_numa_page()/change_pte_range(). For the data segment of the user-mode program, the global variable area is a private mapping. After the pagecache is loaded, the private anonymous page is generated after the COW is triggered. Mlockall can lock COW pages (anonymous pages), but the original file pages cannot be locked and may be reclaimed. If the global variable (private anon page) is accessed when vmf->pte is zeroed in numa fault, a file page fault will be triggered. At this time, the original private file page may have been reclaimed. If the page cache is not available at this time, a major fault will be triggered and the file will be read, causing additional overhead. This issue affects our traffic analysis service. The inbound traffic is heavy. If a major fault occurs, the I/O schedule is triggered and the original I/O is suspended. Generally, the I/O schedule is 0.7 ms. If other applications are operating disks, the system needs to wait for more than 10 ms. However, the inbound traffic is heavy and the NIC buffer is small. As a result, packet loss occurs. But the traffic analysis service can't tolerate packet loss. Fix this by holding PTL and rechecking the PTE in filemap_fault() before triggering a major fault. We do this check only if vma is VM_LOCKED to reduce the performance impact in common scenarios. In our product environment, there were 7 major faults every 12 hours. After the patch is applied, no major fault have been triggered. Testing file page read and write page fault performance in ext4 and ramdisk using will-it-scale[2] on a x86 physical machine. The data is the average change compared with the mainline after the patch is applied. The test results are within the range of fluctuation. We do this check only if vma is VM_LOCKED, therefore, no performance regressions is caused for most common cases. The test results are as follows: processes processes_idle threads threads_idle ext4 private file write: 0.22% 0.26% 1.21% -0.15% ext4 private file read: 0.03% 1.00% 1.39% 0.34% ext4 shared file write: -0.50% -0.02% -0.14% -0.02% ramdisk private file write: 0.07% 0.02% 0.53% 0.04% ramdisk private file read: 0.01% 1.60% -0.32% -0.02% [1] https://lore.kernel.org/linux-mm/9e62fd9a-bee0-52bf-50a7-498fa17434ee@huawei.com/ [2] https://github.com/antonblanchard/will-it-scale/ Suggested-by: "Huang, Ying" Suggested-by: David Hildenbrand Signed-off-by: ZhangPeng Signed-off-by: Kefeng Wang Reviewed-by: "Huang, Ying" Reviewed-by: David Hildenbrand --- v3->v4: - Update the performance data and commit message - Check PTE without lock firstly per Huang, Ying - Update comments for recheck function per David Hildenbrand - Simply return 0 to make it easier to read per David Hildenbrand - Check !FAULT_FLAG_ORIG_PTE_VALID instead of pmd_none() v2->v3: - Do this check only if vma is VM_LOCKED per David Hildenbrand - Hold PTL and recheck the PTE - Place the recheck code in a new function filemap_fault_recheck_pte() v1->v2: - Add more test results per Huang, Ying - Add more comments before check PTE per Huang, Ying, David Hildenbrand and Yin Fengwei - Change pte_offset_map_nolock to pte_offset_map as the PTL won't be used RFC->v1: - Add error handling when ptep == NULL per Huang, Ying and Matthew Wilcox - Check the PTE without acquiring PTL in filemap_fault(), suggested by Huang, Ying and Yin Fengwei - Add pmd_none() check before PTE map - Update commit message and add performance test information mm/filemap.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index b4858d89f1b1..31ab455c4537 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3181,6 +3181,48 @@ static struct file *do_async_mmap_readahead(struct vm_fault *vmf, return fpin; } +static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf) +{ + struct vm_area_struct *vma = vmf->vma; + vm_fault_t ret = 0; + pte_t *ptep; + + /* + * We might have COW'ed a pagecache folio and might now have an mlocked + * anon folio mapped. The original pagecache folio is not mlocked and + * might have been evicted. During a read+clear/modify/write update of + * the PTE, such as done in do_numa_page()/change_pte_range(), we + * temporarily clear the PTE under PT lock and might detect it here as + * "none" when not holding the PT lock. + * + * Not rechecking the PTE under PT lock could result in an unexpected + * major fault in an mlock'ed region. Recheck only for this special + * scenario while holding the PT lock, to not degrade non-mlocked + * scenarios. Recheck the PTE without PT lock firstly, thereby reducing + * the number of times we hold PT lock. + */ + if (!(vma->vm_flags & VM_LOCKED)) + return 0; + + if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)) + return 0; + + ptep = pte_offset_map(vmf->pmd, vmf->address); + if (unlikely(!ptep)) + return VM_FAULT_NOPAGE; + + if (unlikely(!pte_none(ptep_get_lockless(ptep)))) { + ret = VM_FAULT_NOPAGE; + } else { + spin_lock(vmf->ptl); + if (unlikely(!pte_none(ptep_get(ptep)))) + ret = VM_FAULT_NOPAGE; + spin_unlock(vmf->ptl); + } + pte_unmap(ptep); + return ret; +} + /** * filemap_fault - read in file data for page fault handling * @vmf: struct vm_fault containing details of the fault @@ -3236,6 +3278,10 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) mapping_locked = true; } } else { + ret = filemap_fault_recheck_pte_none(vmf); + if (unlikely(ret)) + return ret; + /* No page in the page cache at all */ count_vm_event(PGMAJFAULT); count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);