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 9770FC48291 for ; Mon, 5 Feb 2024 06:54:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 251686B0071; Mon, 5 Feb 2024 01:54:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1DA486B0072; Mon, 5 Feb 2024 01:54:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 053666B0074; Mon, 5 Feb 2024 01:54:43 -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 E32616B0071 for ; Mon, 5 Feb 2024 01:54:43 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B291EA081B for ; Mon, 5 Feb 2024 06:54:43 +0000 (UTC) X-FDA: 81756837246.09.AAF2E62 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) by imf09.hostedemail.com (Postfix) with ESMTP id 78E7B140019 for ; Mon, 5 Feb 2024 06:54:40 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=aXapfaH3; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf09.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.13 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707116081; a=rsa-sha256; cv=none; b=d5FAfajVFSL1mkZoA1vK9mtQvzT9Xdl1CRJ9M4yJazHRuY/z4GZLpEo+7QGciK2qNsn1zy G+cvp+QJhaqVaFuOU5TzVCvC7X5GRDS67N5PU2VqFbm0rspCyfpn3t2f96k1ydFxtCGX4j CW6x5KJ0bYkKMkSnWUgy1DaRA2jQ8J0= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=aXapfaH3; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf09.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.13 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=1707116081; 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=lLT+aITrJlS157u/a5/qJAfhvi/QGjYK7tYS/50ZX6M=; b=ImLqk0zu1invegZMFBaI4TQPuYR8qZM+q+qxGeBu8/F1da1UEP/RlRuoUnK2ZWFCymCagi 8tK32TrTfzD15WTtwopeaDhjdGx3uOid5Ru5arHw/RfZaratFTD0r9CkaIk0mcVCDvTjJs Rv7sO2YpQLkSJMAPMgA6PzNps2XZhm8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707116081; x=1738652081; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=9RL5KR1hpJcQae+rJ4QHCvOl5EYuXsE3JTpP7Zi4wtY=; b=aXapfaH3NWzdaK9+Dobevh1K0UQLH0KR4SFWBmY0qt+350LbZQOj1MzG uRzT8vXOh96XguPlKPZ0h0ZBPcJEBpZkgiPWUizzdZ3vdYO7YTbmNz/H2 6AsBT8KTCUERUmA4YdgNB7E92Zpsty6bPuKSbHPmRyDQvi1oXwjWPF/rl yKGmYJbMn+J8lgAwN/t3S/AjFkmwjq1itgDBRsOpCbfYP2aQoYWny3ZFO B0rYPBOxreCoOmrCi7sYs0rOO0JA+YGcYrz2DuSZGsif7Gn5UgGezxDpM KfKGohq/SxsCSnpoYJtukzFz1VCCRl9R62g0W0LTKlUUKoe9EBgtTTiM7 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10974"; a="11554002" X-IronPort-AV: E=Sophos;i="6.05,242,1701158400"; d="scan'208";a="11554002" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Feb 2024 22:54:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,242,1701158400"; d="scan'208";a="930460" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Feb 2024 22:54:37 -0800 From: "Huang, Ying" To: "zhangpeng (AS)" Cc: , , , , , , , , , Subject: Re: [PATCH] filemap: avoid unnecessary major faults in filemap_fault() In-Reply-To: <85e03dd9-8bd7-d516-ebe4-84dd449a9fb2@huawei.com> (zhangpeng's message of "Mon, 5 Feb 2024 14:43:07 +0800") References: <20240204093526.212636-1-zhangpeng362@huawei.com> <87zfwf39ha.fsf@yhuang6-desk2.ccr.corp.intel.com> <85e03dd9-8bd7-d516-ebe4-84dd449a9fb2@huawei.com> Date: Mon, 05 Feb 2024 14:52:40 +0800 Message-ID: <87mssf2yiv.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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 78E7B140019 X-Stat-Signature: unwi7r789eau7eq3e6hind9o7sydsse8 X-HE-Tag: 1707116080-807987 X-HE-Meta: U2FsdGVkX18oOebqbfnIoKiby4cBRoFHKSwy+lOs+YXKExqQAqak9OYtnVTEg+Wqds+iNoIN1Ot+tIX4V70b/+HLRylz4hcr2geddbH19GPPSIX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nlI/5wys5wdawIVVbyOJlWZRQXskFPMdUwgaJdYmmyZ1gSkf/9i4SVxp5X4SoRz7+1M+Xt0+qVuNbZpUwnxiy5ez4OsODAMLto/FboJgnVxRn0mS3AJSpxAy6cyOZH6NBNUnPGe900Mti6XPUo7H4dc8jpYMw4TUidjU55y/2GT+ 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: "zhangpeng (AS)" writes: > On 2024/2/5 10:56, 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 performance issue[1]. >>> >>> This caused by temporarily cleared PTE during a read/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. >>> >>> Fix this by rechecking the PTE without acquiring PTL in filemap_fault() >>> before triggering a major fault. >>> >>> Testing file anonymous 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, and there >>> is no obvious difference. The test results are as follows: >>> processes processes_idle threads threads_idle >>> ext4 file write: -1.14% -0.08% -1.87% 0.13% >>> ext4 file read: 0.03% -0.65% -0.51% -0.08% >>> ramdisk file write: -1.21% -0.21% -1.12% 0.11% >>> ramdisk file read: 0.00% -0.68% -0.33% -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: Yin Fengwei >>> Signed-off-by: ZhangPeng >>> Signed-off-by: Kefeng Wang >>> --- >>> 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 | 18 ++++++++++++++++++ >>> 1 file changed, 18 insertions(+) >>> >>> diff --git a/mm/filemap.c b/mm/filemap.c >>> index 142864338ca4..b29cdeb6a03b 100644 >>> --- a/mm/filemap.c >>> +++ b/mm/filemap.c >>> @@ -3238,6 +3238,24 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) >>> mapping_locked = true; >>> } >>> } else { >>> + if (!pmd_none(*vmf->pmd)) { >>> + pte_t *ptep; >>> + >>> + ptep = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd, >>> + vmf->address, &vmf->ptl); >>> + if (unlikely(!ptep)) >>> + return VM_FAULT_NOPAGE; >>> + /* >>> + * Recheck pte as the pte can be cleared temporarily >>> + * during a read/modify/write update. >>> + */ >> I think that we should add some comments here about the racy checking. > > I'll add comments in a v2 as follows: > /* > * Recheck PTE as the PTE can be cleared temporarily > * during a read/modify/write update of the PTE, eg, > * do_numa_page()/change_pte_range(). This will trigger > * a major fault, even if we use mlockall, which may > * affect performance. > */ Sorry, my previous words aren't clear enough. I mean some comments as follows, We don't hold PTL here, so the check is still racy. But acquiring PTL hurts performance and the race window seems small enough. -- Best Regards, Huang, Ying >>> + if (unlikely(!pte_none(ptep_get_lockless(ptep)))) >>> + ret = VM_FAULT_NOPAGE; >>> + pte_unmap(ptep); >>> + 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);