Skip to content

Commit 44a0acb

Browse files
Bartosz GolaszewskipH5
authored andcommitted
reset: protect struct reset_controller_dev with its own mutex
Currently we use a single, global mutex - misleadingly names reset_list_mutex - to protect the global list of reset devices, per-controller list of reset control handles and also internal fields of struct reset_control. Locking can be made a lot more fine-grained if we use a separate mutex for serializing operations on the list AND accessing the reset controller device. Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com> Reviewed-by: Philipp Zabel <p.zabel@pengutronix.de> Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
1 parent 78ebbff commit 44a0acb

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

drivers/reset/core.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ int reset_controller_register(struct reset_controller_dev *rcdev)
131131
}
132132

133133
INIT_LIST_HEAD(&rcdev->reset_control_head);
134+
mutex_init(&rcdev->lock);
134135

135136
guard(mutex)(&reset_list_mutex);
136137

@@ -143,6 +144,8 @@ EXPORT_SYMBOL_GPL(reset_controller_register);
143144
static void reset_controller_remove(struct reset_controller_dev *rcdev,
144145
struct reset_control *rstc)
145146
{
147+
lockdep_assert_held(&rcdev->lock);
148+
146149
list_del(&rstc->list);
147150
module_put(rcdev->owner);
148151
put_device(rcdev->dev);
@@ -156,19 +159,22 @@ void reset_controller_unregister(struct reset_controller_dev *rcdev)
156159
{
157160
struct reset_control *rstc, *pos;
158161

159-
guard(mutex)(&reset_list_mutex);
160-
161-
list_del(&rcdev->list);
162+
scoped_guard(mutex, &reset_list_mutex)
163+
list_del(&rcdev->list);
162164

163-
/*
164-
* Numb but don't free the remaining reset control handles that are
165-
* still held by consumers.
166-
*/
167-
list_for_each_entry_safe(rstc, pos, &rcdev->reset_control_head, list) {
168-
rcu_assign_pointer(rstc->rcdev, NULL);
169-
synchronize_srcu(&rstc->srcu);
170-
reset_controller_remove(rcdev, rstc);
165+
scoped_guard(mutex, &rcdev->lock) {
166+
/*
167+
* Numb but don't free the remaining reset control handles that are
168+
* still held by consumers.
169+
*/
170+
list_for_each_entry_safe(rstc, pos, &rcdev->reset_control_head, list) {
171+
rcu_assign_pointer(rstc->rcdev, NULL);
172+
synchronize_srcu(&rstc->srcu);
173+
reset_controller_remove(rcdev, rstc);
174+
}
171175
}
176+
177+
mutex_destroy(&rcdev->lock);
172178
}
173179
EXPORT_SYMBOL_GPL(reset_controller_unregister);
174180

@@ -712,10 +718,12 @@ int reset_control_acquire(struct reset_control *rstc)
712718
if (!rcdev)
713719
return -ENODEV;
714720

715-
list_for_each_entry(rc, &rcdev->reset_control_head, list) {
716-
if (rstc != rc && rstc->id == rc->id) {
717-
if (rc->acquired)
718-
return -EBUSY;
721+
scoped_guard(mutex, &rcdev->lock) {
722+
list_for_each_entry(rc, &rcdev->reset_control_head, list) {
723+
if (rstc != rc && rstc->id == rc->id) {
724+
if (rc->acquired)
725+
return -EBUSY;
726+
}
719727
}
720728
}
721729

@@ -806,7 +814,7 @@ __reset_control_get_internal(struct reset_controller_dev *rcdev,
806814
struct reset_control *rstc;
807815
int ret;
808816

809-
lockdep_assert_held(&reset_list_mutex);
817+
lockdep_assert_held(&rcdev->lock);
810818

811819
/* Expect callers to filter out OPTIONAL and DEASSERTED bits */
812820
if (WARN_ON(flags & ~(RESET_CONTROL_FLAGS_BIT_SHARED |
@@ -868,8 +876,10 @@ static void __reset_control_release(struct kref *kref)
868876

869877
scoped_guard(srcu, &rstc->srcu) {
870878
rcdev = rcu_replace_pointer(rstc->rcdev, NULL, true);
871-
if (rcdev)
879+
if (rcdev) {
880+
guard(mutex)(&rcdev->lock);
872881
reset_controller_remove(rcdev, rstc);
882+
}
873883
}
874884

875885
synchronize_srcu(&rstc->srcu);

include/linux/reset-controller.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define _LINUX_RESET_CONTROLLER_H_
44

55
#include <linux/list.h>
6+
#include <linux/mutex.h>
67

78
struct reset_controller_dev;
89

@@ -40,6 +41,7 @@ struct of_phandle_args;
4041
* device tree to id as given to the reset control ops, defaults
4142
* to :c:func:`of_reset_simple_xlate`.
4243
* @nr_resets: number of reset controls in this reset controller device
44+
* @lock: protects the reset control list from concurrent access
4345
*/
4446
struct reset_controller_dev {
4547
const struct reset_control_ops *ops;
@@ -52,6 +54,7 @@ struct reset_controller_dev {
5254
int (*of_xlate)(struct reset_controller_dev *rcdev,
5355
const struct of_phandle_args *reset_spec);
5456
unsigned int nr_resets;
57+
struct mutex lock;
5558
};
5659

5760
#if IS_ENABLED(CONFIG_RESET_CONTROLLER)

0 commit comments

Comments
 (0)