Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp713643pxb; Fri, 14 Jan 2022 14:46:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJwKYWMBIy6fGtFDKcyPvSm/Jl3cd+tXhvCU0yqHNquwy0CE8LDOQQi7ws5Mz62RHrFN8t9/ X-Received: by 2002:aa7:dc05:: with SMTP id b5mr10779937edu.46.1642200399290; Fri, 14 Jan 2022 14:46:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642200399; cv=none; d=google.com; s=arc-20160816; b=r3xXn8XirJGHBrXHdTMnvqkO92aGK8wPHvIVBFwTVlIq8YIs18+gxHcgHzatfXH3rP K3V+yhZn8UvfCDbWs2vGs6zvgYpPHXjp+xRgCtqVGGy05WNdwgcAufD8rqdTr7A8HMKT +1jtTMTgBXty63t3jZ59+AzX08Aw56BaC5rTACuwxMdReHV810TTmglUbYDa5+/cHMvp f9L/+QKuBEmgcueOYRVBmC0RKRAQpTuD7Qm+vI3a5gHPPU/hUp9Z9XSJWppOVZkPtna0 S+xLbX3YNRK577fMrnwK4fjr8khY9O2MZ0eGnZdedY71MAh6V0PZ4G9n0B68S+99iPyF l5mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ahkubcrgcnWjiRruY+cbCIcOcMw/nGOD40THRvByTZ4=; b=q/esp+PWxacjCK2UydMAxcRwjCQblVKvG397fyVLrAE/JAq2SpkfAZ4U0zoG1IfQZp +0aXag5kdqm8HEMaD+cEm+zMe6RS8jseIobG5/kQtC7rjV4MZUAIHgiGN7HI11avN0nV JEHwXAv9D8CI2OlopfEjg0zm6j7AMJ/l3Dj6qmgBhW+DDKK1ewlyXQiqUh6XM8jWJVIb Hz0I8/Cklq3PIIcv0frhBSLKO6Sc8Bla+EHSNYZXKZ3utSxp5pZE3u587QQ1CeAVkFKx aQZDDP66ll8airsL9tFy1fSilcLhCOa4pO1L4AtyZ52LZaeiiQA9Ew7u62JMvduzY/f4 M9uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rtZ+Wdwb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ds20si4330857ejc.36.2022.01.14.14.46.15; Fri, 14 Jan 2022 14:46:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rtZ+Wdwb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243154AbiANQJA (ORCPT + 99 others); Fri, 14 Jan 2022 11:09:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243135AbiANQI5 (ORCPT ); Fri, 14 Jan 2022 11:08:57 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60E6EC06161C for ; Fri, 14 Jan 2022 08:08:57 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id 59-20020a17090a09c100b001b34a13745eso22354866pjo.5 for ; Fri, 14 Jan 2022 08:08:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ahkubcrgcnWjiRruY+cbCIcOcMw/nGOD40THRvByTZ4=; b=rtZ+WdwbQ/+MOMGU2toCDBOaxzzP5jVuX9NI+4NetFHGHeM2k4yYW94cFWQFAZim+d kImeCZNMQplwGO/A2Ce1YeDiqkteZQ6rqEYq68Fcc4IdoE+HcWQWXqj3zn5OPlr/OquN 1JcgOySDNy5lGKONy5k/023Xq8/YjwqdMch/Lwx+uHmldJqaNIBh8UbWEeZSKI5yE2Pq FhDfiRqxvJn18Xt2VWC32tbRW8ff+mS49slbdlSpqs4PsmpgYlR6vln2vuNQPU0i6sfc VjzZxQnUxS9XmuIASvm60rs6+lYQ/LtPPDBBCIKU+sMkjmRCFabLyxXwCkabZ+MO0XTu Ho4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ahkubcrgcnWjiRruY+cbCIcOcMw/nGOD40THRvByTZ4=; b=jb+Jcn++RXQrrkQVYhyBlvTeZE+cAT9LiODFKGozIsinbmJXTrSe1MLyqADRrpR+YY rd2ZrOlSvX5mFx0SVMGEYR/a2rCp7MJN8yy8KOyRBz4/PhChUQ8Tln+bhiBYPGFi8H7T +02SOG5QjH4AUBwUz4TfVSqP5jxCNAkbGWT62Grlh19UBGuBdNkQoXS5fnLYvL0vSJmZ awUJ3mg4iHYEp2oX3gGOUdK5U34a/djPS8iF41u5hH9UGvjGTpvbrNBtiTuqnrTJZfsD s/2yo/wQkUm81bBRG9f5h/4ZiJNedEpLTkeWV01NfO4m9R5j5oPAp5T+RYeGrn4+kDsg 5gkg== X-Gm-Message-State: AOAM533TbIajpThBXS/wSalLYjWOO4LgtdNB2nUXceFEtUHz8bNVA8/n g2ojkzewsPBqce1QEW5U+jNmVJhbBhaVQw== X-Received: by 2002:a17:90b:4d0f:: with SMTP id mw15mr8150705pjb.65.1642176536637; Fri, 14 Jan 2022 08:08:56 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id 13sm6485615pfm.161.2022.01.14.08.08.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jan 2022 08:08:56 -0800 (PST) Date: Fri, 14 Jan 2022 16:08:52 +0000 From: Sean Christopherson To: Maxim Levitsky Cc: Igor Mammedov , Vitaly Kuznetsov , Paolo Bonzini , kvm@vger.kernel.org, Wanpeng Li , Jim Mattson , linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/2] KVM: x86: Forbid KVM_SET_CPUID{,2} after KVM_RUN Message-ID: References: <20211122175818.608220-1-vkuznets@redhat.com> <20211122175818.608220-3-vkuznets@redhat.com> <16368a89-99ea-e52c-47b6-bd006933ec1f@redhat.com> <20211227183253.45a03ca2@redhat.com> <61325b2b-dc93-5db2-2d0a-dd0900d947f2@redhat.com> <87mtkdqm7m.fsf@redhat.com> <20220103104057.4dcf7948@redhat.com> <5b516b51f81874fe7cafe8ce6846bc9936d83cc7.camel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5b516b51f81874fe7cafe8ce6846bc9936d83cc7.camel@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 14, 2022, Maxim Levitsky wrote: > On Thu, 2022-01-13 at 22:33 +0000, Sean Christopherson wrote: > > On Mon, Jan 03, 2022, Igor Mammedov wrote: > > > On Mon, 03 Jan 2022 09:04:29 +0100 > > > Vitaly Kuznetsov wrote: > > > > > > > Paolo Bonzini writes: > > > > > > > > > On 12/27/21 18:32, Igor Mammedov wrote: > > > > > > > Tweaked and queued nevertheless, thanks. > > > > > > it seems this patch breaks VCPU hotplug, in scenario: > > > > > > > > > > > > 1. hotunplug existing VCPU (QEMU stores VCPU file descriptor in parked cpus list) > > > > > > 2. hotplug it again (unsuspecting QEMU reuses stored file descriptor when recreating VCPU) > > > > > > > > > > > > RHBZ:https://bugzilla.redhat.com/show_bug.cgi?id=2028337#c11 > > > > > > > > > > > > > > > > The fix here would be (in QEMU) to not call KVM_SET_CPUID2 again. > > > > > However, we need to work around it in KVM, and allow KVM_SET_CPUID2 if > > > > > the data passed to the ioctl is the same that was set before. > > > > > > > > Are we sure the data is going to be *exactly* the same? In particular, > > > > when using vCPU fds from the parked list, do we keep the same > > > > APIC/x2APIC id when hotplugging? Or can we actually hotplug with a > > > > different id? > > > > > > If I recall it right, it can be a different ID easily. > > > > No, it cannot. KVM doesn't provide a way for userspace to change the APIC ID of > > a vCPU after the vCPU is created. x2APIC flat out disallows changing the APIC ID, > > and unless there's magic I'm missing, apic_mmio_write() => kvm_lapic_reg_write() > > is not reachable from userspace. > > So after all, it is true that vcpu_id == initial APIC_ID, > and if we don't let guest change it, it will be always like that? Except for kvm_apic_set_state(), which I forgot existed, yes. > You said that its not true in the other mail in the thread. I was wrong, I was thinking that userspace could reach kvm_lapic_reg_write(), but I forgot that there would be no connection without x2apic. But I forgot about kvm_apic_set_state()... > I haven't checked it in the code yet, as I never was much worried about > userspace changing, but I will check it soon. > > I did a quick look and I see that at least the userspace can call > 'kvm_apic_set_state' and it contains snapshot of all apic registers, > including apic id. However it would be very easy to add a check there and > fail if userspace attempts to set APIC_ID != vcpu_id. Yeah, hopefully that doesn't break any userspace. I can't imagine it would, because if the guest disabled and re-enabled the APIC, kvm_lapic_set_base() would restore the APIC ID to vcpu_id. With luck, that's the last hole we need to close...