Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1884195pxb; Fri, 27 Aug 2021 21:24:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw62AE2aWhMsvvwBrSIOTgucHzTFMClhSy+aUus0rth1AWeEaVYW6fwzSJ/0/bGaUn4KISp X-Received: by 2002:a92:c601:: with SMTP id p1mr8653677ilm.284.1630124661450; Fri, 27 Aug 2021 21:24:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630124661; cv=none; d=google.com; s=arc-20160816; b=LKHgYOZGNXyJd1jf2RkDtVzcvb8NxHZCHVEJyZtvKM9kQqjfm2cBffRsCw186BJ983 KA0aMYab65Har8npLZXdB4sLZW/Z8eK5uvz99GsFg0FznZ10cErcPxF9X1GxmXHC90k+ QtQN8ICQb4XvbDhvh1UDBUZjt2p79cgCS7BvINpOFVjy1tAajm1L7L8dDEy900hoFd1y Y8jROjg21HkChcXZwcABizbzAJTS9PODxG7P/ecpn/5OQZv0gLnnul9UZVOLRwtdlYqt tsHE7iRZyEt8Q0bqASqxhCf87vSfWwvC3j/Kk5R0capAjCfPkahjdqJarTmUCnt30mGS EUBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DOQhtRJ283WN5i7pZFdCAMLJddI7+kc0mDNTD4e/NnI=; b=kJ4Q2IHQEysXmp0KKM4hSaJwtn5I6XvA5XbeCrmqG1+73aRdyynqMj1bhqnc2nEtYx Nq8hIeVTKq97QejW5sQAA9ejwhQuMNX0NinYIidtTob9rI0JRkJ4M5/sYudOjbYc4nqP ZYN25dBgczCyyOnd4kZnA8Fz0kNbrIsBSmILJz5/tA+BF3KIQTQr3YIUDp3J9NzNklq6 NbJGl6U+1n0EXOmzKewuh1YIyWPJ2OPsadXimiR/XDiXf+BD38aBMZz7KuQ3RqaqO/cD ubWH5W++BvTSZT1j7ulbxdO1EoZD82mE9X0CuG7+jiGRhL19i5BhZO0CZCHAqf0M1Q5q CtGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=uOhcmas8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d8si8035187ilr.109.2021.08.27.21.24.10; Fri, 27 Aug 2021 21:24:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=uOhcmas8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230502AbhH1EYN (ORCPT + 99 others); Sat, 28 Aug 2021 00:24:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229459AbhH1EYM (ORCPT ); Sat, 28 Aug 2021 00:24:12 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 572DEC0613D9 for ; Fri, 27 Aug 2021 21:23:22 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id j2so5312226pll.1 for ; Fri, 27 Aug 2021 21:23:22 -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=DOQhtRJ283WN5i7pZFdCAMLJddI7+kc0mDNTD4e/NnI=; b=uOhcmas8K2ET6mieFgVELlVqw9VnQ8PHqGHiuSLRHQcRot4aWXhghVdYKMCrq7CnfY +GNSvXNDB5spvFzbTwdnCK6jnS55XNMCN7po5Efk4/vYhebjKdNOZSI+8KGeFiU/bOCc gIiqftF6P3pS6ivslF/iekYGgCH6UDNpbjCFkZ7ocBL7eYOVZBdb4wvbDOkVSO+LFAvt Z/mZGT1ll2qoKlmysbwqBZuPoRBgJoWnORkNwuJyIKc6uuhbL0y20QXDR55pyFsbAm4k 617MUknLTK/z+QKvW5TJq6JJl+KtfZ7APSTO0r+WelWrfRqDXDgBrp64eJeKzcqlWLqH n4+Q== 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=DOQhtRJ283WN5i7pZFdCAMLJddI7+kc0mDNTD4e/NnI=; b=cOUs0Ne61kZj2LYkkz1tb1gZllv8tP/UUTvHtww8Qodi3e8oNqF1GZU7EUb3SEb6Dn 4d5clp02kC5npxHsmOQw+89fRQ6zDjra8vykzXLhkK4zR4Eq4nhZZPseavoEJ7FRflJU 4hBsnBd8v/yW0UN4uoO4D1P5kraYO80xZsSHGHB9ld2C8cNksDrlfE46bT1SDeCS2g7V IMRhtqg3aluYXn/txn7Y/uEsCiR53yDaAeIp179Fj28vocD0CNgrFORYPMLfonZWBRxG Cmm1Smu9L5QCw99/BUxsdxga5Xe5AYNW6++nVDLd+fLH9cA1s1ATWQ4DGMrvI3HbZlEN OPLw== X-Gm-Message-State: AOAM530GcxkMPYlSE7ORyQpV6GdAmJJc8XSOV8NRVePON1SAdOYxKthm LwoUXTaSxZc3oKNa7N5hnYhd5Q== X-Received: by 2002:a17:90a:8b12:: with SMTP id y18mr27121724pjn.72.1630124601867; Fri, 27 Aug 2021 21:23:21 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id u15sm4720383pfl.14.2021.08.27.21.23.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Aug 2021 21:23:21 -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 Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, songmuchun@bytedance.com, Qi Zheng Subject: [PATCH v1 1/2] mm: introduce pmd_install() helper Date: Sat, 28 Aug 2021 12:23:05 +0800 Message-Id: <20210828042306.42886-2-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210828042306.42886-1-zhengqi.arch@bytedance.com> References: <20210828042306.42886-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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() */ -- 2.11.0