Skip to contentSkip to conversation

staging: gpib: Add error handling

PATCH In review Nihar Chaithanya 3 participants 4 messages Dec 21, 2024 – Dec 22, 2024 Staging

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 staging: gpib: Add error handling

The function gpib_register_driver() can fail when kmalloc() fails but it does not return an error code.
The function pcmcia_register_driver() returns an error code but most of the times is not handled in gpib.

Modify gpid_register_driver() to return appropriate error code and also handle the error code returned by pcmcia_register_driver() incase it fails. When any of these functions fail unwind and unregister the driver to avoid semi-registered system.

This issue was reported by Coverity Scan.
Report:
CID 1635894: (#1 of 1): 'Constant' variable guards dead code (DEADCODE)
dead_error_line: Execution cannot reach this statement: return -1;.

---
v2 --> v3 : Modified the gpib_register_driver() to return error code,
added error handling to all the places that the gpib_register_driver() is called.

v1 --> v2 : Replaced the redundant cb_pcmcia_init_module() with
pcmcia_register_driver().

Link to v1 : https://lore.kernel.org/all/[email protected]/
Link to v2 : https://lore.kernel.org/all/[email protected]/

---
 .../gpib/agilent_82350b/agilent_82350b.c      | 21 +++-
 .../gpib/agilent_82357a/agilent_82357a.c      | 13 ++-
 drivers/staging/gpib/cb7210/cb7210.c          | 96 ++++++++++++++-----
 drivers/staging/gpib/cec/cec_gpib.c           | 12 +--
 drivers/staging/gpib/common/gpib_os.c         |  7 +-
 drivers/staging/gpib/eastwood/fluke_gpib.c    | 28 ++++--
 drivers/staging/gpib/fmh_gpib/fmh_gpib.c      | 45 ++++++---
 drivers/staging/gpib/gpio/gpib_bitbang.c      |  6 +-
 drivers/staging/gpib/hp_82335/hp82335.c       |  8 +-
 drivers/staging/gpib/hp_82341/hp_82341.c      | 14 ++-
 drivers/staging/gpib/include/gpibP.h          |  2 +-
 drivers/staging/gpib/ines/ines_gpib.c         | 78 ++++++++++-----
 .../gpib/lpvo_usb_gpib/lpvo_usb_gpib.c        |  6 +-
 drivers/staging/gpib/ni_usb/ni_usb_gpib.c     | 13 ++-
 drivers/staging/gpib/pc2/pc2_gpib.c           | 30 +++++-
 drivers/staging/gpib/tnt4882/tnt4882_gpib.c   | 87 +++++++++++++----
 16 files changed, 355 insertions(+), 111 deletions(-)
drivers/staging/gpib/agilent_82350b/agilent_82350b.c +16 -5
index 53006d0cc79c..39420f2b0b32 100644
@@ -909,14 +909,25 @@ static int __init agilent_82350b_init_module(void)
 	int result;
 
 	result = pci_register_driver(&agilent_82350b_pci_driver);
-	if (result) {
-		pr_err("agilent_82350b: pci_driver_register failed!\n");
+	if (result)
 		return result;
-	}
 
-	gpib_register_driver(&agilent_82350b_unaccel_interface, THIS_MODULE);
-	gpib_register_driver(&agilent_82350b_interface, THIS_MODULE);
+	result = gpib_register_driver(&agilent_82350b_unaccel_interface, THIS_MODULE);
+	if (result)
+		goto err_unaccel;
+
+	result = gpib_register_driver(&agilent_82350b_interface, THIS_MODULE);
+	if (result)
+		goto err_interface;
+
 	return 0;
+
+err_interface:
+	gpib_unregister_driver(&agilent_82350b_unaccel_interface);
+err_unaccel:
+	pci_unregister_driver(&agilent_82350b_pci_driver);
+
+	return result;
 }
 
 static void __exit agilent_82350b_exit_module(void)
drivers/staging/gpib/agilent_82357a/agilent_82357a.c +10 -3
index bf05fb4a736b..60311ce8d164 100644
@@ -1691,14 +1691,21 @@ static struct usb_driver agilent_82357a_bus_driver = {
 static int __init agilent_82357a_init_module(void)
 {
 	int i;
+	int ret = 0;
 
 	pr_info("agilent_82357a_gpib driver loading");
 	for (i = 0; i < MAX_NUM_82357A_INTERFACES; ++i)
 		agilent_82357a_driver_interfaces[i] = NULL;
-	usb_register(&agilent_82357a_bus_driver);
-	gpib_register_driver(&agilent_82357a_gpib_interface, THIS_MODULE);
 
-	return 0;
+	ret = usb_register(&agilent_82357a_bus_driver);
+	if (ret)
+		return ret;
+
+	ret = gpib_register_driver(&agilent_82357a_gpib_interface, THIS_MODULE);
+	if (ret)
+		usb_deregister(&agilent_82357a_bus_driver);
+
+	return ret;
 }
 
 static void __exit agilent_82357a_exit_module(void)
drivers/staging/gpib/cb7210/cb7210.c +74 -22

113 lines not on this page · raw message

drivers/staging/gpib/cec/cec_gpib.c +6 -6
index 3dc933deb401..ffa366c30ac5 100644
@@ -361,17 +361,17 @@ static struct pci_driver cec_pci_driver = {
 
 static int __init cec_init_module(void)
 {
-	int result;
+	int result = 0;
 
 	result = pci_register_driver(&cec_pci_driver);
-	if (result) {
-		pr_err("cec_gpib: pci_driver_register failed!\n");
+	if (result)
 		return result;
-	}
 
-	gpib_register_driver(&cec_pci_interface, THIS_MODULE);
+	result = gpib_register_driver(&cec_pci_interface, THIS_MODULE);
+	if (result)
+		pci_unregister_driver(&cec_pci_driver);
 
-	return 0;
+	return result;
 }
 
 static void cec_exit_module(void)
drivers/staging/gpib/common/gpib_os.c +4 -3
index 405237d8cb47..07795df3b721 100644
@@ -2094,18 +2094,19 @@ void init_gpib_descriptor(gpib_descriptor_t *desc)
 	atomic_set(&desc->io_in_progress, 0);
 }
 
-void gpib_register_driver(gpib_interface_t *interface, struct module *provider_module)
+int gpib_register_driver(gpib_interface_t *interface, struct module *provider_module)
 {
 	struct gpib_interface_list_struct *entry;
 
 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
 	if (!entry)
-		return;
+		return -ENOMEM;
 
 	entry->interface = interface;
 	entry->module = provider_module;
 	list_add(&entry->list, &registered_drivers);
-	pr_info("gpib: registered %s interface\n", interface->name);
+
+	return 0;
 }
 EXPORT_SYMBOL(gpib_register_driver);
 
drivers/staging/gpib/eastwood/fluke_gpib.c +21 -7
index 3f938ab0c84d..2efc0226ec2b 100644
@@ -1154,17 +1154,31 @@ static int __init fluke_init_module(void)
 	int result;
 
 	result = platform_driver_register(&fluke_gpib_platform_driver);
-	if (result) {
-		pr_err("fluke_gpib: platform_driver_register failed!\n");
+	if (result)
 		return result;
-	}
 
-	gpib_register_driver(&fluke_unaccel_interface, THIS_MODULE);
-	gpib_register_driver(&fluke_hybrid_interface, THIS_MODULE);
-	gpib_register_driver(&fluke_interface, THIS_MODULE);
+	result = gpib_register_driver(&fluke_unaccel_interface, THIS_MODULE);
+	if (result)
+		goto err_unaccel;
+
+	result = gpib_register_driver(&fluke_hybrid_interface, THIS_MODULE);
+	if (result)
+		goto err_hybrid;
+
+	result = gpib_register_driver(&fluke_interface, THIS_MODULE);
+	if (result)
+		goto err_interface;
 
-	pr_info("fluke_gpib\n");
 	return 0;
+
+err_interface:
+	gpib_unregister_driver(&fluke_hybrid_interface);
+err_hybrid:
+	gpib_unregister_driver(&fluke_unaccel_interface);
+err_unaccel:
+	platform_driver_unregister(&fluke_gpib_platform_driver);
+
+	return result;
 }
 
 static void __exit fluke_exit_module(void)
drivers/staging/gpib/fmh_gpib/fmh_gpib.c +32 -13

56 lines not on this page · raw message

