Skip to contentSkip to conversation

tty: getting rid of __GFP_NOFAIL in tty_add_file

PATCH In review Jiri Olsa 2 participants 3 messages Sep 2, 2010 – Sep 3, 2010

Subsystems (2)
lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/1 tty: getting rid of __GFP_NOFAIL in tty_add_file

hi,

added error handling for struct tty_file_private allocation, Fixed error paths in tty_open and ptmx_open.

wbr,
jirka

---
 drivers/char/pty.c    |    9 ++++++++-
 drivers/char/tty_io.c |   26 +++++++++++++++++++-------
 include/linux/tty.h   |    4 +++-
 3 files changed, 30 insertions(+), 9 deletions(-)
drivers/char/pty.c +8 -1
index c350d01..cc4f861 100644
@@ -652,6 +652,7 @@ static const struct tty_operations pty_unix98_ops = {
 static int ptmx_open(struct inode *inode, struct file *filp)
 {
 	struct tty_struct *tty;
+	struct tty_file_private *priv;
 	int retval;
 	int index;
 
@@ -664,6 +665,12 @@ static int ptmx_open(struct inode *inode, struct file *filp)
 	if (index < 0)
 		return index;
 
+	priv = alloc_tty_priv();
+	if (!priv)
+		return -ENOMEM;
+
+	filp->private_data = priv;
+
 	mutex_lock(&tty_mutex);
 	tty_lock();
 	tty = tty_init_dev(ptm_driver, index, 1);
@@ -676,7 +683,7 @@ static int ptmx_open(struct inode *inode, struct file *filp)
 
 	set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
 
-	tty_add_file(tty, filp);
+	tty_set_priv(priv, tty, filp);
 
 	retval = devpts_pty_new(inode, tty->link);
 	if (retval)
drivers/char/tty_io.c +19 -7
index 949067a..99d8bcb 100644
@@ -193,17 +193,19 @@ static inline struct tty_struct *file_tty(struct file *file)
 	return ((struct tty_file_private *)file->private_data)->tty;
 }
 
-/* Associate a new file with the tty structure */
-void tty_add_file(struct tty_struct *tty, struct file *file)
+
+struct tty_file_private *alloc_tty_priv(void)
 {
-	struct tty_file_private *priv;
+	return kzalloc(sizeof(struct tty_file_private), GFP_KERNEL);
+}
 
-	/* XXX: must implement proper error handling in callers */
-	priv = kmalloc(sizeof(*priv), GFP_KERNEL|__GFP_NOFAIL);
 
+/* Associate a new file with the tty structure */
+void tty_set_priv(struct tty_file_private *priv, struct tty_struct *tty,
+		  struct file *file)
+{
 	priv->tty = tty;
 	priv->file = file;
-	file->private_data = priv;
 
 	spin_lock(&tty_files_lock);
 	list_add(&priv->list, &tty->tty_files);
@@ -1787,6 +1789,7 @@ int tty_release(struct inode *inode, struct file *filp)
 static int tty_open(struct inode *inode, struct file *filp)
 {
 	struct tty_struct *tty = NULL;
+	struct tty_file_private *priv;
 	int noctty, retval;
 	struct tty_driver *driver;
 	int index;
@@ -1861,6 +1864,15 @@ got_driver:
 		}
 	}
 
+	priv = alloc_tty_priv();
+	if (!priv) {
+		tty_unlock();
+		mutex_unlock(&tty_mutex);
+		return -ENOMEM;
+	}
+
+	filp->private_data = priv;
+
 	if (tty) {
 		retval = tty_reopen(tty);
 		if (retval)
@@ -1875,7 +1887,7 @@ got_driver:
 		return PTR_ERR(tty);
 	}
 
-	tty_add_file(tty, filp);
+	tty_set_priv(priv, tty, filp);
 
 	check_tty_count(tty, "tty_open");
 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
include/linux/tty.h +3 -1
index 67d64e6..48705aa 100644
@@ -465,7 +465,9 @@ extern void proc_clear_tty(struct task_struct *p);
 extern struct tty_struct *get_current_tty(void);
 extern void tty_default_fops(struct file_operations *fops);
 extern struct tty_struct *alloc_tty_struct(void);
-extern void tty_add_file(struct tty_struct *tty, struct file *file);
+struct tty_file_private *alloc_tty_priv(void);
+void tty_set_priv(struct tty_file_private *priv, struct tty_struct *tty,
+		struct file *file);
 extern void free_tty_struct(struct tty_struct *tty);
 extern void initialize_tty_struct(struct tty_struct *tty,
 		struct tty_driver *driver, int idx);
Signature · 1 line
1.7.1
#1 lore raw
Quoted 4 lines · Jiri Olsa

hi,

added error handling for struct tty_file_private allocation, Fixed error paths in tty_open and ptmx_open.

Heh, look at the linux-next tree, there's already a patch from Pekka in there that does this, but in a little different way. Let me know if that version doesn't work for you.

thanks,

greg k-h

author
#2 lore raw
Quoted 9 lines · Greg KH
Quoted 4 lines · Jiri Olsa

hi,

added error handling for struct tty_file_private allocation, Fixed error paths in tty_open and ptmx_open.

Heh, look at the linux-next tree, there's already a patch from Pekka in there that does this, but in a little different way. Let me know if that version doesn't work for you.

cool ;)

I think the current error paths do not release some memory
- tty_struct in tty_open path
- tty_struct/dev_pts_index/inode in ptmx_open error paths

question is if that's a big deal in case we were not able to allocate as much as struct tty_file_private..

I'm attaching my patch rebased for linux-next, but I would not mind this one being omitted for the reason above.

thanks,
jirka

---
Added error handling for struct tty_file_private allocation, Fixed error paths in tty_open and ptmx_open.

---
 drivers/char/pty.c    |   31 +++++++++++++++++------------
 drivers/char/tty_io.c |   52 +++++++++++++++++++++++++++++++-----------------
 include/linux/tty.h   |    5 +++-
 3 files changed, 55 insertions(+), 33 deletions(-)
drivers/char/pty.c +18 -13
index 923a485..08a449c 100644
@@ -652,17 +652,24 @@ static const struct tty_operations pty_unix98_ops = {
 static int ptmx_open(struct inode *inode, struct file *filp)
 {
 	struct tty_struct *tty;
+	struct tty_file_private *priv;
 	int retval;
 	int index;
 
 	nonseekable_open(inode, filp);
 
+	priv = alloc_tty_priv();
+	if (!priv)
+		return -ENOMEM;
+
 	/* find a device that is not in use. */
 	tty_lock();
 	index = devpts_new_index(inode);
 	tty_unlock();
-	if (index < 0)
+	if (index < 0) {
+		free_tty_priv(priv);
 		return index;
+	}
 
 	mutex_lock(&tty_mutex);
 	tty_lock();
@@ -671,32 +678,30 @@ static int ptmx_open(struct inode *inode, struct file *filp)
 
 	if (IS_ERR(tty)) {
 		retval = PTR_ERR(tty);
+		free_tty_priv(priv);
+		devpts_kill_index(inode, index);
 		goto out;
 	}
 
 	set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
-
-	retval = tty_add_file(tty, filp);
-	if (retval)
-		goto out;
+	tty_set_priv(priv, tty, filp);
 
 	retval = devpts_pty_new(inode, tty->link);
 	if (retval)
-		goto out1;
+		goto out_release;
 
 	retval = ptm_driver->ops->open(tty, filp);
 	if (retval)
-		goto out2;
-out1:
-	tty_unlock();
-	return retval;
-out2:
+		goto out_release;
+
+out:
 	tty_unlock();
-	tty_release(inode, filp);
 	return retval;
-out:
+
+out_release:
 	devpts_kill_index(inode, index);
 	tty_unlock();
+	tty_release(inode, filp);
 	return retval;
 }
 
drivers/char/tty_io.c +33 -19

101 lines not on this page · raw message

include/linux/tty.h +4 -1
index 86be0cd..1520a35 100644
@@ -465,8 +465,11 @@ extern dev_t tty_devnum(struct tty_struct *tty);
 extern void proc_clear_tty(struct task_struct *p);
 extern struct tty_struct *get_current_tty(void);
 extern void tty_default_fops(struct file_operations *fops);
+struct tty_file_private *alloc_tty_priv(void);
+void tty_set_priv(struct tty_file_private *priv, struct tty_struct *tty,
+		struct file *file);
+void free_tty_priv(struct tty_file_private *priv);
 extern struct tty_struct *alloc_tty_struct(void);
-extern int tty_add_file(struct tty_struct *tty, struct file *file);
 extern void free_tty_struct(struct tty_struct *tty);
 extern void initialize_tty_struct(struct tty_struct *tty,
 		struct tty_driver *driver, int idx);
Signature · 1 line
1.7.1