From: John Ogness <john.ogness@linutronix.de>
To: Zhu Jun <zhujun2@cmss.chinamobile.com>, pmladek@suse.com
Cc: rostedt@goodmis.org, senozhatsky@chromium.org,
paulmck@kernel.org, printk@lists.linux.dev,
linux-kernel@vger.kernel.org,
Zhu Jun <zhujun2@cmss.chinamobile.com>
Subject: Re: [PATCH v2] printk: ringbuffer: Annotate speculative record reads
Date: Thu, 12 Nov 2026 09:36:39 +0106 [thread overview]
Message-ID: <87pkwah2og.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <20261112031441.120773-1-zhujun2@cmss.chinamobile.com>
Hi Zhu,
On 2026-11-12, Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
> diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c
> index 46a79ec0940a..8a1075d00d0a 100644
> --- a/kernel/printk/printk_ringbuffer.c
> +++ b/kernel/printk/printk_ringbuffer.c
> @@ -1894,9 +1894,10 @@ static bool copy_data(struct prb_data_ring *data_ring,
> if (data_size < (unsigned int)len)
> return false;
>
> - /* Caller interested in the line count? */
Please do not drop the above comment.
> - if (line_count)
> - *line_count = count_lines(data, len);
> + if (line_count) {
> + /* Racing text is rejected by the caller's descriptor recheck. */
> + *line_count = data_race(count_lines(data, len));
> + }
>
> /* Caller interested in the data content? */
> if (!buf || !buf_size)
> @@ -1969,6 +1970,7 @@ static int prb_read(struct printk_ringbuffer *rb, u64 seq,
> atomic_long_t *state_var = &rdesc->state_var;
> struct prb_desc desc;
> unsigned long id;
> + u16 text_len;
> int err;
>
> /* Extract the ID, used to specify the descriptor to read. */
> @@ -1984,12 +1986,16 @@ static int prb_read(struct printk_ringbuffer *rb, u64 seq,
> if (err || !r)
> return err;
>
> - /* If requested, copy meta data. */
Please do not drop the above comment.
> - if (r->info)
> - memcpy(r->info, info, sizeof(*(r->info)));
> + if (r->info) {
> + /* The record may be recycled; the descriptor is rechecked below. */
> + data_race(memcpy(r->info, info, sizeof(*r->info)));
> + }
> +
> + /* The descriptor recheck below also rejects a raced length. */
> + text_len = data_race(READ_ONCE(info->text_len));
There is no need to prevent compiler optimization with
READ_ONCE(). Using data_race() is enough.
>
> /* Copy text data. If it fails, this is a data-less record. */
> - if (!copy_data(&rb->text_data_ring, &desc.text_blk_lpos, info->text_len,
> + if (!copy_data(&rb->text_data_ring, &desc.text_blk_lpos, text_len,
> r->text_buf, r->text_buf_size, line_count)) {
> return -ENOENT;
> }
John Ogness
prev parent reply other threads:[~2026-11-12 8:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 3:14 Zhu Jun
2026-11-12 8:30 ` John Ogness [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=87pkwah2og.fsf@jogness.linutronix.de \
--to=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=paulmck@kernel.org \
--cc=pmladek@suse.com \
--cc=printk@lists.linux.dev \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
--cc=zhujun2@cmss.chinamobile.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
all inboxes | Powered by JetHome®