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

module! improvements #278

Open
wants to merge 9 commits into
base: rust
Choose a base branch
from
Open

module! improvements #278

wants to merge 9 commits into from

Conversation

nbdd0121
Copy link
Member

@nbdd0121 nbdd0121 commented May 19, 2021

This is a large PR. It's best reviewed by commits.

  • commit Rename libmodule to libmacros

Renaming to libmacros allows us to potentially have more procedural macros in the future without introducing additional crates.

  • commit Vendor literal parsing from syn crate
  • commit accept str instead of bstr in module!

Fixes #252. Doing so requires capability to parse strings so I vendored in the lit mod from syn.

  • commit Vendor buffer from syn crate
  • commit module macro cleanup using buffer

Some cleanup with buffer mod vendored from syn. TokenStream itself is very hard to use for parsing, because it does not provide lookahead capability. With the buffer it can be simplified a lot.

  • commit cleanup modinfo generation in module!
  • commit separate parse and codegen in module!

These organizes the code so that parsing and codegen are split into two phased. This allows the module_misc_device macro to do codegen directly, so it does not have to re-generate the module! invocation.

  • commit multiple author/alias support in module!

Fixes #244, fixes #246. Becomes a very simple change with all the cleanups made.

  • commit allow any constexpr to be used as param default

With syn's buffer's lookahead capability this also becomes a trivial change.

@ksquirrel

This comment has been minimized.

@@ -0,0 +1,401 @@
use proc_macro::{Literal, Span};
Copy link
Member

Choose a reason for hiding this comment

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

Missing SPDX.

Comment on lines 3 to 4
//! Shared parsing functions for use in procedural macros. These functions are
//! from [syn](https://github.com/dtolnay/syn).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
//! Shared parsing functions for use in procedural macros. These functions are
//! from [syn](https://github.com/dtolnay/syn).
//! Shared parsing functions for use in procedural macros.
//!
//! These functions come from the [`syn`](https://github.com/dtolnay/syn) crate.

Are the files too different from the original ones? If not, perhaps recording in the comment the Git tag from where they were checked out would be nice. If yes, doing it in the commit message is fair.

Copy link
Member Author

Choose a reason for hiding this comment

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

I does make a few changes to strip away anything that depends on macros, bigint and unicode-xid. But I agree it's better to also record version number in the comment.

Copy link
Member

Choose a reason for hiding this comment

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

That's great, thanks for removing those bits.

Copy link
Member

Choose a reason for hiding this comment

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

Also mentioning that in the comment would be nice too (i.e. so that people doing future updates, if any, are aware of that and do not add bigint etc. back).

@ojeda
Copy link
Member

ojeda commented May 19, 2021

Looks great, as usual!

