x86/mm: XENMEM_{get,set}_pod_target handling adjustments
cover x86/mm: XENMEM_{get,set}_pod_target handling adjustments
In the context of PCI pass-through I came to look at this code again, while considering whether it might be usable for an issue there (which I'll send separate mail about).
1: XENMEM_{get,set}_pod_target are restricted to HVM domains 2: tidy XENMEM_{get,set}_pod_target handling
Jan
PATCH 1/2 x86/mm: XENMEM_{get,set}_pod_target are restricted to HVM domains
While for now PV domains have a host P2M associated with them and hence using XENMEM_get_pod_target on such may not be a real problem, calling p2m_pod_set_mem_target() for a PV domain is surely wrong, even if benign at present.
---
While the output of the two subfunctions is stale by the time the caller accesses it, I wonder in how far it is helpful to provide potentially inconsistent values, due to the lack of any respective locking. It just so happens that "get" has no in-tree caller at all (years ago I did propose it to be exposed to guests themselves, with no real decision taken either way), while of the two "set" callers neither cares about the output.
https://lists.xenproject.org/archives/html/xen-devel/2014-01/msg01524.html
https://lists.xenproject.org/archives/html/xen-devel/2014-02/msg02154.html
Furthermore for "set" I also find it concerning that page-alloc-lock protected data is being accessed without holding that lock. Both d->max_pages and the result of domain_tot_pages() can change in parallel. Then again I wonder whether "set" shouldn't be restricted to before the guest gets actually started.
xen/arch/x86/mm.c +3 -1
@@ -4786,7 +4786,9 @@ long arch_memory_op(unsigned long cmd, Xif ( d ==NULL )return -ESRCH ; -if ( cmd == XENMEM_set_pod_target ) +if ( !is_hvm_domain (d) ) + rc = -EINVAL ; +else if ( cmd == XENMEM_set_pod_target ) rc =xsm_set_pod_target (XSM_PRIV , d);else rc =xsm_get_pod_target (XSM_PRIV , d);
PATCH 1/2 x86/mm: tidy XENMEM_{get,set}_pod_target handling
In reply to Jan BeulichDo away with the "pod_target_out_unlock" label. In particular by folding if()-s, the logic can be expressed with less code (and no goto-s) this way.
Limit scope of "p2m", constifying it at the same time.
xen/arch/x86/mm.c +11 -21
@@ -4777,7 +4777,6 @@ long arch_memory_op(unsigned long cmd, X {xen_pod_target_t target;struct domain *d; -struct p2m_domain *p2m;if (copy_from_guest (&target, arg,1 ) )return -EFAULT ;
@@ -4789,23 +4788,17 @@ long arch_memory_op(unsigned long cmd, Xif ( !is_hvm_domain (d) ) rc = -EINVAL ;else if ( cmd == XENMEM_set_pod_target ) - rc =xsm_set_pod_target (XSM_PRIV , d); -else - rc =xsm_get_pod_target (XSM_PRIV , d); - -if ( rc !=0 ) - goto pod_target_out_unlock; - -if ( cmd == XENMEM_set_pod_target ) { -if ( target.target_pages > d->max_pages ) - { + rc =xsm_set_pod_target (XSM_PRIV , d); +if ( rc ) +ASSERT (rc <0 ); +else if ( target.target_pages > d->max_pages ) rc = -EINVAL ; - goto pod_target_out_unlock; - } - - rc =p2m_pod_set_mem_target (d, target.target_pages); +else + rc =p2m_pod_set_mem_target (d, target.target_pages); } +else + rc =xsm_get_pod_target (XSM_PRIV , d);if ( rc == -ERESTART ) {
@@ -4814,19 +4807,16 @@ long arch_memory_op(unsigned long cmd, X }else if ( rc >=0 ) { - p2m =p2m_get_hostp2m (d); +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + target.tot_pages =domain_tot_pages (d); target.pod_cache_pages = p2m->pod.count; target.pod_entries = p2m->pod.entry_count;if (__copy_to_guest (arg, &target,1 ) ) - { - rc= -EFAULT ; - goto pod_target_out_unlock; - } + rc = -EFAULT ; } - pod_target_out_unlock:rcu_unlock_domain (d);return rc; }
Re: [PATCH 1/2] x86/mm: tidy XENMEM_{get,set}_pod_target handling
Quoted 7 lines · Jan Beulich
Do away with the "
pod_target_out_unlock" label. In particular by folding if()-s, the logic can be expressed with less code (and no goto-s) this way.Limit scope of "p2m", constifying it at the same time.
Signed-off-by Jan Beulich <[email protected]>
And this is of course patch 2/2. I'm sorry for the screw-up.
Jan
Quoted 67 lines
xen/arch/x86/mm.c +11
-21@@ -4777,7 +4777,6 @@ long arch_memory_op(unsigned long cmd, X {xen_pod_target_t target;struct domain *d; -struct p2m_domain *p2m;if (copy_from_guest (&target, arg,1 ) )return -EFAULT ;@@ -4789,23 +4788,17 @@ long arch_memory_op(unsigned long cmd, Xif ( !is_hvm_domain (d) ) rc = -EINVAL ;else if ( cmd == XENMEM_set_pod_target ) - rc =xsm_set_pod_target (XSM_PRIV , d); -else - rc =xsm_get_pod_target (XSM_PRIV , d); - -if ( rc !=0 ) - goto pod_target_out_unlock; - -if ( cmd == XENMEM_set_pod_target ) { -if ( target.target_pages > d->max_pages ) - { + rc =xsm_set_pod_target (XSM_PRIV , d); +if ( rc ) +ASSERT (rc <0 ); +else if ( target.target_pages > d->max_pages ) rc = -EINVAL ; - goto pod_target_out_unlock; - } - - rc =p2m_pod_set_mem_target (d, target.target_pages); +else + rc =p2m_pod_set_mem_target (d, target.target_pages); } +else + rc =xsm_get_pod_target (XSM_PRIV , d);if ( rc == -ERESTART ) {@@ -4814,19 +4807,16 @@ long arch_memory_op(unsigned long cmd, X }else if ( rc >=0 ) { - p2m =p2m_get_hostp2m (d); +const struct p2m_domain *p2m =p2m_get_hostp2m (d); + target.tot_pages =domain_tot_pages (d); target.pod_cache_pages = p2m->pod.count; target.pod_entries = p2m->pod.entry_count;if (__copy_to_guest (arg, &target,1 ) ) - { - rc= -EFAULT ; - goto pod_target_out_unlock; - } + rc = -EFAULT ; } - pod_target_out_unlock:rcu_unlock_domain (d);return rc; }