Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp211318pxb; Wed, 18 Aug 2021 20:21:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyO5CIEPC5nBZqWvsJ8UFa5M0OA0MOrks55DUht4CujI6XmliQFyH+3R+0uO3A25nGQ7rEG X-Received: by 2002:aa7:c40a:: with SMTP id j10mr13586037edq.133.1629343310339; Wed, 18 Aug 2021 20:21:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629343310; cv=none; d=google.com; s=arc-20160816; b=sh6Y2hqSzAQer8IvS4TUkvKd8iJ5NPBi03Uf6N2VjPNEEZetdKTJ7xd9/7cjROLI21 mExS3AIHLCNmoW94TMf9QT9wIjYQA5IhllgEwlgWaZwT7d9I4HDu6dDgfWYOTxOriQf5 5IXrwLyQr7AIwkGP5wSeUPvqV8oFGa1AYjmIIL6YDNYmF+0OuIjDqoJn+1hhGeFA6gGx 0UztBtTZ08Yhr12kfO8nDRGb5IBPnGAPptWbkEXtYBRrm/0BRblZYMvmgnxru2yWkkce LID6UZRJxIUe6X7ksfSM+88LeiR8BXd9ohEX4XE0V95luHSsfcgsIuxklx8jGvTf/cC6 zXTQ== 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=TZKZTZOtvlnbM7sr1/sGaSyg3CoVrbhapZj97svXJc4=; b=g0tUOtAdBGxTewf4WOokdVyFhqUy+OqOJy/WbKIpeQe4nss3BSSzfHdvu9DU4/fL59 oQSPxBs1AhFXtN6V35kNUyWefLABAfuUU2hAmgwZp4UZes2DlaeECXe5lvV+2j4p8N3U KV8g518Wj3yQF6U7jKAoIp2w7eg+r5n+/4y81r1kN1YHsj90H1G8qwrvM4WW59eMe4uS MUZvgmTArHilGRGWbi17E49W2FfdxmEeHTAhK/Am0+I2TImq29XB0pAmKohmFmNPCFJ5 JRtDW0JTNpAJ5qce5umIiZIo8ydQH4X2xvxGtGKFk4GbHs9FFZ/Gc5iH2GZ8itYS9ugJ mCUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=lM2aQf09; 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 n10si2299006ejk.210.2021.08.18.20.21.26; Wed, 18 Aug 2021 20:21:50 -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=lM2aQf09; 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 S235999AbhHSDUg (ORCPT + 99 others); Wed, 18 Aug 2021 23:20:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236027AbhHSDUe (ORCPT ); Wed, 18 Aug 2021 23:20:34 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02FD1C0613D9 for ; Wed, 18 Aug 2021 20:19:59 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id d17so3066875plr.12 for ; Wed, 18 Aug 2021 20:19:58 -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=TZKZTZOtvlnbM7sr1/sGaSyg3CoVrbhapZj97svXJc4=; b=lM2aQf09sZst/3IUuwtoZ4ifaTLps8ik+qJ445iHcaPnQd7m0zuI2P8FkdK1NxGpvz kTTXotWV3FAoaqE+5dC65iH+O9cEkw3uWcYq+CcQ0ozGE8uT7+2j4j5DeIc67FooHXMo GQy9rI8uBPfjjdcpye6luwdS439+JZsEfoghxPK/MLyyof0R1IzUxJG5VSiVZAUq8FiZ 2PiV5muPG5cidLnOoqyyF5j4k/QN+y2yDF7b1jEbg/G6OkLAr0uM2hK0eJa3VU0/W7kT 60RAMPlS9BoP6/lMw6fy/475/LdHvWZVT25l0YIGIcK9ZOC/MGeLaPe1EfabLlBcU81G 3EDg== 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=TZKZTZOtvlnbM7sr1/sGaSyg3CoVrbhapZj97svXJc4=; b=gJk0BD0MUj2d9oGmQimpw96yPxqdxu8a+OuU3WbIKGy/y114Pkw/0hZfXKhL0ASeit sMR8TYLj2chvF1oecTmMbnnbUFbXjaZcpm3re5jMsB7o6wGorLp4QcvsST19IJtV9kV5 kCVM801HYypd6l2vILSKNOupTwau6dj36HVsqZa0LQu7AKUdFx5ZDlQn4qgofMlKd8Bb 81jS0AJS3lOAw53HNGRQEqaXt9NIRez3fSOkimbxgMfbOclbhktQpCGUBnl8xy797OQm J8I2cVnemvzka/8FXbIFJeeherw41VXdQYAoe3AUXmH4hEMZJ69ybvdzys+FO4BHOzhh UqjQ== X-Gm-Message-State: AOAM530GufK55QzMMVi6B+kUUNmoNnRjmWGRe1V0qoXkrvEHG39eFJ/V 0bnrB3xvZ4vXsya5r//OI3oO6g== X-Received: by 2002:a17:90b:8cd:: with SMTP id ds13mr3304740pjb.70.1629343198552; Wed, 18 Aug 2021 20:19:58 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id k3sm1261276pfc.16.2021.08.18.20.19.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Aug 2021 20:19:58 -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 v2 1/9] mm: introduce pmd_install() helper Date: Thu, 19 Aug 2021 11:18:50 +0800 Message-Id: <20210819031858.98043-2-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210819031858.98043-1-zhengqi.arch@bytedance.com> References: <20210819031858.98043-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 --- 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 ce8fc0fd6d6e..57e48217bd71 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2471,6 +2471,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 53913fced7ae..9f773059c6dc 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3210,15 +3210,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