From nobody Fri Sep 25 00:02:40 2026 Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FF5C4848AC for ; Fri, 18 Sep 2026 14:38:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789742296; cv=none; b=uwR8as+B2roD64YjiczQBHYRVXZVt3MMHqeZ4oLKaHOuHk5iTLPsXaSaILVGcZzHF9pxe44fQchkk6WAZecUlY2Sypjjs0MK+5XBZ91Z5WcNVitOohn2wzIE5Q6Q1BbySS1VovU2T2IYFQroxa6xZoCm3meuIRBaRl9XEFRZaoc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789742296; c=relaxed/simple; bh=zY8TQGvRR0TfY/4o05gMCbEeECuiInBxdKlJWP9KB1c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=njQAbUx2Yo1o1wiUMbJb/aRrctGJcyjPTzCkxXeINsbnD/zvxgaXxc0a47fs6X1wM6rMZ0Ftyw722C57W/M49P0ivUfk0Cb3QjQ+fY5CyMZ6iVvvhwhIVfY9TOhcr/RKJLx9m7bac3gMQjfP4XG+F27tDFruLQCt28k46LnHiTo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--stanleyjhu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=JRNdtIcb; arc=none smtp.client-ip=209.85.210.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--stanleyjhu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JRNdtIcb" Received: by mail-pf1-f200.google.com with SMTP id d2e1a72fcca58-8688139460bso975405b3a.0 for ; Fri, 18 Sep 2026 07:38:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789742293; x=1790347093; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=KnLkF7ter7j7ccxH/bbiqxi4gFXxPEmpQ7hBTZwXfuE=; b=JRNdtIcbH4nirvkCgf7FcpNAqxfE+GUKAhxoBq3YfDkRwskzvNrw6EsN2JRGp1x9Z3 8h1iBWwBi9JPO8m/kggeUTQJdTA4U+qRYjOLqerBTxRwQ7/v0pn0VqwXW6LkABmNS1CM PWt6DU1Gg0XldPy+bu8HQSfgtNC5ZpSE23mrY6NHrm6j3u61SFLVWdWBhptaSkSYy4ac GyPSWtJ71JlUhbYpYgpgMh6lGrGO5wSt/BXnj4PrIgzoupSh1PjSnWwurcItvMI1nQZF VzC0c4GeI/2ILWMTVf9jUF3Za8Jh5qielN9mCj5XW8Vz1m5XFXPO+QRHvuvfp9U/Bqti X96g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789742293; x=1790347093; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=KnLkF7ter7j7ccxH/bbiqxi4gFXxPEmpQ7hBTZwXfuE=; b=xPIIrdOJviHeN9aAKpI8KDhbDjUxW1pTO8yRadtAuRPczoNPFSt+soXNx2EpcpJQcm w2ZdUK9x5Op+CMXrVZVzBQvhFCvTCggFZDcNfiw2l7O9vuj29yWmo0hWGoNj4ZhIZPXh a0KxixHswzt1MIMve+xOTbfZwzVy4+1r0tUGOrgzwD4p8BKp4jlnhDrwCumQkProt1Ul E+SG/bbJBjpKzNHyp7QmSDwMSrcjHNUtMdfb7ZNaRV+lk8Upjmkpz7HPWAWEHYfw4LMy 5YYf8OXXCk1WPcdFAy6H0Qfmk7Ge7X3pNlZ4nxf3VJ/KJ7UM4hh9ckugyIOXnQnnIbEY gRvw== X-Forwarded-Encrypted: i=1; AKwUvBx8mZFSVGgOsRtyTziSI3MLrhmnYh/SlnptNHf4v4v6e3EdZYHgsouZam77OWvGHRMpU8vBpGWwobmCkF0=@vger.kernel.org X-Gm-Message-State: AFuF++nDw3jmKBY3VKpZJSXkqeBPBESCAWchGsDLYt5wsSIYxs2eafIb MztTnn6FjHfebj69FeDk171OG7OrBQ6vkaY6vEy+enk7w2RPAA7MuuxSawhAqfz1Nl3j+tkLyol 5c6Tulnc7bptfMvTnQn/j/Q== X-Received: from pfbmu26.prod.google.com ([2002:a05:6a00:6e9a:b0:86b:e98b:a8ab]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:94e3:b0:874:706d:962f with SMTP id d2e1a72fcca58-874de4ff5fdmr5702926b3a.33.1789742293333; Fri, 18 Sep 2026 07:38:13 -0700 (PDT) Date: Fri, 18 Sep 2026 22:38:07 +0800 In-Reply-To: <20260918143809.3034592-1-stanleyjhu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918143809.3034592-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918143809.3034592-2-stanleyjhu@google.com> Subject: [PATCH v2 1/2] scsi: ufs: core: Avoid unsafe MMIO reads in ufshcd_mcq_compl_all_cqes_lock() From: Stanley Jhu To: "Martin K . Petersen" , Bean Huo , Bart Van Assche Cc: Alim Akhtar , Avri Altman , "James E . J . Bottomley" , Manivannan Sadhasivam , Peter Wang , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, stanleyjhu@google.com, stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" During MCQ host reset, ufshcd_host_reset_and_restore() stops the host controller via ufshcd_hba_stop() (HCE =3D 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 =3D 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 =3D 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(): - Synchronize hwq->cq_tail_slot =3D hwq->cq_head_slot in software after sweeping the ring, avoiding CQTPy MMIO reads while HCE =3D 0. - 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 Signed-off-by: Stanley Jhu Reviewed-by: Peter Wang --- v2: - Extract ufshcd_mcq_compl_cqe() to skip empty slots inside ufshcd_mcq_compl_all_cqes_lock() without double CQE checks (dropped Peter Wang's v1 Reviewed-by due to this code change). - Move hardware queue polling and sweeping deduplication to Patch 2/2. Link: https://lore.kernel.org/r/CAE14pdek6ynze+muDZrK+yNX-3ioe3vprxOA4W22qo= kg352tJQ@mail.gmail.com drivers/ufs/core/ufs-mcq.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/drivers/ufs/core/ufs-mcq.c b/drivers/ufs/core/ufs-mcq.c index 8106d55f4041..bfc43a6080e7 100644 --- a/drivers/ufs/core/ufs-mcq.c +++ b/drivers/ufs/core/ufs-mcq.c @@ -312,20 +312,24 @@ static int ufshcd_mcq_get_tag(struct ufs_hba *hba, st= ruct cq_entry *cqe) UFSHCD_NUM_RESERVED; } =20 +static void ufshcd_mcq_compl_cqe(struct ufs_hba *hba, struct cq_entry *cqe) +{ + int tag =3D ufshcd_mcq_get_tag(hba, cqe); + + ufshcd_compl_one_cqe(hba, tag, cqe); + /* After processed the cqe, mark it empty (invalid) entry */ + cqe->command_desc_base_addr =3D 0; +} + static void ufshcd_mcq_process_cqe(struct ufs_hba *hba, struct ufs_hw_queue *hwq) { struct cq_entry *cqe =3D ufshcd_mcq_cur_cqe(hwq); =20 - if (cqe->command_desc_base_addr) { - int tag =3D ufshcd_mcq_get_tag(hba, cqe); - - ufshcd_compl_one_cqe(hba, tag, cqe); - /* After processed the cqe, mark it empty (invalid) entry */ - cqe->command_desc_base_addr =3D 0; - } else { + if (cqe->command_desc_base_addr) + ufshcd_mcq_compl_cqe(hba, cqe); + else dev_err(hba->dev, "Abnormal CQ entry!\n"); - } } =20 /* @@ -333,7 +337,7 @@ static void ufshcd_mcq_process_cqe(struct ufs_hba *hba, * controller disabled (HCE =3D 0). Reading host controller registers, e.g= . the * CQ tail pointer (CQTPy), may not be safe with the host controller disab= led. * Hence, iterate over all completion queue entries. This won't result in - * double completions because ufshcd_mcq_process_cqe() clears a CQE after = it + * double completions because ufshcd_mcq_compl_cqe() clears a CQE after it * has been processed. */ void ufshcd_mcq_compl_all_cqes_lock(struct ufs_hba *hba, @@ -344,13 +348,20 @@ void ufshcd_mcq_compl_all_cqes_lock(struct ufs_hba *h= ba, =20 spin_lock_irqsave(&hwq->cq_lock, flags); while (entries > 0) { - ufshcd_mcq_process_cqe(hba, hwq); + struct cq_entry *cqe =3D ufshcd_mcq_cur_cqe(hwq); + + if (cqe->command_desc_base_addr) + ufshcd_mcq_compl_cqe(hba, cqe); ufshcd_mcq_inc_cq_head_slot(hwq); entries--; } =20 - ufshcd_mcq_update_cq_tail_slot(hwq); - hwq->cq_head_slot =3D hwq->cq_tail_slot; + /* + * All completion entries have been processed and cleared. + * Synchronize tail to head in software to mark the queue empty, + * avoiding an MMIO read of CQTPy while the controller is disabled. + */ + hwq->cq_tail_slot =3D hwq->cq_head_slot; spin_unlock_irqrestore(&hwq->cq_lock, flags); } =20 --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Fri Sep 25 00:02:40 2026 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BD3D4B270B for ; Fri, 18 Sep 2026 14:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789742298; cv=none; b=uOf7JfYkGGOwvFDnjoG8DkNFMNAUWhYd/XHf/Ow47j13sM7G+feCIcX91I06Y9LVjEzUdtXjTMwts7j9bYG5q2qPk3/56eTYGb7z2zVacg5u0WQIyWZ2d8Up045Nr0rZx7Mqo8r0F5ekNQGbBEov7xSNg51ap5qir/Kk9ybGUKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789742298; c=relaxed/simple; bh=+KPB1lKxojDRY5zT5v7uCSLpxLCh41smyn55eJFDsZA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XWZgsZro43XiB4/88VYoMXJaOKs2g6m1t3+1QKxZiT/ekg2xJ4IkkvNflQ1hVkPQAviCwf8XHd7i55Jgf68YAJwFjetW7N2Zr0Ka3qk7Hiix78FqFLk0clmHhZBemyhHyHlUwKdRIdU+GNjxW8apDVcoDhkqX0MbRDxJI7VUdzY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--stanleyjhu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aVA9Qz11; arc=none smtp.client-ip=209.85.210.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--stanleyjhu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aVA9Qz11" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-8716b158692so544506b3a.3 for ; Fri, 18 Sep 2026 07:38:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789742295; x=1790347095; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=3OT5P6cXKt1EfvL7fMqaW1ELy2bttp1x4RCZYKi6/zc=; b=aVA9Qz11E+sndk2B8QAwwmKWPBlneksH4yfSSdzKeubSpR/SVYdfogf1Bryyx2iccS o6k0zdw2O5U5b/7T9+oxnt2pyd043paq04LFdy/1oGuwA+kFO4U6NqMAIsGO2Q3eM+FS unXb9a6zllXduCkfxvcBeYdy8anD/0rRayKqneJzzdEjYHHXzNKJnwyR+bmaQFV6zL8X NVchZyw9bN3H7UDpLmzzRM4GRGdPe8Bfa6NZyLFbxTsfinuJ+Yk+l4vyYdZ4glW272Ix sS+2k4hh+1BLD8wbno5iRA5P2giVQ4NM1BrwMGXKwZkeN7RVJA0kZ2aAEBoiLY9oX+qP ILLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789742295; x=1790347095; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=3OT5P6cXKt1EfvL7fMqaW1ELy2bttp1x4RCZYKi6/zc=; b=N15rUp8aG78j22debNbQ8ATUiuhOvUbR5EVB2rMzYS4M9U7WT+2v2M10NSEl16Gk41 cy46PimEH1Scf3ff7odJx2rsj8J9OxvZyK2vPDm4IRVUxyPVWvTXc2ZU3YzTZIHUeu4s r29UdFeoGfd8Ym2MZjynaEJH7o9ZkClSSIBC3ttUTPtkZRNc41QJvnN4Ouyx9fduDFIw lElIjVRN/kEPTZYS0C8K/X5+Hk94hQSpdf1F4K1wtzdAtMY2jSfLd85ZlQ89iuC+OYui aFNayr0pHgTfGvZguFffWSEWm2vffj3AooMBvcqHfnh5JgnfKpnSOHDRW1Krz5ZHFKjQ B0kA== X-Forwarded-Encrypted: i=1; AKwUvBxNS/qaPJ0vgd9y8T9suMqS83ud0vhC4TzPZSMoq4Fw5QxFIKUR1hM98slIchnQ3FyDGVIpPunE5+7VA3I=@vger.kernel.org X-Gm-Message-State: AFuF++kp+N8DhXvbYw/VeF7lOFZH/w6W2lJAT8yN0nw+57dwGpgRgSw9 NbkV7C/TNj0inM8FvEsuf0JRZrEEQrJ4Wxc+a4k4zeQWqWYK1fDWU3hIzSeh7lPE/sVrQYNNAsb tadeYn0D4rcGeuxSCzcLVxw== X-Received: from pfqf14.prod.google.com ([2002:aa7:9d8e:0:b0:847:9be8:84d5]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4409:b0:872:b1f1:aa8d with SMTP id d2e1a72fcca58-874dc0f8b95mr5056873b3a.14.1789742295298; Fri, 18 Sep 2026 07:38:15 -0700 (PDT) Date: Fri, 18 Sep 2026 22:38:08 +0800 In-Reply-To: <20260918143809.3034592-1-stanleyjhu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918143809.3034592-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918143809.3034592-3-stanleyjhu@google.com> Subject: [PATCH v2 2/2] scsi: ufs: core: Decouple CQ sweep from request iterator in MCQ From: Stanley Jhu To: "Martin K . Petersen" , Bean Huo , Bart Van Assche Cc: Alim Akhtar , Avri Altman , "James E . J . Bottomley" , Manivannan Sadhasivam , Peter Wang , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, stanleyjhu@google.com, stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In MCQ mode, ufshcd_mcq_compl_pending_transfer() uses blk_mq_tagset_busy_iter() to iterate over busy requests during error recovery and host reset. However, both iterator callbacks perform whole-queue operations redundantly for each visited request: - force_compl =3D=3D true: ufshcd_mcq_force_compl_one() calls ufshcd_mcq_compl_all_cqes_lock() on every busy request, sweeping the entire completion ring (hwq->max_entries slots) once per active request under spin_lock_irqsave even though the first sweep already cleared all completion entries. - force_compl =3D=3D false: ufshcd_mcq_compl_one() acquires cq_lock and polls CQTPy over MMIO via ufshcd_mcq_poll_cqe_lock() for every busy request without doing any per-request work. Sweep or poll each hardware queue (hba->uhq[i]) once at the start of ufshcd_mcq_compl_pending_transfer(). When force_compl is true, run blk_mq_tagset_busy_iter() afterward to complete residual in-flight requests with DID_REQUEUE, and remove the now-unused ufshcd_mcq_compl_one() callback. Fixes: ab248643d3d6 ("scsi: ufs: core: Add error handling for MCQ mode") Cc: stable@vger.kernel.org Signed-off-by: Stanley Jhu --- drivers/ufs/core/ufshcd.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index 2ba244cf40ac..a69dcb04d985 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -6044,8 +6044,6 @@ static bool ufshcd_mcq_force_compl_one(struct request= *rq, void *priv) if (blk_mq_is_reserved_rq(rq) || !hwq) return true; =20 - ufshcd_mcq_compl_all_cqes_lock(hba, hwq); - /* * For those cmds of which the cqes are not present in the cq, complete * them explicitly. @@ -6061,19 +6059,6 @@ static bool ufshcd_mcq_force_compl_one(struct reques= t *rq, void *priv) return true; } =20 -static bool ufshcd_mcq_compl_one(struct request *rq, void *priv) -{ - struct scsi_device *sdev =3D rq->q->queuedata; - struct Scsi_Host *shost =3D sdev->host; - struct ufs_hba *hba =3D shost_priv(shost); - struct ufs_hw_queue *hwq =3D ufshcd_mcq_req_to_hwq(hba, rq); - - if (!blk_mq_is_reserved_rq(rq) && hwq) - ufshcd_mcq_poll_cqe_lock(hba, hwq); - - return true; -} - /** * ufshcd_mcq_compl_pending_transfer - MCQ mode function. It is * invoked from the error handler context or ufshcd_host_reset_and_restore= () @@ -6088,10 +6073,18 @@ static bool ufshcd_mcq_compl_one(struct request *rq= , void *priv) static void ufshcd_mcq_compl_pending_transfer(struct ufs_hba *hba, bool force_compl) { - blk_mq_tagset_busy_iter(&hba->host->tag_set, - force_compl ? ufshcd_mcq_force_compl_one : - ufshcd_mcq_compl_one, - NULL); + int i; + + for (i =3D 0; i < hba->nr_hw_queues; i++) { + if (force_compl) + ufshcd_mcq_compl_all_cqes_lock(hba, &hba->uhq[i]); + else + ufshcd_mcq_poll_cqe_lock(hba, &hba->uhq[i]); + } + + if (force_compl) + blk_mq_tagset_busy_iter(&hba->host->tag_set, + ufshcd_mcq_force_compl_one, NULL); } =20 /** --=20 2.55.0.1082.g2b9226bbc0-goog