From patchwork Wed Apr 9 01:41:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 14043966 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 17E791B423C for ; Wed, 9 Apr 2025 01:42:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744162940; cv=none; b=L0bOZBj2XDArWV+vX2mFwshjHObv5bWE3RpclZnUCILr4mI302uofP97yFHcPT/lFW9DkJZ1A9nHaHN9ckxW0wook0zlBJEw764Fdzm4DC1pp4GvM8fs8Y39SBrPWrnmT6BUG1iMqkQev6fp7FwwNBS8lxn2IViOh6EqpsZePM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744162940; c=relaxed/simple; bh=riqkQ1t8/h7MsJZAxC/eXgF19eg6YzSjLsP+88GFCKQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MLcjkkdmSkywI8DFKHFJRrolOoi1rwNMl4dPXlm58xVSHrx2L0YrukvLjxh870dA2H+RVf/wpqEqIC5GtnMKeA7sVz7L2zVALZnRnzsRdYrEL44x1oBnrUJjhrp6eKMC+wZFdz3Wiv7LLG9W7r9rlDCn8ZzQcqhrH8XlNZrWn/U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=G2Aqornc; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="G2Aqornc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744162937; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z1kcoAyb5oE1yadCC0yo8lvxmOIeYp6wUMScV/KBS/Y=; b=G2AqorncyqKcYVi1NzLJW4NLFOGRCoUJRNCSiYalmx98FDBcM1po390IR3zvLLHNqFZKml MdNsiBSCXtLnUgHrlw0dsKezhCf/zbc8zP6VExEB/HqmuEk2xOs10wAb6OXsoptHpDCVJ8 wo/Z5vlAi+B4tYb2aEMn+tA656aLwGg= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-204-FfZk7JGCPmS8NemWl-m-0g-1; Tue, 08 Apr 2025 21:42:14 -0400 X-MC-Unique: FfZk7JGCPmS8NemWl-m-0g-1 X-Mimecast-MFC-AGG-ID: FfZk7JGCPmS8NemWl-m-0g_1744162930 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E85E0180899B; Wed, 9 Apr 2025 01:42:09 +0000 (UTC) Received: from starship.lan (unknown [10.22.65.191]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C53151801766; Wed, 9 Apr 2025 01:42:04 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Alexander Potapenko , "H. Peter Anvin" , Suzuki K Poulose , kvm-riscv@lists.infradead.org, Oliver Upton , Dave Hansen , Jing Zhang , Waiman Long , x86@kernel.org, Kunkun Jiang , Boqun Feng , Anup Patel , Albert Ou , kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Zenghui Yu , Borislav Petkov , Alexandre Ghiti , Keisuke Nishimura , Sebastian Ott , Paolo Bonzini , Atish Patra , Paul Walmsley , Randy Dunlap , Will Deacon , Palmer Dabbelt , linux-riscv@lists.infradead.org, Marc Zyngier , linux-arm-kernel@lists.infradead.org, Joey Gouly , Peter Zijlstra , Ingo Molnar , Andre Przywara , Thomas Gleixner , Sean Christopherson , Catalin Marinas , Maxim Levitsky , Bjorn Helgaas Subject: [PATCH v2 4/4] RISC-V: KVM: switch to kvm_lock/unlock_all_vcpus Date: Tue, 8 Apr 2025 21:41:36 -0400 Message-Id: <20250409014136.2816971-5-mlevitsk@redhat.com> In-Reply-To: <20250409014136.2816971-1-mlevitsk@redhat.com> References: <20250409014136.2816971-1-mlevitsk@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 use kvm_lock/unlock_all_vcpus instead of riscv's own implementation. Note that this does introduce a slight functional change - now vCPUs are unlocked in the same order they were locked and not in the opposite order. Suggested-by: Paolo Bonzini Signed-off-by: Maxim Levitsky --- arch/riscv/kvm/aia_device.c | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/arch/riscv/kvm/aia_device.c b/arch/riscv/kvm/aia_device.c index 39cd26af5a69..a4599de98c6b 100644 --- a/arch/riscv/kvm/aia_device.c +++ b/arch/riscv/kvm/aia_device.c @@ -12,36 +12,6 @@ #include #include -static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx) -{ - struct kvm_vcpu *tmp_vcpu; - - for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { - tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); - mutex_unlock(&tmp_vcpu->mutex); - } -} - -static void unlock_all_vcpus(struct kvm *kvm) -{ - unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1); -} - -static bool lock_all_vcpus(struct kvm *kvm) -{ - struct kvm_vcpu *tmp_vcpu; - unsigned long c; - - kvm_for_each_vcpu(c, tmp_vcpu, kvm) { - if (!mutex_trylock(&tmp_vcpu->mutex)) { - unlock_vcpus(kvm, c - 1); - return false; - } - } - - return true; -} - static int aia_create(struct kvm_device *dev, u32 type) { int ret; @@ -53,7 +23,7 @@ static int aia_create(struct kvm_device *dev, u32 type) return -EEXIST; ret = -EBUSY; - if (!lock_all_vcpus(kvm)) + if (kvm_lock_all_vcpus(kvm, true)) return ret; kvm_for_each_vcpu(i, vcpu, kvm) { @@ -65,7 +35,7 @@ static int aia_create(struct kvm_device *dev, u32 type) kvm->arch.aia.in_kernel = true; out_unlock: - unlock_all_vcpus(kvm); + kvm_unlock_all_vcpus(kvm); return ret; }