Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2290609ybl; Thu, 15 Aug 2019 09:25:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqwmiguUUdtlO0rVgWlTbXoPOI3jDNE7ik/SuMeLcNR0iZnbc+Akr3/bBE71h7HWzN9c8RUg X-Received: by 2002:a62:1d8f:: with SMTP id d137mr6388703pfd.207.1565886333615; Thu, 15 Aug 2019 09:25:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565886333; cv=none; d=google.com; s=arc-20160816; b=adZkfc6Z6RJsZwAePeUn0uvxVcChkhiFfXsgBTAMRk5sIbHieTafOjYXtnFdsaWu0m xkgEGSaCpC2VQuxpkywnhTdtjDFwizBfizpipv/CzwnYWIZ+GjDcjD+o6fhIfuZrmJ/I mWHS6DAWgsvs3wNyolN2KVQG14UW6Yqd757zt2Fegh7IY425m+ehPJZ4G3qRJ259leD+ 1l6GDmKSaBOVLnGoTa99j4X9Wwqwc+FG6V1Qdui7F4dLlqRrUFyYC+l1vGDho3c2mAOb GfkyETjo7diUM3iAJjw5MylMt2CR+7FRwi03S/hoXUrxoc+UAPQ/y3Nl0tMYDN/GwPvF fJhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from; bh=vN28HaPGuVUmMFvhk5+q+/GfcJ2ROCq28Myq8SonokI=; b=l7nsuE5MTywTPGW97C69SNBZRb8jmJnJbXUufw4hjeeeXaAGc1jCl8ilqCtn7pXThm Pu04pW3OgEEvR3lZBBlyuAEYU6MatPh5a2/JATCHr3SZsOBxBUlMwG+uEVYMx/oy5ogg 475GeS/Y9D9gBMGedDzDDzt6HZcdByX+8LsiPhmpXVF3C8mjvT3vzLe+5achX1IJwbDJ ZYW9zT3lqT8dmcG6/0sG3HAMsuaxFp3pGypuWPHXzyxZE3XHOa44GB9Q+YO7JPe2Wl8+ i8h0uY2Y7ZF2/RayH3HewwVXShG7Nc3Qk0pMSxSr58LJc+U7FFsk7YuyQk41NLsI8bCC bsdA== 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 11si2195103pla.248.2019.08.15.09.25.18; Thu, 15 Aug 2019 09:25:33 -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 S1732157AbfHONNh (ORCPT + 99 others); Thu, 15 Aug 2019 09:13:37 -0400 Received: from out4436.biz.mail.alibaba.com ([47.88.44.36]:59528 "EHLO out4436.biz.mail.alibaba.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731933AbfHONNh (ORCPT ); Thu, 15 Aug 2019 09:13:37 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04394;MF=luoben@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0TZYF1iF_1565874193; Received: from localhost(mailfrom:luoben@linux.alibaba.com fp:SMTPD_---0TZYF1iF_1565874193) by smtp.aliyun-inc.com(127.0.0.1); Thu, 15 Aug 2019 21:03:19 +0800 From: Ben Luo To: tglx@linutronix.de, alex.williamson@redhat.com Cc: linux-kernel@vger.kernel.org, tao.ma@linux.alibaba.com, gerry@linux.alibaba.com, nanhai.zou@linux.alibaba.com, linyunsheng@huawei.com Subject: [PATCH v3 2/3] genirq: introduce update_irq_devid() Date: Thu, 15 Aug 2019 21:03:00 +0800 Message-Id: <6343a7e34ffdd0ddcd730996fc5dad3024e42251.1565857737.git.luoben@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Sometimes, only the dev_id field of irqaction need to be changed. E.g. KVM VM with device passthru via VFIO may switch irq injection path between KVM irqfd and userspace eventfd. These two paths share the same irq num and handler for the same vector of a device, only with different dev_id referencing to different fds' contexts. So, instead of free/request irq, only update dev_id of irqaction. This narrows the gap for setting up new irq (and irte, if has) and also gains some performance benefit. Signed-off-by: Ben Luo Reviewed-by: Liu Jiang Reviewed-by: Thomas Gleixner --- include/linux/interrupt.h | 3 ++ kernel/irq/manage.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h index 5b8328a..6060c5a 100644 --- a/include/linux/interrupt.h +++ b/include/linux/interrupt.h @@ -172,6 +172,9 @@ struct irqaction { request_percpu_nmi(unsigned int irq, irq_handler_t handler, const char *devname, void __percpu *dev); +extern int __must_check +update_irq_devid(unsigned int irq, void *dev_id, void *new_dev_id); + extern const void *free_irq(unsigned int, void *); extern void free_percpu_irq(unsigned int, void __percpu *); diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c index 6f9b20f..a76ef61 100644 --- a/kernel/irq/manage.c +++ b/kernel/irq/manage.c @@ -2064,6 +2064,84 @@ int request_threaded_irq(unsigned int irq, irq_handler_t handler, EXPORT_SYMBOL(request_threaded_irq); /** + * update_irq_devid - update irq dev_id to a new one + * @irq: Interrupt line to update + * @dev_id: A cookie to find the irqaction to update + * @new_dev_id: New cookie passed to the handler function + * + * Sometimes, only the cookie data need to be changed. + * Instead of free/request irq, only update dev_id here + * Not only to gain some performance benefit, but also + * reduce the risk of losing interrupt. + * + * E.g. irq affinity setting in a VM with VFIO passthru + * device is carried out in a free-then-request-irq way + * since lack of this very function. The free_irq() + * eventually triggers deactivation of IR domain, which + * will cleanup IRTE. There is a gap before request_irq() + * finally setup the IRTE again. In this gap, a in-flight + * irq buffering in hardware layer may trigger DMAR fault + * and be lost. + * + * On failure, it returns a negative value. On success, + * it returns 0 + */ +int update_irq_devid(unsigned int irq, void *dev_id, void *new_dev_id) +{ + struct irq_desc *desc = irq_to_desc(irq); + struct irqaction *action, **action_ptr; + unsigned long flags; + + if (in_interrupt()) { + WARN(1, "Trying to update IRQ %d (dev_id %p to %p) from IRQ context!\n", + irq, dev_id, new_dev_id); + return -EPERM; + } + + if (!desc) + return -EINVAL; + + chip_bus_lock(desc); + raw_spin_lock_irqsave(&desc->lock, flags); + + /* + * There can be multiple actions per IRQ descriptor, find the right + * one based on the dev_id: + */ + action_ptr = &desc->action; + for (;;) { + action = *action_ptr; + + if (!action) { + raw_spin_unlock_irqrestore(&desc->lock, flags); + chip_bus_sync_unlock(desc); + WARN(1, "Trying to update already-free IRQ %d (dev_id %p to %p)\n", + irq, dev_id, new_dev_id); + return -ENXIO; + } + + if (action->dev_id == dev_id) { + action->dev_id = new_dev_id; + break; + } + action_ptr = &action->next; + } + + raw_spin_unlock_irqrestore(&desc->lock, flags); + chip_bus_sync_unlock(desc); + + /* + * Make sure it's not being used on another CPU: + * There is a risk of UAF for old *dev_id, if it is + * freed in a short time after this func returns + */ + synchronize_irq(irq); + + return 0; +} +EXPORT_SYMBOL_GPL(update_irq_devid); + +/** * request_any_context_irq - allocate an interrupt line * @irq: Interrupt line to allocate * @handler: Function to be called when the IRQ occurs. -- 1.8.3.1