From patchwork Sun Apr 10 13:54:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zach O'Keefe X-Patchwork-Id: 12808145 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 658DDC433F5 for ; Sun, 10 Apr 2022 13:55:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B17606B0073; Sun, 10 Apr 2022 09:55:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A9F846B0074; Sun, 10 Apr 2022 09:55:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A4946B0075; Sun, 10 Apr 2022 09:55:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 799BC6B0073 for ; Sun, 10 Apr 2022 09:55:00 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 49E1F2CF for ; Sun, 10 Apr 2022 13:55:00 +0000 (UTC) X-FDA: 79341115560.13.DBC5C59 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf25.hostedemail.com (Postfix) with ESMTP id D66EEA0007 for ; Sun, 10 Apr 2022 13:54:59 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id h8-20020a25e208000000b00628c0565607so10269805ybe.0 for ; Sun, 10 Apr 2022 06:54:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=vKX9tDF77gkFY4J7UZ8x9dswmSDs5FlwUgGPOHO0F9E=; b=ephcHAQYALiNbiXaNq6CdDrvNc1nqEHGRvP34x7kXoDuM5nWE2MgNBU/0Td2+B/US+ DvFYRls45beF9ktg1ZmMInlSjbCdDoBYgwsqgwkYw5xp/KS+XEnq9ABgv39NQsD8L2Ak TS21p68W77ZGEJNkBy/1suW4swyn+ZVKWE5E9320Gyu1NWls3vfoBCCExdqpw7IHGMSf wsPi/rEBGtjP6MDoum7yFNzFb+XHdwR65agxDPeXKEnEPjSTytHOu7+iUnPzw/57YlC2 OZvxz74lRs1fF4h//aKM3Q754RkRcZANG5FiR4Pm6gYQ0cOSUdq8bKCU52f9EislvYMB wi/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=vKX9tDF77gkFY4J7UZ8x9dswmSDs5FlwUgGPOHO0F9E=; b=R6US6wnsTE1Xo3nd7FG4ICtJIsYIZjchJrbLYeiWGpDNiGHgFOQuVAc4ws52m+iRe0 L0xmtT2zZRXBxM83MOa7dH5+lxGgg3nAvQy4txmXnswGHbhOmgCY+qY4DmIVhqnk1vkA 9o5H74ZJQcHjEXKmQaMDzjvyht/1TfxAM8/K/T15gICoDcVR6Y1lkYSygJHavlZLcK/s dfUT7wfLUiw7EeBMGihc+ten/Ly1+Jspp+cxzI03XdHKKWxuMseBF1E8MAieOPqTSyyY Btx/TfDStynp2NQN9FqTPXtI9L8/knEoNEX+qU92F6zotc2Czqe+mTau0XFrE0662sJx 4iUw== X-Gm-Message-State: AOAM533M1LCyTvQCw8e44D9cnU+q/IRDm2OAKptmF3zHSsGVuCdWbo5E Hk8iSu1l842zqBBBNC4A0wEjqH5tOPmH X-Google-Smtp-Source: ABdhPJyIMHbjZAq/zrvnH2Brx9qUV4NZwVrboptymJGEXYKVthYVaiZCt7mFIX2V3EvDmx/7l7kuXQUvwnlm X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a81:1dd1:0:b0:2ea:c38b:65a8 with SMTP id d200-20020a811dd1000000b002eac38b65a8mr22211063ywd.135.1649598899017; Sun, 10 Apr 2022 06:54:59 -0700 (PDT) Date: Sun, 10 Apr 2022 06:54:34 -0700 In-Reply-To: <20220410135445.3897054-1-zokeefe@google.com> Message-Id: <20220410135445.3897054-2-zokeefe@google.com> Mime-Version: 1.0 References: <20220410135445.3897054-1-zokeefe@google.com> X-Mailer: git-send-email 2.35.1.1178.g4f1659d476-goog Subject: [PATCH 01/12] mm/khugepaged: record SCAN_PMD_MAPPED when scan_pmd() finds THP From: "Zach O'Keefe" To: Alex Shi , David Hildenbrand , David Rientjes , Matthew Wilcox , Michal Hocko , Pasha Tatashin , SeongJae Park , Song Liu , Vlastimil Babka , Yang Shi , Zi Yan , linux-mm@kvack.org Cc: Andrea Arcangeli , Andrew Morton , Arnd Bergmann , Axel Rasmussen , Chris Kennelly , Chris Zankel , Helge Deller , Hugh Dickins , Ivan Kokshaysky , "James E.J. Bottomley" , Jens Axboe , "Kirill A. Shutemov" , Matt Turner , Max Filippov , Miaohe Lin , Minchan Kim , Patrick Xia , Pavel Begunkov , Peter Xu , Thomas Bogendoerfer , "Zach O'Keefe" X-Rspam-User: Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ephcHAQY; spf=pass (imf25.hostedemail.com: domain of 3s-FSYgcKCNoVKGAABACKKCHA.8KIHEJQT-IIGR68G.KNC@flex--zokeefe.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3s-FSYgcKCNoVKGAABACKKCHA.8KIHEJQT-IIGR68G.KNC@flex--zokeefe.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: D66EEA0007 X-Stat-Signature: mmhs5afwf99x7kij51iw45nuw4nofhx5 X-HE-Tag: 1649598899-991574 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: When scanning an anon pmd to see if it's eligible for collapse, return SCAN_PMD_MAPPED if the pmd already maps a THP. Note that SCAN_PMD_MAPPED is different from SCAN_PAGE_COMPOUND used in the file-collapse path, since the latter might identify pte-mapped compound pages. This is required by MADV_COLLAPSE which necessarily needs to know what hugepage-aligned/sized regions are already pmd-mapped. Signed-off-by: Zach O'Keefe --- include/trace/events/huge_memory.h | 3 ++- mm/internal.h | 1 + mm/khugepaged.c | 30 ++++++++++++++++++++++++++---- mm/rmap.c | 15 +++++++++++++-- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h index d651f3437367..9faa678e0a5b 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -33,7 +33,8 @@ EM( SCAN_ALLOC_HUGE_PAGE_FAIL, "alloc_huge_page_failed") \ EM( SCAN_CGROUP_CHARGE_FAIL, "ccgroup_charge_failed") \ EM( SCAN_TRUNCATED, "truncated") \ - EMe(SCAN_PAGE_HAS_PRIVATE, "page_has_private") \ + EM( SCAN_PAGE_HAS_PRIVATE, "page_has_private") \ + EMe(SCAN_PMD_MAPPED, "page_pmd_mapped") \ #undef EM #undef EMe diff --git a/mm/internal.h b/mm/internal.h index 1d3fb3c0f971..db594d611925 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -172,6 +172,7 @@ extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason /* * in mm/rmap.c: */ +pmd_t *mm_find_pmd_raw(struct mm_struct *mm, unsigned long address); extern pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address); /* diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 0cde4b44d799..b403f056a847 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -51,6 +51,7 @@ enum scan_result { SCAN_CGROUP_CHARGE_FAIL, SCAN_TRUNCATED, SCAN_PAGE_HAS_PRIVATE, + SCAN_PMD_MAPPED, }; #define CREATE_TRACE_POINTS @@ -987,6 +988,29 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address, return 0; } +static int find_pmd_or_thp_or_none(struct mm_struct *mm, + unsigned long address, + pmd_t **pmd) +{ + *pmd = mm_find_pmd_raw(mm, address); + pmd_t pmde; + + if (!*pmd) + return SCAN_PMD_NULL; + + pmde = pmd_read_atomic(*pmd); + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + /* See comments in pmd_none_or_trans_huge_or_clear_bad() */ + barrier(); +#endif + if (!pmd_present(pmde) || pmd_none(pmde)) + return SCAN_PMD_NULL; + if (pmd_trans_huge(pmde)) + return SCAN_PMD_MAPPED; + return SCAN_SUCCEED; +} + /* * Bring missing pages in from swap, to complete THP collapse. * Only done if khugepaged_scan_pmd believes it is worthwhile. @@ -1238,11 +1262,9 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, VM_BUG_ON(address & ~HPAGE_PMD_MASK); - pmd = mm_find_pmd(mm, address); - if (!pmd) { - result = SCAN_PMD_NULL; + result = find_pmd_or_thp_or_none(mm, address, &pmd); + if (result != SCAN_SUCCEED) goto out; - } memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load)); pte = pte_offset_map_lock(mm, pmd, address, &ptl); diff --git a/mm/rmap.c b/mm/rmap.c index a1211fa879cf..fb47443f44c6 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -758,13 +758,12 @@ unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma) return vma_address(page, vma); } -pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address) +pmd_t *mm_find_pmd_raw(struct mm_struct *mm, unsigned long address) { pgd_t *pgd; p4d_t *p4d; pud_t *pud; pmd_t *pmd = NULL; - pmd_t pmde; pgd = pgd_offset(mm, address); if (!pgd_present(*pgd)) @@ -779,6 +778,18 @@ pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address) goto out; pmd = pmd_offset(pud, address); +out: + return pmd; +} + +pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address) +{ + pmd_t pmde; + pmd_t *pmd; + + pmd = mm_find_pmd_raw(mm, address); + if (!pmd) + goto out; /* * Some THP functions use the sequence pmdp_huge_clear_flush(), set_pmd_at() * without holding anon_vma lock for write. So when looking for a