drivers/staging/gpib/gpio/gpib_bitbang.c +5 -1
index a2d562cbd65b..2f1fdb6f0802 100644
@@ -1341,8 +1341,12 @@ return_to_local : bb_return_to_local,
 
 static int __init bb_init_module(void)
 {
-	gpib_register_driver(&bb_interface, THIS_MODULE);
+	int result = gpib_register_driver(&bb_interface, THIS_MODULE);
 
+	if (result) {
+		gpib_unregister_driver(&bb_interface);
+		return result;
+	}
 	dbg_printk(0, "module loaded with pin map \"%s\"%s\n",
 		   pin_map, (sn7516x_used) ? " and SN7516x driver support" : "");
 	return 0;
drivers/staging/gpib/hp_82335/hp82335.c +7 -1
index 40afe42aea47..1c44a865ad05 100644
@@ -325,7 +325,13 @@ void hp82335_detach(gpib_board_t *board)
 
 static int __init hp82335_init_module(void)
 {
-	gpib_register_driver(&hp82335_interface, THIS_MODULE);
+	int result = gpib_register_driver(&hp82335_interface, THIS_MODULE);
+
+	if (result) {
+		gpib_unregister_driver(&hp82335_interface);
+		return result;
+	}
+
 	return 0;
 }
 
drivers/staging/gpib/hp_82341/hp_82341.c +11 -3
index 8ad1c885a9fb..daf5f6e02bd5 100644
@@ -807,9 +807,17 @@ MODULE_DEVICE_TABLE(pnp, hp_82341_pnp_table);
 
 static int __init hp_82341_init_module(void)
 {
-	gpib_register_driver(&hp_82341_unaccel_interface, THIS_MODULE);
-	gpib_register_driver(&hp_82341_interface, THIS_MODULE);
-	return 0;
+	int ret = 0;
+
+	ret = gpib_register_driver(&hp_82341_unaccel_interface, THIS_MODULE);
+	if (ret)
+		return ret;
+
+	ret = gpib_register_driver(&hp_82341_interface, THIS_MODULE);
+	if (ret)
+		gpib_unregister_driver(&hp_82341_unaccel_interface);
+
+	return ret;
 }
 
 static void __exit hp_82341_exit_module(void)
drivers/staging/gpib/include/gpibP.h +1 -1
index 5fc42b645ab7..d0cd42c1a0ad 100644
@@ -17,7 +17,7 @@
 #include <linux/fs.h>
 #include <linux/interrupt.h>
 
-void gpib_register_driver(gpib_interface_t *interface, struct module *mod);
+int gpib_register_driver(gpib_interface_t *interface, struct module *mod);
 void gpib_unregister_driver(gpib_interface_t *interface);
 struct pci_dev *gpib_pci_get_device(const gpib_board_config_t *config, unsigned int vendor_id,
 				    unsigned int device_id, struct pci_dev *from);
drivers/staging/gpib/ines/ines_gpib.c +56 -22

96 lines not on this page · raw message

drivers/staging/gpib/lpvo_usb_gpib/lpvo_usb_gpib.c +5 -1
index 796c3a5be545..3e826ab89bc7 100644
@@ -1181,7 +1181,11 @@ static int usb_gpib_init_module(struct usb_interface *interface)
 		return rv;
 
 	if (!assigned_usb_minors) {
-		gpib_register_driver(&usb_gpib_interface, THIS_MODULE);
+		rv = gpib_register_driver(&usb_gpib_interface, THIS_MODULE);
+		if (rv) {
+			gpib_unregister_driver(&usb_gpib_interface);
+			goto exit;
+		}
 	} else {
 		/* check if minor is already registered - maybe useless, but if
 		 *  it happens the code is inconsistent somewhere
drivers/staging/gpib/ni_usb/ni_usb_gpib.c +10 -3
index b7b6fb1be379..f20a23a023d9 100644
@@ -2619,14 +2619,21 @@ static struct usb_driver ni_usb_bus_driver = {
 static int __init ni_usb_init_module(void)
 {
 	int i;
+	int ret = 0;
 
 	pr_info("ni_usb_gpib driver loading\n");
 	for (i = 0; i < MAX_NUM_NI_USB_INTERFACES; i++)
 		ni_usb_driver_interfaces[i] = NULL;
-	usb_register(&ni_usb_bus_driver);
-	gpib_register_driver(&ni_usb_gpib_interface, THIS_MODULE);
 
-	return 0;
+	ret = usb_register(&ni_usb_bus_driver);
+	if (ret)
+		return ret;
+
+	ret = gpib_register_driver(&ni_usb_gpib_interface, THIS_MODULE);
+	if (ret)
+		usb_deregister(&ni_usb_bus_driver);
+
+	return ret;
 }
 
 static void __exit ni_usb_exit_module(void)
drivers/staging/gpib/pc2/pc2_gpib.c +26 -4
index 7b3b34f47341..1d42ced67b2c 100644
@@ -635,12 +635,34 @@ void pc2_2a_detach(gpib_board_t *board)
 
 static int __init pc2_init_module(void)
 {
-	gpib_register_driver(&pc2_interface, THIS_MODULE);
-	gpib_register_driver(&pc2a_interface, THIS_MODULE);
-	gpib_register_driver(&pc2a_cb7210_interface, THIS_MODULE);
-	gpib_register_driver(&pc2_2a_interface, THIS_MODULE);
+	int ret;
+
+	ret = gpib_register_driver(&pc2_interface, THIS_MODULE);
+	if (ret)
+		return ret;
+
+	ret = gpib_register_driver(&pc2a_interface, THIS_MODULE);
+	if (ret)
+		goto err_pc2a;
+
+	ret = gpib_register_driver(&pc2a_cb7210_interface, THIS_MODULE);
+	if (ret)
+		goto err_cb7210;
+
+	ret = gpib_register_driver(&pc2_2a_interface, THIS_MODULE);
+	if (ret)
+		goto err_pc2_2a;
 
 	return 0;
+
+err_pc2_2a:
+	gpib_unregister_driver(&pc2a_cb7210_interface);
+err_cb7210:
+	gpib_unregister_driver(&pc2a_interface);
+err_pc2a:
+	gpib_unregister_driver(&pc2_interface);
+
+	return ret;
 }
 
 static void __exit pc2_exit_module(void)
drivers/staging/gpib/tnt4882/tnt4882_gpib.c +71 -16

101 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 15 lines · Nihar Chaithanya

The function gpib_register_driver() can fail when kmalloc() fails but it does not return an error code.
The function pcmcia_register_driver() returns an error code but most of the times is not handled in gpib.

Modify gpid_register_driver() to return appropriate error code and also handle the error code returned by pcmcia_register_driver() incase it fails. When any of these functions fail unwind and unregister the driver to avoid semi-registered system.

This issue was reported by Coverity Scan.
Report:
CID 1635894: (#1 of 1): 'Constant' variable guards dead code (DEADCODE)
dead_error_line: Execution cannot reach this statement: return -1;.

Quoted 5 lines

---
v2 --> v3 : Modified the gpib_register_driver() to return error code,
added error handling to all the places that the gpib_register_driver() is called.

You are doing a lot of different things all at once here. Please break this up into a patch series, only doing one "logical" thing per change please.

thanks,

greg k-h

#2 lore raw
In reply to Nihar Chaithanya
kernel test robot noticed the following build errors:

Hi Nihar,

kernel test robot noticed the following build errors:

[auto build test ERROR on staging/staging-testing]

url:    https://github.com/intel-lab-lkp/linux/commits/Nihar-Chaithanya/staging-gpib-Add-error-handling/20241222-060646
base:   staging/staging-testing
patch link:    https://lore.kernel.org/r/20241221214822.163667-1-niharchaithanya%40gmail.com
patch subject: [PATCH v3] staging: gpib: Add error handling
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20241222/[email protected]/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241222/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/staging/gpib/fmh_gpib/fmh_gpib.c:15:
In file included from drivers/staging/gpib/fmh_gpib/fmh_gpib.h:9:
In file included from include/linux/dmaengine.h:8:
In file included from include/linux/device.h:32:
In file included from include/linux/device/driver.h:21:
In file included from include/linux/module.h:19:
In file included from include/linux/elf.h:6:
In file included from arch/s390/include/asm/elf.h:181:
In file included from arch/s390/include/asm/mmu_context.h:11:
In file included from arch/s390/include/asm/pgalloc.h:18:
In file included from include/linux/mm.h:2223:
include/linux/vmstat.h:504:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

     504 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     505 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:511:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     511 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     512 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   include/linux/vmstat.h:524:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     524 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     525 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   drivers/staging/gpib/fmh_gpib/fmh_gpib.c:162:45: warning: bitwise operation between different enumeration types ('enum fmh_gpib_auxmr_bits' and 'enum aux_reg_i_bits') [-Wenum-enum-conversion]
     162 |                 write_byte(&priv->nec7210_priv, AUX_I_REG | LOCAL_PPOLL_MODE_BIT, AUXMR);
         |                                                 ~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~
>> drivers/staging/gpib/fmh_gpib/fmh_gpib.c:1694:8: error: use of undeclared label 'err_platform_driver'
    1694 |                 goto err_platform_driver;
         |                      ^

5 warnings and 1 error generated.

vim +/err_platform_driver +1694 drivers/staging/gpib/fmh_gpib/fmh_gpib.c

1687
1688 static int __init fmh_gpib_init_module(void)
1689 {
1690 int result;
1691
1692 result = platform_driver_register(&fmh_gpib_platform_driver);
1693 if (result)

> 1694			goto err_platform_driver;

1695
1696 result = pci_register_driver(&fmh_gpib_pci_driver);
1697 if (result)
1698 goto err_pci_driver;
1699
1700 result = gpib_register_driver(&fmh_gpib_unaccel_interface, THIS_MODULE);
1701 if (result)
1702 goto err_unaccel;
1703
1704 result = gpib_register_driver(&fmh_gpib_interface, THIS_MODULE);
1705 if (result)
1706 goto err_interface;
1707
1708 result = gpib_register_driver(&fmh_gpib_pci_unaccel_interface, THIS_MODULE);
1709 if (result)
1710 goto err_pci_unaccel;
1711
1712 result = gpib_register_driver(&fmh_gpib_pci_interface, THIS_MODULE);
1713 if (result)
1714 goto err_pci;
1715
1716 return 0;
1717
1718 err_pci:
1719 gpib_unregister_driver(&fmh_gpib_pci_unaccel_interface);
1720 err_pci_unaccel:
1721 gpib_unregister_driver(&fmh_gpib_interface);
1722 err_interface:
1723 gpib_unregister_driver(&fmh_gpib_unaccel_interface);
1724 err_unaccel:
1725 pci_unregister_driver(&fmh_gpib_pci_driver);
1726 err_pci_driver:
1727 platform_driver_unregister(&fmh_gpib_platform_driver);
1728
1729 return result;
1730 }
1731

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to Nihar Chaithanya
kernel test robot noticed the following build errors:

Hi Nihar,

kernel test robot noticed the following build errors:

[auto build test ERROR on staging/staging-testing]

url:    https://github.com/intel-lab-lkp/linux/commits/Nihar-Chaithanya/staging-gpib-Add-error-handling/20241222-060646
base:   staging/staging-testing
patch link:    https://lore.kernel.org/r/20241221214822.163667-1-niharchaithanya%40gmail.com
patch subject: [PATCH v3] staging: gpib: Add error handling
config: s390-allyesconfig (https://download.01.org/0day-ci/archive/20241222/[email protected]/config)
compiler: s390-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241222/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/staging/gpib/fmh_gpib/fmh_gpib.c: In function 'fmh_gpib_init_module':

>> drivers/staging/gpib/fmh_gpib/fmh_gpib.c:1694:17: error: label 'err_platform_driver' used but not defined
    1694 |                 goto err_platform_driver;
         |                 ^~~~

vim +/err_platform_driver +1694 drivers/staging/gpib/fmh_gpib/fmh_gpib.c

1687
1688 static int __init fmh_gpib_init_module(void)
1689 {
1690 int result;
1691
1692 result = platform_driver_register(&fmh_gpib_platform_driver);
1693 if (result)

> 1694			goto err_platform_driver;

1695
1696 result = pci_register_driver(&fmh_gpib_pci_driver);
1697 if (result)
1698 goto err_pci_driver;
1699
1700 result = gpib_register_driver(&fmh_gpib_unaccel_interface, THIS_MODULE);
1701 if (result)
1702 goto err_unaccel;
1703
1704 result = gpib_register_driver(&fmh_gpib_interface, THIS_MODULE);
1705 if (result)
1706 goto err_interface;
1707
1708 result = gpib_register_driver(&fmh_gpib_pci_unaccel_interface, THIS_MODULE);
1709 if (result)
1710 goto err_pci_unaccel;
1711
1712 result = gpib_register_driver(&fmh_gpib_pci_interface, THIS_MODULE);
1713 if (result)
1714 goto err_pci;
1715
1716 return 0;
1717
1718 err_pci:
1719 gpib_unregister_driver(&fmh_gpib_pci_unaccel_interface);
1720 err_pci_unaccel:
1721 gpib_unregister_driver(&fmh_gpib_interface);
1722 err_interface:
1723 gpib_unregister_driver(&fmh_gpib_unaccel_interface);
1724 err_unaccel:
1725 pci_unregister_driver(&fmh_gpib_pci_driver);
1726 err_pci_driver:
1727 platform_driver_unregister(&fmh_gpib_platform_driver);
1728
1729 return result;
1730 }
1731

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki