From patchwork Tue Nov 7 15:49:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Vallejo X-Patchwork-Id: 13449071 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 27BB3C00141 for ; Tue, 7 Nov 2023 15:49:43 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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_Pau_Monn=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 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; /* 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); /* 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_x86.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; } -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 = calloc(1, sizeof(*p)); + if ( !p ) + return NULL; + + if ( xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs) ) + goto fail; + + p->leaves.allocated = max(nr_leaves, CPUID_MAX_SERIALISED_LEAVES); + p->leaves.buf = calloc(p->leaves.allocated, sizeof(*p->leaves.buf)); + if ( !p->leaves.buf ) + goto fail; + + p->msrs.allocated = max(nr_msrs, MSR_MAX_SERIALISED_ENTRIES); + p->msrs.buf = 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; } void xc_cpu_policy_destroy(xc_cpu_policy_t *policy) { - if ( policy ) - free(policy); + int err = errno; + + if ( !policy ) + return; + + if ( policy->leaves.buf ) + free(policy->leaves.buf); + if ( policy->msrs.buf ) + free(policy->msrs.buf); + + free(policy); + errno = err; } 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 = -1, err_subleaf = -1, err_msr = -1; int rc; - rc = x86_cpuid_copy_from_buffer(&policy->policy, policy->leaves, + rc = 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; } - rc = x86_msr_copy_from_buffer(&policy->policy, policy->msrs, + rc = 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_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 = ARRAY_SIZE(policy->leaves); - unsigned int nr_msrs = ARRAY_SIZE(policy->msrs); + unsigned int nr_leaves = policy->leaves.allocated; + unsigned int nr_msrs = policy->msrs.allocated; int rc; - rc = get_system_cpu_policy(xch, policy_idx, &nr_leaves, policy->leaves, - &nr_msrs, policy->msrs); + rc = 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; } + policy->leaves.len = nr_leaves; + policy->msrs.len = nr_msrs; + rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs); if ( rc ) { @@ -907,18 +944,22 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx, int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid, xc_cpu_policy_t *policy) { - unsigned int nr_leaves = ARRAY_SIZE(policy->leaves); - unsigned int nr_msrs = ARRAY_SIZE(policy->msrs); + unsigned int nr_leaves = policy->leaves.allocated; + unsigned int nr_msrs = policy->msrs.allocated; int rc; - rc = get_domain_cpu_policy(xch, domid, &nr_leaves, policy->leaves, - &nr_msrs, policy->msrs); + rc = 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; } + policy->leaves.len = nr_leaves; + policy->msrs.len = nr_msrs; + rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs); if ( rc ) { @@ -933,17 +974,22 @@ int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t domid, xc_cpu_policy_t *policy) { uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1; - unsigned int nr_leaves = ARRAY_SIZE(policy->leaves); - unsigned int nr_msrs = ARRAY_SIZE(policy->msrs); + unsigned int nr_leaves = policy->leaves.allocated; + unsigned int nr_msrs = policy->msrs.allocated; int rc; - rc = xc_cpu_policy_serialise(xch, policy, policy->leaves, &nr_leaves, - policy->msrs, &nr_msrs); + rc = xc_cpu_policy_serialise(xch, policy, + policy->leaves.buf, &nr_leaves, + policy->msrs.buf, &nr_msrs); if ( rc ) return rc; - rc = xc_set_domain_cpu_policy(xch, domid, nr_leaves, policy->leaves, - nr_msrs, policy->msrs, + policy->leaves.len = nr_leaves; + policy->msrs.len = nr_msrs; + + rc = 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, unsigned long mfn, #include 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 */ diff --git a/tools/libs/guest/xg_sr_common_x86.c b/tools/libs/guest/xg_sr_common_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 *ctx) cpuid.data = malloc(nr_leaves * sizeof(xen_cpuid_leaf_t)); msrs.data = malloc(nr_msrs * sizeof(xen_msr_entry_t)); - policy = xc_cpu_policy_init(); + policy = 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; xc_interface *xch = xc_interface_open(0, 0, 0); - xc_cpu_policy_t *policy = xc_cpu_policy_init(); + xc_cpu_policy_t *policy = xc_cpu_policy_init(xch); 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); 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); 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); } /* @@ -281,7 +281,7 @@ static void test_rtm_behaviour(void) else return fail(" Got unexpected behaviour %d\n", rtm_behaviour); - if ( host.policy.feat.rtm ) + if ( host->policy.feat.rtm ) { if ( rtm_behaviour == 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); } 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); } } @@ -369,7 +369,7 @@ static void test_guest(struct xen_domctl_createdomain *c) printf(" Created d%u\n", domid); - rc = xc_cpu_policy_get_domain(xch, domid, &guest_policy); + rc = 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; } - dump_tsx_details(&guest_policy.policy, "Cur:"); + dump_tsx_details(&guest_policy->policy, "Cur:"); /* * Check defaults given to the guest. */ - if ( guest_policy.policy.feat.rtm != (rtm_behaviour == RTM_OK) ) + if ( guest_policy->policy.feat.rtm != (rtm_behaviour == RTM_OK) ) fail(" RTM %u in guest, despite rtm behaviour\n", - guest_policy.policy.feat.rtm); + guest_policy->policy.feat.rtm); - 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"); - if ( host.policy.feat.rtm ) + if ( host->policy.feat.rtm ) { unsigned int _7b0; @@ -401,11 +401,11 @@ static void test_guest(struct xen_domctl_createdomain *c) * If host RTM is available, all combinations of guest flags should be * possible. Flip both HLE/RTM to check non-default settings. */ - _7b0 = (guest_policy.policy.feat.raw[0].b ^= + _7b0 = (guest_policy->policy.feat.raw[0].b ^= (bitmaskof(X86_FEATURE_HLE) | bitmaskof(X86_FEATURE_RTM))); /* Set the new policy. */ - rc = xc_cpu_policy_set_domain(xch, domid, &guest_policy); + rc = 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) } /* Re-get the new policy. */ - rc = xc_cpu_policy_get_domain(xch, domid, &guest_policy); + rc = 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; } - dump_tsx_details(&guest_policy.policy, "Cur:"); + dump_tsx_details(&guest_policy->policy, "Cur:"); - if ( guest_policy.policy.feat.raw[0].b != _7b0 ) + if ( guest_policy->policy.feat.raw[0].b != _7b0 ) { fail(" Expected CPUID.7[1].b 0x%08x differs from actual 0x%08x\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; - /* Read all policies except raw. */ - for ( unsigned int i = XEN_SYSCTL_cpu_policy_host; - i <= XEN_SYSCTL_cpu_policy_hvm_default; ++i ) + for ( unsigned int i = 0; i < ARRAY_SIZE(policies); ++i ) { - rc = xc_cpu_policy_get_system(xch, i, &policies[i]); + policies[i] = 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 == XEN_SYSCTL_cpu_policy_raw ) + continue; + + rc = xc_cpu_policy_get_system(xch, i, policies[i]); if ( rc == -1 && errno == 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 */ ) /* Maximum number of MSRs written when serialising a cpu_policy. */ -#define MSR_MAX_SERIALISED_ENTRIES 2 +#define MSR_MAX_SERIALISED_ENTRIES 2U struct cpu_policy { From patchwork Tue Nov 7 15:49:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Vallejo X-Patchwork-Id: 13449067 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 27BEAC41535 for ; Tue, 7 Nov 2023 15:49:43 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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 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_common_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, struct xc_sr_record *rec) int write_x86_cpu_policy_records(struct xc_sr_context *ctx) { + int rc = -1; xc_interface *xch = ctx->xch; - struct xc_sr_record cpuid = { .type = REC_TYPE_X86_CPUID_POLICY, }; - struct xc_sr_record msrs = { .type = REC_TYPE_X86_MSR_POLICY, }; - uint32_t nr_leaves = 0, nr_msrs = 0; - xc_cpu_policy_t *policy = 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 = xc_cpu_policy_init(xch); - cpuid.data = malloc(nr_leaves * sizeof(xen_cpuid_leaf_t)); - msrs.data = malloc(nr_msrs * sizeof(xen_msr_entry_t)); - policy = xc_cpu_policy_init(xch); - if ( !cpuid.data || !msrs.data || !policy ) - { - ERROR("Cannot allocate memory for CPU Policy"); - rc = -1; - goto out; - } - - if ( xc_cpu_policy_get_domain(xch, ctx->domid, policy) ) + if ( !policy || + (rc = xc_cpu_policy_get_domain(xch, ctx->domid, policy)) ) { PERROR("Unable to get d%d CPU Policy", ctx->domid); - rc = -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 = -1; goto out; } - cpuid.length = nr_leaves * sizeof(xen_cpuid_leaf_t); - if ( cpuid.length ) + record = (struct xc_sr_record){ + .type = REC_TYPE_X86_CPUID_POLICY, .data = policy->leaves.buf, + .length = policy->leaves.len * sizeof(*policy->leaves.buf), + }; + if ( record.length ) { - rc = write_record(ctx, &cpuid); + rc = write_record(ctx, &record); if ( rc ) goto out; } - msrs.length = nr_msrs * sizeof(xen_msr_entry_t); - if ( msrs.length ) + record = (struct xc_sr_record){ + .type = REC_TYPE_X86_MSR_POLICY, .data = policy->msrs.buf, + .length = policy->msrs.len * sizeof(*policy->msrs.buf), + }; + if ( record.length ) { - rc = write_record(ctx, &msrs); + rc = write_record(ctx, &record); if ( rc ) goto out; } @@ -100,8 +81,6 @@ int write_x86_cpu_policy_records(struct xc_sr_context *ctx) rc = 0; out: - free(cpuid.data); - free(msrs.data); xc_cpu_policy_destroy(policy); return rc; From patchwork Tue Nov 7 15:49:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Vallejo X-Patchwork-Id: 13449069 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E530DC0018C for ; Tue, 7 Nov 2023 15:49:43 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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 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_x86.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; } +static int cpu_policy_deserialise_on_self(xc_interface *xch, xc_cpu_policy_t *p) +{ + uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1; + int rc; + + rc = x86_cpuid_copy_from_buffer(&p->policy, p->leaves.buf, p->leaves.len, + &err_leaf, &err_subleaf); + if ( rc ) + { + if ( err_leaf != -1 ) + ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d = %s)", + err_leaf, err_subleaf, -rc, strerror(-rc)); + return rc; + } + + rc = x86_msr_copy_from_buffer(&p->policy, p->msrs.buf, p->msrs.len, &err_msr); + if ( rc ) + { + if ( err_msr != -1 ) + ERROR("Failed to deserialise MSR (err MSR %#x) (%d = %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 = p->leaves.allocated; + uint32_t nr_msrs = p->msrs.allocated; + int rc = xc_cpu_policy_serialise(xch, p, + p->leaves.buf, &nr_leaves, + p->msrs.buf, &nr_msrs); + + if ( !rc ) + { + p->leaves.len = nr_leaves; + p->msrs.len = nr_msrs; + } + + return rc; +} + static int compare_leaves(const void *l, const void *r) { const xen_cpuid_leaf_t *lhs = l; @@ -883,35 +927,6 @@ void xc_cpu_policy_destroy(xc_cpu_policy_t *policy) errno = err; } -static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy, - unsigned int nr_leaves, unsigned int nr_entries) -{ - uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1; - int rc; - - rc = x86_cpuid_copy_from_buffer(&policy->policy, policy->leaves.buf, - nr_leaves, &err_leaf, &err_subleaf); - if ( rc ) - { - if ( err_leaf != -1 ) - ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d = %s)", - err_leaf, err_subleaf, -rc, strerror(-rc)); - return rc; - } - - rc = x86_msr_copy_from_buffer(&policy->policy, policy->msrs.buf, - nr_entries, &err_msr); - if ( rc ) - { - if ( err_msr != -1 ) - ERROR("Failed to deserialise MSR (err MSR %#x) (%d = %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, unsigned int policy_idx, policy->leaves.len = nr_leaves; policy->msrs.len = nr_msrs; - rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs); + rc = cpu_policy_deserialise_on_self(xch, policy); if ( rc ) { errno = -rc; @@ -960,7 +975,7 @@ int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid, policy->leaves.len = nr_leaves; policy->msrs.len = nr_msrs; - rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs); + rc = cpu_policy_deserialise_on_self(xch, policy); if ( rc ) { errno = -rc; @@ -974,22 +989,15 @@ int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t domid, xc_cpu_policy_t *policy) { uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1; - unsigned int nr_leaves = policy->leaves.allocated; - unsigned int nr_msrs = policy->msrs.allocated; int rc; - rc = xc_cpu_policy_serialise(xch, policy, - policy->leaves.buf, &nr_leaves, - policy->msrs.buf, &nr_msrs); + rc = cpu_policy_serialise_on_self(xch, policy); if ( rc ) return rc; - policy->leaves.len = nr_leaves; - policy->msrs.len = nr_msrs; - rc = 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 ) { From patchwork Tue Nov 7 15:49:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Vallejo X-Patchwork-Id: 13449068 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8A768C4167B for ; Tue, 7 Nov 2023 15:49:43 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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 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, unsigned 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); /* Manipulate a policy via architectural representations. */ int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t *policy, diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.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, uint32_t domid, return rc; } -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 = -1, err_subleaf = -1, err_msr = -1; int rc; - - rc = cpu_policy_serialise_on_self(xch, policy); - if ( rc ) + if ( (rc = cpu_policy_serialise_on_self(xch, policy)) ) return rc; - rc = 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 domid, + xc_cpu_policy_t *policy) +{ + uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1; + int rc = 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); if ( rc ) { ERROR("Failed to set domain %u policy (%d = %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))); /* Set the new policy. */ - rc = xc_cpu_policy_set_domain(xch, domid, guest_policy); + rc = xc_cpu_policy_set_domain_from_deserialised(xch, domid, guest_policy); if ( rc ) { fail(" Failed to set domain policy: %d - %s\n", From patchwork Tue Nov 7 15:49:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Vallejo X-Patchwork-Id: 13449072 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E56AAC4332F for ; Tue, 7 Nov 2023 15:49:45 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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 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; /* 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); /* 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_x86.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 = NULL; - struct cpu_policy *p = NULL; - uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1; - uint32_t host_featureset[FEATURESET_NR_ENTRIES] = {}; - uint32_t len = 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 = NULL, *def = NULL, *cur = NULL; + /* Determine if domid is PV or HVM */ if ( (rc = 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 = di.flags & XEN_DOMINF_hvm_guest; - rc = xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs); - if ( rc ) - { - PERROR("Failed to obtain policy info size"); - rc = -errno; - goto out; - } - - rc = -ENOMEM; - if ( (leaves = calloc(nr_leaves, sizeof(*leaves))) == NULL || - (p = calloc(1, sizeof(*p))) == NULL ) - goto out; - - /* Get the host policy. */ - rc = 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 != ENOBUFS ) + if ( !(host = xc_cpu_policy_init(xch)) || + !(def = xc_cpu_policy_clone(host)) || + !(cur = xc_cpu_policy_clone(host)) ) { - PERROR("Failed to obtain host featureset"); - rc = -errno; + PERROR("Failed to allocate policy state"); goto out; } - /* Get the domain's default policy. */ - nr_msrs = 0; - rc = get_system_cpu_policy(xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default - : 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 = -errno; - goto out; - } + def_policy = hvm ? XEN_SYSCTL_cpu_policy_hvm_default + : XEN_SYSCTL_cpu_policy_pv_default; - rc = x86_cpuid_copy_from_buffer(p, leaves, nr_leaves, - &err_leaf, &err_subleaf); - if ( rc ) + /* Get the domain type's default policy. */ + if ( (rc = xc_cpu_policy_get_domain(xch, domid, cur)) || + (rc = xc_cpu_policy_get_system(xch, def_policy, def)) || + (rc = xc_cpu_policy_get_system(xch, XEN_SYSCTL_cpu_policy_host, host)) ) { - ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d = %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; } @@ -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 default. */ - p->basic.rdrand = test_bit(X86_FEATURE_RDRAND, host_featureset); - p->feat.hle = test_bit(X86_FEATURE_HLE, host_featureset); - p->feat.rtm = test_bit(X86_FEATURE_RTM, host_featureset); + cur->policy.basic.rdrand = host->policy.basic.rdrand; + cur->policy.feat.hle = host->policy.feat.hle; + cur->policy.feat.rtm = host->policy.feat.rtm; if ( hvm ) - { - p->feat.mpx = test_bit(X86_FEATURE_MPX, host_featureset); - } + cur->policy.feat.mpx = host->policy.feat.mpx; - p->basic.max_leaf = min(p->basic.max_leaf, 0xdu); - p->feat.max_subleaf = 0; - p->extd.max_leaf = min(p->extd.max_leaf, 0x8000001c); + cur->policy.basic.max_leaf = min(host->policy.basic.max_leaf, 0xdu); + cur->policy.feat.max_subleaf = 0; + cur->policy.extd.max_leaf = min(host->policy.extd.max_leaf, 0x8000001c); } if ( featureset ) @@ -730,7 +705,6 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, uint32_t disabled_features[FEATURESET_NR_ENTRIES], feat[FEATURESET_NR_ENTRIES] = {}; static const uint32_t deep_features[] = INIT_DEEP_FEATURES; - unsigned int i, b; /* * 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, /* Check for truncated set bits. */ rc = -EOPNOTSUPP; - for ( i = user_len; i < nr_features; ++i ) + for ( size_t i = user_len; i < nr_features; ++i ) if ( featureset[i] != 0 ) goto out; memcpy(feat, featureset, sizeof(*featureset) * user_len); /* Disable deep dependencies of disabled features. */ - for ( i = 0; i < ARRAY_SIZE(disabled_features); ++i ) + for ( size_t i = 0; i < ARRAY_SIZE(disabled_features); ++i ) disabled_features[i] = ~feat[i] & deep_features[i]; - for ( b = 0; b < sizeof(disabled_features) * CHAR_BIT; ++b ) + for ( size_t b = 0; b < sizeof(disabled_features) * CHAR_BIT; ++b ) { const uint32_t *dfs; @@ -759,24 +733,24 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, !(dfs = x86_cpu_policy_lookup_deep_deps(b)) ) continue; - for ( i = 0; i < ARRAY_SIZE(disabled_features); ++i ) + for ( size_t i = 0; i < ARRAY_SIZE(disabled_features); ++i ) { feat[i] &= ~dfs[i]; disabled_features[i] &= ~dfs[i]; } } - x86_cpu_featureset_to_policy(feat, p); + x86_cpu_featureset_to_policy(feat, &cur->policy); } else { - p->extd.itsc = itsc; + cur->policy.extd.itsc = itsc; if ( hvm ) { - p->basic.pae = pae; - p->basic.vmx = nested_virt; - p->extd.svm = nested_virt; + cur->policy.basic.pae = pae; + cur->policy.basic.vmx = nested_virt; + cur->policy.extd.svm = nested_virt; } } @@ -786,8 +760,8 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, 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 = test_bit(X86_FEATURE_HTT, host_featureset); - p->extd.cmp_legacy = test_bit(X86_FEATURE_CMP_LEGACY, host_featureset); + cur->policy.basic.htt = host->policy.basic.htt; + cur->policy.extd.cmp_legacy = 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 = vcpu_id * 2 to give the illusion of no SMT. */ - p->basic.htt = true; - p->extd.cmp_legacy = false; + cur->policy.basic.htt = true; + cur->policy.extd.cmp_legacy = false; /* * Leaf 1 EBX[23:16] is Maximum Logical Processors Per Package. * Update to reflect vLAPIC_ID = vCPU_ID * 2, but make sure to avoid * overflow. */ - if ( !p->basic.lppp ) - p->basic.lppp = 2; - else if ( !(p->basic.lppp & 0x80) ) - p->basic.lppp *= 2; + if ( !cur->policy.basic.lppp ) + cur->policy.basic.lppp = 2; + else if ( !(cur->policy.basic.lppp & 0x80) ) + cur->policy.basic.lppp *= 2; - switch ( p->x86_vendor ) + switch ( cur->policy.x86_vendor ) { case X86_VENDOR_INTEL: - for ( i = 0; (p->cache.subleaf[i].type && - i < ARRAY_SIZE(p->cache.raw)); ++i ) + for ( size_t i = 0; (cur->policy.cache.subleaf[i].type && + i < ARRAY_SIZE(cur->policy.cache.raw)); ++i ) { - p->cache.subleaf[i].cores_per_package = - (p->cache.subleaf[i].cores_per_package << 1) | 1; - p->cache.subleaf[i].threads_per_cache = 0; + cur->policy.cache.subleaf[i].cores_per_package = + (cur->policy.cache.subleaf[i].cores_per_package << 1) | 1; + cur->policy.cache.subleaf[i].threads_per_cache = 0; } break; @@ -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 != 0 && p->extd.apic_id_size < 0x7 ) - p->extd.apic_id_size++; + if ( cur->policy.extd.apic_id_size != 0 && + cur->policy.extd.apic_id_size < 0x7 ) + cur->policy.extd.apic_id_size++; - p->extd.nc = (p->extd.nc << 1) | 1; + cur->policy.extd.nc = (cur->policy.extd.nc << 1) | 1; } break; } } - rc = x86_cpuid_copy_to_buffer(p, leaves, &nr_leaves); - if ( rc ) - { - ERROR("Failed to serialise CPUID (%d = %s)", -rc, strerror(-rc)); - goto out; - } - - rc = 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 = 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 = -errno; goto out; } @@ -877,8 +843,9 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, rc = 0; out: - free(p); - free(leaves); + xc_cpu_policy_destroy(cur); + xc_cpu_policy_destroy(def); + xc_cpu_policy_destroy(host); return rc; } @@ -911,6 +878,39 @@ xc_cpu_policy_t *xc_cpu_policy_init(xc_interface *xch) return NULL; } +xc_cpu_policy_t *xc_cpu_policy_clone(const xc_cpu_policy_t *other) +{ + xc_cpu_policy_t *policy; + + if ( !other ) + return NULL; + + policy = malloc(sizeof(*policy)); + if ( !policy ) + return NULL; + + *policy = *other; + + /* Override every buffer with identical new ones */ + policy->leaves.buf = calloc(other->leaves.allocated, + sizeof(*other->leaves.buf)); + policy->msrs.buf = 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 = errno; From patchwork Tue Nov 7 15:49:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Vallejo X-Patchwork-Id: 13449070 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7BA88C4167D for ; Tue, 7 Nov 2023 15:49:44 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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 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_x86.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; } -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 = { xend->leaf, xend->subleaf }; - return bsearch(&key, leaves, nr_leaves, sizeof(*leaves), compare_leaves); + return bsearch(&key, p->leaves.buf, p->leaves.len, + sizeof(*p->leaves.buf), compare_leaves); } 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 = -1, err_subleaf = -1, err_msr = -1; - /* - * Three full policies. The host, default for the domain type, - * and domain current. - */ - xen_cpuid_leaf_t *host = NULL, *def = NULL, *cur = NULL; - unsigned int nr_host, nr_def, nr_cur; - - if ( (rc = xc_domain_getinfo_single(xch, domid, &di)) < 0 ) - { - PERROR("Failed to obtain d%d info", domid); - rc = -errno; - goto fail; - } - hvm = di.flags & XEN_DOMINF_hvm_guest; - - rc = xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs); - if ( rc ) - { - PERROR("Failed to obtain policy info size"); - rc = -errno; - goto fail; - } - - rc = -ENOMEM; - if ( (host = calloc(nr_leaves, sizeof(*host))) == NULL || - (def = calloc(nr_leaves, sizeof(*def))) == NULL || - (cur = calloc(nr_leaves, sizeof(*cur))) == NULL ) - { - ERROR("Unable to allocate memory for %u CPUID leaves", nr_leaves); - goto fail; - } - - /* Get the domain's current policy. */ - nr_msrs = 0; - nr_cur = nr_leaves; - rc = get_domain_cpu_policy(xch, domid, &nr_cur, cur, &nr_msrs, NULL); - if ( rc ) - { - PERROR("Failed to obtain d%d current policy", domid); - rc = -errno; - goto fail; - } - - /* Get the domain type's default policy. */ - nr_msrs = 0; - nr_def = nr_leaves; - rc = get_system_cpu_policy(xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default - : 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 = -errno; - goto fail; - } - - /* Get the host policy. */ - nr_msrs = 0; - nr_host = nr_leaves; - rc = 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 = -errno; - goto fail; - } - - rc = -EINVAL; for ( ; xend->leaf != XEN_CPUID_INPUT_UNUSED; ++xend ) { - xen_cpuid_leaf_t *cur_leaf = find_leaf(cur, nr_cur, xend); - const xen_cpuid_leaf_t *def_leaf = find_leaf(def, nr_def, xend); - const xen_cpuid_leaf_t *host_leaf = find_leaf(host, nr_host, xend); + xen_cpuid_leaf_t *cur_leaf = find_leaf(cur, xend); + const xen_cpuid_leaf_t *def_leaf = find_leaf(def, xend); + const xen_cpuid_leaf_t *host_leaf = find_leaf(host, xend); if ( cur_leaf == NULL || def_leaf == NULL || host_leaf == NULL ) { ERROR("Missing leaf %#x, subleaf %#x", xend->leaf, xend->subleaf); - goto fail; + return -EINVAL; } for ( unsigned int i = 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; } clear_bit(31 - j, cur_reg); @@ -446,25 +376,7 @@ static int xc_cpuid_xend_policy( } } - /* Feed the transformed currrent policy back up to Xen. */ - rc = 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 = -errno; - goto fail; - } - - /* Success! */ - - fail: - free(cur); - free(def); - free(host); - - return rc; + return 0; } 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; } -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 = { .idx = index }; - 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); } - 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 = -1, err_subleaf = -1, err_msr = -1; - /* - * Three full policies. The host, default for the domain type, - * and domain current. - */ - xen_msr_entry_t *host = NULL, *def = NULL, *cur = NULL; - unsigned int nr_host, nr_def, nr_cur; - - if ( (rc = xc_domain_getinfo_single(xch, domid, &di)) < 0 ) - { - PERROR("Failed to obtain d%d info", domid); - rc = -errno; - goto out; - } - hvm = di.flags & XEN_DOMINF_hvm_guest; - - rc = xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs); - if ( rc ) - { - PERROR("Failed to obtain policy info size"); - rc = -errno; - goto out; - } - - if ( (host = calloc(nr_msrs, sizeof(*host))) == NULL || - (def = calloc(nr_msrs, sizeof(*def))) == NULL || - (cur = calloc(nr_msrs, sizeof(*cur))) == NULL ) - { - ERROR("Unable to allocate memory for %u CPUID leaves", nr_leaves); - rc = -ENOMEM; - goto out; - } - - /* Get the domain's current policy. */ - nr_leaves = 0; - nr_cur = nr_msrs; - rc = get_domain_cpu_policy(xch, domid, &nr_leaves, NULL, &nr_cur, cur); - if ( rc ) - { - PERROR("Failed to obtain d%d current policy", domid); - rc = -errno; - goto out; - } - - /* Get the domain type's default policy. */ - nr_leaves = 0; - nr_def = nr_msrs; - rc = get_system_cpu_policy(xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default - : 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 = -errno; - goto out; - } - - /* Get the host policy. */ - nr_leaves = 0; - nr_host = nr_msrs; - rc = 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 = -errno; - goto out; - } - for ( ; msr->index != XC_MSR_INPUT_UNUSED; ++msr ) { - xen_msr_entry_t *cur_msr = find_msr(cur, nr_cur, msr->index); - const xen_msr_entry_t *def_msr = find_msr(def, nr_def, msr->index); - const xen_msr_entry_t *host_msr = find_msr(host, nr_host, msr->index); + xen_msr_entry_t *cur_msr = find_msr(cur, msr->index); + const xen_msr_entry_t *def_msr = find_msr(def, msr->index); + const xen_msr_entry_t *host_msr = find_msr(host, msr->index); unsigned int i; if ( cur_msr == NULL || def_msr == NULL || host_msr == NULL ) { ERROR("Missing MSR %#x", msr->index); - rc = -ENOENT; - goto out; + return -ENOENT; } for ( i = 0; i < ARRAY_SIZE(msr->policy) - 1; i++ ) @@ -594,8 +433,7 @@ static int xc_msr_policy(xc_interface *xch, domid_t domid, { ERROR("MSR index %#x: bad character '%c' in policy string '%s'", msr->index, msr->policy[i], msr->policy); - rc = -EINVAL; - goto out; + return -EINVAL; } if ( val ) @@ -605,25 +443,7 @@ static int xc_msr_policy(xc_interface *xch, domid_t domid, } } - /* Feed the transformed policy back up to Xen. */ - rc = 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 = -errno; - goto out; - } - - /* Success! */ - - out: - free(cur); - free(def); - free(host); - - return rc; + return 0; } 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, } } - /* Re-apply the mutated policy onto the domain */ - rc = xc_cpu_policy_set_domain(xch, domid, cur); - if ( rc ) - { - rc = -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 overrides + */ + if ( (rc = cpu_policy_serialise_on_self(xch, cur)) ) goto out; - } - if ( xend && (rc = xc_cpuid_xend_policy(xch, domid, xend)) ) + /* Apply xend-style overrides */ + if ( (xend && (rc = xc_cpuid_xend_policy(xch, domid, xend, host, def, cur))) || + (msr && (rc = xc_msr_policy(xch, domid, msr, host, def, cur))) ) goto out; - if ( msr ) - { - rc = xc_msr_policy(xch, domid, msr); - if ( rc ) - goto out; - } - - rc = 0; + /* Re-apply the mutated policy onto the domain */ + rc = xc_cpu_policy_set_domain_from_serialised(xch, domid, cur); out: xc_cpu_policy_destroy(cur); From patchwork Tue Nov 7 15:49:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Vallejo X-Patchwork-Id: 13449073 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0C798C4167B for ; Tue, 7 Nov 2023 15:49:48 +0000 (UTC) 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 X-Outflank-Mailman: Message body and most headers restored to incoming version 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-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 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_x86.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; } -static int get_system_cpu_policy(xc_interface *xch, uint32_t index, - uint32_t *nr_leaves, xen_cpuid_leaf_t *leaves, - uint32_t *nr_msrs, xen_msr_entry_t *msrs) +static int get_system_cpu_policy_serialised(xc_interface *xch, uint32_t index, + xc_cpu_policy_t *p) { struct xen_sysctl sysctl = {}; + xen_cpuid_leaf_t *leaves = p->leaves.buf; + xen_msr_entry_t *msrs = 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; @@ -144,9 +145,9 @@ static int get_system_cpu_policy(xc_interface *xch, uint32_t index, sysctl.cmd = XEN_SYSCTL_get_cpu_policy; sysctl.u.cpu_policy.index = index; - sysctl.u.cpu_policy.nr_leaves = *nr_leaves; + sysctl.u.cpu_policy.nr_leaves = p->leaves.allocated; set_xen_guest_handle(sysctl.u.cpu_policy.leaves, leaves); - sysctl.u.cpu_policy.nr_msrs = *nr_msrs; + sysctl.u.cpu_policy.nr_msrs = p->msrs.allocated; set_xen_guest_handle(sysctl.u.cpu_policy.msrs, msrs); ret = do_sysctl(xch, &sysctl); @@ -156,23 +157,24 @@ static int get_system_cpu_policy(xc_interface *xch, uint32_t index, if ( !ret ) { - *nr_leaves = sysctl.u.cpu_policy.nr_leaves; - *nr_msrs = sysctl.u.cpu_policy.nr_msrs; + p->leaves.len = sysctl.u.cpu_policy.nr_leaves; + p->msrs.len = sysctl.u.cpu_policy.nr_msrs; } return ret; } -static int get_domain_cpu_policy(xc_interface *xch, uint32_t domid, - uint32_t *nr_leaves, xen_cpuid_leaf_t *leaves, - uint32_t *nr_msrs, xen_msr_entry_t *msrs) +static int get_domain_cpu_policy_serialised(xc_interface *xch, uint32_t domid, + xc_cpu_policy_t *p) { DECLARE_DOMCTL; + xen_cpuid_leaf_t *leaves = p->leaves.buf; + xen_msr_entry_t *msrs = 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; @@ -182,9 +184,9 @@ static int get_domain_cpu_policy(xc_interface *xch, uint32_t domid, domctl.cmd = XEN_DOMCTL_get_cpu_policy; domctl.domain = domid; - domctl.u.cpu_policy.nr_leaves = *nr_leaves; + domctl.u.cpu_policy.nr_leaves = p->leaves.allocated; set_xen_guest_handle(domctl.u.cpu_policy.leaves, leaves); - domctl.u.cpu_policy.nr_msrs = *nr_msrs; + domctl.u.cpu_policy.nr_msrs = p->msrs.allocated; set_xen_guest_handle(domctl.u.cpu_policy.msrs, msrs); ret = do_domctl(xch, &domctl); @@ -194,8 +196,8 @@ static int get_domain_cpu_policy(xc_interface *xch, uint32_t domid, if ( !ret ) { - *nr_leaves = domctl.u.cpu_policy.nr_leaves; - *nr_msrs = domctl.u.cpu_policy.nr_msrs; + p->leaves.len = domctl.u.cpu_policy.nr_leaves; + p->msrs.len = domctl.u.cpu_policy.nr_msrs; } 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 = policy->leaves.allocated; - unsigned int nr_msrs = policy->msrs.allocated; int rc; - rc = get_system_cpu_policy(xch, policy_idx, - &nr_leaves, policy->leaves.buf, - &nr_msrs, policy->msrs.buf); - if ( rc ) + if ( (rc = get_system_cpu_policy_serialised(xch, policy_idx, policy)) ) { PERROR("Failed to obtain %u policy", policy_idx); return rc; } - policy->leaves.len = nr_leaves; - policy->msrs.len = nr_msrs; - rc = cpu_policy_deserialise_on_self(xch, policy); if ( rc ) { @@ -774,22 +768,13 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx, int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid, xc_cpu_policy_t *policy) { - unsigned int nr_leaves = policy->leaves.allocated; - unsigned int nr_msrs = policy->msrs.allocated; - int rc; - - rc = get_domain_cpu_policy(xch, domid, - &nr_leaves, policy->leaves.buf, - &nr_msrs, policy->msrs.buf); + int rc = get_domain_cpu_policy_serialised(xch, domid, policy); if ( rc ) { PERROR("Failed to obtain domain %u policy", domid); return rc; } - policy->leaves.len = nr_leaves; - policy->msrs.len = nr_msrs; - rc = cpu_policy_deserialise_on_self(xch, policy); if ( rc ) {