GitHub does not allow to comment on the commit messages themselves :( So I put those here:

  • It would be best if you move the explanations you have written in the PR body above into the commit messages, i.e. please explain the "why" in the commit messages themselves.
  • For the separation of codegen and parsing, it would be nice to say why avoiding a macro invocation is better, e.g. is it faster? or is it "only" for cleanliness of the code? (do not get me wrong: doing cleanups is great nevertheless!).
  • A super trivial nit: in one of the titles you used backticks for e.g. module! but not for str or bstr. I would do it for all of them (there are other titles like that -- I noticed because that was in the same one! :)

(I also really miss the "suggestion" feature in the commit-by-commit view -- if someone knows how to easily do it without having to move to the "changes" view, please let me know!).

@ksquirrel

This comment has been minimized.

nbdd0121 added 9 commits May 26, 2021 03:31
Renaming to libmacros allows us to potentially have more procedural
macros in the future without introducing additional crates.

Signed-off-by: Gary Guo <[email protected]>
`module!` macro currently have some ad-hoc literal
parsing. `syn` crate provides some proper parsing
for us to use.

Signed-off-by: Gary Guo <[email protected]>
`TokenStream` itself is very hard to use for parsing, as it does not
provide lookahead capability. `syn`'s buffer will provide cleanup
opportunity.

Signed-off-by: Gary Guo <[email protected]>
Make use of `syn` buffer's lookahead capability
to remove `try_*` parsing functions.

Signed-off-by: Gary Guo <[email protected]>
Doing so allows `module_misc_device!` to do codegen directly instead of
generating a `module!` invocation. Generating `module!` invocation is
error-prone and would require significant effort when features are added
to `module!` macro or when syntax is changed.

Signed-off-by: Gary Guo <[email protected]>
@ksquirrel
Copy link
Member

Review of 6f7f4b324c0f:

  • ⚠️ This PR changes more than 20 files.
  • ⚠️ This PR adds/deletes more than 500 lines.
  • ✔️ Commit 0748450: Looks fine!
  • ✔️ Commit fd4ef65: Looks fine!
  • ✔️ Commit ab4c335: Looks fine!
  • ✔️ Commit fc7c60f: Looks fine!
  • ✔️ Commit c677a79: Looks fine!
  • ✔️ Commit c10abf5: Looks fine!
  • ✔️ Commit 5598904: Looks fine!
  • ✔️ Commit 38e0f28: Looks fine!
  • ✔️ Commit 6f7f4b3: Looks fine!

@ojeda
Copy link
Member

ojeda commented May 26, 2021

Perhaps let's take the chance of the conflict to split this into smaller PRs.

The first commit can go in its own PR if others agree on having all procedural macros in a single crate. It would be best to put the pros/cons of that in the commit message.

@nbdd0121
Copy link
Member Author

I couldn't think of any pros for having each procedural macro in their own crate.

@ojeda
Copy link
Member

ojeda commented May 26, 2021

Hmm... faster re-compilation on changes? Perhaps compilation speed overall since it can be parallelized? (because otherwise we use codegen-units 1)

What about the pros of a single crate? Easier Makefile, I guess.

@bjorn3
Copy link
Member

bjorn3 commented May 26, 2021

Using -Ccodegen-units=1 for proc macros doesn't have any benefit. They are not performance critical. At most compiling them with optimizations enabled could improve compilation speed for users of the proc macro, but the couple of percent speedup that -Ccodegen-unit=1 gives is negligible compared to the rest of the time spent in rustc and the default local thin lto (thin lto with only cgus from the current crate participating rather than all object files) used when optimizations are enabled already gives most of the wins that using a single codegen unit would give. By the way is incremental compilation supported as option for development?

@ojeda
Copy link
Member

ojeda commented May 26, 2021

Using -Ccodegen-units=1 for proc macros doesn't have any benefit

Of course, but that is how the flags are setup at the moment; i.e. I was explaining why having split crates would increase compilation speed in our current setup (because if a reader does not know about -Ccodegen-units=1 being used, they would wonder otherwise).

@ojeda
Copy link
Member

ojeda commented May 26, 2021

To be clear: I am happy if we decide to split the flags for proc macros if we end up with a big macros crate.

This was referenced May 26, 2021
ojeda pushed a commit that referenced this pull request Feb 28, 2022
When both bpf_spin_lock and bpf_timer are present in a BPF map value,
copy_map_value needs to skirt both objects when copying a value into and
out of the map. However, the current code does not set both s_off and
t_off in copy_map_value, which leads to a crash when e.g. bpf_spin_lock
is placed in map value with bpf_timer, as bpf_map_update_elem call will
be able to overwrite the other timer object.

When the issue is not fixed, an overwriting can produce the following
splat:

[root@(none) bpf]# ./test_progs -t timer_crash
[   15.930339] bpf_testmod: loading out-of-tree module taints kernel.
[   16.037849] ==================================================================
[   16.038458] BUG: KASAN: user-memory-access in __pv_queued_spin_lock_slowpath+0x32b/0x520
[   16.038944] Write of size 8 at addr 0000000000043ec0 by task test_progs/325
[   16.039399]
[   16.039514] CPU: 0 PID: 325 Comm: test_progs Tainted: G           OE     5.16.0+ #278
[   16.039983] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS ArchLinux 1.15.0-1 04/01/2014
[   16.040485] Call Trace:
[   16.040645]  <TASK>
[   16.040805]  dump_stack_lvl+0x59/0x73
[   16.041069]  ? __pv_queued_spin_lock_slowpath+0x32b/0x520
[   16.041427]  kasan_report.cold+0x116/0x11b
[   16.041673]  ? __pv_queued_spin_lock_slowpath+0x32b/0x520
[   16.042040]  __pv_queued_spin_lock_slowpath+0x32b/0x520
[   16.042328]  ? memcpy+0x39/0x60
[   16.042552]  ? pv_hash+0xd0/0xd0
[   16.042785]  ? lockdep_hardirqs_off+0x95/0xd0
[   16.043079]  __bpf_spin_lock_irqsave+0xdf/0xf0
[   16.043366]  ? bpf_get_current_comm+0x50/0x50
[   16.043608]  ? jhash+0x11a/0x270
[   16.043848]  bpf_timer_cancel+0x34/0xe0
[   16.044119]  bpf_prog_c4ea1c0f7449940d_sys_enter+0x7c/0x81
[   16.044500]  bpf_trampoline_6442477838_0+0x36/0x1000
[   16.044836]  __x64_sys_nanosleep+0x5/0x140
[   16.045119]  do_syscall_64+0x59/0x80
[   16.045377]  ? lock_is_held_type+0xe4/0x140
[   16.045670]  ? irqentry_exit_to_user_mode+0xa/0x40
[   16.046001]  ? mark_held_locks+0x24/0x90
[   16.046287]  ? asm_exc_page_fault+0x1e/0x30
[   16.046569]  ? asm_exc_page_fault+0x8/0x30
[   16.046851]  ? lockdep_hardirqs_on+0x7e/0x100
[   16.047137]  entry_SYSCALL_64_after_hwframe+0x44/0xae
[   16.047405] RIP: 0033:0x7f9e4831718d
[   16.047602] Code: b4 0c 00 0f 05 eb a9 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d b3 6c 0c 00 f7 d8 64 89 01 48
[   16.048764] RSP: 002b:00007fff488086b8 EFLAGS: 00000206 ORIG_RAX: 0000000000000023
[   16.049275] RAX: ffffffffffffffda RBX: 00007f9e48683740 RCX: 00007f9e4831718d
[   16.049747] RDX: 0000000000000000 RSI: 0000000000000000 RDI: 00007fff488086d0
[   16.050225] RBP: 00007fff488086f0 R08: 00007fff488085d7 R09: 00007f9e4cb594a0
[   16.050648] R10: 0000000000000000 R11: 0000000000000206 R12: 00007f9e484cde30
[   16.051124] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
[   16.051608]  </TASK>
[   16.051762] ==================================================================

Fixes: 6813466 ("bpf: Add map side support for bpf timers.")
Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants