From patchwork Thu Jul 12 17:23:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 10522017 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C06C2602C8 for ; Thu, 12 Jul 2018 17:23:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A21FA29B3E for ; Thu, 12 Jul 2018 17:23:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9673B29B68; Thu, 12 Jul 2018 17:23:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1728829B3E for ; Thu, 12 Jul 2018 17:23:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732378AbeGLReB (ORCPT ); Thu, 12 Jul 2018 13:34:01 -0400 Received: from mga03.intel.com ([134.134.136.65]:5981 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726528AbeGLReB (ORCPT ); Thu, 12 Jul 2018 13:34:01 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Jul 2018 10:23:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,344,1526367600"; d="scan'208";a="71842304" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 12 Jul 2018 10:23:28 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 1EA9517F; Thu, 12 Jul 2018 20:23:34 +0300 (EEST) From: Andy Shevchenko To: "Rafael J. Wysocki" , linux-acpi@vger.kernel.org, Linus Walleij , linux-gpio@vger.kernel.org, Hans de Goede , Pierre-Louis Bossart Cc: Andy Shevchenko Subject: [PATCH v1 1/2] ACPI / utils: Introduce acpi_dev_get_first_device() helper Date: Thu, 12 Jul 2018 20:23:31 +0300 Message-Id: <20180712172332.61399-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.18.0 Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP acpi_dev_present() and acpi_dev_get_first_match_name() are missing put_device() call and thus keeping reference counting unbalanced. In order to fix the issue introduce a new helper to convert existing users one-by-one to a better API. Signed-off-by: Andy Shevchenko --- drivers/acpi/utils.c | 50 ++++++++++++++++++++++++----------------- include/acpi/acpi_bus.h | 2 ++ include/linux/acpi.h | 6 +++++ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/drivers/acpi/utils.c b/drivers/acpi/utils.c index 78db97687f26..b54651b3d4bd 100644 --- a/drivers/acpi/utils.c +++ b/drivers/acpi/utils.c @@ -738,7 +738,6 @@ bool acpi_dev_found(const char *hid) EXPORT_SYMBOL(acpi_dev_found); struct acpi_dev_match_info { - const char *dev_name; struct acpi_device_id hid[2]; const char *uid; s64 hrv; @@ -758,8 +757,6 @@ static int acpi_dev_match_cb(struct device *dev, void *data) strcmp(adev->pnp.unique_id, match->uid))) return 0; - match->dev_name = acpi_dev_name(adev); - if (match->hrv == -1) return 1; @@ -771,18 +768,18 @@ static int acpi_dev_match_cb(struct device *dev, void *data) } /** - * acpi_dev_present - Detect that a given ACPI device is present + * acpi_dev_get_first_match - Return a first match of ACPI device if present * @hid: Hardware ID of the device. * @uid: Unique ID of the device, pass NULL to not check _UID * @hrv: Hardware Revision of the device, pass -1 to not check _HRV * - * Return %true if a matching device was present at the moment of invocation. - * Note that if the device is pluggable, it may since have disappeared. + * Return a pointer to the first matching ACPI device. + * Caller must put device back to balance reference counting. * * Note that unlike acpi_dev_found() this function checks the status - * of the device. So for devices which are present in the dsdt, but + * of the device. So for devices which are present in the DSDT, but * which are disabled (their _STA callback returns 0) this function - * will return false. + * will return NULL. * * For this function to work, acpi_bus_scan() must have been executed * which happens in the subsys_initcall() subsection. Hence, do not @@ -790,7 +787,8 @@ static int acpi_dev_match_cb(struct device *dev, void *data) * instead). Calling from module_init() is fine (which is synonymous * with device_initcall()). */ -bool acpi_dev_present(const char *hid, const char *uid, s64 hrv) +struct acpi_device * +acpi_dev_get_first_match(const char *hid, const char *uid, s64 hrv) { struct acpi_dev_match_info match = {}; struct device *dev; @@ -800,7 +798,25 @@ bool acpi_dev_present(const char *hid, const char *uid, s64 hrv) match.hrv = hrv; dev = bus_find_device(&acpi_bus_type, NULL, &match, acpi_dev_match_cb); - return !!dev; + return dev ? to_acpi_device(dev) : NULL; +} +EXPORT_SYMBOL(acpi_dev_get_first_match); + +/** + * acpi_dev_present - Detect that a given ACPI device is present + * @hid: Hardware ID of the device. + * @uid: Unique ID of the device, pass NULL to not check _UID + * @hrv: Hardware Revision of the device, pass -1 to not check _HRV + * + * DEPRECATED, use acpi_dev_get_first_match() directly! + * + * Return %true if a matching device is present. + */ +bool acpi_dev_present(const char *hid, const char *uid, s64 hrv) +{ + struct acpi_device *adev = acpi_dev_get_first_match(hid, uid, hrv); + + return !!adev; } EXPORT_SYMBOL(acpi_dev_present); @@ -810,23 +826,17 @@ EXPORT_SYMBOL(acpi_dev_present); * @uid: Unique ID of the device, pass NULL to not check _UID * @hrv: Hardware Revision of the device, pass -1 to not check _HRV * + * DEPRECATED, use acpi_dev_get_first_match() directly! + * * Return device name if a matching device was present * at the moment of invocation, or NULL otherwise. - * - * See additional information in acpi_dev_present() as well. */ const char * acpi_dev_get_first_match_name(const char *hid, const char *uid, s64 hrv) { - struct acpi_dev_match_info match = {}; - struct device *dev; - - strlcpy(match.hid[0].id, hid, sizeof(match.hid[0].id)); - match.uid = uid; - match.hrv = hrv; + struct acpi_device *adev = acpi_dev_get_first_match(hid, uid, hrv); - dev = bus_find_device(&acpi_bus_type, NULL, &match, acpi_dev_match_cb); - return dev ? match.dev_name : NULL; + return adev ? acpi_dev_name(adev) : NULL; } EXPORT_SYMBOL(acpi_dev_get_first_match_name); diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index ba4dd54f2c82..53ca4403f772 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -91,6 +91,8 @@ acpi_evaluate_dsm_typed(acpi_handle handle, const guid_t *guid, u64 rev, bool acpi_dev_found(const char *hid); bool acpi_dev_present(const char *hid, const char *uid, s64 hrv); +struct acpi_device * +acpi_dev_get_first_match(const char *hid, const char *uid, s64 hrv); const char * acpi_dev_get_first_match_name(const char *hid, const char *uid, s64 hrv); diff --git a/include/linux/acpi.h b/include/linux/acpi.h index e54f40974eb0..098e0af003b4 100644 --- a/include/linux/acpi.h +++ b/include/linux/acpi.h @@ -657,6 +657,12 @@ static inline bool acpi_dev_present(const char *hid, const char *uid, s64 hrv) return false; } +struct acpi_device * +acpi_dev_get_first_match(const char *hid, const char *uid, s64 hrv) +{ + return NULL; +} + static inline const char * acpi_dev_get_first_match_name(const char *hid, const char *uid, s64 hrv) {