Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp468234imm; Wed, 19 Sep 2018 01:44:33 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYWH1pdiGDo42dQXhMYmV7KrwTUTggzAC5JI1grSX7ztimquY6LyMuZHRudUWGr4c1mSXJM X-Received: by 2002:a63:de4b:: with SMTP id y11-v6mr31478412pgi.435.1537346673320; Wed, 19 Sep 2018 01:44:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537346673; cv=none; d=google.com; s=arc-20160816; b=xgFNQ0umIAYHvvdI0eu295OyC/9jhiiTQDxtYD0aUzsOr3D+7ZvrWt/6MGRcXBXwwO UgJK+HI1j2Khf8OOiS6CvSiP7wveTPo82GhcYw8MYzTniZetO1zMuo0/8PVTnWixDWzj TxtVpU3lygZ0WdDUXybPWjfJ5GvTJgUUeGWH+G9+Ih9PB3eLQ4GIMA54OtKfcJ8j9Q1J BMC3QcOWvk5vABhxaB54FjWCq5JLLq8FcX5fDssLTqyx4LmL10yBUdW1Qp400MN53YiP h5Lu5VK8FfCiD8L3R2yAiKxRbwgPewmT3x8PnR+0MnbZLMOvEONiEXVm4+QZcs0yDYBR L+gA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=KK3SO/08E/E44rzghEH/eBHZNS3h+ycWokji5cqIK+c=; b=rI6z4cEaMCW6lA3iVzlrcL/Umh+S+wnllXhe8SSfNA1myGMt7OLtmMM0BbazyBoLAG FP9IJ3nLhCmtW1WM0Q+o6MIt/46vn3stbSV+FUI3xJFYxTwGa3rn+UWmSjYnudkTYQPr qzeBCE2IkkruUMhleFh3PzXsLBLCbd2O4jZBPJjNXjIRqzNXExcS4NxpHQt1YFzYrR7a E2E25YB/vPISg4YPiY8+vt8IXeQ/UIfpB/a9QckHLdptrsQ79RNhViyY/AeRL/YDqMko 9Jv3bkDBT4q997SrZ1/uRmMdumin+GAhe+UDWG584ucy4imXcRtBefJLSmnprfq58zL5 A88A== 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 d11-v6si22662708pln.471.2018.09.19.01.43.48; Wed, 19 Sep 2018 01:44:33 -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 S1731039AbeISOTy (ORCPT + 99 others); Wed, 19 Sep 2018 10:19:54 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:12189 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727796AbeISOTx (ORCPT ); Wed, 19 Sep 2018 10:19:53 -0400 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 083143E240954; Wed, 19 Sep 2018 16:42:59 +0800 (CST) Received: from [127.0.0.1] (10.202.226.41) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.399.0; Wed, 19 Sep 2018 16:42:53 +0800 Subject: Re: [PATCH v2 5/5] scsi: libsas: fix a race condition when smp task timeout To: Jason Yan , , References: <20180919033204.11102-1-yanaijie@huawei.com> <20180919033204.11102-6-yanaijie@huawei.com> CC: , , , , , , , , , , Ewan Milne , Tomas Henzl From: John Garry Message-ID: Date: Wed, 19 Sep 2018 09:42:45 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: <20180919033204.11102-6-yanaijie@huawei.com> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.202.226.41] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 19/09/2018 04:32, Jason Yan wrote: > 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 Uh, I should have added this for my last comment on v1: Reviewed-by: John Garry > Reviewed-by: Hannes Reinecke > --- > 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); > } > >