All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: John Ogness <john.ogness@linutronix.de>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH printk v4 6/8] printk: nbcon: Add sequence handling
Date: Thu, 14 Sep 2023 18:02:16 +0200	[thread overview]
Message-ID: <ZQMuiDPabS3t5TtT@alley> (raw)
In-Reply-To: <20230908185008.468566-7-john.ogness@linutronix.de>

On Fri 2023-09-08 20:56:06, John Ogness wrote:
> From: Thomas Gleixner <tglx@linutronix.de>
> 
> Add an atomic_long_t field @nbcon_seq to the console struct to
> store the sequence number for nbcon consoles. For nbcon consoles
> this will be used instead of the non-atomic @seq field. The new
> field allows for safe atomic sequence number updates without
> requiring any locking.
> 
> On 64bit systems the new field stores the full sequence number.
> On 32bit systems the new field stores the lower 32 bits of the
> sequence number, which are expanded to 64bit as needed by
> folding the values based on the sequence numbers available in
> the ringbuffer.
> 
> For 32bit systems, having a 32bit representation in the console
> is sufficient. If a console ever gets more than 2^31 records
> behind the ringbuffer then this is the least of the problems.
> 
> diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
> index 644c4b9a4540..d23aa132fdcb 100644
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> +/**
> + * nbcon_seq_init - Helper function to initialize the console sequence
> + * @con:	Console to work on
> + *
> + * Set @con->nbcon_seq to the starting record (specified with con->seq).
> + * If the starting record no longer exists, the oldest available record
> + * is chosen. This is especially important on 32bit systems because only
> + * the lower 32 bits of the sequence number are stored. The upper 32 bits
> + * are derived from the sequence numbers available in the ringbuffer.
> + *
> + * For init only. Do not use for runtime updates.
> + */
> +static void nbcon_seq_init(struct console *con)
> +{
> +	u64 seq = max_t(u64, con->seq, prb_first_valid_seq(prb));
> +
> +	atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), __seq_to_nbcon_seq(seq));
> +
> +	/* Clear con->seq since nbcon consoles use con->nbcon_seq instead. */
> +	con->seq = 0;
> +}
> +
> +/**
> + * nbcon_seq_read - Read the current console sequence
> + * @con:	Console to read the sequence of
> + *
> + * Return:	Sequence number of the next record to print on @con.
> + */
> +u64 nbcon_seq_read(struct console *con)
> +{
> +	unsigned long nbcon_seq = atomic_long_read(&ACCESS_PRIVATE(con, nbcon_seq));
> +
> +	return __nbcon_seq_to_seq(nbcon_seq);
> +}
> +
> +/**
> + * nbcon_seq_force - Force console sequence to a specific value
> + * @con:	Console to work on
> + * @seq:	Sequence number value to set
> + *
> + * Only to be used in extreme situations (such as panic with
> + * CONSOLE_REPLAY_ALL).
> + */
> +void nbcon_seq_force(struct console *con, u64 seq)
> +{
> +	atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), __seq_to_nbcon_seq(seq));

We should actually do the same trick as in nbcon_seq_init() to make
sure that the 32-bit seq is shrinked against the prb_first_valid_seq().
I mean to do:

	/* If the starting record no longer exists, the oldest available record
	 * is chosen. This is especially important on 32bit systems because only
	 * the lower 32 bits of the sequence number are stored. The upper 32 bits
	 * are derived from the sequence numbers available in the ringbuffer.
	 */
	u64 valid_seq = max_t(u64, seq, prb_first_valid_seq(prb));

	atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), __seq_to_nbcon_seq(valid));

> +}

And we might implement nbcon_seq_init() using nbcon_seq_force(). I mean:

static void nbcon_seq_init(struct console *con)
{
	nbcon_seq_force(con->seq);

	/* Clear con->seq since nbcon consoles use con->nbcon_seq instead. */
	con->seq = 0;
}

> @@ -540,11 +649,14 @@ static bool nbcon_context_can_proceed(struct nbcon_context *ctxt, struct nbcon_s
>  	nbcon_context_release(ctxt);
>  
>  	/*
> -	 * It is not known whether the handover succeeded. The outermost
> -	 * callsite has to make the final decision whether printing
> -	 * should proceed or not (via reacquire, possibly hostile). The
> -	 * console is now unlocked so go back all the way instead of
> -	 * trying to implement heuristics in tons of places.
> +	 * It is not clear whether the waiter really took over ownership. The
> +	 * outermost callsite must make the final decision whether console
> +	 * ownership is needed for it to proceed. If yes, it must reacquire
> +	 * ownership (possibly hostile) before carefully proceeding.
> +	 *
> +	 * The calling context no longer owns the console so go back all the
> +	 * way instead of trying to implement reacquire heuristics in tons of
> +	 * places.
>  	 */
>  	return false;
>  }

This change probably should have been done in the patch introducing
nbcon_context_can_proceed().

> @@ -636,6 +748,8 @@ bool nbcon_alloc(struct console *con)
>   *
>   * nbcon_alloc() *must* be called and succeed before this function
>   * is called.
> + *
> + * This function expects that the legacy @con->seq has been set.
>   */
>  void nbcon_init(struct console *con)
>  {

Best Regards,
Petr

  reply	other threads:[~2023-09-14 16:03 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-08 18:50 [PATCH printk v4 0/8] provide nbcon base John Ogness
2023-09-08 18:50 ` [PATCH printk v4 1/8] printk: Add non-BKL (nbcon) console basic infrastructure John Ogness
2023-09-08 18:50 ` [PATCH printk v4 2/8] printk: nbcon: Add acquire/release logic John Ogness
2023-09-14 13:16   ` Petr Mladek
2023-09-14 13:23     ` [PATCH v4 2+/8] " Petr Mladek
2023-09-15 17:01       ` John Ogness
2023-09-08 18:50 ` [PATCH printk v4 3/8] printk: Make static printk buffers available to nbcon John Ogness
2023-09-14 13:29   ` Petr Mladek
2023-09-08 18:50 ` [PATCH printk v4 4/8] printk: nbcon: Add buffer management John Ogness
2023-09-14 14:55   ` Petr Mladek
2023-09-08 18:50 ` [PATCH printk v4 5/8] printk: nbcon: Add ownership state functions John Ogness
2023-09-14 15:38   ` Petr Mladek
2023-09-08 18:50 ` [PATCH printk v4 6/8] printk: nbcon: Add sequence handling John Ogness
2023-09-14 16:02   ` Petr Mladek [this message]
2023-09-08 18:50 ` [PATCH printk v4 7/8] printk: nbcon: Add emit function and callback function for atomic printing John Ogness
2023-09-15  8:21   ` Petr Mladek
2023-09-08 18:50 ` [PATCH printk v4 8/8] printk: nbcon: Allow drivers to mark unsafe regions and check state John Ogness
2023-09-15  8:38   ` Petr Mladek

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=ZQMuiDPabS3t5TtT@alley \
    --to=pmladek@suse.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.