scsi: mpi3mr: Fix issues reported by Sashiko
Subsystems (2)
v2 cover scsi: mpi3mr: Fix issues reported by Sashiko
Fixes for issues in the mpi3mr driver reported by Sashiko.
Changes since v1:
- Patch 14: Fixed the block_io count left raised when the last LUN is removed during the command, and the use after free of the SCSI target. Both were reported by Sashiko on v1.
- Patch 15: Reworked on top of patch 14 and documented the new member, as reported by Sashiko on v1.
- Patch 16: Documented the new lock, as reported by Sashiko on v1.
- Dropped patch 17 ("scsi: mpi3mr: Reject a too small reply size when enabling log data"). It is not needed because
commit e550f2b2b4aa ("scsi: mpi3mr: Validate reply frame size bounds") already rejects a reply frame smaller than 64 bytes.
Ranjan Kumar (20):
scsi: mpi3mr: Fix out-of-bounds read of the BSG request
scsi: mpi3mr: Fix integer wrap in BSG buffer entry lengths
scsi: mpi3mr: Remove redundant BSG buffer length checks
scsi: mpi3mr: Fix PRP list leak in BSG passthrough
scsi: mpi3mr: Fix reposting of invalid reply address on the admin
queue
scsi: mpi3mr: Fix endianness of device handles in BSG requests
scsi: mpi3mr: Fix integer overflow in the HDB upload bounds check
scsi: mpi3mr: Fix integer overflow in the log data size calculation
scsi: mpi3mr: Queue DEVICE_INFO_CHANGED events to the bottom half
again
scsi: mpi3mr: Fix target device reference leaks
scsi: mpi3mr: Fix the 32 bit shift when building the phy mask
scsi: mpi3mr: Fix empty device list race in the all targets query
scsi: mpi3mr: Fix the lowest phy number for phys 32 and above
scsi: mpi3mr: Fix target private data use after free in BSG
passthrough
scsi: mpi3mr: Fix block_io accounting when a target is re-created
during BSG
scsi: mpi3mr: Fix sense buffer use after free on BSG command
completion
scsi: mpi3mr: Fix partly written and lost entries in the log data
buffer
scsi: mpi3mr: Fix byte order handling in the NVMe passthrough commands
scsi: mpi3mr: Fix byte order of the BSG status reply
scsi: mpi3mr: Use the correct conversion for the diag buffer post
request
drivers/scsi/mpi3mr/mpi3mr.h | 12 ++ drivers/scsi/mpi3mr/mpi3mr_app.c | 246 +++++++++++++++++-------- drivers/scsi/mpi3mr/mpi3mr_fw.c | 20 +- drivers/scsi/mpi3mr/mpi3mr_os.c | 82 ++++++++- drivers/scsi/mpi3mr/mpi3mr_transport.c | 34 ++-- 5 files changed, 302 insertions(+), 92 deletions(-)
Signature · 1 line
2.47.3
PATCH v2 1/20 scsi: mpi3mr: Fix out-of-bounds read of the BSG request
The driver reads fields from a BSG request without checking its length. A short request, or one that declares more buffer entries than it contains, makes the driver read past the end of the buffer.
Return -EINVAL when the request is too short for the command or for the number of buffer entries it declares.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
drivers/scsi/mpi3mr/mpi3mr_app.c +26 -0
@@ -1690,6 +1690,13 @@ static long mpi3mr_bsg_process_drv_cmds(struct bsg_job *job)struct mpi3mr_bsg_packet *bsg_req =NULL ;struct mpi3mr_bsg_drv_cmd *drvrcmd =NULL ; +if (job->request_len < + offsetofend(struct mpi3mr_bsg_packet, cmd.drvrcmd.opcode)) { +pr_err ("%s: invalid BSG driver command length(%u)\n" , +MPI3MR_DRIVER_NAME , job->request_len); +return -EINVAL ; + } + bsg_req = job->request; drvrcmd = &bsg_req->cmd.drvrcmd;
@@ -2416,10 +2423,22 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)u8 *din_buf =NULL , *dout_buf =NULL ;u8 *sgl_iter =NULL , *sgl_din_iter =NULL , *sgl_dout_iter =NULL ;u16 rmc_size =0 , desc_count =0 ; +size_t req_min; bsg_req = job->request; karg = (struct mpi3mr_bsg_mptcmd *)&bsg_req->cmd.mptcmd; + req_min = offsetof(struct mpi3mr_bsg_packet , + cmd.mptcmd.buf_entry_list.buf_entry); +if (job->request_len >= req_min) + req_min += karg->buf_entry_list.num_of_entries * +sizeof (struct mpi3mr_buf_entry ); +if (job->request_len < req_min) { +pr_err ("%s: invalid BSG MPT command length(%u), need %zu\n" , +MPI3MR_DRIVER_NAME , job->request_len, req_min); +return -EINVAL ; + } + mrioc =mpi3mr_bsg_verify_adapter (karg->mrioc_id);if (!mrioc)return -ENODEV ;
@@ -2995,6 +3014,12 @@ static int mpi3mr_bsg_request(struct bsg_job *job)struct mpi3mr_bsg_packet *bsg_req = job->request; + if (job->request_len < offsetofend(struct mpi3mr_bsg_packet, cmd_type)) { +pr_err ("%s: invalid BSG request length(%u)\n" , +MPI3MR_DRIVER_NAME , job->request_len); + goto out; + } +switch (bsg_req->cmd_type) {case MPI3MR_DRV_CMD : rval =mpi3mr_bsg_process_drv_cmds (job);
@@ -3008,6 +3033,7 @@ static int mpi3mr_bsg_request(struct bsg_job *job)break ; } +out:bsg_job_done (job, rval, reply_payload_rcv_len);return 0 ;
Signature · 1 line
2.47.3
PATCH v2 2/20 scsi: mpi3mr: Fix integer wrap in BSG buffer entry lengths
In reply to Ranjan KumarThe length of each BSG buffer entry is added to a pointer and the result is compared with the end of the payload. On a 32 bit system a large length makes the sum wrap around and pass the check, so the driver goes on to use a buffer much larger than the payload.
Compare each entry length with the number of bytes left in the payload before the pointer is moved.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 35 +++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +34 -1
@@ -2379,6 +2379,26 @@ static int mpi3mr_map_data_buffer_dma(struct mpi3mr_ioc *mrioc, drv_buf->num_dma_desc = needed_desc;return 0 ; } + +/** +* mpi3mr_bsg_is_dout_be - Check if a buffer entry uses the data out buffer +* @buf_type: Buffer entry type +* +* Return: true if the entry is taken from the data out buffer and +* false if it is taken from the data in buffer. +*/ +static bool mpi3mr_bsg_is_dout_be (u8 buf_type) +{ +switch (buf_type) { +case MPI3MR_BSG_BUFTYPE_RAIDMGMT_CMD : +case MPI3MR_BSG_BUFTYPE_DATA_OUT : +case MPI3MR_BSG_BUFTYPE_MPI_REQUEST : +return true; +default : +return false; + } +} +/** * mpi3mr_bsg_process_mpt_cmds - MPI Pass through BSG handler * @job: BSG job reference
@@ -2423,7 +2443,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)u8 *din_buf =NULL , *dout_buf =NULL ;u8 *sgl_iter =NULL , *sgl_din_iter =NULL , *sgl_dout_iter =NULL ;u16 rmc_size =0 , desc_count =0 ; -size_t req_min; +size_t req_min, buf_left; bsg_req = job->request; karg = (struct mpi3mr_bsg_mptcmd *)&bsg_req->cmd.mptcmd;
@@ -2503,6 +2523,19 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) drv_buf_iter = drv_bufs;for (count =0 ; count < bufcnt; count++, buf_entries++, drv_buf_iter++) { +if (mpi3mr_bsg_is_dout_be (buf_entries->buf_type)) + buf_left = job->request_payload.payload_len - + (sgl_dout_iter - dout_buf); +else + buf_left = job->reply_payload.payload_len - + (sgl_din_iter - din_buf); +if (buf_entries->buf_len > buf_left) { +dprint_bsg_err (mrioc,"%s: buffer entry %u exceeds the payload\n" , + __func__, count); +mutex_unlock (&mrioc->bsg_cmds.mutex); + rval = -EINVAL ; + goto out; + }switch (buf_entries->buf_type) {case MPI3MR_BSG_BUFTYPE_RAIDMGMT_CMD :
Signature · 1 line
2.47.3
PATCH v2 3/20 scsi: mpi3mr: Remove redundant BSG buffer length checks
In reply to Ranjan KumarBuffer entry lengths are now checked before the pointer moves, which makes the later pointer comparisons dead code.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 23 ----------------------- 1 file changed, 23 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +0 -23
@@ -2601,14 +2601,6 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) rval = -EINVAL ; goto out; } -if (sgl_iter + mpi_msg_size > - dout_buf + job->request_payload.payload_len) { -dprint_bsg_err (mrioc,"%s: MPI request buf exceeds dout_buf\n" , - __func__); -mutex_unlock (&mrioc->bsg_cmds.mutex); - rval = -EINVAL ; - goto out; - }memcpy (mpi_req, sgl_iter, mpi_msg_size);break ;default :
@@ -2623,21 +2615,6 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) goto out; } -if (sgl_dout_iter > (dout_buf + job->request_payload.payload_len)) { -dprint_bsg_err (mrioc,"%s: data_out buffer length mismatch\n" , - __func__); -mutex_unlock (&mrioc->bsg_cmds.mutex); - rval = -EINVAL ; - goto out; - } -if (sgl_din_iter > (din_buf + job->reply_payload.payload_len)) { -dprint_bsg_err (mrioc,"%s: data_in buffer length mismatch\n" , - __func__); -mutex_unlock (&mrioc->bsg_cmds.mutex); - rval = -EINVAL ; - goto out; - } - drv_buf_iter->bsg_buf = sgl_iter; drv_buf_iter->bsg_buf_len = buf_entries->buf_len; }
Signature · 1 line
2.47.3
PATCH v2 4/20 scsi: mpi3mr: Fix PRP list leak in BSG passthrough
In reply to Ranjan KumarThe PRP list built for an NVMe passthrough request is freed only after the command completes. If posting the request fails or the command times out, the list is never freed. The next request then overwrites the pointer and the old buffer is lost.
Free the PRP list on every exit from the request handler.
If a timed out command is still pending after recovery, the controller may still use its list. Accept a small leak in that case instead of freeing memory the controller may still read.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +18 -6
@@ -2444,6 +2444,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)u8 *sgl_iter =NULL , *sgl_din_iter =NULL , *sgl_dout_iter =NULL ;u16 rmc_size =0 , desc_count =0 ;size_t req_min, buf_left; +bool prp_owned = false, timed_out = false; bsg_req = job->request; karg = (struct mpi3mr_bsg_mptcmd *)&bsg_req->cmd.mptcmd;
@@ -2748,6 +2749,8 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)mutex_unlock (&mrioc->bsg_cmds.mutex); goto out; } +if (mrioc->prp_list_virt) + prp_owned = true; }else if (nvme_fmt ==MPI3MR_NVME_DATA_FORMAT_SGL1 || nvme_fmt ==MPI3MR_NVME_DATA_FORMAT_SGL2 ) { if (mpi3mr_build_nvme_sgl(mrioc,
@@ -2833,6 +2836,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)if (block_io && stgt_priv)atomic_dec (&stgt_priv->block_io);if (!(mrioc->bsg_cmds.state &MPI3MR_CMD_COMPLETE )) { + timed_out = true; mrioc->bsg_cmds.is_waiting =0 ; rval = -EAGAIN ;if (mrioc->bsg_cmds.state &MPI3MR_CMD_RESET )
@@ -2872,12 +2876,6 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) }dprint_bsg_info (mrioc,"%s: bsg request is completed\n" , __func__); -if (mrioc->prp_list_virt) { -dma_free_coherent (&mrioc->pdev->dev, mrioc->prp_sz, - mrioc->prp_list_virt, mrioc->prp_list_dma); - mrioc->prp_list_virt =NULL ; - } -if ((mrioc->bsg_cmds.ioc_status &MPI3_IOCSTATUS_STATUS_MASK ) !=MPI3_IOCSTATUS_SUCCESS ) { dprint_bsg_info(mrioc,
@@ -2946,6 +2944,20 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) } out_unlock: +if (prp_owned) { +/* +* Do not free the PRP list if a timed out command may still be +* using it. Leaking it is safer than letting the controller read +* freed memory. +*/ +if (!timed_out || + (mrioc->bsg_cmds.state &MPI3MR_CMD_COMPLETE ) || + ((mrioc->bsg_cmds.state &MPI3MR_CMD_RESET ) && + !mrioc->unrecoverable)) +dma_free_coherent (&mrioc->pdev->dev, mrioc->prp_sz, + mrioc->prp_list_virt, mrioc->prp_list_dma); + mrioc->prp_list_virt =NULL ; + }if (din_buf) { job->reply_payload_rcv_len = sg_copy_from_buffer(job->reply_payload.sg_list,
Signature · 1 line
2.47.3
PATCH v2 5/20 scsi: mpi3mr: Fix reposting of invalid reply address on the admin queue
In reply to Ranjan KumarAn admin reply whose reply frame address is not one of the driver's reply buffers is still reposted to the hardware reply free queue. Clear the address when the lookup fails so it is not reposted, as the operational reply queues already do.
--- drivers/scsi/mpi3mr/mpi3mr_fw.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/scsi/mpi3mr/mpi3mr_fw.c +8 -1
@@ -402,8 +402,15 @@ static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc, addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc; *reply_dma =le64_to_cpu (addr_desc->reply_frame_address); def_reply =mpi3mr_get_reply_virt_addr (mrioc, *reply_dma); -if (!def_reply) +if (!def_reply) { +ioc_err (mrioc,"def_reply is NULL, invalid reply_frame_address\n" ); +/* +* Do not let the caller repost an address that +* failed virt-addr lookup back to the hardware. +*/ + *reply_dma =0 ; goto out; + } host_tag =le16_to_cpu (def_reply->host_tag); ioc_status =le16_to_cpu (def_reply->ioc_status); if (ioc_status &
Signature · 1 line
2.47.3
PATCH v2 6/20 scsi: mpi3mr: Fix endianness of device handles in BSG requests
In reply to Ranjan KumarThe device handle of a task management request, an NVMe encapsulated request and the target reset sent after a timeout is read from a little endian field and used as a CPU order value. On a big endian system this gives the wrong device handle.
Convert the handle to CPU byte order before it is used.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +5 -4
@@ -2082,7 +2082,7 @@ static int mpi3mr_build_nvme_prp(struct mpi3mr_ioc *mrioc, mrioc->facts.sge_mod_shift) << 32);u64 sgemod_val = ((u64)(mrioc->facts.sge_mod_value) << mrioc->facts.sge_mod_shift) <<32 ; -u16 dev_handle = nvme_encap_request->dev_handle; +u16 dev_handle =le16_to_cpu (nvme_encap_request->dev_handle);struct mpi3mr_tgt_dev *tgtdev;u16 desc_count =0 ;
@@ -2782,7 +2782,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) tm_req = (struct mpi3_scsi_task_mgmt_request *)mpi_req;if (tm_req->task_type !=MPI3_SCSITASKMGMT_TASKTYPE_ABORT_TASK ) { - dev_handle = tm_req->dev_handle; + dev_handle =le16_to_cpu (tm_req->dev_handle); block_io =1 ; } }
@@ -2861,10 +2861,11 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) (mpi_header->function == MPI3_BSG_FUNCTION_SCSI_IO)) {dprint_bsg_err (mrioc,"%s: bsg request timedout after %d seconds,\n" "issuing target reset to (0x%04x)\n" , __func__, - karg->timeout, mpi_header->function_dependent); + karg->timeout, +le16_to_cpu (mpi_header->function_dependent));mpi3mr_issue_tm (mrioc,MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET , - mpi_header->function_dependent,0 , +le16_to_cpu (mpi_header->function_dependent),0 ,MPI3MR_HOSTTAG_BLK_TMS ,MPI3MR_RESETTM_TIMEOUT , &mrioc->host_tm_cmds, &resp_code,NULL ); }
Signature · 1 line
2.47.3
PATCH v2 7/20 scsi: mpi3mr: Fix integer overflow in the HDB upload bounds check
In reply to Ranjan KumarThe start offset and the length of an HDB upload request are added in 32 bits and compared with the buffer size. A large offset makes the sum wrap around and pass the check, so the copy reads far outside the buffer.
Reject the request if the sum overflows.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +3 -1
@@ -1117,6 +1117,7 @@ static long mpi3mr_bsg_upload_hdb(struct mpi3mr_ioc *mrioc,struct diag_buffer_desc *diag_buffer;uint32_t data_out_size;uint32_t data_in_size; +u32 end; data_out_size = job->request_payload.payload_len; data_in_size = job->reply_payload.payload_len;
@@ -1151,7 +1152,8 @@ static long mpi3mr_bsg_upload_hdb(struct mpi3mr_ioc *mrioc,return -EINVAL ; } -if ((upload_hdb.start_offset + upload_hdb.length) > diag_buffer->size) { +if (check_add_overflow (upload_hdb.start_offset, upload_hdb.length, &end) || + end > diag_buffer->size) { dprint_bsg_err(mrioc, "%s: invalid start offset %d, length %d for type %d\n", __func__, upload_hdb.start_offset, upload_hdb.length,
Signature · 1 line
2.47.3
PATCH v2 8/20 scsi: mpi3mr: Fix integer overflow in the log data size calculation
In reply to Ranjan KumarThe number of log entries and their total size are held in 16 bits. With a large entry size the product wraps around, so the driver copies only part of the log data and still reports success.
Use 32 bit variables for both values.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +2 -1
@@ -1346,7 +1346,8 @@ static long mpi3mr_enable_logdata(struct mpi3mr_ioc *mrioc,static long mpi3mr_get_logdata (struct mpi3mr_ioc *mrioc,struct bsg_job *job) { -u16 num_entries, sz, entry_sz = mrioc->logdata_entry_sz; +u16 entry_sz = mrioc->logdata_entry_sz; +u32 num_entries, sz;if ((!mrioc->logdata_buf) || (job->request_payload.payload_len < entry_sz))return -EINVAL ;
Signature · 1 line
2.47.3
PATCH v2 9/20 scsi: mpi3mr: Queue DEVICE_INFO_CHANGED events to the bottom half again
In reply to Ranjan KumarThe DEVICE_INFO_CHANGED event shares its case with the LOG_DATA event. When the log data handling was added to that case, DEVICE_INFO_CHANGED stopped being queued to the bottom half, so device information changes are no longer applied and the event is saved as log data instead.
Give DEVICE_INFO_CHANGED its own case that queues the bottom half.
--- drivers/scsi/mpi3mr/mpi3mr_os.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/scsi/mpi3mr/mpi3mr_os.c +4 -0
@@ -3273,6 +3273,10 @@ void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,break ; }case MPI3_EVENT_DEVICE_INFO_CHANGED : + { + process_evt_bh =1 ; +break ; + }case MPI3_EVENT_LOG_DATA :if (mrioc->reply_sz > offsetof(struct mpi3_event_notification_reply , event_data))
Signature · 1 line
2.47.3
PATCH v2 10/20 scsi: mpi3mr: Fix target device reference leaks
In reply to Ranjan KumarLooking up a target device takes a reference. When a target allocation is refused, or when a device has no HBA port, the reference is not released, so the device is never freed and its count grows with every rescan.
Release the reference on these paths.
--- drivers/scsi/mpi3mr/mpi3mr_os.c | 10 ++++++++-- drivers/scsi/mpi3mr/mpi3mr_transport.c | 7 ++++--- 2 files changed, 12 insertions(+), 5 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_os.c +8 -2
@@ -5114,8 +5114,11 @@ static int mpi3mr_target_alloc(struct scsi_target *starget) scsi_tgt_priv_data->wslen = tgt_dev->wslen;if (tgt_dev->dev_type ==MPI3_DEVICE_DEVFORM_VD ) scsi_tgt_priv_data->throttle_group = tgt_dev->dev_spec.vd_inf.tg; - }else + }else { +if (tgt_dev) +mpi3mr_tgtdev_put (tgt_dev); retval = -ENXIO ; + } }else if (mrioc->sas_transport_enabled && !starget->channel) { rphy =dev_to_rphy (starget->dev.parent); tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
@@ -5132,8 +5135,11 @@ static int mpi3mr_target_alloc(struct scsi_target *starget) tgt_dev->starget = starget;atomic_set (&scsi_tgt_priv_data->block_io,0 ); retval =0 ; - }else + }else { +if (tgt_dev) +mpi3mr_tgtdev_put (tgt_dev); retval = -ENXIO ; + } }spin_unlock_irqrestore (&mrioc->tgtdev_lock, flags);
drivers/scsi/mpi3mr/mpi3mr_transport.c +4 -3
@@ -2575,9 +2575,10 @@ static u8 mpi3mr_get_port_id_by_rphy(struct mpi3mr_ioc *mrioc, struct sas_rphy * tgtdev =__mpi3mr_get_tgtdev_by_addr_and_rphy (mrioc, rphy->identify.sas_address, rphy); -if (tgtdev && tgtdev->dev_spec.sas_sata_inf.hba_port) { - port_id = - tgtdev->dev_spec.sas_sata_inf.hba_port->port_id; +if (tgtdev) { +if (tgtdev->dev_spec.sas_sata_inf.hba_port) + port_id = + tgtdev->dev_spec.sas_sata_inf.hba_port->port_id;mpi3mr_tgtdev_put (tgtdev); }spin_unlock_irqrestore (&mrioc->tgtdev_lock, flags);
Signature · 1 line
2.47.3
PATCH v2 11/20 scsi: mpi3mr: Fix the 32 bit shift when building the phy mask
In reply to Ranjan KumarThe phy mask is 64 bits wide but the bit for a phy is built with a 32 bit shift. For phy numbers 31 and above the result is wrong or undefined, so the mask gets the wrong bits for those phys.
Build the bit with a 64 bit shift.
--- drivers/scsi/mpi3mr/mpi3mr_transport.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_transport.c +5 -5
@@ -610,7 +610,7 @@ static void mpi3mr_delete_sas_phy(struct mpi3mr_ioc *mrioc, mr_sas_port->num_phys--;if (host_node) { - mr_sas_port->phy_mask &= ~(1 << mr_sas_phy->phy_id); + mr_sas_port->phy_mask &= ~BIT_ULL (mr_sas_phy->phy_id);if (mr_sas_port->lowest_phy == mr_sas_phy->phy_id) mr_sas_port->lowest_phy =ffs (mr_sas_port->phy_mask) -1 ;
@@ -641,7 +641,7 @@ static void mpi3mr_add_sas_phy(struct mpi3mr_ioc *mrioc,list_add_tail (&mr_sas_phy->port_siblings, &mr_sas_port->phy_list); mr_sas_port->num_phys++;if (host_node) { - mr_sas_port->phy_mask |= (1 << mr_sas_phy->phy_id); + mr_sas_port->phy_mask |=BIT_ULL (mr_sas_phy->phy_id);if (mr_sas_phy->phy_id < mr_sas_port->lowest_phy) mr_sas_port->lowest_phy =ffs (mr_sas_port->phy_mask) -1 ;
@@ -1417,7 +1417,7 @@ static struct mpi3mr_sas_port *mpi3mr_sas_port_add(struct mpi3mr_ioc *mrioc, &mr_sas_port->phy_list); mr_sas_port->num_phys++;if (mr_sas_node->host_node) - mr_sas_port->phy_mask |= (1 << i); + mr_sas_port->phy_mask |=BIT_ULL (i); }if (!mr_sas_port->num_phys) {
@@ -1814,7 +1814,7 @@ mpi3mr_refresh_sas_ports(struct mpi3mr_ioc *mrioc) found =0 ;for (j =0 ; j < host_port_count; j++) {if (h_port[j].handle == attached_handle) { - h_port[j].phy_mask |= (1 << i); + h_port[j].phy_mask |=BIT_ULL (i); found =1 ;break ; }
@@ -1841,7 +1841,7 @@ mpi3mr_refresh_sas_ports(struct mpi3mr_ioc *mrioc) port_idx = host_port_count; h_port[port_idx].sas_address =le64_to_cpu (sasinf->sas_address); h_port[port_idx].handle = attached_handle; - h_port[port_idx].phy_mask = (1 << i); + h_port[port_idx].phy_mask =BIT_ULL (i); h_port[port_idx].iounit_port_id = sas_io_unit_pg0->phy_data[i].io_unit_port; h_port[port_idx].lowest_phy = sasinf->phy_num; h_port[port_idx].used =0 ;
Signature · 1 line
2.47.3
PATCH v2 12/20 scsi: mpi3mr: Fix empty device list race in the all targets query
In reply to Ranjan KumarThe number of target devices is counted under the target lock but the list is tested for emptiness later without it. If the devices go away in between, the user is given a count with no device list.
Decide from the count that was taken under the lock.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +1 -1
@@ -1499,7 +1499,7 @@ static long mpi3mr_get_all_tgt_info(struct mpi3mr_ioc *mrioc,spin_unlock_irqrestore (&mrioc->tgtdev_lock, flags);if ((job->request_payload.payload_len <=sizeof (u64)) || -list_empty (&mrioc->tgtdev_list)) { + !num_devices) {sg_copy_from_buffer (job->request_payload.sg_list, job->request_payload.sg_cnt, &num_devices,sizeof (num_devices));
Signature · 1 line
2.47.3
PATCH v2 13/20 scsi: mpi3mr: Fix the lowest phy number for phys 32 and above
In reply to Ranjan KumarThe phy mask is 64 bits wide but the lowest phy of a port is found with a call that takes a 32 bit value, so only the low 32 bits of the mask are looked at. For a port with only phys 32 and above the lowest phy comes out as -1.
Find the lowest phy in the whole mask.
--- drivers/scsi/mpi3mr/mpi3mr_transport.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_transport.c +14 -3
@@ -587,6 +587,17 @@ static enum sas_linkrate mpi3mr_convert_phy_link_rate(u8 link_rate)return rc; } +/** +* mpi3mr_lowest_phy - Get the lowest phy number in a phy mask +* @phy_mask: Phy mask of the port +* +* Return: The lowest phy number, or -1 if the mask is empty. +*/ +static int mpi3mr_lowest_phy (u64 phy_mask) +{ +return phy_mask ?__ffs64 (phy_mask) :-1 ; +} +/** * mpi3mr_delete_sas_phy - Remove a single phy from port * @mrioc: Adapter instance reference
@@ -613,7 +624,7 @@ static void mpi3mr_delete_sas_phy(struct mpi3mr_ioc *mrioc, mr_sas_port->phy_mask &= ~BIT_ULL (mr_sas_phy->phy_id);if (mr_sas_port->lowest_phy == mr_sas_phy->phy_id) - mr_sas_port->lowest_phy =ffs (mr_sas_port->phy_mask) -1 ; + mr_sas_port->lowest_phy =mpi3mr_lowest_phy (mr_sas_port->phy_mask); }sas_port_delete_phy (mr_sas_port->port, mr_sas_phy->phy); mr_sas_phy->phy_belongs_to_port =0 ;
@@ -644,7 +655,7 @@ static void mpi3mr_add_sas_phy(struct mpi3mr_ioc *mrioc, mr_sas_port->phy_mask |=BIT_ULL (mr_sas_phy->phy_id);if (mr_sas_phy->phy_id < mr_sas_port->lowest_phy) - mr_sas_port->lowest_phy =ffs (mr_sas_port->phy_mask) -1 ; + mr_sas_port->lowest_phy =mpi3mr_lowest_phy (mr_sas_port->phy_mask); }sas_port_add_phy (mr_sas_port->port, mr_sas_phy->phy); mr_sas_phy->phy_belongs_to_port =1 ;
@@ -1427,7 +1438,7 @@ static struct mpi3mr_sas_port *mpi3mr_sas_port_add(struct mpi3mr_ioc *mrioc, }if (mr_sas_node->host_node) - mr_sas_port->lowest_phy =ffs (mr_sas_port->phy_mask) -1 ; + mr_sas_port->lowest_phy =mpi3mr_lowest_phy (mr_sas_port->phy_mask);if (mr_sas_port->remote_identify.device_type ==SAS_END_DEVICE ) { tgtdev = mpi3mr_get_tgtdev_by_addr(mrioc,
Signature · 1 line
2.47.3
PATCH v2 14/20 scsi: mpi3mr: Fix target private data use after free in BSG passthrough
In reply to Ranjan KumarA BSG task management request raises block_io on the SCSI target private data and lowers it after the command completes. The pointer is read without a lock and the target device reference is dropped before the wait. If the device is removed meanwhile, the private data is freed and block_io is decremented in freed memory.
Update the count under the target device lock and keep the target device reference until the count is released. Lower the count only if the target still has the private data it was raised on.
--- drivers/scsi/mpi3mr/mpi3mr.h | 7 ++++ drivers/scsi/mpi3mr/mpi3mr_app.c | 24 +++++++------- drivers/scsi/mpi3mr/mpi3mr_os.c | 55 ++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 13 deletions(-)
drivers/scsi/mpi3mr/mpi3mr.h +7 -0
@@ -741,6 +741,7 @@ enum mpi3mr_dev_state {* * @list: List pointer * @starget: Scsi_target pointer +* @stgt_priv: Private data of the scsi_target * @dev_handle: FW device handle * @parent_handle: FW parent device handle * @slot: Slot number
@@ -766,6 +767,7 @@ enum mpi3mr_dev_state {struct mpi3mr_tgt_dev {struct list_head list;struct scsi_target *starget; +struct mpi3mr_stgt_priv_data *stgt_priv;u16 dev_handle;u16 parent_handle;u16 slot;
@@ -1533,6 +1535,11 @@ int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type, u16 handle, uint lun, u16 htag, ulong timeout, struct mpi3mr_drv_cmd *drv_cmd, u8 *resp_code, struct scsi_cmnd *scmd); +struct mpi3mr_stgt_priv_data *mpi3mr_tgtdev_block_io_inc (struct mpi3mr_ioc *mrioc, +struct mpi3mr_tgt_dev *tgtdev); +void mpi3mr_tgtdev_block_io_dec (struct mpi3mr_ioc *mrioc, +struct mpi3mr_tgt_dev *tgtdev, +struct mpi3mr_stgt_priv_data *stgt_priv);struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle (struct mpi3mr_ioc *mrioc,u16 handle); void mpi3mr_pel_get_seqnum_complete(struct mpi3mr_ioc *mrioc,
drivers/scsi/mpi3mr/mpi3mr_app.c +11 -13
@@ -2439,7 +2439,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)struct mpi3_scsi_task_mgmt_request *tm_req;u32 erbsz =MPI3MR_SENSE_BUF_SZ , tmplen;u16 dev_handle; -struct mpi3mr_tgt_dev *tgtdev; +struct mpi3mr_tgt_dev *tgtdev =NULL ;struct mpi3mr_stgt_priv_data *stgt_priv =NULL ;struct mpi3mr_bsg_in_reply_buf *bsg_reply_buf =NULL ;u32 din_size =0 , dout_size =0 ;
@@ -2791,14 +2791,8 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) }if (block_io) { tgtdev =mpi3mr_get_tgtdev_by_handle (mrioc, dev_handle); -if (tgtdev) { -if (tgtdev->starget && tgtdev->starget->hostdata) { - stgt_priv = (struct mpi3mr_stgt_priv_data *) - tgtdev->starget->hostdata; -atomic_inc (&stgt_priv->block_io); - } -mpi3mr_tgtdev_put (tgtdev); - } +if (tgtdev) + stgt_priv =mpi3mr_tgtdev_block_io_inc (mrioc, tgtdev); } mrioc->bsg_cmds.state =MPI3MR_CMD_PENDING ;
@@ -2830,14 +2824,18 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)dprint_bsg_err (mrioc,"%s: posting bsg request is failed\n" , __func__); rval = -EAGAIN ; -if (block_io && stgt_priv) -atomic_dec (&stgt_priv->block_io); +if (tgtdev) { +mpi3mr_tgtdev_block_io_dec (mrioc, tgtdev, stgt_priv); +mpi3mr_tgtdev_put (tgtdev); + } goto out_unlock; }wait_for_completion_timeout (&mrioc->bsg_cmds.done, (karg->timeout *HZ )); -if (block_io && stgt_priv) -atomic_dec (&stgt_priv->block_io); +if (tgtdev) { +mpi3mr_tgtdev_block_io_dec (mrioc, tgtdev, stgt_priv); +mpi3mr_tgtdev_put (tgtdev); + }if (!(mrioc->bsg_cmds.state &MPI3MR_CMD_COMPLETE )) { timed_out = true; mrioc->bsg_cmds.is_waiting =0 ;
drivers/scsi/mpi3mr/mpi3mr_os.c +55 -0
79 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH v2 15/20 scsi: mpi3mr: Fix block_io accounting when a target is re-created during BSG
In reply to Ranjan KumarA BSG task management request raises block_io on the SCSI target private data when it starts and lowers it when it completes. If the target is removed and created again while the command waits, the new private data can be allocated at the address of the old one. The address check then passes and the command lowers block_io of the new target, which may be held by another path.
Give each private data a sequence number and lower block_io only when the number is the same as when the command started.
--- drivers/scsi/mpi3mr/mpi3mr.h | 9 ++++---- drivers/scsi/mpi3mr/mpi3mr_app.c | 8 +++---- drivers/scsi/mpi3mr/mpi3mr_os.c | 39 ++++++++++++++++++++------------ 3 files changed, 33 insertions(+), 23 deletions(-)
drivers/scsi/mpi3mr/mpi3mr.h +5 -4
@@ -831,6 +831,7 @@ static inline void mpi3mr_tgtdev_put(struct mpi3mr_tgt_dev *s)* @perst_id: FW assigned Persistent ID * @num_luns: Number of Logical Units * @block_io: I/O blocked to the device or not +* @seq: Sequence number, unique for every allocated private data * @dev_removed: Device removed in the Firmware * @dev_removedelay: Device is waiting to be removed in FW * @dev_type: Device type
@@ -849,6 +850,7 @@ struct mpi3mr_stgt_priv_data {u16 perst_id;u32 num_luns;atomic_t block_io; +u64 seq;u8 dev_removed;u8 dev_removedelay;u8 dev_type;
@@ -1535,11 +1537,10 @@ int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type, u16 handle, uint lun, u16 htag, ulong timeout, struct mpi3mr_drv_cmd *drv_cmd, u8 *resp_code, struct scsi_cmnd *scmd); -struct mpi3mr_stgt_priv_data *mpi3mr_tgtdev_block_io_inc (struct mpi3mr_ioc *mrioc, -struct mpi3mr_tgt_dev *tgtdev); +u64 mpi3mr_tgtdev_block_io_inc (struct mpi3mr_ioc *mrioc, +struct mpi3mr_tgt_dev *tgtdev);void mpi3mr_tgtdev_block_io_dec (struct mpi3mr_ioc *mrioc, -struct mpi3mr_tgt_dev *tgtdev, -struct mpi3mr_stgt_priv_data *stgt_priv); +struct mpi3mr_tgt_dev *tgtdev,u64 seq);struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle (struct mpi3mr_ioc *mrioc,u16 handle); void mpi3mr_pel_get_seqnum_complete(struct mpi3mr_ioc *mrioc,
drivers/scsi/mpi3mr/mpi3mr_app.c +4 -4
@@ -2440,7 +2440,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)u32 erbsz =MPI3MR_SENSE_BUF_SZ , tmplen;u16 dev_handle;struct mpi3mr_tgt_dev *tgtdev =NULL ; -struct mpi3mr_stgt_priv_data *stgt_priv =NULL ; +u64 block_seq =0 ;struct mpi3mr_bsg_in_reply_buf *bsg_reply_buf =NULL ;u32 din_size =0 , dout_size =0 ;u8 *din_buf =NULL , *dout_buf =NULL ;
@@ -2792,7 +2792,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)if (block_io) { tgtdev =mpi3mr_get_tgtdev_by_handle (mrioc, dev_handle);if (tgtdev) - stgt_priv =mpi3mr_tgtdev_block_io_inc (mrioc, tgtdev); + block_seq =mpi3mr_tgtdev_block_io_inc (mrioc, tgtdev); } mrioc->bsg_cmds.state =MPI3MR_CMD_PENDING ;
@@ -2825,7 +2825,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) "%s: posting bsg request is failed\n", __func__); rval = -EAGAIN ;if (tgtdev) { -mpi3mr_tgtdev_block_io_dec (mrioc, tgtdev, stgt_priv); +mpi3mr_tgtdev_block_io_dec (mrioc, tgtdev, block_seq);mpi3mr_tgtdev_put (tgtdev); } goto out_unlock;
@@ -2833,7 +2833,7 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)wait_for_completion_timeout (&mrioc->bsg_cmds.done, (karg->timeout *HZ ));if (tgtdev) { -mpi3mr_tgtdev_block_io_dec (mrioc, tgtdev, stgt_priv); +mpi3mr_tgtdev_block_io_dec (mrioc, tgtdev, block_seq);mpi3mr_tgtdev_put (tgtdev); }if (!(mrioc->bsg_cmds.state &MPI3MR_CMD_COMPLETE )) {
drivers/scsi/mpi3mr/mpi3mr_os.c +24 -15
@@ -788,6 +788,8 @@ struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(return tgtdev; } +static atomic64_t mpi3mr_stgt_seq =ATOMIC64_INIT (0 ); +/** * mpi3mr_tgtdev_block_io_inc - Block I/O on the SCSI target of a tgtdev * @mrioc: Adapter instance reference
@@ -797,44 +799,49 @@ struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(* private data is looked up and incremented under tgtdev_lock, which is * also held when the target is destroyed. * -* Return: The private data that was incremented, to be handed back when -* block_io is decremented. NULL if the tgtdev has no private data. +* Return: Sequence number of the private data that was incremented, to be +* handed back when block_io is decremented. 0 if the tgtdev has no private +* data. */ -struct mpi3mr_stgt_priv_data *mpi3mr_tgtdev_block_io_inc (struct mpi3mr_ioc *mrioc, -struct mpi3mr_tgt_dev *tgtdev) +u64 mpi3mr_tgtdev_block_io_inc (struct mpi3mr_ioc *mrioc, +struct mpi3mr_tgt_dev *tgtdev) { -struct mpi3mr_stgt_priv_data *stgt_priv =NULL ; +struct mpi3mr_stgt_priv_data *stgt_priv;unsigned long flags; +u64 seq =0 ;spin_lock_irqsave (&mrioc->tgtdev_lock, flags); stgt_priv = tgtdev->stgt_priv; -if (stgt_priv) +if (stgt_priv) {atomic_inc (&stgt_priv->block_io); + seq = stgt_priv->seq; + }spin_unlock_irqrestore (&mrioc->tgtdev_lock, flags); -return stgt_priv; +return seq; }/** * mpi3mr_tgtdev_block_io_dec - Undo mpi3mr_tgtdev_block_io_inc * @mrioc: Adapter instance reference * @tgtdev: Target device reference, held by the caller -* @stgt_priv: Value returned by mpi3mr_tgtdev_block_io_inc +* @seq: Value returned by mpi3mr_tgtdev_block_io_inc * -* Decrement block_io only if the tgtdev still has the same private data. -* If the target was removed meanwhile the private data is gone and there -* is nothing to undo. +* Decrement block_io only if the tgtdev still has the private data that was +* incremented. A private data allocated later at the same address has a +* different sequence number and is left alone. */ void mpi3mr_tgtdev_block_io_dec (struct mpi3mr_ioc *mrioc, -struct mpi3mr_tgt_dev *tgtdev, -struct mpi3mr_stgt_priv_data *stgt_priv) +struct mpi3mr_tgt_dev *tgtdev,u64 seq) { +struct mpi3mr_stgt_priv_data *stgt_priv;unsigned long flags; -if (!stgt_priv) +if (!seq)return ;spin_lock_irqsave (&mrioc->tgtdev_lock, flags); -if (tgtdev->stgt_priv == stgt_priv) + stgt_priv = tgtdev->stgt_priv; +if (stgt_priv && stgt_priv->seq == seq)atomic_dec_if_positive (&stgt_priv->block_io);spin_unlock_irqrestore (&mrioc->tgtdev_lock, flags); }
@@ -5141,6 +5148,8 @@ static int mpi3mr_target_alloc(struct scsi_target *starget)if (!scsi_tgt_priv_data)return -ENOMEM ; + scsi_tgt_priv_data->seq =atomic64_inc_return (&mpi3mr_stgt_seq); + starget->hostdata = scsi_tgt_priv_data;spin_lock_irqsave (&mrioc->tgtdev_lock, flags);
Signature · 1 line
2.47.3
PATCH v2 16/20 scsi: mpi3mr: Fix sense buffer use after free on BSG command completion
In reply to Ranjan KumarThe interrupt handler writes the reply and sense data of a BSG command into the command tracker while the ioctl can time out, clear the sense buffer pointer and free the buffer at the same time.
Add a lock to the command tracker and take it in the handler, the reset flush and the ioctl. The callbacks run under it and never sleep. Also initialize the two PEL trackers, which never had their lock set up.
--- drivers/scsi/mpi3mr/mpi3mr.h | 2 ++ drivers/scsi/mpi3mr/mpi3mr_app.c | 35 ++++++++++++++++++++++++-------- drivers/scsi/mpi3mr/mpi3mr_fw.c | 8 ++++++++ drivers/scsi/mpi3mr/mpi3mr_os.c | 3 +++ 4 files changed, 40 insertions(+), 8 deletions(-)
drivers/scsi/mpi3mr/mpi3mr.h +2 -0
@@ -885,6 +885,7 @@ struct mpi3mr_sdev_priv_data {* struct mpi3mr_drv_cmd - Internal command tracker * * @mutex: Command mutex +* @state_lock: Protects the state, the sense buffer and the callback * @done: Completeor for wakeup * @reply: Firmware reply for internal commands * @sensebuf: Sensebuf for SCSI IO commands
@@ -901,6 +902,7 @@ struct mpi3mr_sdev_priv_data {*/ struct mpi3mr_drv_cmd {struct mutex mutex; +spinlock_t state_lock;struct completion done;void *reply;u8 *sensebuf;
drivers/scsi/mpi3mr/mpi3mr_app.c +27 -8
@@ -2447,7 +2447,9 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)u8 *sgl_iter =NULL , *sgl_din_iter =NULL , *sgl_dout_iter =NULL ;u16 rmc_size =0 , desc_count =0 ;size_t req_min, buf_left; -bool prp_owned = false, timed_out = false; +bool prp_owned = false, timed_out = false, completed; +unsigned long flags; +u16 cmd_state; bsg_req = job->request; karg = (struct mpi3mr_bsg_mptcmd *)&bsg_req->cmd.mptcmd;
@@ -2795,11 +2797,13 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) block_seq =mpi3mr_tgtdev_block_io_inc (mrioc, tgtdev); } +spin_lock_irqsave (&mrioc->bsg_cmds.state_lock, flags); mrioc->bsg_cmds.state =MPI3MR_CMD_PENDING ; mrioc->bsg_cmds.is_waiting =1 ; mrioc->bsg_cmds.callback =NULL ; mrioc->bsg_cmds.is_sense =0 ; mrioc->bsg_cmds.sensebuf = sense_buff_k; +spin_unlock_irqrestore (&mrioc->bsg_cmds.state_lock, flags);memset (mrioc->bsg_cmds.reply,0 , mrioc->reply_sz); mpi_header->host_tag =cpu_to_le16 (MPI3MR_HOSTTAG_BSG_CMDS );if (mrioc->logging_level &MPI3_DEBUG_BSG_INFO ) {
@@ -2820,7 +2824,9 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)if (rval) { +spin_lock_irqsave (&mrioc->bsg_cmds.state_lock, flags); mrioc->bsg_cmds.is_waiting =0 ; +spin_unlock_irqrestore (&mrioc->bsg_cmds.state_lock, flags);dprint_bsg_err (mrioc,"%s: posting bsg request is failed\n" , __func__); rval = -EAGAIN ;
@@ -2836,11 +2842,16 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)mpi3mr_tgtdev_block_io_dec (mrioc, tgtdev, block_seq);mpi3mr_tgtdev_put (tgtdev); } -if (!(mrioc->bsg_cmds.state &MPI3MR_CMD_COMPLETE )) { - timed_out = true; +spin_lock_irqsave (&mrioc->bsg_cmds.state_lock, flags); + cmd_state = mrioc->bsg_cmds.state; + completed = cmd_state &MPI3MR_CMD_COMPLETE ; +if (!completed) mrioc->bsg_cmds.is_waiting =0 ; +spin_unlock_irqrestore (&mrioc->bsg_cmds.state_lock, flags); +if (!completed) { + timed_out = true; rval = -EAGAIN ; -if (mrioc->bsg_cmds.state &MPI3MR_CMD_RESET ) +if (cmd_state &MPI3MR_CMD_RESET ) goto out_unlock;if (((mpi_header->function !=MPI3_FUNCTION_SCSI_IO ) && (mpi_header->function != MPI3_FUNCTION_NVME_ENCAPSULATED))
@@ -2870,8 +2881,11 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)MPI3MR_HOSTTAG_BLK_TMS ,MPI3MR_RESETTM_TIMEOUT , &mrioc->host_tm_cmds, &resp_code, NULL); } -if (!(mrioc->bsg_cmds.state &MPI3MR_CMD_COMPLETE ) && - !(mrioc->bsg_cmds.state &MPI3MR_CMD_RESET )) +spin_lock_irqsave (&mrioc->bsg_cmds.state_lock, flags); + cmd_state = mrioc->bsg_cmds.state; +spin_unlock_irqrestore (&mrioc->bsg_cmds.state_lock, flags); +if (!(cmd_state &MPI3MR_CMD_COMPLETE ) && + !(cmd_state &MPI3MR_CMD_RESET ))mpi3mr_soft_reset_handler (mrioc,MPI3MR_RESET_FROM_APP_TIMEOUT ,1 ); goto out_unlock;
@@ -2947,14 +2961,17 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) out_unlock:if (prp_owned) { +spin_lock_irqsave (&mrioc->bsg_cmds.state_lock, flags); + cmd_state = mrioc->bsg_cmds.state; +spin_unlock_irqrestore (&mrioc->bsg_cmds.state_lock, flags);/* * Do not free the PRP list if a timed out command may still be * using it. Leaking it is safer than letting the controller read * freed memory. */ if (!timed_out || - (mrioc->bsg_cmds.state &MPI3MR_CMD_COMPLETE ) || - ((mrioc->bsg_cmds.state &MPI3MR_CMD_RESET ) && + (cmd_state &MPI3MR_CMD_COMPLETE ) || + ((cmd_state &MPI3MR_CMD_RESET ) && !mrioc->unrecoverable))dma_free_coherent (&mrioc->pdev->dev, mrioc->prp_sz, mrioc->prp_list_virt, mrioc->prp_list_dma);
@@ -2966,9 +2983,11 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) job->reply_payload.sg_cnt, din_buf, job->reply_payload.payload_len); } +spin_lock_irqsave (&mrioc->bsg_cmds.state_lock, flags); mrioc->bsg_cmds.is_sense =0 ; mrioc->bsg_cmds.sensebuf =NULL ; mrioc->bsg_cmds.state =MPI3MR_CMD_NOTUSED ; +spin_unlock_irqrestore (&mrioc->bsg_cmds.state_lock, flags);mutex_unlock (&mrioc->bsg_cmds.mutex); out:kfree (sense_buff_k);
drivers/scsi/mpi3mr/mpi3mr_fw.c +8 -0
@@ -442,6 +442,9 @@ static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc, cmdptr =mpi3mr_get_drv_cmd (mrioc, host_tag, def_reply);if (cmdptr) { +unsigned long flags; + +spin_lock_irqsave (&cmdptr->state_lock, flags);if (cmdptr->state &MPI3MR_CMD_PENDING ) { cmdptr->state |=MPI3MR_CMD_COMPLETE ; cmdptr->ioc_loginfo = ioc_loginfo;
@@ -466,6 +469,7 @@ static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc, }else if (cmdptr->callback) cmdptr->callback (mrioc, cmdptr); } +spin_unlock_irqrestore (&cmdptr->state_lock, flags); } out:if (sense_buf)
@@ -5333,6 +5337,9 @@ void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc)static inline void mpi3mr_drv_cmd_comp_reset (struct mpi3mr_ioc *mrioc,struct mpi3mr_drv_cmd *cmdptr) { +unsigned long flags; + +spin_lock_irqsave (&cmdptr->state_lock, flags);if (cmdptr->state &MPI3MR_CMD_PENDING ) { cmdptr->state |=MPI3MR_CMD_RESET ; cmdptr->state &= ~MPI3MR_CMD_PENDING ;
@@ -5342,6 +5349,7 @@ static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_ioc *mrioc, }else if (cmdptr->callback) cmdptr->callback (mrioc, cmdptr); } +spin_unlock_irqrestore (&cmdptr->state_lock, flags); }/**
drivers/scsi/mpi3mr/mpi3mr_os.c +3 -0
@@ -5585,6 +5585,7 @@ static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr, u16 host_tag) {mutex_init (&cmdptr->mutex); +spin_lock_init (&cmdptr->state_lock); cmdptr->reply =NULL ; cmdptr->state =MPI3MR_CMD_NOTUSED ; cmdptr->dev_handle =MPI3MR_INVALID_DEV_HANDLE ;
@@ -5735,6 +5736,8 @@ mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)mpi3mr_init_drv_cmd (&mrioc->cfg_cmds,MPI3MR_HOSTTAG_CFG_CMDS );mpi3mr_init_drv_cmd (&mrioc->transport_cmds,MPI3MR_HOSTTAG_TRANSPORT_CMDS ); +mpi3mr_init_drv_cmd (&mrioc->pel_cmds,MPI3MR_HOSTTAG_PEL_WAIT ); +mpi3mr_init_drv_cmd (&mrioc->pel_abort_cmd,MPI3MR_HOSTTAG_PEL_ABORT );for (i =0 ; i <MPI3MR_NUM_DEVRMCMD ; i++) mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v2 16/20] scsi: mpi3mr: Fix sense buffer use after free on BSG command completion
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] scsi: mpi3mr: race on bsg_cmds.done and reply in mpi3mr_bsg_process_mpt_cmds() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] scsi: mpi3mr: race on bsg_cmds.done and reply in mpi3mr_bsg_process_mpt_cmds()
--
commit 8590d021fe282f240b050a47484ba6f1b211d756
Author: Ranjan Kumar <[email protected]>
scsi: mpi3mr: Fix sense buffer use after free on BSG command completion
This commit adds a spinlock to struct mpi3mr_drv_cmd to serialize access to command state, sense buffers, and completion callbacks between BSG command handling, admin reply processing, and controller resets.
Quoted 4 lines
drivers/scsi/mpi3mr/mpi3mr_app.c +0
-0
[ ... ]
Quoted 4 lines
drivers/scsi/mpi3mr/mpi3mr_app.c +0
-0@@ -2795,11 +2797,13 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) block_seq =mpi3mr_tgtdev_block_io_inc (mrioc, tgtdev); }
drivers/scsi/mpi3mr/mpi3mr_app.c static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) · line 2809
Quoted 10 lines
drivers/scsi/mpi3mr/mpi3mr_app.c +2
-0+spin_lock_irqsave (&mrioc->bsg_cmds.state_lock, flags); mrioc->bsg_cmds.state =MPI3MR_CMD_PENDING ; mrioc->bsg_cmds.is_waiting =1 ; mrioc->bsg_cmds.callback =NULL ; mrioc->bsg_cmds.is_sense =0 ; mrioc->bsg_cmds.sensebuf = sense_buff_k; +spin_unlock_irqrestore (&mrioc->bsg_cmds.state_lock, flags);memset (mrioc->bsg_cmds.reply,0 , mrioc->reply_sz); mpi_header->host_tag =cpu_to_le16 (MPI3MR_HOSTTAG_BSG_CMDS );if (mrioc->logging_level &MPI3_DEBUG_BSG_INFO ) {
[Severity: Medium]
Should memset(mrioc->bsg_cmds.reply, ...) and init_completion(&mrioc->bsg_cmds.done) run before releasing mrioc->bsg_cmds.state_lock?
Once state is set to MPI3MR_CMD_PENDING with is_waiting = 1 and state_lock is released, a concurrent reset or a late reply from an earlier timed-out BSG command can acquire state_lock and complete bsg_cmds before init_completion(&mrioc->bsg_cmds.done) runs:
CPU0 CPU1mpi3mr_bsg_process_mpt_cmds()
spin_lock_irqsave(&state_lock)
bsg_cmds.state = MPI3MR_CMD_PENDING
bsg_cmds.is_waiting = 1
spin_unlock_irqrestore(&state_lock)
mpi3mr_soft_reset_handler()
...
mpi3mr_drv_cmd_comp_reset()
spin_lock_irqsave(&state_lock)
complete(&bsg_cmds.done)
<-- NULL pointer dereference
spin_unlock_irqrestore(&state_lock) init_completion(&bsg_cmds.done)
Because mpi3mr_init_drv_cmd() does not initialize cmdptr->done, on the first BSG command after probe bsg_cmds.done is still all-zeroes and complete() dereferences NULL in swake_up_locked(). On later BSG commands, init_completion() outside state_lock races with complete() on done.wait.lock or resets done.done after the wakeup, and memset() on bsg_cmds.reply races with memcpy() in mpi3mr_process_admin_reply_desc().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Re: [PATCH v2 16/20] scsi: mpi3mr: Fix sense buffer use after free on BSG command completion
Hi,
Thanks for the review.
Quoted 68 lines · On Thu, Nov 19, 2026 at 1:04 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] scsi: mpi3mr: race on bsg_cmds.done and reply inmpi3mr_bsg_process_mpt_cmds()
--commit
8590d021fe282f240b050a47484ba6f1b211d756
Author: Ranjan Kumar <[email protected]>scsi: mpi3mr: Fix sense buffer use after free on BSG command completion
This commit adds a spinlock to struct
mpi3mr_drv_cmdto serialize access to command state, sense buffers, and completion callbacks between BSG command handling, admin reply processing, and controller resets.Quoted 4 lines
drivers/scsi/mpi3mr/mpi3mr_app.c +0
-0[ ... ]
Quoted 4 lines
drivers/scsi/mpi3mr/mpi3mr_app.c +0
-0@@ -2795,11 +2797,13 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) block_seq =mpi3mr_tgtdev_block_io_inc (mrioc, tgtdev); }drivers/scsi/mpi3mr/mpi3mr_app.c static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job) · line 2809
Quoted 10 lines
drivers/scsi/mpi3mr/mpi3mr_app.c +2
-0+spin_lock_irqsave (&mrioc->bsg_cmds.state_lock, flags); mrioc->bsg_cmds.state =MPI3MR_CMD_PENDING ; mrioc->bsg_cmds.is_waiting =1 ; mrioc->bsg_cmds.callback =NULL ; mrioc->bsg_cmds.is_sense =0 ; mrioc->bsg_cmds.sensebuf = sense_buff_k; +spin_unlock_irqrestore (&mrioc->bsg_cmds.state_lock, flags);memset (mrioc->bsg_cmds.reply,0 , mrioc->reply_sz); mpi_header->host_tag =cpu_to_le16 (MPI3MR_HOSTTAG_BSG_CMDS );if (mrioc->logging_level &MPI3_DEBUG_BSG_INFO ) {[Severity: Medium]
Shouldmemset(mrioc->bsg_cmds.reply, ...)andinit_completion(&mrioc->bsg_cmds.done)run before releasingmrioc->bsg_cmds.state_lock?Once state is set to
MPI3MR_CMD_PENDINGwithis_waiting= 1 andstate_lockis released, a concurrent reset or a late reply from an earlier timed-out BSG command can acquirestate_lockand completebsg_cmdsbeforeinit_completion(&mrioc->bsg_cmds.done)runs:CPU0 CPU1
mpi3mr_bsg_process_mpt_cmds()
spin_lock_irqsave(&state_lock)
bsg_cmds.state =MPI3MR_CMD_PENDING
bsg_cmds.is_waiting = 1
spin_unlock_irqrestore(&state_lock)mpi3mr_soft_reset_handler() ... mpi3mr_drv_cmd_comp_reset() spin_lock_irqsave(&state_lock) complete(&bsg_cmds.done) <-- NULL pointer dereference spin_unlock_irqrestore(&state_lock)
init_completion(&bsg_cmds.done)Because
mpi3mr_init_drv_cmd()does not initializecmdptr->done, on the first BSG command after probe bsg_cmds.done is still all-zeroes andcomplete()dereferences NULL inswake_up_locked(). On later BSG commands,init_completion()outsidestate_lockraces withcomplete()on done.wait.lock or resets done.done after the wakeup, andmemset()on bsg_cmds.reply races withmemcpy()inmpi3mr_process_admin_reply_desc().
Quoted 4 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16This ordering is not new. In the current driver the
memset()of the
reply and the init_completion() of done also come after the state is set, and this patch does not touch those two lines, it only puts the state, sense buffer and callback fields under the new lock.
I would rather not mix it into this fix, to keep one logical change per patch. I will send a separate follow-up patch for it.
Thanks,
Ranjan
PATCH v2 17/20 scsi: mpi3mr: Fix partly written and lost entries in the log data buffer
In reply to Ranjan KumarLog data events are saved from the event handlers while the get and enable ioctls read and publish the same buffer, index and entry size, all without a lock. A read can return an entry that is only partly written, and two events handled at the same time can take the same index so one is lost.
Add a lock for the log data buffer and take it in the writer and in both ioctls.
--- drivers/scsi/mpi3mr/mpi3mr.h | 2 ++ drivers/scsi/mpi3mr/mpi3mr_app.c | 43 +++++++++++++++++++++++--------- drivers/scsi/mpi3mr/mpi3mr_fw.c | 3 +++ drivers/scsi/mpi3mr/mpi3mr_os.c | 1 + 4 files changed, 37 insertions(+), 12 deletions(-)
drivers/scsi/mpi3mr/mpi3mr.h +2 -0
@@ -1169,6 +1169,7 @@ struct scmd_priv {* @logdata_buf: Circular buffer to store log data entries * @logdata_buf_idx: Index of entry in buffer to store * @logdata_entry_sz: log data entry size +* @logdata_lock: Lock for the log data buffer, index and entry size * @adm_req_q_bar_writeq_lock: Admin request queue lock * @adm_reply_q_bar_writeq_lock: Admin reply queue lock * @pend_large_data_sz: Counter to track pending large data
@@ -1378,6 +1379,7 @@ struct mpi3mr_ioc {u8 *logdata_buf;u16 logdata_buf_idx;u16 logdata_entry_sz; +spinlock_t logdata_lock;spinlock_t adm_req_q_bar_writeq_lock;spinlock_t adm_reply_q_bar_writeq_lock;
drivers/scsi/mpi3mr/mpi3mr_app.c +31 -12
87 lines not on this page · raw message
drivers/scsi/mpi3mr/mpi3mr_fw.c +3 -0
@@ -5029,6 +5029,7 @@ void mpi3mr_memset_buffers(struct mpi3mr_ioc *mrioc)void mpi3mr_free_mem (struct mpi3mr_ioc *mrioc) {u16 i, j; +unsigned long flags;struct mpi3mr_intr_info *intr_info;struct diag_buffer_desc *diag_buffer;
@@ -5197,8 +5198,10 @@ void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc)kfree (mrioc->throttle_groups); mrioc->throttle_groups =NULL ; +spin_lock_irqsave (&mrioc->logdata_lock, flags);kfree (mrioc->logdata_buf); mrioc->logdata_buf =NULL ; +spin_unlock_irqrestore (&mrioc->logdata_lock, flags); }
drivers/scsi/mpi3mr/mpi3mr_os.c +1 -0
@@ -5720,6 +5720,7 @@ mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)spin_lock_init (&mrioc->adm_reply_q_bar_writeq_lock);spin_lock_init (&mrioc->sas_node_lock);spin_lock_init (&mrioc->trigger_lock); +spin_lock_init (&mrioc->logdata_lock);INIT_LIST_HEAD (&mrioc->fwevt_list);INIT_LIST_HEAD (&mrioc->tgtdev_list);
Signature · 1 line
2.47.3
PATCH v2 18/20 scsi: mpi3mr: Fix byte order handling in the NVMe passthrough commands
In reply to Ranjan KumarThe NVMe passthrough code reads the command dword that selects PRP or SGL and applies the SGE modifier to the addresses without converting between CPU and little endian byte order. On a big endian host the wrong data format is chosen and the modifier bits end up in the wrong place of the address.
Convert the command dword when reading it, and keep the modifier mask and value in little endian form like the address fields they are applied to.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +9 -9
@@ -1966,7 +1966,7 @@ static unsigned int mpi3mr_get_nvme_data_fmt( {u8 format =0 ; - format = ((nvme_encap_request->command[0 ] &0xc000 ) >>14 ); + format = ((le32_to_cpu (nvme_encap_request->command[0 ]) &0xc000 ) >>14 );return format; }
@@ -1995,10 +1995,10 @@ static int mpi3mr_build_nvme_sgl(struct mpi3mr_ioc *mrioc,u16 available_sges =0 , i;u32 sge_element_size =sizeof (struct mpi3mr_nvme_pt_sge );struct mpi3mr_buf_map *drv_buf_iter = drv_bufs; -u64 sgemod_mask = ((u64)((mrioc->facts.sge_mod_mask) << - mrioc->facts.sge_mod_shift) <<32 ); -u64 sgemod_val = ((u64)(mrioc->facts.sge_mod_value) << - mrioc->facts.sge_mod_shift) <<32 ; +__le64 sgemod_mask =cpu_to_le64 (((u64)((mrioc->facts.sge_mod_mask) << + mrioc->facts.sge_mod_shift) <<32 )); +__le64 sgemod_val =cpu_to_le64 (((u64)(mrioc->facts.sge_mod_value) << + mrioc->facts.sge_mod_shift) <<32 );u32 size; nvme_sgl = (struct mpi3mr_nvme_pt_sge *)
@@ -2094,10 +2094,10 @@ static int mpi3mr_build_nvme_prp(struct mpi3mr_ioc *mrioc,size_t length =0 , desc_len;u8 count;struct mpi3mr_buf_map *drv_buf_iter = drv_bufs; -u64 sgemod_mask = ((u64)((mrioc->facts.sge_mod_mask) << - mrioc->facts.sge_mod_shift) <<32 ); -u64 sgemod_val = ((u64)(mrioc->facts.sge_mod_value) << - mrioc->facts.sge_mod_shift) <<32 ; +__le64 sgemod_mask =cpu_to_le64 (((u64)((mrioc->facts.sge_mod_mask) << + mrioc->facts.sge_mod_shift) <<32 )); +__le64 sgemod_val =cpu_to_le64 (((u64)(mrioc->facts.sge_mod_value) << + mrioc->facts.sge_mod_shift) <<32 );u16 dev_handle =le16_to_cpu (nvme_encap_request->dev_handle);struct mpi3mr_tgt_dev *tgtdev;u16 desc_count =0 ;
Signature · 1 line
2.47.3
PATCH v2 19/20 scsi: mpi3mr: Fix byte order of the BSG status reply
In reply to Ranjan KumarThe status reply returned for a BSG MPT command stores the IOC status and log info in little endian fields without converting them from CPU byte order, so a big endian host returns swapped values to the application.
Convert both values to little endian.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +4 -2
@@ -2935,8 +2935,10 @@ static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job)MPI3MR_BSG_MPI_REPLY_BUFTYPE_STATUS ; status_desc = (struct mpi3_status_reply_descriptor *) bsg_reply_buf->reply_buf; - status_desc->ioc_status = mrioc->bsg_cmds.ioc_status; - status_desc->ioc_log_info = mrioc->bsg_cmds.ioc_loginfo; + status_desc->ioc_status = +cpu_to_le16 (mrioc->bsg_cmds.ioc_status); + status_desc->ioc_log_info = +cpu_to_le32 (mrioc->bsg_cmds.ioc_loginfo); } tmplen =min (drv_buf_iter->kern_buf_len, drv_buf_iter->bsg_buf_len);
Signature · 1 line
2.47.3
PATCH v2 20/20 scsi: mpi3mr: Use the correct conversion for the diag buffer post request
In reply to Ranjan KumarThe diag buffer post request is filled in with le64_to_cpu and le32_to_cpu where cpu_to_le64 and cpu_to_le32 are meant. Both swap bytes the same way, so the result does not change, but sparse reports the wrong direction.
Use the helpers that match the direction.
--- drivers/scsi/mpi3mr/mpi3mr_app.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_app.c +2 -2
@@ -266,8 +266,8 @@ int mpi3mr_issue_diag_buf_post(struct mpi3mr_ioc *mrioc, diag_buf_post_req.host_tag =cpu_to_le16 (MPI3MR_HOSTTAG_INITCMDS ); diag_buf_post_req.function =MPI3_FUNCTION_DIAG_BUFFER_POST ; diag_buf_post_req.type = diag_buffer->type; - diag_buf_post_req.address =le64_to_cpu (diag_buffer->dma_addr); - diag_buf_post_req.length =le32_to_cpu (diag_buffer->size); + diag_buf_post_req.address =cpu_to_le64 (diag_buffer->dma_addr); + diag_buf_post_req.length =cpu_to_le32 (diag_buffer->size);if (diag_buffer->is_segmented) diag_buf_post_req.msg_flags |=MPI3_DIAG_BUFFER_POST_MSGFLAGS_SEGMENTED ;
Signature · 1 line
2.47.3