scsi: ibmvfc: Unmap scatterlists before completing BSG jobs
Subsystems (3)
PATCH 1/1 scsi: ibmvfc: Unmap scatterlists before completing BSG jobs
From: Suyuan Wang <[email protected]>
ibmvfc_bsg_request() calls bsg_job_done() before unmapping the request and reply scatterlists.
bsg_job_done() starts block request completion, which can run bsg_teardown_job() and free both scatterlists before the following dma_unmap_sg() calls. The unmaps can then read freed memory.
Complete the job after the common DMA cleanup instead. The completion path has already saved the operation result in bsg_reply and set rc to zero; paths that return an error keep their existing cleanup and leave completion to the FC transport. passthru_mutex stays held until after the completion call.
Found with a static analysis tool and checked by hand.
---
Compile tested on powerpc with clang (W=1, no new warnings). I don't have IBM Power virtual FC hardware.
--- drivers/scsi/ibmvscsi/ibmvfc-core.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/scsi/ibmvscsi/ibmvfc-core.c +3 -2
@@ -2392,14 +2392,15 @@ static int ibmvfc_bsg_request(struct bsg_job *job)ibmvfc_free_event (evt);spin_unlock_irqrestore (&vhost->host->host_lock, flags); bsg_reply->result = rc; -bsg_job_done (job, bsg_reply->result, - bsg_reply->reply_payload_rcv_len); rc =0 ; out:dma_unmap_sg (vhost->dev, job->request_payload.sg_list, job->request_payload.sg_cnt,DMA_TO_DEVICE );dma_unmap_sg (vhost->dev, job->reply_payload.sg_list, job->reply_payload.sg_cnt,DMA_FROM_DEVICE ); +if (!rc) +bsg_job_done (job, bsg_reply->result, + bsg_reply->reply_payload_rcv_len);mutex_unlock (&vhost->passthru_mutex);LEAVE ;return rc;