From patchwork Tue Oct 27 13:55:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 11860503 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5F39C55178 for ; Tue, 27 Oct 2020 13:55:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 83DBA218AC for ; Tue, 27 Oct 2020 13:55:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="u+kVoBLb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S368011AbgJ0Nzk (ORCPT ); Tue, 27 Oct 2020 09:55:40 -0400 Received: from casper.infradead.org ([90.155.50.34]:41602 "EHLO casper.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S368005AbgJ0Nzg (ORCPT ); Tue, 27 Oct 2020 09:55:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=pWNWXU0q2YTOp9LS6CIUuWc+DOyY6dTv9ZJgSxrytoA=; b=u+kVoBLbLajl9QDJRDWxaY+83/ kr4BsivC1jiJYACOGKucrHQfU91AaP4S9sGGpl7EKOSXufEKnwDNcx5x1ndBpwRAd3MNeKwRB5xx0 owgw2wA+y00Bj+5ZjaPZol5Vcf/0aacpfcKfE5/UJBfJrfHgp5fCpVjRONDEwJq4MjFgyyOw66Rj+ fLNsCmO+GnbPmIE/rSKL8DWiYtY7yjA0WrdQ7HSBd9WQlgkeX33AdEKH+HWnXW56nIuY/pMiAqobL kEFCkRYWdczGCkwnFanEM9m/XWvVS3kTKyQYE8KnvURJCYvYW9+UPN5TnjnCAuobFn4XCU5WtCqgi 5Mbj0H5A==; Received: from i7.infradead.org ([2001:8b0:10b:1:21e:67ff:fecb:7a92]) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kXPRe-00009T-1M; Tue, 27 Oct 2020 13:55:26 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.93 #3 (Red Hat Linux)) id 1kXPRd-002iLb-KB; Tue, 27 Oct 2020 13:55:25 +0000 From: David Woodhouse To: bonzini@redhat.com Cc: Alex Williamson , Cornelia Huck , Alexander Viro , Jens Axboe , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 1/3] eventfd: Export eventfd_ctx_do_read() Date: Tue, 27 Oct 2020 13:55:21 +0000 Message-Id: <20201027135523.646811-2-dwmw2@infradead.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201027135523.646811-1-dwmw2@infradead.org> References: <1faa5405-3640-f4ad-5cd9-89a9e5e834e9@redhat.com> <20201027135523.646811-1-dwmw2@infradead.org> MIME-Version: 1.0 Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: David Woodhouse Where events are consumed in the kernel, for example by KVM's irqfd_wakeup() and VFIO's virqfd_wakeup(), they currently lack a mechanism to drain the eventfd's counter. Since the wait queue is already locked while the wakeup functions are invoked, all they really need to do is call eventfd_ctx_do_read(). Add a check for the lock, and export it for them. Signed-off-by: David Woodhouse --- fs/eventfd.c | 5 ++++- include/linux/eventfd.h | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/fs/eventfd.c b/fs/eventfd.c index df466ef81ddd..e265b6dd4f34 100644 --- a/fs/eventfd.c +++ b/fs/eventfd.c @@ -182,11 +182,14 @@ static __poll_t eventfd_poll(struct file *file, poll_table *wait) return events; } -static void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt) +void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt) { + lockdep_assert_held(&ctx->wqh.lock); + *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; ctx->count -= *cnt; } +EXPORT_SYMBOL_GPL(eventfd_ctx_do_read); /** * eventfd_ctx_remove_wait_queue - Read the current counter and removes wait queue. diff --git a/include/linux/eventfd.h b/include/linux/eventfd.h index dc4fd8a6644d..fa0a524baed0 100644 --- a/include/linux/eventfd.h +++ b/include/linux/eventfd.h @@ -41,6 +41,7 @@ struct eventfd_ctx *eventfd_ctx_fileget(struct file *file); __u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n); int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *wait, __u64 *cnt); +void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt); DECLARE_PER_CPU(int, eventfd_wake_count); @@ -82,6 +83,11 @@ static inline bool eventfd_signal_count(void) return false; } +static inline void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt) +{ + +} + #endif #endif /* _LINUX_EVENTFD_H */ From patchwork Tue Oct 27 13:55:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 11860505 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6F70C5517A for ; Tue, 27 Oct 2020 13:55:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 74A0E218AC for ; Tue, 27 Oct 2020 13:55:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="ayd+EFaO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S368023AbgJ0Nzl (ORCPT ); Tue, 27 Oct 2020 09:55:41 -0400 Received: from casper.infradead.org ([90.155.50.34]:41596 "EHLO casper.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S367999AbgJ0Nzf (ORCPT ); Tue, 27 Oct 2020 09:55:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=BqdW9CfmpKED8RdaKfg/p1LH4cUJNyLSfMPsnBACzrg=; b=ayd+EFaOHDonl9ijp5DaxXfLjC x5kSIVTA/AhbaI2jdhSvjseIlT2jMGyjSl9VDVpGyxazEiwMWFBWjpQ2DWPxBkWJf794aDW0r8hNm oGX6fACSuT5VYCB9PBMhZx524z7IHNwWf/6N5o6hWj3FqjbSFdLVJYRbbG/VQP8TrDKRUQeA/g269 bd/nFkfqgGinbOqFca8N44SSwEDtRnCbyDldS7UkGYzWPQ+80cSoYIkbRDZcHoCi5q52QXs1Q7haG dRfdzIrrb/si5v/x5aPGwlWuFAklphNrI1/grS7BHw43WIhlBvl4vJBlEWxr4iGrc/H7won8SFPs6 zHqDKCbw==; Received: from i7.infradead.org ([2001:8b0:10b:1:21e:67ff:fecb:7a92]) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kXPRe-00009U-2F; Tue, 27 Oct 2020 13:55:26 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.93 #3 (Red Hat Linux)) id 1kXPRd-002iLg-Ku; Tue, 27 Oct 2020 13:55:25 +0000 From: David Woodhouse To: bonzini@redhat.com Cc: Alex Williamson , Cornelia Huck , Alexander Viro , Jens Axboe , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 2/3] vfio/virqfd: Drain events from eventfd in virqfd_wakeup() Date: Tue, 27 Oct 2020 13:55:22 +0000 Message-Id: <20201027135523.646811-3-dwmw2@infradead.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201027135523.646811-1-dwmw2@infradead.org> References: <1faa5405-3640-f4ad-5cd9-89a9e5e834e9@redhat.com> <20201027135523.646811-1-dwmw2@infradead.org> MIME-Version: 1.0 Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: David Woodhouse Don't allow the events to accumulate in the eventfd counter, drain them as they are handled. Signed-off-by: David Woodhouse Acked-by: Alex Williamson --- drivers/vfio/virqfd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/vfio/virqfd.c b/drivers/vfio/virqfd.c index 997cb5d0a657..414e98d82b02 100644 --- a/drivers/vfio/virqfd.c +++ b/drivers/vfio/virqfd.c @@ -46,6 +46,9 @@ static int virqfd_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, void __poll_t flags = key_to_poll(key); if (flags & EPOLLIN) { + u64 cnt; + eventfd_ctx_do_read(virqfd->eventfd, &cnt); + /* An event has been signaled, call function */ if ((!virqfd->handler || virqfd->handler(virqfd->opaque, virqfd->data)) && From patchwork Tue Oct 27 13:55:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 11860501 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84DE2C55178 for ; Tue, 27 Oct 2020 13:55:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4AB8F21655 for ; Tue, 27 Oct 2020 13:55:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="u3BI0eTH" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2900855AbgJ0Nza (ORCPT ); Tue, 27 Oct 2020 09:55:30 -0400 Received: from casper.infradead.org ([90.155.50.34]:41550 "EHLO casper.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438386AbgJ0Nza (ORCPT ); Tue, 27 Oct 2020 09:55:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=RaIfHC7BLM4SW2hHIHWeJJ18AZKbmgeAZRKzDoH2DFk=; b=u3BI0eTHwiqt8yMwHim8BcwRqs wT0SyhzyBONY5MrN1aN/NK3iJctEBVEGEFbkfp/6LCLoKV/Q0Zw7vYHdM8I3q7s0iM3UTzuX+HrwX kzOjvWsf7KWwrO09ZUBl3C/HCyUiExt1ZCQl0gZ6GdAG71//8fdzZS/I31RnRXJSApavJ6v6tru1C VZ9n/BiVBkt2Y4so2ZttBOStebLJDz2uOZa0sqapoQGHONWmRYaBbL4n4/f1vunw2sdrJV5jkuLUe bIIFYXxDk2KlGDISrlhD9XZPq9/veawNUfY/2ZriN7BQR2+QooEA5T7zfumUS5+lLaRuSxQ4gGk3t 8maXGEWA==; Received: from i7.infradead.org ([2001:8b0:10b:1:21e:67ff:fecb:7a92]) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kXPRe-00009V-3P; Tue, 27 Oct 2020 13:55:26 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.93 #3 (Red Hat Linux)) id 1kXPRd-002iLl-La; Tue, 27 Oct 2020 13:55:25 +0000 From: David Woodhouse To: bonzini@redhat.com Cc: Alex Williamson , Cornelia Huck , Alexander Viro , Jens Axboe , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 3/3] kvm/eventfd: Drain events from eventfd in irqfd_wakeup() Date: Tue, 27 Oct 2020 13:55:23 +0000 Message-Id: <20201027135523.646811-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201027135523.646811-1-dwmw2@infradead.org> References: <1faa5405-3640-f4ad-5cd9-89a9e5e834e9@redhat.com> <20201027135523.646811-1-dwmw2@infradead.org> MIME-Version: 1.0 Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: David Woodhouse Don't allow the events to accumulate in the eventfd counter, drain them as they are handled. Signed-off-by: David Woodhouse Reported-by: kernel test robot Reported-by: kernel test robot Reported-by: kernel test robot --- virt/kvm/eventfd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index d6408bb497dc..98b5cfa1d69f 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -193,6 +193,9 @@ irqfd_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) int idx; if (flags & EPOLLIN) { + u64 cnt; + eventfd_ctx_do_read(&irqfd->eventfd, &cnt); + idx = srcu_read_lock(&kvm->irq_srcu); do { seq = read_seqcount_begin(&irqfd->irq_entry_sc);