mbox series

[0/8] Add support for binding ACPI platform profile to multiple drivers

Message ID 20241025193055.2235-1-mario.limonciello@amd.com
Headers show
Series Add support for binding ACPI platform profile to multiple drivers | expand

Message

Mario Limonciello Oct. 25, 2024, 7:30 p.m. UTC
Currently there are a number of ASUS products on the market that happen to
have ACPI objects for amd-pmf to bind to as well as an ACPI platform profile
provided by asus-wmi.

The ACPI platform profile support created by amd-pmf on these ASUS products is "Function 9"
which is specifically for "BIOS or EC notification" of power slider position.
This feature is actively used by some designs such as Framework 13 and Framework 16.

On these ASUS designs we keep on quirking more and more of them to turn off this
notification so that asus-wmi can bind.

This however isn't how Windows works.  "Multiple" things are notified for the power
slider position. This series adjusts Linux to behave similarly.

Multiple drivers can now register an ACPI platform profile and will react to set requests.

To avoid chaos, only positions that are common to both drivers are accepted.

This also allows dropping all of the PMF quirks from amd-pmf.

Mario Limonciello (8):
  ACPI: platform-profile: Add a name member to handlers
  platform/surface: aggregator: Add platform handler pointer to device
  ACPI: platform_profile: Add platform handler argument to
    platform_profile_remove()
  ACPI: platform_profile: Add a list to platform profile handler
  ACPI: platform_profile: Use guard(mutex) for register/unregister
  ACPI: platform_profile: Only remove group when no more handler
    registered
  ACPI: platform_profile: Add support for multiple handlers
  platform/x86/amd: pmf: Drop all quirks

 drivers/acpi/platform_profile.c               | 234 ++++++++++--------
 .../surface/surface_platform_profile.c        |   4 +-
 drivers/platform/x86/acer-wmi.c               |   5 +-
 drivers/platform/x86/amd/pmf/Makefile         |   2 +-
 drivers/platform/x86/amd/pmf/core.c           |   1 -
 drivers/platform/x86/amd/pmf/pmf-quirks.c     |  66 -----
 drivers/platform/x86/amd/pmf/pmf.h            |   3 -
 drivers/platform/x86/amd/pmf/sps.c            |   3 +-
 drivers/platform/x86/asus-wmi.c               |   5 +-
 drivers/platform/x86/dell/dell-pc.c           |   3 +-
 drivers/platform/x86/hp/hp-wmi.c              |   3 +-
 drivers/platform/x86/ideapad-laptop.c         |   3 +-
 .../platform/x86/inspur_platform_profile.c    |   5 +-
 drivers/platform/x86/thinkpad_acpi.c          |   3 +-
 include/linux/platform_profile.h              |   4 +-
 include/linux/surface_aggregator/device.h     |   2 +
 16 files changed, 165 insertions(+), 181 deletions(-)
 delete mode 100644 drivers/platform/x86/amd/pmf/pmf-quirks.c


base-commit: 1349dd7dc21c63c9bad0e91fd1bf5f1ada34b0e2

Comments

Mario Limonciello Oct. 25, 2024, 7:30 p.m. UTC | #1
Multiple drivers may attempt to register platform profile handlers,
but only one may be registered and the behavior is non-deterministic
for which one wins.  It's mostly controlled by probing order.

This can be problematic if one driver changes CPU settings and another
driver notifies the EC for changing fan curves.

Modify the ACPI platform profile handler to let multiple drivers
register platform profile handlers and abstract this detail from userspace.
Hans de Goede Oct. 26, 2024, 10:06 a.m. UTC | #2
Hi,

On 25-Oct-24 9:30 PM, Mario Limonciello wrote:
> In order to prepare for allowing multiple handlers, introduce
> a name field that can be used to distinguish between different
> handlers.
> 
> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>

Thanks, patch looks good to me:

Reviewed-by: Hans de Goede <hdegoede@redhat.com>

Regards,

Hans



> ---
>  drivers/platform/surface/surface_platform_profile.c | 1 +
>  drivers/platform/x86/acer-wmi.c                     | 1 +
>  drivers/platform/x86/amd/pmf/sps.c                  | 1 +
>  drivers/platform/x86/asus-wmi.c                     | 1 +
>  drivers/platform/x86/dell/dell-pc.c                 | 1 +
>  drivers/platform/x86/hp/hp-wmi.c                    | 1 +
>  drivers/platform/x86/ideapad-laptop.c               | 1 +
>  drivers/platform/x86/inspur_platform_profile.c      | 1 +
>  drivers/platform/x86/thinkpad_acpi.c                | 1 +
>  include/linux/platform_profile.h                    | 1 +
>  10 files changed, 10 insertions(+)
> 
> diff --git a/drivers/platform/surface/surface_platform_profile.c b/drivers/platform/surface/surface_platform_profile.c
> index 3de864bc66108..61aa488a80eb5 100644
> --- a/drivers/platform/surface/surface_platform_profile.c
> +++ b/drivers/platform/surface/surface_platform_profile.c
> @@ -211,6 +211,7 @@ static int surface_platform_profile_probe(struct ssam_device *sdev)
>  
>  	tpd->sdev = sdev;
>  
> +	tpd->handler.name = "Surface Platform Profile";
>  	tpd->handler.profile_get = ssam_platform_profile_get;
>  	tpd->handler.profile_set = ssam_platform_profile_set;
>  
> diff --git a/drivers/platform/x86/acer-wmi.c b/drivers/platform/x86/acer-wmi.c
> index d09baa3d3d902..53fbc9b4d3df7 100644
> --- a/drivers/platform/x86/acer-wmi.c
> +++ b/drivers/platform/x86/acer-wmi.c
> @@ -1878,6 +1878,7 @@ static int acer_platform_profile_setup(void)
>  	if (quirks->predator_v4) {
>  		int err;
>  
> +		platform_profile_handler.name = "acer-wmi";
>  		platform_profile_handler.profile_get =
>  			acer_predator_v4_platform_profile_get;
>  		platform_profile_handler.profile_set =
> diff --git a/drivers/platform/x86/amd/pmf/sps.c b/drivers/platform/x86/amd/pmf/sps.c
> index 92f7fb22277dc..e2d0cc92c4396 100644
> --- a/drivers/platform/x86/amd/pmf/sps.c
> +++ b/drivers/platform/x86/amd/pmf/sps.c
> @@ -405,6 +405,7 @@ int amd_pmf_init_sps(struct amd_pmf_dev *dev)
>  		amd_pmf_set_sps_power_limits(dev);
>  	}
>  
> +	dev->pprof.name = "amd-pmf";
>  	dev->pprof.profile_get = amd_pmf_profile_get;
>  	dev->pprof.profile_set = amd_pmf_profile_set;
>  
> diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c
> index 2ccc23b259d3e..c7c104c65a85a 100644
> --- a/drivers/platform/x86/asus-wmi.c
> +++ b/drivers/platform/x86/asus-wmi.c
> @@ -3910,6 +3910,7 @@ static int platform_profile_setup(struct asus_wmi *asus)
>  
>  	dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
>  
> +	asus->platform_profile_handler.name = "asus-wmi";
>  	asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
>  	asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
>  
> diff --git a/drivers/platform/x86/dell/dell-pc.c b/drivers/platform/x86/dell/dell-pc.c
> index 972385ca1990b..3cf79e55e3129 100644
> --- a/drivers/platform/x86/dell/dell-pc.c
> +++ b/drivers/platform/x86/dell/dell-pc.c
> @@ -247,6 +247,7 @@ static int thermal_init(void)
>  	thermal_handler = kzalloc(sizeof(*thermal_handler), GFP_KERNEL);
>  	if (!thermal_handler)
>  		return -ENOMEM;
> +	thermal_handler->name = "dell-pc";
>  	thermal_handler->profile_get = thermal_platform_profile_get;
>  	thermal_handler->profile_set = thermal_platform_profile_set;
>  
> diff --git a/drivers/platform/x86/hp/hp-wmi.c b/drivers/platform/x86/hp/hp-wmi.c
> index 81ccc96ffe40a..26cac73caf2b9 100644
> --- a/drivers/platform/x86/hp/hp-wmi.c
> +++ b/drivers/platform/x86/hp/hp-wmi.c
> @@ -1624,6 +1624,7 @@ static int thermal_profile_setup(void)
>  		set_bit(PLATFORM_PROFILE_COOL, platform_profile_handler.choices);
>  	}
>  
> +	platform_profile_handler.name = "hp-wmi";
>  	set_bit(PLATFORM_PROFILE_BALANCED, platform_profile_handler.choices);
>  	set_bit(PLATFORM_PROFILE_PERFORMANCE, platform_profile_handler.choices);
>  
> diff --git a/drivers/platform/x86/ideapad-laptop.c b/drivers/platform/x86/ideapad-laptop.c
> index 9d8c3f064050e..1f94c14c3b832 100644
> --- a/drivers/platform/x86/ideapad-laptop.c
> +++ b/drivers/platform/x86/ideapad-laptop.c
> @@ -1102,6 +1102,7 @@ static int ideapad_dytc_profile_init(struct ideapad_private *priv)
>  
>  	mutex_init(&priv->dytc->mutex);
>  
> +	priv->dytc->pprof.name = "ideapad-laptop";
>  	priv->dytc->priv = priv;
>  	priv->dytc->pprof.profile_get = dytc_profile_get;
>  	priv->dytc->pprof.profile_set = dytc_profile_set;
> diff --git a/drivers/platform/x86/inspur_platform_profile.c b/drivers/platform/x86/inspur_platform_profile.c
> index 8440defa67886..03da2c8cf6789 100644
> --- a/drivers/platform/x86/inspur_platform_profile.c
> +++ b/drivers/platform/x86/inspur_platform_profile.c
> @@ -177,6 +177,7 @@ static int inspur_wmi_probe(struct wmi_device *wdev, const void *context)
>  	priv->wdev = wdev;
>  	dev_set_drvdata(&wdev->dev, priv);
>  
> +	priv->handler.name = "inspur-wmi";
>  	priv->handler.profile_get = inspur_platform_profile_get;
>  	priv->handler.profile_set = inspur_platform_profile_set;
>  
> diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c
> index 4c1b0553f8720..c8c316b8507a5 100644
> --- a/drivers/platform/x86/thinkpad_acpi.c
> +++ b/drivers/platform/x86/thinkpad_acpi.c
> @@ -10549,6 +10549,7 @@ static void dytc_profile_refresh(void)
>  }
>  
>  static struct platform_profile_handler dytc_profile = {
> +	.name = "thinkpad-acpi",
>  	.profile_get = dytc_profile_get,
>  	.profile_set = dytc_profile_set,
>  };
> diff --git a/include/linux/platform_profile.h b/include/linux/platform_profile.h
> index f5492ed413f36..6fa988e417428 100644
> --- a/include/linux/platform_profile.h
> +++ b/include/linux/platform_profile.h
> @@ -27,6 +27,7 @@ enum platform_profile_option {
>  };
>  
>  struct platform_profile_handler {
> +	const char *name;
>  	unsigned long choices[BITS_TO_LONGS(PLATFORM_PROFILE_LAST)];
>  	int (*profile_get)(struct platform_profile_handler *pprof,
>  				enum platform_profile_option *profile);
Hans de Goede Oct. 26, 2024, 10:07 a.m. UTC | #3
Hi,

On 25-Oct-24 9:30 PM, Mario Limonciello wrote:
> To allow registering and unregistering multiple platform handlers calls
> to platform_profile_remove() will need to know which handler is to be
> removed.  Add an argument for this.
> 
> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>

Thanks, patch looks good to me:

Reviewed-by: Hans de Goede <hdegoede@redhat.com>

Regards,

Hans



> ---
>  drivers/acpi/platform_profile.c                     | 2 +-
>  drivers/platform/surface/surface_platform_profile.c | 2 +-
>  drivers/platform/x86/acer-wmi.c                     | 4 ++--
>  drivers/platform/x86/amd/pmf/sps.c                  | 2 +-
>  drivers/platform/x86/asus-wmi.c                     | 4 ++--
>  drivers/platform/x86/dell/dell-pc.c                 | 2 +-
>  drivers/platform/x86/hp/hp-wmi.c                    | 2 +-
>  drivers/platform/x86/ideapad-laptop.c               | 2 +-
>  drivers/platform/x86/inspur_platform_profile.c      | 4 +++-
>  drivers/platform/x86/thinkpad_acpi.c                | 2 +-
>  include/linux/platform_profile.h                    | 2 +-
>  11 files changed, 15 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
> index d2f7fd7743a13..c24744da20916 100644
> --- a/drivers/acpi/platform_profile.c
> +++ b/drivers/acpi/platform_profile.c
> @@ -205,7 +205,7 @@ int platform_profile_register(struct platform_profile_handler *pprof)
>  }
>  EXPORT_SYMBOL_GPL(platform_profile_register);
>  
> -int platform_profile_remove(void)
> +int platform_profile_remove(struct platform_profile_handler *pprof)
>  {
>  	sysfs_remove_group(acpi_kobj, &platform_profile_group);
>  
> diff --git a/drivers/platform/surface/surface_platform_profile.c b/drivers/platform/surface/surface_platform_profile.c
> index 958afd7bce223..0879b739c5e8b 100644
> --- a/drivers/platform/surface/surface_platform_profile.c
> +++ b/drivers/platform/surface/surface_platform_profile.c
> @@ -228,7 +228,7 @@ static int surface_platform_profile_probe(struct ssam_device *sdev)
>  
>  static void surface_platform_profile_remove(struct ssam_device *sdev)
>  {
> -	platform_profile_remove();
> +	platform_profile_remove(&sdev->tpd->handler);
>  }
>  
>  static const struct ssam_device_id ssam_platform_profile_match[] = {
> diff --git a/drivers/platform/x86/acer-wmi.c b/drivers/platform/x86/acer-wmi.c
> index 53fbc9b4d3df7..71761d4220c26 100644
> --- a/drivers/platform/x86/acer-wmi.c
> +++ b/drivers/platform/x86/acer-wmi.c
> @@ -2546,7 +2546,7 @@ static int acer_platform_probe(struct platform_device *device)
>  
>  error_hwmon:
>  	if (platform_profile_support)
> -		platform_profile_remove();
> +		platform_profile_remove(&platform_profile_handler);
>  error_platform_profile:
>  	acer_rfkill_exit();
>  error_rfkill:
> @@ -2569,7 +2569,7 @@ static void acer_platform_remove(struct platform_device *device)
>  	acer_rfkill_exit();
>  
>  	if (platform_profile_support)
> -		platform_profile_remove();
> +		platform_profile_remove(&platform_profile_handler);
>  }
>  
>  #ifdef CONFIG_PM_SLEEP
> diff --git a/drivers/platform/x86/amd/pmf/sps.c b/drivers/platform/x86/amd/pmf/sps.c
> index e2d0cc92c4396..cfa88c0c9e594 100644
> --- a/drivers/platform/x86/amd/pmf/sps.c
> +++ b/drivers/platform/x86/amd/pmf/sps.c
> @@ -425,5 +425,5 @@ int amd_pmf_init_sps(struct amd_pmf_dev *dev)
>  
>  void amd_pmf_deinit_sps(struct amd_pmf_dev *dev)
>  {
> -	platform_profile_remove();
> +	platform_profile_remove(&dev->pprof);
>  }
> diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c
> index c7c104c65a85a..f5f8cda7fd19c 100644
> --- a/drivers/platform/x86/asus-wmi.c
> +++ b/drivers/platform/x86/asus-wmi.c
> @@ -4885,7 +4885,7 @@ static int asus_wmi_add(struct platform_device *pdev)
>  fail_custom_fan_curve:
>  fail_platform_profile_setup:
>  	if (asus->platform_profile_support)
> -		platform_profile_remove();
> +		platform_profile_remove(&asus->platform_profile_handler);
>  fail_fan_boost_mode:
>  fail_platform:
>  	kfree(asus);
> @@ -4912,7 +4912,7 @@ static void asus_wmi_remove(struct platform_device *device)
>  	asus_wmi_battery_exit(asus);
>  
>  	if (asus->platform_profile_support)
> -		platform_profile_remove();
> +		platform_profile_remove(&asus->platform_profile_handler);
>  
>  	kfree(asus);
>  }
> diff --git a/drivers/platform/x86/dell/dell-pc.c b/drivers/platform/x86/dell/dell-pc.c
> index 3cf79e55e3129..4196154cc37d9 100644
> --- a/drivers/platform/x86/dell/dell-pc.c
> +++ b/drivers/platform/x86/dell/dell-pc.c
> @@ -273,7 +273,7 @@ static int thermal_init(void)
>  static void thermal_cleanup(void)
>  {
>  	if (thermal_handler) {
> -		platform_profile_remove();
> +		platform_profile_remove(thermal_handler);
>  		kfree(thermal_handler);
>  	}
>  }
> diff --git a/drivers/platform/x86/hp/hp-wmi.c b/drivers/platform/x86/hp/hp-wmi.c
> index 26cac73caf2b9..bb8771d8b5cd8 100644
> --- a/drivers/platform/x86/hp/hp-wmi.c
> +++ b/drivers/platform/x86/hp/hp-wmi.c
> @@ -1692,7 +1692,7 @@ static void __exit hp_wmi_bios_remove(struct platform_device *device)
>  	}
>  
>  	if (platform_profile_support)
> -		platform_profile_remove();
> +		platform_profile_remove(&platform_profile_handler);
>  }
>  
>  static int hp_wmi_resume_handler(struct device *device)
> diff --git a/drivers/platform/x86/ideapad-laptop.c b/drivers/platform/x86/ideapad-laptop.c
> index 1f94c14c3b832..50819ac919e87 100644
> --- a/drivers/platform/x86/ideapad-laptop.c
> +++ b/drivers/platform/x86/ideapad-laptop.c
> @@ -1135,7 +1135,7 @@ static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
>  	if (!priv->dytc)
>  		return;
>  
> -	platform_profile_remove();
> +	platform_profile_remove(&priv->dytc->pprof);
>  	mutex_destroy(&priv->dytc->mutex);
>  	kfree(priv->dytc);
>  
> diff --git a/drivers/platform/x86/inspur_platform_profile.c b/drivers/platform/x86/inspur_platform_profile.c
> index 03da2c8cf6789..f6bc5ca9da91d 100644
> --- a/drivers/platform/x86/inspur_platform_profile.c
> +++ b/drivers/platform/x86/inspur_platform_profile.c
> @@ -190,7 +190,9 @@ static int inspur_wmi_probe(struct wmi_device *wdev, const void *context)
>  
>  static void inspur_wmi_remove(struct wmi_device *wdev)
>  {
> -	platform_profile_remove();
> +	struct inspur_wmi_priv *priv;
> +	priv = dev_get_drvdata(&wdev->dev);
> +	platform_profile_remove(&priv->handler);
>  }
>  
>  static const struct wmi_device_id inspur_wmi_id_table[] = {
> diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c
> index c8c316b8507a5..619a4db74e5f3 100644
> --- a/drivers/platform/x86/thinkpad_acpi.c
> +++ b/drivers/platform/x86/thinkpad_acpi.c
> @@ -10637,7 +10637,7 @@ static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
>  
>  static void dytc_profile_exit(void)
>  {
> -	platform_profile_remove();
> +	platform_profile_remove(&dytc_profile);
>  }
>  
>  static struct ibm_struct  dytc_profile_driver_data = {
> diff --git a/include/linux/platform_profile.h b/include/linux/platform_profile.h
> index 6fa988e417428..58279b76d740e 100644
> --- a/include/linux/platform_profile.h
> +++ b/include/linux/platform_profile.h
> @@ -36,7 +36,7 @@ struct platform_profile_handler {
>  };
>  
>  int platform_profile_register(struct platform_profile_handler *pprof);
> -int platform_profile_remove(void);
> +int platform_profile_remove(struct platform_profile_handler *pprof);
>  int platform_profile_cycle(void);
>  void platform_profile_notify(void);
>
Hans de Goede Oct. 26, 2024, 10:07 a.m. UTC | #4
Hi,

On 25-Oct-24 9:30 PM, Mario Limonciello wrote:
> guard(mutex) can be used to automatically release mutexes when going
> out of scope.
> 
> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>

Thanks, patch looks good to me:

Reviewed-by: Hans de Goede <hdegoede@redhat.com>

Regards,

Hans



> ---
>  drivers/acpi/platform_profile.c | 19 ++++++-------------
>  1 file changed, 6 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
> index 0c60fc970b6e8..81928adccfade 100644
> --- a/drivers/acpi/platform_profile.c
> +++ b/drivers/acpi/platform_profile.c
> @@ -180,41 +180,34 @@ int platform_profile_register(struct platform_profile_handler *pprof)
>  {
>  	int err;
>  
> -	mutex_lock(&profile_lock);
> +	guard(mutex)(&profile_lock);
>  	/* We can only have one active profile */
> -	if (cur_profile) {
> -		mutex_unlock(&profile_lock);
> +	if (cur_profile)
>  		return -EEXIST;
> -	}
>  
>  	/* Sanity check the profile handler field are set */
>  	if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
> -		!pprof->profile_set || !pprof->profile_get) {
> -		mutex_unlock(&profile_lock);
> +		!pprof->profile_set || !pprof->profile_get)
>  		return -EINVAL;
> -	}
>  
>  	err = sysfs_create_group(acpi_kobj, &platform_profile_group);
> -	if (err) {
> -		mutex_unlock(&profile_lock);
> +	if (err)
>  		return err;
> -	}
>  	list_add_tail(&pprof->list, &platform_profile_handler_list);
>  
>  	cur_profile = pprof;
> -	mutex_unlock(&profile_lock);
>  	return 0;
>  }
>  EXPORT_SYMBOL_GPL(platform_profile_register);
>  
>  int platform_profile_remove(struct platform_profile_handler *pprof)
>  {
> +	guard(mutex)(&profile_lock);
> +
>  	list_del(&pprof->list);
>  
>  	sysfs_remove_group(acpi_kobj, &platform_profile_group);
> -	mutex_lock(&profile_lock);
>  	cur_profile = NULL;
> -	mutex_unlock(&profile_lock);
>  	return 0;
>  }
>  EXPORT_SYMBOL_GPL(platform_profile_remove);
Hans de Goede Oct. 26, 2024, 10:30 a.m. UTC | #5
Hi Mario,

