mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Slaby <jslaby@suse.cz>
To: gregkh@linuxfoundation.org
Cc: alan@linux.intel.com, linux-kernel@vger.kernel.org, jirislaby@gmail.com
Subject: [PATCH v2 21/41] TTY: move cdev_add to tty_register_device
Date: Wed,  8 Aug 2012 22:26:44 +0200	[thread overview]
Message-ID: <1344457604-12823-4-git-send-email-jslaby@suse.cz> (raw)
In-Reply-To: <1344457604-12823-1-git-send-email-jslaby@suse.cz>

We need the /dev/ node not to be available before we call
tty_register_device. Otherwise we might race with open and
tty_struct->port might not be available at that time.

This is not an issue now, but would be a problem after "TTY: use
tty_port_register_device" is applied.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
---
 drivers/tty/tty_io.c       |   48 +++++++++++++++++++++++++++++++++++++-------
 include/linux/tty_driver.h |    2 +-
 2 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 59a73af..f83d450 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -2991,6 +2991,15 @@ EXPORT_SYMBOL_GPL(tty_put_char);
 
 struct class *tty_class;
 
+static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
+		unsigned int index, unsigned int count)
+{
+	/* init here, since reused cdevs cause crashes */
+	cdev_init(&driver->cdevs[index], &tty_fops);
+	driver->cdevs[index].owner = driver->owner;
+	return cdev_add(&driver->cdevs[index], dev, count);
+}
+
 /**
  *	tty_register_device - register a tty device
  *	@driver: the tty driver that describes the tty device
@@ -3013,8 +3022,10 @@ struct class *tty_class;
 struct device *tty_register_device(struct tty_driver *driver, unsigned index,
 				   struct device *device)
 {
+	struct device *ret;
 	char name[64];
 	dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
+	bool cdev = false;
 
 	if (index >= driver->num) {
 		printk(KERN_ERR "Attempt to register invalid tty line number "
@@ -3027,7 +3038,18 @@ struct device *tty_register_device(struct tty_driver *driver, unsigned index,
 	else
 		tty_line_name(driver, index, name);
 
-	return device_create(tty_class, device, dev, NULL, name);
+	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
+		int error = tty_cdev_add(driver, dev, index, 1);
+		if (error)
+			return ERR_PTR(error);
+		cdev = true;
+	}
+
+	ret = device_create(tty_class, device, dev, NULL, name);
+	if (IS_ERR(ret) && cdev)
+		cdev_del(&driver->cdevs[index]);
+
+	return ret;
 }
 EXPORT_SYMBOL(tty_register_device);
 
@@ -3046,6 +3068,8 @@ void tty_unregister_device(struct tty_driver *driver, unsigned index)
 {
 	device_destroy(tty_class,
 		MKDEV(driver->major, driver->minor_start) + index);
+	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC))
+		cdev_del(&driver->cdevs[index]);
 }
 EXPORT_SYMBOL(tty_unregister_device);
 
@@ -3062,6 +3086,7 @@ struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
 		unsigned long flags)
 {
 	struct tty_driver *driver;
+	unsigned int cdevs = 1;
 	int err;
 
 	if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
@@ -3095,6 +3120,13 @@ struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
 			err = -ENOMEM;
 			goto err_free_all;
 		}
+		cdevs = lines;
+	}
+
+	driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
+	if (!driver->cdevs) {
+		err = -ENOMEM;
+		goto err_free_all;
 	}
 
 	return driver;
@@ -3129,8 +3161,10 @@ static void destruct_tty_driver(struct kref *kref)
 				tty_unregister_device(driver, i);
 		}
 		proc_tty_unregister_driver(driver);
-		cdev_del(&driver->cdev);
+		if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
+			cdev_del(&driver->cdevs[0]);
 	}
+	kfree(driver->cdevs);
 	kfree(driver->ports);
 	kfree(driver->termios);
 	kfree(driver->ttys);
@@ -3180,11 +3214,11 @@ int tty_register_driver(struct tty_driver *driver)
 	if (error < 0)
 		goto err;
 
-	cdev_init(&driver->cdev, &tty_fops);
-	driver->cdev.owner = driver->owner;
-	error = cdev_add(&driver->cdev, dev, driver->num);
-	if (error)
-		goto err_unreg_char;
+	if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
+		error = tty_cdev_add(driver, dev, 0, driver->num);
+		if (error)
+			goto err_unreg_char;
+	}
 
 	mutex_lock(&tty_mutex);
 	list_add(&driver->tty_drivers, &tty_drivers);
diff --git a/include/linux/tty_driver.h b/include/linux/tty_driver.h
index 44e05b7..dd976cf 100644
--- a/include/linux/tty_driver.h
+++ b/include/linux/tty_driver.h
@@ -289,7 +289,7 @@ struct tty_operations {
 struct tty_driver {
 	int	magic;		/* magic number for this structure */
 	struct kref kref;	/* Reference management */
-	struct cdev cdev;
+	struct cdev *cdevs;
 	struct module	*owner;
 	const char	*driver_name;
 	const char	*name;
