From patchwork Mon Sep 3 07:22:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 10585563 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6AF0513AC for ; Mon, 3 Sep 2018 07:22:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 540162876E for ; Mon, 3 Sep 2018 07:22:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 470B92879D; Mon, 3 Sep 2018 07:22:55 +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 92DFA2876E for ; Mon, 3 Sep 2018 07:22:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 080976B66AE; Mon, 3 Sep 2018 03:22:37 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E68D36B66B1; Mon, 3 Sep 2018 03:22:36 -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 B54AC6B66AE; Mon, 3 Sep 2018 03:22:36 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by kanga.kvack.org (Postfix) with ESMTP id 579916B66AB for ; Mon, 3 Sep 2018 03:22:36 -0400 (EDT) Received: by mail-pl1-f200.google.com with SMTP id w18-v6so1921091plp.3 for ; Mon, 03 Sep 2018 00:22:36 -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=i3PallYERdv0CWM2pHlJx4yHnR7FXS7oPnYzxmnxn/0=; b=TAxouL6ZU4lWxCNkqdxUnTD5+PfOiJgfHM7xZ1H/t2hsKwOrF2ky2A1nKtO7PbfToB vKZkXIaSBOsPH31gGv44o2nDx8I3k6aGSl1Paj6vQYF/OzvRACKx4ircsoTJMlY5rulK jRkKskwDve4eXOUzGwr0VinO8BdfjCub5pquMLrF59rQvokT5KYw0zyKQLGV3Qz3UvjU O7UdULW1OPDzVCFNAqjMupHbG5iKHEMhRDRU4maWaKR/tmDz9ujaM1mv86W7TH8aYFjh ybCAIBzZUsivKZN11jOW2GYGBPhNXeXAcDK1NpN2w8XQpVvpSe4hmmQHrx6ebO4hmyEC pd0g== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: APzg51DQf1xvQUjajk92nn1nd5YxT+3cspA79++nYXexm64zz7kE1HoY DDNjmk1Y8ZegTPE8KUIEcskByFqnSMldnxhHZmlEqt38XqdmvwuPPMeHzYLgVcbmO6hsGtpQS6b qK2Q8jZ+IuvMmjZKy+gGanIhATlGdOSamxSXa4fX8BUDts987eycHjUz7v264hiHDow== X-Received: by 2002:a17:902:9045:: with SMTP id w5-v6mr27281237plz.10.1535959356030; Mon, 03 Sep 2018 00:22:36 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZc4VXgoUn9j2nmPDhamfqClxXHsi0jrIdaZW1Oi8q1WQ+AdaX2QNq4sfBnGVD/PdzOBQ+B X-Received: by 2002:a17:902:9045:: with SMTP id w5-v6mr27281200plz.10.1535959355224; Mon, 03 Sep 2018 00:22:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535959355; cv=none; d=google.com; s=arc-20160816; b=0d9a5Zzj+9A/Dy9FLYLcKpAfD0wkzk3Cb9Lnl9guYmazdF9jc8InOx4k0yiq7rWbkd k0clD8VGq558jaRBrRE8Dh1dZyynprdrVrQjhEjn+jy/D7aQY0say1LxtB9B2zci79Si 1NFEAHZE15X5RIuinfDOKJCYMdWxzAe+P6qwlDPNkzJWHOHW6bncxLrSa34CG0BBVL/G 4VxFSYAiHDPbwOUbtC0WJEf1sHcKfGDzSt4pdjBXfHdHip2tMBjR2/M07Z65imy61DxY F4ePC0xnFKscoag+TXoVALE180Tbyk7pMaaJxfNyR91VOZF5NAbzmPndOJ4umxqXOI2j SZ4A== 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=i3PallYERdv0CWM2pHlJx4yHnR7FXS7oPnYzxmnxn/0=; b=qdOpS5N/IUhgpzVtJb1TWK+KAfRi2Iab6hfh+GeNx+v4EYuyppQeO/uHZCMLNUl+DZ VJ7xiQY/Aghr25pUdcNDn5M6OEv21Ty4jRphrWDl0bHBuYLECSHSVMzWOLnnFaN0Vb8N bFe01aAf/2gG/VWeN4DATtiiFAwXjmLnU2ALTgvx/tXmOZfyqAiHWG/SlFhxtw7qrqYp VrUkBoyr2eda/lxWv5I9BeO4nvpu5H3/SaoePT7ETHZ0Tzm/uAoXktsfUX3tQapg7HY0 3wBeDDzfr3SCcV9D0qByWlWUL2i2usNRD+uXgXkOhPWf3i3Eh4tTgh17QExeD3lcOMSx 5GQQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga07.intel.com (mga07.intel.com. [134.134.136.100]) by mx.google.com with ESMTPS id az1-v6si9327648plb.513.2018.09.03.00.22.35 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Sep 2018 00:22:35 -0700 (PDT) Received-SPF: pass (google.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) client-ip=134.134.136.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Sep 2018 00:22:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,324,1531810800"; d="scan'208";a="70146890" Received: from yhuang-mobile.sh.intel.com ([10.239.196.86]) by orsmga008.jf.intel.com with ESMTP; 03 Sep 2018 00:22:31 -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 -V5 07/21] swap: Support PMD swap mapping in split_swap_cluster() Date: Mon, 3 Sep 2018 15:22:00 +0800 Message-Id: <20180903072214.24602-8-ying.huang@intel.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20180903072214.24602-1-ying.huang@intel.com> References: <20180903072214.24602-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 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 Cc: Daniel Jordan --- include/linux/swap.h | 6 ++++-- mm/huge_memory.c | 18 ++++++++++------ mm/swapfile.c | 58 +++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 7ee8bfdd0861..1ab197a0e065 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -612,11 +612,13 @@ static inline swp_entry_t get_swap_page(struct page *page) #endif /* CONFIG_SWAP */ +#define SSC_SPLIT_CACHED 0x1 + #ifdef CONFIG_THP_SWAP -extern int split_swap_cluster(swp_entry_t entry); +extern int split_swap_cluster(swp_entry_t entry, unsigned long flags); 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, unsigned long flags) { return 0; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 956c49bfd208..9341c90aa286 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2496,6 +2496,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, SSC_SPLIT_CACHED); + } + for (i = 0; i < HPAGE_PMD_NR; i++) { struct page *subpage = head + i; if (subpage == page) @@ -2719,12 +2730,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 242f70c9e1f2..a16cd903d3ef 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1469,23 +1469,6 @@ void put_swap_page(struct page *page, swp_entry_t entry) unlock_cluster_or_swap_info(si, ci); } -#ifdef CONFIG_THP_SWAP -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; -} -#endif - static int swp_entry_cmp(const void *ent1, const void *ent2) { const swp_entry_t *e1 = ent1, *e2 = ent2; @@ -4064,6 +4047,47 @@ int split_swap_cluster_map(swp_entry_t entry) unlock_cluster(ci); return 0; } + +/* + * We will not try to split all PMD swap mappings to the swap cluster, + * because we haven't enough information available for that. Later, + * when the PMD swap mapping is duplicated or swapin, etc, the PMD + * swap mapping will be split and fallback to the PTE operations. + */ +int split_swap_cluster(swp_entry_t entry, unsigned long flags) +{ + 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, we are done */ + if (!cluster_is_huge(ci)) + goto out; + VM_BUG_ON(!IS_ALIGNED(offset, SWAPFILE_CLUSTER)); + VM_BUG_ON(cluster_count(ci) < SWAPFILE_CLUSTER); + /* + * If not requested, don't split swap cluster that has SWAP_HAS_CACHE + * flag. When the flag is cleared later, the huge swap cluster will + * be split if there is no PMD swap mapping. + */ + if (!(flags & SSC_SPLIT_CACHED) && + si->swap_map[offset] & SWAP_HAS_CACHE) { + ret = -EEXIST; + goto out; + } + cluster_set_swapcount(ci, 0); + cluster_clear_huge(ci); + +out: + unlock_cluster(ci); + put_swap_device(si); + return ret; +} #endif static int __init swapfile_init(void)