Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2099864rwb; Thu, 17 Nov 2022 06:34:47 -0800 (PST) X-Google-Smtp-Source: AA0mqf6/2ETYVJ04PSW80R8EELY/Zst7Wf39owdh+ByBJUs6yaBwXStg8X46oklGARaMpW+QKSFB X-Received: by 2002:a17:902:8c83:b0:186:8516:cd5f with SMTP id t3-20020a1709028c8300b001868516cd5fmr3140501plo.11.1668695687516; Thu, 17 Nov 2022 06:34:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668695687; cv=none; d=google.com; s=arc-20160816; b=GWwIoFAnFqrhG9Bp+vJ6RMs+NOzFRKrYewZhcBO6406UPO6B2Ab25KPX7zKQ3bOBaY vyC61oYXLmxcr61EF3atXnCVmVdkpBA+LZcfWdXXIP7pYWpjBoq2sxHYKXiXHv2PrROS Qcgudkg8Ajm02yBN32dzB12FiFfTyh42aUDmeVhL7KMnCY1DaOMiUUl/M+nrOjJ+SxGX Sj5lglFa+KibnjVM1w00UCVSJJeCrCibI6jEt/XJ/IZKREvyLCCpk3373DhciWmR/ENL /6+M0pRGB8FWBaGYaGJMXXcxwV7Tyay0kO5kVlruEOdkNkcTKoiPbOsxTDa+etn7Ed3m Ngaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=c88mGEKHdnVBM3RKxy4enALUmbIHHYNEBopScrP3LHI=; b=R1/cgU84m7nt5dEo+vaKiYIVTDrdkVqx1y1g6VnHqPYjagx4EK8CFm9oZo7Nfg2Ye5 O01EEJ8Ome+Pvwy3gDb4JRNL7zCRliqUWLjUo1bISwhdHRXCwVgprBVneOOPa06VoHMQ 4NJkcWmEBaD6QcT5AwrQzceRgf7V2QcBIw8xXlv0PhIfPsGJNjncp6VOyUd26tnfwheW 8eUjsTEHf+tiFtGT9bxy6PR4s+MFPEoXpMjzdeObhnz58IxTINYEV3lnG6Nb/WIaWtvh +dR42x09x6OoqLnrJabP3FfYN9ZaYAcxufI+LqNKfWwO1E172tXhHmKXIoRP9BtwMuCX zu1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IHjJFLIf; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-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 c19-20020a6566d3000000b0046fd05d55e0si1125342pgw.567.2022.11.17.06.34.34; Thu, 17 Nov 2022 06:34:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-nfs-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=IHjJFLIf; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-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 S234664AbiKQOa1 (ORCPT + 99 others); Thu, 17 Nov 2022 09:30:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240126AbiKQOaQ (ORCPT ); Thu, 17 Nov 2022 09:30:16 -0500 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 501B02AE3B for ; Thu, 17 Nov 2022 06:29:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668695361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=c88mGEKHdnVBM3RKxy4enALUmbIHHYNEBopScrP3LHI=; b=IHjJFLIfe35tu3PSit35y/Kc4F8+eYO09prCE2tO9AM3SKUxKEn7GDYstF8K7t7k6w/e/d UttA4VY4STIA8XjMKQdunAXCkg6bqmNKq332Fa8Mg8KvMF1PkmHSQGvHo2scX5qKfoFv+W 8nEv51L+KGn5pSxn/xMQaXpRK8mrZ08= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-571-yCXf2dZuOl2CqrqZMfMCgQ-1; Thu, 17 Nov 2022 09:29:19 -0500 X-MC-Unique: yCXf2dZuOl2CqrqZMfMCgQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 24D823C01DF5; Thu, 17 Nov 2022 14:29:19 +0000 (UTC) Received: from dwysocha.rdu.csb (unknown [10.22.32.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D857C35429; Thu, 17 Nov 2022 14:29:18 +0000 (UTC) From: Dave Wysochanski To: David Howells , Daire Byrne , Benjamin Maynard Cc: linux-cachefs@redhat.com, linux-nfs@vger.kernel.org Subject: [PATCH v2] fscache: Fix oops due to race with cookie_lru and use_cookie Date: Thu, 17 Nov 2022 09:29:15 -0500 Message-Id: <20221117142915.1366990-1-dwysocha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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_H2,SPF_HELO_NONE,SPF_NONE 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-nfs@vger.kernel.org If a cookie expires from the LRU and the LRU_DISCARD flag is set, but the state machine has not run yet, it's possible another thread can call fscache_use_cookie and begin to use it. When the cookie_worker finally runs, it will see the LRU_DISCARD flag set, transition the cookie->state to LRU_DISCARDING, which will then withdraw the cookie. Once the cookie is withdrawn the object is removed the below oops will occur because the object associated with the cookie is now NULL. Fix the oops by clearing the LRU_DISCARD bit if another thread uses the cookie before the cookie_worker runs. BUG: kernel NULL pointer dereference, address: 0000000000000008 ... CPU: 31 PID: 44773 Comm: kworker/u130:1 Tainted: G E 6.0.0-5.dneg.x86_64 #1 Hardware name: Google Compute Engine/Google Compute Engine, BIOS Google 08/26/2022 Workqueue: events_unbound netfs_rreq_write_to_cache_work [netfs] RIP: 0010:cachefiles_prepare_write+0x28/0x90 [cachefiles] ... Call Trace: netfs_rreq_write_to_cache_work+0x11c/0x320 [netfs] process_one_work+0x217/0x3e0 worker_thread+0x4a/0x3b0 ? process_one_work+0x3e0/0x3e0 kthread+0xd6/0x100 ? kthread_complete_and_exit+0x20/0x20 ret_from_fork+0x1f/0x30 Fixes: 12bb21a29c19 ("fscache: Implement cookie user counting and resource pinning") Reported-by: Daire Byrne Signed-off-by: Dave Wysochanski --- fs/fscache/cookie.c | 8 ++++++++ include/trace/events/fscache.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/fs/fscache/cookie.c b/fs/fscache/cookie.c index 451d8a077e12..bce2492186d0 100644 --- a/fs/fscache/cookie.c +++ b/fs/fscache/cookie.c @@ -605,6 +605,14 @@ void __fscache_use_cookie(struct fscache_cookie *cookie, bool will_modify) set_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags); queue = true; } + /* + * We could race with cookie_lru which may set LRU_DISCARD bit + * but has yet to run the cookie state machine. If this happens + * and another thread tries to use the cookie, clear LRU_DISCARD + * so we don't end up withdrawing the cookie while in use. + */ + if (test_and_clear_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags)) + fscache_see_cookie(cookie, fscache_cookie_see_lru_discard_clear); break; case FSCACHE_COOKIE_STATE_FAILED: diff --git a/include/trace/events/fscache.h b/include/trace/events/fscache.h index c078c48a8e6d..a6190aa1b406 100644 --- a/include/trace/events/fscache.h +++ b/include/trace/events/fscache.h @@ -66,6 +66,7 @@ enum fscache_cookie_trace { fscache_cookie_put_work, fscache_cookie_see_active, fscache_cookie_see_lru_discard, + fscache_cookie_see_lru_discard_clear, fscache_cookie_see_lru_do_one, fscache_cookie_see_relinquish, fscache_cookie_see_withdraw, @@ -149,6 +150,7 @@ enum fscache_access_trace { EM(fscache_cookie_put_work, "PQ work ") \ EM(fscache_cookie_see_active, "- activ") \ EM(fscache_cookie_see_lru_discard, "- x-lru") \ + EM(fscache_cookie_see_lru_discard_clear,"- lrudc") \ EM(fscache_cookie_see_lru_do_one, "- lrudo") \ EM(fscache_cookie_see_relinquish, "- x-rlq") \ EM(fscache_cookie_see_withdraw, "- x-wth") \ -- 2.31.1