[PATCH 1/3] uio: uio_sercos3: use device-managed functions for simple allocs

From: Alexandru Ardelean
Date: Fri Nov 20 2020 - 03:43:14 EST


This change converts the simple allocations [kzalloc()] to devm_kzalloc()
tying the life-time of these objects to the PCI device object.
It cleans up the error and exit path and bit, and does a minor correction
that -ENOMEM is returned (vs -ENODEV) in case the 'priv' object cannot be
allocated.

Signed-off-by: Alexandru Ardelean <alexandru.ardelean@xxxxxxxxxx>
---
drivers/uio/uio_sercos3.c | 14 ++++----------
1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/drivers/uio/uio_sercos3.c b/drivers/uio/uio_sercos3.c
index 9658a0887fee..b93a5f8f4cba 100644
--- a/drivers/uio/uio_sercos3.c
+++ b/drivers/uio/uio_sercos3.c
@@ -124,16 +124,16 @@ static int sercos3_pci_probe(struct pci_dev *dev,
struct sercos3_priv *priv;
int i;

- info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
+ info = devm_kzalloc(&dev->dev, sizeof(struct uio_info), GFP_KERNEL);
if (!info)
return -ENOMEM;

- priv = kzalloc(sizeof(struct sercos3_priv), GFP_KERNEL);
+ priv = devm_kzalloc(&dev->dev, sizeof(struct sercos3_priv), GFP_KERNEL);
if (!priv)
- goto out_free;
+ return -ENOMEM;

if (pci_enable_device(dev))
- goto out_free_priv;
+ return -ENODEV;

if (pci_request_regions(dev, "sercos3"))
goto out_disable;
@@ -174,10 +174,6 @@ static int sercos3_pci_probe(struct pci_dev *dev,
pci_release_regions(dev);
out_disable:
pci_disable_device(dev);
-out_free_priv:
- kfree(priv);
-out_free:
- kfree(info);
return -ENODEV;
}

@@ -193,8 +189,6 @@ static void sercos3_pci_remove(struct pci_dev *dev)
if (info->mem[i].internal_addr)
iounmap(info->mem[i].internal_addr);
}
- kfree(info->priv);
- kfree(info);
}

static struct pci_device_id sercos3_pci_ids[] = {
--
2.27.0