On 25-Oct-24 9:30 PM, Mario Limonciello wrote:
> Multiple drivers may attempt to register platform profile handlers,
> but only one may be registered and the behavior is non-deterministic
> for which one wins.  It's mostly controlled by probing order.
> 
> This can be problematic if one driver changes CPU settings and another
> driver notifies the EC for changing fan curves.
> 
> Modify the ACPI platform profile handler to let multiple drivers
> register platform profile handlers and abstract this detail from userspace.
> 
> From userspace perspective the user will see profiles available across
> both drivers.  However to avoid chaos only allow changing to profiles
> that are common in both drivers.
> 
> If any problems occur when changing profiles for any driver, then revert
> back to the previous profile.
> 
> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  drivers/acpi/platform_profile.c | 203 ++++++++++++++++++--------------
>  1 file changed, 117 insertions(+), 86 deletions(-)
> 
> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
> index 091ca6941a925..915e3c49f0b5f 100644
> --- a/drivers/acpi/platform_profile.c
> +++ b/drivers/acpi/platform_profile.c
> @@ -9,7 +9,6 @@
>  #include <linux/platform_profile.h>
>  #include <linux/sysfs.h>
>  
> -static struct platform_profile_handler *cur_profile;
>  static LIST_HEAD(platform_profile_handler_list);
>  static DEFINE_MUTEX(profile_lock);
>  
> @@ -36,26 +35,26 @@ static ssize_t platform_profile_choices_show(struct device *dev,
>  					struct device_attribute *attr,
>  					char *buf)
>  {
> +	struct platform_profile_handler *handler;
> +	unsigned long seen = 0;
>  	int len = 0;
> -	int err, i;
> -
> -	err = mutex_lock_interruptible(&profile_lock);
> -	if (err)
> -		return err;
> -
> -	if (!cur_profile) {
> -		mutex_unlock(&profile_lock);
> -		return -ENODEV;
> +	int i;
> +
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			for_each_set_bit(i, handler->choices, PLATFORM_PROFILE_LAST) {
> +				if (seen & BIT(i))
> +					continue;
> +				if (len == 0)
> +					len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
> +				else
> +					len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
> +				seen |= BIT(i);
> +			}
> +		}
>  	}

Since only choices that are available in all registered handlers will be accepted,
should the output not be limited to only those choices ?

E.g.:

	unsigned long choices = 0;
	bool first = true;

	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
		list_for_each_entry(handler, &platform_profile_handler_list, list) {
			if (first) {
				choices = handler->choices;
				first = false;
			} else {
				choices &= handler->choices;
			}
		}
	}

	for_each_set_bit(i, choices, PLATFORM_PROFILE_LAST) {
		if (len == 0)
			len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
		else
			len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
	}
  	len += sysfs_emit_at(buf, len, "\n");
  	return len;
}

?

Also this means that choices can change now as drivers get loaded /
removed. I believe that power-profiles-daemon matches has some hotplug
handling for the sysfs files showing up? How would that work with choices
changing ?

Or am I misremembering and does p-p-d simply assume all drivers are loaded
when it starts ?

> @@ -64,22 +63,20 @@ static ssize_t platform_profile_show(struct device *dev,
>  					char *buf)
>  {
>  	enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
> +	struct platform_profile_handler *handler;
>  	int err;
>  
> -	err = mutex_lock_interruptible(&profile_lock);
> -	if (err)
> -		return err;
>  
> -	if (!cur_profile) {
> -		mutex_unlock(&profile_lock);
> -		return -ENODEV;
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> +		if (!platform_profile_is_registered())
> +			return -ENODEV;
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			err = handler->profile_get(handler, &profile);
> +			if (err)
> +				return err;
> +		}
>  	}

Hmm this just goes with the platform returned by the last handler
called ?

Maybe compare results and log some warning if there are different
results between handlers ?

And maybe also:

1. New patch enforcing that all handlers must support plain balanced
at registration time.

2. Check that all handlers agree when a new handler gets registered
and if not then force all handlers to balanced, together with
a sysfs_notify() ?


>  
> -	err = cur_profile->profile_get(cur_profile, &profile);
> -	mutex_unlock(&profile_lock);
> -	if (err)
> -		return err;
> -
>  	/* Check that profile is valid index */
>  	if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
>  		return -EIO;
> @@ -91,37 +88,48 @@ static ssize_t platform_profile_store(struct device *dev,
>  			    struct device_attribute *attr,
>  			    const char *buf, size_t count)
>  {
> +	struct platform_profile_handler *handler;
> +	enum platform_profile_option profile;
>  	int err, i;
>  
> -	err = mutex_lock_interruptible(&profile_lock);
> -	if (err)
> -		return err;
> -
> -	if (!cur_profile) {
> -		mutex_unlock(&profile_lock);
> -		return -ENODEV;
> -	}
> -
>  	/* Scan for a matching profile */
>  	i = sysfs_match_string(profile_names, buf);
>  	if (i < 0) {
> -		mutex_unlock(&profile_lock);
>  		return -EINVAL;
>  	}
>  
> -	/* Check that platform supports this profile choice */
> -	if (!test_bit(i, cur_profile->choices)) {
> -		mutex_unlock(&profile_lock);
> -		return -EOPNOTSUPP;
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> +		if (!platform_profile_is_registered())
> +			return -ENODEV;
> +
> +		/* Check that all handlers support this profile choice */
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			if (!test_bit(i, handler->choices))
> +				return -EOPNOTSUPP;
> +
> +			/* save the profile so that it can be reverted if necessary */
> +			err = handler->profile_get(handler, &profile);
> +			if (err)
> +				return err;
> +		}

Same issue as before, you are only saving the profile of the last handler called here,
which might even be a profile not supported by all handlers...

Might be easiest to just enforce all handlers support plain balanced
as I suggested above and then on errors revert all handlers to balanced.

This may seem like it is not nice to do, but errors should not happen
so I think this is ok. And if errors do happen then we need to fix
the errors :)

> +
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			err = handler->profile_set(handler, i);
> +			if (err) {
> +				pr_err("Failed to set profile for handler %s\n", handler->name);
> +				break;
> +			}
> +		}
> +		if (err) {
> +			list_for_each_entry_continue_reverse(handler, &platform_profile_handler_list, list) {
> +				if (handler->profile_set(handler, profile))
> +					pr_err("Failed to revert profile for handler %s\n", handler->name);
> +			}
> +			return err;
> +		}
>  	}
>  
> -	err = cur_profile->profile_set(cur_profile, i);
> -	if (!err)
> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
> -
> -	mutex_unlock(&profile_lock);
> -	if (err)
> -		return err;
> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>  	return count;
>  }
>  
> @@ -140,7 +148,8 @@ static const struct attribute_group platform_profile_group = {
>  
>  void platform_profile_notify(void)
>  {
> -	if (!cur_profile)
> +	guard(mutex)(&profile_lock);
> +	if (!platform_profile_is_registered())
>  		return;
>  	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>  }
> @@ -148,40 +157,65 @@ EXPORT_SYMBOL_GPL(platform_profile_notify);
>  
>  int platform_profile_cycle(void)
>  {
> +	struct platform_profile_handler *handler;
>  	enum platform_profile_option profile;
> -	enum platform_profile_option next;
> +	enum platform_profile_option next = PLATFORM_PROFILE_LAST;
> +	enum platform_profile_option next2 = PLATFORM_PROFILE_LAST;
>  	int err;
>  
> -	err = mutex_lock_interruptible(&profile_lock);
> -	if (err)
> -		return err;
> -
> -	if (!cur_profile) {
> -		mutex_unlock(&profile_lock);
> -		return -ENODEV;
> -	}
> -
> -	err = cur_profile->profile_get(cur_profile, &profile);
> -	if (err) {
> -		mutex_unlock(&profile_lock);
> -		return err;
> -	}
> -
> -	next = find_next_bit_wrap(cur_profile->choices, PLATFORM_PROFILE_LAST,
> -				  profile + 1);
> -
> -	if (WARN_ON(next == PLATFORM_PROFILE_LAST)) {
> -		mutex_unlock(&profile_lock);
> -		return -EINVAL;
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> +		/* first pass, make sure all handlers agree on the definition of "next" profile */
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +
> +			err = handler->profile_get(handler, &profile);
> +			if (err)
> +				return err;
> +
> +			if (next == PLATFORM_PROFILE_LAST)
> +				next = find_next_bit_wrap(handler->choices,
> +							  PLATFORM_PROFILE_LAST,
> +							  profile + 1);
> +			else
> +				next2 = find_next_bit_wrap(handler->choices,
> +							   PLATFORM_PROFILE_LAST,
> +							   profile + 1);
> +
> +			if (WARN_ON(next == PLATFORM_PROFILE_LAST))
> +				return -EINVAL;
> +
> +			if (next2 == PLATFORM_PROFILE_LAST)
> +				continue;
> +
> +			if (next != next2) {
> +				pr_warn("Next profile to cycle to is ambiguous between platform_profile handlers\n");
> +				return -EINVAL;
> +			}
> +			next = next2;
> +		}

Hmm, this seems complicated, I would suggest to factor out the code
to "and" together all the handler's choices which I suggested above
for platform_profile_choices_show() into a helper (with the locking
to be done by the caller) and then call that helper here to get
a choices which is the result if all the choices and-ed together and
simply call find_next_bit_wrap() on the resulting and-ed value ?

Ah I guess another issue is that the handlers may also differ on
which profile they return from handler->profile_get(), so same
issue as in platform_profile_show(). I think this requires
another factored out helper to get a single consistent profile
value for all handlers. Then this helper can be used both in
platform_profile_show() and here to get a "truth" value for the
current active profile and show that / use that as base to pick
the next value.

Note the above approach definitely is going to have issues
if handlers mismatch on which profiles are supported since
you do not skip choices which are only available in one of
the handlers.

> +
> +		/*
> +		 * Second pass: apply "next" to each handler
> +		 * If any failures occur unwind and revert all back to the original profile
> +		 */
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			err = handler->profile_set(handler, next);
> +			if (err) {
> +				pr_err("Failed to set profile for handler %s\n", handler->name);
> +				break;
> +			}
> +		}
> +		if (err) {
> +			list_for_each_entry_continue_reverse(handler, &platform_profile_handler_list, list) {
> +				err = handler->profile_set(handler, profile);

Same issue as before, profile contains the profile of the last handler
in the list only.


> +				if (err)
> +					pr_err("Failed to revert profile for handler %s\n", handler->name);
> +			}
> +		}
>  	}
>  
> -	err = cur_profile->profile_set(cur_profile, next);
> -	mutex_unlock(&profile_lock);
> -
> -	if (!err)
> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>  
> -	return err;
> +	return 0;
>  }
>  EXPORT_SYMBOL_GPL(platform_profile_cycle);
>  
> @@ -190,21 +224,19 @@ int platform_profile_register(struct platform_profile_handler *pprof)
>  	int err;
>  
>  	guard(mutex)(&profile_lock);
> -	/* We can only have one active profile */
> -	if (cur_profile)
> -		return -EEXIST;
>  
>  	/* Sanity check the profile handler field are set */
>  	if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
>  		!pprof->profile_set || !pprof->profile_get)
>  		return -EINVAL;
>  
> -	err = sysfs_create_group(acpi_kobj, &platform_profile_group);
> -	if (err)
> -		return err;
> +	if (!platform_profile_is_registered()) {
> +		err = sysfs_create_group(acpi_kobj, &platform_profile_group);
> +		if (err)
> +			return err;
> +	}
>  	list_add_tail(&pprof->list, &platform_profile_handler_list);
>  
> -	cur_profile = pprof;
>  	return 0;
>  }
>  EXPORT_SYMBOL_GPL(platform_profile_register);
> @@ -215,7 +247,6 @@ int platform_profile_remove(struct platform_profile_handler *pprof)
>  
>  	list_del(&pprof->list);
>  
> -	cur_profile = NULL;
>  	if (!platform_profile_is_registered())
>  		sysfs_remove_group(acpi_kobj, &platform_profile_group);
>  


Regards,

Hans
Armin Wolf Oct. 26, 2024, 12:16 p.m. UTC | #6
Am 26.10.24 um 12:30 schrieb Hans de Goede:

> Hi Mario,
>
> On 25-Oct-24 9:30 PM, Mario Limonciello wrote:
>> Multiple drivers may attempt to register platform profile handlers,
>> but only one may be registered and the behavior is non-deterministic
>> for which one wins.  It's mostly controlled by probing order.
>>
>> This can be problematic if one driver changes CPU settings and another
>> driver notifies the EC for changing fan curves.
>>
>> Modify the ACPI platform profile handler to let multiple drivers
>> register platform profile handlers and abstract this detail from userspace.
>>
>>  From userspace perspective the user will see profiles available across
>> both drivers.  However to avoid chaos only allow changing to profiles
>> that are common in both drivers.
>>
>> If any problems occur when changing profiles for any driver, then revert
>> back to the previous profile.
>>
>> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>> ---
>>   drivers/acpi/platform_profile.c | 203 ++++++++++++++++++--------------
>>   1 file changed, 117 insertions(+), 86 deletions(-)
>>
>> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
>> index 091ca6941a925..915e3c49f0b5f 100644
>> --- a/drivers/acpi/platform_profile.c
>> +++ b/drivers/acpi/platform_profile.c
>> @@ -9,7 +9,6 @@
>>   #include <linux/platform_profile.h>
>>   #include <linux/sysfs.h>
>>
>> -static struct platform_profile_handler *cur_profile;
>>   static LIST_HEAD(platform_profile_handler_list);
>>   static DEFINE_MUTEX(profile_lock);
>>
>> @@ -36,26 +35,26 @@ static ssize_t platform_profile_choices_show(struct device *dev,
>>   					struct device_attribute *attr,
>>   					char *buf)
>>   {
>> +	struct platform_profile_handler *handler;
>> +	unsigned long seen = 0;
>>   	int len = 0;
>> -	int err, i;
>> -
>> -	err = mutex_lock_interruptible(&profile_lock);
>> -	if (err)
>> -		return err;
>> -
>> -	if (!cur_profile) {
>> -		mutex_unlock(&profile_lock);
>> -		return -ENODEV;
>> +	int i;
>> +
>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			for_each_set_bit(i, handler->choices, PLATFORM_PROFILE_LAST) {
>> +				if (seen & BIT(i))
>> +					continue;
>> +				if (len == 0)
>> +					len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>> +				else
>> +					len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>> +				seen |= BIT(i);
>> +			}
>> +		}
>>   	}
> Since only choices that are available in all registered handlers will be accepted,
> should the output not be limited to only those choices ?
>
> E.g.:
>
> 	unsigned long choices = 0;
> 	bool first = true;
>
> 	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> 		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> 			if (first) {
> 				choices = handler->choices;
> 				first = false;
> 			} else {
> 				choices &= handler->choices;
> 			}
> 		}
> 	}
>
> 	for_each_set_bit(i, choices, PLATFORM_PROFILE_LAST) {
> 		if (len == 0)
> 			len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
> 		else
> 			len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
> 	}
>    	len += sysfs_emit_at(buf, len, "\n");
>    	return len;
> }
>
> ?
>
> Also this means that choices can change now as drivers get loaded /
> removed. I believe that power-profiles-daemon matches has some hotplug
> handling for the sysfs files showing up? How would that work with choices
> changing ?
>
> Or am I misremembering and does p-p-d simply assume all drivers are loaded
> when it starts ?

After a quick glance at the source code i think it basically assumes that the
sysfs file is present during startup if platform profiles are supported. If the
sysfs file disappears afterwards, the code will simply return an error.

For handling changing choices, sending a poll notification using sysfs_notify()
to the choices file will be the easiest solution.

>> @@ -64,22 +63,20 @@ static ssize_t platform_profile_show(struct device *dev,
>>   					char *buf)
>>   {
>>   	enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
>> +	struct platform_profile_handler *handler;
>>   	int err;
>>
>> -	err = mutex_lock_interruptible(&profile_lock);
>> -	if (err)
>> -		return err;
>>
>> -	if (!cur_profile) {
>> -		mutex_unlock(&profile_lock);
>> -		return -ENODEV;
>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>> +		if (!platform_profile_is_registered())
>> +			return -ENODEV;
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			err = handler->profile_get(handler, &profile);
>> +			if (err)
>> +				return err;
>> +		}
>>   	}
> Hmm this just goes with the platform returned by the last handler
> called ?
>
> Maybe compare results and log some warning if there are different
> results between handlers ?
>
> And maybe also:
>
> 1. New patch enforcing that all handlers must support plain balanced
> at registration time.
>
> 2. Check that all handlers agree when a new handler gets registered
> and if not then force all handlers to balanced, together with
> a sysfs_notify() ?

I begin to wonder why we even need the profile_get callback anyway. The drivers
are already required to:

	"NOT ... let userspace know about any sub-optimal conditions which are impeding
	 reaching the requested performance level."

So what exactly is the reason to read the platform profile from hardware?

>>
>> -	err = cur_profile->profile_get(cur_profile, &profile);
>> -	mutex_unlock(&profile_lock);
>> -	if (err)
>> -		return err;
>> -
>>   	/* Check that profile is valid index */
>>   	if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
>>   		return -EIO;
>> @@ -91,37 +88,48 @@ static ssize_t platform_profile_store(struct device *dev,
>>   			    struct device_attribute *attr,
>>   			    const char *buf, size_t count)
>>   {
>> +	struct platform_profile_handler *handler;
>> +	enum platform_profile_option profile;
>>   	int err, i;
>>
>> -	err = mutex_lock_interruptible(&profile_lock);
>> -	if (err)
>> -		return err;
>> -
>> -	if (!cur_profile) {
>> -		mutex_unlock(&profile_lock);
>> -		return -ENODEV;
>> -	}
>> -
>>   	/* Scan for a matching profile */
>>   	i = sysfs_match_string(profile_names, buf);
>>   	if (i < 0) {
>> -		mutex_unlock(&profile_lock);
>>   		return -EINVAL;
>>   	}
>>
>> -	/* Check that platform supports this profile choice */
>> -	if (!test_bit(i, cur_profile->choices)) {
>> -		mutex_unlock(&profile_lock);
>> -		return -EOPNOTSUPP;
>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>> +		if (!platform_profile_is_registered())
>> +			return -ENODEV;
>> +
>> +		/* Check that all handlers support this profile choice */
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			if (!test_bit(i, handler->choices))
>> +				return -EOPNOTSUPP;
>> +
>> +			/* save the profile so that it can be reverted if necessary */
>> +			err = handler->profile_get(handler, &profile);
>> +			if (err)
>> +				return err;
>> +		}
> Same issue as before, you are only saving the profile of the last handler called here,
> which might even be a profile not supported by all handlers...
>
> Might be easiest to just enforce all handlers support plain balanced
> as I suggested above and then on errors revert all handlers to balanced.
>
> This may seem like it is not nice to do, but errors should not happen
> so I think this is ok. And if errors do happen then we need to fix
> the errors :)
>
>> +
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			err = handler->profile_set(handler, i);
>> +			if (err) {
>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>> +				break;
>> +			}
>> +		}
>> +		if (err) {
>> +			list_for_each_entry_continue_reverse(handler, &platform_profile_handler_list, list) {
>> +				if (handler->profile_set(handler, profile))
>> +					pr_err("Failed to revert profile for handler %s\n", handler->name);
>> +			}
>> +			return err;
>> +		}
>>   	}
>>
>> -	err = cur_profile->profile_set(cur_profile, i);
>> -	if (!err)
>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>> -
>> -	mutex_unlock(&profile_lock);
>> -	if (err)
>> -		return err;
>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>   	return count;
>>   }
>>
>> @@ -140,7 +148,8 @@ static const struct attribute_group platform_profile_group = {
>>
>>   void platform_profile_notify(void)
>>   {
>> -	if (!cur_profile)
>> +	guard(mutex)(&profile_lock);
>> +	if (!platform_profile_is_registered())
>>   		return;
>>   	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>   }
>> @@ -148,40 +157,65 @@ EXPORT_SYMBOL_GPL(platform_profile_notify);
>>
>>   int platform_profile_cycle(void)
>>   {
>> +	struct platform_profile_handler *handler;
>>   	enum platform_profile_option profile;
>> -	enum platform_profile_option next;
>> +	enum platform_profile_option next = PLATFORM_PROFILE_LAST;
>> +	enum platform_profile_option next2 = PLATFORM_PROFILE_LAST;
>>   	int err;
>>
>> -	err = mutex_lock_interruptible(&profile_lock);
>> -	if (err)
>> -		return err;
>> -
>> -	if (!cur_profile) {
>> -		mutex_unlock(&profile_lock);
>> -		return -ENODEV;
>> -	}
>> -
>> -	err = cur_profile->profile_get(cur_profile, &profile);
>> -	if (err) {
>> -		mutex_unlock(&profile_lock);
>> -		return err;
>> -	}
>> -
>> -	next = find_next_bit_wrap(cur_profile->choices, PLATFORM_PROFILE_LAST,
>> -				  profile + 1);
>> -
>> -	if (WARN_ON(next == PLATFORM_PROFILE_LAST)) {
>> -		mutex_unlock(&profile_lock);
>> -		return -EINVAL;
>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>> +		/* first pass, make sure all handlers agree on the definition of "next" profile */
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +
>> +			err = handler->profile_get(handler, &profile);
>> +			if (err)
>> +				return err;
>> +
>> +			if (next == PLATFORM_PROFILE_LAST)
>> +				next = find_next_bit_wrap(handler->choices,
>> +							  PLATFORM_PROFILE_LAST,
>> +							  profile + 1);
>> +			else
>> +				next2 = find_next_bit_wrap(handler->choices,
>> +							   PLATFORM_PROFILE_LAST,
>> +							   profile + 1);
>> +
>> +			if (WARN_ON(next == PLATFORM_PROFILE_LAST))
>> +				return -EINVAL;
>> +
>> +			if (next2 == PLATFORM_PROFILE_LAST)
>> +				continue;
>> +
>> +			if (next != next2) {
>> +				pr_warn("Next profile to cycle to is ambiguous between platform_profile handlers\n");
>> +				return -EINVAL;
>> +			}
>> +			next = next2;
>> +		}
> Hmm, this seems complicated, I would suggest to factor out the code
> to "and" together all the handler's choices which I suggested above
> for platform_profile_choices_show() into a helper (with the locking
> to be done by the caller) and then call that helper here to get
> a choices which is the result if all the choices and-ed together and
> simply call find_next_bit_wrap() on the resulting and-ed value ?
>
> Ah I guess another issue is that the handlers may also differ on
> which profile they return from handler->profile_get(), so same
> issue as in platform_profile_show(). I think this requires
> another factored out helper to get a single consistent profile
> value for all handlers. Then this helper can be used both in
> platform_profile_show() and here to get a "truth" value for the
> current active profile and show that / use that as base to pick
> the next value.
>
> Note the above approach definitely is going to have issues
> if handlers mismatch on which profiles are supported since
> you do not skip choices which are only available in one of
> the handlers.
>
>> +
>> +		/*
>> +		 * Second pass: apply "next" to each handler
>> +		 * If any failures occur unwind and revert all back to the original profile
>> +		 */
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			err = handler->profile_set(handler, next);
>> +			if (err) {
>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>> +				break;
>> +			}
>> +		}
>> +		if (err) {
>> +			list_for_each_entry_continue_reverse(handler, &platform_profile_handler_list, list) {
>> +				err = handler->profile_set(handler, profile);
> Same issue as before, profile contains the profile of the last handler
> in the list only.
>
>
>> +				if (err)
>> +					pr_err("Failed to revert profile for handler %s\n", handler->name);
>> +			}
>> +		}
>>   	}
>>
>> -	err = cur_profile->profile_set(cur_profile, next);
>> -	mutex_unlock(&profile_lock);
>> -
>> -	if (!err)
>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>
>> -	return err;
>> +	return 0;
>>   }
>>   EXPORT_SYMBOL_GPL(platform_profile_cycle);
>>
>> @@ -190,21 +224,19 @@ int platform_profile_register(struct platform_profile_handler *pprof)
>>   	int err;
>>
>>   	guard(mutex)(&profile_lock);
>> -	/* We can only have one active profile */
>> -	if (cur_profile)
>> -		return -EEXIST;
>>
>>   	/* Sanity check the profile handler field are set */
>>   	if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
>>   		!pprof->profile_set || !pprof->profile_get)
>>   		return -EINVAL;
>>
>> -	err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>> -	if (err)
>> -		return err;
>> +	if (!platform_profile_is_registered()) {
>> +		err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>> +		if (err)
>> +			return err;
>> +	}
>>   	list_add_tail(&pprof->list, &platform_profile_handler_list);
>>
>> -	cur_profile = pprof;
>>   	return 0;
>>   }
>>   EXPORT_SYMBOL_GPL(platform_profile_register);
>> @@ -215,7 +247,6 @@ int platform_profile_remove(struct platform_profile_handler *pprof)
>>
>>   	list_del(&pprof->list);
>>
>> -	cur_profile = NULL;
>>   	if (!platform_profile_is_registered())
>>   		sysfs_remove_group(acpi_kobj, &platform_profile_group);
>>
>
> Regards,
>
> Hans
>
>
>
Mark Pearson Oct. 28, 2024, 10:07 a.m. UTC | #7
Thanks Mario,

On Fri, Oct 25, 2024, at 3:30 PM, Mario Limonciello wrote:
> In order to prepare for allowing multiple handlers, introduce
> a name field that can be used to distinguish between different
> handlers.
>
> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  drivers/platform/surface/surface_platform_profile.c | 1 +
>  drivers/platform/x86/acer-wmi.c                     | 1 +
>  drivers/platform/x86/amd/pmf/sps.c                  | 1 +
>  drivers/platform/x86/asus-wmi.c                     | 1 +
>  drivers/platform/x86/dell/dell-pc.c                 | 1 +
>  drivers/platform/x86/hp/hp-wmi.c                    | 1 +
>  drivers/platform/x86/ideapad-laptop.c               | 1 +
>  drivers/platform/x86/inspur_platform_profile.c      | 1 +
>  drivers/platform/x86/thinkpad_acpi.c                | 1 +
>  include/linux/platform_profile.h                    | 1 +
>  10 files changed, 10 insertions(+)
>
> diff --git a/drivers/platform/surface/surface_platform_profile.c 
> b/drivers/platform/surface/surface_platform_profile.c
> index 3de864bc66108..61aa488a80eb5 100644
> --- a/drivers/platform/surface/surface_platform_profile.c
> +++ b/drivers/platform/surface/surface_platform_profile.c
> @@ -211,6 +211,7 @@ static int surface_platform_profile_probe(struct 
> ssam_device *sdev)
> 
>  	tpd->sdev = sdev;
> 
> +	tpd->handler.name = "Surface Platform Profile";
>  	tpd->handler.profile_get = ssam_platform_profile_get;
>  	tpd->handler.profile_set = ssam_platform_profile_set;
> 
> diff --git a/drivers/platform/x86/acer-wmi.c b/drivers/platform/x86/acer-wmi.c
> index d09baa3d3d902..53fbc9b4d3df7 100644
> --- a/drivers/platform/x86/acer-wmi.c
> +++ b/drivers/platform/x86/acer-wmi.c
> @@ -1878,6 +1878,7 @@ static int acer_platform_profile_setup(void)
>  	if (quirks->predator_v4) {
>  		int err;
> 
> +		platform_profile_handler.name = "acer-wmi";
>  		platform_profile_handler.profile_get =
>  			acer_predator_v4_platform_profile_get;
>  		platform_profile_handler.profile_set =
> diff --git a/drivers/platform/x86/amd/pmf/sps.c 
> b/drivers/platform/x86/amd/pmf/sps.c
> index 92f7fb22277dc..e2d0cc92c4396 100644
> --- a/drivers/platform/x86/amd/pmf/sps.c
> +++ b/drivers/platform/x86/amd/pmf/sps.c
> @@ -405,6 +405,7 @@ int amd_pmf_init_sps(struct amd_pmf_dev *dev)
>  		amd_pmf_set_sps_power_limits(dev);
>  	}
> 
> +	dev->pprof.name = "amd-pmf";
>  	dev->pprof.profile_get = amd_pmf_profile_get;
>  	dev->pprof.profile_set = amd_pmf_profile_set;
> 
> diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c
> index 2ccc23b259d3e..c7c104c65a85a 100644
> --- a/drivers/platform/x86/asus-wmi.c
> +++ b/drivers/platform/x86/asus-wmi.c
> @@ -3910,6 +3910,7 @@ static int platform_profile_setup(struct asus_wmi *asus)
> 
>  	dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
> 
> +	asus->platform_profile_handler.name = "asus-wmi";
>  	asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
>  	asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
> 
> diff --git a/drivers/platform/x86/dell/dell-pc.c 
> b/drivers/platform/x86/dell/dell-pc.c
> index 972385ca1990b..3cf79e55e3129 100644
> --- a/drivers/platform/x86/dell/dell-pc.c
> +++ b/drivers/platform/x86/dell/dell-pc.c
> @@ -247,6 +247,7 @@ static int thermal_init(void)
>  	thermal_handler = kzalloc(sizeof(*thermal_handler), GFP_KERNEL);
>  	if (!thermal_handler)
>  		return -ENOMEM;
> +	thermal_handler->name = "dell-pc";
>  	thermal_handler->profile_get = thermal_platform_profile_get;
>  	thermal_handler->profile_set = thermal_platform_profile_set;
> 
> diff --git a/drivers/platform/x86/hp/hp-wmi.c b/drivers/platform/x86/hp/hp-wmi.c
> index 81ccc96ffe40a..26cac73caf2b9 100644
> --- a/drivers/platform/x86/hp/hp-wmi.c
> +++ b/drivers/platform/x86/hp/hp-wmi.c
> @@ -1624,6 +1624,7 @@ static int thermal_profile_setup(void)
>  		set_bit(PLATFORM_PROFILE_COOL, platform_profile_handler.choices);
>  	}
> 
> +	platform_profile_handler.name = "hp-wmi";
>  	set_bit(PLATFORM_PROFILE_BALANCED, platform_profile_handler.choices);
>  	set_bit(PLATFORM_PROFILE_PERFORMANCE, platform_profile_handler.choices);
> 
> diff --git a/drivers/platform/x86/ideapad-laptop.c 
> b/drivers/platform/x86/ideapad-laptop.c
> index 9d8c3f064050e..1f94c14c3b832 100644
> --- a/drivers/platform/x86/ideapad-laptop.c
> +++ b/drivers/platform/x86/ideapad-laptop.c
> @@ -1102,6 +1102,7 @@ static int ideapad_dytc_profile_init(struct 
> ideapad_private *priv)
> 
>  	mutex_init(&priv->dytc->mutex);
> 
> +	priv->dytc->pprof.name = "ideapad-laptop";
>  	priv->dytc->priv = priv;
>  	priv->dytc->pprof.profile_get = dytc_profile_get;
>  	priv->dytc->pprof.profile_set = dytc_profile_set;
> diff --git a/drivers/platform/x86/inspur_platform_profile.c 
> b/drivers/platform/x86/inspur_platform_profile.c
> index 8440defa67886..03da2c8cf6789 100644
> --- a/drivers/platform/x86/inspur_platform_profile.c
> +++ b/drivers/platform/x86/inspur_platform_profile.c
> @@ -177,6 +177,7 @@ static int inspur_wmi_probe(struct wmi_device 
> *wdev, const void *context)
>  	priv->wdev = wdev;
>  	dev_set_drvdata(&wdev->dev, priv);
> 
> +	priv->handler.name = "inspur-wmi";
>  	priv->handler.profile_get = inspur_platform_profile_get;
>  	priv->handler.profile_set = inspur_platform_profile_set;
> 
> diff --git a/drivers/platform/x86/thinkpad_acpi.c 
> b/drivers/platform/x86/thinkpad_acpi.c
> index 4c1b0553f8720..c8c316b8507a5 100644
> --- a/drivers/platform/x86/thinkpad_acpi.c
> +++ b/drivers/platform/x86/thinkpad_acpi.c
> @@ -10549,6 +10549,7 @@ static void dytc_profile_refresh(void)
>  }
> 
>  static struct platform_profile_handler dytc_profile = {
> +	.name = "thinkpad-acpi",
>  	.profile_get = dytc_profile_get,
>  	.profile_set = dytc_profile_set,
>  };
> diff --git a/include/linux/platform_profile.h b/include/linux/platform_profile.h
> index f5492ed413f36..6fa988e417428 100644
> --- a/include/linux/platform_profile.h
> +++ b/include/linux/platform_profile.h
> @@ -27,6 +27,7 @@ enum platform_profile_option {
>  };
> 
>  struct platform_profile_handler {
> +	const char *name;
>  	unsigned long choices[BITS_TO_LONGS(PLATFORM_PROFILE_LAST)];
>  	int (*profile_get)(struct platform_profile_handler *pprof,
>  				enum platform_profile_option *profile);
> -- 
> 2.43.0

Looks good to me.
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>

Mark
Mark Pearson Oct. 28, 2024, 11:01 a.m. UTC | #8
Hi Mario,

On Fri, Oct 25, 2024, at 3:30 PM, Mario Limonciello wrote:
> Multiple drivers may attempt to register platform profile handlers,
> but only one may be registered and the behavior is non-deterministic
> for which one wins.  It's mostly controlled by probing order.
>
> This can be problematic if one driver changes CPU settings and another
> driver notifies the EC for changing fan curves.
>
> Modify the ACPI platform profile handler to let multiple drivers
> register platform profile handlers and abstract this detail from userspace.
>
> From userspace perspective the user will see profiles available across
> both drivers.  However to avoid chaos only allow changing to profiles
> that are common in both drivers.
>
> If any problems occur when changing profiles for any driver, then revert
> back to the previous profile.
>
> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  drivers/acpi/platform_profile.c | 203 ++++++++++++++++++--------------
>  1 file changed, 117 insertions(+), 86 deletions(-)
>
> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
> index 091ca6941a925..915e3c49f0b5f 100644
> --- a/drivers/acpi/platform_profile.c
> +++ b/drivers/acpi/platform_profile.c
> @@ -9,7 +9,6 @@
>  #include <linux/platform_profile.h>
>  #include <linux/sysfs.h>
> 
> -static struct platform_profile_handler *cur_profile;
>  static LIST_HEAD(platform_profile_handler_list);
>  static DEFINE_MUTEX(profile_lock);
> 
> @@ -36,26 +35,26 @@ static ssize_t platform_profile_choices_show(struct 
> device *dev,
>  					struct device_attribute *attr,
>  					char *buf)
>  {
> +	struct platform_profile_handler *handler;
> +	unsigned long seen = 0;
>  	int len = 0;
> -	int err, i;
> -
> -	err = mutex_lock_interruptible(&profile_lock);
> -	if (err)
> -		return err;
> -
> -	if (!cur_profile) {
> -		mutex_unlock(&profile_lock);
> -		return -ENODEV;
> +	int i;
> +
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			for_each_set_bit(i, handler->choices, PLATFORM_PROFILE_LAST) {
> +				if (seen & BIT(i))
> +					continue;
> +				if (len == 0)
> +					len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
> +				else
> +					len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
> +				seen |= BIT(i);
> +			}
> +		}
>  	}
> 
> -	for_each_set_bit(i, cur_profile->choices, PLATFORM_PROFILE_LAST) {
> -		if (len == 0)
> -			len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
> -		else
> -			len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
> -	}
>  	len += sysfs_emit_at(buf, len, "\n");
> -	mutex_unlock(&profile_lock);
>  	return len;
>  }
> 
> @@ -64,22 +63,20 @@ static ssize_t platform_profile_show(struct device *dev,
>  					char *buf)
>  {
>  	enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
> +	struct platform_profile_handler *handler;
>  	int err;
> 
> -	err = mutex_lock_interruptible(&profile_lock);
> -	if (err)
> -		return err;
> 
> -	if (!cur_profile) {
> -		mutex_unlock(&profile_lock);
> -		return -ENODEV;
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> +		if (!platform_profile_is_registered())
> +			return -ENODEV;
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			err = handler->profile_get(handler, &profile);
> +			if (err)
> +				return err;
> +		}
>  	}
> 
> -	err = cur_profile->profile_get(cur_profile, &profile);
> -	mutex_unlock(&profile_lock);
> -	if (err)
> -		return err;
> -
>  	/* Check that profile is valid index */
>  	if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
>  		return -EIO;
> @@ -91,37 +88,48 @@ static ssize_t platform_profile_store(struct device *dev,
>  			    struct device_attribute *attr,
>  			    const char *buf, size_t count)
>  {
> +	struct platform_profile_handler *handler;
> +	enum platform_profile_option profile;
>  	int err, i;
> 
> -	err = mutex_lock_interruptible(&profile_lock);
> -	if (err)
> -		return err;
> -
> -	if (!cur_profile) {
> -		mutex_unlock(&profile_lock);
> -		return -ENODEV;
> -	}
> -
>  	/* Scan for a matching profile */
>  	i = sysfs_match_string(profile_names, buf);
>  	if (i < 0) {
> -		mutex_unlock(&profile_lock);
>  		return -EINVAL;
>  	}
> 
> -	/* Check that platform supports this profile choice */
> -	if (!test_bit(i, cur_profile->choices)) {
> -		mutex_unlock(&profile_lock);
> -		return -EOPNOTSUPP;
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> +		if (!platform_profile_is_registered())
> +			return -ENODEV;
> +
> +		/* Check that all handlers support this profile choice */
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			if (!test_bit(i, handler->choices))
> +				return -EOPNOTSUPP;
> +
> +			/* save the profile so that it can be reverted if necessary */
> +			err = handler->profile_get(handler, &profile);
> +			if (err)
> +				return err;
> +		}
> +
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			err = handler->profile_set(handler, i);
> +			if (err) {
> +				pr_err("Failed to set profile for handler %s\n", handler->name);
> +				break;
> +			}
> +		}
> +		if (err) {
> +			list_for_each_entry_continue_reverse(handler, 
> &platform_profile_handler_list, list) {
> +				if (handler->profile_set(handler, profile))
> +					pr_err("Failed to revert profile for handler %s\n", 
> handler->name);
> +			}
> +			return err;
> +		}
>  	}
> 
> -	err = cur_profile->profile_set(cur_profile, i);
> -	if (!err)
> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
> -
> -	mutex_unlock(&profile_lock);
> -	if (err)
> -		return err;
> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>  	return count;
>  }
> 
> @@ -140,7 +148,8 @@ static const struct attribute_group 
> platform_profile_group = {
> 
>  void platform_profile_notify(void)
>  {
> -	if (!cur_profile)
> +	guard(mutex)(&profile_lock);
> +	if (!platform_profile_is_registered())
>  		return;
>  	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>  }
> @@ -148,40 +157,65 @@ EXPORT_SYMBOL_GPL(platform_profile_notify);
> 
>  int platform_profile_cycle(void)
>  {
> +	struct platform_profile_handler *handler;
>  	enum platform_profile_option profile;
> -	enum platform_profile_option next;
> +	enum platform_profile_option next = PLATFORM_PROFILE_LAST;
> +	enum platform_profile_option next2 = PLATFORM_PROFILE_LAST;
>  	int err;
> 
> -	err = mutex_lock_interruptible(&profile_lock);
> -	if (err)
> -		return err;
> -
> -	if (!cur_profile) {
> -		mutex_unlock(&profile_lock);
> -		return -ENODEV;
> -	}
> -
> -	err = cur_profile->profile_get(cur_profile, &profile);
> -	if (err) {
> -		mutex_unlock(&profile_lock);
> -		return err;
> -	}
> -
> -	next = find_next_bit_wrap(cur_profile->choices, PLATFORM_PROFILE_LAST,
> -				  profile + 1);
> -
> -	if (WARN_ON(next == PLATFORM_PROFILE_LAST)) {
> -		mutex_unlock(&profile_lock);
> -		return -EINVAL;
> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
> +		/* first pass, make sure all handlers agree on the definition of 
> "next" profile */
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +
> +			err = handler->profile_get(handler, &profile);
> +			if (err)
> +				return err;
> +
> +			if (next == PLATFORM_PROFILE_LAST)
> +				next = find_next_bit_wrap(handler->choices,
> +							  PLATFORM_PROFILE_LAST,
> +							  profile + 1);
> +			else
> +				next2 = find_next_bit_wrap(handler->choices,
> +							   PLATFORM_PROFILE_LAST,
> +							   profile + 1);
> +
> +			if (WARN_ON(next == PLATFORM_PROFILE_LAST))
> +				return -EINVAL;
> +
> +			if (next2 == PLATFORM_PROFILE_LAST)
> +				continue;
> +
> +			if (next != next2) {
> +				pr_warn("Next profile to cycle to is ambiguous between 
> platform_profile handlers\n");
> +				return -EINVAL;
> +			}
> +			next = next2;
> +		}
> +
> +		/*
> +		 * Second pass: apply "next" to each handler
> +		 * If any failures occur unwind and revert all back to the original 
> profile
> +		 */
> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
> +			err = handler->profile_set(handler, next);
> +			if (err) {
> +				pr_err("Failed to set profile for handler %s\n", handler->name);
> +				break;
> +			}
> +		}
> +		if (err) {
> +			list_for_each_entry_continue_reverse(handler, 
> &platform_profile_handler_list, list) {
> +				err = handler->profile_set(handler, profile);
> +				if (err)
> +					pr_err("Failed to revert profile for handler %s\n", 
> handler->name);
> +			}
> +		}
>  	}
> 
> -	err = cur_profile->profile_set(cur_profile, next);
> -	mutex_unlock(&profile_lock);
> -
> -	if (!err)
> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
> 
> -	return err;
> +	return 0;
>  }
>  EXPORT_SYMBOL_GPL(platform_profile_cycle);
> 
> @@ -190,21 +224,19 @@ int platform_profile_register(struct 
> platform_profile_handler *pprof)
>  	int err;
> 
>  	guard(mutex)(&profile_lock);
> -	/* We can only have one active profile */
> -	if (cur_profile)
> -		return -EEXIST;
> 
>  	/* Sanity check the profile handler field are set */
>  	if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
>  		!pprof->profile_set || !pprof->profile_get)
>  		return -EINVAL;
> 
> -	err = sysfs_create_group(acpi_kobj, &platform_profile_group);
> -	if (err)
> -		return err;
> +	if (!platform_profile_is_registered()) {
> +		err = sysfs_create_group(acpi_kobj, &platform_profile_group);
> +		if (err)
> +			return err;
> +	}
>  	list_add_tail(&pprof->list, &platform_profile_handler_list);
> 
> -	cur_profile = pprof;
>  	return 0;
>  }
>  EXPORT_SYMBOL_GPL(platform_profile_register);
> @@ -215,7 +247,6 @@ int platform_profile_remove(struct 
> platform_profile_handler *pprof)
> 
>  	list_del(&pprof->list);
> 
> -	cur_profile = NULL;
>  	if (!platform_profile_is_registered())
>  		sysfs_remove_group(acpi_kobj, &platform_profile_group);
> 
> -- 
> 2.43.0

