2015-11-28 02:38:46

by Peter Hurley

[permalink] [raw]
Subject: [PATCH 0/6]

Hi Greg,

This patch series is part of the ongoing effort to cleanup and reduce the
kernel tty interface; specifically removing undefined function declarations,
unused macros, encapsulating global data, undefining unused exports,
limiting functions and data to file scope, etc.

Regards,

Peter Hurley (6):
tty: Unexport system-wide tty_mutex
tty: Eliminate global symbol tty_ldisc_N_TTY
tty: Remove declarations to non-existent functions
tty: Move tty_check_change() helper
tty: Remove unreferenced tty flags macro TTY_DEBUG
tty: Make tty_files_lock per-tty

drivers/tty/n_tty.c | 18 ++++++++----------
drivers/tty/tty_io.c | 37 ++++++++++++++++++++-----------------
drivers/tty/tty_ldisc.c | 6 ------
include/linux/tty.h | 10 ++--------
security/selinux/hooks.c | 4 ++--
5 files changed, 32 insertions(+), 43 deletions(-)

--
2.6.3


2015-11-28 02:40:09

by Peter Hurley

[permalink] [raw]
Subject: [PATCH 1/6] tty: Unexport system-wide tty_mutex

tty_mutex is a core, system-wide lock; there is no reason for any
code outside the tty core to have direct access.

Signed-off-by: Peter Hurley <[email protected]>
---
drivers/tty/tty_io.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 09e8d4f..64c4a7e 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -135,10 +135,8 @@ EXPORT_SYMBOL(tty_std_termios);

LIST_HEAD(tty_drivers); /* linked list of tty drivers */

-/* Mutex to protect creating and releasing a tty. This is shared with
- vt.c for deeply disgusting hack reasons */
+/* Mutex to protect creating and releasing a tty */
DEFINE_MUTEX(tty_mutex);
-EXPORT_SYMBOL(tty_mutex);

/* Spinlock to protect the tty->tty_files list */
DEFINE_SPINLOCK(tty_files_lock);
--
2.6.3

2015-11-28 02:40:04

by Peter Hurley

[permalink] [raw]
Subject: [PATCH 2/6] tty: Eliminate global symbol tty_ldisc_N_TTY

Reduce global tty symbols; move and rename tty_ldisc_begin() as
n_tty_init() and redefine the N_TTY ldisc ops as file scope.

Signed-off-by: Peter Hurley <[email protected]>
---
drivers/tty/n_tty.c | 12 ++++++++----
drivers/tty/tty_io.c | 2 +-
drivers/tty/tty_ldisc.c | 6 ------
include/linux/tty.h | 3 +--
4 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 4f2b5dc..d294e24 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -2499,7 +2499,7 @@ static void n_tty_closing(struct tty_struct *tty)
ldata->closing = 1;
}

