Skip to content

Commit fe6a094

Browse files
torvaldsmaranwilson
authored andcommitted
devpts: clean up interface to pty drivers
This gets rid of the horrible notion of having that struct inode *ptmx_inode be the linchpin of the interface between the pty code and devpts. By de-emphasizing the ptmx inode, a lot of things actually get cleaner, and we will have a much saner way forward. In particular, this will allow us to associate with any particular devpts instance at open-time, and not be artificially tied to one particular ptmx inode. The patch itself is actually fairly straightforward, and apart from some locking and return path cleanups it's pretty mechanical: - the interfaces that devpts exposes all take "struct pts_fs_info *" instead of "struct inode *ptmx_inode" now. NOTE! The "struct pts_fs_info" thing is a completely opaque structure as far as the pty driver is concerned: it's still declared entirely internally to devpts. So the pty code can't actually access it in any way, just pass it as a "cookie" to the devpts code. - the "look up the pts fs info" is now a single clear operation, that also does the reference count increment on the pts superblock. So "devpts_add/del_ref()" is gone, and replaced by a "lookup and get ref" operation (devpts_get_ref(inode)), along with a "put ref" op (devpts_put_ref()). - the pty master "tty->driver_data" field now contains the pts_fs_info, not the ptmx inode. - because we don't care about the ptmx inode any more as some kind of base index, the ref counting can now drop the inode games - it just gets the ref on the superblock. - the pts_fs_info now has a back-pointer to the super_block. That's so that we can easily look up the information we actually need. Although quite often, the pts fs info was actually all we wanted, and not having to look it up based on some magical inode makes things more straightforward. In particular, now that "devpts_get_ref(inode)" operation should really be the *only* place we need to look up what devpts instance we're associated with, and we do it exactly once, at ptmx_open() time. The other side of this is that one ptmx node could now be associated with multiple different devpts instances - you could have a single /dev/ptmx node, and then have multiple mount namespaces with their own instances of devpts mounted on /dev/pts/. And that's all perfectly sane in a model where we just look up the pts instance at open time. This will eventually allow us to get rid of our odd single-vs-multiple pts instance model, but this patch in itself changes no semantics, only an internal binding model. Cc: Eric Biederman <[email protected]> Cc: Peter Anvin <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Al Viro <[email protected]> Cc: Peter Hurley <[email protected]> Cc: Serge Hallyn <[email protected]> Cc: Willy Tarreau <[email protected]> Cc: Aurelien Jarno <[email protected]> Cc: Alan Cox <[email protected]> Cc: Jann Horn <[email protected]> Cc: Greg KH <[email protected]> Cc: Jiri Slaby <[email protected]> Cc: Florian Weimer <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> (cherry picked from commit 67245ff) Orabug: 26743034 Signed-off-by: Maran Wilson <[email protected]> Reviewed-by: Wim ten Have <[email protected]> Reviewed-by: Konrad Rzeszutek Wilk <[email protected]> Conflicts: drivers/tty/pty.c fs/devpts/inode.c There are two patches present in mainline that came before this one which are still missing from UEK. They are: 1) pty: Remove pty_unix98_shutdown() responsible for the conflict in drivers/tty/pty.c 2) devpts: if initialization failed, don't crash when opening /dev/ptmx responsible for the conflict in fs/devpts/inode.c Neither seemed like they were critical enough nor directly tied to the patch I wanted, to justify pulling them along for the ride. So intead, I manually resolved the conflicting chunks of code, applying only the deltas that were related to "devpts: clean up interface to pty drivers" in a way that makes sense for that particular patch.
1 parent 99c5ec2 commit fe6a094

File tree

3 files changed

+61
-78
lines changed

3 files changed

+61
-78
lines changed

drivers/tty/pty.c

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,14 @@ static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
670670
/* this is called once with whichever end is closed last */
671671
static void pty_unix98_shutdown(struct tty_struct *tty)
672672
{
673-
struct inode *ptmx_inode;
673+
struct pts_fs_info *fsi;
674674

675675
if (tty->driver->subtype == PTY_TYPE_MASTER)
676-
ptmx_inode = tty->driver_data;
676+
fsi = tty->driver_data;
677677
else
678-
ptmx_inode = tty->link->driver_data;
679-
devpts_kill_index(ptmx_inode, tty->index);
680-
devpts_del_ref(ptmx_inode);
678+
fsi = tty->link->driver_data;
679+
devpts_kill_index(fsi, tty->index);
680+
devpts_put_ref(fsi);
681681
}
682682

683683
static const struct tty_operations ptm_unix98_ops = {
@@ -729,6 +729,7 @@ static const struct tty_operations pty_unix98_ops = {
729729

730730
static int ptmx_open(struct inode *inode, struct file *filp)
731731
{
732+
struct pts_fs_info *fsi;
732733
struct tty_struct *tty;
733734
struct inode *slave_inode;
734735
int retval;
@@ -743,47 +744,41 @@ static int ptmx_open(struct inode *inode, struct file *filp)
743744
if (retval)
744745
return retval;
745746

747+
fsi = devpts_get_ref(inode, filp);
748+
retval = -ENODEV;
749+
if (!fsi)
750+
goto out_free_file;
751+
746752
/* find a device that is not in use. */
747753
mutex_lock(&devpts_mutex);
748-
index = devpts_new_index(inode);
749-
if (index < 0) {
750-
retval = index;
751-
mutex_unlock(&devpts_mutex);
752-
goto err_file;
753-
}
754-
754+
index = devpts_new_index(fsi);
755755
mutex_unlock(&devpts_mutex);
756756

757-
mutex_lock(&tty_mutex);
758-
tty = tty_init_dev(ptm_driver, index);
757+
retval = index;
758+
if (index < 0)
759+
goto out_put_ref;
759760

760-
if (IS_ERR(tty)) {
761-
retval = PTR_ERR(tty);
762-
goto out;
763-
}
764761

762+
mutex_lock(&tty_mutex);
763+
tty = tty_init_dev(ptm_driver, index);
765764
/* The tty returned here is locked so we can safely
766765
drop the mutex */
767766
mutex_unlock(&tty_mutex);
768767

769-
set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
770-
tty->driver_data = inode;
768+
retval = PTR_ERR(tty);
769+
if (IS_ERR(tty))
770+
goto out;
771771

772772
/*
773-
* In the case where all references to ptmx inode are dropped and we
774-
* still have /dev/tty opened pointing to the master/slave pair (ptmx
775-
* is closed/released before /dev/tty), we must make sure that the inode
776-
* is still valid when we call the final pty_unix98_shutdown, thus we
777-
* hold an additional reference to the ptmx inode. For the same /dev/tty
778-
* last close case, we also need to make sure the super_block isn't
779-
* destroyed (devpts instance unmounted), before /dev/tty is closed and
780-
* on its release devpts_kill_index is called.
773+
* From here on out, the tty is "live", and the index and
774+
* fsi will be killed/put by the tty_release()
781775
*/
782-
devpts_add_ref(inode);
776+
set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
777+
tty->driver_data = fsi;
783778

784779
tty_add_file(tty, filp);
785780

786-
slave_inode = devpts_pty_new(inode,
781+
slave_inode = devpts_pty_new(fsi,
787782
MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index,
788783
tty->link);
789784
if (IS_ERR(slave_inode)) {
@@ -800,12 +795,14 @@ static int ptmx_open(struct inode *inode, struct file *filp)
800795
return 0;
801796
err_release:
802797
tty_unlock(tty);
798+
// This will also put-ref the fsi
803799
tty_release(inode, filp);
804800
return retval;
805801
out:
806-
mutex_unlock(&tty_mutex);
807-
devpts_kill_index(inode, index);
808-
err_file:
802+
devpts_kill_index(fsi, index);
803+
out_put_ref:
804+
devpts_put_ref(fsi);
805+
out_free_file:
809806
tty_free_file(filp);
810807
return retval;
811808
}

fs/devpts/inode.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static const match_table_t tokens = {
128128
struct pts_fs_info {
129129
struct ida allocated_ptys;
130130
struct pts_mount_opts mount_opts;
131+
struct super_block *sb;
131132
struct dentry *ptmx_dentry;
132133
};
133134

@@ -356,7 +357,7 @@ static const struct super_operations devpts_sops = {
356357
.show_options = devpts_show_options,
357358
};
358359

359-
static void *new_pts_fs_info(void)
360+
static void *new_pts_fs_info(struct super_block *sb)
360361
{
361362
struct pts_fs_info *fsi;
362363

@@ -367,6 +368,7 @@ static void *new_pts_fs_info(void)
367368
ida_init(&fsi->allocated_ptys);
368369
fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE;
369370
fsi->mount_opts.ptmxmode = DEVPTS_DEFAULT_PTMX_MODE;
371+
fsi->sb = sb;
370372

371373
return fsi;
372374
}
@@ -382,7 +384,7 @@ devpts_fill_super(struct super_block *s, void *data, int silent)
382384
s->s_op = &devpts_sops;
383385
s->s_time_gran = 1;
384386

385-
s->s_fs_info = new_pts_fs_info();
387+
s->s_fs_info = new_pts_fs_info(s);
386388
if (!s->s_fs_info)
387389
goto fail;
388390

@@ -522,10 +524,8 @@ static struct file_system_type devpts_fs_type = {
522524
* to the System V naming convention
523525
*/
524526

525-
int devpts_new_index(struct inode *ptmx_inode)
527+
int devpts_new_index(struct pts_fs_info *fsi)
526528
{
527-
struct super_block *sb = pts_sb_from_inode(ptmx_inode);
528-
struct pts_fs_info *fsi = DEVPTS_SB(sb);
529529
int index;
530530
int ida_ret;
531531

@@ -558,11 +558,8 @@ int devpts_new_index(struct inode *ptmx_inode)
558558
return index;
559559
}
560560

561-
void devpts_kill_index(struct inode *ptmx_inode, int idx)
561+
void devpts_kill_index(struct pts_fs_info *fsi, int idx)
562562
{
563-
struct super_block *sb = pts_sb_from_inode(ptmx_inode);
564-
struct pts_fs_info *fsi = DEVPTS_SB(sb);
565-
566563
mutex_lock(&allocated_ptys_lock);
567564
ida_remove(&fsi->allocated_ptys, idx);
568565
pty_count--;
@@ -572,21 +569,25 @@ void devpts_kill_index(struct inode *ptmx_inode, int idx)
572569
/*
573570
* pty code needs to hold extra references in case of last /dev/tty close
574571
*/
575-
576-
void devpts_add_ref(struct inode *ptmx_inode)
572+
struct pts_fs_info *devpts_get_ref(struct inode *ptmx_inode, struct file *file)
577573
{
578-
struct super_block *sb = pts_sb_from_inode(ptmx_inode);
574+
struct super_block *sb;
575+
struct pts_fs_info *fsi;
576+
577+
sb = pts_sb_from_inode(ptmx_inode);
578+
if (!sb)
579+
return NULL;
580+
fsi = DEVPTS_SB(sb);
581+
if (!fsi)
582+
return NULL;
579583

580584
atomic_inc(&sb->s_active);
581-
ihold(ptmx_inode);
585+
return fsi;
582586
}
583587

584-
void devpts_del_ref(struct inode *ptmx_inode)
588+
void devpts_put_ref(struct pts_fs_info *fsi)
585589
{
586-
struct super_block *sb = pts_sb_from_inode(ptmx_inode);
587-
588-
iput(ptmx_inode);
589-
deactivate_super(sb);
590+
deactivate_super(fsi->sb);
590591
}
591592

592593
/**
@@ -598,14 +599,13 @@ void devpts_del_ref(struct inode *ptmx_inode)
598599
*
599600
* The created inode is returned. Remove it from /dev/pts/ by devpts_pty_kill.
600601
*/
601-
struct inode *devpts_pty_new(struct inode *ptmx_inode, dev_t device, int index,
602+
struct inode *devpts_pty_new(struct pts_fs_info *fsi, dev_t device, int index,
602603
void *priv)
603604
{
604605
struct dentry *dentry;
605-
struct super_block *sb = pts_sb_from_inode(ptmx_inode);
606+
struct super_block *sb = fsi->sb;
606607
struct inode *inode;
607608
struct dentry *root = sb->s_root;
608-
struct pts_fs_info *fsi = DEVPTS_SB(sb);
609609
struct pts_mount_opts *opts = &fsi->mount_opts;
610610
char s[12];
611611

include/linux/devpts_fs.h

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,24 @@
1515

1616
#include <linux/errno.h>
1717

18+
struct pts_fs_info;
19+
1820
#ifdef CONFIG_UNIX98_PTYS
1921

20-
int devpts_new_index(struct inode *ptmx_inode);
21-
void devpts_kill_index(struct inode *ptmx_inode, int idx);
22-
void devpts_add_ref(struct inode *ptmx_inode);
23-
void devpts_del_ref(struct inode *ptmx_inode);
22+
/* Look up a pts fs info and get a ref to it */
23+
struct pts_fs_info *devpts_get_ref(struct inode *, struct file *);
24+
void devpts_put_ref(struct pts_fs_info *);
25+
26+
int devpts_new_index(struct pts_fs_info *);
27+
void devpts_kill_index(struct pts_fs_info *, int);
28+
2429
/* mknod in devpts */
25-
struct inode *devpts_pty_new(struct inode *ptmx_inode, dev_t device, int index,
26-
void *priv);
30+
struct inode *devpts_pty_new(struct pts_fs_info *, dev_t, int, void *);
2731
/* get private structure */
2832
void *devpts_get_priv(struct inode *pts_inode);
2933
/* unlink */
3034
void devpts_pty_kill(struct inode *inode);
3135

32-
#else
33-
34-
/* Dummy stubs in the no-pty case */
35-
static inline int devpts_new_index(struct inode *ptmx_inode) { return -EINVAL; }
36-
static inline void devpts_kill_index(struct inode *ptmx_inode, int idx) { }
37-
static inline void devpts_add_ref(struct inode *ptmx_inode) { }
38-
static inline void devpts_del_ref(struct inode *ptmx_inode) { }
39-
static inline struct inode *devpts_pty_new(struct inode *ptmx_inode,
40-
dev_t device, int index, void *priv)
41-
{
42-
return ERR_PTR(-EINVAL);
43-
}
44-
static inline void *devpts_get_priv(struct inode *pts_inode)
45-
{
46-
return NULL;
47-
}
48-
static inline void devpts_pty_kill(struct inode *inode) { }
49-
5036
#endif
5137

5238

0 commit comments

Comments
 (0)