Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1186382rdg; Fri, 13 Oct 2023 12:58:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGoevch1sNGh3EXT5QQSFGj9S6GpDfIHJWALcf+xChC6Q0FwTqAjdEh/fnBxQn9eH9vx77v X-Received: by 2002:a05:6e02:156c:b0:351:19a4:8e67 with SMTP id k12-20020a056e02156c00b0035119a48e67mr35807483ilu.28.1697227092455; Fri, 13 Oct 2023 12:58:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697227092; cv=none; d=google.com; s=arc-20160816; b=uVwCfKxIhhgt3uYEx6VR47yq3MlcMAlJbBwbhUNVXfcqJS1T7Pla5KLPedPw6wAYoH bDyymHedj36Vw1gDSJZVfZuVFBn4V6kCPdevxboUH5WPdAjq4EYbjXAstVGUWxASSQ7c ePzYRvPls8McDgzzkiVcJlwoMu1O/LEEeEMhD7SbYCciD20VZnbV0WZbh7klytVwnsLo TF1wFexneBfRQZ3m/6G/Jd10eK807168htjsLEyJlAX6Ah7xuuaTeS9UfBZ+O+Viv0Wv c5vyThGSyr0JmprULkI9SiX6w+A2aQ01sn7rAc5yNZ4+jR92CKBK0hkREdansH2jr8Hb RUzQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MsZgdilxXuo6E3ojTuBeVz+DZ8bCj4cd69shv70/6nQ=; fh=+Cb2XQ2PA67VLAGJa+jxW1LjaBXcHfLinB2fMRnx8lg=; b=n+PwrEAPZZYk5829/hWO6s6vhaQGYURNMZVSlkkt58TTuns3nlRfvvyeTj3NRBWzAG Mwm6l2y8GiF3o6Da7ijTSzerYFTfver3M6qrw7bpvoj/1qfuOYb3SLOHXSKN8JQ4QEji t/JpgjjAY7YpXrbzDMqgZnHLNW5M/NOV5xs8KBbkXPodEfILZNiG2r1mcRhxzyTYKTYt hD4w4zwSrlSAwB1bHdTgKccSxAz4Z+fPJ+lquiZAoGjNHA4TRerE752cvcCBxKrOXIG8 GRXI9yy/5IA7i3uKdsW4Em62qSV5ZNsR4vOYzzoMEpdF0U9YHSOReEmNfhjU4rjS4sd/ f5BQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NV7rlY28; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id m2-20020a056a00080200b00690258a9766si1564315pfk.373.2023.10.13.12.58.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 12:58:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NV7rlY28; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 8E0BF835D2E3; Fri, 13 Oct 2023 12:58:08 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231788AbjJMT5y (ORCPT + 99 others); Fri, 13 Oct 2023 15:57:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231718AbjJMT5x (ORCPT ); Fri, 13 Oct 2023 15:57:53 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94A20AD for ; Fri, 13 Oct 2023 12:57:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697227028; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MsZgdilxXuo6E3ojTuBeVz+DZ8bCj4cd69shv70/6nQ=; b=NV7rlY288PjFV9uQiAYlS2LegB3Gswstlcx8Y1nqr/0+IzXuak8xU6dkIFAheX6WYwXYgp OCjnyKILM4lUsd2PiK7Dqon13BkxBKjkyaNJIsSH7XkqMP5JgtQ5eVq94dyOmDEsg28DSy S0IH0K+AV6Tiws9LW5mtf88ZjON56hk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-584-hqB_LhTUOWa20Kjdy25Klw-1; Fri, 13 Oct 2023 15:57:06 -0400 X-MC-Unique: hqB_LhTUOWa20Kjdy25Klw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7942588B7AC; Fri, 13 Oct 2023 19:57:06 +0000 (UTC) Received: from omen.home.shazbot.org (unknown [10.22.10.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1EF0940C6CA0; Fri, 13 Oct 2023 19:57:06 +0000 (UTC) From: Alex Williamson To: alex.williamson@redhat.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, clg@redhat.com Subject: [PATCH 1/2] vfio/mtty: Fix eventfd leak Date: Fri, 13 Oct 2023 13:56:52 -0600 Message-Id: <20231013195653.1222141-2-alex.williamson@redhat.com> In-Reply-To: <20231013195653.1222141-1-alex.williamson@redhat.com> References: <20231013195653.1222141-1-alex.williamson@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 13 Oct 2023 12:58:08 -0700 (PDT) Found via kmemleak, eventfd context is leaked if not explicitly torn down by userspace. Clear pointers to track released contexts. Also remove unused irq_fd field in mtty structure, set but never used. Fixes: 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.") Signed-off-by: Alex Williamson --- samples/vfio-mdev/mtty.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c index 5af00387c519..0a2760818e46 100644 --- a/samples/vfio-mdev/mtty.c +++ b/samples/vfio-mdev/mtty.c @@ -127,7 +127,6 @@ struct serial_port { /* State of each mdev device */ struct mdev_state { struct vfio_device vdev; - int irq_fd; struct eventfd_ctx *intx_evtfd; struct eventfd_ctx *msi_evtfd; int irq_index; @@ -938,8 +937,10 @@ static int mtty_set_irqs(struct mdev_state *mdev_state, uint32_t flags, { if (flags & VFIO_IRQ_SET_DATA_NONE) { pr_info("%s: disable INTx\n", __func__); - if (mdev_state->intx_evtfd) + if (mdev_state->intx_evtfd) { eventfd_ctx_put(mdev_state->intx_evtfd); + mdev_state->intx_evtfd = NULL; + } break; } @@ -955,7 +956,6 @@ static int mtty_set_irqs(struct mdev_state *mdev_state, uint32_t flags, break; } mdev_state->intx_evtfd = evt; - mdev_state->irq_fd = fd; mdev_state->irq_index = index; break; } @@ -971,8 +971,10 @@ static int mtty_set_irqs(struct mdev_state *mdev_state, uint32_t flags, break; case VFIO_IRQ_SET_ACTION_TRIGGER: if (flags & VFIO_IRQ_SET_DATA_NONE) { - if (mdev_state->msi_evtfd) + if (mdev_state->msi_evtfd) { eventfd_ctx_put(mdev_state->msi_evtfd); + mdev_state->msi_evtfd = NULL; + } pr_info("%s: disable MSI\n", __func__); mdev_state->irq_index = VFIO_PCI_INTX_IRQ_INDEX; break; @@ -993,7 +995,6 @@ static int mtty_set_irqs(struct mdev_state *mdev_state, uint32_t flags, break; } mdev_state->msi_evtfd = evt; - mdev_state->irq_fd = fd; mdev_state->irq_index = index; } break; @@ -1262,6 +1263,22 @@ static unsigned int mtty_get_available(struct mdev_type *mtype) return atomic_read(&mdev_avail_ports) / type->nr_ports; } +static void mtty_close(struct vfio_device *vdev) +{ + struct mdev_state *mdev_state = + container_of(vdev, struct mdev_state, vdev); + + if (mdev_state->intx_evtfd) { + eventfd_ctx_put(mdev_state->intx_evtfd); + mdev_state->intx_evtfd = NULL; + } + if (mdev_state->msi_evtfd) { + eventfd_ctx_put(mdev_state->msi_evtfd); + mdev_state->msi_evtfd = NULL; + } + mdev_state->irq_index = -1; +} + static const struct vfio_device_ops mtty_dev_ops = { .name = "vfio-mtty", .init = mtty_init_dev, @@ -1273,6 +1290,7 @@ static const struct vfio_device_ops mtty_dev_ops = { .unbind_iommufd = vfio_iommufd_emulated_unbind, .attach_ioas = vfio_iommufd_emulated_attach_ioas, .detach_ioas = vfio_iommufd_emulated_detach_ioas, + .close_device = mtty_close, }; static struct mdev_driver mtty_driver = { -- 2.40.1