make better use of domain_vcpu()
cover make better use of domain_vcpu()
I.e. reduce the number of instances where we access d->vcpu[] directly. The series doesn't cover all cases, just a fair part. I think there are no inter-dependencies between the individual patches.
01: x86/gdbsx: don't blindly assume vCPU 0 exists
02: x86/passthrough: don't blindly assume vCPU-s exist
03: x86/altp2m: don't blindly assume vCPU-s exist
04: domctl: use domain_vcpu() in XEN_DOMCTL_{g,s}etvcpucontext handling 05: vm-event: use domain_vcpu() in domctl handling
06: numa: use domain_vcpu() in domain_to_node()
07: x86/time: use domain_vcpu() in tsc_set_info()
08: x86/paging: use domain_vcpu() in paging_domctl()
09: x86/dm: use domain_vcpu()
10: x86/HVM/MTRR: use domain_vcpu()
11: x86/HVM: introduce hvm_load_vcpu_instance()
12: x86/vPMU: use domain_vcpu()
13: x86/domctl: use domain_vcpu() to handle various sub-ops
Jan
PATCH 1/13 x86/gdbsx: don't blindly assume vCPU 0 exists
For the PV case, pass domain_vcpu()'s result into the low-level function, and refuse the lookup there when pgd3val wasn't provided and the vCPU doesn't exist. Additionally also check whether CR3 was ever set.
For the HVM case, simply use domain_vcpu() in the low-level helper and bail if the result is NULL.
xen/arch/x86/gdbsx.c +15 -5
@@ -30,11 +30,15 @@ typedef unsigned char dbgbyte_t;static mfn_t dbg_hvm_va2mfn (dbgva_t vaddr,struct domain *dp,int toaddr,gfn_t *gfn) { +struct vcpu *v =domain_vcpu (dp,0 );mfn_t mfn;uint32_t pfec = PFEC_page_present;p2m_type_t gfntype; - *gfn =_gfn (paging_gva_to_gfn (dp->vcpu[0 ], vaddr, &pfec)); +if ( !v ) +return INVALID_MFN ; + + *gfn =_gfn (paging_gva_to_gfn (v, vaddr, &pfec));if (gfn_eq (*gfn,INVALID_GFN ) )return INVALID_MFN ;
@@ -65,17 +69,21 @@ dbg_hvm_va2mfn(dbgva_t vaddr, struct dom* Returns: mfn for the given (pv guest) vaddr */ static mfn_t -dbg_pv_va2mfn (dbgva_t vaddr,struct domain *dp,uint64_t pgd3val) +dbg_pv_va2mfn (dbgva_t vaddr,const struct vcpu *v,uint64_t pgd3val) {l4_pgentry_t l4e, *l4t;l3_pgentry_t l3e, *l3t;l2_pgentry_t l2e, *l2t;l1_pgentry_t l1e, *l1t; -unsigned long cr3 = (pgd3val ? pgd3val : dp->vcpu[0 ]->arch.cr3); -mfn_t mfn =maddr_to_mfn (cr3_pa (cr3)); +mfn_t mfn;if ( pgd3val ==0 ) { +if ( !v || !v->arch.cr3 ) +return INVALID_MFN ; + + mfn =maddr_to_mfn (cr3_pa (v->arch.cr3)); + l4t =map_domain_page (mfn); l4e = l4t[l4_table_offset (vaddr)];unmap_domain_page (l4t);
@@ -91,6 +99,8 @@ dbg_pv_va2mfn(dbgva_t vaddr, struct doma (l3e_get_flags(l3e) & _PAGE_PSE) )return INVALID_MFN ; } +else + mfn =maddr_to_mfn (cr3_pa (pgd3val)); l2t =map_domain_page (mfn); l2e = l2t[l2_table_offset (vaddr)];
@@ -125,7 +135,7 @@ static unsigned int dbg_rw_guest_mem(str mfn = (is_hvm_domain (dp) ?dbg_hvm_va2mfn (addr, dp, toaddr, &gfn) - :dbg_pv_va2mfn (addr, dp, pgd3)); + :dbg_pv_va2mfn (addr,domain_vcpu (dp,0 ), pgd3));if (mfn_eq (mfn,INVALID_MFN ) )break ;
PATCH 2/13 x86/passthrough: don't blindly assume vCPU-s exist
In reply to Jan BeulichFirst, pirq_guest_bind() really only needs a vCPU to establish IRQ affinity, and it already has fallback logic which can be used if the vCPU passed in is NULL. Have the caller pass in a (guaranteed non-NULL) domain pointer, making the vCPU one optional.
While altering pirq_guest_bind()'s signature also convert its boolean parameter to bool.
Further, while generally hvm_girq_dest_2_vcpu_id() would return only IDs of valid vCPU-s, for a single-vCPU guest it would return 0 no matter whether that vCPU was created already. Use domain_vcpu() when consuming the return value; subsequent code already deals with the pointer being NULL.
---
I'll fill the missing Fixes: when xenbits is willing to work again.
xen/arch/x86/irq.c +11 -10
@@ -1627,14 +1627,15 @@ int pirq_shared(struct domain *d, int pireturn shared; } -int pirq_guest_bind (struct vcpu *v,struct pirq *pirq,int will_share) +int pirq_guest_bind (struct domain *d,const struct vcpu *v,struct pirq *pirq, +bool will_share) {struct irq_desc *desc;irq_guest_action_t *action, *newaction =NULL ;unsigned int max_nr_guests = will_share ? irq_max_guests :1 ;int rc =0 ; -WARN_ON (!rw_is_write_locked (&v->domain->event_lock)); +WARN_ON (!rw_is_write_locked (&d->event_lock));BUG_ON (!local_irq_is_enabled ()); retry:
@@ -1651,7 +1652,7 @@ int pirq_guest_bind(struct vcpu *v, stru {printk (XENLOG_G_INFO "Cannot bind IRQ%d to dom%d. In use by '%s'.\n" , - pirq->pirq, v->domain->domain_id, desc->action->name); + pirq->pirq, d->domain_id, desc->action->name); rc = -EBUSY ; goto unlock_out; }
@@ -1666,7 +1667,7 @@ int pirq_guest_bind(struct vcpu *v, struxfree (newaction);printk (XENLOG_G_INFO "Cannot bind IRQ%d to dom%d. Out of memory.\n" , - pirq->pirq, v->domain->domain_id); + pirq->pirq, d->domain_id);return -ENOMEM ; }
@@ -1690,7 +1691,7 @@ int pirq_guest_bind(struct vcpu *v, stru {const cpumask_t *affinity =NULL ; -if ( !opt_noirqbalance ) +if ( !opt_noirqbalance && v ) affinity =cpumask_of (v->processor);else if ( !cpumask_intersects (desc->affinity, &cpu_online_map) ) {
@@ -1710,7 +1711,7 @@ int pirq_guest_bind(struct vcpu *v, struelse if ( !will_share || !action->shareable ) {printk (XENLOG_G_INFO "Cannot bind IRQ%d to dom%d. %s.\n" , - pirq->pirq, v->domain->domain_id, + pirq->pirq, d->domain_id, will_share ?"Others do not share" :"Will not share with others" ); rc = -EBUSY ;
@@ -1734,17 +1735,17 @@ int pirq_guest_bind(struct vcpu *v, struprintk (XENLOG_G_INFO "Cannot bind IRQ%d to %pd: already at max share %u" " (increase with irq-max-guests= option)\n" , - pirq->pirq, v->domain, irq_max_guests); + pirq->pirq, d, irq_max_guests); rc = -EBUSY ; goto unlock_out; } - action->guest[action->nr_guests++] = v->domain; + action->guest[action->nr_guests++] = d;if ( action->ack_type !=ACKTYPE_NONE ) -set_pirq_eoi (v->domain, pirq->pirq); +set_pirq_eoi (d, pirq->pirq);else -clear_pirq_eoi (v->domain, pirq->pirq); +clear_pirq_eoi (d, pirq->pirq); unlock_out:spin_unlock_irq (&desc->lock);
xen/common/event_channel.c +1 -1
@@ -718,7 +718,7 @@ static int evtchn_bind_pirq(evtchn_bind_ info->evtchn = port; rc = (!is_hvm_domain (d) - ?pirq_guest_bind (v, info, + ?pirq_guest_bind (d, v, info, !!(bind->flags &BIND_PIRQ__WILL_SHARE )) :0 );if ( rc !=0 )
xen/drivers/passthrough/x86/hvm.c +3 -3
@@ -315,7 +315,7 @@ int pt_irq_create_bind(*/ pirq_dpci->dom = d;/* bind after hvm_irq_dpci is setup to avoid race with irq handler*/ - rc =pirq_guest_bind (d->vcpu[0 ], info,0 ); + rc =pirq_guest_bind (d, d->vcpu[0 ], info,0 );if ( rc ==0 && pt_irq_bind->u.msi.gtable ) { rc =msixtbl_pt_register (d, info, pt_irq_bind->u.msi.gtable);
@@ -375,7 +375,7 @@ int pt_irq_create_bind( pirq_dpci->gmsi.dest_vcpu_id = dest_vcpu_id; pirq_dpci->gmsi.posted = false; - vcpu = (dest_vcpu_id >=0 ) ? d->vcpu[dest_vcpu_id] :NULL ; + vcpu = (dest_vcpu_id >=0 ) ?domain_vcpu (d, dest_vcpu_id) :NULL ;if ( iommu_intpost ) {if ( delivery_mode == dest_LowestPrio )
@@ -527,7 +527,7 @@ int pt_irq_create_bind( }/* Deal with gsi for legacy devices */ - rc =pirq_guest_bind (d->vcpu[0 ], info, share); + rc =pirq_guest_bind (d, d->vcpu[0 ], info, share);if (unlikely (rc) ) {/*
xen/include/xen/irq.h +2 -1
@@ -184,7 +184,8 @@ void pirq_cleanup_check(struct pirq *pirextern void pirq_guest_eoi (struct pirq *pirq);extern void desc_guest_eoi (struct irq_desc *desc,struct pirq *pirq);extern int pirq_guest_unmask (struct domain *d); -extern int pirq_guest_bind (struct vcpu *v,struct pirq *pirq,int will_share); +extern int pirq_guest_bind (struct domain *d,const struct vcpu *v, +struct pirq *pirq,bool will_share);extern void pirq_guest_unbind (struct domain *d,struct pirq *pirq);extern void pirq_set_affinity (struct domain *d,int pirq,const cpumask_t *mask);
PATCH 3/13 x86/altp2m: don't blindly assume vCPU-s exist
In reply to Jan BeulichChecking hypercall inputs against just d->max_vcpus doesn't suffice, as the hypercall may be issued ahead of vCPU-s being created. Use domain_vcpu() to also cover other eventualities.
xen/arch/x86/hvm/hvm.c +4 -9
@@ -4697,10 +4697,9 @@ static int do_altp2m_op(case HVMOP_altp2m_vcpu_enable_notify: { -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, a.u.enable_notify.vcpu_id); -if ( a.u.enable_notify.pad || - a.u.enable_notify.vcpu_id >= d->max_vcpus ) +if ( a.u.enable_notify.pad || !v ) { rc = -EINVAL ;break ;
@@ -4712,17 +4711,15 @@ static int do_altp2m_op(break ; } - v = d->vcpu[a.u.enable_notify.vcpu_id]; - rc =altp2m_vcpu_enable_ve (v,_gfn (a.u.enable_notify.gfn));break ; }case HVMOP_altp2m_vcpu_disable_notify: { -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, a.u.enable_notify.vcpu_id); -if ( a.u.disable_notify.vcpu_id >= d->max_vcpus ) +if ( !v ) { rc = -EINVAL ;break ;
@@ -4734,8 +4731,6 @@ static int do_altp2m_op(break ; } - v = d->vcpu[a.u.enable_notify.vcpu_id]; -altp2m_vcpu_disable_ve (v);break ; }
Re: [PATCH 03/13] x86/altp2m: don't blindly assume vCPU-s exist
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4722
Quoted 8 lines · Jan Beulich
[...]
case HVMOP_altp2m_vcpu_disable_notify: {xen/arch/x86/hvm/hvm.c +2
-2-struct vcpu *v; +struct vcpu *v =domain_vcpu (d, a.u.enable_notify.vcpu_id); -if ( a.u.disable_notify.vcpu_id >= d->max_vcpus ) +if ( !v )
I think this should be a.u.disable_notify.vcpu_id, probably a copy and paste accident.
With that changed:
Stephen
Re: [PATCH 03/13] x86/altp2m: don't blindly assume vCPU-s exist
Quoted 7 lines · Stephen Cheng
Quoted 6 lines · Jan Beulich
[...]
case HVMOP_altp2m_vcpu_disable_notify: {(quoted diff) +1
-1- struct vcpu *v; + struct vcpu *v = domain_vcpu(d, a.u.enable_notify.vcpu_id);
Quoted 5 lines
xen/arch/x86/hvm/hvm.c static int do_altp2m_op( · line 4722
Quoted 2 lines
xen/arch/x86/hvm/hvm.c +1
-1-if ( a.u.disable_notify.vcpu_id >= d->max_vcpus ) +if ( !v )I think this should be a.u.disable_notify.vcpu_id, probably a copy and paste accident.
Not quite. You zapped the other hunk following this one:
@@ -4749,8 +4746,6 @@ static int do_altp2m_op(
break;
}- v = d->vcpu[a.u.enable_notify.vcpu_id];
-
altp2m_vcpu_disable_ve(v);
break;
}I did actually _move_ its array index to the function invocation. And while doing so I did notice it's "enable" there, but (not looking at the other field use) implied that "enable" and "disable" use the same struct.
Quoted 2 lines
With that changed:
Reviewed-by Stephen Cheng <[email protected]>
Thanks. I've also amended the description accordingly.
Jan
PATCH 4/13 domctl: use domain_vcpu() in XEN_DOMCTL_{g,s}etvcpucontext handling
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/common/domctl.c +4 -8
@@ -606,12 +606,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xecase XEN_DOMCTL_setvcpucontext: {vcpu_guest_context_u c = { .nat =NULL }; -unsigned int vcpu = op->u.vcpucontext.vcpu; -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, op->u.vcpucontext.vcpu); ret = -EINVAL ; -if ( (d == current->domain) ||/* no domain_pause() */ - (vcpu >= d->max_vcpus) || ((v = d->vcpu[vcpu]) ==NULL ) ) +if ( !v || d == current->domain )/* no domain_pause() */ break ;if (guest_handle_is_null (op->u.vcpucontext.ctxt) )
@@ -792,12 +790,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xecase XEN_DOMCTL_getvcpucontext: {vcpu_guest_context_u c = { .nat =NULL }; -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, op->u.vcpucontext.vcpu); ret = -EINVAL ; -if ( op->u.vcpucontext.vcpu >= d->max_vcpus || - (v = d->vcpu[op->u.vcpucontext.vcpu]) ==NULL || - v == current )/* no vcpu_pause() */ +if ( !v || v == current )/* no vcpu_pause() */ goto getvcpucontext_out; ret = -ENODATA ;
PATCH 5/13 vm-event: use domain_vcpu() in domctl handling
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/common/vm_event.c +1 -1
@@ -622,7 +622,7 @@ int vm_event_domctl(struct domain *d, streturn 0 ; } -if (unlikely (d->vcpu ==NULL ) ||unlikely (d->vcpu[0 ] ==NULL ) ) +if (unlikely (!domain_vcpu (d,0 )) ) { gdprintk(XENLOG_INFO, "Memory event op on a domain (%u) with no vcpus\n",
Re: [PATCH 05/13] vm-event: use domain_vcpu() in domctl handling
Quoted 7 lines · Jan Beulich
If for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
Signed-off-by Jan Beulich <[email protected]>
PATCH 6/13 numa: use domain_vcpu() in domain_to_node()
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/include/xen/numa.h +4 -3
@@ -23,9 +23,10 @@ typedef uint8_t nodeid_t;#define vcpu_to_node (v) (cpu_to_node((v)->processor)) -#define domain_to_node (d) \ - (((d)->vcpu != NULL && (d)->vcpu[0] != NULL) \ - ? vcpu_to_node((d)->vcpu[0]) : NUMA_NO_NODE) +#define domain_to_node (d) ({ \ + const struct vcpu *v_ = domain_vcpu(d, 0); \ + v_ ? vcpu_to_node(v_) : NUMA_NO_NODE; \ +})/* The following content can be used when NUMA feature is enabled */ #ifdef CONFIG_NUMA
PATCH 7/13 x86/time: use domain_vcpu() in tsc_set_info()
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/arch/x86/time.c +6 -3
@@ -3076,12 +3076,16 @@ int tsc_set_info(struct domain *d, d->arch.incarnation = incarnation +1 ;if (is_hvm_domain (d) ) { +struct vcpu *v; +if ( hvm_tsc_scaling_supported && !d->arch.vtsc ) d->arch.hvm.tsc_scaling_ratio =hvm_get_tsc_scaling_ratio (d->arch.tsc_khz);hvm_set_rdtsc_exiting (d, d->arch.vtsc); -if ( d->vcpu && d->vcpu[0 ] && incarnation ==0 ) + + v =domain_vcpu (d,0 ); +if ( v && !incarnation ) {/* * set_tsc_offset() is called from hvm_vcpu_initialise() before
@@ -3092,8 +3096,7 @@ int tsc_set_info(struct domain *d,* will sync their TSC to BSP's sync_tsc. */ d->arch.hvm.sync_tsc =rdtsc (); -hvm_set_tsc_offset (d->vcpu[0 ], - d->vcpu[0 ]->arch.hvm.cache_tsc_offset); +hvm_set_tsc_offset (v, v->arch.hvm.cache_tsc_offset); } }
PATCH 8/13 x86/paging: use domain_vcpu() in paging_domctl()
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/arch/x86/mm/paging.c +1 -1
@@ -668,7 +668,7 @@ int paging_domctl(struct domain *d, strureturn -EINVAL ; } -if (unlikely (d->vcpu ==NULL ) ||unlikely (d->vcpu[0 ] ==NULL ) ) +if (unlikely (!domain_vcpu (d,0 )) ) {gdprintk (XENLOG_DEBUG ,"Paging op on a domain (%u) with no vcpus\n" , d->domain_id);
PATCH 9/13 x86/dm: use domain_vcpu()
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/arch/x86/hvm/dm.c +3 -3
@@ -54,7 +54,7 @@ static int track_dirty_vram(struct domaiif ( d->is_dying )return -ESRCH ; -if ( !d->max_vcpus || !d->vcpu[0 ] ) +if ( !domain_vcpu (d,0 ) )return -EINVAL ;if (DIV_ROUND_UP (nr_frames,BITS_PER_BYTE ) > buf->size )
@@ -302,9 +302,9 @@ static int set_mem_type(struct domain *dstatic int inject_event (struct domain *d,const struct xen_dm_op_inject_event *data) { -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, data->vcpuid); -if ( data->vcpuid >= d->max_vcpus || !(v = d->vcpu[data->vcpuid]) ) +if ( !v )return -EINVAL ; if ( cmpxchg(&v->arch.hvm.inject_event.vector,
PATCH 10/13 x86/HVM/MTRR: use domain_vcpu()
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/arch/x86/hvm/mtrr.c +6 -7
@@ -689,7 +689,7 @@ int hvm_set_mem_pinned_cacheattr(struct finish:if ( flush )memory_type_changed (d); -else if ( d->vcpu && d->vcpu[0 ] ) +else if (domain_vcpu (d,0 ) )p2m_memory_type_changed (d);return rc;
@@ -735,13 +735,12 @@ static int cf_check hvm_save_mtrr_msr(st static int cf_check hvm_load_mtrr_msr(struct domain *d, hvm_domain_context_t *h) { -unsigned int vcpuid, i; -struct vcpu *v; +unsigned int vcpuid =hvm_load_instance (h), i; +struct vcpu *v =domain_vcpu (d, vcpuid);struct mtrr_state *m;struct hvm_hw_mtrr hw_mtrr; - vcpuid =hvm_load_instance (h); -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , d->domain_id, vcpuid);
@@ -788,8 +787,8 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_savevoid memory_type_changed (struct domain *d) { - if ( cache_flush_permitted(d) && - d->vcpu && d->vcpu[0] && p2m_memory_type_changed(d) && + if ( cache_flush_permitted(d) && domain_vcpu(d, 0) && + p2m_memory_type_changed(d) &&/* * Do the p2m type-change, but skip the cache flush if the domain is * not yet running. The check for creation_finished must strictly be
PATCH 11/13 x86/HVM: introduce hvm_load_vcpu_instance()
In reply to Jan BeulichUse this to remove open-coding of domain_vcpu().
---
lapic_load_{hidden,regs}() are left alone, as I fear analysis tools would otherwise pick on the missing NULL checks.
xen/arch/x86/cpu/mcheck/vmce.c +3 -4
@@ -364,15 +364,14 @@ static int cf_check vmce_save_vcpu_ctxt( static int cf_check vmce_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h); -struct vcpu *v; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct hvm_vmce_vcpu ctxt;int err; -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h)); err = -EINVAL ; }else
xen/arch/x86/hvm/hvm.c +26 -31
175 lines not on this page · raw message
xen/arch/x86/hvm/mtrr.c +3 -3
@@ -735,15 +735,15 @@ static int cf_check hvm_save_mtrr_msr(st static int cf_check hvm_load_mtrr_msr(struct domain *d, hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h), i; -struct vcpu *v =domain_vcpu (d, vcpuid); +unsigned int i; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct mtrr_state *m;struct hvm_hw_mtrr hw_mtrr;if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }
xen/arch/x86/hvm/viridian/viridian.c +3 -4
@@ -1152,14 +1152,13 @@ static int cf_check viridian_save_vcpu_c static int cf_check viridian_load_vcpu_ctxt(struct domain *d,hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h); -struct vcpu *v; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct hvm_viridian_vcpu_context ctxt; -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }
xen/arch/x86/hvm/vlapic.c +6 -7
@@ -1577,16 +1577,17 @@ static void lapic_load_fixup(struct vlap } -static int lapic_check_common (const struct domain *d,unsigned int vcpuid) +static int lapic_check_common (const struct domain *d, +const hvm_domain_context_t *h) {if ( !has_vlapic (d) )return -ENODEV ;/* Which vlapic to load? */ -if ( !domain_vcpu (d, vcpuid) ) +if ( !hvm_load_vcpu_instance (d, h) ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vCPU %u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }
@@ -1596,9 +1597,8 @@ static int lapic_check_common(const stru static int cf_check lapic_check_hidden(const struct domain *d,hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h);struct hvm_hw_lapic s; -int rc =lapic_check_common (d, vcpuid); +int rc =lapic_check_common (d, h);if ( rc )return rc;
@@ -1638,10 +1638,9 @@ static int cf_check lapic_load_hidden(st static int cf_check lapic_check_regs(const struct domain *d,hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h);int rc; -if ( (rc =lapic_check_common (d, vcpuid)) ) +if ( (rc =lapic_check_common (d, h)) )return rc;if ( !hvm_get_entry (LAPIC_REGS , h) )
xen/arch/x86/include/asm/hvm/save.h +8 -1
@@ -6,8 +6,8 @@#ifndef __XEN_HVM_SAVE_H__#define __XEN_HVM_SAVE_H__ -#include <xen/types.h> #include <xen/init.h> +#include <xen/sched.h> #include <public/xen.h> #include <public/hvm/save.h>
@@ -97,6 +97,13 @@ static inline unsigned int hvm_load_instreturn d->instance; } +/* Unmarshalling: Wrapper around the above for per-vCPU instances. */ +static inline struct vcpu *hvm_load_vcpu_instance ( +const struct domain *d,const struct hvm_domain_context *h) +{ +return domain_vcpu (d,hvm_load_instance (h)); +} +/* Handler types for different types of save-file entry. * The save handler may save multiple instances of a type into the buffer; * the load handler will be called once for each instance found when
Re: [PATCH 11/13] x86/HVM: introduce hvm_load_vcpu_instance()
Quoted 6 lines · Jan Beulich
Use this to remove open-coding of
domain_vcpu().Signed-off-by Jan Beulich <[email protected]>---
lapic_load_{hidden,regs}() are left alone, as I fear analysis tools would otherwise pick on the missing NULL checks.
I'm sorry, I should have Cc-ed Paul as well.
Jan
Quoted 323 lines
xen/arch/x86/cpu/mcheck/vmce.c +3
-4@@ -364,15 +364,14 @@ static int cf_check vmce_save_vcpu_ctxt( static int cf_check vmce_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h); -struct vcpu *v; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct hvm_vmce_vcpu ctxt;int err; -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h)); err = -EINVAL ; }else xen/arch/x86/hvm/hvm.c +26
-31@@ -804,14 +804,13 @@ static int cf_check hvm_save_tsc_adjust( static int cf_check hvm_load_tsc_adjust(struct domain *d, hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h); -struct vcpu *v; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct hvm_tsc_adjust ctxt; -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }@@ -1018,18 +1017,16 @@ unsigned long hvm_cr4_guest_valid_bits(c static int cf_check hvm_load_cpu_ctxt(struct domain *d, hvm_domain_context_t *h) {const struct cpu_policy *p = d->arch.cpu_policy; -unsigned int vcpuid =hvm_load_instance (h); -struct vcpu *v; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct hvm_hw_cpu ctxt;struct segment_register seg;const char *errstr;unsigned long valid; -/* Which vcpu is this? */ -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%u has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }@@ -1267,19 +1264,17 @@ CHECK_FIELD_(struct, xsave_hdr, reserved static int cf_check hvm_load_cpu_xsave_states(struct domain *d,hvm_domain_context_t *h) { -unsigned int vcpuid, size; +unsigned int size;int err; -struct vcpu *v; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct hvm_hw_cpu_xsave *ctxt;const struct hvm_save_descriptor *desc;unsigned int i, desc_start, desc_length; -/* Which vcpu is this? */ - vcpuid =hvm_load_instance (h); -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }@@ -1298,14 +1293,14 @@ static int cf_check hvm_load_cpu_xsave_s {printk (XENLOG_G_WARNING "HVM%d.%d restore: not enough data left to read xsave descriptor\n" , - d->domain_id, vcpuid); + d->domain_id, v->vcpu_id);return -ENODATA ; }if ( desc->length +sizeof (*desc) > h->size - h->cur) {printk (XENLOG_G_WARNING "HVM%d.%d restore: not enough data left to read %u xsave bytes\n" , - d->domain_id, vcpuid, desc->length); + d->domain_id, v->vcpu_id, desc->length);return -ENODATA ; } if ( desc->length < offsetof(struct hvm_hw_cpu_xsave, save_area) +@@ -1313,7 +1308,7 @@ static int cf_check hvm_load_cpu_xsave_s {printk (XENLOG_G_WARNING "HVM%d.%d restore mismatch: xsave length %u < %zu\n" , - d->domain_id, vcpuid, desc->length, + d->domain_id, v->vcpu_id, desc->length, offsetof(struct hvm_hw_cpu_xsave , save_area) +XSTATE_AREA_MIN_SIZE );return -EINVAL ;@@ -1331,7 +1326,7 @@ static int cf_check hvm_load_cpu_xsave_sprintk (XENLOG_G_WARNING "HVM%d.%d restore: inconsistent xsave state (feat=%#" PRIx64" accum=%#" PRIx64" xcr0=%#" PRIx64" bv=%#" PRIx64" err=%d)\n" , - d->domain_id, vcpuid, ctxt->xfeature_mask, ctxt->xcr0_accum, + d->domain_id, v->vcpu_id, ctxt->xfeature_mask, ctxt->xcr0_accum, ctxt->xcr0, ctxt->save_area.xsave_hdr.xstate_bv, err);return err; }@@ -1350,13 +1345,13 @@ static int cf_check hvm_load_cpu_xsave_s {printk (XENLOG_G_WARNING "HVM%d.%u restore mismatch: xsave length %#x > %#x (non-zero data at %#x)\n" , - d->domain_id, vcpuid, desc->length, size, i); + d->domain_id, v->vcpu_id, desc->length, size, i);return -EOPNOTSUPP ; } }printk (XENLOG_G_WARNING "HVM%d.%u restore mismatch: xsave length %#x > %#x\n" , - d->domain_id, vcpuid, desc->length, size); + d->domain_id, v->vcpu_id, desc->length, size);/* Rewind desc_length to ignore the extraneous zeros. */ desc_length = size; }@@ -1365,14 +1360,14 @@ static int cf_check hvm_load_cpu_xsave_s {printk (XENLOG_G_WARNING "HVM%d.%u restore: compressed xsave state not supported\n" , - d->domain_id, vcpuid); + d->domain_id, v->vcpu_id);return -EOPNOTSUPP ; } else if ( desc_length != size ) {printk (XENLOG_G_WARNING "HVM%d.%u restore mismatch: xsave length %#x != %#x\n" , - d->domain_id, vcpuid, desc_length, size); + d->domain_id, v->vcpu_id, desc_length, size);return -EINVAL ; }/* Checking finished */ @@ -1481,15 +1476,15 @@ static int cf_check hvm_save_cpu_msrs(st static int cf_check hvm_load_cpu_msrs(struct domain *d, hvm_domain_context_t *h) { -unsigned int i, vcpuid =hvm_load_instance (h); -struct vcpu *v; +unsigned int i; +struct vcpu *v =hvm_load_vcpu_instance (d, h);const struct hvm_save_descriptor *desc;struct hvm_msr *ctxt; -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }@@ -1499,21 +1494,21 @@ static int cf_check hvm_load_cpu_msrs(st {printk (XENLOG_G_WARNING "HVM%d.%d restore: not enough data left to read MSR descriptor\n" , - d->domain_id, vcpuid); + d->domain_id, v->vcpu_id);return -ENODATA ; }if ( desc->length +sizeof (*desc) > h->size - h->cur) {printk (XENLOG_G_WARNING "HVM%d.%d restore: not enough data left to read %u MSR bytes\n" , - d->domain_id, vcpuid, desc->length); + d->domain_id, v->vcpu_id, desc->length);return -ENODATA ; }if ( desc->length <HVM_CPU_MSR_SIZE (1 ) ) {printk (XENLOG_G_WARNING "HVM%d.%d restore mismatch: MSR length %u < %zu\n" , - d->domain_id, vcpuid, desc->length,HVM_CPU_MSR_SIZE (1 )); + d->domain_id, v->vcpu_id, desc->length,HVM_CPU_MSR_SIZE (1 ));return -EINVAL ; }@@ -1525,7 +1520,7 @@ static int cf_check hvm_load_cpu_msrs(st {printk (XENLOG_G_WARNING "HVM%d.%d restore mismatch: MSR length %u != %zu\n" , - d->domain_id, vcpuid, desc->length, + d->domain_id, v->vcpu_id, desc->length,HVM_CPU_MSR_SIZE (ctxt->count));return -EOPNOTSUPP ; }xen/arch/x86/hvm/mtrr.c +3
-3@@ -735,15 +735,15 @@ static int cf_check hvm_save_mtrr_msr(st static int cf_check hvm_load_mtrr_msr(struct domain *d, hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h), i; -struct vcpu *v =domain_vcpu (d, vcpuid); +unsigned int i; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct mtrr_state *m;struct hvm_hw_mtrr hw_mtrr;if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }xen/arch/x86/hvm/viridian/viridian.c +3
-4@@ -1152,14 +1152,13 @@ static int cf_check viridian_save_vcpu_c static int cf_check viridian_load_vcpu_ctxt(struct domain *d,hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h); -struct vcpu *v; +struct vcpu *v =hvm_load_vcpu_instance (d, h);struct hvm_viridian_vcpu_context ctxt; -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !v ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vcpu%u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }xen/arch/x86/hvm/vlapic.c +6
-7@@ -1577,16 +1577,17 @@ static void lapic_load_fixup(struct vlap } -static int lapic_check_common (const struct domain *d,unsigned int vcpuid) +static int lapic_check_common (const struct domain *d, +const hvm_domain_context_t *h) {if ( !has_vlapic (d) )return -ENODEV ;/* Which vlapic to load? */ -if ( !domain_vcpu (d, vcpuid) ) +if ( !hvm_load_vcpu_instance (d, h) ) {dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vCPU %u\n" , - d->domain_id, vcpuid); + d->domain_id,hvm_load_instance (h));return -EINVAL ; }@@ -1596,9 +1597,8 @@ static int lapic_check_common(const stru static int cf_check lapic_check_hidden(const struct domain *d,hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h);struct hvm_hw_lapic s; -int rc =lapic_check_common (d, vcpuid); +int rc =lapic_check_common (d, h);if ( rc )return rc;@@ -1638,10 +1638,9 @@ static int cf_check lapic_load_hidden(st static int cf_check lapic_check_regs(const struct domain *d,hvm_domain_context_t *h) { -unsigned int vcpuid =hvm_load_instance (h);int rc; -if ( (rc =lapic_check_common (d, vcpuid)) ) +if ( (rc =lapic_check_common (d, h)) )return rc;if ( !hvm_get_entry (LAPIC_REGS , h) )xen/arch/x86/include/asm/hvm/save.h +8
-1@@ -6,8 +6,8 @@#ifndef __XEN_HVM_SAVE_H__#define __XEN_HVM_SAVE_H__ -#include <xen/types.h> #include <xen/init.h> +#include <xen/sched.h> #include <public/xen.h> #include <public/hvm/save.h> @@ -97,6 +97,13 @@ static inline unsigned int hvm_load_instreturn d->instance; } +/* Unmarshalling: Wrapper around the above for per-vCPU instances. */ +static inline struct vcpu *hvm_load_vcpu_instance ( +const struct domain *d,const struct hvm_domain_context *h) +{ +return domain_vcpu (d,hvm_load_instance (h)); +} +/* Handler types for different types of save-file entry. * The save handler may save multiple instances of a type into the buffer; * the load handler will be called once for each instance found when
PATCH 12/13 x86/vPMU: use domain_vcpu()
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/arch/x86/cpu/vpmu.c +4 -6
@@ -564,15 +564,14 @@ void vpmu_destroy(struct vcpu *v)static int pvpmu_init (struct domain *d,xen_pmu_params_t *params) { -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, params->vcpu);struct vpmu_struct *vpmu;struct page_info *page;uint64_t gfn = params->val; -if ( (params->vcpu >= d->max_vcpus) || (d->vcpu[params->vcpu] ==NULL ) ) +if ( !v )return -EINVAL ; - v = d->vcpu[params->vcpu]; vpmu =vcpu_vpmu (v);if ( !vpmu_available (v) )
@@ -615,12 +614,11 @@ static int pvpmu_init(struct domain *d,static void pvpmu_finish (struct domain *d,xen_pmu_params_t *params) { -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, params->vcpu); -if ( (params->vcpu >= d->max_vcpus) || (d->vcpu[params->vcpu] ==NULL ) ) +if ( !v )return ; - v = d->vcpu[params->vcpu];if ( v != current )vcpu_pause (v);
PATCH 13/13 x86/domctl: use domain_vcpu() to handle various sub-ops
In reply to Jan BeulichIf for nothing else, then to reduce the number of open-coded d->vcpu[] accesses.
No functional change intended.
xen/arch/x86/domctl.c +10 -15
@@ -573,11 +573,10 @@ long arch_do_domctl(case XEN_DOMCTL_sendtrigger: { -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, domctl->u.sendtrigger.vcpu); ret = -ESRCH ; -if ( domctl->u.sendtrigger.vcpu >= d->max_vcpus || - (v = d->vcpu[domctl->u.sendtrigger.vcpu]) ==NULL ) +if ( !v )break ; switch ( domctl->u.sendtrigger.trigger )
@@ -789,11 +788,10 @@ long arch_do_domctl(case XEN_DOMCTL_get_ext_vcpucontext: {struct xen_domctl_ext_vcpucontext *evc = &domctl->u.ext_vcpucontext; -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, evc->vcpu); ret = -ESRCH ; -if ( (evc->vcpu >= d->max_vcpus) || - ((v = d->vcpu[evc->vcpu]) ==NULL ) ) +if ( !v )break ;if ( domctl->cmd == XEN_DOMCTL_get_ext_vcpucontext )
@@ -912,15 +910,14 @@ long arch_do_domctl(case XEN_DOMCTL_debug_op: { -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, domctl->u.debug_op.vcpu); ret = -EOPNOTSUPP ;if ( !IS_ENABLED (CONFIG_HVM ) )break ; ret = -EINVAL ; -if ( (domctl->u.debug_op.vcpu >= d->max_vcpus) || - ((v = d->vcpu[domctl->u.debug_op.vcpu]) ==NULL ) ) +if ( !v )break ; ret = -EINVAL ;
@@ -943,15 +940,14 @@ long arch_do_domctl(case XEN_DOMCTL_getvcpuextstate: {struct xen_domctl_vcpuextstate *evc = &domctl->u.vcpuextstate; -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, evc->vcpu);uint32_t offset =0 ;#define PV_XSAVE_HDR_SIZE (2 * sizeof(uint64_t))#define PV_XSAVE_SIZE (xcr0) (PV_XSAVE_HDR_SIZE + xstate_uncompressed_size(xcr0)) ret = -ESRCH ; -if ( (evc->vcpu >= d->max_vcpus) || - ((v = d->vcpu[evc->vcpu]) ==NULL ) ) +if ( !v ) goto vcpuextstate_out; ret = -EINVAL ;
@@ -1148,7 +1144,7 @@ long arch_do_domctl( {struct xen_domctl_vcpu_msrs *vmsrs = &domctl->u.vcpu_msrs;struct xen_domctl_vcpu_msr msr = {}; -struct vcpu *v; +struct vcpu *v =domain_vcpu (d, vmsrs->vcpu);static const uint32_t msrs_to_send[] = {MSR_SPEC_CTRL ,MSR_INTEL_MISC_FEATURES_ENABLES ,
@@ -1161,8 +1157,7 @@ long arch_do_domctl(uint32_t nr_msrs =ARRAY_SIZE (msrs_to_send); ret = -ESRCH ; -if ( (vmsrs->vcpu >= d->max_vcpus) || - ((v = d->vcpu[vmsrs->vcpu]) ==NULL ) ) +if ( !v )break ; ret = -EINVAL ;
Quoted 4 lines · Jan Beulich
I.e. reduce the number of instances where we access d->vcpu[] directly. The series doesn't cover all cases, just a fair part. I think there are no inter-dependencies between the individual patches.
Quoted 11 lines
[...]
04: domctl: usedomain_vcpu()in XEN_DOMCTL_{g,s}etvcpucontext handling 05: vm-event: usedomain_vcpu()in domctl handling
06: numa: usedomain_vcpu()indomain_to_node()
07: x86/time: usedomain_vcpu()intsc_set_info()
08: x86/paging: usedomain_vcpu()inpaging_domctl()
09: x86/dm: usedomain_vcpu()
10: x86/HVM/MTRR: usedomain_vcpu()
11: x86/HVM: introducehvm_load_vcpu_instance()
12: x86/vPMU: usedomain_vcpu()
13: x86/domctl: usedomain_vcpu()to handle various sub-ops
One small thing: 11 does not apply without 10, since its mtrr.c hunk is on top of 10's change to hvm_load_mtrr_msr(). Maybe 10 could leave that function alone, as 11 rewrites it anyway.
I checked 04-10, 12 and 13 against the old code. In each of them the new domain_vcpu() check covers the cases the old open-coded check did.
For patches 04-10, 12 and 13:
Stephen
Quoted 17 lines · Stephen Cheng
Quoted 4 lines · Jan Beulich
I.e. reduce the number of instances where we access d->vcpu[] directly. The series doesn't cover all cases, just a fair part. I think there are no inter-dependencies between the individual patches.
Quoted 11 lines
[...]
04: domctl: usedomain_vcpu()in XEN_DOMCTL_{g,s}etvcpucontext handling 05: vm-event: usedomain_vcpu()in domctl handling
06: numa: usedomain_vcpu()indomain_to_node()
07: x86/time: usedomain_vcpu()intsc_set_info()
08: x86/paging: usedomain_vcpu()inpaging_domctl()
09: x86/dm: usedomain_vcpu()
10: x86/HVM/MTRR: usedomain_vcpu()
11: x86/HVM: introducehvm_load_vcpu_instance()
12: x86/vPMU: usedomain_vcpu()
13: x86/domctl: usedomain_vcpu()to handle various sub-ops
Quoted 3 lines
One small thing: 11 does not apply without 10, since its
mtrr.chunk is on top of 10's change tohvm_load_mtrr_msr(). Maybe 10 could leave that function alone, as 11 rewrites it anyway.
Right, I did notice the double change to the function, but I wasn't bothered enough to do something about it. If anything, patches 10 and 11 should be swapped, such that the description of what is now 10 wouldn't be inaccurate (as to leaving one case unaddressed). I'm not sure though whether that would be worthwhile the effort.
Quoted 5 lines
I checked 04-10, 12 and 13 against the old code. In each of them the new
domain_vcpu()check covers the cases the old open-coded check did.For patches 04-10, 12 and 13:
Reviewed-by Stephen Cheng <[email protected]>
Thanks.
Jan