Linux s390 Architecture development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "M Nikhil" <nikh1092@linux.ibm.com>,
	linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org
Cc: "Heiko Carstens" <hca@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH v15 2/2] zfcp: Use DMA streaming API to map I/O
Date: Mon, 30 Nov 2026 07:26:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-240586@kernel.org> (raw)
In-Reply-To: <20261130065416.64595-3-nikh1092@linux.ibm.com>

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 <nikh1092@linux.ibm.com>

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 *sdev)
>  	return 0;
>  }
>  
> +/**
> + * 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 would
> + * be unmapped twice.
> + */
> +void zfcp_scsi_unmap_cmnd(struct zfcp_adapter *adapter,
> +			  struct scsi_cmnd *scpnt)
> +{
> +	if ((scsi_get_prot_op(scpnt) != 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 dismissal
> + * by ERP and zfcp_scsi_forget_cmnds() skip @scpnt.  If old_req is no longer
> + * 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, that
> + * path has unmapped @scpnt (or is about to) without touching anything else,
> + * 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 = 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 |= 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 = 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 == 0 and leak the DMA mapping).

> @@ -179,12 +232,28 @@ static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
>  	write_lock_irqsave(&adapter->abort_lock, flags);
>  
>  	old_req = 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 = 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 |= 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 = zfcp_reqlist_find(...)
  zfcp_scsi_unmap_cmnd(adapter, scpnt)
                                      zfcp_fsf_reqid_check()
                                        old_req = zfcp_reqlist_find_rm(...)
                                        zfcp_fsf_req_complete(old_req)
                                          zfcp_fsf_fsfstatus_eval(old_req)
                                            old_req->status |= ..._ERROR
  old_req->status |= ..._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 |= 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_cmnd *scpnt)
>  		zfcp_erp_wait(adapter);
>  		ret = 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 = 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_req *old_req, void *data)
>  {
>  	struct zfcp_scsi_req_filter *filter =
>  		(struct zfcp_scsi_req_filter *)data;
> +	struct scsi_cmnd *scpnt;
>  
> -	/* already aborted - prevent side-effects - or not a SCSI command */
> -	if (old_req->data == 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 != FSF_QTCB_FCP_CMND)
> +	    old_req->qtcb->header.fsf_command != FSF_QTCB_FCP_CMND ||
> +	    old_req->handler == zfcp_fsf_fcp_task_mgmt_handler)
>  		return;
>  
>  	/* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
> @@ -252,8 +359,10 @@ static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data)
>  	    old_req->qtcb->header.lun_handle != filter->lun_handle)
>  		return;
>  
> -	zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req);
> -	old_req->data = NULL;
> +	scpnt = old_req->data;
> +	zfcp_dbf_scsi_nullcmnd(scpnt, old_req);
> +	zfcp_scsi_unmap_cmnd(old_req->adapter, scpnt);
> +	old_req->status |= 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 = 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 = 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 = fc_block_scsi_eh(scpnt);
	if (fc_ret)
		ret = fc_ret;
	...
	return ret;
}

Because zfcp_erp_adapter_reopen() is called with clear = 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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130065416.64595-1-nikh1092@linux.ibm.com?part=2

  reply	other threads:[~2026-11-30  7:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  6:54 [PATCH v15 0/2] zfcp: Use DMA streaming API to map I/O M Nikhil
2026-11-30  6:54 ` [PATCH v15 1/2] zfcp: Replace sg-list with sg_table in zfcp_fsf_ct_els structure M Nikhil
2026-11-30  6:54 ` [PATCH v15 2/2] zfcp: Use DMA streaming API to map I/O M Nikhil
2026-11-30  7:26   ` sashiko-bot [this message]
2026-11-30  7:26 ` [PATCH v15 0/2] " sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-11-27 19:07 M Nikhil
2026-11-27 19:07 ` [PATCH v15 2/2] " M Nikhil
2026-11-26 17:28 [PATCH v15 0/2] " M Nikhil
2026-11-26 17:28 ` [PATCH v15 2/2] " M Nikhil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-240586@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=nikh1092@linux.ibm.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox