Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3769338pxv; Mon, 28 Jun 2021 12:21:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrgnOaPnNICCZPV08ily2cdtKu/tGGuY/x5jdjGlqA/pEWrMCzXRoUOT3ru6A64U+G25uW X-Received: by 2002:a17:906:2b18:: with SMTP id a24mr25944234ejg.239.1624908117024; Mon, 28 Jun 2021 12:21:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624908117; cv=none; d=google.com; s=arc-20160816; b=pogavemBt6f0k4L6qF2qYEPiQ54T0wYSZz1Qb929vmZSw34f0lHZdwzyIonFk23F0i 2SH7otqMYXkgwCvpq/cat321ez/1tz5apNLlcKYD8e0D2eK9+f4O47s8dxG+jNpT3wxm cp5HWmop62eJQaZRPhM0do08e+IrzxVZwYyOQlj2k9mNXxJbD5JXwtLJgaZ4UVnWoflP JxhiZ9+MAyo3dkPSLY1saSkUu3YTqGqtA30M945tqvuqMFSom6j2HPuaB7Ln7TwtEGTt Klo1ZsiKEs3MSZb4siOD/42bTElLoEb5hhGeS5OMfN1ukXiz3ZfvRy9MP2zx9O9PIySk Scsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NAqcfxOqNscaJQV96sR5jg1NPZyLsPevWN0PNKTZ12E=; b=LtAH9dI7QEqVIUu+THqUNP6gHFtBSc6jCQW7WDpHHDBA/aAyDXQvGpjrLPuoFqXl97 ThBF/CuKJP4v4dUnRS+TqPEcr7PiiGnJEoyHI+GrhLWl7/lJkZWDdi+oHD+gqh68goud 0Z+mMY5IZjzkN/aDDVeNLAxcXFAtCXD8qA8Z6wYljDl3p5Gfj0kFPftIPvisAEEEVLKO Z7ozArlqy9vBD43UhmTGlu9JfB/N/1zZQKExbsppVV1yZwXCnFfLtRcsMd06Qp6wDAd2 9OtfK98aT3VkhArd2WWBrMVmroEWnKYs4jUxhD2dF+9uYE715JF9Zat8/SXu3OozG7kF cOMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=k618i0iM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d13si795082edx.562.2021.06.28.12.21.31; Mon, 28 Jun 2021 12:21:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=k618i0iM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233535AbhF1Okj (ORCPT + 99 others); Mon, 28 Jun 2021 10:40:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:37322 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234417AbhF1Oco (ORCPT ); Mon, 28 Jun 2021 10:32:44 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9993E61CDB; Mon, 28 Jun 2021 14:27:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624890463; bh=fFLqZkmzPU1384Yvy/HxBzHvkpdskzmZQkNtHogiB/U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k618i0iMe/MXny3oeo+X9nrUOC4Zvr8yH+jWQhjtn95y0XIYKxsnGQZHHBF2WnURl cEd5KKBCiFGHWLQJk5lcroaZCsBDuSAX6eUJ8gY00L5KkHxgvUVneIA7gQawjwSCwn 1Xrvz6cEmJdn9er23z2x7mm0qZxGkF2r/MsSizcbH6gEDrSS67Xw/ExJNlhX0YxXTt HE4Yt371RrykYvDv50ODgpm6wqAAN7jLBJTSzJzYNl0x0YpFDWBFrYRmu+U+A1RtWL Q28YrOtCqhTAV1HFe/mMbw09aNy5qsVfFbnnbD9xnO+KGucPSvx11nNFKtdhybkIEE zdPQpwviOGhbw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Hugh Dickins , "Kirill A . Shutemov" , Alistair Popple , Matthew Wilcox , Peter Xu , Ralph Campbell , Wang Yugui , Will Deacon , Yang Shi , Zi Yan , Andrew Morton , Linus Torvalds , Greg Kroah-Hartman Subject: [PATCH 5.10 090/101] mm/thp: fix page_vma_mapped_walk() if THP mapped by ptes Date: Mon, 28 Jun 2021 10:25:56 -0400 Message-Id: <20210628142607.32218-91-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210628142607.32218-1-sashal@kernel.org> References: <20210628142607.32218-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.10.47-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.10.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.10.47-rc1 X-KernelTest-Deadline: 2021-06-30T14:25+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hugh Dickins commit a9a7504d9beaf395481faa91e70e2fd08f7a3dde upstream. 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. Link: https://lkml.kernel.org/r/fedb8632-1798-de42-f39e-873551d5bc81@google.com Fixes: ace71a19cec5 ("mm: introduce page_vma_mapped_walk()") Signed-off-by: Hugh Dickins Acked-by: Kirill A. Shutemov Cc: Alistair Popple Cc: Matthew Wilcox Cc: Peter Xu Cc: Ralph Campbell Cc: Wang Yugui Cc: Will Deacon Cc: Yang Shi Cc: Zi Yan Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- 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 5df88f3b454c..2ad76a3d871d 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -115,6 +115,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 @@ -182,16 +189,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); /* @@ -238,7 +251,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; @@ -268,7 +282,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; } /** -- 2.30.2