From patchwork Sun Dec 8 14:59:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 848390 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 37CF21482E7; Sun, 8 Dec 2024 14:59:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733669977; cv=none; b=KlyACPmhmoE1vSaN5NjX1zyGHc3ovgjCJJKRU9V83Z1wUSLoFjp/kt7sB6f3R7MXmtf+3Gv4mxmxtrvEur4d56sKqqLsxUpnaYcts3rt+PKVpdvp9jE6sT7sKtXOAbSra7YzC1lzSc2d0gUQt9Pr7faBsgFc5HS+R3HifIrMI64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733669977; c=relaxed/simple; bh=3iA03vlkmk8eRVaaMnlEdEo46SKU6oHesbNcFaZ3nK8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fVYISuLQdF7d0w9ToaCTVNC4AL38j7vcpLq9ZrgJB+PmjOfpN02pRYNNXHrqaPDSUrTCJjW/4i4l7J9fPSFO5UHrQV6asUY92W/rS4Nq6SXk6GtD7VjwqJyYMf19YarhTUqk4o3BcT5PA5nqh8Kr1H4WlkuvNSmBR9aKdl6DctA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=oEZbeElD; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="oEZbeElD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1733669973; bh=3iA03vlkmk8eRVaaMnlEdEo46SKU6oHesbNcFaZ3nK8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oEZbeElDCGXMIki3voW+rTCAMijAk4Pg7tP/cJmy49BgBYNfmoSFwdb3T64WRliAM 9Pif9KkVO5W9Zwyq7Ji+00HKm9lpiRLOfL9NNzPj/IyYQvMTj5ByfvhLSWCRHEesw6 tDbuiRnPeFWe3qxBYQlHNus7TLoyz4MXXaREyI9c= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sun, 08 Dec 2024 15:59:26 +0100 Subject: [PATCH 1/3] power: supply: cros_charge-control: add mutex for driver data Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241208-cros_charge-control-v2-v1-1-8d168d0f08a3@weissschuh.net> References: <20241208-cros_charge-control-v2-v1-0-8d168d0f08a3@weissschuh.net> In-Reply-To: <20241208-cros_charge-control-v2-v1-0-8d168d0f08a3@weissschuh.net> To: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , Sebastian Reichel , Tzung-Bi Shih Cc: Thomas Koch , Sebastian Reichel , chrome-platform@lists.linux.dev, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , stable@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1733669972; l=4209; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=3iA03vlkmk8eRVaaMnlEdEo46SKU6oHesbNcFaZ3nK8=; b=k1t8pVBTjnSJ3XYByNz9U/4joMtP+VFz+aPp2WmWbAJ1oKmxP3gT2N221g5hbt0rmgNZGAnMl /Tcy19aikYaDFyR/m+BSHIZ/o217xybOlC7YZF5WkY1eHhGITTd96yr X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Concurrent accesses through sysfs may lead to inconsistent state in the priv data. Introduce a mutex to avoid this. Fixes: c6ed48ef5259 ("power: supply: add ChromeOS EC based charge control driver") Cc: stable@vger.kernel.org Signed-off-by: Thomas Weißschuh --- drivers/power/supply/cros_charge-control.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/drivers/power/supply/cros_charge-control.c b/drivers/power/supply/cros_charge-control.c index 17c53591ce197d08d97c94d3d4359a282026dd7d..58ca6d9ed6132af63a36ea4c5bf212acf066936c 100644 --- a/drivers/power/supply/cros_charge-control.c +++ b/drivers/power/supply/cros_charge-control.c @@ -7,8 +7,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -49,6 +51,7 @@ struct cros_chctl_priv { struct attribute *attributes[_CROS_CHCTL_ATTR_COUNT]; struct attribute_group group; + struct mutex lock; /* protects fields below and cros_ec */ enum power_supply_charge_behaviour current_behaviour; u8 current_start_threshold, current_end_threshold; }; @@ -85,6 +88,8 @@ static int cros_chctl_configure_ec(struct cros_chctl_priv *priv) { struct ec_params_charge_control req = {}; + lockdep_assert_held(&priv->lock); + req.cmd = EC_CHARGE_CONTROL_CMD_SET; switch (priv->current_behaviour) { @@ -159,6 +164,7 @@ static ssize_t charge_control_start_threshold_show(struct device *dev, struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr, CROS_CHCTL_ATTR_START_THRESHOLD); + guard(mutex)(&priv->lock); return sysfs_emit(buf, "%u\n", (unsigned int)priv->current_start_threshold); } @@ -169,6 +175,7 @@ static ssize_t charge_control_start_threshold_store(struct device *dev, struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr, CROS_CHCTL_ATTR_START_THRESHOLD); + guard(mutex)(&priv->lock); return cros_chctl_store_threshold(dev, priv, 0, buf, count); } @@ -178,6 +185,7 @@ static ssize_t charge_control_end_threshold_show(struct device *dev, struct devi struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr, CROS_CHCTL_ATTR_END_THRESHOLD); + guard(mutex)(&priv->lock); return sysfs_emit(buf, "%u\n", (unsigned int)priv->current_end_threshold); } @@ -187,6 +195,7 @@ static ssize_t charge_control_end_threshold_store(struct device *dev, struct dev struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr, CROS_CHCTL_ATTR_END_THRESHOLD); + guard(mutex)(&priv->lock); return cros_chctl_store_threshold(dev, priv, 1, buf, count); } @@ -195,6 +204,7 @@ static ssize_t charge_behaviour_show(struct device *dev, struct device_attribute struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr, CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR); + guard(mutex)(&priv->lock); return power_supply_charge_behaviour_show(dev, EC_CHARGE_CONTROL_BEHAVIOURS, priv->current_behaviour, buf); } @@ -210,6 +220,7 @@ static ssize_t charge_behaviour_store(struct device *dev, struct device_attribut if (ret < 0) return ret; + guard(mutex)(&priv->lock); priv->current_behaviour = ret; ret = cros_chctl_configure_ec(priv); @@ -290,6 +301,10 @@ static int cros_chctl_probe(struct platform_device *pdev) if (!priv) return -ENOMEM; + ret = devm_mutex_init(dev, &priv->lock); + if (ret) + return ret; + ret = cros_ec_get_cmd_versions(cros_ec, EC_CMD_CHARGE_CONTROL); if (ret < 0) return ret; @@ -327,7 +342,8 @@ static int cros_chctl_probe(struct platform_device *pdev) priv->current_end_threshold = 100; /* Bring EC into well-known state */ - ret = cros_chctl_configure_ec(priv); + scoped_guard(mutex, &priv->lock) + ret = cros_chctl_configure_ec(priv); if (ret < 0) return ret; From patchwork Sun Dec 8 14:59:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 848629 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 C8D5814B077; Sun, 8 Dec 2024 14:59:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733669977; cv=none; b=IKpEOQyv+B9d/ym/UZ1jqsugxbQL8n008jOSk8MVKUn3uFpSsqFWsOfq51EBP8pURCyJPv10qYIq6WQDn75EQl51MtZ45WjTTSd5FlbVxMWZxUeNSuVO/8781gR7OUwZAa/GejjpUU2K0fkpFj2QgMuRxXUsduRTbKNSa63mvqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733669977; c=relaxed/simple; bh=xAKdt+XUPvyRkhE19c1Wd0o1p1dyMhTM02DujFoE+fc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kpqlmjTLzb4lzEhRs9yZBvO2lS3DdEfHsZE8Nanpz7FvGtSqxTcWvMRj5191v0Ppy/bzQvolaJ1f93WbpqtHF/p3hTQmQmxuPEEJWwrXcLTuCCpvuvYeMEeqatiRoAo6T+jXZibLIz+TkPgq7om8RBWUc+vR4gRIP12pCuibV0M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=DDSCvzVE; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="DDSCvzVE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1733669973; bh=xAKdt+XUPvyRkhE19c1Wd0o1p1dyMhTM02DujFoE+fc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DDSCvzVE9bT2Ixa+Xai9eHEsZ3Xu8HQ4iWxTGQnDLPfK66bSeLdV3ueHoExfhUuUp Fy3ZREPOmIVDtK0nKC3lBXoKjV8PZR8luFWoby91rL4WtfvwTFWiXBJSwvE1gXBQ5f FoTppeyT740usY9ZaKyTBO3N4pdbFn0sElEWN8C4= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sun, 08 Dec 2024 15:59:27 +0100 Subject: [PATCH 2/3] power: supply: cros_charge-control: allow start_threshold == end_threshold Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241208-cros_charge-control-v2-v1-2-8d168d0f08a3@weissschuh.net> References: <20241208-cros_charge-control-v2-v1-0-8d168d0f08a3@weissschuh.net> In-Reply-To: <20241208-cros_charge-control-v2-v1-0-8d168d0f08a3@weissschuh.net> To: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , Sebastian Reichel , Tzung-Bi Shih Cc: Thomas Koch , Sebastian Reichel , chrome-platform@lists.linux.dev, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , stable@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1733669972; l=1214; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=xAKdt+XUPvyRkhE19c1Wd0o1p1dyMhTM02DujFoE+fc=; b=/m2irmWjCvb+/32Yh1ZuYvQFdD+iiny3bpW4IprRJA6kEJYthe1thZIcXseRJ3cNMvvSV97H4 drdxZrE/9IuDCOyJEmqUb4vMq+z0VCW3iYPlX1nB8FnhYU8k/kUl3w4 X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Allow setting the start and stop thresholds to the same value. There is no reason to disallow it. Suggested-by: Thomas Koch Fixes: c6ed48ef5259 ("power: supply: add ChromeOS EC based charge control driver") Cc: stable@vger.kernel.org Signed-off-by: Thomas Weißschuh --- drivers/power/supply/cros_charge-control.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/power/supply/cros_charge-control.c b/drivers/power/supply/cros_charge-control.c index 58ca6d9ed6132af63a36ea4c5bf212acf066936c..108b121db4423187fb65548396fb9195b8801006 100644 --- a/drivers/power/supply/cros_charge-control.c +++ b/drivers/power/supply/cros_charge-control.c @@ -139,11 +139,11 @@ static ssize_t cros_chctl_store_threshold(struct device *dev, struct cros_chctl_ return -EINVAL; if (is_end_threshold) { - if (val <= priv->current_start_threshold) + if (val < priv->current_start_threshold) return -EINVAL; priv->current_end_threshold = val; } else { - if (val >= priv->current_end_threshold) + if (val > priv->current_end_threshold) return -EINVAL; priv->current_start_threshold = val; } From patchwork Sun Dec 8 14:59:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 848630 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 8137D149C4D; Sun, 8 Dec 2024 14:59:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733669977; cv=none; b=bdLxR/g6kTYicMMt1bmEnMGiPZRz+8kocisawghMNfWO8sK0aCVz5TUdaJH3npVRgEP2FvUEWWcS90KvuWtcydk2OWM4GqH6lHVwmB6YFo9wWP4VmRtIkfehx1+2mEIKMj9+F2ATXPqb/q71uQJmro+GGjqP5snvtfWc+ctSc8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733669977; c=relaxed/simple; bh=za3Dkm3Zck5C4BmBz90pqL2Z3JihLGLCqJq5inSro9w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jypyMz/tVwNIeKJFRItvw2z0328Jfg0BVawo68AnPAjcRg2mkV6goR4FuVslMuhysA+wyvFm2u+1BahiDGV+Cb90lx+jPlQ+4FZ08MYGwQHUNANbE/gnranweRO7i9npARMs03cl+gwSziQF19mu/CwHnNrYrvLZVmdyyGV7wn4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=kGfTZjG/; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="kGfTZjG/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1733669973; bh=za3Dkm3Zck5C4BmBz90pqL2Z3JihLGLCqJq5inSro9w=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=kGfTZjG/1FQM6BZi5NANBbvq7rCk+3XIE5MSu/bcvg804QjUlzq2m9T5Xxf11Aqvi Lp+lt3rlldgY4dkZ5sca5sxGGQPTnZLsQZRc/xYgQaqjl6bnEPkdWLLmc1POO0HyED g/HoaQn+uqjOJJJ3gHmwAejcXd33wT0f9xxW+MAY= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Sun, 08 Dec 2024 15:59:28 +0100 Subject: [PATCH 3/3] power: supply: cros_charge-control: hide start threshold on v2 cmd Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241208-cros_charge-control-v2-v1-3-8d168d0f08a3@weissschuh.net> References: <20241208-cros_charge-control-v2-v1-0-8d168d0f08a3@weissschuh.net> In-Reply-To: <20241208-cros_charge-control-v2-v1-0-8d168d0f08a3@weissschuh.net> To: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Benson Leung , Guenter Roeck , Sebastian Reichel , Tzung-Bi Shih Cc: Thomas Koch , Sebastian Reichel , chrome-platform@lists.linux.dev, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , stable@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1733669972; l=2140; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=za3Dkm3Zck5C4BmBz90pqL2Z3JihLGLCqJq5inSro9w=; b=+0EygRWKP9SttMXFlFgC9ncZpGD99nkUvVZGjw06/U4VxGBhCFJqTpVtZNopI4yKgl9mBS5kQ d1KlWsmvkthCrk7KUbcH6JbEOsAxCH1nrhdy6vVZH1XFtzNT8SyFHB8 X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= ECs implementing the v2 command will not stop charging when the end threshold is reached. Instead they will begin discharging until the start threshold is reached, leading to permanent charge and discharge cycles. This defeats the point of the charge control mechanism. Avoid the issue by hiding the start threshold on v2 systems. Instead on those systems program the EC with start == end which forces the EC to reach and stay at that level. v1 does not support thresholds and v3 works correctly, at least judging from the code. Reported-by: Thomas Koch Fixes: c6ed48ef5259 ("power: supply: add ChromeOS EC based charge control driver") Cc: stable@vger.kernel.org Signed-off-by: Thomas Weißschuh --- drivers/power/supply/cros_charge-control.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/drivers/power/supply/cros_charge-control.c b/drivers/power/supply/cros_charge-control.c index 108b121db4423187fb65548396fb9195b8801006..9b0a7500296b4d7eb8cd53153e148926bb98aec1 100644 --- a/drivers/power/supply/cros_charge-control.c +++ b/drivers/power/supply/cros_charge-control.c @@ -139,6 +139,10 @@ static ssize_t cros_chctl_store_threshold(struct device *dev, struct cros_chctl_ return -EINVAL; if (is_end_threshold) { + /* Start threshold is not exposed, use fixed value */ + if (priv->cmd_version == 2) + priv->current_start_threshold = val == 100 ? 0 : val; + if (val < priv->current_start_threshold) return -EINVAL; priv->current_end_threshold = val; @@ -234,12 +238,10 @@ static umode_t cros_chtl_attr_is_visible(struct kobject *kobj, struct attribute { struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(attr, n); - if (priv->cmd_version < 2) { - if (n == CROS_CHCTL_ATTR_START_THRESHOLD) - return 0; - if (n == CROS_CHCTL_ATTR_END_THRESHOLD) - return 0; - } + if (n == CROS_CHCTL_ATTR_START_THRESHOLD && priv->cmd_version < 3) + return 0; + else if (n == CROS_CHCTL_ATTR_END_THRESHOLD && priv->cmd_version < 2) + return 0; return attr->mode; }