Skip to content

Commit 56d7488

Browse files
efarmangregkh
authored andcommitted
s390/vfio_ccw: Selectively expand io_mutex
[ Upstream commit 34f4fef ] The io_mutex was defined to serialize the io_regions, but then has also sort of been associated with the I/O themselves because of the close relationship they share. With the handful of races that are possible, the choices are either to: A) expand the scope of io_mutex to close these remaining windows, or B) reduce the scope of io_mutex to just io_region, and introduce a new lock mechanism for the remaining I/O resources This patch implements A, since B brings with it a lot more interactions that would need to be tracked and kept in a correct hierarchy. It also takes advantage of the workqueue element for cp_free() that now gets called out of fsm_notoper(), which could be invoked out of an interrupt context and thus cannot acquire a mutex itself. Fixes: 4f76617 ("vfio-ccw: protect the I/O region") Cc: stable@vger.kernel.org Signed-off-by: Eric Farman <farman@linux.ibm.com> Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com> Signed-off-by: Christian Borntraeger <borntraeger@linux.ibm.com> Stable-dep-of: 16b0798 ("s390/vfio_ccw: Implement a crw lock") Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c9b85aa commit 56d7488

5 files changed

Lines changed: 19 additions & 5 deletions

File tree

drivers/s390/cio/vfio_ccw_chp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ static ssize_t vfio_ccw_crw_region_read(struct vfio_ccw_private *private,
9797
if (pos + count > sizeof(*region))
9898
return -EINVAL;
9999

100+
mutex_lock(&private->io_mutex);
100101
crw = list_first_entry_or_null(&private->crw,
101102
struct vfio_ccw_crw, next);
102103

103104
if (crw)
104105
list_del(&crw->next);
105106

106-
mutex_lock(&private->io_mutex);
107107
if (i >= private->num_regions) {
108108
ret = -EINVAL;
109109
goto out;

drivers/s390/cio/vfio_ccw_cp.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,17 +904,23 @@ void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
904904
*/
905905
bool cp_iova_pinned(struct channel_program *cp, u64 iova, u64 length)
906906
{
907+
struct vfio_ccw_private *private =
908+
container_of(cp, struct vfio_ccw_private, cp);
907909
struct ccwchain *chain;
908910
int i;
909911

910912
if (!cp->initialized)
911913
return false;
912914

915+
mutex_lock(&private->io_mutex);
913916
list_for_each_entry(chain, &cp->ccwchain_list, next) {
914917
for (i = 0; i < chain->ch_len; i++)
915-
if (page_array_iova_pinned(chain->ch_pa + i, iova, length))
918+
if (page_array_iova_pinned(chain->ch_pa + i, iova, length)) {
919+
mutex_unlock(&private->io_mutex);
916920
return true;
921+
}
917922
}
923+
mutex_unlock(&private->io_mutex);
918924

919925
return false;
920926
}

drivers/s390/cio/vfio_ccw_drv.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,15 @@ static void vfio_ccw_sch_io_todo(struct work_struct *work)
8282

8383
is_final = !(scsw_actl(&irb->scsw) &
8484
(SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT));
85+
mutex_lock(&private->io_mutex);
8586
if (scsw_is_solicited(&irb->scsw)) {
8687
cp_update_scsw(&private->cp, &irb->scsw);
8788
if (is_final && private->state == VFIO_CCW_STATE_CP_PENDING) {
8889
cp_free(&private->cp);
8990
cp_is_finished = true;
9091
}
9192
}
92-
mutex_lock(&private->io_mutex);
9393
memcpy(private->io_region->irb_area, irb, sizeof(*irb));
94-
mutex_unlock(&private->io_mutex);
9594

9695
/*
9796
* Reset to IDLE only if processing of a channel program
@@ -101,6 +100,7 @@ static void vfio_ccw_sch_io_todo(struct work_struct *work)
101100
*/
102101
if (cp_is_finished)
103102
private->state = VFIO_CCW_STATE_IDLE;
103+
mutex_unlock(&private->io_mutex);
104104

105105
if (private->io_trigger)
106106
eventfd_signal(private->io_trigger, 1);
@@ -122,7 +122,9 @@ static void vfio_ccw_notoper_todo(struct work_struct *work)
122122

123123
private = container_of(work, struct vfio_ccw_private, notoper_work);
124124

125+
mutex_lock(&private->io_mutex);
125126
cp_free(&private->cp);
127+
mutex_unlock(&private->io_mutex);
126128
}
127129

128130
/*

drivers/s390/cio/vfio_ccw_fsm.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ static void fsm_notoper(struct vfio_ccw_private *private,
176176
css_sched_sch_todo(sch, SCH_TODO_UNREG);
177177
private->state = VFIO_CCW_STATE_NOT_OPER;
178178

179+
/* This routine could be called from IRQ context, so defer */
179180
queue_work(vfio_ccw_work_q, &private->notoper_work);
180181
}
181182

@@ -412,7 +413,11 @@ static void fsm_close(struct vfio_ccw_private *private,
412413

413414
private->state = VFIO_CCW_STATE_STANDBY;
414415
spin_unlock_irq(sch->lock);
416+
417+
mutex_lock(&private->io_mutex);
415418
cp_free(&private->cp);
419+
mutex_unlock(&private->io_mutex);
420+
416421
return;
417422

418423
err_unlock:

drivers/s390/cio/vfio_ccw_private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ struct vfio_ccw_crw {
7474
* @state: internal state of the device
7575
* @completion: synchronization helper of the I/O completion
7676
* @io_region: MMIO region to input/output I/O arguments/results
77-
* @io_mutex: protect against concurrent update of I/O regions
77+
* @io_mutex: protect against concurrent update of I/O resources
78+
* and @cp lifecycle
7879
* @region: additional regions for other subchannel operations
7980
* @cmd_region: MMIO region for asynchronous I/O commands other than START
8081
* @schib_region: MMIO region for SCHIB information

0 commit comments

Comments
 (0)