From patchwork Thu May 12 17:57:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 9085181 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D8FDD9FB96 for ; Thu, 12 May 2016 17:58:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0428D2025A for ; Thu, 12 May 2016 17:58:07 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id B49B920251 for ; Thu, 12 May 2016 17:58:05 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 442816E924; Thu, 12 May 2016 17:58:05 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8EC266E924 for ; Thu, 12 May 2016 17:58:03 +0000 (UTC) Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 42D74C05B1DB; Thu, 12 May 2016 17:58:03 +0000 (UTC) Received: from shalem.localdomain.com (vpn1-7-160.ams2.redhat.com [10.36.7.160]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4CHvx20027048; Thu, 12 May 2016 13:58:01 -0400 From: Hans de Goede To: Chris Wilson Date: Thu, 12 May 2016 19:57:57 +0200 Message-Id: <1463075877-11916-1-git-send-email-hdegoede@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 12 May 2016 17:58:03 +0000 (UTC) Cc: Hans de Goede , intel-gfx@lists.freedesktop.org Subject: [Intel-gfx] [PATCH xf86-video-intel] Fix fd (and mem) leak when intel_scrn_create fails X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The probe functions in intel_module.c call intel_open_device() before calling intel_scrn_create(), but if the later fails because of e.g. an unsupported (new) pci-id they were not cleaning up the resources claimed by intel_open_device(), esp. leaking the fd is a problem because this breaks the fallback to the modesetting driver. This commit fixes this by adding a intel_close_device() cleanup function and calling that when intel_scrn_create() fails. Signed-off-by: Hans de Goede --- src/intel_device.c | 17 +++++++++++++++++ src/intel_driver.h | 1 + src/intel_module.c | 19 ++++++++++++++++--- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/intel_device.c b/src/intel_device.c index 54c1443..71fd851 100644 --- a/src/intel_device.c +++ b/src/intel_device.c @@ -643,6 +643,23 @@ err_path: return -1; } +void intel_close_device(int entity_num) +{ + struct intel_device *dev; + + dev = xf86GetEntityPrivate(entity_num, intel_device_key)->ptr; + + xf86GetEntityPrivate(entity_num, intel_device_key)->ptr = NULL; + + if (dev->master_count == 0) /* Don't close server-fds */ + close(dev->fd); + + if (dev->render_node != dev->master_node) + free(dev->render_node); + free(dev->master_node); + free(dev); +} + int __intel_peek_fd(ScrnInfoPtr scrn) { struct intel_device *dev; diff --git a/src/intel_driver.h b/src/intel_driver.h index fc9beaf..bece88a 100644 --- a/src/intel_driver.h +++ b/src/intel_driver.h @@ -124,6 +124,7 @@ int intel_entity_get_devid(int index); int intel_open_device(int entity_num, const struct pci_device *pci, struct xf86_platform_device *dev); +void intel_close_device(int entity_num); int __intel_peek_fd(ScrnInfoPtr scrn); struct intel_device *intel_get_device(ScrnInfoPtr scrn, int *fd); int intel_has_render_node(struct intel_device *dev); diff --git a/src/intel_module.c b/src/intel_module.c index 5979cb9..8dc1cc7 100644 --- a/src/intel_module.c +++ b/src/intel_module.c @@ -638,6 +638,8 @@ static Bool intel_pci_probe(DriverPtr driver, struct pci_device *pci, intptr_t match_data) { + Bool result; + if (intel_open_device(entity_num, pci, NULL) == -1) { #if UMS switch (pci->device_id) { @@ -655,7 +657,11 @@ static Bool intel_pci_probe(DriverPtr driver, #endif } - return intel_scrn_create(driver, entity_num, match_data, 0); + result = intel_scrn_create(driver, entity_num, match_data, 0); + if (!result) + intel_close_device(entity_num); + + return result; } #ifdef XSERVER_PLATFORM_BUS @@ -666,6 +672,7 @@ intel_platform_probe(DriverPtr driver, intptr_t match_data) { unsigned scrn_flags = 0; + Bool result; if (intel_open_device(entity_num, dev->pdev, dev) == -1) return FALSE; @@ -677,10 +684,16 @@ intel_platform_probe(DriverPtr driver, } /* if we get any flags we don't understand fail to probe for now */ - if (flags) + if (flags) { + intel_close_device(entity_num); return FALSE; + } + + result = intel_scrn_create(driver, entity_num, match_data, scrn_flags); + if (!result) + intel_close_device(entity_num); - return intel_scrn_create(driver, entity_num, match_data, scrn_flags); + return result; } #endif