[PATCH HID v2 02/11] HID: core: save one kmemdup during .probe()

Benjamin Tissoires posted 11 patches 2 months, 2 weeks ago
There is a newer version of this series
[PATCH HID v2 02/11] HID: core: save one kmemdup during .probe()
Posted by Benjamin Tissoires 2 months, 2 weeks ago
Turns out the first kmemdup is only required for the .report_fixup()
driver callback. There is no need to do two kmemdup() in a raw in case
.report_fixup() is not present.

Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>

---

new in v2
---
 drivers/hid/hid-core.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index a5f5415571cb..172746a082f9 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1206,7 +1206,7 @@ int hid_open_report(struct hid_device *device)
 	struct hid_item item;
 	unsigned int size;
 	const __u8 *start;
-	__u8 *buf;
+	__u8 *buf = NULL;
 	const __u8 *end;
 	const __u8 *next;
 	int ret;
@@ -1227,14 +1227,18 @@ int hid_open_report(struct hid_device *device)
 	if (WARN_ON(!start))
 		return -ENODEV;
 
-	buf = kmemdup(start, size, GFP_KERNEL);
-	if (buf == NULL)
-		return -ENOMEM;
+	if (device->driver->report_fixup) {
+		/*
+		 * device->driver->report_fixup() needs to work
+		 * on a copy of our report descriptor so it can
+		 * change it.
+		 */
+		buf = kmemdup(start, size, GFP_KERNEL);
+		if (buf == NULL)
+			return -ENOMEM;
 
-	if (device->driver->report_fixup)
 		start = device->driver->report_fixup(device, buf, &size);
-	else
-		start = buf;
+	}
 
 	start = kmemdup(start, size, GFP_KERNEL);
 	kfree(buf);

-- 
2.46.0
Re: [PATCH HID v2 02/11] HID: core: save one kmemdup during .probe()
Posted by Peter Hutterer 2 months, 2 weeks ago
On Tue, Sep 10, 2024 at 11:43:38PM +0900, Benjamin Tissoires wrote:
> Turns out the first kmemdup is only required for the .report_fixup()
> driver callback. There is no need to do two kmemdup() in a raw in case

typo: "in a row"?

Cheers,
  Peter

> .report_fixup() is not present.
> 
> Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
> 
> ---
> 
> new in v2
> ---
>  drivers/hid/hid-core.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
> index a5f5415571cb..172746a082f9 100644
> --- a/drivers/hid/hid-core.c
> +++ b/drivers/hid/hid-core.c
> @@ -1206,7 +1206,7 @@ int hid_open_report(struct hid_device *device)
>  	struct hid_item item;
>  	unsigned int size;
>  	const __u8 *start;
> -	__u8 *buf;
> +	__u8 *buf = NULL;
>  	const __u8 *end;
>  	const __u8 *next;
>  	int ret;
> @@ -1227,14 +1227,18 @@ int hid_open_report(struct hid_device *device)
>  	if (WARN_ON(!start))
>  		return -ENODEV;
>  
> -	buf = kmemdup(start, size, GFP_KERNEL);
> -	if (buf == NULL)
> -		return -ENOMEM;
> +	if (device->driver->report_fixup) {
> +		/*
> +		 * device->driver->report_fixup() needs to work
> +		 * on a copy of our report descriptor so it can
> +		 * change it.
> +		 */
> +		buf = kmemdup(start, size, GFP_KERNEL);
> +		if (buf == NULL)
> +			return -ENOMEM;
>  
> -	if (device->driver->report_fixup)
>  		start = device->driver->report_fixup(device, buf, &size);
> -	else
> -		start = buf;
> +	}
>  
>  	start = kmemdup(start, size, GFP_KERNEL);
>  	kfree(buf);
> 
> -- 
> 2.46.0
>