From nobody Wed Apr 8 06:22:29 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7A30C433FE for ; Sat, 22 Oct 2022 15:48:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229955AbiJVPsr (ORCPT ); Sat, 22 Oct 2022 11:48:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229891AbiJVPsc (ORCPT ); Sat, 22 Oct 2022 11:48:32 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F6A924F0F for ; Sat, 22 Oct 2022 08:48:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666453708; 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=i+xYh4ED8NY+eSwwcDjW7bMv+eHis2lS261cJxu7qzg=; b=CZgOp/Rx10BA42ASxMKapDvLpj8fDMB+QecDc9iaU2zT3bMoxG/KWg+c0G7SJnVGnjjDgl GZnQ98kbHuN1AVI97ihZcHqkpG2RTgIG3otWMidp36L9/GTRTJcYXfv/UVVmbd7tb7jwGu HhJuAxilknplp/RyGNUvxUhK0Q2egok= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-63-cSLsb5qPObusKCwipu1UWA-1; Sat, 22 Oct 2022 11:48:23 -0400 X-MC-Unique: cSLsb5qPObusKCwipu1UWA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3122F85A59D; Sat, 22 Oct 2022 15:48:23 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id C65FD4A9279; Sat, 22 Oct 2022 15:48:22 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Maxim Levitsky , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , David Hildenbrand , x86@kernel.org, "H. Peter Anvin" , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH 3/4] KVM: introduce memory transaction semaphore Date: Sat, 22 Oct 2022 11:48:18 -0400 Message-Id: <20221022154819.1823133-4-eesposit@redhat.com> In-Reply-To: <20221022154819.1823133-1-eesposit@redhat.com> References: <20221022154819.1823133-1-eesposit@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Right now the semaphore is only used to signal that a vcpu entered KVM_RUN (not necessarly in guest mode, could be also blocked/halted). Later it will be used by specific ioctls (writers) to wait that all vcpus (readers) exit from KVM_RUN. Signed-off-by: Emanuele Giuseppe Esposito --- virt/kvm/kvm_main.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index c080b93edc0d..ae0240928a4a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -119,6 +119,8 @@ static const struct file_operations stat_fops_per_vm; =20 static struct file_operations kvm_chardev_ops; =20 +static DECLARE_RWSEM(memory_transaction); + static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, unsigned long arg); #ifdef CONFIG_KVM_COMPAT @@ -4074,7 +4076,19 @@ static long kvm_vcpu_ioctl(struct file *filp, synchronize_rcu(); put_pid(oldpid); } + /* + * Notify that a vcpu wants to run, and thus could be reading + * memslots. + * If KVM_KICK_ALL_RUNNING_VCPUS runs afterwards, it will have + * to wait that KVM_RUN exited and up_read() is called. + * If KVM_KICK_ALL_RUNNING_VCPUS already returned but + * KVM_RESUME_ALL_KICKED_VCPUS didn't start yet, then there + * is a request pending for the vcpu that will cause it to + * exit KVM_RUN. + */ + down_read(&memory_transaction); r =3D kvm_arch_vcpu_ioctl_run(vcpu); + up_read(&memory_transaction); trace_kvm_userspace_exit(vcpu->run->exit_reason, r); break; } --=20 2.31.1