I'm still going thru the code changes - but I'm a bit unsure on the implementation itself.

I'd expect that one of the advantages of having different profile handlers register is that you could support extra & new profiles that might be wanted. For example the recent discussion of the AMD handler providing better tools to tweak advanced system settings for gaming etc. Won't this approach limit that? You'll only be able to have common settings.

I find having a common profile and two different handlers a bit tricky on how to handle. My concern is it can easily lead to conflict in settings. 
If two handlers are doing different operations to provide the same effect - then neither handler is (probably) providing what they think is required. With your CPU vs EC example, the EC will often set CPU clock thresholds and the CPU profile handler will be changing that. If this is done I think it should be explicit to the user (admittedly I'm doing this with my Lenovo hat on - but we certify our platforms with our EC profile handler)

I could see providing two separate handlers. e.g. balanced-A and balanced-B (for driver-A and driver-B) and the user maybe choosing which one they want (or both - though the user interface for that is definitely tricky) 
But choosing one option for two different drivers seems confusing and with unknown side-effects. I appreciate it's complicated by your example wanting to add CPU and EC - I know how much work you've been doing on the AMD CPU front which benefits all systems.

Another concern - would this mean that another driver could limit the options available? For instance if someone wrote a new 'mega-turbo' only profile driver and it loaded - it would then mean no profiles were available for anything as no profiles matched?

Let me know if I've misunderstood the architecture. I didn't fully get how the ASUS and Framework platforms were impacted in the intro I'm afraid.

Thanks!
Mark
Mario Limonciello Oct. 28, 2024, 2:10 p.m. UTC | #9
On 10/28/2024 06:01, Mark Pearson wrote:
> Hi Mario,
> 
> On Fri, Oct 25, 2024, at 3:30 PM, Mario Limonciello wrote:
>> Multiple drivers may attempt to register platform profile handlers,
>> but only one may be registered and the behavior is non-deterministic
>> for which one wins.  It's mostly controlled by probing order.
>>
>> This can be problematic if one driver changes CPU settings and another
>> driver notifies the EC for changing fan curves.
>>
>> Modify the ACPI platform profile handler to let multiple drivers
>> register platform profile handlers and abstract this detail from userspace.
>>
>>  From userspace perspective the user will see profiles available across
>> both drivers.  However to avoid chaos only allow changing to profiles
>> that are common in both drivers.
>>
>> If any problems occur when changing profiles for any driver, then revert
>> back to the previous profile.
>>
>> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>> ---
>>   drivers/acpi/platform_profile.c | 203 ++++++++++++++++++--------------
>>   1 file changed, 117 insertions(+), 86 deletions(-)
>>
>> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
>> index 091ca6941a925..915e3c49f0b5f 100644
>> --- a/drivers/acpi/platform_profile.c
>> +++ b/drivers/acpi/platform_profile.c
>> @@ -9,7 +9,6 @@
>>   #include <linux/platform_profile.h>
>>   #include <linux/sysfs.h>
>>
>> -static struct platform_profile_handler *cur_profile;
>>   static LIST_HEAD(platform_profile_handler_list);
>>   static DEFINE_MUTEX(profile_lock);
>>
>> @@ -36,26 +35,26 @@ static ssize_t platform_profile_choices_show(struct
>> device *dev,
>>   					struct device_attribute *attr,
>>   					char *buf)
>>   {
>> +	struct platform_profile_handler *handler;
>> +	unsigned long seen = 0;
>>   	int len = 0;
>> -	int err, i;
>> -
>> -	err = mutex_lock_interruptible(&profile_lock);
>> -	if (err)
>> -		return err;
>> -
>> -	if (!cur_profile) {
>> -		mutex_unlock(&profile_lock);
>> -		return -ENODEV;
>> +	int i;
>> +
>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			for_each_set_bit(i, handler->choices, PLATFORM_PROFILE_LAST) {
>> +				if (seen & BIT(i))
>> +					continue;
>> +				if (len == 0)
>> +					len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>> +				else
>> +					len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>> +				seen |= BIT(i);
>> +			}
>> +		}
>>   	}
>>
>> -	for_each_set_bit(i, cur_profile->choices, PLATFORM_PROFILE_LAST) {
>> -		if (len == 0)
>> -			len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>> -		else
>> -			len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>> -	}
>>   	len += sysfs_emit_at(buf, len, "\n");
>> -	mutex_unlock(&profile_lock);
>>   	return len;
>>   }
>>
>> @@ -64,22 +63,20 @@ static ssize_t platform_profile_show(struct device *dev,
>>   					char *buf)
>>   {
>>   	enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
>> +	struct platform_profile_handler *handler;
>>   	int err;
>>
>> -	err = mutex_lock_interruptible(&profile_lock);
>> -	if (err)
>> -		return err;
>>
>> -	if (!cur_profile) {
>> -		mutex_unlock(&profile_lock);
>> -		return -ENODEV;
>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>> +		if (!platform_profile_is_registered())
>> +			return -ENODEV;
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			err = handler->profile_get(handler, &profile);
>> +			if (err)
>> +				return err;
>> +		}
>>   	}
>>
>> -	err = cur_profile->profile_get(cur_profile, &profile);
>> -	mutex_unlock(&profile_lock);
>> -	if (err)
>> -		return err;
>> -
>>   	/* Check that profile is valid index */
>>   	if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
>>   		return -EIO;
>> @@ -91,37 +88,48 @@ static ssize_t platform_profile_store(struct device *dev,
>>   			    struct device_attribute *attr,
>>   			    const char *buf, size_t count)
>>   {
>> +	struct platform_profile_handler *handler;
>> +	enum platform_profile_option profile;
>>   	int err, i;
>>
>> -	err = mutex_lock_interruptible(&profile_lock);
>> -	if (err)
>> -		return err;
>> -
>> -	if (!cur_profile) {
>> -		mutex_unlock(&profile_lock);
>> -		return -ENODEV;
>> -	}
>> -
>>   	/* Scan for a matching profile */
>>   	i = sysfs_match_string(profile_names, buf);
>>   	if (i < 0) {
>> -		mutex_unlock(&profile_lock);
>>   		return -EINVAL;
>>   	}
>>
>> -	/* Check that platform supports this profile choice */
>> -	if (!test_bit(i, cur_profile->choices)) {
>> -		mutex_unlock(&profile_lock);
>> -		return -EOPNOTSUPP;
>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>> +		if (!platform_profile_is_registered())
>> +			return -ENODEV;
>> +
>> +		/* Check that all handlers support this profile choice */
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			if (!test_bit(i, handler->choices))
>> +				return -EOPNOTSUPP;
>> +
>> +			/* save the profile so that it can be reverted if necessary */
>> +			err = handler->profile_get(handler, &profile);
>> +			if (err)
>> +				return err;
>> +		}
>> +
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			err = handler->profile_set(handler, i);
>> +			if (err) {
>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>> +				break;
>> +			}
>> +		}
>> +		if (err) {
>> +			list_for_each_entry_continue_reverse(handler,
>> &platform_profile_handler_list, list) {
>> +				if (handler->profile_set(handler, profile))
>> +					pr_err("Failed to revert profile for handler %s\n",
>> handler->name);
>> +			}
>> +			return err;
>> +		}
>>   	}
>>
>> -	err = cur_profile->profile_set(cur_profile, i);
>> -	if (!err)
>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>> -
>> -	mutex_unlock(&profile_lock);
>> -	if (err)
>> -		return err;
>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>   	return count;
>>   }
>>
>> @@ -140,7 +148,8 @@ static const struct attribute_group
>> platform_profile_group = {
>>
>>   void platform_profile_notify(void)
>>   {
>> -	if (!cur_profile)
>> +	guard(mutex)(&profile_lock);
>> +	if (!platform_profile_is_registered())
>>   		return;
>>   	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>   }
>> @@ -148,40 +157,65 @@ EXPORT_SYMBOL_GPL(platform_profile_notify);
>>
>>   int platform_profile_cycle(void)
>>   {
>> +	struct platform_profile_handler *handler;
>>   	enum platform_profile_option profile;
>> -	enum platform_profile_option next;
>> +	enum platform_profile_option next = PLATFORM_PROFILE_LAST;
>> +	enum platform_profile_option next2 = PLATFORM_PROFILE_LAST;
>>   	int err;
>>
>> -	err = mutex_lock_interruptible(&profile_lock);
>> -	if (err)
>> -		return err;
>> -
>> -	if (!cur_profile) {
>> -		mutex_unlock(&profile_lock);
>> -		return -ENODEV;
>> -	}
>> -
>> -	err = cur_profile->profile_get(cur_profile, &profile);
>> -	if (err) {
>> -		mutex_unlock(&profile_lock);
>> -		return err;
>> -	}
>> -
>> -	next = find_next_bit_wrap(cur_profile->choices, PLATFORM_PROFILE_LAST,
>> -				  profile + 1);
>> -
>> -	if (WARN_ON(next == PLATFORM_PROFILE_LAST)) {
>> -		mutex_unlock(&profile_lock);
>> -		return -EINVAL;
>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>> +		/* first pass, make sure all handlers agree on the definition of
>> "next" profile */
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +
>> +			err = handler->profile_get(handler, &profile);
>> +			if (err)
>> +				return err;
>> +
>> +			if (next == PLATFORM_PROFILE_LAST)
>> +				next = find_next_bit_wrap(handler->choices,
>> +							  PLATFORM_PROFILE_LAST,
>> +							  profile + 1);
>> +			else
>> +				next2 = find_next_bit_wrap(handler->choices,
>> +							   PLATFORM_PROFILE_LAST,
>> +							   profile + 1);
>> +
>> +			if (WARN_ON(next == PLATFORM_PROFILE_LAST))
>> +				return -EINVAL;
>> +
>> +			if (next2 == PLATFORM_PROFILE_LAST)
>> +				continue;
>> +
>> +			if (next != next2) {
>> +				pr_warn("Next profile to cycle to is ambiguous between
>> platform_profile handlers\n");
>> +				return -EINVAL;
>> +			}
>> +			next = next2;
>> +		}
>> +
>> +		/*
>> +		 * Second pass: apply "next" to each handler
>> +		 * If any failures occur unwind and revert all back to the original
>> profile
>> +		 */
>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>> +			err = handler->profile_set(handler, next);
>> +			if (err) {
>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>> +				break;
>> +			}
>> +		}
>> +		if (err) {
>> +			list_for_each_entry_continue_reverse(handler,
>> &platform_profile_handler_list, list) {
>> +				err = handler->profile_set(handler, profile);
>> +				if (err)
>> +					pr_err("Failed to revert profile for handler %s\n",
>> handler->name);
>> +			}
>> +		}
>>   	}
>>
>> -	err = cur_profile->profile_set(cur_profile, next);
>> -	mutex_unlock(&profile_lock);
>> -
>> -	if (!err)
>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>
>> -	return err;
>> +	return 0;
>>   }
>>   EXPORT_SYMBOL_GPL(platform_profile_cycle);
>>
>> @@ -190,21 +224,19 @@ int platform_profile_register(struct
>> platform_profile_handler *pprof)
>>   	int err;
>>
>>   	guard(mutex)(&profile_lock);
>> -	/* We can only have one active profile */
>> -	if (cur_profile)
>> -		return -EEXIST;
>>
>>   	/* Sanity check the profile handler field are set */
>>   	if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
>>   		!pprof->profile_set || !pprof->profile_get)
>>   		return -EINVAL;
>>
>> -	err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>> -	if (err)
>> -		return err;
>> +	if (!platform_profile_is_registered()) {
>> +		err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>> +		if (err)
>> +			return err;
>> +	}
>>   	list_add_tail(&pprof->list, &platform_profile_handler_list);
>>
>> -	cur_profile = pprof;
>>   	return 0;
>>   }
>>   EXPORT_SYMBOL_GPL(platform_profile_register);
>> @@ -215,7 +247,6 @@ int platform_profile_remove(struct
>> platform_profile_handler *pprof)
>>
>>   	list_del(&pprof->list);
>>
>> -	cur_profile = NULL;
>>   	if (!platform_profile_is_registered())
>>   		sysfs_remove_group(acpi_kobj, &platform_profile_group);
>>
>> -- 
>> 2.43.0
> 
> I'm still going thru the code changes - but I'm a bit unsure on the implementation itself.

