Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1030457ybl; Thu, 22 Aug 2019 08:22:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqzTldt/CkL6ygGwXGxoqIHe/JsM4QMKAKTzDB0SOa9uZfQPX1L/ktSckr6fFq9Svu6YKE+c X-Received: by 2002:a17:902:7782:: with SMTP id o2mr40730438pll.12.1566487334783; Thu, 22 Aug 2019 08:22:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566487334; cv=none; d=google.com; s=arc-20160816; b=MfUy6K5SDTFG5AfTmCPVqGvC9dPcQU3JUsdEn/gzFk6uAvf6PvwSBTPZloz1P1sFQ5 kK21t6MNnbhxAMK/zPqDrcS6ocJ/1JPxX5iCY9oHZvRTweMfiPQyHTDTYFvsNcNK+UYq Dig2y4sHJHbWSREJ6S/57Q9zQRtGzEZ2oGOb+1LemGUmQHAsfKNR2M7I97qgVEwkjmyQ tK9K/o8mk9cvXTCH/V0XFDmYb/BL7WRHBACa3xgCoFzivj+zPnNO9RppJQ+NIsYPRx4M k4d4keGD4T2mwow887m0lrnRqwGwSmi4QszKUyyPQdHWRGmIvv0DSMt9mWCpvzSeDJ2d JLgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=eirfGgLspQS0fyum8EuJRW/02GOh2q60HfknM62dN8M=; b=IUt/o8aJKWdHqXp4euAcMf2Gq115jGJMq2DJeNGAsae0OV9pj0fZ3qin0TbueAiPMq I8CJjZT1+z4fwJ/Ezh17iXunFWNMFpHHvzJZW5BWVkg4OqauBMXGUuGRW55YMw8SJoPI hxgsZNNKaXZlyWQIPvXjO0eqDYzdTvLDQ+IkV89/HDB3IECH096ITka6DnJkFZq5IJ36 LilbHKCxbPVPWRDKZzE65M1T9mXAHVt3N8DfvIibdoiKc9iwPII61YFyhHpqItrWfECt 1GCb8wTZVPdClA5ReJkLuWw/lIHCxcm25TO8MbTaqCuivecbibRgJnA9ANCKpIQBpKMR 1uyA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q9si2632997pjv.72.2019.08.22.08.21.58; Thu, 22 Aug 2019 08:22:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389208AbfHVNld (ORCPT + 99 others); Thu, 22 Aug 2019 09:41:33 -0400 Received: from out30-130.freemail.mail.aliyun.com ([115.124.30.130]:48885 "EHLO out30-130.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387649AbfHVNld (ORCPT ); Thu, 22 Aug 2019 09:41:33 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R671e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04407;MF=luoben@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0Ta8JkQz_1566481288; Received: from bn0418deMacBook-Pro.local(mailfrom:luoben@linux.alibaba.com fp:SMTPD_---0Ta8JkQz_1566481288) by smtp.aliyun-inc.com(127.0.0.1); Thu, 22 Aug 2019 21:41:29 +0800 Subject: Re: [PATCH v3 3/3] vfio_pci: make use of update_irq_devid and optimize irq ops To: "Liu, Jiang" Cc: Thomas Gleixner , alex.williamson@redhat.com, linux-kernel@vger.kernel.org, tao.ma@linux.alibaba.com, nanhai.zou@linux.alibaba.com, linyunsheng@huawei.com References: <7a3606ad-8fa6-45d5-b5a4-ee3f07893a25@linux.alibaba.com> From: luoben Message-ID: Date: Thu, 22 Aug 2019 21:41:28 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 在 2019/8/20 下午11:27, Liu, Jiang 写道: > >> On Aug 20, 2019, at 11:24 PM, luoben wrote: >> >> >> >> 在 2019/8/16 上午12:45, Thomas Gleixner 写道: >>> On Thu, 15 Aug 2019, Ben Luo wrote: >>> >>>> if (vdev->ctx[vector].trigger) { >>>> - free_irq(irq, vdev->ctx[vector].trigger); >>>> - irq_bypass_unregister_producer(&vdev->ctx[vector].producer); >>>> - kfree(vdev->ctx[vector].name); >>>> - eventfd_ctx_put(vdev->ctx[vector].trigger); >>>> - vdev->ctx[vector].trigger = NULL; >>>> + if (vdev->ctx[vector].trigger == trigger) { >>>> + irq_bypass_unregister_producer(&vdev->ctx[vector].producer); >>>> >>> What's the point of unregistering the producer, just to register it again below? >> Whether producer token changed or not, irq_bypass_register_producer() is a way (seems the >> >> only way) to update IRTE by VFIO for posted interrupt. >> >> When posted interrupt is in use, the target IRTE will be used by IOMMU directly to find the >> >> target CPU for an interrupt posted to VM, since hypervisor is bypassed. >> >> irq_bypass_register_producer() will modify IRTE based on the information retrieved from KVM, >> >> >> 0xffffffff8150a920 : modify_irte+0x0/0x180 [kernel] >> 0xffffffff8150ab94 : intel_ir_set_vcpu_affinity+0xf4/0x150 [kernel] >> 0xffffffff81125f3c : irq_set_vcpu_affinity+0x5c/0xa0 [kernel] >> 0xffffffffa0550818 : vmx_update_pi_irte+0x178/0x290 [kvm_intel] // get pi_desc etc. from KVM >> 0xffffffffa052b789 : kvm_arch_irq_bypass_add_producer+0x39/0x50 [kvm_intel] (inexact) >> 0xffffffffa024a50b : __connect+0x7b/0xa0 [kvm] (inexact) >> 0xffffffffa024a6a8 : irq_bypass_register_producer+0x108/0x140 [kvm] (inexact) >> 0xffffffffa0338386 : vfio_msi_set_vector_signal+0x1b6/0x2c0 [vfio_pci] (inexact) >>>> + } else if (trigger) { >>>> + ret = update_irq_devid(irq, >>>> + vdev->ctx[vector].trigger, trigger); >>>> + if (unlikely(ret)) { >>>> + dev_info(&pdev->dev, >>>> + "update_irq_devid %d (token %p) fails: %d\n", >>>> >>> s/fails/failed/ >>> >>> >>>> + irq, vdev->ctx[vector].trigger, ret); >>>> + eventfd_ctx_put(trigger); >>>> + return ret; >>>> + } >>>> >>> This lacks any form of comment why this is correct. dev_id is updated and >>> the producer with the old token is still registered. >>> >> ok, I will add comment like below: >> >> For KVM-VFIO case, once producer and consumer connected successfully, interrupt from passthrough >> >> device will be directly delivered to VM instead of triggering interrupt process in HOST. If producer and >> >> consumer are disconnected, this interrupt process will fall back to remap mode, the handler vfio_msihandler() >> >> registered in corresponding irqaction will use the dev_id to find the right way to deliver the interrupt to VM. >> >> So, it is safe to update dev_id before unregistration of irq-bypass producer, i.e. switch back from posted >> >> mode to remap mode, since only in remap mode the 'dev_id' will be used by interrupt handler. To producer >> >> and consumer, dev_id is only a token for pairing them togather. >>>> + irq_bypass_unregister_producer(&vdev->ctx[vector].producer); >>>> >>> Now it's unregistered. >>> >>> >>>> + eventfd_ctx_put(vdev->ctx[vector].trigger); >>>> + vdev->ctx[vector].producer.token = trigger; >>>> >>> The token is updated and then it's newly registered below. >>> >>> >>>> + vdev->ctx[vector].trigger = trigger; >>>> - vdev->ctx[vector].producer.token = trigger; >>>> - vdev->ctx[vector].producer.irq = irq; >>>> + /* always update irte for posted mode */ >>>> ret = irq_bypass_register_producer(&vdev->ctx[vector].producer); >>>> if (unlikely(ret)) >>>> dev_info(&pdev->dev, >>>> "irq bypass producer (token %p) registration fails: %d\n", >>>> vdev->ctx[vector].producer.token, ret); >>>> >>> I know this code already existed, but again this looks inconsistent. If the >>> registration fails then a subsequent update will try to unregister a not >>> registered producer. Does not make any sense to me. >>> >> irq_bypass_register_producer() also fails on duplicated registration, so maybe an unconditional try to >> >> unregistration is a easy way to keep consistent. >> >> Maybe it's better to change these function names to irq_bypass_try_register_producer() and >> >> irq_bypass_try_unregister_producer() :) >> >> > Hi Ben, > The point is that we shouldn’t blindly try to register again if we fails to unregister a posted interrupt producer. By this way, the fast path (posted interrupt) may get disabled, but it’s safer than blindly ignoring the failure of ungistration. > Thanks, > Gerry This may need another patchset to enhance the irq_bypass register/unregister functions first, at least to return some error code when irq_bypass_try_unregister_producer() fails. I would like to have a try later. Thanks,     Ben