From patchwork Wed May 9 08:38:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 10388695 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4001C60170 for ; Wed, 9 May 2018 08:40:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 29FAA28E81 for ; Wed, 9 May 2018 08:40:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 28BC528E7E; Wed, 9 May 2018 08:40:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 203E228E81 for ; Wed, 9 May 2018 08:40:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D59966B04B3; Wed, 9 May 2018 04:40:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D32916B04B5; Wed, 9 May 2018 04:40:13 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C208B6B04B6; Wed, 9 May 2018 04:40:13 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f72.google.com (mail-pl0-f72.google.com [209.85.160.72]) by kanga.kvack.org (Postfix) with ESMTP id 811FF6B04B3 for ; Wed, 9 May 2018 04:40:13 -0400 (EDT) Received: by mail-pl0-f72.google.com with SMTP id u7-v6so3390524plq.3 for ; Wed, 09 May 2018 01:40:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=lkLDwTTLm/S/r/BMdAAGq7LSI703e+82DGfv0I25y6Q=; b=rDIXfoMCyvgLr4VacDj8GK9alz2XDMFWIpbIbe5hm0tDNYcaZixxmvkiOpfaBh7tGl miPVdOxxnzVsY3L+RXp7cMPKRNzRqfBQl0bJfEitJtPmgbXjwB/vJ4cURQGsxxYLKeLb CdCQgST1tavI2aECGZSYJADZGR1WRGblOfo0TLpqYtkBUfevVLuHEU+XK2k9gX58krti EZ5DChFYd0ZOf+cJIQ4V7BMe5x9Zv723uqdu6qBiq89ASxI04DQ2nEqXp/NOZtx+TGlb dF/24VIVJdEM9H++bfkafAz9whI4PBNDE/gZPzMuYTnYSKpVBHeNU7dXgd6ajnSPusls NGuA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of ying.huang@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=ying.huang@intel.com X-Gm-Message-State: ALQs6tAy2HjxeQMYcjXWF3DhdimY933cBNb70Ab6Pmri6rVzkNZYr/qz g5HDfQdwgYvdbeOfWTTlDZWHWRHPukcQPqxdAkG+1eHsI79XPZ4CxWvPyLwqdSDJebymxHjILSD UX/EcGPUlDUzb339L16lk9AvyY4QBBDHhbNFkyWl9byMThdltDqhp3M3RZlnE2WGoZQ== X-Received: by 10.98.185.9 with SMTP id z9mr20711570pfe.254.1525855213188; Wed, 09 May 2018 01:40:13 -0700 (PDT) X-Google-Smtp-Source: AB8JxZreAWOfp8kdmT4CINRfb2rLg+KrYTzv/n1UhUgZooth0DPa5jmd+0N6+R2LW6w8woIz2uA/ X-Received: by 10.98.185.9 with SMTP id z9mr20711536pfe.254.1525855212307; Wed, 09 May 2018 01:40:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525855212; cv=none; d=google.com; s=arc-20160816; b=cnUdr1LmzhWzONLXSGUFbFhgQn/L6hws3LH9yn7O45TNutPdXoM951TEM22RMsVTHw hjf+2NBeKbtkfbzrrJYapTcfVHeHdJ4O5B3nDKdj6aDH5LM4w4q+1jw2WJNu/oVbqaVk iYzoyPGzG3RAWkp61+7NPt4OjXN9dbNMYolLKThL8dBjerYir/9s80467gCfC1oTStyw YvDHxWQ5JzLY4q68QkgptfdNDs0aYIZ82yhPPbjW6hLEJkWYHeoH7lbAgB+VxMogGdJG AJFKKzon6NgnQKpO6DR7YScvlCy/P7W8JRn8bQq6zSvoKsaSO9Aa2ZaCCSN4NEyGEHYK EQyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=lkLDwTTLm/S/r/BMdAAGq7LSI703e+82DGfv0I25y6Q=; b=Uqy/hTyl0b8+YT1Fuj0bWto6JrKDdsGRQZ8+uzs9KkxerMOph3r2njJ/qVnpT/HoA8 0XjLxHCMIlKbsIS8cFCYfH6QMd6OaVZoaYsCPaA4qyskHulbNstBJ6TjCdujhgcEcZjl s8OANW+vMjENrodsOuELdOrMltocAmnbO3PnWbAyVy5W63lxsoTRD/giP69F5J6nh8o8 bZY+3hOluc2RV2A2zHlGodsX6OpH+/JlvfRcyRCxuRB50t0UO+ggyMalg7VlqoQu/mRI UZMX/D63veM8TB4IN/LZUD1HERiyFk8GCZCLw84CO/eHsYJOLp8/bMOTbf6x/nP641X+ zwdQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ying.huang@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=ying.huang@intel.com Received: from mga06.intel.com (mga06.intel.com. [134.134.136.31]) by mx.google.com with ESMTPS id z9-v6si21682595plk.94.2018.05.09.01.40.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 May 2018 01:40:12 -0700 (PDT) Received-SPF: pass (google.com: domain of ying.huang@intel.com designates 134.134.136.31 as permitted sender) client-ip=134.134.136.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ying.huang@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=ying.huang@intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 May 2018 01:40:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,381,1520924400"; d="scan'208";a="52769888" Received: from yhuang-gentoo.sh.intel.com ([10.239.193.148]) by fmsmga004.fm.intel.com with ESMTP; 09 May 2018 01:40:08 -0700 From: "Huang, Ying" To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , "Kirill A. Shutemov" , Andrea Arcangeli , Michal Hocko , Johannes Weiner , Shaohua Li , Hugh Dickins , Minchan Kim , Rik van Riel , Dave Hansen , Naoya Horiguchi , Zi Yan Subject: [PATCH -mm -V2 20/21] mm, THP, swap: create PMD swap mapping when unmap the THP Date: Wed, 9 May 2018 16:38:45 +0800 Message-Id: <20180509083846.14823-21-ying.huang@intel.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180509083846.14823-1-ying.huang@intel.com> References: <20180509083846.14823-1-ying.huang@intel.com> 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: X-Virus-Scanned: ClamAV using ClamSMTP From: Huang Ying This is the final step of the THP swapin support. When reclaiming a anonymous THP, after allocating the huge swap cluster and add the THP into swap cache, the PMD page mapping will be changed to the mapping to the swap space. Previously, the PMD page mapping will be split before being changed. In this patch, the unmap code is enhanced not to split the PMD mapping, but create a PMD swap mapping to replace it instead. So later when clear the SWAP_HAS_CACHE flag in the last step of swapout, the huge swap cluster will be kept instead of being split, and when swapin, the huge swap cluster will be read as a whole into a THP. That is, the THP will not be split during swapout/swapin. This can eliminate the overhead of splitting/collapsing, and reduce the page fault count, etc. But more important, the utilization of THP is improved greatly, that is, much more THP will be kept when swapping is used, so that we can take full advantage of THP including its high performance for swapout/swapin. Signed-off-by: "Huang, Ying" Cc: "Kirill A. Shutemov" Cc: Andrea Arcangeli Cc: Michal Hocko Cc: Johannes Weiner Cc: Shaohua Li Cc: Hugh Dickins Cc: Minchan Kim Cc: Rik van Riel Cc: Dave Hansen Cc: Naoya Horiguchi Cc: Zi Yan --- include/linux/huge_mm.h | 11 +++++++++++ mm/huge_memory.c | 30 ++++++++++++++++++++++++++++++ mm/rmap.c | 43 +++++++++++++++++++++++++++++++++++++++++-- mm/vmscan.c | 6 +----- 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 5001c28b3d18..d03fcddcc42d 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -404,6 +404,8 @@ static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ +struct page_vma_mapped_walk; + #ifdef CONFIG_THP_SWAP extern void __split_huge_swap_pmd(struct vm_area_struct *vma, unsigned long haddr, @@ -411,6 +413,8 @@ extern void __split_huge_swap_pmd(struct vm_area_struct *vma, extern int split_huge_swap_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long address, pmd_t orig_pmd); extern int do_huge_pmd_swap_page(struct vm_fault *vmf, pmd_t orig_pmd); +extern bool set_pmd_swap_entry(struct page_vma_mapped_walk *pvmw, + struct page *page, unsigned long address, pmd_t pmdval); static inline bool transparent_hugepage_swapin_enabled( struct vm_area_struct *vma) @@ -452,6 +456,13 @@ static inline int do_huge_pmd_swap_page(struct vm_fault *vmf, pmd_t orig_pmd) return 0; } +static inline bool set_pmd_swap_entry(struct page_vma_mapped_walk *pvmw, + struct page *page, unsigned long address, + pmd_t pmdval) +{ + return false; +} + static inline bool transparent_hugepage_swapin_enabled( struct vm_area_struct *vma) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4c2b1f3be038..982369511bcc 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1879,6 +1879,36 @@ int do_huge_pmd_swap_page(struct vm_fault *vmf, pmd_t orig_pmd) count_vm_event(THP_SWPIN_FALLBACK); goto fallback; } + +bool set_pmd_swap_entry(struct page_vma_mapped_walk *pvmw, struct page *page, + unsigned long address, pmd_t pmdval) +{ + struct vm_area_struct *vma = pvmw->vma; + struct mm_struct *mm = vma->vm_mm; + pmd_t swp_pmd; + swp_entry_t entry = { .val = page_private(page) }; + + if (swap_duplicate(&entry, true) < 0) { + set_pmd_at(mm, address, pvmw->pmd, pmdval); + return false; + } + if (list_empty(&mm->mmlist)) { + spin_lock(&mmlist_lock); + if (list_empty(&mm->mmlist)) + list_add(&mm->mmlist, &init_mm.mmlist); + spin_unlock(&mmlist_lock); + } + add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR); + add_mm_counter(mm, MM_SWAPENTS, HPAGE_PMD_NR); + swp_pmd = swp_entry_to_pmd(entry); + if (pmd_soft_dirty(pmdval)) + swp_pmd = pmd_swp_mksoft_dirty(swp_pmd); + set_pmd_at(mm, address, pvmw->pmd, swp_pmd); + + page_remove_rmap(page, true); + put_page(page); + return true; +} #endif static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd) diff --git a/mm/rmap.c b/mm/rmap.c index 5f45d6325c40..4861b1a86e2a 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1402,12 +1402,51 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, continue; } + address = pvmw.address; + +#ifdef CONFIG_THP_SWAP + /* PMD-mapped THP swap entry */ + if (thp_swap_supported() && !pvmw.pte && PageAnon(page)) { + pmd_t pmdval; + + VM_BUG_ON_PAGE(PageHuge(page) || + !PageTransCompound(page), page); + + flush_cache_range(vma, address, + address + HPAGE_PMD_SIZE); + mmu_notifier_invalidate_range_start(mm, address, + address + HPAGE_PMD_SIZE); + if (should_defer_flush(mm, flags)) { + /* check comments for PTE below */ + pmdval = pmdp_huge_get_and_clear(mm, address, + pvmw.pmd); + set_tlb_ubc_flush_pending(mm, + pmd_dirty(pmdval)); + } else + pmdval = pmdp_huge_clear_flush(vma, address, + pvmw.pmd); + + /* + * Move the dirty bit to the page. Now the pmd + * is gone. + */ + if (pmd_dirty(pmdval)) + set_page_dirty(page); + + /* Update high watermark before we lower rss */ + update_hiwater_rss(mm); + + ret = set_pmd_swap_entry(&pvmw, page, address, pmdval); + mmu_notifier_invalidate_range_end(mm, address, + address + HPAGE_PMD_SIZE); + continue; + } +#endif + /* Unexpected PMD-mapped THP? */ VM_BUG_ON_PAGE(!pvmw.pte, page); subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte); - address = pvmw.address; - if (IS_ENABLED(CONFIG_MIGRATION) && (flags & TTU_MIGRATION) && diff --git a/mm/vmscan.c b/mm/vmscan.c index 10c8a38c5eef..b71c1bbad932 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1148,11 +1148,7 @@ static unsigned long shrink_page_list(struct list_head *page_list, * processes. Try to unmap it here. */ if (page_mapped(page)) { - enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH; - - if (unlikely(PageTransHuge(page))) - flags |= TTU_SPLIT_HUGE_PMD; - if (!try_to_unmap(page, flags)) { + if (!try_to_unmap(page, ttu_flags | TTU_BATCH_FLUSH)) { nr_unmap_fail++; goto activate_locked; }