Skip to content

Commit 3747339

Browse files
diandersbroonie
authored andcommitted
regulator: core: Make regulator_lock_two() logic easier to follow
The regulator_lock_two() function could be made clearer in the case of lock contention by having a local variable for each of the held and contended locks. Let's do that. At the same time, let's use the swap() function instead of open coding it. This change is expected to be a no-op and simply improves code clarity. Suggested-by: Stephen Boyd <swboyd@chromium.org> Link: https://lore.kernel.org/r/CAE-0n53Eb1BeDPmjBycXUaQAF4ppiAM6UDWje_jiB9GAmR8MMw@mail.gmail.com Signed-off-by: Douglas Anderson <dianders@chromium.org> Link: https://lore.kernel.org/r/20230413173359.1.I1ae92b25689bd6579952e6d458b79f5f8054a0c9@changeid Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 902f8c9 commit 3747339

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

drivers/regulator/core.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static void regulator_lock_two(struct regulator_dev *rdev1,
219219
struct regulator_dev *rdev2,
220220
struct ww_acquire_ctx *ww_ctx)
221221
{
222-
struct regulator_dev *tmp;
222+
struct regulator_dev *held, *contended;
223223
int ret;
224224

225225
ww_acquire_init(ww_ctx, &regulator_ww_class);
@@ -233,25 +233,18 @@ static void regulator_lock_two(struct regulator_dev *rdev1,
233233
goto exit;
234234
}
235235

236+
held = rdev1;
237+
contended = rdev2;
236238
while (true) {
237-
/*
238-
* Start of loop: rdev1 was locked and rdev2 was contended.
239-
* Need to unlock rdev1, slowly lock rdev2, then try rdev1
240-
* again.
241-
*/
242-
regulator_unlock(rdev1);
243-
244-
ww_mutex_lock_slow(&rdev2->mutex, ww_ctx);
245-
rdev2->ref_cnt++;
246-
rdev2->mutex_owner = current;
247-
ret = regulator_lock_nested(rdev1, ww_ctx);
248-
249-
if (ret == -EDEADLOCK) {
250-
/* More contention; swap which needs to be slow */
251-
tmp = rdev1;
252-
rdev1 = rdev2;
253-
rdev2 = tmp;
254-
} else {
239+
regulator_unlock(held);
240+
241+
ww_mutex_lock_slow(&contended->mutex, ww_ctx);
242+
contended->ref_cnt++;
243+
contended->mutex_owner = current;
244+
swap(held, contended);
245+
ret = regulator_lock_nested(contended, ww_ctx);
246+
247+
if (ret != -EDEADLOCK) {
255248
WARN_ON(ret);
256249
break;
257250
}

0 commit comments

Comments
 (0)