From 2b2bb55990cf1d9dbd59ee326d545798061fce97 Mon Sep 17 00:00:00 2001 From: James Houghton Date: Mon, 26 Oct 2026 21:52:17 +0000 Subject: [PATCH 01/21] scsi: bsg: Don't copy sense data to user memory when canceled scsi_bsg_uring_task_cb() copies the sense data to the submitter's response_addr with copy_to_user(). It runs as io_uring task work, but io_uring may also run it with tw.cancel set from other contexts when the submitting task can't run it: from a kworker when the submitting task has exited (io_tctx_fallback_work()), or from io_cancel_local_task_work() when a DEFER_TASKRUN ring is torn down. In a kworker, current->mm is NULL, and the copy does not reliably fail: the user half of the address space belongs to whatever mm happens to be lazily loaded, if any, so up to max_response_len bytes of sense data may be written into an unrelated process. Don't copy the sense data when the task work is canceled, and complete the command with -ECANCELED. Fixes: 7b6d3255e7f8 ("scsi: bsg: add io_uring passthrough handler") Signed-off-by: James Houghton Reviewed-by: Jens Axboe Link: https://patch.msgid.link/20261026215226.3713941-2-jthoughton@google.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/scsi/scsi_bsg.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c index e4a65d7a2374..3cf1caad02f7 100644 --- a/drivers/scsi/scsi_bsg.c +++ b/drivers/scsi/scsi_bsg.c @@ -43,9 +43,17 @@ static void scsi_bsg_uring_task_cb(struct io_tw_req tw_req, io_tw_token_t tw) if (pdu->bio) ret = blk_rq_unmap_user(pdu->bio); + /* + * If the request is being canceled, e.g. because the submitting task + * has exited, this may run from a kworker or in a different task, so + * don't copy the sense data to the submitter's address. + */ + if (unlikely(tw.cancel)) + ret = -ECANCELED; + if (scsi_status_is_check_condition(scmd->result)) { driver_status = DRIVER_SENSE; - if (pdu->response_addr) + if (pdu->response_addr && !tw.cancel) sense_len_wr = min_t(unsigned int, pdu->max_response_len, scmd->sense_len); } From 6da1bc55c060bec3163b0e6077f3ac9641f8100c Mon Sep 17 00:00:00 2001 From: Stanley Jhu Date: Thu, 29 Oct 2026 15:08:30 +0800 Subject: [PATCH 02/21] scsi: ufs: core: Keep clock-gating hold on failed W-LUN runtime resume 1. __ufshcd_wl_suspend() takes one ufshcd_hold() reference and sets hba->clk_gating.is_suspended while the UFS device W-LUN enters RPM_SUSPENDED. 2. Since commit eef1d74bf88f ("PM: runtime: Only set runtime_error on suspend callback failures"), a failed runtime resume (for example START STOP UNIT completing with an error) leaves the W-LUN in RPM_SUSPENDED with power.runtime_error == 0, while __ufshcd_wl_resume() already clears hba->clk_gating.is_suspended and calls ufshcd_release(). 3. The next W-LUN runtime resume runs __ufshcd_wl_resume() again without an intervening suspend and calls ufshcd_release() a second time, dropping a reference it does not own and underflowing hba->clk_gating.active_reqs on UFSHCD_CAP_CLK_GATING hosts. Keep hba->clk_gating.is_suspended and the suspend hold across a failed W-LUN runtime resume, so the hold stays paired with the W-LUN RPM_SUSPENDED state and the next successful resume releases it. Fixes: eef1d74bf88f ("PM: runtime: Only set runtime_error on suspend callback failures") Cc: stable@vger.kernel.org # v7.3+ Signed-off-by: Stanley Jhu Reviewed-by: Brian Norris Reviewed-by: Peter Wang Link: https://patch.msgid.link/20261029040002.2100002-1-stanleyjhu@google.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/core/ufshcd.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index fd52deb72d21..4f0301d2a5d4 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -10651,8 +10651,14 @@ vendor_suspend: out: if (ret) ufshcd_update_evt_hist(hba, UFS_EVT_WL_RES_ERR, (u32)ret); - hba->clk_gating.is_suspended = false; - ufshcd_release(hba); + /* + * A failed runtime resume leaves the W-LUN RPM_SUSPENDED, so keep + * is_suspended and the suspend hold for the next resume. + */ + if (!ret || pm_op != UFS_RUNTIME_PM) { + hba->clk_gating.is_suspended = false; + ufshcd_release(hba); + } hba->pm_op_in_progress = false; return ret; } From 94196cd3413fba6cf6d1f71d6de401e56caaf29b Mon Sep 17 00:00:00 2001 From: Dmitry Bogdanov Date: Tue, 27 Oct 2026 21:01:22 +0300 Subject: [PATCH 03/21] scsi: Revert "scsi: target: iscsi: Fix hang for aborted WRITE_PENDING commands" This reverts commit d5869dae5080e976d4b03cc33eb7ceb527f242bf as it introduces double completion of the command and other issues. Signed-off-by: Dmitry Bogdanov Reviewed-by: Laurence Oberman Tested-by: Laurence Oberman Link: https://patch.msgid.link/20261027180123.13163-2-d.bogdanov@yadro.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/target/iscsi/iscsi_target.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c index 124ff269b8e7..62ada3a52210 100644 --- a/drivers/target/iscsi/iscsi_target.c +++ b/drivers/target/iscsi/iscsi_target.c @@ -1533,10 +1533,8 @@ __iscsit_check_dataout_hdr(struct iscsit_conn *conn, void *buf, */ if (se_cmd->transport_state & CMD_T_ABORTED) { if (hdr->flags & ISCSI_FLAG_CMD_FINAL && - --cmd->outstanding_r2ts < 1) { + --cmd->outstanding_r2ts < 1) iscsit_stop_dataout_timer(cmd); - target_complete_cmd(se_cmd, SAM_STAT_TASK_ABORTED); - } return iscsit_dump_data_payload(conn, payload_length, 1); } From cae8db99f3956e6446827ae4a9605a1ed815772b Mon Sep 17 00:00:00 2001 From: Dmitry Bogdanov Date: Tue, 27 Oct 2026 21:01:23 +0300 Subject: [PATCH 04/21] scsi: target: iscsi: Fix hanging WRITE_PENDING commands When Data comes for an aborted WRITE command in WRITE_PENDING state it is silently received and the command does not pass to the target. That leads to hanging of other session with LUN_RESET aborting that command. Pass the aborted WRITE commands when all Data has been gathered. Signed-off-by: Dmitry Bogdanov Reviewed-by: Laurence Oberman Tested-by: Laurence Oberman Link: https://patch.msgid.link/20261027180123.13163-3-d.bogdanov@yadro.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/target/iscsi/iscsi_target.c | 17 ----------------- drivers/target/iscsi/iscsi_target_erl1.c | 8 -------- 2 files changed, 25 deletions(-) diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c index 62ada3a52210..03d618cd7672 100644 --- a/drivers/target/iscsi/iscsi_target.c +++ b/drivers/target/iscsi/iscsi_target.c @@ -1518,23 +1518,6 @@ __iscsit_check_dataout_hdr(struct iscsit_conn *conn, void *buf, if (hdr->flags & ISCSI_FLAG_CMD_FINAL) iscsit_stop_dataout_timer(cmd); - return iscsit_dump_data_payload(conn, payload_length, 1); - } - } else { - /* - * For the normal solicited data path: - * - * Check for a delayed TASK_ABORTED status and dump any - * incoming data out payload if one exists. Also, when the - * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current - * data out sequence, we decrement outstanding_r2ts. Once - * outstanding_r2ts reaches zero, go ahead and send the delayed - * TASK_ABORTED status. - */ - if (se_cmd->transport_state & CMD_T_ABORTED) { - if (hdr->flags & ISCSI_FLAG_CMD_FINAL && - --cmd->outstanding_r2ts < 1) - iscsit_stop_dataout_timer(cmd); return iscsit_dump_data_payload(conn, payload_length, 1); } diff --git a/drivers/target/iscsi/iscsi_target_erl1.c b/drivers/target/iscsi/iscsi_target_erl1.c index d8ca06e697d6..44b851e4a602 100644 --- a/drivers/target/iscsi/iscsi_target_erl1.c +++ b/drivers/target/iscsi/iscsi_target_erl1.c @@ -908,8 +908,6 @@ int iscsit_execute_cmd(struct iscsit_cmd *cmd, int ooo) return 0; } spin_unlock_bh(&cmd->istate_lock); - if (cmd->se_cmd.transport_state & CMD_T_ABORTED) - return 0; return transport_send_check_condition_and_sense(se_cmd, cmd->sense_reason, 0); } @@ -927,9 +925,6 @@ int iscsit_execute_cmd(struct iscsit_cmd *cmd, int ooo) if (!(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) { - if (cmd->se_cmd.transport_state & CMD_T_ABORTED) - return 0; - iscsit_set_dataout_sequence_values(cmd); conn->conn_transport->iscsit_get_dataout(conn, cmd, false); } @@ -942,9 +937,6 @@ int iscsit_execute_cmd(struct iscsit_cmd *cmd, int ooo) if ((cmd->data_direction == DMA_TO_DEVICE) && !(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) { - if (cmd->se_cmd.transport_state & CMD_T_ABORTED) - return 0; - iscsit_set_unsolicited_dataout(cmd); } return target_submit(&cmd->se_cmd); From ebf5cab0dd223a36a09742a91be4fea97e46c816 Mon Sep 17 00:00:00 2001 From: Stanley Jhu Date: Thu, 29 Oct 2026 15:08:36 +0800 Subject: [PATCH 05/21] scsi: ufs: core: Restore jiffies conversion of device management timeouts Before commit 08b12cda6c44 ("scsi: ufs: core: Switch to scsi_get_internal_cmd()"), ufshcd_wait_for_dev_cmd() converted the millisecond timeout with msecs_to_jiffies() before waiting for the command. That commit moved the timeout to the block layer and assigns the millisecond value to rq->timeout as is, but rq->timeout is in jiffies. A 1500 ms query timeout (QUERY_REQ_TIMEOUT_DEFAULT) becomes 6 s with CONFIG_HZ=250 and 15 s with CONFIG_HZ=100, and NOP_OUT_TIMEOUT and ADVANCED_RPMB_REQ_TIMEOUT grow the same way. Restore the conversion with msecs_to_jiffies(). Fixes: 08b12cda6c44 ("scsi: ufs: core: Switch to scsi_get_internal_cmd()") Cc: stable@vger.kernel.org # v6.19+ Signed-off-by: Stanley Jhu Reviewed-by: Bart Van Assche Reviewed-by: Peter Wang Link: https://patch.msgid.link/20261029040003.2100003-2-stanleyjhu@google.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/core/ufshcd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index 4f0301d2a5d4..b85b88e4c308 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -3390,7 +3390,7 @@ static int ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct scsi_cmnd *cmd, struct request *rq = scsi_cmd_to_rq(cmd); blk_status_t sts; - rq->timeout = timeout; + rq->timeout = msecs_to_jiffies(timeout); sts = blk_execute_rq(rq, true); if (sts != BLK_STS_OK) return blk_status_to_errno(sts); From cbab12e5b4a7f49d5add4301de275351269cd431 Mon Sep 17 00:00:00 2001 From: Stanley Jhu Date: Thu, 29 Oct 2026 15:08:37 +0800 Subject: [PATCH 06/21] scsi: ufs: core: Introduce ufshcd_rpm_resume_and_get() helper ufshcd_clkscale_enable_store() and ufshcd_auto_hibern8_update() resume the UFS device W-LUN with ufshcd_rpm_get_sync() and ignore its return value. For clkscale_enable: 1. The W-LUN is runtime suspended and clock scaling is disabled. 2. User space writes 1 to clkscale_enable. 3. ufshcd_clkscale_enable_store() calls ufshcd_rpm_get_sync(). 4. The W-LUN runtime resume fails and the W-LUN stays suspended. 5. ufshcd_rpm_get_sync() returns a negative errno, which is ignored. 6. ufshcd_clkscale_enable_store() enables clock scaling and resumes devfreq while the W-LUN is still suspended. For auto_hibern8: 1. The W-LUN starts a runtime suspend. 2. User space writes a new value to auto_hibern8. 3. ufshcd_auto_hibern8_update() sees that the W-LUN is not yet suspended and calls ufshcd_rpm_get_sync(). 4. ufshcd_rpm_get_sync() waits for the runtime suspend to finish and then runs the W-LUN runtime resume. 5. The W-LUN runtime resume fails and the W-LUN stays suspended. 6. ufshcd_rpm_get_sync() returns a negative errno, which is ignored. 7. ufshcd_auto_hibern8_update() writes REG_AUTO_HIBERNATE_IDLE_TIMER while the W-LUN is still suspended. Add ufshcd_rpm_resume_and_get() wrapping pm_runtime_resume_and_get() so failed resumes drop the usage reference automatically, and return early in ufshcd_clkscale_enable_store() and ufshcd_auto_hibern8_update() when it fails. Fixes: b294ff3e3449 ("scsi: ufs: core: Enable power management for wlun") Cc: stable@vger.kernel.org Signed-off-by: Stanley Jhu Reviewed-by: Bart Van Assche Reviewed-by: Peter Wang Link: https://patch.msgid.link/20261029040003.2100003-3-stanleyjhu@google.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/core/ufshcd-priv.h | 5 +++++ drivers/ufs/core/ufshcd.c | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/ufs/core/ufshcd-priv.h b/drivers/ufs/core/ufshcd-priv.h index e55c2a02c1f5..99f654b79035 100644 --- a/drivers/ufs/core/ufshcd-priv.h +++ b/drivers/ufs/core/ufshcd-priv.h @@ -388,6 +388,11 @@ static inline int ufshcd_rpm_get_sync(struct ufs_hba *hba) return pm_runtime_get_sync(&hba->ufs_device_wlun->sdev_gendev); } +static inline int ufshcd_rpm_resume_and_get(struct ufs_hba *hba) +{ + return pm_runtime_resume_and_get(&hba->ufs_device_wlun->sdev_gendev); +} + static inline int ufshcd_rpm_get_if_active(struct ufs_hba *hba) { return pm_runtime_get_if_active(&hba->ufs_device_wlun->sdev_gendev); diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index b85b88e4c308..9cb009a6acbe 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -1864,7 +1864,9 @@ static ssize_t ufshcd_clkscale_enable_store(struct device *dev, if (value == hba->clk_scaling.is_enabled) goto out; - ufshcd_rpm_get_sync(hba); + err = ufshcd_rpm_resume_and_get(hba); + if (err) + goto out; ufshcd_hold(hba); hba->clk_scaling.is_enabled = value; @@ -4792,7 +4794,8 @@ void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit) WRITE_ONCE(hba->ahit, ahit); if (!pm_runtime_suspended(&hba->ufs_device_wlun->sdev_gendev)) { - ufshcd_rpm_get_sync(hba); + if (ufshcd_rpm_resume_and_get(hba)) + return; ufshcd_hold(hba); /* * Serialize with the host driver's AHIT control. Skip the From 93891d1c486efcef2267c32e6e73ca2fd243704e Mon Sep 17 00:00:00 2001 From: Stanley Jhu Date: Thu, 29 Oct 2026 15:08:38 +0800 Subject: [PATCH 07/21] scsi: ufs: sysfs: Propagate runtime PM resume errors All 14 ufshcd_rpm_get_sync() callers in ufs-sysfs.c resume the UFS device W-LUN and ignore the return value. For example, for a UFS descriptor read: 1. The W-LUN is runtime suspended. 2. User space reads a UFS descriptor attribute in sysfs. 3. ufs_sysfs_read_desc_param() calls ufshcd_rpm_get_sync(). 4. The W-LUN runtime resume fails and the W-LUN stays suspended. 5. ufshcd_rpm_get_sync() returns a negative errno, which is ignored. 6. ufs_sysfs_read_desc_param() sends a query UPIU while the W-LUN is still suspended. The other sysfs callers follow the same sequence. They send other query UPIUs, or read a host register in ufshcd_read_hci_reg(). Switch all 14 sysfs callers to ufshcd_rpm_resume_and_get() and return the error after releasing any held buffer and hba->host_sem. Fixes: aef80fd1da32 ("scsi: ufs: sysfs: Resume the proper SCSI device") Fixes: b9d104465a6c ("scsi: ufs: Prepare to add HCI capabilities sysfs") Fixes: 600edc6620a4 ("scsi: ufs: sysfs: Make max_number_of_rtt read-write") Fixes: 6c4148ce7cc1 ("scsi: ufs: wb: Add explicit flush sysfs attribute") Fixes: 500d4b742e0c ("scsi: ufs: core: Add WB buffer resize support") Fixes: ae7795a8c258 ("scsi: ufs: core: Add HID support") Fixes: 1fd2e77b8897 ("scsi: ufs: core: Add device level exception support") Cc: stable@vger.kernel.org # needs "scsi: ufs: core: Introduce ufshcd_rpm_resume_and_get() helper" Signed-off-by: Stanley Jhu Reviewed-by: Bart Van Assche Reviewed-by: Peter Wang Link: https://patch.msgid.link/20261029040003.2100003-4-stanleyjhu@google.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/core/ufs-sysfs.c | 67 ++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/drivers/ufs/core/ufs-sysfs.c b/drivers/ufs/core/ufs-sysfs.c index cf49ad0258e1..19b62cf52410 100644 --- a/drivers/ufs/core/ufs-sysfs.c +++ b/drivers/ufs/core/ufs-sysfs.c @@ -250,13 +250,19 @@ EXPORT_SYMBOL_GPL(ufshcd_us_to_ahit); static int ufshcd_read_hci_reg(struct ufs_hba *hba, u32 *val, unsigned int reg) { + int ret; + down(&hba->host_sem); if (!ufshcd_is_user_access_allowed(hba)) { up(&hba->host_sem); return -EBUSY; } - ufshcd_rpm_get_sync(hba); + ret = ufshcd_rpm_resume_and_get(hba); + if (ret) { + up(&hba->host_sem); + return ret; + } ufshcd_hold(hba); *val = ufshcd_readl(hba, reg); ufshcd_release(hba); @@ -350,7 +356,9 @@ static ssize_t wb_on_store(struct device *dev, struct device_attribute *attr, goto out; } - ufshcd_rpm_get_sync(hba); + res = ufshcd_rpm_resume_and_get(hba); + if (res) + goto out; res = ufshcd_wb_toggle(hba, wb_enable); ufshcd_rpm_put_sync(hba); out: @@ -421,7 +429,9 @@ static ssize_t enable_wb_buf_flush_store(struct device *dev, goto out; } - ufshcd_rpm_get_sync(hba); + res = ufshcd_rpm_resume_and_get(hba); + if (res) + goto out; res = ufshcd_wb_toggle_buf_flush(hba, enable_wb_buf_flush); ufshcd_rpm_put_sync(hba); @@ -490,7 +500,9 @@ static ssize_t wb_resize_enable_store(struct device *dev, goto out; } - ufshcd_rpm_get_sync(hba); + res = ufshcd_rpm_resume_and_get(hba); + if (res) + goto out; res = ufshcd_wb_set_resize_en(hba, mode); ufshcd_rpm_put_sync(hba); @@ -598,7 +610,9 @@ static ssize_t device_lvl_exception_id_show(struct device *dev, if (hba->dev_info.wspecversion < 0x410) return -EOPNOTSUPP; - ufshcd_rpm_get_sync(hba); + err = ufshcd_rpm_resume_and_get(hba); + if (err) + return err; err = ufshcd_query_attr_qword(hba, UPIU_QUERY_OPCODE_READ_ATTR, QUERY_ATTR_IDN_DEV_LVL_EXCEPTION_ID, 0, 0, &exception_id); @@ -1101,7 +1115,9 @@ static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba, goto out; } - ufshcd_rpm_get_sync(hba); + ret = ufshcd_rpm_resume_and_get(hba); + if (ret) + goto out; ret = ufshcd_read_desc_param(hba, desc_id, desc_index, param_offset, desc_buf, param_size); ufshcd_rpm_put_sync(hba); @@ -1482,7 +1498,12 @@ static ssize_t _name##_show(struct device *dev, \ up(&hba->host_sem); \ return -ENOMEM; \ } \ - ufshcd_rpm_get_sync(hba); \ + ret = ufshcd_rpm_resume_and_get(hba); \ + if (ret) { \ + kfree(desc_buf); \ + up(&hba->host_sem); \ + return ret; \ + } \ ret = ufshcd_query_descriptor_retry(hba, \ UPIU_QUERY_OPCODE_READ_DESC, QUERY_DESC_IDN_DEVICE, \ 0, 0, desc_buf, &desc_len); \ @@ -1548,7 +1569,9 @@ static ssize_t _name##_show(struct device *dev, \ } \ if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname)) \ index = ufshcd_wb_get_query_index(hba); \ - ufshcd_rpm_get_sync(hba); \ + ret = ufshcd_rpm_resume_and_get(hba); \ + if (ret) \ + goto out; \ ret = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG, \ QUERY_FLAG_IDN##_uname, index, &flag); \ ufshcd_rpm_put_sync(hba); \ @@ -1608,7 +1631,9 @@ static ssize_t max_number_of_rtt_show(struct device *dev, return -EBUSY; } - ufshcd_rpm_get_sync(hba); + ret = ufshcd_rpm_resume_and_get(hba); + if (ret) + goto out; ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt); ufshcd_rpm_put_sync(hba); @@ -1648,7 +1673,9 @@ static ssize_t max_number_of_rtt_store(struct device *dev, goto out; } - ufshcd_rpm_get_sync(hba); + ret = ufshcd_rpm_resume_and_get(hba); + if (ret) + goto out; memflags = memalloc_noio_save(); shost_for_each_device(sdev, hba->host) @@ -1700,7 +1727,11 @@ static int wb_read_resize_attrs(struct ufs_hba *hba, } index = ufshcd_wb_get_query_index(hba); - ufshcd_rpm_get_sync(hba); + ret = ufshcd_rpm_resume_and_get(hba); + if (ret) { + up(&hba->host_sem); + return ret; + } ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, idn, index, 0, attr_val); ufshcd_rpm_put_sync(hba); @@ -1760,7 +1791,9 @@ static ssize_t _name##_show(struct device *dev, \ } \ if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname)) \ index = ufshcd_wb_get_query_index(hba); \ - ufshcd_rpm_get_sync(hba); \ + ret = ufshcd_rpm_resume_and_get(hba); \ + if (ret) \ + goto out; \ if (ufshcd_is_qword_attr(QUERY_ATTR_IDN##_uname)) \ ret = ufshcd_query_attr_qword(hba, \ UPIU_QUERY_OPCODE_READ_ATTR, \ @@ -1848,7 +1881,11 @@ static int hid_query_attr(struct ufs_hba *hba, enum query_opcode opcode, return -EBUSY; } - ufshcd_rpm_get_sync(hba); + ret = ufshcd_rpm_resume_and_get(hba); + if (ret) { + up(&hba->host_sem); + return ret; + } ret = ufshcd_query_attr(hba, opcode, idn, 0, 0, attr_val); ufshcd_rpm_put_sync(hba); @@ -2121,7 +2158,9 @@ static ssize_t dyn_cap_needed_attribute_show(struct device *dev, goto out; } - ufshcd_rpm_get_sync(hba); + ret = ufshcd_rpm_resume_and_get(hba); + if (ret) + goto out; ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, QUERY_ATTR_IDN_DYN_CAP_NEEDED, lun, 0, &value); ufshcd_rpm_put_sync(hba); From 2b8541edf7c43f0294c45d2e687bbc1288292f4e Mon Sep 17 00:00:00 2001 From: Stanley Jhu Date: Thu, 29 Oct 2026 15:08:39 +0800 Subject: [PATCH 08/21] scsi: ufs: bsg: Abort ufs_bsg_request() when runtime resume fails ufs_bsg_request() resumes the UFS device W-LUN with ufshcd_rpm_get_sync() and ignores its return value: 1. The W-LUN is runtime suspended. 2. User space submits a BSG request. 3. ufs_bsg_request() calls ufshcd_rpm_get_sync(). 4. The W-LUN runtime resume fails and the W-LUN stays suspended. 5. ufshcd_rpm_get_sync() returns a negative errno, which is ignored. 6. ufs_bsg_request() dispatches the BSG command while the W-LUN is still suspended. Use ufshcd_rpm_resume_and_get() and jump to out_complete on error so the BSG job is completed via bsg_job_done() with the failure code without dropping an unacquired runtime PM reference. Fixes: b294ff3e3449 ("scsi: ufs: core: Enable power management for wlun") Cc: stable@vger.kernel.org # needs "scsi: ufs: core: Introduce ufshcd_rpm_resume_and_get() helper" Signed-off-by: Stanley Jhu Reviewed-by: Bart Van Assche Reviewed-by: Peter Wang Link: https://patch.msgid.link/20261029040003.2100003-5-stanleyjhu@google.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/core/ufs_bsg.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/ufs/core/ufs_bsg.c b/drivers/ufs/core/ufs_bsg.c index baf6ae917d36..8df1a2c6abf6 100644 --- a/drivers/ufs/core/ufs_bsg.c +++ b/drivers/ufs/core/ufs_bsg.c @@ -150,7 +150,9 @@ static int ufs_bsg_request(struct bsg_job *job) goto out_complete; } - ufshcd_rpm_get_sync(hba); + ret = ufshcd_rpm_resume_and_get(hba); + if (ret) + goto out_complete; msgcode = bsg_request->msgcode; switch (msgcode) { From 9eb89611d95914e47cec62826e4000396df0f953 Mon Sep 17 00:00:00 2001 From: Stanley Jhu Date: Thu, 29 Oct 2026 15:08:40 +0800 Subject: [PATCH 09/21] scsi: ufs: hwmon, debugfs: Propagate runtime PM resume errors ufs_hwmon_read(), ufs_hwmon_write(), and ufs_debugfs_get_user_access() resume the UFS device W-LUN with ufshcd_rpm_get_sync() and ignore its return value. For example, for a hwmon temperature read: 1. The W-LUN is runtime suspended. 2. User space reads temp1_input of the UFS hwmon device. 3. ufs_hwmon_read() calls ufshcd_rpm_get_sync(). 4. The W-LUN runtime resume fails and the W-LUN stays suspended. 5. ufshcd_rpm_get_sync() returns a negative errno, which is ignored. 6. ufs_hwmon_read() sends an attribute query UPIU while the W-LUN is still suspended. ufs_hwmon_write() and the debugfs writers that use ufs_debugfs_get_user_access() (exception_event_mask and tx_eq_ctrl) follow the same sequence. Convert all three call sites to ufshcd_rpm_resume_and_get() and release hba->host_sem before returning the error, ahead of __acquire(ufs_debugfs) in ufs_debugfs_get_user_access(). Fixes: b294ff3e3449 ("scsi: ufs: core: Enable power management for wlun") Fixes: e88e2d32200a ("scsi: ufs: core: Probe for temperature notification support") Fixes: adbabdcf0db0 ("scsi: ufs: core: Add support to retrain TX Equalization via debugfs") Cc: stable@vger.kernel.org # needs "scsi: ufs: core: Introduce ufshcd_rpm_resume_and_get() helper" Signed-off-by: Stanley Jhu Reviewed-by: Bart Van Assche Reviewed-by: Peter Wang Link: https://patch.msgid.link/20261029040003.2100003-6-stanleyjhu@google.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/core/ufs-debugfs.c | 8 +++++++- drivers/ufs/core/ufs-hwmon.c | 12 ++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/ufs/core/ufs-debugfs.c b/drivers/ufs/core/ufs-debugfs.c index 9f83628f3e2c..44a1a9692324 100644 --- a/drivers/ufs/core/ufs-debugfs.c +++ b/drivers/ufs/core/ufs-debugfs.c @@ -70,12 +70,18 @@ token_context_lock(ufs_debugfs); static int ufs_debugfs_get_user_access(struct ufs_hba *hba) __cond_acquires(0, ufs_debugfs) { + int err; + down(&hba->host_sem); if (!ufshcd_is_user_access_allowed(hba)) { up(&hba->host_sem); return -EBUSY; } - ufshcd_rpm_get_sync(hba); + err = ufshcd_rpm_resume_and_get(hba); + if (err) { + up(&hba->host_sem); + return err; + } __acquire(ufs_debugfs); return 0; } diff --git a/drivers/ufs/core/ufs-hwmon.c b/drivers/ufs/core/ufs-hwmon.c index aa0b60a0ee8a..2b9673d92573 100644 --- a/drivers/ufs/core/ufs-hwmon.c +++ b/drivers/ufs/core/ufs-hwmon.c @@ -61,7 +61,11 @@ static int ufs_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 return -EBUSY; } - ufshcd_rpm_get_sync(hba); + err = ufshcd_rpm_resume_and_get(hba); + if (err) { + up(&hba->host_sem); + return err; + } switch (attr) { case hwmon_temp_enable: @@ -113,7 +117,11 @@ static int ufs_hwmon_write(struct device *dev, enum hwmon_sensor_types type, u32 return -EBUSY; } - ufshcd_rpm_get_sync(hba); + err = ufshcd_rpm_resume_and_get(hba); + if (err) { + up(&hba->host_sem); + return err; + } if (val == 1) err = ufshcd_update_ee_usr_mask(hba, MASK_EE_URGENT_TEMP, 0); From c84374cf4d2cb0c6efff91f7a37e85656cbbd593 Mon Sep 17 00:00:00 2001 From: Shawn Lin Date: Thu, 29 Oct 2026 15:24:55 +0800 Subject: [PATCH 10/21] scsi: ufs: rockchip: Improve RX noise immunity Program VND_RX_SAVE_DET_CTRL for each RX lane to raise the receiver's tolerance against short transients on the incoming signal, as recommended by the Rockchip PHY team. Signed-off-by: Shawn Lin Link: https://patch.msgid.link/20261029151030.1.I61cd43e660b146e49d4380a754e936270e5fb6a1@changeid Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/host/ufs-rockchip.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/ufs/host/ufs-rockchip.c b/drivers/ufs/host/ufs-rockchip.c index bac68f238e1c..87308c9b1070 100644 --- a/drivers/ufs/host/ufs-rockchip.c +++ b/drivers/ufs/host/ufs-rockchip.c @@ -82,6 +82,7 @@ static int ufs_rockchip_rk3576_phy_init(struct ufs_hba *hba) ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_PVALUE2, SEL_RX_LANE0 + i), 0x02); ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_OPTION, SEL_RX_LANE0 + i), 0xf6); ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_POWER_SAVING_CTRL, SEL_RX_LANE0 + i), 0x69); + ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_SAVE_DET_CTRL, SEL_RX_LANE0 + i), 0x18); } /* disable the mphy DME_SET cfg */ From 04feb5fd2c23894ebc2cdaf4a4cc38b8667b17ac Mon Sep 17 00:00:00 2001 From: Dongdong Hao Date: Wed, 4 Nov 2026 17:28:08 +0800 Subject: [PATCH 11/21] scsi: leapraid: Fix init_cmd_priv error handling leapraid_init_cmd_priv() ignores the return value of leapraid_internal_init_cmd_priv() and always returns success. If dma_pool_alloc() fails, io_tracker->chain remains NULL, but the SCSI midlayer considers the command successfully initialized. A subsequent I/O requiring more than LEAPRAID_SGL_INLINE_THRESHOLD DMA-mapped scatter-gather entries dereferences the NULL chain buffer while building the chained SGL, causing a kernel crash. Return the result of leapraid_internal_init_cmd_priv() so that allocation failures propagate to the SCSI midlayer and abort initialization of the affected request. Reviewed-by: Damien Le Moal Signed-off-by: Dongdong Hao Link: https://patch.msgid.link/20261104092808.206067-1-doubled@leap-io-kernel.com Fixes: 5597088c9e79 ("scsi: leapraid: Add new SCSI driver") Signed-off-by: Martin K. Petersen (Oracle) --- drivers/scsi/leapraid/leapraid_os.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/drivers/scsi/leapraid/leapraid_os.c b/drivers/scsi/leapraid/leapraid_os.c index da597eb99377..749e50366dbc 100644 --- a/drivers/scsi/leapraid/leapraid_os.c +++ b/drivers/scsi/leapraid/leapraid_os.c @@ -935,22 +935,16 @@ static int leapraid_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *scmd) { struct leapraid_adapter *adapter = shost_priv(shost); - struct leapraid_io_req_tracker *io_tracker; - io_tracker = scsi_cmd_priv(scmd); - leapraid_internal_init_cmd_priv(adapter, io_tracker); - - return 0; + return leapraid_internal_init_cmd_priv(adapter, scsi_cmd_priv(scmd)); } static int leapraid_exit_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *scmd) { struct leapraid_adapter *adapter = shost_priv(shost); - struct leapraid_io_req_tracker *io_tracker; - io_tracker = scsi_cmd_priv(scmd); - leapraid_internal_exit_cmd_priv(adapter, io_tracker); + leapraid_internal_exit_cmd_priv(adapter, scsi_cmd_priv(scmd)); return 0; } From 30bdadc9aa3a684cd634c7fa49740b1151e7b2e5 Mon Sep 17 00:00:00 2001 From: Kiwoong Kim Date: Fri, 6 Nov 2026 13:12:46 +0900 Subject: [PATCH 12/21] scsi: ufs: core: Skip ABORT TASK for device management commands In MCQ mode, ufshcd_mcq_abort() falls through to ufshcd_try_to_abort_task() for all timed-out commands. However, device management commands (query, NOP) use reserved requests and are not SCSI I/O commands. The ABORT TASK task management function is designed for SCSI I/O UPIUs and is not applicable to device management UPIUs. This is only reachable since device management commands were converted to scsi_get_internal_cmd(); before that, a timed-out query was handled by ufshcd_wait_for_dev_cmd() -> ufshcd_clear_cmd() and never reached ufshcd_try_to_abort_task(). Fix this by checking ufshcd_is_scsi_cmd() in ufshcd_try_to_abort_task(). For a reserved (device management) command, skip the TM path and call ufshcd_clear_cmd() directly. ufshcd_abort() now calls ufshcd_try_to_abort_task() for every request, so its separate reserved-request branch is removed. Fixes: 08b12cda6c44 ("scsi: ufs: core: Switch to scsi_get_internal_cmd()") Reported-by: Bart Van Assche Signed-off-by: Kiwoong Kim Reviewed-by: Bean Huo Reviewed-by: Bart Van Assche Link: https://patch.msgid.link/ab14d2f1414fa1a0a396f160abf8c27c404b4ec4.1793938245.git.kwmad.kim@samsung.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/core/ufshcd.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index 9cb009a6acbe..ddc953b45ce9 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -8067,11 +8067,15 @@ static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) * @hba: Pointer to adapter instance * @tag: Tag of the task to be aborted * - * Abort the pending command in device by sending UFS_ABORT_TASK task management - * command, and in host controller by clearing the door-bell register. There can - * be race between controller sending the command to the device while abort is - * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is - * really issued and then try to abort it. + * Abort the pending SCSI command in device by sending UFS_ABORT_TASK task + * management command, and in host controller by clearing the door-bell + * register. There can be race between controller sending the command to the + * device while abort is issued. To avoid that, first issue UFS_QUERY_TASK to + * check if the command is really issued and then try to abort it. + * + * Device management commands are not SCSI commands, so no task management + * function is issued for them; only the command is cleared in the host + * controller. * * Return: zero on success, non-zero on failure. */ @@ -8088,6 +8092,9 @@ int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag) lrbp = scsi_cmd_priv(cmd); + if (!ufshcd_is_scsi_cmd(cmd)) + goto clear_cmd; + for (poll_cnt = 100; poll_cnt; poll_cnt--) { err = ufshcd_issue_tm_cmd(hba, lrbp->lun, tag, UFS_QUERY_TASK, &resp); @@ -8133,6 +8140,7 @@ int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag) return err; } +clear_cmd: err = ufshcd_clear_cmd(hba, tag); if (err) dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", @@ -8226,10 +8234,7 @@ static int ufshcd_abort(struct scsi_cmnd *cmd) goto release; } - if (blk_mq_is_reserved_rq(rq)) - err = ufshcd_clear_cmd(hba, tag); - else - err = ufshcd_try_to_abort_task(hba, tag); + err = ufshcd_try_to_abort_task(hba, tag); if (err) { dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); From e510b597d8afca2e790e2b5429bc5e01e3386f65 Mon Sep 17 00:00:00 2001 From: Kiwoong Kim Date: Fri, 6 Nov 2026 13:12:47 +0900 Subject: [PATCH 13/21] scsi: ufs: mcq: Poll OCS after sq cleanup for device management commands Extend ufshcd_mcq_sq_cleanup() to poll the OCS field in the UTRD of a device management command after a successful SQ_CUS poll. UFSHCI does not specify the order between the OCS update and the SQ_CUS update, so OCS may still be stale when cleanup returns. Without this poll, a late response to a timed-out query can arrive while a retry is already in flight, leaving the host controller with two query contexts and raising IS.UTPES. Here is the timeline of an actual case I found with kernel 6.18: 1. The driver sent a query but the device didn't respond to it for a long time. [ 7384.156142] [4: kworker/u40:5:10671] exynos-ufs 38300000.ufs: ufshcd_mcq_sq_cleanup, hwq 0: cleanup return code (RTC) 0 [ 7384.156154] [4: kworker/u40:5:10671] exynos-ufs 38300000.ufs: ufshcd_query_flag: Sending flag query for idn 14 failed, err = -11 2. So after the query timed out, the driver did SQ cleanup. Then the host controller updated the CQ tail and raised IS.CQES. Unfortunately, complete() was called by the driver's ISR afterward. 3. The driver started to retry the query. In the meantime, it set OCS to 0xF. Then the driver didn't wait for completion due to the previous complete() call, and just checked OCS. [ 7384.156163] [4: kworker/u40:5:10671] exynos-ufs 38300000.ufs: ufshcd_query_flag: Sending flag query for idn 14 failed, err = 15 4. The driver retried the query again, but the device didn't return anything, so the query also timed out. [ 7385.660108] [3: kworker/u40:5:10671] exynos-ufs 38300000.ufs: ufshcd_query_flag: Sending flag query for idn 14 failed, err = -11 5. A few seconds later, the device returned the response of the first query, but since the host controller was holding two query contexts and couldn't handle it, it raised IS.UTPES. The same problem exists in the current code. If the query times out, the SCSI error handler calls ufshcd_abort() -> ufshcd_mcq_abort() -> ufshcd_mcq_sq_cleanup(), the request is finished, and the retry reuses the same reserved tag, struct scsi_cmnd and devman UCD. A late CQE for the first query then maps to the same tag and completes the retry by calling scsi_done() too early. The retry then returns the OCS of the first query and, if that OCS is OCS_SUCCESS, its response as well. Hence ufshcd_mcq_sq_cleanup() must not return until the CQE of the cleaned-up request has been processed. The poll is limited to device management commands because ufshcd_compl_one_cqe() copies the OCS from the CQE into the UTRD only for them. For SCSI commands, the OCS in the UTRD stays OCS_INVALID_COMMAND_STATUS, so polling it would always time out. The poll waits until OCS is no longer OCS_INVALID_COMMAND_STATUS or MCQ_POLL_US elapses. -ETIMEDOUT is returned only if OCS is still OCS_INVALID_COMMAND_STATUS after the poll. Any other value, such as OCS_ABORTED reported by the host controller for the cleaned-up request, means the cleanup has completed. The UFSHCI specification says "The host controller shall free up any resources associated to the nexus, and shall post a Completion Queue entry with OCS = ABORTED if clean up successful." Each poll iteration reaps the CQ with ufshcd_mcq_poll_cqe_lock() before reading the OCS, as ufshcd_clear_lu_cmds() does after ufshcd_clear_cmd(). This removes the dependency on interrupt delivery. Otherwise the OCS is only updated once the interrupt handler processes the CQE, and a delayed or lost interrupt would make the poll time out and escalate to a host reset although the request has already been cleaned up. The OCS is still read under hwq->cq_lock because the interrupt handler may reap the CQE between the reap and the read. ufshcd_compl_one_cqe() updates the OCS under that lock before it sets cmd->result and calls scsi_done(). Without the lock, cleanup could observe the new OCS and return while the completion is still running. The abort could then finish the command and a retry could reuse the same reserved request before the pending scsi_done() call, which would then complete the retry prematurely. Fixes: 8d7290348992 ("scsi: ufs: mcq: Add supporting functions for MCQ abort") Signed-off-by: Kiwoong Kim Reviewed-by: Bart Van Assche Reviewed-by: Bean Huo Link: https://patch.msgid.link/a1af44cd90dca0dd8a9a06d09a254a4e7e0861b2.1793938245.git.kwmad.kim@samsung.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/core/ufs-mcq.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/drivers/ufs/core/ufs-mcq.c b/drivers/ufs/core/ufs-mcq.c index 25e392f8ede2..005a70462473 100644 --- a/drivers/ufs/core/ufs-mcq.c +++ b/drivers/ufs/core/ufs-mcq.c @@ -554,6 +554,23 @@ static int ufshcd_mcq_sq_start(struct ufs_hba *hba, struct ufs_hw_queue *hwq) return err; } +/* + * Reap the CQ directly so that progress does not depend on interrupt + * delivery. ufshcd_compl_one_cqe() updates the OCS under cq_lock before + * calling scsi_done(). Holding cq_lock while reading the OCS makes sure that + * the completion has finished once the updated OCS is observed, even if the + * CQE was reaped by the interrupt handler. + */ +static enum utp_ocs ufshcd_mcq_poll_ocs(struct ufs_hba *hba, + struct ufs_hw_queue *hwq, + struct ufshcd_lrb *lrbp) +{ + ufshcd_mcq_poll_cqe_lock(hba, hwq); + + guard(spinlock_irqsave)(&hwq->cq_lock); + return lrbp->utr_descriptor_ptr->header.ocs; +} + /** * ufshcd_mcq_sq_cleanup - Clean up submission queue resources * associated with the pending command. @@ -613,6 +630,23 @@ int ufshcd_mcq_sq_cleanup(struct ufs_hba *hba, int task_tag) __func__, id, FIELD_GET(SQ_ICU_ERR_CODE_MASK, readl(reg))); + /* + * For device management commands, there is no order guaranteed between + * OCS update and CUS update, which is why we need to wait here. SCSI + * commands are excluded because the OCS in their UTRD is not updated + * from the CQE. + */ + if (!err && blk_mq_is_reserved_rq(rq)) { + enum utp_ocs ocs; + + err = read_poll_timeout(ufshcd_mcq_poll_ocs, ocs, + ocs != OCS_INVALID_COMMAND_STATUS, 20, + MCQ_POLL_US, false, hba, hwq, lrbp); + if (err) + dev_err(hba->dev, "%s: OCS not updated. hwq=%d, tag=%d err=%d\n", + __func__, id, task_tag, err); + } + if (ufshcd_mcq_sq_start(hba, hwq)) err = -ETIMEDOUT; From b3da73f09499b34faf5c43c50e1d09e4286226de Mon Sep 17 00:00:00 2001 From: Joonhoe Kim <26rote@gmail.com> Date: Fri, 23 Oct 2026 01:50:06 +0900 Subject: [PATCH 14/21] scsi: ufs: qcom: Reprogram ESI after a host controller reset ufs_qcom_config_esi() returns early once the ESI interrupts are set up. ufshcd_config_mcq() also calls it after every host controller reset, which clears the ESI base address, the vector mask and ESI enable. After the error handler has reset the controller, the completions then arrive only on the legacy interrupt while mcq_esi_enabled stays set. Allocate the MSIs and request their interrupts only once, but program the ESI registers on every call. The MSI message is not cached for platform MSIs, so keep the one written at allocation. Fixes: 519b6274a777 ("scsi: ufs: qcom: Add MCQ ESI config vendor specific ops") Signed-off-by: Joonhoe Kim <26rote@gmail.com> Link: https://patch.msgid.link/20261022165006.1275-1-26rote@gmail.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/ufs/host/ufs-qcom.c | 12 +++++++++--- drivers/ufs/host/ufs-qcom.h | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/ufs/host/ufs-qcom.c b/drivers/ufs/host/ufs-qcom.c index 27c87e62f8e9..47fc785612ad 100644 --- a/drivers/ufs/host/ufs-qcom.c +++ b/drivers/ufs/host/ufs-qcom.c @@ -2413,7 +2413,9 @@ static void ufs_qcom_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) { struct device *dev = msi_desc_to_dev(desc); struct ufs_hba *hba = dev_get_drvdata(dev); + struct ufs_qcom_host *host = ufshcd_get_variant(hba); + host->esi_msg = *msg; ufshcd_mcq_config_esi(hba, msg); } @@ -2441,8 +2443,10 @@ static int ufs_qcom_config_esi(struct ufs_hba *hba) struct ufs_qcom_irq *qi; int nr_irqs, ret; - if (host->esi_enabled) - return 0; + if (host->esi_enabled) { + ufshcd_mcq_config_esi(hba, &host->esi_msg); + goto enable; + } /* * 1. We only handle CQs as of now. @@ -2480,12 +2484,14 @@ static int ufs_qcom_config_esi(struct ufs_hba *hba) } } + host->esi_enabled = true; + +enable: if (host->hw_ver.major >= 6) { ufshcd_rmwl(hba, ESI_VEC_MASK, FIELD_PREP(ESI_VEC_MASK, MAX_ESI_VEC - 1), REG_UFS_CFG3); } ufshcd_mcq_enable_esi(hba); - host->esi_enabled = true; return 0; } diff --git a/drivers/ufs/host/ufs-qcom.h b/drivers/ufs/host/ufs-qcom.h index 944b2e532a09..d2647695abe3 100644 --- a/drivers/ufs/host/ufs-qcom.h +++ b/drivers/ufs/host/ufs-qcom.h @@ -5,6 +5,7 @@ #ifndef UFS_QCOM_H_ #define UFS_QCOM_H_ +#include #include #include #include @@ -360,6 +361,7 @@ struct ufs_qcom_host { u32 phy_gear; bool esi_enabled; + struct msi_msg esi_msg; u32 saved_tx_eq_g1_setting; u32 boot_spare_cfg; From fe5988e8708e1d42cb844bd9ac8f0a51388061fc Mon Sep 17 00:00:00 2001 From: Shao Mingyin Date: Mon, 26 Oct 2026 19:49:55 +0800 Subject: [PATCH 15/21] scsi: target: Fix NULL pointer dereference in target_dev_alua_lu_gp_store() Writing "NULL" to the alua_lu_gp attribute clears the ALUA LU group association of a device. The attribute can be written again after the association has already been cleared. In that case lu_gp_mem->lu_gp and lu_gp_new are both NULL, but the code still calls __core_alua_attach_lu_gp_mem(), which dereferences the NULL lu_gp pointer: BUG: kernel NULL pointer dereference, address: 0000000000000000 ... __core_alua_attach_lu_gp_mem target_dev_alua_lu_gp_store Clearing an already cleared association is a no-op, so return early when there is no association left to clear. Fixes: c66ac9db8d4a ("[SCSI] target: Add LIO target core v4.0.0-rc6") Signed-off-by: Shao Mingyin Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261026194955694FnXGoQ352dstLk5HPBqCG@zte.com.cn Signed-off-by: Martin K. Petersen (Oracle) --- drivers/target/target_core_configfs.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c index 2b19a956007b..430fc1eaeca5 100644 --- a/drivers/target/target_core_configfs.c +++ b/drivers/target/target_core_configfs.c @@ -2547,6 +2547,14 @@ static ssize_t target_dev_alua_lu_gp_store(struct config_item *item, spin_lock(&lu_gp_mem->lu_gp_mem_lock); lu_gp = lu_gp_mem->lu_gp; + if (!lu_gp && !lu_gp_new) { + /* + * The LU group association has already been cleared, so + * there is nothing left to do. + */ + spin_unlock(&lu_gp_mem->lu_gp_mem_lock); + return count; + } if (lu_gp) { /* * Clearing an existing lu_gp association, and replacing From c0bb6614132c64205a3c40c32dc1494c1bfeffe1 Mon Sep 17 00:00:00 2001 From: Shao Mingyin Date: Mon, 26 Oct 2026 19:51:33 +0800 Subject: [PATCH 16/21] scsi: target: Fix NULL pointer dereference in core_alua_do_port_transition() core_alua_do_port_transition() assumes that the device has an ALUA LU group member and that the member is associated with an LU group. Neither is guaranteed: internal-use HBAs never allocate dev_alua_lu_gp_mem, and the association with an LU group is dropped when "NULL" is written to the device's alua_lu_gp attribute. The configfs path can reach the second case. After clearing the LU group association, writing the implicit ALUA access state of a target port group calls core_alua_do_port_transition(), which dereferences the NULL lu_gp in atomic_inc(&lu_gp->lu_gp_ref_cnt). Return -ENODEV when the device has no LU group association instead. Fixes: c66ac9db8d4a ("[SCSI] target: Add LIO target core v4.0.0-rc6") Signed-off-by: Shao Mingyin Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261026195133237y2UxJLoV1FK6aD1zrFbIU@zte.com.cn Signed-off-by: Martin K. Petersen (Oracle) --- drivers/target/target_core_alua.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/target/target_core_alua.c b/drivers/target/target_core_alua.c index 140154d93c43..17386c96de13 100644 --- a/drivers/target/target_core_alua.c +++ b/drivers/target/target_core_alua.c @@ -1081,8 +1081,15 @@ int core_alua_do_port_transition( return -EINVAL; local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem; + if (!local_lu_gp_mem) + return -ENODEV; + spin_lock(&local_lu_gp_mem->lu_gp_mem_lock); lu_gp = local_lu_gp_mem->lu_gp; + if (!lu_gp) { + spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock); + return -ENODEV; + } atomic_inc(&lu_gp->lu_gp_ref_cnt); spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock); /* From 46846939d5f33c9333612662c52475f92e753407 Mon Sep 17 00:00:00 2001 From: Shao Mingyin Date: Mon, 26 Oct 2026 19:54:09 +0800 Subject: [PATCH 17/21] scsi: rdac: Keep the error status when controller lookup fails initialize_controller() records SCSI_DH_RES_TEMP_UNAVAIL when get_controller() fails, but then overwrites it with SCSI_DH_OK unconditionally. It therefore reports success with h->ctlr == NULL and rdac_bus_attach() continues with check_ownership(), which dereferences h->ctlr: BUG: kernel NULL pointer dereference, address: 0000000000000000 ... check_ownership rdac_bus_attach get_controller() only fails when its GFP_ATOMIC allocation fails, so the crash happens exactly when memory is already scarce. Only report success after the controller was found and the device was added to its list. Fixes: 327825574132 ("scsi_dh_rdac: switch to scsi_execute_req_flags()") Signed-off-by: Shao Mingyin Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261026195409354OWEI2b6HO7swSphvv1FOc@zte.com.cn Signed-off-by: Martin K. Petersen (Oracle) --- drivers/scsi/device_handler/scsi_dh_rdac.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/scsi/device_handler/scsi_dh_rdac.c b/drivers/scsi/device_handler/scsi_dh_rdac.c index 742534c99386..0986a6e2a6ae 100644 --- a/drivers/scsi/device_handler/scsi_dh_rdac.c +++ b/drivers/scsi/device_handler/scsi_dh_rdac.c @@ -450,14 +450,14 @@ static int initialize_controller(struct scsi_device *sdev, spin_lock(&list_lock); h->ctlr = get_controller(index, array_name, array_id, sdev); - if (!h->ctlr) + if (!h->ctlr) { err = SCSI_DH_RES_TEMP_UNAVAIL; - else { + } else { h->sdev = sdev; list_add_rcu(&h->node, &h->ctlr->dh_list); + err = SCSI_DH_OK; } spin_unlock(&list_lock); - err = SCSI_DH_OK; } return err; } From 612293fb469c81408829e6325bae25725968986f Mon Sep 17 00:00:00 2001 From: Boyan Liu Date: Wed, 28 Oct 2026 14:16:51 +0800 Subject: [PATCH 18/21] scsi: sd_zbc: Cap zone report count by buffer size sd_zbc_report_zones() limits descriptor parsing to the requested zone count and the device-reported ZONE LIST LENGTH, but not to the number of descriptors that fit in the allocated report buffer. A malicious or malfunctioning device can report an oversized length and cause out-of-bounds reads while the descriptors are parsed: BUG: KASAN: slab-out-of-bounds in sd_zbc_report_zones (drivers/scsi/sd_zbc.c:256) Read of size 8 at addr ffff88802c8d2610 by task kworker/u8:2/37 Call Trace: sd_zbc_report_zones (drivers/scsi/sd_zbc.c:256) blk_revalidate_disk_zones (block/blk-zoned.c:2354) sd_zbc_revalidate_zones (drivers/scsi/sd_zbc.c:566) sd_revalidate_disk (drivers/scsi/sd.c:3898) sd_probe (drivers/scsi/sd.c:4101) Cap the descriptor count with min3() over the remaining requested zones, the device-reported count, and max_in_buf derived from buflen. This matches nvme_ns_report_zones() and preserves iteration over reports that require multiple commands. Use shared constants for the REPORT ZONES header and zone descriptor sizes. [mkp: define constants] Fixes: d41003513e61 ("block: rework zone reporting") Cc: stable@vger.kernel.org Reported-by: co+4a4f9998e2b4b490@bugs.sh Signed-off-by: Boyan Liu Link: https://patch.msgid.link/20261028061651.110435-1-yymhvert@gmail.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/scsi/sd_zbc.c | 9 +++++++-- include/scsi/scsi_proto.h | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/scsi/sd_zbc.c b/drivers/scsi/sd_zbc.c index 8cbf171e6fab..06f4e3fcda1d 100644 --- a/drivers/scsi/sd_zbc.c +++ b/drivers/scsi/sd_zbc.c @@ -222,7 +222,7 @@ int sd_zbc_report_zones(struct gendisk *disk, sector_t sector, { struct scsi_disk *sdkp = scsi_disk(disk); sector_t lba = sectors_to_logical(sdkp->device, sector); - unsigned int nr, i; + unsigned int nr, i, max_in_buf; unsigned char *buf; u64 zone_length, start_lba; size_t offset, buflen = 0; @@ -241,13 +241,18 @@ int sd_zbc_report_zones(struct gendisk *disk, sector_t sector, if (!buf) return -ENOMEM; + max_in_buf = (buflen - ZBC_REPORT_ZONES_HEADER_SIZE) / + ZBC_ZONE_DESCRIPTOR_SIZE; + while (zone_idx < nr_zones && lba < sdkp->capacity) { ret = sd_zbc_do_report_zones(sdkp, buf, buflen, lba, true); if (ret) goto out; offset = 0; - nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64); + nr = min3(nr_zones - zone_idx, + get_unaligned_be32(&buf[0]) / ZBC_ZONE_DESCRIPTOR_SIZE, + max_in_buf); if (!nr) break; diff --git a/include/scsi/scsi_proto.h b/include/scsi/scsi_proto.h index 9c7d31aa6bae..0ee0cf3200d0 100644 --- a/include/scsi/scsi_proto.h +++ b/include/scsi/scsi_proto.h @@ -371,6 +371,9 @@ enum zbc_zone_reporting_options { #define ZBC_REPORT_ZONE_PARTIAL 0x80 +#define ZBC_REPORT_ZONES_HEADER_SIZE 64 +#define ZBC_ZONE_DESCRIPTOR_SIZE 64 + /* Zone types of REPORT ZONES zone descriptors */ enum zbc_zone_type { ZBC_ZONE_TYPE_CONV = 0x1, From 48d3b732e5ad3fffe74e033d9a3cf47ebaa314b0 Mon Sep 17 00:00:00 2001 From: Praneeth Balasubramanian Date: Thu, 29 Oct 2026 13:54:56 -0700 Subject: [PATCH 19/21] scsi: target: iscsi: Check for existing sessions only after authentication In the operational-negotiation path (CSG=1), iscsi_target_handle_csg_one() calls iscsi_target_check_for_existing_instances() before iscsi_conn_authenticated(). An initiator can therefore send a first Login PDU with CSG=1, TSIH=0 and no CHAP exchange, carrying the InitiatorName and ISID of an existing session. The target reinstates that session, which tears down the legitimate initiator's connection. Only afterwards does it reject the login with AUTH_FAILED. iscsi_check_for_session_reinstatement() matches on ISID, InitiatorName and SessionType only. The InitiatorName is not secret, and open-iscsi ISIDs are a fixed OUI followed by a small session counter. A remote peer with no credentials can thus repeatedly disconnect a CHAP-authenticated session and keep its I/O failing. The comment above iscsi_target_check_for_existing_instances() already states the intended invariant: NOTE: We check for existing sessions or connections AFTER the initiator has been successfully authenticated in order to protect against faked ISID/TSIH combinations. The security-negotiation path (CSG=0) honours this, calling the check only once authentication completes. Move the CSG=1 call below the authentication check so both paths behave the same. When authentication is not required, iscsi_conn_authenticated() returns true and behaviour is unchanged. Fixes: e48354ce078c ("iscsi-target: Add iSCSI fabric support for target v4.1") Cc: stable@vger.kernel.org Signed-off-by: Praneeth Balasubramanian Link: https://patch.msgid.link/20261029205456.1211532-1-praneethb2002@gmail.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/target/iscsi/iscsi_target_nego.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/target/iscsi/iscsi_target_nego.c b/drivers/target/iscsi/iscsi_target_nego.c index b03ed154ca34..ccd468e26fb9 100644 --- a/drivers/target/iscsi/iscsi_target_nego.c +++ b/drivers/target/iscsi/iscsi_target_nego.c @@ -982,9 +982,6 @@ static int iscsi_target_handle_csg_one(struct iscsit_conn *conn, struct iscsi_lo if (iscsi_target_check_first_request(conn, login) < 0) return -1; - if (iscsi_target_check_for_existing_instances(conn, login) < 0) - return -1; - ret = iscsi_encode_text_output( PHASE_OPERATIONAL|PHASE_DECLARATIVE, SENDER_TARGET, @@ -1008,6 +1005,9 @@ static int iscsi_target_handle_csg_one(struct iscsit_conn *conn, struct iscsi_lo return -1; } + if (iscsi_target_check_for_existing_instances(conn, login) < 0) + return -1; + if (!iscsi_check_negotiated_keys(conn->param_list)) if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) && (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) From 08f08a6d49efc422258edba802faa4fc7677309e Mon Sep 17 00:00:00 2001 From: Ciro Iriarte Date: Sat, 31 Oct 2026 22:21:39 -0300 Subject: [PATCH 20/21] scsi: target: core: Parse APTPL res_type and res_scope as hex core_scsi3_update_aptpl_buf() saves the reservation holder's type and scope to the APTPL metadata file as zero-padded hex ("res_type=%02x", "res_scope=%02x"), but target_pr_res_aptpl_metadata_store() reads them back with "%d" tokens and match_int(), which parse with base 0. A leading zero selects octal, so match_token() parses only the "0" of "res_type=08", the trailing "8" makes the token not match, and the field is ignored as unknown. An Exclusive Access - All Registrants holder (type 0x8) is left with type 0 and the write fails with -EINVAL and Illegal PR type: 0x00 for reservation holder Userspace that restores APTPL state by writing the saved records back to pr/res_aptpl_metadata (rtslib-fb does this when it recreates a storage object) therefore loses every type 8 reservation across a target restart, and with rtslib the storage object creation fails. The other valid types (1, 3, 5, 6, 7) and scope 0 only work because their octal and decimal values coincide. Keep the on-disk format and parse both fields as hex. The valid type and scope values have the same hex and decimal representation, so records written in decimal by hand keep working. Fixes: c66ac9db8d4a ("[SCSI] target: Add LIO target core v4.0.0-rc6") Cc: stable@vger.kernel.org Signed-off-by: Ciro Iriarte Link: https://patch.msgid.link/ea903e6c-09b5-4a11-80e8-6e1387f39ef5@gmail.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/target/target_core_configfs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c index 430fc1eaeca5..385b84bcbfb2 100644 --- a/drivers/target/target_core_configfs.c +++ b/drivers/target/target_core_configfs.c @@ -2126,8 +2126,8 @@ static match_table_t tokens = { {Opt_initiator_sid, "initiator_sid=%s"}, {Opt_sa_res_key, "sa_res_key=%s"}, {Opt_res_holder, "res_holder=%d"}, - {Opt_res_type, "res_type=%d"}, - {Opt_res_scope, "res_scope=%d"}, + {Opt_res_type, "res_type=%x"}, + {Opt_res_scope, "res_scope=%x"}, {Opt_res_all_tg_pt, "res_all_tg_pt=%d"}, {Opt_mapped_lun, "mapped_lun=%u"}, {Opt_target_fabric, "target_fabric=%s"}, @@ -2229,13 +2229,13 @@ static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item, res_holder = arg; break; case Opt_res_type: - ret = match_int(args, &arg); + ret = match_hex(args, &arg); if (ret) goto out; type = (u8)arg; break; case Opt_res_scope: - ret = match_int(args, &arg); + ret = match_hex(args, &arg); if (ret) goto out; break; From e550f2b2b4aab4c5f12db89d04d8978ce4ed7b4e Mon Sep 17 00:00:00 2001 From: "Milan P. Gandhi" Date: Thu, 5 Nov 2026 17:54:01 +0530 Subject: [PATCH 21/21] scsi: mpi3mr: Validate reply frame size bounds Firmware provides reply_frame_size in dwords, which the driver multiplies by 4 to compute facts.reply_sz (u16). If the reported size exceeds U16_MAX / 4, the multiplication overflows u16 before assignment, causing under-allocated DMA reply buffers. Furthermore, several paths assume a minimal reply frame size: - logdata_entry_sz calculations underflow or hit zero when reply_sz <= 20 bytes, leading to zero-size allocations and divide-by-zero crashes. - Unsolicited status descriptors and fixed replies (such as struct mpi3_iounit_control_reply, 64 bytes) require at least 16 dwords. Validate reply_frame_size in mpi3mr_issue_iocfacts() to ensure it is within [16, U16_MAX / 4] dwords before processing facts data. Fixes: 824a156633df ("scsi: mpi3mr: Base driver code") Cc: stable@vger.kernel.org Signed-off-by: Milan P. Gandhi Link: https://patch.msgid.link/20261105122401.266559-1-mgandhi@redhat.com Signed-off-by: Martin K. Petersen (Oracle) --- drivers/scsi/mpi3mr/mpi3mr.h | 1 + drivers/scsi/mpi3mr/mpi3mr_fw.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/drivers/scsi/mpi3mr/mpi3mr.h b/drivers/scsi/mpi3mr/mpi3mr.h index d6e16707fd97..e78383df619c 100644 --- a/drivers/scsi/mpi3mr/mpi3mr.h +++ b/drivers/scsi/mpi3mr/mpi3mr.h @@ -83,6 +83,7 @@ extern atomic64_t event_counter; #define MPI3MR_ADMIN_REPLY_Q_SIZE (8 * MPI3MR_PAGE_SIZE_4K) #define MPI3MR_ADMIN_REQ_FRAME_SZ 128 #define MPI3MR_ADMIN_REPLY_FRAME_SZ 16 +#define MPI3MR_MIN_REPLY_FRAME_SZ_DWORDS 16 /* 64 bytes */ /* Operational queue management definitions */ #define MPI3MR_OP_REQ_Q_QD 512 diff --git a/drivers/scsi/mpi3mr/mpi3mr_fw.c b/drivers/scsi/mpi3mr/mpi3mr_fw.c index f0d3cd398dd0..60a92366e590 100644 --- a/drivers/scsi/mpi3mr/mpi3mr_fw.c +++ b/drivers/scsi/mpi3mr/mpi3mr_fw.c @@ -3261,6 +3261,7 @@ static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc, struct mpi3_ioc_facts_data *facts_data) { struct mpi3_ioc_facts_request iocfacts_req; + u16 reply_frame_size; void *data = NULL; dma_addr_t data_dma; u32 data_len = sizeof(*facts_data); @@ -3318,6 +3319,23 @@ static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc, goto out_unlock; } memcpy(facts_data, (u8 *)data, data_len); + + reply_frame_size = le16_to_cpu(facts_data->reply_frame_size); + /* + * Validate reply_frame_size: + * - Must be at least 16 dwords (64 bytes) to accommodate fixed reply + * structures and avoid underflow/divide-by-zero in logdata sizing. + * - Must not exceed U16_MAX / 4 to prevent integer overflow. + */ + if (reply_frame_size < MPI3MR_MIN_REPLY_FRAME_SZ_DWORDS || + reply_frame_size > (U16_MAX / 4)) { + ioc_err(mrioc, "Invalid reply_frame_size %u (must be %u..%u dwords)\n", + reply_frame_size, MPI3MR_MIN_REPLY_FRAME_SZ_DWORDS, + (unsigned int)(U16_MAX / 4)); + retval = -EINVAL; + goto out_unlock; + } + mpi3mr_process_factsdata(mrioc, facts_data); out_unlock: mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;