Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp25693pxj; Tue, 1 Jun 2021 14:12:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyLVWszvfgOIBFPLRx0MHnF+XL/jDqAYi7MwVOw4E0cuuDfmjfuKv4fy/K3AL+d2rt/8uO X-Received: by 2002:a92:7d07:: with SMTP id y7mr23325948ilc.68.1622581972469; Tue, 01 Jun 2021 14:12:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622581972; cv=none; d=google.com; s=arc-20160816; b=Z1ZMpr+TwlyRv28FPmKK9D7GP33yJv0wQ4zViBorJDw03L2d1bCB/NNSa1q6aBynCI HSDzkje604+aANhm4zjBqxai62MTqYqLkwoAQ6Uugt7juHbByd/WpMSvJZDxvxgXT7+Q t47M+v8y0FKSz6vI5kg0iUzwc42ARSQgTK45H43OvahSAKD22+OgjB1P+Ts1PO0LEo1f P5jKIa5QEYth6T5EfpvLG3lcgBDLUeRMUblXFaKFVdJncNVQpBYpYX1o7LqJjTniAqUp +af/tTu02ZJvgvN7V0PjLlCQ/dgsImCPbkqj7KFLpiMfm6Bmke8BOZkbwq9+YMsPzd6q HZ8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=Zbc201h4vA/zTsuSBg7jG5PuehkrxrykYzZRAEADCP8=; b=vaU+rqG58ThR6ZmzmJCzr0kOn9n2WBDyrNt0Gg3VEP0fJ0xt9imXk63/G4ZQGcROt/ hY7NWGwZgTNPHiEDjXkvLZBEa/ou0h1Hb3s1DBnScEyHcHHcSobrbu+Wgqxf1+kLJO3P uzBQk6pszVZ+Fis76ShAD4ShNohZn+pB24wlHrxA5Wf7JTPMLnbfD/KzS7R29p3r7DF3 xA5x+ngk1XPq9dscIOegz6FIVSwFxi7AAyUaWGFnzDiBetu+fPDjmcRX+WxTg4Qognad t288crWyrAbUhzKfnK3Mxsne3Nrnef4NoBUeS1bSNFoWzXnDaQavxEjcw5+P3NwjSjn6 uccQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fyn3XV5G; 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 p17si19595038jas.72.2021.06.01.14.12.31; Tue, 01 Jun 2021 14:12:52 -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=fyn3XV5G; 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 S234834AbhFAVJl (ORCPT + 99 others); Tue, 1 Jun 2021 17:09:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234513AbhFAVJk (ORCPT ); Tue, 1 Jun 2021 17:09:40 -0400 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CE34C061574 for ; Tue, 1 Jun 2021 14:07:57 -0700 (PDT) Received: by mail-qv1-xf2b.google.com with SMTP id u13so199926qvt.7 for ; Tue, 01 Jun 2021 14:07: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=Zbc201h4vA/zTsuSBg7jG5PuehkrxrykYzZRAEADCP8=; b=fyn3XV5Gn8AMOt/w9jFZQcTUUD+FCmCO4tT29BnKyp4u/dx5EwwqvgdPLw2XdqMp8s eQvzzPzAxwskLgzsdeECw1/GhJjq5o65hZ4LnxfMZpSsOz5y8V6jzgu7Qd/7ztxLcS5Q KlzagkjhvEAMUb/bCJllVGq8UV/61Ldtc/bXQnOHEfHJkxGFXayF8OJdqzEPdj/ePIUZ oa2iHZrsNZxGpwGMBAN9JpUQhv/ab7neSsILqOhh1WZP856AihV1pApvpJZctHhbsmTf 5sNvu43VsavBmYEXH0nKkKaZoizLvz3paYHgGjrm9VM5//rbUt36/sVtBInZsNpK728z WGRg== 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=Zbc201h4vA/zTsuSBg7jG5PuehkrxrykYzZRAEADCP8=; b=oe+eBtqjMvVhupnCMR85pm3Sp9Cc4K1hJHDBdmV/LX/iWi181pt5AX1y2lxCYOmZ9Y lO9PbrUdkX6FgTK+KJAEydkSlafkdp/NnjINVBRGJu/NOxM6HRr7DmK+0GoUBj0zdJj8 ENP+w0mlBWk+ik+hBenZwa+h6xGY4Z3YIp54VWHtHtfIifZfMyroF7xprtjdBC+xiqVQ VeoAOa67jqv1wohnBCb0zD8kwxVoGYrZON6uSXL9Cp4cJo5rPyKhJWdIW1FQHL41jchD d1fz1IRHxWBGRywVdLBQqnrYcmN6Ef2VYM8JVFfFA0qnujs1z+iEXNXzvExJN1b/vsgS bMcA== X-Gm-Message-State: AOAM530ZLAECbsB1YPhknl+hVIWn03GPXPN1QcArxGiPvPSm835TG+R0 /d4vwvBDfgYIeJw1I5emOyOtOw== X-Received: by 2002:ad4:5bef:: with SMTP id k15mr23795389qvc.22.1622581676150; Tue, 01 Jun 2021 14:07: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 w3sm300195qkb.13.2021.06.01.14.07.54 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Tue, 01 Jun 2021 14:07:55 -0700 (PDT) Date: Tue, 1 Jun 2021 14:07:53 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Hugh Dickins , "Kirill A. Shutemov" , Yang Shi , Wang Yugui , Matthew Wilcox , Naoya Horiguchi , Alistair Popple , Ralph Campbell , Zi Yan , Miaohe Lin , Minchan Kim , Jue Wang , Peter Xu , Jan Kara , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/7] mm/thp: try_to_unmap() use TTU_SYNC for safe DEBUG_VM splitting 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Stressing huge tmpfs often crashed on unmap_page()'s VM_BUG_ON_PAGE (!unmap_success): with dump_page() showing mapcount:1, but then its raw struct page output showing _mapcount ffffffff i.e. mapcount 0. And even if that particular VM_BUG_ON_PAGE(!unmap_success) is removed, it is immediately followed by a VM_BUG_ON_PAGE(compound_mapcount(head)), and further down an IS_ENABLED(CONFIG_DEBUG_VM) total_mapcount BUG(): all indicative of some mapcount difficulty in development here perhaps. But the !CONFIG_DEBUG_VM path handles the failures correctly and silently. I believe the problem is that once a racing unmap has cleared pte or pmd, try_to_unmap_one() may skip taking the page table lock, and emerge from try_to_unmap() before the racing task has reached decrementing mapcount. Instead of abandoning the unsafe VM_BUG_ON_PAGE(), and the ones that follow, use PVMW_SYNC in try_to_unmap_one() in this case: adding TTU_SYNC to the options, and passing that from unmap_page() when CONFIG_DEBUG_VM=y. It could be passed in the non-debug case too, but that would sometimes add a little overhead, whereas it's rare for this race to result in failure. mm/memory-failure.c:hwpoison_user_mappings() should probably use the new TTU_SYNC option too, just in case this race coincides with its attempts to unmap a failing page (THP or not); but this commit does not add that. Fixes: fec89c109f3a ("thp: rewrite freeze_page()/unfreeze_page() with generic rmap walkers") Signed-off-by: Hugh Dickins Cc: --- include/linux/rmap.h | 3 ++- mm/huge_memory.c | 4 ++++ mm/page_vma_mapped.c | 8 ++++++++ mm/rmap.c | 17 ++++++++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index def5c62c93b3..891599a4cb8d 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -97,7 +97,8 @@ enum ttu_flags { * do a final flush if necessary */ TTU_RMAP_LOCKED = 0x80, /* do not grab rmap lock: * caller holds it */ - TTU_SPLIT_FREEZE = 0x100, /* freeze pte under splitting thp */ + TTU_SPLIT_FREEZE = 0x100, /* freeze pte under splitting thp */ + TTU_SYNC = 0x200, /* avoid racy checks with PVMW_SYNC */ }; #ifdef CONFIG_MMU diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 9fb7b47da87e..305f709a7aca 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2357,6 +2357,10 @@ static void unmap_page(struct page *page) if (PageAnon(page)) ttu_flags |= TTU_SPLIT_FREEZE; + /* Make sure that the BUGs will not bite */ + if (IS_ENABLED(CONFIG_DEBUG_VM)) + ttu_flags |= TTU_SYNC; + unmap_success = try_to_unmap(page, ttu_flags); VM_BUG_ON_PAGE(!unmap_success, page); } diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index 2cf01d933f13..b45d22738b45 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -212,6 +212,14 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) pvmw->ptl = NULL; } } else if (!pmd_present(pmde)) { + /* + * If PVMW_SYNC, take and drop THP pmd lock so that we + * cannot return prematurely, while zap_huge_pmd() has + * cleared *pmd but not decremented compound_mapcount(). + */ + if ((pvmw->flags & PVMW_SYNC) && + PageTransCompound(pvmw->page)) + spin_unlock(pmd_lock(mm, pvmw->pmd)); return false; } if (!map_pte(pvmw)) diff --git a/mm/rmap.c b/mm/rmap.c index 693a610e181d..07811b4ae793 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1405,6 +1405,15 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; + /* + * When racing against e.g. zap_pte_range() on another cpu, + * in between its ptep_get_and_clear_full() and page_remove_rmap(), + * try_to_unmap() may return false when it is about to become true, + * if page table locking is skipped: use TTU_SYNC to wait for that. + */ + if (flags & TTU_SYNC) + pvmw.flags = PVMW_SYNC; + /* munlock has nothing to gain from examining un-locked vmas */ if ((flags & TTU_MUNLOCK) && !(vma->vm_flags & VM_LOCKED)) return true; @@ -1777,7 +1786,13 @@ bool try_to_unmap(struct page *page, enum ttu_flags flags) else rmap_walk(page, &rwc); - return !page_mapcount(page) ? true : false; + /* + * When racing against e.g. zap_pte_range() on another cpu, + * in between its ptep_get_and_clear_full() and page_remove_rmap(), + * try_to_unmap() may return false when it is about to become true, + * if page table locking is skipped: use TTU_SYNC to wait for that. + */ + return !page_mapcount(page); } /** -- 2.32.0.rc0.204.g9fa02ecfa5-goog