From patchwork Sun Jul 18 04:30:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 12384141 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49713C636CA for ; Sun, 18 Jul 2021 04:31:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id ED6B06101A for ; Sun, 18 Jul 2021 04:31:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ED6B06101A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 836F48D00F6; Sun, 18 Jul 2021 00:31:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E79F8D00EC; Sun, 18 Jul 2021 00:31:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 686528D00F6; Sun, 18 Jul 2021 00:31:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0080.hostedemail.com [216.40.44.80]) by kanga.kvack.org (Postfix) with ESMTP id 42D9D8D00EC for ; Sun, 18 Jul 2021 00:31:02 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id D31F48249980 for ; Sun, 18 Jul 2021 04:31:00 +0000 (UTC) X-FDA: 78374433480.39.FAFEBFB Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by imf22.hostedemail.com (Postfix) with ESMTP id 559461918 for ; Sun, 18 Jul 2021 04:31:00 +0000 (UTC) Received: by mail-pj1-f45.google.com with SMTP id g24so9299737pji.4 for ; Sat, 17 Jul 2021 21:31:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TVYHNGkdyKlwK6dCMYmTnE+nHwe/J3b2KXRbX3JogJw=; b=iY1kKz6c3O1gPTZ6AOqOAbaK+8u3kSYhTOHF1miiIia8vJzWFeh/GkV9PMte6SOl+X ppZvpuFVRcR1nNaqgK+VK/CnwkHuycqIGfZEV2dKNJEK0g1NO+XqE6QmTGyxsqKPzrYg 5lkaeyCkCqcEUzfM+jFOftFYb5Q86+hyrPWYYH7oOn/8LmDsC1Ok+PJ4oTZsC3FWKIOY Ds0t2nGwMluEv/2oI8kqwwVsfvIzyBoi2Gc3RG/cM0XtwI16hKZoiu6JKSfddekZJUZS 53lzS/j0MF5kUG6DAgXSlUzkZgO4WWe4Kbusea98bwMIb7j3+xCXq3/2CtHAyIFBGfGP lG2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TVYHNGkdyKlwK6dCMYmTnE+nHwe/J3b2KXRbX3JogJw=; b=hZULKByuAsTruLmB1mfCLH5eV/aLvvSROt7BZhnrN0kbYxC1RjY+KTT3OXGYotAQtj it3QBgSPiVYSJ9Jwk0enupxiMXOuadt2AxcoNwAZNqSa3c4P87UWkFtgdo4pKtcJ074k zKxmbxf0aR8eWhmTK1ye7Hfmn4yKhhROqxwgSBOmRAXt3bU78hJnYJTNyy8QJQ+Ni3s1 wxGob/fjohrSDTSLl5ohxoMljH+jl9RE6EKJ/0MM/UyUUmZ8hqtHeHLiBBovh6i/QVH6 ClzQxfb3VdPUzvX2ASp9xOc95zOGjaAaQM4MdlSTarWG32ABXyaSIN8J7vjGQYuUxhNr gXow== X-Gm-Message-State: AOAM533jbEWvWt22/t7lu8ziYc4Q1dR9AVOQawd0zrPMvBeBQjnrcO2k FJ09hdBpgFJyAvKtEQ9KwbfVRA== X-Google-Smtp-Source: ABdhPJxr3cbtH+NDceI9FJvUiPTYg/qzlCUaJB88Ak0L8TiKUDaCOmdVLqiJMRp7XqnNQ4NiJ7yiGA== X-Received: by 2002:a17:902:9a04:b029:129:7769:2b01 with SMTP id v4-20020a1709029a04b029012977692b01mr14041091plp.76.1626582659427; Sat, 17 Jul 2021 21:30:59 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.253]) by smtp.gmail.com with ESMTPSA id a22sm16263217pgv.84.2021.07.17.21.30.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Jul 2021 21:30:59 -0700 (PDT) From: Qi Zheng To: akpm@linux-foundation.org, tglx@linutronix.de, hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, songmuchun@bytedance.com, Qi Zheng Subject: [PATCH 2/7] mm: introduce pte_install() helper Date: Sun, 18 Jul 2021 12:30:28 +0800 Message-Id: <20210718043034.76431-3-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210718043034.76431-1-zhengqi.arch@bytedance.com> References: <20210718043034.76431-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 559461918 X-Stat-Signature: rnpbmarne8opmgs3o35fakdwmz3kobd5 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=iY1kKz6c; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf22.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.45 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com X-HE-Tag: 1626582660-417059 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: Currently we have three times the same few lines repeated in the code. Deduplicate them by newly introduced pte_install() helper. Signed-off-by: Qi Zheng --- include/linux/mm.h | 1 + mm/filemap.c | 11 ++--------- mm/memory.c | 34 ++++++++++++++++------------------ 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 57453dba41b9..5efd63a20d72 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2374,6 +2374,7 @@ static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud) return ptl; } +extern void pte_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte); extern void __init pagecache_init(void); extern void __init free_area_init_memoryless_node(int nid); extern void free_initmem(void); diff --git a/mm/filemap.c b/mm/filemap.c index 7b365fdef7a5..4955641f2cf0 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3173,15 +3173,8 @@ static bool filemap_map_pmd(struct vm_fault *vmf, struct page *page) } } - if (pmd_none(*vmf->pmd)) { - vmf->ptl = pmd_lock(mm, vmf->pmd); - if (likely(pmd_none(*vmf->pmd))) { - mm_inc_nr_ptes(mm); - pmd_populate(mm, vmf->pmd, vmf->prealloc_pte); - vmf->prealloc_pte = NULL; - } - spin_unlock(vmf->ptl); - } + if (pmd_none(*vmf->pmd)) + pte_install(mm, vmf->pmd, &vmf->prealloc_pte); /* See comment in handle_pte_fault() */ if (pmd_devmap_trans_unstable(vmf->pmd)) { diff --git a/mm/memory.c b/mm/memory.c index 94a17a9a48ab..a0ac4b33e07a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -433,9 +433,20 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, } } +void pte_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte) +{ + spinlock_t *ptl = pmd_lock(mm, pmd); + + if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ + mm_inc_nr_ptes(mm); + pmd_populate(mm, pmd, *pte); + *pte = NULL; + } + spin_unlock(ptl); +} + int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) { - spinlock_t *ptl; pgtable_t new = pte_alloc_one(mm); if (!new) return -ENOMEM; @@ -455,13 +466,7 @@ int __pte_alloc(struct mm_struct *mm, pmd_t *pmd) */ smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */ - ptl = pmd_lock(mm, pmd); - if (likely(pmd_none(*pmd))) { /* Has another populated it ? */ - mm_inc_nr_ptes(mm); - pmd_populate(mm, pmd, new); - new = NULL; - } - spin_unlock(ptl); + pte_install(mm, pmd, &new); if (new) pte_free(mm, new); return 0; @@ -4026,17 +4031,10 @@ vm_fault_t finish_fault(struct vm_fault *vmf) return ret; } - if (vmf->prealloc_pte) { - vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); - if (likely(pmd_none(*vmf->pmd))) { - mm_inc_nr_ptes(vma->vm_mm); - pmd_populate(vma->vm_mm, vmf->pmd, vmf->prealloc_pte); - vmf->prealloc_pte = NULL; - } - spin_unlock(vmf->ptl); - } else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) { + if (vmf->prealloc_pte) + pte_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); + else if (unlikely(pte_alloc(vma->vm_mm, vmf->pmd))) return VM_FAULT_OOM; - } } /* See comment in handle_pte_fault() */