Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp728274pxj; Wed, 16 Jun 2021 12:09:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxiI/XRy/mfLzCRD6+SrwgJvnQNVywbZL3be/4uwaouXA3YGaJv9h5fqZ8mh4RGB0ELSfKz X-Received: by 2002:a50:fb89:: with SMTP id e9mr1472666edq.131.1623870577467; Wed, 16 Jun 2021 12:09:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623870577; cv=none; d=google.com; s=arc-20160816; b=Na+/Pgk1BBoaOuw9MzzKT0Qung4Yfgzggzaxm8PtEXRWto3zxri1j0AxnfQEh4+o41 9X3EG3DoMUMnr9hrfsbDyC2shQW6SYSWGgDcdbHtZtjNgj2SvzPKBxdKTj+v7QfzfM3c q78fsj6rEcBvkoRfBOpr6XripavHdnnuDFsGy2Xy2fanJCizGKyfLtvGrUc1TIrpg+P9 D6ao19wx4GJiHqXeCwMr5GB0aLea3+J7robAy9AnmjspPgHJRp/+6jUv71id/4+wrlpv fgsEQO9uwebLkaGpDIjtA3SccBi/SL8sUtfnezuZcylK+YdE972t5TxtllBuGmP79wBC D0Gw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Z8ipJtX4GdWcPk1g+UfEhvy3GHP71mo8jemZPJZKpqY=; b=ohyXZgtoASm9uJP80C0OkNXyMSNKqxV2RY4q/V6bdaFpX4LtnOJU9/aOIEnrAcGsoZ M5xRio90qW26tb3tzVVXAqhLGvCjVpDc/LkDBlsNsSSWr3ADPao1FkMRirZgF61Qa0ch 6Alcul+oPgHXoSp/4aU/aRehVowe4rYPqIBNz6bqf7N1r+K9imkITYRnewesYc9e/+Vq PBpNEWsJPnlMzxXo+HLCOriKnKWGhGwJJgHzoqGy5R4zGBJ6mStcQCbjVtkye91M/fnn s6rzd8M+33xu0F0ZhcrhiPs75nboSlrEms6qpkPKfLJlJbkj6kXP4XosWJ2sU2kZIJo8 5tRw== 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 d25si3078695ejo.582.2021.06.16.12.09.14; Wed, 16 Jun 2021 12:09:37 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234007AbhFPO2c (ORCPT + 99 others); Wed, 16 Jun 2021 10:28:32 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:60678 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233997AbhFPO21 (ORCPT ); Wed, 16 Jun 2021 10:28:27 -0400 Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_relay_lmtp) via UNIX with SMTP (IdeaSmtpServer 2.1.0) id 6567bd7b4d9b253f; Wed, 16 Jun 2021 16:26:19 +0200 Received: from kreacher.localnet (89-64-81-4.dynamic.chello.pl [89.64.81.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by v370.home.net.pl (Postfix) with ESMTPSA id 3FEB9669926; Wed, 16 Jun 2021 16:26:19 +0200 (CEST) From: "Rafael J. Wysocki" To: Linux ACPI Cc: LKML , Hans de Goede Subject: [PATCH 3/5] ACPI: scan: Fix device object rescan in acpi_scan_clear_dep() Date: Wed, 16 Jun 2021 16:23:44 +0200 Message-ID: <7272740.EvYhyI6sBW@kreacher> In-Reply-To: <3140195.44csPzL39Z@kreacher> References: <3140195.44csPzL39Z@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="UTF-8" X-CLIENT-IP: 89.64.81.4 X-CLIENT-HOSTNAME: 89-64-81-4.dynamic.chello.pl X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduledrfedvledgjeejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecujffqoffgrffnpdggtffipffknecuuegrihhlohhuthemucduhedtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvffufffkjghfggfgtgesthfuredttddtjeenucfhrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqeenucggtffrrghtthgvrhhnpedvjeelgffhiedukedtleekkedvudfggefhgfegjefgueekjeelvefggfdvledutdenucfkphepkeelrdeigedrkedurdegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepkeelrdeigedrkedurdegpdhhvghlohepkhhrvggrtghhvghrrdhlohgtrghlnhgvthdpmhgrihhlfhhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqpdhrtghpthhtoheplhhinhhugidqrggtphhisehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohephhguvghgohgvuggvsehrvgguhhgrthdrtghomh X-DCC--Metrics: v370.home.net.pl 1024; Body=3 Fuz1=3 Fuz2=3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki In general, acpi_bus_attach() can only be run safely under acpi_scan_lock, but that lock cannot be acquired under acpi_dep_list_lock, so make acpi_scan_clear_dep() schedule deferred execution of acpi_bus_attach() under acpi_scan_lock instead of calling it directly. This also fixes a possible race between acpi_scan_clear_dep() and device removal that might cause a device object that went away to be accessed, because acpi_scan_clear_dep() is changed to acquire a reference on the consumer device object. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/scan.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) Index: linux-pm/drivers/acpi/scan.c =================================================================== --- linux-pm.orig/drivers/acpi/scan.c +++ linux-pm/drivers/acpi/scan.c @@ -2115,16 +2115,56 @@ static int acpi_dev_get_first_consumer_d return 0; } -static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data) -{ +struct acpi_scan_clear_dep_work { + struct work_struct work; struct acpi_device *adev; +}; + +static void acpi_scan_clear_dep_fn(struct work_struct *work) +{ + struct acpi_scan_clear_dep_work *cdw; + + cdw = container_of(work, struct acpi_scan_clear_dep_work, work); - acpi_bus_get_device(dep->consumer, &adev); + acpi_scan_lock_acquire(); + acpi_bus_attach(cdw->adev, true); + acpi_scan_lock_release(); + + acpi_dev_put(cdw->adev); + kfree(cdw); +} + +static bool acpi_scan_clear_dep_queue(struct acpi_device *adev) +{ + struct acpi_scan_clear_dep_work *cdw; + + if (adev->dep_unmet) + return false; + + cdw = kmalloc(sizeof(*cdw), GFP_KERNEL); + if (!cdw) + return false; + + cdw->adev = adev; + INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn); + /* + * Since the work function may block on the lock until the entire + * initial enumeration of devices is complete, put it into the unbound + * workqueue. + */ + queue_work(system_unbound_wq, &cdw->work); + + return true; +} + +static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data) +{ + struct acpi_device *adev = acpi_bus_get_acpi_device(dep->consumer); if (adev) { adev->dep_unmet--; - if (!adev->dep_unmet) - acpi_bus_attach(adev, true); + if (!acpi_scan_clear_dep_queue(adev)) + acpi_dev_put(adev); } list_del(&dep->node);