Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2234063ybi; Thu, 20 Jun 2019 11:19:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqzSAi0GBna5L++L6BNORV7hgB2seBB/xh8us5EbDpn/sTJSoIDuAnK0fsqk/si0e1UDfLoe X-Received: by 2002:a65:4c4a:: with SMTP id l10mr13774860pgr.231.1561054757901; Thu, 20 Jun 2019 11:19:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561054757; cv=none; d=google.com; s=arc-20160816; b=VqLZLa9i49aGkxhna0CmMm0rSs6AnJjYS4dgmg6iIeo4Y40eycCoRICqO7lAGojLfM S91bBf59tLSDiyEMVThDiPkkTyS3P+MYm3ZY+nB+a93NzTJH75vX8cScf1rZMHNJJby5 2jRwh6mg/qlL/5ceE30IxHcfmAOERYoK7Q3RyhhHGUeD3BIsjM/10Q0wiEOvL2QFpgiD o3ZcncvevJfVm2ecLsIAXjGZDjrPKHTucWjBQVc3mzr1BGqYZU4aG0ROVKa6XvCnXcLz IYejZe9FvlggSzxB8kEeA26BEcvgN2pKPquVZNuWxcU8qzey0oBdqwtaZnVFLzu5w13O qR5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UyHKJVcl4fdGdns5jjhDJl+dcaKYLaxTcwlku/naGV0=; b=fqlV6oR96LksHRxtIybXsvMoUhRdGf0FOB1KSri5Ovgqn4ja8yj4hVIKACA8EH3hGN BLShnmhlAphOunTQ1nA1wSwmLuYwfPKr3xx1vTFTitMMTfRW6SLyQyIzF177JVokBz+7 3KqWxOHvtV/iQDf19d6vwYuTBqdhy9xqUruY+GIDmSlJ1Zj5lnmyUhp9jF3irpqU0WU6 Ik3Bv41WzjHJ/1Pq5LBltHy+eF5RHLFV3xbGBPxFAu9QWS0F19dKjboWEiH3AHgZGrNN gMu1yWX+UYB6L1U3ljbf27tbTTJeFgWZUqpatuG6aeZfIc2V/ucGBKE965+lTMYJXXt0 5YSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Z4FyVxNu; 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 h11si164159pgq.170.2019.06.20.11.19.02; Thu, 20 Jun 2019 11:19:17 -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=@kernel.org header.s=default header.b=Z4FyVxNu; 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 S1729970AbfFTSRq (ORCPT + 99 others); Thu, 20 Jun 2019 14:17:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:47042 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727429AbfFTSRm (ORCPT ); Thu, 20 Jun 2019 14:17:42 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D6D34205F4; Thu, 20 Jun 2019 18:17:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1561054661; bh=FcBiB5CC8G9oDaJpE3C38flsRUkDaBLVDjNuHH51egw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z4FyVxNuK/wKj3MlJeTAn237OLr81UFFnNJcZfkkkI4f6LGF/793nsDSIu9Mwwans yBQ/t0eViawM9EtvqJYxjmNQMkUhGRHrLEciMMHLjB8rFWg0eMRzE4YnYKQ5cZmwOO mYNeMYo1I0dCJS7iAwFxAnEIb5Fnhk4INQof2y+I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paul Mackerras , Sasha Levin Subject: [PATCH 5.1 76/98] KVM: PPC: Book3S: Use new mutex to synchronize access to rtas token list Date: Thu, 20 Jun 2019 19:57:43 +0200 Message-Id: <20190620174353.059004124@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190620174349.443386789@linuxfoundation.org> References: <20190620174349.443386789@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 1659e27d2bc1ef47b6d031abe01b467f18cb72d9 ] Currently the Book 3S KVM code uses kvm->lock to synchronize access to the kvm->arch.rtas_tokens list. Because this list is scanned inside kvmppc_rtas_hcall(), which is called with the vcpu mutex held, taking kvm->lock cause a lock inversion problem, which could lead to a deadlock. To fix this, we add a new mutex, kvm->arch.rtas_token_lock, which nests inside the vcpu mutexes, and use that instead of kvm->lock when accessing the rtas token list. This removes the lockdep_assert_held() in kvmppc_rtas_tokens_free(). At this point we don't hold the new mutex, but that is OK because kvmppc_rtas_tokens_free() is only called when the whole VM is being destroyed, and at that point nothing can be looking up a token in the list. Signed-off-by: Paul Mackerras Signed-off-by: Sasha Levin --- arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/kvm/book3s.c | 1 + arch/powerpc/kvm/book3s_rtas.c | 14 ++++++-------- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h index 8d3658275a34..1f9eb75ce95a 100644 --- a/arch/powerpc/include/asm/kvm_host.h +++ b/arch/powerpc/include/asm/kvm_host.h @@ -305,6 +305,7 @@ struct kvm_arch { #ifdef CONFIG_PPC_BOOK3S_64 struct list_head spapr_tce_tables; struct list_head rtas_tokens; + struct mutex rtas_token_lock; DECLARE_BITMAP(enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); #endif #ifdef CONFIG_KVM_MPIC diff --git a/arch/powerpc/kvm/book3s.c b/arch/powerpc/kvm/book3s.c index 10c5579d20ce..020304403bae 100644 --- a/arch/powerpc/kvm/book3s.c +++ b/arch/powerpc/kvm/book3s.c @@ -878,6 +878,7 @@ int kvmppc_core_init_vm(struct kvm *kvm) #ifdef CONFIG_PPC64 INIT_LIST_HEAD_RCU(&kvm->arch.spapr_tce_tables); INIT_LIST_HEAD(&kvm->arch.rtas_tokens); + mutex_init(&kvm->arch.rtas_token_lock); #endif return kvm->arch.kvm_ops->init_vm(kvm); diff --git a/arch/powerpc/kvm/book3s_rtas.c b/arch/powerpc/kvm/book3s_rtas.c index 4e178c4c1ea5..b7ae3dfbf00e 100644 --- a/arch/powerpc/kvm/book3s_rtas.c +++ b/arch/powerpc/kvm/book3s_rtas.c @@ -146,7 +146,7 @@ static int rtas_token_undefine(struct kvm *kvm, char *name) { struct rtas_token_definition *d, *tmp; - lockdep_assert_held(&kvm->lock); + lockdep_assert_held(&kvm->arch.rtas_token_lock); list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) { if (rtas_name_matches(d->handler->name, name)) { @@ -167,7 +167,7 @@ static int rtas_token_define(struct kvm *kvm, char *name, u64 token) bool found; int i; - lockdep_assert_held(&kvm->lock); + lockdep_assert_held(&kvm->arch.rtas_token_lock); list_for_each_entry(d, &kvm->arch.rtas_tokens, list) { if (d->token == token) @@ -206,14 +206,14 @@ int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp) if (copy_from_user(&args, argp, sizeof(args))) return -EFAULT; - mutex_lock(&kvm->lock); + mutex_lock(&kvm->arch.rtas_token_lock); if (args.token) rc = rtas_token_define(kvm, args.name, args.token); else rc = rtas_token_undefine(kvm, args.name); - mutex_unlock(&kvm->lock); + mutex_unlock(&kvm->arch.rtas_token_lock); return rc; } @@ -245,7 +245,7 @@ int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu) orig_rets = args.rets; args.rets = &args.args[be32_to_cpu(args.nargs)]; - mutex_lock(&vcpu->kvm->lock); + mutex_lock(&vcpu->kvm->arch.rtas_token_lock); rc = -ENOENT; list_for_each_entry(d, &vcpu->kvm->arch.rtas_tokens, list) { @@ -256,7 +256,7 @@ int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu) } } - mutex_unlock(&vcpu->kvm->lock); + mutex_unlock(&vcpu->kvm->arch.rtas_token_lock); if (rc == 0) { args.rets = orig_rets; @@ -282,8 +282,6 @@ void kvmppc_rtas_tokens_free(struct kvm *kvm) { struct rtas_token_definition *d, *tmp; - lockdep_assert_held(&kvm->lock); - list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) { list_del(&d->list); kfree(d); -- 2.20.1