HID: allocation neatening and code size reduction
Subsystems (7)
cover HID: allocation neatening and code size reduction
Joe Perches (2):
HID: Use krealloc
HID: Remove unnecessary OOM messages
drivers/hid/hid-a4tech.c | 4 +--- drivers/hid/hid-apple.c | 4 +--- drivers/hid/hid-asus.c | 5 +---- drivers/hid/hid-core.c | 24 +++++++++++------------- drivers/hid/hid-lenovo.c | 7 ++----- drivers/hid/hid-lg.c | 5 ++--- drivers/hid/hid-lg4ff.c | 4 +--- drivers/hid/hid-logitech-dj.c | 12 +++--------- drivers/hid/hid-magicmouse.c | 4 +--- drivers/hid/hid-multitouch.c | 9 +++------ drivers/hid/hid-ntrig.c | 4 +--- drivers/hid/hid-picolcd_core.c | 1 - drivers/hid/hid-picolcd_leds.c | 1 - drivers/hid/hid-prodikeys.c | 5 +---- drivers/hid/hid-rmi.c | 4 +--- drivers/hid/hid-roccat-arvo.c | 5 ++--- drivers/hid/hid-roccat-isku.c | 5 ++--- drivers/hid/hid-roccat-koneplus.c | 5 ++--- drivers/hid/hid-roccat-konepure.c | 5 ++--- drivers/hid/hid-roccat-kovaplus.c | 5 ++--- drivers/hid/hid-roccat-lua.c | 5 ++--- drivers/hid/hid-roccat-pyra.c | 5 ++--- drivers/hid/hid-roccat-ryos.c | 5 ++--- drivers/hid/hid-roccat-savu.c | 5 ++--- drivers/hid/hid-saitek.c | 4 +--- drivers/hid/hid-sensor-hub.c | 6 +----- drivers/hid/hid-sony.c | 1 - 27 files changed, 49 insertions(+), 100 deletions(-)
Signature · 1 line
2.10.0.rc2.1.g053435c
PATCH 2/2 HID: Remove unnecessary OOM messages
Allocation failures from k.alloc and dev_k.alloc calls already have a generic stack dump and these additional logging messages just make the code larger and the allocations more likely to fail.
Remove them.
--- drivers/hid/hid-a4tech.c | 4 +--- drivers/hid/hid-apple.c | 4 +--- drivers/hid/hid-asus.c | 5 +---- drivers/hid/hid-lenovo.c | 7 ++----- drivers/hid/hid-lg.c | 5 ++--- drivers/hid/hid-lg4ff.c | 4 +--- drivers/hid/hid-logitech-dj.c | 12 +++--------- drivers/hid/hid-magicmouse.c | 4 +--- drivers/hid/hid-multitouch.c | 9 +++------ drivers/hid/hid-ntrig.c | 4 +--- drivers/hid/hid-picolcd_core.c | 1 - drivers/hid/hid-picolcd_leds.c | 1 - drivers/hid/hid-prodikeys.c | 5 +---- drivers/hid/hid-rmi.c | 4 +--- drivers/hid/hid-roccat-arvo.c | 5 ++--- drivers/hid/hid-roccat-isku.c | 5 ++--- drivers/hid/hid-roccat-koneplus.c | 5 ++--- drivers/hid/hid-roccat-konepure.c | 5 ++--- drivers/hid/hid-roccat-kovaplus.c | 5 ++--- drivers/hid/hid-roccat-lua.c | 5 ++--- drivers/hid/hid-roccat-pyra.c | 5 ++--- drivers/hid/hid-roccat-ryos.c | 5 ++--- drivers/hid/hid-roccat-savu.c | 5 ++--- drivers/hid/hid-saitek.c | 4 +--- drivers/hid/hid-sensor-hub.c | 6 +----- drivers/hid/hid-sony.c | 1 - 26 files changed, 38 insertions(+), 87 deletions(-)
drivers/hid/hid-a4tech.c +1 -3
@@ -91,10 +91,8 @@ static int a4_probe(struct hid_device *hdev, const struct hid_device_id *id)int ret; a4 =devm_kzalloc (&hdev->dev,sizeof (*a4),GFP_KERNEL ); -if (a4 ==NULL ) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (a4 ==NULL )return -ENOMEM ; - } a4->quirks = id->driver_data;
drivers/hid/hid-apple.c +1 -3
@@ -372,10 +372,8 @@ static int apple_probe(struct hid_device *hdev,int ret; asc =devm_kzalloc (&hdev->dev,sizeof (*asc),GFP_KERNEL ); -if (asc ==NULL ) { -hid_err (hdev,"can't alloc apple descriptor\n" ); +if (asc ==NULL )return -ENOMEM ; - } asc->quirks = quirks;
drivers/hid/hid-asus.c +1 -4
@@ -224,7 +224,6 @@ static int asus_start_multitouch(struct hid_device *hdev)if (!dmabuf) { ret = -ENOMEM ; -hid_err (hdev,"Asus failed to alloc dma buf: %d\n" , ret);return ret; }
@@ -257,10 +256,8 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)struct asus_drvdata *drvdata; drvdata =devm_kzalloc (&hdev->dev,sizeof (*drvdata),GFP_KERNEL ); -if (drvdata ==NULL ) { -hid_err (hdev,"Can't alloc Asus descriptor\n" ); +if (drvdata ==NULL )return -ENOMEM ; - }hid_set_drvdata (hdev, drvdata);
drivers/hid/hid-lenovo.c +2 -5
@@ -688,7 +688,6 @@ static int lenovo_probe_tpkbd(struct hid_device *hdev)sizeof (struct lenovo_drvdata_tpkbd ), GFP_KERNEL);if (data_pointer ==NULL ) { -hid_err (hdev,"Could not allocate memory for driver data\n" ); ret = -ENOMEM ; goto err; }
@@ -700,7 +699,6 @@ static int lenovo_probe_tpkbd(struct hid_device *hdev) name_mute =devm_kzalloc (&hdev->dev, name_sz,GFP_KERNEL ); name_micmute =devm_kzalloc (&hdev->dev, name_sz,GFP_KERNEL );if (name_mute ==NULL || name_micmute ==NULL ) { -hid_err (hdev,"Could not allocate memory for led data\n" ); ret = -ENOMEM ; goto err; }
@@ -746,10 +744,9 @@ static int lenovo_probe_cptkbd(struct hid_device *hdev) cptkbd_data =devm_kzalloc (&hdev->dev,sizeof (*cptkbd_data),GFP_KERNEL ); -if (cptkbd_data ==NULL ) { -hid_err (hdev,"can't alloc keyboard descriptor\n" ); +if (cptkbd_data ==NULL )return -ENOMEM ; - } +hid_set_drvdata (hdev, cptkbd_data);/*
drivers/hid/hid-lg.c +2 -3
@@ -728,10 +728,9 @@ static int lg_probe(struct hid_device *hdev, const struct hid_device_id *id) } drv_data =kzalloc (sizeof (struct lg_drv_data ),GFP_KERNEL ); -if (!drv_data) { -hid_err (hdev,"Insufficient memory, cannot allocate driver data\n" ); +if (!drv_data)return -ENOMEM ; - } + drv_data->quirks = id->driver_data;hid_set_drvdata (hdev, (void *)drv_data);
drivers/hid/hid-lg4ff.c +1 -3
@@ -1403,10 +1403,8 @@ int lg4ff_init(struct hid_device *hid)for (j =0 ; j <5 ; j++) { led =kzalloc (sizeof (struct led_classdev )+name_sz,GFP_KERNEL ); -if (!led) { -hid_err (hid,"can't allocate memory for LED %d\n" , j); +if (!led) goto err_leds; - } name = (void *)(&led[1 ]);snprintf (name, name_sz,"%s::RPM%d" ,dev_name (&hid->dev), j+1 );
drivers/hid/hid-logitech-dj.c +3 -9
@@ -418,12 +418,8 @@ static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,strlcat (dj_hiddev->phys, tmpstr,sizeof (dj_hiddev->phys)); dj_dev =kzalloc (sizeof (struct dj_device ),GFP_KERNEL ); - -if (!dj_dev) { -dev_err (&djrcv_hdev->dev,"%s: failed allocating dj_device\n" , - __func__); +if (!dj_dev) goto dj_device_allocate_fail; - } dj_dev->reports_supported =get_unaligned_le32 ( dj_report->report_params +DEVICE_PAIRED_RF_REPORT_TYPE );
@@ -1008,11 +1004,9 @@ static int logi_dj_probe(struct hid_device *hdev,/* Treat interface 2 */ djrcv_dev =kzalloc (sizeof (struct dj_receiver_dev ),GFP_KERNEL ); -if (!djrcv_dev) { -dev_err (&hdev->dev, -"%s:failed allocating dj_receiver_dev\n" , __func__); +if (!djrcv_dev)return -ENOMEM ; - } + djrcv_dev->hdev = hdev;INIT_WORK (&djrcv_dev->work, delayedwork_callback);spin_lock_init (&djrcv_dev->lock);
drivers/hid/hid-magicmouse.c +1 -3
@@ -500,10 +500,8 @@ static int magicmouse_probe(struct hid_device *hdev,int ret; msc =devm_kzalloc (&hdev->dev,sizeof (*msc),GFP_KERNEL ); -if (msc ==NULL ) { -hid_err (hdev,"can't alloc magicmouse descriptor\n" ); +if (msc ==NULL )return -ENOMEM ; - } msc->scroll_accel =SCROLL_ACCEL_DEFAULT ;
drivers/hid/hid-multitouch.c +3 -6
@@ -1128,10 +1128,9 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id) } td =devm_kzalloc (&hdev->dev,sizeof (struct mt_device ),GFP_KERNEL ); -if (!td) { -dev_err (&hdev->dev,"cannot allocate multitouch data\n" ); +if (!td)return -ENOMEM ; - } + td->mtclass = *mtclass; td->inputmode =-1 ; td->maxcontact_report_id =-1 ;
@@ -1142,10 +1141,8 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id) td->fields =devm_kzalloc (&hdev->dev,sizeof (struct mt_fields ),GFP_KERNEL ); -if (!td->fields) { -dev_err (&hdev->dev,"cannot allocate multitouch fields data\n" ); +if (!td->fields)return -ENOMEM ; - }if (id->vendor ==HID_ANY_ID && id->product ==HID_ANY_ID ) td->serial_maybe = true;
drivers/hid/hid-ntrig.c +1 -3
@@ -905,10 +905,8 @@ static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id) | HID_QUIRK_NO_INIT_REPORTS; nd =kmalloc (sizeof (struct ntrig_data ),GFP_KERNEL ); -if (!nd) { -hid_err (hdev,"cannot allocate N-Trig data\n" ); +if (!nd)return -ENOMEM ; - } nd->reading_mt =0 ; nd->min_width =0 ;
drivers/hid/hid-picolcd_core.c +0 -1
@@ -549,7 +549,6 @@ static int picolcd_probe(struct hid_device *hdev,*/ data =kzalloc (sizeof (struct picolcd_data ),GFP_KERNEL );if (data ==NULL ) { -hid_err (hdev,"can't allocate space for Minibox PicoLCD device data\n" ); error = -ENOMEM ; goto err_no_cleanup; }
drivers/hid/hid-picolcd_leds.c +0 -1
@@ -122,7 +122,6 @@ int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)for (i =0 ; i <8 ; i++) { led =kzalloc (sizeof (struct led_classdev )+name_sz,GFP_KERNEL );if (!led) { -dev_err (dev,"can't allocate memory for LED %d\n" , i); ret = -ENOMEM ; goto err; }
drivers/hid/hid-prodikeys.c +1 -4
@@ -802,16 +802,13 @@ static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id)struct pcmidi_snd *pm =NULL ; pk =kzalloc (sizeof (*pk),GFP_KERNEL ); -if (pk ==NULL ) { -hid_err (hdev,"can't alloc descriptor\n" ); +if (pk ==NULL )return -ENOMEM ; - } pk->hdev = hdev; pm =kzalloc (sizeof (*pm),GFP_KERNEL );if (pm ==NULL ) { -hid_err (hdev,"can't alloc descriptor\n" ); ret = -ENOMEM ; goto err_free_pk; }
drivers/hid/hid-rmi.c +1 -3
@@ -666,10 +666,8 @@ static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id) alloc_size = data->output_report_size + data->input_report_size; data->writeReport =devm_kzalloc (&hdev->dev, alloc_size,GFP_KERNEL ); -if (!data->writeReport) { -hid_err (hdev,"failed to allocate buffer for HID reports\n" ); +if (!data->writeReport)return -ENOMEM ; - } data->readReport = data->writeReport + data->output_report_size;
drivers/hid/hid-roccat-arvo.c +2 -3
@@ -300,10 +300,9 @@ static int arvo_init_specials(struct hid_device *hdev) } arvo =kzalloc (sizeof (*arvo),GFP_KERNEL ); -if (!arvo) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!arvo)return -ENOMEM ; - } +hid_set_drvdata (hdev, arvo); retval =arvo_init_arvo_device_struct (usb_dev, arvo);
drivers/hid/hid-roccat-isku.c +2 -3
@@ -280,10 +280,9 @@ static int isku_init_specials(struct hid_device *hdev) } isku =kzalloc (sizeof (*isku),GFP_KERNEL ); -if (!isku) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!isku)return -ENOMEM ; - } +hid_set_drvdata (hdev, isku); retval =isku_init_isku_device_struct (usb_dev, isku);
drivers/hid/hid-roccat-koneplus.c +2 -3
@@ -385,10 +385,9 @@ static int koneplus_init_specials(struct hid_device *hdev) == USB_INTERFACE_PROTOCOL_MOUSE) { koneplus =kzalloc (sizeof (*koneplus),GFP_KERNEL ); -if (!koneplus) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!koneplus)return -ENOMEM ; - } +hid_set_drvdata (hdev, koneplus); retval =koneplus_init_koneplus_device_struct (usb_dev, koneplus);
drivers/hid/hid-roccat-konepure.c +2 -3
@@ -89,10 +89,9 @@ static int konepure_init_specials(struct hid_device *hdev) } konepure =kzalloc (sizeof (*konepure),GFP_KERNEL ); -if (!konepure) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!konepure)return -ENOMEM ; - } +hid_set_drvdata (hdev, konepure); retval =roccat_common2_device_init_struct (usb_dev, konepure);
drivers/hid/hid-roccat-kovaplus.c +2 -3
@@ -452,10 +452,9 @@ static int kovaplus_init_specials(struct hid_device *hdev) == USB_INTERFACE_PROTOCOL_MOUSE) { kovaplus =kzalloc (sizeof (*kovaplus),GFP_KERNEL ); -if (!kovaplus) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!kovaplus)return -ENOMEM ; - } +hid_set_drvdata (hdev, kovaplus); retval =kovaplus_init_kovaplus_device_struct (usb_dev, kovaplus);
drivers/hid/hid-roccat-lua.c +2 -3
@@ -123,10 +123,9 @@ static int lua_init_specials(struct hid_device *hdev)int retval; lua =kzalloc (sizeof (*lua),GFP_KERNEL ); -if (!lua) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!lua)return -ENOMEM ; - } +hid_set_drvdata (hdev, lua); retval =lua_init_lua_device_struct (usb_dev, lua);
drivers/hid/hid-roccat-pyra.c +2 -3
@@ -404,10 +404,9 @@ static int pyra_init_specials(struct hid_device *hdev) == USB_INTERFACE_PROTOCOL_MOUSE) { pyra =kzalloc (sizeof (*pyra),GFP_KERNEL ); -if (!pyra) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!pyra)return -ENOMEM ; - } +hid_set_drvdata (hdev, pyra); retval =pyra_init_pyra_device_struct (usb_dev, pyra);
drivers/hid/hid-roccat-ryos.c +2 -3
@@ -97,10 +97,9 @@ static int ryos_init_specials(struct hid_device *hdev) } ryos =kzalloc (sizeof (*ryos),GFP_KERNEL ); -if (!ryos) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!ryos)return -ENOMEM ; - } +hid_set_drvdata (hdev, ryos); retval =roccat_common2_device_init_struct (usb_dev, ryos);
drivers/hid/hid-roccat-savu.c +2 -3
@@ -69,10 +69,9 @@ static int savu_init_specials(struct hid_device *hdev) } savu =kzalloc (sizeof (*savu),GFP_KERNEL ); -if (!savu) { -hid_err (hdev,"can't alloc device descriptor\n" ); +if (!savu)return -ENOMEM ; - } +hid_set_drvdata (hdev, savu); retval =roccat_common2_device_init_struct (usb_dev, savu);
drivers/hid/hid-saitek.c +1 -3
@@ -45,10 +45,8 @@ static int saitek_probe(struct hid_device *hdev,int ret; ssc =devm_kzalloc (&hdev->dev,sizeof (*ssc),GFP_KERNEL ); -if (ssc ==NULL ) { -hid_err (hdev,"can't alloc saitek descriptor\n" ); +if (ssc ==NULL )return -ENOMEM ; - } ssc->quirks = quirks; ssc->mode =-1 ;
drivers/hid/hid-sensor-hub.c +1 -5
@@ -640,10 +640,8 @@ static int sensor_hub_probe(struct hid_device *hdev,struct hid_sensor_hub_device *collection_hsdev =NULL ; sd =devm_kzalloc (&hdev->dev,sizeof (*sd),GFP_KERNEL ); -if (!sd) { -hid_err (hdev,"cannot allocate Sensor data\n" ); +if (!sd)return -ENOMEM ; - }hid_set_drvdata (hdev, sd); sd->quirks = id->driver_data;
@@ -676,7 +674,6 @@ static int sensor_hub_probe(struct hid_device *hdev,sizeof (struct mfd_cell ), GFP_KERNEL);if (sd->hid_sensor_hub_client_devs ==NULL ) { -hid_err (hdev,"Failed to allocate memory for mfd cells\n" ); ret = -ENOMEM ; goto err_stop_hw; }
@@ -690,7 +687,6 @@ static int sensor_hub_probe(struct hid_device *hdev, hsdev =devm_kzalloc (&hdev->dev,sizeof (*hsdev),GFP_KERNEL );if (!hsdev) { -hid_err (hdev,"cannot allocate hid_sensor_hub_device\n" ); ret = -ENOMEM ; goto err_stop_hw; }
drivers/hid/hid-sony.c +0 -1
@@ -1950,7 +1950,6 @@ static int sony_leds_init(struct sony_sc *sc) led =kzalloc (sizeof (struct led_classdev ) + name_sz,GFP_KERNEL );if (!led) { -hid_err (hdev,"Couldn't allocate memory for LED %d\n" , n); ret = -ENOMEM ; goto error_leds; }
Signature · 1 line
2.10.0.rc2.1.g053435c
PATCH 1/2 HID: Use krealloc
In reply to Joe PerchesThis kmalloc/memcpy is equivalent to krealloc, so use krealloc to avoid a possibly unnecessary memcpy.
Miscellanea:
o Add temporary variables osize and nsize to improve readability
o krealloc has a defect and does not support GFP_ZERO so the memset must be kept
--- drivers/hid/hid-core.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
drivers/hid/hid-core.c +11 -13
@@ -131,21 +131,19 @@ static int open_collection(struct hid_parser *parser, unsigned type) }if (parser->device->maxcollection == parser->device->collection_size) { - collection =kmalloc (sizeof (struct hid_collection ) * - parser->device->collection_size *2 ,GFP_KERNEL ); -if (collection ==NULL ) { -hid_err (parser->device,"failed to reallocate collection array\n" ); +unsigned int osize = parser->device->collection_size; +unsigned int nsize = osize *2 ; + + collection =krealloc (parser->device->collection, + nsize *sizeof (struct hid_collection ), +GFP_KERNEL ); +if (!collection)return -ENOMEM ; - } -memcpy (collection, parser->device->collection, -sizeof (struct hid_collection ) * - parser->device->collection_size); -memset (collection + parser->device->collection_size,0 , -sizeof (struct hid_collection ) * - parser->device->collection_size); -kfree (parser->device->collection); + +memset (collection + osize,0 , +sizeof (struct hid_collection ) * osize); parser->device->collection = collection; - parser->device->collection_size *=2 ; + parser->device->collection_size = nsize; } parser->collection_stack[parser->collection_stack_ptr++] =
Signature · 1 line
2.10.0.rc2.1.g053435c
Re: [PATCH 2/2] HID: Remove unnecessary OOM messages
In reply to Joe Perchesdrivers/hid/hid-asus.c static int asus_start_multitouch(struct hid_device *hdev) · line 228
Quoted 11 lines · Joe Perches
drivers/hid/hid-asus.c +0
-1@@ -224,7 +224,6 @@ static int asus_start_multitouch(struct hid_device *hdev)if (!dmabuf) { ret = -ENOMEM ; -hid_err (hdev,"Asus failed to alloc dma buf: %d\n" , ret);return ret; }
Trivial set and return value missed.
Re: [PATCH 2/2] HID: Remove unnecessary OOM messages
Quoted 2 lines · Michel Hermier
drivers/hid/hid-asus.c
Quoted 1 line · Joe Perches
drivers/hid/hid-asus.c +0
-0
[]
Quoted 9 lines
drivers/hid/hid-asus.c static int asus_start_multitouch(struct hid_device *hdev) · line 228
Quoted 7 lines
drivers/hid/hid-asus.c +0
-1@@ -224,7 +224,6 @@ static int asus_start_multitouch(struct hid_device *hdev)if (!dmabuf) { ret = -ENOMEM ; -hid_err (hdev,"Asus failed to alloc dma buf: %d\n" , ret);return ret; }Trivial set and return value missed.
You're welcome to submit a follow-on patch.
The compiler should produce the same code
in any case.