Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4303874pxu; Wed, 9 Dec 2020 13:36:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJxy36FUhvz7kP5LFIym3gtQhL7vwQzby3E8JOMdc4ZuPA4vhXUrKcYCQ+9LJlSNMuRrMkZL X-Received: by 2002:a17:907:3fa3:: with SMTP id hr35mr3733602ejc.71.1607549803407; Wed, 09 Dec 2020 13:36:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607549803; cv=none; d=google.com; s=arc-20160816; b=SEowaSRgR5IX7pK5mF6dQcEtl8Wv+2UiVqU6PF1uKy4AHWhrlyXOr1lWEKVEpSsY/h 1/ESxt+amjcLFGRCD32PqpE5AZQgLVSyufZnGUrCQh8MYWvprQF3hxlbRDQ3JhXf3kMn +jCn9p3cybkl0BL6hNOlzZAR4Tje9erSdhMuXXO4y4rHlNWe0ASkfgu8ORHChyjBJr8g jMWZMzYf5orlk82wMymfkHx2Ht14d6dlhEyJMwF9r9sdtmJyBEZo1rhEw2zOpri2rdAO g6f6DyHDTACj/x86Vnyx9onS8lFCh+GgMZz5weHBeYkrxm45kxgtlbFAi2KLwIiuK98a fzCg== 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 :message-id:date:subject:cc:to:from; bh=HFtENaN3YipfkFNUA7L94dspL60t4AvimGRvrQlq7ds=; b=MfE6l8al+4pAHR8P+1qvb8m5TL34KmY+rQELc89w1gl9Sv5+U+0fyCzM2yO/lU99e/ tR3D/xFexMNYvs8XdXjZcdUCEWGwmNYfBCehhiAQh3C63tE2Mf7FKSH1Wce9/AOHtsDl zm/3SgvCYLk6E1H/cBK2uNLBjK8KKaHYJ9RcJ9ftvWQH+DPUW0avbmfKYK/1A7nKD2fZ AlfCdP/3r17ocKszpx/JgVY54wFX5nFVgssjgvuy1AJz+xlSVBmtek788d6b8WXKryo2 Ifp148vzFd5M7gkcSK/20rmvWD0w2GzCyklV6sUyRqUjlKxLzbyjBfHPlZCbc5SKrSdl M7Pg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r22si1506369edv.428.2020.12.09.13.36.20; Wed, 09 Dec 2020 13:36:43 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388138AbgLIVeZ (ORCPT + 99 others); Wed, 9 Dec 2020 16:34:25 -0500 Received: from mx2.suse.de ([195.135.220.15]:34018 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388045AbgLIVeY (ORCPT ); Wed, 9 Dec 2020 16:34:24 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 74ACFACE1; Wed, 9 Dec 2020 21:33:36 +0000 (UTC) From: Enzo Matsumiya To: linux-nvme@lists.infradead.org Cc: Enzo Matsumiya , Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , linux-kernel@vger.kernel.org Subject: [PATCH] nvme: hwmon: fix crash on device teardown Date: Wed, 9 Dec 2020 18:32:27 -0300 Message-Id: <20201209213228.5044-1-ematsumiya@suse.de> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix a possible NULL pointer dereference when trying to read hwmon sysfs entries associated to NVMe-oF devices that were hot-removed or disconnected. Unregister the NVMe hwmon device upon controller teardown (nvme_stop_ctrl()). Signed-off-by: Enzo Matsumiya --- drivers/nvme/host/core.c | 1 + drivers/nvme/host/hwmon.c | 8 ++++++++ drivers/nvme/host/nvme.h | 2 ++ 3 files changed, 11 insertions(+) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index 9a270e49df17..becc80a0c3b8 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -4344,6 +4344,7 @@ EXPORT_SYMBOL_GPL(nvme_complete_async_event); void nvme_stop_ctrl(struct nvme_ctrl *ctrl) { + nvme_hwmon_exit(ctrl); nvme_mpath_stop(ctrl); nvme_stop_keep_alive(ctrl); flush_work(&ctrl->async_event_work); diff --git a/drivers/nvme/host/hwmon.c b/drivers/nvme/host/hwmon.c index 552dbc04567b..7f62cca4c577 100644 --- a/drivers/nvme/host/hwmon.c +++ b/drivers/nvme/host/hwmon.c @@ -71,6 +71,9 @@ static int nvme_hwmon_read(struct device *dev, enum hwmon_sensor_types type, int temp; int err; + if (data->ctrl->state != NVME_CTRL_LIVE) + return -EAGAIN; + /* * First handle attributes which don't require us to read * the smart log. @@ -253,3 +256,8 @@ int nvme_hwmon_init(struct nvme_ctrl *ctrl) return 0; } + +void nvme_hwmon_exit(struct nvme_ctrl *ctrl) +{ + hwmon_device_unregister(ctrl->dev); +} diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h index 567f7ad18a91..621e9b1575f6 100644 --- a/drivers/nvme/host/nvme.h +++ b/drivers/nvme/host/nvme.h @@ -807,11 +807,13 @@ static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev) #ifdef CONFIG_NVME_HWMON int nvme_hwmon_init(struct nvme_ctrl *ctrl); +void nvme_hwmon_exit(struct nvme_ctrl *ctrl); #else static inline int nvme_hwmon_init(struct nvme_ctrl *ctrl) { return 0; } +static inline void nvme_hwmon_exit(struct nvme_ctrl *ctrl) { } #endif u32 nvme_command_effects(struct nvme_ctrl *ctrl, struct nvme_ns *ns, -- 2.29.2