Linux Serial subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jiayi Zhang" <zhangjiayi1014@gmail.com>
Cc: linux-serial@vger.kernel.org
Subject: Re: [PATCH] vt: drop console_lock before calling __do_SAK() in vc_SAK()
Date: Wed, 02 Dec 2026 18:26:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-246432@kernel.org> (raw)
In-Reply-To: <20261202181217.4160067-1-zhangjiayi1014@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] vt: premature tty_kref_put() in vc_SAK() before re-acquiring
  console_lock
--

commit ae876e3ff5feb53c222ade62935a9819ac41e34c
Author: Jiayi Zhang <zhangjiayi1014@gmail.com>

vt: drop console_lock before calling __do_SAK() in vc_SAK()

This commit drops console_lock around __do_SAK() in vc_SAK() while holding
a tty reference to avoid a lock ordering deadlock between console_lock and
the tty buffer lock.

> diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c
> index ecefe4a666f26..62a52df1c2c15 100644
> --- a/drivers/tty/vt/vt_ioctl.c
> +++ b/drivers/tty/vt/vt_ioctl.c
> @@ -986,17 +986,25 @@ void vc_SAK(struct work_struct *work)
>  	struct vc_data *vc;
>  	struct tty_struct *tty;
>  
> -	guard(console_lock)();
> +	console_lock();
>  	vc = vc_con->d;
> -	if (!vc)
> +	if (!vc) {
> +		console_unlock();
>  		return;
> +	}
>  
> -	/* FIXME: review tty ref counting */
> -	tty = vc->port.tty;
> +	tty = tty_port_tty_get(&vc->port);
>  	/* SAK should also work in all raw modes and reset them properly. */
> -	if (tty)
> +	if (tty) {
> +		console_unlock();
>  		__do_SAK(tty);
> -	reset_vc(vc);
> +		tty_kref_put(tty);
> +		console_lock();
> +		vc = vc_con->d;
> +	}
> +	if (vc)
> +		reset_vc(vc);
> +	console_unlock();
>  }

[Severity: Medium]
Can calling tty_kref_put(tty) before re-acquiring console_lock() allow the
console to be freed and reallocated before reset_vc() runs?

If the process killed by __do_SAK() closes the tty before tty_kref_put(),
dropping the reference outside console_lock() can trigger con_cleanup()
and drop vc->port.kref to 1 while unlocked:

kworker (vc_SAK)                  other tasks
__do_SAK(tty)
  /* last user exits, closes tty */
tty_kref_put(tty)
  release_one_tty()
    con_cleanup()
      tty_port_put(&vc->port)
                                  vt_disallocate()
                                    kfree(vc)
                                  vt_setactivate()
                                    vc_allocate()
                                      INIT_WORK(&vc_con->SAK_work, ...)
                                    /* configures new vc_con->d */
console_lock()
vc = vc_con->d
reset_vc(vc)                      <-- clobbers newly allocated vc

In addition to reset_vc() overwriting the new console's vt_mode and
vt_pid, vc_allocate() calls INIT_WORK() on vc_con->SAK_work while vc_SAK()
is still running, which corrupts the work item if fn_SAK() re-queued it.

Putting the tty reference after console_unlock() at the end of vc_SAK()
keeps vc->port.kref elevated across the entire unlocked window so
vt_disallocate() still sees the console as in use.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202181217.4160067-1-zhangjiayi1014@gmail.com?part=1

      reply	other threads:[~2026-12-02 18:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 18:12 [PATCH] vt: drop console_lock before calling __do_SAK() in vc_SAK() Jiayi Zhang
2026-12-02 18:26 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-246432@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zhangjiayi1014@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox