Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1498863rbb; Mon, 26 Feb 2024 11:08:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUUdDhTP9EgQp6zUgvXLYUuUwAdk4Sn/FJ3WjPD9410lq/dZxbnYV13BwW9LckL3DoAGyxQkZV4UEF24PPWu1TIDIE0WUOAPRX8TLlD9Q== X-Google-Smtp-Source: AGHT+IGCflcUKCyg6ANfFsUyzPZMiWkuPMUYqbYvPwRrB3K9eQjI1JbNoYHGEzyni5jfoCPzupUc X-Received: by 2002:a50:ed9a:0:b0:566:131b:5b5f with SMTP id h26-20020a50ed9a000000b00566131b5b5fmr1548113edr.26.1708974480188; Mon, 26 Feb 2024 11:08:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708974480; cv=pass; d=google.com; s=arc-20160816; b=doIFORrc0U8aJv4sNoSexu4ZaRdEF9kjwAadqhTM4pvsVcIavqB5p4UbQt8CYMLj2D YA1SRBPExIr7R0F9XQWPUzfr3N0DNhTYB/ABWMAFl0vZ3HPpHwNdzxvBhx7gaIXwCUaA xIN9XyBEMSC+mvuoLV70i7O5tbF/aRIarM1SGfTQl23SPe7Q0LhYc+jXuYjoyq9283K4 ptmd08cFCkP1L9ppNr258aDyp3BHbYidgn1CLQhvgqLZFH5sylWvjPWRT+UO4u5uAmhb FgDNq17kVQiygYd57xe47Ghh1U2shwu+biewYKWIP6mtKl43swOW85mM+oBNRPk1kCMP NJaw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=+GguoVTCj+D4yyYhhVzaIqifzwJU/fPpInE2qMCgiHY=; fh=TLdZJsFzSA0fpm6Q4iUv+uB0sG0oU+h3Bp6HiSGyZm8=; b=mRbi9uiDoy+w3aYg2PRbR9Rw46yF8Pi1whF6XWkbvb409hc5NBXjOEN7EU7wNHq2GN mcCzIo9po3p42tEQjPLtboXX8UDLogMn3Ih4arRXiFUa2AL5G5cq6vhvIRYa+enfoOnu 5xd9/TH6C1F9CeHFmRy1uzb5AqQA/7AeajQGMZJtLNgoWoGU+CoBuaksyNZKJ2IOkKXk w02a1ln+uE3IBbagNXKgMCWgll4Xm8Quq3o3kcDEhRhSt2k3iyf/uHOkYxHUw/GJNLz/ xAseIXJiw1gniKMaDIq7aqZgipug03MQmWM37oOGGmHGEFutL5SkOVL5l1Kii/ip0vja H7iA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GccLdLsM; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-82187-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-82187-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z3-20020a05640240c300b005642ed50e9fsi22354edb.682.2024.02.26.11.08.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 11:08:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-82187-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GccLdLsM; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-82187-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-82187-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2DADD1F247A1 for ; Mon, 26 Feb 2024 19:06:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 90915132C05; Mon, 26 Feb 2024 19:03:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GccLdLsM" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 202FB12FB3F for ; Mon, 26 Feb 2024 19:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708974235; cv=none; b=SmZMtCl3l7+Saf1nPAf8H5yWC7QmprHNhdXsymcEzXoblNpxL6QfjQfL8K400nXU9W17UAkll2CtHkwSZCD9o1544HFZFgvwk5D3xYn8pDYVjw5cyM2nw9UIITz6ts1fvlCy+PnwkcJjFJPMiTHNbZNJF/rcaPRI4rIKDZlUfgk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708974235; c=relaxed/simple; bh=rCfHCHTYWoz4975u7BrV2pN/NoZkUaeFEvOlrnvEp1c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=IgBM8JURpKtR1K9BIi4C5zFqSUxiK2GT5KzlI/zLQY5VUJTFoB/Ia7Vrd+GdfDNDuFABMVD0bsscGYqFRkOaxP8g7uaBka02UzpWnu+uAjL9Vwm8kGJwpkRUujwpw+9dBKSCos5xfiD1uADE3/NzyoeefYIsxzkcxATA5F4JBJE= 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=GccLdLsM; arc=none smtp.client-ip=170.10.133.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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708974231; 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=+GguoVTCj+D4yyYhhVzaIqifzwJU/fPpInE2qMCgiHY=; b=GccLdLsMGbwypJn7YSnQQGVPSxHR/FkhZ7d5Mt2upAke4fTPTJdxBifW0mY/JveGCXy02v gAeuea57wKx7dnl6E+Nmu2IG8Ha9jZwXtIvVW1tEruiSLvxnKhHPDQQShd6+W6F0H5fSoD ViYP57RmjWRXAhHWkoXSiwBnAD94EFk= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-584-UC0NhyPPMJKzqNTTWv8S8A-1; Mon, 26 Feb 2024 14:03:47 -0500 X-MC-Unique: UC0NhyPPMJKzqNTTWv8S8A-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 D924D38212D8; Mon, 26 Feb 2024 19:03:46 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id B0A5D492BC6; Mon, 26 Feb 2024 19:03:46 +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 Subject: [PATCH v3 06/15] KVM: introduce new vendor op for KVM_GET_DEVICE_ATTR Date: Mon, 26 Feb 2024 14:03:35 -0500 Message-Id: <20240226190344.787149-7-pbonzini@redhat.com> In-Reply-To: <20240226190344.787149-1-pbonzini@redhat.com> References: <20240226190344.787149-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-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 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. Reviewed-by: Michael Roth Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 43 ++++++++++++++++++++---------- 3 files changed, 31 insertions(+), 14 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_host.h index aaf5a25ea7ed..651ce10cc152 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1770,6 +1770,7 @@ struct kvm_x86_ops { void (*enable_smi_window)(struct kvm_vcpu *vcpu); #endif + 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 *argp); 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 14c969782d73..a12af5042d82 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4791,34 +4791,49 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) return r; } -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 = u64_to_user_ptr(attr->addr); + int r; if (attr->group) return -ENXIO; switch (attr->attr) { case KVM_X86_XCOMP_GUEST_SUPP: - if (put_user(kvm_caps.supported_xcr0, uaddr)) - return -EFAULT; - return 0; + r = 0; + *val = kvm_caps.supported_xcr0; + break; default: - return -ENXIO; + r = -ENXIO; + if (kvm_x86_ops.dev_get_attr) + r = static_call(kvm_x86_dev_get_attr)(attr->attr, val); + break; } + + return r; +} + +static int kvm_x86_dev_get_attr(struct kvm_device_attr *attr) +{ + u64 __user *uaddr = u64_to_user_ptr(attr->addr); + int r; + u64 val; + + r = __kvm_x86_dev_get_attr(attr, &val); + if (r < 0) + return r; + + if (put_user(val, uaddr)) + return -EFAULT; + + return 0; } static int kvm_x86_dev_has_attr(struct kvm_device_attr *attr) { - if (attr->group) - return -ENXIO; + u64 val; - switch (attr->attr) { - case KVM_X86_XCOMP_GUEST_SUPP: - return 0; - default: - return -ENXIO; - } + return __kvm_x86_dev_get_attr(attr, &val); } long kvm_arch_dev_ioctl(struct file *filp, -- 2.39.1