From patchwork Thu Jun 10 06:52:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12311981 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60FC7C47094 for ; Thu, 10 Jun 2021 06:52:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0A7BD613DE for ; Thu, 10 Jun 2021 06:52:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0A7BD613DE Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 942246B0036; Thu, 10 Jun 2021 02:52:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 919696B006E; Thu, 10 Jun 2021 02:52:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7922B6B0070; Thu, 10 Jun 2021 02:52:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0236.hostedemail.com [216.40.44.236]) by kanga.kvack.org (Postfix) with ESMTP id 45EEC6B0036 for ; Thu, 10 Jun 2021 02:52:41 -0400 (EDT) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id DB4AD5DDC for ; Thu, 10 Jun 2021 06:52:40 +0000 (UTC) X-FDA: 78236896080.13.10AAFEB Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) by imf07.hostedemail.com (Postfix) with ESMTP id 8EC66A000257 for ; Thu, 10 Jun 2021 06:52:35 +0000 (UTC) Received: by mail-qk1-f176.google.com with SMTP id k4so26376546qkd.0 for ; Wed, 09 Jun 2021 23:52:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=wPomTLe6CFftHojVSA5u1wexcpwAxzEQRGtDVYSW6+s=; b=iI++N3FVNIIBdaZPkAyQ282hUtC9qN+UvCMayG6voYHqIBNoZg9iTLOV8/yobWd0qD t5j/wpMpnNQpZX0fvnA0I7clVpIc0Z/D/eOqSRq2x5fAOxilbxDRaH47SQra+Jtl95gG 6rjZt9jmANiCu553oPYamuydpCiIBfDeCuo8I07jAOxVEpXLoQZkqbpatpVGTDDzKNwR NuozFU/z4okvWcVDq4DIwr2iTzt13KJqKW8qwosEz7s8Tvlf4bzBblR5CtO3Z3JEY8X9 ppgPbqauTyQUhoJ5eMr1mU226mQPTSf9ZJuVDwcpm2dS8Nwas4iL01nwwfDDfihW8bmm UFpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=wPomTLe6CFftHojVSA5u1wexcpwAxzEQRGtDVYSW6+s=; b=kE1a2dD1u1BAEEXEOWYIKxHCFmVixFzGVQLz3TXqXmBsiSZw21HLC2wYysXXcIIZ/c 2KM5JFvAGkUbq0BysZTFBxWLHJEPpnfsCFdWdLuq9SVKelCurYm0iOLZldAkgxl4aA0F DJUa1qr2RPhFq0CunKfxURMYnZwYpb+uQFd5mBnm9Coe/bqY6Mi//wa0zJ6n6m/D2D1+ 27PSmeahtbHKo1uCujyoneyQ2SQeBX0wK/mVvwbfG4tcHpKg05MqoZShNlrRiO9xfmku zp/Qg5ndvASs16GycKCYFS43lnlMHWzSCHFssaKo608VXfzfATynI6hlYOjZMxjYaJWm N+mg== X-Gm-Message-State: AOAM531DTd3uX+7D40Kxrl096MbpTnGj8PS5o6cR4i5uabA/hnZvhBYM SGLBf2WW7H26S0a/7rTIkjKG2w== X-Google-Smtp-Source: ABdhPJyQI0UMif8PlESv2eTJgfLB9PUenRlpUP2M0SClQAZIdQdIkhs67p1JmPDKCwhCQr3B8G8OiQ== X-Received: by 2002:a05:620a:e89:: with SMTP id w9mr3215130qkm.256.1623307959660; Wed, 09 Jun 2021 23:52:39 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id o16sm1635297qtw.10.2021.06.09.23.52.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 23:52:39 -0700 (PDT) Date: Wed, 9 Jun 2021 23:52:37 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.anvils To: Andrew Morton cc: Hugh Dickins , "Kirill A. Shutemov" , Yang Shi , Wang Yugui , Matthew Wilcox , Alistair Popple , Ralph Campbell , Zi Yan , Peter Xu , Will Deacon , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 10/11] mm/thp: fix page_vma_mapped_walk() if THP mapped by ptes In-Reply-To: <589b358c-febc-c88e-d4c2-7834b37fa7bf@google.com> Message-ID: References: <589b358c-febc-c88e-d4c2-7834b37fa7bf@google.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8EC66A000257 X-Stat-Signature: iidhhn7uqg8xoexi5w176ufib36c8713 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=iI++N3FV; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of hughd@google.com designates 209.85.222.176 as permitted sender) smtp.mailfrom=hughd@google.com X-HE-Tag: 1623307955-766748 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: Running certain tests with a DEBUG_VM kernel would crash within hours, on the total_mapcount BUG() in split_huge_page_to_list(), while trying to free up some memory by punching a hole in a shmem huge page: split's try_to_unmap() was unable to find all the mappings of the page (which, on a !DEBUG_VM kernel, would then keep the huge page pinned in memory). Crash dumps showed two tail pages of a shmem huge page remained mapped by pte: ptes in a non-huge-aligned vma of a gVisor process, at the end of a long unmapped range; and no page table had yet been allocated for the head of the huge page to be mapped into. Although designed to handle these odd misaligned huge-page-mapped-by-pte cases, page_vma_mapped_walk() falls short by returning false prematurely when !pmd_present or !pud_present or !p4d_present or !pgd_present: there are cases when a huge page may span the boundary, with ptes present in the next. Restructure page_vma_mapped_walk() as a loop to continue in these cases, while keeping its layout much as before. Add a step_forward() helper to advance pvmw->address across those boundaries: originally I tried to use mm's standard p?d_addr_end() macros, but hit the same crash 512 times less often: because of the way redundant levels are folded together, but folded differently in different configurations, it was just too difficult to use them correctly; and step_forward() is simpler anyway. Fixes: ace71a19cec5 ("mm: introduce page_vma_mapped_walk()") Signed-off-by: Hugh Dickins Cc: Acked-by: Kirill A. Shutemov --- mm/page_vma_mapped.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index f6839f536645..6eb2f1863506 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -116,6 +116,13 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw) return pfn_is_match(pvmw->page, pfn); } +static void step_forward(struct page_vma_mapped_walk *pvmw, unsigned long size) +{ + pvmw->address = (pvmw->address + size) & ~(size - 1); + if (!pvmw->address) + pvmw->address = ULONG_MAX; +} + /** * page_vma_mapped_walk - check if @pvmw->page is mapped in @pvmw->vma at * @pvmw->address @@ -183,16 +190,22 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) if (pvmw->pte) goto next_pte; restart: - { + do { pgd = pgd_offset(mm, pvmw->address); - if (!pgd_present(*pgd)) - return false; + if (!pgd_present(*pgd)) { + step_forward(pvmw, PGDIR_SIZE); + continue; + } p4d = p4d_offset(pgd, pvmw->address); - if (!p4d_present(*p4d)) - return false; + if (!p4d_present(*p4d)) { + step_forward(pvmw, P4D_SIZE); + continue; + } pud = pud_offset(p4d, pvmw->address); - if (!pud_present(*pud)) - return false; + if (!pud_present(*pud)) { + step_forward(pvmw, PUD_SIZE); + continue; + } pvmw->pmd = pmd_offset(pud, pvmw->address); /* @@ -240,7 +253,8 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) spin_unlock(ptl); } - return false; + step_forward(pvmw, PMD_SIZE); + continue; } if (!map_pte(pvmw)) goto next_pte; @@ -270,7 +284,9 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) spin_lock(pvmw->ptl); } goto this_pte; - } + } while (pvmw->address < end); + + return false; } /**