Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2898698pxb; Tue, 24 Aug 2021 10:08:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw7B/VFdVolh5rYgYy2gAmRK2ezwzgrvpIhqFo0PWVDmi1dfLGbrdZwxkifwuffsFZQMQ2U X-Received: by 2002:a17:907:ea1:: with SMTP id ho33mr893060ejc.271.1629824916545; Tue, 24 Aug 2021 10:08:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629824916; cv=none; d=google.com; s=arc-20160816; b=I77BI5aHPsCv73lzDaI2iAuHptK1Y4EWs4PxfCsBb/J+I6v2OjwViYVVCsMRdaNpPW j6jNv4nqUsRXOJqks1iZBNS9615MrWwXFZ8/2VV2nC5PF8YOVMFmg/LVPboRIw2akMKL YZyct0USOpuJGj3gKvAPmr3x9rh6eR2YZANwUoO8ND9AiD9ZIspCMXLecc/slVbWXJzc WQF/SNW0RGKdGTLHnc0Elg6NofRBWpi/p+83bXSTvam8I/KC6dOYVIQZ4C4NlMgAdXIH ccxeafDJl+22kOocH/wLEe66qcJ5XBmHT7fH0DDbSxzPpf+gvez6cL1oSWlRDRh9zB+x Q8sA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=q2CGOkcFWB1V1rulYNU5ViHVhT2e29MZsRiU/XYoO80=; b=G8i4a3PPtYn26IJ+g+pq26p//BtU/Dq8smyVHVb/YL6bCqYDEsJHBay+Oj0e3jwzAO sF3KHk7us9XU2pudVZLaFQ4cCFemj75PQfTuNC5g5n6UiYlwX1lZeUQv3Vh97wEbG/0v MT5npL6tTT2stboQAv8/38P4yWm+Q6pLIJAAATOvzKwBfLeZ7j+5W6bazpj+0z12FWNB r07PE+GCncE+6P1t93Kzfd+9Mj4RJ7lVqWI1MDzb88UnUANjQ49YM3Jb7KSMBWo7ZH8q 1UT76npAahGlxv++uBi+Wqc32ZaoahEa8F4ZEiz8vulu9B8EjOeBHsKLCTZInkm+7dVh HNZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Ep/Rb2lh"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p16si17308238ejm.464.2021.08.24.10.08.11; Tue, 24 Aug 2021 10:08:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Ep/Rb2lh"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238796AbhHXRHe (ORCPT + 99 others); Tue, 24 Aug 2021 13:07:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:46256 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239805AbhHXREz (ORCPT ); Tue, 24 Aug 2021 13:04:55 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B5AA861929; Tue, 24 Aug 2021 16:59:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824372; bh=X32CSpRw5m3YxVpFqf32TgeBgt7/BOBOoWhxI/BFt7M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ep/Rb2lhg+skrwg05ClwzSLQFaJmVv1+NNIrCexo1NsyhDuJSwAZblRaIs/nxaAu9 jingykGBKjXB6W7W0AUbhWhTSxpRo+CgT8zFdIItyAWS4cy3M1QgkFAR6SKE+IAtKv 73UdSG6WhmFZfVMvL3rIehK4zklnprulvBMGHN4pAdrH7z4Z+wgjK0VFXCF2T4tade bajwXpVPUs3lQGesE4/bN+9uOsY4OB66YnJ2jDf8vQrycTIWglJ+SCihNrHsY1DZS2 UVutg4SC2MD9wQrbn5cATfq1DPcV7xQhcwS7FXPzgndxUP2INNEOzyp6KfliQcSbNC i1IVATkz58O9A== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Igor Pylypiv , Vishakha Channapattan , Jack Wang , "Martin K . Petersen" , Sasha Levin Subject: [PATCH 5.10 22/98] scsi: pm80xx: Fix TMF task completion race condition Date: Tue, 24 Aug 2021 12:57:52 -0400 Message-Id: <20210824165908.709932-23-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824165908.709932-1-sashal@kernel.org> References: <20210824165908.709932-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.10.61-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.10.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.10.61-rc1 X-KernelTest-Deadline: 2021-08-26T16:58+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Igor Pylypiv [ Upstream commit d712d3fb484b7fa8d1d57e9ca6f134bb9d8c18b1 ] The TMF timeout timer may trigger at the same time when the response from a controller is being handled. When this happens the SAS task may get freed before the response processing is finished. Fix this by calling complete() only when SAS_TASK_STATE_DONE is not set. A similar race condition was fixed in commit b90cd6f2b905 ("scsi: libsas: fix a race condition when smp task timeout") Link: https://lore.kernel.org/r/20210707185945.35559-1-ipylypiv@google.com Reviewed-by: Vishakha Channapattan Acked-by: Jack Wang Signed-off-by: Igor Pylypiv Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/pm8001/pm8001_sas.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/drivers/scsi/pm8001/pm8001_sas.c b/drivers/scsi/pm8001/pm8001_sas.c index 39de9a9360d3..c3bb58885033 100644 --- a/drivers/scsi/pm8001/pm8001_sas.c +++ b/drivers/scsi/pm8001/pm8001_sas.c @@ -684,8 +684,7 @@ int pm8001_dev_found(struct domain_device *dev) void pm8001_task_done(struct sas_task *task) { - if (!del_timer(&task->slow_task->timer)) - return; + del_timer(&task->slow_task->timer); complete(&task->slow_task->completion); } @@ -693,9 +692,14 @@ static void pm8001_tmf_timedout(struct timer_list *t) { struct sas_task_slow *slow = from_timer(slow, t, timer); struct sas_task *task = slow->task; + unsigned long flags; - task->task_state_flags |= SAS_TASK_STATE_ABORTED; - complete(&task->slow_task->completion); + spin_lock_irqsave(&task->task_state_lock, flags); + if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { + task->task_state_flags |= SAS_TASK_STATE_ABORTED; + complete(&task->slow_task->completion); + } + spin_unlock_irqrestore(&task->task_state_lock, flags); } #define PM8001_TASK_TIMEOUT 20 @@ -748,13 +752,10 @@ static int pm8001_exec_internal_tmf_task(struct domain_device *dev, } res = -TMF_RESP_FUNC_FAILED; /* Even TMF timed out, return direct. */ - if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { - if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { - pm8001_dbg(pm8001_ha, FAIL, - "TMF task[%x]timeout.\n", - tmf->tmf); - goto ex_err; - } + if (task->task_state_flags & SAS_TASK_STATE_ABORTED) { + pm8001_dbg(pm8001_ha, FAIL, "TMF task[%x]timeout.\n", + tmf->tmf); + goto ex_err; } if (task->task_status.resp == SAS_TASK_COMPLETE && @@ -834,12 +835,9 @@ pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha, wait_for_completion(&task->slow_task->completion); res = TMF_RESP_FUNC_FAILED; /* Even TMF timed out, return direct. */ - if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { - if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { - pm8001_dbg(pm8001_ha, FAIL, - "TMF task timeout.\n"); - goto ex_err; - } + if (task->task_state_flags & SAS_TASK_STATE_ABORTED) { + pm8001_dbg(pm8001_ha, FAIL, "TMF task timeout.\n"); + goto ex_err; } if (task->task_status.resp == SAS_TASK_COMPLETE && -- 2.30.2