From patchwork Tue Sep 13 09:32:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juergen Gross X-Patchwork-Id: 12974617 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C7A3FC6FA82 for ; Tue, 13 Sep 2022 09:33:20 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.406232.648627 (Exim 4.92) (envelope-from ) id 1oY2I0-0006NE-Pw; Tue, 13 Sep 2022 09:33:08 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 406232.648627; Tue, 13 Sep 2022 09:33:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oY2I0-0006N5-N6; Tue, 13 Sep 2022 09:33:08 +0000 Received: by outflank-mailman (input) for mailman id 406232; Tue, 13 Sep 2022 09:33:07 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oY2Hz-0006MX-KI for xen-devel@lists.xenproject.org; Tue, 13 Sep 2022 09:33:07 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2001:67c:2178:6::1d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 125de643-3347-11ed-a31c-8f8a9ae3403f; Tue, 13 Sep 2022 11:33:06 +0200 (CEST) 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-out2.suse.de (Postfix) with ESMTPS id 267C95C0F1; Tue, 13 Sep 2022 09:33:06 +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 E2EB513AB5; Tue, 13 Sep 2022 09:33:05 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id z1kxNlFOIGN6MwAAMHmgww (envelope-from ); Tue, 13 Sep 2022 09:33:05 +0000 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: 125de643-3347-11ed-a31c-8f8a9ae3403f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1663061586; 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=lx6c3891hAGbDFpKqMPhKGwIWWdvpb5PtucMX1OQj20=; b=V7FRTGGCSfXDd7GA62lTqPCmja654fPfHi28hgx4JJ3X0S/jQGkHafuDdBVTXNuLwo/ZKI B9/971Z1/pBw/0zNmddvQnkQWHnJIRQPARDeCblnfP33OqBzQOs+kP7y2sTWNyVzkgNh45 QebKl15vpmHmg55MonKJX+gey77f+tE= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu Subject: [PATCH v2 1/2] xen: add knownalive_domain_from_domid() helper Date: Tue, 13 Sep 2022 11:32:55 +0200 Message-Id: <20220913093256.22880-2-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220913093256.22880-1-jgross@suse.com> References: <20220913093256.22880-1-jgross@suse.com> MIME-Version: 1.0 Add a helper knownalive_domain_from_domid() returning the struct domain pointer for a domain give by its domid and which is known not being able to be released (its reference count isn't incremented and no rcu_lock_domain() is called for it). In order to simplify coding add an internal helper for doing the lookup and call that from the new function and similar functions. Signed-off-by: Juergen Gross --- V2: - rename helper to knownalive_domain_from_domid() (Jan Beulich) - enhance comment in header (Jan Beulich) - rename internal helper (Julien Grall) --- xen/common/domain.c | 53 +++++++++++++++++++++++++---------------- xen/include/xen/sched.h | 4 ++++ 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/xen/common/domain.c b/xen/common/domain.c index 8dd6cd5a8f..35e0dc5139 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -835,25 +835,32 @@ out: return 0; } - -struct domain *get_domain_by_id(domid_t dom) +/* rcu_read_lock(&domlist_read_lock) must be held. */ +static struct domain *domid_to_domain(domid_t dom) { struct domain *d; - rcu_read_lock(&domlist_read_lock); - for ( d = rcu_dereference(domain_hash[DOMAIN_HASH(dom)]); d != NULL; d = rcu_dereference(d->next_in_hashbucket) ) { if ( d->domain_id == dom ) - { - if ( unlikely(!get_domain(d)) ) - d = NULL; - break; - } + return d; } + return NULL; +} + +struct domain *get_domain_by_id(domid_t dom) +{ + struct domain *d; + + rcu_read_lock(&domlist_read_lock); + + d = domid_to_domain(dom); + if ( d && unlikely(!get_domain(d)) ) + d = NULL; + rcu_read_unlock(&domlist_read_lock); return d; @@ -862,20 +869,26 @@ struct domain *get_domain_by_id(domid_t dom) struct domain *rcu_lock_domain_by_id(domid_t dom) { - struct domain *d = NULL; + struct domain *d; rcu_read_lock(&domlist_read_lock); - for ( d = rcu_dereference(domain_hash[DOMAIN_HASH(dom)]); - d != NULL; - d = rcu_dereference(d->next_in_hashbucket) ) - { - if ( d->domain_id == dom ) - { - rcu_lock_domain(d); - break; - } - } + d = domid_to_domain(dom); + if ( d ) + rcu_lock_domain(d); + + rcu_read_unlock(&domlist_read_lock); + + return d; +} + +struct domain *knownalive_domain_from_domid(domid_t dom) +{ + struct domain *d; + + rcu_read_lock(&domlist_read_lock); + + d = domid_to_domain(dom); rcu_read_unlock(&domlist_read_lock); diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 557b3229f6..9e9c3d834b 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -737,8 +737,12 @@ static inline struct domain *rcu_lock_current_domain(void) return /*rcu_lock_domain*/(current->domain); } +/* Get struct domain AND increase ref-count of domain. */ struct domain *get_domain_by_id(domid_t dom); +/* Get struct domain known to have reference held or being RCU-locked. */ +struct domain *knownalive_domain_from_domid(domid_t dom); + struct domain *get_pg_owner(domid_t domid); static inline void put_pg_owner(struct domain *pg_owner) From patchwork Tue Sep 13 09:32:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juergen Gross X-Patchwork-Id: 12974619 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C88C8C6FA8A for ; Tue, 13 Sep 2022 09:33:22 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.406233.648638 (Exim 4.92) (envelope-from ) id 1oY2I6-0006gu-43; Tue, 13 Sep 2022 09:33:14 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 406233.648638; Tue, 13 Sep 2022 09:33:14 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oY2I6-0006gl-0m; Tue, 13 Sep 2022 09:33:14 +0000 Received: by outflank-mailman (input) for mailman id 406233; Tue, 13 Sep 2022 09:33:12 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oY2I4-0006MX-KT for xen-devel@lists.xenproject.org; Tue, 13 Sep 2022 09:33:12 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 15b43bcc-3347-11ed-a31c-8f8a9ae3403f; Tue, 13 Sep 2022 11:33:11 +0200 (CEST) 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 BC60434A56; Tue, 13 Sep 2022 09:33:11 +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 8515613AB5; Tue, 13 Sep 2022 09:33:11 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id sKg1H1dOIGOQMwAAMHmgww (envelope-from ); Tue, 13 Sep 2022 09:33:11 +0000 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: 15b43bcc-3347-11ed-a31c-8f8a9ae3403f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1663061591; 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=fCT/cuoPBGBD3g7UNXsZEuzKp2LwjEpE8HR7l0u5x5Q=; b=XWLZ7yzP442dEmPKWuPDa4r7Sf53z1Z5qEcTw1vtk49OHlGciYylr4mRFLj7Lk4ae6NBD0 HqFerIkB3T/PfTs9n9i1wYS+P+D/6I+Ks9G/2AXmb8lOaqEGo7nJUBGxQkBHfsOq8sKByx gAcj5M8uzlCXO+h5S/XPfT5myj98O6Y= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu Subject: [PATCH v2 2/2] xen/gnttab: reduce size of struct active_grant_entry Date: Tue, 13 Sep 2022 11:32:56 +0200 Message-Id: <20220913093256.22880-3-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220913093256.22880-1-jgross@suse.com> References: <20220913093256.22880-1-jgross@suse.com> MIME-Version: 1.0 The size of struct active_grant_entry for 64-bit builds is 40 or 48 bytes today (with or without NDEBUG). It can easily be reduced by 8 bytes by replacing the trans_domain pointer with the domid of the related domain. trans_domain is only ever used for transitive grants, which doesn't have any known users. This reduction will result in less memory usage and (for production builds) in faster code, as indexing into the active_grant_entry array will be much easier with an entry having a power-of-2 size. The performance loss when using transitive grants shouldn't really matter, given the probability that those aren't in use at all. Signed-off-by: Juergen Gross --- V2: - rename trans_domid to src_domid (Jan Beulich) --- xen/common/grant_table.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index fba329dcc2..59342df3b7 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -381,13 +381,13 @@ struct active_grant_entry { }) domid_t domid; /* Domain being granted access. */ + domid_t src_domid; /* Domain granting access. */ unsigned int start:15; /* For sub-page grants, the start offset in the page. */ bool is_sub_page:1; /* True if this is a sub-page grant. */ unsigned int length:16; /* For sub-page grants, the length of the grant. */ grant_ref_t trans_gref; - struct domain *trans_domain; mfn_t mfn; /* Machine frame being granted. */ #ifndef NDEBUG gfn_t gfn; /* Guest's idea of the frame being granted. */ @@ -1095,7 +1095,7 @@ map_grant_ref( act->start = 0; act->length = PAGE_SIZE; act->is_sub_page = false; - act->trans_domain = rd; + act->src_domid = rd->domain_id; act->trans_gref = ref; } } @@ -2494,7 +2494,8 @@ release_grant_for_copy( else { status = &status_entry(rgt, gref); - td = act->trans_domain; + td = (act->src_domid == rd->domain_id) + ? rd : knownalive_domain_from_domid(act->src_domid); trans_gref = act->trans_gref; } @@ -2657,7 +2658,7 @@ acquire_grant_for_copy( !mfn_eq(act->mfn, grant_mfn) || act->start != trans_page_off || act->length != trans_length || - act->trans_domain != td || + act->src_domid != td->domain_id || act->trans_gref != trans_gref || !act->is_sub_page)) ) { @@ -2676,7 +2677,7 @@ acquire_grant_for_copy( act->domid = ldom; act->start = trans_page_off; act->length = trans_length; - act->trans_domain = td; + act->src_domid = td->domain_id; act->trans_gref = trans_gref; act->mfn = grant_mfn; act_set_gfn(act, INVALID_GFN); @@ -2738,7 +2739,7 @@ acquire_grant_for_copy( act->is_sub_page = is_sub_page; act->start = trans_page_off; act->length = trans_length; - act->trans_domain = td; + act->src_domid = td->domain_id; act->trans_gref = trans_gref; act->mfn = grant_mfn; }