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

Use bindgen as a program rather than as a library #33

Merged
merged 1 commit into from
Nov 28, 2020
Merged

Conversation

ojeda
Copy link
Member

@ojeda ojeda commented Nov 28, 2020

This allows us to remove the kernel build.rs script
and simplifies the build. Cargo.lock is tiny now.
The shlex dependency also goes away.

Note that this commit does not use the list of included
functions/types/vars etc., so it is not equivalent.

Signed-off-by: Finn Behrens me@kloenk.de
Signed-off-by: Miguel Ojeda ojeda@kernel.org

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

This is Finn's from #23 split into only the bindgen part and a few extra cleanups.

We probably want to discuss a bit this one. @alex @geofft Any comments? I am not sure why you used the included types/functions/vars functionality in the build.rs script -- perhaps to minimize size or to avoid some warnings?

Finn also mentions in #32:

What's the direction we want to go with build.rs? I'm more a fan of removing it, as we only have to have bingen, and not some libclang path set.

It definitely looks simpler and it is also quicker to build. So unless there is a reason not to do it this way, I would go for it.

@ojeda ojeda mentioned this pull request Nov 28, 2020
ojeda pushed a commit that referenced this pull request Nov 28, 2020
When multiple adapters are present in the system, pci hot-removing second
adapter leads to the following warning as both the adapters registered
thermal zone device with same thermal zone name/type.
Therefore, use unique thermal zone name during thermal zone device
initialization. Also mark thermal zone dev NULL once unregistered.

[  414.370143] ------------[ cut here ]------------
[  414.370944] sysfs group 'power' not found for kobject 'hwmon0'
[  414.371747] WARNING: CPU: 9 PID: 2661 at fs/sysfs/group.c:281
 sysfs_remove_group+0x76/0x80
[  414.382550] CPU: 9 PID: 2661 Comm: bash Not tainted 5.8.0-rc6+ #33
[  414.383593] Hardware name: Supermicro X10SRA-F/X10SRA-F, BIOS 2.0a 06/23/2016
[  414.384669] RIP: 0010:sysfs_remove_group+0x76/0x80
[  414.385738] Code: 48 89 df 5b 5d 41 5c e9 d8 b5 ff ff 48 89 df e8 60 b0 ff ff
 eb cb 49 8b 14 24 48 8b 75 00 48 c7 c7 90 ae 13 bb e8 6a 27 d0 ff <0f> 0b 5b 5d
 41 5c c3 0f 1f 00 0f 1f 44 00 00 48 85 f6 74 31 41 54
[  414.388404] RSP: 0018:ffffa22bc080fcb0 EFLAGS: 00010286
[  414.389638] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000
[  414.390829] RDX: 0000000000000001 RSI: ffff8ee2de3e9510 RDI: ffff8ee2de3e9510
[  414.392064] RBP: ffffffffbaef2ee0 R08: 0000000000000000 R09: 0000000000000000
[  414.393224] R10: 0000000000000000 R11: 000000002b30006c R12: ffff8ee260720008
[  414.394388] R13: ffff8ee25e0a40e8 R14: ffffa22bc080ff08 R15: ffff8ee2c3be5020
[  414.395661] FS:  00007fd2a7171740(0000) GS:ffff8ee2de200000(0000)
 knlGS:0000000000000000
[  414.396825] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  414.398011] CR2: 00007f178ffe5020 CR3: 000000084c5cc003 CR4: 00000000003606e0
[  414.399172] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[  414.400352] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[  414.401473] Call Trace:
[  414.402685]  device_del+0x89/0x400
[  414.403819]  device_unregister+0x16/0x60
[  414.405024]  hwmon_device_unregister+0x44/0xa0
[  414.406112]  thermal_remove_hwmon_sysfs+0x196/0x200
[  414.407256]  thermal_zone_device_unregister+0x1b5/0x1f0
[  414.408415]  cxgb4_thermal_remove+0x3c/0x4f [cxgb4]
[  414.409668]  remove_one+0x212/0x290 [cxgb4]
[  414.410875]  pci_device_remove+0x36/0xb0
[  414.412004]  device_release_driver_internal+0xe2/0x1c0
[  414.413276]  pci_stop_bus_device+0x64/0x90
[  414.414433]  pci_stop_and_remove_bus_device_locked+0x16/0x30
[  414.415609]  remove_store+0x75/0x90
[  414.416790]  kernfs_fop_write+0x114/0x1b0
[  414.417930]  vfs_write+0xcf/0x210
[  414.419059]  ksys_write+0xa7/0xe0
[  414.420120]  do_syscall_64+0x4c/0xa0
[  414.421278]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[  414.422335] RIP: 0033:0x7fd2a686afd0
[  414.423396] Code: Bad RIP value.
[  414.424549] RSP: 002b:00007fffc1446148 EFLAGS: 00000246 ORIG_RAX:
 0000000000000001
