From nobody Sat May 18 09:22:56 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=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1699372198; cv=none; d=zohomail.com; s=zohoarc; b=HLjGveVUhZHAGMp0jdI53Iy9V5YJdk13bFBPNlZc6ep8Y1ZwUKtgoh0TSRgkR9pBDNdmSGMwFPL58SHD2b4+2aoHYSDbWIe4EITqVOsvP2mJu8VSZRScN9r4iTjTYDPLz0pzOvfNK3NkcysxbitsaKPrTZ6h5+rCyPYlpLGcbQM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699372198; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=BkvpmHSMusCYlZr4kFl4G1ly5WpvU3SZ+lcbW7voFmc=; b=hGnFCrWShHBHuWhbqFsDmRQsxnP7hkADKy4dZ2k8Eq3cw7f3pLkUgv9NpftMVZRACrn2O5n9tE+CLZfuOJVdoYlaCasF4QQDzHT2fVBRlMQV1UF5yecA9WxRDsZkRwGOLq+2tbrJpgTynaH0r2pPpqra6xJT/xykVylrILika44= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1699372198456185.40098457307658; Tue, 7 Nov 2023 07:49:58 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.628909.980837 (Exim 4.92) (envelope-from ) id 1r0OKX-0004fR-JP; Tue, 07 Nov 2023 15:49:29 +0000 Received: by outflank-mailman (output) from mailman id 628909.980837; Tue, 07 Nov 2023 15:49:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r0OKX-0004fK-GI; Tue, 07 Nov 2023 15:49:29 +0000 Received: by outflank-mailman (input) for mailman id 628909; Tue, 07 Nov 2023 15:49:28 +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 1r0OKW-0004f1-9m for xen-devel@lists.xenproject.org; Tue, 07 Nov 2023 15:49:28 +0000 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [2a00:1450:4864:20::335]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3aec3b6f-7d85-11ee-98da-6d05b1d4d9a1; Tue, 07 Nov 2023 16:49:27 +0100 (CET) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-40891d38e3fso40796825e9.1 for ; Tue, 07 Nov 2023 07:49:26 -0800 (PST) Received: from EMEAENGAAD19049.citrite.net ([2.223.46.215]) by smtp.gmail.com with ESMTPSA id n26-20020a1c721a000000b0040849ce7116sm15809852wmc.43.2023.11.07.07.49.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 07:49:25 -0800 (PST) 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: 3aec3b6f-7d85-11ee-98da-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1699372166; x=1699976966; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BkvpmHSMusCYlZr4kFl4G1ly5WpvU3SZ+lcbW7voFmc=; b=epQ4X2GWFarSYAFXZd3PRTscNQ818x0uDBsDXFpksDzhlq5VEG04RU//7+n6hG8K6W yfTzXqBBNV1VThvE8ib8HrGCdn3i/BlfyVD7f3ZCxWgHmJ4alKCAMjTXNyD6trSd+UUn shH510ZzGE/LHZAslD/wI/aiF+UTzpeefL4LE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699372166; x=1699976966; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BkvpmHSMusCYlZr4kFl4G1ly5WpvU3SZ+lcbW7voFmc=; b=dHJG+/lCsgOm+Hd1GMLYs4VpImUmyIQjhcgfiqtXi77tf3Su6sL4E4ZY7O4iWbXvKm 7g5/BFOf7gT0UBZNhI4BgwgFnl35vWzK3COr5cpZ8HoUy+dUvEtoUb4ZbIfUvtnNrgKb p0KoZ1Z0ym7osqUcPhNkW7CT2sR40ekMwrZFWywpOpUGssSVu2/VDZiM39SXRbTm8kJn 6qP/CFLd9YP/Ho5Ur//luzrqGb8kTzEGMxgy7cHsf6EBMBmQsWfPKtIr6+AC9rdRujGj KMAw/fyCrbS73HPw09MfYVb3WIljykqTmpMyobnj3JjhMxFlXhkmLlFIAhMcVFeNxl1Q 6smQ== X-Gm-Message-State: AOJu0YznuzVXjBkIJ/LDkMsByjO2YXDc/ekDbzBK674yPhxtnIHfpamZ AOe50lXaVDewQrgO7FhxPAw2RtDq0wj7gA09LqI= X-Google-Smtp-Source: AGHT+IHGdLuZ0IMWw9OVnDgXTXpSs5u56jgwjA0OzJ85L6nEi9Q0Pm8CL1hsz8lZ4aKxo8fFX7HnpQ== X-Received: by 2002:a05:600c:3c87:b0:405:4776:735a with SMTP id bg7-20020a05600c3c8700b004054776735amr3159633wmb.2.1699372165985; Tue, 07 Nov 2023 07:49:25 -0800 (PST) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Wei Liu , Anthony PERARD , Juergen Gross , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH 1/7] tools/xenguest: Dynamically allocate xc_cpu_policy_t contents Date: Tue, 7 Nov 2023 15:49:15 +0000 Message-Id: <20231107154921.54979-2-alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231107154921.54979-1-alejandro.vallejo@cloud.com> References: <20231107154921.54979-1-alejandro.vallejo@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1699372199292100001 Content-Type: text/plain; charset="utf-8" This is part of a larger effort to reduce technical debt in this area and prevent policy internals from leaking in plumbing code that needs not be aware of them. This patch turns the internal static buffers into dynamic ones and performs the allocations based on policy sizes probed using a hypercall. This is meant to help dealing with mismatched versions of toolstack/Xen, as no assumptions are made with regards of the sizes of the buffers. With this, we are now able to use these buffers for serialization instead of out-of-band data structures. The scheme is simple and involves keeping track of the buffer occupancy through the "len" field and its capacity through the "allocated" field; think "vector" in higher-level languages. Both trackers refer to entries rather than octets because most of the code deals in those terms. While at this, make a minor change to MSR_MAX_SERIALISED_ENTRIES so it's unsigned in order to please the max() macro. Signed-off-by: Alejandro Vallejo Reviewed-by: Anthony PERARD --- tools/include/xenguest.h | 2 +- tools/libs/guest/xg_cpuid_x86.c | 86 +++++++++++++++++++++------- tools/libs/guest/xg_private.h | 17 +++++- tools/libs/guest/xg_sr_common_x86.c | 2 +- tools/misc/xen-cpuid.c | 2 +- tools/tests/tsx/test-tsx.c | 61 +++++++++++--------- xen/include/xen/lib/x86/cpu-policy.h | 2 +- 7 files changed, 119 insertions(+), 53 deletions(-) diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h index e01f494b77..702965addc 100644 --- a/tools/include/xenguest.h +++ b/tools/include/xenguest.h @@ -787,7 +787,7 @@ xen_pfn_t *xc_map_m2p(xc_interface *xch, typedef struct xc_cpu_policy xc_cpu_policy_t; =20 /* Create and free a xc_cpu_policy object. */ -xc_cpu_policy_t *xc_cpu_policy_init(void); +xc_cpu_policy_t *xc_cpu_policy_init(xc_interface *xch); void xc_cpu_policy_destroy(xc_cpu_policy_t *policy); =20 /* Retrieve a system policy, or get/set a domains policy. */ diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x8= 6.c index f2b1e80901..3545f3e530 100644 --- a/tools/libs/guest/xg_cpuid_x86.c +++ b/tools/libs/guest/xg_cpuid_x86.c @@ -839,15 +839,48 @@ out: return rc; } =20 -xc_cpu_policy_t *xc_cpu_policy_init(void) +xc_cpu_policy_t *xc_cpu_policy_init(xc_interface *xch) { - return calloc(1, sizeof(struct xc_cpu_policy)); + uint32_t nr_leaves, nr_msrs; + xc_cpu_policy_t *p =3D calloc(1, sizeof(*p)); + if ( !p ) + return NULL; + + if ( xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs) ) + goto fail; + + p->leaves.allocated =3D max(nr_leaves, CPUID_MAX_SERIALISED_LEAVES); + p->leaves.buf =3D calloc(p->leaves.allocated, sizeof(*p->leaves.buf)); + if ( !p->leaves.buf ) + goto fail; + + p->msrs.allocated =3D max(nr_msrs, MSR_MAX_SERIALISED_ENTRIES); + p->msrs.buf =3D calloc(p->msrs.allocated, sizeof(*p->msrs.buf)); + if ( !p->msrs.buf ) + goto fail; + + /* Success */ + return p; + + fail: + xc_cpu_policy_destroy(p); + return NULL; } =20 void xc_cpu_policy_destroy(xc_cpu_policy_t *policy) { - if ( policy ) - free(policy); + int err =3D errno; + + if ( !policy ) + return; + + if ( policy->leaves.buf ) + free(policy->leaves.buf); + if ( policy->msrs.buf ) + free(policy->msrs.buf); + + free(policy); + errno =3D err; } =20 static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy, @@ -856,7 +889,7 @@ static int deserialize_policy(xc_interface *xch, xc_cpu= _policy_t *policy, uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; int rc; =20 - rc =3D x86_cpuid_copy_from_buffer(&policy->policy, policy->leaves, + rc =3D x86_cpuid_copy_from_buffer(&policy->policy, policy->leaves.buf, nr_leaves, &err_leaf, &err_subleaf); if ( rc ) { @@ -866,7 +899,7 @@ static int deserialize_policy(xc_interface *xch, xc_cpu= _policy_t *policy, return rc; } =20 - rc =3D x86_msr_copy_from_buffer(&policy->policy, policy->msrs, + rc =3D x86_msr_copy_from_buffer(&policy->policy, policy->msrs.buf, nr_entries, &err_msr); if ( rc ) { @@ -882,18 +915,22 @@ static int deserialize_policy(xc_interface *xch, xc_c= pu_policy_t *policy, int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx, xc_cpu_policy_t *policy) { - unsigned int nr_leaves =3D ARRAY_SIZE(policy->leaves); - unsigned int nr_msrs =3D ARRAY_SIZE(policy->msrs); + unsigned int nr_leaves =3D policy->leaves.allocated; + unsigned int nr_msrs =3D policy->msrs.allocated; int rc; =20 - rc =3D get_system_cpu_policy(xch, policy_idx, &nr_leaves, policy->leav= es, - &nr_msrs, policy->msrs); + rc =3D get_system_cpu_policy(xch, policy_idx, + &nr_leaves, policy->leaves.buf, + &nr_msrs, policy->msrs.buf); if ( rc ) { PERROR("Failed to obtain %u policy", policy_idx); return rc; } =20 + policy->leaves.len =3D nr_leaves; + policy->msrs.len =3D nr_msrs; + rc =3D deserialize_policy(xch, policy, nr_leaves, nr_msrs); if ( rc ) { @@ -907,18 +944,22 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsig= ned int policy_idx, int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid, xc_cpu_policy_t *policy) { - unsigned int nr_leaves =3D ARRAY_SIZE(policy->leaves); - unsigned int nr_msrs =3D ARRAY_SIZE(policy->msrs); + unsigned int nr_leaves =3D policy->leaves.allocated; + unsigned int nr_msrs =3D policy->msrs.allocated; int rc; =20 - rc =3D get_domain_cpu_policy(xch, domid, &nr_leaves, policy->leaves, - &nr_msrs, policy->msrs); + rc =3D get_domain_cpu_policy(xch, domid, + &nr_leaves, policy->leaves.buf, + &nr_msrs, policy->msrs.buf); if ( rc ) { PERROR("Failed to obtain domain %u policy", domid); return rc; } =20 + policy->leaves.len =3D nr_leaves; + policy->msrs.len =3D nr_msrs; + rc =3D deserialize_policy(xch, policy, nr_leaves, nr_msrs); if ( rc ) { @@ -933,17 +974,22 @@ int xc_cpu_policy_set_domain(xc_interface *xch, uint3= 2_t domid, xc_cpu_policy_t *policy) { uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; - unsigned int nr_leaves =3D ARRAY_SIZE(policy->leaves); - unsigned int nr_msrs =3D ARRAY_SIZE(policy->msrs); + unsigned int nr_leaves =3D policy->leaves.allocated; + unsigned int nr_msrs =3D policy->msrs.allocated; int rc; =20 - rc =3D xc_cpu_policy_serialise(xch, policy, policy->leaves, &nr_leaves, - policy->msrs, &nr_msrs); + rc =3D xc_cpu_policy_serialise(xch, policy, + policy->leaves.buf, &nr_leaves, + policy->msrs.buf, &nr_msrs); if ( rc ) return rc; =20 - rc =3D xc_set_domain_cpu_policy(xch, domid, nr_leaves, policy->leaves, - nr_msrs, policy->msrs, + policy->leaves.len =3D nr_leaves; + policy->msrs.len =3D nr_msrs; + + rc =3D xc_set_domain_cpu_policy(xch, domid, + nr_leaves, policy->leaves.buf, + nr_msrs, policy->msrs.buf, &err_leaf, &err_subleaf, &err_msr); if ( rc ) { diff --git a/tools/libs/guest/xg_private.h b/tools/libs/guest/xg_private.h index d73947094f..94957ea5ca 100644 --- a/tools/libs/guest/xg_private.h +++ b/tools/libs/guest/xg_private.h @@ -174,9 +174,22 @@ int pin_table(xc_interface *xch, unsigned int type, un= signed long mfn, #include =20 struct xc_cpu_policy { + /** Deserialised policy */ struct cpu_policy policy; - xen_cpuid_leaf_t leaves[CPUID_MAX_SERIALISED_LEAVES]; - xen_msr_entry_t msrs[MSR_MAX_SERIALISED_ENTRIES]; + + /** Serialised buffer of CPUID leaves */ + struct { + xen_cpuid_leaf_t *buf; /**< Base address */ + size_t allocated; /**< Memory allocated to the buffer (in entries)= */ + size_t len; /**< Number of entries in use (in entries) */ + } leaves; + + /** Serialised buffer of MSRs */ + struct { + xen_msr_entry_t *buf; /**< Base address */ + size_t allocated; /**< Memory allocated to the buffer (in entries)= */ + size_t len; /**< Number of entries in use (in entries) */ + } msrs; }; #endif /* x86 */ =20 diff --git a/tools/libs/guest/xg_sr_common_x86.c b/tools/libs/guest/xg_sr_c= ommon_x86.c index 563b4f0168..ad63c675ed 100644 --- a/tools/libs/guest/xg_sr_common_x86.c +++ b/tools/libs/guest/xg_sr_common_x86.c @@ -59,7 +59,7 @@ int write_x86_cpu_policy_records(struct xc_sr_context *ct= x) =20 cpuid.data =3D malloc(nr_leaves * sizeof(xen_cpuid_leaf_t)); msrs.data =3D malloc(nr_msrs * sizeof(xen_msr_entry_t)); - policy =3D xc_cpu_policy_init(); + policy =3D xc_cpu_policy_init(xch); if ( !cpuid.data || !msrs.data || !policy ) { ERROR("Cannot allocate memory for CPU Policy"); diff --git a/tools/misc/xen-cpuid.c b/tools/misc/xen-cpuid.c index 7370f1b56e..a87b9f99f2 100644 --- a/tools/misc/xen-cpuid.c +++ b/tools/misc/xen-cpuid.c @@ -532,7 +532,7 @@ int main(int argc, char **argv) uint32_t i, max_leaves, max_msrs; =20 xc_interface *xch =3D xc_interface_open(0, 0, 0); - xc_cpu_policy_t *policy =3D xc_cpu_policy_init(); + xc_cpu_policy_t *policy =3D xc_cpu_policy_init(xch); =20 if ( !xch ) err(1, "xc_interface_open"); diff --git a/tools/tests/tsx/test-tsx.c b/tools/tests/tsx/test-tsx.c index b7e1972ce8..3371bb26f7 100644 --- a/tools/tests/tsx/test-tsx.c +++ b/tools/tests/tsx/test-tsx.c @@ -58,7 +58,7 @@ static xc_interface *xch; * don't care about the raw policy (index 0) so reuse that for the guest * policy. */ -static struct xc_cpu_policy policies[6]; +static struct xc_cpu_policy *policies[6]; #define guest_policy policies[0] #define host policies[XEN_SYSCTL_cpu_policy_host] #define pv_max policies[XEN_SYSCTL_cpu_policy_pv_max] @@ -151,15 +151,15 @@ static void test_tsx_msrs(void) { printf("Testing MSR_TSX_FORCE_ABORT consistency\n"); test_tsx_msr_consistency( - MSR_TSX_FORCE_ABORT, host.policy.feat.tsx_force_abort); + MSR_TSX_FORCE_ABORT, host->policy.feat.tsx_force_abort); =20 printf("Testing MSR_TSX_CTRL consistency\n"); test_tsx_msr_consistency( - MSR_TSX_CTRL, host.policy.arch_caps.tsx_ctrl); + MSR_TSX_CTRL, host->policy.arch_caps.tsx_ctrl); =20 printf("Testing MSR_MCU_OPT_CTRL consistency\n"); test_tsx_msr_consistency( - MSR_MCU_OPT_CTRL, host.policy.feat.srbds_ctrl); + MSR_MCU_OPT_CTRL, host->policy.feat.srbds_ctrl); } =20 /* @@ -281,7 +281,7 @@ static void test_rtm_behaviour(void) else return fail(" Got unexpected behaviour %d\n", rtm_behaviour); =20 - if ( host.policy.feat.rtm ) + if ( host->policy.feat.rtm ) { if ( rtm_behaviour =3D=3D RTM_UD ) fail(" Host reports RTM, but appears unavailable\n"); @@ -347,13 +347,13 @@ static void test_def_max_policies(void) if ( xen_has_pv ) { printf("Testing PV default/max policies\n"); - test_guest_policies(&pv_max.policy, &pv_default.policy); + test_guest_policies(&pv_max->policy, &pv_default->policy); } =20 if ( xen_has_hvm ) { printf("Testing HVM default/max policies\n"); - test_guest_policies(&hvm_max.policy, &hvm_default.policy); + test_guest_policies(&hvm_max->policy, &hvm_default->policy); } } =20 @@ -369,7 +369,7 @@ static void test_guest(struct xen_domctl_createdomain *= c) =20 printf(" Created d%u\n", domid); =20 - rc =3D xc_cpu_policy_get_domain(xch, domid, &guest_policy); + rc =3D xc_cpu_policy_get_domain(xch, domid, guest_policy); if ( rc ) { fail(" Failed to obtain domain policy: %d - %s\n", @@ -377,23 +377,23 @@ static void test_guest(struct xen_domctl_createdomain= *c) goto out; } =20 - dump_tsx_details(&guest_policy.policy, "Cur:"); + dump_tsx_details(&guest_policy->policy, "Cur:"); =20 /* * Check defaults given to the guest. */ - if ( guest_policy.policy.feat.rtm !=3D (rtm_behaviour =3D=3D RTM_OK) ) + if ( guest_policy->policy.feat.rtm !=3D (rtm_behaviour =3D=3D RTM_OK) ) fail(" RTM %u in guest, despite rtm behaviour\n", - guest_policy.policy.feat.rtm); + guest_policy->policy.feat.rtm); =20 - if ( guest_policy.policy.feat.hle || - guest_policy.policy.feat.tsx_force_abort || - guest_policy.policy.feat.rtm_always_abort || - guest_policy.policy.feat.srbds_ctrl || - guest_policy.policy.arch_caps.tsx_ctrl ) + if ( guest_policy->policy.feat.hle || + guest_policy->policy.feat.tsx_force_abort || + guest_policy->policy.feat.rtm_always_abort || + guest_policy->policy.feat.srbds_ctrl || + guest_policy->policy.arch_caps.tsx_ctrl ) fail(" Unexpected features advertised\n"); =20 - if ( host.policy.feat.rtm ) + if ( host->policy.feat.rtm ) { unsigned int _7b0; =20 @@ -401,11 +401,11 @@ static void test_guest(struct xen_domctl_createdomain= *c) * If host RTM is available, all combinations of guest flags shoul= d be * possible. Flip both HLE/RTM to check non-default settings. */ - _7b0 =3D (guest_policy.policy.feat.raw[0].b ^=3D + _7b0 =3D (guest_policy->policy.feat.raw[0].b ^=3D (bitmaskof(X86_FEATURE_HLE) | bitmaskof(X86_FEATURE_RTM))); =20 /* Set the new policy. */ - rc =3D xc_cpu_policy_set_domain(xch, domid, &guest_policy); + rc =3D xc_cpu_policy_set_domain(xch, domid, guest_policy); if ( rc ) { fail(" Failed to set domain policy: %d - %s\n", @@ -414,7 +414,7 @@ static void test_guest(struct xen_domctl_createdomain *= c) } =20 /* Re-get the new policy. */ - rc =3D xc_cpu_policy_get_domain(xch, domid, &guest_policy); + rc =3D xc_cpu_policy_get_domain(xch, domid, guest_policy); if ( rc ) { fail(" Failed to obtain domain policy: %d - %s\n", @@ -422,12 +422,12 @@ static void test_guest(struct xen_domctl_createdomain= *c) goto out; } =20 - dump_tsx_details(&guest_policy.policy, "Cur:"); + dump_tsx_details(&guest_policy->policy, "Cur:"); =20 - if ( guest_policy.policy.feat.raw[0].b !=3D _7b0 ) + if ( guest_policy->policy.feat.raw[0].b !=3D _7b0 ) { fail(" Expected CPUID.7[1].b 0x%08x differs from actual 0x%08= x\n", - _7b0, guest_policy.policy.feat.raw[0].b); + _7b0, guest_policy->policy.feat.raw[0].b); goto out; } } @@ -480,11 +480,18 @@ static void test_tsx(void) { int rc; =20 - /* Read all policies except raw. */ - for ( unsigned int i =3D XEN_SYSCTL_cpu_policy_host; - i <=3D XEN_SYSCTL_cpu_policy_hvm_default; ++i ) + for ( unsigned int i =3D 0; i < ARRAY_SIZE(policies); ++i ) { - rc =3D xc_cpu_policy_get_system(xch, i, &policies[i]); + policies[i] =3D xc_cpu_policy_init(xch); + if ( !policies[i] ) + return fail("Failed to allocate policy[%u]: %d - %s\n", + i, errno, strerror(errno)); + + /* Read all policies except raw. */ + if ( i =3D=3D XEN_SYSCTL_cpu_policy_raw ) + continue; + + rc =3D xc_cpu_policy_get_system(xch, i, policies[i]); =20 if ( rc =3D=3D -1 && errno =3D=3D EOPNOTSUPP ) { diff --git a/xen/include/xen/lib/x86/cpu-policy.h b/xen/include/xen/lib/x86= /cpu-policy.h index d5e447e9dc..c4dd81f2bb 100644 --- a/xen/include/xen/lib/x86/cpu-policy.h +++ b/xen/include/xen/lib/x86/cpu-policy.h @@ -110,7 +110,7 @@ const char *x86_cpuid_vendor_to_str(unsigned int vendor= ); 2 /* hv_limit and hv2_limit */ ) =20 /* Maximum number of MSRs written when serialising a cpu_policy. */ -#define MSR_MAX_SERIALISED_ENTRIES 2 +#define MSR_MAX_SERIALISED_ENTRIES 2U =20 struct cpu_policy { --=20 2.34.1 From nobody Sat May 18 09:22:56 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=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1699372194; cv=none; d=zohomail.com; s=zohoarc; b=eKlGwCdd64LQ2JEBrb4vKaiMjmQAlgOE9vl3LlvDfxwE/dCGRLQe2N544zroKr0IEVGjto8yOjtccmjTZhJcEAvFGd4LlzLpFXJDYa8d0OVfnxvZqZYUG6Ky+NTLka4ckKwFt3mWEqtzdnI2KMxaedlB/3qpdwS3kEHChimJLOI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699372194; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4PH+ltmnAqqEVilWJSYQVlpn9gAh+kNEqLJKqQ8kDow=; b=FuxYW5WrVJLh+YNN7z1hy+FNBV+txPaZmUaqC1l1tKwHQeo/t/FyHhh02b3zZejLvR8vOg2yUZ6R9ALgrv43/9TWMs8mmH4stqpMFvkBPeJVPuIhB8/YKWLVwalXlV851ipEBJNW584+5HtzIOpnnDc7mq1B0LUZgJAXrmc4wTs= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1699372194406678.3899892554266; Tue, 7 Nov 2023 07:49:54 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.628911.980849 (Exim 4.92) (envelope-from ) id 1r0OKY-0004pe-6M; Tue, 07 Nov 2023 15:49:30 +0000 Received: by outflank-mailman (output) from mailman id 628911.980849; Tue, 07 Nov 2023 15:49:30 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r0OKX-0004o2-Vy; Tue, 07 Nov 2023 15:49:29 +0000 Received: by outflank-mailman (input) for mailman id 628911; Tue, 07 Nov 2023 15:49:29 +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 1r0OKW-0004f1-Vp for xen-devel@lists.xenproject.org; Tue, 07 Nov 2023 15:49:28 +0000 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [2a00:1450:4864:20::336]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3b954827-7d85-11ee-98da-6d05b1d4d9a1; Tue, 07 Nov 2023 16:49:28 +0100 (CET) Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-40839652b97so43425785e9.3 for ; Tue, 07 Nov 2023 07:49:28 -0800 (PST) Received: from EMEAENGAAD19049.citrite.net ([2.223.46.215]) by smtp.gmail.com with ESMTPSA id n26-20020a1c721a000000b0040849ce7116sm15809852wmc.43.2023.11.07.07.49.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 07:49:26 -0800 (PST) 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: 3b954827-7d85-11ee-98da-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1699372167; x=1699976967; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4PH+ltmnAqqEVilWJSYQVlpn9gAh+kNEqLJKqQ8kDow=; b=F1rw5aUbAnsg3ci/ltsmy72UzPYihqy5pKD0OuAiPDyx1+XV2jNnm5hR+uOnzUDXwy adrcNEcKd2mgJJiJEiijqJYWgNSGGL9HldNHjxbLpEk/O+MAJSO5YLU6cLFi9teQv8+F rBsDzFla/v5kV458L14alaxpNMjiYBqjQQ/2w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699372167; x=1699976967; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4PH+ltmnAqqEVilWJSYQVlpn9gAh+kNEqLJKqQ8kDow=; b=hFaRYxZrl5p47fE9IaYTDjyGY6rC/DhBPPmTPL3YmyY4mD8oekN4W4Pv0UwjqmHJsg e4GBhx9Vhd/FUF9kpq0pmsOBh1GkM7Fmmd5rxAE4X6NgyaBiCdpwLvhlDD4TIMb/80SH m2XTyQ09pSIlPT0ClcWNJ0xTzMGonxYBwjooUY2TUlO7VqLz5OQTTXoHSVNO0dVexCsE O3YRcPoao0YcesoMxe4xIMNW1C/18JUHfo0XAp9PI1X3D05pcCsBsT+9tHrr6b5IXRlx S19ykp1fjTX04mGiV3wWWedXNrbbwN+5PcqEfVp+OryRys+rmAp1HbNmzwf+PHYVMq1p DX8w== X-Gm-Message-State: AOJu0YwngIF2Lw+J9crY8jA89Pqy14YAGeOih480bHgN24Mnp5hD60kE ERWvPqnFiQisJe342SlMgejfzsQNxtzLj+f+xYc= X-Google-Smtp-Source: AGHT+IHbGl1ImjqsJJC/LtJ/XxrNw1Yq7Y39EFnxTRFMDZvidth2gihrppp3vxakaCZKl4LTvY/Ykg== X-Received: by 2002:a05:600c:3112:b0:408:575e:f24f with SMTP id g18-20020a05600c311200b00408575ef24fmr2701397wmo.28.1699372166576; Tue, 07 Nov 2023 07:49:26 -0800 (PST) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Wei Liu , Anthony PERARD , Juergen Gross Subject: [PATCH 2/7] tools/xg: Simplify write_x86_cpu_policy_records() Date: Tue, 7 Nov 2023 15:49:16 +0000 Message-Id: <20231107154921.54979-3-alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231107154921.54979-1-alejandro.vallejo@cloud.com> References: <20231107154921.54979-1-alejandro.vallejo@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1699372195183100005 Content-Type: text/plain; charset="utf-8" With the policy automatically getting appropriate serialised buffer sizes, we can remove boilerplate from this function. Furthermore, the extra dynamic allocations aren't needed anymore as the serialised buffers inside the policy can be used instead. Signed-off-by: Alejandro Vallejo --- tools/libs/guest/xg_sr_common_x86.c | 55 +++++++++-------------------- 1 file changed, 17 insertions(+), 38 deletions(-) diff --git a/tools/libs/guest/xg_sr_common_x86.c b/tools/libs/guest/xg_sr_c= ommon_x86.c index ad63c675ed..c8fd64775f 100644 --- a/tools/libs/guest/xg_sr_common_x86.c +++ b/tools/libs/guest/xg_sr_common_x86.c @@ -44,55 +44,36 @@ int handle_x86_tsc_info(struct xc_sr_context *ctx, stru= ct xc_sr_record *rec) =20 int write_x86_cpu_policy_records(struct xc_sr_context *ctx) { + int rc =3D -1; xc_interface *xch =3D ctx->xch; - struct xc_sr_record cpuid =3D { .type =3D REC_TYPE_X86_CPUID_POLICY, }; - struct xc_sr_record msrs =3D { .type =3D REC_TYPE_X86_MSR_POLICY, }; - uint32_t nr_leaves =3D 0, nr_msrs =3D 0; - xc_cpu_policy_t *policy =3D NULL; - int rc; - - if ( xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs) < 0 ) - { - PERROR("Unable to get CPU Policy size"); - return -1; - } + struct xc_sr_record record; + xc_cpu_policy_t *policy =3D xc_cpu_policy_init(xch); =20 - cpuid.data =3D malloc(nr_leaves * sizeof(xen_cpuid_leaf_t)); - msrs.data =3D malloc(nr_msrs * sizeof(xen_msr_entry_t)); - policy =3D xc_cpu_policy_init(xch); - if ( !cpuid.data || !msrs.data || !policy ) - { - ERROR("Cannot allocate memory for CPU Policy"); - rc =3D -1; - goto out; - } - - if ( xc_cpu_policy_get_domain(xch, ctx->domid, policy) ) + if ( !policy || + (rc =3D xc_cpu_policy_get_domain(xch, ctx->domid, policy)) ) { PERROR("Unable to get d%d CPU Policy", ctx->domid); - rc =3D -1; - goto out; - } - if ( xc_cpu_policy_serialise(xch, policy, cpuid.data, &nr_leaves, - msrs.data, &nr_msrs) ) - { - PERROR("Unable to serialize d%d CPU Policy", ctx->domid); - rc =3D -1; goto out; } =20 - cpuid.length =3D nr_leaves * sizeof(xen_cpuid_leaf_t); - if ( cpuid.length ) + record =3D (struct xc_sr_record){ + .type =3D REC_TYPE_X86_CPUID_POLICY, .data =3D policy->leaves.buf, + .length =3D policy->leaves.len * sizeof(*policy->leaves.buf), + }; + if ( record.length ) { - rc =3D write_record(ctx, &cpuid); + rc =3D write_record(ctx, &record); if ( rc ) goto out; } =20 - msrs.length =3D nr_msrs * sizeof(xen_msr_entry_t); - if ( msrs.length ) + record =3D (struct xc_sr_record){ + .type =3D REC_TYPE_X86_MSR_POLICY, .data =3D policy->msrs.buf, + .length =3D policy->msrs.len * sizeof(*policy->msrs.buf), + }; + if ( record.length ) { - rc =3D write_record(ctx, &msrs); + rc =3D write_record(ctx, &record); if ( rc ) goto out; } @@ -100,8 +81,6 @@ int write_x86_cpu_policy_records(struct xc_sr_context *c= tx) rc =3D 0; =20 out: - free(cpuid.data); - free(msrs.data); xc_cpu_policy_destroy(policy); =20 return rc; --=20 2.34.1 From nobody Sat May 18 09:22:56 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=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1699372201; cv=none; d=zohomail.com; s=zohoarc; b=FRmcQzEvhH1PLW33AuStA//d4nAV5eAobhF3hKN/mtC0150xsbOZcQRH5YAi4vtz/8BAlQCBD2PMUSFEoua2vSbOAljBZscZyDGz01Ahwyz8pQsP4J/4tvzMbdMWLEmoax+DIuf3xhD5DUqqYhrERRE4YqxoehaJi3YnKI7NGUU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699372201; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kLSFAxdv6KNhnV/xuN+r8R+eidxuGYd2af8Ta5nu6pw=; b=BOQmAsvwLeP4mVQnahuDVstMaJQJUxPEUq8G/fLFBC2MZ6psRrzzQDtkxJXZ/bwwBo6cDyRM5/MbCEfCTUHG2sZzPbGq0LWrjui31asbzQSxeUNcBrPCnedk0fehyyUmHaIRacRyJVbrDt5MYdeOa55hNPB/mJunVzX+2EtNYLY= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1699372201372337.636825106842; Tue, 7 Nov 2023 07:50:01 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.628912.980866 (Exim 4.92) (envelope-from ) id 1r0OKa-0005Qb-GV; Tue, 07 Nov 2023 15:49:32 +0000 Received: by outflank-mailman (output) from mailman id 628912.980866; Tue, 07 Nov 2023 15:49:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r0OKa-0005QO-Dd; Tue, 07 Nov 2023 15:49:32 +0000 Received: by outflank-mailman (input) for mailman id 628912; Tue, 07 Nov 2023 15:49:31 +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 1r0OKZ-0004f2-4g for xen-devel@lists.xenproject.org; Tue, 07 Nov 2023 15:49:31 +0000 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [2a00:1450:4864:20::12b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3c7c6c9e-7d85-11ee-9b0e-b553b5be7939; Tue, 07 Nov 2023 16:49:29 +0100 (CET) Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-509109104e2so7674258e87.3 for ; Tue, 07 Nov 2023 07:49:29 -0800 (PST) Received: from EMEAENGAAD19049.citrite.net ([2.223.46.215]) by smtp.gmail.com with ESMTPSA id n26-20020a1c721a000000b0040849ce7116sm15809852wmc.43.2023.11.07.07.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 07:49:27 -0800 (PST) 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: 3c7c6c9e-7d85-11ee-9b0e-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1699372168; x=1699976968; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kLSFAxdv6KNhnV/xuN+r8R+eidxuGYd2af8Ta5nu6pw=; b=J0TaW+iBE/oMprdQN5MYmDwwfDdoUGAWqV3rlJ239wf7kSbQZ+byHjJBsPr9hTN6oc yDcX15KWrIMigNut/1YIN1MZIia4Dm8RkhBJLzFOkweKHAFaV7bDoya0syx2VTMEUem/ tZWNSBclyU7K8Rx5WKaBycdTtvxloDvPvyoMU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699372168; x=1699976968; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kLSFAxdv6KNhnV/xuN+r8R+eidxuGYd2af8Ta5nu6pw=; b=H807TLw/Hpv1DW7rbNN2a26TyCNJWYtSDTo9Eq2zsDSevvBHBr437oMhuC9W//OG1O CkC/vF9rA5rfR2Gc2qBLdY7qVPAxUqpYUkq+s+EG0V6CBmOwpxY0mFHMqktagXdvvBiL unpx13PIyKK4l0TgvtMq/PaB4xKkzwSfzv9W10pqRVmZjgFN+RZM/S3rCt5yAiexyOxF eT1bjZ0apJzAkZ2Pyd29uII5UgHO/aNh5pFq7YGsReQd9aYFDkwz6CkvFsIa9gXF/VIM 4k6SNmW30BmS9/c4M2uQ7hnQttKrIDttpvSkyd1uPbX/uxOaQjxhZCThN1BYfDmpu3pT XwuQ== X-Gm-Message-State: AOJu0Yz4B1xIjsS6rupYMY2st7RNJxeVrFHqfTBsgJuQRDJfVKmhadz3 FGFguOS6Vvs1Z2xeFeWKJRc7V81qRz2J6oVk8FQ= X-Google-Smtp-Source: AGHT+IEQfrpjmK80gCQLWrqBtV5qfNMtAALxBQY/fc6WYhjYZ2jjhswqBKOUvc+VpdKWeLkI+HxALQ== X-Received: by 2002:a2e:9f13:0:b0:2c6:ed5e:bbf0 with SMTP id u19-20020a2e9f13000000b002c6ed5ebbf0mr12789337ljk.34.1699372168177; Tue, 07 Nov 2023 07:49:28 -0800 (PST) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Wei Liu , Anthony PERARD , Juergen Gross Subject: [PATCH 3/7] tools/xg: Add self-(de)serialisation functions for cpu_policy Date: Tue, 7 Nov 2023 15:49:17 +0000 Message-Id: <20231107154921.54979-4-alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231107154921.54979-1-alejandro.vallejo@cloud.com> References: <20231107154921.54979-1-alejandro.vallejo@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1699372203211100005 Content-Type: text/plain; charset="utf-8" These allow a policy to internally (de)serialize itself, so that we don't have to carry around serialization buffers when perfectly good ones are present inside. Both moved on top of the xend overrides as they are needed there in future patches Signed-off-by: Alejandro Vallejo --- tools/libs/guest/xg_cpuid_x86.c | 90 ++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x8= 6.c index 3545f3e530..ac75ce2b4e 100644 --- a/tools/libs/guest/xg_cpuid_x86.c +++ b/tools/libs/guest/xg_cpuid_x86.c @@ -254,6 +254,50 @@ int xc_set_domain_cpu_policy(xc_interface *xch, uint32= _t domid, return ret; } =20 +static int cpu_policy_deserialise_on_self(xc_interface *xch, xc_cpu_policy= _t *p) +{ + uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; + int rc; + + rc =3D x86_cpuid_copy_from_buffer(&p->policy, p->leaves.buf, p->leaves= .len, + &err_leaf, &err_subleaf); + if ( rc ) + { + if ( err_leaf !=3D -1 ) + ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x)= (%d =3D %s)", + err_leaf, err_subleaf, -rc, strerror(-rc)); + return rc; + } + + rc =3D x86_msr_copy_from_buffer(&p->policy, p->msrs.buf, p->msrs.len, = &err_msr); + if ( rc ) + { + if ( err_msr !=3D -1 ) + ERROR("Failed to deserialise MSR (err MSR %#x) (%d =3D %s)", + err_msr, -rc, strerror(-rc)); + return rc; + } + + return 0; +} + +static int cpu_policy_serialise_on_self(xc_interface *xch, xc_cpu_policy_t= *p) +{ + uint32_t nr_leaves =3D p->leaves.allocated; + uint32_t nr_msrs =3D p->msrs.allocated; + int rc =3D xc_cpu_policy_serialise(xch, p, + p->leaves.buf, &nr_leaves, + p->msrs.buf, &nr_msrs); + + if ( !rc ) + { + p->leaves.len =3D nr_leaves; + p->msrs.len =3D nr_msrs; + } + + return rc; +} + static int compare_leaves(const void *l, const void *r) { const xen_cpuid_leaf_t *lhs =3D l; @@ -883,35 +927,6 @@ void xc_cpu_policy_destroy(xc_cpu_policy_t *policy) errno =3D err; } =20 -static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy, - unsigned int nr_leaves, unsigned int nr_entr= ies) -{ - uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; - int rc; - - rc =3D x86_cpuid_copy_from_buffer(&policy->policy, policy->leaves.buf, - nr_leaves, &err_leaf, &err_subleaf); - if ( rc ) - { - if ( err_leaf !=3D -1 ) - ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x)= (%d =3D %s)", - err_leaf, err_subleaf, -rc, strerror(-rc)); - return rc; - } - - rc =3D x86_msr_copy_from_buffer(&policy->policy, policy->msrs.buf, - nr_entries, &err_msr); - if ( rc ) - { - if ( err_msr !=3D -1 ) - ERROR("Failed to deserialise MSR (err MSR %#x) (%d =3D %s)", - err_msr, -rc, strerror(-rc)); - return rc; - } - - return 0; -} - int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx, xc_cpu_policy_t *policy) { @@ -931,7 +946,7 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigne= d int policy_idx, policy->leaves.len =3D nr_leaves; policy->msrs.len =3D nr_msrs; =20 - rc =3D deserialize_policy(xch, policy, nr_leaves, nr_msrs); + rc =3D cpu_policy_deserialise_on_self(xch, policy); if ( rc ) { errno =3D -rc; @@ -960,7 +975,7 @@ int xc_cpu_policy_get_domain(xc_interface *xch, uint32_= t domid, policy->leaves.len =3D nr_leaves; policy->msrs.len =3D nr_msrs; =20 - rc =3D deserialize_policy(xch, policy, nr_leaves, nr_msrs); + rc =3D cpu_policy_deserialise_on_self(xch, policy); if ( rc ) { errno =3D -rc; @@ -974,22 +989,15 @@ int xc_cpu_policy_set_domain(xc_interface *xch, uint3= 2_t domid, xc_cpu_policy_t *policy) { uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; - unsigned int nr_leaves =3D policy->leaves.allocated; - unsigned int nr_msrs =3D policy->msrs.allocated; int rc; =20 - rc =3D xc_cpu_policy_serialise(xch, policy, - policy->leaves.buf, &nr_leaves, - policy->msrs.buf, &nr_msrs); + rc =3D cpu_policy_serialise_on_self(xch, policy); if ( rc ) return rc; =20 - policy->leaves.len =3D nr_leaves; - policy->msrs.len =3D nr_msrs; - rc =3D xc_set_domain_cpu_policy(xch, domid, - nr_leaves, policy->leaves.buf, - nr_msrs, policy->msrs.buf, + policy->leaves.len, policy->leaves.buf, + policy->msrs.len, policy->msrs.buf, &err_leaf, &err_subleaf, &err_msr); if ( rc ) { --=20 2.34.1 From nobody Sat May 18 09:22:56 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=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1699372202; cv=none; d=zohomail.com; s=zohoarc; b=ixp0slhXqQYp9zKDEwhcTudCReHhmbvjW0hNs5y6tDLAIpINGhqEmZMWTVotZl5WsFTIyLnAMKX6qB1LJXF4B55tiRBnfgkE6uhmw1WBwsb2rocjttizVAC/3OejNEvLh5b01zWKqrpZhuaY7p7E7Is32k1TLSMKDoNPfoPIfUw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699372202; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0jiUkZX/lOtTWPH5LYfdV9AHbgGtbP06hzMjaTd5OCM=; b=CTZt4Zxva0Myyu24+QSm6Ax8tOqDzggBJB+dCJvavYMGb7Mqn1MBpVmxQMz4CsaUvuISAAtZMZDLfY/NsouFfhiYHc7vx91bEPp7YouEESRAjF6rCE4qeB7dc7iPcw21djLdYOzSvH+HTh7koF+pqPLeK7F+ioZ2upmXa0c1PEk= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1699372202930732.5827900358013; Tue, 7 Nov 2023 07:50:02 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.628913.980871 (Exim 4.92) (envelope-from ) id 1r0OKa-0005UR-Sj; Tue, 07 Nov 2023 15:49:32 +0000 Received: by outflank-mailman (output) from mailman id 628913.980871; Tue, 07 Nov 2023 15:49:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r0OKa-0005TF-Mq; Tue, 07 Nov 2023 15:49:32 +0000 Received: by outflank-mailman (input) for mailman id 628913; Tue, 07 Nov 2023 15:49:31 +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 1r0OKZ-0004f2-Kx for xen-devel@lists.xenproject.org; Tue, 07 Nov 2023 15:49:31 +0000 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [2a00:1450:4864:20::231]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3cd35fa3-7d85-11ee-9b0e-b553b5be7939; Tue, 07 Nov 2023 16:49:30 +0100 (CET) Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-2c504a5e1deso76812941fa.2 for ; Tue, 07 Nov 2023 07:49:30 -0800 (PST) Received: from EMEAENGAAD19049.citrite.net ([2.223.46.215]) by smtp.gmail.com with ESMTPSA id n26-20020a1c721a000000b0040849ce7116sm15809852wmc.43.2023.11.07.07.49.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 07:49:28 -0800 (PST) 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: 3cd35fa3-7d85-11ee-9b0e-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1699372169; x=1699976969; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0jiUkZX/lOtTWPH5LYfdV9AHbgGtbP06hzMjaTd5OCM=; b=aj/Jbu+QM+wxszoAF+dBDZvECfkN9N7NHUOK8Y+uYwDX2psQ3SgQiQSkHq+geeE1su 1VSlLG/zaqLqLGd7sENV21i7nplo6IHx4j6p+/0Zt9EFMDoF++NxEuVFcjXGzIOAwbsY BA0mO6Q3+iObOhOnwy33aeyNuGqYbXSENuar4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699372169; x=1699976969; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0jiUkZX/lOtTWPH5LYfdV9AHbgGtbP06hzMjaTd5OCM=; b=a8a2WxesjaZmcI88zfOL16rf8aclVPkF0AA377l97lUgva48j/w0wW3zwOpVFNn3vO JB4qJw4JO8koGI9P6mMRCjty7W5DzUTKOOf3BNuerw+INsGlqYHiqPj5yVIjD7IH3QaE C0UJAYKsacBmVBMXFIGNJLFvgCNcdhW9+VfJ0vOBmIop8p3qhMol7Ry2nMnXuT2vUgsv XwVEuV2jFxu/tms40tGdGmeDS8UEpdEvwl/otJ/BTSJtTR/0O2Dx43SORzxR2Pr68a4S nJKE7iG2yHHP8GDhnryCYn80PthAobYLUuPJlzGRu1/hu6KH7g0KUuTIG4qfUboodOoF jk9Q== X-Gm-Message-State: AOJu0YxzTEzUj0Ydnrg4pVE4w5JYyXYt0lfd3cTuq0Wi0Lc21wRolya1 cvWd8Pc2aIfx0IEHPinziyWk9LP9hdWPYuqn024= X-Google-Smtp-Source: AGHT+IFmwp4HP9zrnpfBEm8j5iQIJVQsEddmM00wdeRcT3qA+x4VfZlFYl9CWgHoK6RI5zuOeiXwew== X-Received: by 2002:a2e:b0ed:0:b0:2bd:19c5:3950 with SMTP id h13-20020a2eb0ed000000b002bd19c53950mr24956350ljl.33.1699372168684; Tue, 07 Nov 2023 07:49:28 -0800 (PST) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Wei Liu , Anthony PERARD , Juergen Gross Subject: [PATCH 4/7] tools/xg: Split xc_cpu_policy_set_domain() Date: Tue, 7 Nov 2023 15:49:18 +0000 Message-Id: <20231107154921.54979-5-alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231107154921.54979-1-alejandro.vallejo@cloud.com> References: <20231107154921.54979-1-alejandro.vallejo@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1699372203220100006 Content-Type: text/plain; charset="utf-8" xc_cpu_policy_set_domain_from_serialised() converts the cpu policy into its serialised form first and then sends that to Xen. Meanwhile, xc_cpu_policy_domain_set_from_deserialised() uses whatever is already in the internal serialisation buffer of the policy object. This split helps in a future patch where modifications are done in the serialized form and we don't want to do a serialization round-trip to send it to Xen. Signed-off-by: Alejandro Vallejo --- tools/include/xenguest.h | 8 ++++++-- tools/libs/guest/xg_cpuid_x86.c | 24 ++++++++++++++---------- tools/tests/tsx/test-tsx.c | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h index 702965addc..a66d9f7807 100644 --- a/tools/include/xenguest.h +++ b/tools/include/xenguest.h @@ -795,8 +795,12 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsign= ed int policy_idx, xc_cpu_policy_t *policy); int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid, xc_cpu_policy_t *policy); -int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t domid, - xc_cpu_policy_t *policy); +int xc_cpu_policy_set_domain_from_serialised(xc_interface *xch, + uint32_t domid, + xc_cpu_policy_t *policy); +int xc_cpu_policy_set_domain_from_deserialised(xc_interface *xch, + uint32_t domid, + xc_cpu_policy_t *policy); =20 /* Manipulate a policy via architectural representations. */ int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t *poli= cy, diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x8= 6.c index ac75ce2b4e..8fafeb2a02 100644 --- a/tools/libs/guest/xg_cpuid_x86.c +++ b/tools/libs/guest/xg_cpuid_x86.c @@ -985,20 +985,24 @@ int xc_cpu_policy_get_domain(xc_interface *xch, uint3= 2_t domid, return rc; } =20 -int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t domid, - xc_cpu_policy_t *policy) +int xc_cpu_policy_set_domain_from_deserialised(xc_interface *xch, uint32_t= domid, + xc_cpu_policy_t *policy) { - uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; int rc; - - rc =3D cpu_policy_serialise_on_self(xch, policy); - if ( rc ) + if ( (rc =3D cpu_policy_serialise_on_self(xch, policy)) ) return rc; =20 - rc =3D xc_set_domain_cpu_policy(xch, domid, - policy->leaves.len, policy->leaves.buf, - policy->msrs.len, policy->msrs.buf, - &err_leaf, &err_subleaf, &err_msr); + return xc_cpu_policy_set_domain_from_serialised(xch, domid, policy); +} + +int xc_cpu_policy_set_domain_from_serialised(xc_interface *xch, uint32_t d= omid, + xc_cpu_policy_t *policy) +{ + uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; + int rc =3D xc_set_domain_cpu_policy(xch, domid, + policy->leaves.len, policy->leaves.b= uf, + policy->msrs.len, policy->msrs.buf, + &err_leaf, &err_subleaf, &err_msr); if ( rc ) { ERROR("Failed to set domain %u policy (%d =3D %s)", domid, -rc, diff --git a/tools/tests/tsx/test-tsx.c b/tools/tests/tsx/test-tsx.c index 3371bb26f7..21b5640796 100644 --- a/tools/tests/tsx/test-tsx.c +++ b/tools/tests/tsx/test-tsx.c @@ -405,7 +405,7 @@ static void test_guest(struct xen_domctl_createdomain *= c) (bitmaskof(X86_FEATURE_HLE) | bitmaskof(X86_FEATURE_RTM))); =20 /* Set the new policy. */ - rc =3D xc_cpu_policy_set_domain(xch, domid, guest_policy); + rc =3D xc_cpu_policy_set_domain_from_deserialised(xch, domid, gues= t_policy); if ( rc ) { fail(" Failed to set domain policy: %d - %s\n", --=20 2.34.1 From nobody Sat May 18 09:22:56 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=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1699372202; cv=none; d=zohomail.com; s=zohoarc; b=RLqP//FhwmDy/DgnIsb30W+u6qGo1JG0vplbKIZSpvVfmRWYW+lNEs/PtGVodEoJZgXS6ELGnW6B820I8heTVzCQDprQA9Lra1mXD9NkdCFlDndq2zAPZvG1p8Jd0mi4petWwk335qEEKGtxt3JMwjgLxBYqDuVYZW64COWKBxQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699372202; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vDz6sGbi3amh7p4RXJVmm4OJgTg+X2ePEDBCKg+zF5k=; b=TlG1HEKwBdtHApSAUmvP+UU32vrLH1dj0zgjtBWv7VjWKO2QVfUYbAi/0QNTapyTmCcs5DF5Z5mjLYD0MjMPscZ8k88SSklV+9hwg7moALnPAgTQOexz1d+QPWQYSEGLBmWl9oSKoKDnpVk8uECt5pU/iOD/lzKgS7COErfCJL0= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1699372202096804.9246823377404; Tue, 7 Nov 2023 07:50:02 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.628914.980877 (Exim 4.92) (envelope-from ) id 1r0OKb-0005ag-6b; Tue, 07 Nov 2023 15:49:33 +0000 Received: by outflank-mailman (output) from mailman id 628914.980877; Tue, 07 Nov 2023 15:49:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r0OKb-0005Z0-2K; Tue, 07 Nov 2023 15:49:33 +0000 Received: by outflank-mailman (input) for mailman id 628914; Tue, 07 Nov 2023 15:49:32 +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 1r0OKZ-0004f1-S3 for xen-devel@lists.xenproject.org; Tue, 07 Nov 2023 15:49:31 +0000 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [2a00:1450:4864:20::32e]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3d4b71b0-7d85-11ee-98da-6d05b1d4d9a1; Tue, 07 Nov 2023 16:49:30 +0100 (CET) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-40838915cecso42705195e9.2 for ; Tue, 07 Nov 2023 07:49:30 -0800 (PST) Received: from EMEAENGAAD19049.citrite.net ([2.223.46.215]) by smtp.gmail.com with ESMTPSA id n26-20020a1c721a000000b0040849ce7116sm15809852wmc.43.2023.11.07.07.49.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 07:49:28 -0800 (PST) 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: 3d4b71b0-7d85-11ee-98da-6d05b1d4d9a1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1699372170; x=1699976970; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vDz6sGbi3amh7p4RXJVmm4OJgTg+X2ePEDBCKg+zF5k=; b=lSDk6vs8/JC4JmQtCcUUdEEHUZ37PXu+OmTBD0qgdCb9BOA7j+99gwK95WFJ56tfh9 nDoWJYcwmMaHValPZxzCCCxINCoJWfCIMpu1ZlgPjNqq3qmDo2MaHiO+MDLOEzyaaieS Z+JcpngiJXPUjJfiXUuonYjFJKRnWKgZkB0Fg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699372170; x=1699976970; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vDz6sGbi3amh7p4RXJVmm4OJgTg+X2ePEDBCKg+zF5k=; b=gpTDMJS9qXvyEyzjUGV1kcBXcEJyOFIGF1W0FN6S+t9Z9OhvmjVURYBvOIh6NVo1uz XJENLUVWFUULGiVaYTYXCMJNWDoB9+YQlRkhNJMeDrVbeUpTc5F+7rRRL4gph2z7QaDC MPGETNQ9pGhp4zC7yhY939W2Dpdg6rhiKIS130uMKJp0uA8r9En5XT1ZXRv2Ef911C4S v8bjVJbdE5d47BnLHilO9r5/IBWlEod4D3nAo6XtQrOOvVsuLN1L48GVxCgzZO1w4Sow lAYHoVIcW3XJNAo+R6aDnbM3sqCqK/pVBFqcdm2JExEglJhKkH29s3FeOI9SO7Ud7NIb aigA== X-Gm-Message-State: AOJu0YzOy7UbsQyzu2paQP9g3OVrcwmmApDoQKQHEe9mvWMeM2vLShRl N8E+o1uO/fMlrd7Gv28W2TrBBFOM9TCDKMEGQDY= X-Google-Smtp-Source: AGHT+IFmW3jjWcsrqSaiOv0teP9ieNlXQyUDrN5eM399cB2e2LXIW4gFHVF2DUtXCSDhz27yUCHcSg== X-Received: by 2002:a05:600c:46d4:b0:405:49aa:d578 with SMTP id q20-20020a05600c46d400b0040549aad578mr2552796wmo.37.1699372170112; Tue, 07 Nov 2023 07:49:30 -0800 (PST) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Wei Liu , Anthony PERARD , Juergen Gross Subject: [PATCH 5/7] tools/xg: Streamline xc_cpuid_apply_policy() Date: Tue, 7 Nov 2023 15:49:19 +0000 Message-Id: <20231107154921.54979-6-alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231107154921.54979-1-alejandro.vallejo@cloud.com> References: <20231107154921.54979-1-alejandro.vallejo@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1699372203277100009 Content-Type: text/plain; charset="utf-8" Instantiates host, default and domain policies in order to clean up a lot of boilerplate hypercalls. This is partial work in order to deduplicate the same hypercalls being used when updating CPUID and MSR parts of the policy. Signed-off-by: Alejandro Vallejo --- tools/include/xenguest.h | 1 + tools/libs/guest/xg_cpuid_x86.c | 184 ++++++++++++++++---------------- 2 files changed, 93 insertions(+), 92 deletions(-) diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h index a66d9f7807..f0b58bb395 100644 --- a/tools/include/xenguest.h +++ b/tools/include/xenguest.h @@ -788,6 +788,7 @@ typedef struct xc_cpu_policy xc_cpu_policy_t; =20 /* Create and free a xc_cpu_policy object. */ xc_cpu_policy_t *xc_cpu_policy_init(xc_interface *xch); +xc_cpu_policy_t *xc_cpu_policy_clone(const xc_cpu_policy_t *other); void xc_cpu_policy_destroy(xc_cpu_policy_t *policy); =20 /* Retrieve a system policy, or get/set a domains policy. */ diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x8= 6.c index 8fafeb2a02..acc94fb16b 100644 --- a/tools/libs/guest/xg_cpuid_x86.c +++ b/tools/libs/guest/xg_cpuid_x86.c @@ -635,13 +635,14 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t= domid, bool restore, int rc; bool hvm; xc_domaininfo_t di; - unsigned int i, nr_leaves, nr_msrs; - xen_cpuid_leaf_t *leaves =3D NULL; - struct cpu_policy *p =3D NULL; - uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; - uint32_t host_featureset[FEATURESET_NR_ENTRIES] =3D {}; - uint32_t len =3D ARRAY_SIZE(host_featureset); + uint32_t def_policy; + /* + * Three full policies. The host, default for the domain type, + * and domain current. + */ + xc_cpu_policy_t *host =3D NULL, *def =3D NULL, *cur =3D NULL; =20 + /* Determine if domid is PV or HVM */ if ( (rc =3D xc_domain_getinfo_single(xch, domid, &di)) < 0 ) { PERROR("Failed to obtain d%d info", domid); @@ -650,48 +651,24 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t= domid, bool restore, } hvm =3D di.flags & XEN_DOMINF_hvm_guest; =20 - rc =3D xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs); - if ( rc ) - { - PERROR("Failed to obtain policy info size"); - rc =3D -errno; - goto out; - } - - rc =3D -ENOMEM; - if ( (leaves =3D calloc(nr_leaves, sizeof(*leaves))) =3D=3D NULL || - (p =3D calloc(1, sizeof(*p))) =3D=3D NULL ) - goto out; - - /* Get the host policy. */ - rc =3D xc_get_cpu_featureset(xch, XEN_SYSCTL_cpu_featureset_host, - &len, host_featureset); - /* Tolerate "buffer too small", as we've got the bits we need. */ - if ( rc && errno !=3D ENOBUFS ) + if ( !(host =3D xc_cpu_policy_init(xch)) || + !(def =3D xc_cpu_policy_clone(host)) || + !(cur =3D xc_cpu_policy_clone(host)) ) { - PERROR("Failed to obtain host featureset"); - rc =3D -errno; + PERROR("Failed to allocate policy state"); goto out; } =20 - /* Get the domain's default policy. */ - nr_msrs =3D 0; - rc =3D get_system_cpu_policy(xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_defa= ult - : XEN_SYSCTL_cpu_policy_pv_default, - &nr_leaves, leaves, &nr_msrs, NULL); - if ( rc ) - { - PERROR("Failed to obtain %s default policy", hvm ? "hvm" : "pv"); - rc =3D -errno; - goto out; - } + def_policy =3D hvm ? XEN_SYSCTL_cpu_policy_hvm_default + : XEN_SYSCTL_cpu_policy_pv_default; =20 - rc =3D x86_cpuid_copy_from_buffer(p, leaves, nr_leaves, - &err_leaf, &err_subleaf); - if ( rc ) + /* Get the domain type's default policy. */ + if ( (rc =3D xc_cpu_policy_get_domain(xch, domid, cur)) || + (rc =3D xc_cpu_policy_get_system(xch, def_policy, def)) || + (rc =3D xc_cpu_policy_get_system(xch, XEN_SYSCTL_cpu_policy_host,= host)) ) { - ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d= =3D %s)", - err_leaf, err_subleaf, -rc, strerror(-rc)); + PERROR("Failed to obtain d%d, %s and/or host policies", + domid, hvm ? "hvm" : "pv"); goto out; } =20 @@ -711,18 +688,16 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t= domid, bool restore, * - Re-enable features which have become (possibly) off by defaul= t. */ =20 - p->basic.rdrand =3D test_bit(X86_FEATURE_RDRAND, host_featureset); - p->feat.hle =3D test_bit(X86_FEATURE_HLE, host_featureset); - p->feat.rtm =3D test_bit(X86_FEATURE_RTM, host_featureset); + cur->policy.basic.rdrand =3D host->policy.basic.rdrand; + cur->policy.feat.hle =3D host->policy.feat.hle; + cur->policy.feat.rtm =3D host->policy.feat.rtm; =20 if ( hvm ) - { - p->feat.mpx =3D test_bit(X86_FEATURE_MPX, host_featureset); - } + cur->policy.feat.mpx =3D host->policy.feat.mpx; =20 - p->basic.max_leaf =3D min(p->basic.max_leaf, 0xdu); - p->feat.max_subleaf =3D 0; - p->extd.max_leaf =3D min(p->extd.max_leaf, 0x8000001c); + cur->policy.basic.max_leaf =3D min(host->policy.basic.max_leaf, 0x= du); + cur->policy.feat.max_subleaf =3D 0; + cur->policy.extd.max_leaf =3D min(host->policy.extd.max_leaf, 0x80= 00001c); } =20 if ( featureset ) @@ -730,7 +705,6 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t d= omid, bool restore, uint32_t disabled_features[FEATURESET_NR_ENTRIES], feat[FEATURESET_NR_ENTRIES] =3D {}; static const uint32_t deep_features[] =3D INIT_DEEP_FEATURES; - unsigned int i, b; =20 /* * The user supplied featureset may be shorter or longer than @@ -741,17 +715,17 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t= domid, bool restore, =20 /* Check for truncated set bits. */ rc =3D -EOPNOTSUPP; - for ( i =3D user_len; i < nr_features; ++i ) + for ( size_t i =3D user_len; i < nr_features; ++i ) if ( featureset[i] !=3D 0 ) goto out; =20 memcpy(feat, featureset, sizeof(*featureset) * user_len); =20 /* Disable deep dependencies of disabled features. */ - for ( i =3D 0; i < ARRAY_SIZE(disabled_features); ++i ) + for ( size_t i =3D 0; i < ARRAY_SIZE(disabled_features); ++i ) disabled_features[i] =3D ~feat[i] & deep_features[i]; =20 - for ( b =3D 0; b < sizeof(disabled_features) * CHAR_BIT; ++b ) + for ( size_t b =3D 0; b < sizeof(disabled_features) * CHAR_BIT; ++= b ) { const uint32_t *dfs; =20 @@ -759,24 +733,24 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t= domid, bool restore, !(dfs =3D x86_cpu_policy_lookup_deep_deps(b)) ) continue; =20 - for ( i =3D 0; i < ARRAY_SIZE(disabled_features); ++i ) + for ( size_t i =3D 0; i < ARRAY_SIZE(disabled_features); ++i ) { feat[i] &=3D ~dfs[i]; disabled_features[i] &=3D ~dfs[i]; } } =20 - x86_cpu_featureset_to_policy(feat, p); + x86_cpu_featureset_to_policy(feat, &cur->policy); } else { - p->extd.itsc =3D itsc; + cur->policy.extd.itsc =3D itsc; =20 if ( hvm ) { - p->basic.pae =3D pae; - p->basic.vmx =3D nested_virt; - p->extd.svm =3D nested_virt; + cur->policy.basic.pae =3D pae; + cur->policy.basic.vmx =3D nested_virt; + cur->policy.extd.svm =3D nested_virt; } } =20 @@ -786,8 +760,8 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t d= omid, bool restore, * On hardware without CPUID Faulting, PV guests see real topology. * As a consequence, they also need to see the host htt/cmp fields. */ - p->basic.htt =3D test_bit(X86_FEATURE_HTT, host_featureset); - p->extd.cmp_legacy =3D test_bit(X86_FEATURE_CMP_LEGACY, host_featu= reset); + cur->policy.basic.htt =3D host->policy.basic.htt; + cur->policy.extd.cmp_legacy =3D host->policy.extd.cmp_legacy; } else { @@ -795,28 +769,28 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t= domid, bool restore, * Topology for HVM guests is entirely controlled by Xen. For now= , we * hardcode APIC_ID =3D vcpu_id * 2 to give the illusion of no SMT. */ - p->basic.htt =3D true; - p->extd.cmp_legacy =3D false; + cur->policy.basic.htt =3D true; + cur->policy.extd.cmp_legacy =3D false; =20 /* * Leaf 1 EBX[23:16] is Maximum Logical Processors Per Package. * Update to reflect vLAPIC_ID =3D vCPU_ID * 2, but make sure to a= void * overflow. */ - if ( !p->basic.lppp ) - p->basic.lppp =3D 2; - else if ( !(p->basic.lppp & 0x80) ) - p->basic.lppp *=3D 2; + if ( !cur->policy.basic.lppp ) + cur->policy.basic.lppp =3D 2; + else if ( !(cur->policy.basic.lppp & 0x80) ) + cur->policy.basic.lppp *=3D 2; =20 - switch ( p->x86_vendor ) + switch ( cur->policy.x86_vendor ) { case X86_VENDOR_INTEL: - for ( i =3D 0; (p->cache.subleaf[i].type && - i < ARRAY_SIZE(p->cache.raw)); ++i ) + for ( size_t i =3D 0; (cur->policy.cache.subleaf[i].type && + i < ARRAY_SIZE(cur->policy.cache.raw)); ++i ) { - p->cache.subleaf[i].cores_per_package =3D - (p->cache.subleaf[i].cores_per_package << 1) | 1; - p->cache.subleaf[i].threads_per_cache =3D 0; + cur->policy.cache.subleaf[i].cores_per_package =3D + (cur->policy.cache.subleaf[i].cores_per_package << 1) = | 1; + cur->policy.cache.subleaf[i].threads_per_cache =3D 0; } break; =20 @@ -836,30 +810,22 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t= domid, bool restore, * apic_id_size values greater than 7. Limit the value to * 7 for now. */ - if ( p->extd.nc < 0x7f ) + if ( cur->policy.extd.nc < 0x7f ) { - if ( p->extd.apic_id_size !=3D 0 && p->extd.apic_id_size <= 0x7 ) - p->extd.apic_id_size++; + if ( cur->policy.extd.apic_id_size !=3D 0 && + cur->policy.extd.apic_id_size < 0x7 ) + cur->policy.extd.apic_id_size++; =20 - p->extd.nc =3D (p->extd.nc << 1) | 1; + cur->policy.extd.nc =3D (cur->policy.extd.nc << 1) | 1; } break; } } =20 - rc =3D x86_cpuid_copy_to_buffer(p, leaves, &nr_leaves); - if ( rc ) - { - ERROR("Failed to serialise CPUID (%d =3D %s)", -rc, strerror(-rc)); - goto out; - } - - rc =3D xc_set_domain_cpu_policy(xch, domid, nr_leaves, leaves, 0, NULL, - &err_leaf, &err_subleaf, &err_msr); + /* Re-apply the mutated policy onto the domain */ + rc =3D xc_cpu_policy_set_domain(xch, domid, cur); if ( rc ) { - PERROR("Failed to set d%d's policy (err leaf %#x, subleaf %#x, msr= %#x)", - domid, err_leaf, err_subleaf, err_msr); rc =3D -errno; goto out; } @@ -877,8 +843,9 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t d= omid, bool restore, rc =3D 0; =20 out: - free(p); - free(leaves); + xc_cpu_policy_destroy(cur); + xc_cpu_policy_destroy(def); + xc_cpu_policy_destroy(host); =20 return rc; } @@ -911,6 +878,39 @@ xc_cpu_policy_t *xc_cpu_policy_init(xc_interface *xch) return NULL; } =20 +xc_cpu_policy_t *xc_cpu_policy_clone(const xc_cpu_policy_t *other) +{ + xc_cpu_policy_t *policy; + + if ( !other ) + return NULL; + + policy =3D malloc(sizeof(*policy)); + if ( !policy ) + return NULL; + + *policy =3D *other; + + /* Override every buffer with identical new ones */ + policy->leaves.buf =3D calloc(other->leaves.allocated, + sizeof(*other->leaves.buf)); + policy->msrs.buf =3D calloc(other->msrs.allocated, + sizeof(*other->msrs.buf)); + + if ( !policy->leaves.buf || !policy->msrs.buf ) + { + xc_cpu_policy_destroy(policy); + return NULL; + } + + memcpy(policy->leaves.buf, other->leaves.buf, + other->leaves.allocated); + memcpy(policy->msrs.buf, other->msrs.buf, + other->msrs.allocated); + + return policy; +} + void xc_cpu_policy_destroy(xc_cpu_policy_t *policy) { int err =3D errno; --=20 2.34.1 From nobody Sat May 18 09:22:56 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=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1699372201; cv=none; d=zohomail.com; s=zohoarc; b=jahfI04NeaFiF+GFWo9i3FWKIKTJiE+zU3fzbvdUupy7tZgzMZa/of76RxaS9AnAb8Y/fYEJkfB1REh5cLQRQPX27xTJsnCA0lkY+r8sABGWOquXTL/nVF+xw89ufgKUrsob1cw3nCqlQW1jaCQBiMX3LGjgRmQGy5PlzOV6YoY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699372201; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Zg9I/CTQ9r8/XZi0s/EqCV9zo0mb+8u/y0FvnwuuK8Q=; b=POXI7w1yvPtxDRN0rP4is6Y8WyHTO4VUtBbQ2vI75Ub9p1BH2G6QMa4G7LM7MIisfKVlHIrd8jfRJN4OFXtQj+lyB48zJT12ofIabcL6zYi7lg4c3XV4Nly1QLKyPfftTG5eAA6fP25/CF/h7GPxFHRu0Le4uLWoO7fKsThzZe4= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1699372201831492.25503601652974; Tue, 7 Nov 2023 07:50:01 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.628915.980895 (Exim 4.92) (envelope-from ) id 1r0OKc-00068s-O3; Tue, 07 Nov 2023 15:49:34 +0000 Received: by outflank-mailman (output) from mailman id 628915.980895; Tue, 07 Nov 2023 15:49:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r0OKc-00068B-Kl; Tue, 07 Nov 2023 15:49:34 +0000 Received: by outflank-mailman (input) for mailman id 628915; Tue, 07 Nov 2023 15:49:33 +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 1r0OKb-0004f2-Jc for xen-devel@lists.xenproject.org; Tue, 07 Nov 2023 15:49:33 +0000 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [2a00:1450:4864:20::330]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3dc9f351-7d85-11ee-9b0e-b553b5be7939; Tue, 07 Nov 2023 16:49:31 +0100 (CET) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-40837ebba42so36606305e9.0 for ; Tue, 07 Nov 2023 07:49:31 -0800 (PST) Received: from EMEAENGAAD19049.citrite.net ([2.223.46.215]) by smtp.gmail.com with ESMTPSA id n26-20020a1c721a000000b0040849ce7116sm15809852wmc.43.2023.11.07.07.49.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 07:49:30 -0800 (PST) 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: 3dc9f351-7d85-11ee-9b0e-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1699372171; x=1699976971; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Zg9I/CTQ9r8/XZi0s/EqCV9zo0mb+8u/y0FvnwuuK8Q=; b=ilCAKlVlmt+kYaPsSoS8uEzEHCp1Hy4tlxP81q1iUoMRNVXg41b0V0cvtGXG/7L5/X QFhLkQwRb8AtlTv/eQ63j4AtjrzQxr1o/G7KkRj7pKsYxBAYWV9IryZaFlUiONR2JSn9 wtL5SQCYsLCzAaXARgn/k/v1fxHocrGtBUG/A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699372171; x=1699976971; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Zg9I/CTQ9r8/XZi0s/EqCV9zo0mb+8u/y0FvnwuuK8Q=; b=dz+niKDzCIphwFgPNrCiUxcUOD9HfeHSCPhEuArT7m+6vGpq0Dh7RIRmez4kg8NFuT yAR0LD9ZbLhmtjhDtvzAh2+9IOJ7/tWFb8TQccK/chQWUcmMG869YMUidXwOw4XsSN09 FkruRqI9kHWwYRCofPyUrXdBa4qbaKCHHKkpB5jXVlOXF4L11OHjqs64v7A301qOFB4a Jpb3kyYLYJczkrfEx0/J3TRzkKuxA+BnIPpwVlv0TPu7wNWg4w3jRvvvGDbVqts2L6mz 1LexHeNgk8/36W2uOdXUNB78nDaNIEr+MGMUABpvFddJT3j675F6mPk14NI/C9EUQXMO bJYA== X-Gm-Message-State: AOJu0YxDMm0hH75FZY8aWubJNwFlnqwsXdU857gs9VQLssNI9DpYpm6e XsZnmt/Nam5eUGXJ5lDiCMnNuzEBtpaRz8j1aWg= X-Google-Smtp-Source: AGHT+IGHBCo0k8bNpvm6r5Lr7wMITTEJcpxCM8440tjO1RCgbZ8S1fYoK/EGjveG3B1gn2/3WKFp8g== X-Received: by 2002:a05:600c:45cd:b0:405:1c14:9227 with SMTP id s13-20020a05600c45cd00b004051c149227mr2921945wmo.33.1699372170830; Tue, 07 Nov 2023 07:49:30 -0800 (PST) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Wei Liu , Anthony PERARD , Juergen Gross Subject: [PATCH 6/7] tools/xg: Simplify xc_cpuid_xend_policy() and xc_msr_policy() Date: Tue, 7 Nov 2023 15:49:20 +0000 Message-Id: <20231107154921.54979-7-alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231107154921.54979-1-alejandro.vallejo@cloud.com> References: <20231107154921.54979-1-alejandro.vallejo@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1699372203273100007 Content-Type: text/plain; charset="utf-8" Remove all duplication in CPUID and MSR xend-style overrides. They had an incredible amount of overhead for no good reason. After this patch, CPU policy application takes a number of hypercalls to recover the policy state and then those are passed to the xend-style override code so it can avoid them. Furthermore, the ultimate reapplication of the policy to the domain in Xen is done only once after both CPUID and MSRs have been fixed up. BUG!!! apply_policy is sending the policy after deserialise when it poked at it in its serialised form. Signed-off-by: Alejandro Vallejo --- tools/libs/guest/xg_cpuid_x86.c | 261 +++++--------------------------- 1 file changed, 38 insertions(+), 223 deletions(-) diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x8= 6.c index acc94fb16b..e2a2659953 100644 --- a/tools/libs/guest/xg_cpuid_x86.c +++ b/tools/libs/guest/xg_cpuid_x86.c @@ -312,102 +312,32 @@ static int compare_leaves(const void *l, const void = *r) return 0; } =20 -static xen_cpuid_leaf_t *find_leaf( - xen_cpuid_leaf_t *leaves, unsigned int nr_leaves, - const struct xc_xend_cpuid *xend) +static xen_cpuid_leaf_t *find_leaf(xc_cpu_policy_t *p, + const struct xc_xend_cpuid *xend) { const xen_cpuid_leaf_t key =3D { xend->leaf, xend->subleaf }; =20 - return bsearch(&key, leaves, nr_leaves, sizeof(*leaves), compare_leave= s); + return bsearch(&key, p->leaves.buf, p->leaves.len, + sizeof(*p->leaves.buf), compare_leaves); } =20 static int xc_cpuid_xend_policy( - xc_interface *xch, uint32_t domid, const struct xc_xend_cpuid *xend) + xc_interface *xch, uint32_t domid, + const struct xc_xend_cpuid *xend, + xc_cpu_policy_t *host, + xc_cpu_policy_t *def, + xc_cpu_policy_t *cur) { - int rc; - bool hvm; - xc_domaininfo_t di; - unsigned int nr_leaves, nr_msrs; - uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; - /* - * Three full policies. The host, default for the domain type, - * and domain current. - */ - xen_cpuid_leaf_t *host =3D NULL, *def =3D NULL, *cur =3D NULL; - unsigned int nr_host, nr_def, nr_cur; - - if ( (rc =3D xc_domain_getinfo_single(xch, domid, &di)) < 0 ) - { - PERROR("Failed to obtain d%d info", domid); - rc =3D -errno; - goto fail; - } - hvm =3D di.flags & XEN_DOMINF_hvm_guest; - - rc =3D xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs); - if ( rc ) - { - PERROR("Failed to obtain policy info size"); - rc =3D -errno; - goto fail; - } - - rc =3D -ENOMEM; - if ( (host =3D calloc(nr_leaves, sizeof(*host))) =3D=3D NULL || - (def =3D calloc(nr_leaves, sizeof(*def))) =3D=3D NULL || - (cur =3D calloc(nr_leaves, sizeof(*cur))) =3D=3D NULL ) - { - ERROR("Unable to allocate memory for %u CPUID leaves", nr_leaves); - goto fail; - } - - /* Get the domain's current policy. */ - nr_msrs =3D 0; - nr_cur =3D nr_leaves; - rc =3D get_domain_cpu_policy(xch, domid, &nr_cur, cur, &nr_msrs, NULL); - if ( rc ) - { - PERROR("Failed to obtain d%d current policy", domid); - rc =3D -errno; - goto fail; - } - - /* Get the domain type's default policy. */ - nr_msrs =3D 0; - nr_def =3D nr_leaves; - rc =3D get_system_cpu_policy(xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_defa= ult - : XEN_SYSCTL_cpu_policy_pv_default, - &nr_def, def, &nr_msrs, NULL); - if ( rc ) - { - PERROR("Failed to obtain %s def policy", hvm ? "hvm" : "pv"); - rc =3D -errno; - goto fail; - } - - /* Get the host policy. */ - nr_msrs =3D 0; - nr_host =3D nr_leaves; - rc =3D get_system_cpu_policy(xch, XEN_SYSCTL_cpu_policy_host, - &nr_host, host, &nr_msrs, NULL); - if ( rc ) - { - PERROR("Failed to obtain host policy"); - rc =3D -errno; - goto fail; - } - - rc =3D -EINVAL; for ( ; xend->leaf !=3D XEN_CPUID_INPUT_UNUSED; ++xend ) { - xen_cpuid_leaf_t *cur_leaf =3D find_leaf(cur, nr_cur, xend); - const xen_cpuid_leaf_t *def_leaf =3D find_leaf(def, nr_def, xend); - const xen_cpuid_leaf_t *host_leaf =3D find_leaf(host, nr_host, xen= d); + xen_cpuid_leaf_t *cur_leaf =3D find_leaf(cur, xend); + const xen_cpuid_leaf_t *def_leaf =3D find_leaf(def, xend); + const xen_cpuid_leaf_t *host_leaf =3D find_leaf(host, xend); =20 if ( cur_leaf =3D=3D NULL || def_leaf =3D=3D NULL || host_leaf =3D= =3D NULL ) { ERROR("Missing leaf %#x, subleaf %#x", xend->leaf, xend->suble= af); - goto fail; + return -EINVAL; } =20 for ( unsigned int i =3D 0; i < ARRAY_SIZE(xend->policy); i++ ) @@ -436,7 +366,7 @@ static int xc_cpuid_xend_policy( { ERROR("Bad character '%c' in policy[%d] string '%s'", xend->policy[i][j], i, xend->policy[i]); - goto fail; + return -EINVAL; } =20 clear_bit(31 - j, cur_reg); @@ -446,25 +376,7 @@ static int xc_cpuid_xend_policy( } } =20 - /* Feed the transformed currrent policy back up to Xen. */ - rc =3D xc_set_domain_cpu_policy(xch, domid, nr_cur, cur, 0, NULL, - &err_leaf, &err_subleaf, &err_msr); - if ( rc ) - { - PERROR("Failed to set d%d's policy (err leaf %#x, subleaf %#x, msr= %#x)", - domid, err_leaf, err_subleaf, err_msr); - rc =3D -errno; - goto fail; - } - - /* Success! */ - - fail: - free(cur); - free(def); - free(host); - - return rc; + return 0; } =20 static int compare_msr(const void *l, const void *r) @@ -478,104 +390,31 @@ static int compare_msr(const void *l, const void *r) return lhs->idx < rhs->idx ? -1 : 1; } =20 -static xen_msr_entry_t *find_msr( - xen_msr_entry_t *msrs, unsigned int nr_msrs, - uint32_t index) +static xen_msr_entry_t *find_msr(xc_cpu_policy_t *p, uint32_t index) { const xen_msr_entry_t key =3D { .idx =3D index }; =20 - return bsearch(&key, msrs, nr_msrs, sizeof(*msrs), compare_msr); + return bsearch(&key, p->msrs.buf, p->msrs.len, + sizeof(*p->msrs.buf), compare_msr); } =20 - static int xc_msr_policy(xc_interface *xch, domid_t domid, - const struct xc_msr *msr) + const struct xc_msr *msr, + xc_cpu_policy_t *host, + xc_cpu_policy_t *def, + xc_cpu_policy_t *cur) { - int rc; - bool hvm; - xc_domaininfo_t di; - unsigned int nr_leaves, nr_msrs; - uint32_t err_leaf =3D -1, err_subleaf =3D -1, err_msr =3D -1; - /* - * Three full policies. The host, default for the domain type, - * and domain current. - */ - xen_msr_entry_t *host =3D NULL, *def =3D NULL, *cur =3D NULL; - unsigned int nr_host, nr_def, nr_cur; - - if ( (rc =3D xc_domain_getinfo_single(xch, domid, &di)) < 0 ) - { - PERROR("Failed to obtain d%d info", domid); - rc =3D -errno; - goto out; - } - hvm =3D di.flags & XEN_DOMINF_hvm_guest; - - rc =3D xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs); - if ( rc ) - { - PERROR("Failed to obtain policy info size"); - rc =3D -errno; - goto out; - } - - if ( (host =3D calloc(nr_msrs, sizeof(*host))) =3D=3D NULL || - (def =3D calloc(nr_msrs, sizeof(*def))) =3D=3D NULL || - (cur =3D calloc(nr_msrs, sizeof(*cur))) =3D=3D NULL ) - { - ERROR("Unable to allocate memory for %u CPUID leaves", nr_leaves); - rc =3D -ENOMEM; - goto out; - } - - /* Get the domain's current policy. */ - nr_leaves =3D 0; - nr_cur =3D nr_msrs; - rc =3D get_domain_cpu_policy(xch, domid, &nr_leaves, NULL, &nr_cur, cu= r); - if ( rc ) - { - PERROR("Failed to obtain d%d current policy", domid); - rc =3D -errno; - goto out; - } - - /* Get the domain type's default policy. */ - nr_leaves =3D 0; - nr_def =3D nr_msrs; - rc =3D get_system_cpu_policy(xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_defa= ult - : XEN_SYSCTL_cpu_policy_pv_default, - &nr_leaves, NULL, &nr_def, def); - if ( rc ) - { - PERROR("Failed to obtain %s def policy", hvm ? "hvm" : "pv"); - rc =3D -errno; - goto out; - } - - /* Get the host policy. */ - nr_leaves =3D 0; - nr_host =3D nr_msrs; - rc =3D get_system_cpu_policy(xch, XEN_SYSCTL_cpu_policy_host, - &nr_leaves, NULL, &nr_host, host); - if ( rc ) - { - PERROR("Failed to obtain host policy"); - rc =3D -errno; - goto out; - } - for ( ; msr->index !=3D XC_MSR_INPUT_UNUSED; ++msr ) { - xen_msr_entry_t *cur_msr =3D find_msr(cur, nr_cur, msr->index); - const xen_msr_entry_t *def_msr =3D find_msr(def, nr_def, msr->inde= x); - const xen_msr_entry_t *host_msr =3D find_msr(host, nr_host, msr->i= ndex); + xen_msr_entry_t *cur_msr =3D find_msr(cur, msr->index); + const xen_msr_entry_t *def_msr =3D find_msr(def, msr->index); + const xen_msr_entry_t *host_msr =3D find_msr(host, msr->index); unsigned int i; =20 if ( cur_msr =3D=3D NULL || def_msr =3D=3D NULL || host_msr =3D=3D= NULL ) { ERROR("Missing MSR %#x", msr->index); - rc =3D -ENOENT; - goto out; + return -ENOENT; } =20 for ( i =3D 0; i < ARRAY_SIZE(msr->policy) - 1; i++ ) @@ -594,8 +433,7 @@ static int xc_msr_policy(xc_interface *xch, domid_t dom= id, { ERROR("MSR index %#x: bad character '%c' in policy string = '%s'", msr->index, msr->policy[i], msr->policy); - rc =3D -EINVAL; - goto out; + return -EINVAL; } =20 if ( val ) @@ -605,25 +443,7 @@ static int xc_msr_policy(xc_interface *xch, domid_t do= mid, } } =20 - /* Feed the transformed policy back up to Xen. */ - rc =3D xc_set_domain_cpu_policy(xch, domid, 0, NULL, nr_cur, cur, - &err_leaf, &err_subleaf, &err_msr); - if ( rc ) - { - PERROR("Failed to set d%d's policy (err leaf %#x, subleaf %#x, msr= %#x)", - domid, err_leaf, err_subleaf, err_msr); - rc =3D -errno; - goto out; - } - - /* Success! */ - - out: - free(cur); - free(def); - free(host); - - return rc; + return 0; } =20 int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, @@ -822,25 +642,20 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t= domid, bool restore, } } =20 - /* Re-apply the mutated policy onto the domain */ - rc =3D xc_cpu_policy_set_domain(xch, domid, cur); - if ( rc ) - { - rc =3D -errno; + /* + * Ensure changes we just made are reflected in the serialised form of + * the policy, as otherwise they they will be missed by the xend overr= ides + */ + if ( (rc =3D cpu_policy_serialise_on_self(xch, cur)) ) goto out; - } =20 - if ( xend && (rc =3D xc_cpuid_xend_policy(xch, domid, xend)) ) + /* Apply xend-style overrides */ + if ( (xend && (rc =3D xc_cpuid_xend_policy(xch, domid, xend, host, def= , cur))) || + (msr && (rc =3D xc_msr_policy(xch, domid, msr, host, def, cur))) ) goto out; =20 - if ( msr ) - { - rc =3D xc_msr_policy(xch, domid, msr); - if ( rc ) - goto out; - } - - rc =3D 0; + /* Re-apply the mutated policy onto the domain */ + rc =3D xc_cpu_policy_set_domain_from_serialised(xch, domid, cur); =20 out: xc_cpu_policy_destroy(cur); --=20 2.34.1 From nobody Sat May 18 09:22:56 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=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1699372203; cv=none; d=zohomail.com; s=zohoarc; b=INND5OlOmwaUtbAY7NC0a/Ik/6gDxZzisv90HMnjreoI1nv8erWnWZ2o+5t/MmXwn4ZFmtBN+MlRrHybOxMw1WZvWEV1NLF/Q9GpIA4NP1N36bXm7IfTLRJGwRPCxjC99BN7YPF/cTEkR1RQRJ2hwOdIvftDnJK6cYt0GpylDSg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699372203; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=wDBR4US0usgZD2c5iZwJM+sDkiWc35BKuxl/wMSfHac=; b=j33+/eeIu/0v+rNg+wvIHpTDSdXQ3/Ln7sRYsAx4x8/Iwdl46IKUWDpqJU7EkQcOFWvr8qVCA0URe31ELnsRr/ig93i1SUmx/AZB56wobZcmzxYjt6+g0ynI7tXQCFS6GpsingDelEXYQt4/zJ4H5gZiFgRjZs04CjVHOC9HYpE= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1699372203455880.2165503726275; Tue, 7 Nov 2023 07:50:03 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.628916.980903 (Exim 4.92) (envelope-from ) id 1r0OKd-0006Gf-9u; Tue, 07 Nov 2023 15:49:35 +0000 Received: by outflank-mailman (output) from mailman id 628916.980903; Tue, 07 Nov 2023 15:49:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r0OKd-0006F9-3E; Tue, 07 Nov 2023 15:49:35 +0000 Received: by outflank-mailman (input) for mailman id 628916; Tue, 07 Nov 2023 15:49:34 +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 1r0OKc-0004f2-75 for xen-devel@lists.xenproject.org; Tue, 07 Nov 2023 15:49:34 +0000 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [2a00:1450:4864:20::629]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3e36e0fc-7d85-11ee-9b0e-b553b5be7939; Tue, 07 Nov 2023 16:49:32 +0100 (CET) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-9c41e95efcbso867542466b.3 for ; Tue, 07 Nov 2023 07:49:32 -0800 (PST) Received: from EMEAENGAAD19049.citrite.net ([2.223.46.215]) by smtp.gmail.com with ESMTPSA id n26-20020a1c721a000000b0040849ce7116sm15809852wmc.43.2023.11.07.07.49.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 07:49:31 -0800 (PST) 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: 3e36e0fc-7d85-11ee-9b0e-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1699372171; x=1699976971; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wDBR4US0usgZD2c5iZwJM+sDkiWc35BKuxl/wMSfHac=; b=QvNIK8271sfLDZrFCM5JT9b7DQNM5NA4KOVIdJaxFPtGwGIZl7ur4B15mcP87fgW7o zlmwwg26u0pMJvxMuM45W3Xt8zR4Zdqkgk9xkzt8zGeqLLOxQ0b3fkKw08yb2TdV5TXl 0GjeOV8h66RY8Htsr3WxIXCkLUruphaqnsfvc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699372171; x=1699976971; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wDBR4US0usgZD2c5iZwJM+sDkiWc35BKuxl/wMSfHac=; b=T0IifDnJcB6dS2q8HLgo8J+g2FjGaM0CJL8b2qxcUnyooks39+QAbc5BBzsgyUnfnF dAYrBHnoPP9tyXN0hKBKua1j90jgZvxhNNzodRQLHeuN3j/geYqhS5m+oeWRx7yaipf3 761HB/h+e/sXQnrFDVj6DrCEWbPd4qPzbFiQmU9qbTyNjCciWBp6Nm14FkhvikkxsrOy dO5Oknxqficn5yh6Y6zszRO1ArRsUaUFdGQONkRQGMKnSGfp6Am84rKy2sEZhkvce8Sv ENXsKvmIBDFrmlsoF1KXdQzSOqgmWTaxap7jLlr/o8LsWq+38BxYYzhU1o/L6RrD+RPb GB6Q== X-Gm-Message-State: AOJu0YyKBHDRhziSn+l6JDzwCIM2SkXkTwtZRUl0MutTv0Q652p4ZJVz 5VYNEo4Ky+Rf0ObwbL9jyXqSRO0jiWSKiM2121o= X-Google-Smtp-Source: AGHT+IGqE9FPvetndnMzBHNgitBPMVRCK0GIGWphjpwGakTNt5adzQgfKtbmCnTDP6JlgVaAn6C4tg== X-Received: by 2002:a17:907:7203:b0:9dd:64ca:dc69 with SMTP id dr3-20020a170907720300b009dd64cadc69mr10494988ejc.1.1699372171370; Tue, 07 Nov 2023 07:49:31 -0800 (PST) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Wei Liu , Anthony PERARD , Juergen Gross Subject: [PATCH 7/7] tools/xg: Simplify hypercall stubs Date: Tue, 7 Nov 2023 15:49:21 +0000 Message-Id: <20231107154921.54979-8-alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231107154921.54979-1-alejandro.vallejo@cloud.com> References: <20231107154921.54979-1-alejandro.vallejo@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1699372205205100013 Content-Type: text/plain; charset="utf-8" Now there are no pending dependencies on the current form of the hypercall stubs. Replace them with simpler forms that only take the xc_cpu_policy object. This way the plumbing logic becomes a lot simpler, allowing the policy to be extended without touching the plumbing code. Signed-off-by: Alejandro Vallejo --- tools/libs/guest/xg_cpuid_x86.c | 59 ++++++++++++--------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x8= 6.c index e2a2659953..cf07a69764 100644 --- a/tools/libs/guest/xg_cpuid_x86.c +++ b/tools/libs/guest/xg_cpuid_x86.c @@ -125,16 +125,17 @@ int xc_cpu_policy_get_size(xc_interface *xch, uint32_= t *nr_leaves, return ret; } =20 -static int get_system_cpu_policy(xc_interface *xch, uint32_t index, - uint32_t *nr_leaves, xen_cpuid_leaf_t *le= aves, - uint32_t *nr_msrs, xen_msr_entry_t *msrs) +static int get_system_cpu_policy_serialised(xc_interface *xch, uint32_t in= dex, + xc_cpu_policy_t *p) { struct xen_sysctl sysctl =3D {}; + xen_cpuid_leaf_t *leaves =3D p->leaves.buf; + xen_msr_entry_t *msrs =3D p->msrs.buf; DECLARE_HYPERCALL_BOUNCE(leaves, - *nr_leaves * sizeof(*leaves), + p->leaves.allocated * sizeof(*leaves), XC_HYPERCALL_BUFFER_BOUNCE_OUT); DECLARE_HYPERCALL_BOUNCE(msrs, - *nr_msrs * sizeof(*msrs), + p->msrs.allocated * sizeof(*msrs), XC_HYPERCALL_BUFFER_BOUNCE_OUT); int ret; =20 @@ -144,9 +145,9 @@ static int get_system_cpu_policy(xc_interface *xch, uin= t32_t index, =20 sysctl.cmd =3D XEN_SYSCTL_get_cpu_policy; sysctl.u.cpu_policy.index =3D index; - sysctl.u.cpu_policy.nr_leaves =3D *nr_leaves; + sysctl.u.cpu_policy.nr_leaves =3D p->leaves.allocated; set_xen_guest_handle(sysctl.u.cpu_policy.leaves, leaves); - sysctl.u.cpu_policy.nr_msrs =3D *nr_msrs; + sysctl.u.cpu_policy.nr_msrs =3D p->msrs.allocated; set_xen_guest_handle(sysctl.u.cpu_policy.msrs, msrs); =20 ret =3D do_sysctl(xch, &sysctl); @@ -156,23 +157,24 @@ static int get_system_cpu_policy(xc_interface *xch, u= int32_t index, =20 if ( !ret ) { - *nr_leaves =3D sysctl.u.cpu_policy.nr_leaves; - *nr_msrs =3D sysctl.u.cpu_policy.nr_msrs; + p->leaves.len =3D sysctl.u.cpu_policy.nr_leaves; + p->msrs.len =3D sysctl.u.cpu_policy.nr_msrs; } =20 return ret; } =20 -static int get_domain_cpu_policy(xc_interface *xch, uint32_t domid, - uint32_t *nr_leaves, xen_cpuid_leaf_t *le= aves, - uint32_t *nr_msrs, xen_msr_entry_t *msrs) +static int get_domain_cpu_policy_serialised(xc_interface *xch, uint32_t do= mid, + xc_cpu_policy_t *p) { DECLARE_DOMCTL; + xen_cpuid_leaf_t *leaves =3D p->leaves.buf; + xen_msr_entry_t *msrs =3D p->msrs.buf; DECLARE_HYPERCALL_BOUNCE(leaves, - *nr_leaves * sizeof(*leaves), + p->leaves.allocated * sizeof(*leaves), XC_HYPERCALL_BUFFER_BOUNCE_OUT); DECLARE_HYPERCALL_BOUNCE(msrs, - *nr_msrs * sizeof(*msrs), + p->msrs.allocated * sizeof(*msrs), XC_HYPERCALL_BUFFER_BOUNCE_OUT); int ret; =20 @@ -182,9 +184,9 @@ static int get_domain_cpu_policy(xc_interface *xch, uin= t32_t domid, =20 domctl.cmd =3D XEN_DOMCTL_get_cpu_policy; domctl.domain =3D domid; - domctl.u.cpu_policy.nr_leaves =3D *nr_leaves; + domctl.u.cpu_policy.nr_leaves =3D p->leaves.allocated; set_xen_guest_handle(domctl.u.cpu_policy.leaves, leaves); - domctl.u.cpu_policy.nr_msrs =3D *nr_msrs; + domctl.u.cpu_policy.nr_msrs =3D p->msrs.allocated; set_xen_guest_handle(domctl.u.cpu_policy.msrs, msrs); =20 ret =3D do_domctl(xch, &domctl); @@ -194,8 +196,8 @@ static int get_domain_cpu_policy(xc_interface *xch, uin= t32_t domid, =20 if ( !ret ) { - *nr_leaves =3D domctl.u.cpu_policy.nr_leaves; - *nr_msrs =3D domctl.u.cpu_policy.nr_msrs; + p->leaves.len =3D domctl.u.cpu_policy.nr_leaves; + p->msrs.len =3D domctl.u.cpu_policy.nr_msrs; } =20 return ret; @@ -745,22 +747,14 @@ void xc_cpu_policy_destroy(xc_cpu_policy_t *policy) int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx, xc_cpu_policy_t *policy) { - unsigned int nr_leaves =3D policy->leaves.allocated; - unsigned int nr_msrs =3D policy->msrs.allocated; int rc; =20 - rc =3D get_system_cpu_policy(xch, policy_idx, - &nr_leaves, policy->leaves.buf, - &nr_msrs, policy->msrs.buf); - if ( rc ) + if ( (rc =3D get_system_cpu_policy_serialised(xch, policy_idx, policy)= ) ) { PERROR("Failed to obtain %u policy", policy_idx); return rc; } =20 - policy->leaves.len =3D nr_leaves; - policy->msrs.len =3D nr_msrs; - rc =3D cpu_policy_deserialise_on_self(xch, policy); if ( rc ) { @@ -774,22 +768,13 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsig= ned int policy_idx, int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid, xc_cpu_policy_t *policy) { - unsigned int nr_leaves =3D policy->leaves.allocated; - unsigned int nr_msrs =3D policy->msrs.allocated; - int rc; - - rc =3D get_domain_cpu_policy(xch, domid, - &nr_leaves, policy->leaves.buf, - &nr_msrs, policy->msrs.buf); + int rc =3D get_domain_cpu_policy_serialised(xch, domid, policy); if ( rc ) { PERROR("Failed to obtain domain %u policy", domid); return rc; } =20 - policy->leaves.len =3D nr_leaves; - policy->msrs.len =3D nr_msrs; - rc =3D cpu_policy_deserialise_on_self(xch, policy); if ( rc ) { --=20 2.34.1