History

TTY: pty, fix pty counting

tty_operations->remove is normally called like: queue_release_one_tty ->tty_shutdown ->tty_driver_remove_tty ->tty_operations->remove However tty_shutdown() is called from queue_release_one_tty() only if tty_operations->shutdown is NULL. But for pty, it is not. pty_unix98_shutdown() is used there as ->shutdown. So tty_operations->remove of pty (i.e. pty_unix98_remove()) is never called. This results in invalid pty_count. I.e. what can be seen in /proc/sys/kernel/pty/nr. I see this was already reported at: https://lkml.org/lkml/2009/11/5/370 But it was not fixed since then. This patch is kind of a hackish way. The problem lies in ->install. We allocate there another tty (so-called tty->link). So ->install is called once, but ->remove twice, for both tty and tty->link. The fix here is to count both tty and tty->link and divide the count by 2 for user. And to have ->remove called, let's make tty_driver_remove_tty() global and call that from pty_unix98_shutdown() (tty_operations->shutdown). While at it, let's document that when ->shutdown is defined, tty_shutdown() is not called.

Jiri Slaby committed 15 years ago

Commit 24d406a1 parent dbb3b1c4 files · +21 −4Browse files

Unified · Split

4 files
  1. drivers/tty/pty.c +15 −2
  2. drivers/tty/tty_io.c +1 −2
  3. include/linux/tty.h +2 −0
  4. include/linux/tty_driver.h +3 −0
drivers/tty/pty.c+15−2
@@ -446,8 +446,19 @@ static inline void legacy_pty_init(void) { } · 445 unmodified lines
446446 int pty_limit = NR_UNIX98_PTY_DEFAULT;
447447 static int pty_limit_min;
448448 static int pty_limit_max = NR_UNIX98_PTY_MAX;
449+static int tty_count;
449450 static int pty_count;
450451
452+static inline void pty_inc_count(void)
453+{
454+ pty_count = (++tty_count) / 2;
455+}
456+
457+static inline void pty_dec_count(void)
458+{
459+ pty_count = (--tty_count) / 2;
460+}
461+
451462 static struct cdev ptmx_cdev;
452463
453464 static struct ctl_table pty_table[] = {
@@ -542,6 +553,7 @@ static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, · 88 unmodified lines
542553
543554 static void pty_unix98_shutdown(struct tty_struct *tty)
544555 {
556+ tty_driver_remove_tty(tty->driver, tty);
545557 /* We have our own method as we don't use the tty index */
546558 kfree(tty->termios);
547559 }
@@ -588,7 +600,8 @@ static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) · 40 unmodified lines
588600 */
589601 tty_driver_kref_get(driver);
590602 tty->count++;
591− pty_count++;
603+ pty_inc_count(); /* tty */
604+ pty_inc_count(); /* tty->link */
592605 return 0;
593606 err_free_mem:
594607 deinitialize_tty_struct(o_tty);
@@ -602,7 +615,7 @@ err_free_tty: · 7 unmodified lines
602615
603616 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
604617 {
605− pty_count--;
618+ pty_dec_count();
606619 }
607620
608621 static const struct tty_operations ptm_unix98_ops = {
drivers/tty/tty_io.c+1−2
@@ -1295,8 +1295,7 @@ static int tty_driver_install_tty(struct tty_driver *driver, · 1294 unmodified lines
12951295 *
12961296 * Locking: tty_mutex for now
12971297 */
1298−static void tty_driver_remove_tty(struct tty_driver *driver,
1299− struct tty_struct *tty)
1298+void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
13001299 {
13011300 if (driver->ops->remove)
13021301 driver->ops->remove(driver, tty);
include/linux/tty.h+2−0
@@ -421,6 +421,8 @@ extern void tty_driver_flush_buffer(struct tty_struct *tty); · 420 unmodified lines
421421 extern void tty_throttle(struct tty_struct *tty);
422422 extern void tty_unthrottle(struct tty_struct *tty);
423423 extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
424+extern void tty_driver_remove_tty(struct tty_driver *driver,
425+ struct tty_struct *tty);
424426 extern void tty_shutdown(struct tty_struct *tty);
425427 extern void tty_free_termios(struct tty_struct *tty);
426428 extern int is_current_pgrp_orphaned(void);
include/linux/tty_driver.h+3−0
@@ -47,6 +47,9 @@ · 46 unmodified lines
4747 *
4848 * This routine is called synchronously when a particular tty device
4949 * is closed for the last time freeing up the resources.
50+ * Note that tty_shutdown() is not called if ops->shutdown is defined.
51+ * This means one is responsible to take care of calling ops->remove (e.g.
52+ * via tty_driver_remove_tty) and releasing tty->termios.
5053 *
5154 *
5255 * void (*cleanup)(struct tty_struct * tty);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t