Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3188503pxv; Mon, 12 Jul 2021 11:23:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrHEAd0h75XMJP2FnW2kahuLzs1RNhe8OfCMGlSvc1l8VY2ODf/LSkQl9J73NhJMl9TKJH X-Received: by 2002:a05:6402:216:: with SMTP id t22mr179453edv.70.1626114216460; Mon, 12 Jul 2021 11:23:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626114216; cv=none; d=google.com; s=arc-20160816; b=pC/BK7HAH3ynxNWb2DJsU5FYTzud/+ZU5ACBLYmwLOciAUIcsYh31ppDwxCKODgRwB x6wza8q088SOMTbW0SpBjjCA/mZFniTBr8mI6V2bDqsbSwVf16PtnT2wvPzHvnhVLklX b/MuOL5bPcGHLr2ELQtAorXi2CrHQRIKJkgEbiGbq4piXh8QYL7Q1bM3bFu7FffOK2Pj 05yqRQlEJ2CnxWlPtT3sYrBJmVb2VQBRFmvKpdVadI6+/bHffqWaLUfFhZ3lePL0SbiL 48Jv+tlCQ8woP2XJixGJsni3bXOqOr/YryVx44OKWsEsrQqr6+r6VgALtmPVDHXKZkJc g+MA== 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=MPqH/Fva08QXjG3ymZDnCtXSBEdiME3161/RAEnXEM8=; b=Mx+8rD+Hbu57qGrwaGAPwaxfz2iZ6EbOw4V9ZXJvD8/bRyq6rEt5yiWNTNh3ILAPwE BoiT2WO74ikyBdfvzZbPzsO8tgEmo2fg21LKNANPUQqtmGs7G2e/jXEd8XB4MG6GdV0G PQizQKNBx47rmdS3VjVYhtWGiXrOPtfAFLVNykbVqQkt28VL9XSI0gn3JiozoApjcRNW 34gR07sUl3QMCFrj0j1m+AHb0EoBmrfo9UzSlfN9kiqB72hqy1+IYy/TDQxezvt/JUEF Zwrt0b2rrqXVuOw2QKXA46VsAzR1lupwy1yLgIDQ8CDxf+r96px3c9fUuDDXkPZmfwe2 YvLA== 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bl4si21335915ejb.689.2021.07.12.11.23.13; Mon, 12 Jul 2021 11:23:36 -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; 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=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235866AbhGLSYo (ORCPT + 99 others); Mon, 12 Jul 2021 14:24:44 -0400 Received: from mga17.intel.com ([192.55.52.151]:17337 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230477AbhGLSYo (ORCPT ); Mon, 12 Jul 2021 14:24:44 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10043"; a="190413164" X-IronPort-AV: E=Sophos;i="5.84,234,1620716400"; d="scan'208";a="190413164" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2021 11:21:49 -0700 X-IronPort-AV: E=Sophos;i="5.84,234,1620716400"; d="scan'208";a="569602150" Received: from smile.fi.intel.com (HELO smile) ([10.237.68.40]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2021 11:21:45 -0700 Received: from andy by smile with local (Exim 4.94.2) (envelope-from ) id 1m30Yl-00CK2l-Lr; Mon, 12 Jul 2021 21:21:39 +0300 From: Andy Shevchenko To: "Rafael J. Wysocki" , Andy Shevchenko , Daniel Scally , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, linux-media@vger.kernel.org, devel@acpica.org Cc: "Rafael J. Wysocki" , Len Brown , Ard Biesheuvel , Yong Zhi , Sakari Ailus , Bingbu Cao , Tianshu Qiu , Mauro Carvalho Chehab , Robert Moore , Erik Kaneda Subject: [PATCH v2 1/1] ACPI: utils: Fix reference counting in for_each_acpi_dev_match() Date: Mon, 12 Jul 2021 21:21:21 +0300 Message-Id: <20210712182121.2936794-1-andy.shevchenko@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently it's possible to iterate over the dangling pointer in case the device suddenly disappears. This may happen becase callers put it at the end of a loop. Instead, let's move that call inside acpi_dev_get_next_match_dev(). Fixes: 803abec64ef9 ("media: ipu3-cio2: Add cio2-bridge to ipu3-cio2 driver") Fixes: bf263f64e804 ("media: ACPI / bus: Add acpi_dev_get_next_match_dev() and helper macro") Fixes: edbd1bc4951e ("efi/dev-path-parser: Switch to use for_each_acpi_dev_match()") Signed-off-by: Andy Shevchenko --- v2: - rebased on top of v5.14-rc1 and hence added fix for EFI code - added kernel documentation update to point out that acpi_dev_get_next_match_dev() drops a reference on the given ACPI device (Rafael) drivers/acpi/utils.c | 7 +++---- drivers/firmware/efi/dev-path-parser.c | 1 - drivers/media/pci/intel/ipu3/cio2-bridge.c | 6 ++---- include/acpi/acpi_bus.h | 5 ----- 4 files changed, 5 insertions(+), 14 deletions(-) diff --git a/drivers/acpi/utils.c b/drivers/acpi/utils.c index e7ddd281afff..d5cedffeeff9 100644 --- a/drivers/acpi/utils.c +++ b/drivers/acpi/utils.c @@ -860,11 +860,9 @@ EXPORT_SYMBOL(acpi_dev_present); * Return the next match of ACPI device if another matching device was present * at the moment of invocation, or NULL otherwise. * - * FIXME: The function does not tolerate the sudden disappearance of @adev, e.g. - * in the case of a hotplug event. That said, the caller should ensure that - * this will never happen. - * * The caller is responsible for invoking acpi_dev_put() on the returned device. + * On the other hand the function invokes acpi_dev_put() on the given @adev + * assuming that its reference counter had been increased beforehand. * * See additional information in acpi_dev_present() as well. */ @@ -880,6 +878,7 @@ acpi_dev_get_next_match_dev(struct acpi_device *adev, const char *hid, const cha match.hrv = hrv; dev = bus_find_device(&acpi_bus_type, start, &match, acpi_dev_match_cb); + acpi_dev_put(adev); return dev ? to_acpi_device(dev) : NULL; } EXPORT_SYMBOL(acpi_dev_get_next_match_dev); diff --git a/drivers/firmware/efi/dev-path-parser.c b/drivers/firmware/efi/dev-path-parser.c index 10d4457417a4..eb9c65f97841 100644 --- a/drivers/firmware/efi/dev-path-parser.c +++ b/drivers/firmware/efi/dev-path-parser.c @@ -34,7 +34,6 @@ static long __init parse_acpi_path(const struct efi_dev_path *node, break; if (!adev->pnp.unique_id && node->acpi.uid == 0) break; - acpi_dev_put(adev); } if (!adev) return -ENODEV; diff --git a/drivers/media/pci/intel/ipu3/cio2-bridge.c b/drivers/media/pci/intel/ipu3/cio2-bridge.c index 4657e99df033..59a36f922675 100644 --- a/drivers/media/pci/intel/ipu3/cio2-bridge.c +++ b/drivers/media/pci/intel/ipu3/cio2-bridge.c @@ -173,10 +173,8 @@ static int cio2_bridge_connect_sensor(const struct cio2_sensor_config *cfg, int ret; for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) { - if (!adev->status.enabled) { - acpi_dev_put(adev); + if (!adev->status.enabled) continue; - } if (bridge->n_sensors >= CIO2_NUM_PORTS) { acpi_dev_put(adev); @@ -185,7 +183,6 @@ static int cio2_bridge_connect_sensor(const struct cio2_sensor_config *cfg, } sensor = &bridge->sensors[bridge->n_sensors]; - sensor->adev = adev; strscpy(sensor->name, cfg->hid, sizeof(sensor->name)); ret = cio2_bridge_read_acpi_buffer(adev, "SSDB", @@ -215,6 +212,7 @@ static int cio2_bridge_connect_sensor(const struct cio2_sensor_config *cfg, goto err_free_swnodes; } + sensor->adev = acpi_dev_get(adev); adev->fwnode.secondary = fwnode; dev_info(&cio2->dev, "Found supported sensor %s\n", diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 1ae993fee4a5..b9d434a93632 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -707,11 +707,6 @@ acpi_dev_get_first_match_dev(const char *hid, const char *uid, s64 hrv); * @hrv: Hardware Revision of the device, pass -1 to not check _HRV * * The caller is responsible for invoking acpi_dev_put() on the returned device. - * - * FIXME: Due to above requirement there is a window that may invalidate @adev - * and next iteration will use a dangling pointer, e.g. in the case of a - * hotplug event. That said, the caller should ensure that this will never - * happen. */ #define for_each_acpi_dev_match(adev, hid, uid, hrv) \ for (adev = acpi_dev_get_first_match_dev(hid, uid, hrv); \ -- 2.32.0