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 6750EC54E41 for ; Wed, 6 Mar 2024 08:49:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0D7C6B007E; Wed, 6 Mar 2024 03:49:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BBDCC6B0080; Wed, 6 Mar 2024 03:49:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A853F6B0081; Wed, 6 Mar 2024 03:49:47 -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 99E956B007E for ; Wed, 6 Mar 2024 03:49:47 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6A6D0A1138 for ; Wed, 6 Mar 2024 08:49:47 +0000 (UTC) X-FDA: 81865991214.19.BDA8785 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by imf03.hostedemail.com (Postfix) with ESMTP id C844E20004 for ; Wed, 6 Mar 2024 08:49:44 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=aHiNEVvk; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf03.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.7 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709714985; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gZY8ZoFZudDfjnzjfNGfaEcF2fqKi7UkW7hitzTHVDo=; b=PaBgox9VHYMDNWbeTv6Z7GMsGDcP4s0Macc3kywA4U0nV9uQQaG1zF7r6PWrUN4Hx61AmM 6H58gVO1t7EnUMROxRoSKNi0Of6Wvjs3vqql6rpDc/+AscGHkZilzRVjuaR6zmmGaIkL8J qPFFA7g34GyhCuMhlnxlXwB3LT40AEo= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=aHiNEVvk; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf03.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.7 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709714985; a=rsa-sha256; cv=none; b=aMyw1oD76F5rrlevNutAzIPA//9Nyd8faRzOPM9uwQWApPSlxXVGmojT1jHXaW3ZcJW/JB V9KbV5LMgm9uS0XVBfgcIQ4VY4AEvmJ/ixqHBIen2042Pr/Mk+CWcIj8rJdXtkT+4SG5gt zp/fEQP4PZlKPyfn6hpJq3Gfxd7yve8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709714985; x=1741250985; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=yYuU7VBni22INYGto9gt3oHjxTlQN5HG/8o/ajfAzJk=; b=aHiNEVvkn7WAE5EWeTtmey3joh7R9OOLc9K5XPBlf6G5aWK+akeqiv29 LlwVfp1pBJ0W+1RRHr5OfqFR4MtNnCz61zueOWzScM+wzBzlAqmTSVID/ ayFcbKxW1EJPIhVks8MYfGWuwRR0z+MSE7IGO3m1KX8A1R6gGLm/yPOd4 BDsFfEgtP1nyqwevfNxsf2YVMUI4BU0eE5N2mb4nXmkl9EYQmhFHpLA8N aRhP3N8mNQ37vjPSVNjFnA0m5kSOf6QH6vqpvbGzGAjh81jzigP+sg9o7 ad5iGnALO0ZK8N7RR4qoewUbmllB+Jrk/gvPRe+1nStNTM/sZirBjATUI Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11004"; a="29765370" X-IronPort-AV: E=Sophos;i="6.06,207,1705392000"; d="scan'208";a="29765370" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2024 00:49:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,207,1705392000"; d="scan'208";a="9628784" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by fmviesa010-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2024 00:49:40 -0800 From: "Huang, Ying" To: Peng Zhang Cc: , , , , , , , , , , Subject: Re: [PATCH v4] filemap: avoid unnecessary major faults in filemap_fault() In-Reply-To: <20240306083809.1236634-1-zhangpeng362@huawei.com> (Peng Zhang's message of "Wed, 6 Mar 2024 16:38:09 +0800") References: <20240306083809.1236634-1-zhangpeng362@huawei.com> Date: Wed, 06 Mar 2024 16:47:45 +0800 Message-ID: <871q8n926m.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: C844E20004 X-Stat-Signature: 75kc9cfc9nt7msomsobcpunnhhrxxzkn X-Rspam-User: X-HE-Tag: 1709714984-877619 X-HE-Meta: U2FsdGVkX18CTUkVtdGcFmGwn6ubM02KfO/0ChwHE6284cygn2MIBJbn+0avxO+AxE4DpzhyVEJyzw8im5u2d9VTfrjlcn+NLywHWvvnRB+FFgZLtpWiXpFz7MQj8l9f29Q5jMwHe/g4qIDPKopLKyvJUuRB1+joWXMcjQI9U5nWJwbYGrOTvISnAv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yaDOH3RbkIG3kRg56ZaIBN17e/4FXOoMgc87yQnsXWSDstBw/TqxFRgJnb8g6QvBo3dK3VVQZnchY2X0x97jcRNyfPqUKBtaTvxaxyODdA== 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: 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 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 LGTM, Thanks! Feel free to add Reviewed-by: "Huang, Ying" > --- > 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);