-- 
1.7.10.4



      parent reply	other threads:[~2012-08-08 20:26 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-07 19:47 [PATCH 00/41] TTY buffer in tty_port -- prep no. 4 Jiri Slaby
2012-08-07 19:47 ` [PATCH 01/41] TTY: pty, stop passing NULL to free_tty_struct Jiri Slaby
2012-08-07 19:47 ` [PATCH 02/41] TTY: 68328serial, fix compilation Jiri Slaby
2012-08-08 15:24   ` Alan Cox
2012-08-08 19:33     ` Jiri Slaby
2012-08-07 19:47 ` [PATCH 03/41] TTY: n_gsm, use tty_port_install Jiri Slaby
2012-08-07 19:47 ` [PATCH 04/41] misc: pti, add const to pci_device_id table Jiri Slaby
2012-08-07 19:47 ` [PATCH 05/41] misc: pti, pci drvdata cannot be NULL in ->remove Jiri Slaby
2012-08-07 19:47 ` [PATCH 06/41] misc: pti, stop using iomap's unmap on ioremap space Jiri Slaby
2012-08-07 19:47 ` [PATCH 07/41] misc: pti, move ->remove to the PCI code Jiri Slaby
2012-08-07 19:47 ` [PATCH 08/41] misc: pti, do the opposite of ->probe in ->remove Jiri Slaby
2012-08-07 19:47 ` [PATCH 09/41] misc: pti, fix fail paths Jiri Slaby
2012-08-07 19:47 ` [PATCH 10/41] misc: pti, fix tty_port count Jiri Slaby
2012-08-07 19:47 ` [PATCH 11/41] misc: pti, use tty_port_register_device Jiri Slaby
2012-08-07 19:47 ` [PATCH 12/41] mxser: allow overlapping vector Jiri Slaby
2012-08-13  1:50   ` Rusty Russell
2012-08-13  7:23     ` Jiri Slaby
2012-08-13  8:16       ` [PATCH] TTY: mxser, fix invalid module_parm permissions Jiri Slaby
2012-08-13  8:18       ` Jiri Slaby
2012-08-07 19:47 ` [PATCH 13/41] TTY: ttyprintk, unregister tty driver on failure Jiri Slaby
2012-08-07 19:47 ` [PATCH 14/41] TTY: ttyprintk, don't touch behind tty->write_buf Jiri Slaby
2012-08-07 19:47 ` [PATCH 15/41] TTY: ttyprintk, initialize tty_port earlier Jiri Slaby
2012-08-07 19:47 ` [PATCH 16/41] TTY: tty3270, free tty driver properly Jiri Slaby
2012-08-07 19:47 ` [PATCH 17/41] TTY: pass flags to alloc_tty_driver Jiri Slaby
2012-08-07 19:47 ` [PATCH 18/41] TTY: pty, switch to tty_alloc_driver Jiri Slaby
2012-08-08 15:37   ` Alan Cox
2012-08-08 20:11     ` Jiri Slaby
2012-08-08 22:19       ` Alan Cox
2012-08-07 19:47 ` [PATCH 19/41] TTY: move allocations " Jiri Slaby
2012-08-07 19:47 ` [PATCH 20/41] TTY: add support for unnumbered device nodes Jiri Slaby
2012-08-07 19:47 ` [PATCH 21/41] TTY: move cdev_add to tty_register_device Jiri Slaby
2012-08-07 19:47 ` [PATCH 22/41] TTY: use tty_port_register_device Jiri Slaby
2012-08-07 19:47 ` [PATCH 23/41] TTY: automatically create nodes for some drivers Jiri Slaby
2012-08-07 19:47 ` [PATCH 24/41] TTY: tty_port, add some documentation Jiri Slaby
2012-08-07 19:47 ` [PATCH 25/41] TTY: add tty_port_link_device Jiri Slaby
2012-08-07 19:47 ` [PATCH 26/41] TTY: use tty_port_link_device Jiri Slaby
2012-08-07 19:47 ` [PATCH 27/41] TTY: synclink_cs, sanitize fail paths Jiri Slaby
2012-08-07 19:47 ` [PATCH 28/41] TTY: synclink_cs, use dynamic tty devices Jiri Slaby
2012-08-07 19:47 ` [PATCH 29/41] TTY: synclink_cs, final cleanup in synclink_cs_init Jiri Slaby
2012-08-07 19:47 ` [PATCH 30/41] TTY: moxa, convert to dynamic device Jiri Slaby
2012-08-07 19:47 ` [PATCH 31/41] TTY: nfcon, add tty_port and link it Jiri Slaby
2012-08-07 19:47 ` [PATCH 32/41] TTY: con3215, unset raw3215[line] Jiri Slaby
2012-08-07 19:47 ` [PATCH 33/41] TTY: con3215, add tty install Jiri Slaby
2012-08-07 19:47 ` [PATCH 34/41] TTY: i4l, " Jiri Slaby
2012-08-07 19:48 ` [PATCH 35/41] TTY: synclink, " Jiri Slaby
2012-08-07 19:48 ` [PATCH 36/41] TTY: synclinkmp, " Jiri Slaby
2012-08-07 19:48 ` [PATCH 37/41] TTY: ircomm_tty, " Jiri Slaby
2012-08-07 19:48 ` [PATCH 38/41] TTY: tty3270, " Jiri Slaby
2012-08-07 19:48 ` [PATCH 39/41] TTY: hvc_console, " Jiri Slaby
2012-08-07 19:48 ` [PATCH 40/41] TTY: hvcs, clean hvcs_open a bit Jiri Slaby
2012-08-07 19:48 ` [PATCH 41/41] TTY: hvcs, add tty install Jiri Slaby
2012-08-08 20:26 ` [PATCH v2 18/41] TTY: pty, switch to tty_alloc_driver Jiri Slaby
2012-08-08 20:26   ` [PATCH v2 19/41] TTY: move allocations " Jiri Slaby
2012-08-08 20:26   ` [PATCH v2 20/41] TTY: add support for unnumbered device nodes Jiri Slaby
2012-08-08 20:26   ` Jiri Slaby [this message]

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=1344457604-12823-4-git-send-email-jslaby@suse.cz \
    --to=jslaby@suse.cz \
    --cc=alan@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@gmail.com \
    --cc=linux-kernel@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®