From mboxrd@z Thu Jan 1 00:00:00 1970 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 DE2A0C25B08 for ; Fri, 5 Aug 2022 19:40:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241361AbiHETkt (ORCPT ); Fri, 5 Aug 2022 15:40:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240504AbiHETkr (ORCPT ); Fri, 5 Aug 2022 15:40:47 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA09F11452 for ; Fri, 5 Aug 2022 12:40:45 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id x39so4755067lfu.7 for ; Fri, 05 Aug 2022 12:40:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc; bh=rty6mPaEYkYj+hk2AIwEoGnTyks1fl0dWOatixd39IQ=; b=ZVdfbFAYeIUhGFL1MsmiiKPKr/iDRfw7LNLWovNYdZMMSqGycmpqxqzuBFS59yToBK MPF47XlwvkHwloFZ0QU+WvcrJW+xDfoqtCH6+uSUqgYSW2NOjGo2det5Od7Njoq5Ml6/ +CwAoGIa929cRNttmpn9qxEz7B25vEtt9azLRc4iOHcQBoQNErztOE3hjKnLTHyzGigl 4mvtAuLzu+sJRNekNhuKmQSjb7IotNHEnum47KHJxbGqltNqRcXB5U3smOfpln9CRsBE QX1tQ+doOlI35hmgOmUSmYGQtWtHtzm0jO8ApsmID8pbklD+AmnrFFEawPFrZlRm05Ee pU5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=rty6mPaEYkYj+hk2AIwEoGnTyks1fl0dWOatixd39IQ=; b=ClFEGE0Nl+q5oTU/tetMBsDw9Q0bNvw9yUnDCFwSOndE2oKYXRi4SQ891SSpsA0A4B fjCCSG/pCl17F4d5XPr55ZSa0rPafMeEVVUvkLgHTkXV4VftsKitc4KsVlOmk4oEkSRM 1L75tMfexKAX/1Uhqfk6pRwvDiu7P9RQePdgw8ECXBuPrehpF4npjzLdFRlcFVAV9e0x QekBcCoiGFYKtkm01ivPSaPkzvXHjRvOZHv3jlas4nATgJODbOih9kNYwq0Dyy//4VzA HLF0lYhwk0QwE4H59A+xzJOLnTMrK53rEa4G9TyerSmRWL7Q0JnpBu1i4i4oc0tIX5Ox t6dg== X-Gm-Message-State: ACgBeo1RgPMNGdkn/sKboIdw0qb1FbUXWWh8xcnyaEnDfzQLB8z7o9oL AV3XX9xfSOGu+8C9vUK3jISPyA== X-Google-Smtp-Source: AA6agR4yyOputGlNoeiiinUA89rWH8MyQHy5pMHZ5HQQAH93EmzS4UkCpxA03zHmJI4OgjW4LGuxNA== X-Received: by 2002:a05:6512:3981:b0:48a:6fb9:74b7 with SMTP id j1-20020a056512398100b0048a6fb974b7mr2647305lfu.98.1659728443958; Fri, 05 Aug 2022 12:40:43 -0700 (PDT) Received: from dmaluka.office.semihalf.net ([83.142.187.84]) by smtp.gmail.com with ESMTPSA id o4-20020a056512230400b0048a407f41bbsm560079lfu.238.2022.08.05.12.40.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 12:40:43 -0700 (PDT) From: Dmytro Maluka To: Sean Christopherson , 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 , Rong L Liu , Zhenyu Wang , Tomasz Nowicki , Grzegorz Jaszczyk , upstream@semihalf.com, Dmitry Torokhov , Dmytro Maluka Subject: [PATCH v2 0/5] KVM: Fix oneshot interrupts forwarding Date: Fri, 5 Aug 2022 21:39:14 +0200 Message-Id: <20220805193919.1470653-1-dmy@semihalf.com> X-Mailer: git-send-email 2.37.1.559.g78731f0fdb-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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