From 5da933cc477b746652b245c8ad6d150ad3b6bb9e Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Mon, 5 Aug 2019 01:43:41 +0000 Subject: [PATCH] scst: Fix a race condition between LUN translation and LUN removal git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@8496 d57e44dd-8a1f-0410-8b47-8ef2f437770f --- scst/src/scst_targ.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scst/src/scst_targ.c b/scst/src/scst_targ.c index f3d812559..450265082 100644 --- a/scst/src/scst_targ.c +++ b/scst/src/scst_targ.c @@ -4996,6 +4996,9 @@ static int scst_translate_lun(struct scst_cmd *cmd) rcu_read_lock(); tgt_dev = scst_lookup_tgt_dev(cmd->sess, cmd->lun); + if (tgt_dev && + !atomic_inc_not_zero(&tgt_dev->tgt_dev_cmd_count)) + tgt_dev = NULL; rcu_read_unlock(); if (tgt_dev) { @@ -5014,6 +5017,7 @@ static int scst_translate_lun(struct scst_cmd *cmd) "the device will not be visible remotely", (unsigned long long)cmd->lun); nul_dev = true; + scst_tgt_dev_dec_cmd_count(tgt_dev); } } if (unlikely(res != 0)) { @@ -5142,7 +5146,7 @@ static int __scst_init_cmd(struct scst_cmd *cmd) scst_set_cmd_state(cmd, SCST_CMD_STATE_PARSE); - cnt = atomic_inc_return(&tgt_dev->tgt_dev_cmd_count) - 1; + cnt = atomic_read(&tgt_dev->tgt_dev_cmd_count) - 1; if (unlikely(cnt > dev->max_tgt_dev_commands)) { TRACE(TRACE_FLOW_CONTROL, "Too many pending commands (%d) in "