linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [UFS v3] UFS fixes
@ 2020-10-24 15:06 Jaegeuk Kim
  2020-10-24 15:06 ` [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable Jaegeuk Kim
                   ` (4 more replies)
  0 siblings, 5 replies; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-24 15:06 UTC (permalink / raw)
  To: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team
  Cc: cang, alim.akhtar, avri.altman, bvanassche

Change log from v2:
 - use active_req-- instead of __ufshcd_release to avoid UFS timeout

Change log from v1:
 - remove clkgating_enable check in __ufshcd_release
 - use __uhfshcd_release instead of active_req.



^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable
  2020-10-24 15:06 [UFS v3] UFS fixes Jaegeuk Kim
@ 2020-10-24 15:06 ` Jaegeuk Kim
  2020-10-26  3:28   ` Can Guo
  2020-10-24 15:06 ` [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs Jaegeuk Kim
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-24 15:06 UTC (permalink / raw)
  To: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team
  Cc: cang, alim.akhtar, avri.altman, bvanassche, Jaegeuk Kim

From: Jaegeuk Kim <jaegeuk@google.com>

When giving a stress test which enables/disables clkgating, we hit device
timeout sometimes. This patch avoids subtle racy condition to address it.

If we use __ufshcd_release(), I've seen that gate_work can be called in parallel
with ungate_work, which results in UFS timeout when doing hibern8.
Should avoid it.

Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
---
 drivers/scsi/ufs/ufshcd.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
index b8f573a02713..e0b479f9eb8a 100644
--- a/drivers/scsi/ufs/ufshcd.c
+++ b/drivers/scsi/ufs/ufshcd.c
@@ -1807,19 +1807,19 @@ static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
 		return -EINVAL;
 
 	value = !!value;
+
+	spin_lock_irqsave(hba->host->host_lock, flags);
 	if (value == hba->clk_gating.is_enabled)
 		goto out;
 
-	if (value) {
-		ufshcd_release(hba);
-	} else {
-		spin_lock_irqsave(hba->host->host_lock, flags);
+	if (value)
+		hba->clk_gating.active_reqs--;
+	else
 		hba->clk_gating.active_reqs++;
-		spin_unlock_irqrestore(hba->host->host_lock, flags);
-	}
 
 	hba->clk_gating.is_enabled = value;
 out:
+	spin_unlock_irqrestore(hba->host->host_lock, flags);
 	return count;
 }
 
-- 
2.29.0.rc1.297.gfa9743e501-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs
  2020-10-24 15:06 [UFS v3] UFS fixes Jaegeuk Kim
  2020-10-24 15:06 ` [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable Jaegeuk Kim
@ 2020-10-24 15:06 ` Jaegeuk Kim
  2020-10-26 18:25   ` asutoshd
  2020-10-27 17:48   ` kernel test robot
  2020-10-24 15:06 ` [PATCH v3 3/5] scsi: ufs: use WQ_HIGHPRI for gating work Jaegeuk Kim
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-24 15:06 UTC (permalink / raw)
  To: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team
  Cc: cang, alim.akhtar, avri.altman, bvanassche, Jaegeuk Kim

From: Jaegeuk Kim <jaegeuk@google.com>

In order to conduct FFU or RPMB operations, UFS needs to clear UAC. This patch
clears it explicitly, so that we could get no failure given early execution.

Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
---
 drivers/scsi/ufs/ufshcd.c | 70 +++++++++++++++++++++++++++++++++++----
 drivers/scsi/ufs/ufshcd.h |  1 +
 2 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
index e0b479f9eb8a..011e80a21170 100644
--- a/drivers/scsi/ufs/ufshcd.c
+++ b/drivers/scsi/ufs/ufshcd.c
@@ -7057,7 +7057,6 @@ static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
 static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
 {
 	int ret = 0;
-	struct scsi_device *sdev_rpmb;
 	struct scsi_device *sdev_boot;
 
 	hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
@@ -7070,14 +7069,14 @@ static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
 	ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device);
 	scsi_device_put(hba->sdev_ufs_device);
 
-	sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
+	hba->sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
 		ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
-	if (IS_ERR(sdev_rpmb)) {
-		ret = PTR_ERR(sdev_rpmb);
+	if (IS_ERR(hba->sdev_rpmb)) {
+		ret = PTR_ERR(hba->sdev_rpmb);
 		goto remove_sdev_ufs_device;
 	}
-	ufshcd_blk_pm_runtime_init(sdev_rpmb);
-	scsi_device_put(sdev_rpmb);
+	ufshcd_blk_pm_runtime_init(hba->sdev_rpmb);
+	scsi_device_put(hba->sdev_rpmb);
 
 	sdev_boot = __scsi_add_device(hba->host, 0, 0,
 		ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
@@ -7601,6 +7600,63 @@ static int ufshcd_add_lus(struct ufs_hba *hba)
 	return ret;
 }
 
+static int
+ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp);
+
+static int ufshcd_clear_ua_wlun(struct ufs_hba *hba, u8 wlun)
+{
+	struct scsi_device *sdp;
+	unsigned long flags;
+	int ret = 0;
+
+	spin_lock_irqsave(hba->host->host_lock, flags);
+	if (wlun  == UFS_UPIU_UFS_DEVICE_WLUN)
+		sdp = hba->sdev_ufs_device;
+	else if (wlun  == UFS_UPIU_RPMB_WLUN)
+		sdp = hba->sdev_rpmb;
+	else
+		BUG_ON(1);
+	if (sdp) {
+		ret = scsi_device_get(sdp);
+		if (!ret && !scsi_device_online(sdp)) {
+			ret = -ENODEV;
+			scsi_device_put(sdp);
+		}
+	} else {
+		ret = -ENODEV;
+	}
+	spin_unlock_irqrestore(hba->host->host_lock, flags);
+	if (ret)
+		goto out_err;
+
+	ret = ufshcd_send_request_sense(hba, sdp);
+	scsi_device_put(sdp);
+out_err:
+	if (ret)
+		dev_err(hba->dev, "%s: UAC clear LU=%x ret = %d\n",
+				__func__, wlun, ret);
+	return ret;
+}
+
+static int ufshcd_clear_ua_wluns(struct ufs_hba *hba)
+{
+	int ret = 0;
+
+	if (!hba->wlun_dev_clr_ua)
+		goto out;
+
+	ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_UFS_DEVICE_WLUN);
+	if (!ret)
+		ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_RPMB_WLUN);
+	if (!ret)
+		hba->wlun_dev_clr_ua = false;
+out:
+	if (ret)
+		dev_err(hba->dev, "%s: Failed to clear UAC WLUNS ret = %d\n",
+				__func__, ret);
+	return ret;
+}
+
 /**
  * ufshcd_probe_hba - probe hba to detect device and initialize
  * @hba: per-adapter instance
@@ -7720,6 +7776,8 @@ static void ufshcd_async_scan(void *data, async_cookie_t cookie)
 		pm_runtime_put_sync(hba->dev);
 		ufshcd_exit_clk_scaling(hba);
 		ufshcd_hba_exit(hba);
+	} else {
+		ufshcd_clear_ua_wluns(hba);
 	}
 }
 
diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h
index 47eb1430274c..718881d038f5 100644
--- a/drivers/scsi/ufs/ufshcd.h
+++ b/drivers/scsi/ufs/ufshcd.h
@@ -681,6 +681,7 @@ struct ufs_hba {
 	 * "UFS device" W-LU.
 	 */
 	struct scsi_device *sdev_ufs_device;
