From nobody Mon Feb 9 11:47:53 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E49D80BF8 for ; Fri, 9 Feb 2024 18:37:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707503873; cv=none; b=KW/j5rhGarv3oQ7Z8ffdCkbXal+ENc37eVO1RAsMU6HuJlVAieipNYZ6pv1SjR02lUm3dkC27EKiJBbzkaxSSXzNqdbE9JO8xCgKPSjdmDqHCZczGZskst0ClJEzKrUFOhVDx6zo3oJqWGNoOA8+6Dv9IHVl8HCpIo45u6ndTOA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707503873; c=relaxed/simple; bh=nxL7p5ga/pbwBNJ0AcArtO5cQqiGKNRbW0hrGmJ/ETA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=sTetZnR5IUqkpa3n5n+zvuIhfxVnLAk5RQGhKEewehS2WQxSpXtFu1nyUYIpwXf1FhPQ5lD9qIoJ7Sp+ip5Bf70ddsIGa6qXJ6Cjz66d6H9s8GeSpii2ivVNL0AS5AIzASdD0HdZaOI37T8voeOUhgWGq8KDS7iUAJtO/EeyN8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=IfLY0GrF; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="IfLY0GrF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707503869; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uTQ6AKR5avCm33VMOIR+Irc60rlBw+yA9bG+WGeGyGk=; b=IfLY0GrFBxvcktjJA91UUwY54JBOafZB5X2TUtjuYK0YSFjX3gM7ROrKw1evSistrIcPfb 1gYBemVhciTcfApegriHGXSo18BZYzATUbNhdevJ1g/WwheRW0HiBdMz754Y/76PqJosFS psCTDMyCh1hdZsZ3u8PGedY+dP2JjH4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-9-_wNBaf-GMFunlgOexE6Pww-1; Fri, 09 Feb 2024 13:37:44 -0500 X-MC-Unique: _wNBaf-GMFunlgOexE6Pww-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5BD75862DC2; Fri, 9 Feb 2024 18:37:44 +0000 (UTC) Received: from virtlab511.virt.lab.eng.bos.redhat.com (virtlab511.virt.lab.eng.bos.redhat.com [10.19.152.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2DC76492BC6; Fri, 9 Feb 2024 18:37:44 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com, aik@amd.com, isaku.yamahata@intel.com Subject: [PATCH 02/10] KVM: introduce new vendor op for KVM_GET_DEVICE_ATTR Date: Fri, 9 Feb 2024 13:37:34 -0500 Message-Id: <20240209183743.22030-3-pbonzini@redhat.com> In-Reply-To: <20240209183743.22030-1-pbonzini@redhat.com> References: <20240209183743.22030-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 Content-Type: text/plain; charset="utf-8" Allow vendor modules to provide their own attributes on /dev/kvm. To avoid proliferation of vendor ops, implement KVM_HAS_DEVICE_ATTR and KVM_GET_DEVICE_ATTR in terms of the same function. You're not supposed to use KVM_GET_DEVICE_ATTR to do complicated computations, especially on /dev/kvm. Signed-off-by: Paolo Bonzini Reviewed-by: Michael Roth --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 52 +++++++++++++++++++----------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 378ed944b849..ac8b7614e79d 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -122,6 +122,7 @@ KVM_X86_OP(enter_smm) KVM_X86_OP(leave_smm) KVM_X86_OP(enable_smi_window) #endif +KVM_X86_OP_OPTIONAL(dev_get_attr) KVM_X86_OP_OPTIONAL(mem_enc_ioctl) KVM_X86_OP_OPTIONAL(mem_enc_register_region) KVM_X86_OP_OPTIONAL(mem_enc_unregister_region) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index d271ba20a0b2..0bcd9ae16097 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1769,6 +1769,7 @@ struct kvm_x86_ops { void (*enable_smi_window)(struct kvm_vcpu *vcpu); #endif =20 + int (*dev_get_attr)(u64 attr, u64 *val); int (*mem_enc_ioctl)(struct kvm *kvm, void __user *argp); int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *ar= gp); int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *= argp); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bf10a9073a09..8746530930d5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4804,37 +4804,53 @@ static inline void __user *kvm_get_attr_addr(struct= kvm_device_attr *attr) return uaddr; } =20 -static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr) +static int __kvm_x86_dev_get_attr(struct kvm_device_attr *attr, u64 *val) { - u64 __user *uaddr =3D kvm_get_attr_addr(attr); + int r; =20 if (attr->group) return -ENXIO; =20 + switch (attr->attr) { + case KVM_X86_XCOMP_GUEST_SUPP: + r =3D 0; + *val =3D kvm_caps.supported_xcr0; + break; + default: + r =3D -ENXIO; + if (kvm_x86_ops.dev_get_attr) + r =3D kvm_x86_ops.dev_get_attr(attr->attr, val); + break; + } + + return r; +} + +static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr) +{ + u64 __user *uaddr; + int r; + u64 val; + + r =3D __kvm_x86_dev_get_attr(attr, &val); + if (r < 0) + return r; + + uaddr =3D kvm_get_attr_addr(attr); if (IS_ERR(uaddr)) return PTR_ERR(uaddr); =20 - switch (attr->attr) { - case KVM_X86_XCOMP_GUEST_SUPP: - if (put_user(kvm_caps.supported_xcr0, uaddr)) - return -EFAULT; - return 0; - default: - return -ENXIO; - } + if (put_user(val, uaddr)) + return -EFAULT; + + return 0; } =20 static int kvm_x86_dev_has_attr(struct kvm_device_attr *attr) { - if (attr->group) - return -ENXIO; + u64 val; =20 - switch (attr->attr) { - case KVM_X86_XCOMP_GUEST_SUPP: - return 0; - default: - return -ENXIO; - } + return __kvm_x86_dev_get_attr(attr, &val); } =20 long kvm_arch_dev_ioctl(struct file *filp, --=20 2.39.0