lkml.org 
[lkml]   [2022]   [Feb]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH v4 10/20] rust: add `kernel` crate
On Sat 2022-02-12 14:03:36, Miguel Ojeda wrote:
> From: Wedson Almeida Filho <wedsonaf@google.com>
>
> The `kernel` crate currently includes all the abstractions that wrap
> kernel features written in C.
>
> These abstractions call the C side of the kernel via the generated
> bindings with the `bindgen` tool. Modules developed in Rust should
> never call the bindings themselves.
>
> In the future, as the abstractions grow in number, we may need
> to split this crate into several, possibly following a similar
> subdivision in subsystems as the kernel itself and/or moving
> the code to the actual subsystems.
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 82abfaf3c2aa..c042386667f2 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -392,7 +392,10 @@ static struct latched_seq clear_seq = {
> /* the maximum size of a formatted record (i.e. with prefix added per line) */
> #define CONSOLE_LOG_MAX 1024
>
> -/* the maximum size allowed to be reserved for a record */
> +/*
> + * The maximum size allowed to be reserved for a record.
> + * Keep in sync with rust/kernel/print.rs.
> + */
> #define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX)

What exactly should we keep in sync, please?

I see only handling of KERN_* prefix in print.rs. I do not see there
any counter part of LOG_LINE_MAX, CONSOLE_LOG_MAX, or PREFIX_MAX.

Also note that PREFIX_MAX is the maximal lenght of the prefix printed
on console. It is log level + time stamp + caller id. For example:

<12>[ 123.632345][ T3260]

It seems that print.rs defines max size of the prefix in the printk
format where the log level is defined using KERN_* pair of characters.

>
> #define LOG_LEVEL(v) ((v) & 0x07)
> diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
> new file mode 100644
> index 000000000000..dba4ef10c722
> --- /dev/null
> +++ b/rust/kernel/print.rs
> @@ -0,0 +1,417 @@
> +/// Format strings.
> +///
> +/// Public but hidden since it should only be used from public macros.
> +#[doc(hidden)]
> +pub mod format_strings {
> + use crate::bindings;
> +
> + /// The length we copy from the `KERN_*` kernel prefixes.
> + const LENGTH_PREFIX: usize = 2;
> +
> + /// The length of the fixed format strings.
> + pub const LENGTH: usize = 10;

I am sorry but I am not familiar with rust. What are these limits
2 and 10 used for, please?

I guess that 2 is the size of a single KERN_* identifier.
But what is 10?

Note that printk() format prefix is typically just a single KERN_*
identifier. But there might be more. Well, in practice only the
following combination makes sense: KERN_CONT + KERN_<LEVEL>.


> + /// Generates a fixed format string for the kernel's [`_printk`].
> + ///
> + /// The format string is always the same for a given level, i.e. for a
> + /// given `prefix`, which are the kernel's `KERN_*` constants.
> + ///
> + /// [`_printk`]: ../../../../include/linux/printk.h
> + const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] {
> + // Ensure the `KERN_*` macros are what we expect.
> + assert!(prefix[0] == b'\x01');
> + if is_cont {
> + assert!(prefix[1] == b'c');
> + } else {
> + assert!(prefix[1] >= b'0' && prefix[1] <= b'7');
> + }
> + assert!(prefix[2] == b'\x00');
> +
> + let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont {
> + b"%pA\0\0\0\0\0"
> + } else {
> + b"%s: %pA\0"
> + };
> +
> + [
> + prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5],
> + suffix[6], suffix[7],
> + ]
> + }

Finally, is there any way to test whether any change in the printk
code breaks the rust support?

Best Regards,
Petr

\
 
 \ /
  Last update: 2022-02-22 10:07    [W:0.254 / U:26.828 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site