From nobody Fri Apr 19 18:53:57 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=fail; 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=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1595936288; cv=none; d=zohomail.com; s=zohoarc; b=gcV5xWepbS5AhoxxSnB90C4ZlDSEwHwC6XAIZzPjVHahHAylP1fwZZcR6XeUMbZjxAugt7Bt6S3aaA0MU1gyrwQfNihzoq5XZPIXesU3lAAv67l6T/T0qu4G/A9wa/+clmPKsTNzERKePMAEfm5UahaYMT3xJQg3zmqFXnPCD1s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595936288; h=Content-Type: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=a4Gjw00MOr93HpVEmjf8sUAuJ7cQgFqBba0j+SyrIjI=; b=esbfBdI4160NIXLLF0PsASvzP7Vp+dpWGBDmAZzzXYLK972oF/R4j4kDnjfrxBEO75ogcmiUKG0HsxpnyA8RntgwyPFqoh7mmLOHJes0h3UBKaIIlrn1myEMa7A2nOrkS+hbO4hYLnc936qkcfbomAxXCRHXJYmpNIwMz/UGTZ8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1595936288407138.73504755676652; Tue, 28 Jul 2020 04:38:08 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvZ-0006Gx-HM; Tue, 28 Jul 2020 11:37:49 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvY-0006Bw-Pl for xen-devel@lists.xenproject.org; Tue, 28 Jul 2020 11:37:48 +0000 Received: from esa4.hc3370-68.iphmx.com (unknown [216.71.155.144]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id bd6816ae-d0c6-11ea-a8af-12813bfff9fa; Tue, 28 Jul 2020 11:37:39 +0000 (UTC) X-Inumbo-ID: bd6816ae-d0c6-11ea-a8af-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1595936259; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RkXLHOS6wpf6G4+l7e752XZD9SMg/CfYFIDpBklde9o=; b=UcOibjlZKdsXXmgl2O37fcJAMRgllKBVnkO4kyiP5Ket5e2D8pok7zbg qpYun1NeYIG4176lkYGWebFwQ81vp8RuhT3/3IuLrkQiuY4b/jAXP8bj4 ZhgPOFJ7fa38OyvXrvYfVUKABG5bH+y+F1ezBZ5Nkz6BwGtz01gRZR9n1 8=; Authentication-Results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: +NVwCWu+BqEKtJP+3sNKXmvYcsDwY3TNoqsX/w6ha0A1PxR8C7sdHpDcaDYy54oGf00KjegAz8 IW5ca5hUNk8RSkFr0dn+7l59ixRfGcfoXDkblqTU/gfTTiiy82fY0uW/MGawNQ1e7ZUMd/9jEc 3Emj+Z+Ltol8BTtjVr8OqF2uDwMwXA8Dl3+qQ6a+y5yZ+pSwKIP/GhMnbAhUP56Zoj5XhSaHER JwBJHoAC93xw00Jgn/T4bsYwWezZncg5UKlZbh6hE8hzUpPZ2G2WkWA0vJliIWwxHo4kYAccEb 3s8= X-SBRS: 2.7 X-MesageID: 24202968 X-Ironport-Server: esa4.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.75,406,1589256000"; d="scan'208";a="24202968" From: Andrew Cooper To: Xen-devel Subject: [PATCH 1/5] xen/memory: Introduce CONFIG_ARCH_ACQUIRE_RESOURCE Date: Tue, 28 Jul 2020 12:37:07 +0100 Message-ID: <20200728113712.22966-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200728113712.22966-1-andrew.cooper3@citrix.com> References: <20200728113712.22966-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Paul Durrant , Andrew Cooper , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Jan Beulich , Hubert Jasudowicz , Volodymyr Babchuk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) New architectures shouldn't be forced to implement no-op stubs for unused functionality. Introduce CONFIG_ARCH_ACQUIRE_RESOURCE which can be opted in to, and provide compatibility logic in xen/mm.h No functional change. Signed-off-by: Andrew Cooper Acked-by: Jan Beulich Acked-by: Julien Grall Reviewed-by: Paul Durrant --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monn=C3=A9 CC: Stefano Stabellini CC: Julien Grall CC: Volodymyr Babchuk CC: Paul Durrant CC: Micha=C5=82 Leszczy=C5=84ski CC: Hubert Jasudowicz --- xen/arch/x86/Kconfig | 1 + xen/common/Kconfig | 3 +++ xen/include/asm-arm/mm.h | 8 -------- xen/include/xen/mm.h | 9 +++++++++ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/xen/arch/x86/Kconfig b/xen/arch/x86/Kconfig index a636a4bb1e..e7644a0a9d 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -6,6 +6,7 @@ config X86 select ACPI select ACPI_LEGACY_TABLES_LOOKUP select ARCH_SUPPORTS_INT128 + select ARCH_ACQUIRE_RESOURCE select COMPAT select CORE_PARKING select HAS_ALTERNATIVE diff --git a/xen/common/Kconfig b/xen/common/Kconfig index 15e3b79ff5..593459ea6e 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -22,6 +22,9 @@ config GRANT_TABLE =20 If unsure, say Y. =20 +config ARCH_ACQUIRE_RESOURCE + bool + config HAS_ALTERNATIVE bool =20 diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h index f8ba49b118..0b7de3102e 100644 --- a/xen/include/asm-arm/mm.h +++ b/xen/include/asm-arm/mm.h @@ -358,14 +358,6 @@ static inline void put_page_and_type(struct page_info = *page) =20 void clear_and_clean_page(struct page_info *page); =20 -static inline -int arch_acquire_resource(struct domain *d, unsigned int type, unsigned in= t id, - unsigned long frame, unsigned int nr_frames, - xen_pfn_t mfn_list[]) -{ - return -EOPNOTSUPP; -} - unsigned int arch_get_dma_bitsize(void); =20 #endif /* __ARCH_ARM_MM__ */ diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 1061765bcd..1b2c1f6b32 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -685,4 +685,13 @@ static inline void put_page_alloc_ref(struct page_info= *page) } } =20 +#ifndef CONFIG_ARCH_ACQUIRE_RESOURCE +static inline int arch_acquire_resource( + struct domain *d, unsigned int type, unsigned int id, unsigned long fr= ame, + unsigned int nr_frames, xen_pfn_t mfn_list[]) +{ + return -EOPNOTSUPP; +} +#endif /* !CONFIG_ARCH_ACQUIRE_RESOURCE */ + #endif /* __XEN_MM_H__ */ --=20 2.11.0 From nobody Fri Apr 19 18:53:57 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=fail; 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=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1595936282; cv=none; d=zohomail.com; s=zohoarc; b=IkS0C9TcPMpk88xhblJ0qrBVTAODpPspb+f4OWLg8V9YFBpsjY3D6XVWfNrSlmRzbdw3NRHs9eDEuLeL5HJ8A9uIeHa17jdXy8SjWNM1SjOgMdrET4+nkBc2NvKxw+4mUsoSCkORESpx+ON0ppLp+s3cBXvtlNvV516Ed0md428= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595936282; h=Content-Type: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=WofKXicyWcsm5ymDktuj/UmYdfVzMt8bhqnv+GeEPKQ=; b=Y3h/pOXSY7W7hAxVHWLhhj/X5n2MXDgwu37yb4N/CReFI8RVe32QIyZvv1v/bsR9k3Mi5wOm4PlKUZzCvwJ65sZ0zey9nS+zj28Zt7Mnxci31mgPitI2EO6c3koUVDKhczHKXNPZ2LoxKAbQ/g31uwigPmdJdnZ5oX7aPBOZeiM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1595936282401104.19004205228771; Tue, 28 Jul 2020 04:38:02 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvL-0006C7-97; Tue, 28 Jul 2020 11:37:35 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvJ-0006Bw-Tb for xen-devel@lists.xenproject.org; Tue, 28 Jul 2020 11:37:33 +0000 Received: from esa5.hc3370-68.iphmx.com (unknown [216.71.155.168]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id b9750142-d0c6-11ea-a8af-12813bfff9fa; Tue, 28 Jul 2020 11:37:32 +0000 (UTC) X-Inumbo-ID: b9750142-d0c6-11ea-a8af-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1595936252; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C30vCiBJRWVkXa2f7MgjSiHtu+zIa4xf5Yikt9EeBnM=; b=fdH3oDwtkYReN3a/EGRfwUz4TF2umd7FiSBCUdJo6YacrK9ZerkYKUei kikt1cYEfGYvtXdJyn0VsACQiib9wEJx5zungJcYBMUczjRQsWhhCg0D1 +UxTjigSksyn7cOxJTeKXgwJUu3GcofzSxqlCrNZTcD7fH8xaTMmB5NdH 8=; Authentication-Results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: v8/lpfGxqn1loOuLjuecXkShCkCaTzInJqJvVSwD90xze7nR9dWxBGetJ8AkmkPDm6ggBCixtj fWgVUCLAw5wVADZIE/CwCCxOHO6HooXWpZyta1JLuWM5MuVJYdMxy9lBWTZvEC+zgLh1S/tlm6 aIzCb7YW6EpP8kpY/IcUlqBqP08k+uLsOlUvjdhrILKPD1gZqLHopcC8LU6l8bC0ewQkHqdlCo RHpjcua+Ef5zJdHsxucOLdUb96w23iXIsKBKaYbzokWui+CgrRwBsaZcyqU1iEZz4utw3fCO4Y plY= X-SBRS: 2.7 X-MesageID: 23524684 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.75,406,1589256000"; d="scan'208";a="23524684" From: Andrew Cooper To: Xen-devel Subject: [PATCH 2/5] xen/gnttab: Rework resource acquisition Date: Tue, 28 Jul 2020 12:37:08 +0100 Message-ID: <20200728113712.22966-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200728113712.22966-1-andrew.cooper3@citrix.com> References: <20200728113712.22966-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Hubert Jasudowicz , Stefano Stabellini , Julien Grall , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Paul Durrant , Jan Beulich , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) The existing logic doesn't function in the general case for mapping a guests grant table, due to arbitrary 32 frame limit, and the default grant table limit being 64. In order to start addressing this, rework the existing grant table logic by implementing a single gnttab_acquire_resource(). This is far more efficient than the previous acquire_grant_table() in memory.c because it doesn't take the grant table write lock, and attempt to grow the table, for every single frame. The new gnttab_acquire_resource() function subsumes the previous two gnttab_get_{shared,status}_frame() helpers. No functional change. Signed-off-by: Andrew Cooper --- CC: George Dunlap CC: Ian Jackson CC: Jan Beulich CC: Konrad Rzeszutek Wilk CC: Stefano Stabellini CC: Wei Liu CC: Julien Grall CC: Paul Durrant CC: Micha=C5=82 Leszczy=C5=84ski CC: Hubert Jasudowicz --- xen/common/grant_table.c | 93 ++++++++++++++++++++++++++++++---------= ---- xen/common/memory.c | 42 +------------------ xen/include/xen/grant_table.h | 19 ++++----- 3 files changed, 75 insertions(+), 79 deletions(-) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 9f0cae52c0..122d1e7596 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -4013,6 +4013,72 @@ static int gnttab_get_shared_frame_mfn(struct domain= *d, return 0; } =20 +int gnttab_acquire_resource( + struct domain *d, unsigned int id, unsigned long frame, + unsigned int nr_frames, xen_pfn_t mfn_list[]) +{ + struct grant_table *gt =3D d->grant_table; + unsigned int i =3D nr_frames, tot_frames; + void **vaddrs; + int rc =3D 0; + + /* Input sanity. */ + if ( !nr_frames ) + return -EINVAL; + + /* Overflow checks */ + if ( frame + nr_frames < frame ) + return -EINVAL; + + tot_frames =3D frame + nr_frames; + if ( tot_frames !=3D frame + nr_frames ) + return -EINVAL; + + /* Grow table if necessary. */ + grant_write_lock(gt); + switch ( id ) + { + mfn_t tmp; + + case XENMEM_resource_grant_table_id_shared: + rc =3D gnttab_get_shared_frame_mfn(d, tot_frames - 1, &tmp); + break; + + case XENMEM_resource_grant_table_id_status: + if ( gt->gt_version !=3D 2 ) + { + default: + rc =3D -EINVAL; + break; + } + rc =3D gnttab_get_status_frame_mfn(d, tot_frames - 1, &tmp); + break; + } + + /* Any errors from growing the table? */ + if ( rc ) + goto out; + + switch ( id ) + { + case XENMEM_resource_grant_table_id_shared: + vaddrs =3D gt->shared_raw; + break; + + case XENMEM_resource_grant_table_id_status: + vaddrs =3D (void **)gt->status; + break; + } + + for ( i =3D 0; i < nr_frames; ++i ) + mfn_list[i] =3D virt_to_mfn(vaddrs[frame + i]); + + out: + grant_write_unlock(gt); + + return rc; +} + int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t= *mfn) { int rc =3D 0; @@ -4047,33 +4113,6 @@ int gnttab_map_frame(struct domain *d, unsigned long= idx, gfn_t gfn, mfn_t *mfn) return rc; } =20 -int gnttab_get_shared_frame(struct domain *d, unsigned long idx, - mfn_t *mfn) -{ - struct grant_table *gt =3D d->grant_table; - int rc; - - grant_write_lock(gt); - rc =3D gnttab_get_shared_frame_mfn(d, idx, mfn); - grant_write_unlock(gt); - - return rc; -} - -int gnttab_get_status_frame(struct domain *d, unsigned long idx, - mfn_t *mfn) -{ - struct grant_table *gt =3D d->grant_table; - int rc; - - grant_write_lock(gt); - rc =3D (gt->gt_version =3D=3D 2) ? - gnttab_get_status_frame_mfn(d, idx, mfn) : -EINVAL; - grant_write_unlock(gt); - - return rc; -} - static void gnttab_usage_print(struct domain *rd) { int first =3D 1; diff --git a/xen/common/memory.c b/xen/common/memory.c index 714077c1e5..dc3a7248e3 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1007,44 +1007,6 @@ static long xatp_permission_check(struct domain *d, = unsigned int space) return xsm_add_to_physmap(XSM_TARGET, current->domain, d); } =20 -static int acquire_grant_table(struct domain *d, unsigned int id, - unsigned long frame, - unsigned int nr_frames, - xen_pfn_t mfn_list[]) -{ - unsigned int i =3D nr_frames; - - /* Iterate backwards in case table needs to grow */ - while ( i-- !=3D 0 ) - { - mfn_t mfn =3D INVALID_MFN; - int rc; - - switch ( id ) - { - case XENMEM_resource_grant_table_id_shared: - rc =3D gnttab_get_shared_frame(d, frame + i, &mfn); - break; - - case XENMEM_resource_grant_table_id_status: - rc =3D gnttab_get_status_frame(d, frame + i, &mfn); - break; - - default: - rc =3D -EINVAL; - break; - } - - if ( rc ) - return rc; - - ASSERT(!mfn_eq(mfn, INVALID_MFN)); - mfn_list[i] =3D mfn_x(mfn); - } - - return 0; -} - static int acquire_resource( XEN_GUEST_HANDLE_PARAM(xen_mem_acquire_resource_t) arg) { @@ -1091,8 +1053,8 @@ static int acquire_resource( switch ( xmar.type ) { case XENMEM_resource_grant_table: - rc =3D acquire_grant_table(d, xmar.id, xmar.frame, xmar.nr_frames, - mfn_list); + rc =3D gnttab_acquire_resource(d, xmar.id, xmar.frame, xmar.nr_fra= mes, + mfn_list); break; =20 default: diff --git a/xen/include/xen/grant_table.h b/xen/include/xen/grant_table.h index 98603604b8..5a2c75b880 100644 --- a/xen/include/xen/grant_table.h +++ b/xen/include/xen/grant_table.h @@ -56,10 +56,10 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, gra= nt_ref_t ref, =20 int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t *mfn); -int gnttab_get_shared_frame(struct domain *d, unsigned long idx, - mfn_t *mfn); -int gnttab_get_status_frame(struct domain *d, unsigned long idx, - mfn_t *mfn); + +int gnttab_acquire_resource( + struct domain *d, unsigned int id, unsigned long frame, + unsigned int nr_frames, xen_pfn_t mfn_list[]); =20 #else =20 @@ -93,14 +93,9 @@ static inline int gnttab_map_frame(struct domain *d, uns= igned long idx, return -EINVAL; } =20 -static inline int gnttab_get_shared_frame(struct domain *d, unsigned long = idx, - mfn_t *mfn) -{ - return -EINVAL; -} - -static inline int gnttab_get_status_frame(struct domain *d, unsigned long = idx, - mfn_t *mfn) +static inline int gnttab_acquire_resource( + struct domain *d, unsigned int id, unsigned long frame, + unsigned int nr_frames, xen_pfn_t mfn_list[]) { return -EINVAL; } --=20 2.11.0 From nobody Fri Apr 19 18:53:57 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=fail; 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=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1595936282; cv=none; d=zohomail.com; s=zohoarc; b=W34ks10eWG4kkyKBMNh0n2mi4yvByja46UkMKr17c4xotYvfUusDQJa34ERGnggMiqhG3vUiiY5kxJoWxgu7YGi4SA0ZaFoBaC9h593ZpCKX8v9dJjWYJkJwWSNlPRhLAEtNaUv9UdtDAVRXwHF3757KwfHS7LpwPfBuWytenDM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595936282; h=Content-Type: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=+SYcpVkzIik/eUpfRCRKytRGL6vWyx6GBG++Y8+Y3FA=; b=Ox00bbzPTVp0XZBtLztkjMC8N4ePaDw/GI+2sqOrQ1T0i2Da8ZaTVePXqOVqK+a1+cBgpnsduzXk07cKnWYc8zs2L/4ROjAP0ck7jzLZpI39lgzB98osQtIQRw8bf3ZBMIMwKIQY+kpkSmG1v+1g4mgWfvQT/IR7dUSPYuLBg9w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1595936282499681.0201824495912; Tue, 28 Jul 2020 04:38:02 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvV-0006FD-6t; Tue, 28 Jul 2020 11:37:45 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvT-0006Bw-Pe for xen-devel@lists.xenproject.org; Tue, 28 Jul 2020 11:37:43 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id ba9e3aa2-d0c6-11ea-a8af-12813bfff9fa; Tue, 28 Jul 2020 11:37:34 +0000 (UTC) X-Inumbo-ID: ba9e3aa2-d0c6-11ea-a8af-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1595936255; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9OlWrYwwBC2nlui7wolnYoUzIAvQh25bk2h1rFifnms=; b=L0Mz4JCHJQYSLsCUOIiX9d8Rt8Ofd/rtttuupIqrU1rBa5gOkv+x91v8 hsrAi7I7tlEBe+zZ8p4K47qvl45IQFvREj4VV1iJ7/YcPDdLtjRkW1uWG cForkMqkSxWe0pBdQN+yvkgg3MEW1+TZ0nRQBcAEeNsYBeOjrwq2Hv8GO w=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: m9XKyherp46SXYwrhXV7kJyXn2sW1WqwI7PhNSStILK1R1a6B7s+lNNAMvY7NJ74rwDWP3UBuv olRkvmTVZG1Jj3Oh2Vrhb+XUWNcZJ+r06st/fSD9kjuShRn45Oci6B+3QBg4JtFCmnPhi5kBYr GFqrFR6o1gKt147gUkmpPn3+xD/NQ2g64DQT09uG1lKFC6EEqZIXBP224VXDMSbEInZ4m1m1Da Tgp3k2gNjrjces4wgCWiDqdZ8fio1q1Dp+WMUG/29LEdv8+sSVBC0v+T2KEeXlqFhqev8N0NwE XnA= X-SBRS: 2.7 X-MesageID: 23330309 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.75,406,1589256000"; d="scan'208";a="23330309" From: Andrew Cooper To: Xen-devel Subject: [PATCH 3/5] xen/memory: Fix compat XENMEM_acquire_resource for size requests Date: Tue, 28 Jul 2020 12:37:09 +0100 Message-ID: <20200728113712.22966-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200728113712.22966-1-andrew.cooper3@citrix.com> References: <20200728113712.22966-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Hubert Jasudowicz , Stefano Stabellini , Julien Grall , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Paul Durrant , Jan Beulich , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Copy the nr_frames from the correct structure, so the caller doesn't unconditionally receive 0. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich Reviewed-by: Paul Durrant --- CC: George Dunlap CC: Ian Jackson CC: Jan Beulich CC: Konrad Rzeszutek Wilk CC: Stefano Stabellini CC: Wei Liu CC: Julien Grall CC: Paul Durrant CC: Micha=C5=82 Leszczy=C5=84ski CC: Hubert Jasudowicz --- xen/common/compat/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xen/common/compat/memory.c b/xen/common/compat/memory.c index 3851f756c7..ed92e05b08 100644 --- a/xen/common/compat/memory.c +++ b/xen/common/compat/memory.c @@ -599,7 +599,7 @@ int compat_memory_op(unsigned int cmd, XEN_GUEST_HANDLE= _PARAM(void) compat) if ( __copy_field_to_guest( guest_handle_cast(compat, compat_mem_acquire_resource_t), - &cmp.mar, nr_frames) ) + nat.mar, nr_frames) ) return -EFAULT; } else --=20 2.11.0 From nobody Fri Apr 19 18:53:57 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=fail; 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=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1595936282; cv=none; d=zohomail.com; s=zohoarc; b=h5eXmxQsUoDWT+f9yla8Je5UMu3KVkXILft4D/PKUycl1oxpIzirei6wlSdFRxspP7OkYUNX8n5sMyOsSPrcxH/TJ1zpPzptDt2uHzGScEvtbs5V76wSy+dmnMleO9pJBCt0qDRyLn0UedALRM5JXy1iHbwzKo+3ofeeANxJqzI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595936282; h=Content-Type: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=MrqZVu3nAGJve4lpRBo3SnE61LBv1vEf+43SNYYrwmo=; b=jd6OjlbKB8ruyopyhGPk3y9rMBGoP+x6qFARGLSk0GjIvNCsdXg5d0396QCKTWVhkn9MxRLN+jeu0iwh68ypORFIZAd6zBHmtnyrMF+dPPG1GAOfD1jHu1DYhcyHEck7b3kvM/EBBdshO4MurSH3qIZ4PPDtY2xz0uG1RCpY6fo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1595936282026788.678544324108; Tue, 28 Jul 2020 04:38:02 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvP-0006Cs-L1; Tue, 28 Jul 2020 11:37:39 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvO-0006Br-Bq for xen-devel@lists.xenproject.org; Tue, 28 Jul 2020 11:37:38 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id ba239a7c-d0c6-11ea-8b28-bc764e2007e4; Tue, 28 Jul 2020 11:37:33 +0000 (UTC) X-Inumbo-ID: ba239a7c-d0c6-11ea-8b28-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1595936254; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IUokeGrxEyJUToEgizkOaWgPFondXajxhHjfPToParY=; b=Z3jQOnKbywGACgi4shJBcoRDnBmKektgKvqVOnkg0nMDWCUG/AiQ8O90 Eac1cIbL4jO5+vgS9xUWKtfyFOu2oAn7A0p/q3DzrUZeGWta8lGis/XwC OixhgnQEdQYykr22DHMvrCwvccPxzMOS/OEygz9H1CSeho9xv+9Zh74Bi U=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: UY50K4X8eCfaARt6jfYigiGMp9wttQgys0GM6l06PUGOhv67KMyifBt5Koa2iVXe8HR7jZv58K iw/MPZgVj6r5wH237feE/FViWjXNfb9AAijjir4IgJG0JoTntdN3Xu5an9aB5H19hG96PIdVhW U4MDbxOvVeSQXN+sFHc36FF48NO0q26LJpN8vyvRhNA94RuiM8YhlWX0dpfyCK3+f81TS/imjS /ko1vmsr22NxCsI8MksVJdlCQnvitfS6O6O9C/yG/rYVjW/Z5nSMEJoJX6Ycc49nAGqFNQfyFr Alc= X-SBRS: 2.7 X-MesageID: 23330308 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.75,406,1589256000"; d="scan'208";a="23330308" From: Andrew Cooper To: Xen-devel Subject: [PATCH 4/5] xen/memory: Fix acquire_resource size semantics Date: Tue, 28 Jul 2020 12:37:10 +0100 Message-ID: <20200728113712.22966-5-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200728113712.22966-1-andrew.cooper3@citrix.com> References: <20200728113712.22966-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Hubert Jasudowicz , Stefano Stabellini , Julien Grall , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Paul Durrant , Jan Beulich , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Calling XENMEM_acquire_resource with a NULL frame_list is a request for the size of the resource, but the returned 32 is bogus. If someone tries to follow it for XENMEM_resource_ioreq_server, the acquire call will fail as IOREQ servers currently top out at 2 frames, and it is on= ly half the size of the default grant table limit for guests. Also, no users actually request a resource size, because it was never wired= up in the sole implemenation of resource aquisition in Linux. Introduce a new resource_max_frames() to calculate the size of a resource, = and implement it the IOREQ and grant subsystems. It is impossible to guarentee that a mapping call following a successful si= ze call will succedd (e.g. The target IOREQ server gets destroyed, or the doma= in switches from grant v2 to v1). Document the restriction, and use the flexibility to simplify the paths to be lockless. Signed-off-by: Andrew Cooper --- CC: George Dunlap CC: Ian Jackson CC: Jan Beulich CC: Konrad Rzeszutek Wilk CC: Stefano Stabellini CC: Wei Liu CC: Julien Grall CC: Paul Durrant CC: Micha=C5=82 Leszczy=C5=84ski CC: Hubert Jasudowicz --- xen/arch/x86/mm.c | 20 ++++++++++++++++ xen/common/grant_table.c | 19 +++++++++++++++ xen/common/memory.c | 55 +++++++++++++++++++++++++++++++++------= ---- xen/include/asm-x86/mm.h | 3 +++ xen/include/public/memory.h | 16 +++++++++---- xen/include/xen/grant_table.h | 8 +++++++ xen/include/xen/mm.h | 6 +++++ 7 files changed, 110 insertions(+), 17 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 82bc676553..f73a90a2ab 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -4600,6 +4600,26 @@ int xenmem_add_to_physmap_one( return rc; } =20 +unsigned int arch_resource_max_frames( + struct domain *d, unsigned int type, unsigned int id) +{ + unsigned int nr =3D 0; + + switch ( type ) + { +#ifdef CONFIG_HVM + case XENMEM_resource_ioreq_server: + if ( !is_hvm_domain(d) ) + break; + /* One frame for the buf-ioreq ring, and one frame per 128 vcpus. = */ + nr =3D 1 + DIV_ROUND_UP(d->max_vcpus * sizeof(struct ioreq), PAGE_= SIZE); + break; +#endif + } + + return nr; +} + int arch_acquire_resource(struct domain *d, unsigned int type, unsigned int id, unsigned long frame, unsigned int nr_frames, xen_pfn_t mfn_list[]) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 122d1e7596..0962fc7169 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -4013,6 +4013,25 @@ static int gnttab_get_shared_frame_mfn(struct domain= *d, return 0; } =20 +unsigned int gnttab_resource_max_frames(struct domain *d, unsigned int id) +{ + unsigned int nr =3D 0; + + /* Don't need the grant lock. This limit is fixed at domain create ti= me. */ + switch ( id ) + { + case XENMEM_resource_grant_table_id_shared: + nr =3D d->grant_table->max_grant_frames; + break; + + case XENMEM_resource_grant_table_id_status: + nr =3D grant_to_status_frames(d->grant_table->max_grant_frames); + break; + } + + return nr; +} + int gnttab_acquire_resource( struct domain *d, unsigned int id, unsigned long frame, unsigned int nr_frames, xen_pfn_t mfn_list[]) diff --git a/xen/common/memory.c b/xen/common/memory.c index dc3a7248e3..21edabf9cc 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1007,6 +1007,26 @@ static long xatp_permission_check(struct domain *d, = unsigned int space) return xsm_add_to_physmap(XSM_TARGET, current->domain, d); } =20 +/* + * Return 0 on any kind of error. Caller converts to -EINVAL. + * + * All nonzero values should be repeatable (i.e. derived from some fixed + * proerty of the domain), and describe the full resource (i.e. mapping the + * result of this call will be the entire resource). + */ +static unsigned int resource_max_frames(struct domain *d, + unsigned int type, unsigned int id) +{ + switch ( type ) + { + case XENMEM_resource_grant_table: + return gnttab_resource_max_frames(d, id); + + default: + return arch_resource_max_frames(d, type, id); + } +} + static int acquire_resource( XEN_GUEST_HANDLE_PARAM(xen_mem_acquire_resource_t) arg) { @@ -1018,6 +1038,7 @@ static int acquire_resource( * use-cases then per-CPU arrays or heap allocations may be required. */ xen_pfn_t mfn_list[32]; + unsigned int max_frames; int rc; =20 if ( copy_from_guest(&xmar, arg, 1) ) @@ -1026,19 +1047,6 @@ static int acquire_resource( if ( xmar.pad !=3D 0 ) return -EINVAL; =20 - if ( guest_handle_is_null(xmar.frame_list) ) - { - if ( xmar.nr_frames ) - return -EINVAL; - - xmar.nr_frames =3D ARRAY_SIZE(mfn_list); - - if ( __copy_field_to_guest(arg, &xmar, nr_frames) ) - return -EFAULT; - - return 0; - } - if ( xmar.nr_frames > ARRAY_SIZE(mfn_list) ) return -E2BIG; =20 @@ -1050,6 +1058,27 @@ static int acquire_resource( if ( rc ) goto out; =20 + max_frames =3D resource_max_frames(d, xmar.type, xmar.id); + + rc =3D -EINVAL; + if ( !max_frames ) + goto out; + + if ( guest_handle_is_null(xmar.frame_list) ) + { + if ( xmar.nr_frames ) + goto out; + + xmar.nr_frames =3D max_frames; + + rc =3D -EFAULT; + if ( __copy_field_to_guest(arg, &xmar, nr_frames) ) + goto out; + + rc =3D 0; + goto out; + } + switch ( xmar.type ) { case XENMEM_resource_grant_table: diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h index 7e74996053..b0caf372a8 100644 --- a/xen/include/asm-x86/mm.h +++ b/xen/include/asm-x86/mm.h @@ -649,6 +649,9 @@ static inline bool arch_mfn_in_directmap(unsigned long = mfn) return mfn <=3D (virt_to_mfn(eva - 1) + 1); } =20 +unsigned int arch_resource_max_frames(struct domain *d, + unsigned int type, unsigned int id); + int arch_acquire_resource(struct domain *d, unsigned int type, unsigned int id, unsigned long frame, unsigned int nr_frames, xen_pfn_t mfn_list[]); diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index 21057ed78e..cea88cf40c 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -639,10 +639,18 @@ struct xen_mem_acquire_resource { #define XENMEM_resource_grant_table_id_status 1 =20 /* - * IN/OUT - As an IN parameter number of frames of the resource - * to be mapped. However, if the specified value is 0 and - * frame_list is NULL then this field will be set to the - * maximum value supported by the implementation on return. + * IN/OUT + * + * As an IN parameter number of frames of the resource to be mapped. + * + * When frame_list is NULL and nr_frames is 0, this is interpreted as a + * request for the size of the resource, which shall be returned in the + * nr_frames field. + * + * The size of a resource will never be zero, but a nonzero result doe= sn't + * guarentee that a subsequent mapping request will be successful. Th= ere + * are further type/id specific constraints which may change between t= he + * two calls. */ uint32_t nr_frames; uint32_t pad; diff --git a/xen/include/xen/grant_table.h b/xen/include/xen/grant_table.h index 5a2c75b880..bae4d79623 100644 --- a/xen/include/xen/grant_table.h +++ b/xen/include/xen/grant_table.h @@ -57,6 +57,8 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant= _ref_t ref, int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t *mfn); =20 +unsigned int gnttab_resource_max_frames(struct domain *d, unsigned int id); + int gnttab_acquire_resource( struct domain *d, unsigned int id, unsigned long frame, unsigned int nr_frames, xen_pfn_t mfn_list[]); @@ -93,6 +95,12 @@ static inline int gnttab_map_frame(struct domain *d, uns= igned long idx, return -EINVAL; } =20 +static inline unsigned int gnttab_resource_max_frames( + struct domain *d, unsigned int id) +{ + return 0; +} + static inline int gnttab_acquire_resource( struct domain *d, unsigned int id, unsigned long frame, unsigned int nr_frames, xen_pfn_t mfn_list[]) diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 1b2c1f6b32..c184dc1db1 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -686,6 +686,12 @@ static inline void put_page_alloc_ref(struct page_info= *page) } =20 #ifndef CONFIG_ARCH_ACQUIRE_RESOURCE +static inline unsigned int arch_resource_max_frames( + struct domain *d, unsigned int type, unsigned int id) +{ + return 0; +} + static inline int arch_acquire_resource( struct domain *d, unsigned int type, unsigned int id, unsigned long fr= ame, unsigned int nr_frames, xen_pfn_t mfn_list[]) --=20 2.11.0 From nobody Fri Apr 19 18:53:57 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=fail; 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=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1595936282; cv=none; d=zohomail.com; s=zohoarc; b=IF0ZU2oO+hbeFMZhMPcDGz3qJuGlGKj0q8udvOP6Ahpfv4OAqQud/IJZy7vSgNzJwQBr/NO4vPcPjtnijIoKN5ofdODuppmoMB+3jV4LSvN5tSVj+luv94acQKdhJHi5K+vrtX6tKffClMP5mNlM8WLGvg/kJLD/z2G/N+dOGrU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595936282; h=Content-Type: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=w2aWs/Vrz8IicfEaur2vRUym15wUi2fmHtRvuAALeqM=; b=XFpUMjzZh6P+vrepn2jJnnEcwkmdembiBsSedqgnYR2IRQfN/teJpyxydhvILMfceASXUHkbuCMVgzQDkUPseZT8Vjc9tnoMhOumyokFm0NFYwPPSKvTddKwG/ySrvaJ/O3D13rAJJgR2rZEhh/GIf1H2cvV1QTpQFVQSOe3zSs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1595936282179786.1857286685251; Tue, 28 Jul 2020 04:38:02 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvL-0006C1-0x; Tue, 28 Jul 2020 11:37:35 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k0NvJ-0006Br-G5 for xen-devel@lists.xenproject.org; Tue, 28 Jul 2020 11:37:33 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id b8e18ad4-d0c6-11ea-8b28-bc764e2007e4; Tue, 28 Jul 2020 11:37:32 +0000 (UTC) X-Inumbo-ID: b8e18ad4-d0c6-11ea-8b28-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1595936253; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hLRKErlmwe4NVS9+YaW7c8jBL06IM4hgS3F+Z8g4rWY=; b=HZNfHV1mwLDlauzLWmUJKA5XZhlwyFlS9owkEwXwzO8hd9Fv7NKocNck chbTN+2ZzJfnbbaKWcIO4iYAd/DOCTV2T36fcMppE+hHUfshJS3sZlfJF kD1iurYe0m35l9GhRNAHAV17V+W+dtKNN5Lb4P4E/D0uVSsL113NyVX3c s=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: wkiniwnR8YeCE2n7IgGkdtyN/iNg7wK2C/34nL3lEyhS9OFHgUrvsEX3usdR+sM70PAJ/iRaiI EymaPtsnlwm3M8kzBbowTG9KbqS+F7OBNRlM5UQF13mso1MLzwOCytn1b35heDGL2ejhRFjISm 8IfRqWq4XH7SHoQqBM5Ih5WSb4NO4l991xaBIpIlYY9ZQpiKkIBc1BRuZUen6ilHoMsg0PuFCn t/AVz+o0hGJ9TrS2VEZY4CttZo2t1Xxw01+6bYfu/5SgeAvXSnJnQPHq7p9+dV44I+kLhYWxhV JT0= X-SBRS: 2.7 X-MesageID: 23330303 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.75,406,1589256000"; d="scan'208";a="23330303" From: Andrew Cooper To: Xen-devel Subject: [PATCH 5/5] tools/foreignmem: Support querying the size of a resource Date: Tue, 28 Jul 2020 12:37:11 +0100 Message-ID: <20200728113712.22966-6-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200728113712.22966-1-andrew.cooper3@citrix.com> References: <20200728113712.22966-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Hubert Jasudowicz , Wei Liu , Paul Durrant , Andrew Cooper , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) With the Xen side of this interface fixed to return real sizes, userspace needs to be able to make the query. Introduce xenforeignmemory_resource_size() for the purpose, bumping the library minor version and providing compatiblity for the non-Linux builds. Its not possible to reuse the IOCTL_PRIVCMD_MMAP_RESOURCE infrastructure, because it depends on having already mmap()'d a suitably sized region before it will make an XENMEM_acquire_resource hypercall to Xen. Instead, open a xencall handle and make an XENMEM_acquire_resource hypercall directly. Signed-off-by: Andrew Cooper Acked-by: Wei Liu Reviewed-by: Paul Durrant --- CC: Ian Jackson CC: Wei Liu CC: Paul Durrant CC: Micha=C5=82 Leszczy=C5=84ski CC: Hubert Jasudowicz --- tools/libs/foreignmemory/Makefile | 2 +- tools/libs/foreignmemory/core.c | 14 +++++++++ .../libs/foreignmemory/include/xenforeignmemory.h | 15 ++++++++++ tools/libs/foreignmemory/libxenforeignmemory.map | 4 +++ tools/libs/foreignmemory/linux.c | 35 ++++++++++++++++++= ++++ tools/libs/foreignmemory/private.h | 14 +++++++++ 6 files changed, 83 insertions(+), 1 deletion(-) diff --git a/tools/libs/foreignmemory/Makefile b/tools/libs/foreignmemory/M= akefile index 28f1bddc96..8e07f92c59 100644 --- a/tools/libs/foreignmemory/Makefile +++ b/tools/libs/foreignmemory/Makefile @@ -2,7 +2,7 @@ XEN_ROOT =3D $(CURDIR)/../../.. include $(XEN_ROOT)/tools/Rules.mk =20 MAJOR =3D 1 -MINOR =3D 3 +MINOR =3D 4 LIBNAME :=3D foreignmemory USELIBS :=3D toollog toolcore =20 diff --git a/tools/libs/foreignmemory/core.c b/tools/libs/foreignmemory/cor= e.c index 63f12e2450..5d95c59c48 100644 --- a/tools/libs/foreignmemory/core.c +++ b/tools/libs/foreignmemory/core.c @@ -53,6 +53,10 @@ xenforeignmemory_handle *xenforeignmemory_open(xentoollo= g_logger *logger, if (!fmem->logger) goto err; } =20 + fmem->xcall =3D xencall_open(fmem->logger, 0); + if ( !fmem->xcall ) + goto err; + rc =3D osdep_xenforeignmemory_open(fmem); if ( rc < 0 ) goto err; =20 @@ -61,6 +65,7 @@ xenforeignmemory_handle *xenforeignmemory_open(xentoollog= _logger *logger, err: xentoolcore__deregister_active_handle(&fmem->tc_ah); osdep_xenforeignmemory_close(fmem); + xencall_close(fmem->xcall); xtl_logger_destroy(fmem->logger_tofree); free(fmem); return NULL; @@ -75,6 +80,7 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem) =20 xentoolcore__deregister_active_handle(&fmem->tc_ah); rc =3D osdep_xenforeignmemory_close(fmem); + xencall_close(fmem->xcall); xtl_logger_destroy(fmem->logger_tofree); free(fmem); return rc; @@ -188,6 +194,14 @@ int xenforeignmemory_unmap_resource( return rc; } =20 +int xenforeignmemory_resource_size( + xenforeignmemory_handle *fmem, domid_t domid, unsigned int type, + unsigned int id, unsigned long *nr_frames) +{ + return osdep_xenforeignmemory_resource_size(fmem, domid, type, + id, nr_frames); +} + /* * Local variables: * mode: C diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/li= bs/foreignmemory/include/xenforeignmemory.h index d594be8df0..1ba2f5316b 100644 --- a/tools/libs/foreignmemory/include/xenforeignmemory.h +++ b/tools/libs/foreignmemory/include/xenforeignmemory.h @@ -179,6 +179,21 @@ xenforeignmemory_resource_handle *xenforeignmemory_map= _resource( int xenforeignmemory_unmap_resource( xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres); =20 +/** + * Determine the maximum size of a specific resource. + * + * @parm fmem handle to the open foreignmemory interface + * @parm domid the domain id + * @parm type the resource type + * @parm id the type-specific resource identifier + * + * Return 0 on success and fills in *nr_frames. Sets errno and return -1 = on + * error. + */ +int xenforeignmemory_resource_size( + xenforeignmemory_handle *fmem, domid_t domid, unsigned int type, + unsigned int id, unsigned long *nr_frames); + #endif =20 /* diff --git a/tools/libs/foreignmemory/libxenforeignmemory.map b/tools/libs/= foreignmemory/libxenforeignmemory.map index d5323c87d9..8aca341b99 100644 --- a/tools/libs/foreignmemory/libxenforeignmemory.map +++ b/tools/libs/foreignmemory/libxenforeignmemory.map @@ -19,3 +19,7 @@ VERS_1.3 { xenforeignmemory_map_resource; xenforeignmemory_unmap_resource; } VERS_1.2; +VERS_1.4 { + global: + xenforeignmemory_resource_size; +} VERS_1.3; diff --git a/tools/libs/foreignmemory/linux.c b/tools/libs/foreignmemory/li= nux.c index 8daa5828e3..67e0ca1e83 100644 --- a/tools/libs/foreignmemory/linux.c +++ b/tools/libs/foreignmemory/linux.c @@ -28,6 +28,8 @@ =20 #include "private.h" =20 +#include + #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w)= )-1)) =20 #ifndef O_CLOEXEC @@ -340,6 +342,39 @@ int osdep_xenforeignmemory_map_resource( return 0; } =20 +int osdep_xenforeignmemory_resource_size( + xenforeignmemory_handle *fmem, domid_t domid, unsigned int type, + unsigned int id, unsigned long *nr_frames) +{ + int rc; + struct xen_mem_acquire_resource *xmar =3D + xencall_alloc_buffer(fmem->xcall, sizeof(*xmar)); + + if ( !xmar ) + { + PERROR("Could not bounce memory for acquire_resource hypercall"); + return -1; + } + + *xmar =3D (struct xen_mem_acquire_resource){ + .domid =3D domid, + .type =3D type, + .id =3D id, + }; + + rc =3D xencall2(fmem->xcall, __HYPERVISOR_memory_op, + XENMEM_acquire_resource, (uintptr_t)xmar); + if ( rc ) + goto out; + + *nr_frames =3D xmar->nr_frames; + + out: + xencall_free_buffer(fmem->xcall, xmar); + + return rc; +} + /* * Local variables: * mode: C diff --git a/tools/libs/foreignmemory/private.h b/tools/libs/foreignmemory/= private.h index 8f1bf081ed..1a6b685f45 100644 --- a/tools/libs/foreignmemory/private.h +++ b/tools/libs/foreignmemory/private.h @@ -4,6 +4,7 @@ #include =20 #include +#include =20 #include =20 @@ -20,6 +21,7 @@ =20 struct xenforeignmemory_handle { xentoollog_logger *logger, *logger_tofree; + xencall_handle *xcall; unsigned flags; int fd; Xentoolcore__Active_Handle tc_ah; @@ -74,6 +76,15 @@ static inline int osdep_xenforeignmemory_unmap_resource( { return 0; } + +static inline int osdep_xenforeignmemory_resource_size( + xenforeignmemory_handle *fmem, domid_t domid, unsigned int type, + unsigned int id, unsigned long *nr_frames) +{ + errno =3D EOPNOTSUPP; + return -1; +} + #else int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem, domid_t domid); @@ -81,6 +92,9 @@ int osdep_xenforeignmemory_map_resource( xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres); int osdep_xenforeignmemory_unmap_resource( xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres); +int osdep_xenforeignmemory_resource_size( + xenforeignmemory_handle *fmem, domid_t domid, unsigned int type, + unsigned int id, unsigned long *nr_frames); #endif =20 #define PERROR(_f...) \ --=20 2.11.0