| @@ -271,10 +271,13 @@ const struct file_operations tty_ldiscs_proc_fops = { · 270 unmodified lines |
| 271 | 271 | |
| 272 | 272 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) |
| 273 | 273 | { |
| 274 | + struct tty_ldisc *ld; |
| 275 | + |
| 274 | 276 | ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT); |
| 275 | | − if (!tty->ldisc) |
| 277 | + ld = tty->ldisc; |
| 278 | + if (!ld) |
| 276 | 279 | ldsem_up_read(&tty->ldisc_sem); |
| 277 | | − return tty->ldisc; |
| 280 | + return ld; |
| 278 | 281 | } |
| 279 | 282 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); |
| 280 | 283 | |
| @@ -488,41 +491,6 @@ static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) · 207 unmodified lines |
| 488 | 491 | tty_ldisc_debug(tty, "%p: closed\n", ld); |
| 489 | 492 | } |
| 490 | 493 | |
| 491 | | −/** |
| 492 | | − * tty_ldisc_restore - helper for tty ldisc change |
| 493 | | − * @tty: tty to recover |
| 494 | | − * @old: previous ldisc |
| 495 | | − * |
| 496 | | − * Restore the previous line discipline or N_TTY when a line discipline |
| 497 | | − * change fails due to an open error |
| 498 | | − */ |
| 499 | | − |
| 500 | | −static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) |
| 501 | | −{ |
| 502 | | − struct tty_ldisc *new_ldisc; |
| 503 | | − int r; |
| 504 | | − |
| 505 | | − /* There is an outstanding reference here so this is safe */ |
| 506 | | − old = tty_ldisc_get(tty, old->ops->num); |
| 507 | | − WARN_ON(IS_ERR(old)); |
| 508 | | − tty->ldisc = old; |
| 509 | | − tty_set_termios_ldisc(tty, old->ops->num); |
| 510 | | − if (tty_ldisc_open(tty, old) < 0) { |
| 511 | | − tty_ldisc_put(old); |
| 512 | | − /* This driver is always present */ |
| 513 | | − new_ldisc = tty_ldisc_get(tty, N_TTY); |
| 514 | | − if (IS_ERR(new_ldisc)) |
| 515 | | − panic("n_tty: get"); |
| 516 | | − tty->ldisc = new_ldisc; |
| 517 | | − tty_set_termios_ldisc(tty, N_TTY); |
| 518 | | − r = tty_ldisc_open(tty, new_ldisc); |
| 519 | | − if (r < 0) |
| 520 | | − panic("Couldn't open N_TTY ldisc for " |
| 521 | | − "%s --- error %d.", |
| 522 | | − tty_name(tty), r); |
| 523 | | − } |
| 524 | | −} |
| 525 | | − |
| 526 | 494 | /** |
| 527 | 495 | * tty_set_ldisc - set line discipline |
| 528 | 496 | * @tty: the terminal to set |
| @@ -536,12 +504,7 @@ static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) · 7 unmodified lines |
| 536 | 504 | |
| 537 | 505 | int tty_set_ldisc(struct tty_struct *tty, int disc) |
| 538 | 506 | { |
| 539 | | − int retval; |
| 540 | | − struct tty_ldisc *old_ldisc, *new_ldisc; |
| 541 | | − |
| 542 | | − new_ldisc = tty_ldisc_get(tty, disc); |
| 543 | | − if (IS_ERR(new_ldisc)) |
| 544 | | − return PTR_ERR(new_ldisc); |
| 507 | + int retval, old_disc; |
| 545 | 508 | |
| 546 | 509 | tty_lock(tty); |
| 547 | 510 | retval = tty_ldisc_lock(tty, 5 * HZ); |
| @@ -554,7 +517,8 @@ int tty_set_ldisc(struct tty_struct *tty, int disc) · 6 unmodified lines |
| 554 | 517 | } |
| 555 | 518 | |
| 556 | 519 | /* Check the no-op case */ |
| 557 | | − if (tty->ldisc->ops->num == disc) |
| 520 | + old_disc = tty->ldisc->ops->num; |
| 521 | + if (old_disc == disc) |
| 558 | 522 | goto out; |
| 559 | 523 | |
| 560 | 524 | if (test_bit(TTY_HUPPED, &tty->flags)) { |
| @@ -563,34 +527,25 @@ int tty_set_ldisc(struct tty_struct *tty, int disc) · 2 unmodified lines |
| 563 | 527 | goto out; |
| 564 | 528 | } |
| 565 | 529 | |
| 566 | | − old_ldisc = tty->ldisc; |
| 567 | | − |
| 568 | | − /* Shutdown the old discipline. */ |
| 569 | | − tty_ldisc_close(tty, old_ldisc); |
| 570 | | − |
| 571 | | − /* Now set up the new line discipline. */ |
| 572 | | − tty->ldisc = new_ldisc; |
| 573 | | − tty_set_termios_ldisc(tty, disc); |
| 574 | | − |
| 575 | | − retval = tty_ldisc_open(tty, new_ldisc); |
| 530 | + retval = tty_ldisc_reinit(tty, disc); |
| 576 | 531 | if (retval < 0) { |
| 577 | 532 | /* Back to the old one or N_TTY if we can't */ |
| 578 | | − tty_ldisc_put(new_ldisc); |
| 579 | | − tty_ldisc_restore(tty, old_ldisc); |
| 533 | + if (tty_ldisc_reinit(tty, old_disc) < 0) { |
| 534 | + pr_err("tty: TIOCSETD failed, reinitializing N_TTY\n"); |
| 535 | + if (tty_ldisc_reinit(tty, N_TTY) < 0) { |
| 536 | + /* At this point we have tty->ldisc == NULL. */ |
| 537 | + pr_err("tty: reinitializing N_TTY failed\n"); |
| 538 | + } |
| 539 | + } |
| 580 | 540 | } |
| 581 | 541 | |
| 582 | | − if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) { |
| 542 | + if (tty->ldisc && tty->ldisc->ops->num != old_disc && |
| 543 | + tty->ops->set_ldisc) { |
| 583 | 544 | down_read(&tty->termios_rwsem); |
| 584 | 545 | tty->ops->set_ldisc(tty); |
| 585 | 546 | up_read(&tty->termios_rwsem); |
| 586 | 547 | } |
| 587 | 548 | |
| 588 | | − /* At this point we hold a reference to the new ldisc and a |
| 589 | | − reference to the old ldisc, or we hold two references to |
| 590 | | − the old ldisc (if it was restored as part of error cleanup |
| 591 | | − above). In either case, releasing a single reference from |
| 592 | | − the old ldisc is correct. */ |
| 593 | | − new_ldisc = old_ldisc; |
| 594 | 549 | out: |
| 595 | 550 | tty_ldisc_unlock(tty); |
| 596 | 551 | |
| @@ -598,7 +553,6 @@ out: · 1 unmodified line |
| 598 | 553 | already running */ |
| 599 | 554 | tty_buffer_restart_work(tty->port); |
| 600 | 555 | err: |
| 601 | | − tty_ldisc_put(new_ldisc); /* drop the extra reference */ |
| 602 | 556 | tty_unlock(tty); |
| 603 | 557 | return retval; |
| 604 | 558 | } |
| @@ -659,10 +613,8 @@ int tty_ldisc_reinit(struct tty_struct *tty, int disc) · 54 unmodified lines |
| 659 | 613 | int retval; |
| 660 | 614 | |
| 661 | 615 | ld = tty_ldisc_get(tty, disc); |
| 662 | | − if (IS_ERR(ld)) { |
| 663 | | − BUG_ON(disc == N_TTY); |
| 616 | + if (IS_ERR(ld)) |
| 664 | 617 | return PTR_ERR(ld); |
| 665 | | − } |
| 666 | 618 | |
| 667 | 619 | if (tty->ldisc) { |
| 668 | 620 | tty_ldisc_close(tty, tty->ldisc); |
| @@ -674,10 +626,8 @@ int tty_ldisc_reinit(struct tty_struct *tty, int disc) · 5 unmodified lines |
| 674 | 626 | tty_set_termios_ldisc(tty, disc); |
| 675 | 627 | retval = tty_ldisc_open(tty, tty->ldisc); |
| 676 | 628 | if (retval) { |
| 677 | | − if (!WARN_ON(disc == N_TTY)) { |
| 678 | | − tty_ldisc_put(tty->ldisc); |
| 679 | | − tty->ldisc = NULL; |
| 680 | | − } |
| 629 | + tty_ldisc_put(tty->ldisc); |
| 630 | + tty->ldisc = NULL; |
| 681 | 631 | } |
| 682 | 632 | return retval; |
| 683 | 633 | } |