From nobody Mon Feb 9 20:32:48 2026 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1704898643; cv=none; d=zohomail.com; s=zohoarc; b=mHM85jDF7pfvzaA1louF7YmsPEKSZ1bybnhUbv+E3rYfQZX5cz3X5oC/b9jMPZU8w7xAHze4QBzAd9Cn0AVik23j2hemaQxkoW/uWnmCYOr9zZjO4JWHQr3FuKNCusuBhJHN6f37sLTkmDluoDYCXb9qht8H/LBtWmeZ0MRiw4o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1704898643; h=Content-Type: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=C+Ns9F9oXu8JWmZgiuS6JpNqxTmui1PVJh0bmPBcMr0=; b=j8GFUaxAzu/BVk8u3qH2E+iKfuoTZcx1s1n3cM0Ju1uAHCD2xVTu9BEBZbKcPhbfUH+NwKm7IKooJwzVyKyvJ41wY1VZPlwJa3f4NbnB59G9zp/uHvwpQs6XJDxX2DNGDu4UsR9+QjrM+01YbFne7qNLSUri29+zPFhK4nZ5kLI= 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1704898643695340.233555505896; Wed, 10 Jan 2024 06:57:23 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.665543.1035752 (Exim 4.92) (envelope-from ) id 1rNa11-0006cw-IE; Wed, 10 Jan 2024 14:57:11 +0000 Received: by outflank-mailman (output) from mailman id 665543.1035752; Wed, 10 Jan 2024 14:57:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rNa11-0006cp-Fh; Wed, 10 Jan 2024 14:57:11 +0000 Received: by outflank-mailman (input) for mailman id 665543; Wed, 10 Jan 2024 14:57:09 +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 1rNa0z-0006ap-Ju for xen-devel@lists.xenproject.org; Wed, 10 Jan 2024 14:57:09 +0000 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [2a00:1450:4864:20::32f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 8634b0bf-afc8-11ee-9b0f-b553b5be7939; Wed, 10 Jan 2024 15:57:07 +0100 (CET) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-40e5a5b0fb5so1485455e9.3 for ; Wed, 10 Jan 2024 06:57:07 -0800 (PST) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id bg3-20020a05600c3c8300b0040d91fa270fsm2449640wmb.36.2024.01.10.06.57.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 10 Jan 2024 06:57:06 -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: 8634b0bf-afc8-11ee-9b0f-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1704898627; x=1705503427; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=C+Ns9F9oXu8JWmZgiuS6JpNqxTmui1PVJh0bmPBcMr0=; b=Mx6pf1HDgvmNX6mWNkvuFQ8aN7pqz73N+zVNPXhiNQTd/KTB6YWbJec8Ha1iqnPDy7 yKznLNnp16aqkQhRJ/uXF/8vYyhUV6znbPRRQhGlvIocHniveWpoWhnIi133FCGRxcSZ HhU8tAL9jDdiJtA5lRIl4lEamfTEK5PqNeI765EQ9ugPe6NrTGxuIBFjn219erH/ZghG NA/LyVWrxlF0lX2WsOmG26orEfAuPHGKz8AFQMDVVEmoUf3ew2bX5uM9fSk5jXGPxxL8 qffRWxdJPPPVz5v124TnjciWVEXWzcZrjlbhpHP2s9ICmuS1Ud9W7A2fUeJL/PvwtI1Z bcbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704898627; x=1705503427; h=content-transfer-encoding:in-reply-to:autocrypt:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=C+Ns9F9oXu8JWmZgiuS6JpNqxTmui1PVJh0bmPBcMr0=; b=SIcWCmHj6aIYLycF2YDIIdLlR0mq+5L2O6LQmWOItDSQsJCI5pPcFRxMv5bGbmsb+j 3oGLnCSHyKYP7WdgbGuZwR264GZ1/cWjS91iD8M8L7bAurtqUBHb8tU7dqonk5saQwDX qO5IGG0/MNpbnFGVp8s+EwQyIfh69Y8h5EwvZE5pBb/r00zG+xcrW1f0nLo2fZEJ+xxs V2s2eWktqWOvtW1kmMeLoX0z7ABC/H1H6H8ipMAuxTLFNzUzyNT8ZklBL6nejMh/UigY HdUy+/yasmk26OFPLH9ZJ70NuH19lF4QMl+woxVmYoQX6kcDh4YjjlW9VZEVqHbLogrh Fn8Q== X-Gm-Message-State: AOJu0YzjMOBpyfFc9aAR3vPd9eliaGujcA4jnUeb2f0l+58wPzVJUauP znpSTqxHeX13ooM003B7k3QrGvelp7SIAj4KMkyXQCXU/w== X-Google-Smtp-Source: AGHT+IGwd4dDRXqLVlNJ8Knn15D1lvRQSiFU6wgYFjBngTtUJpgQwL87pfUkRcwNA3YuHcuzxz2yZg== X-Received: by 2002:a05:600c:3791:b0:40e:4f24:64d6 with SMTP id o17-20020a05600c379100b0040e4f2464d6mr640947wmr.41.1704898627129; Wed, 10 Jan 2024 06:57:07 -0800 (PST) Message-ID: <88437dea-8448-4420-a608-957f3d861bba@suse.com> Date: Wed, 10 Jan 2024 15:57:06 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v5 1/5] x86/HVM: split restore state checking from state loading Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= References: <8110e31e-5411-467e-9c59-06751902853a@suse.com> Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6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AxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <8110e31e-5411-467e-9c59-06751902853a@suse.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1704898644801100001 ..., at least as reasonably feasible without making a check hook mandatory (in particular strict vs relaxed/zero-extend length checking can't be done early this way). Note that only one of the two uses of "real" hvm_load() is accompanied with a "checking" one. The other directly consumes hvm_save() output, which ought to be well-formed. This means that while input data related checks don't need repeating in the "load" function when already done by the "check" one (albeit assertions to this effect may be desirable), domain state related checks (e.g. has_xyz(d)) will be required in both places. With the split arch_hvm_{check,load}(), also invoke the latter only after downing all the vCPU-s. Suggested-by: Roger Pau Monn=C3=A9 Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monn=C3=A9 --- Do we really need all the copying involved in use of _hvm_read_entry() (backing hvm_load_entry()? Zero-extending loads are likely easier to handle that way, but for strict loads all we gain is a reduced risk of unaligned accesses (compared to simply pointing into h->data[]). --- v5: Add comment. Re-order vCPU-down vs arch_hvm_load(). v4: Fold hvm_check() into hvm_load(). v2: New. --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -379,8 +379,12 @@ long arch_do_domctl( if ( copy_from_guest(c.data, domctl->u.hvmcontext.buffer, c.size) = !=3D 0 ) goto sethvmcontext_out; =20 + ret =3D hvm_load(d, false, &c); + if ( ret ) + goto sethvmcontext_out; + domain_pause(d); - ret =3D hvm_load(d, &c); + ret =3D hvm_load(d, true, &c); domain_unpause(d); =20 sethvmcontext_out: --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -5391,7 +5391,7 @@ int hvm_copy_context_and_params(struct d } =20 c.cur =3D 0; - rc =3D hvm_load(dst, &c); + rc =3D hvm_load(dst, true, &c); =20 out: vfree(c.data); --- a/xen/arch/x86/hvm/save.c +++ b/xen/arch/x86/hvm/save.c @@ -30,7 +30,8 @@ static void arch_hvm_save(struct domain d->arch.hvm.sync_tsc =3D rdtsc(); } =20 -static int arch_hvm_load(struct domain *d, const struct hvm_save_header *h= dr) +static int arch_hvm_check(const struct domain *d, + const struct hvm_save_header *hdr) { uint32_t eax, ebx, ecx, edx; =20 @@ -55,6 +56,11 @@ static int arch_hvm_load(struct domain * "(%#"PRIx32") and restored on another (%#"PRIx32").\n", d->domain_id, hdr->cpuid, eax); =20 + return 0; +} + +static void arch_hvm_load(struct domain *d, const struct hvm_save_header *= hdr) +{ /* Restore guest's preferred TSC frequency. */ if ( hdr->gtsc_khz ) d->arch.tsc_khz =3D hdr->gtsc_khz; @@ -66,13 +72,12 @@ static int arch_hvm_load(struct domain * =20 /* VGA state is not saved/restored, so we nobble the cache. */ d->arch.hvm.stdvga.cache =3D STDVGA_CACHE_DISABLED; - - return 0; } =20 /* List of handlers for various HVM save and restore types */ static struct { hvm_save_handler save; + hvm_check_handler check; hvm_load_handler load; const char *name; size_t size; @@ -88,6 +93,7 @@ void __init hvm_register_savevm(uint16_t { ASSERT(typecode <=3D HVM_SAVE_CODE_MAX); ASSERT(hvm_sr_handlers[typecode].save =3D=3D NULL); + ASSERT(hvm_sr_handlers[typecode].check =3D=3D NULL); ASSERT(hvm_sr_handlers[typecode].load =3D=3D NULL); hvm_sr_handlers[typecode].save =3D save_state; hvm_sr_handlers[typecode].load =3D load_state; @@ -275,12 +281,15 @@ int hvm_save(struct domain *d, hvm_domai return 0; } =20 -int hvm_load(struct domain *d, hvm_domain_context_t *h) +/* + * @real =3D false requests checking of the incoming state, while @real = =3D true + * requests actual loading, which will then assume that checking was alrea= dy + * done or is unnecessary. + */ +int hvm_load(struct domain *d, bool real, hvm_domain_context_t *h) { const struct hvm_save_header *hdr; struct hvm_save_descriptor *desc; - hvm_load_handler handler; - struct vcpu *v; int rc; =20 if ( d->is_dying ) @@ -291,50 +300,92 @@ int hvm_load(struct domain *d, hvm_domai if ( !hdr ) return -ENODATA; =20 - rc =3D arch_hvm_load(d, hdr); - if ( rc ) - return rc; + rc =3D arch_hvm_check(d, hdr); + if ( real ) + { + struct vcpu *v; + + ASSERT(!rc); =20 - /* Down all the vcpus: we only re-enable the ones that had state saved= . */ - for_each_vcpu(d, v) - if ( !test_and_set_bit(_VPF_down, &v->pause_flags) ) - vcpu_sleep_nosync(v); + /* + * Down all the vcpus: we only re-enable the ones that had state + * saved. + */ + for_each_vcpu(d, v) + if ( !test_and_set_bit(_VPF_down, &v->pause_flags) ) + vcpu_sleep_nosync(v); + + arch_hvm_load(d, hdr); + } + else if ( rc ) + return rc; =20 for ( ; ; ) { + const char *name; + hvm_load_handler load; + if ( h->size - h->cur < sizeof(struct hvm_save_descriptor) ) { /* Run out of data */ printk(XENLOG_G_ERR "HVM%d restore: save did not end with a null entry\n", d->domain_id); + ASSERT(!real); return -ENODATA; } =20 /* Read the typecode of the next entry and check for the end-mark= er */ desc =3D (struct hvm_save_descriptor *)(&h->data[h->cur]); - if ( desc->typecode =3D=3D 0 ) + if ( desc->typecode =3D=3D HVM_SAVE_CODE(END) ) + { + /* Reset cursor for hvm_load(, true, ). */ + if ( !real ) + h->cur =3D 0; return 0; + } =20 /* Find the handler for this entry */ - if ( (desc->typecode > HVM_SAVE_CODE_MAX) || - ((handler =3D hvm_sr_handlers[desc->typecode].load) =3D=3D NU= LL) ) + if ( desc->typecode >=3D ARRAY_SIZE(hvm_sr_handlers) || + !(name =3D hvm_sr_handlers[desc->typecode].name) || + !(load =3D hvm_sr_handlers[desc->typecode].load) ) { printk(XENLOG_G_ERR "HVM%d restore: unknown entry typecode %u\= n", d->domain_id, desc->typecode); + ASSERT(!real); return -EINVAL; } =20 - /* Load the entry */ - printk(XENLOG_G_INFO "HVM%d restore: %s %"PRIu16"\n", d->domain_id, - hvm_sr_handlers[desc->typecode].name, desc->instance); - rc =3D handler(d, h); + if ( real ) + { + /* Load the entry */ + printk(XENLOG_G_INFO "HVM restore %pd: %s %"PRIu16"\n", d, + name, desc->instance); + rc =3D load(d, h); + } + else + { + /* Check the entry. */ + hvm_check_handler check =3D hvm_sr_handlers[desc->typecode].ch= eck; + + if ( !check ) + { + if ( desc->length > h->size - h->cur - sizeof(*desc) ) + return -ENODATA; + h->cur +=3D sizeof(*desc) + desc->length; + rc =3D 0; + } + else + rc =3D check(d, h); + } + if ( rc ) { - printk(XENLOG_G_ERR "HVM%d restore: failed to load entry %u/%u= rc %d\n", - d->domain_id, desc->typecode, desc->instance, rc); + printk(XENLOG_G_ERR "HVM restore %pd: failed to %s %s:%u rc %d= \n", + d, real ? "load" : "check", name, desc->instance, rc); return rc; } + process_pending_softirqs(); } =20 --- a/xen/arch/x86/include/asm/hvm/save.h +++ b/xen/arch/x86/include/asm/hvm/save.h @@ -103,6 +103,8 @@ static inline unsigned int hvm_load_inst * restoring. Both return non-zero on error. */ typedef int (*hvm_save_handler) (struct vcpu *v, hvm_domain_context_t *h); +typedef int (*hvm_check_handler)(const struct domain *d, + hvm_domain_context_t *h); typedef int (*hvm_load_handler) (struct domain *d, hvm_domain_context_t *h); =20 @@ -140,6 +142,6 @@ size_t hvm_save_size(struct domain *d); int hvm_save(struct domain *d, hvm_domain_context_t *h); int hvm_save_one(struct domain *d, unsigned int typecode, unsigned int ins= tance, XEN_GUEST_HANDLE_64(uint8) handle, uint64_t *bufsz); -int hvm_load(struct domain *d, hvm_domain_context_t *h); +int hvm_load(struct domain *d, bool real, hvm_domain_context_t *h); =20 #endif /* __XEN_HVM_SAVE_H__ */