Subject: [arm:cex7 23/26] drivers/bus/fsl-mc/fsl-mc-uapi.c:88 fsl_mc_uapi_dev_open() warn: possible memory leak of 'priv_data'

From: Dan Carpenter <[email-masked]>
Date: Sun, 22 Sep 2019 20:48:04 +0300

tree:   git://git.armlinux.org.uk/~rmk/linux-arm.git cex7
head:   f959c38798814d3063719e70f279280fde355a49
commit: 00d36fa7e9c860aae5034c5f52acd91e6fb4ab4b [23/26] fsl-mc: add uapi interface for restool

If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <[email-masked]>
Reported-by: Dan Carpenter <[email-masked]>

smatch warnings:
drivers/bus/fsl-mc/fsl-mc-uapi.c:88 fsl_mc_uapi_dev_open() warn: possible memory leak of 'priv_data'

git remote add arm git://git.armlinux.org.uk/~rmk/linux-arm.git
git remote update arm
git checkout 00d36fa7e9c860aae5034c5f52acd91e6fb4ab4b
vim +/priv_data +88 drivers/bus/fsl-mc/fsl-mc-uapi.c

00d36fa7e9c860 Russell King 2019-09-12  45  static int fsl_mc_uapi_dev_open(struct inode *inode, struct file *filep)
00d36fa7e9c860 Russell King 2019-09-12  46  {
00d36fa7e9c860 Russell King 2019-09-12  47  	struct fsl_mc_device *root_mc_device;
00d36fa7e9c860 Russell King 2019-09-12  48  	struct uapi_priv_data *priv_data;
00d36fa7e9c860 Russell King 2019-09-12  49  	struct fsl_mc_io *dynamic_mc_io;
00d36fa7e9c860 Russell King 2019-09-12  50  	struct fsl_mc_uapi *mc_uapi;
00d36fa7e9c860 Russell King 2019-09-12  51  	struct fsl_mc_bus *mc_bus;
00d36fa7e9c860 Russell King 2019-09-12  52  	int error;
00d36fa7e9c860 Russell King 2019-09-12  53  
00d36fa7e9c860 Russell King 2019-09-12  54  	priv_data = kzalloc(sizeof(*priv_data), GFP_KERNEL);
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Can we use devm_ here?

00d36fa7e9c860 Russell King 2019-09-12  55  	if (!priv_data)
00d36fa7e9c860 Russell King 2019-09-12  56  		return -ENOMEM;
00d36fa7e9c860 Russell King 2019-09-12  57  
00d36fa7e9c860 Russell King 2019-09-12  58  	mc_uapi = container_of(filep->private_data, struct fsl_mc_uapi, misc);
00d36fa7e9c860 Russell King 2019-09-12  59  	mc_bus = container_of(mc_uapi, struct fsl_mc_bus, uapi_misc);
00d36fa7e9c860 Russell King 2019-09-12  60  	root_mc_device = &mc_bus->mc_dev;
00d36fa7e9c860 Russell King 2019-09-12  61  
00d36fa7e9c860 Russell King 2019-09-12  62  	mutex_lock(&mc_uapi->mutex);
00d36fa7e9c860 Russell King 2019-09-12  63  
00d36fa7e9c860 Russell King 2019-09-12  64  	if (!mc_uapi->local_instance_in_use) {
00d36fa7e9c860 Russell King 2019-09-12  65  		priv_data->mc_io = mc_uapi->static_mc_io;
00d36fa7e9c860 Russell King 2019-09-12  66  		mc_uapi->local_instance_in_use = 1;
00d36fa7e9c860 Russell King 2019-09-12  67  	} else {
00d36fa7e9c860 Russell King 2019-09-12  68  		error = fsl_mc_portal_allocate(root_mc_device, 0,
00d36fa7e9c860 Russell King 2019-09-12  69  					       &dynamic_mc_io);
00d36fa7e9c860 Russell King 2019-09-12  70  		if (error) {
00d36fa7e9c860 Russell King 2019-09-12  71  			dev_dbg(&root_mc_device->dev,
00d36fa7e9c860 Russell King 2019-09-12  72  				"Could not allocate MC portal\n");
00d36fa7e9c860 Russell King 2019-09-12  73  			goto error_portal_allocate;
                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
00d36fa7e9c860 Russell King 2019-09-12  74  		}
00d36fa7e9c860 Russell King 2019-09-12  75  
00d36fa7e9c860 Russell King 2019-09-12  76  		priv_data->mc_io = dynamic_mc_io;
00d36fa7e9c860 Russell King 2019-09-12  77  	}
00d36fa7e9c860 Russell King 2019-09-12  78  	priv_data->uapi = mc_uapi;
00d36fa7e9c860 Russell King 2019-09-12  79  	filep->private_data = priv_data;
00d36fa7e9c860 Russell King 2019-09-12  80  
00d36fa7e9c860 Russell King 2019-09-12  81  	mutex_unlock(&mc_uapi->mutex);
00d36fa7e9c860 Russell King 2019-09-12  82  
00d36fa7e9c860 Russell King 2019-09-12  83  	return 0;
00d36fa7e9c860 Russell King 2019-09-12  84  
00d36fa7e9c860 Russell King 2019-09-12  85  error_portal_allocate:
00d36fa7e9c860 Russell King 2019-09-12  86  	mutex_unlock(&mc_uapi->mutex);
00d36fa7e9c860 Russell King 2019-09-12  87  
00d36fa7e9c860 Russell King 2019-09-12 @88  	return error;
00d36fa7e9c860 Russell King 2019-09-12  89  }

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

_______________________________________________
linux-arm-kernel mailing list
[email-masked]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel