From patchwork Thu Apr 18 12:06:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13634594 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 83126C4345F for ; Thu, 18 Apr 2024 12:07:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D64F36B007B; Thu, 18 Apr 2024 08:07:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D14D26B0093; Thu, 18 Apr 2024 08:07:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C031C6B0096; Thu, 18 Apr 2024 08:07:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A775C6B007B for ; Thu, 18 Apr 2024 08:07:15 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 48CFE41091 for ; Thu, 18 Apr 2024 12:07:15 +0000 (UTC) X-FDA: 82022527230.01.083CB85 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf18.hostedemail.com (Postfix) with ESMTP id 4ECFE1C000A for ; Thu, 18 Apr 2024 12:07:11 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=wangkefeng.wang@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=1713442033; 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=NjZN5fZ6Zk7zWoafeBE8FhVSUTnyCFMZboxq5z7/3J8=; b=WniDjz2RfIwSHT5Eo1Kzcu0pzLdBgcG5Bu+PChCNQwaawqxET4W9xw+XCv/NWdVM4atBZB oYivMDCQhxiNBY1r2QtxeD0BuL5HzD79QqSRW04b1zS7SBEL9+IvBP24k2DFqd1M6GT4Vf y3A8ZrVIRX7dwf9CAEMjayvuRS/OkyY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713442033; a=rsa-sha256; cv=none; b=tMcfzqcDCvK0BiQAMbQD2zLrMJPAX/+Kwt8wDybMi/TroglJfH7fLB00KTo0HIT8BLo2cR 9SlF7r80goe98mBpumO/M4FkeBN0y4jmHEDM9C/2LaT4ORZ5CreiToY3jil66lFJgEqJra xHcLoo0eledSuQZWt0lkpxeql4mAAo4= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VKxLy56SLzJscy; Thu, 18 Apr 2024 20:06:02 +0800 (CST) Received: from dggpemm100001.china.huawei.com (unknown [7.185.36.93]) by mail.maildlp.com (Postfix) with ESMTPS id 9043F1800C4; Thu, 18 Apr 2024 20:07:04 +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; Thu, 18 Apr 2024 20:07:04 +0800 From: Kefeng Wang To: Andrew Morton CC: Peter Xu , , Kefeng Wang Subject: [PATCH v2] mm: memory: check userfaultfd_wp() in vmf_orig_pte_uffd_wp() Date: Thu, 18 Apr 2024 20:06:41 +0800 Message-ID: <20240418120641.2653165-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: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm100001.china.huawei.com (7.185.36.93) X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 4ECFE1C000A X-Stat-Signature: 6xn34h7nonsxt38d9n1zdr7z1sdaes5o X-HE-Tag: 1713442031-317054 X-HE-Meta: U2FsdGVkX1+7F4GC9nc59lHJR2PLIhUJQZ+/zLeFdi+kUzuWABKb/oh1mxzjwNLjLXb1kvmSRp10oTn0dTfXFLeg2g2E3bRKnWDSvTshAgUlu9t9IbbmZsj/xf0eJO9WzUxwcjDwE03J20vqZ9B21ISHez8bgJJ38r6GD3Vziwedi+U9EVonX087XpSEmlYlsRMk/YVGhbG6pyB7wKZX3E0khM7YWb1/9EyTBd0R7pA90/UzwLaQU6nw7KRYGvuVef6bz3X61do9pLF1ZR9qALL69UmTG9VSDs9cP3/ZlRhoIvZrTrVw0XJBElbKA6wKeJXCjoYZwLf7PP8BQd76UCP5dxu+CWU0pDY3oR1/Izm5Uz0ck5e+SLeadB14yCVon8OcrAOvoQg5eyZTJ3OPlKnuvR89xNhukH2rdMNtn5UjYHw0M7txDiHKlcbXOk0Skg6pXv4/f33n2dzTXoc8ydj/dSIbFE+yQCKxANip3WBzU9XCyGIliQcgVlHfG7HAcUlbv4tRS0uqGyz7tCP/wyFhDnwaOFfzR/0pr9KdBX2FZUMJZDENj0P/XEyVVrq/zA8SqGTca8vdtvZJKlURV3syXh5G1DWe8DETnqoOxLiicDXjBLWk5RxNKbjHovemo5XdfM7FtiwUVfiqCDm7S1EbXfWvmZt6l33y69riDRH3to4vTaaAaI6Ag9O3d09qeQDoAU19Wv32+pckibLeNJAWfUukoh5sF1M6RF510J88vF42L2aQ6U2g6uoz0dgVwg0wWeWc1qulHyVkpMRGB5RldYevANpprzQ3ot5qv7C7L5HYPKuGlpogvAMEED3aAi3prVCN8NTvC+9JoRNq7lKlqSklTX+lEAdQnAXdcT9tnCECeXjfv0TOT4Trdq9mU6fEmSMn/VU= 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: Add userfaultfd_wp() check in vmf_orig_pte_uffd_wp() to avoid the unnecessary pte_marker_entry_uffd_wp() in most pagefault, difference as shows below from perf data of lat_pagefault, note, the function vmf_orig_pte_uffd_wp() is not inlined in the two kernel versions. 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 In addition, directly call vmf_orig_pte_uffd_wp() in do_anonymous_page() and set_pte_range() to save a uffd_wp variable. Signed-off-by: Kefeng Wang --- v2: update changelog mm/memory.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 5ae2409d3cb9..2cf54def3995 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -116,6 +116,8 @@ static bool vmf_orig_pte_uffd_wp(struct vm_fault *vmf) { if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)) return false; + if (!userfaultfd_wp(vmf->vma)) + return false; return pte_marker_uffd_wp(vmf->orig_pte); } @@ -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)) {