Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp551507lqt; Mon, 18 Mar 2024 16:34:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVv2lOj5GkmBr8lHa3CCAXzzq6quWoQ2XzMZ9M9WTNxnAF+weTlYR7vvXCr1wqzAUcfgw4Z+NslYrGRaDodBaQrifIuuyoZI2kQYxoe6A== X-Google-Smtp-Source: AGHT+IFIwWs/J9wchbr9RjeVTaximgPbfCA64MLGFqCkbH8aiNQff4xqsK7DRUJAKx7o4BANJljR X-Received: by 2002:a05:620a:57d9:b0:788:5f38:d159 with SMTP id wl25-20020a05620a57d900b007885f38d159mr15067433qkn.11.1710804881737; Mon, 18 Mar 2024 16:34:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710804881; cv=pass; d=google.com; s=arc-20160816; b=hS08hK7odqWSlUjdIzcteqvFgZlSp+vcNabBlFKDsBEzoWqHUTapBvexq/6INmux1I sTT8N2loYcwOfj5Lh0zq3OlfL+qei9lmuydJG5RD6QrzPYiE1H2nxhxIAzSo/5pT0TfE 3rMH52a67En5IpuU8NEHzgZBEtII6vKnZ4vbKXNn5NAt4cIU0UxvzdMcVZW3W7oTvnHV Rx1bg8jzt4rCiIowTUEn7YeMkQxwf1lhDE3h4gyxwnN6lmmA1TQoNHGVDCYPxeXhQgkg d7/3cU1tNojIIkcPAY1wczV49IbYou/evYzMIRgHBMzhoconxpHDSzXwzkfYrDdVduCw LJag== 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=njlglT30so/A+1RKK68wAaixBuIYtSjbbdb5gDm3L0U=; fh=3hAI0TnvvweyuafrDQR9sJAViJ87fw+qG0excEgpXGk=; b=mdFPelViwe4jXkHmK1w3zONCK0Jjk2vMvhvgr1YbHT+X/hRRdLhoMGPcalHMaispRi i8RTW8ptLhA63/SY6dMnmkd9FyQrSiEnPAkYxGiYdBE+sgKad7dg5hr5OIAiNN0vS/cK k36blwMGPhK94tHoNsX4apKbu3iATc9USjhiUV/Kn+OvnY5i/poXQomIPY6WTupWEyW3 09WgxmRpBA2OnTQ/0E+2k+UB6mUr7GjbtK5jv17lzEp2yncOxD0Ivppd1Zsq29uSwY0v TjCU7i0Qz2adckOtbjy7bPKuuY+Y1iZJ52C6bDnUKeSm1RjM/1L6OzLi89Nxih1bY945 ogwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ByT3qbJu; 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-106807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-106807-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id xy10-20020a05620a5dca00b00789eb26bda5si6463377qkn.729.2024.03.18.16.34.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 16:34:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-106807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ByT3qbJu; 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-106807-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-106807-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 76C871C21AF3 for ; Mon, 18 Mar 2024 23:34:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0A6D5FDBF; Mon, 18 Mar 2024 23:34:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ByT3qbJu" 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 A04925F870 for ; Mon, 18 Mar 2024 23:33:59 +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=1710804841; cv=none; b=QvjdONMhkaxswwVWR5x5z1UGAzr7bsf86AcqSubNH60tLdvTQY1e4mt8gEVJef0V6LIh2qibdAhHaI0XU4YOucKrwoeSk52yQdnNDRy1qB7uL9puQaVhCrTy2kpj80TwUXjBf+dXslPWREEyMSJ3KZ71/EjOmXjIgTAHeR1NV0o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710804841; c=relaxed/simple; bh=54nyVR2dGgebG8omWQWEhwokjb41+ZuOxUUjTXEZ9U8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=S+K6y1aDjF8UKjqtqEvTdAIY/tqrg6cGm0spayA8v8iatdOO3pHGGhQ8T5xokPf0ZkXkqIhmurkGI6OflIk0+/+ZV7/7c2cD1GOGD2TZVbIfXbLeV9kuM5qvTaX6EE8h30dixYrH/O/y0taoe2IXnCqTtcZeug3flWGibWf5sDU= 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=ByT3qbJu; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710804838; 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=njlglT30so/A+1RKK68wAaixBuIYtSjbbdb5gDm3L0U=; b=ByT3qbJuxKfn2mceB1AVf/b2FVpM/nwEYI1UUj9wjsp9jwhPTZj4ctSV+nuMGNhr3oR0ll RB5r+DWR4PH8g9gtG9Cr9smkuDCS92NzRp3mYtxeiJmJ3mmhKYkRhZk1fjcnC9F8AMA9Wf iQNgZdAdW2V+C2zjEFtS7IrTg8+8wOI= 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-191-dXPgK_oqMVuYSDoRB_bD7A-1; Mon, 18 Mar 2024 19:33:55 -0400 X-MC-Unique: dXPgK_oqMVuYSDoRB_bD7A-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 9D08638000A1; Mon, 18 Mar 2024 23:33:54 +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 766E61121312; Mon, 18 Mar 2024 23:33:54 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: michael.roth@amd.com, isaku.yamahata@intel.com, seanjc@google.com Subject: [PATCH v4 04/15] KVM: introduce new vendor op for KVM_GET_DEVICE_ATTR Date: Mon, 18 Mar 2024 19:33:41 -0400 Message-ID: <20240318233352.2728327-5-pbonzini@redhat.com> In-Reply-To: <20240318233352.2728327-1-pbonzini@redhat.com> References: <20240318233352.2728327-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.3 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 110d7f29ca9a..5187fcf4b610 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -121,6 +121,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 16e07a2eee19..f6cc7bfb5462 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1778,6 +1778,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 3d2029402513..e8253aa8ef5e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4842,34 +4842,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.43.0