From patchwork Thu Jun 6 08:50:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: oushixiong X-Patchwork-Id: 13688360 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AEDA8C27C54 for ; Thu, 6 Jun 2024 11:47:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 465E110E910; Thu, 6 Jun 2024 11:47:06 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=163.com header.i=@163.com header.b="psrqJZSX"; dkim-atps=neutral X-Greylist: delayed 2763 seconds by postgrey-1.36 at gabe; Thu, 06 Jun 2024 09:38:06 UTC Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) by gabe.freedesktop.org (Postfix) with ESMTP id 743A010E8AD for ; Thu, 6 Jun 2024 09:38:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=/5nNd xAHvn8XrVpENTsC9WADZwaWod7V36HzzLbSoTc=; b=psrqJZSXv4dSzC+cjOy1Y DSL2U+wPy3MJD5VIpSzntUHwah7WbhEYWnAjD2IPLkTyXxomCG+HiqI/GpBWMNVh t8/PqOOpJOwEtZ4ori0B2s2errC1yIpbpLUSMSaMtYyHSipQ+YbNKM+x8tnUwqk4 qzNulu9YcbhG3i6ACyesgE= Received: from localhost.localdomain (unknown [111.48.69.247]) by gzga-smtp-mta-g2-5 (Coremail) with SMTP id _____wDn77VGeGFmxpB0Dg--.65491S2; Thu, 06 Jun 2024 16:50:15 +0800 (CST) From: oushixiong1025@163.com To: Xinliang Liu Cc: Tian Tao , Xinwei Kong , Sumit Semwal , Yongqin Liu , John Stultz , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Shixiong Ou Subject: [PATCH] drm/hisilicon: Fix a NULL pointer access when call hibmc_unload Date: Thu, 6 Jun 2024 16:50:12 +0800 Message-Id: <20240606085012.284167-1-oushixiong1025@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CM-TRANSID: _____wDn77VGeGFmxpB0Dg--.65491S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxJF4kGryxGFy3XF4DGr4kZwb_yoW5Kw4UpF W7XFW3tr4vq39xGF45JrW09FZ0ka1avayIkF1Sy3s3uws0kryDXF18JF4xCFy8XrWkCF95 Z3Z7Gr4UZr1UZw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UMCJdUUUUU= X-Originating-IP: [111.48.69.247] X-CM-SenderInfo: xrxvxxx0lr0wirqskqqrwthudrp/1tbiXRjyD2XAljx7NgABs7 X-Mailman-Approved-At: Thu, 06 Jun 2024 11:47:04 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Shixiong Ou If Calling hibmc_mm_init() failed in hibmc_load(), the hibmc_unload() will access a NULL pointer, as it don't call ww_mutex_init() to initialize mode_config.connection_mutex but try to lock it when calling drm_atomic_helper_shutdown() in hibmc_unload(). [ 50.939211][ 0] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000018 ...... [ 51.149882][ 0] Call trace: [ 51.152750][ 0] ww_mutex_lock+0xf0/0x1e0 [ 51.156829][ 0] drm_modeset_lock+0x184/0x2c0 [ 51.161254][ 0] drm_modeset_lock_all_ctx+0x98/0x188 [ 51.166284][ 0] drm_atomic_helper_shutdown+0xa4/0x128 [ 51.171487][ 0] hibmc_unload+0x50/0x2f0 [ 51.175479][ 0] hibmc_load+0x5d8/0x888 [ 51.179386][ 0] drm_dev_register+0x280/0x558 [ 51.183811][ 0] drm_get_pci_dev+0x140/0x3c8 [ 51.188150][ 0] hibmc_pci_probe+0x148/0x190 [ 51.192489][ 0] local_pci_probe+0xc4/0x180 [ 51.196742][ 0] pci_device_probe+0x328/0x530 [ 51.201167][ 0] really_probe+0x498/0x9a0 [ 51.205248][ 0] driver_probe_device+0x224/0x308 [ 51.209932][ 0] device_driver_attach+0xec/0x128 [ 51.214616][ 0] __driver_attach+0x144/0x280 [ 51.218955][ 0] bus_for_each_dev+0x120/0x1a0 [ 51.223380][ 0] driver_attach+0x48/0x60 [ 51.227372][ 0] bus_add_driver+0x328/0x578 [ 51.231625][ 0] driver_register+0x148/0x398 [ 51.235965][ 0] __pci_register_driver+0x15c/0x1c8 [ 51.240823][ 0] hibmc_init+0x2c/0x34 [ 51.244557][ 0] do_one_initcall+0xc8/0x4a8 [ 51.248810][ 0] kernel_init_freeable+0x678/0x75c [ 51.253582][ 0] kernel_init+0x18/0x128 [ 51.257489][ 0] ret_from_fork+0x10/0x18 Add a initialized flag to avoid this. Signed-off-by: Shixiong Ou --- drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c | 8 ++++++-- drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c index 57c21ec452b7..343f64d66e75 100644 --- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c +++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c @@ -99,6 +99,7 @@ static int hibmc_kms_init(struct hibmc_drm_private *priv) ret = drmm_mode_config_init(dev); if (ret) return ret; + priv->mode_config_initialized = true; dev->mode_config.min_width = 0; dev->mode_config.min_height = 0; @@ -240,9 +241,12 @@ static int hibmc_hw_init(struct hibmc_drm_private *priv) static int hibmc_unload(struct drm_device *dev) { struct pci_dev *pdev = to_pci_dev(dev->dev); + struct hibmc_drm_private *priv = to_hibmc_drm_private(dev); - drm_atomic_helper_shutdown(dev); - + if(priv->mode_config_initialized){ + drm_atomic_helper_shutdown(dev); + priv->mode_config_initialized = false; + } free_irq(pdev->irq, dev); pci_disable_msi(to_pci_dev(dev->dev)); diff --git a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h index 207aa3f660b0..08fd7cb59bb5 100644 --- a/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h +++ b/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h @@ -37,6 +37,7 @@ struct hibmc_drm_private { struct drm_crtc crtc; struct drm_encoder encoder; struct hibmc_connector connector; + bool mode_config_initialized; }; static inline struct hibmc_connector *to_hibmc_connector(struct drm_connector *connector)