+	struct scsi_device *sdev_rpmb;
 
 	enum ufs_dev_pwr_mode curr_dev_pwr_mode;
 	enum uic_link_state uic_link_state;
-- 
2.29.0.rc1.297.gfa9743e501-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v3 3/5] scsi: ufs: use WQ_HIGHPRI for gating work
  2020-10-24 15:06 [UFS v3] UFS fixes Jaegeuk Kim
  2020-10-24 15:06 ` [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable Jaegeuk Kim
  2020-10-24 15:06 ` [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs Jaegeuk Kim
@ 2020-10-24 15:06 ` Jaegeuk Kim
  2020-10-26 18:27   ` asutoshd
  2020-10-24 15:06 ` [PATCH v3 4/5] scsi: add more contexts in the ufs tracepoints Jaegeuk Kim
  2020-10-24 15:06 ` [PATCH v3 5/5] scsi: ufs: fix clkgating on/off correctly Jaegeuk Kim
  4 siblings, 1 reply; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-24 15:06 UTC (permalink / raw)
  To: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team
  Cc: cang, alim.akhtar, avri.altman, bvanassche, Jaegeuk Kim

From: Jaegeuk Kim <jaegeuk@google.com>

Must have WQ_MEM_RECLAIM
``WQ_MEM_RECLAIM``
  All wq which might be used in the memory reclaim paths **MUST**
  have this flag set.  The wq is guaranteed to have at least one
  execution context regardless of memory pressure.

Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
---
 drivers/scsi/ufs/ufshcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
index 011e80a21170..bc0d623aed66 100644
--- a/drivers/scsi/ufs/ufshcd.c
+++ b/drivers/scsi/ufs/ufshcd.c
@@ -1867,7 +1867,7 @@ static void ufshcd_init_clk_gating(struct ufs_hba *hba)
 	snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
 		 hba->host->host_no);
 	hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
-							   WQ_MEM_RECLAIM);
+					WQ_MEM_RECLAIM | WQ_HIGHPRI);
 
 	hba->clk_gating.is_enabled = true;
 
-- 
2.29.0.rc1.297.gfa9743e501-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v3 4/5] scsi: add more contexts in the ufs tracepoints
  2020-10-24 15:06 [UFS v3] UFS fixes Jaegeuk Kim
                   ` (2 preceding siblings ...)
  2020-10-24 15:06 ` [PATCH v3 3/5] scsi: ufs: use WQ_HIGHPRI for gating work Jaegeuk Kim
@ 2020-10-24 15:06 ` Jaegeuk Kim
  2020-10-24 15:06 ` [PATCH v3 5/5] scsi: ufs: fix clkgating on/off correctly Jaegeuk Kim
  4 siblings, 0 replies; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-24 15:06 UTC (permalink / raw)
  To: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team
  Cc: cang, alim.akhtar, avri.altman, bvanassche, Jaegeuk Kim

From: Jaegeuk Kim <jaegeuk@google.com>

This adds user-friendly tracepoints with group id.

Reviewed-by: Can Guo <cang@codeaurora.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
---
 drivers/scsi/ufs/ufshcd.c  |  6 ++++--
 include/trace/events/ufs.h | 21 +++++++++++++++++----
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
index bc0d623aed66..c94610cbecae 100644
--- a/drivers/scsi/ufs/ufshcd.c
+++ b/drivers/scsi/ufs/ufshcd.c
@@ -348,7 +348,7 @@ static void ufshcd_add_command_trace(struct ufs_hba *hba,
 		unsigned int tag, const char *str)
 {
 	sector_t lba = -1;
-	u8 opcode = 0;
+	u8 opcode = 0, group_id = 0;
 	u32 intr, doorbell;
 	struct ufshcd_lrb *lrbp = &hba->lrb[tag];
 	struct scsi_cmnd *cmd = lrbp->cmd;
@@ -374,13 +374,15 @@ static void ufshcd_add_command_trace(struct ufs_hba *hba,
 				lba = cmd->request->bio->bi_iter.bi_sector;
 			transfer_len = be32_to_cpu(
 				lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
+			if (opcode == WRITE_10)
+				group_id = lrbp->cmd->cmnd[6];
 		}
 	}
 
 	intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
 	doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
 	trace_ufshcd_command(dev_name(hba->dev), str, tag,
-				doorbell, transfer_len, intr, lba, opcode);
+			doorbell, transfer_len, intr, lba, opcode, group_id);
 }
 
 static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
diff --git a/include/trace/events/ufs.h b/include/trace/events/ufs.h
index 84841b3a7ffd..50654f352639 100644
--- a/include/trace/events/ufs.h
+++ b/include/trace/events/ufs.h
@@ -11,6 +11,15 @@
 
 #include <linux/tracepoint.h>
 
+#define str_opcode(opcode)						\
+	__print_symbolic(opcode,					\
+		{ WRITE_16,		"WRITE_16" },			\
+		{ WRITE_10,		"WRITE_10" },			\
+		{ READ_16,		"READ_16" },			\
+		{ READ_10,		"READ_10" },			\
+		{ SYNCHRONIZE_CACHE,	"SYNC" },			\
+		{ UNMAP,		"UNMAP" })
+
 #define UFS_LINK_STATES			\
 	EM(UIC_LINK_OFF_STATE)		\
 	EM(UIC_LINK_ACTIVE_STATE)	\
