Rust for Linux List
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Yifei Yao" <donplat@barrensea.org>
Cc: <a.hindborg@kernel.org>, <aliceryhl@google.com>,
	<bjorn3_gh@protonmail.com>, <boqun@kernel.org>,
	<brauner@kernel.org>, <dakr@kernel.org>, <gary@garyguo.net>,
	<jack@suse.cz>, <linux-fsdevel@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <lossin@kernel.org>,
	<ojeda@kernel.org>, <rust-for-linux@vger.kernel.org>,
	<tmgross@umich.edu>, <viro@zeniv.linux.org.uk>
Subject: Re: [PATCH v2] rust: seq_file: route seq_print! directly to seq_write
Date: Sat, 16 May 2026 20:34:34 +0100	[thread overview]
Message-ID: <DIKCPS55DEZB.EITY48PJ1UO2@garyguo.net> (raw)
In-Reply-To: <20260516040812.477347-1-donplat@barrensea.org>

On Sat May 16, 2026 at 5:08 AM BST, Yifei Yao wrote:
> Currently, the `seq_print!` macro formats output by passing a Rust
> `fmt::Arguments` object to the C `seq_printf` function using the `%pA`.
> This involves crossing the FFI boundary, parsing the
> format string in C via `vsnprintf`, and triggering a callback back
> into Rust to perform the actual buffer write.
>
> This patch implements `core::fmt::Write` for `&SeqFile` and updates
> `call_printf` to use it, routing Rust's formatting directly to
> `bindings::seq_write`. This approach leverages the bounded string
> slices naturally produced by the Rust formatting machinery and copies
> them directly into the `seq_file` buffer.
>
> By mapping to `seq_write`, we bypass the C string parsing state machine
> and the `%pA` FFI callback, streamlining the execution path. Since
> `seq_write` uses a bounded `memcpy` relying on the explicitly passed
> length, it perfectly matches Rust's `&str` semantics and operates
> safely without requiring null-termination.
>
> Note that `fmt::Write` chunks the output into multiple `seq_write` calls.
> If an overflow occurs during a partial write, `seq_write` sets the
> overflow flag. The `seq_file` subsystem inherently handles this by
> discarding the current record and retrying with a larger buffer.
> Therefore, this optimization introduces no observable semantic change
> to user-space.

Seems that this description is AI-generated. If you used them this must be
disclosed per kernel policy:
https://docs.kernel.org/process/coding-assistants.html.

What's the benefit of this change? Given a single "%pA" string vsnprintf doesn't
do much before passing it back to the Rust formatting machinery. `seq_printf`
just formats directly into its internal buffer, so it's not like there's any
additional copy being optimized out with this change.

This also changes the behaviour when write does overflow, where the previous
code would write as much as possible to the buffer and the new one would not
write anything if the last chunk being written would cause overflow.

Best,
Gary

      parent reply	other threads:[~2026-05-16 19:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-14  5:37 [PATCH] rust: seq_file: route seq_print! directly to seq_write Donjuanplatinum
2026-05-14  7:04 ` Greg KH
2026-05-16  4:08 ` [PATCH v2] " Yifei Yao
2026-05-16  7:57   ` Onur Özkan
2026-05-16  8:21     ` donplat
2026-05-16 19:34   ` Gary Guo [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=DIKCPS55DEZB.EITY48PJ1UO2@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=brauner@kernel.org \
    --cc=dakr@kernel.org \
    --cc=donplat@barrensea.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=viro@zeniv.linux.org.uk \
    /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