From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796023575; b=5Bam6Xz1z70hsUqZN/zOLKNtT3THHlwZuWh5yYSeYsOELxXBlzpm+uJYXaTSRHy2PIkQ jDkyEya2ZfBCTDnA0IV8mak4ylrg/+HAewiyVAkweUbjFhzdvEGwgVhW5tIxVBxC3MteT my4EE8AFMC66z2/7rM3nAPxL6BiTFatWNjk5nCSa+nnoDDk5I0nx0Ye9fZunh3ENsD/vS ApP8Vg/aMgfWwMRZLaCc3cdJ7a8sQqGcIQ3Xxby07nB2K41MHsqhyj1Vj3eo9AN/Xew41 jlK3czTTgs4lpER/U2qS3ZR88Bhzf6+3S0K02w63zTQoWRiL1wQU5An1+niK2NXuN7Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796023575; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=CGox8lGCN5lg7rcFolp/tqo0FzX7gnRTBbG1L9ZmQE8=; b=cKM0rcsAbb57iSdcNUZrfDWfvuBRD/owo30GEACbSDZ4IqP52dpJNmsxBdX1yjKDYAJY sh+QktST8z8gyApchAEz/dlRdGbTP3gEk6Pm6WbavbNd8BomcP4qy43lCcFw3MIfuwrcM +SBkhd4joKg9BfNf5CaFPiH55Kg/g7LBEswEjZq2DPVMGDJRhOxHeMsKyv1ofcAn74D4p Jw+I3S6ofepBI2JcL/pnbCuQIytdrLlbuvwVJFACPeJLlq0ev3poucH6xcu21bcICqC6s EWMDw68oN3Qz85WAUuhkItMi1bd0YtwFGqsV7CSKWQUWy+OU9qPVBybjXu5FMTYsSpA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NafF27vI; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA501606A31C; Mon, 30 Nov 2026 07:26:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NafF27vI Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDA101F00898; Mon, 30 Nov 2026 07:26:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796023575; bh=CGox8lGCN5lg7rcFolp/tqo0FzX7gnRTBbG1L9ZmQE8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=NafF27vISsuku85qtxmosE3SesSTaWLqZ6xhNSOuUUDtGiVTXg69/h+BECffWpEYz 1TygfatMuzBGSxuUR3rTZ7gcdfYmy9P7JDYoE8zZ8+66Cl+e2zkw2RuriN5CYurbCK 9TPF1BdWErxJo/HGrU3Ya6fOJKWVaJ++DCr86f06RMJjpGgZhtb3FHG6rF5yGflGDk t6pnY2kBfjcloElhZcSIusdFiFiTexCx3BYUZ8kUsujxHNIVWUchT9UmR6C2KLrtqe huUYPqWKGjhDe5+/PJ0xV7linoAH76UgjDZDVzhLB0hrIZNJKsuAbNG/XNyfnajB7s g3Pum8be8CLCQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v15 2/2] zfcp: Use DMA streaming API to map I/O Reply-To: sashiko-reviews@lists.linux.dev To: "M Nikhil" , linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org Cc: "Heiko Carstens" , "Vasily Gorbik" , "Alexander Gordeev" , "Christian Borntraeger" In-Reply-To: <20261130065416.64595-3-nikh1092@linux.ibm.com> References: <20261130065416.64595-1-nikh1092@linux.ibm.com> <20261130065416.64595-3-nikh1092@linux.ibm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 07:26:14 +0000 X-Mailing-List: linux-s390@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] scsi: zfcp: race between req_list removal and scsi_cmnd DMA unmap - [High] scsi: zfcp: premature DMA unmap and UAF on adapter shutdown in SCSI EH - [High] scsi: zfcp: UAF and DMA leak in zfcp_scsi_eh_host_reset_handler() - [Medium] scsi: zfcp: data race on req->status between abort and request completion - [Medium] scsi: zfcp: stale handle filter in zfcp_scsi_forget_cmnd() skips DMA unmap -- commit 333147fc69f308b7d567f51ba5732bec1331a0de Author: M Nikhil zfcp: Use DMA streaming API to map I/O This commit converts zfcp I/O buffer mappings (QTCB, SRB, CT/ELS, and SCSI I/O) to use the DMA streaming API via dma_map_sgtable() and dma_unmap_sgtable(), and coordinates SCSI command DMA unmapping across completion and SCSI error handling paths. > diff --git a/drivers/s390/scsi/zfcp_scsi.c b/drivers/s390/scsi/zfcp_scsi.c > index ea4b968352f4d..9f57176986d18 100644 > --- a/drivers/s390/scsi/zfcp_scsi.c > +++ b/drivers/s390/scsi/zfcp_scsi.c > @@ -163,6 +163,59 @@ static int zfcp_scsi_sdev_init(struct scsi_device *s= dev) > return 0; > } > =20 > +/** > + * zfcp_scsi_unmap_cmnd() - Unmap the DMA mappings of a SCSI command. > + * @adapter: Adapter whose device the command was mapped for. > + * @scpnt: SCSI command whose data and protection SG tables are unmapped. > + * > + * Caller must guarantee that the hardware no longer uses the buffers and > + * that nobody else unmaps the same command, otherwise the SG tables wou= ld > + * be unmapped twice. > + */ > +void zfcp_scsi_unmap_cmnd(struct zfcp_adapter *adapter, > + struct scsi_cmnd *scpnt) > +{ > + if ((scsi_get_prot_op(scpnt) !=3D SCSI_PROT_NORMAL) && > + scsi_prot_sg_count(scpnt)) > + zfcp_fsf_req_unmap_sg(&adapter->ccw_device->dev, > + &scpnt->prot_sdb->table, > + scpnt->sc_data_direction); > + > + zfcp_fsf_req_unmap_sg(&adapter->ccw_device->dev, > + &scpnt->sdb.table, > + scpnt->sc_data_direction); > +} > + > +/** > + * zfcp_scsi_abort_unmap() - Unmap a command from the abort handler. > + * @adapter: Adapter the aborted command was sent on. > + * @scpnt: The command whose hardware use is known to be over. > + * @old_reqid: Request ID of the original FCP command, taken at handler = entry. > + * > + * Under abort_lock, unmap @scpnt and mark old_req > + * ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED, so that its completion, its dismiss= al > + * by ERP and zfcp_scsi_forget_cmnds() skip @scpnt. If old_req is no lo= nger > + * on the request list, it was completed by the hardware or dismissed by= ERP. > + * Because ZFCP_STATUS_FSFREQ_SCSI_EH_OWNED was set at handler entry, th= at > + * path has unmapped @scpnt (or is about to) without touching anything e= lse, > + * so there is nothing left to do here. > + */ > +static void zfcp_scsi_abort_unmap(struct zfcp_adapter *adapter, > + struct scsi_cmnd *scpnt, u64 old_reqid) > +{ > + struct zfcp_fsf_req *old_req; > + unsigned long flags; > + > + write_lock_irqsave(&adapter->abort_lock, flags); > + old_req =3D zfcp_reqlist_find(adapter->req_list, old_reqid); > + if (old_req && > + !(old_req->status & ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED)) { > + zfcp_scsi_unmap_cmnd(adapter, scpnt); > + old_req->status |=3D ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED; > + } > + write_unlock_irqrestore(&adapter->abort_lock, flags); > +} [Severity: High] Can zfcp_scsi_eh_abort_handler() or zfcp_scsi_task_mgmt_function() return to scsi_eh while zfcp_fsf_fcp_cmnd_handler() is still about to dereference and unmap scpnt? Both zfcp_fsf_reqid_check() and zfcp_fsf_req_dismiss_all() remove old_req from adapter->req_list without holding adapter->abort_lock before zfcp_fsf_req_complete() reaches zfcp_fsf_fcp_cmnd_handler() (and zfcp_fsf_req_dismiss_all() splices the entire list into a local remove_queue upfront). If zfcp_scsi_abort_unmap() or zfcp_scsi_forget_cmnds() runs in that window, it does not find old_req in adapter->req_list and returns to scsi_eh before CPU0 has unmapped scpnt: CPU0 CPU1 zfcp_fsf_reqid_check() old_req =3D zfcp_reqlist_find_rm(...) zfcp_fsf_req_complete(old_req) zfcp_scsi_eh_abort_handler(scpnt) ... zfcp_scsi_abort_unmap(...) write_lock(&adapter->abort_lock) zfcp_reqlist_find(...) -> NULL write_unlock(&adapter->abort_lock) return SUCCESS scsi_finish_command(scpnt) scsi_free_sgtables(scpnt) zfcp_fsf_fcp_cmnd_handler(old_req) read_lock(&adapter->abort_lock) zfcp_scsi_unmap_cmnd(..., scpnt) <-- use-after-free Once the EH callback returns, scsi_eh can free scpnt's SG tables via scsi_finish_command(), requeue scpnt, or zero scpnt->sdb in scsi_eh_prep_cmnd() for a TEST UNIT READY (which makes CPU0's later zfcp_fsf_req_unmap_sg() see sgt->nents =3D=3D 0 and leak the DMA mapping). > @@ -179,12 +232,28 @@ static int zfcp_scsi_eh_abort_handler(struct scsi_c= mnd *scpnt) > write_lock_irqsave(&adapter->abort_lock, flags); > =20 > old_req =3D zfcp_reqlist_find(adapter->req_list, old_reqid); > - if (!old_req) { > + if (!old_req || > + (old_req->status & (ZFCP_STATUS_FSFREQ_SCSI_EH_OWNED | > + ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED))) { > write_unlock_irqrestore(&adapter->abort_lock, flags); > zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL); > return FAILED; /* completion could be in progress */ > } > - old_req->data =3D NULL; > + > + /* > + * Hand scpnt over to scsi_eh: the SCSI_EH_OWNED flag makes the > + * completion handler stop at the DMA unmap and never touch scpnt > + * otherwise. The unmap is deferred until the hardware has stopped > + * using the buffers, so that dma_unmap_sgtable() is never called > + * while the FCP channel may still be writing into the pages. > + * > + * The flag stays set on all return paths, as scsi_eh may finish or > + * requeue scpnt at any time later, including after device, target or > + * host reset. Whoever sees the hardware done with the buffers first > + * unmaps exactly once: this handler (zfcp_scsi_abort_unmap()), > + * zfcp_scsi_forget_cmnds() or the completion/dismissal of old_req. > + */ > + old_req->status |=3D ZFCP_STATUS_FSFREQ_SCSI_EH_OWNED; [Severity: Medium] Can non-atomic updates to old_req->status race between the abort paths and request completion or dismissal? zfcp_reqlist_find() releases req_list->lock before returning old_req, so while zfcp_scsi_eh_abort_handler() or zfcp_scsi_abort_unmap() holds adapter->abort_lock, another CPU can remove old_req from req_list and modify old_req->status without adapter->abort_lock: CPU0 CPU1 zfcp_scsi_abort_unmap() write_lock(&adapter->abort_lock) old_req =3D zfcp_reqlist_find(...) zfcp_scsi_unmap_cmnd(adapter, scpnt) zfcp_fsf_reqid_check() old_req =3D zfcp_reqlist_find_rm(..= .) zfcp_fsf_req_complete(old_req) zfcp_fsf_fsfstatus_eval(old_req) old_req->status |=3D ..._ERROR old_req->status |=3D ..._SCSI_UNMAPPED <-- torn RMW on old_req->status write_unlock(&adapter->abort_lock) zfcp_fsf_fcp_cmnd_handler(old_req) zfcp_fsf_req_dismiss_all(), zfcp_fsf_protstatus_eval(), and zfcp_fsf_fsfstatus_eval() all perform non-atomic |=3D on req->status before zfcp_fsf_fcp_cmnd_handler() takes adapter->abort_lock. If a torn RMW drops ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED or ZFCP_STATUS_FSFREQ_SCSI_EH_OWNED, zfcp_fsf_fcp_cmnd_handler() will unmap scpnt a second time or call scsi_done() on a command owned by scsi_eh. > @@ -198,16 +267,34 @@ static int zfcp_scsi_eh_abort_handler(struct scsi_c= mnd *scpnt) > zfcp_erp_wait(adapter); > ret =3D fc_block_scsi_eh(scpnt); > if (ret) { > + /* > + * fast_io_fail_tmo fired. This may come from a > + * per-port reopen that closes the port without > + * shutting QDIO down or dismissing requests, so the > + * hardware may still use the buffers. Only unmap if > + * QDIO is down; otherwise escalate and leave the > + * unmap to a later completion, dismissal or reset. > + */ > zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL); > - return ret; > + if (atomic_read(&adapter->status) & > + ZFCP_STATUS_ADAPTER_QDIOUP) > + return FAILED; > + retval =3D ret; /* FAST_IO_FAIL, QDIO is down */ > + goto out_unmap; > } > if (!(atomic_read(&adapter->status) & > ZFCP_STATUS_COMMON_RUNNING)) { > + /* > + * Adapter is gone; ERP shut down QDIO and dismissed > + * all requests before clearing RUNNING. Safe to > + * unmap now. > + */ > zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL); > - return SUCCESS; > + goto out_unmap; > } > } [Severity: High] Can checking ZFCP_STATUS_COMMON_RUNNING and ZFCP_STATUS_ADAPTER_QDIOUP here and in zfcp_scsi_task_mgmt_function() unmap buffers while DMA is still active, or leave commands mapped when reset returns SUCCESS? In zfcp_erp_adapter_shutdown() -> zfcp_erp_adapter_reopen(), zfcp_erp_adapter_block() clears ZFCP_STATUS_COMMON_RUNNING synchronously before enqueueing the ERP action that later closes QDIO and dismisses requests on the ERP thread: drivers/s390/scsi/zfcp_erp.c:_zfcp_erp_adapter_reopen() { zfcp_erp_adapter_block(adapter, clear); if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) { zfcp_erp_adapter_failed(adapter, "erareo1"); return -EIO; } return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter, NULL, NULL, dbftag, 0); } If zfcp_erp_adapter_shutdown() runs after zfcp_erp_wait() returns (or between zfcp_erp_adapter_block() and zfcp_erp_action_enqueue()), the !ZFCP_STATUS_COMMON_RUNNING branch in zfcp_scsi_eh_abort_handler() jumps to out_unmap and unmaps scpnt while QDIO is still up and the FCP channel may still be doing DMA. In zfcp_scsi_task_mgmt_function(), the matching !ZFCP_STATUS_COMMON_RUNNING check returns SUCCESS without checking QDIOUP or calling zfcp_scsi_forget_cmnds(), so scsi_eh frees or reuses scpnt while old_req is still on adapter->req_list without ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED, causing a use-after-free in zfcp_fsf_fcp_cmnd_handler() when old_req is later dismissed. Also, zfcp_qdio_close() clears ZFCP_STATUS_ADAPTER_QDIOUP before calling qdio_shutdown(), so the lockless QDIOUP checks on the FAST_IO_FAIL paths can observe QDIOUP clear and unmap buffers while qdio_shutdown() is still terminating active DMA. [ ... ] > @@ -237,11 +338,17 @@ static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_r= eq *old_req, void *data) > { > struct zfcp_scsi_req_filter *filter =3D > (struct zfcp_scsi_req_filter *)data; > + struct scsi_cmnd *scpnt; > =20 > - /* already aborted - prevent side-effects - or not a SCSI command */ > - if (old_req->data =3D=3D NULL || > + /* > + * Already unmapped - prevent side-effects - or not a SCSI I/O command. > + * Commands owned by scsi_eh (SCSI_EH_OWNED) are still unmapped here, > + * as the hardware is done with them after a successful reset. > + */ > + if (old_req->status & ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED || > zfcp_fsf_req_is_status_read_buffer(old_req) || > - old_req->qtcb->header.fsf_command !=3D FSF_QTCB_FCP_CMND) > + old_req->qtcb->header.fsf_command !=3D FSF_QTCB_FCP_CMND || > + old_req->handler =3D=3D zfcp_fsf_fcp_task_mgmt_handler) > return; > =20 > /* (tmf_scope =3D=3D FCP_TMF_TGT_RESET || tmf_scope =3D=3D FCP_TMF_LUN_= RESET) */ > @@ -252,8 +359,10 @@ static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_re= q *old_req, void *data) > old_req->qtcb->header.lun_handle !=3D filter->lun_handle) > return; > =20 > - zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req); > - old_req->data =3D NULL; > + scpnt =3D old_req->data; > + zfcp_dbf_scsi_nullcmnd(scpnt, old_req); > + zfcp_scsi_unmap_cmnd(old_req->adapter, scpnt); > + old_req->status |=3D ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED; > } [Severity: Medium] Can zfcp_scsi_forget_cmnd() miss commands if a port or LUN reopen updated port->handle or zfcp_sdev->lun_handle before the TMF ran? Previously, zfcp_scsi_eh_abort_handler() cleared old_req->data =3D NULL on entry even if the abort later failed. Now, when an abort fails (for example with FSF_LUN_BOXED or FSF_PORT_BOXED, which triggers a LUN or port reopen), old_req->data still points to scpnt without ZFCP_STATUS_FSFREQ_SCSI_UNMAPPED, and zfcp_scsi_task_mgmt_function() waits in zfcp_erp_wait() for that reopen to finish before sending the TMF. A port or LUN reopen does not dismiss requests from adapter->req_list, and can assign a new port->handle or zfcp_sdev->lun_handle. Because zfcp_scsi_forget_cmnds() filters by the current handles in zsdev while old_req->qtcb->header still holds the old handles, zfcp_scsi_forget_cmnd() will skip old_req, leaving scpnt mapped when reset returns SUCCESS and causing zfcp_fsf_fcp_cmnd_handler() to dereference scpnt after scsi_eh has finished or requeued it. Matching against scpnt->device (and sdev_to_zfcp(scpnt->device)->port) instead of the QTCB handles would avoid this. > @@ -305,6 +414,21 @@ static int zfcp_scsi_task_mgmt_function(struct scsi_= device *sdev, u8 tm_flags) > ret =3D fc_block_rport(rport); > if (ret) { > zfcp_dbf_scsi_devreset("fiof", sdev, tm_flags, NULL); > + /* > + * QDIO still up: the FCP channel may still be using > + * the buffers of outstanding commands. Do not let > + * scsi_eh finish them; escalate to host reset instead. > + */ > + if (atomic_read(&adapter->status) & > + ZFCP_STATUS_ADAPTER_QDIOUP) > + return FAILED; > + /* > + * QDIO down: the hardware is done with the buffers. > + * Detach and unmap the commands so that a later > + * completion or dismissal of their requests cannot > + * touch a finished scpnt. > + */ > + zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags); > return ret; > } [Severity: High] When abort and device/target reset return FAILED here because fast_io_fail fired while ZFCP_STATUS_ADAPTER_QDIOUP is set, does escalating to zfcp_scsi_eh_host_reset_handler() actually shut down QDIO and unmap the commands? On a local link-down event (zfcp_fsf_link_down_info_eval()), zfcp sets ZFCP_STATUS_COMMON_ERP_FAILED on adapter->status while leaving ZFCP_STATUS_ADAPTER_QDIOUP set and outstanding requests on adapter->req_list. When fast_io_fail_tmo fires and scsi_eh escalates to zfcp_scsi_eh_host_reset_handler(), it calls: drivers/s390/scsi/zfcp_scsi.c:zfcp_scsi_eh_host_reset_handler() { ... zfcp_erp_adapter_reopen(adapter, 0, "schrh_1"); zfcp_erp_wait(adapter); fc_ret =3D fc_block_scsi_eh(scpnt); if (fc_ret) ret =3D fc_ret; ... return ret; } Because zfcp_erp_adapter_reopen() is called with clear =3D 0 while ZFCP_STATUS_COMMON_ERP_FAILED is set on adapter->status, zfcp_erp_handle_failed() suppresses the adapter reopen, so QDIO is not closed and zfcp_fsf_req_dismiss_all() never runs. zfcp_scsi_eh_host_reset_handler() then returns FAST_IO_FAIL (or SUCCESS) to scsi_eh, which finishes and frees the commands while their DMA mappings are still active; when the link later recovers or the adapter shuts down, zfcp_fsf_req_dismiss_all() -> zfcp_fsf_fcp_cmnd_handler() dereferences the freed scpnt to unmap it. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130065416.6459= 5-1-nikh1092@linux.ibm.com?part=3D2