Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp871250rdb; Fri, 23 Feb 2024 02:45:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXbYRLksX1K+BS77DfxgYkAqqx6mjoRIZ602upkphUbo4GSsokAXS057p7Fe0EjdEZ9AEgq7vUOmVAVThR59uEJnhHefXZlzOftEq0uiw== X-Google-Smtp-Source: AGHT+IE80Eob9/TKMiZs031MA4TYhVwoXtiW84hDAA0yMjZ07X7B2Zq2/wzPkqLQEwH7rzRW/0vu X-Received: by 2002:aa7:d916:0:b0:565:7ce5:abc6 with SMTP id a22-20020aa7d916000000b005657ce5abc6mr489940edr.40.1708685116460; Fri, 23 Feb 2024 02:45:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708685116; cv=pass; d=google.com; s=arc-20160816; b=eO9UymLnhcKMFaa8AyqAWzOYRAI83oBQ2lQ0IKHO+MjNwpj6N6XAO+NRgkXyfB12/K eU+xgWoUgPWVDppe7eMK/g7EbtxfVIT2TP14a3bg8XZ1cgqFiqJAui6Ws1zpHKJF9o58 IFc6vDlnFrt/ZqcW+ofptTxPaZ72iS6ZtjJ7bTgpwFXP48P5TGgmeESuCfZCUr+Nd+u2 TrPi9DrEEX1BHLsGoUdprXPVMe2xtrJGxMGPnbTveQYq0i0owKgwrRqEMqB/DIkyW5tQ KwkAw1vHmPUW3hDDqoP4TMFU1HZaahFnaFxO8M8n40h9MYD2nYb4bc+nLcKtZxeRa6yg XapQ== 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=LiCqbXFLA/UyGnlYUq9cP+Y4tIkdjEj7HNX9/TX+2pE=; fh=TLdZJsFzSA0fpm6Q4iUv+uB0sG0oU+h3Bp6HiSGyZm8=; b=p+pTFXOZ4sSXt/7dvZiVh8Oxe6UOniSuH2OcrlMQc6RXKvV5JxuVlNY1MGrkEh/Y+Z ZW8j5DigyHhgORzkqj+NHONaBD+pD5OUMmBAOcQBal6Uehf3LsvvB+3cnW9Cg/3FwLzl 0mVqJ5MQO3M5tJBaR0LIrHk07vMeRHUs9lK8d4TmEnYN85r4JgkMv5y/O2lvP7W6+so5 0n27kMiTyIGUGaTIa1ekj+uxso/L/vANftn6DJXAG8sRFn8+PT+Q9IRGcI2XXLFp40UP w0P4hq9MGJMVZ9LqxlvvezxWa8JPmNLXZHyp5Fgv8VWMxstYV/BKS3RltbSjYZ5OlpaH vdVQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GxFC1CYp; 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-78166-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78166-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 dh28-20020a0564021d3c00b0056400f8616csi6008016edb.566.2024.02.23.02.45.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 02:45:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-78166-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=GxFC1CYp; 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-78166-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78166-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 3B54D1F232FF for ; Fri, 23 Feb 2024 10:45:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EFEA87E783; Fri, 23 Feb 2024 10:41:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GxFC1CYp" 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 8EC7467A01 for ; Fri, 23 Feb 2024 10:41:04 +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=1708684866; cv=none; b=pOSn/AtjJl9C7zWZrin598FTtuGz/Ruin23cAyb7YTIuBngdHeaI1Mr5mXXp+YCgtNysv6269bMg4x2mtYD1Rssp59sotRva48DRMexIUguUW7CPl1mjqZyy+jZ4YsMNcLpGk7na1Lmi0Q13iGvi7iZuaDooxNSet368vrIQ8VM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708684866; c=relaxed/simple; bh=G5tKkyjprqLewL3O1LDTvZquCuMpAlrU/ub17lN3RL0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=BWvIExB/2eO4u1hJSt4xkmDPi159eF2aSLFGp3ZU0C2Ig6FK8RHlR9AezaEbYIsybx/s9/nYAd34H102YawiRsuioowfGHr1NEnYtxMDtEAnHcl8IXqf+2croLmUjxU154sFvN963ResLoHkm98xOg0ajFKQlAMH3AikLhvJhZY= 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=GxFC1CYp; 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=1708684863; 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=LiCqbXFLA/UyGnlYUq9cP+Y4tIkdjEj7HNX9/TX+2pE=; b=GxFC1CYpyvXbSpcz2LksCXlH9ieG6yLz/8cPp5476qIRsp0FpBCHt/GgyuESt6uSW1Ah0T vs2lemsYHGrr+kNNVfMJ4seM8XDEkr5ucvfM3BTLr7YT02Lkn7I223OiKkiMOqAABH8aE6 ag9eWPLDfZ3r3/T2c5tHlFzG8G1kClU= Received: from mimecast-mx02.redhat.com (66.187.233.88 [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-607-2Tu4Y4_uNRWlVLT0SWqalQ-1; Fri, 23 Feb 2024 05:40:11 -0500 X-MC-Unique: 2Tu4Y4_uNRWlVLT0SWqalQ-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 CA4F6185A784; Fri, 23 Feb 2024 10:40:10 +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 A3C27112132A; Fri, 23 Feb 2024 10:40:10 +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 v2 02/11] KVM: introduce new vendor op for KVM_GET_DEVICE_ATTR Date: Fri, 23 Feb 2024 05:40:00 -0500 Message-Id: <20240223104009.632194-3-pbonzini@redhat.com> In-Reply-To: <20240223104009.632194-1-pbonzini@redhat.com> References: <20240223104009.632194-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. Signed-off-by: Paolo Bonzini Message-Id: <20240209183743.22030-3-pbonzini@redhat.com> 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 | 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_host.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 + 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 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; } -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 = kvm_get_attr_addr(attr); + int r; if (attr->group) return -ENXIO; + switch (attr->attr) { + case KVM_X86_XCOMP_GUEST_SUPP: + r = 0; + *val = kvm_caps.supported_xcr0; + break; + default: + r = -ENXIO; + if (kvm_x86_ops.dev_get_attr) + r = 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 = __kvm_x86_dev_get_attr(attr, &val); + if (r < 0) + return r; + + uaddr = kvm_get_attr_addr(attr); if (IS_ERR(uaddr)) return PTR_ERR(uaddr); - 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; } 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