From patchwork Sat Oct 22 15:48:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 7903 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1259680wrr; Sat, 22 Oct 2022 08:52:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM78nZHx4qKYBzcC36yQMVnpixVOZQpshWjCGVg7rVu/Y/A2JcciRnBTp7R/bITlGmXArEnR X-Received: by 2002:a05:6402:274c:b0:461:9921:60cf with SMTP id z12-20020a056402274c00b00461992160cfmr548217edd.353.1666453955934; Sat, 22 Oct 2022 08:52:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666453955; cv=none; d=google.com; s=arc-20160816; b=p/8aSiSNJJz2DJ56FWSoAfjZcbKsA0F+QHgyFakcFu6xJeLsPKshdjeVZTPxIK7nu6 Rp/8pIzmjeVwqpJb/4EK+Ww8uji3+bsktoU6CDfjurCOtIAUCq5L4qQYjSZZIu/T3Oug dAT4W8j8QYXG4coQvfJ2RxnUJwpQsu4sxvGyCZmu+aIfaWk2KYtoCC/n0xrzda57GFa+ hms1y4zvImkY4fLOMb5P4cMt4tAmsheIJxatubF1xfGetMBfkRr6Z5rXBbsUhR2DSjXk lsGVIjU7oWi4m+vLOEawVUc40NLwpECnhkYTsdxnvAh3n5N5rmbPO3/GLE8XglCQrhoR HZJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i+xYh4ED8NY+eSwwcDjW7bMv+eHis2lS261cJxu7qzg=; b=gbvrdxl+2BhHQBiqXfjHDsJemzgGZRHsqJETAKWpbWmkuLdk9mu26urrTNKJjBRATB nB4LkD7X35OZGpmtPGZBWJsFhsAlfoOrw5XlprmCGbrytEuj39Ya9WvAykPYDJujMOWS CXpey/oAwLbOj5t7hDbw/X00Iepq/AuWGabfa/b8ufC383YorkgaXTBBkj7CTOeoG8IU XRQxFeCBGq7iHa8skSqWdkyryaBOUlDtW0cLhEp/P8mPumj9cmeeZ5/oDNjhzcX56jfy b4vbTPVXlwTzTDltkHKI27ejNe5sPGWefHBQuBIZkX7sUOUYJU9IT8dZyRo+fKawk8y2 5d4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="CZgOp/Rx"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hk6-20020a170906c9c600b0077497671e2asi21697270ejb.406.2022.10.22.08.52.10; Sat, 22 Oct 2022 08:52:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="CZgOp/Rx"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229900AbiJVPsp (ORCPT + 99 others); Sat, 22 Oct 2022 11:48:45 -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 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747403623396310015?= X-GMAIL-MSGID: =?utf-8?q?1747403623396310015?= 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; static struct file_operations kvm_chardev_ops; +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 = kvm_arch_vcpu_ioctl_run(vcpu); + up_read(&memory_transaction); trace_kvm_userspace_exit(vcpu->run->exit_reason, r); break; }