From nobody Thu Nov 28 12:56:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1670947544; cv=none; d=zohomail.com; s=zohoarc; b=YyPjZzSQk1NTf1JYA5ldCPMa8ObelJRChsDTwUZSG7xHu6Y+Hc5n49bvQs7KQ4suOic5A0omRrkItsIA2gw47ltmweCTVRJtYK+kZqLBwpkJlIlYIGEufUkxzBIloJgE4RSPnxyK9nsn6X9HEgTchLjKDNvalWz8ddqUFWQ1ixI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1670947544; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/yMggqf7XCSTmCxwrfXIT/H70kRm49+PbV95j/KSC7I=; b=DAG18A8Emw0VlZ347Sn10RiL4qLqrXqo1TUgX+Tx3qs8VcmYrqxNr4e+gOhrzrEaT6LaSdf33IvWnEZ566gf6LKLGbSOOd9HgSCZPrAAP8PIMI5GyK/EY4PhB6Me5imo35y/Jhe2pue13uo8r7L1zU6wHDWvozMriGemSWpf4jY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1670947544839355.5743569196421; Tue, 13 Dec 2022 08:05:44 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.460762.718764 (Exim 4.92) (envelope-from ) id 1p57mC-0005Y0-FJ; Tue, 13 Dec 2022 16:05:04 +0000 Received: by outflank-mailman (output) from mailman id 460762.718764; Tue, 13 Dec 2022 16:05:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1p57mC-0005Xh-7i; Tue, 13 Dec 2022 16:05:04 +0000 Received: by outflank-mailman (input) for mailman id 460762; Tue, 13 Dec 2022 16:05:02 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1p57ic-0001ta-IL for xen-devel@lists.xenproject.org; Tue, 13 Dec 2022 16:01:22 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 630383cb-7aff-11ed-8fd2-01056ac49cbb; Tue, 13 Dec 2022 17:01:21 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 6346D22087; Tue, 13 Dec 2022 16:01:21 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 37D3A138EE; Tue, 13 Dec 2022 16:01:21 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id xqFWDNGhmGO0KQAAMHmgww (envelope-from ); Tue, 13 Dec 2022 16:01:21 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 630383cb-7aff-11ed-8fd2-01056ac49cbb DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1670947281; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/yMggqf7XCSTmCxwrfXIT/H70kRm49+PbV95j/KSC7I=; b=eJTnmfVHa+Fe9VBT/jLUCFwiGUCSo2tGv5qe/2No3kcPkxN/9758h+w4QUV01yyazPe3PF 8l6B03c4QB08rVTiyoMKANlnRxi9B1S7RTZyMb4K2fYhsAWpmRqC0KNrN3RCPkWHTnZoO7 j66dojP344oQO/SeKeJu2Q2DI7wqMrE= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v2 06/19] tools/xenstore: add hashlist for finding struct domain by domid Date: Tue, 13 Dec 2022 17:00:32 +0100 Message-Id: <20221213160045.28170-7-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20221213160045.28170-1-jgross@suse.com> References: <20221213160045.28170-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1670947545332100002 Content-Type: text/plain; charset="utf-8" Today finding a struct domain by its domain id requires to scan the list of domains until finding the correct domid. Add a hashlist for being able to speed this up. This allows to remove the linking of struct domain in a list. Note that the list of changed domains per transaction is kept as a list, as there are no known use cases with more than 4 domains being touched in a single transaction (this would be a device handled by a driver domain and being assigned to a HVM domain with device model in a stubdom, plus the control domain). Some simple performance tests comparing the scanning and hashlist have shown that the hashlist will win as soon as more than 6 entries need to be scanned. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - add comment, fix return value of check_domain() (Julien Grall) --- tools/xenstore/xenstored_domain.c | 102 ++++++++++++++++++------------ 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index e669c89e94..3ad1028edb 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -48,8 +48,6 @@ static struct node_perms dom_introduce_perms; =20 struct domain { - struct list_head list; - /* The id of this domain */ unsigned int domid; =20 @@ -96,7 +94,7 @@ struct domain bool wrl_delay_logged; }; =20 -static LIST_HEAD(domains); +static struct hashtable *domhash; =20 static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod) { @@ -309,7 +307,7 @@ static int destroy_domain(void *_domain) =20 domain_tree_remove(domain); =20 - list_del(&domain->list); + hashtable_remove(domhash, &domain->domid); =20 if (!domain->introduced) return 0; @@ -341,43 +339,50 @@ static bool get_domain_info(unsigned int domid, xc_do= minfo_t *dominfo) dominfo->domid =3D=3D domid; } =20 -void check_domains(void) +static int check_domain(const void *k, void *v, void *arg) { xc_dominfo_t dominfo; - struct domain *domain; struct connection *conn; - int notify =3D 0; bool dom_valid; + struct domain *domain =3D v; + bool *notify =3D arg; =20 - again: - list_for_each_entry(domain, &domains, list) { - dom_valid =3D get_domain_info(domain->domid, &dominfo); - if (!domain->introduced) { - if (!dom_valid) { - talloc_free(domain); - goto again; - } - continue; - } - if (dom_valid) { - if ((dominfo.crashed || dominfo.shutdown) - && !domain->shutdown) { - domain->shutdown =3D true; - notify =3D 1; - } - if (!dominfo.dying) - continue; - } - if (domain->conn) { - /* domain is a talloc child of domain->conn. */ - conn =3D domain->conn; - domain->conn =3D NULL; - talloc_unlink(talloc_autofree_context(), conn); - notify =3D 0; /* destroy_domain() fires the watch */ - goto again; + dom_valid =3D get_domain_info(domain->domid, &dominfo); + if (!domain->introduced) { + if (!dom_valid) + talloc_free(domain); + return 0; + } + if (dom_valid) { + if ((dominfo.crashed || dominfo.shutdown) + && !domain->shutdown) { + domain->shutdown =3D true; + *notify =3D true; } + if (!dominfo.dying) + return 0; + } + if (domain->conn) { + /* domain is a talloc child of domain->conn. */ + conn =3D domain->conn; + domain->conn =3D NULL; + talloc_unlink(talloc_autofree_context(), conn); + *notify =3D false; /* destroy_domain() fires the watch */ + + /* Above unlink might result in 2 domains being freed! */ + return 1; } =20 + return 0; +} + +void check_domains(void) +{ + bool notify =3D false; + + while (hashtable_iterate(domhash, check_domain, ¬ify)) + ; + if (notify) fire_watches(NULL, NULL, "@releaseDomain", NULL, true, NULL); } @@ -415,13 +420,7 @@ static char *talloc_domain_path(const void *context, u= nsigned int domid) =20 static struct domain *find_domain_struct(unsigned int domid) { - struct domain *i; - - list_for_each_entry(i, &domains, list) { - if (i->domid =3D=3D domid) - return i; - } - return NULL; + return hashtable_search(domhash, &domid); } =20 int domain_get_quota(const void *ctx, struct connection *conn, @@ -470,9 +469,13 @@ static struct domain *alloc_domain(const void *context= , unsigned int domid) domain->generation =3D generation; domain->introduced =3D false; =20 - talloc_set_destructor(domain, destroy_domain); + if (!hashtable_insert(domhash, &domain->domid, domain)) { + talloc_free(domain); + errno =3D ENOMEM; + return NULL; + } =20 - list_add(&domain->list, &domains); + talloc_set_destructor(domain, destroy_domain); =20 return domain; } @@ -906,10 +909,25 @@ void dom0_init(void) xenevtchn_notify(xce_handle, dom0->port); } =20 +static unsigned int domhash_fn(void *k) +{ + return *(unsigned int *)k; +} + +static int domeq_fn(void *key1, void *key2) +{ + return *(unsigned int *)key1 =3D=3D *(unsigned int *)key2; +} + void domain_init(int evtfd) { int rc; =20 + /* Start with a random rather low domain count for the hashtable. */ + domhash =3D create_hashtable(8, domhash_fn, domeq_fn, 0); + if (!domhash) + barf_perror("Failed to allocate domain hashtable"); + xc_handle =3D talloc(talloc_autofree_context(), xc_interface*); if (!xc_handle) barf_perror("Failed to allocate domain handle"); --=20 2.35.3