From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E799EEB64D9 for ; Fri, 7 Jul 2023 19:23:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6EBA58D0001; Fri, 7 Jul 2023 15:23:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 69C1F6B0078; Fri, 7 Jul 2023 15:23:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 564928D0001; Fri, 7 Jul 2023 15:23:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 476646B0075 for ; Fri, 7 Jul 2023 15:23:09 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 13EE8A069F for ; Fri, 7 Jul 2023 19:23:09 +0000 (UTC) X-FDA: 80985788898.18.CB61174 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf02.hostedemail.com (Postfix) with ESMTP id 1DFE780013 for ; Fri, 7 Jul 2023 19:23:06 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tgLCFacD; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688757787; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bVzwqDCXswNVqFcgL0ALQEMEJDUQGqbFL2Bn7HygaSU=; b=KfWL8xbAhnknY9QrLePW9mR9gW7N9a+VjBhp4FMbRoARlnY+7BO2pzAFAUQJ6nxiTRnqZ1 zQQn/4Jb+JnImHQkys1slqjO2icMDO05fKSGlR+ow45eCJMYt15/6Ssxp77jVkUBuu6UZv 7LEbkohbaaZeKbPdoKfDN9aWYLKBZKc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tgLCFacD; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688757787; a=rsa-sha256; cv=none; b=0rKpm5+e/c90/OGEU7zbUxI8lNnLpg6RrYrEY8cqxsslbfHmg1o7X/DXvXXB2eo86/vzBZ VIL6MGT7s0Ee2NDy+w6KrowchkmoaydShGcz4E+quW4+tzDS3EEfaVLAo/CmwC2frDfwsq 3p61tHI4UtGY5eApPrNybxFNQGthk1E= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B601A61A34; Fri, 7 Jul 2023 19:23:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63501C433C7; Fri, 7 Jul 2023 19:23:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1688757785; bh=94A738552oPcNLUcdadUB8TIEGBu7Z7b4zP4i/CSZhk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tgLCFacDRH2vFBvGsS1BdApLvk5sLS7WaSV2sgoXnSapnBONfvmv9X8eKnvugMJHE jqjgRwSKqOqTX8fmIldEGKg0KhXTvE0Bema9AGOztxJ3On1k5IAODVtjRLYQSXxP2D t/ZQ+Qpmi+iWBisfwT5UB4sgtSMbE1mZirUi9Sv4DwscQ0HrO92bVw63QK67r9cxrQ QxbM6U35zant3vdXmjw3fsF5Ss+czDaGBBWJvUTejeoPPj3ruozwzQAv9CaKvhOlKV 2nM3B31uev1RK+ZATSoyzNzgygragekkgVMBHp/70Fr46Mi/QGbs3iEtE2igexxk6o AYNbqLhudXizg== From: SeongJae Park To: David Wysochanski Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>, David Howells , Andrew Morton , Matthew Wilcox , Linus Torvalds , Jeff Layton , Christoph Hellwig , linux-afs@lists.infradead.org, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs-developer@lists.sourceforge.net, linux-erofs@lists.ozlabs.org, linux-ext4@vger.kernel.org, linux-cachefs@redhat.com, linux-fsdevel@vger.kernel.org, Rohith Surabattula , Steve French , Shyam Prasad N , Dominique Martinet , Ilya Dryomov , linux-mm@kvack.org, Daire Byrne , SeongJae Park Subject: Re: [BUG mm-unstable] BUG: KASAN: use-after-free in shrink_folio_list+0x9f4/0x1ae0 Date: Fri, 7 Jul 2023 19:23:01 +0000 Message-Id: <20230707192301.27308-1-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 1DFE780013 X-Stat-Signature: 9uxnddc97nixf6zhk54d73hcm9uscp8s X-Rspam-User: X-HE-Tag: 1688757786-165746 X-HE-Meta: U2FsdGVkX1+h9G6s35anM/xNCQGUUo7pUpjJ/wDpPP9sTBkhWEWpb2GMfRJT6RiNQKSJJF2DwzBKORRCLVdtPYDA5XFUcb4tkNFVsDBIMzTeHQTWY1czChrkNbS/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r1vO3JAs0D+LwdKlmHiW6G6K8qOIvgKOVPCTmN1g/4w80fJquyJ1yoQRuxYWHfH6tYKhLlH2SJUQsXgofsBO9c1UIT6iRvOaXvB/EQvIVpLPAQOomHtw969fWEGf+QssSFuuejLHVnib7GKxKRKTfW3+LN3Msg9krMgdBfRptm64zC1wUwhWXWQdz4OKdOCQpfWdiLOzjxgSYs+zANlMYaqC5ZsjL4llbjqszFhE20WPZjwszXuWI8s3ORTQNAV7x9XC462QqLGOg6dHHRDSIueaC9nWCCVtibV97WtouR6TdnMd7MWhzjHPOQ14Ixj76f4ysTTdwp58pLbCNxuDVNdai8MKX7ak1o9V0z04lWYRidqg4iWvyLBvbJCjYNH0kWETrSax1wGKfEj/RWqrH5/Hkx2MliKeN5z28yQuoF+G6CjPhhOGBpBtcN24tpRiiaB+8mljv7HZ0PSb6hs0FUD03lsd4Drv5NN+gSCh7cK12LCYf07icM3BGsP6Dq2WDjE248WeFO2TlC4wvMj36L5+SnP1GRIw3DmSSRVe+dJhIVjEyIRivCasktmaChjQMGlZys8bppZ/muDaeryUZAxcObiuHNXJelaaMfITy/OjeYttPt5p/mEGnK92qbfM1cSc/xIKVfk9+Cz+an54nB0 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: On Fri, 7 Jul 2023 14:12:06 -0400 David Wysochanski wrote: > On Fri, Jul 7, 2023 at 12:46 PM Hyeonggon Yoo <42.hyeyoo@gmail.com> wrote: > > > > On Sat, Jul 8, 2023 at 1:39 AM Hyeonggon Yoo <42.hyeyoo@gmail.com> wrote: > > > > > > On Wed, Jun 28, 2023 at 11:48:52AM +0100, David Howells wrote: > > > > Fscache has an optimisation by which reads from the cache are skipped until > > > > we know that (a) there's data there to be read and (b) that data isn't > > > > entirely covered by pages resident in the netfs pagecache. This is done > > > > with two flags manipulated by fscache_note_page_release(): > > > > > > > > if (... > > > > test_bit(FSCACHE_COOKIE_HAVE_DATA, &cookie->flags) && > > > > test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags)) > > > > clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags); > > > > > > > > where the NO_DATA_TO_READ flag causes cachefiles_prepare_read() to indicate > > > > that netfslib should download from the server or clear the page instead. > > > > > > > > The fscache_note_page_release() function is intended to be called from > > > > ->releasepage() - but that only gets called if PG_private or PG_private_2 > > > > is set - and currently the former is at the discretion of the network > > > > filesystem and the latter is only set whilst a page is being written to the > > > > cache, so sometimes we miss clearing the optimisation. > > > > > > > > Fix this by following Willy's suggestion[1] and adding an address_space > > > > flag, AS_RELEASE_ALWAYS, that causes filemap_release_folio() to always call > > > > ->release_folio() if it's set, even if PG_private or PG_private_2 aren't > > > > set. > > > > > > > > Note that this would require folio_test_private() and page_has_private() to > > > > become more complicated. To avoid that, in the places[*] where these are > > > > used to conditionalise calls to filemap_release_folio() and > > > > try_to_release_page(), the tests are removed the those functions just > > > > jumped to unconditionally and the test is performed there. > > > > > > > > [*] There are some exceptions in vmscan.c where the check guards more than > > > > just a call to the releaser. I've added a function, folio_needs_release() > > > > to wrap all the checks for that. > > > > > > > > AS_RELEASE_ALWAYS should be set if a non-NULL cookie is obtained from > > > > fscache and cleared in ->evict_inode() before truncate_inode_pages_final() > > > > is called. > > > > > > > > Additionally, the FSCACHE_COOKIE_NO_DATA_TO_READ flag needs to be cleared > > > > and the optimisation cancelled if a cachefiles object already contains data > > > > when we open it. > > > > > > > > Fixes: 1f67e6d0b188 ("fscache: Provide a function to note the release of a page") > > > > Fixes: 047487c947e8 ("cachefiles: Implement the I/O routines") > > > > Reported-by: Rohith Surabattula > > > > Suggested-by: Matthew Wilcox > > > > Signed-off-by: David Howells > > > > > > Hi David, > > > > > > I was bisecting a use-after-free BUG on the latest mm-unstable, > > > where HEAD is 347e208de0e4 ("rmap: pass the folio to __page_check_anon_rmap()"). > > > > > > According to my bisection, this is the first bad commit. > > > Use-After-Free is triggered on reclamation path when swap is enabled. > > > > This was originally occurred during kernel compilation but > > can easily be reproduced via: > > > > stress-ng --bigheap $(nproc) > > > > > (and couldn't trigger without swap enabled) > > > > > > the config, KASAN splat, bisect log are attached. > > > hope this isn't too late :( > > > > > > > cc: Matthew Wilcox > > > > cc: Linus Torvalds > > > > cc: Steve French > > > > cc: Shyam Prasad N > > > > cc: Rohith Surabattula > > > > cc: Dave Wysochanski > > > > cc: Dominique Martinet > > > > cc: Ilya Dryomov > > > > cc: linux-cachefs@redhat.com > > > > cc: linux-cifs@vger.kernel.org > > > > cc: linux-afs@lists.infradead.org > > > > cc: v9fs-developer@lists.sourceforge.net > > > > cc: ceph-devel@vger.kernel.org > > > > cc: linux-nfs@vger.kernel.org > > > > cc: linux-fsdevel@vger.kernel.org > > > > cc: linux-mm@kvack.org > > > > --- > > > > > > > > Notes: > > > > ver #7) > > > > - Make NFS set AS_RELEASE_ALWAYS. > > > > > > > > ver #4) > > > > - Split out merging of folio_has_private()/filemap_release_folio() call > > > > pairs into a preceding patch. > > > > - Don't need to clear AS_RELEASE_ALWAYS in ->evict_inode(). > > > > > > > > ver #3) > > > > - Fixed mapping_clear_release_always() to use clear_bit() not set_bit(). > > > > - Moved a '&&' to the correct line. > > > > > > > > ver #2) > > > > - Rewrote entirely according to Willy's suggestion[1]. > > > > > > > > fs/9p/cache.c | 2 ++ > > > > fs/afs/internal.h | 2 ++ > > > > fs/cachefiles/namei.c | 2 ++ > > > > fs/ceph/cache.c | 2 ++ > > > > fs/nfs/fscache.c | 3 +++ > > > > fs/smb/client/fscache.c | 2 ++ > > > > include/linux/pagemap.h | 16 ++++++++++++++++ > > > > mm/internal.h | 5 ++++- > > > > 8 files changed, 33 insertions(+), 1 deletion(-) > > > I think myself / Daire Byrne may have already tracked this down and I > found a 1-liner that fixed a similar crash in his environment. > > Can you try this patch on top and let me know if it still crashes? > https://github.com/DaveWysochanskiRH/kernel/commit/902c990e311120179fa5de99d68364b2947b79ec I also encountered this issue with my DAMON tests, and was trying to find a time slot for deep dive. And I confirmed your fix works. Thank you for this great work. Please Cc me when you post the patch if possible. Tested-by: SeongJae Park Thanks, SJ > >