[2/3] platform/x86: wmi-bmof: Simplify read_bmof()

Message ID 20230730043817.12888-2-W_Armin@gmx.de
State New
Headers
Series [1/3] platform/x86: wmi-bmof: Use device_create_bin_file() |

Commit Message

Armin Wolf July 30, 2023, 4:38 a.m. UTC
  Replace offset handling code with a single call
to memory_read_from_buffer() to simplify read_bmof().

Tested on a ASUS PRIME B650-PLUS.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
---
 drivers/platform/x86/wmi-bmof.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)

--
2.39.2
  

Comments

Thomas Weißschuh July 30, 2023, 7:10 a.m. UTC | #1
On 2023-07-30 06:38:16+0200, Armin Wolf wrote:
> Replace offset handling code with a single call
> to memory_read_from_buffer() to simplify read_bmof().
> 
> Tested on a ASUS PRIME B650-PLUS.
> 
> Signed-off-by: Armin Wolf <W_Armin@gmx.de>

Reviewed-by: Thomas Weißschuh <linux@weissschuh.net>
Tested-by: Thomas Weißschuh <linux@weissschuh.net>

> ---
>  drivers/platform/x86/wmi-bmof.c | 22 +++++-----------------
>  1 file changed, 5 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/platform/x86/wmi-bmof.c b/drivers/platform/x86/wmi-bmof.c
> index d0516cacfcb5..644d2fd889c0 100644
> --- a/drivers/platform/x86/wmi-bmof.c
> +++ b/drivers/platform/x86/wmi-bmof.c
> @@ -25,25 +25,13 @@ struct bmof_priv {
>  	struct bin_attribute bmof_bin_attr;
>  };
> 
> -static ssize_t
> -read_bmof(struct file *filp, struct kobject *kobj,
> -	 struct bin_attribute *attr,
> -	 char *buf, loff_t off, size_t count)
> +static ssize_t read_bmof(struct file *filp, struct kobject *kobj, struct bin_attribute *attr,
> +			 char *buf, loff_t off, size_t count)
>  {
> -	struct bmof_priv *priv =
> -		container_of(attr, struct bmof_priv, bmof_bin_attr);
> +	struct bmof_priv *priv = container_of(attr, struct bmof_priv, bmof_bin_attr);
> 
> -	if (off < 0)
> -		return -EINVAL;
> -
> -	if (off >= priv->bmofdata->buffer.length)
> -		return 0;
> -
> -	if (count > priv->bmofdata->buffer.length - off)
> -		count = priv->bmofdata->buffer.length - off;
> -
> -	memcpy(buf, priv->bmofdata->buffer.pointer + off, count);
> -	return count;
> +	return memory_read_from_buffer(buf, count, &off, priv->bmofdata->buffer.pointer,
> +				       priv->bmofdata->buffer.length);
>  }
> 
>  static int wmi_bmof_probe(struct wmi_device *wdev, const void *context)
> --
> 2.39.2
>
  
Thomas Weißschuh July 30, 2023, 7:35 a.m. UTC | #2
On 2023-07-30 06:38:16+0200, Armin Wolf wrote:
> Replace offset handling code with a single call
> to memory_read_from_buffer() to simplify read_bmof().
> 
> Tested on a ASUS PRIME B650-PLUS.
> 
> Signed-off-by: Armin Wolf <W_Armin@gmx.de>
> ---
>  drivers/platform/x86/wmi-bmof.c | 22 +++++-----------------
>  1 file changed, 5 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/platform/x86/wmi-bmof.c b/drivers/platform/x86/wmi-bmof.c
> index d0516cacfcb5..644d2fd889c0 100644
> --- a/drivers/platform/x86/wmi-bmof.c
> +++ b/drivers/platform/x86/wmi-bmof.c
> @@ -25,25 +25,13 @@ struct bmof_priv {
>  	struct bin_attribute bmof_bin_attr;
>  };
> 
> -static ssize_t
> -read_bmof(struct file *filp, struct kobject *kobj,
> -	 struct bin_attribute *attr,
> -	 char *buf, loff_t off, size_t count)
> +static ssize_t read_bmof(struct file *filp, struct kobject *kobj, struct bin_attribute *attr,
> +			 char *buf, loff_t off, size_t count)
>  {
> -	struct bmof_priv *priv =
> -		container_of(attr, struct bmof_priv, bmof_bin_attr);
> +	struct bmof_priv *priv = container_of(attr, struct bmof_priv, bmof_bin_attr);
> 
> -	if (off < 0)
> -		return -EINVAL;
> -
> -	if (off >= priv->bmofdata->buffer.length)
> -		return 0;
> -
> -	if (count > priv->bmofdata->buffer.length - off)
> -		count = priv->bmofdata->buffer.length - off;
> -
> -	memcpy(buf, priv->bmofdata->buffer.pointer + off, count);
> -	return count;
> +	return memory_read_from_buffer(buf, count, &off, priv->bmofdata->buffer.pointer,
> +				       priv->bmofdata->buffer.length);

Note: sysfs_kf_bin_read() seems also to be doing most of this validation.
So the invalid input values, expect for negative "off", can't happen anyways.
memory_read_from_buffer() is still nice though.

>  }
> 
>  static int wmi_bmof_probe(struct wmi_device *wdev, const void *context)
> --
> 2.39.2
>
  

Patch

diff --git a/drivers/platform/x86/wmi-bmof.c b/drivers/platform/x86/wmi-bmof.c
index d0516cacfcb5..644d2fd889c0 100644
--- a/drivers/platform/x86/wmi-bmof.c
+++ b/drivers/platform/x86/wmi-bmof.c
@@ -25,25 +25,13 @@  struct bmof_priv {
 	struct bin_attribute bmof_bin_attr;
 };

-static ssize_t
-read_bmof(struct file *filp, struct kobject *kobj,
-	 struct bin_attribute *attr,
-	 char *buf, loff_t off, size_t count)
+static ssize_t read_bmof(struct file *filp, struct kobject *kobj, struct bin_attribute *attr,
+			 char *buf, loff_t off, size_t count)
 {
-	struct bmof_priv *priv =
-		container_of(attr, struct bmof_priv, bmof_bin_attr);
+	struct bmof_priv *priv = container_of(attr, struct bmof_priv, bmof_bin_attr);

-	if (off < 0)
-		return -EINVAL;
-
-	if (off >= priv->bmofdata->buffer.length)
-		return 0;
-
-	if (count > priv->bmofdata->buffer.length - off)
-		count = priv->bmofdata->buffer.length - off;
-
-	memcpy(buf, priv->bmofdata->buffer.pointer + off, count);
-	return count;
+	return memory_read_from_buffer(buf, count, &off, priv->bmofdata->buffer.pointer,
+				       priv->bmofdata->buffer.length);
 }

 static int wmi_bmof_probe(struct wmi_device *wdev, const void *context)