Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp557090imm; Wed, 23 May 2018 01:28:01 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpa4zobXfo02vE7+wHZKBTtlfk5Yg6uIjmL7z5sWFsHdbQcy7lnd3ewQLQK6UMw5rQZyGAk X-Received: by 2002:a65:4204:: with SMTP id c4-v6mr1535294pgq.26.1527064081538; Wed, 23 May 2018 01:28:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527064081; cv=none; d=google.com; s=arc-20160816; b=Nv4FHoN3y7ae0pgRVW+bjTzdH3+TE1rc199YbOHwqbm5TZ1fRLHGVs0UhB56Pwu5Tj tU/oc5AP+O8HpSwaUbutELffG7ilYAgvH5aTLdQy8+5JgZGAyqkVk913mXOEUSae9pUf yep7XFPcHA5gZL1L88sKDuI2OSM7lHLKi3pfPJa5vavH8Qa2ZD53f1Ciu1vV6Igcs3j8 5YobRRLVS4eWXKD94FsjJ5yx5pIyrWu2UNkNo9R/KaDUt9B/ouATYOegBnJv1m6527lH dFKwjB62ut64lJHUeRTAZ2/7Mys9P7ChQduecCT6oTwHpibaVXzn9eCuuyIFD1vkQUec +njg== 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=GB0Mxnm3esMtKy3qXmt4bOuZ4eA8O+409KNASCaDicI=; b=kOZnCA336D32G0RM+8v9qjrzw06L5v1NnyztY37cqI9AK9hO0bfZrhLqD30eKwpKDL HgNxufq6AC1McwTx1Vc9fgnWBoNguQpNPqty4cWUf7WA/XNE5AyRB8hv0CbpJ+25DnTL AH7G9HLpfq1Hw2Gy4Ml8TdpyFvR+uBdJf2NKIHm8CYZ3FolKM+4V8RV7aYiwPgg8fOXH NzY4RUL3D4auVIK6ndfprMnE/5ArKPFJBNCPVH2Ah3ADdsy4GzBIkRXqJuPLHzLTmbpH V+aZzy7PFTjUk5O5DkpOCJnK8wJ+qVjSV08qtWbc3DE2r9eYLjeRGRrid5Q6kVEej7e0 n6AA== 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 r39-v6si18721620pld.249.2018.05.23.01.27.47; Wed, 23 May 2018 01:28:01 -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 S932096AbeEWI05 (ORCPT + 99 others); Wed, 23 May 2018 04:26:57 -0400 Received: from mga03.intel.com ([134.134.136.65]:7709 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754520AbeEWI0v (ORCPT ); Wed, 23 May 2018 04:26:51 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 May 2018 01:26:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,432,1520924400"; d="scan'208";a="57726050" Received: from yhuang6-ux31a.sh.intel.com ([10.239.197.97]) by fmsmga001.fm.intel.com with ESMTP; 23 May 2018 01:26:48 -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 -V3 07/21] mm, THP, swap: Support PMD swap mapping in split_swap_cluster() Date: Wed, 23 May 2018 16:26:11 +0800 Message-Id: <20180523082625.6897-8-ying.huang@intel.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180523082625.6897-1-ying.huang@intel.com> References: <20180523082625.6897-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 splitting a THP in swap cache or failing to allocate a THP when swapin a huge swap cluster, the huge swap cluster will be split. In addition to clear the huge flag of the swap cluster, the PMD swap mapping count recorded in cluster_count() will be set to 0. But we will not touch PMD swap mappings themselves, because it is hard to find them all sometimes. When the PMD swap mappings are operated later, it will be found that the huge swap cluster has been split and the PMD swap mappings will be split at that time. Unless splitting a THP in swap cache (specified via "force" parameter), split_swap_cluster() will return -EEXIST if there is SWAP_HAS_CACHE flag in swap_map[offset]. Because this indicates there is a THP corresponds to this huge swap cluster, and it isn't desired to split the THP. When splitting a THP in swap cache, the position to call split_swap_cluster() is changed to before unlocking sub-pages. So that all sub-pages will be kept locked from the THP has been split to the huge swap cluster is split. This makes the code much easier to be reasoned. 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/swap.h | 4 ++-- mm/huge_memory.c | 18 ++++++++++++------ mm/swapfile.c | 45 ++++++++++++++++++++++++++++++--------------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index bb9de2cb952a..878f132dabc0 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -617,10 +617,10 @@ static inline swp_entry_t get_swap_page(struct page *page) #endif /* CONFIG_SWAP */ #ifdef CONFIG_THP_SWAP -extern int split_swap_cluster(swp_entry_t entry); +extern int split_swap_cluster(swp_entry_t entry, bool force); extern int split_swap_cluster_map(swp_entry_t entry); #else -static inline int split_swap_cluster(swp_entry_t entry) +static inline int split_swap_cluster(swp_entry_t entry, bool force) { return 0; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 84d5d8ff869e..e363e13f6751 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2502,6 +2502,17 @@ static void __split_huge_page(struct page *page, struct list_head *list, unfreeze_page(head); + /* + * Split swap cluster before unlocking sub-pages. So all + * sub-pages will be kept locked from THP has been split to + * swap cluster is split. + */ + if (PageSwapCache(head)) { + swp_entry_t entry = { .val = page_private(head) }; + + split_swap_cluster(entry, true); + } + for (i = 0; i < HPAGE_PMD_NR; i++) { struct page *subpage = head + i; if (subpage == page) @@ -2728,12 +2739,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) __dec_node_page_state(page, NR_SHMEM_THPS); spin_unlock(&pgdata->split_queue_lock); __split_huge_page(page, list, flags); - if (PageSwapCache(head)) { - swp_entry_t entry = { .val = page_private(head) }; - - ret = split_swap_cluster(entry); - } else - ret = 0; + ret = 0; } else { if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) { pr_alert("total_mapcount: %u, page_count(): %u\n", diff --git a/mm/swapfile.c b/mm/swapfile.c index 05f53c4c0cfe..1e723d3a9a6f 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1414,21 +1414,6 @@ static void swapcache_free_cluster(swp_entry_t entry) } } } - -int split_swap_cluster(swp_entry_t entry) -{ - struct swap_info_struct *si; - struct swap_cluster_info *ci; - unsigned long offset = swp_offset(entry); - - si = _swap_info_get(entry); - if (!si) - return -EBUSY; - ci = lock_cluster(si, offset); - cluster_clear_huge(ci); - unlock_cluster(ci); - return 0; -} #else static inline void swapcache_free_cluster(swp_entry_t entry) { @@ -4072,6 +4057,36 @@ int split_swap_cluster_map(swp_entry_t entry) unlock_cluster(ci); return 0; } + +int split_swap_cluster(swp_entry_t entry, bool force) +{ + struct swap_info_struct *si; + struct swap_cluster_info *ci; + unsigned long offset = swp_offset(entry); + int ret = 0; + + si = get_swap_device(entry); + if (!si) + return -EINVAL; + ci = lock_cluster(si, offset); + /* The swap cluster has been split by someone else */ + if (!cluster_is_huge(ci)) + goto out; + VM_BUG_ON(!is_cluster_offset(offset)); + VM_BUG_ON(cluster_count(ci) < SWAPFILE_CLUSTER); + /* If not forced, don't split swap cluster has swap cache */ + if (!force && si->swap_map[offset] & SWAP_HAS_CACHE) { + ret = -EEXIST; + goto out; + } + cluster_set_count(ci, SWAPFILE_CLUSTER); + cluster_clear_huge(ci); + +out: + unlock_cluster(ci); + put_swap_device(si); + return ret; +} #endif static int __init swapfile_init(void) -- 2.16.1