Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2640766lqp; Mon, 25 Mar 2024 05:25:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhahcU8mYGyu2c78IdFbAmihtkdTfZcwqDwvjIIU/SAkEa1sS753LXG04YBJQtDkmYCmLrbnGBfxwOxRwESIVU5Dps32fWkxLYmBcmlQ== X-Google-Smtp-Source: AGHT+IEq84+HXFFfQDkW29rU7z+A5otpxtZoCmMdDrqrjWlEy9H1h9ti13ZrUMIV4VYphmK1qjtQ X-Received: by 2002:a50:955a:0:b0:568:c608:8058 with SMTP id v26-20020a50955a000000b00568c6088058mr4866942eda.17.1711369522946; Mon, 25 Mar 2024 05:25:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711369522; cv=pass; d=google.com; s=arc-20160816; b=byQNGAw8l5JX/8PV58Myij2ZYCZ6s9fwRJJwd1kfX/R59rkvK0y/HWgQB4u1ziqQ3Q g97Swx5bP4UdLO9D3TATnxIpJBDlZnEILqqgiWmgGYA1wU8xvi7fczB29tDxA2cY0Co9 W1QiR+shebLCHXmr/0yg7XnLsFtKP18K1+xr2OX50ogk6m2Jvvi5XhDAw8gJd4+GfXDk AkN/VUgE6FRh0W3QwKdvfOPXcO3ZlKTvCF5i2V00+dC732pPtWrFNF54dDk/Opslk7on q2DmEjh+MYW1vtc0AcGgTV072Izi94cA3L6Giay05W8AsTZpb5tCqoQX6WazC9vMOUOH /R6A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=iqc52fNCJobqUzERbW40WnlDTkGcB7WwGc1zVlyZKvQ=; fh=oHVwDuUn2G0q/nBHwBMg+/uC92NaPvoReDtlYdC95QM=; b=YyHJS2ZMH/r45+pQTv2A4MCpDaG2D8XuGEwuRX6KK0Akld/qf/+bNOPVNbtSj3BGT5 EtPQ4FczC+rdxccvHdolClyDfkcnwYukGfDbWWoaAPPSE2wyF2Z8iBl/OfTE3S3ommyA xGmV0B9aHqE6qPLRl8zRSxYTa48UoluP6cn3G+KlWbC4q+VZfnzNL1ImRDXsj6wOcMZq zHDNk5Babvq+FiH7+3Z89OWxWeUziRg8FnaKSMKsE8Yxh1HuKk6QU5l7j97mvrr3h44g LViPa9lwXWNzf0DXiLfKtOndazSmpLtzheoD5v/x4LDM4iLQkv81gRTdZo3KOPxl7Zrc xp2g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=TOkzjnL6; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-116456-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116456-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a16-20020a05640213d000b0056c0a21e4dbsi1255500edx.18.2024.03.25.05.25.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 05:25:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-116456-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=TOkzjnL6; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-116456-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116456-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A83971F38625 for ; Mon, 25 Mar 2024 12:25:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A09715B0EB; Mon, 25 Mar 2024 07:31:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="TOkzjnL6" Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C0661311AA for ; Mon, 25 Mar 2024 03:22:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711336960; cv=none; b=iJhcF5rMPVVq8OVsc9T0Zg/3EvMJph0b9EGrG8MxVm8lKh6YgLHCSze3tlytbFENscfVeGOxuyNi5fUXc+K5Y4G3ctKhupLtTevvYhnMtCl/y2FH7MIwaqW3IgwLAXD5P78j/3/mLASfqJu0c6lKHXbiSRgKSNPitGmoAFcJxqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711336960; c=relaxed/simple; bh=i80VoZMqBoYXLl9VuHTIymeFBZ6nqwDeIFzNjjVljJA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rZTTmgxEFyv7FXsobNBvtmeCbFae2g733muSS1MpUvdjYiRmjrTYFGdtZLY6gZRE3fZkkavwTdXoIFbkM8M/aDk6L8rmfwU1QCGKutgrQvJ3V+v1tv7462wIptEYRxNT7XHUfDbRGareZIHj2J9e2UxzOSZvVCXWcIxwkUwfYhw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=TOkzjnL6; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=iqc52fNCJobqUzERbW40WnlDTkGcB7WwGc1zVlyZKvQ=; b=TOkzjnL6BUv7T92lJO5XntAlUY KsDq4bgdcc08zQCTrImHMntACEY4Wg1uzkw4ZyBude71X+tFe9pNWd5tIewIriDl/MPkVDG9FQ8KF kEwc42NZDXGkM+Yizn5h6IZ87AIt61Y9lNeSJ7IvwF83GoGrNK4UL10MSISSo0hl2k9M6U3WcMaKD pi+cF81QoD63cqjDpj5x3KC5YVi343PdsYXlSlbObk1SSnOIlM21k4OzDtCrmTxxLS1+1jlZZa0CR 3u4JcxK8uA5e6qPgVIA39JEAVlsHNHgdSn6PGQENQRSi5b4hlC289YznYRMQcjWtueOjqB8f5VnRy SHA6Dccg==; Received: from willy by casper.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1roaut-0000000FYNr-0Ljl; Mon, 25 Mar 2024 03:22:31 +0000 Date: Mon, 25 Mar 2024 03:22:30 +0000 From: Matthew Wilcox To: Zhaoyang Huang Cc: =?utf-8?B?6buE5pyd6ZizIChaaGFveWFuZyBIdWFuZyk=?= , Andrew Morton , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , =?utf-8?B?5bq357qq5ruoIChTdGV2ZSBLYW5nKQ==?= Subject: Re: summarize all information again at bottom//reply: reply: [PATCH] mm: fix a race scenario in folio_isolate_lru Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Sun, Mar 24, 2024 at 07:14:27PM +0800, Zhaoyang Huang wrote: > ok. It seems like madvise is robust enough to leave no BUGs. I presume > another two scenarios which call folio_isloate_lru by any other ways > but PTE. Besides, scenario 2 reminds me of a previous bug reported by > me as find_get_entry entered in a livelock where the folio's refcnt == > 0 but remains at xarray which causes the reset->retry loops forever. I > would like to reply in that thread for more details. > > Scenario 1: > 0. Thread_bad gets the folio by find_get_entry and preempted before > folio_lock (could be the second round scan of > truncate_inode_pages_range) > refcnt == 2(page_cache, fbatch_bad), PG_lru == true, PG_lock == false > folio = find_get_entry > folio_try_get_rcu > > folio_try_lock > > 1. Thread_truncate get the folio via > truncate_inode_pages_range->find_lock_entries > refcnt == 3(page_cache, fbatch_bad, fbatch_truncate), PG_lru == > true, PG_lock == true Hang on, you can't have two threads in truncate_inode_pages_range() at the same time. I appreciate that we don't have any documentation of that, but if it were possible, we'd see other crashes. Removing the folio from the page cache sets folio->mapping to NULL. And __filemap_remove_folio() uses folio->mapping in filemap_unaccount_folio() and page_cache_delete(), so we'd get NULL pointer dereferences. I see a hint in the DAX code that it's an fs-dependent lock: /* * This gets called from truncate / punch_hole path. As such, the caller * must hold locks protecting against concurrent modifications of the * page cache (usually fs-private i_mmap_sem for writing). Since the * caller has seen a DAX entry for this index, we better find it * at that index as well... */ so maybe that's why there's no lockdep_assert() in truncate_inode_pages_range(), but there should be a comment. > Scenario 2: > 0. Thread_bad gets the folio by find_get_entry and preempted before > folio_lock (could be the second round scan of > truncate_inode_pages_range) > refcnt == 2(page_cache, fbatch_bad), PG_lru == true, PG_lock == false > folio = find_get_entry > folio_try_get_rcu > > folio_try_lock > > 1. Thread_readahead remove the folio from page cache and drop one > refcnt by filemap_remove_folio(get rid of the folios which failed to > launch IO during readahead) > refcnt == 1(fbatch_bad), PG_lru == true, PG_lock == true So readaahead inserts the folio locked, and then calls filemap_remove_folio() without having unlocked it. filemap_remove_folio() sets folio->mapping to NULL in page_cache_delete(). When "Thread_bad" wakes up, it gets the folio lock, calls truncate_inode_folio() and sees that folio->mapping != mapping, so it doesn't call filemap_remove_folio(). > 4. Thread_bad schedule back from step 0 and clear one refcnt wrongly > when doing truncate_inode_folio->filemap_remove_folio as it take this > refcnt as the page cache one > refcnt == 1'(thread_isolate), PG_lru == false, PG_lock == false > find_get_entries > folio = find_get_entry > folio_try_get_rcu > folio_lock > mapping != mapping as folio_lock_entries does> > truncate_inode_folio > filemap_remove_folio >