Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2946529pxb; Thu, 10 Feb 2022 08:54:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJzqXdLQP3IKb0vF3byEsDXJ5dV/2OMonavIjNt8yExhD0kJ9JFOFdI055DjD1PmOm41BcBD X-Received: by 2002:a17:907:16a6:: with SMTP id hc38mr7404121ejc.291.1644512051128; Thu, 10 Feb 2022 08:54:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644512051; cv=none; d=google.com; s=arc-20160816; b=iZkyD4ILLFWSVrJbJi/yHYLoVHIX6ix7qSuhZhaRJCxaSqLateJFb+eejPb62kch9d 3JJsEor14fZldqJs1FY88eEZUoyQR7YISTCs7UeenUqtRjfDXfN5RFThO+F8gjW858tZ jlvZEHQGhCim7zaqpIwO3e6tgQHo8EjqXpbfWrSJPJ/eCPlNZZ06oS6tODlhVIOU8dOr s0qFVKWKvV3H8PygYJItKiOoEERuztEg7YyhdBk4fxJWRFhBHZVLu3UhOsfVC+u6VykQ N1IQzlr89SYV8nZ8B04eTF+XhcAEeTF0zR5NJt7yVn956BuPl8HGd1Vd/J2pAn8m4v+P khkA== 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 :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=xL0Fc6Y3WPKoozada2yiwmnyb5RG6hYBAv2pgHAjv0I=; b=01Hc8ITj0hkTTxmwNZNrjCvJ6TY1qNKCiR0nYhhAWdO0eX6kQZm1DgpgCt1+t59EKd lyZEMAY/v7+iZdEFCRUk1bbo1qjKPJywZbynDiWVO92rLtrZ/QGwFr0NTSQrBz6tENKV SJQ04u7hBAvSiUrkibc7LUIvIqXJxNlep6KRHByvnuHbAevZ1Af/pi1UxxOlLYNEKCW3 B5JsFtspSnWswE+FYqsk2U5HO9+KCT/AB2kDOhWpMubnko3Q/4KSzbNCCTFArnUVJTYi P132LOVw946kZjhsjsDSmPFfGmADSrd+xgglWepJdy3OlwUiceUVlR6/wMO8jTKlpKoL Wcrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=akRrpZej; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds14si14947335ejc.57.2022.02.10.08.53.42; Thu, 10 Feb 2022 08:54:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=akRrpZej; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244171AbiBJQ0K (ORCPT + 99 others); Thu, 10 Feb 2022 11:26:10 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:41482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239459AbiBJQ0H (ORCPT ); Thu, 10 Feb 2022 11:26:07 -0500 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96927C26; Thu, 10 Feb 2022 08:26:05 -0800 (PST) Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21AEwuHT017096; Thu, 10 Feb 2022 16:26:03 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=date : from : to : cc : subject : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=pp1; bh=xL0Fc6Y3WPKoozada2yiwmnyb5RG6hYBAv2pgHAjv0I=; b=akRrpZejDL0EAH6FjplSNeA3HtKaMWIU7XB9Gs1uP4z+TLSAlLgcCzxvifPCdDAl7W+9 yM1KFiDH3se0e+KWcUm6hVzjjyU2xc0YxG/lWuNVaiWGw24uSHFbk3oC6tXhPM3xMJuZ PFb73DAnbvd6Ie01qlY0oQ+gdhwlJXQEAOsgrfcMY4aUaj3XZ+iRvr1OBKYixae3mx1O b8EFgcgxYTkWEqvL8QVXO6ONMRh6csUI9mDdji5qwDmpD/ttpdCAK8YTF6c7rqlvWsfe 5SHymG+2MF2M/4+H1Nrg5HSaOZZk/B3Qwd0BSv94i8EaioQEXKxUgcRFuHZmuRkTQpDo Ug== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 3e54xutbf2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 10 Feb 2022 16:26:03 +0000 Received: from m0098420.ppops.net (m0098420.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 21AFUQJS031296; Thu, 10 Feb 2022 16:26:02 GMT Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0b-001b2d01.pphosted.com with ESMTP id 3e54xutbek-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 10 Feb 2022 16:26:02 +0000 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 21AG7Srr012792; Thu, 10 Feb 2022 16:26:01 GMT Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by ppma01fra.de.ibm.com with ESMTP id 3e1gv9ywu4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 10 Feb 2022 16:26:00 +0000 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 21AGPv5v46727526 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Feb 2022 16:25:57 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8AF274C046; Thu, 10 Feb 2022 16:25:57 +0000 (GMT) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EC2B34C040; Thu, 10 Feb 2022 16:25:56 +0000 (GMT) Received: from p-imbrenda (unknown [9.145.14.149]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 10 Feb 2022 16:25:56 +0000 (GMT) Date: Thu, 10 Feb 2022 17:25:55 +0100 From: Claudio Imbrenda To: Christian Borntraeger Cc: Janis Schoetterl-Glausch , Heiko Carstens , Janosch Frank , Alexander Gordeev , David Hildenbrand , Jonathan Corbet , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, Paolo Bonzini , Sven Schnelle , Vasily Gorbik Subject: Re: [PATCH v3 06/10] KVM: s390: Add vm IOCTL for key checked guest absolute memory access Message-ID: <20220210172555.43141947@p-imbrenda> In-Reply-To: <023fcd5d-4ee5-ad0e-ae7d-53b181e31a4e@linux.ibm.com> References: <20220209170422.1910690-1-scgl@linux.ibm.com> <20220209170422.1910690-7-scgl@linux.ibm.com> <023fcd5d-4ee5-ad0e-ae7d-53b181e31a4e@linux.ibm.com> Organization: IBM X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: SDfxbmOMzC78WX1-gllTL1p7SQK76M7n X-Proofpoint-ORIG-GUID: 1FBftRU3S__ZJ_zoIE_f7FUQ4hYOI2qk X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-02-10_07,2022-02-09_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 impostorscore=0 phishscore=0 mlxscore=0 bulkscore=0 mlxlogscore=999 lowpriorityscore=0 priorityscore=1501 suspectscore=0 malwarescore=0 clxscore=1011 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202100085 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 10 Feb 2022 15:40:03 +0100 Christian Borntraeger wrote: > Am 09.02.22 um 18:04 schrieb Janis Schoetterl-Glausch: > > Channel I/O honors storage keys and is performed on absolute memory. > > For I/O emulation user space therefore needs to be able to do key > > checked accesses. > > The vm IOCTL supports read/write accesses, as well as checking > > if an access would succeed. > > Unlike relying on KVM_S390_GET_SKEYS for key checking would, > > the vm IOCTL performs the check in lockstep with the read or write, > > by, ultimately, mapping the access to move instructions that > > support key protection checking with a supplied key. > > Fetch and storage protection override are not applicable to absolute > > accesses and so are not applied as they are when using the vcpu memop. > > > > Signed-off-by: Janis Schoetterl-Glausch > > Acked-by: Janosch Frank > > same comment that you did for the vcpu part (default key) > with that. > > Reviewed-by: Christian Borntraeger I guess @linux.ibm.com ? > > --- > > arch/s390/kvm/gaccess.c | 72 ++++++++++++++++++++++++++++++++++++ > > arch/s390/kvm/gaccess.h | 6 +++ > > arch/s390/kvm/kvm-s390.c | 79 ++++++++++++++++++++++++++++++++++++++++ > > include/uapi/linux/kvm.h | 2 + > > 4 files changed, 159 insertions(+) > > > > diff --git a/arch/s390/kvm/gaccess.c b/arch/s390/kvm/gaccess.c > > index 37838f637707..d53a183c2005 100644 > > --- a/arch/s390/kvm/gaccess.c > > +++ b/arch/s390/kvm/gaccess.c > > @@ -795,6 +795,35 @@ static int low_address_protection_enabled(struct kvm_vcpu *vcpu, > > return 1; > > } > > > > +static int vm_check_access_key(struct kvm *kvm, u8 access_key, > > + enum gacc_mode mode, gpa_t gpa) > > +{ > > + u8 storage_key, access_control; > > + bool fetch_protected; > > + unsigned long hva; > > + int r; > > + > > + if (access_key == 0) > > + return 0; > > + > > + hva = gfn_to_hva(kvm, gpa_to_gfn(gpa)); > > + if (kvm_is_error_hva(hva)) > > + return PGM_ADDRESSING; > > + > > + mmap_read_lock(current->mm); > > + r = get_guest_storage_key(current->mm, hva, &storage_key); > > + mmap_read_unlock(current->mm); > > + if (r) > > + return r; > > + access_control = FIELD_GET(_PAGE_ACC_BITS, storage_key); > > + if (access_control == access_key) > > + return 0; > > + fetch_protected = storage_key & _PAGE_FP_BIT; > > + if ((mode == GACC_FETCH || mode == GACC_IFETCH) && !fetch_protected) > > + return 0; > > + return PGM_PROTECTION; > > +} > > + > > static bool fetch_prot_override_applicable(struct kvm_vcpu *vcpu, enum gacc_mode mode, > > union asce asce) > > { > > @@ -994,6 +1023,26 @@ access_guest_page_with_key(struct kvm *kvm, enum gacc_mode mode, gpa_t gpa, > > return 0; > > } > > > > +int access_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, void *data, > > + unsigned long len, enum gacc_mode mode, u8 access_key) > > +{ > > + int offset = offset_in_page(gpa); > > + int fragment_len; > > + int rc; > > + > > + while (min(PAGE_SIZE - offset, len) > 0) { > > + fragment_len = min(PAGE_SIZE - offset, len); > > + rc = access_guest_page_with_key(kvm, mode, gpa, data, fragment_len, access_key); > > + if (rc) > > + return rc; > > + offset = 0; > > + len -= fragment_len; > > + data += fragment_len; > > + gpa += fragment_len; > > + } > > + return 0; > > +} > > + > > int access_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, > > void *data, unsigned long len, enum gacc_mode mode, > > u8 access_key) > > @@ -1144,6 +1193,29 @@ int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar, > > return rc; > > } > > > > +/** > > + * check_gpa_range - test a range of guest physical addresses for accessibility > > + * @kvm: virtual machine instance > > + * @gpa: guest physical address > > + * @length: length of test range > > + * @mode: access mode to test, relevant for storage keys > > + * @access_key: access key to mach the storage keys with > > + */ > > +int check_gpa_range(struct kvm *kvm, unsigned long gpa, unsigned long length, > > + enum gacc_mode mode, u8 access_key) > > +{ > > + unsigned int fragment_len; > > + int rc = 0; > > + > > + while (length && !rc) { > > + fragment_len = min(PAGE_SIZE - offset_in_page(gpa), length); > > + rc = vm_check_access_key(kvm, access_key, mode, gpa); > > + length -= fragment_len; > > + gpa += fragment_len; > > + } > > + return rc; > > +} > > + > > /** > > * kvm_s390_check_low_addr_prot_real - check for low-address protection > > * @vcpu: virtual cpu > > diff --git a/arch/s390/kvm/gaccess.h b/arch/s390/kvm/gaccess.h > > index c5f2e7311b17..1124ff282012 100644 > > --- a/arch/s390/kvm/gaccess.h > > +++ b/arch/s390/kvm/gaccess.h > > @@ -193,6 +193,12 @@ int guest_translate_address_with_key(struct kvm_vcpu *vcpu, unsigned long gva, u > > int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar, > > unsigned long length, enum gacc_mode mode, u8 access_key); > > > > +int check_gpa_range(struct kvm *kvm, unsigned long gpa, unsigned long length, > > + enum gacc_mode mode, u8 access_key); > > + > > +int access_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, void *data, > > + unsigned long len, enum gacc_mode mode, u8 access_key); > > + > > int access_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, > > void *data, unsigned long len, enum gacc_mode mode, > > u8 access_key); > > diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c > > index 85763ec7bc60..7ee3d2e8ecf2 100644 > > --- a/arch/s390/kvm/kvm-s390.c > > +++ b/arch/s390/kvm/kvm-s390.c > > @@ -2365,6 +2365,76 @@ static bool access_key_invalid(u8 access_key) > > return access_key > 0xf; > > } > > > > +static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) > > +{ > > + void __user *uaddr = (void __user *)mop->buf; > > + u64 supported_flags; > > + void *tmpbuf = NULL; > > + int r, srcu_idx; > > + > > + supported_flags = KVM_S390_MEMOP_F_SKEY_PROTECTION > > + | KVM_S390_MEMOP_F_CHECK_ONLY; > > + if (mop->flags & ~supported_flags) > > + return -EINVAL; > > + if (mop->size > MEM_OP_MAX_SIZE) > > + return -E2BIG; > > + if (kvm_s390_pv_is_protected(kvm)) > > + return -EINVAL; > > + if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { > > + if (access_key_invalid(mop->key)) > > + return -EINVAL; > > + } > > + if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { > > + tmpbuf = vmalloc(mop->size); > > + if (!tmpbuf) > > + return -ENOMEM; > > + } > > + > > + srcu_idx = srcu_read_lock(&kvm->srcu); > > + > > + if (kvm_is_error_gpa(kvm, mop->gaddr)) { > > + r = PGM_ADDRESSING; > > + goto out_unlock; > > + } > > + > > + switch (mop->op) { > > + case KVM_S390_MEMOP_ABSOLUTE_READ: { > > + if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { > > + r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_FETCH, mop->key); > > + } else { > > + r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, > > + mop->size, GACC_FETCH, mop->key); > > + if (r == 0) { > > + if (copy_to_user(uaddr, tmpbuf, mop->size)) > > + r = -EFAULT; > > + } > > + } > > + break; > > + } > > + case KVM_S390_MEMOP_ABSOLUTE_WRITE: { > > + if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { > > + r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_STORE, mop->key); > > + } else { > > + if (copy_from_user(tmpbuf, uaddr, mop->size)) { > > + r = -EFAULT; > > + break; > > + } > > + r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, > > + mop->size, GACC_STORE, mop->key); > > + } > > + break; > > + } > > + default: > > + r = -EINVAL; > > + } > > + > > +out_unlock: > > + srcu_read_unlock(&kvm->srcu, srcu_idx); > > + > > + vfree(tmpbuf); > > + return r; > > +} > > + > > long kvm_arch_vm_ioctl(struct file *filp, > > unsigned int ioctl, unsigned long arg) > > { > > @@ -2489,6 +2559,15 @@ long kvm_arch_vm_ioctl(struct file *filp, > > } > > break; > > } > > + case KVM_S390_MEM_OP: { > > + struct kvm_s390_mem_op mem_op; > > + > > + if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) > > + r = kvm_s390_vm_mem_op(kvm, &mem_op); > > + else > > + r = -EFAULT; > > + break; > > + } > > default: > > r = -ENOTTY; > > } > > diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h > > index 44558cf4c52e..b83c9286e017 100644 > > --- a/include/uapi/linux/kvm.h > > +++ b/include/uapi/linux/kvm.h > > @@ -575,6 +575,8 @@ struct kvm_s390_mem_op { > > #define KVM_S390_MEMOP_LOGICAL_WRITE 1 > > #define KVM_S390_MEMOP_SIDA_READ 2 > > #define KVM_S390_MEMOP_SIDA_WRITE 3 > > +#define KVM_S390_MEMOP_ABSOLUTE_READ 4 > > +#define KVM_S390_MEMOP_ABSOLUTE_WRITE 5 > > /* flags for kvm_s390_mem_op->flags */ > > #define KVM_S390_MEMOP_F_CHECK_ONLY (1ULL << 0) > > #define KVM_S390_MEMOP_F_INJECT_EXCEPTION (1ULL << 1)