From patchwork Wed Nov 8 11:17:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 162977 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp844183vqo; Wed, 8 Nov 2023 03:24:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHH1123HOmls0QOrqbAquX4Rw6ROmouVD7ZMPZPk2xKvy0oyqufr2Mpe6Bt3KgWSrhf7dS/ X-Received: by 2002:a05:6a20:1584:b0:16b:e46e:1246 with SMTP id h4-20020a056a20158400b0016be46e1246mr1952961pzj.30.1699442681167; Wed, 08 Nov 2023 03:24:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699442681; cv=none; d=google.com; s=arc-20160816; b=A9hI+put6Mo+F6dbF7hjMMdtVQ/6MQUe7RTPMNVhtJwiaUjuBcdgzDFiiK2c5M+nD6 AtGFkaABPC0DaWQ539lpPXoZrIBxJqKWBRQFQcwpas9evb17GCCWCjmArtto8wxMMwPF r2dygLvf/GSOFH/MIRjvpF/1jV5So3Pe1PiRZqvgEmRx+GrTSFhVjjtUQ5v2+drWDHTZ F+lBkq4tCvt1TVCQ7oxj/it/HDJqv88XnuqdleIwH67OLPajhnQmqmor9+tYsTgQLdfn qL8sp2osCD3WtRwf5xKOk2ywPvX/3y1P1iunIek+AFaJ9IGQqLndDJh5DgMBuqb6o9w5 63Rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xf82pqS3tbHruxRFe/9qoNTFiZgXupnu5dstVI/jtoA=; fh=Qdq7NqGm5JR9LpctBpXjoRI38Lb2mCk6xy26GEDp1Bg=; b=SizulAv6Uwapw5F7A4lzsTwPZtsHq4M2ozx4PUDjYHyfaUdZzN8MCWdZB3Quil660E Rj+ohDhTyPPycP9IdepdiA3GgBKmQwaMtuMTg85bpFV26yB7Dr5zw/Za8MqxSizRPdtX K1MZPUmW809f9slwA2IV0NYQQvUbTSVAdK0jLSZBKnjQqqz0rU4doEcjL7iTODLH/y0P 1Mkrn0NDN3EkhkXRqvKtCWOBRiTnLR/DadeQtY2Glaoi+sqVJVgd/bkXCEUKMAVqjUS+ tvgjCHkffeKT6cJXOwylqpWxWs3lywbHOMZqVji9Of7QfpOJJL1V5z0IVQFeLHXj3bqj 580Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=n49ZCsyV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id g19-20020a056a0023d300b006be2d998584si13076049pfc.47.2023.11.08.03.24.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 03:24:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=n49ZCsyV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 76EAC83AF8A8; Wed, 8 Nov 2023 03:24:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344608AbjKHLY2 (ORCPT + 32 others); Wed, 8 Nov 2023 06:24:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344673AbjKHLYG (ORCPT ); Wed, 8 Nov 2023 06:24:06 -0500 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A11AF1FCE; Wed, 8 Nov 2023 03:23:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1699442639; x=1730978639; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xf82pqS3tbHruxRFe/9qoNTFiZgXupnu5dstVI/jtoA=; b=n49ZCsyVQdBfWID3w3tnCBi3s065Hx05roSFRRi6awKchxMxny7b+b69 HhnWSkpHhcSGMlybeE8G089x52+EEHlLGVd4LeZs4cL/WS55zTRzr6NwB c5+xOnVsQ4SzsfvX1nxSycy2CZGiLr1+fZZMkgEOY4RWbkrUIPtEyEE9E 8=; X-IronPort-AV: E=Sophos;i="6.03,286,1694736000"; d="scan'208";a="164959696" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2c-m6i4x-94edd59b.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 11:23:48 +0000 Received: from smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2c-m6i4x-94edd59b.us-west-2.amazon.com (Postfix) with ESMTPS id 5BC8140D4F; Wed, 8 Nov 2023 11:23:46 +0000 (UTC) Received: from EX19MTAEUC002.ant.amazon.com [10.0.17.79:21927] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.4.34:2525] with esmtp (Farcaster) id c9b4010e-6bfd-4a71-a295-fab821898611; Wed, 8 Nov 2023 11:23:45 +0000 (UTC) X-Farcaster-Flow-ID: c9b4010e-6bfd-4a71-a295-fab821898611 Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:40 +0000 Received: from dev-dsk-nsaenz-1b-189b39ae.eu-west-1.amazon.com (10.13.235.138) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Wed, 8 Nov 2023 11:23:35 +0000 From: Nicolas Saenz Julienne To: CC: , , , , , , , , , , , , , , , Nicolas Saenz Julienne Subject: [RFC 26/33] KVM: x86: hyper-vsm: Allow setting per-VTL memory attributes Date: Wed, 8 Nov 2023 11:17:59 +0000 Message-ID: <20231108111806.92604-27-nsaenz@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231108111806.92604-1-nsaenz@amazon.com> References: <20231108111806.92604-1-nsaenz@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.13.235.138] X-ClientProxiedBy: EX19D035UWB001.ant.amazon.com (10.13.138.33) To EX19D004EUC001.ant.amazon.com (10.252.51.190) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 08 Nov 2023 03:24:40 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781994808748811777 X-GMAIL-MSGID: 1781994808748811777 Introduce KVM_SET_MEMORY_ATTRIBUTES ioctl support for VTL KVM devices. The attributes are stored in an xarray private to the VTL device. The following memory attributes are supported: - KVM_MEMORY_ATTRIBUTE_READ - KVM_MEMORY_ATTRIBUTE_WRITE - KVM_MEMORY_ATTRIBUTE_EXECUTE - KVM_MEMORY_ATTRIBUTE_NO_ACCESS Although only some combinations are valid, see code comment below. Signed-off-by: Nicolas Saenz Julienne --- arch/x86/kvm/hyperv.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 0d8402dba596..bcace0258af1 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -62,6 +62,10 @@ */ #define HV_EXT_CALL_MAX (HV_EXT_CALL_QUERY_CAPABILITIES + 64) +#define KVM_HV_VTL_ATTRS \ + (KVM_MEMORY_ATTRIBUTE_READ | KVM_MEMORY_ATTRIBUTE_WRITE | \ + KVM_MEMORY_ATTRIBUTE_EXECUTE | KVM_MEMORY_ATTRIBUTE_NO_ACCESS) + static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, bool vcpu_kick); @@ -3025,6 +3029,7 @@ int kvm_vm_ioctl_get_hv_vsm_state(struct kvm *kvm, struct kvm_hv_vsm_state *stat struct kvm_hv_vtl_dev { int vtl; + struct xarray mem_attrs; }; static int kvm_hv_vtl_get_attr(struct kvm_device *dev, @@ -3047,16 +3052,71 @@ static void kvm_hv_vtl_release(struct kvm_device *dev) { struct kvm_hv_vtl_dev *vtl_dev = dev->private; + xa_destroy(&vtl_dev->mem_attrs); kfree(vtl_dev); kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */ } +/* + * The TLFS lists the valid memory protection combinations (15.9.3): + * - No access + * - Read-only, no execute + * - Read-only, execute + * - Read/write, no execute + * - Read/write, execute + */ +static bool kvm_hv_validate_vtl_mem_attributes(struct kvm_memory_attributes *attrs) +{ + u64 attr = attrs->attributes; + + if (attr & ~KVM_HV_VTL_ATTRS) + return false; + + if (attr == KVM_MEMORY_ATTRIBUTE_NO_ACCESS) + return true; + + if (!(attr & KVM_MEMORY_ATTRIBUTE_READ)) + return false; + + return true; +} + +static long kvm_hv_vtl_ioctl(struct kvm_device *dev, unsigned int ioctl, + unsigned long arg) +{ + switch (ioctl) { + case KVM_SET_MEMORY_ATTRIBUTES: { + struct kvm_hv_vtl_dev *vtl_dev = dev->private; + struct kvm_memory_attributes attrs; + int r; + + if (copy_from_user(&attrs, (void __user *)arg, sizeof(attrs))) + return -EFAULT; + + r = -EINVAL; + if (!kvm_hv_validate_vtl_mem_attributes(&attrs)) + return r; + + r = kvm_ioctl_set_mem_attributes(dev->kvm, &vtl_dev->mem_attrs, + KVM_HV_VTL_ATTRS, &attrs); + if (r) + return r; + break; + } + default: + return -ENOTTY; + } + + return 0; +} + static int kvm_hv_vtl_create(struct kvm_device *dev, u32 type); static struct kvm_device_ops kvm_hv_vtl_ops = { .name = "kvm-hv-vtl", .create = kvm_hv_vtl_create, .release = kvm_hv_vtl_release, + .ioctl = kvm_hv_vtl_ioctl, .get_attr = kvm_hv_vtl_get_attr, }; @@ -3076,6 +3136,7 @@ static int kvm_hv_vtl_create(struct kvm_device *dev, u32 type) vtl++; vtl_dev->vtl = vtl; + xa_init(&vtl_dev->mem_attrs); dev->private = vtl_dev; return 0;