Commit
375f3a5691dd46b5d05b90a8968872ec8b2248c5
scsi: ufs: core: Avoid unsafe MMIO reads in ufshcd_mcq_compl_all_cqes_lock()
During MCQ host reset, ufshcd_host_reset_and_restore() stops the host
controller via ufshcd_hba_stop() (HCE = 0) before calling
ufshcd_complete_requests(hba, true) ->
ufshcd_mcq_compl_pending_transfer(hba, true) ->
ufshcd_mcq_force_compl_one() -> ufshcd_mcq_compl_all_cqes_lock(). Because
ufshcd_mcq_force_compl_one() is its sole caller,
ufshcd_mcq_compl_all_cqes_lock() always runs with HCE = 0.
Despite the comment above ufshcd_mcq_compl_all_cqes_lock() stating that
reading CQTPy may not be safe with the controller disabled, the function
still calls ufshcd_mcq_update_cq_tail_slot() at the end of its sweep:
1. Unsafe CQTPy MMIO read:
Calling ufshcd_mcq_update_cq_tail_slot() at the end of the sweep reads
CQTPy over MMIO while HCE = 0, directly contradicting the function's
documented contract (commit 1373df88d535 ("scsi: ufs: core: Add a
comment block above ufshcd_mcq_compl_all_cqes_lock()")) that reading
CQTPy may not be safe with the controller disabled.
2. Spurious error logs on empty slots:
Sweeping all max_entries slots visits empty entries where
command_desc_base_addr is 0, causing ufshcd_mcq_process_cqe() to log
unguarded dev_err(hba->dev, "Abnormal CQ entry!\n") messages.
Fix both issues in ufshcd_mcq_compl_all_cqes_lock():
- Remove the ufshcd_mcq_update_cq_tail_slot() call and the redundant
hwq->cq_head_slot = hwq->cq_tail_slot assignment without replacement.
The two indices are already equal after the sweep: they are equal when
the sweep starts, since ufshcd_mcq_poll_cqe_lock() consumes entries
until cq_head_slot reaches cq_tail_slot, and the sweep advances
cq_head_slot by exactly one full ring. Both indices are also
reinitialized before the queue is reused.
- Extract ufshcd_mcq_compl_cqe() and invoke it only on non-empty slots
during full-ring sweeps, keeping "Abnormal CQ entry!" logging strictly
for unexpected empty entries in ufshcd_mcq_poll_cqe_lock().
Fixes: ab248643d3d6 ("scsi: ufs: core: Add error handling for MCQ mode")
Cc: stable@vger.kernel.org
Reviewed-by: Peter Wang <peter.wang@mediatek.com>
Signed-off-by: Stanley Jhu <stanleyjhu@google.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Link: https://patch.msgid.link/20260928035816.1294326-2-stanleyjhu@google.com
Signed-off-by: Martin K. Petersen (Oracle) <mkp@kernel.org>
Stanley Jhu <stanleyjhu@google.com> · 2026-09-28 03:58 UTC · committed by Martin K. Petersen (Oracle)
1 file changed,
+17
−13
modified drivers/ufs/core/ufs-mcq.c
| @@ -312,20 +312,24 @@ static int ufshcd_mcq_get_tag(struct ufs_hba *hba, struct cq_entry *cqe) | |||
| 312 | 312 | UFSHCD_NUM_RESERVED; | |
| 313 | 313 | } | |
| 314 | 314 | ||
| 315 | + | static void ufshcd_mcq_compl_cqe(struct ufs_hba *hba, struct cq_entry *cqe) | |
| 316 | + | { | |
| 317 | + | int tag = ufshcd_mcq_get_tag(hba, cqe); | |
| 318 | + | ||
| 319 | + | ufshcd_compl_one_cqe(hba, tag, cqe); | |
| 320 | + | /* After processing the CQE, mark it as an empty (invalid) entry. */ | |
| 321 | + | cqe->command_desc_base_addr = 0; | |
| 322 | + | } | |
| 323 | + | ||
| 315 | 324 | static void ufshcd_mcq_process_cqe(struct ufs_hba *hba, | |
| 316 | 325 | struct ufs_hw_queue *hwq) | |
| 317 | 326 | { | |
| 318 | 327 | struct cq_entry *cqe = ufshcd_mcq_cur_cqe(hwq); | |
| 319 | 328 | ||
| 320 | - | if (cqe->command_desc_base_addr) { | |
| 321 | - | int tag = ufshcd_mcq_get_tag(hba, cqe); | |
| 322 | - | ||
| 323 | - | ufshcd_compl_one_cqe(hba, tag, cqe); | |
| 324 | - | /* After processed the cqe, mark it empty (invalid) entry */ | |
| 325 | - | cqe->command_desc_base_addr = 0; | |
| 326 | - | } else { | |
| 329 | + | if (cqe->command_desc_base_addr) | |
| 330 | + | ufshcd_mcq_compl_cqe(hba, cqe); | |
| 331 | + | else | |
| 327 | 332 | dev_err(hba->dev, "Abnormal CQ entry!\n"); | |
| 328 | - | } | |
| 329 | 333 | } | |
| 330 | 334 | ||
| 331 | 335 | /* | |
| @@ -333,7 +337,7 @@ static void ufshcd_mcq_process_cqe(struct ufs_hba *hba, | |||
| 333 | 337 | * controller disabled (HCE = 0). Reading host controller registers, e.g. the | |
| 334 | 338 | * CQ tail pointer (CQTPy), may not be safe with the host controller disabled. | |
| 335 | 339 | * Hence, iterate over all completion queue entries. This won't result in | |
| 336 | - | * double completions because ufshcd_mcq_process_cqe() clears a CQE after it | |
| 340 | + | * double completions because ufshcd_mcq_compl_cqe() clears a CQE after it | |
| 337 | 341 | * has been processed. | |
| 338 | 342 | */ | |
| 339 | 343 | void ufshcd_mcq_compl_all_cqes_lock(struct ufs_hba *hba, | |
| @@ -344,13 +348,13 @@ void ufshcd_mcq_compl_all_cqes_lock(struct ufs_hba *hba, | |||
| 344 | 348 | ||
| 345 | 349 | spin_lock_irqsave(&hwq->cq_lock, flags); | |
| 346 | 350 | while (entries > 0) { | |
| 347 | - | ufshcd_mcq_process_cqe(hba, hwq); | |
| 351 | + | struct cq_entry *cqe = ufshcd_mcq_cur_cqe(hwq); | |
| 352 | + | ||
| 353 | + | if (cqe->command_desc_base_addr) | |
| 354 | + | ufshcd_mcq_compl_cqe(hba, cqe); | |
| 348 | 355 | ufshcd_mcq_inc_cq_head_slot(hwq); | |
| 349 | 356 | entries--; | |
| 350 | 357 | } | |
| 351 | - | ||
| 352 | - | ufshcd_mcq_update_cq_tail_slot(hwq); | |
| 353 | - | hwq->cq_head_slot = hwq->cq_tail_slot; | |
| 354 | 358 | spin_unlock_irqrestore(&hwq->cq_lock, flags); | |
| 355 | 359 | } | |
| 356 | 360 | ||