Re: [PATCH 2/7] KVM: Add KVM_MAP_MEMORY vcpu ioctl to pre-populate guest memory

From: Sean Christopherson
Date: Wed Apr 17 2024 - 17:07:54 EST


On Wed, Apr 17, 2024, Isaku Yamahata wrote:
> > + vcpu_load(vcpu);
> > + idx = srcu_read_lock(&vcpu->kvm->srcu);
> > +
> > + r = 0;
> > + full_size = mapping->size;
> > + while (mapping->size) {

Maybe pre-check !mapping->size? E.g. there's no reason to load the vCPU and
acquire SRCU just to do nothing. Then this can be a do-while loop and doesn't
need to explicitly initialize 'r'.

> > + if (signal_pending(current)) {
> > + r = -EINTR;
> > + break;
> > + }
> > +
> > + r = kvm_arch_vcpu_map_memory(vcpu, mapping);

Requiring arch code to address @mapping is cumbersone. If the arch call returns
a long, then can't we do?

if (r < 0)
break;

mapping->size -= r;
mapping->gpa += r;

> > + if (r)
> > + break;
> > +
> > + cond_resched();
> > + }
> > +
> > + srcu_read_unlock(&vcpu->kvm->srcu, idx);
> > + vcpu_put(vcpu);
> > +
> > + /* Return success if at least one page was mapped successfully. */
> > + return full_size == mapping->size ? r : 0;

I just saw Paolo's update that this is intentional, but this strikes me as odd,
as it requires userspace to redo the ioctl() to figure out why the last one failed.

Not a sticking point, just odd to my eyes.