Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2891659pxb; Tue, 24 Aug 2021 10:00:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxWiMFp2sjtwcxYfTfZecBhFw3D1kohOysK5b4DJOmy0znytzvcYhLf8erGfmAtu3O8twU X-Received: by 2002:a17:906:3598:: with SMTP id o24mr2001971ejb.191.1629824409750; Tue, 24 Aug 2021 10:00:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629824409; cv=none; d=google.com; s=arc-20160816; b=fZ2I+TcAEWugTjZu8PL+jaDwkfg+cHbngppY//IpJgD3/AiHmiEFclKVGn7WPmtfjn zFdFCW9+wUeMslFDTCNHpQPQ1qqcP+bTAVHcoLz4UgxcXI/J7V4qm4fklei3G0FZLEya YLQC1UixSGJ2IRn60Im+uEoABgP4TR97pD9WbauHpX25due0YCAAdrKso4rEyxkAPLF/ sFeBvTSryWbaCOCkYt5F5/4nffDAAj8y4Y2pLT1EW3+jAHGYiHLUKXCbH6Iak1iwHAVK 2ENa3gS7VaLe9xO+IX1kp5wFPeqEOWEz77jp0ahWRInsGkdeZaVQ1wdB/BxXmy5uOAV2 BkXQ== 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=OiJbpuV4+IdQ9AMKlYeRzKcl6SexjxFboG4kP7wh/Ic=; b=oQw1OLKGTWJmDMAlgFDqBjQfBTeZnQD0D8cD+N8HodOzbQ3q0fvZRCbqUdSKtrf1nK oFCdXKHKKZVY034lOdorysQEFk/emFPE3bM8WRWlacRz0xrQlRSNu6bldAqBnv2cwK+1 OhoJ1LFNlEozfalXUPcvoJZOxB6CQZA+GS8EuxQJtsbqB6nVCQOkI1Z29OPwRZvTkVL1 Cu1h3sm5wc4Qc4e2wk9ZO0MrFb2rKdxPki4iG0/64lplp/jJRzoviRcdfNjMc/hsc649 HLR9EnbpsZZKBjeFO6lAY/Rbdbn+yXanY9cHaVLC8JWn7Y++/VrWp75l8QaWUkaWpADg S2nQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sjFM2Kgm; 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 qk18si11801886ejc.690.2021.08.24.09.59.45; Tue, 24 Aug 2021 10:00:09 -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=sjFM2Kgm; 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 S234738AbhHXQ5X (ORCPT + 99 others); Tue, 24 Aug 2021 12:57:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:38844 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233895AbhHXQ5E (ORCPT ); Tue, 24 Aug 2021 12:57:04 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6E148613D5; Tue, 24 Aug 2021 16:56:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824180; bh=QI4CIrLHLF0c+/CtvW+aELJ7gfzhZZI94zKlXwQ6kgA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sjFM2KgmrcJDgtXvds9FReV4IBKX4Qggtio/pMMdL8tc+p94ZYOXNiXg2V4/+ze7m NnKOwXIYsweVRoCGa1vUwZOfNMHbEVEQ5drcOxFd3jwCX58gk/u+1hhE8iDZh8wHKb PSg1Lx/4xMGAycjRxRNsGWunZD27J+hLiQEDU3ebzrhflBj/4FykCuRbOyiFN9Himx lOEIKuW5R7pZ8ivC3gL69aAtaM7/Lv82s4rn4ly9xVJRkK5uNy2Z8JkR8ZUKDXbx2M mAIqcit+2pXD7Q++zxy0gAdcpxHHWMcdy0OPiZ+pZbTMwTJz3axCP9C+IWZUm92jKt vzf3lpZnypkMA== 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.13 011/127] scsi: pm80xx: Fix TMF task completion race condition Date: Tue, 24 Aug 2021 12:54:11 -0400 Message-Id: <20210824165607.709387-12-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824165607.709387-1-sashal@kernel.org> References: <20210824165607.709387-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.13.13-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.13.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.13.13-rc1 X-KernelTest-Deadline: 2021-08-26T16:55+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 335cf37e6cb9..2e429e31f1f0 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