mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mike Christie <mchristi@redhat.com>
To: manish.rangankar@cavium.com, target-devel@vger.kernel.org,
	nab@linux-iscsi.org, kys@microsoft.com, haiyangz@microsoft.com,
	sthemmin@microsoft.com, gregkh@linuxfoundation.org,
	mst@redhat.com, linux-kernel@vger.kernel.org
Cc: Mike Christie <mchristi@redhat.com>
Subject: [PATCH 1/1] uio: Fix uio_device memory leak
Date: Wed,  7 Jun 2017 15:06:44 -0500	[thread overview]
Message-ID: <1496866004-32328-1-git-send-email-mchristi@redhat.com> (raw)

It looks like there might be 2 issues with the uio_device allocation, or it
looks like we are leaking the device for possibly a specific type of device
case that I could not find but one of you may know about.

Issues:
1. We use devm_kzalloc to allocate the uio_device, but the release
function, devm_kmalloc_release, is just a noop, so the memory is never freed.

2. We are actually assigning the resource allocated by devm_kzalloc to
the parent device and not the device that goes with the uio_device.
If devm_kmalloc_release did free the memory, it would not actually be
freed until the parent is which in a lot of cases is not until module
removal, so there could have been thousands of uio_registe/unregister_device
sequences.

It seems like this is either a bug, or was done deliberately to support
some type of device that needs that memory to not be freed. I
checked upstream uio users, but did not see any type of device like this
though.

If this is a bug, this patch, made over Linus's tree, fixes the problems by 
just allocating the uio_device with the uio_info struct since they need to be
allocated/freed/accessed at the same times.

Signed-off-by: Mike Christie <mchristi@redhat.com>
---
 drivers/target/target_core_user.c |  6 +++---
 drivers/uio/uio.c                 | 17 +++++------------
 include/linux/uio_driver.h        |  2 +-
 3 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/drivers/target/target_core_user.c b/drivers/target/target_core_user.c
index beb5f09..00fde52 100644
--- a/drivers/target/target_core_user.c
+++ b/drivers/target/target_core_user.c
@@ -1299,7 +1299,7 @@ static int tcmu_configure_device(struct se_device *dev)
 	kref_get(&udev->kref);
 
 	ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
-				 udev->uio_info.uio_dev->minor);
+				 udev->uio_info.uio_dev.minor);
 	if (ret)
 		goto err_netlink;
 
@@ -1332,7 +1332,7 @@ static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
 
 static bool tcmu_dev_configured(struct tcmu_dev *udev)
 {
-	return udev->uio_info.uio_dev ? true : false;
+	return !!(udev->se_dev.dev_flags & DF_CONFIGURED);
 }
 
 static void tcmu_blocks_release(struct tcmu_dev *udev)
@@ -1381,7 +1381,7 @@ static void tcmu_free_device(struct se_device *dev)
 
 	if (tcmu_dev_configured(udev)) {
 		tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
-				   udev->uio_info.uio_dev->minor);
+				   udev->uio_info.uio_dev.minor);
 
 		uio_unregister_device(&udev->uio_info);
 	}
diff --git a/drivers/uio/uio.c b/drivers/uio/uio.c
index ff04b7f..7593db6 100644
--- a/drivers/uio/uio.c
+++ b/drivers/uio/uio.c
@@ -399,7 +399,7 @@ static void uio_free_minor(struct uio_device *idev)
  */
 void uio_event_notify(struct uio_info *info)
 {
-	struct uio_device *idev = info->uio_dev;
+	struct uio_device *idev = &info->uio_dev;
 
 	atomic_inc(&idev->event);
 	wake_up_interruptible(&idev->wait);
@@ -812,13 +812,8 @@ int __uio_register_device(struct module *owner,
 	if (!parent || !info || !info->name || !info->version)
 		return -EINVAL;
 
-	info->uio_dev = NULL;
-
-	idev = devm_kzalloc(parent, sizeof(*idev), GFP_KERNEL);
-	if (!idev) {
-		return -ENOMEM;
-	}
-
+	idev = &info->uio_dev;
+	memset(idev, 0, sizeof(*idev));
 	idev->owner = owner;
 	idev->info = info;
 	init_waitqueue_head(&idev->wait);
@@ -841,8 +836,6 @@ int __uio_register_device(struct module *owner,
 	if (ret)
 		goto err_uio_dev_add_attributes;
 
-	info->uio_dev = idev;
-
 	if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
 		/*
 		 * Note that we deliberately don't use devm_request_irq
@@ -879,10 +872,10 @@ void uio_unregister_device(struct uio_info *info)
 {
 	struct uio_device *idev;
 
-	if (!info || !info->uio_dev)
+	if (!info)
 		return;
 
-	idev = info->uio_dev;
+	idev = &info->uio_dev;
 
 	uio_free_minor(idev);
 
diff --git a/include/linux/uio_driver.h b/include/linux/uio_driver.h
index 3c85c81..4b1b1c8 100644
--- a/include/linux/uio_driver.h
+++ b/include/linux/uio_driver.h
@@ -95,7 +95,7 @@ struct uio_device {
  * @irqcontrol:		disable/enable irqs when 0/1 is written to /dev/uioX
  */
 struct uio_info {
-	struct uio_device	*uio_dev;
+	struct uio_device	uio_dev;
 	const char		*name;
 	const char		*version;
 	struct uio_mem		mem[MAX_UIO_MAPS];
-- 
2.7.2

             reply	other threads:[~2017-06-07 20:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-07 20:06 Mike Christie [this message]
2017-06-13 14:01 ` Greg KH
2017-06-14  0:16   ` Mike Christie
2017-06-14  0:35     ` Mike Christie
2017-06-14  5:03       ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1496866004-32328-1-git-send-email-mchristi@redhat.com \
    --to=mchristi@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=haiyangz@microsoft.com \
    --cc=kys@microsoft.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=manish.rangankar@cavium.com \
    --cc=mst@redhat.com \
    --cc=nab@linux-iscsi.org \
    --cc=sthemmin@microsoft.com \
    --cc=target-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®