zfcp: Use DMA streaming API to map I/O
Subsystems (2)
v12 cover zfcp: Use DMA streaming API to map I/O
In the existing zFCP driver, virtual addresses are translated directly into bus (physical addresses) for DMA operations on all I/O buffers passing from zFCP driver to the device.
Need for using DMA-API in zFCP driver:
In a Secure Execution environment, the host must ensure that device DMA accesses are restricted to non-guest-private, page-sized memory blocks. This prevents I/O failures that can occur when a device attempts to access guest-private memory. Therefore, guest memory pages must be prepared by the hypervisor before they become accessible to an I/O device.
To support this requirement, the host Linux kernel must be reliably informed whenever a memory region becomes eligible for device DMA access and when that access must be revoked. This information must be maintained correctly at least at page size granularity. To satisfy these requirements, all I/O buffers used by the zFCP driver should be mapped through the DMA API.
DMA API Integration in zFCP:
The zFCP driver is updated to use the DMA streaming API's to map all the I/O buffers which will be passed to the device.
All zFCP I/O buffers (QTCB, SRB, CT/ELS and SCSI I/O) are represented as scatter-gather lists and mapped through DMA API using dma_map_sgtable(). Once the DMA transfer is complete, the corresponding mappings are released using dma_unmap_sgtable().
By routing all device-visible memory through the DMA mapping layer, the driver ensures that:
1. DMA access is properly authorized before use.
2. DMA is revoked after use.
3. Secure Execution memory restrictions are honored.
M Nikhil (2):
zfcp: Replace sg-list with sg_table in zfcp_fsf_ct_els structure
zfcp: Use DMA streaming API to map I/O
drivers/s390/scsi/zfcp_aux.c | 16 +++ drivers/s390/scsi/zfcp_dbf.c | 16 +-- drivers/s390/scsi/zfcp_def.h | 4 + drivers/s390/scsi/zfcp_ext.h | 10 +- drivers/s390/scsi/zfcp_fc.c | 36 +++-- drivers/s390/scsi/zfcp_fsf.c | 260 ++++++++++++++++++++++++++++------ drivers/s390/scsi/zfcp_fsf.h | 9 +- drivers/s390/scsi/zfcp_qdio.c | 37 +++-- drivers/s390/scsi/zfcp_qdio.h | 27 ++-- 9 files changed, 321 insertions(+), 94 deletions(-)
Signature · 1 line
2.53.0
PATCH v12 1/2 zfcp: Replace sg-list with sg_table in zfcp_fsf_ct_els structure
The zfcp_fsf_ct_els data structure is updated to replace the sg_req and sg_resp members, which are of type struct scatterlist, with request and response sg_table instances.
These sg_table structure reference the corresponding scatterlists (sg_req and sg_resp) maintained in struct zfcp_fc_req.
These changes allows to track the original and mapped scatterlist nents once zfcp is converted to use DMA-API for mapping the sg lists.
--- drivers/s390/scsi/zfcp_dbf.c | 16 ++++++++-------- drivers/s390/scsi/zfcp_fc.c | 36 ++++++++++++++++++++++++------------ drivers/s390/scsi/zfcp_fsf.c | 6 ++++-- drivers/s390/scsi/zfcp_fsf.h | 9 +++++---- 4 files changed, 41 insertions(+), 26 deletions(-)
drivers/s390/scsi/zfcp_dbf.c +8 -8
@@ -617,9 +617,9 @@ void zfcp_dbf_san_req(char *tag, struct zfcp_fsf_req *fsf, u32 d_id)if (unlikely (!debug_level_enabled (dbf->san,ZFCP_DBF_SAN_LEVEL )))return ; - length = (u16 )zfcp_qdio_real_bytes (ct_els->req); -zfcp_dbf_san (tag, dbf,"san_req" , ct_els->req,ZFCP_DBF_SAN_REQ , - length, fsf->req_id, d_id, length); + length = (u16 )zfcp_qdio_real_bytes (ct_els->sgt_req.sgl); +zfcp_dbf_san (tag, dbf,"san_req" , ct_els->sgt_req.sgl, +ZFCP_DBF_SAN_REQ , length, fsf->req_id, d_id, length); } static u16 zfcp_dbf_san_res_cap_len_if_gpn_ft(char *tag,
@@ -627,9 +627,9 @@ static u16 zfcp_dbf_san_res_cap_len_if_gpn_ft(char *tag, u16 len) {struct zfcp_fsf_ct_els *ct_els = fsf->data; -struct fc_ct_hdr *reqh =sg_virt (ct_els->req); +struct fc_ct_hdr *reqh =sg_virt (ct_els->sgt_req.sgl);struct fc_ns_gid_ft *reqn = (struct fc_ns_gid_ft *)(reqh +1 ); -struct scatterlist *resp_entry = ct_els->resp; +struct scatterlist *resp_entry = ct_els->sgt_resp.sgl;struct fc_ct_hdr *resph;struct fc_gpn_ft_resp *acc;int max_entries, x, last =0 ;
@@ -698,9 +698,9 @@ void zfcp_dbf_san_res(char *tag, struct zfcp_fsf_req *fsf)if (unlikely (!debug_level_enabled (dbf->san,ZFCP_DBF_SAN_LEVEL )))return ; - length = (u16 )zfcp_qdio_real_bytes (ct_els->resp); -zfcp_dbf_san (tag, dbf,"san_res" , ct_els->resp,ZFCP_DBF_SAN_RES , - length, fsf->req_id, ct_els->d_id, + length = (u16 )zfcp_qdio_real_bytes (ct_els->sgt_resp.sgl); +zfcp_dbf_san (tag, dbf,"san_res" , ct_els->sgt_resp.sgl, +ZFCP_DBF_SAN_RES , length, fsf->req_id, ct_els->d_id,zfcp_dbf_san_res_cap_len_if_gpn_ft (tag, fsf, length)); }
drivers/s390/scsi/zfcp_fc.c +24 -12
@@ -393,10 +393,12 @@ static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port, fc_req->ct_els.port = port; fc_req->ct_els.handler = zfcp_fc_complete; fc_req->ct_els.handler_data = &completion; - fc_req->ct_els.req = &fc_req->sg_req; - fc_req->ct_els.resp = &fc_req->sg_rsp; + fc_req->ct_els.sgt_req.sgl = &fc_req->sg_req; + fc_req->ct_els.sgt_resp.sgl = &fc_req->sg_rsp;sg_init_one (&fc_req->sg_req, gid_pn_req,sizeof (*gid_pn_req));sg_init_one (&fc_req->sg_rsp, gid_pn_rsp,sizeof (*gid_pn_rsp)); + fc_req->ct_els.sgt_req.orig_nents =sg_nents (&fc_req->sg_req); + fc_req->ct_els.sgt_resp.orig_nents =sg_nents (&fc_req->sg_rsp);zfcp_fc_ct_ns_init (&gid_pn_req->ct_hdr,FC_NS_GID_PN ,ZFCP_FC_CT_SIZE_PAGE );
@@ -558,12 +560,14 @@ static int zfcp_fc_adisc(struct zfcp_port *port)return -ENOMEM ; fc_req->ct_els.port = port; - fc_req->ct_els.req = &fc_req->sg_req; - fc_req->ct_els.resp = &fc_req->sg_rsp; + fc_req->ct_els.sgt_req.sgl = &fc_req->sg_req; + fc_req->ct_els.sgt_resp.sgl = &fc_req->sg_rsp;sg_init_one (&fc_req->sg_req, &fc_req->u.adisc.req,sizeof (struct fc_els_adisc ));sg_init_one (&fc_req->sg_rsp, &fc_req->u.adisc.rsp,sizeof (struct fc_els_adisc )); + fc_req->ct_els.sgt_req.orig_nents =sg_nents (&fc_req->sg_req); + fc_req->ct_els.sgt_resp.orig_nents =sg_nents (&fc_req->sg_rsp); fc_req->ct_els.handler = zfcp_fc_adisc_handler; fc_req->ct_els.handler_data = fc_req;
@@ -706,8 +710,10 @@ static int zfcp_fc_send_gpn_ft(struct zfcp_fc_req *fc_req, ct_els->handler = zfcp_fc_complete; ct_els->handler_data = &completion; - ct_els->req = &fc_req->sg_req; - ct_els->resp = &fc_req->sg_rsp; + ct_els->sgt_req.sgl = &fc_req->sg_req; + ct_els->sgt_resp.sgl = &fc_req->sg_rsp; + ct_els->sgt_req.orig_nents =sg_nents (&fc_req->sg_req); + ct_els->sgt_resp.orig_nents =sg_nents (&fc_req->sg_rsp); ret =zfcp_fsf_send_ct (&adapter->gs->ds, ct_els,NULL ,ZFCP_FC_CTELS_TMO );
@@ -865,8 +871,10 @@ static int zfcp_fc_gspn(struct zfcp_adapter *adapter, ct_els->handler = zfcp_fc_complete; ct_els->handler_data = &completion; - ct_els->req = &fc_req->sg_req; - ct_els->resp = &fc_req->sg_rsp; + ct_els->sgt_req.sgl = &fc_req->sg_req; + ct_els->sgt_resp.sgl = &fc_req->sg_rsp; + ct_els->sgt_req.orig_nents =sg_nents (&fc_req->sg_req); + ct_els->sgt_resp.orig_nents =sg_nents (&fc_req->sg_rsp); ret =zfcp_fsf_send_ct (&adapter->gs->ds, ct_els,NULL ,ZFCP_FC_CTELS_TMO );
@@ -924,8 +932,10 @@ static void zfcp_fc_rspn(struct zfcp_adapter *adapter, ct_els->handler = zfcp_fc_complete; ct_els->handler_data = &completion; - ct_els->req = &fc_req->sg_req; - ct_els->resp = &fc_req->sg_rsp; + ct_els->sgt_req.sgl = &fc_req->sg_req; + ct_els->sgt_resp.sgl = &fc_req->sg_rsp; + ct_els->sgt_req.orig_nents =sg_nents (&fc_req->sg_req); + ct_els->sgt_resp.orig_nents =sg_nents (&fc_req->sg_rsp); ret =zfcp_fsf_send_ct (&adapter->gs->ds, ct_els,NULL ,ZFCP_FC_CTELS_TMO );
@@ -1090,8 +1100,10 @@ int zfcp_fc_exec_bsg_job(struct bsg_job *job)if (!(atomic_read (&adapter->status) &ZFCP_STATUS_COMMON_OPEN ))return -EINVAL ; - ct_els->req = job->request_payload.sg_list; - ct_els->resp = job->reply_payload.sg_list; + ct_els->sgt_req.sgl = job->request_payload.sg_list; + ct_els->sgt_resp.sgl = job->reply_payload.sg_list; + ct_els->sgt_req.orig_nents =sg_nents (ct_els->sgt_req.sgl); + ct_els->sgt_resp.orig_nents =sg_nents (ct_els->sgt_resp.sgl); ct_els->handler_data = job;switch (bsg_request->msgcode) {
drivers/s390/scsi/zfcp_fsf.c +4 -2
@@ -1242,7 +1242,8 @@ int zfcp_fsf_send_ct(struct zfcp_fc_wka_port *wka_port, } req->status |=ZFCP_STATUS_FSFREQ_CLEANUP ; - ret =zfcp_fsf_setup_ct_els (req, ct->req, ct->resp, timeout); + ret =zfcp_fsf_setup_ct_els (req, ct->sgt_req.sgl, ct->sgt_resp.sgl, + timeout);if (ret) goto failed_send;
@@ -1342,7 +1343,8 @@ int zfcp_fsf_send_els(struct zfcp_adapter *adapter, u32 d_id,if (!zfcp_adapter_multi_buffer_active (adapter))zfcp_qdio_sbal_limit (qdio, &req->qdio_req,2 ); - ret =zfcp_fsf_setup_ct_els (req, els->req, els->resp, timeout); + ret =zfcp_fsf_setup_ct_els (req, els->sgt_req.sgl, els->sgt_resp.sgl, + timeout);if (ret) goto failed_send;
drivers/s390/scsi/zfcp_fsf.h +5 -4
@@ -487,8 +487,9 @@ struct zfcp_blk_drv_data {/** * struct zfcp_fsf_ct_els - zfcp data for ct or els request -* @req: scatter-gather list for request, points to &zfcp_fc_req.sg_req or BSG -* @resp: scatter-gather list for response, points to &zfcp_fc_req.sg_rsp or BSG +* @sgt_req: sg_table for request, its sgl points to &zfcp_fc_req.sg_req or BSG +* @sgt_resp: sg_table for response, its sgl points to &zfcp_fc_req.sg_resp +* or BSG * @handler: handler function (called for response to the request) * @handler_data: data passed to handler function * @port: Optional pointer to port for zfcp internal ELS (only test link ADISC)
@@ -496,8 +497,8 @@ struct zfcp_blk_drv_data {* @d_id: Destination ID of either open WKA port for CT or of D_ID for ELS */ struct zfcp_fsf_ct_els { -struct scatterlist *req; -struct scatterlist *resp; +struct sg_table sgt_req; +struct sg_table sgt_resp;void (*handler)(void *);void *handler_data;struct zfcp_port *port;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v12 1/2] zfcp: Replace sg-list with sg_table in zfcp_fsf_ct_els structure
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
The
zfcp_fsf_ct_elsdata structure is updated to replace thesg_reqandsg_respmembers, which are of type struct scatterlist, with request and responsesg_tableinstances.These
sg_tablestructure reference the corresponding scatterlists (sg_reqandsg_resp) maintained in structzfcp_fc_req.These changes allows to track the original and mapped scatterlist nents once zfcp is converted to use DMA-API for mapping the sg lists.
Signed-off-by M Nikhil <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v12 2/2 zfcp: Use DMA streaming API to map I/O
In reply to M NikhilIn the existing zFCP driver, virtual addresses are translated directly into bus (physical) addresses for DMA operations on all I/O buffers passed from the zFCP driver to the device.
Need for DMA API:
In secure execution environment, the host must ensure that device DMA addresses are restricted to non-guest-private, page-sized memory blocks. This prevents I/O failures that can occur when a device attempts to access guest-private memory. Therefore, guest memory pages must be prepared by the hypervisor before they become accessible to an I/O device.
To support this requirement, the host linux kernel must be reliably informed whenever a memory region becomes eligible for device DMA access and when that access must be revoked. This information must be maintained correctly at least at page level granularity.
To satisfy these requirements, all I/O buffers used by the zFCP driver should be mapped through the DMA API.
DMA streaming API usage in zFCP:
Changes in this patch-series converts all I/O buffer mappings in the zFCP driver to use the DMA streaming API. Specifically, dma_map_sgtable() and dma_unmap_sgtable() are used to map and unmap the scatter-gather lists.
All zFCP I/O buffers (QTCB, SRB, CT/ELS and SCSI I/O) are represented using scatter-gather lists. The sg_table structure, which encapsulates the scatterlist together with the original and mapped entry counts, is used to manage DMA mappings efficiently.
For QTCB and status read buffers, which are not currently scatter-gather based, sg_init_one() is used to initialize a single-entry scatterlist. The resulting scatterlist is then associated with an sg-table by initializing its sgl and orig_nents fields before DMA mapping.
For SCSI I/O, the SCSI midlayer already maintains sg_table instances for both normal and protection enabled SCSI commands. These sg tables are mapped using the same DMA mapping functions.
DMA mapping is performed after the associated I/O buffers have been populated or updated. Consequently, no explicit DMA synchronization for device access before passing the DMA address to the adapter. On the receive path, synchronization for CPU access is handled implicitly by the DMA unmap operations.
--- drivers/s390/scsi/zfcp_aux.c | 16 +++ drivers/s390/scsi/zfcp_def.h | 4 + drivers/s390/scsi/zfcp_ext.h | 10 +- drivers/s390/scsi/zfcp_fsf.c | 260 ++++++++++++++++++++++++++++------ drivers/s390/scsi/zfcp_qdio.c | 37 +++-- drivers/s390/scsi/zfcp_qdio.h | 27 ++-- 6 files changed, 283 insertions(+), 71 deletions(-)
drivers/s390/scsi/zfcp_aux.c +16 -0
@@ -329,6 +329,19 @@ static void zfcp_destroy_adapter_work_queue(struct zfcp_adapter *adapter) } +static int zfcp_set_dma_mask_for_adapter (struct zfcp_adapter *adapter) +{ +int ret; + + ret =dma_set_mask_and_coherent (&adapter->ccw_device->dev, +DMA_BIT_MASK (64 )); +if (ret) +dev_err (&adapter->ccw_device->dev, +"Setting the DMA mask failed for the FCP device.\n" ); + +return ret; +} +/** * zfcp_adapter_enqueue - enqueue a new adapter to the list * @ccw_device: pointer to the struct cc_device
@@ -404,6 +417,9 @@ struct zfcp_adapter *zfcp_adapter_enqueue(struct ccw_device *ccw_device)rwlock_init (&adapter->erp_lock);rwlock_init (&adapter->abort_lock); +if (zfcp_set_dma_mask_for_adapter (adapter)) + goto failed; +if (zfcp_erp_thread_setup (adapter)) goto failed;
drivers/s390/scsi/zfcp_def.h +4 -0
@@ -322,6 +322,8 @@ static inline u64 zfcp_scsi_dev_lun(struct scsi_device *sdev)* @pool: reference to memory pool if used for this request * @issued: time when request was send (STCK) * @handler: handler which should be called to process response +* @metadata_sgt: sg_table for metadata of a QTCB or SRB request +* @metadata_sg: scatterlist for metadata of a QTCB or SRB request */ struct zfcp_fsf_req {struct list_head list;
@@ -337,6 +339,8 @@ struct zfcp_fsf_req {mempool_t *pool;unsigned long long issued;void (*handler)(struct zfcp_fsf_req *); +struct sg_table metadata_sgt; +struct scatterlist metadata_sg; }; static inline
drivers/s390/scsi/zfcp_ext.h +8 -2
@@ -150,6 +150,11 @@ enum zfcp_fsf_print_fmt {extern ssize_t zfcp_fsf_scnprint_fc_security (char *buf,size_t size,u32 fc_security,enum zfcp_fsf_print_fmt fmt); +int zfcp_fsf_req_map_sg (struct zfcp_fsf_req *req,struct sg_table *sgt, +enum dma_data_direction dma_dir); +void zfcp_fsf_req_unmap_sg (struct zfcp_fsf_req *req, +struct sg_table *sgt, +enum dma_data_direction dma_dir);/* zfcp_qdio.c */ extern int zfcp_qdio_setup (struct zfcp_adapter *);
@@ -157,8 +162,9 @@ extern void zfcp_qdio_destroy(struct zfcp_qdio *); extern int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio) __must_hold(qdio->req_q_lock);extern int zfcp_qdio_send (struct zfcp_qdio *,struct zfcp_qdio_req *); -extern int zfcp_qdio_sbals_from_sg (struct zfcp_qdio *,struct zfcp_qdio_req *, -struct scatterlist *); +int zfcp_qdio_sbals_from_sg (struct zfcp_qdio *qdio,struct sg_table *sgt, +struct zfcp_fsf_req *req, +enum dma_data_direction dma_dir);extern void zfcp_qdio_shost_update (struct zfcp_adapter *const adapter,const struct zfcp_qdio *const qdio);extern int zfcp_qdio_open (struct zfcp_qdio *);
drivers/s390/scsi/zfcp_fsf.c +216 -44
510 lines not on this page · raw message
drivers/s390/scsi/zfcp_qdio.c +23 -14
@@ -233,31 +233,40 @@ zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req) }/** -* zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list +* zfcp_qdio_sbals_from_sg - DMA map scatter-gather list and fill SBALs from it * @qdio: pointer to struct zfcp_qdio -* @q_req: pointer to struct zfcp_qdio_req -* @sg: scatter-gather list -* Returns: zero or -EINVAL on error +* @sgt: pointer to scattergather list table +* @req: pointer to zfcp_fsf_req +* @dma_dir: DMA direction of I/O +* Returns: zero on success, -EINVAL or -EIO otherwise */ -int zfcp_qdio_sbals_from_sg (struct zfcp_qdio *qdio,struct zfcp_qdio_req *q_req, -struct scatterlist *sg) +int zfcp_qdio_sbals_from_sg (struct zfcp_qdio *qdio,struct sg_table *sgt, +struct zfcp_fsf_req *req, +enum dma_data_direction dma_dir) {struct qdio_buffer_element *sbale; +int i; +struct scatterlist *sg_element; + +if (zfcp_fsf_req_map_sg (req, sgt, dma_dir)) +return -EIO ;/* set storage-block type for this request */ - sbale =zfcp_qdio_sbale_req (qdio, q_req); - sbale->sflags |= q_req->sbtype; + sbale =zfcp_qdio_sbale_req (qdio, &req->qdio_req); + sbale->sflags |= req->qdio_req.sbtype; -for (; sg; sg =sg_next (sg)) { - sbale =zfcp_qdio_sbale_next (qdio, q_req); +for_each_sgtable_dma_sg (sgt, sg_element, i) { + sbale =zfcp_qdio_sbale_next (qdio, &req->qdio_req);if (!sbale) {atomic_inc (&qdio->req_q_full); -zfcp_qdio_zero_sbals (qdio->req_q, q_req->sbal_first, - q_req->sbal_number); +zfcp_qdio_zero_sbals (qdio->req_q, + req->qdio_req.sbal_first, + req->qdio_req.sbal_number); +zfcp_fsf_req_unmap_sg (req, sgt, dma_dir);return -EINVAL ; } - sbale->addr =u64_to_dma64 (sg_phys (sg)); - sbale->length = sg->length; + sbale->addr =u64_to_dma64 (sg_dma_address (sg_element)); + sbale->length =sg_dma_len (sg_element); }return 0 ; }
drivers/s390/scsi/zfcp_qdio.h +16 -11
@@ -11,6 +11,7 @@#define ZFCP_QDIO_H #include <linux/interrupt.h> +#include <linux/types.h> #include <asm/qdio.h> #define ZFCP_QDIO_SBALE_LEN PAGE_SIZE
@@ -106,16 +107,19 @@ zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)* @q_req: the qdio request to start * @req_id: The request id * @sbtype: type flags to set for all sbals -* @data: First data block -* @len: Length of first data block +* @has_qtcb: FSF request has a QTCB +* @qtcb_len: Length of first data block QTCB, if any +* @qtcb_dma_handle: DMA handle of the QTCB, if any * * This is the start of putting the request into the queue, the last * step is passing the request to zfcp_qdio_send. The request queue * lock must be held during the whole process from init to send. */ static inline -void zfcp_qdio_req_init (struct zfcp_qdio *qdio,struct zfcp_qdio_req *q_req, -u64 req_id,u8 sbtype,void *data,u32 len) +void zfcp_qdio_req_init (struct zfcp_qdio *qdio, +struct zfcp_qdio_req *q_req,u64 req_id, +u8 sbtype,bool has_qtcb,u32 qtcb_len, +dma_addr_t qtcb_dma_handle) {struct qdio_buffer_element *sbale; int count = min(atomic_read(&qdio->req_q_free),
@@ -133,33 +137,34 @@ void zfcp_qdio_req_init(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req, sbale->eflags =0 ; sbale->sflags =SBAL_SFLAGS0_COMMAND | sbtype; -if (unlikely (!data)) +if (unlikely (!has_qtcb))return ; sbale++; - sbale->addr =virt_to_dma64 (data); - sbale->length = len; + sbale->addr =u64_to_dma64 (qtcb_dma_handle); + sbale->length = qtcb_len; }/** * zfcp_qdio_fill_next - Fill next sbale, only for single sbal requests * @qdio: pointer to struct zfcp_qdio * @q_req: pointer to struct zfcp_queue_req -* @data: pointer to data +* @dma_handle: dma address * @len: length of data * * This is only required for single sbal requests, calling it when * wrapping around to the next sbal is a bug. */ static inline -void zfcp_qdio_fill_next (struct zfcp_qdio *qdio,struct zfcp_qdio_req *q_req, -void *data,u32 len) +void zfcp_qdio_fill_next (struct zfcp_qdio *qdio, +struct zfcp_qdio_req *q_req, +dma_addr_t dma_handle,u32 len) {struct qdio_buffer_element *sbale;BUG_ON (q_req->sbale_curr == qdio->max_sbale_per_sbal -1 ); q_req->sbale_curr++; sbale =zfcp_qdio_sbale_curr (qdio, q_req); - sbale->addr =virt_to_dma64 (data); + sbale->addr =u64_to_dma64 (dma_handle); sbale->length = len; }
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [Critical] scsi: NULL pointer dereference in DMA mapping of SCSI scatterlists - [High] scsi: corrupted…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [Critical] scsi: NULL pointer dereference in DMA mapping of SCSI scatterlists
- [High] scsi: corrupted QDIO request queue on DMA mapping failure
- [High] scsi: hardware protocol error due to CPU/DMA segment count mismatch
- [High] scsi: transmission of stale data due to QTCB population after DMA mapping
- [High] scsi: permanent DMA mapping leak on aborted or forgotten SCSI commands --
commit 2b39704cf1d28c9469faf5ad32bab284b1cbd1c2
Author: M Nikhil <[email protected]>
zfcp: Use DMA streaming API to map I/O
This commit converts all I/O buffer mappings in the zFCP driver to use the DMA streaming API. This ensures device DMA addresses are restricted to non-guest-private, page-sized memory blocks, which is necessary for secure execution environments. All I/O buffers are now represented using scatter-gather lists.
Quoted 4 lines
drivers/s390/scsi/zfcp_fsf.c +0
-0
[ ... ]
Quoted 7 lines
drivers/s390/scsi/zfcp_fsf.c +3
-0@@ -84,6 +85,60 @@ static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req) req->status |=ZFCP_STATUS_FSFREQ_ERROR ; } +/** +* zfcp_fsf_req_map_sg - map the scattergather list for DMA transfer +* @req: pointer to zfcp_fsf_req
drivers/s390/scsi/zfcp_fsf.c static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req) · line 100
Quoted 10 lines
drivers/s390/scsi/zfcp_fsf.c +10
-0+* @sgt: pointer to scattergather list table +* @dma_dir: DMA data direction +* Returns: 0 on success, -EIO otherwise +*/ +int zfcp_fsf_req_map_sg (struct zfcp_fsf_req *req,struct sg_table *sgt, +enum dma_data_direction dma_dir) +{ +if (valid_dma_direction (dma_dir) && sgt->orig_nents) { +if (dma_map_sgtable (&req->adapter->ccw_device->dev, sgt, + dma_dir,0 ))
[Severity: Critical]
Will dma_map_sgtable() crash here if the scatterlist has coalesced segments?
In the block/SCSI layer, orig_nents represents the allocated capacity, while the actual number of populated/initialized segments is stored in nents after blk_rq_map_sg() merges segments and marks the final initialized segment with sg_mark_end().
Because dma_map_sgtable() relies on orig_nents to iterate, it will iterate past the sg_mark_end() terminator if the initialized segment count is smaller than the allocated capacity. This causes sg_next() to return NULL, leading to a fatal NULL pointer dereference inside the architecture's DMA mapping implementation.
Quoted 15 lines
drivers/s390/scsi/zfcp_fsf.c +15
-0+return -EIO ; + }else if ((dma_dir ==DMA_NONE ) && sgt->orig_nents) { +return -EIO ; + } + +return 0 ; +} + +/** +* zfcp_fsf_req_unmap_sg - unmap the scattergather list after DMA transfer +* @req: pointer to zfcp_fsf_req +* @sgt: pointer to scattergather list table +* @dma_dir: DMA direction of the sg +*/ +void zfcp_fsf_req_unmap_sg(struct zfcp_fsf_req *req, struct sg_table *sgt,
drivers/s390/scsi/zfcp_fsf.c static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req) · line 125
Quoted 10 lines
drivers/s390/scsi/zfcp_fsf.c +10
-0+ enum dma_data_direction dma_dir) +{ +/* +* We still need the check for mapped sg count here to not perform +* unmap for SCSI commands which have zero payload. +*/ +if (!sgt->nents) +return ; + +dma_unmap_sgtable (&req->adapter->ccw_device->dev, sgt, dma_dir,0 );
[Severity: Critical]
Does the unmap path suffer from the identical issue?
If the initialized segment count is smaller than the allocated capacity, dma_unmap_sgtable() could also iterate past the sg_mark_end() terminator and trigger a NULL pointer dereference.
Quoted 2 lines
drivers/s390/scsi/zfcp_fsf.c +2
-0+} +
[ ... ]
Quoted 5 lines
drivers/s390/scsi/zfcp_fsf.c +0
-1@@ -870,10 +928,27 @@ static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_qdio *qdio, req->qtcb->prefix.qtcb_version =FSF_QTCB_CURRENT_VERSION ; req->qtcb->header.req_handle = req->req_id; req->qtcb->header.fsf_command = fsf_cmd; - }
drivers/s390/scsi/zfcp_fsf.c static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_qdio *qdio, · line 938
Quoted 10 lines
drivers/s390/scsi/zfcp_fsf.c +7
-2-zfcp_qdio_req_init (adapter->qdio, &req->qdio_req, req->req_id, sbtype, - req->qtcb,sizeof (struct fsf_qtcb )); +sg_init_one (&req->metadata_sg, req->qtcb, +sizeof (struct fsf_qtcb )); + req->metadata_sgt.sgl = &req->metadata_sg; + req->metadata_sgt.orig_nents =1 ; + +if (zfcp_fsf_req_map_sg (req, &req->metadata_sgt, +DMA_BIDIRECTIONAL )) {
[Severity: High]
Could this DMA mapping snapshot the QTCB before critical fields are populated?
The metadata SG table is mapped for DMA here. However, callers of zfcp_fsf_req_create() (such as zfcp_fsf_fcp_cmnd) proceed to populate critical fields of the QTCB via normal CPU memory writes after this function returns.
In systems using SWIOTLB (such as s390x Secure Execution environments), the DMA mapping copies the initial state of the buffer into a bounce buffer at mapping time.
Because there is no dma_sync_sgtable_for_device() following the CPU writes, the bounce buffer retains the uninitialized state, and the FCP adapter will process an empty or stale command.
Quoted 3 lines
drivers/s390/scsi/zfcp_fsf.c +3
-0+zfcp_fsf_req_free (req); +return ERR_PTR (-EIO ); + }
[ ... ]
Quoted 6 lines
drivers/s390/scsi/zfcp_fsf.c +1
-1@@ -1150,57 +1263,70 @@ static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,u32 feat = adapter->adapter_features;if (zfcp_adapter_multi_buffer_active (adapter)) { -if (zfcp_qdio_sbals_from_sg (qdio, &req->qdio_req, sg_req)) +if (zfcp_qdio_sbals_from_sg (qdio, sgt_req, req,DMA_TO_DEVICE ))
drivers/s390/scsi/zfcp_fsf.c static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req, · line 1273
Quoted 10 lines
drivers/s390/scsi/zfcp_fsf.c +5
-3return -EIO ; + qtcb->bottom.support.req_buf_length = -zfcp_qdio_real_bytes (sg_req); -if (zfcp_qdio_sbals_from_sg (qdio, &req->qdio_req, sg_resp)) -return -EIO ; +zfcp_qdio_real_bytes (sgt_req->sgl); + +if (zfcp_qdio_sbals_from_sg (qdio, sgt_resp, req,DMA_FROM_DEVICE )) + goto err_unmap;
[Severity: High]
When chaining multiple SG tables, what happens if the second mapping call fails?
The first zfcp_qdio_sbals_from_sg() call populates the SBALs successfully using sgt_req. If the second mapping call for sgt_resp fails, the error path aborts the request without clearing the SBAL elements dirtied by the first call.
These SBALs retain stale descriptors. When the next request reuses these SBAL positions, the QDIO hardware might read the corrupted boundary flags, prematurely terminating scatter-gather list processing and dropping payloads.
drivers/s390/scsi/zfcp_fsf.c static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req, · line 1279
Quoted 8 lines
drivers/s390/scsi/zfcp_fsf.c +4
-2+ qtcb->bottom.support.resp_buf_length = -zfcp_qdio_real_bytes (sg_resp); +zfcp_qdio_real_bytes (sgt_resp->sgl); -zfcp_qdio_set_data_div (qdio, &req->qdio_req,sg_nents (sg_req)); +zfcp_qdio_set_data_div (qdio, &req->qdio_req, +sg_nents (sgt_req->sgl));
[Severity: High]
Is it safe to set the data division using the pre-mapped CPU segment count rather than the DMA-mapped segment count?
The FCP adapter hardware expects the Data Division descriptor to specify the number of hardware SBALEs mapped for the first buffer sequence. The code sets this count using sg_nents(sgt_req->sgl).
However, zfcp_qdio_sbals_from_sg() populates the SBALEs using for_each_sgtable_dma_sg(), which iterates only sgt->nents times (the actual number of coalesced DMA segments).
If the IOMMU coalesces segments, the Data Division count will be larger than the actual number of SBALEs appended. The hardware could consume subsequent, unrelated SBALEs as part of the request payload, causing silent data corruption and hardware protocol violations.
Quoted 4 lines
drivers/s390/scsi/zfcp_fsf.c +0
-0zfcp_qdio_set_sbale_last (qdio, &req->qdio_req);zfcp_qdio_set_scount (qdio, &req->qdio_req);return 0 ; }
[ ... ]
drivers/s390/scsi/zfcp_fsf.c static void zfcp_fsf_fcp_cmnd_handler(struct zfcp_fsf_req *req) · line 2635
Quoted 11 lines
drivers/s390/scsi/zfcp_fsf.c +7
-0@@ -2474,6 +2626,13 @@ static void zfcp_fsf_fcp_cmnd_handler(struct zfcp_fsf_req *req)return ; } +if ((scsi_get_prot_op (scpnt) !=SCSI_PROT_NORMAL ) && +scsi_prot_sg_count (scpnt)) +zfcp_fsf_req_unmap_sg (req, &scpnt->prot_sdb->table, + scpnt->sc_data_direction); + +zfcp_fsf_req_unmap_sg (req, &scpnt->sdb.table, scpnt->sc_data_direction); +
[Severity: High]
Can aborted SCSI commands cause a permanent DMA mapping leak?
When a SCSI command times out, the error handling path (such as zfcp_scsi_eh_abort_handler) detaches the FSF request from the SCSI command by setting req->data = NULL.
When the hardware eventually completes the aborted request, this handler is invoked. Because req->data is NULL, the handler returns early (just above the added unmap lines), completely bypassing the calls to zfcp_fsf_req_unmap_sg() for the SCSI data buffers.
Since the DMA mapping is now fully managed within zfcp, these IOVAs and bounce buffer allocations would be leaked permanently, potentially exhausting the IOMMU translation table under load.
drivers/s390/scsi/zfcp_fsf.c int zfcp_fsf_fcp_cmnd(struct scsi_cmnd *scsi_cmnd) · line 2784
Quoted 13 lines
drivers/s390/scsi/zfcp_fsf.c +4
-2zfcp_fsf_fcp_handler_common (req, scpnt->device);if (unlikely (req->status &ZFCP_STATUS_FSFREQ_ERROR )) {@@ -2619,18 +2778,20 @@ int zfcp_fsf_fcp_cmnd(struct scsi_cmnd *scsi_cmnd) scsi_prot_sg_count(scsi_cmnd)) {zfcp_qdio_set_data_div (qdio, &req->qdio_req,scsi_prot_sg_count (scsi_cmnd)); - retval =zfcp_qdio_sbals_from_sg (qdio, &req->qdio_req, -scsi_prot_sglist (scsi_cmnd)); + retval =zfcp_qdio_sbals_from_sg (qdio, + &scsi_cmnd->prot_sdb->table, + req, + scsi_cmnd->sc_data_direction);
[Severity: High]
Could the same CPU/DMA segment count mismatch occur here?
The code sets the data division using scsi_prot_sg_count() (the unmapped protection segment count), while zfcp_qdio_sbals_from_sg() populates SBALEs using the coalesced DMA segments.
Quoted 2 lines
drivers/s390/scsi/zfcp_fsf.c +0
-0if (retval) goto failed_scsi_cmnd;
[ ... ]
Quoted 21 lines
drivers/s390/scsi/zfcp_qdio.c +5
-6@@ -233,31 +233,40 @@ zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req) }/** -* zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list +* zfcp_qdio_sbals_from_sg - DMA map scatter-gather list and fill SBALs from it * @qdio: pointer to struct zfcp_qdio -* @q_req: pointer to struct zfcp_qdio_req -* @sg: scatter-gather list -* Returns: zero or -EINVAL on error +* @sgt: pointer to scattergather list table +* @req: pointer to zfcp_fsf_req +* @dma_dir: DMA direction of I/O +* Returns: zero on success, -EINVAL or -EIO otherwise */ -int zfcp_qdio_sbals_from_sg (struct zfcp_qdio *qdio,struct zfcp_qdio_req *q_req, -struct scatterlist *sg)
drivers/s390/scsi/zfcp_qdio.c zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req) · line 252
Quoted 10 lines
drivers/s390/scsi/zfcp_qdio.c +8
-0+int zfcp_qdio_sbals_from_sg (struct zfcp_qdio *qdio,struct sg_table *sgt, +struct zfcp_fsf_req *req, +enum dma_data_direction dma_dir) {struct qdio_buffer_element *sbale; +int i; +struct scatterlist *sg_element; + +if (zfcp_fsf_req_map_sg (req, sgt, dma_dir)) +return -EIO ;
[Severity: High]
Can this early return corrupt the QDIO request queue?
If zfcp_fsf_req_map_sg() fails here, the function returns -EIO immediately without calling zfcp_qdio_zero_sbals() to zero the reserved SBALs in the queue.
Quoted 4 lines
drivers/s390/scsi/zfcp_qdio.c +0
-2/* set storage-block type for this request */ - sbale =zfcp_qdio_sbale_req (qdio, q_req); - sbale->sflags |= q_req->sbtype;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2