From mboxrd@z Thu Jan 1 00:00:00 1970 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 18186C48BF6 for ; Thu, 29 Feb 2024 06:35:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8220D6B009D; Thu, 29 Feb 2024 01:35:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7AAE56B009E; Thu, 29 Feb 2024 01:35:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 625C96B009F; Thu, 29 Feb 2024 01:35: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 4CD986B009D for ; Thu, 29 Feb 2024 01:35:51 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F39E341217 for ; Thu, 29 Feb 2024 06:35:50 +0000 (UTC) X-FDA: 81843880860.01.3F35C6C Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf15.hostedemail.com (Postfix) with ESMTP id 99C23A000C for ; Thu, 29 Feb 2024 06:35:47 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf15.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709188549; 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:in-reply-to:references:references; bh=yrLSfbMNbAWDDjTgRIokXI+pyoy/I6OTy4rFcOUnmGY=; b=Qs7Safgdd8YfqqPCzGlh1BgT9geHtMRZHZocNybszbrdxULygt3lbwAJxIOAVlZK47oyzh kp4OgG5C2qmPiJA/eI+KnJfXULo4Jy+Y1lRt68DTBucyDi6bivYDODruCH9ux27tIkSAQ7 3D0jt4C6S2+HWZXDx9jG6A0F6EGGHLs= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf15.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709188549; a=rsa-sha256; cv=none; b=jUQuDg1Ge4ZMvV/nviDVhbd2ADp9K6QLGEXgM3RaqVEUO2Tg5omZ0OZJhELEdz9yhdIfuZ rCkdtPeUIQNJPA8YDHOPkOwNjJYNhsdo6CrzwNS40Wz773iNO3reIhzN8GzG2lbaHQIVF8 RL78Xi2q/8/A33NXd794sLQNPnzzIqw= Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4TlhKg659HzNnCY; Thu, 29 Feb 2024 14:35:03 +0800 (CST) Received: from kwepemm600020.china.huawei.com (unknown [7.193.23.147]) by mail.maildlp.com (Postfix) with ESMTPS id 73D41140118; Thu, 29 Feb 2024 14:35:43 +0800 (CST) Received: from [10.174.179.160] (10.174.179.160) 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; Thu, 29 Feb 2024 14:35:42 +0800 Message-ID: <9650670c-abb7-bd0f-a5ff-2499d4963429@huawei.com> Date: Thu, 29 Feb 2024 14:35:41 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH v3] filemap: avoid unnecessary major faults in filemap_fault() Content-Language: en-US To: "Huang, Ying" CC: , , , , , , , , , , References: <20240229060907.836589-1-zhangpeng362@huawei.com> <87il27dbo4.fsf@yhuang6-desk2.ccr.corp.intel.com> From: "zhangpeng (AS)" In-Reply-To: <87il27dbo4.fsf@yhuang6-desk2.ccr.corp.intel.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.160] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemm600020.china.huawei.com (7.193.23.147) X-Rspam-User: X-Stat-Signature: pfn55xjd1s6yh9hzoisjgee3nuhsr186 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 99C23A000C X-HE-Tag: 1709188547-304584 X-HE-Meta: U2FsdGVkX1+KEsJb7CKyBWEuXA2MWnBK7bHroHHlyTD3GsL49fXasuBtctYVsBmI8eMJBCFEOZVaKe4wNHSHkHKXsK5eYGcMpCORwOersm1dE4LdYuVD6XC7plXQSQMBM1L25dZxDrR2daCml9wV4guc5vCzVpjWotwjmIgkRAG4V0ANtYOzE4Mb8WYwGG2kTa9VKFL0IREP9XQiOengQ38oymZi6gpb1GURkrLTxHt3fc4Y/6GHaAhuh4aKZOSScZwOyaennWDm/7m77YrBIPDTAAliTBOlPqTsW5fhogc5eeARgipT5VkDsdojMRmuHsLbEbgmTBQppeaMXl22UCbxUsiGd99N06kgfUqtN3y6zzVc410+WYT9rinez8ibaSnd4cCGwRLfnpej/HeKPc75zSOl9FEOo+iD1ajce0pKT+50v1t2yyGnefJ9PlPQr3TjNpH87MC2fVufRL39Bd3XlhCappMqL1GOl9RoFfIqEE3fwoOdDKWXGyPqGbiX2alCVRxaAylN7FC/FMMUgLG4TKqaNSLHzFQDMYe5w58Lq9+K+HzoDyb0TA/2nvAYah3JaZCmyQNYnTb+OUxlR0GS51Qghs4nfP1zdVBeO2noD3NY3kq1wPLj6Sg2yvBMU4rXdEewAW8nTkOznd6UrJdERDEOFi1lXH06ecfzETl4a5zQopyZVJgNmMQejpSFcghjXXiV2juDz4DNYwxJrgUpt2R5E19cBsMgagzqXUOsN7Vb6bLDckk3jpHiCYjLhyl8zNASuYbom6X1RslK8Zp2cWg5OhNHgFkCEIIzec6++inQQm+0AdcVu9Y2Bl5VQm8+rffp9iRupyMbAgLULuwAs7Uw+DKZKRBKuNEpMK4jNRbL9EDRBpv+PiX3LrJ48ZAAp+3fbKOW/pQwftDl41yjABA5sq+PNxjF/7NN/CwzrwCat8mUWPMKrI4MTz6cSR5kshl2jUuZINmL4C/ K7WANeij AosaUspQMMzsEHcexdb+k+Ch9OBQ+OiseenOKS7DcnwbaEu1qbrUbyk9ADb/4LZXdrgIj01xetarUD1lXSaeyOzNC+ajfVnWZjKs931+6r7HdKU0ay/Efy8NeRUSmJ/gRugy5hZJedGQW7KR401g1tx+u9poPHzrCtDCDIkQgN2Mf1F0WMYDrtYp+ErRvP9nYouXLOzkal+lPBi1w6l1lv5j04215SKe4iVzch93k2CC3OS5e/wW42ChqeikaZ//W0O8asXaWNHgBous+K4hJNvp92jHzXsTlvTBSzq1mkdXHZxR3wLS9LatuSTk3oALKse81vQ64k0/wsJAZ5mKdGiayemQwCawvJncOrmX7m0NKzWM= 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: On 2024/2/29 14:31, Huang, Ying wrote: > Peng Zhang writes: > >> 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. In >> our service test environment, the baseline is 7 major faults / 12 hours. >> After the patch is applied, no major fault will be triggered. >> >> Testing file anonymous page read and write page fault performance in >> ext4, tmpfs 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 indicates some performance regressions. >> 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.51% 0.08% -0.03% -0.04% >> ext4 shared file write: 0.135% -0.531% 2.883% -0.772% >> ramdisk private file write: -0.48% 0.23% -1.08% 0.27% >> ramdisk private file read: 0.07% -6.90% -5.85% -0.70% > Have you retested with the VM_LOCKED optimization? Why are there still > performance regression? Sorry I will update the performance data in the next version. >> tmpfs private file write: -0.344% -0.110% 0.200% 0.145% >> tmpfs shared file write: 0.958% 0.101% 2.781% -0.337% >> tmpfs private file read: -0.16% 0.00% -0.12% 0.41% >> >> [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 >> --- >> 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 | 40 ++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 40 insertions(+) >> >> diff --git a/mm/filemap.c b/mm/filemap.c >> index b4858d89f1b1..2668bac68df7 100644 >> --- a/mm/filemap.c >> +++ b/mm/filemap.c >> @@ -3181,6 +3181,42 @@ static struct file *do_async_mmap_readahead(struct vm_fault *vmf, >> return fpin; >> } >> >> +/* >> + * filemap_fault_recheck_pte - hold PTL and recheck whether pte is none. >> + * @vmf - the vm_fault for this fault. >> + * >> + * Recheck PTE as the PTE can be cleared temporarily during a read+clear/modify >> + * /write update of the PTE, eg, do_numa_page()/change_pte_range(). This will >> + * trigger an unexpected major fault, even if we use mlockall(), which may >> + * increase IO and thus cause other unexpected behavior. >> + * >> + * Return VM_FAULT_NOPAGE if the PTE is not none or pte_offset_map_lock() >> + * fails. In other cases, 0 is returned. >> + */ >> +static vm_fault_t filemap_fault_recheck_pte(struct vm_fault *vmf) >> +{ >> + struct vm_area_struct *vma = vmf->vma; >> + vm_fault_t ret = 0; >> + pte_t *ptep; >> + >> + if (!(vma->vm_flags & VM_LOCKED)) >> + return ret; >> + >> + if (pmd_none(*vmf->pmd)) >> + return ret; >> + > How about check PTE without lock firstly? I guess that this can improve > performance in common case (no race). > >> + ptep = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, >> + &vmf->ptl); >> + if (unlikely(!ptep)) >> + return VM_FAULT_NOPAGE; >> + >> + if (unlikely(!pte_none(ptep_get(ptep)))) >> + ret = VM_FAULT_NOPAGE; >> + >> + pte_unmap_unlock(ptep, vmf->ptl); >> + return ret; >> +} >> + >> /** >> * filemap_fault - read in file data for page fault handling >> * @vmf: struct vm_fault containing details of the fault >> @@ -3236,6 +3272,10 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) >> mapping_locked = true; >> } >> } else { >> + ret = filemap_fault_recheck_pte(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); > -- > Best Regards, > Huang, Ying -- Best Regards, Peng