Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp4691104rdb; Fri, 29 Dec 2023 09:59:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IHmj3WqEOeiJEON2dC9jfQSzyQx7Q4auZcevYjrDDZ6V99GVjlYVKOdgrJ0TuZFNr40qL6B X-Received: by 2002:a0d:c8c1:0:b0:5d7:1940:7d66 with SMTP id k184-20020a0dc8c1000000b005d719407d66mr6289191ywd.61.1703872793657; Fri, 29 Dec 2023 09:59:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703872793; cv=none; d=google.com; s=arc-20160816; b=ZOXrlqQ0QrTWgRFBcwRe6I171I+J8R8pE3Q3Jq46N9Nc6oF1JVqkhFV3POnvEKWrIN MNHFWQZLaOUnhZ0BKDomilfB9X4fs3AYBFGSQOoZ7Fnm1JcKiem6vjGMu3kKnf1Zg/ft 2R4nlCTQ52Vn/yqSv+aT2kCDfZhBCfHqmOpMI6ugvmWUVhTwAGYr8Xw6Asu5Qx2lNSIq RJ3qm4aNsZIPTyuKvgeIv2GCazcoD8ER41dvqf/j9NxO3I51p2YjTxDVjyztZZZxqlHD E30XokSMCweTUzQdfrEJKKPlcFmkSc4WC9gi5eXjuGatEnfLRAXB23D+gYyTdQFLdzut biCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=yeKqaIlvVPoYPC4W0RMtxJ/Ybm4HjQYyUHUu8GgGRhQ=; fh=5MJZ8InpVpiYD653tw8TMSfv9moeoVc1pc5G1TvszNA=; b=OyjqxnzcuC/DfmeUTYGDz6lrv8Dd3WpxrhRmjlUDSVAOhC9VE6WkkZNYWKHon/tQ3t zs3RwXfmhAdU+SFXSfUZI9AXwmSaUvLhDt3osxzugpyTySmTFfcarqy4NWkmsXA6H0Rs B9S4zsZWIdTOkNFeyk5ke/0uLD3rX5ImJGaN4pxx3DUL0oa7yTLZ6NuVyOHzQg7jcsau 5MSvUdR3Co6d/QLRPBDxLxTxE1LTXtUf16weE3HPkF595Jn0zJJkQ959GnbFQpkiWx2w TT38JTZl6FOL48NzVtpX92GGYDy2LmldVzbdE0bfsDzfO6KAW/DCKMm5to0JnpD45PlT lrig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=b9b8S3nV; spf=pass (google.com: domain of linux-kernel+bounces-13194-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13194-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id bk14-20020a05620a1a0e00b0077f3b68f92asi21019354qkb.372.2023.12.29.09.59.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Dec 2023 09:59:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13194-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=b9b8S3nV; spf=pass (google.com: domain of linux-kernel+bounces-13194-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13194-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 7A13C1C2175E for ; Fri, 29 Dec 2023 17:59:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E3A9012B66; Fri, 29 Dec 2023 17:59:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="b9b8S3nV" X-Original-To: linux-kernel@vger.kernel.org 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 97064134AA for ; Fri, 29 Dec 2023 17:59:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703872760; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=yeKqaIlvVPoYPC4W0RMtxJ/Ybm4HjQYyUHUu8GgGRhQ=; b=b9b8S3nVKyTH+PoeF9OS2jjhBthr1RqdzRTvV/CDAI/BL809id83k+SJukPrRsNubVRaEW W9NK8D0mTrO20AHl84LKsM4rOy5l2f9c+IwtAeHfbpU/zthXzc+mwAoGDXg3MwQl46OKmI G9u8DKkFm1bRTxRmnMvngMuHIa/RiY4= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-608-Qu68Deq4O3OAk3s3APu6jQ-1; Fri, 29 Dec 2023 12:59:13 -0500 X-MC-Unique: Qu68Deq4O3OAk3s3APu6jQ-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-336f07b29e6so1435288f8f.1 for ; Fri, 29 Dec 2023 09:59:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703872752; x=1704477552; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yeKqaIlvVPoYPC4W0RMtxJ/Ybm4HjQYyUHUu8GgGRhQ=; b=Q5huEl2bdRUkVXWj5baxcwljSPTPrrHXGwSqf312PCzMhf0uGJIwOp5uAbxGR/4Jgf ZedWdentR3FCHADDpdWW9KrqtSFq9TFj5sb/tZKtGuiL3pVxzduOL/iSuS+iGn8jEu9Z es9k+pvHachtjAfucYA1BcdAmj47wr+CubrTGyqz2AFfiEM2kqoqh08PGoef742S54gk +tAOauPCOCIUyCcXN2c6XgpYsqSEl21MtTLnCAMKiwX0U18y5j/3mtrjfiftiliymVaM ZH3qO014GC5KToYLygzSI9ajAdXCyHEfox04dyI7YSJjx3MfVPHykvkaLMv6aozcZFR6 ZDXw== X-Gm-Message-State: AOJu0YzQuYajqMQUNF3xiPNZACvhKcPxJpsIaOEEh47nQYPSobLT8m2p Z+HKBmdIpmYxZARdYDVXI27JV1noGN3gk58gQ3XqQYhobhazJ6Xp8Qws6INCelM2s4VtG/Zno7d xVQs0vYGnWukZGOpifBXRaDoetQy7B/Pl1UiW93uguEO4AqbIva+QHbNb X-Received: by 2002:a05:600c:4fcc:b0:40d:5166:f08a with SMTP id o12-20020a05600c4fcc00b0040d5166f08amr4881315wmq.134.1703872752407; Fri, 29 Dec 2023 09:59:12 -0800 (PST) X-Received: by 2002:a05:600c:4fcc:b0:40d:5166:f08a with SMTP id o12-20020a05600c4fcc00b0040d5166f08amr4881309wmq.134.1703872752069; Fri, 29 Dec 2023 09:59:12 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: In-Reply-To: From: Prasad Pandit Date: Fri, 29 Dec 2023 23:28:55 +0530 Message-ID: Subject: Fwd: About patch bdedff263132 - KVM: x86: Route pending NMIs To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Sean Christopherson Content-Type: text/plain; charset="UTF-8" Hello Sean, On Tue, 31 Oct 2023 at 17:45, Prasad Pandit wrote: > On Mon, 30 Oct 2023 at 20:41, Sean Christopherson wrote: >>> - kvm_make_request(KVM_REQ_NMI, vcpu); >>> + if (events->nmi.pending) >>> + kvm_make_request(KVM_REQ_NMI, vcpu); > > > > This looks sane, but it should be unnecessary as KVM_REQ_NMI nmi_queued=0 should > > be a (costly) nop. Hrm, unless the vCPU is in HLT, in which case KVM will treat > > a spurious KVM_REQ_NMI as a wake event. When I made this change, my assumption > > was that userspace would set KVM_VCPUEVENT_VALID_NMI_PENDING iff there was > > relevant information to process. But if I'm reading the code correctly, QEMU > > invokes KVM_SET_VCPU_EVENTS with KVM_VCPUEVENT_VALID_NMI_PENDING at the end of > > machine creation. > > QEMU: qemu_thread_start kvm_start_vcpu_thread kvm_vcpu_thread_fn kvm_cpu_exec kvm_arch_put_registers kvm_put_vcpu_events (cpu=..., level=1) qemu_thread_start (args=0x559fdc852110) at ../util/qemu-thread-posix.c:534 kvm_vcpu_thread_fn (arg=0x559fdc84cdc0) at ../accel/kvm/kvm-accel-ops.c:56 qemu_wait_io_event (cpu=0x559fdc84cdc0) at ../softmmu/cpus.c:435 qemu_wait_io_event_common (cpu=0x559fdc84cdc0) at ../softmmu/cpus.c:411 process_queued_cpu_work (cpu=0x559fdc84cdc0) at ../cpus-common.c:351 do_kvm_cpu_synchronize_post_reset (cpu=0x559fdc84cdc0, arg=...) at ../accel/kvm/kvm-all.c:2808 kvm_arch_put_registers (cpu=0x559fdc84cdc0, level=2) at ../target/i386/kvm/kvm.c:4664 kvm_put_vcpu_events (cpu=0x559fdc84cdc0, level=2) at ../target/i386/kvm/kvm.c:4308 qemu_thread_start (args=0x559fdc852110) at ../util/qemu-thread-posix.c:534 kvm_vcpu_thread_fn (arg=0x559fdc84cdc0) at ../accel/kvm/kvm-accel-ops.c:56 qemu_wait_io_event (cpu=0x559fdc84cdc0) at ../softmmu/cpus.c:435 qemu_wait_io_event_common (cpu=0x559fdc84cdc0) at ../softmmu/cpus.c:411 process_queued_cpu_work (cpu=0x559fdc84cdc0) at ../cpus-common.c:351 do_kvm_cpu_synchronize_post_init (cpu=0x559fdc84cdc0, arg=...) at ../accel/kvm/kvm-all.c:2819 kvm_arch_put_registers (cpu=0x559fdc84cdc0, level=3) at ../target/i386/kvm/kvm.c:4664 kvm_put_vcpu_events (cpu=0x559fdc84cdc0, level=3) at ../target/i386/kvm/kvm.c:4308 Kernel: kvm_vcpu_ioctl mutex_lock_killable(&vcpu->mutex) kvm_arch_vcpu_ioctl(, KVM_SET_VCPU_EVENTS, ... ) mutex_unlock(&vcpu->mutex); -> kvm_vcpu_ioctl_x86_set_vcpu_events() * Above are 3 different ways in which KVM_SET_VCPU_EVENTS ioctl(2) gets called. QEMU/target/i386/kvm/kvm.c: kvm_put_vcpu_events() if (level >= KVM_PUT_RESET_STATE) { events.flags |= KVM_VCPUEVENT_VALID_NMI_PENDING; But KVM_VCPUEVENT_VALID_NMI_PENDING is set only when level >= 2(KVM_PUT_RESET_STATE). ie. in the first (level=1) case _NMI_PENDING is not set. * In the real-time host set-up I have, KVM_VCPUEVENT_VALID_NMI_PENDING is called twice for each VCPU and after that kernel goes into what looks like a lock contention loop. Each time KVM_VCPUEVENT_VALID_NMI_PENDING is called with 'cpu->env->nmi_injected = 0' and 'cpu->env->nmi_pending = 0'. ie. for each VCPU two NMI events are injected via - kvm_make_request(KVM_REQ_NMI, vcpu), when vcpu has no NMIs pending. # perf lock report -t Name acquired contended avg wait total wait max wait min wait CPU 3/KVM 154017 154017 62.19 us 9.58 s 101.01 us 1.49 us CPU 9/KVM 152796 152796 62.67 us 9.58 s 95.92 us 1.49 us CPU 7/KVM 151554 151554 63.16 us 9.57 s 102.70 us 1.48 us CPU 1/KVM 151273 151273 65.30 us 9.88 s 98.88 us 1.52 us CPU 6/KVM 151107 151107 63.34 us 9.57 s 107.64 us 1.50 us CPU 8/KVM 151038 151038 63.37 us 9.57 s 102.93 us 1.51 us CPU 2/KVM 150701 150701 63.52 us 9.57 s 99.24 us 1.50 us CPU 5/KVM 150695 150695 63.56 us 9.58 s 142.15 us 1.50 us CPU 4/KVM 150527 150527 63.60 us 9.57 s 102.04 us 1.44 us qemu-system-x86 665 665 65.92 us 43.84 ms 100.67 us 1.55 us CPU 0/KVM 2 2 210.46 us 420.92 us 411.89 us 9.03 us qemu-system-x86 1 1 404.91 us 404.91 us 404.91 us 404.91 us TC tc-pc.ram 1 1 414.22 us 414.22 us 414.22 us 414.22 us === output for debug=== bad: 10, total: 13 bad rate: 76.92 % histogram of events caused bad sequence acquire: 0 acquired: 10 contended: 0 release: 0 * VCPU#0 thread seems to wait indefinitely to get qemu_mutex_iothread_lock() to make any progress. The proposed patch above to check 'events->nmi_pending' for non-zero value helps to fix this issue. ...wdyt? Thank you. --- - Prasad PS: The kvm_make_request() routine has following comment, I wonder if this is what is happening with empty NMI events. Request that don't require vCPU action should never be logged in vcpu->requests. The vCPU won't clear the request, so it will stay logged indefinitely and prevent the vCPU from entering the guest.