From patchwork Thu Apr 14 18:06:01 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: 12813842 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 A6927C433EF for ; Thu, 14 Apr 2022 18:06:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1C15B6B0073; Thu, 14 Apr 2022 14:06:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 16FD96B0074; Thu, 14 Apr 2022 14:06:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 010096B0075; Thu, 14 Apr 2022 14:06:37 -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 E41246B0073 for ; Thu, 14 Apr 2022 14:06:37 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B78AB63760 for ; Thu, 14 Apr 2022 18:06:37 +0000 (UTC) X-FDA: 79356264834.13.A94A673 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf17.hostedemail.com (Postfix) with ESMTP id 1D80E40005 for ; Thu, 14 Apr 2022 18:06:36 +0000 (UTC) Received: by mail-pg1-f201.google.com with SMTP id v188-20020a632fc5000000b0039d3ce300eeso3084950pgv.4 for ; Thu, 14 Apr 2022 11:06:36 -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=9sSTo5Mxf0tASsQXorJe5tCc/cpdNsuKRXm4qsHQW/g=; b=mzKDhXyqe8SR5YOuNqZnUY1zKLYxRwTTufdvlcdhPSmG+j5MX3gs8N+1GKOStlXCMq k42jNhNL0mr68IaqgBOP1CdBm2QeoHyt42upyXu0FxVJj9F2OrpY/NNuHiwHtv5D5Rkq 6XaI4dU/fyTJNGArszTG0Z/AOkDiOFeMJ2sPhHX+dqwtMdOEGTJjG/0Cdxh3T181ai1p 7kf2TNnFsuOwiVO5J+YeYB2Tqm0B04zaEQRBaYfybs+pBidSTxaOGb29o3vY3CiHzRla 0ydoTb30KLAdU6OJNOAzqNdmg1H9JFTHAv0pO0AgFweYgc3VwZZX9XHm6MIWgUm3G3sF YTsw== 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=9sSTo5Mxf0tASsQXorJe5tCc/cpdNsuKRXm4qsHQW/g=; b=qMh2Ney1sfzyxk2I0QYoKpGklXwvcrz/q0KZ5HBNc636DwrNSCgBKJZeEaq+V3keH6 aUrC+lLFftiFR1Rzhc6MB3w3YLjSNeKyPWr4X5NeXk3GSJPL3REeEVuLVVa+7BP0L8Cn LJKep4HNUI0nGacszqhipI4IcxFsAwKfx4P2BVvimNgYfLi4MoLFXVEmcCWvIF7yVckd 9CPpiZyi3CzxMQuQxQQRsvrgOL1MvUmloxBRK3SzFyJuQB+7f0nuJD9ETKMI2ye/QlAl BaaERoiFMedpcfY6URsaFuFX98qCspTa3n3YvP+W0sYSB85KUFSOLPyAvCSbVdPAqZjT wYOQ== X-Gm-Message-State: AOAM5312mBRiMy0DWdu2xJVxpZn+bbIjf0OyHLFhTSZpuI1qwGD6SfwR LojhWawyRrTHhsrDGD3dgzvV6bfoPdle X-Google-Smtp-Source: ABdhPJyjCupz7I5cjFgQOSEhST8H5JcUJd55cx2sx+KwSEfLlvQiJOOLCJhBGwWMHMm07c8wMwGtHNJ3fwUz X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a17:90b:1804:b0:1cb:82e3:5cd0 with SMTP id lw4-20020a17090b180400b001cb82e35cd0mr5660653pjb.8.1649959595903; Thu, 14 Apr 2022 11:06:35 -0700 (PDT) Date: Thu, 14 Apr 2022 11:06:01 -0700 In-Reply-To: <20220414180612.3844426-1-zokeefe@google.com> Message-Id: <20220414180612.3844426-2-zokeefe@google.com> Mime-Version: 1.0 References: <20220414180612.3844426-1-zokeefe@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH v2 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" , kernel test robot X-Stat-Signature: 5ka5g19zdo34wptozco3ozcummo4p6x9 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 1D80E40005 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=mzKDhXyq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3q2JYYgcKCOolaWQQRQSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--zokeefe.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3q2JYYgcKCOolaWQQRQSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--zokeefe.bounces.google.com X-Rspam-User: X-HE-Tag: 1649959596-930584 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 Reported-by: kernel test robot --- 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 48eb2d24fcd2..24fca92bd51a 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -173,6 +173,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 cb43c3aee8b2..5e5404aa6579 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_t pmde; + + *pmd = mm_find_pmd_raw(mm, address); + 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 edfe61f95a7f..bf2a3a08d965 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -759,13 +759,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)) @@ -780,6 +779,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