-struct tty_ldisc_ops tty_ldisc_N_TTY = {
+static struct tty_ldisc_ops n_tty_ops = {
.magic = TTY_LDISC_MAGIC,
.name = "n_tty",
.open = n_tty_open,
@@ -2521,14 +2521,18 @@ struct tty_ldisc_ops tty_ldisc_N_TTY = {
* n_tty_inherit_ops - inherit N_TTY methods
* @ops: struct tty_ldisc_ops where to save N_TTY methods
*
- * Enables a 'subclass' line discipline to 'inherit' N_TTY
- * methods.
+ * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
*/

void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
{
- *ops = tty_ldisc_N_TTY;
+ *ops = n_tty_ops;
ops->owner = NULL;
ops->refcount = ops->flags = 0;
}
EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
+
+void __init n_tty_init(void)
+{
+ tty_register_ldisc(N_TTY, &n_tty_ops);
+}
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 64c4a7e..0778fa9 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -3566,7 +3566,7 @@ void __init console_init(void)
initcall_t *call;

/* Setup the default TTY line discipline. */
- tty_ldisc_begin();
+ n_tty_init();

/*
* set up the console device so that later boot sequences can
diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c
index 8a960fd..5734c22 100644
--- a/drivers/tty/tty_ldisc.c
+++ b/drivers/tty/tty_ldisc.c
@@ -840,9 +840,3 @@ void tty_ldisc_deinit(struct tty_struct *tty)
tty_ldisc_put(tty->ldisc);
tty->ldisc = NULL;
}
-
-void tty_ldisc_begin(void)
-{
- /* Setup the default TTY line discipline. */
- (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
-}
diff --git a/include/linux/tty.h b/include/linux/tty.h
index cc15147..9e3231e 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -577,7 +577,6 @@ extern int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty);
extern void tty_ldisc_release(struct tty_struct *tty);
extern void tty_ldisc_init(struct tty_struct *tty);
extern void tty_ldisc_deinit(struct tty_struct *tty);
-extern void tty_ldisc_begin(void);

static inline int tty_ldisc_receive_buf(struct tty_ldisc *ld, unsigned char *p,
char *f, int count)
@@ -594,8 +593,8 @@ static inline int tty_ldisc_receive_buf(struct tty_ldisc *ld, unsigned char *p,


/* n_tty.c */
-extern struct tty_ldisc_ops tty_ldisc_N_TTY;
extern void n_tty_inherit_ops(struct tty_ldisc_ops *ops);
+extern void __init n_tty_init(void);

/* tty_audit.c */
#ifdef CONFIG_AUDIT
--
2.6.3

2015-11-28 02:39:57

by Peter Hurley

[permalink] [raw]
Subject: [PATCH 3/6] tty: Remove declarations to non-existent functions

tty_read_raw_data() and tty_signal() no longer exist; remove
declarations.

Signed-off-by: Peter Hurley <[email protected]>
---
include/linux/tty.h | 3 ---
1 file changed, 3 deletions(-)

diff --git a/include/linux/tty.h b/include/linux/tty.h
index 9e3231e..584bf4c 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -432,8 +432,6 @@ extern struct device *tty_register_device_attr(struct tty_driver *driver,
void *drvdata,
const struct attribute_group **attr_grp);
extern void tty_unregister_device(struct tty_driver *driver, unsigned index);
-extern int tty_read_raw_data(struct tty_struct *tty, unsigned char *bufp,
- int buflen);
extern void tty_write_message(struct tty_struct *tty, char *msg);
extern int tty_send_xchar(struct tty_struct *tty, char ch);
extern int tty_put_char(struct tty_struct *tty, unsigned char c);
@@ -447,7 +445,6 @@ extern int tty_unthrottle_safe(struct tty_struct *tty);
extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
extern int is_current_pgrp_orphaned(void);
extern int is_ignored(int sig);
-extern int tty_signal(int sig, struct tty_struct *tty);
extern void tty_hangup(struct tty_struct *tty);
extern void tty_vhangup(struct tty_struct *tty);
extern int tty_hung_up_p(struct file *filp);
--
2.6.3

2015-11-28 02:39:09

by Peter Hurley

[permalink] [raw]
Subject: [PATCH 4/6] tty: Move tty_check_change() helper

Move is_ignored() to drivers/tty/tty_io.c and re-declare in file
scope.

Signed-off-by: Peter Hurley <[email protected]>
---
drivers/tty/n_tty.c | 6 ------
drivers/tty/tty_io.c | 6 ++++++
include/linux/tty.h | 1 -
3 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index d294e24..f61be24 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -1753,12 +1753,6 @@ static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
return n_tty_receive_buf_common(tty, cp, fp, count, 1);
}

-int is_ignored(int sig)
-{
- return (sigismember(&current->blocked, sig) ||
- current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
-}
-
/**
* n_tty_set_termios - termios data changed
* @tty: terminal
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 0778fa9..a8a1f62 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -381,6 +381,12 @@ struct tty_driver *tty_find_polling_driver(char *name, int *line)
EXPORT_SYMBOL_GPL(tty_find_polling_driver);
#endif

+static int is_ignored(int sig)
+{
+ return (sigismember(&current->blocked, sig) ||
+ current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
+}
+
/**
* tty_check_change - check for POSIX terminal changes
* @tty: tty to check
diff --git a/include/linux/tty.h b/include/linux/tty.h
index 584bf4c..8f16d52 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -444,7 +444,6 @@ extern int tty_throttle_safe(struct tty_struct *tty);
extern int tty_unthrottle_safe(struct tty_struct *tty);
extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
extern int is_current_pgrp_orphaned(void);
-extern int is_ignored(int sig);
extern void tty_hangup(struct tty_struct *tty);
extern void tty_vhangup(struct tty_struct *tty);
extern int tty_hung_up_p(struct file *filp);
--
2.6.3

2015-11-28 02:38:51

by Peter Hurley

[permalink] [raw]
Subject: [PATCH 5/6] tty: Remove unreferenced tty flags macro TTY_DEBUG

The TTY_DEBUG macro is not used; remove.

Signed-off-by: Peter Hurley <[email protected]>
---
include/linux/tty.h | 1 -
1 file changed, 1 deletion(-)

diff --git a/include/linux/tty.h b/include/linux/tty.h
index 8f16d52..168f198 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -335,7 +335,6 @@ struct tty_file_private {
#define TTY_IO_ERROR 1 /* Cause an I/O error (may be no ldisc too) */
#define TTY_OTHER_CLOSED 2 /* Other side (if any) has closed */
#define TTY_EXCLUSIVE 3 /* Exclusive open mode */
-#define TTY_DEBUG 4 /* Debugging */
#define TTY_DO_WRITE_WAKEUP 5 /* Call write_wakeup after queuing new */
#define TTY_OTHER_DONE 6 /* Closed pty has completed input processing */
#define TTY_LDISC_OPEN 11 /* Line discipline is open */
--
2.6.3

2015-11-28 02:39:05

by Peter Hurley

[permalink] [raw]
Subject: [PATCH 6/6] tty: Make tty_files_lock per-tty

Access to tty->tty_files list is always per-tty, never for all ttys
simultaneously. Replace global tty_files_lock spinlock with per-tty
->files_lock. Initialize when the ->tty_files list is inited, in
alloc_tty_struct().

Signed-off-by: Peter Hurley <[email protected]>
---
drivers/tty/tty_io.c | 25 ++++++++++++-------------
include/linux/tty.h | 2 +-
security/selinux/hooks.c | 4 ++--
3 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index a8a1f62..5c8f519 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -138,9 +138,6 @@ LIST_HEAD(tty_drivers); /* linked list of tty drivers */
/* Mutex to protect creating and releasing a tty */
DEFINE_MUTEX(tty_mutex);

-/* Spinlock to protect the tty->tty_files list */
-DEFINE_SPINLOCK(tty_files_lock);
-
static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
ssize_t redirected_tty_write(struct file *, const char __user *,
@@ -202,9 +199,9 @@ void tty_add_file(struct tty_struct *tty, struct file *file)
priv->tty = tty;
priv->file = file;

- spin_lock(&tty_files_lock);
+ spin_lock(&tty->files_lock);
list_add(&priv->list, &tty->tty_files);
- spin_unlock(&tty_files_lock);
+ spin_unlock(&tty->files_lock);
}

/**
@@ -225,10 +222,11 @@ void tty_free_file(struct file *file)
static void tty_del_file(struct file *file)
{
struct tty_file_private *priv = file->private_data;
+ struct tty_struct *tty = priv->tty;

- spin_lock(&tty_files_lock);
+ spin_lock(&tty->files_lock);
list_del(&priv->list);
- spin_unlock(&tty_files_lock);
+ spin_unlock(&tty->files_lock);
tty_free_file(file);
}

@@ -286,11 +284,11 @@ static int check_tty_count(struct tty_struct *tty, const char *routine)
struct list_head *p;
int count = 0;

- spin_lock(&tty_files_lock);
+ spin_lock(&tty->files_lock);
list_for_each(p, &tty->tty_files) {
count++;
}
- spin_unlock(&tty_files_lock);
+ spin_unlock(&tty->files_lock);
if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
tty->driver->subtype == PTY_TYPE_SLAVE &&
tty->link && tty->link->count)
@@ -713,7 +711,7 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
workqueue with the lock held */
check_tty_count(tty, "tty_hangup");

- spin_lock(&tty_files_lock);
+ spin_lock(&tty->files_lock);
/* This breaks for file handles being sent over AF_UNIX sockets ? */
list_for_each_entry(priv, &tty->tty_files, list) {
filp = priv->file;
@@ -725,7 +723,7 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
__tty_fasync(-1, filp, 0); /* can't block */
filp->f_op = &hung_up_tty_fops;
}
- spin_unlock(&tty_files_lock);
+ spin_unlock(&tty->files_lock);

refs = tty_signal_session_leader(tty, exit_session);
/* Account for the p->signal references we killed */
@@ -1641,9 +1639,9 @@ static void release_one_tty(struct work_struct *work)
tty_driver_kref_put(driver);
module_put(owner);

- spin_lock(&tty_files_lock);
+ spin_lock(&tty->files_lock);
list_del_init(&tty->tty_files);
- spin_unlock(&tty_files_lock);
+ spin_unlock(&tty->files_lock);

put_pid(tty->pgrp);
put_pid(tty->session);
@@ -3166,6 +3164,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
mutex_init(&tty->atomic_write_lock);
spin_lock_init(&tty->ctrl_lock);
spin_lock_init(&tty->flow_lock);
+ spin_lock_init(&tty->files_lock);
INIT_LIST_HEAD(&tty->tty_files);
INIT_WORK(&tty->SAK_work, do_SAK_work);

diff --git a/include/linux/tty.h b/include/linux/tty.h
index 168f198..a2d167e 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -302,6 +302,7 @@ struct tty_struct {
struct work_struct hangup_work;
void *disc_data;
void *driver_data;
+ spinlock_t files_lock; /* protects tty_files list */
struct list_head tty_files;

#define N_TTY_BUF_SIZE 4096
@@ -509,7 +510,6 @@ extern int tty_standard_install(struct tty_driver *driver,
struct tty_struct *tty);

extern struct mutex tty_mutex;
-extern spinlock_t tty_files_lock;

#define tty_is_writelocked(tty) (mutex_is_locked(&tty->atomic_write_lock))

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index d0cfaa9..ae6dbc75 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2352,7 +2352,7 @@ static inline void flush_unauthorized_files(const struct cred *cred,

tty = get_current_tty();
if (tty) {
- spin_lock(&tty_files_lock);
+ spin_lock(&tty->files_lock);
if (!list_empty(&tty->tty_files)) {
struct tty_file_private *file_priv;

@@ -2367,7 +2367,7 @@ static inline void flush_unauthorized_files(const struct cred *cred,
if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
drop_tty = 1;
}
- spin_unlock(&tty_files_lock);
+ spin_unlock(&tty->files_lock);
tty_kref_put(tty);
}
/* Reset controlling tty. */
--
2.6.3