Received: by 2002:a25:1104:0:0:0:0:0 with SMTP id 4csp149082ybr; Fri, 22 May 2020 03:18:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzULSnOGiVf+pnwvF+9S309T0FIrG9CjDjwGaMcoyx8XXGNSEnPPxy7zPeNA7tGN9NuI2Fy X-Received: by 2002:a17:906:16c2:: with SMTP id t2mr7117677ejd.17.1590142693832; Fri, 22 May 2020 03:18:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590142693; cv=none; d=google.com; s=arc-20160816; b=F6MjdcuUNICVR79wmN/+fxuljF3X0ITVyS28e2OCtsC9jrRvqAQuT3BSFwDTiPYefT MQ4tPH/iqHu3UzGDhK/uwzqIQeEzjDbQYselcl7k9jalgHvtv9Xt1CqTgaV6sBK3RErp /UwA9GqFokDdChFlOe3bgfmJe2FW4SuzMJe/Vtiwr1xTE28K+NONzuiUNdrXzhyaSaFx T8PVUiscicgDiRteOBQ9QFKWXT3NUN4ax+Y8w6SylkqjzXm4Uh6Dmtk9Bt1tdxWh+FRO F7sIMW4h9jTDfSdZD6Oa+vh5uPhmygKWMDRxTvYTQfEwIVrTzZrUpUGABtVHkIxcU2yH 6Ljg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=3HYxB7x/RUYeYsXTIciTAB5xWK8LkdywlB5eocgLdQ0=; b=xlIT0HXJ0Kui64++LDvM2cUMzak5HQA8tapjwatFRC5QibsfqJHvJQYcnhrQJzh3l0 iw4zG18SICUAHztVTg/KCLkL3oKt1l/DYvHkAsybrUJyCQivOw12ZleWjIwl+GS1sWhJ JwWFnUBUdW0loj9NtenWZnkK3bh9ONlEbc2mV3L1CgaRMXr5hk9bFTzvk7X3F6xQYVM8 AXvqJEP94WiyX/EebU/FQZzw8GJa04P/PPRL1O/SJ6OjBn1nVlfX6ePI08HJwMOeMokd EbhIeVegfGGSAZdOJpkiD2OfNCl8oKwK29rswHlMjh8mIpjviBqfH1eLKp+zUeGjRbWv eOaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=IekGMGPO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s1si3922407eja.243.2020.05.22.03.17.49; Fri, 22 May 2020 03:18:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=IekGMGPO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729997AbgEVKPa (ORCPT + 99 others); Fri, 22 May 2020 06:15:30 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:60925 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729994AbgEVKP1 (ORCPT ); Fri, 22 May 2020 06:15:27 -0400 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20200522101525euoutp01fa6652e7dceaf538dc37156bfdedfa8e~RUjObqn892709827098euoutp012 for ; Fri, 22 May 2020 10:15:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20200522101525euoutp01fa6652e7dceaf538dc37156bfdedfa8e~RUjObqn892709827098euoutp012 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1590142525; bh=3HYxB7x/RUYeYsXTIciTAB5xWK8LkdywlB5eocgLdQ0=; h=From:To:Cc:Subject:Date:References:From; b=IekGMGPOzNhxP44nfZreaPriyNBk7hAoPdvbmvNiXz/ln33P04aQdABFviLn9mFCY B4oRMo0mEvEi+dU7Fk1aEsE05saDekiAouBBkglyiASqGrWEAkmNiQXWWO51ZR7etb jaCZluQmzgNXOpkA6YCdTUrejKKdsOzAooVFfMHM= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20200522101524eucas1p29f7a3412e8b0e5dac432cff706978a68~RUjOG-Jrb0131201312eucas1p2v; Fri, 22 May 2020 10:15:24 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 82.7F.60679.C36A7CE5; Fri, 22 May 2020 11:15:24 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20200522101524eucas1p1aeef4a054a80b5d822ed3dc4b16139d7~RUjNtIl9n1170811708eucas1p1o; Fri, 22 May 2020 10:15:24 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20200522101524eusmtrp1e6df08f904aec341f6609ab7cbf267f0~RUjNscnWn2581025810eusmtrp1m; Fri, 22 May 2020 10:15:24 +0000 (GMT) X-AuditID: cbfec7f4-0e5ff7000001ed07-eb-5ec7a63c203f Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id C1.9B.07950.C36A7CE5; Fri, 22 May 2020 11:15:24 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20200522101523eusmtip1cdfc4bd183275555e857da2166e0bd3b~RUjNMSTAp2127421274eusmtip19; Fri, 22 May 2020 10:15:23 +0000 (GMT) From: Marek Szyprowski To: linux-pm@vger.kernel.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Marek Szyprowski , Wolfram Sang , Bibby Hsieh , Tomasz Figa , Bartosz Golaszewski , srv_heupstream@mediatek.com, Bartlomiej Zolnierkiewicz , linux-samsung-soc@vger.kernel.org Subject: [PATCH] i2c: core: fix NULL pointer dereference in suspend/resume callbacks Date: Fri, 22 May 2020 12:13:27 +0200 Message-Id: <20200522101327.13456-1-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpnleLIzCtJLcpLzFFi42LZduznOV2bZcfjDG6dV7HYOGM9q8WXuadY LGZumMFi0fH3C6PF5V1z2Cw+9x5htJhxfh+Txdojd9ktpt8Vsvjc+o/NYuWJWcwO3B7vb7Sy e8xuuMji0XJyP4tH35ZVjB4nTz1h8fi8SS6ALYrLJiU1J7MstUjfLoEr48rJ66wFT5Qq1k+6 wtLAuFW2i5GDQ0LAROL2NK4uRi4OIYEVjBLn+w8zQzhfGCVuNM9hhXA+M0q8X3+HpYuRE6xj 291GJhBbSGA5o8TfIw5wHfPvfgIrYhMwlOh628UGYosIhEt8uXGbDaSIWeAUk8S1/72MIAlh gTCJV78fsILYLAKqEp9+PQebyitgK/Hl3h1miG3yEqs3HAC7SULgN5vE1ocXoBIuEkeWr4ay hSVeHd/CDmHLSJye3MMC0dDMKPHw3Fp2CKeHUeJy0wxGiCpriTvnfrGBgoBZQFNi/S59iLCj xKa/V5ghIcMnceOtIEiYGcictG06VJhXoqNNCKJaTWLW8XVwaw9euAR1jofE2xUnGSEhFCtx 6fkX5gmMcrMQdi1gZFzFKJ5aWpybnlpslJdarlecmFtcmpeul5yfu4kRmDxO/zv+ZQfjrj9J hxgFOBiVeHgfJB+LE2JNLCuuzD3EKMHBrCTCu5D/aJwQb0piZVVqUX58UWlOavE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IuKEwFeZN0KcQIAAA== X-CMS-MailID: 20200522101524eucas1p1aeef4a054a80b5d822ed3dc4b16139d7 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20200522101524eucas1p1aeef4a054a80b5d822ed3dc4b16139d7 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200522101524eucas1p1aeef4a054a80b5d822ed3dc4b16139d7 References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit 6fe12cdbcfe3 ("i2c: core: support bus regulator controlling in adapter") added generic suspend and resume functions for i2c devices. Those functions unconditionally access an i2c_client structure assigned to the given i2c device. However, there exist i2c devices in the system without a valid i2c_client. Add the needed check before accessing the i2c_client. This fixes the following issue observed on Samsung Exynos4412-based Odroid U3 board: 8<--- cut here --- Unable to handle kernel NULL pointer dereference at virtual address 00000018 pgd = 2aed198a [00000018] *pgd=00000000 Internal error: Oops: 5 [#1] PREEMPT SMP ARM Modules linked in: CPU: 1 PID: 1295 Comm: rtcwake Not tainted 5.7.0-rc6-02700-g4773d1324da6 #739 Hardware name: Samsung Exynos (Flattened Device Tree) PC is at i2c_suspend_late+0x20/0x48 LR is at dpm_run_callback+0xb4/0x3fc pc : [] lr : [] psr: 20000053 ... Process rtcwake (pid: 1295, stack limit = 0x7f1885cf) Stack: (0xec8f3d70 to 0xec8f4000) ... [] (i2c_suspend_late) from [] (dpm_run_callback+0xb4/0x3fc) [] (dpm_run_callback) from [] (__device_suspend_late+0xcc/0x16c) [] (__device_suspend_late) from [] (dpm_suspend_late+0x10c/0x568) [] (dpm_suspend_late) from [] (suspend_devices_and_enter+0x31c/0xc70) [] (suspend_devices_and_enter) from [] (pm_suspend+0x3f8/0x480) [] (pm_suspend) from [] (state_store+0x6c/0xc8) [] (state_store) from [] (kernfs_fop_write+0x10c/0x228) [] (kernfs_fop_write) from [] (__vfs_write+0x30/0x1d0) [] (__vfs_write) from [] (vfs_write+0xa4/0x170) [] (vfs_write) from [] (ksys_write+0x60/0xd8) [] (ksys_write) from [] (ret_fast_syscall+0x0/0x28) Exception stack(0xec8f3fa8 to 0xec8f3ff0) ... ---[ end trace a43afef431782f37 ]--- Fixes: 6fe12cdbcfe3 ("i2c: core: support bus regulator controlling in adapter") Signed-off-by: Marek Szyprowski --- This fixes suspend/resume issue observed on various board with linux-next from 20200521. --- drivers/i2c/i2c-core-base.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index 5be24bf8a194..b531f5ad06b2 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -454,11 +454,13 @@ static int i2c_device_remove(struct device *dev) static int i2c_resume_early(struct device *dev) { struct i2c_client *client = i2c_verify_client(dev); - struct i2c_adapter *adap = client->adapter; int err; + if (!client) + return 0; + if (!pm_runtime_status_suspended(&client->dev)) { - err = regulator_enable(adap->bus_regulator); + err = regulator_enable(client->adapter->bus_regulator); if (err) return err; } @@ -469,15 +471,17 @@ static int i2c_resume_early(struct device *dev) static int i2c_suspend_late(struct device *dev) { struct i2c_client *client = i2c_verify_client(dev); - struct i2c_adapter *adap = client->adapter; int err; + if (!client) + return 0; + err = pm_generic_suspend_late(&client->dev); if (err) return err; if (!pm_runtime_status_suspended(&client->dev)) - return regulator_disable(adap->bus_regulator); + return regulator_disable(client->adapter->bus_regulator); return 0; } @@ -487,10 +491,12 @@ static int i2c_suspend_late(struct device *dev) static int i2c_runtime_resume(struct device *dev) { struct i2c_client *client = i2c_verify_client(dev); - struct i2c_adapter *adap = client->adapter; int err; - err = regulator_enable(adap->bus_regulator); + if (!client) + return 0; + + err = regulator_enable(client->adapter->bus_regulator); if (err) return err; @@ -500,14 +506,16 @@ static int i2c_runtime_resume(struct device *dev) static int i2c_runtime_suspend(struct device *dev) { struct i2c_client *client = i2c_verify_client(dev); - struct i2c_adapter *adap = client->adapter; int err; + if (!client) + return 0; + err = pm_generic_runtime_suspend(&client->dev); if (err) return err; - return regulator_disable(adap->bus_regulator); + return regulator_disable(client->adapter->bus_regulator); } #endif -- 2.17.1