Received: by 10.223.176.5 with SMTP id f5csp3416211wra; Mon, 29 Jan 2018 12:52:16 -0800 (PST) X-Google-Smtp-Source: AH8x225WrXqTJn8g+Ddtsw3Q37rmg3Ddkmnc8ume2w2xhZai3wbbQSAf6V2tHjyZf1e1YVHnw80H X-Received: by 10.99.60.88 with SMTP id i24mr22195571pgn.1.1517259135907; Mon, 29 Jan 2018 12:52:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517259135; cv=none; d=google.com; s=arc-20160816; b=zSQ0hpLbhGdTeN+/Eqoh0KjkAQ9F6YI+HaXW1/pLvsKKOjw25T8iJba94XA7Y3Lx2/ LRS4753JR+4bqncOnxztpBDbYK7Jx1956VW9biqA3C9r4hS1spwoO6EWCiJqptT0bqdC 8494fUtHjHl/om4lNkBfNLRBChCSOCHNt/um/7dY4paJnarFYEvW0i6ZSQLpHbOWgWIB R/k3OvX2EHtl90uCzRenxusTlpKnfltn+VguzC85db/WsTgLu7Mi87FtwJWrrKGKSqKW OwUpyCTWiSwaMIwHJcoIWr0Tt2pZYNmWkKxoOQdX8SjcsWtZQjXtSeVlB1IBRrL+u2xu wXFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=OnQ7N9Zih2bKqYhwq6DFshQb5R9azmkkPZ6loyPKf94=; b=g6uRqLVPGrksdNaWLVD5m9DhEwg2Fg95CvTqslJiqJ7imD1IGLYT53wCvgKz75ORHx z0AbTyoxLHG/+cAOzR/WD6RXRIEkk9lh9aDm5NY6oQHWLfJxU+iIfZP9Ak3V3IrojYLj UQTFjMxOO51TOqaag6c4Sz61GdrLJuSyeTHxtTeAwPjxrB60O0FYHiC2e17Wgt7adNC3 9hy7euzr/KRs2PPyz3AwcUvOmvCOhwj38IUkPZG6L4wpVaWiGyJhSN8+cGpTsO8+yv3V MSOZ6loUR3ZOjbIzE4ZL4bzg8cr/KbPTo3im5o+kNgNmwevsHaoumulRSJLiRWDO/e+Z xmcA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b14si175187pgu.764.2018.01.29.12.52.01; Mon, 29 Jan 2018 12:52:15 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753540AbeA2UKm (ORCPT + 99 others); Mon, 29 Jan 2018 15:10:42 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:57682 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753514AbeA2UKj (ORCPT ); Mon, 29 Jan 2018 15:10:39 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id EF8A32FDA; Mon, 29 Jan 2018 13:07:48 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Hildenbrand , Christian Borntraeger , Claudio Imbrenda , Cornelia Huck Subject: [PATCH 4.14 12/71] KVM: s390: add proper locking for CMMA migration bitmap Date: Mon, 29 Jan 2018 13:56:40 +0100 Message-Id: <20180129123828.110726851@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180129123827.271171825@linuxfoundation.org> References: <20180129123827.271171825@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Christian Borntraeger commit 1de1ea7efeb9e8543212210e34518b4049ccd285 upstream. Some parts of the cmma migration bitmap is already protected with the kvm->lock (e.g. the migration start). On the other hand the read of the cmma bits is not protected against a concurrent free, neither is the emulation of the ESSA instruction. Let's extend the locking to all related ioctls by using the slots lock for - kvm_s390_vm_start_migration - kvm_s390_vm_stop_migration - kvm_s390_set_cmma_bits - kvm_s390_get_cmma_bits In addition to that, we use synchronize_srcu before freeing the migration structure as all users hold kvm->srcu for read. (e.g. the ESSA handler). Reported-by: David Hildenbrand Signed-off-by: Christian Borntraeger Fixes: 190df4a212a7 (KVM: s390: CMMA tracking, ESSA emulation, migration mode) Reviewed-by: Claudio Imbrenda Reviewed-by: David Hildenbrand Reviewed-by: Cornelia Huck Signed-off-by: Greg Kroah-Hartman --- arch/s390/kvm/kvm-s390.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) --- a/arch/s390/kvm/kvm-s390.c +++ b/arch/s390/kvm/kvm-s390.c @@ -768,7 +768,7 @@ static void kvm_s390_sync_request_broadc /* * Must be called with kvm->srcu held to avoid races on memslots, and with - * kvm->lock to avoid races with ourselves and kvm_s390_vm_stop_migration. + * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. */ static int kvm_s390_vm_start_migration(struct kvm *kvm) { @@ -824,7 +824,7 @@ static int kvm_s390_vm_start_migration(s } /* - * Must be called with kvm->lock to avoid races with ourselves and + * Must be called with kvm->slots_lock to avoid races with ourselves and * kvm_s390_vm_start_migration. */ static int kvm_s390_vm_stop_migration(struct kvm *kvm) @@ -839,6 +839,8 @@ static int kvm_s390_vm_stop_migration(st if (kvm->arch.use_cmma) { kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); + /* We have to wait for the essa emulation to finish */ + synchronize_srcu(&kvm->srcu); vfree(mgs->pgste_bitmap); } kfree(mgs); @@ -848,14 +850,12 @@ static int kvm_s390_vm_stop_migration(st static int kvm_s390_vm_set_migration(struct kvm *kvm, struct kvm_device_attr *attr) { - int idx, res = -ENXIO; + int res = -ENXIO; - mutex_lock(&kvm->lock); + mutex_lock(&kvm->slots_lock); switch (attr->attr) { case KVM_S390_VM_MIGRATION_START: - idx = srcu_read_lock(&kvm->srcu); res = kvm_s390_vm_start_migration(kvm); - srcu_read_unlock(&kvm->srcu, idx); break; case KVM_S390_VM_MIGRATION_STOP: res = kvm_s390_vm_stop_migration(kvm); @@ -863,7 +863,7 @@ static int kvm_s390_vm_set_migration(str default: break; } - mutex_unlock(&kvm->lock); + mutex_unlock(&kvm->slots_lock); return res; } @@ -1753,7 +1753,9 @@ long kvm_arch_vm_ioctl(struct file *filp r = -EFAULT; if (copy_from_user(&args, argp, sizeof(args))) break; + mutex_lock(&kvm->slots_lock); r = kvm_s390_get_cmma_bits(kvm, &args); + mutex_unlock(&kvm->slots_lock); if (!r) { r = copy_to_user(argp, &args, sizeof(args)); if (r) @@ -1767,7 +1769,9 @@ long kvm_arch_vm_ioctl(struct file *filp r = -EFAULT; if (copy_from_user(&args, argp, sizeof(args))) break; + mutex_lock(&kvm->slots_lock); r = kvm_s390_set_cmma_bits(kvm, &args); + mutex_unlock(&kvm->slots_lock); break; } default: