From patchwork Tue Sep 19 13:41:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141990 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp3514558vqi; Tue, 19 Sep 2023 09:23:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGuYJzNpxcz8ku7KheGjvOEO/NyzpJ1fKErs1FKBeD5zb97ck+hsvFRNyZGGeR+yYGjZUrC X-Received: by 2002:a17:90a:7bc9:b0:271:c314:a591 with SMTP id d9-20020a17090a7bc900b00271c314a591mr168584pjl.47.1695140608775; Tue, 19 Sep 2023 09:23:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695140608; cv=none; d=google.com; s=arc-20160816; b=fMwO3lv9McuwHdcD1uA5Yu1TcAIJWS8MupfSEPaoTlizO6vrcEFDxEX3ePVFwMDBjk K6qv15NSdYkW8UzanOVpiwgRTHVv+NkmGe96RS28CxHI3VE5OkDgJiXbci1135INxbwZ AUq1CO2BiGGcSvfXfHyOWvLmfEzXVQaF4xbxNuDP9TwdbcQT8oNKLXAvHQDXh/bTmu0y D76PuB9QaVXonKo1FOTA6+21JeS70QGLbPXvfK7as63Yftn7XA+ioQI9/kSXfGTnPoDZ 2gsOYML4Gn/b/2grO52dpIVqRoLDbl1H7BAFtSY7L9h5C/TjsHFg5jgnxoBQTnacv9dy OG4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ArUkbNlkp1CHiW7Wj8MKCbs12nDVBj/n1JhP/fpqVdY=; fh=8AvbwpPKF0Qe5WwS5eyABOVDUChKGxIrkPaOnjrOqKo=; b=RR6uLtQ3X5a2KHSHa5rtRxr3DzSyqBlC+SmahmTGVzd7/8oD/7SAX7OosLlZYsqL57 /V4JavoLw3tRqfRS9KNz822LdEn8g7WHcs5YittLn6nszUT+0P2MkU7foEOphDtW5+so /Ot9F8EGS4oJV/miUdHxrU0+/O5CbdksWX26VTfVYv7qiegO4o5WCelDffDOxUWvbhdL gZzIlEQZspTZ3TgWIgpTwUtFU4pPI7WnDzO4nCxxw62995Hj4tX7iu3H7gYipQpYSw7a POnbhJfcZzyQFVkEHb8pOz72YPXr2gkyupLia7tBNxK9xq+Y5z/KXDK80T41GRXPBzTp fyBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=PPApTmdn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id f6-20020a17090a700600b002743b58483asi9824294pjk.66.2023.09.19.09.23.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 09:23:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=PPApTmdn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 3496881D2D73; Tue, 19 Sep 2023 06:42:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232419AbjISNmi (ORCPT + 26 others); Tue, 19 Sep 2023 09:42:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232455AbjISNmc (ORCPT ); Tue, 19 Sep 2023 09:42:32 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EC65118; Tue, 19 Sep 2023 06:42:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=ArUkbNlkp1CHiW7Wj8MKCbs12nDVBj/n1JhP/fpqVdY=; b=PPApTmdn0ViXx/ldVOrnxbjwOe 2SXFfZM3SQgGrXvWr9aZwKrsz8UdMTxbDDFIRU9eNOkGvPzJS2J6a/oQTw6IYsKrKakveWNYVLujw 5txbhVKNaN34jL2FQlx34Fn8/r0nYETm7GVbQONcW3Mak3CgNUpOg7tL5oJqwarIxXHg=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiazP-000474-8S; Tue, 19 Sep 2023 13:42:07 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] helo=REM-PW02S00X.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qiazP-0005jy-0a; Tue, 19 Sep 2023 13:42:07 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH v4 08/13] KVM: xen: prevent vcpu_id from changing whilst shared_info is valid Date: Tue, 19 Sep 2023 13:41:44 +0000 Message-Id: <20230919134149.6091-9-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230919134149.6091-1-paul@xen.org> References: <20230919134149.6091-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 19 Sep 2023 06:42:41 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777483759158588112 X-GMAIL-MSGID: 1777483759158588112 From: Paul Durrant To further prepare for automatically using the vcpu_info structures embedded in the shared_info page, we need to ensure that the Xen vcpu_id cannot change under our feet. We can do this by simply returning -EBUSY to any attempt to modify the attribute while the shinfo_cache is active. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: David Woodhouse Cc: Sean Christopherson Cc: Paolo Bonzini Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: x86@kernel.org v3: - New in this version. --- Documentation/virt/kvm/api.rst | 3 ++- arch/x86/kvm/xen.c | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e9df4df6fe48..8d85fd7709f0 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -5605,7 +5605,8 @@ KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID This attribute is available when the KVM_CAP_XEN_HVM ioctl indicates support for KVM_XEN_HVM_CONFIG_EVTCHN_SEND features. It sets the Xen vCPU ID of the given vCPU, to allow timer-related VCPU operations to - be intercepted by KVM. + be intercepted by KVM. Note that this must be set before the + shared_info page is set. KVM_XEN_VCPU_ATTR_TYPE_TIMER This attribute is available when the KVM_CAP_XEN_HVM ioctl indicates diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 9449cfe1048f..a9aada47e9b8 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -752,6 +752,18 @@ int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data) return r; } +static int kvm_xen_set_vcpu_id(struct kvm_vcpu *vcpu, unsigned int vcpu_id) +{ + struct kvm *kvm = vcpu->kvm; + struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache; + + if (gpc->active) + return -EBUSY; + + vcpu->arch.xen.vcpu_id = vcpu_id; + return 0; +} + int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) { int idx, r = -ENOENT; @@ -941,10 +953,8 @@ int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) case KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID: if (data->u.vcpu_id >= KVM_MAX_VCPUS) r = -EINVAL; - else { - vcpu->arch.xen.vcpu_id = data->u.vcpu_id; - r = 0; - } + else + r = kvm_xen_set_vcpu_id(vcpu, data->u.vcpu_id); break; case KVM_XEN_VCPU_ATTR_TYPE_TIMER: