Skip to content

Conversation

@lhw2002426
Copy link
Contributor

add rng device and virtio rng

@lhw2002426 lhw2002426 force-pushed the add_rng branch 3 times, most recently from e929b30 to 23519af Compare July 20, 2025 15:10
@ken4647
Copy link
Contributor

ken4647 commented Jul 21, 2025

A lot of warnings when make clippy, try to fix them.

@ken4647
Copy link
Contributor

ken4647 commented Jul 21, 2025

try to change commit message to feat: add virtio-rng device

@ken4647
Copy link
Contributor

ken4647 commented Jul 21, 2025

❯ make clippy
git submodule update --init
cargo clippy --all-features --workspace --exclude axlog  
   Compiling axerrno v0.1.0 (/home/kenable/code/pr_check/crates/axerrno)
    Checking axlog v0.1.0 (/home/kenable/code/pr_check/crates/axlog)
warning: unused imports: `distributions::Standard` and `prelude::*`
  --> crates/driver_rng/src/lib.rs:16:12
   |
16 | use rand::{distributions::Standard, prelude::*};
   |            ^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: `driver_rng` (lib) generated 1 warning (run `cargo clippy --fix --lib -p driver_rng` to apply 1 suggestion)
   Compiling ruxhal v0.1.0 (/home/kenable/code/pr_check/modules/ruxhal)
    Checking page_table v0.1.0 (/home/kenable/code/pr_check/crates/page_table)
    Checking driver_net v0.1.0 (/home/kenable/code/pr_check/crates/driver_net)
   Compiling ruxdriver v0.1.0 (/home/kenable/code/pr_check/modules/ruxdriver)
    Checking linked_list v0.1.0 (/home/kenable/code/pr_check/crates/linked_list)
    Checking ruxdtb v0.1.0 (/home/kenable/code/pr_check/modules/ruxdtb)
   Compiling ruxos_posix_api v0.1.0 (/home/kenable/code/pr_check/api/ruxos_posix_api)
    Checking scheduler v0.1.0 (/home/kenable/code/pr_check/crates/scheduler)
    Checking axio v0.1.0 (/home/kenable/code/pr_check/crates/axio)
    Checking axalloc v0.1.0 (/home/kenable/code/pr_check/modules/axalloc)
    Checking iovec v0.0.1 (/home/kenable/code/pr_check/crates/iovec)
    Checking driver_virtio v0.1.0 (/home/kenable/code/pr_check/crates/driver_virtio)
warning: unused import: `core::ptr`
  --> crates/driver_virtio/src/rng.rs:13:5
   |
13 | use core::ptr;
   |     ^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: unused import: `log::info`
  --> crates/driver_virtio/src/rng.rs:24:5
   |
24 | use log::info;
   |     ^^^^^^^^^

warning: unused imports: `error`, `info`, and `warn`
  --> crates/driver_virtio/src/lib.rs:59:11
   |
59 | use log::{error, info, warn};
   |           ^^^^^  ^^^^  ^^^^

warning: variable does not need to be mutable
  --> crates/driver_virtio/src/rng.rs:33:13
   |
33 |         let mut virtio = InnerDev::new(transport).unwrap();
   |             ----^^^^^^
   |             |
   |             help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

warning: `driver_virtio` (lib) generated 4 warnings (run `cargo clippy --fix --lib -p driver_virtio` to apply 4 suggestions)
    Checking axfs_vfs v0.1.0 (/home/kenable/code/pr_check/crates/axfs_vfs)
    Checking tty v0.0.1 (/home/kenable/code/pr_check/crates/tty)
    Checking ruxfifo v0.1.0 (/home/kenable/code/pr_check/crates/ruxfifo)
    Checking capability v0.1.0 (/home/kenable/code/pr_check/crates/capability)
    Checking axfs_devfs v0.1.0 (/home/kenable/code/pr_check/crates/axfs_devfs)
    Checking axfs_ramfs v0.1.0 (/home/kenable/code/pr_check/crates/axfs_ramfs)
    Checking ruxfdtable v0.1.0 (/home/kenable/code/pr_check/modules/ruxfdtable)
    Checking ruxfs v0.1.0 (/home/kenable/code/pr_check/modules/ruxfs)
    Checking ruxrand v0.1.0 (/home/kenable/code/pr_check/modules/ruxrand)
warning: unused import: `prelude::*`
 --> modules/ruxrand/src/rng.rs:4:37
  |
4 | use rand::{distributions::Standard, prelude::*};
  |                                     ^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: variables can be used directly in the `format!` string
  --> modules/ruxrand/src/rng.rs:15:5
   |
15 |     debug!("Initializing per-CPU RNG for CPU ID: {}", cpuid);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
   = note: `#[warn(clippy::uninlined_format_args)]` on by default
help: change this to
   |
15 -     debug!("Initializing per-CPU RNG for CPU ID: {}", cpuid);
15 +     debug!("Initializing per-CPU RNG for CPU ID: {cpuid}");
   |

warning: returning the result of a `let` binding from a block
  --> modules/ruxrand/src/rng.rs:42:5
   |
41 |     let value = unsafe { core::ptr::read_unaligned(dst.as_ptr() as *const T) };
   |     --------------------------------------------------------------------------- unnecessary `let` binding
42 |     value
   |     ^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return
   = note: `#[warn(clippy::let_and_return)]` on by default
help: return the expression directly
   |
41 ~     
42 ~     unsafe { core::ptr::read_unaligned(dst.as_ptr() as *const T) }
   |

warning: `ruxrand` (lib) generated 3 warnings (run `cargo clippy --fix --lib -p ruxrand` to apply 3 suggestions)
    Checking ruxtask v0.1.0 (/home/kenable/code/pr_check/modules/ruxtask)
    Checking axsync v0.1.0 (/home/kenable/code/pr_check/modules/axsync)
    Checking ruxvda v0.1.0 (/home/kenable/code/pr_check/modules/ruxvda)
    Checking ruxfutex v0.1.0 (/home/kenable/code/pr_check/modules/ruxfutex)
    Checking ruxmm v0.1.0 (/home/kenable/code/pr_check/modules/ruxmm)
    Checking ruxfuse v0.1.0 (/home/kenable/code/pr_check/modules/ruxfuse)
    Checking ruxnet v0.1.0 (/home/kenable/code/pr_check/modules/ruxnet)
    Checking ruxdisplay v0.1.0 (/home/kenable/code/pr_check/modules/ruxdisplay)
    Checking rux9p v0.1.0 (/home/kenable/code/pr_check/modules/rux9p)
    Checking ruxruntime v0.1.0 (/home/kenable/code/pr_check/modules/ruxruntime)
    Checking ruxfeat v0.1.0 (/home/kenable/code/pr_check/api/ruxfeat)
    Checking arceos_api v0.1.0 (/home/kenable/code/pr_check/api/arceos_api)
    Checking axstd v0.1.0 (/home/kenable/code/pr_check/ulib/axstd)
warning: unreachable expression
   --> api/ruxos_posix_api/src/imp/getrandom.rs:127:5
    |
124 |           return random::<c_int>();
    |           ------------------------ any code following this expression is unreachable
...
127 | /     {
128 | |         match has_rdrand() {
129 | |             true => (random_hw() >> 33) as c_int,
130 | |             false => rand_lcg32() as c_int,
131 | |         }
132 | |     }
    | |_____^ unreachable expression
    |
    = note: `#[warn(unreachable_code)]` on by default

warning: unreachable expression
   --> api/ruxos_posix_api/src/imp/getrandom.rs:148:5
    |
145 |           return random::<c_long>();
    |           ------------------------- any code following this expression is unreachable
...
148 | /     {
149 | |         match has_rdrand() {
150 | |             true => (random_hw() >> 1) as c_long,
151 | |             false => random_lcg64() as c_long,
152 | |         }
153 | |     }
    | |_____^ unreachable expression

    Checking ruxos-display-draw-map v0.1.0 (/home/kenable/code/pr_check/apps/display/draw_map)
    Checking ruxos-display-basic-painting v0.1.0 (/home/kenable/code/pr_check/apps/display/basic_painting)
    Checking arceos-shell v0.1.0 (/home/kenable/code/pr_check/apps/fs/shell)
warning: variables can be used directly in the `format!` string
   --> api/ruxos_posix_api/src/imp/getrandom.rs:178:17
    |
178 |                 warn!("Failed to get random bytes: {:?}", e);
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
    = note: `#[warn(clippy::uninlined_format_args)]` on by default
help: change this to
    |
178 -                 warn!("Failed to get random bytes: {:?}", e);
178 +                 warn!("Failed to get random bytes: {e:?}");
    |

warning: `ruxos_posix_api` (lib) generated 3 warnings (run `cargo clippy --fix --lib -p ruxos_posix_api` to apply 1 suggestion)
    Checking ruxmusl v0.1.0 (/home/kenable/code/pr_check/ulib/ruxmusl)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.40s
cargo clippy -p axlog  
    Checking axlog v0.1.0 (/home/kenable/code/pr_check/crates/axlog)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s

@ken4647
Copy link
Contributor

ken4647 commented Jul 21, 2025

you need to fix clippy before go next step

false => return rand_lcg32() as c_int,
}
}
rand_lcg32() as c_int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unreachable expression
   --> api/ruxos_posix_api/src/imp/getrandom.rs:133:5
    |
128 | /         match has_rdrand() {
129 | |             true => return (random_hw() >> 33) as c_int,
130 | |             false => return rand_lcg32() as c_int,
131 | |         }
    | |_________- any code following this `match` expression is unreachable, as all arms diverge
132 |       }
133 |       rand_lcg32() as c_int
    |       ^^^^^^^^^^^^^^^^^^^^^ unreachable expression
    |
    = note: `#[warn(unreachable_code)]` on by default

warning: unreachable expression
   --> api/ruxos_posix_api/src/imp/getrandom.rs:152:5
    |
146 | /         match has_rdrand() {
147 | |             true => return (random_hw() >> 1) as c_long,
148 | |             false => return random_lcg64() as c_long,
149 | |         }
    | |_________- any code following this `match` expression is unreachable, as all arms diverge
...
152 |       random_lcg64() as c_long
    |       ^^^^^^^^^^^^^^^^^^^^^^^^ unreachable expression

fn info(&self) -> driver_rng::RngInfo {
unreachable!()
}
fn request_entropy(&mut self, dst: &mut [u8]) -> DevResult<usize> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: unused variable: `dst`
   --> modules/ruxdriver/src/dummy.rs:135:43
    |
135 |             fn request_entropy(&mut self, dst: &mut [u8]) -> DevResult<usize> {
    |                                           ^^^ help: if this is intentional, prefix it with an underscore: `_dst`
    |
    = note: `#[warn(unused_variables)]` on by default

let all_devices = ruxdriver::init_drivers();

#[cfg(feature = "rng")]
ruxrand::init(all_devices.rng, cpu_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you move ruxrand::init(cpu_id); there, it won't be initiazed when feature=rand is enabled, are you sure is it right. What will happen if rng is enabled without a virtio-rng?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If run with RNG=y but without FEATURE virtio-rng It will raise an error that there is no rng.

type PercpuRngType = Xoshiro256StarStar;

#[def_percpu]
pub(crate) static PERCPU_RNG: LazyInit<PercpuRngType> = LazyInit::new();
Copy link
Contributor

@ken4647 ken4647 Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code structure here is too similar to ruxrand, and it seems to involve almost no virtio hardware, so there's no hardware abstraction. It's also hard to see the necessity of this...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be precise, I believe this crate should only retain a lib.rs file. The other two pseudo-random number generators should be placed together within ruxrand; otherwise, it could easily cause confusion.

@lhw2002426 lhw2002426 force-pushed the add_rng branch 3 times, most recently from e736934 to 716af40 Compare July 24, 2025 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants