All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Claudio Imbrenda" <imbrenda@linux.ibm.com>
Cc: "Alexander Gordeev" <agordeev@linux.ibm.com>,
	linux-s390@vger.kernel.org, kvm@vger.kernel.org,
	"Heiko Carstens" <hca@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>
Subject: Re: [GIT PULL v1 07/23] s390/vfio_ccw: Cancel existing workqueues
Date: Mon, 03 Aug 2026 16:41:35 +0000	[thread overview]
Message-ID: <20260803164136.CA5371F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260803160924.236807-8-imbrenda@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Pre-existing issues:
- [High] Double-free and use-after-free of channel program structures due to concurrent execution of `cp_free()`.
- [High] Use-after-free of `vfio_ccw_private` due to concurrent execution of `vfio_ccw_mdev_remove()` and `vfio_ccw_chp_event()`.
- [High] Data race and linked list corruption of `private->crw`.
- [High] Use-after-free of `eventfd_ctx` due to lack of synchronization in `vfio_ccw_mdev_set_irqs()`.
--

--- Patch [7]: [GIT PULL v1 07/23] s390/vfio_ccw: Cancel existing workqueues ---
commit 9c72e034f4d8d6193d6ae49cecd7dc50eda41903
Author: Eric Farman <farman@linux.ibm.com>
Subject: s390/vfio_ccw: Cancel existing workqueues

The initialization of the io_work and crw_work workqueues begs the
question of whether they should be un-initialized. Add the corresponding
cleanup tags in _release_dev to ensure work isn't dispatched after
the private struct is free'd.

> diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
> index 032a1cdf4df70..d361d1fde3a0d 100644
> --- a/drivers/s390/cio/vfio_ccw_ops.c
> +++ b/drivers/s390/cio/vfio_ccw_ops.c
> @@ -131,6 +131,13 @@ static void vfio_ccw_mdev_release_dev(struct vfio_device *vdev)
>  		container_of(vdev, struct vfio_ccw_private, vdev);
>  	struct vfio_ccw_crw *crw, *temp;
>  
> +	/*
> +	 * Ensure these work items are fully drained, so none can
> +	 * fire after being released.
> +	 */
> +	cancel_work_sync(&private->io_work);
> +	cancel_work_sync(&private->crw_work);

[Severity: High]
This is a pre-existing issue, but does this cancellation prevent subsequent
queuing if vfio_ccw_chp_event() races with vfio_ccw_mdev_remove()?

vfio_ccw_chp_event() reads private locklessly before it is cleared and
prepares to queue work:

drivers/s390/cio/vfio_ccw_drv.c:vfio_ccw_chp_event() {
    struct vfio_ccw_private *private = dev_get_drvdata(&parent->dev);
    ...
        vfio_ccw_queue_crw(private, CRW_RSC_CPATH, CRW_ERC_PERRN,
                           link->chpid.id);
}

If vfio_ccw_mdev_remove() concurrently clears the driver data, releases the
device, and drains the currently empty workqueue via
cancel_work_sync(&private->crw_work), can vfio_ccw_chp_event() subsequently
queue work on the freed private object, leading to a use-after-free when
vfio_ccw_crw_todo() executes?

>  	list_for_each_entry_safe(crw, temp, &private->crw, next) {

[Severity: High]
This is a pre-existing issue, but is the private->crw linked list protected
against concurrent modifications here?

vfio_ccw_queue_crw() appends elements to the list with list_add_tail() while
holding sch->lock. Concurrently, userspace triggering a read on the CRW
region invokes vfio_ccw_crw_region_read(), which locklessly calls
list_first_entry_or_null() and list_del() before acquiring
private->io_mutex:

drivers/s390/cio/vfio_ccw_chp.c:vfio_ccw_crw_region_read() {
    ...
    crw = list_first_entry_or_null(&private->crw,
                                   struct vfio_ccw_crw, next);
    if (crw)
        list_del(&crw->next);
    mutex_lock(&private->io_mutex);
}

Can multiple concurrent reads, or a read concurrent with an asynchronous
channel path event, corrupt the list pointers?

>  		list_del(&crw->next);
>  		kfree(crw);
>  	}

[ ... ]

> @@ -202,6 +209,14 @@ static void vfio_ccw_mdev_close_device(struct vfio_device *vdev)
>  		container_of(vdev, struct vfio_ccw_private, vdev);
>  
>  	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
> +
> +	/*
> +	 * Ensure these work items are drained, in the event the
> +	 * device is re-opened instead of released.
> +	 */
> +	cancel_work_sync(&private->io_work);

[Severity: High]
This is a pre-existing issue, but does placing cancel_work_sync() after
vfio_ccw_fsm_event() leave a double-free race intact?

The device is closed via vfio_ccw_mdev_close_device(), which triggers the
FSM CLOSE event and calls fsm_close(). fsm_close() invokes cp_free()
entirely locklessly:

drivers/s390/cio/vfio_ccw_fsm.c:fsm_close() {
    ...
    spin_unlock_irq(&sch->lock);
    cp_free(&private->cp);
}

Concurrently, if the io_work workqueue is already running
vfio_ccw_sch_io_todo(), it locklessly evaluates
private->state == VFIO_CCW_STATE_CP_PENDING and also calls cp_free():

