rust: Fix build error
Subsystems (2)
PATCH 1/1 rust: Fix build error
When CONFIG_DEBUG_SPINLOCK=y and CONFIG_PREEMPT_RT=y the following build error occurs:
In file included from rust/helpers/helpers.c:22:
rust/helpers/spinlock.c: In function ‘rust_helper___spin_lock_init’:
rust/helpers/spinlock.c:10:30: error: implicit declaration of function ‘spinlock_check’; did you mean ‘spin_lock_bh’? [-Wimplicit-function-declaration]
10 | __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
| ^~~~~~~~~~~~~~
| spin_lock_bh
rust/helpers/spinlock.c:10:30: error: passing argument 1 of ‘__raw_spin_lock_init’ makes pointer from integer without a cast [-Wint-conversion]
10 | __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
| ^~~~~~~~~~~~~~~~~~~~
| |
| int
In file included from ./include/linux/wait.h:9,
from ./include/linux/wait_bit.h:8,
from ./include/linux/fs.h:6,
from ./include/linux/highmem.h:5,
from ./include/linux/bvec.h:10,
from ./include/linux/blk_types.h:10,
from ./include/linux/blkdev.h:9,
from ./include/linux/blk-mq.h:5,
from rust/helpers/blk.c:3,
from rust/helpers/helpers.c:10:
./include/linux/spinlock.h:101:52: note: expected ‘raw_spinlock_t *’ {aka ‘struct raw_spinlock *’} but argument is of type ‘int’
101 | extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
| ~~~~~~~~~~~~~~~~^~~~
make[2]: *** [scripts/Makefile.build:229: rust/helpers/helpers.o] Error 1Error observed while building a rt-debug kernel for aarch64.
On a PREEMPT_RT build, spin locks have been mapped to rt_mutex types, so avoid the raw_spinlock_init call for RT.
--- rust/helpers/spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/helpers/spinlock.c +1 -1
@@ -6,7 +6,7 @@void rust_helper___spin_lock_init (spinlock_t *lock,const char *name,struct lock_class_key *key) { -#ifdef CONFIG_DEBUG_SPINLOCK +#if defined(CONFIG_DEBUG_SPINLOCK ) && !defined(CONFIG_PREEMPT_RT )__raw_spin_lock_init (spinlock_check (lock), name, key,LD_WAIT_CONFIG );#else spin_lock_init (lock);
Signature · 1 line
2.46.2
Hi Eder,
Thanks for the patch!
Quoted 29 lines · Eder Zulian
When
CONFIG_DEBUG_SPINLOCK=yandCONFIG_PREEMPT_RT=ythe following build error occurs:In file included from rust/helpers/helpers.c:22: rust/helpers/spinlock.c: In function ‘rust_helper___spin_lock_init’: rust/helpers/spinlock.c:10:30: error: implicit declaration of function ‘spinlock_check’; did you mean ‘spin_lock_bh’? [-Wimplicit-function-declaration] 10 | __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG); | ^~~~~~~~~~~~~~ | spin_lock_bh rust/helpers/spinlock.c:10:30: error: passing argument 1 of ‘__raw_spin_lock_init’ makes pointer from integer without a cast [-Wint-conversion] 10 | __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG); | ^~~~~~~~~~~~~~~~~~~~ | | | int In file included from ./include/linux/wait.h:9, from ./include/linux/wait_bit.h:8, from ./include/linux/fs.h:6, from ./include/linux/highmem.h:5, from ./include/linux/bvec.h:10, from ./include/linux/blk_types.h:10, from ./include/linux/blkdev.h:9, from ./include/linux/blk-mq.h:5, from rust/helpers/blk.c:3, from rust/helpers/helpers.c:10: ./include/linux/spinlock.h:101:52: note: expected ‘raw_spinlock_t *’ {aka ‘struct raw_spinlock *’} but argument is of type ‘int’ 101 | extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name, | ~~~~~~~~~~~~~~~~^~~~ make[2]: *** [scripts/Makefile.build:229: rust/helpers/helpers.o] Error 1
Quoted 5 lines
Error observed while building a rt-debug kernel for aarch64.
On a
PREEMPT_RTbuild, spin locks have been mapped tort_mutextypes, so avoid theraw_spinlock_initcall for RT.
This is true, but to keep lockdep working I think we need to open code the PREEMPT_RT version of spin_lock_init(), please see below
Quoted 2 lines
Suggested-by Clark Williams <[email protected]>
^ unnecessary emtpy line here.
Quoted 10 lines
Signed-off-by Eder Zulian <[email protected]>--- rust/helpers/spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)rust/helpers/spinlock.c +0
-0
rust/helpers/spinlock.c line 12
Quoted 8 lines
rust/helpers/spinlock.c +1
-1@@ -6,7 +6,7 @@void rust_helper___spin_lock_init (spinlock_t *lock,const char *name,struct lock_class_key *key) { -#ifdef CONFIG_DEBUG_SPINLOCK +#if defined(CONFIG_DEBUG_SPINLOCK ) && !defined(CONFIG_PREEMPT_RT )__raw_spin_lock_init (spinlock_check (lock), name, key,LD_WAIT_CONFIG );#else spin_lock_init (lock);
This should be:
void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
struct lock_class_key *key)
{
#ifdef CONFIG_DEBUG_SPINLOCK
# if defined(CONFIG_PREEMPT_RT) rt_mutex_base_init(&(lock)->lock); __rt_spin_lock_init(lock, name, key, false); # else /* !CONFIG_PREEMPT_RT */ __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG); # endif /* CONFIG_PREEMPT_RT */ #else spin_lock_init(lock); #endif }
Regards,
Boqun
Quoted 4 lines
Signature · 1 line
2.46.2
Hi Boqun,
Quoted 3 lines · Boqun Feng
Hi Eder,
Thanks for the patch!
Sure, my pleasure.
Quoted 37 lines · Eder Zulian
Quoted 29 lines
When
CONFIG_DEBUG_SPINLOCK=yandCONFIG_PREEMPT_RT=ythe following build error occurs:In file included from rust/helpers/helpers.c:22: rust/helpers/spinlock.c: In function ‘rust_helper___spin_lock_init’: rust/helpers/spinlock.c:10:30: error: implicit declaration of function ‘spinlock_check’; did you mean ‘spin_lock_bh’? [-Wimplicit-function-declaration] 10 | __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG); | ^~~~~~~~~~~~~~ | spin_lock_bh rust/helpers/spinlock.c:10:30: error: passing argument 1 of ‘__raw_spin_lock_init’ makes pointer from integer without a cast [-Wint-conversion] 10 | __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG); | ^~~~~~~~~~~~~~~~~~~~ | | | int In file included from ./include/linux/wait.h:9, from ./include/linux/wait_bit.h:8, from ./include/linux/fs.h:6, from ./include/linux/highmem.h:5, from ./include/linux/bvec.h:10, from ./include/linux/blk_types.h:10, from ./include/linux/blkdev.h:9, from ./include/linux/blk-mq.h:5, from rust/helpers/blk.c:3, from rust/helpers/helpers.c:10: ./include/linux/spinlock.h:101:52: note: expected ‘raw_spinlock_t *’ {aka ‘struct raw_spinlock *’} but argument is of type ‘int’ 101 | extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name, | ~~~~~~~~~~~~~~~~^~~~ make[2]: *** [scripts/Makefile.build:229: rust/helpers/helpers.o] Error 1Quoted 5 lines
Error observed while building a rt-debug kernel for aarch64.
On a
PREEMPT_RTbuild, spin locks have been mapped tort_mutextypes, so avoid theraw_spinlock_initcall for RT.
Quoted 7 lines
This is true, but to keep lockdep working I think we need to open code the
PREEMPT_RTversion ofspin_lock_init(), please see belowQuoted 2 lines
Suggested-by Clark Williams <[email protected]>^ unnecessary emtpy line here.
Thanks for pointing it out. Should I fix it and send a v2?
Quoted 22 lines
Quoted 11 lines
Signed-off-by Eder Zulian <[email protected]>--- rust/helpers/spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)rust/helpers/spinlock.c +0
-0rust/helpers/spinlock.c line 12
Quoted 8 lines
rust/helpers/spinlock.c +1
-1@@ -6,7 +6,7 @@void rust_helper___spin_lock_init (spinlock_t *lock,const char *name,struct lock_class_key *key) { -#ifdef CONFIG_DEBUG_SPINLOCK +#if defined(CONFIG_DEBUG_SPINLOCK ) && !defined(CONFIG_PREEMPT_RT )__raw_spin_lock_init (spinlock_check (lock), name, key,LD_WAIT_CONFIG );#else spin_lock_init (lock);This should be:
Quoted 5 lines
void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
structlock_class_key*key)
{
#ifdefCONFIG_DEBUG_SPINLOCK# if defined(CONFIG_PREEMPT_RT)
Even though I don't have a strong preference on this, in my opinion, there is no much difference here just a line break and inverted logic. Perhaps it would be better to write like this: 'if (IS_ENABLED(CONFIG_PREEMPT_RT && ... ) { this } else { that }', however, spinlock_check() is not declared for PREEMPT_RT kernels because its declaration is conditional to #ifndef CONFIG_PREEMPT_RT in 'spinlock.h'. spinlock_check() as is does not make sense for PREEMPT_RT because spinlock is not mapped to raw_spinlock but to rt_mutex (cf. 'spinlock_types.h')
Quoted 2 lines
rt_mutex_base_init(&(lock)->lock); __rt_spin_lock_init(lock, name, key, false);
Apparently, this ^ matches spin_lock_init() in 'spinlock_rt.h', if true, we could call spin_lock_init() for PREEMPT_RT kernels. However the debug version of __rt_spin_lock_init() (instrumented with lockdep in 'spinlock_rt.c') depends on CONFIG_DEBUG_LOCK_ALLOC. Luckily, in my opnion, the #ifdefs are already in place.
Quoted 8 lines
# else /* !CONFIG_PREEMPT_RT */ __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG); # endif /* CONFIG_PREEMPT_RT */ #else spin_lock_init(lock); #endif }
Please let me know what you think, and bear with me if I'm missing something. Thanks.
Quoted 8 lines
Regards,
BoqunQuoted 4 lines
Signature · 1 line
2.46.2
Quoted 2 lines · Eder Zulian
Error observed while building a rt-debug kernel for aarch64.
Thanks for testing with Rust enabled!
Quoted 1 line
Suggested-by Clark Williams <[email protected]>
Do you mean Reported-by?
Also, I am not sure which Fixes: tag would fit best here, since PREEMPT_RT has been around for quite a while, but only enabled very recently. Thomas: do you have a preference?
In addition (sorry, it was in my backlog):
Finally, I think we should perhaps put a helper in spinlock{_,rt}.h that takes the key (instead of having this #ifdef here) and then just use that from the Rust helpers, because we don't want to duplicate such logic (conditionals) in helpers. And with the RT init open coding that Boqun mentioned, even more. After all, helpers are meant to be as straightforward as possible, and if we have this sort of thing in helpers, it is harder for everyone to keep them in sync.
In other words, I see helpers as following the same "avoid #ifdefs" rule that we prefer in C source files vs. headers.
What do you think, Thomas?
Quoted 1 line
Spurious newline.
Cheers,
Miguel
Quoted 19 lines · Miguel Ojeda
Quoted 2 lines · Eder Zulian
Error observed while building a rt-debug kernel for aarch64.
Thanks for testing with Rust enabled!
Quoted 1 line
Suggested-by Clark Williams <[email protected]>Do you mean
Reported-by?Also, I am not sure which
Fixes:tag would fit best here, sincePREEMPT_RThas been around for quite a while, but only enabled very recently. Thomas: do you have a preference?In addition (sorry, it was in my backlog):
Quoted 8 lines
Finally, I think we should perhaps put a helper in
spinlock{_,rt}.hthat takes thekey(instead of having this#ifdefhere) and then just use that from the Rust helpers, because we don't want to duplicate such logic (conditionals) in helpers. And with the RT init open coding that Boqun mentioned, even more. After all, helpers are meant to be as straightforward as possible, and if we have this sort of thing in helpers, it is harder for everyone to keep them in sync.
Make sense, and we did have something for !PREEMPT_RT spinlock:
https://lore.kernel.org/rust-for-linux/[email protected]/
and we can do the same thing for PREEMPT_RT:
(untested code)
include/linux/spinlock_rt.h +18 -4
@@ -16,20 +16,34 @@ static inline void __rt_spin_lock_init(spinlock_t *lock, const char *name, }#endif +static inline void __spin_lock_init_with_key (spinlock_t *lock, +const char *name, +struct lock_class_key *key, +bool percpu) +{ +rt_mutex_base_init (&lock->lock); +__rt_spin_lock_init (slock, name, key, percpu); +} + +static inline void spin_lock_init_with_key (spinlock_t *lock, +const char *name, +struct lock_class_key *key) +{ +__spin_lock_init_with_key (lock, name, key, false); +} +#define spin_lock_init (slock) \ do { \ static struct lock_class_key __key; \ \ - rt_mutex_base_init(&(slock)->lock); \ - __rt_spin_lock_init(slock, #slock, &__key, false); \ + spin_lock_init_with_key(slock, #slock, &__key); \ } while (0)#define local_spin_lock_init (slock) \ do { \ static struct lock_class_key __key; \ \ - rt_mutex_base_init(&(slock)->lock); \ - __rt_spin_lock_init(slock, #slock, &__key, true); \ + __spin_lock_init_with_key(slock, #slock, &__key, true); \ } while (0)extern void rt_spin_lock (spinlock_t *lock);
Quoted 5 lines
In other words, I see helpers as following the same "avoid
#ifdefs" rule that we prefer in C source files vs. headers.What do you think, Thomas?
Thanks for copying Thomas, my reply to Eder is here:
https://lore.kernel.org/rust-for-linux/Zw1_rXUyjTBOh0QH@boqun-archlinux/
Regards,
Boqun
Quoted 6 lines
Quoted 1 line
Spurious newline.
Cheers,
Miguel
Hi Miguel,
Quoted 5 lines · Miguel Ojeda
Quoted 2 lines · Eder Zulian
Error observed while building a rt-debug kernel for aarch64.
Thanks for testing with Rust enabled!
Sure, it's been fun!
Quoted 4 lines
Quoted 2 lines
Suggested-by Clark Williams <[email protected]>Do you mean
Reported-by?
Yes, my mistake.
Quoted 5 lines
Also, I am not sure which
Fixes:tag would fit best here, sincePREEMPT_RThas been around for quite a while, but only enabled very recently. Thomas: do you have a preference?
I can try to find a culprit and add a 'Fixes:' tag. In my opnion, at first glance, it would be the patch that introduced the Rust helper for spinlocks. Not sure.
Quoted 5 lines
In addition (sorry, it was in my backlog):
I can fix it and send a v2 if that's ok. Is it valid to add two 'Reported-by' tags (Clark and kernel test robot)?
Quoted 4 lines
Finally, I think we should perhaps put a helper in
spinlock{_,rt}.hthat takes thekey(instead of having this#ifdefhere) and then just use that from the Rust helpers, because we don't want to duplicate such logic (conditionals) in helpers. And with the RT init
Agreed. We don't want code replicated. In my reply to Boqun I added some notes. If that makes sense, we could avoid even the helper in 'spinlock{_,rt}.h'?
Quoted 3 lines
open coding that Boqun mentioned, even more. After all, helpers are meant to be as straightforward as possible, and if we have this sort of thing in helpers, it is harder for everyone to keep them in sync.
Please correct me if I misunderstood. It seems that Rust doesn't have a pre-processor step to replace macros in the code and the Rust compiler works with 'objects/entities' created for functions and variables, but macros would be ignored (since they are string substitution.) Do you have pointers for good docs on this?
Quoted 9 lines
In other words, I see helpers as following the same "avoid
#ifdefs" rule that we prefer in C source files vs. headers.What do you think, Thomas?
Quoted 1 line
Spurious newline.
Thanks, I'll fix the spurious new line.
Quoted 1 line
Thank you.
Quoted 3 lines
Cheers,
Miguel
Quoted 3 lines · Eder Zulian
I can fix it and send a v2 if that's ok. Is it valid to add two 'Reported-by' tags (Clark and kernel test robot)?
Yeah, I think so, at least if they were independently reported. Please add a Closes if you have it for Clark's report.
Quoted 3 lines
Agreed. We don't want code replicated. In my reply to Boqun I added some notes. If that makes sense, we could avoid even the helper in 'spinlock{_,rt}.h'?
Hmm... I am not sure I follow your reply to Boqun. In your version, under DEBUG_SPINLOCK && PREEMPT_RT, you call spin_lock_init, but that means we are not passing the given key but creating a new static/single one, no? That is why Boqun mentioned that.
Quoted 5 lines
Please correct me if I misunderstood. It seems that Rust doesn't have a pre-processor step to replace macros in the code and the Rust compiler works with 'objects/entities' created for functions and variables, but macros would be ignored (since they are string substitution.) Do you have pointers for good docs on this?
I am not sure what exactly you are referring to, so perhaps this quick summary helps (apologies if you already know all this!).
Rust does not understand C, at all. So we use bindgen, which is a tool that internally uses libclang to parse C headers and emit Rust code to use them from Rust. Clang (of course) knows about macros and can parse them and expand them etc., but those macros (typically) expand into C code, not Rust code. So (typically) we can't simply use the macro because it does not generate valid Rust code. Thus we use a C source file to declare helpers that call the C macros (which is fine because it is a C file compiled by a C compiler), and then we can call the C helper function from Rust.
The problem Boqun pointed out is that, now, since you introduced an extra condition in the same #ifdef, then spin_lock_init is also called in a case when DEBUG_SPINLOCK=y, which means the key that was passed as a parameter is not used but the macro will provide a new one.
Does that help?
As for docs, I am happy to point to some -- do you mean on the Rust side of things?
Thanks for the patch!
Cheers,
Miguel
Hi,
Really sorry for the long pause, I was OOO for some days...
Quoted 8 lines · Miguel Ojeda
Quoted 3 lines · Eder Zulian
I can fix it and send a v2 if that's ok. Is it valid to add two 'Reported-by' tags (Clark and kernel test robot)?
Yeah, I think so, at least if they were independently reported. Please add a
Closesif you have it for Clark's report.
Quoted 9 lines
Quoted 3 lines
Agreed. We don't want code replicated. In my reply to Boqun I added some notes. If that makes sense, we could avoid even the helper in 'spinlock{_,rt}.h'?
Hmm... I am not sure I follow your reply to Boqun. In your version, under
DEBUG_SPINLOCK && PREEMPT_RT, you callspin_lock_init, but that means we are not passing the given key but creating a new static/single one, no? That is why Boqun mentioned that.
I shall send v2 based on Boqun's suggestion soon.
Quoted 19 lines
Quoted 5 lines
Please correct me if I misunderstood. It seems that Rust doesn't have a pre-processor step to replace macros in the code and the Rust compiler works with 'objects/entities' created for functions and variables, but macros would be ignored (since they are string substitution.) Do you have pointers for good docs on this?
I am not sure what exactly you are referring to, so perhaps this quick summary helps (apologies if you already know all this!).
Rust does not understand C, at all. So we use
bindgen, which is a tool that internally useslibclangto parse C headers and emit Rust code to use them from Rust. Clang (of course) knows about macros and can parse them and expand them etc., but those macros (typically) expand into C code, not Rust code. So (typically) we can't simply use the macro because it does not generate valid Rust code. Thus we use a C source file to declare helpers that call the C macros (which is fine because it is a C file compiled by a C compiler), and then we can call the C helper function from Rust.
Quoted 7 lines
The problem Boqun pointed out is that, now, since you introduced an extra condition in the same
#ifdef, thenspin_lock_initis also called in a case whenDEBUG_SPINLOCK=y, which means the key that was passed as a parameter is not used but the macro will provide a new one.Does that help?
Yes, thank you!
Quoted 9 lines
As for docs, I am happy to point to some -- do you mean on the Rust side of things?
Thanks for the patch!
Cheers,
Miguel
Cheeers,
Eder