Received: by 10.213.65.68 with SMTP id h4csp2195972imn; Sun, 8 Apr 2018 22:13:54 -0700 (PDT) X-Google-Smtp-Source: AIpwx49vYVd+lkm9BhDTQGSrfZIQJRmz0EKVMuiDSgtJ7NvrffN7lFKF5SZWvAyxBnsHO5vgHoqF X-Received: by 10.99.64.65 with SMTP id n62mr23800635pga.259.1523250834148; Sun, 08 Apr 2018 22:13:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523250834; cv=none; d=google.com; s=arc-20160816; b=nZ0uCT3+QndQts6EWq6PP8uT4VWH3KmbUbyfIYu95/VTca8SKkpZYtUEpZ0/zxAJDO xUfffMfR/6Ovw63kMhbEbcZLgxRCHD70Q6PbZHJ7IHJKFXZmXW6NrPqnfCMYw/1+dK5T m5cBpHAqElAmikioHqf2fwkdVcb60/AzdMvRKmdIisSg3/uVkCn3pxdxEBcsu+K2OTce CMAv4H+MnVaC/vYxBNVLTgv3a2QHfHcmqHtHWDQEAJd97BZP1kT0HtHJm6iiXYl7hGpq QHHbtzFLt/iMe3T1M+X7FCNKJlh9BoE5og7Fsg4LTMfWswO1Y8TiMUvc/r1YITZ6IwKs tLFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=X6y/opAYAOujZEy4aW+1UBKbbUHe/TAZQ1gH5WdyxyY=; b=XxxYy1Ar/uq3x+pngp7qZnPbMBcyQwbjJdajP1SEWEaUti65BWhaDu7nw78pB1M4AV hZHynqk+l2UI4Cvl4J/jedv/anh8YXcvZq1IiGa7ZA/oUHc2dyEzfnsm6GFFsuRkSLuq GG1BG/0IkmBPovpc6uiIXkh5UCaof8+wYyhJd0OUJoTM7XsUFIw2cdBtRDQN9+L6yWm4 fqSDz/8FEwFOeS2OJQVwW+9zkk4/b5hBxoVNE3m20WrUsrYx83Y7uVrAS8t+49+N0QC8 RJQovAfdyI0VNko1RJziURXlg6z8qXMhK62LUJ4krfDQExnF5By1q7L2kN90GflGmbG9 s4iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=IHdhgsDK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c5-v6si14184113plo.461.2018.04.08.22.13.17; Sun, 08 Apr 2018 22:13:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=IHdhgsDK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753841AbeDIASw (ORCPT + 99 others); Sun, 8 Apr 2018 20:18:52 -0400 Received: from mail-bn3nam01on0092.outbound.protection.outlook.com ([104.47.33.92]:62923 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753729AbeDIASq (ORCPT ); Sun, 8 Apr 2018 20:18:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=X6y/opAYAOujZEy4aW+1UBKbbUHe/TAZQ1gH5WdyxyY=; b=IHdhgsDKoJhkaV0tr1cz1+q3YY/YNqCw+8uAzIxA5pkKA1f/uJgIrZRN63rBhf6ggujuoSW/N7WxZpPQizvrwkLQ+IY2nfe8BdKU7wLWHl5IGXl+qpNsKkqY/sIzsz845cO3C8OwTP5mqciYRiK7GDaqZvIotooyHE299qDt1R0= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB1128.namprd21.prod.outlook.com (52.132.133.20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.696.0; Mon, 9 Apr 2018 00:18:39 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059%2]) with mapi id 15.20.0696.003; Mon, 9 Apr 2018 00:18:39 +0000 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: David Hildenbrand , Christian Borntraeger , Sasha Levin Subject: [PATCH AUTOSEL for 4.15 082/189] KVM: s390: vsie: use READ_ONCE to access some SCB fields Thread-Topic: [PATCH AUTOSEL for 4.15 082/189] KVM: s390: vsie: use READ_ONCE to access some SCB fields Thread-Index: AQHTz5gxW2wJA8Z0TUyOo1OcUKXVqQ== Date: Mon, 9 Apr 2018 00:17:49 +0000 Message-ID: <20180409001637.162453-82-alexander.levin@microsoft.com> References: <20180409001637.162453-1-alexander.levin@microsoft.com> In-Reply-To: <20180409001637.162453-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB1128;7:jMj6V8b0Ga23tq9FoYOWYeZyE3LQh0gWSACs8LRxpyWzTc6LgP06SvFi/YUw2qUMxyDtoYaC95GHkbmF5MMSV7LEElWukF33MfHjOZsOjixQ3VJ6hJlOeDv1vIuZzCdDuCnJoI+KajXD5Q6n28E65ahInI9ZHzPV2UJQDmR9K2ij1LyDtngS+ji5De+DlFX3DsX4ntDNGzAw+cB3QVfgGR43rsuAiu1cjoSRjkxTzvB3RYoBoTdcxEMq6D7jfsab;20:/TP9eYlHiVzX3F0jgIJ1VvHWol723J7Lf6JvIKk10BJWfGMAISnWvfgoh+WZiM9SKuGrJboPiZml1DmbiDqdBrf/rN7yWA+cBzbNo72v943VxbFHqjjL8wf4ULm1pWZpbATborRfNtSZo7eefeot1JFbFTh0AUOAZPfDkjS+ZSs= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: a2863a27-75e0-4577-929a-08d59daf71db x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB1128; x-ms-traffictypediagnostic: DM5PR2101MB1128: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(190383065149520)(104084551191319); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(8121501046)(5005006)(3231221)(944501327)(52105095)(3002001)(93006095)(93001095)(10201501046)(6055026)(61426038)(61427038)(6041310)(20161123560045)(20161123564045)(20161123558120)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:DM5PR2101MB1128;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB1128; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(346002)(39860400002)(396003)(376002)(366004)(39380400002)(199004)(189003)(66066001)(86362001)(59450400001)(3660700001)(3280700002)(2900100001)(25786009)(36756003)(6506007)(76176011)(2906002)(6486002)(86612001)(99286004)(102836004)(1076002)(7736002)(575784001)(97736004)(2616005)(446003)(486006)(11346002)(476003)(26005)(305945005)(6666003)(6512007)(4326008)(107886003)(478600001)(6436002)(5660300001)(10290500003)(5250100002)(22452003)(316002)(105586002)(186003)(8676002)(110136005)(54906003)(72206003)(53936002)(2501003)(68736007)(14454004)(6116002)(8936002)(3846002)(81156014)(10090500001)(106356001)(81166006)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB1128;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: TT6AP1VJT7ArlhMNVh1GNM5UYYlMs8ZDJcRNBCZGK5BNV6gJ7m5qekmgeEmX2tPgAUKLwyc+xo/G0QZgv4yjspa+6JOKdnJM8bnNDVGuvtNMzudDjcRdgAhd8An831p7yph4lhwd/yuZykbZgyTxHdifyctKqv7vCPK5QpSG+Pledc92pkSuDxPSsU+n3d0IflOumZPpnw2i/VSGrS24ljZOlbZVDbcfZkh/U8VBvzHVIxu952HX43sMDc9piMFJez933ZSBH6LLn3fuE3ZVp96dELJT3W016CWfaN1P+v5WvbdixZPKYUGMT1dxLnGjHLtf/Ddf8+pLeLfKx4iAFAE36kb7jzQXMFcDmRYUU/FCv2fsSV+hyRFlr1FSd9DXyQBbgHwvTMLvobeCvyLkj7No49cQwvNDmT4j+bkZQt8= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: a2863a27-75e0-4577-929a-08d59daf71db X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:17:49.8827 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB1128 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Hildenbrand [ Upstream commit b3ecd4aa8632a86428605ab73393d14779019d82 ] Another VCPU might try to modify the SCB while we are creating the shadow SCB. In general this is no problem - unless the compiler decides to not load values once, but e.g. twice. For us, this is only relevant when checking/working with such values. E.g. the prefix value, the mso, state of transactional execution and addresses of satellite blocks. E.g. if we blindly forward values (e.g. general purpose registers or execution controls after masking), we don't care. Leaving unpin_blocks() untouched for now, will handle it separately. The worst thing right now that I can see would be a missed prefix un/remap (mso, prefix, tx) or using wrong guest addresses. Nothing critical, but let's try to avoid unpredictable behavior. Signed-off-by: David Hildenbrand Message-Id: <20180116171526.12343-2-david@redhat.com> Reviewed-by: Christian Borntraeger Acked-by: Cornelia Huck Signed-off-by: Christian Borntraeger Signed-off-by: Sasha Levin --- arch/s390/kvm/vsie.c | 50 +++++++++++++++++++++++++++++++-----------------= -- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/arch/s390/kvm/vsie.c b/arch/s390/kvm/vsie.c index 751348348477..11fc525e6e7f 100644 --- a/arch/s390/kvm/vsie.c +++ b/arch/s390/kvm/vsie.c @@ -28,7 +28,11 @@ struct vsie_page { * the same offset as that in struct sie_page! */ struct mcck_volatile_info mcck_info; /* 0x0200 */ - /* the pinned originial scb */ + /* + * The pinned original scb. Be aware that other VCPUs can modify + * it while we read from it. Values that are used for conditions or + * are reused conditionally, should be accessed via READ_ONCE. + */ struct kvm_s390_sie_block *scb_o; /* 0x0218 */ /* the shadow gmap in use by the vsie_page */ struct gmap *gmap; /* 0x0220 */ @@ -140,12 +144,13 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct= vsie_page *vsie_page) { struct kvm_s390_sie_block *scb_s =3D &vsie_page->scb_s; struct kvm_s390_sie_block *scb_o =3D vsie_page->scb_o; - u32 crycb_addr =3D scb_o->crycbd & 0x7ffffff8U; + const uint32_t crycbd_o =3D READ_ONCE(scb_o->crycbd); + const u32 crycb_addr =3D crycbd_o & 0x7ffffff8U; unsigned long *b1, *b2; u8 ecb3_flags; =20 scb_s->crycbd =3D 0; - if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1)) + if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1)) return 0; /* format-1 is supported with message-security-assist extension 3 */ if (!test_kvm_facility(vcpu->kvm, 76)) @@ -183,12 +188,15 @@ static void prepare_ibc(struct kvm_vcpu *vcpu, struct= vsie_page *vsie_page) { struct kvm_s390_sie_block *scb_s =3D &vsie_page->scb_s; struct kvm_s390_sie_block *scb_o =3D vsie_page->scb_o; + /* READ_ONCE does not work on bitfields - use a temporary variable */ + const uint32_t __new_ibc =3D scb_o->ibc; + const uint32_t new_ibc =3D READ_ONCE(__new_ibc) & 0x0fffU; __u64 min_ibc =3D (sclp.ibc >> 16) & 0x0fffU; =20 scb_s->ibc =3D 0; /* ibc installed in g2 and requested for g3 */ - if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) { - scb_s->ibc =3D scb_o->ibc & 0x0fffU; + if (vcpu->kvm->arch.model.ibc && new_ibc) { + scb_s->ibc =3D new_ibc; /* takte care of the minimum ibc level of the machine */ if (scb_s->ibc < min_ibc) scb_s->ibc =3D min_ibc; @@ -259,6 +267,10 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct vs= ie_page *vsie_page) { struct kvm_s390_sie_block *scb_o =3D vsie_page->scb_o; struct kvm_s390_sie_block *scb_s =3D &vsie_page->scb_s; + /* READ_ONCE does not work on bitfields - use a temporary variable */ + const uint32_t __new_prefix =3D scb_o->prefix; + const uint32_t new_prefix =3D READ_ONCE(__new_prefix); + const bool wants_tx =3D READ_ONCE(scb_o->ecb) & ECB_TE; bool had_tx =3D scb_s->ecb & ECB_TE; unsigned long new_mso =3D 0; int rc; @@ -306,14 +318,14 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct v= sie_page *vsie_page) scb_s->icpua =3D scb_o->icpua; =20 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM)) - new_mso =3D scb_o->mso & 0xfffffffffff00000UL; + new_mso =3D READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL; /* if the hva of the prefix changes, we have to remap the prefix */ - if (scb_s->mso !=3D new_mso || scb_s->prefix !=3D scb_o->prefix) + if (scb_s->mso !=3D new_mso || scb_s->prefix !=3D new_prefix) prefix_unmapped(vsie_page); /* SIE will do mso/msl validity and exception checks for us */ scb_s->msl =3D scb_o->msl & 0xfffffffffff00000UL; scb_s->mso =3D new_mso; - scb_s->prefix =3D scb_o->prefix; + scb_s->prefix =3D new_prefix; =20 /* We have to definetly flush the tlb if this scb never ran */ if (scb_s->ihcpu !=3D 0xffffU) @@ -325,11 +337,11 @@ static int shadow_scb(struct kvm_vcpu *vcpu, struct v= sie_page *vsie_page) if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP)) scb_s->ecb |=3D scb_o->ecb & ECB_HOSTPROTINT; /* transactional execution */ - if (test_kvm_facility(vcpu->kvm, 73)) { + if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) { /* remap the prefix is tx is toggled on */ - if ((scb_o->ecb & ECB_TE) && !had_tx) + if (!had_tx) prefix_unmapped(vsie_page); - scb_s->ecb |=3D scb_o->ecb & ECB_TE; + scb_s->ecb |=3D ECB_TE; } /* branch prediction */ if (test_kvm_facility(vcpu->kvm, 82)) @@ -539,9 +551,9 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsi= e_page *vsie_page) gpa_t gpa; int rc =3D 0; =20 - gpa =3D scb_o->scaol & ~0xfUL; + gpa =3D READ_ONCE(scb_o->scaol) & ~0xfUL; if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO)) - gpa |=3D (u64) scb_o->scaoh << 32; + gpa |=3D (u64) READ_ONCE(scb_o->scaoh) << 32; if (gpa) { if (!(gpa & ~0x1fffUL)) rc =3D set_validity_icpt(scb_s, 0x0038U); @@ -561,7 +573,7 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsi= e_page *vsie_page) scb_s->scaol =3D (u32)(u64)hpa; } =20 - gpa =3D scb_o->itdba & ~0xffUL; + gpa =3D READ_ONCE(scb_o->itdba) & ~0xffUL; if (gpa && (scb_s->ecb & ECB_TE)) { if (!(gpa & ~0x1fffU)) { rc =3D set_validity_icpt(scb_s, 0x0080U); @@ -576,7 +588,7 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsi= e_page *vsie_page) scb_s->itdba =3D hpa; } =20 - gpa =3D scb_o->gvrd & ~0x1ffUL; + gpa =3D READ_ONCE(scb_o->gvrd) & ~0x1ffUL; if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) { if (!(gpa & ~0x1fffUL)) { rc =3D set_validity_icpt(scb_s, 0x1310U); @@ -594,7 +606,7 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsi= e_page *vsie_page) scb_s->gvrd =3D hpa; } =20 - gpa =3D scb_o->riccbd & ~0x3fUL; + gpa =3D READ_ONCE(scb_o->riccbd) & ~0x3fUL; if (gpa && (scb_s->ecb3 & ECB3_RI)) { if (!(gpa & ~0x1fffUL)) { rc =3D set_validity_icpt(scb_s, 0x0043U); @@ -612,8 +624,8 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsi= e_page *vsie_page) if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) { unsigned long sdnxc; =20 - gpa =3D scb_o->sdnxo & ~0xfUL; - sdnxc =3D scb_o->sdnxo & 0xfUL; + gpa =3D READ_ONCE(scb_o->sdnxo) & ~0xfUL; + sdnxc =3D READ_ONCE(scb_o->sdnxo) & 0xfUL; if (!gpa || !(gpa & ~0x1fffUL)) { rc =3D set_validity_icpt(scb_s, 0x10b0U); goto unpin; @@ -778,7 +790,7 @@ static void retry_vsie_icpt(struct vsie_page *vsie_page= ) static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page= ) { struct kvm_s390_sie_block *scb_s =3D &vsie_page->scb_s; - __u32 fac =3D vsie_page->scb_o->fac & 0x7ffffff8U; + __u32 fac =3D READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U; =20 if (fac && test_kvm_facility(vcpu->kvm, 7)) { retry_vsie_icpt(vsie_page); --=20 2.15.1