diff mbox series

[1/3] hwmon: (oxp-sensors): remove static board variable

Message ID 20230704131715.44454-6-gregkh@linuxfoundation.org (mailing list archive)
State Changes Requested
Headers show
Series driver core: remove final user of devm_device_add_groups() | expand

Commit Message

Greg Kroah-Hartman July 4, 2023, 1:17 p.m. UTC
Drivers should not have a single static variable for the type of device
they are bound to.  While this driver is really going to only have one
device at a time in the system, remove the static variable and instead,
look up the device type when needed.

Cc: Joaquín Ignacio Aramendía <samsagax@gmail.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: linux-hwmon@vger.kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/hwmon/oxp-sensors.c | 47 ++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 22 deletions(-)

Comments

Guenter Roeck July 4, 2023, 1:39 p.m. UTC | #1
On 7/4/23 06:17, Greg Kroah-Hartman wrote:
> Drivers should not have a single static variable for the type of device
> they are bound to.  While this driver is really going to only have one
> device at a time in the system, remove the static variable and instead,
> look up the device type when needed.
> 

This is expensive. I think it would be much better to just move
the board type detection into the init code and not instantiate
the driver in the fist place if the board type is unknown.

We can handle the static variable separately if it really bothers
you that much.

> Cc: Joaquín Ignacio Aramendía <samsagax@gmail.com>
> Cc: Guenter Roeck <linux@roeck-us.net>
> Cc: linux-hwmon@vger.kernel.org
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
>   drivers/hwmon/oxp-sensors.c | 47 ++++++++++++++++++++-----------------
>   1 file changed, 25 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/hwmon/oxp-sensors.c b/drivers/hwmon/oxp-sensors.c
> index e1a907cae820..3bcba0c476c4 100644
> --- a/drivers/hwmon/oxp-sensors.c
> +++ b/drivers/hwmon/oxp-sensors.c
> @@ -48,10 +48,9 @@ enum oxp_board {
>   	oxp_mini_amd,
>   	oxp_mini_amd_a07,
>   	oxp_mini_amd_pro,
> +	UNKNOWN,
>   };
>   
> -static enum oxp_board board;
> -
>   /* Fan reading and PWM */
>   #define OXP_SENSOR_FAN_REG		0x76 /* Fan reading is 2 registers long */
>   #define OXP_SENSOR_PWM_ENABLE_REG	0x4A /* PWM enable is 1 register long */
> @@ -136,6 +135,24 @@ static const struct dmi_system_id dmi_table[] = {
>   	{},
>   };
>   
> +static enum oxp_board get_board_type(void)
> +{
> +	const struct dmi_system_id *dmi_entry;
> +
> +	/*
> +	 * Have to check for AMD processor here because DMI strings are the
> +	 * same between Intel and AMD boards, the only way to tell them apart
> +	 * is the CPU.
> +	 * Intel boards seem to have different EC registers and values to
> +	 * read/write.
> +	 */
> +	dmi_entry = dmi_first_match(dmi_table);
> +	if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
> +		return UNKNOWN;
> +
> +	return (enum oxp_board)(unsigned long)dmi_entry->driver_data;
> +}
> +
>   /* Helper functions to handle EC read/write */
>   static int read_from_ec(u8 reg, int size, long *val)
>   {
> @@ -182,7 +199,7 @@ static int tt_toggle_enable(void)
>   	u8 reg;
>   	u8 val;
>   
> -	switch (board) {
> +	switch (get_board_type()) {
>   	case oxp_mini_amd_a07:
>   		reg = OXP_OLD_TURBO_SWITCH_REG;
>   		val = OXP_OLD_TURBO_TAKE_VAL;
> @@ -203,7 +220,7 @@ static int tt_toggle_disable(void)
>   	u8 reg;
>   	u8 val;
>   
> -	switch (board) {
> +	switch (get_board_type()) {
>   	case oxp_mini_amd_a07:
>   		reg = OXP_OLD_TURBO_SWITCH_REG;
>   		val = OXP_OLD_TURBO_RETURN_VAL;
> @@ -249,7 +266,7 @@ static ssize_t tt_toggle_show(struct device *dev,
>   	u8 reg;
>   	long val;
>   
> -	switch (board) {
> +	switch (get_board_type()) {
>   	case oxp_mini_amd_a07:
>   		reg = OXP_OLD_TURBO_SWITCH_REG;
>   		break;
> @@ -315,7 +332,7 @@ static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
>   			ret = read_from_ec(OXP_SENSOR_PWM_REG, 1, val);
>   			if (ret)
>   				return ret;
> -			switch (board) {
> +			switch (get_board_type()) {
>   			case aya_neo_2:
>   			case aya_neo_air:
>   			case aya_neo_air_pro:
> @@ -357,7 +374,7 @@ static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
>   		case hwmon_pwm_input:
>   			if (val < 0 || val > 255)
>   				return -EINVAL;
> -			switch (board) {
> +			switch (get_board_type()) {
>   			case aya_neo_2:
>   			case aya_neo_air:
>   			case aya_neo_air_pro:
> @@ -412,25 +429,11 @@ static const struct hwmon_chip_info oxp_ec_chip_info = {
>   /* Initialization logic */
>   static int oxp_platform_probe(struct platform_device *pdev)
>   {
> -	const struct dmi_system_id *dmi_entry;
>   	struct device *dev = &pdev->dev;
>   	struct device *hwdev;
>   	int ret;
>   
> -	/*
> -	 * Have to check for AMD processor here because DMI strings are the
> -	 * same between Intel and AMD boards, the only way to tell them apart
> -	 * is the CPU.
> -	 * Intel boards seem to have different EC registers and values to
> -	 * read/write.
> -	 */
> -	dmi_entry = dmi_first_match(dmi_table);
> -	if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
> -		return -ENODEV;
> -
> -	board = (enum oxp_board)(unsigned long)dmi_entry->driver_data;
> -
> -	switch (board) {
> +	switch (get_board_type()) {

This now always registers the hwmon device, which really should not happen.

Guenter

>   	case aok_zoe_a1:
>   	case oxp_mini_amd_a07:
>   	case oxp_mini_amd_pro:
Greg Kroah-Hartman July 4, 2023, 1:44 p.m. UTC | #2
On Tue, Jul 04, 2023 at 06:39:07AM -0700, Guenter Roeck wrote:
> On 7/4/23 06:17, Greg Kroah-Hartman wrote:
> > Drivers should not have a single static variable for the type of device
> > they are bound to.  While this driver is really going to only have one
> > device at a time in the system, remove the static variable and instead,
> > look up the device type when needed.
> > 
> 
> This is expensive. I think it would be much better to just move
> the board type detection into the init code and not instantiate
> the driver in the fist place if the board type is unknown.

The board type detection is all over the place in the driver, it's not
just for "unknown" types, so how about just saving the board type at
probe time and using it then for all other places?

> We can handle the static variable separately if it really bothers
> you that much.

I did this change to make patch 2/3 more "obvious" what is happening
when the in_visible() callback happens, so that you don't have to worry
about the saved value or not.  But this whole patch isn't really needed
if you don't mind the lookup just happening in the in_visible() callback
for the first time.

thanks,

greg k-h
Guenter Roeck July 4, 2023, 2:14 p.m. UTC | #3
On 7/4/23 06:44, Greg Kroah-Hartman wrote:
> On Tue, Jul 04, 2023 at 06:39:07AM -0700, Guenter Roeck wrote:
>> On 7/4/23 06:17, Greg Kroah-Hartman wrote:
>>> Drivers should not have a single static variable for the type of device
>>> they are bound to.  While this driver is really going to only have one
>>> device at a time in the system, remove the static variable and instead,
>>> look up the device type when needed.
>>>
>>
>> This is expensive. I think it would be much better to just move
>> the board type detection into the init code and not instantiate
>> the driver in the fist place if the board type is unknown.
> 
> The board type detection is all over the place in the driver, it's not
> just for "unknown" types, so how about just saving the board type at
> probe time and using it then for all other places?
> 

I must be missing something. The current code detects the board type
only once, in the probe function. Otherwise the static variable is used.
You are replacing it with repeated calls to get_board_type().
The whole point of the static variable is to avoid the cost of repeated
calls to dmi_first_match().

>> We can handle the static variable separately if it really bothers
>> you that much.
> 
> I did this change to make patch 2/3 more "obvious" what is happening
> when the in_visible() callback happens, so that you don't have to worry
> about the saved value or not.  But this whole patch isn't really needed
> if you don't mind the lookup just happening in the in_visible() callback
> for the first time.
> 

That would at least be a minimal change, and just add one extra lookup
which is only called once (or zero, if it is used to save the board type).

As I said, my solution would be to move the board type detection
into the init function and not instantiate the driver in the first
place if the probe function would bail out anyway. Personally I'd keep
the static variable for simplicity, but if you really dislike it
that much, we could pass it around in platform and later driver data.
But it seems to me that this could (and should) be a separate patch
that doesn't have to be hurried in.

Thanks,
Guenter
Greg Kroah-Hartman July 4, 2023, 4:14 p.m. UTC | #4
On Tue, Jul 04, 2023 at 07:14:54AM -0700, Guenter Roeck wrote:
> On 7/4/23 06:44, Greg Kroah-Hartman wrote:
> > On Tue, Jul 04, 2023 at 06:39:07AM -0700, Guenter Roeck wrote:
> > > On 7/4/23 06:17, Greg Kroah-Hartman wrote:
> > > > Drivers should not have a single static variable for the type of device
> > > > they are bound to.  While this driver is really going to only have one
> > > > device at a time in the system, remove the static variable and instead,
> > > > look up the device type when needed.
> > > > 
> > > 
> > > This is expensive. I think it would be much better to just move
> > > the board type detection into the init code and not instantiate
> > > the driver in the fist place if the board type is unknown.
> > 
> > The board type detection is all over the place in the driver, it's not
> > just for "unknown" types, so how about just saving the board type at
> > probe time and using it then for all other places?
> > 
> 
> I must be missing something. The current code detects the board type
> only once, in the probe function. Otherwise the static variable is used.
> You are replacing it with repeated calls to get_board_type().
> The whole point of the static variable is to avoid the cost of repeated
> calls to dmi_first_match().

Ah, ok, yes, I was refering to the fact that the driver relies on the
detection of the device type in lots of different places (and doesn't
ever error out from the detection call.)

> > > We can handle the static variable separately if it really bothers
> > > you that much.
> > 
> > I did this change to make patch 2/3 more "obvious" what is happening
> > when the in_visible() callback happens, so that you don't have to worry
> > about the saved value or not.  But this whole patch isn't really needed
> > if you don't mind the lookup just happening in the in_visible() callback
> > for the first time.
> > 
> 
> That would at least be a minimal change, and just add one extra lookup
> which is only called once (or zero, if it is used to save the board type).

Ok, I'll switch it up, but really, it's just a simple table lookup loop,
and none of the detection calls are on a "hot path" that I can
determine.  Or am I missing something?

> As I said, my solution would be to move the board type detection
> into the init function and not instantiate the driver in the first
> place if the probe function would bail out anyway.

That's not the case today, the only way the probe function would fail
today is if the registering of the sysfs files fail.  It does not matter
if the board detection call passes or not.

> Personally I'd keep
> the static variable for simplicity, but if you really dislike it
> that much, we could pass it around in platform and later driver data.
> But it seems to me that this could (and should) be a separate patch
> that doesn't have to be hurried in.

Fair enough, let me rework this and resend a v2 series when I get a
chance soon.

thanks for the review.

greg k-h
Guenter Roeck July 4, 2023, 4:43 p.m. UTC | #5
On 7/4/23 09:14, Greg Kroah-Hartman wrote:
> On Tue, Jul 04, 2023 at 07:14:54AM -0700, Guenter Roeck wrote:
>> On 7/4/23 06:44, Greg Kroah-Hartman wrote:
>>> On Tue, Jul 04, 2023 at 06:39:07AM -0700, Guenter Roeck wrote:
>>>> On 7/4/23 06:17, Greg Kroah-Hartman wrote:
>>>>> Drivers should not have a single static variable for the type of device
>>>>> they are bound to.  While this driver is really going to only have one
>>>>> device at a time in the system, remove the static variable and instead,
>>>>> look up the device type when needed.
>>>>>
>>>>
>>>> This is expensive. I think it would be much better to just move
>>>> the board type detection into the init code and not instantiate
>>>> the driver in the fist place if the board type is unknown.
>>>
>>> The board type detection is all over the place in the driver, it's not
>>> just for "unknown" types, so how about just saving the board type at
>>> probe time and using it then for all other places?
>>>
>>
>> I must be missing something. The current code detects the board type
>> only once, in the probe function. Otherwise the static variable is used.
>> You are replacing it with repeated calls to get_board_type().
>> The whole point of the static variable is to avoid the cost of repeated
>> calls to dmi_first_match().
> 
> Ah, ok, yes, I was refering to the fact that the driver relies on the
> detection of the device type in lots of different places (and doesn't
> ever error out from the detection call.)
> 

I am lost again. Current code:

         dmi_entry = dmi_first_match(dmi_table);
         if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
                 return -ENODEV;



>>>> We can handle the static variable separately if it really bothers
>>>> you that much.
>>>
>>> I did this change to make patch 2/3 more "obvious" what is happening
>>> when the in_visible() callback happens, so that you don't have to worry
>>> about the saved value or not.  But this whole patch isn't really needed
>>> if you don't mind the lookup just happening in the in_visible() callback
>>> for the first time.
>>>
>>
>> That would at least be a minimal change, and just add one extra lookup
>> which is only called once (or zero, if it is used to save the board type).
> 
> Ok, I'll switch it up, but really, it's just a simple table lookup loop,
> and none of the detection calls are on a "hot path" that I can
> determine.  Or am I missing something?
> 
>> As I said, my solution would be to move the board type detection
>> into the init function and not instantiate the driver in the first
>> place if the probe function would bail out anyway.
> 
> That's not the case today, the only way the probe function would fail
> today is if the registering of the sysfs files fail.  It does not matter
> if the board detection call passes or not.
> 

Again,
         dmi_entry = dmi_first_match(dmi_table);
         if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
                 return -ENODEV;
                 ^^^^^^^^^^^^^^^

What am I missing ?

Guenter
Greg Kroah-Hartman July 4, 2023, 4:52 p.m. UTC | #6
On Tue, Jul 04, 2023 at 09:43:39AM -0700, Guenter Roeck wrote:
> On 7/4/23 09:14, Greg Kroah-Hartman wrote:
> > On Tue, Jul 04, 2023 at 07:14:54AM -0700, Guenter Roeck wrote:
> > > On 7/4/23 06:44, Greg Kroah-Hartman wrote:
> > > > On Tue, Jul 04, 2023 at 06:39:07AM -0700, Guenter Roeck wrote:
> > > > > On 7/4/23 06:17, Greg Kroah-Hartman wrote:
> > > > > > Drivers should not have a single static variable for the type of device
> > > > > > they are bound to.  While this driver is really going to only have one
> > > > > > device at a time in the system, remove the static variable and instead,
> > > > > > look up the device type when needed.
> > > > > > 
> > > > > 
> > > > > This is expensive. I think it would be much better to just move
> > > > > the board type detection into the init code and not instantiate
> > > > > the driver in the fist place if the board type is unknown.
> > > > 
> > > > The board type detection is all over the place in the driver, it's not
> > > > just for "unknown" types, so how about just saving the board type at
> > > > probe time and using it then for all other places?
> > > > 
> > > 
> > > I must be missing something. The current code detects the board type
> > > only once, in the probe function. Otherwise the static variable is used.
> > > You are replacing it with repeated calls to get_board_type().
> > > The whole point of the static variable is to avoid the cost of repeated
> > > calls to dmi_first_match().
> > 
> > Ah, ok, yes, I was refering to the fact that the driver relies on the
> > detection of the device type in lots of different places (and doesn't
> > ever error out from the detection call.)
> > 
> 
> I am lost again. Current code:
> 
>         dmi_entry = dmi_first_match(dmi_table);
>         if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
>                 return -ENODEV;
> 
> 
> 
> > > > > We can handle the static variable separately if it really bothers
> > > > > you that much.
> > > > 
> > > > I did this change to make patch 2/3 more "obvious" what is happening
> > > > when the in_visible() callback happens, so that you don't have to worry
> > > > about the saved value or not.  But this whole patch isn't really needed
> > > > if you don't mind the lookup just happening in the in_visible() callback
> > > > for the first time.
> > > > 
> > > 
> > > That would at least be a minimal change, and just add one extra lookup
> > > which is only called once (or zero, if it is used to save the board type).
> > 
> > Ok, I'll switch it up, but really, it's just a simple table lookup loop,
> > and none of the detection calls are on a "hot path" that I can
> > determine.  Or am I missing something?
> > 
> > > As I said, my solution would be to move the board type detection
> > > into the init function and not instantiate the driver in the first
> > > place if the probe function would bail out anyway.
> > 
> > That's not the case today, the only way the probe function would fail
> > today is if the registering of the sysfs files fail.  It does not matter
> > if the board detection call passes or not.
> > 
> 
> Again,
>         dmi_entry = dmi_first_match(dmi_table);
>         if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
>                 return -ENODEV;
>                 ^^^^^^^^^^^^^^^
> 
> What am I missing ?

Nothing, I'm missing it, sorry.  Been a long day, let me redo this...

greg k-h
Joaquín Ignacio Aramendía July 4, 2023, 7:44 p.m. UTC | #7
Hello.

>
> On 7/4/23 06:17, Greg Kroah-Hartman wrote:
> > Drivers should not have a single static variable for the type of device
> > they are bound to.  While this driver is really going to only have one
> > device at a time in the system, remove the static variable and instead,
> > look up the device type when needed.
> >
>
> This is expensive. I think it would be much better to just move
> the board type detection into the init code and not instantiate
> the driver in the fist place if the board type is unknown.

I meant to do this, since the probe function is called only once and
is meant for hotpluggable things. But since it works as it is and only
loads in supported hardware it didn't seem necessary.
Modify what you see fit. And thanks for the attention to the quality
of the driver.

Joaquín
diff mbox series

Patch

diff --git a/drivers/hwmon/oxp-sensors.c b/drivers/hwmon/oxp-sensors.c
index e1a907cae820..3bcba0c476c4 100644
--- a/drivers/hwmon/oxp-sensors.c
+++ b/drivers/hwmon/oxp-sensors.c
@@ -48,10 +48,9 @@  enum oxp_board {
 	oxp_mini_amd,
 	oxp_mini_amd_a07,
 	oxp_mini_amd_pro,
+	UNKNOWN,
 };
 
-static enum oxp_board board;
-
 /* Fan reading and PWM */
 #define OXP_SENSOR_FAN_REG		0x76 /* Fan reading is 2 registers long */
 #define OXP_SENSOR_PWM_ENABLE_REG	0x4A /* PWM enable is 1 register long */
@@ -136,6 +135,24 @@  static const struct dmi_system_id dmi_table[] = {
 	{},
 };
 
+static enum oxp_board get_board_type(void)
+{
+	const struct dmi_system_id *dmi_entry;
+
+	/*
+	 * Have to check for AMD processor here because DMI strings are the
+	 * same between Intel and AMD boards, the only way to tell them apart
+	 * is the CPU.
+	 * Intel boards seem to have different EC registers and values to
+	 * read/write.
+	 */
+	dmi_entry = dmi_first_match(dmi_table);
+	if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
+		return UNKNOWN;
+
+	return (enum oxp_board)(unsigned long)dmi_entry->driver_data;
+}
+
 /* Helper functions to handle EC read/write */
 static int read_from_ec(u8 reg, int size, long *val)
 {
@@ -182,7 +199,7 @@  static int tt_toggle_enable(void)
 	u8 reg;
 	u8 val;
 
-	switch (board) {
+	switch (get_board_type()) {
 	case oxp_mini_amd_a07:
 		reg = OXP_OLD_TURBO_SWITCH_REG;
 		val = OXP_OLD_TURBO_TAKE_VAL;
@@ -203,7 +220,7 @@  static int tt_toggle_disable(void)
 	u8 reg;
 	u8 val;
 
-	switch (board) {
+	switch (get_board_type()) {
 	case oxp_mini_amd_a07:
 		reg = OXP_OLD_TURBO_SWITCH_REG;
 		val = OXP_OLD_TURBO_RETURN_VAL;
@@ -249,7 +266,7 @@  static ssize_t tt_toggle_show(struct device *dev,
 	u8 reg;
 	long val;
 
-	switch (board) {
+	switch (get_board_type()) {
 	case oxp_mini_amd_a07:
 		reg = OXP_OLD_TURBO_SWITCH_REG;
 		break;
@@ -315,7 +332,7 @@  static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
 			ret = read_from_ec(OXP_SENSOR_PWM_REG, 1, val);
 			if (ret)
 				return ret;
-			switch (board) {
+			switch (get_board_type()) {
 			case aya_neo_2:
 			case aya_neo_air:
 			case aya_neo_air_pro:
@@ -357,7 +374,7 @@  static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
 		case hwmon_pwm_input:
 			if (val < 0 || val > 255)
 				return -EINVAL;
-			switch (board) {
+			switch (get_board_type()) {
 			case aya_neo_2:
 			case aya_neo_air:
 			case aya_neo_air_pro:
@@ -412,25 +429,11 @@  static const struct hwmon_chip_info oxp_ec_chip_info = {
 /* Initialization logic */
 static int oxp_platform_probe(struct platform_device *pdev)
 {
-	const struct dmi_system_id *dmi_entry;
 	struct device *dev = &pdev->dev;
 	struct device *hwdev;
 	int ret;
 
-	/*
-	 * Have to check for AMD processor here because DMI strings are the
-	 * same between Intel and AMD boards, the only way to tell them apart
-	 * is the CPU.
-	 * Intel boards seem to have different EC registers and values to
-	 * read/write.
-	 */
-	dmi_entry = dmi_first_match(dmi_table);
-	if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
-		return -ENODEV;
-
-	board = (enum oxp_board)(unsigned long)dmi_entry->driver_data;
-
-	switch (board) {
+	switch (get_board_type()) {
 	case aok_zoe_a1:
 	case oxp_mini_amd_a07:
 	case oxp_mini_amd_pro: