Re: [PATCH 7/7] KVM: selftests: Add "delete" testcase to set_memory_region_test

From: Peter Xu
Date: Mon Mar 23 2020 - 17:58:55 EST


On Mon, Mar 23, 2020 at 02:43:18PM -0700, Sean Christopherson wrote:
> On Mon, Mar 23, 2020 at 03:06:36PM -0400, Peter Xu wrote:
> > On Fri, Mar 20, 2020 at 01:55:46PM -0700, Sean Christopherson wrote:
> > > + /*
> > > + * Spin until the memory region is moved to a misaligned address. This
> > > + * may or may not trigger MMIO, as the window where the memslot is
> > > + * invalid is quite small.
> > > + */
> > > + val = guest_spin_on_val(0);
> > > + GUEST_ASSERT(val == 1 || val == MMIO_VAL);
> > > +
> > > + /* Spin until the memory region is realigned. */
> > > + GUEST_ASSERT(guest_spin_on_val(MMIO_VAL) == 1);
> >
> > IIUC ideally we should do GUEST_SYNC() after each GUEST_ASSERT() to
> > make sure the two threads are in sync. Otherwise e.g. there's no
> > guarantee that the main thread won't run too fast to quickly remove
> > the memslot and re-add it back before the guest_spin_on_val() starts
> > above, then the assert could trigger when it reads the value as zero.
>
> Hrm, I was thinking ucall wasn't available across pthreads, but it's just
> dumped into a global variable. I'll rework this to replace the udelay()
> hacks with proper synchronization.

I think ucall should work for pthread (shared address space of either
kvm_run or guest memories), however my thought was even simpler than
that, something like:

- in guest code: do GUEST_SYNC after each GUEST_ASSERT
- introduce a global_sem
- in vcpu thread: when receive GUEST_SYNC, do "sem_post(&global_sem)"
- in main thread: replace all usleep() with "sem_wait(&global_sem)"

--
Peter Xu