FYI, I split it up in v2 to make each chunk and intent behind it more 
manageable to review instead of patch 7 being "so" big.

V2 covers some of the points below as well based on some feedback from 
Hans and Armin.

> 
> I'd expect that one of the advantages of having different profile handlers register is that you could support extra & new profiles that might be wanted. For example the recent discussion of the AMD handler providing better tools to tweak advanced system settings for gaming etc. Won't this approach limit that? You'll only be able to have common settings.

Well that RFC it turns out won't really be scalable because SPS is done 
differently in AMD Strix and newer.  I haven't revisited it yet.

But yes this approach would conceptually limit that idea because common 
settings are all that is presented.

> 
> I find having a common profile and two different handlers a bit tricky on how to handle. My concern is it can easily lead to conflict in settings.
> If two handlers are doing different operations to provide the same effect - then neither handler is (probably) providing what they think is required. With your CPU vs EC example, the EC will often set CPU clock thresholds and the CPU profile handler will be changing that. If this is done I think it should be explicit to the user (admittedly I'm doing this with my Lenovo hat on - but we certify our platforms with our EC profile handler)
> 
> I could see providing two separate handlers. e.g. balanced-A and balanced-B (for driver-A and driver-B) and the user maybe choosing which one they want (or both - though the user interface for that is definitely tricky)
> But choosing one option for two different drivers seems confusing and with unknown side-effects. I appreciate it's complicated by your example wanting to add CPU and EC - I know how much work you've been doing on the AMD CPU front which benefits all systems.
> 

Thinking through your comments I guess another way to approach this 
would be "per-driver" sysfs knobs.  Here's my thought.

1) /sys/firmware/acpi/platform_profile_choices would contain only things 
that are common and if there is something NOT common then also the 
string "custom".

2) /sys/firmware/acpi/platform_profile would accept writes for 
everything in platform profile choices except "custom".

3) Each driver handler would also export it's own sysfs files to 
represent the driver state.

3) If the user changed the main knob at 
/sys/firmware/acpi/platform_profile then it would change all driver 
handlers.

4) If the user changed sysfs for any driver individually then the main 
knob /sys/firmware/acpi/platform_profile would export "custom".

Hans what do you think?

> Another concern - would this mean that another driver could limit the options available? For instance if someone wrote a new 'mega-turbo' only profile driver and it loaded - it would then mean no profiles were available for anything as no profiles matched?

Yes.  I don't think it's a problem in practice right now (as we only 
just recently have two drivers vying for this position), but it /could/ 
be something that happens.

> 
> Let me know if I've misunderstood the architecture. I didn't fully get how the ASUS and Framework platforms were impacted in the intro I'm afraid.
> 
> Thanks!
> Mark

Framework isn't affected, it was just showing that there are platforms 
that use the BIOS/EC notification concept and not just SPS values that 
the driver programs so it can't "go away" to solve this issue.

ASUS is the only thing affected right now.
Armin Wolf Oct. 28, 2024, 4:51 p.m. UTC | #10
Am 28.10.24 um 15:10 schrieb Mario Limonciello:

> On 10/28/2024 06:01, Mark Pearson wrote:
>> Hi Mario,
>>
>> On Fri, Oct 25, 2024, at 3:30 PM, Mario Limonciello wrote:
>>> Multiple drivers may attempt to register platform profile handlers,
>>> but only one may be registered and the behavior is non-deterministic
>>> for which one wins.  It's mostly controlled by probing order.
>>>
>>> This can be problematic if one driver changes CPU settings and another
>>> driver notifies the EC for changing fan curves.
>>>
>>> Modify the ACPI platform profile handler to let multiple drivers
>>> register platform profile handlers and abstract this detail from 
>>> userspace.
>>>
>>>  From userspace perspective the user will see profiles available across
>>> both drivers.  However to avoid chaos only allow changing to profiles
>>> that are common in both drivers.
>>>
>>> If any problems occur when changing profiles for any driver, then 
>>> revert
>>> back to the previous profile.
>>>
>>> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
>>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>>> ---
>>>   drivers/acpi/platform_profile.c | 203 
>>> ++++++++++++++++++--------------
>>>   1 file changed, 117 insertions(+), 86 deletions(-)
>>>
>>> diff --git a/drivers/acpi/platform_profile.c 
>>> b/drivers/acpi/platform_profile.c
>>> index 091ca6941a925..915e3c49f0b5f 100644
>>> --- a/drivers/acpi/platform_profile.c
>>> +++ b/drivers/acpi/platform_profile.c
>>> @@ -9,7 +9,6 @@
>>>   #include <linux/platform_profile.h>
>>>   #include <linux/sysfs.h>
>>>
>>> -static struct platform_profile_handler *cur_profile;
>>>   static LIST_HEAD(platform_profile_handler_list);
>>>   static DEFINE_MUTEX(profile_lock);
>>>
>>> @@ -36,26 +35,26 @@ static ssize_t platform_profile_choices_show(struct
>>> device *dev,
>>>                       struct device_attribute *attr,
>>>                       char *buf)
>>>   {
>>> +    struct platform_profile_handler *handler;
>>> +    unsigned long seen = 0;
>>>       int len = 0;
>>> -    int err, i;
>>> -
>>> -    err = mutex_lock_interruptible(&profile_lock);
>>> -    if (err)
>>> -        return err;
>>> -
>>> -    if (!cur_profile) {
>>> -        mutex_unlock(&profile_lock);
>>> -        return -ENODEV;
>>> +    int i;
>>> +
>>> +    scoped_cond_guard(mutex_intr, return -ERESTARTSYS, 
>>> &profile_lock) {
>>> +        list_for_each_entry(handler, 
>>> &platform_profile_handler_list, list) {
>>> +            for_each_set_bit(i, handler->choices, 
>>> PLATFORM_PROFILE_LAST) {
>>> +                if (seen & BIT(i))
>>> +                    continue;
>>> +                if (len == 0)
>>> +                    len += sysfs_emit_at(buf, len, "%s", 
>>> profile_names[i]);
>>> +                else
>>> +                    len += sysfs_emit_at(buf, len, " %s", 
>>> profile_names[i]);
>>> +                seen |= BIT(i);
>>> +            }
>>> +        }
>>>       }
>>>
>>> -    for_each_set_bit(i, cur_profile->choices, PLATFORM_PROFILE_LAST) {
>>> -        if (len == 0)
>>> -            len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>>> -        else
>>> -            len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>>> -    }
>>>       len += sysfs_emit_at(buf, len, "\n");
>>> -    mutex_unlock(&profile_lock);
>>>       return len;
>>>   }
>>>
>>> @@ -64,22 +63,20 @@ static ssize_t platform_profile_show(struct 
>>> device *dev,
>>>                       char *buf)
>>>   {
>>>       enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
>>> +    struct platform_profile_handler *handler;
>>>       int err;
>>>
>>> -    err = mutex_lock_interruptible(&profile_lock);
>>> -    if (err)
>>> -        return err;
>>>
>>> -    if (!cur_profile) {
>>> -        mutex_unlock(&profile_lock);
>>> -        return -ENODEV;
>>> +    scoped_cond_guard(mutex_intr, return -ERESTARTSYS, 
>>> &profile_lock) {
>>> +        if (!platform_profile_is_registered())
>>> +            return -ENODEV;
>>> +        list_for_each_entry(handler, 
>>> &platform_profile_handler_list, list) {
>>> +            err = handler->profile_get(handler, &profile);
>>> +            if (err)
>>> +                return err;
>>> +        }
>>>       }
>>>
>>> -    err = cur_profile->profile_get(cur_profile, &profile);
>>> -    mutex_unlock(&profile_lock);
>>> -    if (err)
>>> -        return err;
>>> -
>>>       /* Check that profile is valid index */
>>>       if (WARN_ON((profile < 0) || (profile >= 
>>> ARRAY_SIZE(profile_names))))
>>>           return -EIO;
>>> @@ -91,37 +88,48 @@ static ssize_t platform_profile_store(struct 
>>> device *dev,
>>>                   struct device_attribute *attr,
>>>                   const char *buf, size_t count)
>>>   {
>>> +    struct platform_profile_handler *handler;
>>> +    enum platform_profile_option profile;
>>>       int err, i;
>>>
>>> -    err = mutex_lock_interruptible(&profile_lock);
>>> -    if (err)
>>> -        return err;
>>> -
>>> -    if (!cur_profile) {
>>> -        mutex_unlock(&profile_lock);
>>> -        return -ENODEV;
>>> -    }
>>> -
>>>       /* Scan for a matching profile */
>>>       i = sysfs_match_string(profile_names, buf);
>>>       if (i < 0) {
>>> -        mutex_unlock(&profile_lock);
>>>           return -EINVAL;
>>>       }
>>>
>>> -    /* Check that platform supports this profile choice */
>>> -    if (!test_bit(i, cur_profile->choices)) {
>>> -        mutex_unlock(&profile_lock);
>>> -        return -EOPNOTSUPP;
>>> +    scoped_cond_guard(mutex_intr, return -ERESTARTSYS, 
>>> &profile_lock) {
>>> +        if (!platform_profile_is_registered())
>>> +            return -ENODEV;
>>> +
>>> +        /* Check that all handlers support this profile choice */
>>> +        list_for_each_entry(handler, 
>>> &platform_profile_handler_list, list) {
>>> +            if (!test_bit(i, handler->choices))
>>> +                return -EOPNOTSUPP;
>>> +
>>> +            /* save the profile so that it can be reverted if 
>>> necessary */
>>> +            err = handler->profile_get(handler, &profile);
>>> +            if (err)
>>> +                return err;
>>> +        }
>>> +
>>> +        list_for_each_entry(handler, 
>>> &platform_profile_handler_list, list) {
>>> +            err = handler->profile_set(handler, i);
>>> +            if (err) {
>>> +                pr_err("Failed to set profile for handler %s\n", 
>>> handler->name);
>>> +                break;
>>> +            }
>>> +        }
>>> +        if (err) {
>>> +            list_for_each_entry_continue_reverse(handler,
>>> &platform_profile_handler_list, list) {
>>> +                if (handler->profile_set(handler, profile))
>>> +                    pr_err("Failed to revert profile for handler 
>>> %s\n",
>>> handler->name);
>>> +            }
>>> +            return err;
>>> +        }
>>>       }
>>>
>>> -    err = cur_profile->profile_set(cur_profile, i);
>>> -    if (!err)
>>> -        sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>> -
>>> -    mutex_unlock(&profile_lock);
>>> -    if (err)
>>> -        return err;
>>> +    sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>       return count;
>>>   }
>>>
>>> @@ -140,7 +148,8 @@ static const struct attribute_group
>>> platform_profile_group = {
>>>
>>>   void platform_profile_notify(void)
>>>   {
>>> -    if (!cur_profile)
>>> +    guard(mutex)(&profile_lock);
>>> +    if (!platform_profile_is_registered())
>>>           return;
>>>       sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>   }
>>> @@ -148,40 +157,65 @@ EXPORT_SYMBOL_GPL(platform_profile_notify);
>>>
>>>   int platform_profile_cycle(void)
>>>   {
>>> +    struct platform_profile_handler *handler;
>>>       enum platform_profile_option profile;
>>> -    enum platform_profile_option next;
>>> +    enum platform_profile_option next = PLATFORM_PROFILE_LAST;
>>> +    enum platform_profile_option next2 = PLATFORM_PROFILE_LAST;
>>>       int err;
>>>
>>> -    err = mutex_lock_interruptible(&profile_lock);
>>> -    if (err)
>>> -        return err;
>>> -
>>> -    if (!cur_profile) {
>>> -        mutex_unlock(&profile_lock);
>>> -        return -ENODEV;
>>> -    }
>>> -
>>> -    err = cur_profile->profile_get(cur_profile, &profile);
>>> -    if (err) {
>>> -        mutex_unlock(&profile_lock);
>>> -        return err;
>>> -    }
>>> -
>>> -    next = find_next_bit_wrap(cur_profile->choices, 
>>> PLATFORM_PROFILE_LAST,
>>> -                  profile + 1);
>>> -
>>> -    if (WARN_ON(next == PLATFORM_PROFILE_LAST)) {
>>> -        mutex_unlock(&profile_lock);
>>> -        return -EINVAL;
>>> +    scoped_cond_guard(mutex_intr, return -ERESTARTSYS, 
>>> &profile_lock) {
>>> +        /* first pass, make sure all handlers agree on the 
>>> definition of
>>> "next" profile */
>>> +        list_for_each_entry(handler, 
>>> &platform_profile_handler_list, list) {
>>> +
>>> +            err = handler->profile_get(handler, &profile);
>>> +            if (err)
>>> +                return err;
>>> +
>>> +            if (next == PLATFORM_PROFILE_LAST)
>>> +                next = find_next_bit_wrap(handler->choices,
>>> +                              PLATFORM_PROFILE_LAST,
>>> +                              profile + 1);
>>> +            else
>>> +                next2 = find_next_bit_wrap(handler->choices,
>>> +                               PLATFORM_PROFILE_LAST,
>>> +                               profile + 1);
>>> +
>>> +            if (WARN_ON(next == PLATFORM_PROFILE_LAST))
>>> +                return -EINVAL;
>>> +
>>> +            if (next2 == PLATFORM_PROFILE_LAST)
>>> +                continue;
>>> +
>>> +            if (next != next2) {
>>> +                pr_warn("Next profile to cycle to is ambiguous between
>>> platform_profile handlers\n");
>>> +                return -EINVAL;
>>> +            }
>>> +            next = next2;
>>> +        }
>>> +
>>> +        /*
>>> +         * Second pass: apply "next" to each handler
>>> +         * If any failures occur unwind and revert all back to the 
>>> original
>>> profile
>>> +         */
>>> +        list_for_each_entry(handler, 
>>> &platform_profile_handler_list, list) {
>>> +            err = handler->profile_set(handler, next);
>>> +            if (err) {
>>> +                pr_err("Failed to set profile for handler %s\n", 
>>> handler->name);
>>> +                break;
>>> +            }
>>> +        }
>>> +        if (err) {
>>> +            list_for_each_entry_continue_reverse(handler,
>>> &platform_profile_handler_list, list) {
>>> +                err = handler->profile_set(handler, profile);
>>> +                if (err)
>>> +                    pr_err("Failed to revert profile for handler 
>>> %s\n",
>>> handler->name);
>>> +            }
>>> +        }
>>>       }
>>>
>>> -    err = cur_profile->profile_set(cur_profile, next);
>>> -    mutex_unlock(&profile_lock);
>>> -
>>> -    if (!err)
>>> -        sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>> +    sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>
>>> -    return err;
>>> +    return 0;
>>>   }
>>>   EXPORT_SYMBOL_GPL(platform_profile_cycle);
>>>
>>> @@ -190,21 +224,19 @@ int platform_profile_register(struct
>>> platform_profile_handler *pprof)
>>>       int err;
>>>
>>>       guard(mutex)(&profile_lock);
>>> -    /* We can only have one active profile */
>>> -    if (cur_profile)
>>> -        return -EEXIST;
>>>
>>>       /* Sanity check the profile handler field are set */
>>>       if (!pprof || bitmap_empty(pprof->choices, 
>>> PLATFORM_PROFILE_LAST) ||
>>>           !pprof->profile_set || !pprof->profile_get)
>>>           return -EINVAL;
>>>
>>> -    err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>>> -    if (err)
>>> -        return err;
>>> +    if (!platform_profile_is_registered()) {
>>> +        err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>>> +        if (err)
>>> +            return err;
>>> +    }
>>>       list_add_tail(&pprof->list, &platform_profile_handler_list);
>>>
>>> -    cur_profile = pprof;
>>>       return 0;
>>>   }
>>>   EXPORT_SYMBOL_GPL(platform_profile_register);
>>> @@ -215,7 +247,6 @@ int platform_profile_remove(struct
>>> platform_profile_handler *pprof)
>>>
>>>       list_del(&pprof->list);
>>>
>>> -    cur_profile = NULL;
>>>       if (!platform_profile_is_registered())
>>>           sysfs_remove_group(acpi_kobj, &platform_profile_group);
>>>
>>> -- 
>>> 2.43.0
>>
>> I'm still going thru the code changes - but I'm a bit unsure on the 
>> implementation itself.
>
> FYI, I split it up in v2 to make each chunk and intent behind it more 
> manageable to review instead of patch 7 being "so" big.
>
> V2 covers some of the points below as well based on some feedback from 
> Hans and Armin.
>
>>
>> I'd expect that one of the advantages of having different profile 
>> handlers register is that you could support extra & new profiles that 
>> might be wanted. For example the recent discussion of the AMD handler 
>> providing better tools to tweak advanced system settings for gaming 
>> etc. Won't this approach limit that? You'll only be able to have 
>> common settings.
>
> Well that RFC it turns out won't really be scalable because SPS is 
> done differently in AMD Strix and newer.  I haven't revisited it yet.
>
> But yes this approach would conceptually limit that idea because 
> common settings are all that is presented.
>
>>
>> I find having a common profile and two different handlers a bit 
>> tricky on how to handle. My concern is it can easily lead to conflict 
>> in settings.
>> If two handlers are doing different operations to provide the same 
>> effect - then neither handler is (probably) providing what they think 
>> is required. With your CPU vs EC example, the EC will often set CPU 
>> clock thresholds and the CPU profile handler will be changing that. 
>> If this is done I think it should be explicit to the user (admittedly 
>> I'm doing this with my Lenovo hat on - but we certify our platforms 
>> with our EC profile handler)
>>
>> I could see providing two separate handlers. e.g. balanced-A and 
>> balanced-B (for driver-A and driver-B) and the user maybe choosing 
>> which one they want (or both - though the user interface for that is 
>> definitely tricky)
>> But choosing one option for two different drivers seems confusing and 
>> with unknown side-effects. I appreciate it's complicated by your 
>> example wanting to add CPU and EC - I know how much work you've been 
>> doing on the AMD CPU front which benefits all systems.
>>
>
> Thinking through your comments I guess another way to approach this 
> would be "per-driver" sysfs knobs.  Here's my thought.
>
> 1) /sys/firmware/acpi/platform_profile_choices would contain only 
> things that are common and if there is something NOT common then also 
> the string "custom".
>
> 2) /sys/firmware/acpi/platform_profile would accept writes for 
> everything in platform profile choices except "custom".
>
> 3) Each driver handler would also export it's own sysfs files to 
> represent the driver state.
>
> 3) If the user changed the main knob at 
> /sys/firmware/acpi/platform_profile then it would change all driver 
> handlers.
>
> 4) If the user changed sysfs for any driver individually then the main 
> knob /sys/firmware/acpi/platform_profile would export "custom".
>
Sound like a good idea to me. Maybe we can create a "platform-profile" class for the per-driver sysfs interface?
The legacy platform profile sysfs interface can then use class_for_each_device() when getting/setting the current profiles.

For handling notifications we can add a notifier similar to the power supply subsystem. The legacy platform profile sysfs interface
can use this to receive notifications and forward those to the global sysfs attrs.

In the end old userspace applications can continue to use the legacy platform profile sysfs interface while new applications can
use the platform-profile class.

Thanks,
Armin Wolf

> Hans what do you think?
>
>> Another concern - would this mean that another driver could limit the 
>> options available? For instance if someone wrote a new 'mega-turbo' 
>> only profile driver and it loaded - it would then mean no profiles 
>> were available for anything as no profiles matched?
>
> Yes.  I don't think it's a problem in practice right now (as we only 
> just recently have two drivers vying for this position), but it 
> /could/ be something that happens.
>
>>
>> Let me know if I've misunderstood the architecture. I didn't fully 
>> get how the ASUS and Framework platforms were impacted in the intro 
>> I'm afraid.
>>
>> Thanks!
>> Mark
>
> Framework isn't affected, it was just showing that there are platforms 
> that use the BIOS/EC notification concept and not just SPS values that 
> the driver programs so it can't "go away" to solve this issue.
>
> ASUS is the only thing affected right now.
>
Mark Pearson Oct. 28, 2024, 5:20 p.m. UTC | #11
Thanks Mario,

On Mon, Oct 28, 2024, at 10:10 AM, Mario Limonciello wrote:
> On 10/28/2024 06:01, Mark Pearson wrote:
>> Hi Mario,
>> 
>> On Fri, Oct 25, 2024, at 3:30 PM, Mario Limonciello wrote:
>>> Multiple drivers may attempt to register platform profile handlers,
>>> but only one may be registered and the behavior is non-deterministic
>>> for which one wins.  It's mostly controlled by probing order.
>>>
>>> This can be problematic if one driver changes CPU settings and another
>>> driver notifies the EC for changing fan curves.
>>>
>>> Modify the ACPI platform profile handler to let multiple drivers
>>> register platform profile handlers and abstract this detail from userspace.
>>>
>>>  From userspace perspective the user will see profiles available across
>>> both drivers.  However to avoid chaos only allow changing to profiles
>>> that are common in both drivers.
>>>
>>> If any problems occur when changing profiles for any driver, then revert
>>> back to the previous profile.
>>>
>>> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
>>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>>> ---
>>>   drivers/acpi/platform_profile.c | 203 ++++++++++++++++++--------------
>>>   1 file changed, 117 insertions(+), 86 deletions(-)
>>>
>>> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
>>> index 091ca6941a925..915e3c49f0b5f 100644
>>> --- a/drivers/acpi/platform_profile.c
>>> +++ b/drivers/acpi/platform_profile.c
>>> @@ -9,7 +9,6 @@
>>>   #include <linux/platform_profile.h>
>>>   #include <linux/sysfs.h>
>>>
>>> -static struct platform_profile_handler *cur_profile;
>>>   static LIST_HEAD(platform_profile_handler_list);
>>>   static DEFINE_MUTEX(profile_lock);
>>>
>>> @@ -36,26 +35,26 @@ static ssize_t platform_profile_choices_show(struct
>>> device *dev,
>>>   					struct device_attribute *attr,
>>>   					char *buf)
>>>   {
>>> +	struct platform_profile_handler *handler;
>>> +	unsigned long seen = 0;
>>>   	int len = 0;
>>> -	int err, i;
>>> -
>>> -	err = mutex_lock_interruptible(&profile_lock);
>>> -	if (err)
>>> -		return err;
>>> -
>>> -	if (!cur_profile) {
>>> -		mutex_unlock(&profile_lock);
>>> -		return -ENODEV;
>>> +	int i;
>>> +
>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>> +			for_each_set_bit(i, handler->choices, PLATFORM_PROFILE_LAST) {
>>> +				if (seen & BIT(i))
>>> +					continue;
>>> +				if (len == 0)
>>> +					len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>>> +				else
>>> +					len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>>> +				seen |= BIT(i);
>>> +			}
>>> +		}
>>>   	}
>>>
>>> -	for_each_set_bit(i, cur_profile->choices, PLATFORM_PROFILE_LAST) {
>>> -		if (len == 0)
>>> -			len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>>> -		else
>>> -			len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>>> -	}
>>>   	len += sysfs_emit_at(buf, len, "\n");
>>> -	mutex_unlock(&profile_lock);
>>>   	return len;
>>>   }
>>>
>>> @@ -64,22 +63,20 @@ static ssize_t platform_profile_show(struct device *dev,
>>>   					char *buf)
>>>   {
>>>   	enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
>>> +	struct platform_profile_handler *handler;
>>>   	int err;
>>>
>>> -	err = mutex_lock_interruptible(&profile_lock);
>>> -	if (err)
>>> -		return err;
>>>
>>> -	if (!cur_profile) {
>>> -		mutex_unlock(&profile_lock);
>>> -		return -ENODEV;
>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>> +		if (!platform_profile_is_registered())
>>> +			return -ENODEV;
>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>> +			err = handler->profile_get(handler, &profile);
>>> +			if (err)
>>> +				return err;
>>> +		}
>>>   	}
>>>
>>> -	err = cur_profile->profile_get(cur_profile, &profile);
>>> -	mutex_unlock(&profile_lock);
>>> -	if (err)
>>> -		return err;
>>> -
>>>   	/* Check that profile is valid index */
>>>   	if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
>>>   		return -EIO;
>>> @@ -91,37 +88,48 @@ static ssize_t platform_profile_store(struct device *dev,
>>>   			    struct device_attribute *attr,
>>>   			    const char *buf, size_t count)
>>>   {
>>> +	struct platform_profile_handler *handler;
>>> +	enum platform_profile_option profile;
>>>   	int err, i;
>>>
>>> -	err = mutex_lock_interruptible(&profile_lock);
>>> -	if (err)
>>> -		return err;
>>> -
>>> -	if (!cur_profile) {
>>> -		mutex_unlock(&profile_lock);
>>> -		return -ENODEV;
>>> -	}
>>> -
>>>   	/* Scan for a matching profile */
>>>   	i = sysfs_match_string(profile_names, buf);
>>>   	if (i < 0) {
>>> -		mutex_unlock(&profile_lock);
>>>   		return -EINVAL;
>>>   	}
>>>
>>> -	/* Check that platform supports this profile choice */
>>> -	if (!test_bit(i, cur_profile->choices)) {
>>> -		mutex_unlock(&profile_lock);
>>> -		return -EOPNOTSUPP;
>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>> +		if (!platform_profile_is_registered())
>>> +			return -ENODEV;
>>> +
>>> +		/* Check that all handlers support this profile choice */
>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>> +			if (!test_bit(i, handler->choices))
>>> +				return -EOPNOTSUPP;
>>> +
>>> +			/* save the profile so that it can be reverted if necessary */
>>> +			err = handler->profile_get(handler, &profile);
>>> +			if (err)
>>> +				return err;
>>> +		}
>>> +
>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>> +			err = handler->profile_set(handler, i);
>>> +			if (err) {
>>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>>> +				break;
>>> +			}
>>> +		}
>>> +		if (err) {
>>> +			list_for_each_entry_continue_reverse(handler,
>>> &platform_profile_handler_list, list) {
>>> +				if (handler->profile_set(handler, profile))
>>> +					pr_err("Failed to revert profile for handler %s\n",
>>> handler->name);
>>> +			}
>>> +			return err;
>>> +		}
>>>   	}
>>>
>>> -	err = cur_profile->profile_set(cur_profile, i);
>>> -	if (!err)
>>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>> -
>>> -	mutex_unlock(&profile_lock);
>>> -	if (err)
>>> -		return err;
>>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>   	return count;
>>>   }
>>>
>>> @@ -140,7 +148,8 @@ static const struct attribute_group
>>> platform_profile_group = {
>>>
>>>   void platform_profile_notify(void)
>>>   {
>>> -	if (!cur_profile)
>>> +	guard(mutex)(&profile_lock);
>>> +	if (!platform_profile_is_registered())
>>>   		return;
>>>   	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>   }
>>> @@ -148,40 +157,65 @@ EXPORT_SYMBOL_GPL(platform_profile_notify);
>>>
>>>   int platform_profile_cycle(void)
>>>   {
>>> +	struct platform_profile_handler *handler;
>>>   	enum platform_profile_option profile;
>>> -	enum platform_profile_option next;
>>> +	enum platform_profile_option next = PLATFORM_PROFILE_LAST;
>>> +	enum platform_profile_option next2 = PLATFORM_PROFILE_LAST;
>>>   	int err;
>>>
>>> -	err = mutex_lock_interruptible(&profile_lock);
>>> -	if (err)
>>> -		return err;
>>> -
>>> -	if (!cur_profile) {
>>> -		mutex_unlock(&profile_lock);
>>> -		return -ENODEV;
>>> -	}
>>> -
>>> -	err = cur_profile->profile_get(cur_profile, &profile);
>>> -	if (err) {
>>> -		mutex_unlock(&profile_lock);
>>> -		return err;
>>> -	}
>>> -
>>> -	next = find_next_bit_wrap(cur_profile->choices, PLATFORM_PROFILE_LAST,
>>> -				  profile + 1);
>>> -
>>> -	if (WARN_ON(next == PLATFORM_PROFILE_LAST)) {
>>> -		mutex_unlock(&profile_lock);
>>> -		return -EINVAL;
>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>> +		/* first pass, make sure all handlers agree on the definition of
>>> "next" profile */
>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>> +
>>> +			err = handler->profile_get(handler, &profile);
>>> +			if (err)
>>> +				return err;
>>> +
>>> +			if (next == PLATFORM_PROFILE_LAST)
>>> +				next = find_next_bit_wrap(handler->choices,
>>> +							  PLATFORM_PROFILE_LAST,
>>> +							  profile + 1);
>>> +			else
>>> +				next2 = find_next_bit_wrap(handler->choices,
>>> +							   PLATFORM_PROFILE_LAST,
>>> +							   profile + 1);
>>> +
>>> +			if (WARN_ON(next == PLATFORM_PROFILE_LAST))
>>> +				return -EINVAL;
>>> +
>>> +			if (next2 == PLATFORM_PROFILE_LAST)
>>> +				continue;
>>> +
>>> +			if (next != next2) {
>>> +				pr_warn("Next profile to cycle to is ambiguous between
>>> platform_profile handlers\n");
>>> +				return -EINVAL;
>>> +			}
>>> +			next = next2;
>>> +		}
>>> +
>>> +		/*
>>> +		 * Second pass: apply "next" to each handler
>>> +		 * If any failures occur unwind and revert all back to the original
>>> profile
>>> +		 */
>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>> +			err = handler->profile_set(handler, next);
>>> +			if (err) {
>>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>>> +				break;
>>> +			}
>>> +		}
>>> +		if (err) {
>>> +			list_for_each_entry_continue_reverse(handler,
>>> &platform_profile_handler_list, list) {
>>> +				err = handler->profile_set(handler, profile);
>>> +				if (err)
>>> +					pr_err("Failed to revert profile for handler %s\n",
>>> handler->name);
>>> +			}
>>> +		}
>>>   	}
>>>
>>> -	err = cur_profile->profile_set(cur_profile, next);
>>> -	mutex_unlock(&profile_lock);
>>> -
>>> -	if (!err)
>>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>
>>> -	return err;
>>> +	return 0;
>>>   }
>>>   EXPORT_SYMBOL_GPL(platform_profile_cycle);
>>>
>>> @@ -190,21 +224,19 @@ int platform_profile_register(struct
>>> platform_profile_handler *pprof)
>>>   	int err;
>>>
>>>   	guard(mutex)(&profile_lock);
>>> -	/* We can only have one active profile */
>>> -	if (cur_profile)
>>> -		return -EEXIST;
>>>
>>>   	/* Sanity check the profile handler field are set */
>>>   	if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
>>>   		!pprof->profile_set || !pprof->profile_get)
>>>   		return -EINVAL;
>>>
>>> -	err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>>> -	if (err)
>>> -		return err;
>>> +	if (!platform_profile_is_registered()) {
>>> +		err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>>> +		if (err)
>>> +			return err;
>>> +	}
>>>   	list_add_tail(&pprof->list, &platform_profile_handler_list);
>>>
>>> -	cur_profile = pprof;
>>>   	return 0;
>>>   }
>>>   EXPORT_SYMBOL_GPL(platform_profile_register);
>>> @@ -215,7 +247,6 @@ int platform_profile_remove(struct
>>> platform_profile_handler *pprof)
>>>
>>>   	list_del(&pprof->list);
>>>
>>> -	cur_profile = NULL;
>>>   	if (!platform_profile_is_registered())
>>>   		sysfs_remove_group(acpi_kobj, &platform_profile_group);
>>>
>>> -- 
>>> 2.43.0
>> 
>> I'm still going thru the code changes - but I'm a bit unsure on the implementation itself.
>
> FYI, I split it up in v2 to make each chunk and intent behind it more 
> manageable to review instead of patch 7 being "so" big.
>
> V2 covers some of the points below as well based on some feedback from 
> Hans and Armin.
>

