scsi: ufs: Fix broken hba->outstanding_tasks
Subsystems (2)
cover scsi: ufs: Fix broken hba->outstanding_tasks
Currently bits in hba->outstanding_tasks are cleared only after their corresponding task management commands are successfully done by __ufshcd_issue_tm_cmd().
If timeout happens in a task management command, its corresponding bit in hba->outstanding_tasks will not be cleared until next task management command with the same tag used successfully finishes.‧
This is wrong and can lead to some issues, like power consumpton issue. For example, ufshcd_release() and ufshcd_gate_work() will do nothing if hba->outstanding_tasks is not zero even if both UFS host and devices are actually idle.
Because error handling flow, i.e., ufshcd_reset_and_restore(), will be triggered after any task management command times out, we fix this by clearing corresponding hba->outstanding_tasks bits during this flow. To achieve this, we need a mask to track timed-out commands and thus error handling flow can clear their tags specifically.
Stanley Chu (2):
scsi: ufs: Make new function for clearing outstanding task bits
scsi: ufs: Fix broken hba->outstanding_tasks
drivers/scsi/ufs/ufshcd.c | 49 +++++++++++++++++++++++++++++++++------ drivers/scsi/ufs/ufshcd.h | 1 + 2 files changed, 43 insertions(+), 7 deletions(-)
Signature · 7 lines
2.18.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH 1/2 scsi: ufs: Make new function for clearing outstanding task bits
Make a new function "ufshcd_outstanding_task_clear()" used to clear bits in hba->outstanding_tasks for future wider usage.
--- drivers/scsi/ufs/ufshcd.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/scsi/ufs/ufshcd.c +12 -1
@@ -723,6 +723,17 @@ static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)__clear_bit (tag, &hba->outstanding_reqs); } +/** +* ufshcd_outstanding_task_clear - Clear a bit in outstanding task field +* @hba: per adapter instance +* @tag: position of the bit to be cleared +*/ +static inline void ufshcd_outstanding_task_clear (struct ufs_hba *hba,int tag) +{ +__clear_bit (tag, &hba->outstanding_tasks); +dev_info (hba->dev,"clear outstanding_tasks: %d\n" , tag); +} +/** * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY * @reg: Register value of host controller status
@@ -5679,7 +5690,7 @@ static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,ufshcd_add_tm_upiu_trace (hba, task_tag,"tm_complete" );spin_lock_irqsave (hba->host->host_lock, flags); -__clear_bit (free_slot, &hba->outstanding_tasks); +ufshcd_outstanding_task_clear (hba, free_slot);spin_unlock_irqrestore (hba->host->host_lock, flags); }
Signature · 1 line
2.18.0
PATCH 2/2 scsi: ufs: Fix broken hba->outstanding_tasks
In reply to Stanley ChuCurrently bits in hba->outstanding_tasks are cleared only after their corresponding task management commands are successfully done by __ufshcd_issue_tm_cmd().
If timeout happens in a task management command, its corresponding bit in hba->outstanding_tasks will not be cleared until next task management command with the same tag used successfully finishes.‧
This is wrong and can lead to some issues, like power consumpton issue. For example, ufshcd_release() and ufshcd_gate_work() will do nothing if hba->outstanding_tasks is not zero even if both UFS host and devices are actually idle.
Because error handling flow, i.e., ufshcd_reset_and_restore(), will be triggered after any task management command times out, we fix this by clearing corresponding hba->outstanding_tasks bits during this flow. To achieve this, we need a mask to track timed-out commands and thus error handling flow can clear their tags specifically.
--- drivers/scsi/ufs/ufshcd.c | 38 +++++++++++++++++++++++++++++++------- drivers/scsi/ufs/ufshcd.h | 1 + 2 files changed, 32 insertions(+), 7 deletions(-)
drivers/scsi/ufs/ufshcd.c +31 -7
@@ -731,7 +731,6 @@ static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)static inline void ufshcd_outstanding_task_clear (struct ufs_hba *hba,int tag) {__clear_bit (tag, &hba->outstanding_tasks); -dev_info (hba->dev,"clear outstanding_tasks: %d\n" , tag); }/**
@@ -5540,11 +5539,34 @@ static void ufshcd_check_errors(struct ufs_hba *hba)*/ static void ufshcd_tmc_handler (struct ufs_hba *hba) { -u32 tm_doorbell; +u32 tm_doorbell, tag; +unsigned long tm_err_handled =0 ; +unsigned long tm_done; tm_doorbell =ufshcd_readl (hba,REG_UTP_TASK_REQ_DOOR_BELL ); hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; -wake_up (&hba->tm_wq); + tm_done = hba->tm_condition; + +/* clean resources for timed-out tasks */ +for_each_set_bit (tag, &hba->tm_condition, hba->nutmrs) { +if (test_and_clear_bit (tag, &hba->tm_slots_err)) { +clear_bit (tag, &hba->tm_condition); +ufshcd_put_tm_slot (hba, tag); +ufshcd_outstanding_task_clear (hba, tag); +__set_bit (tag, &tm_err_handled); + } + } + +/* +* Now tag waiters can get free tags if tags were occupied +* by timed-out tasks +*/ +if (tm_err_handled) +wake_up (&hba->tm_tag_wq); + +/* if we have normal tasks, they shall have post-processing */ +if (tm_err_handled != tm_done) +wake_up (&hba->tm_wq); }/**
@@ -5682,6 +5704,7 @@ static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,if (ufshcd_clear_tm_cmd (hba, free_slot))dev_WARN (hba->dev,"%s: unable clear tm cmd (slot %d) after timeout\n" , __func__, free_slot); +set_bit (free_slot, &hba->tm_slots_err); err = -ETIMEDOUT ; }else { err =0 ;
@@ -5692,12 +5715,13 @@ static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,spin_lock_irqsave (hba->host->host_lock, flags);ufshcd_outstanding_task_clear (hba, free_slot);spin_unlock_irqrestore (hba->host->host_lock, flags); - } -clear_bit (free_slot, &hba->tm_condition); -ufshcd_put_tm_slot (hba, free_slot); -wake_up (&hba->tm_tag_wq); +if (!(test_bit (free_slot, &hba->tm_slots_err))) { +clear_bit (free_slot, &hba->tm_condition); +ufshcd_put_tm_slot (hba, free_slot); +wake_up (&hba->tm_tag_wq); + }ufshcd_release (hba);return err;
drivers/scsi/ufs/ufshcd.h +1 -0
@@ -645,6 +645,7 @@ struct ufs_hba {wait_queue_head_t tm_tag_wq;unsigned long tm_condition;unsigned long tm_slots_in_use; +unsigned long tm_slots_err;struct uic_command *active_uic_cmd;struct mutex uic_cmd_mutex;
Signature · 7 lines
2.18.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Hi Avri, Alim and Pedrom,
Gentle ping for this fix.
Quoted 19 lines · Stanley Chu
Currently bits in
hba->outstanding_tasksare cleared only after their corresponding task management commands are successfully done by__ufshcd_issue_tm_cmd().If timeout happens in a task management command, its corresponding bit in
hba->outstanding_taskswill not be cleared until next task management command with the same tag used successfully finishes.‧This is wrong and can lead to some issues, like power consumpton issue. For example,
ufshcd_release()andufshcd_gate_work()will do nothing ifhba->outstanding_tasksis not zero even if both UFS host and devices are actually idle.Because error handling flow, i.e.,
ufshcd_reset_and_restore(), will be triggered after any task management command times out, we fix this by clearing correspondinghba->outstanding_tasksbits during this flow. To achieve this, we need a mask to track timed-out commands and thus error handling flow can clear their tags specifically.
Quoted 8 lines
Stanley Chu (2):
scsi: ufs: Make new function for clearing outstanding task bits
scsi: ufs: Fix brokenhba->outstanding_tasksdrivers/scsi/ufs/ufshcd.c | 49 +++++++++++++++++++++++++++++++++------ drivers/scsi/ufs/ufshcd.h | 1 + 2 files changed, 43 insertions(+), 7 deletions(-)
Thanks,
Stanley
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Hi,
Quoted 8 lines
Currently bits in
hba->outstanding_tasksare cleared only after their corresponding task management commands are successfully done by__ufshcd_issue_tm_cmd().If timeout happens in a task management command, its corresponding bit in
hba->outstanding_taskswill not be cleared until next task management command with the same tag used successfully finishes.‧
ufshcd_clear_tm_cmd is also called as part of ufshcd_err_handler. Does this change something in your assumptions?
Thanks,
Avri
Quoted 16 lines
This is wrong and can lead to some issues, like power consumpton issue. For example,
ufshcd_release()andufshcd_gate_work()will do nothing ifhba->outstanding_tasksis not zero even if both UFS host and devices are actually idle.Because error handling flow, i.e.,
ufshcd_reset_and_restore(), will be triggered after any task management command times out, we fix this by clearing correspondinghba->outstanding_tasksbits during this flow. To achieve this, we need a mask to track timed-out commands and thus error handling flow can clear their tags specifically.Stanley Chu (2):
scsi: ufs: Make new function for clearing outstanding task bits
scsi: ufs: Fix brokenhba->outstanding_tasks
Quoted 6 lines
drivers/scsi/ufs/ufshcd.c | 49 +++++++++++++++++++++++++++++++++------ drivers/scsi/ufs/ufshcd.h | 1 + 2 files changed, 43 insertions(+), 7 deletions(-)--
2.18.0
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 8 lines
Hi,
Quoted 5 lines
Currently bits in
hba->outstanding_tasksare cleared only after their corresponding task management commands are successfully done by__ufshcd_issue_tm_cmd().
Quoted 5 lines
Quoted 3 lines
If timeout happens in a task management command, its corresponding bit in
hba->outstanding_taskswill not be cleared until next task management command with the same tag used successfully finishes.‧
ufshcd_clear_tm_cmdis also called as part ofufshcd_err_handler. Does this change something in your assumptions?
And BTW there is a specific __clear_bit in __ufshcd_issue_tm_cmd() in case of a TO.
Quoted 4 lines
Thanks,
Avri
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Quoted 9 lines
Quoted 9 lines
Hi,
Quoted 5 lines
Currently bits in
hba->outstanding_tasksare cleared only after their corresponding task management commands are successfully done by__ufshcd_issue_tm_cmd().
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
If timeout happens in a task management command, its corresponding bit in
hba->outstanding_taskswill not be cleared until next task management command with the same tag used successfully finishes.‧
ufshcd_clear_tm_cmdis also called as part ofufshcd_err_handler. Does this change something in your assumptions?And BTW there is a specific
__clear_bitin__ufshcd_issue_tm_cmd()in case of a TO.
Gave it another look -
If indeed this bit isn't cleared as part of the error flow that the timeout triggers, I think you should relate to ufshcd_clear_tm_cmd specifically in your commit log - Because this is the obvious place where the bit cleanup should take place.
Also the fix should be much more intuitive IMO -
Today we do __clear_bit() on success, ufshcd_clear_tm_cmd() on error, And also ufshcd_put_tm_slot() either way?
Maybe you can choose a single place to clear it, without any additional code?
Thanks,
Avri
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Hi Avri,
Quoted 22 lines · Avri Altman
Quoted 9 lines
Quoted 9 lines
Hi,
Quoted 5 lines
Currently bits in
hba->outstanding_tasksare cleared only after their corresponding task management commands are successfully done by__ufshcd_issue_tm_cmd().Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
If timeout happens in a task management command, its corresponding bit in
hba->outstanding_taskswill not be cleared until next task management command with the same tag used successfully finishes.‧
ufshcd_clear_tm_cmdis also called as part ofufshcd_err_handler. Does this change something in your assumptions?And BTW there is a specific
__clear_bitin__ufshcd_issue_tm_cmd()in case of a TO.Gave it another look -
If indeed this bit isn't cleared as part of the error flow that the timeout triggers, I think you should relate toufshcd_clear_tm_cmdspecifically in your commit log - Because this is the obvious place where the bit cleanup should take place.
Quoted 5 lines
Also the fix should be much more intuitive IMO -
Today we do__clear_bit()on success,ufshcd_clear_tm_cmd()on error, And alsoufshcd_put_tm_slot()either way?Maybe you can choose a single place to clear it, without any additional code?
ufshcd_clear_tm_cmd() is similar to ufshcd_clear_cmd() which tries to write timed-out bit in "clear register". They do not clean bits in both outstanding masks.
Another reason to not to do outstanding bits cleanup in ufshcd_clear_tm_cmd() is: if host is unable to clear TM command by setting "clear register", the TM command may be still "outstanding" in the device. In this case, it may be better to do cleanup after reset is done. Cleanup includes bits in hba->outstanding_tasks and hba->tm_slots_in_use which is possibly cleaned too early by ufshcd_put_tm_slot() if command is timed-out now.
Referring to error handling flow of hba->outstanding_reqs, all timed-out bits will be cleaned by ufshcd_reset_and_restore() => ufshcd_transfer_req_compl() after reset is done. Similar handling for hba->outstanding_tasks could be applied, i.e., do cleanup by ufshcd_reset_and_restore() => ufshcd_tmc_handler().
The next thing is what you suggested: How to make the fix more intuitive. Patchset v2 will be uploaded for review: It tries to "re-factor" cleanup jobs first, and then add fixed flow to make the whole patch more readable.
One more thing, above description and flow is done through UFSHCD SCSI error handling routines registered with SCSI Midlayer. For TM command timeout happening in bsg path without error handling triggered by SCSI layer, we may need to consider to handle those tasks in future patches.
Quoted 3 lines
Thanks,
Avri
Thanks,
Stanley
Quoted 4 lines
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Stanley,
Quoted 52 lines
Hi Avri,
Quoted 7 lines · Avri Altman
Quoted 7 lines
Quoted 7 lines
Hi,
Quoted 3 lines
Currently bits in
hba->outstanding_tasksare cleared only after their corresponding task management commands are successfully doneby
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
__ufshcd_issue_tm_cmd().If timeout happens in a task management command, its
corresponding
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
bit in
hba->outstanding_taskswill not be cleared until next task management command with the same tag used successfullyfinishes.‧
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
ufshcd_clear_tm_cmdis also called as part ofufshcd_err_handler. Does this change something in your assumptions?And BTW there is a specific
__clear_bitin__ufshcd_issue_tm_cmd()incase
Quoted 4 lines
Quoted 1 line
of a TO.
Gave it another look -
If indeed this bit isn't cleared as part of the error flow that the timeouttriggers,
Quoted 1 line
I think you should relate to
ufshcd_clear_tm_cmdspecifically in yourcommit log -
Quoted 1 line
Because this is the obvious place where the bit cleanup should take
place.
Quoted 6 lines
Also the fix should be much more intuitive IMO -
Today we do__clear_bit()on success,ufshcd_clear_tm_cmd()on error, And alsoufshcd_put_tm_slot()either way?Maybe you can choose a single place to clear it, without any additional
code?
ufshcd_clear_tm_cmd()is similar toufshcd_clear_cmd()which tries to write timed-out bit in "clear register". They do not clean bits in both outstanding masks.Another reason to not to do outstanding bits cleanup in
ufshcd_clear_tm_cmd()is: if host is unable to clear TM command by setting "clear register", the TM command may be still "outstanding" in the device. In this case, it may be better to do cleanup after reset is done. Cleanup includes bits inhba->outstanding_tasksandhba->tm_slots_in_usewhich is possibly cleaned too early byufshcd_put_tm_slot()if command is timed-out now.
Quoted 5 lines
Referring to error handling flow of
hba->outstanding_reqs, all timed-out bits will be cleaned byufshcd_reset_and_restore()=>ufshcd_transfer_req_compl()after reset is done. Similar handling forhba->outstanding_taskscould be applied, i.e., do cleanup byufshcd_reset_and_restore()=>ufshcd_tmc_handler().
Fair enough. Thank you for the detailed explanation.
Quoted 10 lines
The next thing is what you suggested: How to make the fix more intuitive. Patchset v2 will be uploaded for review: It tries to "re-factor" cleanup jobs first, and then add fixed flow to make the whole patch more readable.
One more thing, above description and flow is done through UFSHCD SCSI error handling routines registered with SCSI Midlayer. For TM command timeout happening in bsg path without error handling triggered by SCSI layer, we may need to consider to handle those tasks in future patches.
Please do.
Thanks,
Avri
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek