From: Steven Rostedt <rostedt@goodmis.org>
To: Petr Mladek <pmladek@suse.com>
Cc: John Ogness <john.ogness@linutronix.de>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Thomas Gleixner <tglx@linutronix.de>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH printk v3 6/6] printk: syslog: close window between wait and read
Date: Mon, 28 Jun 2021 10:52:40 -0400 [thread overview]
Message-ID: <20210628105240.4fc1be2e@oasis.local.home> (raw)
In-Reply-To: <YNneRIEIa7pHahTu@alley>
On Mon, 28 Jun 2021 16:35:48 +0200
Petr Mladek <pmladek@suse.com> wrote:
> I was double checking the code and the locking is really hard to
> follow. I would if the following approach make it easier. The main
> trick is that the lock is taken at the beginnig and release at
> the end. It is only temporary released around a single line
> when needed.
>
> static int syslog_print(char __user *buf, int size)
> {
> struct printk_info info;
> struct printk_record r;
> char *text;
> int len = 0;
> u64 seq;
>
> text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL);
> if (!text)
> return -ENOMEM;
>
> prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
>
> mutex_lock(&syslog_lock);
>
> /*
> * Wait for the @syslog_seq record to be vailable. @syslog_seq may
> * change while waiting.
> */
> do {
> seq = syslog_seq;
>
> mutex_unlock(&syslog_lock);
> len = wait_event_interruptible(log_wait, prb_read_valid(prb, seq, NULL));
> mutex_lock(&syslog_lock);
>
> if (len)
> goto out;
> } while (syslog_seq != seq);
>
> /*
> * Copy records that fit into the buffer. The above cycle makes sure
> * that the first record is always available.
> */
> do {
> size_t n;
> size_t skip;
> unsigned long err;
>
> if (!prb_read_valid(prb, syslog_seq, &r))
> break;
>
> if (r.info->seq != syslog_seq) {
> /* message is gone, move to next valid one */
> syslog_seq = r.info->seq;
> syslog_partial = 0;
> }
>
> /*
> * To keep reading/counting partial line consistent,
> * use printk_time value as of the beginning of a line.
> */
> if (!syslog_partial)
> syslog_time = printk_time;
>
> skip = syslog_partial;
> n = record_print_text(&r, true, syslog_time);
> if (n - syslog_partial <= size) {
> /* message fits into buffer, move forward */
> syslog_seq = r.info->seq + 1;
> n -= syslog_partial;
> syslog_partial = 0;
> } else if (!len){
> /* partial read(), remember position */
> n = size;
> syslog_partial += n;
> } else
> n = 0;
>
> if (!n)
> break;
>
> mutex_unlock(&syslog_lock);
> err = copy_to_user(buf, text + skip, n);
> mutex_lock(&syslog_lock);
>
> if (err && !len) {
> len = -EFAULT;
> break;
> }
>
> len += n;
> size -= n;
> buf += n;
> } while(size);
> out:
> mutex_unlock(&syslog_lock);
> kfree(text);
> return len;
> }
That's a much more common approach to locking, that may not be as
efficient, but is much easier to keep straight, and less error prone.
-- Steve
next prev parent reply other threads:[~2021-06-28 15:30 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-24 11:11 [PATCH printk v3 0/6] printk: remove safe buffers John Ogness
2021-06-24 11:11 ` [PATCH printk v3 1/6] lib/nmi_backtrace: explicitly serialize banner and regs John Ogness
2021-06-24 12:26 ` Petr Mladek
2021-06-24 11:11 ` [PATCH printk v3 2/6] printk: track/limit recursion John Ogness
2021-06-24 12:55 ` Petr Mladek
2021-06-24 11:11 ` [PATCH printk v3 3/6] printk: remove safe buffers John Ogness
2021-06-24 14:49 ` Petr Mladek
2021-06-24 15:35 ` John Ogness
2021-06-25 12:41 ` Petr Mladek
2021-06-24 11:11 ` [PATCH printk v3 4/6] printk: remove NMI tracking John Ogness
2021-06-25 12:36 ` Petr Mladek
2021-06-25 13:34 ` Russell King (Oracle)
2021-06-24 11:11 ` [PATCH printk v3 5/6] printk: convert @syslog_lock to mutex John Ogness
2021-06-24 11:11 ` [PATCH printk v3 6/6] printk: syslog: close window between wait and read John Ogness
2021-06-24 14:57 ` Petr Mladek
2021-06-24 15:25 ` Petr Mladek
2021-06-25 8:11 ` John Ogness
2021-06-25 14:55 ` Petr Mladek
2021-06-25 13:33 ` Steven Rostedt
2021-06-25 14:14 ` John Ogness
2021-06-28 14:35 ` Petr Mladek
2021-06-28 14:52 ` Steven Rostedt [this message]
2021-06-28 15:00 ` John Ogness
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=20210628105240.4fc1be2e@oasis.local.home \
--to=rostedt@goodmis.org \
--cc=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=pmladek@suse.com \
--cc=senozhatsky@chromium.org \
--cc=tglx@linutronix.de \
/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;
as well as URLs for NNTP newsgroup(s).