From: "Jan Beulich" Subject: [PATCH] make domain_create() return a proper error code Date: Mon, 03 Sep 2012 07:57:33 +0100 Message-ID: <504470FD0200007800098213@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartE5D4FCCD.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartE5D4FCCD.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline While triggered by the XSA-9 fix, this really is of more general use; that fix just pointed out very sharply that the current situation with all domain creation failures reported to user (tools) space as -ENOMEM is very unfortunate (actively misleading users _and_ support personnel). Pull over the pointer <-> error code conversion infrastructure from Linux, and use it in domain_create() and all it callers. Signed-off-by: Jan Beulich --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -237,7 +238,7 @@ void __init start_xen(unsigned long boot =20 /* Create initial domain 0. */ dom0 =3D domain_create(0, 0, 0); - if ( dom0 =3D=3D NULL ) + if ( IS_ERR(dom0) ) printk("domain_create failed\n"); if ( (dom0 =3D=3D NULL) || (alloc_dom0_vcpu0() =3D=3D NULL) ) panic("Error creating domain 0\n"); --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -91,7 +91,7 @@ #include #include #include -#include +#include #include #include #include @@ -309,7 +309,7 @@ void __init arch_init_memory(void) * their domain field set to dom_xen. */ dom_xen =3D domain_create(DOMID_XEN, DOMCRF_dummy, 0); - BUG_ON(dom_xen =3D=3D NULL); + BUG_ON(IS_ERR(dom_xen)); =20 /* * Initialise our DOMID_IO domain. @@ -317,14 +317,14 @@ void __init arch_init_memory(void) * array. Mappings occur at the priv of the caller. */ dom_io =3D domain_create(DOMID_IO, DOMCRF_dummy, 0); - BUG_ON(dom_io =3D=3D NULL); + BUG_ON(IS_ERR(dom_io)); =20 /* - * Initialise our DOMID_IO domain. + * Initialise our COW domain. * This domain owns sharable pages. */ dom_cow =3D domain_create(DOMID_COW, DOMCRF_dummy, 0); - BUG_ON(dom_cow =3D=3D NULL); + BUG_ON(IS_ERR(dom_cow)); =20 /* First 1MB of RAM is historically marked as I/O. */ for ( i =3D 0; i < 0x100; i++ ) --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -1319,7 +1320,7 @@ void __init __start_xen(unsigned long mb =20 /* Create initial domain 0. */ dom0 =3D domain_create(0, DOMCRF_s3_integrity, 0); - if ( (dom0 =3D=3D NULL) || (alloc_dom0_vcpu0() =3D=3D NULL) ) + if ( IS_ERR(dom0) || (alloc_dom0_vcpu0() =3D=3D NULL) ) panic("Error creating domain 0\n"); =20 dom0->is_privileged =3D 1; --- a/xen/common/cpupool.c +++ b/xen/common/cpupool.c @@ -370,14 +370,18 @@ out: int cpupool_add_domain(struct domain *d, int poolid) { struct cpupool *c; - int rc =3D 1; + int rc; int n_dom =3D 0; =20 if ( poolid =3D=3D CPUPOOLID_NONE ) return 0; spin_lock(&cpupool_lock); c =3D cpupool_find_by_id(poolid); - if ( (c !=3D NULL) && cpumask_weight(c->cpu_valid) ) + if ( c =3D=3D NULL ) + rc =3D -ESRCH; + else if ( !cpumask_weight(c->cpu_valid) ) + rc =3D -ENODEV; + else { c->n_dom++; n_dom =3D c->n_dom; --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -196,17 +196,17 @@ struct domain *domain_create( struct domain *d, **pd; enum { INIT_xsm =3D 1u<<0, INIT_watchdog =3D 1u<<1, INIT_rangeset =3D = 1u<<2, INIT_evtchn =3D 1u<<3, INIT_gnttab =3D 1u<<4, INIT_arch =3D = 1u<<5 }; - int init_status =3D 0; + int err, init_status =3D 0; int poolid =3D CPUPOOLID_NONE; =20 if ( (d =3D alloc_domain_struct()) =3D=3D NULL ) - return NULL; + return ERR_PTR(-ENOMEM); =20 d->domain_id =3D domid; =20 lock_profile_register_struct(LOCKPROF_TYPE_PERDOM, d, domid, = "Domain"); =20 - if ( xsm_alloc_security_domain(d) !=3D 0 ) + if ( (err =3D xsm_alloc_security_domain(d)) !=3D 0 ) goto fail; init_status |=3D INIT_xsm; =20 @@ -226,6 +226,7 @@ struct domain *domain_create( spin_lock_init(&d->shutdown_lock); d->shutdown_code =3D -1; =20 + err =3D -ENOMEM; if ( !zalloc_cpumask_var(&d->domain_dirty_cpumask) ) goto fail; =20 @@ -251,7 +252,7 @@ struct domain *domain_create( =20 if ( !is_idle_domain(d) ) { - if ( xsm_domain_create(d, ssidref) !=3D 0 ) + if ( (err =3D xsm_domain_create(d, ssidref)) !=3D 0 ) goto fail; =20 d->is_paused_by_controller =3D 1; @@ -266,29 +267,30 @@ struct domain *domain_create( =20 radix_tree_init(&d->pirq_tree); =20 - if ( evtchn_init(d) !=3D 0 ) + if ( (err =3D evtchn_init(d)) !=3D 0 ) goto fail; init_status |=3D INIT_evtchn; =20 - if ( grant_table_create(d) !=3D 0 ) + if ( (err =3D grant_table_create(d)) !=3D 0 ) goto fail; init_status |=3D INIT_gnttab; =20 poolid =3D 0; =20 + err =3D -ENOMEM; d->mem_event =3D xzalloc(struct mem_event_per_domain); if ( !d->mem_event ) goto fail; } =20 - if ( arch_domain_create(d, domcr_flags) !=3D 0 ) + if ( (err =3D arch_domain_create(d, domcr_flags)) !=3D 0 ) goto fail; init_status |=3D INIT_arch; =20 - if ( cpupool_add_domain(d, poolid) !=3D 0 ) + if ( (err =3D cpupool_add_domain(d, poolid)) !=3D 0 ) goto fail; =20 - if ( sched_init_domain(d) !=3D 0 ) + if ( (err =3D sched_init_domain(d)) !=3D 0 ) goto fail; =20 if ( !is_idle_domain(d) ) @@ -329,7 +331,7 @@ struct domain *domain_create( xsm_free_security_domain(d); free_cpumask_var(d->domain_dirty_cpumask); free_domain_struct(d); - return NULL; + return ERR_PTR(err); } =20 =20 --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -455,10 +456,12 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domc if ( op->u.createdomain.flags & XEN_DOMCTL_CDF_oos_off ) domcr_flags |=3D DOMCRF_oos_off; =20 - ret =3D -ENOMEM; d =3D domain_create(dom, domcr_flags, op->u.createdomain.ssidref);= - if ( d =3D=3D NULL ) + if ( IS_ERR(d) ) + { + ret =3D PTR_ERR(d); break; + } =20 ret =3D 0; =20 --- a/xen/common/schedule.c +++ b/xen/common/schedule.c @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include @@ -1323,7 +1323,7 @@ void __init scheduler_init(void) panic("scheduler returned error on init\n"); =20 idle_domain =3D domain_create(DOMID_IDLE, 0, 0); - BUG_ON(idle_domain =3D=3D NULL); + BUG_ON(IS_ERR(idle_domain)); idle_domain->vcpu =3D idle_vcpu; idle_domain->max_vcpus =3D nr_cpu_ids; if ( alloc_vcpu(idle_domain, 0, 0) =3D=3D NULL ) --- /dev/null +++ b/xen/include/xen/err.h @@ -0,0 +1,57 @@ +#if !defined(__XEN_ERR_H__) && !defined(__ASSEMBLY__) +#define __XEN_ERR_H__ + +#include +#include + +/* + * Kernel pointers have redundant information, so we can use a + * scheme where we can return either an error code or a dentry + * pointer with the same return value. + * + * This could be a per-architecture thing, to allow different + * error and pointer decisions. + */ +#define MAX_ERRNO 4095 + +#define IS_ERR_VALUE(x) unlikely((x) >=3D (unsigned long)-MAX_ERRNO) + +static inline void *__must_check ERR_PTR(long error) +{ + return (void *)error; +} + +static inline long __must_check PTR_ERR(const void *ptr) +{ + return (long)ptr; +} + +static inline long __must_check IS_ERR(const void *ptr) +{ + return IS_ERR_VALUE((unsigned long)ptr); +} + +static inline long __must_check IS_ERR_OR_NULL(const void *ptr) +{ + return !ptr || IS_ERR_VALUE((unsigned long)ptr); +} + +/** + * ERR_CAST - Explicitly cast an error-valued pointer to another pointer = type + * @ptr: The pointer to cast. + * + * Explicitly cast an error-valued pointer to another pointer type in = such a + * way as to make it clear that's what's going on. + */ +static inline void * __must_check ERR_CAST(const void *ptr) +{ + /* cast away the const */ + return (void *)ptr; +} + +static inline int __must_check PTR_RET(const void *ptr) +{ + return IS_ERR(ptr) ? PTR_ERR(ptr) : 0; +} + +#endif /* __XEN_ERR_H__ */ --=__PartE5D4FCCD.0__= Content-Type: text/plain; name="domain_create-return-value.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="domain_create-return-value.patch" make domain_create() return a proper error code=0A=0AWhile triggered by = the XSA-9 fix, this really is of more general use;=0Athat fix just pointed = out very sharply that the current situation=0Awith all domain creation = failures reported to user (tools) space as=0A-ENOMEM is very unfortunate = (actively misleading users _and_ support=0Apersonnel).=0A=0APull over the = pointer <-> error code conversion infrastructure from=0ALinux, and use it = in domain_create() and all it callers.=0A=0ASigned-off-by: Jan Beulich = =0A=0A--- a/xen/arch/arm/setup.c=0A+++ b/xen/arch/arm/se= tup.c=0A@@ -26,6 +26,7 @@=0A #include =0A #include =0A #include =0A+#include =0A #include = =0A #include =0A #include =0A@@ -237,7 = +238,7 @@ void __init start_xen(unsigned long boot=0A =0A /* Create = initial domain 0. */=0A dom0 =3D domain_create(0, 0, 0);=0A- if ( = dom0 =3D=3D NULL )=0A+ if ( IS_ERR(dom0) )=0A printk("domain= _create failed\n");=0A if ( (dom0 =3D=3D NULL) || (alloc_dom0_vcpu0() = =3D=3D NULL) )=0A panic("Error creating domain 0\n");=0A--- = a/xen/arch/x86/mm.c=0A+++ b/xen/arch/x86/mm.c=0A@@ -91,7 +91,7 @@=0A = #include =0A #include =0A #include =0A= -#include =0A+#include =0A #include = =0A #include =0A #include =0A@@ -309,7 +309,7 @@ = void __init arch_init_memory(void)=0A * their domain field set to = dom_xen.=0A */=0A dom_xen =3D domain_create(DOMID_XEN, DOMCRF_dumm= y, 0);=0A- BUG_ON(dom_xen =3D=3D NULL);=0A+ BUG_ON(IS_ERR(dom_xen));= =0A =0A /*=0A * Initialise our DOMID_IO domain.=0A@@ -317,14 = +317,14 @@ void __init arch_init_memory(void)=0A * array. Mappings = occur at the priv of the caller.=0A */=0A dom_io =3D domain_create= (DOMID_IO, DOMCRF_dummy, 0);=0A- BUG_ON(dom_io =3D=3D NULL);=0A+ = BUG_ON(IS_ERR(dom_io));=0A =0A /*=0A- * Initialise our = DOMID_IO domain.=0A+ * Initialise our COW domain.=0A * This = domain owns sharable pages.=0A */=0A dom_cow =3D domain_create(DOM= ID_COW, DOMCRF_dummy, 0);=0A- BUG_ON(dom_cow =3D=3D NULL);=0A+ = BUG_ON(IS_ERR(dom_cow));=0A =0A /* First 1MB of RAM is historically = marked as I/O. */=0A for ( i =3D 0; i < 0x100; i++ )=0A--- a/xen/arch/x= 86/setup.c=0A+++ b/xen/arch/x86/setup.c=0A@@ -1,6 +1,7 @@=0A #include = =0A #include =0A #include =0A+#include= =0A #include =0A #include =0A = #include =0A@@ -1319,7 +1320,7 @@ void __init __start_xen(uns= igned long mb=0A =0A /* Create initial domain 0. */=0A dom0 =3D = domain_create(0, DOMCRF_s3_integrity, 0);=0A- if ( (dom0 =3D=3D NULL) = || (alloc_dom0_vcpu0() =3D=3D NULL) )=0A+ if ( IS_ERR(dom0) || = (alloc_dom0_vcpu0() =3D=3D NULL) )=0A panic("Error creating domain = 0\n");=0A =0A dom0->is_privileged =3D 1;=0A--- a/xen/common/cpupool.c= =0A+++ b/xen/common/cpupool.c=0A@@ -370,14 +370,18 @@ out:=0A int = cpupool_add_domain(struct domain *d, int poolid)=0A {=0A struct = cpupool *c;=0A- int rc =3D 1;=0A+ int rc;=0A int n_dom =3D 0;=0A = =0A if ( poolid =3D=3D CPUPOOLID_NONE )=0A return 0;=0A = spin_lock(&cpupool_lock);=0A c =3D cpupool_find_by_id(poolid);=0A- = if ( (c !=3D NULL) && cpumask_weight(c->cpu_valid) )=0A+ if ( c =3D=3D = NULL )=0A+ rc =3D -ESRCH;=0A+ else if ( !cpumask_weight(c->cpu_va= lid) )=0A+ rc =3D -ENODEV;=0A+ else=0A {=0A = c->n_dom++;=0A n_dom =3D c->n_dom;=0A--- a/xen/common/domain.c=0A++= + b/xen/common/domain.c=0A@@ -9,7 +9,7 @@=0A #include =0A = #include =0A #include =0A-#include =0A= +#include =0A #include =0A #include =0A #include =0A@@ -196,17 +196,17 @@ struct domain = *domain_create(=0A struct domain *d, **pd;=0A enum { INIT_xsm =3D = 1u<<0, INIT_watchdog =3D 1u<<1, INIT_rangeset =3D 1u<<2,=0A = INIT_evtchn =3D 1u<<3, INIT_gnttab =3D 1u<<4, INIT_arch =3D 1u<<5 };=0A- = int init_status =3D 0;=0A+ int err, init_status =3D 0;=0A int = poolid =3D CPUPOOLID_NONE;=0A =0A if ( (d =3D alloc_domain_struct()) = =3D=3D NULL )=0A- return NULL;=0A+ return ERR_PTR(-ENOMEM);= =0A =0A d->domain_id =3D domid;=0A =0A lock_profile_register_struct= (LOCKPROF_TYPE_PERDOM, d, domid, "Domain");=0A =0A- if ( xsm_alloc_secur= ity_domain(d) !=3D 0 )=0A+ if ( (err =3D xsm_alloc_security_domain(d)) = !=3D 0 )=0A goto fail;=0A init_status |=3D INIT_xsm;=0A =0A@@ = -226,6 +226,7 @@ struct domain *domain_create(=0A spin_lock_init(&d->sh= utdown_lock);=0A d->shutdown_code =3D -1;=0A =0A+ err =3D = -ENOMEM;=0A if ( !zalloc_cpumask_var(&d->domain_dirty_cpumask) )=0A = goto fail;=0A =0A@@ -251,7 +252,7 @@ struct domain *domain_create(=0A = =0A if ( !is_idle_domain(d) )=0A {=0A- if ( xsm_domain_creat= e(d, ssidref) !=3D 0 )=0A+ if ( (err =3D xsm_domain_create(d, = ssidref)) !=3D 0 )=0A goto fail;=0A =0A d->is_paused_by= _controller =3D 1;=0A@@ -266,29 +267,30 @@ struct domain *domain_create(=0A= =0A radix_tree_init(&d->pirq_tree);=0A =0A- if ( evtchn_ini= t(d) !=3D 0 )=0A+ if ( (err =3D evtchn_init(d)) !=3D 0 )=0A = goto fail;=0A init_status |=3D INIT_evtchn;=0A =0A- if = ( grant_table_create(d) !=3D 0 )=0A+ if ( (err =3D grant_table_creat= e(d)) !=3D 0 )=0A goto fail;=0A init_status |=3D = INIT_gnttab;=0A =0A poolid =3D 0;=0A =0A+ err =3D = -ENOMEM;=0A d->mem_event =3D xzalloc(struct mem_event_per_domain);= =0A if ( !d->mem_event )=0A goto fail;=0A }=0A = =0A- if ( arch_domain_create(d, domcr_flags) !=3D 0 )=0A+ if ( (err = =3D arch_domain_create(d, domcr_flags)) !=3D 0 )=0A goto fail;=0A = init_status |=3D INIT_arch;=0A =0A- if ( cpupool_add_domain(d, = poolid) !=3D 0 )=0A+ if ( (err =3D cpupool_add_domain(d, poolid)) !=3D = 0 )=0A goto fail;=0A =0A- if ( sched_init_domain(d) !=3D 0 = )=0A+ if ( (err =3D sched_init_domain(d)) !=3D 0 )=0A goto = fail;=0A =0A if ( !is_idle_domain(d) )=0A@@ -329,7 +331,7 @@ struct = domain *domain_create(=0A xsm_free_security_domain(d);=0A = free_cpumask_var(d->domain_dirty_cpumask);=0A free_domain_struct(d);=0A= - return NULL;=0A+ return ERR_PTR(err);=0A }=0A =0A =0A--- a/xen/comm= on/domctl.c=0A+++ b/xen/common/domctl.c=0A@@ -9,6 +9,7 @@=0A #include = =0A #include =0A #include =0A+#includ= e =0A #include =0A #include =0A #include = =0A@@ -455,10 +456,12 @@ long do_domctl(XEN_GUEST_HANDLE(xe= n_domc=0A if ( op->u.createdomain.flags & XEN_DOMCTL_CDF_oos_off = )=0A domcr_flags |=3D DOMCRF_oos_off;=0A =0A- ret =3D = -ENOMEM;=0A d =3D domain_create(dom, domcr_flags, op->u.createdomai= n.ssidref);=0A- if ( d =3D=3D NULL )=0A+ if ( IS_ERR(d) = )=0A+ {=0A+ ret =3D PTR_ERR(d);=0A break;=0A+= }=0A =0A ret =3D 0;=0A =0A--- a/xen/common/schedule.c=0A+++= b/xen/common/schedule.c=0A@@ -28,7 +28,7 @@=0A #include =0A= #include =0A #include =0A-#include =0A= +#include =0A #include =0A #include = =0A #include =0A@@ -1323,7 +1323,7 @@ void = __init scheduler_init(void)=0A panic("scheduler returned error on = init\n");=0A =0A idle_domain =3D domain_create(DOMID_IDLE, 0, 0);=0A- = BUG_ON(idle_domain =3D=3D NULL);=0A+ BUG_ON(IS_ERR(idle_domain));=0A = idle_domain->vcpu =3D idle_vcpu;=0A idle_domain->max_vcpus =3D = nr_cpu_ids;=0A if ( alloc_vcpu(idle_domain, 0, 0) =3D=3D NULL )=0A--- = /dev/null=0A+++ b/xen/include/xen/err.h=0A@@ -0,0 +1,57 @@=0A+#if = !defined(__XEN_ERR_H__) && !defined(__ASSEMBLY__)=0A+#define __XEN_ERR_H__= =0A+=0A+#include =0A+#include =0A+=0A+/*=0A+ = * Kernel pointers have redundant information, so we can use a=0A+ * scheme = where we can return either an error code or a dentry=0A+ * pointer with = the same return value.=0A+ *=0A+ * This could be a per-architecture thing, = to allow different=0A+ * error and pointer decisions.=0A+ */=0A+#define = MAX_ERRNO 4095=0A+=0A+#define IS_ERR_VALUE(x) unlikely((x) >=3D = (unsigned long)-MAX_ERRNO)=0A+=0A+static inline void *__must_check = ERR_PTR(long error)=0A+{=0A+ return (void *)error;=0A+}=0A+=0A+static = inline long __must_check PTR_ERR(const void *ptr)=0A+{=0A+ return = (long)ptr;=0A+}=0A+=0A+static inline long __must_check IS_ERR(const void = *ptr)=0A+{=0A+ return IS_ERR_VALUE((unsigned long)ptr);=0A+}=0A+=0A+static= inline long __must_check IS_ERR_OR_NULL(const void *ptr)=0A+{=0A+ = return !ptr || IS_ERR_VALUE((unsigned long)ptr);=0A+}=0A+=0A+/**=0A+ * = ERR_CAST - Explicitly cast an error-valued pointer to another pointer = type=0A+ * @ptr: The pointer to cast.=0A+ *=0A+ * Explicitly cast an = error-valued pointer to another pointer type in such a=0A+ * way as to = make it clear that's what's going on.=0A+ */=0A+static inline void * = __must_check ERR_CAST(const void *ptr)=0A+{=0A+ /* cast away the const = */=0A+ return (void *)ptr;=0A+}=0A+=0A+static inline int __must_check = PTR_RET(const void *ptr)=0A+{=0A+ return IS_ERR(ptr) ? PTR_ERR(ptr) = : 0;=0A+}=0A+=0A+#endif /* __XEN_ERR_H__ */=0A --=__PartE5D4FCCD.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__PartE5D4FCCD.0__=--