Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp5611843rwb; Tue, 9 Aug 2022 00:33:32 -0700 (PDT) X-Google-Smtp-Source: AA6agR7vrI8qCtGQz+uB76Rtq22F+6ll5tzcsJIMGppjC2Mfana9WFibCiSs/s/KfZgPUq4rp1oi X-Received: by 2002:a17:907:3e91:b0:730:ba28:28d2 with SMTP id hs17-20020a1709073e9100b00730ba2828d2mr16836776ejc.10.1660030412646; Tue, 09 Aug 2022 00:33:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660030412; cv=none; d=google.com; s=arc-20160816; b=kJpKaHDCjRyvOlNa1tAocVc++JD8GJrVvROP30vFQEP8E9ZQ5MItdNFjzQos8wJ9sA VnrcRkmftLaSAdMI83RIkfb8t0C3RQiBPyLKmSTB1tzG/JSPFxIZwYiLZFyxrzkW3Shp RuAnID/nGRT2+NcYaBmPiRigf3dxzKUFOQEejyEW/WMgwfj1YzPuMDwLrhVblKN8XHaR 4ubI6ATVkQsCz9kPpz0K3PnrlWFAEhjXLQ1JtIMqieMlH+RfyZf2OQJzwLNesUzqFZFC 68fYB3f7qJvESFt7A7VY/seBgWIWIkYc6Kz74DW6uH827Jz7wb7gs6Bk168g+eYVdxxo piHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=tTxM+7+FZK4rcvhtVun0/PJ+nOo+VMVRQjQ1dHG97U0=; b=xspOZ3D4uTd4VNBAM1Y1YAI098XC61Y3eOSV/NHUby7U7rHsx2Us5wO0tLKjq0fKpR Xor/iUT0+iSg4SpI70VbTWK61Phh0DTrypz5K3i1kyk/ytTbwlyA7C3UBWZuX1A47egR Citx1GkZ4Ww0MhEKpgaH4AKZun4lhkSfRza17EfWhH1ddXTkDd0mVhM9iyUZDjw/w1CW Xq0+yK501+1WeCzwyCd3PsZEYmGS5Xa7tRgoCCvQA7vLZpF9xhK0pqz5qWMPNv6yOlgb +sQU7XF2UJVcYw3PH28Pq7BEaPaMlp5pZ/socnxXD5Mv8RwgxtxqODgq/I0BZ4xYqNg5 oDtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@semihalf.com header.s=google header.b=gDVJfXB4; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=semihalf.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qb27-20020a1709077e9b00b00732f9939b10si486142ejc.322.2022.08.09.00.33.02; Tue, 09 Aug 2022 00:33:32 -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=@semihalf.com header.s=google header.b=gDVJfXB4; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=semihalf.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236139AbiHIHYi (ORCPT + 99 others); Tue, 9 Aug 2022 03:24:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236554AbiHIHYg (ORCPT ); Tue, 9 Aug 2022 03:24:36 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA5A120F66 for ; Tue, 9 Aug 2022 00:24:32 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id t1so15830038lft.8 for ; Tue, 09 Aug 2022 00:24:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf.com; s=google; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:from:references:cc:to:subject:from:to:cc; bh=tTxM+7+FZK4rcvhtVun0/PJ+nOo+VMVRQjQ1dHG97U0=; b=gDVJfXB4D8sJ6RBnuq1AvXEE5d4tPDGrPdFA+CXJ/lj8Z77S5L25rxif8dSggGlzjs RrYsTkpPnhviTvP6yPRt55PcQClVI9czE5h4RVxiYVgfHeQOvJF998+JDrMNZL3Wdhpi 5OGPXURct7ZKChNJhzk94a5MLOffXQ+XeCSSSbHue0+9UDOmZla1ZpgSt7r1m97+wx1k dmTDt/7j+/P3FAuGlKtJRHGAiuGT3Dq2wKRHnF9KDb0Tz3v8GJqAM4pGzn6X5FeIDkGC Ky+ODlwF7cYAPcv/eVSlY2eRQjq5HlAf8Mt1jUMvtZu02vjq0xZ9mjqYjShKAIXEEe41 OHvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:from:references:cc:to:subject :x-gm-message-state:from:to:cc; bh=tTxM+7+FZK4rcvhtVun0/PJ+nOo+VMVRQjQ1dHG97U0=; b=woBQWrnwOL90Ywnvf/4g8WoQzT3ikiZLtHdRmoWfz4EM2uAEhDG+eAYiH6AGk5at0w gD8p8xu4QTcTTvRA61Nm+QvqgZ/C8MvK7wtj/vsjbxz43uW32/TtBswXEwx09LxcdTYU Rua1v7wuhAPuaeAFK1pJSqYVX/gl3bE40qLAJj4XyebS+l/xIYeeOvaWokLhuMEGg4af cC/rTCtSU3VDEFXUzMDX43d7yDQF8IydvGIqHh+aKRyNJe2Kz3TrCx8YGyaf993dEDrP /5bwVRs0r1IpJGI3PYDkMG9FytbBF8+hXKNjeLinWiX2zb0gVVH055Wisf5fIA7q9drP zs+Q== X-Gm-Message-State: ACgBeo3werQQqg7x5VCDGP9C+frmM4v7d6CPNtzKvfUTh0AUwBkZ37Zd r/Glt9OxHuFvXkTxEAL+t1hw6w== X-Received: by 2002:a05:6512:3b28:b0:48b:2071:e423 with SMTP id f40-20020a0565123b2800b0048b2071e423mr8231558lfv.434.1660029871039; Tue, 09 Aug 2022 00:24:31 -0700 (PDT) Received: from ?IPv6:2a02:a31b:33d:9c00:463a:87e3:44fc:2b2f? ([2a02:a31b:33d:9c00:463a:87e3:44fc:2b2f]) by smtp.gmail.com with ESMTPSA id v5-20020a197405000000b0048a8b6914d2sm1656517lfe.155.2022.08.09.00.24.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 09 Aug 2022 00:24:30 -0700 (PDT) Subject: Re: [PATCH v2 0/5] KVM: Fix oneshot interrupts forwarding To: "Dong, Eddie" , "Christopherson,, Sean" , Paolo Bonzini , "kvm@vger.kernel.org" Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "x86@kernel.org" , "H. Peter Anvin" , "linux-kernel@vger.kernel.org" , Eric Auger , Alex Williamson , "Liu, Rong L" , Zhenyu Wang , Tomasz Nowicki , Grzegorz Jaszczyk , "upstream@semihalf.com" , Dmitry Torokhov References: <20220805193919.1470653-1-dmy@semihalf.com> From: Dmytro Maluka Message-ID: Date: Tue, 9 Aug 2022 09:24:28 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On 8/9/22 1:26 AM, Dong, Eddie wrote: >> >> The existing KVM mechanism for forwarding of level-triggered interrupts using >> resample eventfd doesn't work quite correctly in the case of interrupts that are >> handled in a Linux guest as oneshot interrupts (IRQF_ONESHOT). Such an >> interrupt is acked to the device in its threaded irq handler, i.e. later than it is >> acked to the interrupt controller (EOI at the end of hardirq), not earlier. The >> existing KVM code doesn't take that into account, which results in erroneous >> extra interrupts in the guest caused by premature re-assert of an >> unacknowledged IRQ by the host. > > Interesting... How it behaviors in native side? In native it behaves correctly, since Linux masks such a oneshot interrupt at the beginning of hardirq, so that the EOI at the end of hardirq doesn't result in its immediate re-assert, and then unmasks it later, after its threaded irq handler completes. In handle_fasteoi_irq(): if (desc->istate & IRQS_ONESHOT) mask_irq(desc); handle_irq_event(desc); cond_unmask_eoi_irq(desc, chip); and later in unmask_threaded_irq(): unmask_irq(desc); I also mentioned that in patch #3 description: "Linux keeps such interrupt masked until its threaded handler finishes, to prevent the EOI from re-asserting an unacknowledged interrupt. However, with KVM + vfio (or whatever is listening on the resamplefd) we don't check that the interrupt is still masked in the guest at the moment of EOI. Resamplefd is notified regardless, so vfio prematurely unmasks the host physical IRQ, thus a new (unwanted) physical interrupt is generated in the host and queued for injection to the guest." > >> >> This patch series fixes this issue (for now on x86 only) by checking if the >> interrupt is unmasked when we receive irq ack (EOI) and, in case if it's masked, >> postponing resamplefd notify until the guest unmasks it. >> >> Patches 1 and 2 extend the existing support for irq mask notifiers in KVM, >> which is a prerequisite needed for KVM irqfd to use mask notifiers to know >> when an interrupt is masked or unmasked. >> >> Patch 3 implements the actual fix: postponing resamplefd notify in irqfd until >> the irq is unmasked. >> >> Patches 4 and 5 just do some optional renaming for consistency, as we are now >> using irq mask notifiers in irqfd along with irq ack notifiers. >> >> Please see individual patches for more details. >> >> v2: >> - Fixed compilation failure on non-x86: mask_notifier_list moved from >> x86 "struct kvm_arch" to generic "struct kvm". >> - kvm_fire_mask_notifiers() also moved from x86 to generic code, even >> though it is not called on other architectures for now. >> - Instead of kvm_irq_is_masked() implemented >> kvm_register_and_fire_irq_mask_notifier() to fix potential race >> when reading the initial IRQ mask state. >> - Renamed for clarity: >> - irqfd_resampler_mask() -> irqfd_resampler_mask_notify() >> - kvm_irq_has_notifier() -> kvm_irq_has_ack_notifier() >> - resampler->notifier -> resampler->ack_notifier >> - Reorganized code in irqfd_resampler_ack() and >> irqfd_resampler_mask_notify() to make it easier to follow. >> - Don't follow unwanted "return type on separate line" style for >> irqfd_resampler_mask_notify(). >> >> Dmytro Maluka (5): >> KVM: x86: Move irq mask notifiers from x86 to generic KVM >> KVM: x86: Add kvm_register_and_fire_irq_mask_notifier() >> KVM: irqfd: Postpone resamplefd notify for oneshot interrupts >> KVM: irqfd: Rename resampler->notifier >> KVM: Rename kvm_irq_has_notifier() >> >> arch/x86/include/asm/kvm_host.h | 17 +--- >> arch/x86/kvm/i8259.c | 6 ++ >> arch/x86/kvm/ioapic.c | 8 +- >> arch/x86/kvm/ioapic.h | 1 + >> arch/x86/kvm/irq_comm.c | 74 +++++++++++------ >> arch/x86/kvm/x86.c | 1 - >> include/linux/kvm_host.h | 21 ++++- >> include/linux/kvm_irqfd.h | 16 +++- >> virt/kvm/eventfd.c | 136 ++++++++++++++++++++++++++++---- >> virt/kvm/kvm_main.c | 1 + >> 10 files changed, 221 insertions(+), 60 deletions(-) >> >> -- >> 2.37.1.559.g78731f0fdb-goog >