Skip to content
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

Improve module macro #215

Closed
wedsonaf opened this issue Apr 23, 2021 · 15 comments · Fixed by #234
Closed

Improve module macro #215

wedsonaf opened this issue Apr 23, 2021 · 15 comments · Fixed by #234
Labels
• lib Related to the `rust/` library.

Comments

@wedsonaf
Copy link

wedsonaf commented Apr 23, 2021

  1. Fields should be specifiable in any order
  2. Only license and type should be mandatory, the others should be optional
@ojeda ojeda added prio: normal • lib Related to the `rust/` library. labels Apr 23, 2021
@kloenk
Copy link
Member

kloenk commented Apr 28, 2021

will try myself on this now, as I need alias for the net stuff.

@ojeda
Copy link
Member

ojeda commented Apr 28, 2021

Field should be specifiable in any order

I commented on this somewhere else. While the C side allows any ordering (and uses separate macros), I think forcing always the same order will be better for consistency long-term and to always see our module! macro written the same way. The {}-style macro can be misleading though, perhaps we can change that.

(Also, we need to be mindful that if we allow arbitrary orderings now, it will be harder to go back and enforce a fixed order later on because it will require fixing many modules).

@kloenk
Copy link
Member

kloenk commented Apr 28, 2021

If we have a fixed ordering, what if we don't need a field in a driver? For example alias_rtnl_link I only need in the network driver. what about drivers not using that field. IMHO it's easier to implement without fixed ordering, and also I don't see a real reason for ordering, besides convenience.

@wedsonaf
Copy link
Author

I commented on this somewhere else. While the C side allows any ordering (and uses separate macros), I think forcing always the same order will be better for consistency long-term and to always see our module! macro written the same way. The {}-style macro can be misleading though, perhaps we can change that.

I don't mind if we decide to force a strict order. (I'm more interested in making optional arguments truly optional.) However, at the moment I'm failing to see the benefits we get from it (the strict ordering).

@ojeda
Copy link
Member

ojeda commented Apr 28, 2021

If we have a fixed ordering, what if we don't need a field in a driver?

Optional fields are OK, that is orthogonal to the ordering.

I don't see a real reason for ordering, besides convenience.

Consistency is the reason :) In my view, anything that allows arbitrary ordering needs to be justified, rather than the opposite...

@ojeda
Copy link
Member

ojeda commented Apr 28, 2021

However, at the moment I'm failing to see the benefits we get from it (the strict ordering).

Basically, we need to choose between easier-to-write vs. easier-to-read. Since most of the time we are reading, I think it is best to enforce the fixed ordering.

In the end, I assume people will be copy-pasting the macro from one file to another, so I thought it was not a big deal to have a fixed ordering.

As you know, I usually "fight" for consistency everywhere. :-P But if people really hate it (three people so far have suggested arbitrary orderings already), I am not going to be against everyone -- we can change it.

@kloenk
Copy link
Member

kloenk commented Apr 28, 2021

@wedsonaf you said name is optional, what should the __LOG_PREFIX be if name is not set? should it default to the stringified version of type?

@ojeda
Copy link
Member

ojeda commented Apr 28, 2021

One alternative could be the file/crate name, since kernel modules that are single-file typically have the same name as the kernel module name.

@wedsonaf
Copy link
Author

Basically, we need to choose between easier-to-write vs. easier-to-read. Since most of the time we are reading, I think it is best to enforce the fixed ordering.

We do need to make it minimally easy to write. For example, if I swap the order of author and description, here's what I get:

error: proc macro panicked
  --> drivers/android/rust_binder.rs:32:1
   |
32 | / module! {
33 | |     type: BinderModule,
34 | |     name: b"rust_binder",
35 | |     description: b"Android Binder",
...  |
38 | |     params: {},
39 | | }
   | |_^
   |
   = help: message: assertion failed: `(left == right)`
             left: `"description"`,
            right: `"author"`

So IMO we either need to allow any order or have a better error reporting when we get things out of order.

@kloenk
Copy link
Member

kloenk commented Apr 28, 2021

im building something like panic!("'{}' is not allowed at this position", name); where name is the name of the current field, e.g. "author"

@wedsonaf
Copy link
Author

im building something like panic!("'{}' is not allowed at this position", name); where name is the name of the current field, e.g. "author"

Suppose I'm writing a driver and I get the error message above. It's certainly more helpful than what I get today, but it doesn't guide me to a solution: perhaps also specify what is allowed?

@ojeda
Copy link
Member

ojeda commented Apr 28, 2021

We do need to make it minimally easy to write.

Agreed, better error reporting for module! was discussed elsewhere, but the current errors are a consequence of the current (straightforward) implementation. In general, we should not base the design off those limitations.

@kloenk
Copy link
Member

kloenk commented Apr 28, 2021

One alternative could be the file/crate name, since kernel modules that are single-file typically have the same name as the kernel module name.

I only see this unstable api[1], to get the file/crate name. as I could stringify the type I would prefer not to use this. are you ok with that?

1: proc_macro::Span

@ojeda
Copy link
Member

ojeda commented Apr 28, 2021

are you ok with that?

Hmm... usually kernel module names have snake_case. Not sure what we should do. Perhaps keep the name non-optional for the moment, and we can revisit later.

@ojeda
Copy link
Member

ojeda commented Apr 28, 2021

To improve error reporting easily, what you can do is walk the token stream as if ordering was arbitrary, since that it is easy, but keep a list of seen keys. Then, at the very end, check that the list is as expected, and otherwise you can panic with:

Keys need to be ordered like this: <list properly ordered>

e.g. if the user gave license and type, only those need to appear -- no need to give all the possibilities.

That way, errors that are about invalid keys or invalid values are reported first, without bothering the user about the ordering. Then, at the end, where you know everything is valid but only the ordering was wrong, you can report exactly that, since you know only the ordering was wrong.

@ojeda ojeda closed this as completed in #234 May 1, 2021
ojeda pushed a commit that referenced this issue Oct 7, 2024
…ent event class

While running checkpatch.pl against a patch that modifies the
btrfs_qgroup_extent event class, it complained about using a comma instead
of a semicolon:

  $ ./scripts/checkpatch.pl qgroups/0003-btrfs-qgroups-remove-bytenr-field-from-struct-btrfs_.patch
  WARNING: Possible comma where semicolon could be used
  #215: FILE: include/trace/events/btrfs.h:1720:
  +		__entry->bytenr		= bytenr,
		__entry->num_bytes	= rec->num_bytes;

  total: 0 errors, 1 warnings, 184 lines checked

So replace the comma with a semicolon to silence checkpatch and possibly
other tools. It also makes the code consistent with the rest.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
• lib Related to the `rust/` library.
Development

Successfully merging a pull request may close this issue.

3 participants