[  414.425638] RAX: ffffffffffffffda RBX: 0000000000000002 RCX: 00007fd2a686afd0
[  414.426830] RDX: 0000000000000002 RSI: 00007fd2a7196000 RDI: 0000000000000001
[  414.427927] RBP: 00007fd2a7196000 R08: 000000000000000a R09: 00007fd2a7171740
[  414.428923] R10: 00007fd2a7171740 R11: 0000000000000246 R12: 00007fd2a6b43400
[  414.430082] R13: 0000000000000002 R14: 0000000000000001 R15: 0000000000000000
[  414.431027] irq event stamp: 76300
[  414.435678] ---[ end trace 13865acb4d5ab00f ]---

Fixes: b187191 ("cxgb4: Add thermal zone support")
Signed-off-by: Potnuri Bharat Teja <bharat@chelsio.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
@alex
Copy link
Member

alex commented Nov 28, 2020

The reason we had the explicit list was because there were some types/functions that could not be generated safely (e.g. due to large arrays that Rust didn't impl some traits for) -- perhaps all those issues are fixed now.

@kloenk
Copy link
Member

kloenk commented Nov 28, 2020

Bindgen worked and compiled for me, just had the problem with builtin. That did not have to come from bindgen

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

It worked for me too -- there is a single warning, though:

warning: `#[derive]` can't be used on a `#[repr(packed)]` struct that does not derive Copy (error E0133)
     --> include/generated/rust_bindings.rs:17212:10
      |
17212 | #[derive(Debug, Default)]
      |          ^^^^^
      |
      = note: `#[warn(safe_packed_borrows)]` on by default
      = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
      = note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
      = note: this warning originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

Perhaps we can exclude rather than include?

        --blacklist-function <function>...         Mark <function> as hidden.
        --blacklist-item <item>...                 Mark <item> as hidden.
        --blacklist-type <type>...                 Mark <type> as hidden.

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

The CI needs a small change to avoid this warning:

error: 'rustfmt' is not installed for the toolchain 'nightly-2020-08-27-x86_64-unknown-linux-gnu'
To install, run `rustup component add rustfmt`
Failed to run rustfmt: Internal rustfmt error (non-fatal, continuing)

In the docs, as well, mentioning bindgen is required.

@kloenk
Copy link
Member

kloenk commented Nov 28, 2020

I think I disabled rustfmt for the bindings in my pr. Not sure which flag it was though

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

I think I disabled rustfmt for the bindings in my pr. Not sure which flag it was though

--no-rustfmt-bindings; I removed it to match better the original settings in build.rs.

I think it is worth to have rustfmt. We could avoid the dependency if it was a third-party tool, but since it comes with the compiler (in the standalone installers), I think it is fair to require it.

I will update the CI and run it again.

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

Nowadays the default profile for rustup includes rustfmt, though, so I am not sure why the CI complains. Perhaps the image uses the minimal one...

@kloenk
Copy link
Member

kloenk commented Nov 28, 2020

I'm strongly against rustup. I don't see a reason to fmt it, as it's not code for user api. So I would not fmt it. So it does not break in a context without rustup, rustfmt.

@kloenk
Copy link
Member

kloenk commented Nov 28, 2020

I think it is worth to have rustfmt. We could avoid the dependency if it was a third-party tool, but since it comes with the compiler (in the standalone installers), I think it is fair to require it.

It does not come if you don't use rustup. I see the usecase of nix, where I have to figure out how to create rust-src without rustup, and that I will only put rustc and cargo into the sandbox, so there would not be a rustfmt in some build envs.

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

I'm strongly against rustup. I don't see a reason to fmt it, as it's not code for user api. So I would not fmt it. So it does not break in a context without rustup, rustfmt.

(Did you mean against rustfmt?) Note that we don't require rustup -- it is just in the CI here that we use it. In the quick start guide I tell people that they can use either the standalone installers or rustup (or, of course, their own distro packages -- we should mention that too).

As for formatting the bindings or not, I think it is useful to have the file formatted, specially since people will need to take a look from time to time. On its own, rustfmt may be useful later too if we decide to tell people to use it (I am trying to get more people to use clang-format for C in the kernel, and it is not easy... :-)

In any case, rustfmt not being found by bindgen does not fail the build even if it is not there (as you see in the CI above).

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

I see the usecase of nix, where I have to figure out how to create rust-src without rustup, and that I will only put rustc and cargo into the sandbox, so there would not be a rustfmt in some build envs.

Hmm... do you think it is that hard to get in in those environments? I mean, if one can build Rust, one can enable it during build, so most distros/package managers should offer it at some point, no?

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

Force-pushed changes:

  • Mentioned bindgen in the quick start guide.
  • Fixed the rustfmt bit in the CI.
  • Removed last mention of shlex removed in Cargo.lock. For some reason, Cargo was not removing it, but if you let it re-create the file from scratch, it goes away as expected.

The only remaining thing is the warning. I think it is OK to leave it like that for the moment -- hopefully it will resolve itself since other people seem to have hit the same issue with bindgen. If not, we can exclude the offending entity later on.

So this is ready to go.

@kloenk
Copy link
Member

kloenk commented Nov 28, 2020

I see the usecase of nix, where I have to figure out how to create rust-src without rustup, and that I will only put rustc and cargo into the sandbox, so there would not be a rustfmt in some build envs.

Hmm... do you think it is that hard to get in in those environments? I mean, if one can build Rust, one can enable it during build, so most distros/package managers should offer it at some point, no?

You can enable it in the env. But it's one dependency more you have to download. And it makes the closure bigger. That's why I would like to keep rustfmt optional.

Copy link
Member

@kloenk kloenk left a comment

Choose a reason for hiding this comment

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

LGTM, only the rustfmt thing :-)

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

