From patchwork Wed Aug 21 14:58:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 11107271 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 54F601399 for ; Wed, 21 Aug 2019 15:01:28 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 2625C20870 for ; Wed, 21 Aug 2019 15:01:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="LsCmpDQn" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2625C20870 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i0S5P-00008L-TU; Wed, 21 Aug 2019 14:59:43 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i0S5O-00007p-5W for xen-devel@lists.xenproject.org; Wed, 21 Aug 2019 14:59:42 +0000 X-Inumbo-ID: 4b3f2e1e-c424-11e9-8980-bc764e2007e4 Received: from esa5.hc3370-68.iphmx.com (unknown [216.71.155.168]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 4b3f2e1e-c424-11e9-8980-bc764e2007e4; Wed, 21 Aug 2019 14:59:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1566399577; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5bT8VNZBi8pv4KOOjesiQnYptMnqBRVha5j2EfSm4GY=; b=LsCmpDQn86NiMcfeHZX0OP69Kk5ioTjVwZB2XpbP0rH9S3DJvn7fjS2w FExpL9igH9k0IyPC6ptH6jGsdy/oO08AhiV/hJjbdaBMaQTru3LVYLt5/ 2WloDlqU11DLOsr8VQXFUDCQTxbBr09gCKnm45uJqaTYjhkXeBHRkO4IC w=; Authentication-Results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=roger.pau@citrix.com; spf=Pass smtp.mailfrom=roger.pau@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa5.hc3370-68.iphmx.com: no sender authenticity information available from domain of roger.pau@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa5.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa5.hc3370-68.iphmx.com: domain of roger.pau@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa5.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa5.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa5.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: nCXijWtaoMBfqm+VIndD5WjgmqD16URcrxVUc/eP4rVfGQFzRKlW/bEesPhzOmJCqT78C4eT4k rwF+TdsPceBSaxVriVN+/JXo2cAALyOgC1UgVPGWzNxVhNZS1uS13L/w5Rsp9IjHZ56E8xlDUI yiGQw5HnMmj3SkhFnOIOO2nDm21U8NOW3YyZr+eLpKNMvmIBsN2k1bPdDF52at331l2aroQ/MB AsCQ+HT3kz/8fGd9+AaCO3PGXw/l0frgyamdGXO9pF9suKTota2pPQdZdftTuAJZDdWOrtURgO CXI= X-SBRS: 2.7 X-MesageID: 4717068 X-Ironport-Server: esa5.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.64,412,1559534400"; d="scan'208";a="4717068" From: Roger Pau Monne To: Date: Wed, 21 Aug 2019 16:58:58 +0200 Message-ID: <20190821145903.45934-3-roger.pau@citrix.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190821145903.45934-1-roger.pau@citrix.com> References: <20190821145903.45934-1-roger.pau@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 2/7] ioreq: add internal ioreq initialization support X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Paul Durrant , Wei Liu , Jan Beulich , Roger Pau Monne Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Add support for internal ioreq servers to initialization and deinitialization routines, prevent some functions from being executed against internal ioreq servers and add guards to only allow internal callers to modify internal ioreq servers. External callers (ie: from hypercalls) are only allowed to deal with external ioreq servers. Signed-off-by: Roger Pau Monné --- xen/arch/x86/hvm/dm.c | 9 +- xen/arch/x86/hvm/ioreq.c | 150 +++++++++++++++++++++----------- xen/include/asm-x86/hvm/ioreq.h | 8 +- 3 files changed, 108 insertions(+), 59 deletions(-) diff --git a/xen/arch/x86/hvm/dm.c b/xen/arch/x86/hvm/dm.c index d6d0e8be89..5ca8b66d67 100644 --- a/xen/arch/x86/hvm/dm.c +++ b/xen/arch/x86/hvm/dm.c @@ -417,7 +417,7 @@ static int dm_op(const struct dmop_args *op_args) break; rc = hvm_create_ioreq_server(d, data->handle_bufioreq, - &data->id); + &data->id, false); break; } @@ -452,7 +452,7 @@ static int dm_op(const struct dmop_args *op_args) break; rc = hvm_map_io_range_to_ioreq_server(d, data->id, data->type, - data->start, data->end); + data->start, data->end, false); break; } @@ -466,7 +466,8 @@ static int dm_op(const struct dmop_args *op_args) break; rc = hvm_unmap_io_range_from_ioreq_server(d, data->id, data->type, - data->start, data->end); + data->start, data->end, + false); break; } @@ -529,7 +530,7 @@ static int dm_op(const struct dmop_args *op_args) if ( data->pad ) break; - rc = hvm_set_ioreq_server_state(d, data->id, !!data->enabled); + rc = hvm_set_ioreq_server_state(d, data->id, !!data->enabled, false); break; } diff --git a/xen/arch/x86/hvm/ioreq.c b/xen/arch/x86/hvm/ioreq.c index a79cabb680..23ef9b0c02 100644 --- a/xen/arch/x86/hvm/ioreq.c +++ b/xen/arch/x86/hvm/ioreq.c @@ -89,6 +89,9 @@ bool hvm_io_pending(struct vcpu *v) { struct hvm_ioreq_vcpu *sv; + if ( s->internal ) + continue; + list_for_each_entry ( sv, &s->ioreq_vcpu_list, list_entry ) @@ -193,6 +196,9 @@ bool handle_hvm_io_completion(struct vcpu *v) { struct hvm_ioreq_vcpu *sv; + if ( s->internal ) + continue; + list_for_each_entry ( sv, &s->ioreq_vcpu_list, list_entry ) @@ -431,6 +437,9 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page) FOR_EACH_IOREQ_SERVER(d, id, s) { + if ( s->internal ) + continue; + if ( (s->ioreq.page == page) || (s->bufioreq.page == page) ) { found = true; @@ -696,15 +705,18 @@ static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s) if ( s->enabled ) goto done; - hvm_remove_ioreq_gfn(s, false); - hvm_remove_ioreq_gfn(s, true); + if ( !s->internal ) + { + hvm_remove_ioreq_gfn(s, false); + hvm_remove_ioreq_gfn(s, true); - s->enabled = true; + list_for_each_entry ( sv, + &s->ioreq_vcpu_list, + list_entry ) + hvm_update_ioreq_evtchn(s, sv); + } - list_for_each_entry ( sv, - &s->ioreq_vcpu_list, - list_entry ) - hvm_update_ioreq_evtchn(s, sv); + s->enabled = true; done: spin_unlock(&s->lock); @@ -717,8 +729,11 @@ static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s) if ( !s->enabled ) goto done; - hvm_add_ioreq_gfn(s, true); - hvm_add_ioreq_gfn(s, false); + if ( !s->internal ) + { + hvm_add_ioreq_gfn(s, true); + hvm_add_ioreq_gfn(s, false); + } s->enabled = false; @@ -728,40 +743,47 @@ static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s) static int hvm_ioreq_server_init(struct hvm_ioreq_server *s, struct domain *d, int bufioreq_handling, - ioservid_t id) + ioservid_t id, bool internal) { struct domain *currd = current->domain; struct vcpu *v; int rc; + s->internal = internal; s->target = d; - - get_knownalive_domain(currd); - s->emulator = currd; - spin_lock_init(&s->lock); - INIT_LIST_HEAD(&s->ioreq_vcpu_list); - spin_lock_init(&s->bufioreq_lock); - - s->ioreq.gfn = INVALID_GFN; - s->bufioreq.gfn = INVALID_GFN; rc = hvm_ioreq_server_alloc_rangesets(s, id); if ( rc ) return rc; - s->bufioreq_handling = bufioreq_handling; - - for_each_vcpu ( d, v ) + if ( !internal ) { - rc = hvm_ioreq_server_add_vcpu(s, v); - if ( rc ) - goto fail_add; + get_knownalive_domain(currd); + + s->emulator = currd; + INIT_LIST_HEAD(&s->ioreq_vcpu_list); + spin_lock_init(&s->bufioreq_lock); + + s->ioreq.gfn = INVALID_GFN; + s->bufioreq.gfn = INVALID_GFN; + + s->bufioreq_handling = bufioreq_handling; + + for_each_vcpu ( d, v ) + { + rc = hvm_ioreq_server_add_vcpu(s, v); + if ( rc ) + goto fail_add; + } } + else + s->handler = NULL; return 0; fail_add: + ASSERT(!internal); hvm_ioreq_server_remove_all_vcpus(s); hvm_ioreq_server_unmap_pages(s); @@ -774,27 +796,31 @@ static int hvm_ioreq_server_init(struct hvm_ioreq_server *s, static void hvm_ioreq_server_deinit(struct hvm_ioreq_server *s) { ASSERT(!s->enabled); - hvm_ioreq_server_remove_all_vcpus(s); - - /* - * NOTE: It is safe to call both hvm_ioreq_server_unmap_pages() and - * hvm_ioreq_server_free_pages() in that order. - * This is because the former will do nothing if the pages - * are not mapped, leaving the page to be freed by the latter. - * However if the pages are mapped then the former will set - * the page_info pointer to NULL, meaning the latter will do - * nothing. - */ - hvm_ioreq_server_unmap_pages(s); - hvm_ioreq_server_free_pages(s); hvm_ioreq_server_free_rangesets(s); - put_domain(s->emulator); + if ( !s->internal ) + { + hvm_ioreq_server_remove_all_vcpus(s); + + /* + * NOTE: It is safe to call both hvm_ioreq_server_unmap_pages() and + * hvm_ioreq_server_free_pages() in that order. + * This is because the former will do nothing if the pages + * are not mapped, leaving the page to be freed by the latter. + * However if the pages are mapped then the former will set + * the page_info pointer to NULL, meaning the latter will do + * nothing. + */ + hvm_ioreq_server_unmap_pages(s); + hvm_ioreq_server_free_pages(s); + + put_domain(s->emulator); + } } int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling, - ioservid_t *id) + ioservid_t *id, bool internal) { struct hvm_ioreq_server *s; unsigned int i; @@ -826,7 +852,7 @@ int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling, */ set_ioreq_server(d, i, s); - rc = hvm_ioreq_server_init(s, d, bufioreq_handling, i); + rc = hvm_ioreq_server_init(s, d, bufioreq_handling, i, internal); if ( rc ) { set_ioreq_server(d, i, NULL); @@ -863,7 +889,8 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) goto out; rc = -EPERM; - if ( s->emulator != current->domain ) + /* NB: internal servers cannot be destroyed. */ + if ( s->internal || s->emulator != current->domain ) goto out; domain_pause(d); @@ -908,7 +935,11 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id, goto out; rc = -EPERM; - if ( s->emulator != current->domain ) + /* + * NB: don't allow external callers to fetch information about internal + * ioreq servers. + */ + if ( s->internal || s->emulator != current->domain ) goto out; if ( ioreq_gfn || bufioreq_gfn ) @@ -955,7 +986,7 @@ int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id, goto out; rc = -EPERM; - if ( s->emulator != current->domain ) + if ( s->internal || s->emulator != current->domain ) goto out; rc = hvm_ioreq_server_alloc_pages(s); @@ -991,7 +1022,7 @@ int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id, int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, uint32_t type, uint64_t start, - uint64_t end) + uint64_t end, bool internal) { struct hvm_ioreq_server *s; struct rangeset *r; @@ -1009,7 +1040,12 @@ int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, goto out; rc = -EPERM; - if ( s->emulator != current->domain ) + /* + * NB: don't allow external callers to modify the ranges of internal + * servers. + */ + if ( (s->internal != internal) || + (!internal && s->emulator != current->domain) ) goto out; switch ( type ) @@ -1043,7 +1079,7 @@ int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id, uint32_t type, uint64_t start, - uint64_t end) + uint64_t end, bool internal) { struct hvm_ioreq_server *s; struct rangeset *r; @@ -1061,7 +1097,12 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id, goto out; rc = -EPERM; - if ( s->emulator != current->domain ) + /* + * NB: don't allow external callers to modify the ranges of internal + * servers. + */ + if ( s->internal != internal || + (!internal && s->emulator != current->domain) ) goto out; switch ( type ) @@ -1122,7 +1163,7 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, goto out; rc = -EPERM; - if ( s->emulator != current->domain ) + if ( s->internal || s->emulator != current->domain ) goto out; rc = p2m_set_ioreq_server(d, flags, s); @@ -1142,7 +1183,7 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, } int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id, - bool enabled) + bool enabled, bool internal) { struct hvm_ioreq_server *s; int rc; @@ -1156,7 +1197,8 @@ int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id, goto out; rc = -EPERM; - if ( s->emulator != current->domain ) + if ( s->internal != internal || + (!internal && s->emulator != current->domain) ) goto out; domain_pause(d); @@ -1185,6 +1227,8 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v) FOR_EACH_IOREQ_SERVER(d, id, s) { + if ( s->internal ) + continue; rc = hvm_ioreq_server_add_vcpu(s, v); if ( rc ) goto fail; @@ -1218,7 +1262,11 @@ void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v) spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); FOR_EACH_IOREQ_SERVER(d, id, s) + { + if ( s->internal ) + continue; hvm_ioreq_server_remove_vcpu(s, v); + } spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); } diff --git a/xen/include/asm-x86/hvm/ioreq.h b/xen/include/asm-x86/hvm/ioreq.h index e2588e912f..e8119b26a6 100644 --- a/xen/include/asm-x86/hvm/ioreq.h +++ b/xen/include/asm-x86/hvm/ioreq.h @@ -24,7 +24,7 @@ bool handle_hvm_io_completion(struct vcpu *v); bool is_ioreq_server_page(struct domain *d, const struct page_info *page); int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling, - ioservid_t *id); + ioservid_t *id, bool internal); int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id); int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id, unsigned long *ioreq_gfn, @@ -34,14 +34,14 @@ int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id, unsigned long idx, mfn_t *mfn); int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, uint32_t type, uint64_t start, - uint64_t end); + uint64_t end, bool internal); int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id, uint32_t type, uint64_t start, - uint64_t end); + uint64_t end, bool internal); int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, uint32_t type, uint32_t flags); int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id, - bool enabled); + bool enabled, bool internal); int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v); void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v);