Ack - sorry for that, on PTO this week/last week and not checking email very often. Saw the v2 after I sent the comments on v1 (my inbox is a little bit out of control right now).
I've just started looking thru that - let me know if better to move that conversation there

>> 
>> I'd expect that one of the advantages of having different profile handlers register is that you could support extra & new profiles that might be wanted. For example the recent discussion of the AMD handler providing better tools to tweak advanced system settings for gaming etc. Won't this approach limit that? You'll only be able to have common settings.
>
> Well that RFC it turns out won't really be scalable because SPS is done 
> differently in AMD Strix and newer.  I haven't revisited it yet.
>
> But yes this approach would conceptually limit that idea because common 
> settings are all that is presented.
>
>> 
>> I find having a common profile and two different handlers a bit tricky on how to handle. My concern is it can easily lead to conflict in settings.
>> If two handlers are doing different operations to provide the same effect - then neither handler is (probably) providing what they think is required. With your CPU vs EC example, the EC will often set CPU clock thresholds and the CPU profile handler will be changing that. If this is done I think it should be explicit to the user (admittedly I'm doing this with my Lenovo hat on - but we certify our platforms with our EC profile handler)
>> 
>> I could see providing two separate handlers. e.g. balanced-A and balanced-B (for driver-A and driver-B) and the user maybe choosing which one they want (or both - though the user interface for that is definitely tricky)
>> But choosing one option for two different drivers seems confusing and with unknown side-effects. I appreciate it's complicated by your example wanting to add CPU and EC - I know how much work you've been doing on the AMD CPU front which benefits all systems.
>> 
>
> Thinking through your comments I guess another way to approach this 
> would be "per-driver" sysfs knobs.  Here's my thought.
>
> 1) /sys/firmware/acpi/platform_profile_choices would contain only things 
> that are common and if there is something NOT common then also the 
> string "custom".
>
> 2) /sys/firmware/acpi/platform_profile would accept writes for 
> everything in platform profile choices except "custom".
>
> 3) Each driver handler would also export it's own sysfs files to 
> represent the driver state.
>
> 3) If the user changed the main knob at 
> /sys/firmware/acpi/platform_profile then it would change all driver 
> handlers.
>
> 4) If the user changed sysfs for any driver individually then the main 
> knob /sys/firmware/acpi/platform_profile would export "custom".

I need to think about it a bit more - I still have some concerns about two drivers doing the same thing. In some cases they can complement each other nicely, but in other cases they will treat on each others toes.

Just to throw another idea on the pile:

 - If only one profile then it can have low-power, balanced, performance as currently
 - If two or more profiles, belonging as an example to driverA and driverB, their profile names become balanced-driverA and balanced-driverB. You could choose specifically if you wanted to activate one of them by using the driver name
 - Each driver would have a priority. I'm biased here, but a vendor platform driver would have priority 1, a CPU vendor driver priority 2, etc. This would be used to determine which driver version would be used for the cases where multiple options are present and user chooses, for example, just balanced. If two drivers of the same priority compete then first driver loaded wins.

So as an example: if we have the case where we have thinkpad_acpi (priority 1) and amd (priority 2) profile drivers offering all three profiles then:

/sys/blah/platform_profile_choices would offer:
lower-power, balanced, performance, low-power-thinkpad_acpi, balanced-thinkpad_acpi, performance-thinkpad_acpi, low-power-amd, balanced-amd, performance-amd.

If the user chooses balanced - it would activate the balanced-thinkpad_acpi profile as it has the higher priority.
The user could then also activate balanced-amd separately if they choose. Multi balanced profiles are OK.

Initially this would all be from command line, but I'm sure some nice user space GUI can be done to allow you to choose which profiles are available and active/in-active.

Maybe also add an option under platform-profiles to have an "enable all matching profiles" to override the priority implementation? In this case choosing balanced would enable all balanced profiles available (in priority order?)

Would that work for the ASUS case or not?

>
> Hans what do you think?
>

>> Another concern - would this mean that another driver could limit the options available? For instance if someone wrote a new 'mega-turbo' only profile driver and it loaded - it would then mean no profiles were available for anything as no profiles matched?
>
> Yes.  I don't think it's a problem in practice right now (as we only 
> just recently have two drivers vying for this position), but it /could/ 
> be something that happens.
>

I'd certainly like to leave the window open so that there could be, for instance, a 'gaming' profile option that does some high-performance mode with maybe power shifting towards the GPU and some other graphics only tuning things? 
I wouldn't like to shut down the ability to have options for things like that.

