From patchwork Tue Aug 31 13:21:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 12467213 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,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 1CAD5C432BE for ; Tue, 31 Aug 2021 13:21:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7F9BB61056 for ; Tue, 31 Aug 2021 13:21:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7F9BB61056 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 1BD396B0072; Tue, 31 Aug 2021 09:21:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 146AE6B0073; Tue, 31 Aug 2021 09:21:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F2A156B0074; Tue, 31 Aug 2021 09:21:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0201.hostedemail.com [216.40.44.201]) by kanga.kvack.org (Postfix) with ESMTP id E2E3E6B0072 for ; Tue, 31 Aug 2021 09:21:27 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 8DF681812C29A for ; Tue, 31 Aug 2021 13:21:27 +0000 (UTC) X-FDA: 78535437414.03.2B632F9 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf14.hostedemail.com (Postfix) with ESMTP id 424976001982 for ; Tue, 31 Aug 2021 13:21:27 +0000 (UTC) Received: by mail-pf1-f182.google.com with SMTP id t42so14902030pfg.12 for ; Tue, 31 Aug 2021 06:21:27 -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=gBv4DIzKRcOu69g1wqCeY57nVaNCSDRinCugPrU4tQ4=; b=Br7vpBZuYB1KrRcH3aKb7TMzr+uOaRmUFrH3fj/WmfhYU79w3x5hfLOQLdYXzEluyi qG6kfJdrSnRDysJ3mx5rqJSFGrb8GQ9huouLqjh71elt8wDLUoivxP/y6KeNmpfL2ArN Catlao+CgXWLXb0HaiHJcPptCx4UHRytvDGzWi9XVPcsPRk5aIbJBv7VZmwD2Bhz6Dzm UmA1mvHNW8gBrQyN2a2Wb2CVsSguzQGeLd61qiLWlC7Jx8x8afme3bckRf3xz4HMea5t pVrPLSUZOK7V0Tcb8DdKtd6SlS1NKFOUQFzCF9Dq3A5sqns7HDFWN2EYf8kvkV8gF9Gm rItA== 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=gBv4DIzKRcOu69g1wqCeY57nVaNCSDRinCugPrU4tQ4=; b=Z69NxaK+cJjTTdIRZG5gOgO3yyeqhxdp2y095HXWsk8GMxUa4Y8eQX53Nn8W++5VXl JVtJK42WrLqXtKCUrYooTYI0suQ1G/4t3j6NFkAS2K705G9Q6NjKr1krznDtWc8XOJPk jdZcrHXVfezyix1sLhg4wVEjvZ5V4yG0I60uB9UJWqWxpRoyxuom0/I51VU2gtfJtjAt rOHfhi0/Vlix0qRECc46DguF+GyPkel9i04zDOlIhJE9+4jZRPvC/51LL/mngGptMPUb ojHkGy+0kfdj24vojqPG+0CYRcxX9TIaf2lmIc5TcQZwGQJjxUNdy71IjmxBcovihHJt oQKw== X-Gm-Message-State: AOAM5330v8otcO+u0zoWZO+KDmoQfclEt4oaH6F7djAuaaecgegmqb7G 1Aiw19SXJ4aUgMSJZBPbBcB0Sw== X-Google-Smtp-Source: ABdhPJwQBi8vJ/iQd/BGDlY9g6T4MsHzHwWv2+qPpqkNnzGGbU5YKSYjZBBxI+8Rqp+HGFPKuZantQ== X-Received: by 2002:a63:fc1d:: with SMTP id j29mr26405642pgi.54.1630416086289; Tue, 31 Aug 2021 06:21:26 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.230]) by smtp.gmail.com with ESMTPSA id k190sm9548352pgc.11.2021.08.31.06.21.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 31 Aug 2021 06:21:25 -0700 (PDT) From: Qi Zheng To: akpm@linux-foundation.org, tglx@linutronix.de, hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, kirill.shutemov@linux.intel.com, mika.penttila@nextfour.com, david@redhat.com, vbabka@suse.cz Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, songmuchun@bytedance.com, Qi Zheng Subject: [PATCH v2 1/2] mm: introduce pmd_install() helper Date: Tue, 31 Aug 2021 21:21:10 +0800 Message-Id: <20210831132111.85437-2-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210831132111.85437-1-zhengqi.arch@bytedance.com> References: <20210831132111.85437-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=Br7vpBZu; spf=pass (imf14.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 424976001982 X-Stat-Signature: puu51q9syacas96he66bmadfjnww5y7n X-HE-Tag: 1630416087-827006 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 pmd_install() helper. Signed-off-by: Qi Zheng Reviewed-by: David Hildenbrand Reviewed-by: Muchun Song Acked-by: Kirill A. Shutemov --- 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 a3cc83d64564..0af420a7e382 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2463,6 +2463,7 @@ static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud) return ptl; } +extern void pmd_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 c90b6e4984c9..923cbba1bf37 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3209,15 +3209,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)) + pmd_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 39e7a1495c3c..ef7b1762e996 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 pmd_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); + pmd_install(mm, pmd, &new); if (new) pte_free(mm, new); return 0; @@ -4027,17 +4032,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) + pmd_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() */