xenstored patches from split hardware control
v6 cover xenstored patches from split hardware control
This is a subset of patches focusing on xenstored changes from my split hardware control domain series.
"tools/xenstored: Check NULL domain in domain_chk_quota()" is new.
The xenstore changes are reviewed aside from the new commit.
stubdom was tested in gitlab-ci - xl list shows Domain-0 and Xenstore.
"tools/xenstored: Use priv_domid for manual nodes and permission" is an interesting result of looking to rename some internal variables to better align their purpose.
Any review or guidance on the approach is appreciated.
Jason Andryuk (8):
tools/xenstored: Read event channel from xenstored page tools/xenstored: Add get_domain_evtchn() to find evtchn tools/xenstored: Auto-introduce domains tools/xenstored: Check NULL domain in domain_chk_quota() tools/xenstored: Use priv_domid for manual nodes and permission tools/xenstored: Rename dom0_domid to store_domid tools/xenstored: Remove stubdom special casing tools/xenstored: Remove hardcoded implicit path
tools/xenstored/core.c | 17 +++--- tools/xenstored/core.h | 8 +-- tools/xenstored/domain.c | 128 +++++++++++++++++++++++++++++---------- tools/xenstored/domain.h | 2 +- tools/xenstored/minios.c | 21 +++++-- tools/xenstored/posix.c | 18 +++++- 6 files changed, 141 insertions(+), 53 deletions(-)
Signature · 1 line
2.50.1
PATCH v6 1/8 tools/xenstored: Read event channel from xenstored page
Make introduce_domain() use an event channel from the the xenstore page. It is only used if non-zero. Otherwise the passed in event channel port is used.
The is useful for a xenstored stubdom to configure domains autonomously.
---
v4:
R-b Juergen
v2:
Remove iface_port variable.
--- tools/xenstored/domain.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/xenstored/domain.c +4 -0
@@ -1036,6 +1036,10 @@ static struct domain *introduce_domain(const void *ctx, interface =map_interface (domid);if (!interface && !restore)return NULL ; + +if (interface->evtchn_port) + port = interface->evtchn_port; +if (new_domain (domain, port, restore)) { rc = errno;if (interface)
Signature · 1 line
2.50.1
PATCH v6 2/8 tools/xenstored: Add get_domain_evtchn() to find evtchn
In reply to Jason AndryukAdd helpers to lookup the event channel for a domid. This hides some of the differences between dom0 and stubdom xenstored. Each version defines its own.
It highlights the different meanings between get_xenbus_evtchn() in a stubdom, where it looks up dom0's event channel, and dom0, where it looks up the local event channel.
get_domain_evtchn() replaces get_xenbus_evtchn(), and get_xenbus_evtchn() is removed from minios.c as it is inlined in the new function.
The default return 0 will be fine as any other auto-introduced domain will needs the event channel populated in the grant.
---
v6:
R-b: Juergen
v5:
Split get_domain_evtchn() in minios.c and posix.c versions s/dom0/stubdom/
--- tools/xenstored/core.h | 2 +- tools/xenstored/domain.c | 10 +++++++--- tools/xenstored/minios.c | 19 ++++++++++++++++--- tools/xenstored/posix.c | 16 +++++++++++++++- 4 files changed, 39 insertions(+), 8 deletions(-)
tools/xenstored/core.h +1 -1
@@ -397,7 +397,7 @@ static inline bool domain_is_unprivileged(const struct connection *conn)extern xenevtchn_handle *xce_handle;/* in domain.c */ /* Return the event channel used by xenbus. */ -evtchn_port_t get_xenbus_evtchn (void ); +evtchn_port_t get_domain_evtchn (unsigned int domid);void early_init (bool live_update,bool dofork,const char *pidfile);void late_init (bool live_update);
tools/xenstored/domain.c +7 -3
@@ -1330,7 +1330,7 @@ void dom0_init(void)evtchn_port_t port;struct domain *dom0; - port =get_xenbus_evtchn (); + port =get_domain_evtchn (xenbus_master_domid ());if (port ==-1 )barf_perror ("Failed to initialize dom0 port" );
@@ -1345,13 +1345,17 @@ void stubdom_init(bool live_update) {#ifdef __MINIOS__struct domain *stubdom; +evtchn_port_t port;if (stub_domid <0 )return ;if (!live_update) { - stubdom =introduce_domain (NULL , stub_domid, xenbus_evtchn, - false); + port =get_domain_evtchn (stub_domid); +if (port ==-1 ) +barf_perror ("Failed to initialize stubdom port" ); + + stubdom =introduce_domain (NULL , stub_domid, port, false);if (!stubdom)barf_perror ("Failed to initialize stubdom" );
tools/xenstored/minios.c +16 -3
@@ -41,9 +41,22 @@ struct connection *add_socket_connection(int fd)barf ("socket based connection without sockets" ); } -evtchn_port_t get_xenbus_evtchn (void ) -{ -return dom0_event; +/* +* minios stubdom looks up dom0's event channel from the command line +* (--event). The stubdom's own event channel is returned directly. +* +* Any other existing domains from dom0less/Hyperlaunch will have +* the event channel in the xenstore page, so lookup here isn't necessary. +* --event would not be set, so it would default to 0. +*/ +evtchn_port_t get_domain_evtchn (unsigned int domid) +{ +if (domid == stub_domid) +return xenbus_evtchn; +else if (domid == priv_domid) +return dom0_event; + +return 0 ; }void *xenbus_map (void )
tools/xenstored/posix.c +15 -1
@@ -139,7 +139,7 @@ void unmap_xenbus(void *interface)munmap (interface,getpagesize ()); } -evtchn_port_t get_xenbus_evtchn (void ) +static evtchn_port_t get_xenbus_evtchn (void ) {int fd;int rc;
@@ -166,6 +166,20 @@ evtchn_port_t get_xenbus_evtchn(void)return port; } +/* +* dom0 xenstored uses get_xenbus_evtchn() to lookup with XENSTORED_PORT_DEV. +* +* Any other existing domains from dom0less/Hyperlaunch will have +* the event channel in the xenstore page, so lookup here isn't necessary. +*/ +evtchn_port_t get_domain_evtchn (unsigned int domid) +{ +if (domid ==xenbus_master_domid ()) +return get_xenbus_evtchn (); + +return 0 ; +} +void *xenbus_map (void ) {int fd;
Signature · 1 line
2.50.1
PATCH v6 3/8 tools/xenstored: Auto-introduce domains
In reply to Jason AndryukReplace dom0_init() with init_domains() which uses libxenmanage to iterate through all existing domains, storing them in a list. The xenstore domain is introduced first, and then all the other domains are introduced. The xenstore domain needs to be introduced first to setup structures needed for firing watches.
dom0_domid is updated with the xenstore domain, since it really indicates the local domain.
priv_domid is set to the control domain. This makes it limited to a single domain.
These features let xenstore automatically connect any existing domains, which means it doesn't need to be done manually from init-dom0less.
For a legacy dom0, the result should be unchanged.
For a late xenstore stubdom it should also be the same, but priv_domid would be set automatically to control domain (which default to 0 normally).
Always signal the event channel for initial domains. This gets dom0 (a local xenstored domain) to connect.
Also always write XENSTORE_CONNECTED since we know we are connected at this point.
To support ARM dom0less domains with xen,enhanced = "no-xenstore" a failed introduce_domain() becomes non-fatal. Normally, HVM_PARAM_STORE_EVTCHN is used to identify .
priv_domid from the command line is used, or the first control domain is used.
dom0_domid will set to the last xenstore found. This will handle dom0 or dom0less, where only 1 xenstore domain can exist, or stubdom, where dom0 and dom1 exist, and we want to take the stubdom.
---
v6:
R-b: Juergen
v5:
Add init_domain() helper and call for dom0_domid first outside loop. Fix HVM_PARAM_STORE_EVTCHN typo
Only take first priv_domid and respect command line setting Take last dom0_domid
Set priv_domid & dom0_domid default values - bail if both unset Use talloc_realloc
Remove domain_conn_reset()
--- tools/xenstored/core.c | 6 +-- tools/xenstored/domain.c | 100 ++++++++++++++++++++++++++++++--------- tools/xenstored/domain.h | 2 +- 3 files changed, 81 insertions(+), 27 deletions(-)
tools/xenstored/core.c +3 -3
@@ -2567,9 +2567,9 @@ static struct option options[] = {#endif { NULL, 0, NULL, 0 } }; -int dom0_domid =0 ; +int dom0_domid =DOMID_INVALID ;int dom0_event =0 ; -int priv_domid =0 ; +int priv_domid =DOMID_INVALID ;domid_t stub_domid =DOMID_INVALID ;static unsigned int get_optval_uint (const char *arg)
@@ -2760,7 +2760,7 @@ int main(int argc, char *argv[])/* Listen to hypervisor. */ if (!live_update) {domain_init (-1 ); -dom0_init (); +init_domains (); }/* redirect to /dev/null now we're ready to accept connections */
tools/xenstored/domain.c +77 -23
120 lines not on this page · raw message
tools/xenstored/domain.h +1 -1
@@ -92,7 +92,7 @@ int do_set_feature(const void *ctx, struct connection *conn,void domain_early_init (void );void domain_init (int evtfd); -void dom0_init (void ); +void init_domains (void );void stubdom_init (bool live_update);void domain_deinit (void );void ignore_connection (struct connection *conn,unsigned int err);
Signature · 1 line
2.50.1
PATCH v6 4/8 tools/xenstored: Check NULL domain in domain_chk_quota()
In reply to Jason AndryukWith separate control and xenstore domains, the xenstore domain itself is no longer privileged. Local socket connections do not have conn->domain assigned, but domain_chk_quota() will go ahead and dereference it. The !domid_is_unprivileged(conn->id) check usually means the function would have already returned.
Bail out on !conn->domain to avoid faulting. This adds a similar check to that in domain_max_chk()
---
v6:
New
--- tools/xenstored/domain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xenstored/domain.c +1 -1
@@ -1762,7 +1762,7 @@ static bool domain_chk_quota(struct connection *conn, unsigned int mem)time_t now;struct domain *domain; -if (!conn || !domid_is_unprivileged (conn->id) || +if (!conn || !conn->domain || !domid_is_unprivileged (conn->id) || conn->is_ignored)return false;
Signature · 1 line
2.50.1
Re: [PATCH v6 4/8] tools/xenstored: Check NULL domain in domain_chk_quota()
Quoted 10 lines · Jason Andryuk
With separate control and xenstore domains, the xenstore domain itself is no longer privileged. Local socket connections do not have
conn->domainassigned, butdomain_chk_quota()will go ahead and dereference it. The !domid_is_unprivileged(conn->id) check usually means the function would have already returned.Bail out on !conn->domain to avoid faulting. This adds a similar check to that in
domain_max_chk()Signed-off-by Jason Andryuk <[email protected]>
Juergen
PATCH v6 5/8 tools/xenstored: Use priv_domid for manual nodes and permission
In reply to Jason AndryukUsually, priv_domid == dom0_domid == 0, and that is what is expected. If we rename s/dom0_domid/store_domid/, it seems more likely we want to actually have the priv_domid as the owner.
That leads to follow on changes to ensure that the priv_domid is created first.
---
v5:
R-b Juergen
s/Local/Privileged/ in comment
--- tools/xenstored/core.c | 4 ++-- tools/xenstored/domain.c | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-)
tools/xenstored/core.c +2 -2
@@ -2269,7 +2269,7 @@ struct connection *get_connection_by_id(unsigned int conn_id)static void manual_node (const char *name,const char *child) {struct node *node; -struct xs_permissions perms = { .id = dom0_domid, +struct xs_permissions perms = { .id = priv_domid, .perms =XS_PERM_NONE }; node = talloc_zero(NULL, struct node);
@@ -2320,7 +2320,7 @@ void setup_structure(bool live_update)manual_node ("/tool/xenstored" ,NULL );manual_node ("@releaseDomain" ,NULL );manual_node ("@introduceDomain" ,NULL ); -domain_nbentry_fix (dom0_domid,5 , true); +domain_nbentry_fix (priv_domid,5 , true); } }
tools/xenstored/domain.c +7 -7
@@ -1026,7 +1026,7 @@ static struct domain *introduce_domain(const void *ctx,struct domain *domain;int rc;struct xenstore_domain_interface *interface; -bool is_master_domain = (domid ==xenbus_master_domid ()); +bool is_priv_domain = (domid == priv_domid); domain =find_or_alloc_domain (ctx, domid);if (!domain)
@@ -1051,13 +1051,13 @@ static struct domain *introduce_domain(const void *ctx,if (!restore) interface->server_features = domain->features; -if (is_master_domain) +if (is_priv_domain)setup_structure (restore);/* Now domain belongs to its connection. */ talloc_steal (domain->conn, domain); -if (!is_master_domain && !restore) +if (!is_priv_domain && !restore)fire_special_watches ("@introduceDomain" ); }else {/* Use XS_INTRODUCE for recreating the xenbus event-channel. */
@@ -1392,15 +1392,15 @@ void init_domains(void)barf ("Could not determine xenstore domid\n" );/* -* Local domid must be first to setup structures for firing the special -* watches. +* Privileged domid must be first to setup structures for firing the +* special watches. */ -if (init_domain (dom0_domid)) +if (init_domain (priv_domid)) introduce_count++;for (unsigned int i =0 ; i < nr_domids; i++) { domid = domids[i]; -if (domid == dom0_domid) +if (domid == priv_domid)continue ;if (init_domain (domid))
Signature · 1 line
2.50.1
PATCH v6 6/8 tools/xenstored: Rename dom0_domid to store_domid
In reply to Jason AndryukThe dom0_domid variable is misnamed and conflates purposes. If we have xenstored running in a Linux domain that is not dom0, this variable controls the lookup of /proc/xen/xsd_kva and the event channel.
Rename to store_domid to better show its purpose.
One implication of this change is that the xenstore domain is not privileged by virtue of considering store_domid as privileged.
domain_is_unprivileged() removes the dom0_domid/store_domid check, so xenstore domain is no longer considered privileged.
onearg_domain() is updated to return EINVAL for store_domid or priv_domid to maintain the ability to call XS_RESUME.
xenbus_master_domid() is removed with store_domid being used instead.
Add a description of the -m/--master-domid options while doing this.
---
v6:
R-b: Juergen
v5:
Remove xenbus_master_domid() domain_is_unprivileged() drop dom0/store_domid. XS_RESUME/onearg_domain() check store_domid || priv_domid
--- tools/xenstored/core.c | 9 ++++++--- tools/xenstored/core.h | 6 ++---- tools/xenstored/domain.c | 14 +++++++------- tools/xenstored/posix.c | 4 ++-- 4 files changed, 17 insertions(+), 16 deletions(-)
tools/xenstored/core.c +6 -3
@@ -2539,7 +2539,10 @@ static void usage(void)" allowed timeout candidates are:\n" " watch-event: time a watch-event is kept pending\n" " -K, --keep-orphans don't delete nodes owned by a domain when the\n" -" domain is deleted (this is a security risk!)\n"); +" domain is deleted (this is a security risk!)\n" +" -m, --master-domid specify the domid of the domain where xenstored\n" +" is running. defaults to 0\n" +); }
@@ -2567,7 +2570,7 @@ static struct option options[] = {#endif { NULL, 0, NULL, 0 } }; -int dom0_domid =DOMID_INVALID ; +int store_domid =DOMID_INVALID ;int dom0_event =0 ;int priv_domid =DOMID_INVALID ;domid_t stub_domid =DOMID_INVALID ;
@@ -2736,7 +2739,7 @@ int main(int argc, char *argv[]) dom0_event =get_optval_uint (optarg);break ;case 'm' : - dom0_domid =get_optval_uint (optarg); + store_domid =get_optval_uint (optarg);break ;case 'p' : priv_domid =get_optval_uint (optarg);
tools/xenstored/core.h +2 -4
@@ -365,7 +365,7 @@ do { \trace ("tdb: " __VA_ARGS__); \ }while (0 ) -extern int dom0_domid; +extern int store_domid;extern int dom0_event;extern int priv_domid;extern domid_t stub_domid;
@@ -382,11 +382,9 @@ uint64_t get_now_msec(void);void *xenbus_map (void );void unmap_xenbus (void *interface); -static inline int xenbus_master_domid (void ) {return dom0_domid; } -static inline bool domid_is_unprivileged (unsigned int domid) { -return domid != dom0_domid && domid != priv_domid; +return domid != priv_domid; }static inline bool domain_is_unprivileged (const struct connection *conn)
tools/xenstored/domain.c +7 -7
@@ -509,7 +509,7 @@ static const struct interface_funcs domain_funcs = {static void *map_interface (domid_t domid) { -if (domid ==xenbus_master_domid ()) +if (domid == store_domid)return xenbus_map ();#ifdef __MINIOS__
@@ -524,7 +524,7 @@ static void *map_interface(domid_t domid)static void unmap_interface (domid_t domid,void *interface) { -if (domid ==xenbus_master_domid ()) +if (domid == store_domid)unmap_xenbus (interface);else if (domid != stub_domid)xengnttab_unmap (*xgt_handle, interface,1 );
@@ -1158,7 +1158,7 @@ static struct domain *onearg_domain(struct connection *conn,return ERR_PTR (-EINVAL ); domid =atoi (domid_str); -if (domid == dom0_domid) +if (domid == store_domid || domid == priv_domid)return ERR_PTR (-EINVAL );return find_connected_domain (domid);
@@ -1381,14 +1381,14 @@ void init_domains(void)* have 1 domain. stubdom there will be dom0 and dom1, * so this will take the second for stubdom. */ - dom0_domid = domid; + store_domid = domid; } } -if (dom0_domid ==DOMID_INVALID ) - dom0_domid = priv_domid; +if (store_domid ==DOMID_INVALID ) + store_domid = priv_domid; -if (dom0_domid ==DOMID_INVALID ) +if (store_domid ==DOMID_INVALID )barf ("Could not determine xenstore domid\n" );/*
tools/xenstored/posix.c +2 -2
@@ -174,7 +174,7 @@ static evtchn_port_t get_xenbus_evtchn(void)*/ evtchn_port_t get_domain_evtchn (unsigned int domid) { -if (domid ==xenbus_master_domid ()) +if (domid == store_domid)return get_xenbus_evtchn ();return 0 ;
@@ -280,7 +280,7 @@ static void accept_connection(int sock) conn =new_connection (&socket_funcs);if (conn) { conn->fd = fd; - conn->id = dom0_domid; + conn->id = store_domid; }else close (fd); }
Signature · 1 line
2.50.1
PATCH v6 7/8 tools/xenstored: Remove stubdom special casing
In reply to Jason Andryukposix.c and minios.c implement the same named functions serving slightly different purposes.
For xenbus_map()posix.c maps the local /dev/xen/xsd_kvaminios.c maps dom0 via grant and there is open coding for stub_domid in map_interface.
Change xenbus_map() to map the local domain's interface. The default grant table mapping is performed otherwise.
---
v6:
R-b: Juergen
v5:
Re-order later
--- tools/xenstored/domain.c | 5 ----- tools/xenstored/minios.c | 4 +--- 2 files changed, 1 insertion(+), 8 deletions(-)
tools/xenstored/domain.c +0 -5
@@ -512,11 +512,6 @@ static void *map_interface(domid_t domid)if (domid == store_domid)return xenbus_map (); -#ifdef __MINIOS__ -if (domid == stub_domid) -return xenstore_buf; -#endif -return xengnttab_map_grant_ref (*xgt_handle, domid,GNTTAB_RESERVED_XENSTORE ,PROT_READ |PROT_WRITE );
tools/xenstored/minios.c +1 -3
@@ -61,13 +61,11 @@ evtchn_port_t get_domain_evtchn(unsigned int domid)void *xenbus_map (void ) { -return xengnttab_map_grant_ref (*xgt_handle,xenbus_master_domid (), -GNTTAB_RESERVED_XENSTORE ,PROT_READ |PROT_WRITE ); +return xenstore_buf; }void unmap_xenbus (void *interface) { -xengnttab_unmap (*xgt_handle, interface,1 ); }void early_init (bool live_update,bool dofork,const char *pidfile)
Signature · 1 line
2.50.1
PATCH v6 8/8 tools/xenstored: Remove hardcoded implicit path
In reply to Jason AndryukUpdate get_implicit_path to return the correct value for a non-dom0 xenstored domain.
---
v5:
R-b: Juergen
--- tools/xenstored/domain.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
tools/xenstored/domain.c +6 -1
@@ -1312,11 +1312,13 @@ static int close_xgt_handle(void *_handle)return 0 ; } +static char store_domain_path[] ="/local/domain/65535" ; +/* Returns the implicit path of a connection (only domains have this) */ const char *get_implicit_path (const struct connection *conn) {if (!conn->domain) -return "/local/domain/0" ; +return store_domain_path;return conn->domain->path; }
@@ -1386,6 +1388,9 @@ void init_domains(void)if (store_domid ==DOMID_INVALID )barf ("Could not determine xenstore domid\n" ); +snprintf (store_domain_path,sizeof (store_domain_path), +"/local/domain/%u" , store_domid); +/* * Privileged domid must be first to setup structures for firing the * special watches.
Signature · 1 line
2.50.1