Commit 4a8e8e0a authored by Hannes Reinecke's avatar Hannes Reinecke Committed by Greg Kroah-Hartman

scsi: sg: close race condition in sg_remove_sfp_usercontext()


[ Upstream commit 97d27b0d ]

sg_remove_sfp_usercontext() is clearing any sg requests, but needs to
take 'rq_list_lock' when modifying the list.
Reported-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarHannes Reinecke <hare@suse.com>
Reviewed-by: default avatarJohannes Thumshirn <jthumshirn@suse.de>
Tested-by: default avatarJohannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: default avatarSasha Levin <alexander.levin@microsoft.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 9088ad9f
...@@ -535,6 +535,7 @@ sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos) ...@@ -535,6 +535,7 @@ sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
} else } else
count = (old_hdr->result == 0) ? 0 : -EIO; count = (old_hdr->result == 0) ? 0 : -EIO;
sg_finish_rem_req(srp); sg_finish_rem_req(srp);
sg_remove_request(sfp, srp);
retval = count; retval = count;
free_old_hdr: free_old_hdr:
kfree(old_hdr); kfree(old_hdr);
...@@ -575,6 +576,7 @@ sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp) ...@@ -575,6 +576,7 @@ sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp)
} }
err_out: err_out:
err2 = sg_finish_rem_req(srp); err2 = sg_finish_rem_req(srp);
sg_remove_request(sfp, srp);
return err ? : err2 ? : count; return err ? : err2 ? : count;
} }
...@@ -811,6 +813,7 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp, ...@@ -811,6 +813,7 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp,
SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp, SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
"sg_common_write: start_req err=%d\n", k)); "sg_common_write: start_req err=%d\n", k));
sg_finish_rem_req(srp); sg_finish_rem_req(srp);
sg_remove_request(sfp, srp);
return k; /* probably out of space --> ENOMEM */ return k; /* probably out of space --> ENOMEM */
} }
if (atomic_read(&sdp->detaching)) { if (atomic_read(&sdp->detaching)) {
...@@ -823,6 +826,7 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp, ...@@ -823,6 +826,7 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp,
} }
sg_finish_rem_req(srp); sg_finish_rem_req(srp);
sg_remove_request(sfp, srp);
return -ENODEV; return -ENODEV;
} }
...@@ -1312,6 +1316,7 @@ sg_rq_end_io_usercontext(struct work_struct *work) ...@@ -1312,6 +1316,7 @@ sg_rq_end_io_usercontext(struct work_struct *work)
struct sg_fd *sfp = srp->parentfp; struct sg_fd *sfp = srp->parentfp;
sg_finish_rem_req(srp); sg_finish_rem_req(srp);
sg_remove_request(sfp, srp);
kref_put(&sfp->f_ref, sg_remove_sfp); kref_put(&sfp->f_ref, sg_remove_sfp);
} }
...@@ -1856,8 +1861,6 @@ sg_finish_rem_req(Sg_request *srp) ...@@ -1856,8 +1861,6 @@ sg_finish_rem_req(Sg_request *srp)
else else
sg_remove_scat(sfp, req_schp); sg_remove_scat(sfp, req_schp);
sg_remove_request(sfp, srp);
return ret; return ret;
} }
...@@ -2204,12 +2207,17 @@ sg_remove_sfp_usercontext(struct work_struct *work) ...@@ -2204,12 +2207,17 @@ sg_remove_sfp_usercontext(struct work_struct *work)
struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work); struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
struct sg_device *sdp = sfp->parentdp; struct sg_device *sdp = sfp->parentdp;
Sg_request *srp; Sg_request *srp;
unsigned long iflags;
/* Cleanup any responses which were never read(). */ /* Cleanup any responses which were never read(). */
write_lock_irqsave(&sfp->rq_list_lock, iflags);
while (!list_empty(&sfp->rq_list)) { while (!list_empty(&sfp->rq_list)) {
srp = list_first_entry(&sfp->rq_list, Sg_request, entry); srp = list_first_entry(&sfp->rq_list, Sg_request, entry);
sg_finish_rem_req(srp); sg_finish_rem_req(srp);
list_del(&srp->entry);
srp->parentfp = NULL;
} }
write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
if (sfp->reserve.bufflen > 0) { if (sfp->reserve.bufflen > 0) {
SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp, SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment