From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796130223; b=Rtg0jbUpnU22rHZxSUXqwxljhNx3AsC51R2ea7StKiX+BUIAXlSxFyi/cYNien/c4O6P u+uSI4pTjIO5xGlE3FajRojSLLpaHNbHZKfvs+C+Po0q5+0W3yuEQaUxc1f9TEgfgfWL+ 6AEk1U+CRy11BRxMyCBLOaZJxY8b59kmZfhxD0ybaMUzBtH+74tv5moN7bJccGOzH//O9 SM5wDmuSNGh7Boqm78psr2aJPygNf73j1V3mgeES6yMHyc4yXFORRgKOPIA+50x00PIpU COslGZyJaepvGLYzZ/V5p1xAeMaTA8PzrEclvgjU3lQ7atwpv9pQI3oXmNTApnetNEA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796130223; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=cfNnLlo805At/BPzhbSKGZRNBQwo2rglZlbzhNEJ1QQ=; b=nIZID+J/+Y3HToy1kziV0faJeW3PfwpvEg1Zjp/41O1CfabWxZ0nvxtHZ3pbU02JVa9g Ctw6RH6MoVVE+CTsjR27m5rSm9qLH5nDC0aSs7a95QB97N+1a+SiqDcnSpoF1TuOt9/K1 ncv7Afde+g9KaPLVH1QF9E/2uY71zNsvxp1/uPYXf5lz5G2SCNS9PFlSom+X6ak7e0tIx t/ripr1zdkStyrTOAYfh4pE4x41TRxgeE+7Q8Iog3D4AmFR+RTc7Vp8mQj08OztoQu3h+ ZRrZl6AL6yrp/oKaux5VSeuOBTnp2nZJ/TWchcTJ+/A9PkrdQsI3vrBIwWemyVN8vhA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bdOUrGmY; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 532B2606A329; Tue, 1 Dec 2026 13:03:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bdOUrGmY Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37C151F0089A; Tue, 1 Dec 2026 13:03:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796130222; bh=cfNnLlo805At/BPzhbSKGZRNBQwo2rglZlbzhNEJ1QQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bdOUrGmYknjn5KIICt2Gn2XwqdL+SSjG/X5JLSpW4vRt0xVbj3Z8vxabkvP+Lvu05 TbjdfqF0X/D7slSm8eDNLNjNyiYtclTw9EC03EIdhrwrewdx83lOezt895ZtubXbnG aGYY/NlTwpzpJdKIYf3ZFMMZ4XFRtywgvR2vNVTLbhDbu6EMbCBCNHK8MtSdYZFnBW RE3o4UYZeR6Elc7StMtltXnRuBFRjkcPxkX77AMTY0qCjjRH5768EEay1KCr8XmYa3 u5Bs21+86L9RVaZHUfSh57NBTtGt7rnn4flu4n4FGm2Q40vu5b8OV5uJsTsYo222J0 2lD7e+nCMP1xw== From: Tzung-Bi Shih To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Johan Hovold Cc: Jonathan Corbet , Shuah Khan , Randy Dunlap , Bartosz Golaszewski , Linus Walleij , Philipp Zabel , driver-core@lists.linux.dev, linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, tzungbi@kernel.org Subject: [PATCH v16 4/4] reset: Leverage revocable for accessing struct reset_controller_dev Date: Tue, 1 Dec 2026 21:03:03 +0800 Message-ID: <20261201130303.14530-5-tzungbi@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201130303.14530-1-tzungbi@kernel.org> References: <20261201130303.14530-1-tzungbi@kernel.org> X-Mailing-List: driver-core@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Commit 78ebbff6d1a0 ("reset: handle removing supplier before consumers") protected the `rcdev` pointer in `struct reset_control` with SRCU so that the reset core can handle suppliers unbinding while consumers still hold reset control handles. Because `struct reset_control` handles can outlive `rcdev`, each of the N handles had to embed its own `srcu_struct` and call synchronize_srcu() on release, and reset_controller_unregister() had to call synchronize_srcu() N times in a loop. Leverage dynamic revocable to allocate a single reference-counted `struct revocable` for `rcdev` shared across all N `struct reset_control` handles (1-to-N). This reduces N `srcu_struct` instances per controller to 1, drops synchronize_srcu() from reset_control_put_internal(), and replaces the N synchronize_srcu() calls on unregistration with a single revocable_revoke(). Signed-off-by: Tzung-Bi Shih --- Test: $ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_RESET_CONTROLLER=y \ --kconfig_add CONFIG_RESET_KUNIT_TEST=y \ reset_test $ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_RESET_CONTROLLER=y \ --kconfig_add CONFIG_RESET_KUNIT_TEST=y \ --kconfig_add CONFIG_PROVE_LOCKING=y \ --kconfig_add CONFIG_DEBUG_KERNEL=y \ --kconfig_add CONFIG_KASAN=y \ --arch=x86_64 \ --raw_output=all \ reset_test --- v16: - Rebase onto next-20261130. - Fix conflicts mainly with bd357ffa1145 ("reset: core: Serialize concurrent deassert/reset of shared resets"). - Drop __reset_control_free() as it was a 1-line function. - Reorder in reset_controller_unregister(). - Move rstc to the local list (with kref_get()) before revocable_revoke(). - reset_controller_remove() can only be called once (either in reset_controller_unregister() or reset_control_put_internal()). - Drop the list_empty() guard in reset_controller_remove(). v15: https://lore.kernel.org/all/20261102161710.24076-5-tzungbi@kernel.org - Rename: - "rev" -> "revocable" in struct reset_controller_dev. - "rev" -> "rcdev_revocable" in struct reset_control. v14: https://lore.kernel.org/all/20261011121246.162759-6-tzungbi@kernel.org - New to the series. --- drivers/reset/core.c | 119 ++++++++++--------------------- include/linux/reset-controller.h | 3 + 2 files changed, 39 insertions(+), 83 deletions(-) diff --git a/drivers/reset/core.c b/drivers/reset/core.c index 0bff49d9b558..afc17a3c698a 100644 --- a/drivers/reset/core.c +++ b/drivers/reset/core.c @@ -24,8 +24,8 @@ #include #include #include +#include #include -#include static DEFINE_MUTEX(reset_list_mutex); static LIST_HEAD(reset_controller_list); @@ -37,9 +37,7 @@ static DEFINE_IDA(reset_gpio_ida); /** * struct reset_control - a reset control - * @rcdev: a pointer to the reset controller device - * this reset control belongs to - * @srcu: protects the rcdev pointer from removal during consumer access + * @rcdev_revocable: revocable handle for accessing the reset controller device * @list: list entry for the rcdev's reset controller list * @id: ID of the reset controller in the reset * controller device @@ -53,11 +51,10 @@ static DEFINE_IDA(reset_gpio_ida); * will be either 0 or 1. * @shared_lock: serializes the shared refcount update and the hardware op for * controllers whose ops may sleep (rcdev->can_sleep); taken only - * inside the SRCU read-side critical section + * with revocable access */ struct reset_control { - struct reset_controller_dev __rcu *rcdev; - struct srcu_struct srcu; + struct revocable *rcdev_revocable; struct list_head list; unsigned int id; struct kref refcnt; @@ -144,6 +141,10 @@ int reset_controller_register(struct reset_controller_dev *rcdev) return -EINVAL; } + rcdev->revocable = revocable_alloc(rcdev); + if (!rcdev->revocable) + return -ENOMEM; + if (rcdev->of_xlate) rcdev->fwnode_reset_n_cells = rcdev->of_reset_n_cells; @@ -164,7 +165,6 @@ int reset_controller_register(struct reset_controller_dev *rcdev) EXPORT_SYMBOL_GPL(reset_controller_register); static void __reset_control_release(struct kref *kref); -static void __reset_control_free(struct reset_control *rstc); /* * Removes rstc from its list and drops the references on rcdev's owner module @@ -197,27 +197,29 @@ void reset_controller_unregister(struct reset_controller_dev *rcdev) * Numb but don't free the remaining reset control handles that are * still held by consumers. * - * Hold a temporary reference on rstc before clearing rstc->rcdev: - * once rstc->rcdev is NULL, a concurrent reset_control_put_internal() - * will not acquire rcdev->lock and could otherwise free rstc. + * Hold a temporary reference on rstc before revocable_revoke(): + * once revocable_revoke() clears the resource pointer, a concurrent + * reset_control_put_internal() will not acquire rcdev->lock and + * could otherwise free rstc. */ list_for_each_entry_safe(rstc, pos, &rcdev->reset_control_head, list) { kref_get(&rstc->refcnt); - rcu_assign_pointer(rstc->rcdev, NULL); list_move(&rstc->list, &reset_control_head); } } /* - * Run synchronize_srcu() outside rcdev->lock to avoid a circular + * Run revocable_revoke() outside rcdev->lock to avoid a circular * locking dependency with paths that acquire rcdev->lock inside - * the rstc->srcu read-side critical section. + * the revocable access critical section. */ + revocable_revoke(rcdev->revocable); + revocable_put(rcdev->revocable); + list_for_each_entry_safe(rstc, pos, &reset_control_head, list) { - synchronize_srcu(&rstc->srcu); reset_controller_remove(rcdev, rstc); if (kref_put(&rstc->refcnt, __reset_control_release)) - __reset_control_free(rstc); + kfree(rstc); } mutex_destroy(&rcdev->lock); @@ -438,11 +440,7 @@ int reset_control_reset(struct reset_control *rstc) if (reset_control_is_array(rstc)) return reset_control_array_reset(rstc_to_array(rstc)); - guard(srcu)(&rstc->srcu); - - rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu); - if (!rcdev) - return -ENODEV; + revocable_try_access_or_return(rstc->rcdev_revocable, rcdev); /* * For a shared reset only the first caller runs the hardware op while @@ -590,11 +588,7 @@ int reset_control_assert(struct reset_control *rstc) if (reset_control_is_array(rstc)) return reset_control_array_assert(rstc_to_array(rstc)); - guard(srcu)(&rstc->srcu); - - rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu); - if (!rcdev) - return -ENODEV; + revocable_try_access_or_return(rstc->rcdev_revocable, rcdev); /* * For a shared reset the last caller runs the hardware op while the @@ -695,11 +689,7 @@ int reset_control_deassert(struct reset_control *rstc) if (reset_control_is_array(rstc)) return reset_control_array_deassert(rstc_to_array(rstc)); - guard(srcu)(&rstc->srcu); - - rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu); - if (!rcdev) - return -ENODEV; + revocable_try_access_or_return(rstc->rcdev_revocable, rcdev); /* * For a shared reset only the first caller runs the hardware op while @@ -762,11 +752,7 @@ int reset_control_status(struct reset_control *rstc) if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc)) return -EINVAL; - guard(srcu)(&rstc->srcu); - - rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu); - if (!rcdev) - return -ENODEV; + revocable_try_access_or_return(rstc->rcdev_revocable, rcdev); if (rcdev->ops->status) return rcdev->ops->status(rcdev, rstc->id); @@ -812,11 +798,7 @@ int reset_control_acquire(struct reset_control *rstc) if (rstc->acquired) return 0; - guard(srcu)(&rstc->srcu); - - rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu); - if (!rcdev) - return -ENODEV; + revocable_try_access_or_return(rstc->rcdev_revocable, rcdev); scoped_guard(mutex, &rcdev->lock) { list_for_each_entry(rc, &rcdev->reset_control_head, list) { @@ -912,7 +894,6 @@ __reset_control_get_internal(struct reset_controller_dev *rcdev, bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED; bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED; struct reset_control *rstc; - int ret; lockdep_assert_held(&rcdev->lock); @@ -943,19 +924,13 @@ __reset_control_get_internal(struct reset_controller_dev *rcdev, if (!rstc) return ERR_PTR(-ENOMEM); - ret = init_srcu_struct(&rstc->srcu); - if (ret) { - kfree(rstc); - return ERR_PTR(ret); - } - if (!try_module_get(rcdev->owner)) { - cleanup_srcu_struct(&rstc->srcu); kfree(rstc); return ERR_PTR(-ENODEV); } - rcu_assign_pointer(rstc->rcdev, rcdev); + revocable_get(rcdev->revocable); + rstc->rcdev_revocable = rcdev->revocable; list_add(&rstc->list, &rcdev->reset_control_head); rstc->id = index; kref_init(&rstc->refcnt); @@ -967,34 +942,12 @@ __reset_control_get_internal(struct reset_controller_dev *rcdev, return rstc; } -/* - * Detaches rstc from rcdev (if still attached) when refcnt reaches 0. - * If rstc is still attached, the caller must hold rcdev->lock. - */ static void __reset_control_release(struct kref *kref) { struct reset_control *rstc = container_of(kref, struct reset_control, refcnt); - struct reset_controller_dev *rcdev; - - rcdev = rcu_replace_pointer(rstc->rcdev, NULL, true); - if (rcdev) { - lockdep_assert_held(&rcdev->lock); - reset_controller_remove(rcdev, rstc); - } - mutex_destroy(&rstc->shared_lock); -} - -/* - * Must be called outside the rstc->srcu read-side critical section after - * kref_put(..., __reset_control_release) returns true. - */ -static void __reset_control_free(struct reset_control *rstc) -{ - synchronize_srcu(&rstc->srcu); - cleanup_srcu_struct(&rstc->srcu); - kfree(rstc); + revocable_put(rstc->rcdev_revocable); } static void reset_control_put_internal(struct reset_control *rstc) @@ -1005,19 +958,19 @@ static void reset_control_put_internal(struct reset_control *rstc) if (IS_ERR_OR_NULL(rstc)) return; - scoped_guard(srcu, &rstc->srcu) { - rcdev = srcu_dereference(rstc->rcdev, &rstc->srcu); - if (rcdev) { - guard(mutex)(&rcdev->lock); - ret = kref_put(&rstc->refcnt, __reset_control_release); - } else { - /* Already detached from rcdev. */ - ret = kref_put(&rstc->refcnt, __reset_control_release); - } + revocable_try_access_with(rstc->rcdev_revocable, rcdev); + if (rcdev) { + guard(mutex)(&rcdev->lock); + ret = kref_put(&rstc->refcnt, __reset_control_release); + if (ret) + reset_controller_remove(rcdev, rstc); + } else { + /* Already detached from rcdev. */ + ret = kref_put(&rstc->refcnt, __reset_control_release); } if (ret) - __reset_control_free(rstc); + kfree(rstc); } static void reset_gpio_aux_device_release(struct device *dev) diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h index 587fb57c2c52..4fe6c8a49e05 100644 --- a/include/linux/reset-controller.h +++ b/include/linux/reset-controller.h @@ -28,6 +28,7 @@ struct reset_control_ops { struct module; struct device_node; struct of_phandle_args; +struct revocable; /** * struct reset_controller_dev - reset controller entity that might @@ -51,6 +52,7 @@ struct of_phandle_args; * serialize the shared reset, assert and deassert paths under a * mutex * @lock: protects the reset control list from concurrent access + * @revocable: revocable provider handle for itself */ struct reset_controller_dev { const struct reset_control_ops *ops; @@ -69,6 +71,7 @@ struct reset_controller_dev { unsigned int nr_resets; bool can_sleep; struct mutex lock; + struct revocable *revocable; }; #if IS_ENABLED(CONFIG_RESET_CONTROLLER) -- 2.53.0