From patchwork Mon Apr 22 03:00:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13637597 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 E122DC4345F for ; Mon, 22 Apr 2024 03:01:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E0626B0088; Sun, 21 Apr 2024 23:01:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 090966B0089; Sun, 21 Apr 2024 23:01:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC1A06B008A; Sun, 21 Apr 2024 23:01:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CED716B0088 for ; Sun, 21 Apr 2024 23:01:43 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 20F991207C8 for ; Mon, 22 Apr 2024 03:01:43 +0000 (UTC) X-FDA: 82035667686.02.BC4BBA8 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf10.hostedemail.com (Postfix) with ESMTP id B7DF2C0007 for ; Mon, 22 Apr 2024 03:01:39 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf10.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713754901; 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=JgTJTQIl48XemebTC42IS0xU1uhodolhcdl1YSvwlYA=; b=OBDxHEpQumFBo4ZdwOo/VJL1i/RdGcCMXMg2+SFru5TGDXe4GXjNNLeijwNigDbCFxP2ZC yxF9EBo5W0kX879Wj/t1ANVeApl8fT+GFpI2xkGteCeizzjxJlfuadg5CVgyPFJ4tP1OBF 41R7C1/mfqGsMYsYCvozlHe/o+KbXWE= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf10.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713754901; a=rsa-sha256; cv=none; b=hw39rGaXuzZXXahxH3w1x2LXectTJzmcJQWOFwOdr9EP5i47LcCzurkt86Dmnev9kmfyjL gGppRlA7vsg0ohpEZDzMXdGgzIsXMy8IZUMzJgFgoquQRRegg+61FAnbWetD3xBRcLAsrA ZwtPQ5I44+ZncG29A3PzN72mkHj3MMI= Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4VN92206gKz1yn37; Mon, 22 Apr 2024 10:59:06 +0800 (CST) Received: from dggpemm100001.china.huawei.com (unknown [7.185.36.93]) by mail.maildlp.com (Postfix) with ESMTPS id 7389C1A016C; Mon, 22 Apr 2024 11:01:34 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by dggpemm100001.china.huawei.com (7.185.36.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Mon, 22 Apr 2024 11:01:34 +0800 From: Kefeng Wang To: Andrew Morton CC: Peter Xu , , Kefeng Wang Subject: [PATCH v3] mm: memory: check userfaultfd_wp() in vmf_orig_pte_uffd_wp() Date: Mon, 22 Apr 2024 11:00:39 +0800 Message-ID: <20240422030039.3293568-1-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm100001.china.huawei.com (7.185.36.93) X-Stat-Signature: b9ubbinkrbp6ecfwqbcx6xhu7ttcwncp X-Rspamd-Queue-Id: B7DF2C0007 X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1713754899-720554 X-HE-Meta: U2FsdGVkX19QcXtX6iv4NvTPnSbLuomxI45aG0rsu3ayxXNENKdzJiLlqNw7BuJnGl/TR8FlXApR/FXmJ7MqeoDLVYB0lD8amcRQHnkTDKuJDGLpp+n0iLNCK6tS3AMtR4rq8VmGE+jxfPxoMTyG3SRHH8JpfviLYEWs+NTTh8uV4bEq9lzjvhIYbne9iCNnCiG3akOMfR+3gNDFKcFkocWsz3shOk6UVkfDbncyUc0nWYzG/CmKhUT/MQLjt6Vx5w1HAZedev0oMAE4iFUAROAqn57jMufucuYNuyrUxtAfr+CrNh6Pn2e095zV4R7UL/D5FE2PzrLuRpToBwyAjWf0LOTRdaZKgKjFfrgKuGemPocH13fkAH7w7XFJcWzmMK9fghLSx9DOyrUhBMKXpqp3Yz0E0XFLmzopH9HL98MZxJ2AzOGpgOHxbYQ483mz5/FcMfgr/OJsJtMZSKxEkTil846W1aqBVkw6FGOhoKW6EgIoRj327O1FUXjdOtGjZXsXUtIcvA/d9iuCmHuB6OWJMswKgbLjGNKFRAEllo2zeJi5LRY+lx3z4m/WQXcZCbRkduMaEuHohoh6ffOszmQUDvfK/glVfObWNlWrHX7Eo1VkPDgUJgdbfcKcuhFjTvNe+yFJdJR1zcqL5CknUA87B+uA64K5jLQs7lvEiCjaWFLY53n4OHbTw17vomyQqbR1ivgo7gopGd7yda9nct6TQnvJwVORVHyei2vGDEd5xu2aNy/7p4/BHS1n04CDu6vhOb+JCCXHcqaKMyuaE01J5Qnm//Vj6rbqEU0Z64FQceofJp8KD9gsqOTqShI96Qq2MTWCsZaEVL6xXcUxI+PIxgquwYP8Yq/4OIDM+kMeECvEZEkAnfqR8L6xF7L6ajDAALnaWUvbka770J4HK1stL56gco+53KMS5HUsx6BNhoXQNKqwjnKazu3jBMyd+3pgksPyzdTN5lt1XY/ 20rGEda4 Q/ZrbXjJS9PWmsKAEo6PD5VvQ/bp2ppFnRwxOKG+nZKZ+Xb1y7CpKow4kSG7nzKKCrB0CgZIweCxTL206/wxx/AHKwPZZQq0mP8BQ9WB8liv0GAsyyU8beXhcr9hQPx93fMscvRYEZA1NayTFaGBOGTVNaYv7UNGVKShz6j8tEt97hGQw6t6WJe/Pjko+EI4Yy13o 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: Adding userfaultfd_wp() check in vmf_orig_pte_uffd_wp() to avoid the unnecessary FAULT_FLAG_ORIG_PTE_VALID check/pte_marker_entry_uffd_wp() in most pagefault, note, the function vmf_orig_pte_uffd_wp() is not inlined in the two kernel versions, the difference is shown below, perf date, perf report -i perf.data.before | grep vmf 0.17% 0.13% lat_pagefault [kernel.kallsyms] [k] vmf_orig_pte_uffd_wp.part.0.isra.0 perf report -i perf.data.after | grep vmf lat_pagefault -W 5 -N 5 /tmp/XXX latency before after diff average(8 tests) 0.262675 0.2600375 -0.0026375 Although it's a small, but the uffd_wp is a new feature than previous kernel, when the vma is not registered with UFFD_WP, let's avoid to execute the new logical, also adding __always_inline attribute to vmf_orig_pte_uffd_wp(), which make set_pte_range() only check VM_UFFD_WP flags without the function call. In addition, directly call the vmf_orig_pte_uffd_wp() in do_anonymous_page() and set_pte_range() to save an uffd_wp variable. Signed-off-by: Kefeng Wang Reviewed-by: Peter Xu --- v3: - move userfaultfd_wp() check uppper and add __always_inline to vmf_orig_pte_uffd_wp() - update changelog and add lat_pagefault results mm/memory.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index d4e5cd9bc183..09ed76e5b8c0 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -112,8 +112,10 @@ static bool vmf_pte_changed(struct vm_fault *vmf); * Return true if the original pte was a uffd-wp pte marker (so the pte was * wr-protected). */ -static bool vmf_orig_pte_uffd_wp(struct vm_fault *vmf) +static __always_inline bool vmf_orig_pte_uffd_wp(struct vm_fault *vmf) { + if (!userfaultfd_wp(vmf->vma)) + return false; if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)) return false; @@ -4388,7 +4390,6 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf) */ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) { - bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); struct vm_area_struct *vma = vmf->vma; unsigned long addr = vmf->address; struct folio *folio; @@ -4488,7 +4489,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) folio_add_new_anon_rmap(folio, vma, addr); folio_add_lru_vma(folio, vma); setpte: - if (uffd_wp) + if (vmf_orig_pte_uffd_wp(vmf)) entry = pte_mkuffd_wp(entry); set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr_pages); @@ -4663,7 +4664,6 @@ void set_pte_range(struct vm_fault *vmf, struct folio *folio, struct page *page, unsigned int nr, unsigned long addr) { struct vm_area_struct *vma = vmf->vma; - bool uffd_wp = vmf_orig_pte_uffd_wp(vmf); bool write = vmf->flags & FAULT_FLAG_WRITE; bool prefault = in_range(vmf->address, addr, nr * PAGE_SIZE); pte_t entry; @@ -4678,7 +4678,7 @@ void set_pte_range(struct vm_fault *vmf, struct folio *folio, if (write) entry = maybe_mkwrite(pte_mkdirty(entry), vma); - if (unlikely(uffd_wp)) + if (unlikely(vmf_orig_pte_uffd_wp(vmf))) entry = pte_mkuffd_wp(entry); /* copy-on-write page */ if (write && !(vma->vm_flags & VM_SHARED)) {