From: "Frédéric Weisbecker" <fweisbec@gmail.com>
To: "Ingo Molnar" <mingo@elte.hu>
Cc: Linux-Kernel <linux-kernel@vger.kernel.org>,
"Alan Cox" <alan@lxorguk.ukuu.org.uk>
Subject: Re: [RFC][PATCH] tty: remove the BKL in n_tty.c
Date: Mon, 13 Oct 2008 10:54:07 +0200 [thread overview]
Message-ID: <c62985530810130154q65a162adg2bc018cfddf4dbc9@mail.gmail.com> (raw)
In-Reply-To: <20081013084719.03972D1B9DE@perso1.free.fr>
2008/10/13 Frederic Weisbecker <fweisbec@gmail.com>:
>
> By using a bkl tracer, I saw that the opost function in n_tty.c, used when we
> write to the console, requests a lock_kernel(). And since I'm displaying the
> trace during the tracing, I see this lock very often.
>
> The opost and opost_block functions in drivers/char/n_tty.c use
> the bkl to protect the field tty->column.
> Actually this attribute seems to be only touched on these two functions and
> also we are in user context.
>
> A mutex seems to be sufficient to prevent from race conditions here.
> I made some tests by writing on a tty console whith several threads and it
> seems to be correct.
>
> I don't know where to post this patch. I'm not sure if the kill-the-bkl is
> still on the air on the -tip tree.
>
> Any comments?
>
> Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
> ---
> diff --git a/drivers/char/n_tty.c b/drivers/char/n_tty.c
> index 708c2b1..55c963f 100644
> --- a/drivers/char/n_tty.c
> +++ b/drivers/char/n_tty.c
> @@ -62,6 +62,13 @@
> #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
> #define TTY_THRESHOLD_UNTHROTTLE 128
>
> +
> +/*
> + * This Mutex is used to replace a big kernel lock that protected
> + * the access to tty->column.
> + */
> +static DEFINE_MUTEX(tty_column_mutex);
> +
> static inline unsigned char *alloc_buf(void)
> {
> gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
> @@ -264,18 +271,19 @@ static inline int is_continuation(unsigned char c, struct tty_struct *tty)
> * relevant in the world today. If you ever need them, add them here.
> *
> * Called from both the receive and transmit sides and can be called
> - * re-entrantly. Relies on lock_kernel() for tty->column state.
> + * re-entrantly.
> */
>
> static int opost(unsigned char c, struct tty_struct *tty)
> {
> int space, spaces;
> + int ret = 0;
>
> space = tty_write_room(tty);
> if (!space)
> return -1;
>
> - lock_kernel();
> + mutex_lock(&tty_column_mutex);
> if (O_OPOST(tty)) {
> switch (c) {
> case '\n':
> @@ -283,8 +291,8 @@ static int opost(unsigned char c, struct tty_struct *tty)
> tty->column = 0;
> if (O_ONLCR(tty)) {
> if (space < 2) {
> - unlock_kernel();
> - return -1;
> + ret = -1;
> + goto out;
> }
> tty_put_char(tty, '\r');
> tty->column = 0;
> @@ -293,8 +301,7 @@ static int opost(unsigned char c, struct tty_struct *tty)
> break;
> case '\r':
> if (O_ONOCR(tty) && tty->column == 0) {
> - unlock_kernel();
> - return 0;
> + goto out;
> }
> if (O_OCRNL(tty)) {
> c = '\n';
> @@ -308,13 +315,12 @@ static int opost(unsigned char c, struct tty_struct *tty)
> spaces = 8 - (tty->column & 7);
> if (O_TABDLY(tty) == XTABS) {
> if (space < spaces) {
> - unlock_kernel();
> - return -1;
> + ret = -1;
> + goto out;
> }
> tty->column += spaces;
> tty->ops->write(tty, " ", spaces);
> - unlock_kernel();
> - return 0;
> + goto out;
> }
> tty->column += spaces;
> break;
> @@ -331,8 +337,9 @@ static int opost(unsigned char c, struct tty_struct *tty)
> }
> }
> tty_put_char(tty, c);
> - unlock_kernel();
> - return 0;
> +out:
> + mutex_unlock(&tty_column_mutex);
> + return ret;
> }
>
> /**
> @@ -346,8 +353,7 @@ static int opost(unsigned char c, struct tty_struct *tty)
> * the simple cases normally found and helps to generate blocks of
> * symbols for the console driver and thus improve performance.
> *
> - * Called from write_chan under the tty layer write lock. Relies
> - * on lock_kernel for the tty->column state.
> + * Called from write_chan under the tty layer write lock.
> */
>
> static ssize_t opost_block(struct tty_struct *tty,
> @@ -363,7 +369,7 @@ static ssize_t opost_block(struct tty_struct *tty,
> if (nr > space)
> nr = space;
>
> - lock_kernel();
> + mutex_lock(&tty_column_mutex);
> for (i = 0, cp = buf; i < nr; i++, cp++) {
> switch (*cp) {
> case '\n':
> @@ -398,7 +404,8 @@ break_out:
> if (tty->ops->flush_chars)
> tty->ops->flush_chars(tty);
> i = tty->ops->write(tty, buf, i);
> - unlock_kernel();
> +
> + mutex_unlock(&tty_column_mutex);
> return i;
> }
>
>
>
(Ading Alan in Cc)
next prev parent reply other threads:[~2008-10-13 8:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-13 10:47 Frederic Weisbecker
2008-10-13 8:54 ` Frédéric Weisbecker [this message]
2008-10-13 9:24 ` Alan Cox
2008-10-13 9:37 ` Frédéric Weisbecker
2008-10-13 10:37 ` Andi Kleen
2008-10-13 10:40 ` Alan Cox
2008-10-13 12:23 ` Frédéric Weisbecker
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=c62985530810130154q65a162adg2bc018cfddf4dbc9@mail.gmail.com \
--to=fweisbec@gmail.com \
--cc=alan@lxorguk.ukuu.org.uk \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
/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
all inboxes | Powered by JetHome®