Skip to content

Commit 17e01e3

Browse files
efarmangregkh
authored andcommitted
s390/vfio_ccw: Free all memory if cp_init() fails
[ Upstream commit 74186c2 ] 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. Fixes: 8b515be ("vfio-ccw: Fix memory leak and don't call cp_free in cp_init") Cc: stable@vger.kernel.org Reviewed-by: Farhan Ali <alifm@linux.ibm.com> Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com> Signed-off-by: Eric Farman <farman@linux.ibm.com> Signed-off-by: Christian Borntraeger <borntraeger@linux.ibm.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f833821 commit 17e01e3

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

drivers/s390/cio/vfio_ccw_cp.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,6 @@ static int ccwchain_handle_ccw(u32 cda, struct channel_program *cp)
490490
/* Loop for tics on this new chain. */
491491
ret = ccwchain_loop_tic(chain, cp);
492492

493-
if (ret)
494-
ccwchain_free(chain);
495-
496493
return ret;
497494
}
498495

@@ -521,6 +518,23 @@ static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
521518
return 0;
522519
}
523520

521+
static int ccwchain_build_ccws(u32 cda, struct channel_program *cp)
522+
{
523+
struct ccwchain *chain, *temp;
524+
int ret;
525+
526+
ret = ccwchain_handle_ccw(cda, cp);
527+
528+
if (ret) {
529+
/* Cleanup if an error occurred */
530+
list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
531+
ccwchain_free(chain);
532+
}
533+
}
534+
535+
return ret;
536+
}
537+
524538
static int ccwchain_fetch_tic(struct ccwchain *chain,
525539
int idx,
526540
struct channel_program *cp)
@@ -699,7 +713,7 @@ int cp_init(struct channel_program *cp, union orb *orb)
699713
memcpy(&cp->orb, orb, sizeof(*orb));
700714

701715
/* Build a ccwchain for the first CCW segment */
702-
ret = ccwchain_handle_ccw(orb->cmd.cpa, cp);
716+
ret = ccwchain_build_ccws(orb->cmd.cpa, cp);
703717

704718
if (!ret) {
705719
cp->initialized = true;

0 commit comments

Comments
 (0)