Skip to contentSkip to conversation

Fix abnormal clock scaling behaviors

PATCH Superseded [email protected] 2 participants 12 messages Aug 23, 2023 – Oct 13, 2023 1 message not in the archive

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix abnormal clock scaling behaviors

From: Peter Wang <[email protected]>

This patch fix abnormal clock scaling behaviors.

Peter Wang (3):
ufs: core: only suspend clock scaling if scale down
ufs: core: fix abnormal scale up after last cmd finish
ufs: core: fix abnormal scale up after scale down

 drivers/ufs/core/ufshcd.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
Signature · 1 line
2.18.0
#1 lore raw

PATCH 1/3 ufs: core: only suspend clock scaling if scale down

From: Peter Wang <[email protected]>

If clock scale up and suspend clock scaling, ufs will keep high performance/power mode but no read/write requests on going. It is logic wrong and have power concern.

---
 drivers/ufs/core/ufshcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +1 -1
index 129446775796..e3672e55efae 100644
@@ -1458,7 +1458,7 @@ static int ufshcd_devfreq_target(struct device *dev,
 		ktime_to_us(ktime_sub(ktime_get(), start)), ret);
 
 out:
-	if (sched_clk_scaling_suspend_work)
+	if (sched_clk_scaling_suspend_work && !scale_up)
 		queue_work(hba->clk_scaling.workq,
 			   &hba->clk_scaling.suspend_work);
 
Signature · 1 line
2.18.0
#2 lore raw

Re: [PATCH v1 1/3] ufs: core: only suspend clock scaling if scale down

Quoted 16 lines · On 8/23/23 02:29, [email protected] wrote:

From: Peter Wang <[email protected]>

If clock scale up and suspend clock scaling, ufs will keep high performance/power mode but no read/write requests on going. It is logic wrong and have power concern.

---
  drivers/ufs/core/ufshcd.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +0 -0
index 129446775796..e3672e55efae 100644

drivers/ufs/core/ufshcd.c static int ufshcd_devfreq_target(struct device *dev, · line 1464

Quoted 8 lines
drivers/ufs/core/ufshcd.c +1 -1
@@ -1458,7 +1458,7 @@ static int ufshcd_devfreq_target(struct device *dev,
  		ktime_to_us(ktime_sub(ktime_get(), start)), ret);
 
  out:
-	if (sched_clk_scaling_suspend_work)
+	if (sched_clk_scaling_suspend_work && !scale_up)
  		queue_work(hba->clk_scaling.workq,
  			   &hba->clk_scaling.suspend_work);
 

Is this perhaps the same patch as
https://lore.kernel.org/linux-scsi/[email protected]/ ?

Thanks,

Bart.

#4 lore raw

PATCH 2/3 ufs: core: fix abnormal scale up after last cmd finish

In reply to [email protected]

From: Peter Wang <[email protected]>

When ufshcd_clk_scaling_suspend_work(Thread A) running and new command coming, ufshcd_clk_scaling_start_busy(Thread B) may get host_lock after Thread A first time release host_lock. Then Thread A second time get host_lock will set clk_scaling.window_start_t = 0 which scale up clock abnormal next polling_ms time.

Below is racing step:
1 hba->clk_scaling.suspend_work (Thread A)
ufshcd_clk_scaling_suspend_work
2 spin_lock_irqsave(hba->host->host_lock, irq_flags);
3 hba->clk_scaling.is_suspended = true;
4 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
__ufshcd_suspend_clkscaling
7 spin_lock_irqsave(hba->host->host_lock, flags);
8 hba->clk_scaling.window_start_t = 0;
9 spin_unlock_irqrestore(hba->host->host_lock, flags);

ufshcd_send_command (Thread B)
ufshcd_clk_scaling_start_busy

5			spin_lock_irqsave(hba->host->host_lock, flags);
			....

6 spin_unlock_irqrestore(hba->host->host_lock, flags);

---
 drivers/ufs/core/ufshcd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +2 -1
index e3672e55efae..017f32b3a789 100644
@@ -1385,9 +1385,10 @@ static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
 		return;
 	}
 	hba->clk_scaling.is_suspended = true;
+	hba->clk_scaling.window_start_t = 0;
 	spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
 
-	__ufshcd_suspend_clkscaling(hba);
+	devfreq_suspend_device(hba->devfreq);
 }
 
 static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
Signature · 1 line
2.18.0
#5 lore raw

Re: [PATCH v1 2/3] ufs: core: fix abnormal scale up after last cmd finish

Quoted 36 lines · On 8/23/23 02:29, [email protected] wrote:

From: Peter Wang <[email protected]>

When ufshcd_clk_scaling_suspend_work(Thread A) running and new command coming, ufshcd_clk_scaling_start_busy(Thread B) may get host_lock after Thread A first time release host_lock. Then Thread A second time get host_lock will set clk_scaling.window_start_t = 0 which scale up clock abnormal next polling_ms time.

Below is racing step:
1 hba->clk_scaling.suspend_work (Thread A)
ufshcd_clk_scaling_suspend_work
2 spin_lock_irqsave(hba->host->host_lock, irq_flags);
3 hba->clk_scaling.is_suspended = true;
4 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
__ufshcd_suspend_clkscaling
7 spin_lock_irqsave(hba->host->host_lock, flags);
8 hba->clk_scaling.window_start_t = 0;
9 spin_unlock_irqrestore(hba->host->host_lock, flags);

ufshcd_send_command (Thread B)
ufshcd_clk_scaling_start_busy

5			spin_lock_irqsave(hba->host->host_lock, flags);
			....

6 spin_unlock_irqrestore(hba->host->host_lock, flags);

---
  drivers/ufs/core/ufshcd.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +0 -0
index e3672e55efae..017f32b3a789 100644
@@ -1385,9 +1385,10 @@ static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
  		return;

drivers/ufs/core/ufshcd.c static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) · line 1394

Quoted 10 lines
drivers/ufs/core/ufshcd.c +2 -1
  	}
  	hba->clk_scaling.is_suspended = true;
+	hba->clk_scaling.window_start_t = 0;
  	spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
 
-	__ufshcd_suspend_clkscaling(hba);
+	devfreq_suspend_device(hba->devfreq);
  }
 
  static void ufshcd_clk_scaling_resume_work(struct work_struct *work)

There is a second __ufshcd_suspend_clkscaling() call in the same source file. Please fix that call too.

Thanks,

Bart.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Re: [PATCH v1 2/3] ufs: core: fix abnormal scale up after last cmd finish

Quoted 3 lines · Peter Wang (王信友)

Second __ufshcd_suspend_clkscaling is called from ufs wl suspend, which no on-going cmd.

So, it should be not a problem. (cmd finish suspend clock scaling racing with new cmd start busy)

This patch inlines one of the two __ufshcd_suspend_clkscaling() calls. I think it would be better if both calls are inlined.

Thanks,

Bart.

#8 lore raw

Re: [PATCH v1 2/3] ufs: core: fix abnormal scale up after last cmd finish

Quoted 10 lines · Bart Van Assche

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 1 line · Peter Wang (王信友)

Second __ufshcd_suspend_clkscaling is called from ufs wl suspend,

which no on-going cmd.

Quoted 2 lines

So, it should be not a problem. (cmd finish suspend clock scaling

racing with new cmd start busy)

Quoted 7 lines

This patch inlines one of the two __ufshcd_suspend_clkscaling() calls.
I think it would be better if both calls are inlined.

Thanks,

Bart.

Hi Bart,

Could you help review the new patch.

Thanks.
Peter

#9 lore raw

PATCH 3/3 ufs: core: fix abnormal scale up after scale down

In reply to [email protected]

From: Peter Wang <[email protected]>

When no active_reqs, devfreq_monitor(Thread A) will suspend clock scaling. But it may have racing with clk_scaling.suspend_work(Thread B) and actually not suspend clock scaling(requue after suspend).
Next time after polling_ms, devfreq_monitor read clk_scaling.window_start_t = 0 then scale up clock abnormal.

Below is racing step:

	devfreq->work (Thread A)
	devfreq_monitor
		update_devfreq
		.....
			ufshcd_devfreq_target
			queue_work(hba->clk_scaling.workq,
1			   &hba->clk_scaling.suspend_work)
		.....
5	queue_delayed_work(devfreq_wq, &devfreq->work,
			msecs_to_jiffies(devfreq->profile->polling_ms));

2 hba->clk_scaling.suspend_work (Thread B)

	ufshcd_clk_scaling_suspend_work
		__ufshcd_suspend_clkscaling
			devfreq_suspend_device(hba->devfreq);
3				cancel_delayed_work_sync(&devfreq->work);
4			hba->clk_scaling.window_start_t = 0;
	.....
---
 drivers/ufs/core/ufshcd.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/ufs/core/ufshcd.c +7 -0
index 017f32b3a789..d3b913d389f4 100644
@@ -1431,6 +1431,13 @@ static int ufshcd_devfreq_target(struct device *dev,
 		return 0;
 	}
 
+	/* Skip scaling clock when clock scaling is suspended */
+	if (hba->clk_scaling.is_suspended) {
+		spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
+		dev_warn(hba->dev, "clock scaling is suspended, skip");
+		return 0;
+	}
+
 	if (!hba->clk_scaling.active_reqs)
 		sched_clk_scaling_suspend_work = true;
 
Signature · 1 line
2.18.0
reviewer Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH v1 3/3] ufs: core: fix abnormal scale up after scale down

Quoted 5 lines · On 8/23/23 02:29, [email protected] wrote:

When no active_reqs, devfreq_monitor(Thread A) will suspend clock scaling. But it may have racing with clk_scaling.suspend_work(Thread B) and actually not suspend clock scaling(requue after suspend).
Next time after polling_ms, devfreq_monitor read clk_scaling.window_start_t = 0 then scale up clock abnormal.

reviewer Reviewed-by on v1 3/3
#11 lore raw

Re: [PATCH v1 3/3] ufs: core: fix abnormal scale up after scale down

In reply to [email protected]
Quoted 5 lines · On 8/23/23 02:29, [email protected] wrote:

When no active_reqs, devfreq_monitor(Thread A) will suspend clock scaling. But it may have racing with clk_scaling.suspend_work(Thread B) and actually not suspend clock scaling(requue after suspend).
Next time after polling_ms, devfreq_monitor read clk_scaling.window_start_t = 0 then scale up clock abnormal.