Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp2073368rwb; Fri, 5 Aug 2022 12:43:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR6jbiMLdzbOxccn/0c6zG5/+otdhWJZUaGnpiTiyQ/9WcNna4Z7acKXfX1HhOyeYqJE6gRt X-Received: by 2002:a17:907:2bd6:b0:730:a2f7:f885 with SMTP id gv22-20020a1709072bd600b00730a2f7f885mr6274496ejc.214.1659728629677; Fri, 05 Aug 2022 12:43:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659728629; cv=none; d=google.com; s=arc-20160816; b=SbFc0Y0VGCpAL1YxmLHv8TUQliK55szCMXNT8M4DyjtCadxMzuiGGKavUH2qEXGYM/ Vex1LJmURE0ImqVH1WUpREQXtm4cYEBHmg8Y+RVJ1CiIQUJz1Y2WS70rCWssQT5/KkEN d7VR7sOyMqsIJyiuiHqzZkPLPFxb6oGSlWSzV4lbgDrO2LbkMs81G89NKeUqSltl2iNI MiqwTwjHIJKaoQpzK4dn4YYfR+w6beMkYkG5TY0sjpvLtFXgaY2wUQZk4+ZaqCHAAfSg GiBLyPCGBHzKlHAo/rE3ug/BTJhedPj+ADGDPW/EfgPm7WIfcLIxD1DNS14degigWZkH muUA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=rty6mPaEYkYj+hk2AIwEoGnTyks1fl0dWOatixd39IQ=; b=V8o0snx9QhEtwO7mKyO/hEQjoNlbZ5zyyNl08oqAgv8eBq6JK5AuMf/bNI2CLuaKX5 iOfekOI1P0OvccqY3QfOUaM5lX11EBGn3IGbfH5gwIdssg3c4ckSWCODq+W0pHd5jQa4 QCW860YScKsTbTASIGwvHmXS6U2y+pfX7E6XSKI6MvF1eqh42PKexZq3BU7IPK64xOR0 8x1232tTTdQ7fTmoXzB0wuwO8iFVEzjcgNv5aMfdqXrN3h9KQ+2ko9zAVzYKVlPNPj9p wzuDiuhp/ZRRS/nzbPHjsnyUtTVdayY0JdG6j2h1jWWEixQA5I/krlagYngTIaGaFoJA bANw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@semihalf.com header.s=google header.b=ZVdfbFAY; 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 cw11-20020a170906478b00b0072b60c7df5csi4905361ejc.204.2022.08.05.12.43.23; Fri, 05 Aug 2022 12:43:49 -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=ZVdfbFAY; 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 S238395AbiHETks (ORCPT + 99 others); Fri, 5 Aug 2022 15:40:48 -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-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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 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