Skip to content

rust: miscdev: support names created at runtime #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2022

Conversation

wedsonaf
Copy link

@wedsonaf wedsonaf commented Feb 15, 2022

The registration now holds an owned C string while the miscdev is
registered, and frees it on drop.

Now names are formatted with the fmt macro instead of being static
C strings. This enables scenarios when the device name is constructed at
runtime, i.e., where a static C string does not exist.

Signed-off-by: Wedson Almeida Filho [email protected]

Options::new().register_new(name, open_data)
}

/// Registers a miscellaneous device with the rest of the kernel.
///
/// It must be pinned because the memory block that represents the registration is
/// self-referential.
pub fn register(self: Pin<&mut Self>, name: &'static CStr, open_data: T::OpenData) -> Result {
pub fn register(self: Pin<&mut Self>, name: fmt::Arguments<'_>, open_data: T::OpenData) -> Result {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe accept Cow<'static, CStr> instead? That allows avoiding an allocation in the common case of the name being constant. Or impl Into<Cow<'static, CStr>> to allow directly passing in both a CStr and CString without having to construct a Cow<CStr>.

Copy link
Author

Choose a reason for hiding this comment

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

I'd like to leave this as is now, and once #678 is done (or as part of it) we can optimise this avoid the allocation when using a &'static CStr

The registration now holds an owned C string while the miscdev is
registered, and frees it on drop.

Now names are formatted with the `fmt` macro instead of being static
C strings. This enables scenarios when the device name is constructed at
runtime, i.e., where a static C string does not exist.

Signed-off-by: Wedson Almeida Filho <[email protected]>
@wedsonaf
Copy link
Author

Rebased now that all dependencies are merged.

@m-falkowski1
Copy link

m-falkowski1 commented Feb 16, 2022

@wedsonaf

This approach could also be used in the hwrng registration #592. Will it be suitable there?

@wedsonaf
Copy link
Author

This approach could also be used in the hwrng registration #592. Will it be suitable there?

Yes, definitely.

Do you expect people will generate names dynamically for hwrng?

@m-falkowski1
Copy link

m-falkowski1 commented Feb 17, 2022

Yes, definitely.

Then I'm working on this.

Do you expect people will generate names dynamically for hwrng?

Actually, this is the case in the trng driver already. The name for hwrng driver is set to the name of pdev.
So it may be provided in Rust's context with pdev.name() call probably.

@wedsonaf wedsonaf merged commit e2c5979 into Rust-for-Linux:rust Feb 18, 2022
@wedsonaf wedsonaf deleted the miscdev-name branch February 18, 2022 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants