From patchwork Sun Aug 2 19:16:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 11696845 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 56AFE138A for ; Sun, 2 Aug 2020 19:16:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 234D320792 for ; Sun, 2 Aug 2020 19:16:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="skZxXq54" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 234D320792 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 76F1C8D00CD; Sun, 2 Aug 2020 15:16:58 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6F82E8D00AA; Sun, 2 Aug 2020 15:16:58 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C0658D00CD; Sun, 2 Aug 2020 15:16:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0051.hostedemail.com [216.40.44.51]) by kanga.kvack.org (Postfix) with ESMTP id 402F48D00AA for ; Sun, 2 Aug 2020 15:16:58 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id F2C4D8248047 for ; Sun, 2 Aug 2020 19:16:57 +0000 (UTC) X-FDA: 77106586074.12.cloth20_620d87826f97 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id C9C5D18004A02 for ; Sun, 2 Aug 2020 19:16:57 +0000 (UTC) X-Spam-Summary: 1,0,0,f6d596910ba952fd,d41d8cd98f00b204,hughd@google.com,,RULES_HIT:41:69:355:379:800:960:966:973:988:989:1260:1277:1313:1314:1345:1359:1437:1516:1518:1535:1542:1593:1594:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2731:2898:3138:3139:3140:3141:3142:3152:3354:3865:3866:3867:3868:3870:3871:3872:3874:4117:4321:4385:5007:6119:6261:6653:7903:10004:10400:11026:11473:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12679:12740:12895:12986:13161:13229:13439:13870:14096:14097:14181:14394:14659:14721:21067:21080:21433:21444:21451:21611:21627:21939:30054:30070,0,RBL:209.85.160.196:@google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yf9iw8ex3g7ispa54xf7gk3mfswopq3emzgjf971nu81e6s7myp5gt3r8tsxw.791zxpasxh8c3yp57h3pc3k7s1d1oebgshzd8tnmp1k9umuf4cnfsmimtzscefq.a-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:n one X-HE-Tag: cloth20_620d87826f97 X-Filterd-Recvd-Size: 6017 Received: from mail-qt1-f196.google.com (mail-qt1-f196.google.com [209.85.160.196]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Sun, 2 Aug 2020 19:16:57 +0000 (UTC) Received: by mail-qt1-f196.google.com with SMTP id x12so18288258qtp.1 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=NG7ESgIJmtojX4pHW4TaPiHeeJCdHbVRt5cOGlifXmdtOdL7+OJUIt1m0It9TtfEa0 VkZkZHP3ENiawMMw+aEoygKBoGfjqmyqDD6B7u87/cYWJnIZYx4HC7ly8YFx/qV27Hc1 gdk/DrkMiudeU7B8/9ka23Dg4sd8h1qsF868+RnMkcyPTiNOdjWtcxECCxQrtjLDNOxJ xvWMofuijZDDCMsP8//SbHrFuhiCOlPMFRh9R2jiInQpTsvYPq8pX6tSu6AmL3Rh13TC p0m4TNorl/90qttMNjAo038ExJYq6S9uNK1tB4I1sqoVDJf/v0rO0NhB0T1+rlA/sMAX E4ow== X-Gm-Message-State: AOAM5313w/tPjL6vXP68MECKDA5SUPihjUXEArx9G8rSSR6vpU1ovgNB iR7VJVpzARhNx5zGGXbJ8NiyRA== X-Google-Smtp-Source: ABdhPJwfwVY08GwUjd/rgvaHaGv+O8c3N9g5P+qpYC1MQ4+3Mj1k9RTVsD1nA95F1RjtX3WwvID/YA== 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 X-Rspamd-Queue-Id: C9C5D18004A02 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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: 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+ Acked-by: Kirill A. Shutemov --- 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);