Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp586261lqb; Fri, 24 May 2024 07:26:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVqi4Pc8tZTYIzs9X6NQV9HpXXS92v0Kn+fFlKfyWR/2wJU5L/xbQ5xHGBmFSnBR0EcsBb+0038lmG+geM13ax6Mkjw6TLdxNEtwBTBKw== X-Google-Smtp-Source: AGHT+IHcGqggqvoW0Ghk3olVtaz8NebrN2TobsVVE/Ygzc4sOFQYYYrCroiDqfCra11In2Rp7XdH X-Received: by 2002:a17:90b:201:b0:2bd:82c9:bd40 with SMTP id 98e67ed59e1d1-2bf5f408a4amr2233629a91.48.1716560804090; Fri, 24 May 2024 07:26:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716560804; cv=pass; d=google.com; s=arc-20160816; b=KmuOyM+GVCC2QP3rgLDb1dadnZuDfbUz18p8qvXZ+dQgQnpVeM+/rfZrixAZ+O4xag CaeXrYNVBRO0fjoNBrKjsZFfERUVwS42NWOEs/64uAsj4iBW5OrjhD4s0hFIk97BnG0/ QP8rA6lAHToQ8Sj16SETnWS/Q3nXypOCQPYFzPWbrOSmCIQiQX68zHVQAYp91vUewgWG ku0TQo5wSmmZdXp/9dB4xq5WekvMHWiAjAQlecLktzUngE2nUwSJ8bJm3h+heG6imUvE ylRsWEm37eatfVtT5/NOXmIbNWHR9zdohMqijcK6rXW4KnoCrUk8IdAb7K//c/mOzB4t FAzw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:content-transfer-encoding:content-id:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:subject:cc:to :from:organization:dkim-signature; bh=GLnGM17UL4b12TgJD1s/b9rU7JDsVx4PpoKaNTMgNmk=; fh=gL/hDkYrax8lYQUlK4NjQ+9Gc1wQYb8hhZysdyqSzOc=; b=uk0IprM3DX2FulCLNoWCm5DNLimM/PhgVCCwsI1GrH2uBNpDuFsLXu3IScpbw/7/cX dIiRL4k8GbkPne6Pc44kCIb7Z2PBh/rD2u7P0n4wh7SoNu/9aC0dqMi/YAhN5D4I6eHT qP5jwTdftxLk3F1Ck1J/sjOKlB0pDbEzfidsDDuGRTUdA0P5q+ltoNjFeYlV6W2S4NHG FRsp25nQX79pspgAdBBzoD3177V5g6Oplyfom1qZ1AkJTPk60ApMLzDcA3fAR9y5tzQ+ 9hrmCbFfYvTMmfU21S+T76ezKJE9MWsFJs2WoXevcTQbijo9XhbGTBj66J95L8SKREfZ aS4A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P39J+PgD; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-188790-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188790-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2bf71f89365si862435a91.146.2024.05.24.07.26.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 07:26:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-188790-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P39J+PgD; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-188790-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188790-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id AF786281AFB for ; Fri, 24 May 2024 14:26:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9A85D12C528; Fri, 24 May 2024 14:26:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="P39J+PgD" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 40F6586279 for ; Fri, 24 May 2024 14:26:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716560789; cv=none; b=QLadKPg3u86C6+qlY0SZMqPFtrtCZbG6rpwpebfjrNPaYYbDo0mNQSzXz7/4WMcmmzrI4aXyi1DIs2NpL50X75OeA5NcB+EVgVaGCkuusu12AjU3fs6GeQlw07eFBPbnaYO2yTDllDSPWdX1I2fQLqS2yJcRlvNpI/8hDPQje9I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716560789; c=relaxed/simple; bh=/hO+Wdk51Ceeo7dHaf4yVaejFzzuVQkXGewi20m1Row=; h=From:To:cc:Subject:MIME-Version:Content-Type:Date:Message-ID; b=IeN+CXgQTCf/UO34qew/sBLb6zGiWHkdviJiNWf27bv+Et/tOGEXz+tkhkSMDVvCxIt7cDKSoRL67tVzhOSf+B3faODpWtDzHAutlh2AcxT/Qa120LIGsgMP6QELxTtyGDdktGB1dpKWTrVLy0/aEgiWz9bFuwjoRjvSJpLVJxE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=P39J+PgD; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716560786; 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; bh=GLnGM17UL4b12TgJD1s/b9rU7JDsVx4PpoKaNTMgNmk=; b=P39J+PgDdkmvyI1GHVFrwyuES3UkvaMWko3H+3VIpXLHLDWWqFVixQmBCnbGvvAOjSBSzH +tmFKUGB3k+JG6SKRTEBB++gB1BYkeFDFPKaDmL7s4cSpdOQYsR/c0KcrKugjKf4725hpl 6BDqQQxks7kJQp/Ho3iHhNCMXbprswM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-182-QUYKM_jeN5mRBk6fOMzu1g-1; Fri, 24 May 2024 10:26:23 -0400 X-MC-Unique: QUYKM_jeN5mRBk6fOMzu1g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4613C800994; Fri, 24 May 2024 14:26:22 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 63F432026D68; Fri, 24 May 2024 14:26:12 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 From: David Howells To: Dominique Martinet cc: dhowells@redhat.com, syzbot+df038d463cca332e8414@syzkaller.appspotmail.com, syzbot+d7c7a495a5e466c031b6@syzkaller.appspotmail.com, syzbot+1527696d41a634cc1819@syzkaller.appspotmail.com, Eric Van Hensbergen , Latchesar Ionkov , Christian Schoenebeck , Jeff Layton , Steve French , Hillf Danton , Christian Brauner , v9fs@lists.linux.dev, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, netfs@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] netfs, 9p: Fix race between umount and async request completion 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-ID: <755890.1716560771.1@warthog.procyon.org.uk> Content-Transfer-Encoding: quoted-printable Date: Fri, 24 May 2024 15:26:11 +0100 Message-ID: <755891.1716560771@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 netfs, 9p: Fix race between umount and async request completion There's a problem in 9p's interaction with netfslib whereby a crash occurs because the 9p_fid structs get forcibly destroyed during client teardown (without paying attention to their refcounts) before netfslib has finished with them. However, it's not a simple case of deferring the clunking that p9_fid_put() does as that requires the p9_client record to still be present. The problem is that netfslib has to unlock pages and clear the IN_PROGRESS flag before destroying the objects involved - including the fid - and, in any case, nothing checks to see if writeback completed barring looking at the page flags. Fix this by keeping a count of outstanding I/O requests (of any type) and waiting for it to quiesce during inode eviction. Reported-by: syzbot+df038d463cca332e8414@syzkaller.appspotmail.com Link: https://lore.kernel.org/all/0000000000005be0aa061846f8d6@google.com/ Reported-by: syzbot+d7c7a495a5e466c031b6@syzkaller.appspotmail.com Link: https://lore.kernel.org/all/000000000000b86c5e06130da9c6@google.com/ Reported-by: syzbot+1527696d41a634cc1819@syzkaller.appspotmail.com Link: https://lore.kernel.org/all/000000000000041f960618206d7e@google.com/ Signed-off-by: David Howells Reviewed-by: Dominique Martinet Tested-by: syzbot+d7c7a495a5e466c031b6@syzkaller.appspotmail.com cc: Eric Van Hensbergen cc: Latchesar Ionkov cc: Christian Schoenebeck cc: Jeff Layton cc: Steve French cc: Hillf Danton cc: v9fs@lists.linux.dev cc: linux-afs@lists.infradead.org cc: linux-cifs@vger.kernel.org cc: netfs@lists.linux.dev cc: linux-fsdevel@vger.kernel.org --- Notes: Changes =3D=3D=3D=3D=3D=3D=3D ver #3) - Adjust commit message; no change to the diff. ver #2) - Wait for outstanding I/O before clobbering the pagecache. fs/9p/vfs_inode.c | 1 + fs/afs/inode.c | 1 + fs/netfs/objects.c | 5 +++++ fs/smb/client/cifsfs.c | 1 + include/linux/netfs.h | 18 ++++++++++++++++++ 5 files changed, 26 insertions(+) diff --git a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c index 8c9a896d691e..effb3aa1f3ed 100644 --- a/fs/9p/vfs_inode.c +++ b/fs/9p/vfs_inode.c @@ -349,6 +349,7 @@ void v9fs_evict_inode(struct inode *inode) __le32 __maybe_unused version; = if (!is_bad_inode(inode)) { + netfs_wait_for_outstanding_io(inode); truncate_inode_pages_final(&inode->i_data); = version =3D cpu_to_le32(v9inode->qid.version); diff --git a/fs/afs/inode.c b/fs/afs/inode.c index 94fc049aff58..15bb7989c387 100644 --- a/fs/afs/inode.c +++ b/fs/afs/inode.c @@ -648,6 +648,7 @@ void afs_evict_inode(struct inode *inode) = ASSERTCMP(inode->i_ino, =3D=3D, vnode->fid.vnode); = + netfs_wait_for_outstanding_io(inode); truncate_inode_pages_final(&inode->i_data); = afs_set_cache_aux(vnode, &aux); diff --git a/fs/netfs/objects.c b/fs/netfs/objects.c index c90d482b1650..f4a642727479 100644 --- a/fs/netfs/objects.c +++ b/fs/netfs/objects.c @@ -72,6 +72,7 @@ struct netfs_io_request *netfs_alloc_request(struct addr= ess_space *mapping, } } = + atomic_inc(&ctx->io_count); trace_netfs_rreq_ref(rreq->debug_id, 1, netfs_rreq_trace_new); netfs_proc_add_rreq(rreq); netfs_stat(&netfs_n_rh_rreq); @@ -124,6 +125,7 @@ static void netfs_free_request(struct work_struct *wor= k) { struct netfs_io_request *rreq =3D container_of(work, struct netfs_io_request, work); + struct netfs_inode *ictx =3D netfs_inode(rreq->inode); unsigned int i; = trace_netfs_rreq(rreq, netfs_rreq_trace_free); @@ -142,6 +144,9 @@ static void netfs_free_request(struct work_struct *wor= k) } kvfree(rreq->direct_bv); } + + if (atomic_dec_and_test(&ictx->io_count)) + wake_up_var(&ictx->io_count); call_rcu(&rreq->rcu, netfs_free_request_rcu); } = diff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c index ec5b639f421a..14810ffd15c8 100644 --- a/fs/smb/client/cifsfs.c +++ b/fs/smb/client/cifsfs.c @@ -431,6 +431,7 @@ cifs_free_inode(struct inode *inode) static void cifs_evict_inode(struct inode *inode) { + netfs_wait_for_outstanding_io(inode); truncate_inode_pages_final(&inode->i_data); if (inode->i_state & I_PINNING_NETFS_WB) cifs_fscache_unuse_inode_cookie(inode, true); diff --git a/include/linux/netfs.h b/include/linux/netfs.h index d2d291a9cdad..3ca3906bb8da 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -68,6 +68,7 @@ struct netfs_inode { loff_t remote_i_size; /* Size of the remote file */ loff_t zero_point; /* Size after which we assume there's no data * on the server */ + atomic_t io_count; /* Number of outstanding reqs */ unsigned long flags; #define NETFS_ICTX_ODIRECT 0 /* The file has DIO in progress */ #define NETFS_ICTX_UNBUFFERED 1 /* I/O should not use the pagecache */ @@ -474,6 +475,7 @@ static inline void netfs_inode_init(struct netfs_inode= *ctx, ctx->remote_i_size =3D i_size_read(&ctx->inode); ctx->zero_point =3D LLONG_MAX; ctx->flags =3D 0; + atomic_set(&ctx->io_count, 0); #if IS_ENABLED(CONFIG_FSCACHE) ctx->cache =3D NULL; #endif @@ -517,4 +519,20 @@ static inline struct fscache_cookie *netfs_i_cookie(s= truct netfs_inode *ctx) #endif } = +/** + * netfs_wait_for_outstanding_io - Wait for outstanding I/O to complete + * @ctx: The netfs inode to wait on + * + * Wait for outstanding I/O requests of any type to complete. This is in= tended + * to be called from inode eviction routines. This makes sure that any + * resources held by those requests are cleaned up before we let the inod= e get + * cleaned up. + */ +static inline void netfs_wait_for_outstanding_io(struct inode *inode) +{ + struct netfs_inode *ictx =3D netfs_inode(inode); + + wait_var_event(&ictx->io_count, atomic_read(&ictx->io_count) =3D=3D 0); +} + #endif /* _LINUX_NETFS_H */