Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1268376ybh; Thu, 23 Jul 2020 04:50:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJymI3VI40cA3V6FPkg7KfQqwHTTVb/rdUwG4GMKLYRu+EfEi3uLf/dBn8Z0PRBa+9WjBAqX X-Received: by 2002:aa7:c98d:: with SMTP id c13mr3756568edt.188.1595505010616; Thu, 23 Jul 2020 04:50:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595505010; cv=none; d=google.com; s=arc-20160816; b=RRgXAOT/wfG823fMlmfOL9wcuHSQIdfs5NAo/H9fwe/x0HziTRIBtVtadxLsRic0KP WpyUM2pE0ACYhpMD8stCK2BaodoRPxGF4lJpll9nK8ZyIGhUj2H2scdaPS6lBGDS0xKq pF3G05KbZLmrtwRsRFRaIwtGngoP1ccYvVQFTgdsniHbny+yPl/a5Vv0ahrhG7b5qwzZ C5OoYgjZUZgbTl2/Fl3QxN3irBKEzMO0MLhhISh9Kpe4XiKxu6uqwVBzE+9im4WX0nnU W4elH4zPnVuBkLdivQI+OipJ0+mndmHcejZ7uIkzCDv7MDADMAD2u9RJ4VObbZIPpFvD QagA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:ironport-sdr; bh=GwmojgtMfUuJAnIcAHTSP6kY0Q2dd80HqzHRhffDV3Q=; b=ifLM+o+zrWA+ULv5yFXtGp5kW5xjo7tORLClKwOPttKpytWDNzVvWOqzjh7UcOMVdU +Hu61N+ymqznWExmEJohqgCFNTrSloCMwpqKceaMYnFTMk1mAmNOOkSe8TFTVHlBxTe/ Fu45SpSNikQgVij/RN6YohBN166+pj9w3+QHwRtxx+aSQBukL6cHWElkNEIMT88ljGB1 tguZ7GTCMqEnJGs09EEly3F+cJC5o1zBypL9a/wrBOCsj5AaiUHIBXOxfju++MWoW2v8 0QfGgH9BCpf24GnbIIvmXnFoXnb8Cw1HRYAVD4dT3hnsUzyCb6v6gwJ2ud3o6TujIx2u VNDQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h16si1908872edr.390.2020.07.23.04.49.48; Thu, 23 Jul 2020 04:50:10 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728903AbgGWLrF (ORCPT + 99 others); Thu, 23 Jul 2020 07:47:05 -0400 Received: from labrats.qualcomm.com ([199.106.110.90]:38539 "EHLO labrats.qualcomm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728890AbgGWLrE (ORCPT ); Thu, 23 Jul 2020 07:47:04 -0400 IronPort-SDR: ychC/TbzUTb7TEV2/BZYRAvGhNVTDuIibCmoF+Ne3UtmSiEgpxyP8MjtDl35vA9ZzreYCBpf3O 0FJruqDdg5NVAtaSvvHfi/nK50/onJSN94zE1eQd57kW0fXvZbz7tOL1p/LXX3iFaRoouTum2b ANe2EHyFyBchjNypBE8TALQ+t6jENF3VX+sDreGtykrzyacR48I93+P1GCtuSuH1dRuUwwNVLj 5DCeqViYql6SjahhR5aUko/+T/ZkicBP+VhD4+A6b0UYaOKonsrwPY9oVUdaMCcHUsIv28v+XE iH0= X-IronPort-AV: E=Sophos;i="5.75,386,1589266800"; d="scan'208";a="29048257" Received: from unknown (HELO ironmsg04-sd.qualcomm.com) ([10.53.140.144]) by labrats.qualcomm.com with ESMTP; 23 Jul 2020 04:47:04 -0700 Received: from pacamara-linux.qualcomm.com ([192.168.140.135]) by ironmsg04-sd.qualcomm.com with ESMTP; 23 Jul 2020 04:47:03 -0700 Received: by pacamara-linux.qualcomm.com (Postfix, from userid 359480) id A959722E23; Thu, 23 Jul 2020 04:47:03 -0700 (PDT) From: Can Guo To: asutoshd@codeaurora.org, nguyenb@codeaurora.org, hongwus@codeaurora.org, rnayak@codeaurora.org, sh425.lee@samsung.com, linux-scsi@vger.kernel.org, kernel-team@android.com, saravanak@google.com, salyzyn@google.com, cang@codeaurora.org Cc: Alim Akhtar , Avri Altman , "James E.J. Bottomley" , "Martin K. Petersen" , Stanley Chu , Bean Huo , Bart Van Assche , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v6 8/8] scsi: ufs: Fix a racing problem btw error handler and runtime PM ops Date: Thu, 23 Jul 2020 04:46:26 -0700 Message-Id: <1595504787-19429-9-git-send-email-cang@codeaurora.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1595504787-19429-1-git-send-email-cang@codeaurora.org> References: <1595504787-19429-1-git-send-email-cang@codeaurora.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Current IRQ handler blocks scsi requests before scheduling eh_work, when error handler calls pm_runtime_get_sync, if ufshcd_suspend/resume sends a scsi cmd, most likely the SSU cmd, since scsi requests are blocked, pm_runtime_get_sync() will never return because ufshcd_suspend/reusme is blocked by the scsi cmd. Some changes and code re-arrangement can be made to resolve it. o In queuecommand path, hba->ufshcd_state check and ufshcd_send_command should stay into the same spin lock. This is to make sure that no more commands leak into doorbell after hba->ufshcd_state is changed. o Don't block scsi requests before scheduling eh_work, let error handler block scsi requests when it is ready to start error recovery. o Don't let scsi layer keep requeuing the scsi cmds sent from hba runtime PM ops, let them pass or fail them. Let them pass if eh_work is scheduled due to non-fatal errors. Fail them fail if eh_work is scheduled due to fatal errors, otherwise the cmds may eventually time out since UFS is in bad state, which gets error handler blocked for too long. If we fail the scsi cmds sent from hba runtime PM ops, hba runtime PM ops fails too, but it does not hurt since error handler can recover hba runtime PM error. Signed-off-by: Can Guo --- drivers/scsi/ufs/ufshcd.c | 84 +++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c index b2bafa3..9c8c43f 100644 --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -126,7 +126,8 @@ enum { UFSHCD_STATE_RESET, UFSHCD_STATE_ERROR, UFSHCD_STATE_OPERATIONAL, - UFSHCD_STATE_EH_SCHEDULED, + UFSHCD_STATE_EH_SCHEDULED_FATAL, + UFSHCD_STATE_EH_SCHEDULED_NON_FATAL, }; /* UFSHCD error handling flags */ @@ -2515,34 +2516,6 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) if (!down_read_trylock(&hba->clk_scaling_lock)) return SCSI_MLQUEUE_HOST_BUSY; - spin_lock_irqsave(hba->host->host_lock, flags); - switch (hba->ufshcd_state) { - case UFSHCD_STATE_OPERATIONAL: - break; - case UFSHCD_STATE_EH_SCHEDULED: - case UFSHCD_STATE_RESET: - err = SCSI_MLQUEUE_HOST_BUSY; - goto out_unlock; - case UFSHCD_STATE_ERROR: - set_host_byte(cmd, DID_ERROR); - cmd->scsi_done(cmd); - goto out_unlock; - default: - dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", - __func__, hba->ufshcd_state); - set_host_byte(cmd, DID_BAD_TARGET); - cmd->scsi_done(cmd); - goto out_unlock; - } - - /* if error handling is in progress, don't issue commands */ - if (ufshcd_eh_in_progress(hba)) { - set_host_byte(cmd, DID_ERROR); - cmd->scsi_done(cmd); - goto out_unlock; - } - spin_unlock_irqrestore(hba->host->host_lock, flags); - hba->req_abort_count = 0; err = ufshcd_hold(hba, true); @@ -2578,11 +2551,50 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) /* Make sure descriptors are ready before ringing the doorbell */ wmb(); - /* issue command to the controller */ spin_lock_irqsave(hba->host->host_lock, flags); + switch (hba->ufshcd_state) { + case UFSHCD_STATE_OPERATIONAL: + case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL: + break; + case UFSHCD_STATE_EH_SCHEDULED_FATAL: + /* + * If we are here, eh_work is either scheduled or running. + * Before eh_work sets ufshcd_state to STATE_RESET, it flushes + * runtime PM ops by calling pm_runtime_get_sync(). If a scsi + * cmd, e.g. the SSU cmd, is sent by PM ops, it can never be + * finished if we let SCSI layer keep retrying it, which gets + * eh_work stuck forever. Neither can we let it pass, because + * ufs now is not in good status, so the SSU cmd may eventually + * time out, blocking eh_work for too long. So just let it fail. + */ + if (hba->pm_op_in_progress) { + hba->force_reset = true; + set_host_byte(cmd, DID_BAD_TARGET); + goto out_compl_cmd; + } + case UFSHCD_STATE_RESET: + err = SCSI_MLQUEUE_HOST_BUSY; + goto out_compl_cmd; + case UFSHCD_STATE_ERROR: + set_host_byte(cmd, DID_ERROR); + goto out_compl_cmd; + default: + dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", + __func__, hba->ufshcd_state); + set_host_byte(cmd, DID_BAD_TARGET); + goto out_compl_cmd; + } ufshcd_send_command(hba, tag); -out_unlock: spin_unlock_irqrestore(hba->host->host_lock, flags); + goto out; + +out_compl_cmd: + scsi_dma_unmap(lrbp->cmd); + lrbp->cmd = NULL; + spin_unlock_irqrestore(hba->host->host_lock, flags); + ufshcd_release(hba); + if (!err) + cmd->scsi_done(cmd); out: up_read(&hba->clk_scaling_lock); return err; @@ -5553,7 +5565,11 @@ static inline void ufshcd_schedule_eh_work(struct ufs_hba *hba) { /* handle fatal errors only when link is not in error state */ if (hba->ufshcd_state != UFSHCD_STATE_ERROR) { - hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED; + if (hba->force_reset || ufshcd_is_link_broken(hba) || + ufshcd_is_saved_err_fatal(hba)) + hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL; + else + hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL; queue_work(hba->eh_wq, &hba->eh_work); } } @@ -5659,6 +5675,7 @@ static void ufshcd_err_handler(struct work_struct *work) spin_unlock_irqrestore(hba->host->host_lock, flags); ufshcd_err_handling_prepare(hba); spin_lock_irqsave(hba->host->host_lock, flags); + ufshcd_scsi_block_requests(hba); hba->ufshcd_state = UFSHCD_STATE_RESET; /* Complete requests that have door-bell cleared by h/w */ @@ -5912,9 +5929,6 @@ static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba) */ hba->saved_err |= hba->errors; hba->saved_uic_err |= hba->uic_error; - - /* block commands from scsi mid-layer */ - ufshcd_scsi_block_requests(hba); ufshcd_schedule_eh_work(hba); retval |= IRQ_HANDLED; } -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.