Skip to contentSkip to conversation

media: dvb-usb: break long strings in dvb_usb_device_init

PATCH Superseded Dongliang Mu 3 participants 6 messages Jun 21, 2021 – Jul 25, 2021 Media (V4L/DVB)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 media: dvb-usb: break long strings in dvb_usb_device_init

---
 drivers/media/usb/dvb-usb/dvb-usb-init.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/media/usb/dvb-usb/dvb-usb-init.c +5 -3
index 28e1fd64dd3c..edc477cedaa9 100644
@@ -286,13 +286,15 @@ int dvb_usb_device_init(struct usb_interface *intf,
 
 	desc = dvb_usb_find_device(udev, &d->props, &cold);
 	if (!desc) {
-		deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
+		deb_err("something went very wrong,
+			 device was not found in current device list.\n");
 		ret = -ENODEV;
 		goto error;
 	}
 
 	if (cold) {
-		info("found a '%s' in cold state, will try to load a firmware", desc->name);
+		info("found a %s in cold state, will try to load a firmware",
+		     desc->name);
 		ret = dvb_usb_download_firmware(udev, props);
 		if (!props->no_reconnect || ret != 0)
 			goto error;
@@ -314,7 +316,7 @@ int dvb_usb_device_init(struct usb_interface *intf,
 	if (du)
 		*du = d;
 
-	info("%s successfully initialized and connected.", desc->name);
+	info("%s is successfully initialized and connected.", desc->name);
 	return 0;
 
  error:
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/3 media: dvb-usb: move kfree(d) to dvb_usb_device_exit

As d is allocated in dvb_usb_device_init, so move to the deallocation of d from dvb_usb_exit to dvb_usb_device_exit

---
 drivers/media/usb/dvb-usb/dvb-usb-init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/dvb-usb/dvb-usb-init.c +2 -2
index edc477cedaa9..21ad51be4820 100644
@@ -147,7 +147,6 @@ static int dvb_usb_exit(struct dvb_usb_device *d)
 		d->props.priv_destroy(d);
 
 	kfree(d->priv);
-	kfree(d);
 	return 0;
 }
 
@@ -333,9 +332,10 @@ void dvb_usb_device_exit(struct usb_interface *intf)
 	char name[40];
 
 	usb_set_intfdata(intf, NULL);
-	if (d != NULL && d->desc != NULL) {
+	if (d && d->desc) {
 		strscpy(name, d->desc->name, sizeof(name));
 		dvb_usb_exit(d);
+		kfree(d);
 	} else {
 		strscpy(name, default_name, sizeof(name));
 	}
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 2/3] media: dvb-usb: move kfree(d) to dvb_usb_device_exit

Quoted 2 lines · Dongliang Mu

As d is allocated in dvb_usb_device_init, so move to the deallocation of d from dvb_usb_exit to dvb_usb_device_exit

There is nothing wrong with this patch, however I'm not sure what this improves though. The code is just as good/messy as before, is it not?

Sean

Quoted 32 lines
---
 drivers/media/usb/dvb-usb/dvb-usb-init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/dvb-usb/dvb-usb-init.c +2 -2
index edc477cedaa9..21ad51be4820 100644
@@ -147,7 +147,6 @@ static int dvb_usb_exit(struct dvb_usb_device *d)
 		d->props.priv_destroy(d);
 
 	kfree(d->priv);
-	kfree(d);
 	return 0;
 }
 
@@ -333,9 +332,10 @@ void dvb_usb_device_exit(struct usb_interface *intf)
 	char name[40];
 
 	usb_set_intfdata(intf, NULL);
-	if (d != NULL && d->desc != NULL) {
+	if (d && d->desc) {
 		strscpy(name, d->desc->name, sizeof(name));
 		dvb_usb_exit(d);
+		kfree(d);
 	} else {
 		strscpy(name, default_name, sizeof(name));
 	}
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/3 media: dvb-usb: Fix error handling in dvb_usb_i2c_init

In reply to Dongliang Mu

In dvb_usb_i2c_init, if i2c_add_adapter fails, it only prints an error message, and then continues to set DVB_USB_STATE_I2C. This affects the logic of dvb_usb_i2c_exit, which leads to that, the deletion of i2c_adap even if the i2c_add_adapter fails.

Fix this by returning at the failure of i2c_add_adapter and then move dvb_usb_i2c_exit out of the error handling code of dvb_usb_i2c_init.

Fixes 13a79f14ab28 ("media: dvb-usb: Fix memory leak at error in dvb_usb_device_init()")
---
 drivers/media/usb/dvb-usb/dvb-usb-i2c.c  | 9 +++++++--
 drivers/media/usb/dvb-usb/dvb-usb-init.c | 3 ++-
 2 files changed, 9 insertions(+), 3 deletions(-)
drivers/media/usb/dvb-usb/dvb-usb-i2c.c +7 -2
index 2e07106f4680..bc4b2abdde1a 100644
@@ -17,7 +17,8 @@ int dvb_usb_i2c_init(struct dvb_usb_device *d)
 
 	if (d->props.i2c_algo == NULL) {
 		err("no i2c algorithm specified");
-		return -EINVAL;
+		ret = -EINVAL;
+		goto err;
 	}
 
 	strscpy(d->i2c_adap.name, d->desc->name, sizeof(d->i2c_adap.name));
@@ -27,11 +28,15 @@ int dvb_usb_i2c_init(struct dvb_usb_device *d)
 
 	i2c_set_adapdata(&d->i2c_adap, d);
 
-	if ((ret = i2c_add_adapter(&d->i2c_adap)) < 0)
+	ret = i2c_add_adapter(&d->i2c_adap);
+	if (ret < 0) {
 		err("could not add i2c adapter");
+		goto err;
+	}
 
 	d->state |= DVB_USB_STATE_I2C;
 
+err:
 	return ret;
 }
 
drivers/media/usb/dvb-usb/dvb-usb-init.c +2 -1
index 21ad51be4820..ae3f76787279 100644
@@ -147,6 +147,7 @@ static int dvb_usb_exit(struct dvb_usb_device *d)
 		d->props.priv_destroy(d);
 
 	kfree(d->priv);
+	d->priv = NULL;
 	return 0;
 }
 
@@ -193,8 +194,8 @@ static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
 
 err_adapter_init:
 	dvb_usb_adapter_exit(d);
-err_i2c_init:
 	dvb_usb_i2c_exit(d);
+err_i2c_init:
 	if (d->priv && d->props.priv_destroy)
 		d->props.priv_destroy(d);
 err_priv_init:
Signature · 1 line
2.25.1
#4 lore raw
In reply to Dongliang Mu
Thank you for the patch! Perhaps something to improve:

Hi Dongliang,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linuxtv-media/master]
[also build test WARNING on v5.13-rc7 next-20210618]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Dongliang-Mu/media-dvb-usb-break-long-strings-in-dvb_usb_device_init/20210621-130906
base:   git://linuxtv.org/media_tree.git master
config: x86_64-randconfig-a012-20210621 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project e1adf90826a57b674eee79b071fb46c1f5683cd0)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/2cb920d86e9a83188dc0c72083640ca03e580a33
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Dongliang-Mu/media-dvb-usb-break-long-strings-in-dvb_usb_device_init/20210621-130906 git checkout 2cb920d86e9a83188dc0c72083640ca03e580a33
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> drivers/media/usb/dvb-usb/dvb-usb-init.c:289:11: warning: missing terminating '"' character [-Winvalid-pp-token]
                   deb_err("something went very wrong,
                           ^
   drivers/media/usb/dvb-usb/dvb-usb-init.c:290:51: warning: missing terminating '"' character [-Winvalid-pp-token]
                            device was not found in current device list.\n");
                                                                          ^
   drivers/media/usb/dvb-usb/dvb-usb-init.c:289:3: error: unterminated function-like macro invocation
                   deb_err("something went very wrong,
                   ^
   drivers/media/usb/dvb-usb/dvb-usb-common.h:22:9: note: macro 'deb_err' defined here
   #define deb_err(args...)   dprintk(dvb_usb_debug,0x010,args)
           ^
   drivers/media/usb/dvb-usb/dvb-usb-init.c:350:23: error: expected '}'
   MODULE_LICENSE("GPL");
                         ^
   drivers/media/usb/dvb-usb/dvb-usb-init.c:288:13: note: to match this '{'
           if (!desc) {
                      ^
   drivers/media/usb/dvb-usb/dvb-usb-init.c:350:23: error: expected '}'
   MODULE_LICENSE("GPL");
                         ^

drivers/media/usb/dvb-usb/dvb-usb-init.c:269:1: note: to match this '{' {
^
2 warnings and 3 errors generated.

vim +289 drivers/media/usb/dvb-usb/dvb-usb-init.c

261
262 /*
263 * USB
264 */
265 int dvb_usb_device_init(struct usb_interface *intf,
266 const struct dvb_usb_device_properties *props,
267 struct module *owner, struct dvb_usb_device **du,
268 short *adapter_nums)
269 {
270 struct usb_device *udev = interface_to_usbdev(intf);
271 struct dvb_usb_device *d = NULL;
272 const struct dvb_usb_device_description *desc = NULL;
273
274 int ret = -ENOMEM, cold = 0;
275
276 if (du != NULL)
277 *du = NULL;
278
279 d = kzalloc(sizeof(*d), GFP_KERNEL);
280 if (!d) {
281 err("no memory for 'struct dvb_usb_device'");
282 return -ENOMEM;
283 }
284
285 memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
286
287 desc = dvb_usb_find_device(udev, &d->props, &cold);
288 if (!desc) {

 > 289			deb_err("something went very wrong,

290 device was not found in current device list.\n");
291 ret = -ENODEV;
292 goto error;
293 }
294
295 if (cold) {
296 info("found a %s in cold state, will try to load a firmware",
297 desc->name);
298 ret = dvb_usb_download_firmware(udev, props);
299 if (!props->no_reconnect || ret != 0)
300 goto error;
301 }
302
303 info("found a '%s' in warm state.", desc->name);
304 d->udev = udev;
305 d->desc = desc;
306 d->owner = owner;
307
308 usb_set_intfdata(intf, d);
309
310 ret = dvb_usb_init(d, adapter_nums);
311 if (ret) {
312 info("%s error while loading driver (%d)", desc->name, ret);
313 goto error;
314 }
315
316 if (du)
317 *du = d;
318
319 info("%s is successfully initialized and connected.", desc->name);
320 return 0;
321
322 error:
323 usb_set_intfdata(intf, NULL);
324 kfree(d);
325 return ret;
326 }
327 EXPORT_SYMBOL(dvb_usb_device_init);
328

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw
In reply to Dongliang Mu
Thank you for the patch! Yet something to improve:

Hi Dongliang,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linuxtv-media/master]
[also build test ERROR on v5.13-rc7 next-20210618]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Dongliang-Mu/media-dvb-usb-break-long-strings-in-dvb_usb_device_init/20210621-130906
base:   git://linuxtv.org/media_tree.git master
config: arm64-randconfig-r014-20210621 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project e1adf90826a57b674eee79b071fb46c1f5683cd0)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/2cb920d86e9a83188dc0c72083640ca03e580a33
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Dongliang-Mu/media-dvb-usb-break-long-strings-in-dvb_usb_device_init/20210621-130906 git checkout 2cb920d86e9a83188dc0c72083640ca03e580a33
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

drivers/media/usb/dvb-usb/dvb-usb-init.c:289:11: warning: missing terminating '"' character [-Winvalid-pp-token]
deb_err("something went very wrong,
^
drivers/media/usb/dvb-usb/dvb-usb-init.c:290:51: warning: missing terminating '"' character [-Winvalid-pp-token]

                            device was not found in current device list.\n");
                                                                          ^
>> drivers/media/usb/dvb-usb/dvb-usb-init.c:289:3: error: unterminated function-like macro invocation
                   deb_err("something went very wrong,
                   ^
   drivers/media/usb/dvb-usb/dvb-usb-common.h:22:9: note: macro 'deb_err' defined here
   #define deb_err(args...)   dprintk(dvb_usb_debug,0x010,args)
           ^
>> drivers/media/usb/dvb-usb/dvb-usb-init.c:350:23: error: expected '}'
   MODULE_LICENSE("GPL");
                         ^
   drivers/media/usb/dvb-usb/dvb-usb-init.c:288:13: note: to match this '{'
           if (!desc) {
                      ^
>> drivers/media/usb/dvb-usb/dvb-usb-init.c:350:23: error: expected '}'
   MODULE_LICENSE("GPL");
                         ^

drivers/media/usb/dvb-usb/dvb-usb-init.c:269:1: note: to match this '{' {
^
2 warnings and 3 errors generated.

vim +289 drivers/media/usb/dvb-usb/dvb-usb-init.c

261
262 /*
263 * USB
264 */
265 int dvb_usb_device_init(struct usb_interface *intf,
266 const struct dvb_usb_device_properties *props,
267 struct module *owner, struct dvb_usb_device **du,
268 short *adapter_nums)
269 {
270 struct usb_device *udev = interface_to_usbdev(intf);
271 struct dvb_usb_device *d = NULL;
272 const struct dvb_usb_device_description *desc = NULL;
273
274 int ret = -ENOMEM, cold = 0;
275
276 if (du != NULL)
277 *du = NULL;
278
279 d = kzalloc(sizeof(*d), GFP_KERNEL);
280 if (!d) {
281 err("no memory for 'struct dvb_usb_device'");
282 return -ENOMEM;
283 }
284
285 memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
286
287 desc = dvb_usb_find_device(udev, &d->props, &cold);
288 if (!desc) {

 > 289			deb_err("something went very wrong,
 > 290				 device was not found in current device list.\n");
   291			ret = -ENODEV;
   292			goto error;
   293		}
   294	
   295		if (cold) {
   296			info("found a %s in cold state, will try to load a firmware",
   297			     desc->name);
   298			ret = dvb_usb_download_firmware(udev, props);
   299			if (!props->no_reconnect || ret != 0)
   300				goto error;
   301		}
   302	
   303		info("found a '%s' in warm state.", desc->name);
   304		d->udev = udev;
   305		d->desc = desc;
   306		d->owner = owner;
   307	
   308		usb_set_intfdata(intf, d);
   309	
   310		ret = dvb_usb_init(d, adapter_nums);
   311		if (ret) {
   312			info("%s error while loading driver (%d)", desc->name, ret);
   313			goto error;
   314		}
   315	
   316		if (du)
   317			*du = d;
   318	
   319		info("%s is successfully initialized and connected.", desc->name);
   320		return 0;
   321	
   322	 error:
   323		usb_set_intfdata(intf, NULL);
   324		kfree(d);
   325		return ret;
   326	}
   327	EXPORT_SYMBOL(dvb_usb_device_init);
   328	
   329	void dvb_usb_device_exit(struct usb_interface *intf)
   330	{
   331		struct dvb_usb_device *d = usb_get_intfdata(intf);
   332		const char *default_name = "generic DVB-USB module";
   333		char name[40];
   334	
   335		usb_set_intfdata(intf, NULL);
   336		if (d != NULL && d->desc != NULL) {
   337			strscpy(name, d->desc->name, sizeof(name));
   338			dvb_usb_exit(d);
   339		} else {
   340			strscpy(name, default_name, sizeof(name));
   341		}
   342		info("%s successfully deinitialized and disconnected.", name);
   343	
   344	}
   345	EXPORT_SYMBOL(dvb_usb_device_exit);
   346	
   347	MODULE_VERSION("1.0");
   348	MODULE_AUTHOR("Patrick Boettcher <[email protected]>");
   349	MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
 > 350	MODULE_LICENSE("GPL");

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org