From patchwork Sat Jun 4 00:39:51 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: 12869459 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 3610BCCA47D for ; Sat, 4 Jun 2022 00:40:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B8478D0005; Fri, 3 Jun 2022 20:40:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F43E8D0001; Fri, 3 Jun 2022 20:40:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B74E8D0005; Fri, 3 Jun 2022 20:40:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 46A0C8D0001 for ; Fri, 3 Jun 2022 20:40:17 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 262E81210F7 for ; Sat, 4 Jun 2022 00:40:17 +0000 (UTC) X-FDA: 79538696874.19.F636766 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf22.hostedemail.com (Postfix) with ESMTP id 423BCC0017 for ; Sat, 4 Jun 2022 00:40:13 +0000 (UTC) Received: by mail-pf1-f202.google.com with SMTP id x18-20020a62fb12000000b0051bab667811so4685720pfm.5 for ; Fri, 03 Jun 2022 17:40:16 -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=lqmTpORZ3MQn6/m+oz8fm9SRtE9DcdUbGqykxpzOvOY=; b=PiXCdwYe0Tt3l8cdC5KZZsLlRUc/t20KgAhhCTd78fAv+th3yrCrnEiG1qKUS0rqR3 psagYiv4PrDxghoApAWK610i5FAzwQXLrEkWpRsYLn+9UdjTJotG1EluNebbI0Hw8psP bAJRQrwc/FPIxg5snkTC10o54ItQ4c39UPl6Ka/Nw7bk4m+yhbjp7vAqBg95wA96pz4l 96VztdO9q5V+KTQrcVsR+QPXtkGt4DAyjyCL7xTymsSVqRceMsjsDXCC7Xt9+vYybhKQ /omau5gpHdb3haD4TvbLo2Otw3b0v6PxYU8ZmFg8SARgrXTQbTvQMQ60rqn+Bqz/qAyV 3hPQ== 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=lqmTpORZ3MQn6/m+oz8fm9SRtE9DcdUbGqykxpzOvOY=; b=Tk+5yLKSUOtw8QSH/ka1XD/wpJZMa93Va26T/034lYbRsxJsF+XISndLgPlRE6fO+j XdtBJT48NJ7OMWIE7RUbNKJXoZ0XoxJFBjlWEFvnaC+vIbKb6+IQSh1e6tOuDIW1BQUK nhrHVOReCVa4HN3kwtD+U2ViOYzgEOfy4TksR6ShyDx4UxEitjlaZvAigb4Y0KGJYIXb 8Op0uqkDhX7JHqD+fmupGZeaseM9iGOTv8Bnuxszm7zrhwvffldaf48Fz7lvJ1q4kLQM 2ivEzcEH8I2TH3EbvIPOBloVmo+7AmqPAjS+16EwV1KzFWz76OKnak1BZUK0Q6vIQLXR +o0Q== X-Gm-Message-State: AOAM5325Kbe1GU5PwJ+E40sakSmmMg0E5NxybSS6p9v5XWt1NhrJAo0M 6o+FinWLW4KmD/vtE7yTPeNPY8ky9P7B X-Google-Smtp-Source: ABdhPJwl0aIj/QO6+P0rGCC6j4uruqp1OTjD2bNNQh87dhpPOIZT7PP1reGvB4PU1Icq1iIAo/9IN0JZoM+J X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a17:903:41d1:b0:166:3c6:f055 with SMTP id u17-20020a17090341d100b0016603c6f055mr12566717ple.112.1654303215645; Fri, 03 Jun 2022 17:40:15 -0700 (PDT) Date: Fri, 3 Jun 2022 17:39:51 -0700 In-Reply-To: <20220604004004.954674-1-zokeefe@google.com> Message-Id: <20220604004004.954674-3-zokeefe@google.com> Mime-Version: 1.0 References: <20220604004004.954674-1-zokeefe@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH v6 02/15] 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 , Peter Xu , Rongwei Wang , 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 , Thomas Bogendoerfer , "Zach O'Keefe" Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=PiXCdwYe; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 376maYgcKCMgD2ysstsu22uzs.q20zw18B-00y9oqy.25u@flex--zokeefe.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=376maYgcKCMgD2ysstsu22uzs.q20zw18B-00y9oqy.25u@flex--zokeefe.bounces.google.com X-Stat-Signature: sm7fkss77qnah6emuzhadxis8z7qns3f X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 423BCC0017 X-HE-Tag: 1654303213-644269 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 | 1 + mm/internal.h | 1 + mm/khugepaged.c | 32 ++++++++++++++++++++++++++---- mm/rmap.c | 15 ++++++++++++-- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h index d651f3437367..55392bf30a03 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -11,6 +11,7 @@ EM( SCAN_FAIL, "failed") \ EM( SCAN_SUCCEED, "succeeded") \ EM( SCAN_PMD_NULL, "pmd_null") \ + EM( SCAN_PMD_MAPPED, "page_pmd_mapped") \ EM( SCAN_EXCEED_NONE_PTE, "exceed_none_pte") \ EM( SCAN_EXCEED_SWAP_PTE, "exceed_swap_pte") \ EM( SCAN_EXCEED_SHARED_PTE, "exceed_shared_pte") \ diff --git a/mm/internal.h b/mm/internal.h index 6e14749ad1e5..f768c7fae668 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -188,6 +188,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 cc3d6fb446d5..7a914ca19e96 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -28,6 +28,7 @@ enum scan_result { SCAN_FAIL, SCAN_SUCCEED, SCAN_PMD_NULL, + SCAN_PMD_MAPPED, SCAN_EXCEED_NONE_PTE, SCAN_EXCEED_SWAP_PTE, SCAN_EXCEED_SHARED_PTE, @@ -901,6 +902,31 @@ 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)) + return SCAN_PMD_NULL; + if (pmd_trans_huge(pmde)) + return SCAN_PMD_MAPPED; + if (pmd_bad(pmde)) + return SCAN_FAIL; + return SCAN_SUCCEED; +} + /* * Bring missing pages in from swap, to complete THP collapse. * Only done if khugepaged_scan_pmd believes it is worthwhile. @@ -1146,11 +1172,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 04fac1af870b..c9979c6ad7a1 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -767,13 +767,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)) @@ -788,6 +787,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