From 95622e49b215bcd3ff61f666db6d74eedd4f5b51 Mon Sep 17 00:00:00 2001 From: Vladislav Litvinov Date: Mon, 15 Apr 2024 15:16:33 +0200 Subject: [PATCH] drivers: regulator: fix reference count underflow Fixes regulator reference count underflow and adds error code for attempting to disable an already disabled regulator Signed-off-by: Vladislav Litvinov --- drivers/regulator/regulator_common.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/drivers/regulator/regulator_common.c b/drivers/regulator/regulator_common.c index 104c66e19cc..0190388a21e 100644 --- a/drivers/regulator/regulator_common.c +++ b/drivers/regulator/regulator_common.c @@ -183,12 +183,14 @@ int regulator_disable(const struct device *dev) (void)k_mutex_lock(&data->lock, K_FOREVER); #endif - data->refcnt--; + if (data->refcnt > 0) { + data->refcnt--; - if (data->refcnt == 0) { - ret = api->disable(dev); - if (ret < 0) { - data->refcnt++; + if (data->refcnt == 0) { + ret = api->disable(dev); + if (ret < 0) { + data->refcnt++; + } } }