>> 
>> Let me know if I've misunderstood the architecture. I didn't fully get how the ASUS and Framework platforms were impacted in the intro I'm afraid.
>> 
>> Thanks!
>> Mark
>
> Framework isn't affected, it was just showing that there are platforms 
> that use the BIOS/EC notification concept and not just SPS values that 
> the driver programs so it can't "go away" to solve this issue.
>
> ASUS is the only thing affected right now.
Mario Limonciello Oct. 28, 2024, 5:35 p.m. UTC | #12
On 10/28/2024 12:20, Mark Pearson wrote:
> Thanks Mario,
> 
> On Mon, Oct 28, 2024, at 10:10 AM, Mario Limonciello wrote:
>> On 10/28/2024 06:01, Mark Pearson wrote:
>>> Hi Mario,
>>>
>>> On Fri, Oct 25, 2024, at 3:30 PM, Mario Limonciello wrote:
>>>> Multiple drivers may attempt to register platform profile handlers,
>>>> but only one may be registered and the behavior is non-deterministic
>>>> for which one wins.  It's mostly controlled by probing order.
>>>>
>>>> This can be problematic if one driver changes CPU settings and another
>>>> driver notifies the EC for changing fan curves.
>>>>
>>>> Modify the ACPI platform profile handler to let multiple drivers
>>>> register platform profile handlers and abstract this detail from userspace.
>>>>
>>>>   From userspace perspective the user will see profiles available across
>>>> both drivers.  However to avoid chaos only allow changing to profiles
>>>> that are common in both drivers.
>>>>
>>>> If any problems occur when changing profiles for any driver, then revert
>>>> back to the previous profile.
>>>>
>>>> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
>>>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>>>> ---
>>>>    drivers/acpi/platform_profile.c | 203 ++++++++++++++++++--------------
>>>>    1 file changed, 117 insertions(+), 86 deletions(-)
>>>>
>>>> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
>>>> index 091ca6941a925..915e3c49f0b5f 100644
>>>> --- a/drivers/acpi/platform_profile.c
>>>> +++ b/drivers/acpi/platform_profile.c
>>>> @@ -9,7 +9,6 @@
>>>>    #include <linux/platform_profile.h>
>>>>    #include <linux/sysfs.h>
>>>>
>>>> -static struct platform_profile_handler *cur_profile;
>>>>    static LIST_HEAD(platform_profile_handler_list);
>>>>    static DEFINE_MUTEX(profile_lock);
>>>>
>>>> @@ -36,26 +35,26 @@ static ssize_t platform_profile_choices_show(struct
>>>> device *dev,
>>>>    					struct device_attribute *attr,
>>>>    					char *buf)
>>>>    {
>>>> +	struct platform_profile_handler *handler;
>>>> +	unsigned long seen = 0;
>>>>    	int len = 0;
>>>> -	int err, i;
>>>> -
>>>> -	err = mutex_lock_interruptible(&profile_lock);
>>>> -	if (err)
>>>> -		return err;
>>>> -
>>>> -	if (!cur_profile) {
>>>> -		mutex_unlock(&profile_lock);
>>>> -		return -ENODEV;
>>>> +	int i;
>>>> +
>>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>> +			for_each_set_bit(i, handler->choices, PLATFORM_PROFILE_LAST) {
>>>> +				if (seen & BIT(i))
>>>> +					continue;
>>>> +				if (len == 0)
>>>> +					len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>>>> +				else
>>>> +					len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>>>> +				seen |= BIT(i);
>>>> +			}
>>>> +		}
>>>>    	}
>>>>
>>>> -	for_each_set_bit(i, cur_profile->choices, PLATFORM_PROFILE_LAST) {
>>>> -		if (len == 0)
>>>> -			len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>>>> -		else
>>>> -			len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>>>> -	}
>>>>    	len += sysfs_emit_at(buf, len, "\n");
>>>> -	mutex_unlock(&profile_lock);
>>>>    	return len;
>>>>    }
>>>>
>>>> @@ -64,22 +63,20 @@ static ssize_t platform_profile_show(struct device *dev,
>>>>    					char *buf)
>>>>    {
>>>>    	enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
>>>> +	struct platform_profile_handler *handler;
>>>>    	int err;
>>>>
>>>> -	err = mutex_lock_interruptible(&profile_lock);
>>>> -	if (err)
>>>> -		return err;
>>>>
>>>> -	if (!cur_profile) {
>>>> -		mutex_unlock(&profile_lock);
>>>> -		return -ENODEV;
>>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>>> +		if (!platform_profile_is_registered())
>>>> +			return -ENODEV;
>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>> +			err = handler->profile_get(handler, &profile);
>>>> +			if (err)
>>>> +				return err;
>>>> +		}
>>>>    	}
>>>>
>>>> -	err = cur_profile->profile_get(cur_profile, &profile);
>>>> -	mutex_unlock(&profile_lock);
>>>> -	if (err)
>>>> -		return err;
>>>> -
>>>>    	/* Check that profile is valid index */
>>>>    	if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
>>>>    		return -EIO;
>>>> @@ -91,37 +88,48 @@ static ssize_t platform_profile_store(struct device *dev,
>>>>    			    struct device_attribute *attr,
>>>>    			    const char *buf, size_t count)
>>>>    {
>>>> +	struct platform_profile_handler *handler;
>>>> +	enum platform_profile_option profile;
>>>>    	int err, i;
>>>>
>>>> -	err = mutex_lock_interruptible(&profile_lock);
>>>> -	if (err)
>>>> -		return err;
>>>> -
>>>> -	if (!cur_profile) {
>>>> -		mutex_unlock(&profile_lock);
>>>> -		return -ENODEV;
>>>> -	}
>>>> -
>>>>    	/* Scan for a matching profile */
>>>>    	i = sysfs_match_string(profile_names, buf);
>>>>    	if (i < 0) {
>>>> -		mutex_unlock(&profile_lock);
>>>>    		return -EINVAL;
>>>>    	}
>>>>
>>>> -	/* Check that platform supports this profile choice */
>>>> -	if (!test_bit(i, cur_profile->choices)) {
>>>> -		mutex_unlock(&profile_lock);
>>>> -		return -EOPNOTSUPP;
>>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>>> +		if (!platform_profile_is_registered())
>>>> +			return -ENODEV;
>>>> +
>>>> +		/* Check that all handlers support this profile choice */
>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>> +			if (!test_bit(i, handler->choices))
>>>> +				return -EOPNOTSUPP;
>>>> +
>>>> +			/* save the profile so that it can be reverted if necessary */
>>>> +			err = handler->profile_get(handler, &profile);
>>>> +			if (err)
>>>> +				return err;
>>>> +		}
>>>> +
>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>> +			err = handler->profile_set(handler, i);
>>>> +			if (err) {
>>>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>>>> +				break;
>>>> +			}
>>>> +		}
>>>> +		if (err) {
>>>> +			list_for_each_entry_continue_reverse(handler,
>>>> &platform_profile_handler_list, list) {
>>>> +				if (handler->profile_set(handler, profile))
>>>> +					pr_err("Failed to revert profile for handler %s\n",
>>>> handler->name);
>>>> +			}
>>>> +			return err;
>>>> +		}
>>>>    	}
>>>>
>>>> -	err = cur_profile->profile_set(cur_profile, i);
>>>> -	if (!err)
>>>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>> -
>>>> -	mutex_unlock(&profile_lock);
>>>> -	if (err)
>>>> -		return err;
>>>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>>    	return count;
>>>>    }
>>>>
>>>> @@ -140,7 +148,8 @@ static const struct attribute_group
>>>> platform_profile_group = {
>>>>
>>>>    void platform_profile_notify(void)
>>>>    {
>>>> -	if (!cur_profile)
>>>> +	guard(mutex)(&profile_lock);
>>>> +	if (!platform_profile_is_registered())
>>>>    		return;
>>>>    	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>>    }
>>>> @@ -148,40 +157,65 @@ EXPORT_SYMBOL_GPL(platform_profile_notify);
>>>>
>>>>    int platform_profile_cycle(void)
>>>>    {
>>>> +	struct platform_profile_handler *handler;
>>>>    	enum platform_profile_option profile;
>>>> -	enum platform_profile_option next;
>>>> +	enum platform_profile_option next = PLATFORM_PROFILE_LAST;
>>>> +	enum platform_profile_option next2 = PLATFORM_PROFILE_LAST;
>>>>    	int err;
>>>>
>>>> -	err = mutex_lock_interruptible(&profile_lock);
>>>> -	if (err)
>>>> -		return err;
>>>> -
>>>> -	if (!cur_profile) {
>>>> -		mutex_unlock(&profile_lock);
>>>> -		return -ENODEV;
>>>> -	}
>>>> -
>>>> -	err = cur_profile->profile_get(cur_profile, &profile);
>>>> -	if (err) {
>>>> -		mutex_unlock(&profile_lock);
>>>> -		return err;
>>>> -	}
>>>> -
>>>> -	next = find_next_bit_wrap(cur_profile->choices, PLATFORM_PROFILE_LAST,
>>>> -				  profile + 1);
>>>> -
>>>> -	if (WARN_ON(next == PLATFORM_PROFILE_LAST)) {
>>>> -		mutex_unlock(&profile_lock);
>>>> -		return -EINVAL;
>>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>>> +		/* first pass, make sure all handlers agree on the definition of
>>>> "next" profile */
>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>> +
>>>> +			err = handler->profile_get(handler, &profile);
>>>> +			if (err)
>>>> +				return err;
>>>> +
>>>> +			if (next == PLATFORM_PROFILE_LAST)
>>>> +				next = find_next_bit_wrap(handler->choices,
>>>> +							  PLATFORM_PROFILE_LAST,
>>>> +							  profile + 1);
>>>> +			else
>>>> +				next2 = find_next_bit_wrap(handler->choices,
>>>> +							   PLATFORM_PROFILE_LAST,
>>>> +							   profile + 1);
>>>> +
>>>> +			if (WARN_ON(next == PLATFORM_PROFILE_LAST))
>>>> +				return -EINVAL;
>>>> +
>>>> +			if (next2 == PLATFORM_PROFILE_LAST)
>>>> +				continue;
>>>> +
>>>> +			if (next != next2) {
>>>> +				pr_warn("Next profile to cycle to is ambiguous between
>>>> platform_profile handlers\n");
>>>> +				return -EINVAL;
>>>> +			}
>>>> +			next = next2;
>>>> +		}
>>>> +
>>>> +		/*
>>>> +		 * Second pass: apply "next" to each handler
>>>> +		 * If any failures occur unwind and revert all back to the original
>>>> profile
>>>> +		 */
>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>> +			err = handler->profile_set(handler, next);
>>>> +			if (err) {
>>>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>>>> +				break;
>>>> +			}
>>>> +		}
>>>> +		if (err) {
>>>> +			list_for_each_entry_continue_reverse(handler,
>>>> &platform_profile_handler_list, list) {
>>>> +				err = handler->profile_set(handler, profile);
>>>> +				if (err)
>>>> +					pr_err("Failed to revert profile for handler %s\n",
>>>> handler->name);
>>>> +			}
>>>> +		}
>>>>    	}
>>>>
>>>> -	err = cur_profile->profile_set(cur_profile, next);
>>>> -	mutex_unlock(&profile_lock);
>>>> -
>>>> -	if (!err)
>>>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>>
>>>> -	return err;
>>>> +	return 0;
>>>>    }
>>>>    EXPORT_SYMBOL_GPL(platform_profile_cycle);
>>>>
>>>> @@ -190,21 +224,19 @@ int platform_profile_register(struct
>>>> platform_profile_handler *pprof)
>>>>    	int err;
>>>>
>>>>    	guard(mutex)(&profile_lock);
>>>> -	/* We can only have one active profile */
>>>> -	if (cur_profile)
>>>> -		return -EEXIST;
>>>>
>>>>    	/* Sanity check the profile handler field are set */
>>>>    	if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
>>>>    		!pprof->profile_set || !pprof->profile_get)
>>>>    		return -EINVAL;
>>>>
>>>> -	err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>>>> -	if (err)
>>>> -		return err;
>>>> +	if (!platform_profile_is_registered()) {
>>>> +		err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>>>> +		if (err)
>>>> +			return err;
>>>> +	}
>>>>    	list_add_tail(&pprof->list, &platform_profile_handler_list);
>>>>
>>>> -	cur_profile = pprof;
>>>>    	return 0;
>>>>    }
>>>>    EXPORT_SYMBOL_GPL(platform_profile_register);
>>>> @@ -215,7 +247,6 @@ int platform_profile_remove(struct
>>>> platform_profile_handler *pprof)
>>>>
>>>>    	list_del(&pprof->list);
>>>>
>>>> -	cur_profile = NULL;
>>>>    	if (!platform_profile_is_registered())
>>>>    		sysfs_remove_group(acpi_kobj, &platform_profile_group);
>>>>
>>>> -- 
>>>> 2.43.0
>>>
>>> I'm still going thru the code changes - but I'm a bit unsure on the implementation itself.
>>
>> FYI, I split it up in v2 to make each chunk and intent behind it more
>> manageable to review instead of patch 7 being "so" big.
>>
>> V2 covers some of the points below as well based on some feedback from
>> Hans and Armin.
>>
> 
> Ack - sorry for that, on PTO this week/last week and not checking email very often. Saw the v2 after I sent the comments on v1 (my inbox is a little bit out of control right now).
> I've just started looking thru that - let me know if better to move that conversation there
> 
>>>
>>> I'd expect that one of the advantages of having different profile handlers register is that you could support extra & new profiles that might be wanted. For example the recent discussion of the AMD handler providing better tools to tweak advanced system settings for gaming etc. Won't this approach limit that? You'll only be able to have common settings.
>>
>> Well that RFC it turns out won't really be scalable because SPS is done
>> differently in AMD Strix and newer.  I haven't revisited it yet.
>>
>> But yes this approach would conceptually limit that idea because common
>> settings are all that is presented.
>>
>>>
>>> I find having a common profile and two different handlers a bit tricky on how to handle. My concern is it can easily lead to conflict in settings.
>>> If two handlers are doing different operations to provide the same effect - then neither handler is (probably) providing what they think is required. With your CPU vs EC example, the EC will often set CPU clock thresholds and the CPU profile handler will be changing that. If this is done I think it should be explicit to the user (admittedly I'm doing this with my Lenovo hat on - but we certify our platforms with our EC profile handler)
>>>
>>> I could see providing two separate handlers. e.g. balanced-A and balanced-B (for driver-A and driver-B) and the user maybe choosing which one they want (or both - though the user interface for that is definitely tricky)
>>> But choosing one option for two different drivers seems confusing and with unknown side-effects. I appreciate it's complicated by your example wanting to add CPU and EC - I know how much work you've been doing on the AMD CPU front which benefits all systems.
>>>
>>
>> Thinking through your comments I guess another way to approach this
>> would be "per-driver" sysfs knobs.  Here's my thought.
>>
>> 1) /sys/firmware/acpi/platform_profile_choices would contain only things
>> that are common and if there is something NOT common then also the
>> string "custom".
>>
>> 2) /sys/firmware/acpi/platform_profile would accept writes for
>> everything in platform profile choices except "custom".
>>
>> 3) Each driver handler would also export it's own sysfs files to
>> represent the driver state.
>>
>> 3) If the user changed the main knob at
>> /sys/firmware/acpi/platform_profile then it would change all driver
>> handlers.
>>
>> 4) If the user changed sysfs for any driver individually then the main
>> knob /sys/firmware/acpi/platform_profile would export "custom".
> 
> I need to think about it a bit more - I still have some concerns about two drivers doing the same thing. In some cases they can complement each other nicely, but in other cases they will treat on each others toes.
> 
> Just to throw another idea on the pile:
> 
>   - If only one profile then it can have low-power, balanced, performance as currently
>   - If two or more profiles, belonging as an example to driverA and driverB, their profile names become balanced-driverA and balanced-driverB. You could choose specifically if you wanted to activate one of them by using the driver name
>   - Each driver would have a priority. I'm biased here, but a vendor platform driver would have priority 1, a CPU vendor driver priority 2, etc. This would be used to determine which driver version would be used for the cases where multiple options are present and user chooses, for example, just balanced. If two drivers of the same priority compete then first driver loaded wins.
> 
> So as an example: if we have the case where we have thinkpad_acpi (priority 1) and amd (priority 2) profile drivers offering all three profiles then:
> 
> /sys/blah/platform_profile_choices would offer:
> lower-power, balanced, performance, low-power-thinkpad_acpi, balanced-thinkpad_acpi, performance-thinkpad_acpi, low-power-amd, balanced-amd, performance-amd.
> 
> If the user chooses balanced - it would activate the balanced-thinkpad_acpi profile as it has the higher priority.
> The user could then also activate balanced-amd separately if they choose. Multi balanced profiles are OK.
> 
> Initially this would all be from command line, but I'm sure some nice user space GUI can be done to allow you to choose which profiles are available and active/in-active.
> 
> Maybe also add an option under platform-profiles to have an "enable all matching profiles" to override the priority implementation? In this case choosing balanced would enable all balanced profiles available (in priority order?)
> 
> Would that work for the ASUS case or not?

At least for the ASUS case "today" I think "priority" could work, but 
what happens if one day ASUS prefers to use amd-pmf instead of the 
custom WMI stuff for some systems?  IMO you don't want to have it set in 
stone for priorities.

I do worry that generally though users won't know what they want if 
there is a "balanced", "balanced-vendor" and "balanced-cpu".

And I don't think we can use the same interface to select multiple at 
once.  I think if we're to have multiple handlers exposed and allow 
different options then it needs to be a class interface (or something 
similar).

> 
>>
>> Hans what do you think?
>>
> 
>>> Another concern - would this mean that another driver could limit the options available? For instance if someone wrote a new 'mega-turbo' only profile driver and it loaded - it would then mean no profiles were available for anything as no profiles matched?
>>
>> Yes.  I don't think it's a problem in practice right now (as we only
>> just recently have two drivers vying for this position), but it /could/
>> be something that happens.
>>
> 
> I'd certainly like to leave the window open so that there could be, for instance, a 'gaming' profile option that does some high-performance mode with maybe power shifting towards the GPU and some other graphics only tuning things?
> I wouldn't like to shut down the ability to have options for things like that.
> 

It comes down to how much of a catch all this knob needs to be in the 
kernel.  If you put too much into the single knob it doesn't become 
useful anymore generically.

At least for AMD APU + AMD dGPU designs there already exists a bias knob 
that lets you control the power share.

https://docs.kernel.org/gpu/amdgpu/driver-misc.html#gpu-smartshift-information

Similarly you can manually adjust clocks and performance profiles for 
GPU from standard files:

https://docs.kernel.org/gpu/amdgpu/thermal.html#gpu-sysfs-power-state-interfaces

So my gut says this idea of gaming mode should be stuck to userspace. 
There is already such a project for that:

https://github.com/FeralInteractive/gamemode

