From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 040D8280035; Thu, 15 Jan 2026 16:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768496353; cv=none; b=FNWEtprSFWeypjoU4c2zQ3p6CUG3BCnJ1Sb2NyLw6QJ6K/f63csdKgzbNNPH3/ETn1FMxel7cajwb04hDoY2OonpR5jHJOY8aFoqRbxjqqGpns2sXwOmho18Gv4kKGKDhkjB3rdfn/ws9Vchi82RNjXrM/THsoppxFeecDLWp7o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768496353; c=relaxed/simple; bh=8ol9Y02Oj5/a/JR5UMSZKnSsddj3m7M6qTskGktUvpI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ekd8+FRX1euAt6d5qbEt7WqoDuXwbP28zYqZd0T82CzkgFDHOOW66vs/Aj78n5/4K2E5H8ZtJEwbxCct1iianvUNNPXjAUt6Llwcbp1OAQA8l5EKhJHlRvzHMDGKeLxD0UvhXA4JABxECa9DfCM+ykgVNdVXSKpix7P+3ej6NLc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=rOYLS2fA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="rOYLS2fA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83D2BC16AAE; Thu, 15 Jan 2026 16:59:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1768496352; bh=8ol9Y02Oj5/a/JR5UMSZKnSsddj3m7M6qTskGktUvpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rOYLS2fARxZRWhr0q6lE35A8UHc8MGVa2TkO9hWVX++4biM49eNgz0HSPejIvZ1LE Uh95doHxKDG09+HIZCM7uxb4tumQfhetQ5Nz7UxM/TkyG/fusv0fg2k8Y9WNsCEvED izBjBkNVSGsy3TWAZjtsOCyCHEW5LtVlqLcQDY7I= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Sumeet Pawnikar , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 6.18 166/181] powercap: fix race condition in register_control_type() Date: Thu, 15 Jan 2026 17:48:23 +0100 Message-ID: <20260115164208.307158619@linuxfoundation.org> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260115164202.305475649@linuxfoundation.org> References: <20260115164202.305475649@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sumeet Pawnikar [ Upstream commit 7bda1910c4bccd4b8d4726620bb3d6bbfb62286e ] The device becomes visible to userspace via device_register() even before it fully initialized by idr_init(). If userspace or another thread tries to register a zone immediately after device_register(), the control_type_valid() will fail because the control_type is not yet in the list. The IDR is not yet initialized, so this race condition causes zone registration failure. Move idr_init() and list addition before device_register() fix the race condition. Signed-off-by: Sumeet Pawnikar [ rjw: Subject adjustment, empty line added ] Link: https://patch.msgid.link/20251205190216.5032-1-sumeet4linux@gmail.com Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- drivers/powercap/powercap_sys.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/powercap/powercap_sys.c b/drivers/powercap/powercap_sys.c index 4112a00973382..d14b36b75189d 100644 --- a/drivers/powercap/powercap_sys.c +++ b/drivers/powercap/powercap_sys.c @@ -625,17 +625,23 @@ struct powercap_control_type *powercap_register_control_type( INIT_LIST_HEAD(&control_type->node); control_type->dev.class = &powercap_class; dev_set_name(&control_type->dev, "%s", name); - result = device_register(&control_type->dev); - if (result) { - put_device(&control_type->dev); - return ERR_PTR(result); - } idr_init(&control_type->idr); mutex_lock(&powercap_cntrl_list_lock); list_add_tail(&control_type->node, &powercap_cntrl_list); mutex_unlock(&powercap_cntrl_list_lock); + result = device_register(&control_type->dev); + if (result) { + mutex_lock(&powercap_cntrl_list_lock); + list_del(&control_type->node); + mutex_unlock(&powercap_cntrl_list_lock); + + idr_destroy(&control_type->idr); + put_device(&control_type->dev); + return ERR_PTR(result); + } + return control_type; } EXPORT_SYMBOL_GPL(powercap_register_control_type); -- 2.51.0