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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A83BC001E0 for ; Fri, 7 Jul 2023 19:23:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233024AbjGGTXK (ORCPT ); Fri, 7 Jul 2023 15:23:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233020AbjGGTXJ (ORCPT ); Fri, 7 Jul 2023 15:23:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49F8519AE; Fri, 7 Jul 2023 12:23:08 -0700 (PDT) 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 C1B1A61A53; 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 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org 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 > > 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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 20756EB64D9 for ; Fri, 7 Jul 2023 19:23:20 +0000 (UTC) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=tgLCFacD; dkim-atps=neutral Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4QyNbV2drSz3c3P for ; Sat, 8 Jul 2023 05:23:18 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=tgLCFacD; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=kernel.org (client-ip=139.178.84.217; helo=dfw.source.kernel.org; envelope-from=sj@kernel.org; receiver=lists.ozlabs.org) Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4QyNbL1hkfz3bwd for ; Sat, 8 Jul 2023 05:23:10 +1000 (AEST) 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 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-BeenThere: linux-erofs@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development of Linux EROFS file system List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Dominique Martinet , David Howells , linux-mm@kvack.org, Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-afs@lists.infradead.org, Shyam Prasad N , linux-cifs@vger.kernel.org, Matthew Wilcox , Christoph Hellwig , linux-cachefs@redhat.com, v9fs-developer@lists.sourceforge.net, Ilya Dryomov , linux-ext4@vger.kernel.org, ceph-devel@vger.kernel.org, linux-nfs@vger.kernel.org, SeongJae Park , Rohith Surabattula , Linus Torvalds , Jeff Layton , Steve French , Daire Byrne , linux-fsdevel@vger.kernel.org, Andrew Morton , linux-erofs@lists.ozlabs.org Errors-To: linux-erofs-bounces+linux-erofs=archiver.kernel.org@lists.ozlabs.org Sender: "Linux-erofs" 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 > >