Commit f0de0e8d authored by Linus Torvalds's avatar Linus Torvalds Committed by Live-CD User

tty-ldisc: make /proc/tty/ldiscs use ldisc_ops instead of ldiscs

The /proc/tty/ldiscs file is totally and utterly un-interested in the
"struct tty_ldisc" structures, and only cares about the underlying ldisc
operations.

So don't make it create a dummy 'struct ldisc' only to get a pointer to
the operations, and then destroy it.  Instead, we split up the function
'tty_ldisc_try_get()', and create a 'get_ldops()' helper that just looks
up the ldisc operations based on the ldisc number.

That makes the code simpler to read (smaller and more well-defined
helper functions), and allows the /proc functions to avoid creating that
useless dummy only to immediately free it again.
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
Tested-by: default avatarSergey Senozhatsky <sergey.senozhatsky@mail.by>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>
Cc: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 7a4b2310
...@@ -145,6 +145,34 @@ int tty_unregister_ldisc(int disc) ...@@ -145,6 +145,34 @@ int tty_unregister_ldisc(int disc)
} }
EXPORT_SYMBOL(tty_unregister_ldisc); EXPORT_SYMBOL(tty_unregister_ldisc);
static struct tty_ldisc_ops *get_ldops(int disc)
{
unsigned long flags;
struct tty_ldisc_ops *ldops, *ret;
spin_lock_irqsave(&tty_ldisc_lock, flags);
ret = ERR_PTR(-EINVAL);
ldops = tty_ldiscs[disc];
if (ldops) {
ret = ERR_PTR(-EAGAIN);
if (try_module_get(ldops->owner)) {
ldops->refcount++;
ret = ldops;
}
}
spin_unlock_irqrestore(&tty_ldisc_lock, flags);
return ret;
}
static void put_ldops(struct tty_ldisc_ops *ldops)
{
unsigned long flags;
spin_lock_irqsave(&tty_ldisc_lock, flags);
ldops->refcount--;
module_put(ldops->owner);
spin_unlock_irqrestore(&tty_ldisc_lock, flags);
}
/** /**
* tty_ldisc_try_get - try and reference an ldisc * tty_ldisc_try_get - try and reference an ldisc
...@@ -156,36 +184,21 @@ EXPORT_SYMBOL(tty_unregister_ldisc); ...@@ -156,36 +184,21 @@ EXPORT_SYMBOL(tty_unregister_ldisc);
static struct tty_ldisc *tty_ldisc_try_get(int disc) static struct tty_ldisc *tty_ldisc_try_get(int disc)
{ {
unsigned long flags;
struct tty_ldisc *ld; struct tty_ldisc *ld;
struct tty_ldisc_ops *ldops; struct tty_ldisc_ops *ldops;
int err = -EINVAL;
ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL); ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
if (ld == NULL) if (ld == NULL)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
spin_lock_irqsave(&tty_ldisc_lock, flags); ldops = get_ldops(disc);
ld->ops = NULL; if (IS_ERR(ldops)) {
ldops = tty_ldiscs[disc];
/* Check the entry is defined */
if (ldops) {
/* If the module is being unloaded we can't use it */
if (!try_module_get(ldops->owner))
err = -EAGAIN;
else {
/* lock it */
ldops->refcount++;
ld->ops = ldops;
atomic_set(&ld->users, 1);
err = 0;
}
}
spin_unlock_irqrestore(&tty_ldisc_lock, flags);
if (err) {
kfree(ld); kfree(ld);
return ERR_PTR(err); return ERR_CAST(ldops);
} }
ld->ops = ldops;
atomic_set(&ld->users, 1);
return ld; return ld;
} }
...@@ -234,13 +247,13 @@ static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) ...@@ -234,13 +247,13 @@ static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
static int tty_ldiscs_seq_show(struct seq_file *m, void *v) static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
{ {
int i = *(loff_t *)v; int i = *(loff_t *)v;
struct tty_ldisc *ld; struct tty_ldisc_ops *ldops;
ld = tty_ldisc_try_get(i); ldops = get_ldops(i);
if (IS_ERR(ld)) if (IS_ERR(ldops))
return 0; return 0;
seq_printf(m, "%-10s %2d\n", ld->ops->name ? ld->ops->name : "???", i); seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
put_ldisc(ld); put_ldops(ldops);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment