Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1569926pxa; Sun, 2 Aug 2020 12:17:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyCadb7DQmUQF6Af8/YVRGEizdzbcEq2PdvmMoiNJmJT5DgI/CmL8uusD3EWyklWioTY7h X-Received: by 2002:a50:d9cb:: with SMTP id x11mr12436289edj.93.1596395871655; Sun, 02 Aug 2020 12:17:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596395871; cv=none; d=google.com; s=arc-20160816; b=aw4ESRHJNssOHjffyY2fojF9YD4IqIgug2kquiVXpGlt2vUmvNgEuuu8ryhORpbU3R pQLTnoXYKPtE4JhdCVaHZ+5SYOAH6War+1xH89w5cI4VTNh2XiZnfBUIanZdJScWjFpK 99hZv0IML0i5lRboRAptJaJiqMY8ruKekyoximmO2UKRgkNJMyXosaQB/Acn7bIVy/+M 94p7fsePdKJ3h1juO6gOK/GSprHvrn78YmEzIK/ugoWyYDaWQ9Fy7JAn1KPJMyYvbaX3 0wS3RcdYV0aL6PkvLYPw8/XLBSMOAGGcKusuca4MF3++AG/Ahw5zaJKyMiwu9VF0lYVe JMOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature; bh=wynZ9zIDAE/hK/2qRtBgXqgR/4DY/aRh94k5GpXzQlM=; b=z+OWFVmVU7Wk30w1V+65gyW/IF1kEps3SLUbHSNFkzGHoWN0JPpYiN7pBKdqHY13G1 wFoG2f5YigZY/+pr/tSfD8V/pIAQZN5hKmVunJKrV2O6XVZeQzWO7N+iD75z6vv8cJIV ymyM3kHVqTKIMt/vbyu5DuMlDA6/yWv/gi1ijCNkdi1uLN1BSEGTevXcZeK2feoszmEG YxgEzpGsW23NYzKhUibCqWXRrXyKhzSw7wuFoGVDAoSHM+QZUoBR07RFdZUjNTtBS5H2 6kqwr/6usp71yab6ndfxxBIIREeTkvwR2yUWdniNXLcEj1WKxqjV/SodgXQAb7it4jzY kTEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=skZxXq54; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e10si9643057edy.274.2020.08.02.12.17.29; Sun, 02 Aug 2020 12:17:51 -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=@google.com header.s=20161025 header.b=skZxXq54; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726913AbgHBTQ6 (ORCPT + 99 others); Sun, 2 Aug 2020 15:16:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725925AbgHBTQ5 (ORCPT ); Sun, 2 Aug 2020 15:16:57 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF9B6C06174A for ; Sun, 2 Aug 2020 12:16:57 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id s16so26784308qtn.7 for ; Sun, 02 Aug 2020 12:16:57 -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 :user-agent:mime-version; bh=wynZ9zIDAE/hK/2qRtBgXqgR/4DY/aRh94k5GpXzQlM=; b=skZxXq54gBYMDReRoAFvzjlJQMgazo+he7aVO9uXOp+HJWgWqTcPzm4esfVXrKoe6B T2NOVM98IlcbT2rqdb4muE+pylAfQHa6W7AbnQezwLn/4UBSgc28Fq05wM0H3WuJavEB N1EGdjBVUZI+6+GOQJHWHNm6OHKF7PAVIH7/qkmqWXL2rFVqRm/98nFd9RGCUDvqHPiX tLbhejfnu6UB0D9J37sOISArT/ig0SrMk4OkSVRaT/+t2PmwObr46QWiPPomBi1M0tCD eGkxRuqY4RRm0HjbXdCc+nFOWEQecxYaQumPhK/Q4I5g0DZTaX/D3cf5H3yf3ARP88Ns 9y9g== 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:user-agent:mime-version; bh=wynZ9zIDAE/hK/2qRtBgXqgR/4DY/aRh94k5GpXzQlM=; b=QgOqjg8ZHnAWIM3k6tSvbcjQXb4EZ/fFDBhnhbP/6WS/rCjGj6Q2DDqYMqf+LrldJc GBsZWQzEGMhcZ599eKLhBwR3wrWlxKBcXA0mAlDEGoZMKoBDz9zK52XE2GU8V4P2rDtB Tz8uBykm3rRzSXrlLnoL1oHfDsY65+OI8OCi9BsK9qHG3xJ9egXqFu/8hK0POtg2v8H7 WzAtu/qHxfobvL2ovyUwDX0zVM9W/EEzXsFcjjes0651fTx5sUO2AfckUITpjfPLkDzz nGG3RZAnJAK0fLtrQvjSueT2KASbnFpSgd7fCGBd75r5Puph6Ll6VrM5/9/VoHpc2YEv s0Cw== X-Gm-Message-State: AOAM532E5xqdw7ANoU9bCGN0BIMO7glBBs2uAktqj1YugBXkyBBwzISR ytTWuRlv+KYQldJZevo5q/qufg== X-Received: by 2002:ac8:450c:: with SMTP id q12mr13267601qtn.292.1596395816642; Sun, 02 Aug 2020 12:16:56 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id m3sm17486113qka.31.2020.08.02.12.16.54 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Sun, 02 Aug 2020 12:16:55 -0700 (PDT) Date: Sun, 2 Aug 2020 12:16:53 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: "Kirill A. Shutemov" , Andrea Arcangeli , Song Liu , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] khugepaged: retract_page_tables() remember to test exit In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Only once have I seen this scenario (and forgot even to notice what forced the eventual crash): a sequence of "BUG: Bad page map" alerts from vm_normal_page(), from zap_pte_range() servicing exit_mmap(); pmd:00000000, pte values corresponding to data in physical page 0. The pte mappings being zapped in this case were supposed to be from a huge page of ext4 text (but could as well have been shmem): my belief is that it was racing with collapse_file()'s retract_page_tables(), found *pmd pointing to a page table, locked it, but *pmd had become 0 by the time start_pte was decided. In most cases, that possibility is excluded by holding mmap lock; but exit_mmap() proceeds without mmap lock. Most of what's run by khugepaged checks khugepaged_test_exit() after acquiring mmap lock: khugepaged_collapse_pte_mapped_thps() and hugepage_vma_revalidate() do so, for example. But retract_page_tables() did not: fix that (using an mm variable instead of vma->vm_mm repeatedly). Fixes: f3f0e1d2150b ("khugepaged: add support of collapse for tmpfs/shmem pages") Signed-off-by: Hugh Dickins Cc: stable@vger.kernel.org # v4.8+ --- mm/khugepaged.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) --- 5.8-rc7/mm/khugepaged.c 2020-07-26 16:58:02.189038680 -0700 +++ linux/mm/khugepaged.c 2020-08-02 10:53:37.892660983 -0700 @@ -1538,6 +1538,7 @@ out: static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) { struct vm_area_struct *vma; + struct mm_struct *mm; unsigned long addr; pmd_t *pmd, _pmd; @@ -1566,7 +1567,8 @@ static void retract_page_tables(struct a continue; if (vma->vm_end < addr + HPAGE_PMD_SIZE) continue; - pmd = mm_find_pmd(vma->vm_mm, addr); + mm = vma->vm_mm; + pmd = mm_find_pmd(mm, addr); if (!pmd) continue; /* @@ -1576,17 +1578,19 @@ static void retract_page_tables(struct a * mmap_lock while holding page lock. Fault path does it in * reverse order. Trylock is a way to avoid deadlock. */ - if (mmap_write_trylock(vma->vm_mm)) { - spinlock_t *ptl = pmd_lock(vma->vm_mm, pmd); - /* assume page table is clear */ - _pmd = pmdp_collapse_flush(vma, addr, pmd); - spin_unlock(ptl); - mmap_write_unlock(vma->vm_mm); - mm_dec_nr_ptes(vma->vm_mm); - pte_free(vma->vm_mm, pmd_pgtable(_pmd)); + if (mmap_write_trylock(mm)) { + if (!khugepaged_test_exit(mm)) { + spinlock_t *ptl = pmd_lock(mm, pmd); + /* assume page table is clear */ + _pmd = pmdp_collapse_flush(vma, addr, pmd); + spin_unlock(ptl); + mm_dec_nr_ptes(mm); + pte_free(mm, pmd_pgtable(_pmd)); + } + mmap_write_unlock(mm); } else { /* Try again later */ - khugepaged_add_pte_mapped_thp(vma->vm_mm, addr); + khugepaged_add_pte_mapped_thp(mm, addr); } } i_mmap_unlock_write(mapping);