Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp455384lqt; Thu, 6 Jun 2024 08:23:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUW7eVGVgQzukc1ZXVy9g6b0hxkdNgR2cSUqku21VklQhdHLip3W22RC7J9Rz4i23rxuVME5pcL+1JnVVNGgnezkvBSa5rQ2kz219xRgw== X-Google-Smtp-Source: AGHT+IFwOo+CI3Zdn2cs+lyPZMx1wYEfA6wIMVwscD7qyPt3ExwxPNIaW3SM+DtDdsdDiCbLxqGG X-Received: by 2002:a17:902:e5c8:b0:1f6:7f20:d988 with SMTP id d9443c01a7336-1f6a5a6b41fmr63642565ad.42.1717687391613; Thu, 06 Jun 2024 08:23:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717687391; cv=pass; d=google.com; s=arc-20160816; b=CpyvVUuZcJW7Z6xFpvEuIei0zBozggfftsVlvjceg4YhXvTS/wmyq8G9iKAIJ3k22K 3pxVF7YI+VzZLZ0apjOOLiVXRBK182hejxaqMWQmh4tA4VAGeGTtA6NJSPCtrwSdCZd7 hyZy/x8zhfTp0FPPrfflC4kJhmMY4WYXgTrasZO9CUlsUFYxFfLVVklbIVTB3upBo8Ns PD4YrdI8fXHzJVuOt6sJyfgo3Y+CbW6bXMjAFEuKHnjJsFasDKPDq+rORDct/F84kmkG jncYf8UKLbdb7xSBr1tekd2gqygtR/NiLcu5WirdwzlQ5rhbZOcUJw245f7DbBIfu6Nh l3YA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=l8ahgH0mGUPAqk1OF3KBfx3ChmFc0HzDuRee+txiEw8=; fh=yA3vuFiZmJfoItPaS2BpWDAzGgfs+KcnNwQwMnTjy1k=; b=0YCxjSszWBgKL4OH0j318QR5xAvpBAqLTcMeFz9tglRdu8/E8aobEJj+Kb8uixp5YE YQbgFQOil0cD3cECuXHSGNCX2hZQ86+2XJLnb9vEJOK9zMcz636LMeURpWSu25xcwjMt hCwzBLSLaIdyS68f/L7Mw1S1t2BW0Wb/StNAQKVb4PpJrxhGP10ogLtcbDv+GMbcy8gu 29KLDz9ZaQAcAoM+NcxQUHQm6BwoEubnsEvveowJoqGzU3whKMpxVk1Nb2JMIfEDFJtv bTh75Y3ew9nEDTeIA9HxLigQ/vITGQQYvRaNw1r4KCj3Kv2/up2lU6f7jDHnSXleEYr/ zvjQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=eovFnlwp; arc=pass (i=1 spf=pass spfdomain=weissschuh.net dkim=pass dkdomain=weissschuh.net dmarc=pass fromdomain=weissschuh.net); spf=pass (google.com: domain of linux-kernel+bounces-204533-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204533-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=weissschuh.net Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de21972170si254a12.233.2024.06.06.08.23.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 08:23:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-204533-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=eovFnlwp; arc=pass (i=1 spf=pass spfdomain=weissschuh.net dkim=pass dkdomain=weissschuh.net dmarc=pass fromdomain=weissschuh.net); spf=pass (google.com: domain of linux-kernel+bounces-204533-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204533-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=weissschuh.net Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 34C7428CDEF for ; Thu, 6 Jun 2024 15:14:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AE7651A2FD8; Thu, 6 Jun 2024 14:51:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="eovFnlwp" 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 7BF9A1A2550; Thu, 6 Jun 2024 14:51:06 +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=1717685470; cv=none; b=I3GWlLNiOJjjsIgC4QGDZLsIgFTBHjUEP2KcNkuD5M/RvOeJtltZO7EZUd5/PhpCqcPHg6SeoMDiR2yHdeJ+D2+Ug945EaoHWFrgCOk849ucI4qxHXA48V3rXex6AdZYFfp1AHHQujeHuyTppX99T7wftvgHimgUPzbFV8+4gMA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717685470; c=relaxed/simple; bh=AWI2uk7badd/hUC4297caKc5NtjQlqqUKCxfXKFVIU8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uxRDglt9ebC5xkCAO7Ljj4If5ZCSgDRkJfo8ke25Nko6pmudTkN3/m1KhgmYHdm2ercu4xKon3O0qoJTlVtOTqQpBs8mPmxed4HhSsC5xpOwJTBvoqN/CX8yGCSq6YZwlssCmDaTZ6fM2+jlheJw2HKDO24wMQnDwaDGZ8beyqE= 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=eovFnlwp; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1717685463; bh=AWI2uk7badd/hUC4297caKc5NtjQlqqUKCxfXKFVIU8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=eovFnlwpvI6uwrpTB8a2qkDcxOr0KglMSmQoXNoMme3dNdamlwCsKIrGzU0EKA3Ms gPWuTWXJWXYATI/dSetER3HkxbANlCrbv5mWli2txy+6rMgco0mfBW79e683GLFZN+ HfUKiO53BmhR9D14vWihNXdUULLEq00AzXTX9PSw= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 06 Jun 2024 16:50:58 +0200 Subject: [PATCH RFC 4/6] power: supply: core: add locking around extension access Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20240606-power-supply-extensions-v1-4-b45669290bdc@weissschuh.net> References: <20240606-power-supply-extensions-v1-0-b45669290bdc@weissschuh.net> In-Reply-To: <20240606-power-supply-extensions-v1-0-b45669290bdc@weissschuh.net> To: Sebastian Reichel , Jeremy Soller , System76 Product Development , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717685463; l=8061; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=AWI2uk7badd/hUC4297caKc5NtjQlqqUKCxfXKFVIU8=; b=II7dmk6ur4eLxXY6hNllwIAjb77cbZIJkV0I0YZAC9hBQ9d+uPQcXMuNagnqJ+Ht8Y2aSN7JM KreRqpzlw1zDRK6npalNgY50gDr+4cBB0w2oar49k9cBfyqwx/rJcmW X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Signed-off-by: Thomas Weißschuh --- drivers/power/supply/power_supply.h | 6 ++- drivers/power/supply/power_supply_core.c | 65 +++++++++++++++++++++++++------ drivers/power/supply/power_supply_sysfs.c | 22 ++++++----- include/linux/power_supply.h | 2 + 4 files changed, 73 insertions(+), 22 deletions(-) diff --git a/drivers/power/supply/power_supply.h b/drivers/power/supply/power_supply.h index 7f9e139064cc..b469a9719045 100644 --- a/drivers/power/supply/power_supply.h +++ b/drivers/power/supply/power_supply.h @@ -13,10 +13,14 @@ struct device; struct device_type; struct power_supply; -bool power_supply_has_property(const struct power_supply *psy, +bool power_supply_has_property(struct power_supply *psy, enum power_supply_property psp); +bool power_supply_has_property_nolock(struct power_supply *psy, + enum power_supply_property psp); bool power_supply_ext_has_property(const struct power_supply_ext *psy_ext, enum power_supply_property psp); +int power_supply_property_is_writeable_nolock(struct power_supply *psy, + enum power_supply_property psp); #ifdef CONFIG_SYSFS diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index b32bbca9a848..ded9748be550 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -1218,14 +1218,26 @@ bool power_supply_ext_has_property(const struct power_supply_ext *psy_ext, return found; } -bool power_supply_has_property(const struct power_supply *psy, - enum power_supply_property psp) +bool power_supply_has_property_nolock(struct power_supply *psy, + enum power_supply_property psp) { + lockdep_assert_held(&psy->ext_lock); + if (power_supply_ext_has_property(psy->ext, psp)) return true; return psy_desc_has_property(psy->desc, psp); } +bool power_supply_has_property(struct power_supply *psy, + enum power_supply_property psp) +{ + scoped_guard(rwsem_read, &psy->ext_lock) { + if (power_supply_ext_has_property(psy->ext, psp)) + return true; + } + return psy_desc_has_property(psy->desc, psp); +} + int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) @@ -1236,9 +1248,12 @@ int power_supply_get_property(struct power_supply *psy, return -ENODEV; } - if (power_supply_ext_has_property(psy->ext, psp)) - return psy->ext->get_property(psy, psp, val); - else if (psy_desc_has_property(psy->desc, psp)) + scoped_guard(rwsem_read, &psy->ext_lock) { + if (power_supply_ext_has_property(psy->ext, psp)) + return psy->ext->get_property(psy, psp, val); + } + + if (psy_desc_has_property(psy->desc, psp)) return psy->desc->get_property(psy, psp, val); else if (power_supply_battery_info_has_prop(psy->battery_info, psp)) return power_supply_battery_info_get_prop(psy->battery_info, psp, val); @@ -1254,11 +1269,13 @@ int power_supply_set_property(struct power_supply *psy, if (atomic_read(&psy->use_cnt) <= 0) return -ENODEV; - if (power_supply_ext_has_property(psy->ext, psp)) { - if (psy->ext->set_property) - return psy->ext->set_property(psy, psp, val); - else - return -ENODEV; + scoped_guard(rwsem_read, &psy->ext_lock) { + if (power_supply_ext_has_property(psy->ext, psp)) { + if (psy->ext->set_property) + return psy->ext->set_property(psy, psp, val); + else + return -ENODEV; + } } if (!psy->desc->set_property) @@ -1274,6 +1291,28 @@ int power_supply_property_is_writeable(struct power_supply *psy, if (atomic_read(&psy->use_cnt) <= 0) return -ENODEV; + scoped_guard(rwsem_read, &psy->ext_lock) { + if (power_supply_ext_has_property(psy->ext, psp)) { + if (psy->ext->property_is_writeable) + return psy->ext->property_is_writeable(psy, psp); + else + return -ENODEV; + } + } + + if (!psy->desc->property_is_writeable) + return -ENODEV; + + return psy->desc->property_is_writeable(psy, psp); +} +EXPORT_SYMBOL_GPL(power_supply_property_is_writeable); + +int power_supply_property_is_writeable_nolock(struct power_supply *psy, + enum power_supply_property psp) +{ + if (atomic_read(&psy->use_cnt) <= 0) + return -ENODEV; + if (power_supply_ext_has_property(psy->ext, psp)) { if (psy->ext->property_is_writeable) return psy->ext->property_is_writeable(psy, psp); @@ -1286,7 +1325,6 @@ int power_supply_property_is_writeable(struct power_supply *psy, return psy->desc->property_is_writeable(psy, psp); } -EXPORT_SYMBOL_GPL(power_supply_property_is_writeable); void power_supply_external_power_changed(struct power_supply *psy) { @@ -1315,6 +1353,8 @@ static int power_supply_update_groups(struct power_supply *psy) int power_supply_register_extension(struct power_supply *psy, const struct power_supply_ext *ext) { + guard(rwsem_write)(&psy->ext_lock); + if (psy->ext) return -EEXIST; @@ -1325,6 +1365,8 @@ EXPORT_SYMBOL_GPL(power_supply_register_extension); void power_supply_unregister_extension(struct power_supply *psy, const struct power_supply_ext *ext) { + guard(rwsem_write)(&psy->ext_lock); + if (psy->ext != ext) dev_warn(&psy->dev, "Trying to unregister invalid extension"); @@ -1492,6 +1534,7 @@ __power_supply_register(struct device *parent, } spin_lock_init(&psy->changed_lock); + init_rwsem(&psy->ext_lock); rc = device_add(dev); if (rc) goto device_add_failed; diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c index 3487f161e9bf..d47804cbb500 100644 --- a/drivers/power/supply/power_supply_sysfs.c +++ b/drivers/power/supply/power_supply_sysfs.c @@ -378,8 +378,8 @@ static umode_t power_supply_attr_is_visible(struct kobject *kobj, if (attrno == POWER_SUPPLY_PROP_TYPE) return mode; - if (power_supply_has_property(psy, attrno)) { - if (power_supply_property_is_writeable(psy, attrno) > 0) + if (power_supply_has_property_nolock(psy, attrno)) { + if (power_supply_property_is_writeable_nolock(psy, attrno) > 0) mode |= S_IWUSR; return mode; } @@ -458,7 +458,7 @@ static int add_prop_uevent(const struct device *dev, struct kobj_uevent_env *env int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env) { - const struct power_supply *psy = dev_get_drvdata(dev); + struct power_supply *psy = dev_get_drvdata(dev); const enum power_supply_property *battery_props = power_supply_battery_info_properties; unsigned long psy_drv_properties[POWER_SUPPLY_ATTR_CNT / @@ -490,13 +490,15 @@ int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env) if (ret) goto out; - if (psy->ext) { - for (j = 0; j < psy->ext->num_properties; j++) { - set_bit(psy->ext->properties[j], psy_drv_properties); - ret = add_prop_uevent(dev, env, psy->ext->properties[j], - prop_buf); - if (ret) - goto out; + scoped_guard(rwsem_read, &psy->ext_lock) { + if (psy->ext) { + for (j = 0; j < psy->ext->num_properties; j++) { + set_bit(psy->ext->properties[j], psy_drv_properties); + ret = add_prop_uevent(dev, env, psy->ext->properties[j], + prop_buf); + if (ret) + goto out; + } } } diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 0c1e23081d2a..6bba7e6ab161 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -306,6 +307,7 @@ struct power_supply { size_t num_supplies; struct device_node *of_node; + struct rw_semaphore ext_lock; const struct power_supply_ext *ext; /* Driver private data */ -- 2.45.2