Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp32293064rwd; Fri, 7 Jul 2023 11:26:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlE29K935rVnL2VdTI9nrhniJoKCrFK9iVM+4jkvXoeMZNVIJOCl0wdy6ZTCjU2AiERFBdbE X-Received: by 2002:a17:906:7313:b0:974:c32c:b485 with SMTP id di19-20020a170906731300b00974c32cb485mr5499943ejc.45.1688754391317; Fri, 07 Jul 2023 11:26:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688754391; cv=none; d=google.com; s=arc-20160816; b=Fb7Y+VZIrdxOcTUJ8gloSfiv3twKvzUQf+WwRwDY3hYT7aqUkMP/xiNSELUlSNG7LT R1EHlyfIkrlF+8VTUMxUVmSzDtxMjXEm2IoHCp+hso2bDGvPsOZDbMWkGJf8GsYdsEvV RtkFOsq3ujmlF+zmfTzuDXKudY533Db7ID007b+ZzXrDNo/TT9HK3OcqWSoNoXPJi8L+ pOF6H1BEJF+nIajz5i2PWf8a0ib7UjhkPiOYdzc/FKd/rvES0UIVWMzdoI2OtW/cokNl ng0jXtIvItoOhLZvRxQhtg8bXyQNOcN2dqmDrJ56wxfW+AqTjEKHyarzf5vnnLFS7sEe nKBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=dYp6SX4idj0K2s8mMqxJ4XFt6H9/PBaHUEfz7SiAvZA=; fh=IjGL293cp5uKlELlZkzEUZSypBXA+ixqm8ry22Viq50=; b=LPtF+AM+WOZd6cKnZ9J+857LF8pkhO87DbsYM4mO3hiYs5AMyOzhRPdV5kleOolQV/ BJLm1MdWFoh5WzKXJu0kp79JoaTNIhK8Th9fwSemVqGAfcc8bniA696YJhK1oEH6l4no YAu8VnTpt9QB/dlgUof4n4wgahC1KCmtBkJQl6rmwQZHtMUcfMdlsZQ1+eF7yNlORXfR w+njpwjvL1fb7xARY40aW+pOYzza1wi6P4WEz9tkaLSOvOgfvTwV7j882+fYeAJ5cpMq LIcNh1VaTOIX5ArmT2RYRZCONhWXpeBqw9OYNv8EsUv/6gyS2IaWiU3oSxeA8qR3rYhJ drJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PsvYJEbx; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l17-20020a170906079100b00992ffb64c31si1659226ejc.681.2023.07.07.11.26.00; Fri, 07 Jul 2023 11:26:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PsvYJEbx; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232593AbjGGSN3 (ORCPT + 99 others); Fri, 7 Jul 2023 14:13:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232566AbjGGSN2 (ORCPT ); Fri, 7 Jul 2023 14:13:28 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2495A268C for ; Fri, 7 Jul 2023 11:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688753565; h=from:from: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; bh=dYp6SX4idj0K2s8mMqxJ4XFt6H9/PBaHUEfz7SiAvZA=; b=PsvYJEbxUBucMhQkMVERI4Y0gEC6D59jE1JTSnGBpr/j2uTq6wbAp2u6+O91yQFDF+OjMP 8sJyE4C/cuur2Tyw3y+mt5EnJMVng6NLXk7BA2akSPXx7J9ecywqo+LxBUNktYyXkpU2GM bR145k8uQxS7PfmVrCbTqr+qw75cSWU= Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-311-h1CRbqq_MPyXfhHrk3J2AA-1; Fri, 07 Jul 2023 14:12:44 -0400 X-MC-Unique: h1CRbqq_MPyXfhHrk3J2AA-1 Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-262e5e719eeso2833471a91.1 for ; Fri, 07 Jul 2023 11:12:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688753563; x=1691345563; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dYp6SX4idj0K2s8mMqxJ4XFt6H9/PBaHUEfz7SiAvZA=; b=h8TO7RBBRox7d7jF+G/e3Yi6eenYMGYWJfgthOG3xY31SABGZfGvSWobYI4I9txTgM oVhTaMqHKMNNyHJX1ajU5ej8uNrWF3BNH+A8rN+zcfIkGfMG7kcsIEESFsXbHsv5GcD1 32GsdopGE8Pmc0/NgnWNrb+NAQavCWnM/ufqO0rR3YkR5c52fnjKFb1V19ZbxH+fdbz2 SZe7Qr0+IdxFW18P9wuNfWH7eYfC6DX3aBDFnEWvRxb57DacHm6Ik2VoBbuUP01sVLAS puO7JQ/TFBdKVumU0JWSV9P4B77wDMXqqs5lhJCKWxnksMbgAgVM5RG0kAhEwZCsFBQM B3Xg== X-Gm-Message-State: ABy/qLaAfBrdI87IxwBliaTZcj+vKCYVOkUkUDFxesxI7BSLUHdi7j2Q Vr2sF6LKir7eFQflooLGu/tZEAx3QejzXGj9NhIQPPL/kHEbfjMdysKgwA0WCSi9ks+6/HLVGPF 4vC3oMepxCZ+aNkWpG7O6tRBM65GWNIiTFh/0hg== X-Received: by 2002:a17:90a:bd01:b0:262:b229:7e45 with SMTP id y1-20020a17090abd0100b00262b2297e45mr4613497pjr.11.1688753563140; Fri, 07 Jul 2023 11:12:43 -0700 (PDT) X-Received: by 2002:a17:90a:bd01:b0:262:b229:7e45 with SMTP id y1-20020a17090abd0100b00262b2297e45mr4613468pjr.11.1688753562822; Fri, 07 Jul 2023 11:12:42 -0700 (PDT) MIME-Version: 1.0 References: <20230628104852.3391651-1-dhowells@redhat.com> <20230628104852.3391651-3-dhowells@redhat.com> In-Reply-To: From: David Wysochanski Date: Fri, 7 Jul 2023 14:12:06 -0400 Message-ID: Subject: Re: [BUG mm-unstable] BUG: KASAN: use-after-free in shrink_folio_list+0x9f4/0x1ae0 To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Fri, Jul 7, 2023 at 12:46=E2=80=AFPM Hyeonggon Yoo <42.hyeyoo@gmail.com>= wrote: > > On Sat, Jul 8, 2023 at 1:39=E2=80=AFAM 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 d= one > > > 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->flag= s); > > > > > > where the NO_DATA_TO_READ flag causes cachefiles_prepare_read() to in= dicate > > > that netfslib should download from the server or clear the page inste= ad. > > > > > > The fscache_note_page_release() function is intended to be called fro= m > > > ->releasepage() - but that only gets called if PG_private or PG_priva= te_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_spa= ce > > > flag, AS_RELEASE_ALWAYS, that causes filemap_release_folio() to alway= s call > > > ->release_folio() if it's set, even if PG_private or PG_private_2 are= n't > > > set. > > > > > > Note that this would require folio_test_private() and page_has_privat= e() 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_rele= ase() > > > 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_fi= nal() > > > is called. > > > > > > Additionally, the FSCACHE_COOKIE_NO_DATA_TO_READ flag needs to be cle= ared > > > and the optimisation cancelled if a cachefiles object already contain= s 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 se= t_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/902c990e311120179fa5de99= d68364b2947b79ec