soc: qcom: socinfo.rs: Add Rust Socinfo Driver implementation
Subsystems (3)
RFC 1/1 soc: qcom: socinfo.rs: Add Rust Socinfo Driver implementation
Add initial work for Rust port of qcom/socinfo.c driver. Socinfo driver reads QCOM SMEM to learn current device information (e.g. SM8250).
- Added several C libraries to bindings_helper.h for FFI support from bindgen.
- Implemented PlatformDriver and KernelModule traits for SocInfoDriver
- Register with platform device framework
- Add devicetree compatibles
- Did not port functions referenced from SMEM, wanted to show a working proof of concept for Rust leaf driver with FFI code
- Register the discovered SMEM data with socinfo core.
---
This patch relies on Mateusz Jabłoński's try_format! macro patch from GitHub:
https://github.com/Rust-for-Linux/linux/pull/478
--- drivers/soc/qcom/Kconfig | 9 + drivers/soc/qcom/Makefile | 1 + drivers/soc/qcom/socinfo_rust.rs | 464 +++++++++++++++++++++++++++++++++++++++ rust/kernel/bindings_helper.h | 7 + 4 files changed, 481 insertions(+) create mode 100644 drivers/soc/qcom/socinfo_rust.rs
drivers/soc/qcom/Kconfig +9 -0
@@ -190,6 +190,15 @@ config QCOM_SOCINFO Say yes here to support the Qualcomm socinfo driver, providing information about the SoC to user space. +config QCOM_SOCINFO_RUST + tristate "Rust implementation of Qualcomm socinfo driver" + depends on HAS_RUST && QCOM_SMEM + help + This driver provides alternative Rust-based kernel-side support + for the socinfo driver. + + If unsure, say N. + config QCOM_WCNSS_CTRL tristate "Qualcomm WCNSS control driver" depends on ARCH_QCOM || COMPILE_TEST
drivers/soc/qcom/Makefile +1 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_QCOM_SMEM) += smem.o obj-$(CONFIG_QCOM_SMEM_STATE) += smem_state.o obj-$(CONFIG_QCOM_SMP2P) += smp2p.o obj-$(CONFIG_QCOM_SMSM) += smsm.o +obj-$(CONFIG_QCOM_SOCINFO_RUST) += socinfo_rust.o obj-$(CONFIG_QCOM_SOCINFO) += socinfo.o obj-$(CONFIG_QCOM_WCNSS_CTRL) += wcnss_ctrl.o obj-$(CONFIG_QCOM_APR) += apr.o
drivers/soc/qcom/socinfo_rust.rs new file +464 -0
465 lines not on this page · raw message
rust/kernel/bindings_helper.h +7 -0
@@ -18,6 +18,13 @@#include <linux/platform_device.h> #include <linux/of_platform.h> #include <linux/security.h> +#include <linux/sys_soc.h> +#include <linux/err.h> +#include <linux/debugfs.h> +#include <linux/soc/qcom/smem.h> +#include <linux/types.h> +#include <linux/device.h> +#include <linux/random.h> // `bindgen` gets confused at certain things const gfp_t BINDINGS_GFP_KERNEL =GFP_KERNEL ;
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Hi Antonio,
Thanks a lot for giving Rust in the kernel a try!
Cc'ing the socinfo maintainers.
A few comments inline. In general, you should seek to avoid unsafe in the Rust module as much as possible by providing safe abstractions inside the kernel crate instead, which are the ones dealing with the C-Rust interface / bindings.
Quoted 2 lines · Antonio Martorana
+ depends on
HAS_RUST&&QCOM_SMEM
This should be RUST, not HAS_RUST (the former is whether it is actually enabled, the latter whether the toolchain was found).
Review
Quoted 1 line
(quoted diff) +0
-0@@ -0,0 +1,464 @@
The SPDX identifier is missing at the top.
drivers/soc/qcom/socinfo_rust.rs line 4
Quoted 1 line
drivers/soc/qcom/socinfo_rust.rs new file +1
-0+#![feature(allocator_api,global_asm)]
Please format the code (you can use make rustfmt or manually call the rustfmt tool yourself).
Also, please pass the linter too if you did not do it (CLIPPY=1).
drivers/soc/qcom/socinfo_rust.rs line 29
Quoted 7 lines
drivers/soc/qcom/socinfo_rust.rs new file +7
-0+module! { +type :SocInfoDriver , +name :b"socinfo_rust" , + author:b"Antonio Martorana" , + description:b"QCOM socinfo rust implementation" , + license:b"GPL v2" , +}
This is a proof of concept, so it is OK, but I am not sure if we should say "rust" in the description for actual non-proof-of-concept modules. My guess is that most maintainers will only want to maintainer a single module, whether in C or Rust.
drivers/soc/qcom/socinfo_rust.rs line 57
Quoted 5 lines
drivers/soc/qcom/socinfo_rust.rs new file +5
-0+/* +* SMEM item id, used to acquire handles to respective +* SMEM region. +*/ +const SMEM_HW_SW_BUILD_ID :u32 =137 ;
In general, we do not use /*-style comments. In addition, this should be a documentation comment, i.e. ///.
Same for other places.
drivers/soc/qcom/socinfo_rust.rs line 64
Quoted 6 lines
drivers/soc/qcom/socinfo_rust.rs new file +6
-0+/* C code has #ifdef */ +const SMEM_IMAGE_VERSION_BLOCKS_COUNT :usize =32 ; +const SMEM_IMAGE_VERSION_SIZE :usize =4096 ; +const SMEM_IMAGE_VERSION_NAME_SIZE :usize =75 ; +const SMEM_IMAGE_VERSION_VARIANT_SIZE :usize =20 ; +const SMEM_IMAGE_VERSION_OEM_SIZE :usize =32 ;
We have support for conditional compilation based on the kernel configuration via e.g. an attribute like #[cfg(CONFIG_X)].
Ideally you can put this inside a module, which would allow you to have a single condition compilation avoid having to repeat the attribute, as well as avoiding repeated prefixes like SMEM_IMAGE_VERSION.
drivers/soc/qcom/socinfo_rust.rs line 77
Quoted 12 lines
drivers/soc/qcom/socinfo_rust.rs new file +12
-0+/* +* SMEM Image table indices +*/ +const SMEM_IMAGE_TABLE_BOOT_INDEX :u32 =0 ; +const SMEM_IMAGE_TABLE_TZ_INDEX :u32 =1 ; +const SMEM_IMAGE_TABLE_RPM_INDEX :u32 =3 ; +const SMEM_IMAGE_TABLE_APPS_INDEX :u32 =10 ; +const SMEM_IMAGE_TABLE_MPSS_INDEX :u32 =11 ; +const SMEM_IMAGE_TABLE_ADSP_INDEX :u32 =12 ; +const SMEM_IMAGE_TABLE_CNSS_INDEX :u32 =13 ; +const SMEM_IMAGE_TABLE_VIDEO_INDEX :u32 =14 ; +const SMEM_IMAGE_VERSION_TABLE :u32 =496 ;
Same here -- this could be a mod and a doc comment on it.
drivers/soc/qcom/socinfo_rust.rs line 121
Quoted 2 lines
drivers/soc/qcom/socinfo_rust.rs new file +2
-0+struct SocInfo { + fmt: bindings::__le32 ,
Rust modules should not access bindings in general -- abstractions should be provided in rust/.
Also, __le32 is for sparse -- to do something similar, we could use a wrapper type that encodes the endianness.
drivers/soc/qcom/socinfo_rust.rs line 306
Quoted 1 line
drivers/soc/qcom/socinfo_rust.rs new file +1
-0+unsafe {let ref_mut_seq_priv = seq_private.as_mut ().unwrap (); }
Unsafe blocks must have a SAFETY: annotation, justifying all the requirements of as_mut() (alignment, no aliasing, etc.).
They should also be as small as possible (to clearly indicate the part that is unsafe), without using a single block for several unsafe operations.
drivers/soc/qcom/socinfo_rust.rs line 334
Quoted 3 lines
drivers/soc/qcom/socinfo_rust.rs new file +3
-0+if model <0 { +return -22 ;//EINVAL + }
We have the constants available. Also, the function should return a Result, not a naked integer.
drivers/soc/qcom/socinfo_rust.rs line 342
Quoted 1 line
drivers/soc/qcom/socinfo_rust.rs new file +1
-0+unsafe { bindings::seq_printf (seqas *mut bindings::seq_file ,format,PMIC_MODELS [modelas usize ])};
seq_printf should likely be a macro like pr_info! etc. that we have.
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+/* +fn qcom_show_pmic_model_array(seq: &mut bindings::seq_file, p: &mut c_types::c_void) -> u32{ + +}
drivers/soc/qcom/socinfo_rust.rs line 363
Quoted 10 lines
drivers/soc/qcom/socinfo_rust.rs new file +10
-0+ +fn qcom_show_pmic_die_revision (seq: &mut bindings::seq_file , p: &mut c_types::c_void ) ->u32 { + +} + +fn qcom_show_chip_id (seq: &mut bindings::seq_file , p: &mut c_types::c_void ) ->u32 { + +} + +*/
Commented out code (also in other places).
Cheers,
Miguel
On Mon, Aug 16, 2021 at 06:23:32PM -0700, Antonio Martorana wrote:
[...]
drivers/soc/qcom/socinfo_rust.rs line 4
Quoted 9 lines
new file mode 100644
index 0000000..8af697adrivers/soc/qcom/socinfo_rust.rs new file +4
-0@@ -0,0 +1,464 @@ +//! Qualcomm support for socinfo.c + +#![no_std] +#![feature(allocator_api,global_asm)]
Not really related to this patch, but I wish there was a more ergonomic way to specify these crate level features. I think these two lines are c&p in a lot of places.
[...]
drivers/soc/qcom/socinfo_rust.rs line 37
Quoted 7 lines
drivers/soc/qcom/socinfo_rust.rs new file +7
-0+/* +* SoC version type with major number in the upper 16 bits and minor +* number in the lower 16 bits. +*/ +const fn socinfo_major (ver:u32 ) ->u32 { +let major:u32 = (ver >>16 ) &0xFFFF ; +return major;
This can be simplified as
const fn socinfo_major(ver: u32) -> u32 {
(ver >> 16) & 0xFFFF
}drivers/soc/qcom/socinfo_rust.rs line 48
Quoted 11 lines
drivers/soc/qcom/socinfo_rust.rs new file +11
-0+} + +const fn socinfo_minor (ver:u32 ) ->u32 { +let minor:u32 = ver &0xFFFF ; +return minor; +} + +const fn socinfo_version (maj:u32 , min:u32 ) ->u32 { +let version:u32 = ((maj &0xFFFF ) <<16 ) | (min &0xFFFF ); +return version; +}
Same for these two functions. They can be simplified as well.
I have one further question: why aren't these helpers implemented as methods of SocInfo? Like:
impl SocInfo {
fn major(&self) -> u32 {
(self.ver >> 16) & 0xFFFF
}
}Thanks,
Wei.
RFC v2 1/1 soc: qcom: socinfo.rs: Add Rust Socinfo Driver implementation
In reply to Antonio MartoranaTo: Miguel Ojeda <[email protected]>,
Alex Gaynor <[email protected]>, Wedson Almeida Filho <[email protected]>
Cc: [email protected],
Trilok Soni <[email protected]>, Elliot Berman <[email protected]>, linux-arm-msm <[email protected]>, Alex Gaynor <[email protected]>, Wei Liu <[email protected]>
Add initial work for Rust port of qcom/socinfo.c driver. Socinfo driver reads QCOM SMEM to learn current device information (e.g. SM8250).
- Added several C libraries to bindings_helper.h for FFI support from bindgen.
- Implemented PlatformDriver and KernelModule traits for SocInfoDriver
- Register with platform device framework
- Add devicetree compatibles
- Did not port functions referenced from SMEM, wanted to show a working proof of concept for Rust leaf driver with FFI code
- Register the discovered SMEM data with socinfo core.
---
This patch relies on Mateusz Jabłoński's try_format! macro patch from GitHub:
https://github.com/Rust-for-Linux/linux/pull/478
V2: Addressed formating issues, added conditional compilation to debug_fs variables, fixed Kconfig, and still working on removing references to bindings::
by abstracting FFI functions through rust/ .
--- drivers/soc/qcom/Kconfig | 9 + drivers/soc/qcom/Makefile | 1 + drivers/soc/qcom/socinfo_rust.rs | 794 +++++++++++++++++++++++++++++++++++++++ rust/kernel/bindings_helper.h | 7 + 4 files changed, 811 insertions(+) create mode 100644 drivers/soc/qcom/socinfo_rust.rs
drivers/soc/qcom/Kconfig +9 -0
@@ -190,6 +190,15 @@ config QCOM_SOCINFO Say yes here to support the Qualcomm socinfo driver, providing information about the SoC to user space. +config QCOM_SOCINFO_RUST + tristate "Rust implementation of Qualcomm socinfo driver" + depends on RUST && QCOM_SMEM + help + This driver provides alternative Rust-based kernel-side support + for the socinfo driver. + + If unsure, say N. + config QCOM_WCNSS_CTRL tristate "Qualcomm WCNSS control driver" depends on ARCH_QCOM || COMPILE_TEST
drivers/soc/qcom/Makefile +1 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_QCOM_SMEM) += smem.o obj-$(CONFIG_QCOM_SMEM_STATE) += smem_state.o obj-$(CONFIG_QCOM_SMP2P) += smp2p.o obj-$(CONFIG_QCOM_SMSM) += smsm.o +obj-$(CONFIG_QCOM_SOCINFO_RUST) += socinfo_rust.o obj-$(CONFIG_QCOM_SOCINFO) += socinfo.o obj-$(CONFIG_QCOM_WCNSS_CTRL) += wcnss_ctrl.o obj-$(CONFIG_QCOM_APR) += apr.o
drivers/soc/qcom/socinfo_rust.rs new file +794 -0
794 lines not on this page · raw message
rust/kernel/bindings_helper.h +7 -0
@@ -18,6 +18,13 @@#include <linux/platform_device.h> #include <linux/of_platform.h> #include <linux/security.h> +#include <linux/sys_soc.h> +#include <linux/err.h> +#include <linux/debugfs.h> +#include <linux/soc/qcom/smem.h> +#include <linux/types.h> +#include <linux/device.h> +#include <linux/random.h> // `bindgen` gets confused at certain things const gfp_t BINDINGS_GFP_KERNEL =GFP_KERNEL ;
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Hi Antonio,
Thanks again for working on this.
Quoted 4 lines · Antonio Martorana
V2: Addressed formating issues, added conditional compilation to
debug_fsvariables, fixedKconfig, and still working on removing references to bindings::
by abstracting FFI functions throughrust/.
Normally the changelog goes outside the commit message, i.e. after ---.
drivers/soc/qcom/socinfo_rust.rs line 27
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+/// SoC version type with major number in the upper 16 bits and minor +/// number in the lower 16 bits. + +const fn socinfo_major (ver:u32 ) ->u32 {
Doc comments should "touch" the function they document. But using /// like shown here would document only socinfo_major, and what the comment says is not really the function documentation. What I meant in v1 is that perhaps you can find a better way to document all of them, or none.
Given socinfo_version is not used, I would remove that one, and put "// The upper 16 bits are the major." and "// The lower 16 bits are the minor." as normal comments inside the function body. Even better, you could create a SocInfoVersion type.
drivers/soc/qcom/socinfo_rust.rs line 56
Quoted 3 lines
drivers/soc/qcom/socinfo_rust.rs new file +3
-0+/// +/// SMEM Image table indices +///
No empty comment lines before/after, please. In addition, /// is a doc comment, not a normal comment. You are trying to give a heading for a set of consts, not document the first one.
We should add a lint for the former, and probably we could also try one for the latter, too...
Review
Quoted 2 lines
(quoted diff) +2
-0+#[cfg(CONFIG_DEBUG_FS)] +const PMIC_MODELS: [&str; 37] = [
This is fine, but since you have created a config_debug_fs module, perhaps you could move everything inside it.
Related: perhaps the name for that module could be something simpler, like debugfs.
drivers/soc/qcom/socinfo_rust.rs line 143
Quoted 3 lines
drivers/soc/qcom/socinfo_rust.rs new file +3
-0+unsafe { +let ref_mut_seq_priv = seq_private.as_mut ().unwrap (); + }
The // SAFETY: comments in several places are missing.
Quoted 1 line
+fn
qcom_show_pmic_model(seq: &mut bindings::seq_file, p: &mut c_types::c_void)-> Result {
Several of these functions cannot fail -- so why Result?
...ah, I see, you are panicking now -- but then you don't need the Result. If this was userspace and you were not expecting a failure, then yes, panic might have been a good idea. However, here we are supposed to return the error like you did in v1. When I mentioned you should use Result, it means you should return the EINVAL as such (using the pre-defined error constants we have in rust/kernel/error.rs, which are in the prelude for ease-of-use, see e.g. https://rust-for-linux.github.io/docs/kernel/prelude/struct.Error.html).
drivers/soc/qcom/socinfo_rust.rs line 173
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+// SAFETY: `socinfo` is guaranteed to be: +// - A valid (and instalized), non-null pointer +// - Is properly aligned +// - Adheres to aliasing rules
The // SAFETY: comment should explain why the callee's preconditions hold, not just state them (which does not help much a future reader searching for UB).
Review
Quoted 2 lines
(quoted diff) +2
-0+ unsafe { + let mut_socinfo = socinfo.as_mut().unwrap();
As mentioned in v1, unsafe blocks should be split and reduced as much as possible.
drivers/soc/qcom/socinfo_rust.rs line 177
Quoted 2 lines
drivers/soc/qcom/socinfo_rust.rs new file +2
-0+let test_model =socinfo_minor ((*socinfo).pmic_model); + model = test_model;
Why is test_model there?
drivers/soc/qcom/socinfo_rust.rs line 183
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+let mut check_valid_model:bool =false ; +if !PMIC_MODELS [modelas usize ].is_empty () { + check_valid_model =true ; + }
Try to avoid mutability where possible, e.g.:
let valid_model = !PMIC_MODELS[model as usize].is_empty();
Quoted 1 line
+ if model <
PMIC_MODELS.len()as u32 &&check_valid_model{
Something looks odd -- we are checking whether the model is within PMIC_MODELS, but you already used it to index (which is safe, but because it will panic if wrong -- which we do not want!).
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+#[cfg( CONFIG_DEBUG_FS )] +fn qcom_show_pmic_model_array (seq: &mut bindings::seq_file , p: &mut c_types::c_void ) ->Result { +Ok (()) +}
drivers/soc/qcom/socinfo_rust.rs line 213
Quoted 10 lines
drivers/soc/qcom/socinfo_rust.rs new file +10
-0+ +#[cfg( CONFIG_DEBUG_FS )] +fn qcom_show_pmic_die_revision (seq: &mut bindings::seq_file , p: &mut c_types::c_void ) ->Result { +Ok (()) +} + +#[cfg( CONFIG_DEBUG_FS )] +fn qcom_show_chip_id (seq: &mut bindings::seq_file , p: &mut c_types::c_void ) ->Result { +Ok (()) +}
Even if not commented out, this is still dead code -- it is best to remove it until you implement them.
Quoted 1 line
+ fmt:
bindings::__le32,
Same thing about bindings:: from v1 etc. -- if this were not a proof of concept, it should be properly abstracted.
Quoted 1 line
+const
SOC_ID: [SocId; 105] = [
For lots of data, it is usually better to use static to have a single instance somewhere in memory (vs. e.g. inlining).
drivers/soc/qcom/socinfo_rust.rs line 251
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+SocId { + id:87 , + name:c_str! ("MSM8960" ), + },
For repetitive things like this (and especially if the automatic formatting makes it look bad!), a local "macro by example" is usually a good approach, e.g.:
soc_ids!(3,
87 "MSM8960"
109 "APQ8064"
122 "MSM8660A etc etc"
);or any other syntax you like for the particular case. It looks much better, and separates the details of the type from the data table.
The macro can be something like:
macro_rules! soc_ids(
($length:literal, $($id:literal $name:literal)*) => {
static SOC_ID: [SocID; $length] = [
$(SocID { id: $id, name: $name },)*
];
}
);https://godbolt.org/z/dWofxYn19 to play with it.
drivers/soc/qcom/socinfo_rust.rs line 673
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+struct SocId { + id:u32 , + name: &'static str::CStr , +}
Even if Rust allows otherwise, I would still try to put declarations before they are used.
Quoted 1 line
+fn
socinfo_machine(id: &u32)-> Option<&'staticstr::CStr> {
It is simpler to pass an integer as-is, rather than a pointer to it.
Quoted 1 line
+ for idx in
SOC_ID{
You probably want &SOC_ID here (see the static vs. const discussion above).
drivers/soc/qcom/socinfo_rust.rs line 681
Quoted 5 lines
drivers/soc/qcom/socinfo_rust.rs new file +5
-0+if idx.id == *id { +return Some (idx.name); + } + } +None
Loops like this are good candidates for a functional style -- something like:
SOC_ID.iter().find(|x| x.id == id).map(|x| x.name)
Whether this looks better or not, of course, is up for debate ;)
drivers/soc/qcom/socinfo_rust.rs line 773
Quoted 3 lines
drivers/soc/qcom/socinfo_rust.rs new file +3
-0+Ok (drv_data) + } +fn remove (device_id:i32 , _drv_data:Self ::DrvData ) ->Result {
Newline between functions, please.
Normally we only prefix by underscore if unused.
Cheers,
Miguel
Just CC'ing the recipients who were missed, and thank you for all the feedback
its much appreciated.
Cheers,
Antonio Martorana
Quoted 204 lines · Miguel Ojeda
Hi Antonio,
Thanks again for working on this.
Quoted 4 lines · Antonio Martorana
V2: Addressed formating issues, added conditional compilation to
debug_fsvariables, fixedKconfig, and still working on removing references to bindings::
by abstracting FFI functions throughrust/.Normally the changelog goes outside the commit message, i.e. after
---.drivers/soc/qcom/socinfo_rust.rs line 27
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+/// SoC version type with major number in the upper 16 bits and minor +/// number in the lower 16 bits. + +const fn socinfo_major (ver:u32 ) ->u32 {Doc comments should "touch" the function they document. But using
///like shown here would document onlysocinfo_major, and what the comment says is not really the function documentation. What I meant in v1 is that perhaps you can find a better way to document all of them, or none.Given
socinfo_versionis not used, I would remove that one, and put "// The upper 16 bits are the major." and "// The lower 16 bits are the minor." as normal comments inside the function body. Even better, you could create aSocInfoVersiontype.drivers/soc/qcom/socinfo_rust.rs line 56
Quoted 3 lines
drivers/soc/qcom/socinfo_rust.rs new file +3
-0+/// +/// SMEM Image table indices +/// No empty comment lines before/after, please. In addition,
///is a doc comment, not a normal comment. You are trying to give a heading for a set ofconsts, not document the first one.We should add a lint for the former, and probably we could also try one for the latter, too...
Review
Quoted 2 lines
(quoted diff) +2
-0+#[cfg(CONFIG_DEBUG_FS)] +const PMIC_MODELS: [&str; 37] = [This is fine, but since you have created a
config_debug_fsmodule, perhaps you could move everything inside it.Related: perhaps the name for that module could be something simpler, like
debugfs.drivers/soc/qcom/socinfo_rust.rs line 143
Quoted 3 lines
drivers/soc/qcom/socinfo_rust.rs new file +3
-0+unsafe { +let ref_mut_seq_priv = seq_private.as_mut ().unwrap (); + }The
// SAFETY:comments in several places are missing.Quoted 2 lines
+fn
qcom_show_pmic_model(seq: &mut bindings::seq_file, p: &mut c_types::c_void)-> Result {Several of these functions cannot fail -- so why
Result?...ah, I see, you are panicking now -- but then you don't need the
Result. If this was userspace and you were not expecting a failure, then yes, panic might have been a good idea. However, here we are supposed to return the error like you did in v1. When I mentioned you should useResult, it means you should return theEINVALas such (using the pre-defined error constants we have inrust/kernel/error.rs, which are in the prelude for ease-of-use, see e.g. https://rust-for-linux.github.io/docs/kernel/prelude/struct.Error.html).drivers/soc/qcom/socinfo_rust.rs line 173
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+// SAFETY: `socinfo` is guaranteed to be: +// - A valid (and instalized), non-null pointer +// - Is properly aligned +// - Adheres to aliasing rules The
// SAFETY:comment should explain why the callee's preconditions hold, not just state them (which does not help much a future reader searching for UB).Review
Quoted 2 lines
(quoted diff) +2
-0+ unsafe { + let mut_socinfo = socinfo.as_mut().unwrap();As mentioned in v1,
unsafeblocks should be split and reduced as much as possible.drivers/soc/qcom/socinfo_rust.rs line 177
Quoted 2 lines
drivers/soc/qcom/socinfo_rust.rs new file +2
-0+let test_model =socinfo_minor ((*socinfo).pmic_model); + model = test_model;Why is
test_modelthere?drivers/soc/qcom/socinfo_rust.rs line 183
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+let mut check_valid_model:bool =false ; +if !PMIC_MODELS [modelas usize ].is_empty () { + check_valid_model =true ; + }Try to avoid mutability where possible, e.g.:
let valid_model = !PMIC_MODELS[model as usize].is_empty();Quoted 1 line
+ if model <
PMIC_MODELS.len()as u32 &&check_valid_model{Something looks odd -- we are checking whether the model is within
PMIC_MODELS, but you already used it to index (which is safe, but because it will panic if wrong -- which we do not want!).Quoted 15 lines
(quoted diff) +2
-0+#[cfg(CONFIG_DEBUG_FS)] +fn qcom_show_pmic_model_array(seq: &mut bindings::seq_file, p: &mut
c_types::c_void) -> Result {drivers/soc/qcom/socinfo_rust.rs new file +5
-0+Ok (()) +} + +#[cfg( CONFIG_DEBUG_FS )] +fn qcom_show_pmic_die_revision(seq: &mut bindings::seq_file, p: &mut
c_types::c_void) -> Result {drivers/soc/qcom/socinfo_rust.rs new file +5
-0+Ok (()) +} + +#[cfg( CONFIG_DEBUG_FS )] +fn qcom_show_chip_id(seq: &mut bindings::seq_file, p: &mut
c_types::c_void) -> Result {Review
Quoted 2 lines
(quoted diff) +2
-0+ Ok(()) +}Even if not commented out, this is still dead code -- it is best to remove it until you implement them.
Quoted 1 line
+ fmt:
bindings::__le32,Same thing about
bindings::from v1 etc. -- if this were not a proof of concept, it should be properly abstracted.Quoted 1 line
+const
SOC_ID: [SocId; 105] = [For lots of data, it is usually better to use
staticto have a single instance somewhere in memory (vs. e.g. inlining).drivers/soc/qcom/socinfo_rust.rs line 251
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+SocId { + id:87 , + name:c_str! ("MSM8960" ), + },For repetitive things like this (and especially if the automatic formatting makes it look bad!), a local "macro by example" is usually a good approach, e.g.:
soc_ids!(3, 87 "MSM8960" 109 "APQ8064" 122 "MSM8660A etc etc" );or any other syntax you like for the particular case. It looks much better, and separates the details of the type from the data table.
The macro can be something like:
macro_rules! soc_ids( ($length:literal, $($id:literal $name:literal)*) => { static SOC_ID: [SocID; $length] = [ $(SocID { id: $id, name: $name },)* ]; } );https://godbolt.org/z/dWofxYn19 to play with it.
drivers/soc/qcom/socinfo_rust.rs line 673
Quoted 4 lines
drivers/soc/qcom/socinfo_rust.rs new file +4
-0+struct SocId { + id:u32 , + name: &'static str::CStr , +}Even if Rust allows otherwise, I would still try to put declarations before they are used.
Quoted 1 line
+fn
socinfo_machine(id: &u32)-> Option<&'staticstr::CStr> {It is simpler to pass an integer as-is, rather than a pointer to it.
Quoted 1 line
+ for idx in
SOC_ID{You probably want
&SOC_IDhere (see thestaticvs.constdiscussion above).drivers/soc/qcom/socinfo_rust.rs line 681
Quoted 5 lines
drivers/soc/qcom/socinfo_rust.rs new file +5
-0+if idx.id == *id { +return Some (idx.name); + } + } +None Loops like this are good candidates for a functional style -- something like:
SOC_ID.iter().find(|x| x.id == id).map(|x| x.name)Whether this looks better or not, of course, is up for debate ;)
drivers/soc/qcom/socinfo_rust.rs line 773
Quoted 3 lines
drivers/soc/qcom/socinfo_rust.rs new file +3
-0+Ok (drv_data) + } +fn remove (device_id:i32 , _drv_data:Self ::DrvData ) ->Result {Newline between functions, please.
Normally we only prefix by underscore if unused.
Cheers,
Miguel