@@ -215,9 +224,10 @@ DEFINE_EVENT(ufshcd_template, ufshcd_init,
 TRACE_EVENT(ufshcd_command,
 	TP_PROTO(const char *dev_name, const char *str, unsigned int tag,
 			u32 doorbell, int transfer_len, u32 intr, u64 lba,
-			u8 opcode),
+			u8 opcode, u8 group_id),
 
-	TP_ARGS(dev_name, str, tag, doorbell, transfer_len, intr, lba, opcode),
+	TP_ARGS(dev_name, str, tag, doorbell, transfer_len,
+				intr, lba, opcode, group_id),
 
 	TP_STRUCT__entry(
 		__string(dev_name, dev_name)
@@ -228,6 +238,7 @@ TRACE_EVENT(ufshcd_command,
 		__field(u32, intr)
 		__field(u64, lba)
 		__field(u8, opcode)
+		__field(u8, group_id)
 	),
 
 	TP_fast_assign(
@@ -239,13 +250,15 @@ TRACE_EVENT(ufshcd_command,
 		__entry->intr = intr;
 		__entry->lba = lba;
 		__entry->opcode = opcode;
+		__entry->group_id = group_id;
 	),
 
 	TP_printk(
-		"%s: %s: tag: %u, DB: 0x%x, size: %d, IS: %u, LBA: %llu, opcode: 0x%x",
+		"%s: %s: tag: %u, DB: 0x%x, size: %d, IS: %u, LBA: %llu, opcode: 0x%x (%s), group_id: 0x%x",
 		__get_str(str), __get_str(dev_name), __entry->tag,
 		__entry->doorbell, __entry->transfer_len,
-		__entry->intr, __entry->lba, (u32)__entry->opcode
+		__entry->intr, __entry->lba, (u32)__entry->opcode,
+		str_opcode(__entry->opcode), (u32)__entry->group_id
 	)
 );
 
-- 
2.29.0.rc1.297.gfa9743e501-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v3 5/5] scsi: ufs: fix clkgating on/off correctly
  2020-10-24 15:06 [UFS v3] UFS fixes Jaegeuk Kim
                   ` (3 preceding siblings ...)
  2020-10-24 15:06 ` [PATCH v3 4/5] scsi: add more contexts in the ufs tracepoints Jaegeuk Kim
@ 2020-10-24 15:06 ` Jaegeuk Kim
  2020-10-26 18:33   ` asutoshd
  4 siblings, 1 reply; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-24 15:06 UTC (permalink / raw)
  To: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team
  Cc: cang, alim.akhtar, avri.altman, bvanassche, Jaegeuk Kim

The below call stack prevents clk_gating at every IO completion.
We can remove the condition, ufshcd_any_tag_in_use(), since clkgating_work
will check it again.

ufshcd_complete_requests(struct ufs_hba *hba)
  ufshcd_transfer_req_compl()
    __ufshcd_transfer_req_compl()
      __ufshcd_release(hba)
        if (ufshcd_any_tag_in_use() == 1)
           return;
  ufshcd_tmc_handler(hba);
    blk_mq_tagset_busy_iter();

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 drivers/scsi/ufs/ufshcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
index c94610cbecae..38043c6b8d5f 100644
--- a/drivers/scsi/ufs/ufshcd.c
+++ b/drivers/scsi/ufs/ufshcd.c
@@ -1746,7 +1746,7 @@ static void __ufshcd_release(struct ufs_hba *hba)
 
 	if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
 	    hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
-	    ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks ||
+	    hba->outstanding_tasks ||
 	    hba->active_uic_cmd || hba->uic_async_done)
 		return;
 
-- 
2.29.0.rc1.297.gfa9743e501-goog


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable
  2020-10-24 15:06 ` [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable Jaegeuk Kim
@ 2020-10-26  3:28   ` Can Guo
  2020-10-26  6:13     ` Jaegeuk Kim
  0 siblings, 1 reply; 16+ messages in thread
From: Can Guo @ 2020-10-26  3:28 UTC (permalink / raw)
  To: Jaegeuk Kim
  Cc: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team,
	alim.akhtar, avri.altman, bvanassche, Jaegeuk Kim

On 2020-10-24 23:06, Jaegeuk Kim wrote:
> From: Jaegeuk Kim <jaegeuk@google.com>
> 
> When giving a stress test which enables/disables clkgating, we hit 
> device
> timeout sometimes. This patch avoids subtle racy condition to address 
> it.
> 
> If we use __ufshcd_release(), I've seen that gate_work can be called in 
> parallel
> with ungate_work, which results in UFS timeout when doing hibern8.
> Should avoid it.
> 

I don't understand this comment. gate_work and ungate_work are queued on
an ordered workqueue and an ordered workqueue executes at most one work 
item
at any given time in the queued order. How can the two run in parallel?

Thanks,

Can Guo.

> Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
> ---
>  drivers/scsi/ufs/ufshcd.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> index b8f573a02713..e0b479f9eb8a 100644
> --- a/drivers/scsi/ufs/ufshcd.c
> +++ b/drivers/scsi/ufs/ufshcd.c
> @@ -1807,19 +1807,19 @@ static ssize_t
> ufshcd_clkgate_enable_store(struct device *dev,
>  		return -EINVAL;
> 
>  	value = !!value;
> +
> +	spin_lock_irqsave(hba->host->host_lock, flags);
>  	if (value == hba->clk_gating.is_enabled)
>  		goto out;
> 
> -	if (value) {
> -		ufshcd_release(hba);
> -	} else {
> -		spin_lock_irqsave(hba->host->host_lock, flags);
> +	if (value)
> +		hba->clk_gating.active_reqs--;
> +	else
>  		hba->clk_gating.active_reqs++;
> -		spin_unlock_irqrestore(hba->host->host_lock, flags);
> -	}
> 
>  	hba->clk_gating.is_enabled = value;
>  out:
> +	spin_unlock_irqrestore(hba->host->host_lock, flags);
>  	return count;
>  }

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable
  2020-10-26  3:28   ` Can Guo
@ 2020-10-26  6:13     ` Jaegeuk Kim
  2020-10-26  6:43       ` Can Guo
  0 siblings, 1 reply; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-26  6:13 UTC (permalink / raw)
  To: Can Guo
  Cc: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team,
	alim.akhtar, avri.altman, bvanassche

On 10/26, Can Guo wrote:
> On 2020-10-24 23:06, Jaegeuk Kim wrote:
> > From: Jaegeuk Kim <jaegeuk@google.com>
> > 
> > When giving a stress test which enables/disables clkgating, we hit
> > device
> > timeout sometimes. This patch avoids subtle racy condition to address
> > it.
> > 
> > If we use __ufshcd_release(), I've seen that gate_work can be called in
> > parallel
> > with ungate_work, which results in UFS timeout when doing hibern8.
> > Should avoid it.
> > 
> 
> I don't understand this comment. gate_work and ungate_work are queued on
> an ordered workqueue and an ordered workqueue executes at most one work item
> at any given time in the queued order. How can the two run in parallel?

When I hit UFS stuck, I saw this by clkgating tracepoint.

- REQ_CLK_OFF
- CLKS_OFF
- REQ_CLK_OFF
- REQ_CLKS_ON
..

By using active_req, I don't see any problem.

