Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp377676imm; Thu, 21 Jun 2018 20:59:27 -0700 (PDT) X-Google-Smtp-Source: ADUXVKK4VaU/F8+lhow8Y1Vd2OdpETxZRCTDEpQc4ymR/F9TKDq3qFx9oM9dhE0FfQLJbKxemvtA X-Received: by 2002:a65:6103:: with SMTP id z3-v6mr20460352pgu.125.1529639966983; Thu, 21 Jun 2018 20:59:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529639966; cv=none; d=google.com; s=arc-20160816; b=mEYN2Dz8jJMZKMewTj+ZWBnUK2odAJsMP58FlbLtSQGooAWHcHWr33W4RqEnbbjMIP d5k8s5w9V81yNLQswK5a/QrDYHSWiEIpuU3KAVj3qTol8GD1ecmLxVPAbezSrK8dcUeY tKoCCNvNxSyDTxkO9cvziXoObIFS9A5nzlvV7B2Nsa3D8VF153g4qMxSC1LMeeHc4Dyn TiijWfO4vruM/s3/MN04o/Htk7XYo4Ia1+SrzDDtYwiPhrqsiWreKCZjtoZTBG6dphcD CsRyNzVrXVaDHStd9aiEnI8oPcrJ8l2NfC+BwTBLKk61TgOw/XOUZILeU9TxkI/rOZA3 Koog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=3CIL/VFGgRtEZ8AmjeiN8j+oHlGhmj5XQFWTKYoA2Lc=; b=PZZ9qw5wQ8vaGGVcE8D6j+4greDoxKPQ//ooYpv6ntCp0EZ2D1GOKVObrBVLD77SvO S3eC5YWK2JRpoQkHRKN59H8DTiT159BS56llcIitv6ECRyOUsnkes+iR/CykQI5iPSWQ wJCeDktLiewjGAOYjj85d6kgzdxMk9wqZ4mHl0HXL3UssmtugeU1oP2f1GhHf2Zzdozy ORxTkn67lLVFLib4Ox9NsgZXwVepbKDZr2VvNXzkF1quTSEvgrujXal4vFy+xy9yvxUz Y72og0K/vQuH9uQOs6q3FkPqqBF9OGIbt7vhT0X7/2XQBl0am/2d9roN5Bl3/ebsfggq DtPA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a66-v6si4578905pli.122.2018.06.21.20.59.12; Thu, 21 Jun 2018 20:59:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934556AbeFVDzs (ORCPT + 99 others); Thu, 21 Jun 2018 23:55:48 -0400 Received: from mga17.intel.com ([192.55.52.151]:9403 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964777AbeFVDzo (ORCPT ); Thu, 21 Jun 2018 23:55:44 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Jun 2018 20:55:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,255,1526367600"; d="scan'208";a="65335160" Received: from wanpingl-mobl.ccr.corp.intel.com (HELO yhuang6-ux31a.ccr.corp.intel.com) ([10.254.212.200]) by fmsmga004.fm.intel.com with ESMTP; 21 Jun 2018 20:55:41 -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 , Daniel Jordan Subject: [PATCH -mm -v4 13/21] mm, THP, swap: Support PMD swap mapping in madvise_free() Date: Fri, 22 Jun 2018 11:51:43 +0800 Message-Id: <20180622035151.6676-14-ying.huang@intel.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20180622035151.6676-1-ying.huang@intel.com> References: <20180622035151.6676-1-ying.huang@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Huang Ying When madvise_free() found a PMD swap mapping, if only part of the huge swap cluster is operated on, the PMD swap mapping will be split and fallback to PTE swap mapping processing. Otherwise, if all huge swap cluster is operated on, free_swap_and_cache() will be called to decrease the PMD swap mapping count and probably free the swap space and the THP in swap cache too. 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 Cc: Daniel Jordan --- mm/huge_memory.c | 50 +++++++++++++++++++++++++++++++++++--------------- mm/madvise.c | 2 +- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 73fc77633642..fe946ca09b28 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1842,6 +1842,15 @@ static inline void __split_huge_swap_pmd(struct vm_area_struct *vma, } #endif +static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd) +{ + pgtable_t pgtable; + + pgtable = pgtable_trans_huge_withdraw(mm, pmd); + pte_free(mm, pgtable); + mm_dec_nr_ptes(mm); +} + /* * Return true if we do MADV_FREE successfully on entire pmd page. * Otherwise, return false. @@ -1862,15 +1871,35 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, goto out_unlocked; orig_pmd = *pmd; - if (is_huge_zero_pmd(orig_pmd)) - goto out; - if (unlikely(!pmd_present(orig_pmd))) { - VM_BUG_ON(thp_migration_supported() && - !is_pmd_migration_entry(orig_pmd)); - goto out; + swp_entry_t entry = pmd_to_swp_entry(orig_pmd); + + if (is_migration_entry(entry)) { + VM_BUG_ON(!thp_migration_supported()); + goto out; + } else if (thp_swap_supported() && !non_swap_entry(entry)) { + /* If part of THP is discarded */ + if (next - addr != HPAGE_PMD_SIZE) { + unsigned long haddr = addr & HPAGE_PMD_MASK; + + __split_huge_swap_pmd(vma, haddr, pmd); + goto out; + } + free_swap_and_cache(entry, true); + pmd_clear(pmd); + zap_deposited_table(mm, pmd); + if (current->mm == mm) + sync_mm_rss(mm); + add_mm_counter(mm, MM_SWAPENTS, -HPAGE_PMD_NR); + ret = true; + goto out; + } else + VM_BUG_ON(1); } + if (is_huge_zero_pmd(orig_pmd)) + goto out; + page = pmd_page(orig_pmd); /* * If other processes are mapping this page, we couldn't discard @@ -1916,15 +1945,6 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, return ret; } -static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd) -{ - pgtable_t pgtable; - - pgtable = pgtable_trans_huge_withdraw(mm, pmd); - pte_free(mm, pgtable); - mm_dec_nr_ptes(mm); -} - int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr) { diff --git a/mm/madvise.c b/mm/madvise.c index a96abb915e22..fc423d96d4d6 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -321,7 +321,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, unsigned long next; next = pmd_addr_end(addr, end); - if (pmd_trans_huge(*pmd)) + if (pmd_trans_huge(*pmd) || is_swap_pmd(*pmd)) if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next)) goto next; -- 2.16.4