Received: by 2002:a4a:301c:0:0:0:0:0 with SMTP id q28-v6csp239396oof; Mon, 24 Sep 2018 19:43:54 -0700 (PDT) X-Google-Smtp-Source: ACcGV630CLDiQVg3cD+kZm7LcXIuAWgGyBvEnk5R0xFkRnYzu8ZJgnE6h74UpZmy+22wwthgVEZa X-Received: by 2002:a62:b0e:: with SMTP id t14-v6mr1391631pfi.36.1537843434581; Mon, 24 Sep 2018 19:43:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537843434; cv=none; d=google.com; s=arc-20160816; b=lTiSD1x9krOk7VWKYxCeooVNUSBmQvOr5w84WywVq1+Vs4GVugrP5I/cXPrQ0d3lyO +RuJoR6EUrB8YdSwmZaiQ+gwbtVB4uwxi5wMjgdYHVgZZMy8+ELewpoUAdZtHRvEFGup HOtmaTjOCGsGmFhdidmOLic9MH0tqzLCSFPbLq7tNt7wIo/jiIbTD4RGwyEH4Oc9L4NA NxwqP/ZttR9Xci7SH7J99UlH4U7ohQy3tgOuH5HxjxJm0cGMiIq0tLRM7sNo5poXIDK1 /TDENTGPu4ZK/3faryUnZlRc7CyOGeTz8bKVjq7N+hYwlEV1lNRAilFC7nkJrZiSNDHq ro6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=9PuPwGPWi+cP1wv7o6O7wJW51RLRqOu7PMcCRatZntk=; b=NJSGxC9nOAnzJ3H2Hr2zEiR/2/6wmgTQB6p7u35TuX0xherKGULvqliztXp0jiDyUs Fefhc7NnK3FNhMwgeoRxpY5mGD8f2cDiKkSiRg3TkVJIOPnHOMrEOrslwDeAxC7X7W8E NXeFu9zQoT0x1JdLRUjvfFdzeqfe+FWuX1QEhxRYaclwr8g8Ls+YvMQ83+b+Mg4Y20ct FBxCOv4mPH/7xhMo7B82ZREpin5aYW9bfxCKME6DuqbFVBmHcCsHCJMINqepyaDU++32 wSdXyVGpXuFuiK/YG1RQqM3zHkh/I4JbtkHUr13SOcB/XXdmKrTAB6y0Z+bN9dUAhzMH LcYw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d39-v6si1115344pla.41.2018.09.24.19.43.39; Mon, 24 Sep 2018 19:43:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728367AbeIYIsr (ORCPT + 99 others); Tue, 25 Sep 2018 04:48:47 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:53453 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726340AbeIYIsr (ORCPT ); Tue, 25 Sep 2018 04:48:47 -0400 Received: from DGGEMS402-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 35962417AD68A; Tue, 25 Sep 2018 10:43:27 +0800 (CST) Received: from huawei.com (10.175.124.28) by DGGEMS402-HUB.china.huawei.com (10.3.19.202) with Microsoft SMTP Server id 14.3.399.0; Tue, 25 Sep 2018 10:43:20 +0800 From: Jason Yan To: , CC: , , , , , , , , , , , Jason Yan , Ewan Milne , Tomas Henzl Subject: [PATCH v3 5/5] scsi: libsas: fix a race condition when smp task timeout Date: Tue, 25 Sep 2018 10:56:54 +0800 Message-ID: <20180925025654.44053-6-yanaijie@huawei.com> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180925025654.44053-1-yanaijie@huawei.com> References: <20180925025654.44053-1-yanaijie@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.124.28] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the lldd is processing the complete sas task in interrupt and set the task stat as SAS_TASK_STATE_DONE, the smp timeout timer is able to be triggered at the same time. And smp_task_timedout() will complete the task wheter the SAS_TASK_STATE_DONE is set or not. Then the sas task may freed before lldd end the interrupt process. Thus a use-after-free will happen. Fix this by calling the complete() only when SAS_TASK_STATE_DONE is not set. And remove the check of the return value of the del_timer(). Once the LLDD sets DONE, it must call task->done(), which will call smp_task_done()->complete() and the task will be completed and freed correctly. Reported-by: chenxiang Signed-off-by: Jason Yan CC: John Garry CC: Johannes Thumshirn CC: Ewan Milne CC: Christoph Hellwig CC: Tomas Henzl CC: Dan Williams CC: Hannes Reinecke Reviewed-by: Hannes Reinecke Reviewed-by: John Garry Reviewed-by: Johannes Thumshirn --- drivers/scsi/libsas/sas_expander.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c index 52222940d398..0d1f72752ca2 100644 --- a/drivers/scsi/libsas/sas_expander.c +++ b/drivers/scsi/libsas/sas_expander.c @@ -48,17 +48,16 @@ static void smp_task_timedout(struct timer_list *t) unsigned long flags; spin_lock_irqsave(&task->task_state_lock, flags); - if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) + 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); - - complete(&task->slow_task->completion); } static void smp_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); } -- 2.14.4