From patchwork Fri Nov 24 02:31:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13466893 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 6D6EEC61D97 for ; Fri, 24 Nov 2023 02:31:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7DF06B06E7; Thu, 23 Nov 2023 21:31:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C04466B06E8; Thu, 23 Nov 2023 21:31:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF2DA6B06E9; Thu, 23 Nov 2023 21:31:26 -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 999CA6B06E7 for ; Thu, 23 Nov 2023 21:31:26 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6AEBF1CC2F7 for ; Fri, 24 Nov 2023 02:31:26 +0000 (UTC) X-FDA: 81491271372.25.0808E55 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf13.hostedemail.com (Postfix) with ESMTP id 3E1F32001E for ; Fri, 24 Nov 2023 02:31:22 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.187 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=1700793084; 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=dimB3vvJ/JxnqYKq3uzB2o/mHIYE4NAdAET5RAi1l+Q=; b=ACV8WbeaDdsVoVDAOFoaDenAQJD5iEmRhaGKRVx5sarvACEEdsBHxC+AHgAcTmBfoEUmjo Q3R4fam8srsW42a3nROmjTuT2ZW63Z89NB1G3wktHUsf7f2JFA1czIyDttdlO1K/K9rUxh t6eu2hU2H+lEBDvSgk3Xw1q1DhFiZs4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700793084; a=rsa-sha256; cv=none; b=KjbEUvVSD/BXIZpjgZizqObNMhnOGillHwGx2kJrRCIBV9zlEvMzShI029wpIVSxknD8Bx LvIxntnxrLG+x8BcDzBA8tIpAG9Lgu6dHuLCn7dR6sbB+znW1tNwHC/OkUvYTd3leqfBQb geHxs8RQawuMLNKpbUUllQtdD+vJHEU= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of zhangpeng362@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=zhangpeng362@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from kwepemm000020.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4SbzR40YJ6zsRPs; Fri, 24 Nov 2023 10:27:44 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by kwepemm000020.china.huawei.com (7.193.23.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Fri, 24 Nov 2023 10:31:14 +0800 From: Peng Zhang To: , CC: , , , , , , , , , , ZhangPeng Subject: [PATCH] mm: filemap: avoid unnecessary major faults in filemap_fault() Date: Fri, 24 Nov 2023 10:31:07 +0800 Message-ID: <20231124023107.571059-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: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm000020.china.huawei.com (7.193.23.93) X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 3E1F32001E X-Rspam-User: X-Stat-Signature: higuo6srq45kwc1iofpuogby7myk4xs6 X-Rspamd-Server: rspam03 X-HE-Tag: 1700793082-556546 X-HE-Meta: U2FsdGVkX1+QrnQsrqUVHr921MRfZgdsR3ZjcBm2PENIESosk3RNPnFzy/qavYK//1dIssiTJlfcqHT10i9bNPNHfItBk5j7aUtD9/+RHe1ZmIC8fNA+nB5AUtPK6PV537eI3uC5Jin2AOdPx5nup1vPA/EcLEFEwhUe4fjJvnUfTFnp8b66yWuBKI7ztaHFRnwc8B7D708Irj7XfTgAP0dTHQzqDSzJIbehXvqS5hnfOPwbnDqnbkOrglXNhsysy4+qNVbEkulbpxSgjFVl6/w8Umx0Jm4wiEdQGNEe/tOmaSeF+3rz39/SPjTSNXT+j9noCcA4VGl3Wxdv5pj3T5jYCUrJW+/amKwB0BBeM73Y7fcuNjIcPOFNKpN5ZMC3D+WueqJRbiYJ9juaxV6600SFEb5Ekx1rFtBHtdYuZuSFCPuZJ+b+S4oS8DQksf0FNHsLitozBMWqtgSD2rKQNNotlMZy/OoO3RLpLrFuCjd88MC//fdIsLIr4y15Sg2UwtROfZ5pITeJFFKYvrO/dRoNKAQFwSh9Y7PiJFOm9YHU5ZAiRjO4LJl2LbLqmdX501gBHQhaxZ0cPGbVNEV7AgAJUiBMPl/KEsO3AsUv9Q8Dl5v6j6BmP8RR02W8UIf9pjob1R0elb7IdiUAAN1KLRALaGHx5rK0/bf0ppr5aMskYnGc72V2LD0fN7iX9GHFDpwb6Cael+F46XPHSXLF9GOWK/DSrgqcRJwtf8b7N7ZOLzSfCdfL3SRgRJqBu6ktM6nOttyvpdpB0XZiwqUM9hfeB3PirsQerHyE2Z3WMG6qkNuA61NsyzBojtFr7o0oOYMh11cMc1gEjZrbfr+v3cWA7U35lGITY4I6mfGp9h6xCqXt0ybuqLWDVtIv+1ER86u/5U7EI9MW7S+UBVRrOWMjg5pCMhI0rb/YElEg4JJwltLxAFQPodlzSSnYFbC24QA4GnZxpwZyeHJPxUq Yz8uvRtH VSHQTrDrca1oHgMRDuIqmrs3AgrGOcNXlkhOj+I/xxb1YGMauacn0KlXMKeMouo5pXHVvVo2vVvNo1a3Coiux2xV6cjqZ+5SR7BtYfzbxnINzcAihnKXKYILZiBKPda3ffB/teqzs9F28nNa4lVsPBp22EcBp69+Aw/g7SXiUHnjEFHWIt9ngKdrnmZ60uGqtvn9AQsPT6bII0VCJC0cNpu+IbSg1Fey2z5m4fcZVZf0dzizOf9nVfTQ8mxSVTsyEYO4ZCXy2VHCo0WTgNVO6ol3If+Dr/Q/F2Q9PZLz7ufyHsNs= 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 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 by holding ptl in filemap_fault() before triggering a major fault. We tested the performance of file private mapping page fault (page_fault2.c of will-it-scale [2]) and file shared mapping page fault (page_fault3.c of will-it-scale). The difference in performance (in operations per second) before and after patch applied is about 0.7% on a x86 physical machine. [1] https://lore.kernel.org/linux-mm/9e62fd9a-bee0-52bf-50a7-498fa17434ee@huawei.com/ [2] https://github.com/antonblanchard/will-it-scale/tree/master Suggested-by: "Huang, Ying" Signed-off-by: ZhangPeng Signed-off-by: Kefeng Wang Reviewed-by: Yin Fengwei --- RFC->v1: - Update commit message and add RB from Yin Fengwei - Add error handling when ptep == NULL per Huang, Ying and Matthew Wilcox mm/filemap.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index 71f00539ac00..f3dcabdbc810 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3226,6 +3226,20 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) mapping_locked = true; } } else { + pte_t *ptep = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, + vmf->address, &vmf->ptl); + if (unlikely(!ptep)) + return VM_FAULT_NOPAGE; + /* + * Recheck pte with ptl locked as the pte can be cleared + * temporarily during a read/modify/write update. + */ + if (unlikely(!pte_none(ptep_get(ptep)))) + ret = VM_FAULT_NOPAGE; + pte_unmap_unlock(ptep, vmf->ptl); + 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);