Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1256649pxj; Fri, 4 Jun 2021 09:42:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwtYA1rVq1bYlHCyb+lcIJ3PYUm50gVBHrOct2ri94G9OZw7KU2XRx4Y+/vc5Vmg4cDwNP0 X-Received: by 2002:a17:906:1dc2:: with SMTP id v2mr5138593ejh.8.1622824948632; Fri, 04 Jun 2021 09:42:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622824948; cv=none; d=google.com; s=arc-20160816; b=oLkAKn4VQVTfph/5KF97G83DHzRmHIE5kaShuKdTw+8U4w54JvFpr38amuD+MEpCcy aBUu5wY7JeI4Ltgp5Bmj6iogd+6zJJnyzNfwOsJzQHA2+5tRgz/RsWH1U7eRTLK1Lcdp HPkEVHOruMpApQdCyYnjfsl6xLuvaNak5tzUTYQNxAMhgNSGEP354/lL7gMBhT0Kmlri zjJY1Qz1IuUN9abD8hJv2skdZsb9IWN2jGKALlfhmu9fgzQ0wBnJrTK27k34IcHc6gKs FS7zaAF03WZ6iGPM4aHIl9oBSKPeH+Fa0poSysxPlsRk9QYlf8qp7cPhLf9U5HU7D6Q5 O49A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=lH2Y44otqOO8m2Z4QXChzT2bnP+V+GekPu5UvhOWowA=; b=osV361ZMLn3EcB/O3n2nOJDXRybdE7eyTzGtTD1C0Yg5Dddws7z1XXiwC4hRCQQmGW Ng5sGt2V5Uf60L9Uv8npvW4+mUgquYDMncHt///pSYcIb5ujK8iFbD15ZpblcDIHNWhb wjoqdvA5iyf/wdUGfFnHvSg6SaB4XgAS1Kl/Iz0UFYtIz7OaEP5YOidOTiEmtXCTZN3G F95rlrjqfsMDBR0CzvVHlAN7eFHrCuQaAJlCB6bKIbgg/RCmzrdnvwemRCbKBzm2Wl2c qbM5zl3i4+BCDa0qZbF/xQQqYgQsnKAK+XcfM64idQWkgvA/bxJjTpFvULpmofEaPm75 PSDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=WcXs5AdB; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v24si5811470edy.327.2021.06.04.09.42.04; Fri, 04 Jun 2021 09:42:28 -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=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=WcXs5AdB; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229892AbhFDQmU (ORCPT + 99 others); Fri, 4 Jun 2021 12:42:20 -0400 Received: from mail-lf1-f47.google.com ([209.85.167.47]:44554 "EHLO mail-lf1-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229690AbhFDQmT (ORCPT ); Fri, 4 Jun 2021 12:42:19 -0400 Received: by mail-lf1-f47.google.com with SMTP id r198so11654989lff.11 for ; Fri, 04 Jun 2021 09:40:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=lH2Y44otqOO8m2Z4QXChzT2bnP+V+GekPu5UvhOWowA=; b=WcXs5AdBjkF+CE0fh3kikx+81uypOq9k4xyT49W67A4aj6YLiccUUyGe2cW7L4CptT paEfhEFGq8IfuxmcFkl6CWwLrk9CUMuVjhg5gEaZfTGTOBplAkuzAf2ACr5O6hOlvc5s uTfYHX3rfu2RjRJaluTtDcatJfoZyWlyeFGiIHu7ovuvgco4thoSK+15kExrMLBP7wn2 HZJEAC1D0uUPzTRucDMCir3GdbHN4eFTT4nHNg1VTPjE8yA/anj5ym/P24HNiKpH2l9P 1pedReMy0XjBPXXiAlsvXpnrVw4SgTBjQVp+nLnX+pwMlMU2pzo23uFkDTmnOrWVB6Tc 4Siw== 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:message-id:references :mime-version:content-disposition:in-reply-to; bh=lH2Y44otqOO8m2Z4QXChzT2bnP+V+GekPu5UvhOWowA=; b=FU81a2p7x9+iPViQqse5Q1NovBxifUT+SqpVIIZJc2QqI2/f9d/pRVSgpVnLvWbMjv WJwpq632i+wf+aeYcePdHt7yxSVR6YaSKY73ydbdui7RpgywOrjiIh2ERIfnr5BA5gCN bIj40t0md3kVSxrHLqgUau9G9qQBsHamBAZZi6sCrWhismuC8/hm2E4JeQCA7S3dluC3 lmn1aF0f+r3QHgNAolVeDNUPIa8wuPU7zldFc+/De4sVcKXmdqsOZBWhppn76xfbTU8D D/gKmb4hIFzjkOYq1Lqq3BjXiB6tQcAkYg30BgvkDQTDbuPpbxNgde+rHKbT3gq+p/jJ T4zA== X-Gm-Message-State: AOAM532vmg3zjiy5fOk/vwkGVdwL0ey9S5HrPQzlngSCIqNEOQBuyr8X jtr+sfprMg0h2X04nEmrAgCndA== X-Received: by 2002:a19:4959:: with SMTP id l25mr3176253lfj.225.1622824762056; Fri, 04 Jun 2021 09:39:22 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id z6sm277482lfs.64.2021.06.04.09.39.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Jun 2021 09:39:21 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id 9AB471027A9; Fri, 4 Jun 2021 19:39:33 +0300 (+03) Date: Fri, 4 Jun 2021 19:39:33 +0300 From: "Kirill A. Shutemov" To: Hugh Dickins Cc: Andrew Morton , "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: Re: [PATCH 6/7] mm/thp: unmap_mapping_page() to fix THP truncate_cleanup_page() Message-ID: <20210604163933.h6dj6cgr6tudpprd@box.shutemov.name> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 01, 2021 at 02:15:35PM -0700, Hugh Dickins wrote: > There is a race between THP unmapping and truncation, when truncate sees > pmd_none() and skips the entry, after munmap's zap_huge_pmd() cleared it, > but before its page_remove_rmap() gets to decrement compound_mapcount: > generating false "BUG: Bad page cache" reports that the page is still > mapped when deleted. This commit fixes that, but not in the way I hoped. > > The first attempt used try_to_unmap(page, TTU_SYNC|TTU_IGNORE_MLOCK) > instead of unmap_mapping_range() in truncate_cleanup_page(): it has often > been an annoyance that we usually call unmap_mapping_range() with no pages > locked, but there apply it to a single locked page. try_to_unmap() looks > more suitable for a single locked page. > > However, try_to_unmap_one() contains a VM_BUG_ON_PAGE(!pvmw.pte,page): > it is used to insert THP migration entries, but not used to unmap THPs. > Copy zap_huge_pmd() and add THP handling now? Perhaps, but their TLB > needs are different, I'm too ignorant of the DAX cases, and couldn't > decide how far to go for anon+swap. Set that aside. > > The second attempt took a different tack: make no change in truncate.c, > but modify zap_huge_pmd() to insert an invalidated huge pmd instead of > clearing it initially, then pmd_clear() between page_remove_rmap() and > unlocking at the end. Nice. But powerpc blows that approach out of the > water, with its serialize_against_pte_lookup(), and interesting pgtable > usage. It would need serious help to get working on powerpc (with a > minor optimization issue on s390 too). Set that aside. > > Just add an "if (page_mapped(page)) synchronize_rcu();" or other such > delay, after unmapping in truncate_cleanup_page()? Perhaps, but though > that's likely to reduce or eliminate the number of incidents, it would > give less assurance of whether we had identified the problem correctly. > > This successful iteration introduces "unmap_mapping_page(page)" instead > of try_to_unmap(), and goes the usual unmap_mapping_range_tree() route, > with an addition to details. Then zap_pmd_range() watches for this case, > and does spin_unlock(pmd_lock) if so - just like page_vma_mapped_walk() > now does in the PVMW_SYNC case. Not pretty, but safe. > > Note that unmap_mapping_page() is doing a VM_BUG_ON(!PageLocked) to > assert its interface; but currently that's only used to make sure that > page->mapping is stable, and zap_pmd_range() doesn't care if the page is > locked or not. Along these lines, in invalidate_inode_pages2_range() > move the initial unmap_mapping_range() out from under page lock, before > then calling unmap_mapping_page() under page lock if still mapped. > > Fixes: fc127da085c2 ("truncate: handle file thp") > Signed-off-by: Hugh Dickins > Cc: I think adding support for THP in try_to_unmap_one() is the most future-proof way. We would need it there eventually. But this works too: Acked-by: Kirill A. Shutemov -- Kirill A. Shutemov