You can enable it in the env. But it's one dependency more you have to download. And it makes the closure bigger. That's why I would like to keep rustfmt optional.

I see, makes sense. To avoid the benign error message, we could detect if it is installed or not, and use the flag accordingly. Do you want to give it a go?

@kloenk
Copy link
Member

kloenk commented Nov 28, 2020

I see, makes sense. To avoid the benign error message, we could detect if it is installed or not, and use the flag accordingly. Do you want to give it a go?

Sounds good. If you can implement that, great.

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

I just realized that, for some reason, bindgen does not need to be installed in the CI (?). Howevers, the docs should explain how to install it. Let me do that...

@kloenk
Copy link
Member

kloenk commented Nov 28, 2020

I just realized that, for some reason, bindgen does not need to be installed in the CI (?). Howevers, the docs should explain how to install it. Let me do that...

Maybe it was still in the cache? I think I installed it with cargo install bindgen, but not sure. I only know that I cannot use the bindgen from nixpkgs, as it still uses clang 9 xD

@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

Maybe it was still in the cache? I think I installed it with cargo install bindgen, but not sure. I only know that I cannot use the bindgen from nixpkgs, as it still uses clang 9 xD

The CI should be spinning up a new system every time from the image, otherwise it would be non-deterministic, so I don't know. Anyway, the CI is not important as long as it works :-)

This allows us to remove the `kernel` `build.rs` script
and simplifies the build. `Cargo.lock` is tiny now.
The shlex dependency also goes away.

Note that this commit does *not* use the list of included
functions/types/vars etc., so it is not equivalent.

Signed-off-by: Finn Behrens <me@kloenk.de>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
@ojeda
Copy link
Member Author

ojeda commented Nov 28, 2020

OK, I rewrote a bit the quick start -- now the different tools are split into several sections.

@ojeda ojeda merged commit 62d4aaa into rust Nov 28, 2020
@ojeda ojeda deleted the rust-bindgen branch November 28, 2020 19:49
ojeda pushed a commit that referenced this pull request May 4, 2022
The kmemleak_*_phys() apis do not check the address for lowmem's min
boundary, while the caller may pass an address below lowmem, which will
trigger an oops:

  # echo scan > /sys/kernel/debug/kmemleak
  Unable to handle kernel paging request at virtual address ff5fffffffe00000
  Oops [#1]
  Modules linked in:
  CPU: 2 PID: 134 Comm: bash Not tainted 5.18.0-rc1-next-20220407 #33
  Hardware name: riscv-virtio,qemu (DT)
  epc : scan_block+0x74/0x15c
   ra : scan_block+0x72/0x15c
  epc : ffffffff801e5806 ra : ffffffff801e5804 sp : ff200000104abc30
   gp : ffffffff815cd4e8 tp : ff60000004cfa340 t0 : 0000000000000200
   t1 : 00aaaaaac23954cc t2 : 00000000000003ff s0 : ff200000104abc90
   s1 : ffffffff81b0ff28 a0 : 0000000000000000 a1 : ff5fffffffe01000
   a2 : ffffffff81b0ff28 a3 : 0000000000000002 a4 : 0000000000000001
   a5 : 0000000000000000 a6 : ff200000104abd7c a7 : 0000000000000005
   s2 : ff5fffffffe00ff9 s3 : ffffffff815cd998 s4 : ffffffff815d0e90
   s5 : ffffffff81b0ff28 s6 : 0000000000000020 s7 : ffffffff815d0eb0
   s8 : ffffffffffffffff s9 : ff5fffffffe00000 s10: ff5fffffffe01000
   s11: 0000000000000022 t3 : 00ffffffaa17db4c t4 : 000000000000000f
   t5 : 0000000000000001 t6 : 0000000000000000
  status: 0000000000000100 badaddr: ff5fffffffe00000 cause: 000000000000000d
    scan_gray_list+0x12e/0x1a6
    kmemleak_scan+0x2aa/0x57e
    kmemleak_write+0x32a/0x40c
    full_proxy_write+0x56/0x82
    vfs_write+0xa6/0x2a6
    ksys_write+0x6c/0xe2
    sys_write+0x22/0x2a
    ret_from_syscall+0x0/0x2

The callers may not quite know the actual address they pass(e.g. from
devicetree).  So the kmemleak_*_phys() apis should guarantee the address
they finally use is in lowmem range, so check the address for lowmem's
min boundary.

Link: https://lkml.kernel.org/r/20220413122925.33856-1-patrick.wang.shcn@gmail.com
Signed-off-by: Patrick Wang <patrick.wang.shcn@gmail.com>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
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