>>>
>>> Let me know if I've misunderstood the architecture. I didn't fully get how the ASUS and Framework platforms were impacted in the intro I'm afraid.
>>>
>>> Thanks!
>>> Mark
>>
>> Framework isn't affected, it was just showing that there are platforms
>> that use the BIOS/EC notification concept and not just SPS values that
>> the driver programs so it can't "go away" to solve this issue.
>>
>> ASUS is the only thing affected right now.
Mark Pearson Oct. 28, 2024, 8:11 p.m. UTC | #13
On Mon, Oct 28, 2024, at 1:35 PM, Mario Limonciello wrote:
> On 10/28/2024 12:20, Mark Pearson wrote:
>> Thanks Mario,
>> 
>> On Mon, Oct 28, 2024, at 10:10 AM, Mario Limonciello wrote:
>>> On 10/28/2024 06:01, Mark Pearson wrote:
>>>> Hi Mario,
>>>>
>>>> On Fri, Oct 25, 2024, at 3:30 PM, Mario Limonciello wrote:
>>>>> Multiple drivers may attempt to register platform profile handlers,
>>>>> but only one may be registered and the behavior is non-deterministic
>>>>> for which one wins.  It's mostly controlled by probing order.
>>>>>
>>>>> This can be problematic if one driver changes CPU settings and another
>>>>> driver notifies the EC for changing fan curves.
>>>>>
>>>>> Modify the ACPI platform profile handler to let multiple drivers
>>>>> register platform profile handlers and abstract this detail from userspace.
>>>>>
>>>>>   From userspace perspective the user will see profiles available across
>>>>> both drivers.  However to avoid chaos only allow changing to profiles
>>>>> that are common in both drivers.
>>>>>
>>>>> If any problems occur when changing profiles for any driver, then revert
>>>>> back to the previous profile.
>>>>>
>>>>> Tested-by: Matthew Schwartz <matthew.schwartz@linux.dev>
>>>>> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
>>>>> ---
>>>>>    drivers/acpi/platform_profile.c | 203 ++++++++++++++++++--------------
>>>>>    1 file changed, 117 insertions(+), 86 deletions(-)
>>>>>
>>>>> diff --git a/drivers/acpi/platform_profile.c b/drivers/acpi/platform_profile.c
>>>>> index 091ca6941a925..915e3c49f0b5f 100644
>>>>> --- a/drivers/acpi/platform_profile.c
>>>>> +++ b/drivers/acpi/platform_profile.c
>>>>> @@ -9,7 +9,6 @@
>>>>>    #include <linux/platform_profile.h>
>>>>>    #include <linux/sysfs.h>
>>>>>
>>>>> -static struct platform_profile_handler *cur_profile;
>>>>>    static LIST_HEAD(platform_profile_handler_list);
>>>>>    static DEFINE_MUTEX(profile_lock);
>>>>>
>>>>> @@ -36,26 +35,26 @@ static ssize_t platform_profile_choices_show(struct
>>>>> device *dev,
>>>>>    					struct device_attribute *attr,
>>>>>    					char *buf)
>>>>>    {
>>>>> +	struct platform_profile_handler *handler;
>>>>> +	unsigned long seen = 0;
>>>>>    	int len = 0;
>>>>> -	int err, i;
>>>>> -
>>>>> -	err = mutex_lock_interruptible(&profile_lock);
>>>>> -	if (err)
>>>>> -		return err;
>>>>> -
>>>>> -	if (!cur_profile) {
>>>>> -		mutex_unlock(&profile_lock);
>>>>> -		return -ENODEV;
>>>>> +	int i;
>>>>> +
>>>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>>> +			for_each_set_bit(i, handler->choices, PLATFORM_PROFILE_LAST) {
>>>>> +				if (seen & BIT(i))
>>>>> +					continue;
>>>>> +				if (len == 0)
>>>>> +					len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>>>>> +				else
>>>>> +					len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>>>>> +				seen |= BIT(i);
>>>>> +			}
>>>>> +		}
>>>>>    	}
>>>>>
>>>>> -	for_each_set_bit(i, cur_profile->choices, PLATFORM_PROFILE_LAST) {
>>>>> -		if (len == 0)
>>>>> -			len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
>>>>> -		else
>>>>> -			len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
>>>>> -	}
>>>>>    	len += sysfs_emit_at(buf, len, "\n");
>>>>> -	mutex_unlock(&profile_lock);
>>>>>    	return len;
>>>>>    }
>>>>>
>>>>> @@ -64,22 +63,20 @@ static ssize_t platform_profile_show(struct device *dev,
>>>>>    					char *buf)
>>>>>    {
>>>>>    	enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
>>>>> +	struct platform_profile_handler *handler;
>>>>>    	int err;
>>>>>
>>>>> -	err = mutex_lock_interruptible(&profile_lock);
>>>>> -	if (err)
>>>>> -		return err;
>>>>>
>>>>> -	if (!cur_profile) {
>>>>> -		mutex_unlock(&profile_lock);
>>>>> -		return -ENODEV;
>>>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>>>> +		if (!platform_profile_is_registered())
>>>>> +			return -ENODEV;
>>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>>> +			err = handler->profile_get(handler, &profile);
>>>>> +			if (err)
>>>>> +				return err;
>>>>> +		}
>>>>>    	}
>>>>>
>>>>> -	err = cur_profile->profile_get(cur_profile, &profile);
>>>>> -	mutex_unlock(&profile_lock);
>>>>> -	if (err)
>>>>> -		return err;
>>>>> -
>>>>>    	/* Check that profile is valid index */
>>>>>    	if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
>>>>>    		return -EIO;
>>>>> @@ -91,37 +88,48 @@ static ssize_t platform_profile_store(struct device *dev,
>>>>>    			    struct device_attribute *attr,
>>>>>    			    const char *buf, size_t count)
>>>>>    {
>>>>> +	struct platform_profile_handler *handler;
>>>>> +	enum platform_profile_option profile;
>>>>>    	int err, i;
>>>>>
>>>>> -	err = mutex_lock_interruptible(&profile_lock);
>>>>> -	if (err)
>>>>> -		return err;
>>>>> -
>>>>> -	if (!cur_profile) {
>>>>> -		mutex_unlock(&profile_lock);
>>>>> -		return -ENODEV;
>>>>> -	}
>>>>> -
>>>>>    	/* Scan for a matching profile */
>>>>>    	i = sysfs_match_string(profile_names, buf);
>>>>>    	if (i < 0) {
>>>>> -		mutex_unlock(&profile_lock);
>>>>>    		return -EINVAL;
>>>>>    	}
>>>>>
>>>>> -	/* Check that platform supports this profile choice */
>>>>> -	if (!test_bit(i, cur_profile->choices)) {
>>>>> -		mutex_unlock(&profile_lock);
>>>>> -		return -EOPNOTSUPP;
>>>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>>>> +		if (!platform_profile_is_registered())
>>>>> +			return -ENODEV;
>>>>> +
>>>>> +		/* Check that all handlers support this profile choice */
>>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>>> +			if (!test_bit(i, handler->choices))
>>>>> +				return -EOPNOTSUPP;
>>>>> +
>>>>> +			/* save the profile so that it can be reverted if necessary */
>>>>> +			err = handler->profile_get(handler, &profile);
>>>>> +			if (err)
>>>>> +				return err;
>>>>> +		}
>>>>> +
>>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>>> +			err = handler->profile_set(handler, i);
>>>>> +			if (err) {
>>>>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>>>>> +				break;
>>>>> +			}
>>>>> +		}
>>>>> +		if (err) {
>>>>> +			list_for_each_entry_continue_reverse(handler,
>>>>> &platform_profile_handler_list, list) {
>>>>> +				if (handler->profile_set(handler, profile))
>>>>> +					pr_err("Failed to revert profile for handler %s\n",
>>>>> handler->name);
>>>>> +			}
>>>>> +			return err;
>>>>> +		}
>>>>>    	}
>>>>>
>>>>> -	err = cur_profile->profile_set(cur_profile, i);
>>>>> -	if (!err)
>>>>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>>> -
>>>>> -	mutex_unlock(&profile_lock);
>>>>> -	if (err)
>>>>> -		return err;
>>>>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>>>    	return count;
>>>>>    }
>>>>>
>>>>> @@ -140,7 +148,8 @@ static const struct attribute_group
>>>>> platform_profile_group = {
>>>>>
>>>>>    void platform_profile_notify(void)
>>>>>    {
>>>>> -	if (!cur_profile)
>>>>> +	guard(mutex)(&profile_lock);
>>>>> +	if (!platform_profile_is_registered())
>>>>>    		return;
>>>>>    	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>>>    }
>>>>> @@ -148,40 +157,65 @@ EXPORT_SYMBOL_GPL(platform_profile_notify);
>>>>>
>>>>>    int platform_profile_cycle(void)
>>>>>    {
>>>>> +	struct platform_profile_handler *handler;
>>>>>    	enum platform_profile_option profile;
>>>>> -	enum platform_profile_option next;
>>>>> +	enum platform_profile_option next = PLATFORM_PROFILE_LAST;
>>>>> +	enum platform_profile_option next2 = PLATFORM_PROFILE_LAST;
>>>>>    	int err;
>>>>>
>>>>> -	err = mutex_lock_interruptible(&profile_lock);
>>>>> -	if (err)
>>>>> -		return err;
>>>>> -
>>>>> -	if (!cur_profile) {
>>>>> -		mutex_unlock(&profile_lock);
>>>>> -		return -ENODEV;
>>>>> -	}
>>>>> -
>>>>> -	err = cur_profile->profile_get(cur_profile, &profile);
>>>>> -	if (err) {
>>>>> -		mutex_unlock(&profile_lock);
>>>>> -		return err;
>>>>> -	}
>>>>> -
>>>>> -	next = find_next_bit_wrap(cur_profile->choices, PLATFORM_PROFILE_LAST,
>>>>> -				  profile + 1);
>>>>> -
>>>>> -	if (WARN_ON(next == PLATFORM_PROFILE_LAST)) {
>>>>> -		mutex_unlock(&profile_lock);
>>>>> -		return -EINVAL;
>>>>> +	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) {
>>>>> +		/* first pass, make sure all handlers agree on the definition of
>>>>> "next" profile */
>>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>>> +
>>>>> +			err = handler->profile_get(handler, &profile);
>>>>> +			if (err)
>>>>> +				return err;
>>>>> +
>>>>> +			if (next == PLATFORM_PROFILE_LAST)
>>>>> +				next = find_next_bit_wrap(handler->choices,
>>>>> +							  PLATFORM_PROFILE_LAST,
>>>>> +							  profile + 1);
>>>>> +			else
>>>>> +				next2 = find_next_bit_wrap(handler->choices,
>>>>> +							   PLATFORM_PROFILE_LAST,
>>>>> +							   profile + 1);
>>>>> +
>>>>> +			if (WARN_ON(next == PLATFORM_PROFILE_LAST))
>>>>> +				return -EINVAL;
>>>>> +
>>>>> +			if (next2 == PLATFORM_PROFILE_LAST)
>>>>> +				continue;
>>>>> +
>>>>> +			if (next != next2) {
>>>>> +				pr_warn("Next profile to cycle to is ambiguous between
>>>>> platform_profile handlers\n");
>>>>> +				return -EINVAL;
>>>>> +			}
>>>>> +			next = next2;
>>>>> +		}
>>>>> +
>>>>> +		/*
>>>>> +		 * Second pass: apply "next" to each handler
>>>>> +		 * If any failures occur unwind and revert all back to the original
>>>>> profile
>>>>> +		 */
>>>>> +		list_for_each_entry(handler, &platform_profile_handler_list, list) {
>>>>> +			err = handler->profile_set(handler, next);
>>>>> +			if (err) {
>>>>> +				pr_err("Failed to set profile for handler %s\n", handler->name);
>>>>> +				break;
>>>>> +			}
>>>>> +		}
>>>>> +		if (err) {
>>>>> +			list_for_each_entry_continue_reverse(handler,
>>>>> &platform_profile_handler_list, list) {
>>>>> +				err = handler->profile_set(handler, profile);
>>>>> +				if (err)
>>>>> +					pr_err("Failed to revert profile for handler %s\n",
>>>>> handler->name);
>>>>> +			}
>>>>> +		}
>>>>>    	}
>>>>>
>>>>> -	err = cur_profile->profile_set(cur_profile, next);
>>>>> -	mutex_unlock(&profile_lock);
>>>>> -
>>>>> -	if (!err)
>>>>> -		sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>>> +	sysfs_notify(acpi_kobj, NULL, "platform_profile");
>>>>>
>>>>> -	return err;
>>>>> +	return 0;
>>>>>    }
>>>>>    EXPORT_SYMBOL_GPL(platform_profile_cycle);
>>>>>
>>>>> @@ -190,21 +224,19 @@ int platform_profile_register(struct
>>>>> platform_profile_handler *pprof)
>>>>>    	int err;
>>>>>
>>>>>    	guard(mutex)(&profile_lock);
>>>>> -	/* We can only have one active profile */
>>>>> -	if (cur_profile)
>>>>> -		return -EEXIST;
>>>>>
>>>>>    	/* Sanity check the profile handler field are set */
>>>>>    	if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
>>>>>    		!pprof->profile_set || !pprof->profile_get)
>>>>>    		return -EINVAL;
>>>>>
>>>>> -	err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>>>>> -	if (err)
>>>>> -		return err;
>>>>> +	if (!platform_profile_is_registered()) {
>>>>> +		err = sysfs_create_group(acpi_kobj, &platform_profile_group);
>>>>> +		if (err)
>>>>> +			return err;
>>>>> +	}
>>>>>    	list_add_tail(&pprof->list, &platform_profile_handler_list);
>>>>>
>>>>> -	cur_profile = pprof;
>>>>>    	return 0;
>>>>>    }
>>>>>    EXPORT_SYMBOL_GPL(platform_profile_register);
>>>>> @@ -215,7 +247,6 @@ int platform_profile_remove(struct
>>>>> platform_profile_handler *pprof)
>>>>>
>>>>>    	list_del(&pprof->list);
>>>>>
>>>>> -	cur_profile = NULL;
>>>>>    	if (!platform_profile_is_registered())
>>>>>    		sysfs_remove_group(acpi_kobj, &platform_profile_group);
>>>>>
>>>>> -- 
>>>>> 2.43.0
>>>>
>>>> I'm still going thru the code changes - but I'm a bit unsure on the implementation itself.
>>>
>>> FYI, I split it up in v2 to make each chunk and intent behind it more
>>> manageable to review instead of patch 7 being "so" big.
>>>
>>> V2 covers some of the points below as well based on some feedback from
>>> Hans and Armin.
>>>
>> 
>> Ack - sorry for that, on PTO this week/last week and not checking email very often. Saw the v2 after I sent the comments on v1 (my inbox is a little bit out of control right now).
>> I've just started looking thru that - let me know if better to move that conversation there
>> 
>>>>
>>>> I'd expect that one of the advantages of having different profile handlers register is that you could support extra & new profiles that might be wanted. For example the recent discussion of the AMD handler providing better tools to tweak advanced system settings for gaming etc. Won't this approach limit that? You'll only be able to have common settings.
>>>
>>> Well that RFC it turns out won't really be scalable because SPS is done
>>> differently in AMD Strix and newer.  I haven't revisited it yet.
>>>
>>> But yes this approach would conceptually limit that idea because common
>>> settings are all that is presented.
>>>
>>>>
>>>> I find having a common profile and two different handlers a bit tricky on how to handle. My concern is it can easily lead to conflict in settings.
>>>> If two handlers are doing different operations to provide the same effect - then neither handler is (probably) providing what they think is required. With your CPU vs EC example, the EC will often set CPU clock thresholds and the CPU profile handler will be changing that. If this is done I think it should be explicit to the user (admittedly I'm doing this with my Lenovo hat on - but we certify our platforms with our EC profile handler)
>>>>
>>>> I could see providing two separate handlers. e.g. balanced-A and balanced-B (for driver-A and driver-B) and the user maybe choosing which one they want (or both - though the user interface for that is definitely tricky)
>>>> But choosing one option for two different drivers seems confusing and with unknown side-effects. I appreciate it's complicated by your example wanting to add CPU and EC - I know how much work you've been doing on the AMD CPU front which benefits all systems.
>>>>
>>>
>>> Thinking through your comments I guess another way to approach this
>>> would be "per-driver" sysfs knobs.  Here's my thought.
>>>
>>> 1) /sys/firmware/acpi/platform_profile_choices would contain only things
>>> that are common and if there is something NOT common then also the
>>> string "custom".
>>>
>>> 2) /sys/firmware/acpi/platform_profile would accept writes for
>>> everything in platform profile choices except "custom".
>>>
>>> 3) Each driver handler would also export it's own sysfs files to
>>> represent the driver state.
>>>
>>> 3) If the user changed the main knob at
>>> /sys/firmware/acpi/platform_profile then it would change all driver
>>> handlers.
>>>
>>> 4) If the user changed sysfs for any driver individually then the main
>>> knob /sys/firmware/acpi/platform_profile would export "custom".
>> 
>> I need to think about it a bit more - I still have some concerns about two drivers doing the same thing. In some cases they can complement each other nicely, but in other cases they will treat on each others toes.
>> 
>> Just to throw another idea on the pile:
>> 
>>   - If only one profile then it can have low-power, balanced, performance as currently
>>   - If two or more profiles, belonging as an example to driverA and driverB, their profile names become balanced-driverA and balanced-driverB. You could choose specifically if you wanted to activate one of them by using the driver name
>>   - Each driver would have a priority. I'm biased here, but a vendor platform driver would have priority 1, a CPU vendor driver priority 2, etc. This would be used to determine which driver version would be used for the cases where multiple options are present and user chooses, for example, just balanced. If two drivers of the same priority compete then first driver loaded wins.
>> 
>> So as an example: if we have the case where we have thinkpad_acpi (priority 1) and amd (priority 2) profile drivers offering all three profiles then:
>> 
>> /sys/blah/platform_profile_choices would offer:
>> lower-power, balanced, performance, low-power-thinkpad_acpi, balanced-thinkpad_acpi, performance-thinkpad_acpi, low-power-amd, balanced-amd, performance-amd.
>> 
>> If the user chooses balanced - it would activate the balanced-thinkpad_acpi profile as it has the higher priority.
>> The user could then also activate balanced-amd separately if they choose. Multi balanced profiles are OK.
>> 
>> Initially this would all be from command line, but I'm sure some nice user space GUI can be done to allow you to choose which profiles are available and active/in-active.
>> 
>> Maybe also add an option under platform-profiles to have an "enable all matching profiles" to override the priority implementation? In this case choosing balanced would enable all balanced profiles available (in priority order?)
>> 
>> Would that work for the ASUS case or not?
>
> At least for the ASUS case "today" I think "priority" could work, but 
> what happens if one day ASUS prefers to use amd-pmf instead of the 
> custom WMI stuff for some systems?  IMO you don't want to have it set in 
> stone for priorities.
>
At that point they can submit a patch to set their priority according to their needs...as it only impacts their machines that's totally their choice how they want to do it.
But (see below notes), I think I withdraw the idea, it is unnecessarily messy.

> I do worry that generally though users won't know what they want if 
> there is a "balanced", "balanced-vendor" and "balanced-cpu".
>

fair

> And I don't think we can use the same interface to select multiple at 
> once.  I think if we're to have multiple handlers exposed and allow 
> different options then it needs to be a class interface (or something 
> similar).
>
Yeah - saw the class proposal after sending this. That probably makes sense and is simpler.

Still not sure how that solve the problem with multiple drivers offering the same profile and treading on each other. 

I've been through various arguments in my head and whilst I am selfishly concerned about causing issues and supporting users, I suspect the reality is it will work out just fine and I'm over-thinking it and the profiles will play nicely together.

Can we have a module argument so we can set a preferred profile driver, for cases where things go wrong? e.g platform_profile.use_only=thinkpad-acpi (as an example). 
Just so if there are issues it's easy to debug if a particular profile handler is causing the problem. These changes are going to make debugging profile issues more complicated :)

>> 
>>>
>>> Hans what do you think?
>>>
>> 
>>>> Another concern - would this mean that another driver could limit the options available? For instance if someone wrote a new 'mega-turbo' only profile driver and it loaded - it would then mean no profiles were available for anything as no profiles matched?
>>>
>>> Yes.  I don't think it's a problem in practice right now (as we only
>>> just recently have two drivers vying for this position), but it /could/
>>> be something that happens.
>>>
>> 
>> I'd certainly like to leave the window open so that there could be, for instance, a 'gaming' profile option that does some high-performance mode with maybe power shifting towards the GPU and some other graphics only tuning things?
>> I wouldn't like to shut down the ability to have options for things like that.
>> 
>
> It comes down to how much of a catch all this knob needs to be in the 
> kernel.  If you put too much into the single knob it doesn't become 
> useful anymore generically.
>
> At least for AMD APU + AMD dGPU designs there already exists a bias knob 
> that lets you control the power share.
>
> https://docs.kernel.org/gpu/amdgpu/driver-misc.html#gpu-smartshift-information
>
> Similarly you can manually adjust clocks and performance profiles for 
> GPU from standard files:
>
> https://docs.kernel.org/gpu/amdgpu/thermal.html#gpu-sysfs-power-state-interfaces
>
> So my gut says this idea of gaming mode should be stuck to userspace. 
> There is already such a project for that:
>
> https://github.com/FeralInteractive/gamemode
> 
The gaming profile was just an example, but after more consideration, I think you're right that user space makes more sense for these cases, and similar others.

Mark