drivers/s390/cio/vfio_ccw_drv.c:vfio_ccw_sch_io_todo() {
    ...
    if (is_final && private->state == VFIO_CCW_STATE_CP_PENDING) {
        cp_free(&private->cp);
    ...
}

Since the new cancel_work_sync(&private->io_work) executes after the FSM
event, can io_work still execute concurrently with the FSM close handler
and double-free the channel program structures?

[Severity: High]
This isn't a bug introduced by this patch, but does the lack of
synchronization in vfio_ccw_mdev_set_irqs() allow a use-after-free of the
eventfd_ctx object?

In vfio_ccw_mdev_set_irqs(), userspace can unregister an eventfd, executing
eventfd_ctx_put(*ctx):

drivers/s390/cio/vfio_ccw_ops.c:vfio_ccw_mdev_set_irqs() {
    ...
        if (fd == -1) {
            if (*ctx)
                eventfd_ctx_put(*ctx);
            *ctx = NULL;
        }
}

There appear to be no locks preventing the workqueue from concurrently
reading private->io_trigger. If vfio_ccw_sch_io_todo() reads the pointer
locklessly:

drivers/s390/cio/vfio_ccw_drv.c:vfio_ccw_sch_io_todo() {
    ...
    if (private->io_trigger)
        eventfd_signal(private->io_trigger);
}

Can the workqueue execute eventfd_signal() on freed memory if
vfio_ccw_mdev_set_irqs() frees the context after it was read but before it
is signaled?

> +	cancel_work_sync(&private->crw_work);
> +
>  	vfio_ccw_unregister_dev_regions(private);
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260803160924.236807-1-imbrenda@linux.ibm.com?part=7

  reply	other threads:[~2026-08-03 16:41 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03 16:09 [GIT PULL v1 00/23] KVM: s390: Misc KVM and vfio-ccw fixes Claudio Imbrenda
2026-08-03 16:09 ` [GIT PULL v1 01/23] s390/vfio_ccw: Free all memory if cp_init() fails Claudio Imbrenda
2026-08-03 16:28   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 02/23] s390/vfio_ccw: Limit the number of channel program segments Claudio Imbrenda
2026-08-03 16:32   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 03/23] s390/vfio_ccw: Fix out of bounds check on CCW array Claudio Imbrenda
2026-08-03 16:17   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 04/23] s390/vfio_ccw: Ensure first IDAW remains constant Claudio Imbrenda
2026-08-03 16:24   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 05/23] s390/vfio_ccw: Calculate idal length based on idaw type Claudio Imbrenda
2026-08-03 16:24   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 06/23] s390/vfio_ccw: Ensure index for read/write regions are within range Claudio Imbrenda
2026-08-03 16:34   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 07/23] s390/vfio_ccw: Cancel existing workqueues Claudio Imbrenda
2026-08-03 16:41   ` sashiko-bot [this message]
2026-08-03 16:09 ` [GIT PULL v1 08/23] s390/vfio_ccw: Move cp cleanup out of not operational Claudio Imbrenda
2026-08-03 16:39   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 09/23] s390/vfio_ccw: Selectively expand io_mutex Claudio Imbrenda
2026-08-03 16:54   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 10/23] s390/vfio_ccw: Implement a crw lock Claudio Imbrenda
2026-08-03 16:51   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 11/23] KVM: s390: Fix unlikely NULL gmap dereference Claudio Imbrenda
2026-08-03 16:43   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 12/23] KVM: s390: Do not free SCA if it was not allocated Claudio Imbrenda
2026-08-03 16:49   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 13/23] KVM: s390: Fix kvm_s390_vcpu_unsetup_cmma() Claudio Imbrenda
2026-08-03 16:54   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 14/23] KVM: s390: Fix overclearing ESCA in case of error Claudio Imbrenda
2026-08-03 17:03   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 15/23] KVM: s390: ucontrol: Fix sca_clear_ext_call() Claudio Imbrenda
2026-08-03 17:09   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 16/23] KVM: s390: Fix leaking of PGM_ADDRESSING to userspace Claudio Imbrenda
2026-08-03 17:14   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 17/23] KVM: s390: Fix race in __do_essa() Claudio Imbrenda
2026-08-03 17:04   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 18/23] KVM: s390: cmma: Fix dirty tracking when removing memslot Claudio Imbrenda
2026-08-03 17:12   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 19/23] KVM: s390: ucontrol: Add missing locking around gmap_remove_child() Claudio Imbrenda
2026-08-03 17:19   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 20/23] KVM: s390: Free the mmu cache when kvm_arch_vcpu_create() fails Claudio Imbrenda
2026-08-03 17:13   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 21/23] KVM: s390: Return -EINTR if a signal is pending while faulting-in Claudio Imbrenda
2026-08-03 17:40   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 22/23] KVM: s390: Fix ordering when adding to SCA Claudio Imbrenda
2026-08-03 17:26   ` sashiko-bot
2026-08-03 16:09 ` [GIT PULL v1 23/23] KVM: s390: Fix cleanup in kvm_s390_pv_create_cpu() Claudio Imbrenda
2026-08-03 17:19   ` sashiko-bot
2026-08-06 13:48 ` [GIT PULL v1 00/23] KVM: s390: Misc KVM and vfio-ccw fixes Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260803164136.CA5371F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.