> 
> Thanks,
> 
> Can Guo.
> 
> > Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
> > ---
> >  drivers/scsi/ufs/ufshcd.c | 12 ++++++------
> >  1 file changed, 6 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> > index b8f573a02713..e0b479f9eb8a 100644
> > --- a/drivers/scsi/ufs/ufshcd.c
> > +++ b/drivers/scsi/ufs/ufshcd.c
> > @@ -1807,19 +1807,19 @@ static ssize_t
> > ufshcd_clkgate_enable_store(struct device *dev,
> >  		return -EINVAL;
> > 
> >  	value = !!value;
> > +
> > +	spin_lock_irqsave(hba->host->host_lock, flags);
> >  	if (value == hba->clk_gating.is_enabled)
> >  		goto out;
> > 
> > -	if (value) {
> > -		ufshcd_release(hba);
> > -	} else {
> > -		spin_lock_irqsave(hba->host->host_lock, flags);
> > +	if (value)
> > +		hba->clk_gating.active_reqs--;
> > +	else
> >  		hba->clk_gating.active_reqs++;
> > -		spin_unlock_irqrestore(hba->host->host_lock, flags);
> > -	}
> > 
> >  	hba->clk_gating.is_enabled = value;
> >  out:
> > +	spin_unlock_irqrestore(hba->host->host_lock, flags);
> >  	return count;
> >  }

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable
  2020-10-26  6:13     ` Jaegeuk Kim
@ 2020-10-26  6:43       ` Can Guo
  2020-10-26 19:48         ` Jaegeuk Kim
  0 siblings, 1 reply; 16+ messages in thread
From: Can Guo @ 2020-10-26  6:43 UTC (permalink / raw)
  To: Jaegeuk Kim
  Cc: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team,
	alim.akhtar, avri.altman, bvanassche

On 2020-10-26 14:13, Jaegeuk Kim wrote:
> On 10/26, Can Guo wrote:
>> On 2020-10-24 23:06, Jaegeuk Kim wrote:
>> > From: Jaegeuk Kim <jaegeuk@google.com>
>> >
>> > When giving a stress test which enables/disables clkgating, we hit
>> > device
>> > timeout sometimes. This patch avoids subtle racy condition to address
>> > it.
>> >
>> > If we use __ufshcd_release(), I've seen that gate_work can be called in
>> > parallel
>> > with ungate_work, which results in UFS timeout when doing hibern8.
>> > Should avoid it.
>> >
>> 
>> I don't understand this comment. gate_work and ungate_work are queued 
>> on
>> an ordered workqueue and an ordered workqueue executes at most one 
>> work item
>> at any given time in the queued order. How can the two run in 
>> parallel?
> 
> When I hit UFS stuck, I saw this by clkgating tracepoint.
> 
> - REQ_CLK_OFF
> - CLKS_OFF
> - REQ_CLK_OFF
> - REQ_CLKS_ON
> ..
> 

I don't see how can you tell that the two works are running in parallel
just from above trace. May I know what is the exact error by "UFS 
timeout
when doing hibern8"?

By using __ufshcd_release() here, I do see one potential issue if your 
test
quickly toggles on/off of clk_gating - disable it, enable it, disable it 
and
enable it, which will cause that __ufshcd_release() being called twice, 
meaning
we queue two gate_works back to back. So can you try below code and let 
me know
if it helps or not? I am OK with your current change, but I would like 
to
understand the problem. Thanks.

diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
index 1791bce..3eee438 100644
--- a/drivers/scsi/ufs/ufshcd.c
+++ b/drivers/scsi/ufs/ufshcd.c
@@ -2271,6 +2271,8 @@ static void ufshcd_gate_work(struct work_struct 
*work)
         unsigned long flags;

         spin_lock_irqsave(hba->host->host_lock, flags);
+       if (hba->clk_gating.state == CLKS_OFF)
+               goto rel_lock;
         /*
          * In case you are here to cancel this work the gating state
          * would be marked as REQ_CLKS_ON. In this case save time by

Regards,

Can Guo.

> By using active_req, I don't see any problem.
> 
>> 
>> Thanks,
>> 
>> Can Guo.
>> 
>> > Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
>> > ---
>> >  drivers/scsi/ufs/ufshcd.c | 12 ++++++------
>> >  1 file changed, 6 insertions(+), 6 deletions(-)
>> >
>> > diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
>> > index b8f573a02713..e0b479f9eb8a 100644
>> > --- a/drivers/scsi/ufs/ufshcd.c
>> > +++ b/drivers/scsi/ufs/ufshcd.c
>> > @@ -1807,19 +1807,19 @@ static ssize_t
>> > ufshcd_clkgate_enable_store(struct device *dev,
>> >  		return -EINVAL;
>> >
>> >  	value = !!value;
>> > +
>> > +	spin_lock_irqsave(hba->host->host_lock, flags);
>> >  	if (value == hba->clk_gating.is_enabled)
>> >  		goto out;
>> >
>> > -	if (value) {
>> > -		ufshcd_release(hba);
>> > -	} else {
>> > -		spin_lock_irqsave(hba->host->host_lock, flags);
>> > +	if (value)
>> > +		hba->clk_gating.active_reqs--;
>> > +	else
>> >  		hba->clk_gating.active_reqs++;
>> > -		spin_unlock_irqrestore(hba->host->host_lock, flags);
>> > -	}
>> >
>> >  	hba->clk_gating.is_enabled = value;
>> >  out:
>> > +	spin_unlock_irqrestore(hba->host->host_lock, flags);
>> >  	return count;
>> >  }

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs
  2020-10-24 15:06 ` [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs Jaegeuk Kim
@ 2020-10-26 18:25   ` asutoshd
  2020-10-26 19:43     ` Jaegeuk Kim
  2020-10-27 17:48   ` kernel test robot
  1 sibling, 1 reply; 16+ messages in thread
From: asutoshd @ 2020-10-26 18:25 UTC (permalink / raw)
  To: Jaegeuk Kim
  Cc: linux-kernel, linux-,
	linux-f2fs-devel, kernel-team, cang, alim.akhtar, avri.altman,
	bvanassche, Jaegeuk Kim

On 2020-10-24 08:06, Jaegeuk Kim wrote:
> From: Jaegeuk Kim <jaegeuk@google.com>
> 
> In order to conduct FFU or RPMB operations, UFS needs to clear UAC. 
> This patch
> clears it explicitly, so that we could get no failure given early 
> execution.
> 

What's the meaning of 'given early execution'?

> Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
> ---
>  drivers/scsi/ufs/ufshcd.c | 70 +++++++++++++++++++++++++++++++++++----
>  drivers/scsi/ufs/ufshcd.h |  1 +
>  2 files changed, 65 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> index e0b479f9eb8a..011e80a21170 100644
> --- a/drivers/scsi/ufs/ufshcd.c
> +++ b/drivers/scsi/ufs/ufshcd.c
> @@ -7057,7 +7057,6 @@ static inline void
> ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
>  static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
>  {
>  	int ret = 0;
> -	struct scsi_device *sdev_rpmb;
>  	struct scsi_device *sdev_boot;
> 
>  	hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
> @@ -7070,14 +7069,14 @@ static int ufshcd_scsi_add_wlus(struct ufs_hba 
> *hba)
>  	ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device);
>  	scsi_device_put(hba->sdev_ufs_device);
> 
> -	sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
> +	hba->sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
>  		ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
> -	if (IS_ERR(sdev_rpmb)) {
> -		ret = PTR_ERR(sdev_rpmb);
> +	if (IS_ERR(hba->sdev_rpmb)) {
> +		ret = PTR_ERR(hba->sdev_rpmb);
>  		goto remove_sdev_ufs_device;
>  	}
> -	ufshcd_blk_pm_runtime_init(sdev_rpmb);
> -	scsi_device_put(sdev_rpmb);
> +	ufshcd_blk_pm_runtime_init(hba->sdev_rpmb);
> +	scsi_device_put(hba->sdev_rpmb);
> 
>  	sdev_boot = __scsi_add_device(hba->host, 0, 0,
>  		ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
> @@ -7601,6 +7600,63 @@ static int ufshcd_add_lus(struct ufs_hba *hba)
>  	return ret;
>  }
> 
> +static int
> +ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device 
> *sdp);
> +
> +static int ufshcd_clear_ua_wlun(struct ufs_hba *hba, u8 wlun)
> +{
> +	struct scsi_device *sdp;
> +	unsigned long flags;
> +	int ret = 0;
> +
> +	spin_lock_irqsave(hba->host->host_lock, flags);
> +	if (wlun  == UFS_UPIU_UFS_DEVICE_WLUN)
> +		sdp = hba->sdev_ufs_device;
> +	else if (wlun  == UFS_UPIU_RPMB_WLUN)
> +		sdp = hba->sdev_rpmb;
> +	else
> +		BUG_ON(1);
> +	if (sdp) {
> +		ret = scsi_device_get(sdp);
> +		if (!ret && !scsi_device_online(sdp)) {
> +			ret = -ENODEV;
> +			scsi_device_put(sdp);
> +		}
> +	} else {
> +		ret = -ENODEV;
> +	}
> +	spin_unlock_irqrestore(hba->host->host_lock, flags);
> +	if (ret)
> +		goto out_err;
> +
> +	ret = ufshcd_send_request_sense(hba, sdp);
> +	scsi_device_put(sdp);
> +out_err:
> +	if (ret)
> +		dev_err(hba->dev, "%s: UAC clear LU=%x ret = %d\n",
> +				__func__, wlun, ret);
> +	return ret;
> +}
> +
> +static int ufshcd_clear_ua_wluns(struct ufs_hba *hba)
> +{
> +	int ret = 0;
> +
> +	if (!hba->wlun_dev_clr_ua)
> +		goto out;
> +
> +	ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_UFS_DEVICE_WLUN);
> +	if (!ret)
> +		ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_RPMB_WLUN);
> +	if (!ret)
> +		hba->wlun_dev_clr_ua = false;
> +out:
> +	if (ret)
> +		dev_err(hba->dev, "%s: Failed to clear UAC WLUNS ret = %d\n",
> +				__func__, ret);
> +	return ret;
> +}
> +
>  /**
>   * ufshcd_probe_hba - probe hba to detect device and initialize
>   * @hba: per-adapter instance
> @@ -7720,6 +7776,8 @@ static void ufshcd_async_scan(void *data,
> async_cookie_t cookie)
>  		pm_runtime_put_sync(hba->dev);
>  		ufshcd_exit_clk_scaling(hba);
>  		ufshcd_hba_exit(hba);
> +	} else {
> +		ufshcd_clear_ua_wluns(hba);
>  	}
>  }
> 
> diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h
> index 47eb1430274c..718881d038f5 100644
> --- a/drivers/scsi/ufs/ufshcd.h
> +++ b/drivers/scsi/ufs/ufshcd.h
> @@ -681,6 +681,7 @@ struct ufs_hba {
>  	 * "UFS device" W-LU.
>  	 */
>  	struct scsi_device *sdev_ufs_device;
> +	struct scsi_device *sdev_rpmb;
> 
>  	enum ufs_dev_pwr_mode curr_dev_pwr_mode;
>  	enum uic_link_state uic_link_state;

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 3/5] scsi: ufs: use WQ_HIGHPRI for gating work
  2020-10-24 15:06 ` [PATCH v3 3/5] scsi: ufs: use WQ_HIGHPRI for gating work Jaegeuk Kim
@ 2020-10-26 18:27   ` asutoshd
  0 siblings, 0 replies; 16+ messages in thread
From: asutoshd @ 2020-10-26 18:27 UTC (permalink / raw)
  To: Jaegeuk Kim
  Cc: linux-kernel, linux-,
	linux-f2fs-devel, kernel-team, cang, alim.akhtar, avri.altman,
	bvanassche, Jaegeuk Kim

On 2020-10-24 08:06, Jaegeuk Kim wrote:
> From: Jaegeuk Kim <jaegeuk@google.com>
> 
> Must have WQ_MEM_RECLAIM
> ``WQ_MEM_RECLAIM``
>   All wq which might be used in the memory reclaim paths **MUST**
>   have this flag set.  The wq is guaranteed to have at least one
>   execution context regardless of memory pressure.
> 
> Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
> ---

Reviewed-by: Asutosh Das <asutoshd@codeaurora.org>

>  drivers/scsi/ufs/ufshcd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> index 011e80a21170..bc0d623aed66 100644
> --- a/drivers/scsi/ufs/ufshcd.c
> +++ b/drivers/scsi/ufs/ufshcd.c
> @@ -1867,7 +1867,7 @@ static void ufshcd_init_clk_gating(struct ufs_hba 
> *hba)
>  	snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
>  		 hba->host->host_no);
>  	hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
> -							   WQ_MEM_RECLAIM);
> +					WQ_MEM_RECLAIM | WQ_HIGHPRI);
> 
>  	hba->clk_gating.is_enabled = true;

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 5/5] scsi: ufs: fix clkgating on/off correctly
  2020-10-24 15:06 ` [PATCH v3 5/5] scsi: ufs: fix clkgating on/off correctly Jaegeuk Kim
@ 2020-10-26 18:33   ` asutoshd
  0 siblings, 0 replies; 16+ messages in thread
From: asutoshd @ 2020-10-26 18:33 UTC (permalink / raw)
  To: Jaegeuk Kim
  Cc: linux-kernel, linux-,
	linux-f2fs-devel, kernel-team, cang, alim.akhtar, avri.altman,
	bvanassche

On 2020-10-24 08:06, Jaegeuk Kim wrote:
> The below call stack prevents clk_gating at every IO completion.
> We can remove the condition, ufshcd_any_tag_in_use(), since 
> clkgating_work
> will check it again.
> 
> ufshcd_complete_requests(struct ufs_hba *hba)
>   ufshcd_transfer_req_compl()
>     __ufshcd_transfer_req_compl()
>       __ufshcd_release(hba)
>         if (ufshcd_any_tag_in_use() == 1)
>            return;
>   ufshcd_tmc_handler(hba);
>     blk_mq_tagset_busy_iter();
> 
> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> ---

Reviewed-by: Asutosh Das <asutoshd@codeaurora.org>

>  drivers/scsi/ufs/ufshcd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> index c94610cbecae..38043c6b8d5f 100644
> --- a/drivers/scsi/ufs/ufshcd.c
> +++ b/drivers/scsi/ufs/ufshcd.c
> @@ -1746,7 +1746,7 @@ static void __ufshcd_release(struct ufs_hba *hba)
> 
>  	if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
>  	    hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
> -	    ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks ||
> +	    hba->outstanding_tasks ||
>  	    hba->active_uic_cmd || hba->uic_async_done)
>  		return;

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs
  2020-10-26 18:25   ` asutoshd
@ 2020-10-26 19:43     ` Jaegeuk Kim
  0 siblings, 0 replies; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-26 19:43 UTC (permalink / raw)
  To: asutoshd
  Cc: linux-kernel, linux-,
	linux-f2fs-devel, kernel-team, cang, alim.akhtar, avri.altman,
	bvanassche

On 10/26, asutoshd@codeaurora.org wrote:
> On 2020-10-24 08:06, Jaegeuk Kim wrote:
> > From: Jaegeuk Kim <jaegeuk@google.com>
> > 
> > In order to conduct FFU or RPMB operations, UFS needs to clear UAC. This
> > patch
> > clears it explicitly, so that we could get no failure given early
> > execution.
> > 
> 
> What's the meaning of 'given early execution'?

I saw there's hba->wlun_dev_clr_ua to clear UA at ufshcd_set_dev_pwr_mode(),
and thus, assumed there's other path to clear UA. So, with this patch, user
can try FFU or RPMB requests regardless of that being done.

> 
> > Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
> > ---
> >  drivers/scsi/ufs/ufshcd.c | 70 +++++++++++++++++++++++++++++++++++----
> >  drivers/scsi/ufs/ufshcd.h |  1 +
> >  2 files changed, 65 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> > index e0b479f9eb8a..011e80a21170 100644
> > --- a/drivers/scsi/ufs/ufshcd.c
> > +++ b/drivers/scsi/ufs/ufshcd.c
> > @@ -7057,7 +7057,6 @@ static inline void
> > ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
> >  static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
> >  {
> >  	int ret = 0;
> > -	struct scsi_device *sdev_rpmb;
> >  	struct scsi_device *sdev_boot;
> > 
> >  	hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
> > @@ -7070,14 +7069,14 @@ static int ufshcd_scsi_add_wlus(struct ufs_hba
> > *hba)
> >  	ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device);
> >  	scsi_device_put(hba->sdev_ufs_device);
> > 
> > -	sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
> > +	hba->sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
> >  		ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
> > -	if (IS_ERR(sdev_rpmb)) {
> > -		ret = PTR_ERR(sdev_rpmb);
> > +	if (IS_ERR(hba->sdev_rpmb)) {
> > +		ret = PTR_ERR(hba->sdev_rpmb);
> >  		goto remove_sdev_ufs_device;
> >  	}
> > -	ufshcd_blk_pm_runtime_init(sdev_rpmb);
> > -	scsi_device_put(sdev_rpmb);
> > +	ufshcd_blk_pm_runtime_init(hba->sdev_rpmb);
> > +	scsi_device_put(hba->sdev_rpmb);
> > 
> >  	sdev_boot = __scsi_add_device(hba->host, 0, 0,
> >  		ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
> > @@ -7601,6 +7600,63 @@ static int ufshcd_add_lus(struct ufs_hba *hba)
> >  	return ret;
> >  }
> > 
> > +static int
> > +ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device
> > *sdp);
> > +
> > +static int ufshcd_clear_ua_wlun(struct ufs_hba *hba, u8 wlun)
> > +{
> > +	struct scsi_device *sdp;
> > +	unsigned long flags;
> > +	int ret = 0;
> > +
> > +	spin_lock_irqsave(hba->host->host_lock, flags);
> > +	if (wlun  == UFS_UPIU_UFS_DEVICE_WLUN)
> > +		sdp = hba->sdev_ufs_device;
> > +	else if (wlun  == UFS_UPIU_RPMB_WLUN)
> > +		sdp = hba->sdev_rpmb;
> > +	else
> > +		BUG_ON(1);
> > +	if (sdp) {
> > +		ret = scsi_device_get(sdp);
> > +		if (!ret && !scsi_device_online(sdp)) {
> > +			ret = -ENODEV;
> > +			scsi_device_put(sdp);
> > +		}
> > +	} else {
> > +		ret = -ENODEV;
> > +	}
> > +	spin_unlock_irqrestore(hba->host->host_lock, flags);
> > +	if (ret)
> > +		goto out_err;
> > +
> > +	ret = ufshcd_send_request_sense(hba, sdp);
> > +	scsi_device_put(sdp);
> > +out_err:
> > +	if (ret)
> > +		dev_err(hba->dev, "%s: UAC clear LU=%x ret = %d\n",
> > +				__func__, wlun, ret);
> > +	return ret;
> > +}
> > +
> > +static int ufshcd_clear_ua_wluns(struct ufs_hba *hba)
> > +{
> > +	int ret = 0;
> > +
> > +	if (!hba->wlun_dev_clr_ua)
> > +		goto out;
> > +
> > +	ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_UFS_DEVICE_WLUN);
> > +	if (!ret)
> > +		ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_RPMB_WLUN);
> > +	if (!ret)
> > +		hba->wlun_dev_clr_ua = false;
> > +out:
> > +	if (ret)
> > +		dev_err(hba->dev, "%s: Failed to clear UAC WLUNS ret = %d\n",
> > +				__func__, ret);
> > +	return ret;
> > +}
> > +
> >  /**
> >   * ufshcd_probe_hba - probe hba to detect device and initialize
> >   * @hba: per-adapter instance
> > @@ -7720,6 +7776,8 @@ static void ufshcd_async_scan(void *data,
> > async_cookie_t cookie)
> >  		pm_runtime_put_sync(hba->dev);
> >  		ufshcd_exit_clk_scaling(hba);
> >  		ufshcd_hba_exit(hba);
> > +	} else {
> > +		ufshcd_clear_ua_wluns(hba);
> >  	}
> >  }
> > 
> > diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h
> > index 47eb1430274c..718881d038f5 100644
> > --- a/drivers/scsi/ufs/ufshcd.h
> > +++ b/drivers/scsi/ufs/ufshcd.h
> > @@ -681,6 +681,7 @@ struct ufs_hba {
> >  	 * "UFS device" W-LU.
> >  	 */
> >  	struct scsi_device *sdev_ufs_device;
> > +	struct scsi_device *sdev_rpmb;
> > 
> >  	enum ufs_dev_pwr_mode curr_dev_pwr_mode;
> >  	enum uic_link_state uic_link_state;

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable
  2020-10-26  6:43       ` Can Guo
@ 2020-10-26 19:48         ` Jaegeuk Kim
  2020-10-27  2:44           ` Can Guo
  0 siblings, 1 reply; 16+ messages in thread
From: Jaegeuk Kim @ 2020-10-26 19:48 UTC (permalink / raw)
  To: Can Guo
  Cc: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team,
	alim.akhtar, avri.altman, bvanassche

On 10/26, Can Guo wrote:
> On 2020-10-26 14:13, Jaegeuk Kim wrote:
> > On 10/26, Can Guo wrote:
> > > On 2020-10-24 23:06, Jaegeuk Kim wrote:
> > > > From: Jaegeuk Kim <jaegeuk@google.com>
> > > >
> > > > When giving a stress test which enables/disables clkgating, we hit
> > > > device
> > > > timeout sometimes. This patch avoids subtle racy condition to address
> > > > it.
> > > >
> > > > If we use __ufshcd_release(), I've seen that gate_work can be called in
> > > > parallel
> > > > with ungate_work, which results in UFS timeout when doing hibern8.
> > > > Should avoid it.
> > > >
> > > 
> > > I don't understand this comment. gate_work and ungate_work are
> > > queued on
> > > an ordered workqueue and an ordered workqueue executes at most one
> > > work item
> > > at any given time in the queued order. How can the two run in
> > > parallel?
> > 
> > When I hit UFS stuck, I saw this by clkgating tracepoint.
> > 
> > - REQ_CLK_OFF
> > - CLKS_OFF
> > - REQ_CLK_OFF
> > - REQ_CLKS_ON
> > ..
> > 
> 
> I don't see how can you tell that the two works are running in parallel
> just from above trace. May I know what is the exact error by "UFS timeout
> when doing hibern8"?
> 
> By using __ufshcd_release() here, I do see one potential issue if your test
> quickly toggles on/off of clk_gating - disable it, enable it, disable it and
> enable it, which will cause that __ufshcd_release() being called twice,
> meaning
> we queue two gate_works back to back. So can you try below code and let me
> know
> if it helps or not? I am OK with your current change, but I would like to
> understand the problem. Thanks.
> 
> diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> index 1791bce..3eee438 100644
> --- a/drivers/scsi/ufs/ufshcd.c
> +++ b/drivers/scsi/ufs/ufshcd.c
> @@ -2271,6 +2271,8 @@ static void ufshcd_gate_work(struct work_struct *work)
>         unsigned long flags;
> 
>         spin_lock_irqsave(hba->host->host_lock, flags);
> +       if (hba->clk_gating.state == CLKS_OFF)
> +               goto rel_lock;
>         /*
>          * In case you are here to cancel this work the gating state
>          * would be marked as REQ_CLKS_ON. In this case save time by

This doesn't help. So, I checked this back again, and, like what you said, now
suspect __ufshcd_release() which changed state to REQ_CLKS_OFF on CLKS_OFF.

With the below change, I can see the issue anymore. Let me send v4.

---
 drivers/scsi/ufs/ufshcd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
index b8f573a02713..cc8d5f0c3fdc 100644
--- a/drivers/scsi/ufs/ufshcd.c
+++ b/drivers/scsi/ufs/ufshcd.c
@@ -1745,7 +1745,8 @@ static void __ufshcd_release(struct ufs_hba *hba)
 	if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
 	    hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
 	    ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks ||
-	    hba->active_uic_cmd || hba->uic_async_done)
+	    hba->active_uic_cmd || hba->uic_async_done ||
+	    hba->clk_gating.state == CLKS_OFF)
 		return;
 
 	hba->clk_gating.state = REQ_CLKS_OFF;
-- 
2.29.0.rc1.297.gfa9743e501-goog


> 
> Regards,
> 
> Can Guo.
> 
> > By using active_req, I don't see any problem.
> > 
> > > 
> > > Thanks,
> > > 
> > > Can Guo.
> > > 
> > > > Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
> > > > ---
> > > >  drivers/scsi/ufs/ufshcd.c | 12 ++++++------
> > > >  1 file changed, 6 insertions(+), 6 deletions(-)
> > > >
> > > > diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> > > > index b8f573a02713..e0b479f9eb8a 100644
> > > > --- a/drivers/scsi/ufs/ufshcd.c
> > > > +++ b/drivers/scsi/ufs/ufshcd.c
> > > > @@ -1807,19 +1807,19 @@ static ssize_t
> > > > ufshcd_clkgate_enable_store(struct device *dev,
> > > >  		return -EINVAL;
> > > >
> > > >  	value = !!value;
> > > > +
> > > > +	spin_lock_irqsave(hba->host->host_lock, flags);
> > > >  	if (value == hba->clk_gating.is_enabled)
> > > >  		goto out;
> > > >
> > > > -	if (value) {
> > > > -		ufshcd_release(hba);
> > > > -	} else {
> > > > -		spin_lock_irqsave(hba->host->host_lock, flags);
> > > > +	if (value)
> > > > +		hba->clk_gating.active_reqs--;
> > > > +	else
> > > >  		hba->clk_gating.active_reqs++;
> > > > -		spin_unlock_irqrestore(hba->host->host_lock, flags);
> > > > -	}
> > > >
> > > >  	hba->clk_gating.is_enabled = value;
> > > >  out:
> > > > +	spin_unlock_irqrestore(hba->host->host_lock, flags);
> > > >  	return count;
> > > >  }

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable
  2020-10-26 19:48         ` Jaegeuk Kim
@ 2020-10-27  2:44           ` Can Guo
  0 siblings, 0 replies; 16+ messages in thread
From: Can Guo @ 2020-10-27  2:44 UTC (permalink / raw)
  To: Jaegeuk Kim
  Cc: linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team,
	alim.akhtar, avri.altman, bvanassche

On 2020-10-27 03:48, Jaegeuk Kim wrote:
> On 10/26, Can Guo wrote:
>> On 2020-10-26 14:13, Jaegeuk Kim wrote:
>> > On 10/26, Can Guo wrote:
>> > > On 2020-10-24 23:06, Jaegeuk Kim wrote:
>> > > > From: Jaegeuk Kim <jaegeuk@google.com>
>> > > >
>> > > > When giving a stress test which enables/disables clkgating, we hit
>> > > > device
>> > > > timeout sometimes. This patch avoids subtle racy condition to address
>> > > > it.
>> > > >
>> > > > If we use __ufshcd_release(), I've seen that gate_work can be called in
>> > > > parallel
>> > > > with ungate_work, which results in UFS timeout when doing hibern8.
>> > > > Should avoid it.
>> > > >
>> > >
>> > > I don't understand this comment. gate_work and ungate_work are
>> > > queued on
>> > > an ordered workqueue and an ordered workqueue executes at most one
>> > > work item
>> > > at any given time in the queued order. How can the two run in
>> > > parallel?
>> >
>> > When I hit UFS stuck, I saw this by clkgating tracepoint.
>> >
>> > - REQ_CLK_OFF
>> > - CLKS_OFF
>> > - REQ_CLK_OFF
>> > - REQ_CLKS_ON
>> > ..
>> >
>> 
>> I don't see how can you tell that the two works are running in 
>> parallel
>> just from above trace. May I know what is the exact error by "UFS 
>> timeout
>> when doing hibern8"?
>> 
>> By using __ufshcd_release() here, I do see one potential issue if your 
>> test
>> quickly toggles on/off of clk_gating - disable it, enable it, disable 
>> it and
>> enable it, which will cause that __ufshcd_release() being called 
>> twice,
>> meaning
>> we queue two gate_works back to back. So can you try below code and 
>> let me
>> know
>> if it helps or not? I am OK with your current change, but I would like 
>> to
>> understand the problem. Thanks.
>> 
>> diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
>> index 1791bce..3eee438 100644
>> --- a/drivers/scsi/ufs/ufshcd.c
>> +++ b/drivers/scsi/ufs/ufshcd.c
>> @@ -2271,6 +2271,8 @@ static void ufshcd_gate_work(struct work_struct 
>> *work)
>>         unsigned long flags;
>> 
>>         spin_lock_irqsave(hba->host->host_lock, flags);
>> +       if (hba->clk_gating.state == CLKS_OFF)
>> +               goto rel_lock;
>>         /*
>>          * In case you are here to cancel this work the gating state
>>          * would be marked as REQ_CLKS_ON. In this case save time by
> 
> This doesn't help. So, I checked this back again, and, like what you 
> said, now
> suspect __ufshcd_release() which changed state to REQ_CLKS_OFF on 
> CLKS_OFF.
> 

Aha, sorry that I gave the right analysis but wrong fix - my check won't 
help
since it is checking CLKS_OFF, but at that moment it has become 
CLKS_REQ_OFF.
Your fix is fulfiling the right purpose.

Thanks,

Can Guo.


> With the below change, I can see the issue anymore. Let me send v4.
> 
> ---
>  drivers/scsi/ufs/ufshcd.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
> index b8f573a02713..cc8d5f0c3fdc 100644
> --- a/drivers/scsi/ufs/ufshcd.c
> +++ b/drivers/scsi/ufs/ufshcd.c
> @@ -1745,7 +1745,8 @@ static void __ufshcd_release(struct ufs_hba *hba)
>  	if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
>  	    hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
>  	    ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks ||
> -	    hba->active_uic_cmd || hba->uic_async_done)
> +	    hba->active_uic_cmd || hba->uic_async_done ||
> +	    hba->clk_gating.state == CLKS_OFF)
>  		return;
> 
>  	hba->clk_gating.state = REQ_CLKS_OFF;
> --
> 2.29.0.rc1.297.gfa9743e501-goog
> 
> 
>> 
>> Regards,
>> 
>> Can Guo.
>> 
>> > By using active_req, I don't see any problem.
>> >
>> > >
>> > > Thanks,
>> > >
>> > > Can Guo.
>> > >
>> > > > Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
>> > > > ---
>> > > >  drivers/scsi/ufs/ufshcd.c | 12 ++++++------
>> > > >  1 file changed, 6 insertions(+), 6 deletions(-)
>> > > >
>> > > > diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
>> > > > index b8f573a02713..e0b479f9eb8a 100644
>> > > > --- a/drivers/scsi/ufs/ufshcd.c
>> > > > +++ b/drivers/scsi/ufs/ufshcd.c
>> > > > @@ -1807,19 +1807,19 @@ static ssize_t
>> > > > ufshcd_clkgate_enable_store(struct device *dev,
>> > > >  		return -EINVAL;
>> > > >
>> > > >  	value = !!value;
>> > > > +
>> > > > +	spin_lock_irqsave(hba->host->host_lock, flags);
>> > > >  	if (value == hba->clk_gating.is_enabled)
>> > > >  		goto out;
>> > > >
>> > > > -	if (value) {
>> > > > -		ufshcd_release(hba);
>> > > > -	} else {
>> > > > -		spin_lock_irqsave(hba->host->host_lock, flags);
>> > > > +	if (value)
>> > > > +		hba->clk_gating.active_reqs--;
>> > > > +	else
>> > > >  		hba->clk_gating.active_reqs++;
>> > > > -		spin_unlock_irqrestore(hba->host->host_lock, flags);
>> > > > -	}
>> > > >
>> > > >  	hba->clk_gating.is_enabled = value;
>> > > >  out:
>> > > > +	spin_unlock_irqrestore(hba->host->host_lock, flags);
>> > > >  	return count;
>> > > >  }

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs
  2020-10-24 15:06 ` [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs Jaegeuk Kim
  2020-10-26 18:25   ` asutoshd
@ 2020-10-27 17:48   ` kernel test robot
  1 sibling, 0 replies; 16+ messages in thread
From: kernel test robot @ 2020-10-27 17:48 UTC (permalink / raw)
  To: Jaegeuk Kim, linux-kernel, linux-scsi, linux-f2fs-devel, kernel-team
  Cc: kbuild-all, clang-built-linux, cang, alim.akhtar, avri.altman,
	bvanassche, Jaegeuk Kim

[-- Attachment #1: Type: text/plain, Size: 4999 bytes --]

Hi Jaegeuk,

I love your patch! Perhaps something to improve:

[auto build test WARNING on scsi/for-next]
[also build test WARNING on mkp-scsi/for-next linus/master linux/master v5.10-rc1 next-20201027]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jaegeuk-Kim/scsi-ufs-atomic-update-for-clkgating_enable/20201025-000720
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi.git for-next
config: x86_64-randconfig-a012-20201027 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project f2c25c70791de95d2466e09b5b58fc37f6ccd7a4)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/48327c0914e146090df19aaa6df3f0ed569d9669
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jaegeuk-Kim/scsi-ufs-atomic-update-for-clkgating_enable/20201025-000720
        git checkout 48327c0914e146090df19aaa6df3f0ed569d9669
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/scsi/ufs/ufshcd.c:7618:3: warning: variable 'sdp' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
                   BUG_ON(1);
                   ^~~~~~~~~
   include/asm-generic/bug.h:63:32: note: expanded from macro 'BUG_ON'
   #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
                                  ^~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:56:28: note: expanded from macro 'if'
   #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:58:30: note: expanded from macro '__trace_if_var'
   #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/scsi/ufs/ufshcd.c:7619:6: note: uninitialized use occurs here
           if (sdp) {
               ^~~
   include/linux/compiler.h:56:47: note: expanded from macro 'if'
   #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                                                 ^~~~
   include/linux/compiler.h:58:52: note: expanded from macro '__trace_if_var'
   #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                                      ^~~~
   drivers/scsi/ufs/ufshcd.c:7618:3: note: remove the 'if' if its condition is always true
                   BUG_ON(1);
                   ^
   include/asm-generic/bug.h:63:32: note: expanded from macro 'BUG_ON'
   #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
                                  ^
   include/linux/compiler.h:56:23: note: expanded from macro 'if'
   #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                         ^
   drivers/scsi/ufs/ufshcd.c:7608:25: note: initialize the variable 'sdp' to silence this warning
           struct scsi_device *sdp;
                                  ^
                                   = NULL
   1 warning generated.

vim +7618 drivers/scsi/ufs/ufshcd.c

  7605	
  7606	static int ufshcd_clear_ua_wlun(struct ufs_hba *hba, u8 wlun)
  7607	{
  7608		struct scsi_device *sdp;
  7609		unsigned long flags;
  7610		int ret = 0;
  7611	
  7612		spin_lock_irqsave(hba->host->host_lock, flags);
  7613		if (wlun  == UFS_UPIU_UFS_DEVICE_WLUN)
  7614			sdp = hba->sdev_ufs_device;
  7615		else if (wlun  == UFS_UPIU_RPMB_WLUN)
  7616			sdp = hba->sdev_rpmb;
  7617		else
> 7618			BUG_ON(1);
  7619		if (sdp) {
  7620			ret = scsi_device_get(sdp);
  7621			if (!ret && !scsi_device_online(sdp)) {
  7622				ret = -ENODEV;
  7623				scsi_device_put(sdp);
  7624			}
  7625		} else {
  7626			ret = -ENODEV;
  7627		}
  7628		spin_unlock_irqrestore(hba->host->host_lock, flags);
  7629		if (ret)
  7630			goto out_err;
  7631	
  7632		ret = ufshcd_send_request_sense(hba, sdp);
  7633		scsi_device_put(sdp);
  7634	out_err:
  7635		if (ret)
  7636			dev_err(hba->dev, "%s: UAC clear LU=%x ret = %d\n",
  7637					__func__, wlun, ret);
  7638		return ret;
  7639	}
  7640	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 31213 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2020-10-27 17:49 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-24 15:06 [UFS v3] UFS fixes Jaegeuk Kim
2020-10-24 15:06 ` [PATCH v3 1/5] scsi: ufs: atomic update for clkgating_enable Jaegeuk Kim
2020-10-26  3:28   ` Can Guo
2020-10-26  6:13     ` Jaegeuk Kim
2020-10-26  6:43       ` Can Guo
2020-10-26 19:48         ` Jaegeuk Kim
2020-10-27  2:44           ` Can Guo
2020-10-24 15:06 ` [PATCH v3 2/5] scsi: ufs: clear UAC for FFU and RPMB LUNs Jaegeuk Kim
2020-10-26 18:25   ` asutoshd
2020-10-26 19:43     ` Jaegeuk Kim
2020-10-27 17:48   ` kernel test robot
2020-10-24 15:06 ` [PATCH v3 3/5] scsi: ufs: use WQ_HIGHPRI for gating work Jaegeuk Kim
2020-10-26 18:27   ` asutoshd
2020-10-24 15:06 ` [PATCH v3 4/5] scsi: add more contexts in the ufs tracepoints Jaegeuk Kim
2020-10-24 15:06 ` [PATCH v3 5/5] scsi: ufs: fix clkgating on/off correctly Jaegeuk Kim
2020-10-26 18:33   ` asutoshd

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).