s390/vfio-ccw stable fixes
v2 cover s390/vfio-ccw stable fixes
Hi,
The recent s390/vfio-ccw fixes applied to linux-6.6.y had a build error on the first patch, and some merge conflicts on three others. I fixed/resubmitted the other three yesterday, but neglected to notice the build error presented on the first patch.
Here are those all fixed together (I hope). Built on top of the current stable queue that includes the other fixes.
Eric Farman (4):
s390/vfio_ccw: Free all memory if cp_init() fails s390/vfio_ccw: Ensure first IDAW remains constant s390/vfio_ccw: Calculate idal length based on idaw type s390/vfio_ccw: Implement a crw lock
drivers/s390/cio/vfio_ccw_chp.c | 26 ++++++++++------- drivers/s390/cio/vfio_ccw_cp.c | 43 +++++++++++++++++++++++++---- drivers/s390/cio/vfio_ccw_cp.h | 2 ++ drivers/s390/cio/vfio_ccw_drv.c | 6 ++++ drivers/s390/cio/vfio_ccw_ops.c | 4 +++ drivers/s390/cio/vfio_ccw_private.h | 4 +++ 6 files changed, 69 insertions(+), 16 deletions(-)
Signature · 1 line
2.55.0
PATCH v2 1/4 s390/vfio_ccw: Free all memory if cp_init() fails
The routine cp_free() is called to unpin/free any memory once an I/O is completed successfully, or if cp_prefetch() fails. But if cp_init() fails, and cp->initialized is not enabled, the same routine cannot be used to free all the memory.
An attempt to address this exists in ccwchain_handle_ccw(), where a single call to ccwchain_free() is made for the currently-processed CCW segment. But this will leak other segments (created as a result of a Transfer in Channel) that had been allocated as part of the same channel program.
Address this by performing the cleanup outside of the recursive ccwchain_handle_ccw()/ccwchain_loop_tic() logic.
--- drivers/s390/cio/vfio_ccw_cp.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/s390/cio/vfio_ccw_cp.c +18 -4
@@ -450,9 +450,6 @@ static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)/* Loop for tics on this new chain. */ ret =ccwchain_loop_tic (chain, cp); -if (ret) -ccwchain_free (chain); -return ret; }
@@ -481,6 +478,23 @@ static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)return 0 ; } +static int ccwchain_build_ccws (u32 cda,struct channel_program *cp) +{ +struct ccwchain *chain, *temp; +int ret; + + ret =ccwchain_handle_ccw (cda, cp); + +if (ret) { +/* Cleanup if an error occurred */ +list_for_each_entry_safe (chain, temp, &cp->ccwchain_list, next) { +ccwchain_free (chain); + } + } + +return ret; +} +static int ccwchain_fetch_tic (struct ccw1 *ccw,struct channel_program *cp) {
@@ -728,7 +742,7 @@ int cp_init(struct channel_program *cp, union orb *orb)memcpy (&cp->orb, orb,sizeof (*orb));/* Build a ccwchain for the first CCW segment */ - ret =ccwchain_handle_ccw (orb->cmd.cpa, cp); + ret =ccwchain_build_ccws (orb->cmd.cpa, cp);if (!ret) cp->initialized = true;
Signature · 1 line
2.55.0
PATCH v2 2/4 s390/vfio_ccw: Ensure first IDAW remains constant
In reply to Eric FarmanThe first IDAW in a list does not need to be on a 2K/4K boundary like all others, and so is read separately to accurately calculate the size of the buffer needed to read the full IDAL.
Verify that the address found in the first IDAW is unchanged between reads, to ensure a consistent set of IDAWs being worked with.
--- drivers/s390/cio/vfio_ccw_cp.c | 16 ++++++++++++++++ drivers/s390/cio/vfio_ccw_cp.h | 2 ++ 2 files changed, 18 insertions(+)
drivers/s390/cio/vfio_ccw_cp.c +16 -0
@@ -521,6 +521,7 @@ static unsigned long *get_guest_idal(struct ccw1 *ccw, &container_of(cp, struct vfio_ccw_private, cp)->vdev;unsigned long *idaws;unsigned int *idaws_f1; +u64 first_idaw;int idal_len = idaw_nr *sizeof (*idaws);int idaw_size =idal_is_2k (cp) ?PAGE_SIZE /2 :PAGE_SIZE ;int idaw_mask = ~(idaw_size -1 );
@@ -537,6 +538,18 @@ static unsigned long *get_guest_idal(struct ccw1 *ccw,kfree (idaws);return ERR_PTR (ret); } + + idaws_f1 = (unsigned int *)idaws; +if (cp->orb.cmd.c64) + first_idaw = idaws[0 ]; +else + first_idaw = (unsigned long )(idaws_f1[0 ]); + +/* Unexpected mismatch from earlier read */ +if (first_idaw != cp->guest_iova) { +kfree (idaws); +return ERR_PTR (-EINVAL ); + } }else {/* Fabricate an IDAL based off CCW data address */ if (cp->orb.cmd.c64) {
@@ -598,6 +611,9 @@ static int ccw_count_idaws(struct ccw1 *ccw, iova = ccw->cda; } +/* Save the read address for later */ + cp->guest_iova = iova; +/* Format-1 IDAWs operate on 2K each */ if (!cp->orb.cmd.c64)return idal_2k_nr_words ((void *)iova, bytes);
drivers/s390/cio/vfio_ccw_cp.h +2 -0
@@ -35,6 +35,7 @@* @initialized: whether this instance is actually initialized * @guest_cp: copy of guest channel program * @ccwchain_count: number of channel program segments (linked by TIC) +* @guest_iova: first data address of a guest channel program * * @ccwchain_list is the head of a ccwchain list, that contents the * translated result of the guest channel program that pointed out by
@@ -46,6 +47,7 @@ struct channel_program {bool initialized;struct ccw1 *guest_cp;unsigned int ccwchain_count; +u64 guest_iova; };int cp_init (struct channel_program *cp,union orb *orb);
Signature · 1 line
2.55.0
PATCH v2 3/4 s390/vfio_ccw: Calculate idal length based on idaw type
In reply to Eric FarmanSashiko pointed out that get_guest_idal() unconditionally calculates the length of the IDAL presuming everything is a Format-2 IDAW.
The output of vfio-ccw is always Format-2, but the input can be either Format-1 (31-bit addresses) or Format-2 (64-bit addresses). As a result, the size of the guest IDAL may be incorrect and should be trimmed down.
--- drivers/s390/cio/vfio_ccw_cp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/s390/cio/vfio_ccw_cp.c +3 -2
@@ -233,6 +233,7 @@ static void convert_ccw0_to_ccw1(struct ccw1 *source, unsigned long len) }#define idal_is_2k (_cp) (!(_cp)->orb.cmd.c64 || (_cp)->orb.cmd.i2k) +#define get_idaw_size (_cp) ((_cp)->orb.cmd.c64 ? sizeof(u64) : sizeof(u32))/* * Helpers to operate ccwchain.
@@ -522,7 +523,7 @@ static unsigned long *get_guest_idal(struct ccw1 *ccw,unsigned long *idaws;unsigned int *idaws_f1;u64 first_idaw; -int idal_len = idaw_nr *sizeof (*idaws); +int idal_len = idaw_nr *get_idaw_size (cp);int idaw_size =idal_is_2k (cp) ?PAGE_SIZE /2 :PAGE_SIZE ;int idaw_mask = ~(idaw_size -1 );int i, ret;
@@ -587,7 +588,7 @@ static int ccw_count_idaws(struct ccw1 *ccw,struct vfio_device *vdev = &container_of(cp, struct vfio_ccw_private, cp)->vdev;u64 iova; -int size = cp->orb.cmd.c64 ?sizeof (u64) :sizeof (u32); +int size =get_idaw_size (cp);int ret;int bytes =1 ;
Signature · 1 line
2.55.0
PATCH v2 4/4 s390/vfio_ccw: Implement a crw lock
In reply to Eric FarmanUnlike the channel_program struct, which covers synchronous I/O submissions and asynchronous interrupts, the CRW region relies exclusively on asynchronous events coming from hardware.
Implement a lock to manage the list of those payloads, to ensure they are read cohesively.
--- drivers/s390/cio/vfio_ccw_chp.c | 26 ++++++++++++++++---------- drivers/s390/cio/vfio_ccw_drv.c | 6 ++++++ drivers/s390/cio/vfio_ccw_ops.c | 4 ++++ drivers/s390/cio/vfio_ccw_private.h | 4 ++++ 4 files changed, 30 insertions(+), 10 deletions(-)
drivers/s390/cio/vfio_ccw_chp.c +16 -10
@@ -93,18 +93,13 @@ static ssize_t vfio_ccw_crw_region_read(struct vfio_ccw_private *private,loff_t pos = *ppos &VFIO_CCW_OFFSET_MASK ;struct ccw_crw_region *region;struct vfio_ccw_crw *crw; +unsigned long flags;int ret;if (pos + count >sizeof (*region))return -EINVAL ;mutex_lock (&private->io_mutex); - crw =list_first_entry_or_null (&private->crw, - struct vfio_ccw_crw, next); - -if (crw) -list_del (&crw->next); -if (i >= private->num_regions) { ret = -EINVAL ; goto out;
@@ -113,6 +108,16 @@ static ssize_t vfio_ccw_crw_region_read(struct vfio_ccw_private *private, i =array_index_nospec (i, private->num_regions); region = private->region[i].data; +spin_lock_irqsave (&private->crw_lock, flags); + crw =list_first_entry_or_null (&private->crw, + struct vfio_ccw_crw, next); + +if (crw) +list_del (&crw->next); + +/* Drop CRW lock while copying to userspace */ +spin_unlock_irqrestore (&private->crw_lock, flags); +if (crw)memcpy (®ion->crw, &crw->crw,sizeof (region->crw));
@@ -122,15 +127,16 @@ static ssize_t vfio_ccw_crw_region_read(struct vfio_ccw_private *private, ret = count; region->crw =0 ; - -out: -mutex_unlock (&private->io_mutex); -kfree (crw);/* Notify the guest if more CRWs are on our queue */ +spin_lock_irqsave (&private->crw_lock, flags);if (!list_empty (&private->crw) && private->crw_trigger)eventfd_signal (private->crw_trigger,1 ); +spin_unlock_irqrestore (&private->crw_lock, flags); + +out: +mutex_unlock (&private->io_mutex);return ret; }
drivers/s390/cio/vfio_ccw_drv.c +6 -0
@@ -118,11 +118,14 @@ void vfio_ccw_sch_io_todo(struct work_struct *work)void vfio_ccw_crw_todo (struct work_struct *work) {struct vfio_ccw_private *private; +unsigned long flags; private = container_of(work, struct vfio_ccw_private, crw_work); +spin_lock_irqsave (&private->crw_lock, flags);if (!list_empty (&private->crw) && private->crw_trigger)eventfd_signal (private->crw_trigger,1 ); +spin_unlock_irqrestore (&private->crw_lock, flags); }void vfio_ccw_notoper_todo (struct work_struct *work)
@@ -286,6 +289,7 @@ static void vfio_ccw_queue_crw(struct vfio_ccw_private *private, unsigned int rsid) {struct vfio_ccw_crw *crw; +unsigned long flags;/* * If unable to allocate a CRW, just drop the event and
@@ -303,7 +307,9 @@ static void vfio_ccw_queue_crw(struct vfio_ccw_private *private, crw->crw.erc = erc; crw->crw.rsid = rsid; +spin_lock_irqsave (&private->crw_lock, flags);list_add_tail (&crw->next, &private->crw); +spin_unlock_irqrestore (&private->crw_lock, flags);queue_work (vfio_ccw_work_q, &private->crw_work); }
drivers/s390/cio/vfio_ccw_ops.c +4 -0
@@ -55,6 +55,7 @@ static int vfio_ccw_mdev_init_dev(struct vfio_device *vdev)INIT_WORK (&private->io_work, vfio_ccw_sch_io_todo);INIT_WORK (&private->crw_work, vfio_ccw_crw_todo);INIT_WORK (&private->notoper_work, vfio_ccw_notoper_todo); +spin_lock_init (&private->crw_lock); private->cp.guest_cp =kcalloc (CCWCHAIN_LEN_MAX ,sizeof (struct ccw1 ),GFP_KERNEL );
@@ -132,6 +133,7 @@ static void vfio_ccw_mdev_release_dev(struct vfio_device *vdev)struct vfio_ccw_private *private = container_of(vdev, struct vfio_ccw_private, vdev);struct vfio_ccw_crw *crw, *temp; +unsigned long flags;/* * Ensure these work items are fully drained, so none can
@@ -147,10 +149,12 @@ static void vfio_ccw_mdev_release_dev(struct vfio_device *vdev)cancel_work_sync (&private->crw_work);flush_work (&private->notoper_work); +spin_lock_irqsave (&private->crw_lock, flags);list_for_each_entry_safe (crw, temp, &private->crw, next) {list_del (&crw->next);kfree (crw); } +spin_unlock_irqrestore (&private->crw_lock, flags);kmem_cache_free (vfio_ccw_crw_region, private->crw_region);kmem_cache_free (vfio_ccw_schib_region, private->schib_region);
drivers/s390/cio/vfio_ccw_private.h +4 -0
@@ -98,6 +98,8 @@ struct vfio_ccw_parent {* @cp: channel program for the current I/O operation * @irb: irb info received from interrupt * @scsw: scsw info +* @crw_lock: serialization of CRW list information +* @crw: list of Channel Report Word elements * @io_trigger: eventfd ctx for signaling userspace I/O results * @crw_trigger: eventfd ctx for signaling userspace CRW information * @req_trigger: eventfd ctx for signaling userspace to return device
@@ -120,6 +122,8 @@ struct vfio_ccw_private {struct channel_program cp;struct irb irb;union scsw scsw; + +spinlock_t crw_lock;struct list_head crw;struct eventfd_ctx *io_trigger;
Signature · 1 line
2.55.0
Sasha Levin
maintainerQuoted 2 lines · Eric Farman
Here are those all fixed together (I hope). Built on top of the current stable queue that includes the other fixes.
All four are already in the 6.6 queue, so there was nothing to re-apply there.
While checking that, I noticed 74186c2968f8 ("s390/vfio_ccw: Free all memory if cp_init() fails") is missing from 6.1, 5.15 and 5.10 too, so I queued it there as well - spelled u32 instead of dma32_t, which does not exist on those trees. Thanks.
Signature · 2 lines
Thanks, Sasha
Quoted 6 lines · Sasha Levin
Quoted 2 lines · Eric Farman
Here are those all fixed together (I hope). Built on top of the current stable queue that includes the other fixes.
All four are already in the 6.6 queue, so there was nothing to re-apply there.
Excellent!
Quoted 6 lines
While checking that, I noticed
74186c2968f8("s390/vfio_ccw: Free all memory ifcp_init()fails") is missing from 6.1, 5.15 and 5.10 too, so I queued it there as well - spelled u32 instead ofdma32_t, which does not exist on those trees. Thanks.
Nice! That's been on my list (I started looking at 6.1 and then got sidetracked). Thank you!!