From patchwork Fri Mar 1 00:07:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13577780 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E3AB629 for ; Fri, 1 Mar 2024 00:10:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251808; cv=none; b=pjC636n0ZoPQESKYjzo1Q1s/xSU0ZvSHrCjFQDd1pmNbL2XJqDA8tkuEsJ90dylEmXNHQqPrQxplvSfcvp+efjMtX4gE40O74Rm6OtyEgN98ng+lZCQdhY3yiJteP+bd1i0rtSSbbmS7HYuJ9PMs1/Lt5VrmTgqxga2AKbaE+DE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251808; c=relaxed/simple; bh=qXQ2oUvVgRI4k71x4p0GR1bPnxu5BO9O+6p7Ce06sV0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CRPRbzHczFbTXNBL1eaAD87QWoFu7RYiIzHfdwWCER01L/hhMHIQyaehVNyTlOlxizIgCxuz/UGn470tSg/ZrV9VnhJI+b9X9whkfJcXOykKE8zg423E9g2R7PVGloL3J1amiowq+aK5NaI73A6t80xZ9j+WEA6oKBbdcH4WFN8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=VgynxoeL; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=or90K7HB; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=VgynxoeL; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=or90K7HB; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="VgynxoeL"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="or90K7HB"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="VgynxoeL"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="or90K7HB" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 2818522361; Fri, 1 Mar 2024 00:10:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709251805; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Pu+Aw5m68F52YnvoMFzIucdUgFDCCW+gbyNhlvNHPQY=; b=VgynxoeLEngzLLF9cc0jbE3P3KfsY/VIcexxkUOQz+5ImYAT2pf8ySwLc60Lxe7LBbwpDW /54JaFX9eWqWcek9+Oiv1XPUrLGmMa2x2TtmnJE1T6joxKQOYVArI0Dify8hYMEhgFkkq7 8BEEmbzBO/elBd/tw1V775yARzNDQcw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709251805; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Pu+Aw5m68F52YnvoMFzIucdUgFDCCW+gbyNhlvNHPQY=; b=or90K7HBRJguI0hJmosHQTK5kwuBVWBk1yfyal3iT6M8iSlL83JpBOm7rioq6dnCQt/1A7 hsxNOk4Rbk84sEBw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709251805; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Pu+Aw5m68F52YnvoMFzIucdUgFDCCW+gbyNhlvNHPQY=; b=VgynxoeLEngzLLF9cc0jbE3P3KfsY/VIcexxkUOQz+5ImYAT2pf8ySwLc60Lxe7LBbwpDW /54JaFX9eWqWcek9+Oiv1XPUrLGmMa2x2TtmnJE1T6joxKQOYVArI0Dify8hYMEhgFkkq7 8BEEmbzBO/elBd/tw1V775yARzNDQcw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709251805; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Pu+Aw5m68F52YnvoMFzIucdUgFDCCW+gbyNhlvNHPQY=; b=or90K7HBRJguI0hJmosHQTK5kwuBVWBk1yfyal3iT6M8iSlL83JpBOm7rioq6dnCQt/1A7 hsxNOk4Rbk84sEBw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id E1BF613AB0; Fri, 1 Mar 2024 00:10:02 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id 6tg7JNoc4WWxJwAAD6G6ig (envelope-from ); Fri, 01 Mar 2024 00:10:02 +0000 From: NeilBrown To: Chuck Lever , Jeff Layton Cc: linux-nfs@vger.kernel.org, Olga Kornievskaia , Dai Ngo , Tom Talpey Subject: [PATCH 1/3] nfsd: move nfsd4_cstate_assign_replay() earlier in open handling. Date: Fri, 1 Mar 2024 11:07:37 +1100 Message-ID: <20240301000950.2306-2-neilb@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240301000950.2306-1-neilb@suse.de> References: <20240301000950.2306-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Level: X-Spamd-Bar: / Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=VgynxoeL; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=or90K7HB X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-0.25 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; BROKEN_CONTENT_TYPE(1.50)[]; DWL_DNSWL_MED(-2.00)[suse.de:dkim]; RCPT_COUNT_FIVE(0.00)[6]; NEURAL_HAM_LONG(-1.00)[-1.000]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DKIM_TRACE(0.00)[suse.de:+]; MX_GOOD(-0.01)[]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim,suse.de:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; NEURAL_HAM_SHORT(-0.20)[-1.000]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-1.74)[93.41%] X-Spam-Score: -0.25 X-Rspamd-Queue-Id: 2818522361 X-Spam-Flag: NO Rather than taking the rp_mutex in nfsd4_cleanup_open_state() (which seems counter-intuitive), take it and assign rp_owner as soon as possible. This will support a future change when nfsd4_cstate_assign_replay() might fail. Signed-off-by: NeilBrown --- fs/nfsd/nfs4state.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 7d6c657e0409..e625f738f7b0 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -5066,15 +5066,15 @@ nfsd4_process_open1(struct nfsd4_compound_state *cstate, strhashval = ownerstr_hashval(&open->op_owner); oo = find_openstateowner_str(strhashval, open, clp); open->op_openowner = oo; - if (!oo) { + if (!oo) goto new_owner; - } if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { /* Replace unconfirmed owners without checking for replay. */ release_openowner(oo); open->op_openowner = NULL; goto new_owner; } + nfsd4_cstate_assign_replay(cstate, &oo->oo_owner); status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); if (status) return status; @@ -5084,6 +5084,7 @@ nfsd4_process_open1(struct nfsd4_compound_state *cstate, if (oo == NULL) return nfserr_jukebox; open->op_openowner = oo; + nfsd4_cstate_assign_replay(cstate, &oo->oo_owner); alloc_stateid: open->op_stp = nfs4_alloc_open_stateid(clp); if (!open->op_stp) @@ -5835,12 +5836,8 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) { - if (open->op_openowner) { - struct nfs4_stateowner *so = &open->op_openowner->oo_owner; - - nfsd4_cstate_assign_replay(cstate, so); - nfs4_put_stateowner(so); - } + if (cstate->replay_owner) + nfs4_put_stateowner(cstate->replay_owner); if (open->op_file) kmem_cache_free(file_slab, open->op_file); if (open->op_stp) From patchwork Fri Mar 1 00:07:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13577781 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0906D365 for ; Fri, 1 Mar 2024 00:10:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251818; cv=none; b=meRG898OpRyKRKm7TjJLRCu6gbR/+Z+FM296YKVdBCQtGY6vI0Mz5fE/8iz2dOn7FYUfnu5q1JH469h+TO9erII2IvihquRZAT+Qv/IfrYcMao2KAY/NCPL4Zf5u/PdMhYWUYUc5mP2IYejtGUJ7MkzpSfTZrdf4pNrXPKGStcs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251818; c=relaxed/simple; bh=+AboxGb62em+KGVWRFHAd2/9u6cSbqyKvRCixdLjXZE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fnm9tPt6kWhM7xaP6eheRsqlshZQRqt3wnXAowO0IHjAs7gKXUt3Ouz0Rw7+ZqfUUWlSiSgR4+wKqlGwy0wqNbyY4hb3oSthabFE1GV9tbZ162jQA3S1erZr7+5DlG4rmLVLWATAD+GyFQBZS7SNO+GC7gYeqrNDKY4YE4f1cg4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=VGB2QD0J; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=bd+vr29u; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=VGB2QD0J; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=bd+vr29u; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="VGB2QD0J"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="bd+vr29u"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="VGB2QD0J"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="bd+vr29u" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 178541F814; Fri, 1 Mar 2024 00:10:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709251815; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V0erjh0+e81QFKq/QeT9ISBY7sIMd7Ps9vcYmTt40zg=; b=VGB2QD0J9XkV7PqFtbpNCDER4W2lh1eQYrIpIRYXVVkOEMuqLdMpLtOg4XjHVhxDN7g0JP nwqfymts9DU67oJ5hHYbGjbjcsMXktHIc/C9MyiN6Hbe5ndPVU6Iw+QYvhtm0UT2IiR5K9 wLjd47XfOkSYz0WkatIXrEGNts6pBWs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709251815; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V0erjh0+e81QFKq/QeT9ISBY7sIMd7Ps9vcYmTt40zg=; b=bd+vr29uCnGMtYGA8AbM8Kb4ANPkIdVghso3aLvI1q+qJevtlN316GOUk0e9SB3jTS5Jx1 1VthP6kyLFDO4aDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709251815; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V0erjh0+e81QFKq/QeT9ISBY7sIMd7Ps9vcYmTt40zg=; b=VGB2QD0J9XkV7PqFtbpNCDER4W2lh1eQYrIpIRYXVVkOEMuqLdMpLtOg4XjHVhxDN7g0JP nwqfymts9DU67oJ5hHYbGjbjcsMXktHIc/C9MyiN6Hbe5ndPVU6Iw+QYvhtm0UT2IiR5K9 wLjd47XfOkSYz0WkatIXrEGNts6pBWs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709251815; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V0erjh0+e81QFKq/QeT9ISBY7sIMd7Ps9vcYmTt40zg=; b=bd+vr29uCnGMtYGA8AbM8Kb4ANPkIdVghso3aLvI1q+qJevtlN316GOUk0e9SB3jTS5Jx1 1VthP6kyLFDO4aDw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id C9A2813AB0; Fri, 1 Mar 2024 00:10:12 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id 1iJTHuQc4WW4JwAAD6G6ig (envelope-from ); Fri, 01 Mar 2024 00:10:12 +0000 From: NeilBrown To: Chuck Lever , Jeff Layton Cc: linux-nfs@vger.kernel.org, Olga Kornievskaia , Dai Ngo , Tom Talpey Subject: [PATCH 2/3] nfsd: replace rp_mutex to avoid deadlock in move_to_close_lru() Date: Fri, 1 Mar 2024 11:07:38 +1100 Message-ID: <20240301000950.2306-3-neilb@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240301000950.2306-1-neilb@suse.de> References: <20240301000950.2306-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: 0.70 X-Spamd-Result: default: False [0.70 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; BROKEN_CONTENT_TYPE(1.50)[]; RCPT_COUNT_FIVE(0.00)[6]; NEURAL_HAM_LONG(-1.00)[-1.000]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Flag: NO move_to_close_lru() waits for sc_count to become zero while holding rp_mutex. This can deadlock if another thread holds a reference and is waiting for rp_mutex. By the time we get to move_to_close_lru() the openowner is unhashed and cannot be found any more. So code waiting for the mutex can safely retry the lookup if move_to_close_lru() has started. So change rp_mutex to an atomic_t with three states: RP_UNLOCK - state is still hashed, not locked for reply RP_LOCKED - state is still hashed, is locked for reply RP_UNHASHED - state is now hashed, no code can get a lock. Use wait_ver_event() to wait for either a lock, or for the owner to be unhashed. In the latter case, retry the lookup. Signed-off-by: NeilBrown --- fs/nfsd/nfs4state.c | 46 ++++++++++++++++++++++++++++++++++++--------- fs/nfsd/state.h | 2 +- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index e625f738f7b0..5dab316932d3 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -4442,21 +4442,32 @@ nfsd4_init_leases_net(struct nfsd_net *nn) atomic_set(&nn->nfsd_courtesy_clients, 0); } +enum rp_lock { + RP_UNLOCKED, + RP_LOCKED, + RP_UNHASHED, +}; + static void init_nfs4_replay(struct nfs4_replay *rp) { rp->rp_status = nfserr_serverfault; rp->rp_buflen = 0; rp->rp_buf = rp->rp_ibuf; - mutex_init(&rp->rp_mutex); + atomic_set(&rp->rp_locked, RP_UNLOCKED); } -static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, - struct nfs4_stateowner *so) +static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, + struct nfs4_stateowner *so) { if (!nfsd4_has_session(cstate)) { - mutex_lock(&so->so_replay.rp_mutex); + wait_var_event(&so->so_replay.rp_locked, + atomic_cmpxchg(&so->so_replay.rp_locked, + RP_UNLOCKED, RP_LOCKED) != RP_LOCKED); + if (atomic_read(&so->so_replay.rp_locked) == RP_UNHASHED) + return -EAGAIN; cstate->replay_owner = nfs4_get_stateowner(so); } + return 0; } void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) @@ -4465,7 +4476,8 @@ void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) if (so != NULL) { cstate->replay_owner = NULL; - mutex_unlock(&so->so_replay.rp_mutex); + atomic_set(&so->so_replay.rp_locked, RP_UNLOCKED); + wake_up_var(&so->so_replay.rp_locked); nfs4_put_stateowner(so); } } @@ -4691,7 +4703,11 @@ move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) * Wait for the refcount to drop to 2. Since it has been unhashed, * there should be no danger of the refcount going back up again at * this point. + * Some threads with a reference might be waiting for rp_locked, + * so tell them to stop waiting. */ + atomic_set(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED); + wake_up_var(&oo->oo_owner.so_replay.rp_locked); wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); release_all_access(s); @@ -5064,6 +5080,7 @@ nfsd4_process_open1(struct nfsd4_compound_state *cstate, clp = cstate->clp; strhashval = ownerstr_hashval(&open->op_owner); +retry: oo = find_openstateowner_str(strhashval, open, clp); open->op_openowner = oo; if (!oo) @@ -5074,17 +5091,24 @@ nfsd4_process_open1(struct nfsd4_compound_state *cstate, open->op_openowner = NULL; goto new_owner; } - nfsd4_cstate_assign_replay(cstate, &oo->oo_owner); + if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) { + nfs4_put_stateowner(&oo->oo_owner); + goto retry; + } status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); if (status) return status; goto alloc_stateid; new_owner: oo = alloc_init_open_stateowner(strhashval, open, cstate); + open->op_openowner = oo; if (oo == NULL) return nfserr_jukebox; - open->op_openowner = oo; - nfsd4_cstate_assign_replay(cstate, &oo->oo_owner); + if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) { + WARN_ON(1); + nfs4_put_stateowner(&oo->oo_owner); + goto new_owner; + } alloc_stateid: open->op_stp = nfs4_alloc_open_stateid(clp); if (!open->op_stp) @@ -6841,11 +6865,15 @@ nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, trace_nfsd_preprocess(seqid, stateid); *stpp = NULL; +retry: status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); if (status) return status; stp = openlockstateid(s); - nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); + if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) { + nfs4_put_stateowner(stp->st_stateowner); + goto retry; + } status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); if (!status) diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h index 41bdc913fa71..6a3becd86112 100644 --- a/fs/nfsd/state.h +++ b/fs/nfsd/state.h @@ -446,7 +446,7 @@ struct nfs4_replay { unsigned int rp_buflen; char *rp_buf; struct knfsd_fh rp_openfh; - struct mutex rp_mutex; + atomic_t rp_locked; char rp_ibuf[NFSD4_REPLAY_ISIZE]; }; From patchwork Fri Mar 1 00:07:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13577782 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB825384 for ; Fri, 1 Mar 2024 00:10:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251824; cv=none; b=CwYXSf3QUAxD6XG+1lAnpdjtjcGyLkWlQfa4Gn05yXkPVtAGc0rcFj1H57WGJh/2jOELC9RNzi/VxcrmWpE7oex6e9fWcKVtexzLvPE8QV0hnDIAHC/+kj/ykluuO92mFaNnEG72mI834z2jwBDM/d8DciB+X6HbnmbUvVMHhuY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251824; c=relaxed/simple; bh=aLBNSdV6iVvF+w6WPD283bORe+Hh7tlShxRIvk54TZw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YAL9UUBBlf1dmVpRcJzbGtjgjgJO3FPu4N4u4k2mkUT0+1WjzwftXKwHIoiqxAPNDy5EW1QoRUjp3/XYzsbxzEH7MMhBO/w+ZaFfq3XUPgiGS/maYg/p6Cd0sDsBsmgH4oYK/fJBPa73wGJuqNL8aGsSx8W1Z/6Hthr/4d1U0EU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Prjnlh8R; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=yrOlbXaj; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=eqqIioZV; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=epNwi7KV; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Prjnlh8R"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="yrOlbXaj"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="eqqIioZV"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="epNwi7KV" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id F30171F821; Fri, 1 Mar 2024 00:10:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709251821; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KmEqQd8n9zeincJNeY3kwPoL1y7+9v2BJ70vP/sqOow=; b=Prjnlh8R6J9yIi4OxOKgITqdK+T9GHz+bSV5/DNZSMY5hHeYYGuS5JtvQLOTmr/CGVEA7u nj9u94Ey3l5rsfvDnJrqeC/3O6Brpuv9hTl5QH1axa1HT4JuXCItaN7L5TgQ/U78kBXNlI xrMaOECBiDpps8LCgqaBCu1royCkCvE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709251821; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KmEqQd8n9zeincJNeY3kwPoL1y7+9v2BJ70vP/sqOow=; b=yrOlbXajpq19VilG4GJagdsxXfbKWbLvZ2v0RX6grvmCAk2lbSvyfPgS3/5JreGUVtQly/ GA3Hh5EbHwu2yxBw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709251820; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KmEqQd8n9zeincJNeY3kwPoL1y7+9v2BJ70vP/sqOow=; b=eqqIioZV+YeG3YLcBk6uLDEln8sbZDqMMUUEXaoHAVgXR6dl2JxTElelmcj81O4NRKu9Nn Xt56QN8z8fmVrT3Xw2MJiZ0yRE5iKC5ojeyl0t4swQ8adcqywqWO8i0gscuVcyIVT2cM5+ 4eGdYHvO4JVCQb+YmVzaHdvU9/0oNEA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709251820; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KmEqQd8n9zeincJNeY3kwPoL1y7+9v2BJ70vP/sqOow=; b=epNwi7KV/myzhgGuUWHhN+axDnyL3c0R5wDw3zsFliW7x0HO5GqFyvYtlMhWqb9k2r8msM 2PRcqrSZdBzlvRAg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B42DF13AB0; Fri, 1 Mar 2024 00:10:18 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id 0i7rGOoc4WW8JwAAD6G6ig (envelope-from ); Fri, 01 Mar 2024 00:10:18 +0000 From: NeilBrown To: Chuck Lever , Jeff Layton Cc: linux-nfs@vger.kernel.org, Olga Kornievskaia , Dai Ngo , Tom Talpey Subject: [PATCH 3/3] nfsd: drop st_mutex_mutex before calling move_to_close_lru() Date: Fri, 1 Mar 2024 11:07:39 +1100 Message-ID: <20240301000950.2306-4-neilb@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240301000950.2306-1-neilb@suse.de> References: <20240301000950.2306-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spamd-Result: default: False [1.90 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; BROKEN_CONTENT_TYPE(1.50)[]; RCPT_COUNT_FIVE(0.00)[6]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Level: * X-Spam-Score: 1.90 X-Spam-Flag: NO move_to_close_lru() is currently called with ->st_mutex held. This can lead to a deadlock as move_to_close_lru() waits for sc_count to drop to 2, and some threads holding a reference might be waiting for the mutex. These references will never be dropped so sc_count will never reach 2. There can be no harm in dropping ->st_mutex before move_to_close_lru() because the only place that takes the mutex is nfsd4_lock_ol_stateid(), and it quickly aborts if sc_type is NFS4_CLOSED_STID, which it will be before move_to_close_lru() is called. See also https://lore.kernel.org/lkml/4dd1fe21e11344e5969bb112e954affb@jd.com/T/ where this problem was raised but not successfully resolved. Signed-off-by: NeilBrown Reviewed-by: Jeff Layton Signed-off-by: Chuck Lever --- fs/nfsd/nfs4state.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 5dab316932d3..bb0f563cb2d2 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -7015,7 +7015,7 @@ nfsd4_open_downgrade(struct svc_rqst *rqstp, return status; } -static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) +static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) { struct nfs4_client *clp = s->st_stid.sc_client; bool unhashed; @@ -7032,11 +7032,11 @@ static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) list_for_each_entry(stp, &reaplist, st_locks) nfs4_free_cpntf_statelist(clp->net, &stp->st_stid); free_ol_stateid_reaplist(&reaplist); + return false; } else { spin_unlock(&clp->cl_lock); free_ol_stateid_reaplist(&reaplist); - if (unhashed) - move_to_close_lru(s, clp->net); + return unhashed; } } @@ -7052,6 +7052,7 @@ nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *stp; struct net *net = SVC_NET(rqstp); struct nfsd_net *nn = net_generic(net, nfsd_net_id); + bool need_move_to_close_list; dprintk("NFSD: nfsd4_close on file %pd\n", cstate->current_fh.fh_dentry); @@ -7074,8 +7075,10 @@ nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, */ nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); - nfsd4_close_open_stateid(stp); + need_move_to_close_list = nfsd4_close_open_stateid(stp); mutex_unlock(&stp->st_mutex); + if (need_move_to_close_list) + move_to_close_lru(stp, net); /* v4.1+ suggests that we send a special stateid in here, since the * clients should just ignore this anyway. Since this is not useful