From patchwork Wed Nov 6 15:14:24 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?VmlsbGUgU3lyasOkbMOk?= X-Patchwork-Id: 3147941 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 93F41BEEB2 for ; Wed, 6 Nov 2013 15:15:02 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F407820583 for ; Wed, 6 Nov 2013 15:15:00 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 57EBA20450 for ; Wed, 6 Nov 2013 15:14:56 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DF34AF0431; Wed, 6 Nov 2013 07:14:38 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mga14.intel.com (mga14.intel.com [143.182.124.37]) by gabe.freedesktop.org (Postfix) with ESMTP id 0C13FF03BB; Wed, 6 Nov 2013 07:14:27 -0800 (PST) Received: from azsmga001.ch.intel.com ([10.2.17.19]) by azsmga102.ch.intel.com with ESMTP; 06 Nov 2013 07:14:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.93,535,1378882800"; d="scan'208";a="385485050" Received: from stinkbox.fi.intel.com (HELO stinkbox) ([10.237.72.98]) by azsmga001.ch.intel.com with SMTP; 06 Nov 2013 07:14:24 -0800 Received: by stinkbox (sSMTP sendmail emulation); Wed, 06 Nov 2013 17:14:24 +0200 From: ville.syrjala@linux.intel.com To: dri-devel@lists.freedesktop.org Subject: [PATCH v2 2/3] drm/sysfs: Don't pollute connector->kdev if drm_sysfs_connector_add() fails Date: Wed, 6 Nov 2013 17:14:24 +0200 Message-Id: <1383750864-4060-1-git-send-email-ville.syrjala@linux.intel.com> X-Mailer: git-send-email 1.8.1.5 In-Reply-To: <87fvrbs4mr.fsf@intel.com> References: <87fvrbs4mr.fsf@intel.com> MIME-Version: 1.0 Cc: intel-gfx@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dri-devel-bounces@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org X-Spam-Status: No, score=-4.2 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 From: Ville Syrjälä Currently if drm_sysfs_connector_add() fails, it can leave connector->kdev populated with an ERR_PTR value, or pointing to an already freed device. Use a temporary kdev pointer during drm_sysfs_connector_add(), and only set connector->kdev if the function succeeds. This avoids oopsing if drm_sysfs_connector_add() gets called for a connector where drm_sysfs_connector_add() previously failed. Give drm_sysfs_device_add() the same treatment for the sake of consistency. v2: s/drm_connector_sysfs_add/drm_sysfs_connector_add Make drm_sysfs_device_add() tolerate multiple calls Reviewed-by: Jani Nikula Signed-off-by: Ville Syrjälä --- drivers/gpu/drm/drm_sysfs.c | 46 +++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/drivers/gpu/drm/drm_sysfs.c b/drivers/gpu/drm/drm_sysfs.c index 1a35ea5..95c701a 100644 --- a/drivers/gpu/drm/drm_sysfs.c +++ b/drivers/gpu/drm/drm_sysfs.c @@ -370,6 +370,7 @@ static struct bin_attribute edid_attr = { int drm_sysfs_connector_add(struct drm_connector *connector) { struct drm_device *dev = connector->dev; + struct device *kdev; int attr_cnt = 0; int opt_cnt = 0; int i; @@ -378,22 +379,22 @@ int drm_sysfs_connector_add(struct drm_connector *connector) if (connector->kdev) return 0; - connector->kdev = device_create(drm_class, dev->primary->kdev, - 0, connector, "card%d-%s", - dev->primary->index, drm_get_connector_name(connector)); + kdev = device_create(drm_class, dev->primary->kdev, + 0, connector, "card%d-%s", + dev->primary->index, drm_get_connector_name(connector)); DRM_DEBUG("adding \"%s\" to sysfs\n", drm_get_connector_name(connector)); - if (IS_ERR(connector->kdev)) { - DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev)); - ret = PTR_ERR(connector->kdev); + if (IS_ERR(kdev)) { + DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(kdev)); + ret = PTR_ERR(kdev); goto out; } /* Standard attributes */ for (attr_cnt = 0; attr_cnt < ARRAY_SIZE(connector_attrs); attr_cnt++) { - ret = device_create_file(connector->kdev, &connector_attrs[attr_cnt]); + ret = device_create_file(kdev, &connector_attrs[attr_cnt]); if (ret) goto err_out_files; } @@ -410,7 +411,7 @@ int drm_sysfs_connector_add(struct drm_connector *connector) case DRM_MODE_CONNECTOR_Component: case DRM_MODE_CONNECTOR_TV: for (opt_cnt = 0; opt_cnt < ARRAY_SIZE(connector_attrs_opt1); opt_cnt++) { - ret = device_create_file(connector->kdev, &connector_attrs_opt1[opt_cnt]); + ret = device_create_file(kdev, &connector_attrs_opt1[opt_cnt]); if (ret) goto err_out_files; } @@ -419,21 +420,23 @@ int drm_sysfs_connector_add(struct drm_connector *connector) break; } - ret = sysfs_create_bin_file(&connector->kdev->kobj, &edid_attr); + ret = sysfs_create_bin_file(&kdev->kobj, &edid_attr); if (ret) goto err_out_files; /* Let userspace know we have a new connector */ drm_sysfs_hotplug_event(dev); + connector->kdev = kdev; + return 0; err_out_files: for (i = 0; i < opt_cnt; i++) - device_remove_file(connector->kdev, &connector_attrs_opt1[i]); + device_remove_file(kdev, &connector_attrs_opt1[i]); for (i = 0; i < attr_cnt; i++) - device_remove_file(connector->kdev, &connector_attrs[i]); - device_unregister(connector->kdev); + device_remove_file(kdev, &connector_attrs[i]); + device_unregister(kdev); out: return ret; @@ -501,6 +504,10 @@ EXPORT_SYMBOL(drm_sysfs_hotplug_event); int drm_sysfs_device_add(struct drm_minor *minor) { char *minor_str; + struct device *kdev; + + if (minor->kdev) + return 0; if (minor->type == DRM_MINOR_CONTROL) minor_str = "controlD%d"; @@ -509,13 +516,16 @@ int drm_sysfs_device_add(struct drm_minor *minor) else minor_str = "card%d"; - minor->kdev = device_create(drm_class, minor->dev->dev, - MKDEV(DRM_MAJOR, minor->index), - minor, minor_str, minor->index); - if (IS_ERR(minor->kdev)) { - DRM_ERROR("device create failed %ld\n", PTR_ERR(minor->kdev)); - return PTR_ERR(minor->kdev); + kdev = device_create(drm_class, minor->dev->dev, + MKDEV(DRM_MAJOR, minor->index), + minor, minor_str, minor->index); + if (IS_ERR(kdev)) { + DRM_ERROR("device create failed %ld\n", PTR_ERR(kdev)); + return PTR_ERR(kdev); } + + minor->kdev = kdev; + return 0; }