Re: [PATCHv3 4/6] tty/lockdep: Add ldisc_sem asserts

From: Dmitry Safonov
Date: Tue Sep 11 2018 - 08:53:51 EST


On Tue, 2018-09-11 at 14:01 +0200, Peter Zijlstra wrote:
> On Tue, Sep 11, 2018 at 02:48:19AM +0100, Dmitry Safonov wrote:
> > Make sure under CONFIG_LOCKDEP that each change to line discipline
> > is done with held write semaphor.
>
> But you don't do that. You just assert it is held, not for writing.
>
> > Otherwise potential reader will have a good time dereferencing
> > incomplete/uninitialized ldisc.
> >
> > Exception here is tty_ldisc_open(), as it's called without
> > ldisc_sem
> > locked by tty_init_dev() for the tty->link.
>
> You fail to explain how that is not broken...

I'll add the explanation to the next version.

>
> > Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> > Cc: Jiri Slaby <jslaby@xxxxxxxx>
> > Signed-off-by: Dmitry Safonov <dima@xxxxxxxxxx>
> > ---
> > drivers/tty/tty_ldisc.c | 5 +++++
> > 1 file changed, 5 insertions(+)
> >
> > diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c
> > index fc4c97cae01e..202cb645582f 100644
> > --- a/drivers/tty/tty_ldisc.c
> > +++ b/drivers/tty/tty_ldisc.c
> > @@ -471,6 +471,7 @@ static int tty_ldisc_open(struct tty_struct
> > *tty, struct tty_ldisc *ld)
> >
> > static void tty_ldisc_close(struct tty_struct *tty, struct
> > tty_ldisc *ld)
> > {
> > + lockdep_assert_held(&tty->ldisc_sem);
>
> Did you want:
>
> lockdep_assert_held_exclusive(&tty->ldisc_sem);
>
> ?

Oh, yes, thanks.

>
> > WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
> > clear_bit(TTY_LDISC_OPEN, &tty->flags);
> > if (ld->ops->close)

--
Thanks,
Dmitry