stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, GiulioDP <depasquale.giulio@gmail.com>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>
Subject: [PATCH 3.13 03/22] Input: mousedev - fix race when creating mixed device
Date: Mon, 31 Mar 2014 21:08:33 -0700	[thread overview]
Message-ID: <20140401040703.796714167@linuxfoundation.org> (raw)
In-Reply-To: <20140401040703.045139933@linuxfoundation.org>

3.13-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Dmitry Torokhov <dmitry.torokhov@gmail.com>

commit e4dbedc7eac7da9db363a36f2bd4366962eeefcc upstream.

We should not be using static variable mousedev_mix in methods that can be
called before that singleton gets assigned. While at it let's add open and
close methods to mousedev structure so that we do not need to test if we
are dealing with multiplexor or normal device and simply call appropriate
method directly.

This fixes: https://bugzilla.kernel.org/show_bug.cgi?id=71551

Reported-by: GiulioDP <depasquale.giulio@gmail.com>
Tested-by: GiulioDP <depasquale.giulio@gmail.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 drivers/input/mousedev.c |   73 +++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 31 deletions(-)

--- a/drivers/input/mousedev.c
+++ b/drivers/input/mousedev.c
@@ -67,7 +67,6 @@ struct mousedev {
 	struct device dev;
 	struct cdev cdev;
 	bool exist;
-	bool is_mixdev;
 
 	struct list_head mixdev_node;
 	bool opened_by_mixdev;
@@ -77,6 +76,9 @@ struct mousedev {
 	int old_x[4], old_y[4];
 	int frac_dx, frac_dy;
 	unsigned long touch;
+
+	int (*open_device)(struct mousedev *mousedev);
+	void (*close_device)(struct mousedev *mousedev);
 };
 
 enum mousedev_emul {
@@ -116,9 +118,6 @@ static unsigned char mousedev_imex_seq[]
 static struct mousedev *mousedev_mix;
 static LIST_HEAD(mousedev_mix_list);
 
-static void mixdev_open_devices(void);
-static void mixdev_close_devices(void);
-
 #define fx(i)  (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
 #define fy(i)  (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
 
@@ -428,9 +427,7 @@ static int mousedev_open_device(struct m
 	if (retval)
 		return retval;
 
-	if (mousedev->is_mixdev)
-		mixdev_open_devices();
-	else if (!mousedev->exist)
+	if (!mousedev->exist)
 		retval = -ENODEV;
 	else if (!mousedev->open++) {
 		retval = input_open_device(&mousedev->handle);
@@ -446,9 +443,7 @@ static void mousedev_close_device(struct
 {
 	mutex_lock(&mousedev->mutex);
 
-	if (mousedev->is_mixdev)
-		mixdev_close_devices();
-	else if (mousedev->exist && !--mousedev->open)
+	if (mousedev->exist && !--mousedev->open)
 		input_close_device(&mousedev->handle);
 
 	mutex_unlock(&mousedev->mutex);
@@ -459,21 +454,29 @@ static void mousedev_close_device(struct
  * stream. Note that this function is called with mousedev_mix->mutex
  * held.
  */
-static void mixdev_open_devices(void)
+static int mixdev_open_devices(struct mousedev *mixdev)
 {
-	struct mousedev *mousedev;
+	int error;
+
+	error = mutex_lock_interruptible(&mixdev->mutex);
+	if (error)
+		return error;
 
-	if (mousedev_mix->open++)
-		return;
+	if (!mixdev->open++) {
+		struct mousedev *mousedev;
 
-	list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
-		if (!mousedev->opened_by_mixdev) {
-			if (mousedev_open_device(mousedev))
-				continue;
+		list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
+			if (!mousedev->opened_by_mixdev) {
+				if (mousedev_open_device(mousedev))
+					continue;
 
-			mousedev->opened_by_mixdev = true;
+				mousedev->opened_by_mixdev = true;
+			}
 		}
 	}
+
+	mutex_unlock(&mixdev->mutex);
+	return 0;
 }
 
 /*
@@ -481,19 +484,22 @@ static void mixdev_open_devices(void)
  * device. Note that this function is called with mousedev_mix->mutex
  * held.
  */
-static void mixdev_close_devices(void)
+static void mixdev_close_devices(struct mousedev *mixdev)
 {
-	struct mousedev *mousedev;
+	mutex_lock(&mixdev->mutex);
 
-	if (--mousedev_mix->open)
-		return;
+	if (!--mixdev->open) {
+		struct mousedev *mousedev;
 
-	list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
-		if (mousedev->opened_by_mixdev) {
-			mousedev->opened_by_mixdev = false;
-			mousedev_close_device(mousedev);
+		list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
+			if (mousedev->opened_by_mixdev) {
+				mousedev->opened_by_mixdev = false;
+				mousedev_close_device(mousedev);
+			}
 		}
 	}
+
+	mutex_unlock(&mixdev->mutex);
 }
 
 
@@ -522,7 +528,7 @@ static int mousedev_release(struct inode
 	mousedev_detach_client(mousedev, client);
 	kfree(client);
 
-	mousedev_close_device(mousedev);
+	mousedev->close_device(mousedev);
 
 	return 0;
 }
@@ -550,7 +556,7 @@ static int mousedev_open(struct inode *i
 	client->mousedev = mousedev;
 	mousedev_attach_client(mousedev, client);
 
-	error = mousedev_open_device(mousedev);
+	error = mousedev->open_device(mousedev);
 	if (error)
 		goto err_free_client;
 
@@ -861,16 +867,21 @@ static struct mousedev *mousedev_create(
 
 	if (mixdev) {
 		dev_set_name(&mousedev->dev, "mice");
+
+		mousedev->open_device = mixdev_open_devices;
+		mousedev->close_device = mixdev_close_devices;
 	} else {
 		int dev_no = minor;
 		/* Normalize device number if it falls into legacy range */
 		if (dev_no < MOUSEDEV_MINOR_BASE + MOUSEDEV_MINORS)
 			dev_no -= MOUSEDEV_MINOR_BASE;
 		dev_set_name(&mousedev->dev, "mouse%d", dev_no);
+
+		mousedev->open_device = mousedev_open_device;
+		mousedev->close_device = mousedev_close_device;
 	}
 
 	mousedev->exist = true;
-	mousedev->is_mixdev = mixdev;
 	mousedev->handle.dev = input_get_device(dev);
 	mousedev->handle.name = dev_name(&mousedev->dev);
 	mousedev->handle.handler = handler;
@@ -919,7 +930,7 @@ static void mousedev_destroy(struct mous
 	device_del(&mousedev->dev);
 	mousedev_cleanup(mousedev);
 	input_free_minor(MINOR(mousedev->dev.devt));
-	if (!mousedev->is_mixdev)
+	if (mousedev != mousedev_mix)
 		input_unregister_handle(&mousedev->handle);
 	put_device(&mousedev->dev);
 }



  parent reply	other threads:[~2014-04-01  4:08 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-01  4:08 [PATCH 3.13 00/22] 3.13.9-stable review Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 01/22] ext4: atomically set inode->i_flags in ext4_set_inode_flags() Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 02/22] rcuwalk: recheck mount_lock after mountpoint crossing attempts Greg Kroah-Hartman
2014-04-01  4:08 ` Greg Kroah-Hartman [this message]
2014-04-01  4:08 ` [PATCH 3.13 04/22] Input: synaptics - add manual min/max quirk Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 05/22] Input: synaptics - add manual min/max quirk for ThinkPad X240 Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 06/22] Input: cypress_ps2 - dont report as a button pads Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 07/22] xen/balloon: flush persistent kmaps in correct position Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 08/22] Revert "xen: properly account for _PAGE_NUMA during xen pte translations" Greg Kroah-Hartman
2014-04-10 10:05   ` Steven Noonan
2014-04-10 10:08     ` David Vrabel
2014-04-01  4:08 ` [PATCH 3.13 09/22] i2c: cpm: Fix build by adding of_address.h and of_irq.h Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 10/22] drm/i915: Undo gtt scratch pte unmapping again Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 11/22] x86: fix boot on uniprocessor systems Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 12/22] make prepend_name() work correctly when called with negative *buflen Greg Kroah-Hartman
2014-04-09 20:17   ` Sasha Levin
2014-04-01  4:08 ` [PATCH 3.13 13/22] net: mvneta: rename MVNETA_GMAC2_PSC_ENABLE to MVNETA_GMAC2_PCS_ENABLE Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 14/22] net: mvneta: fix usage as a module on RGMII configurations Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 15/22] random32: avoid attempt to late reseed if in the middle of seeding Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 16/22] resizable namespace.c hashes Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 17/22] keep shadowed vfsmounts together Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 18/22] dont bother with propagate_mnt() unless the target is shared Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 19/22] switch mnt_hash to hlist Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 20/22] mm: close PageTail race Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 21/22] cgroup: protect modifications to cgroup_idr with cgroup_mutex Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 22/22] netfilter: nf_conntrack_dccp: fix skb_header_pointer API usages Greg Kroah-Hartman
2014-04-02  0:03 ` [PATCH 3.13 00/22] 3.13.9-stable review Guenter Roeck
2014-04-03 22:45   ` Greg Kroah-Hartman
2014-04-04 13:30     ` Shuah Khan

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=20140401040703.796714167@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=depasquale.giulio@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@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;
as well as URLs for NNTP newsgroup(s).