Skip to content

Commit 6a06987

Browse files
DarksonnYury Norov
authored andcommitted
rust: bitops: fix missing _find_* functions on 32-bit ARM
On 32-bit ARM, you may encounter linker errors such as this one: ld.lld: error: undefined symbol: _find_next_zero_bit >>> referenced by rust_binder_main.43196037ba7bcee1-cgu.0 >>> drivers/android/binder/rust_binder_main.o:(<rust_binder_main::process::Process>::insert_or_update_handle) in archive vmlinux.a >>> referenced by rust_binder_main.43196037ba7bcee1-cgu.0 >>> drivers/android/binder/rust_binder_main.o:(<rust_binder_main::process::Process>::insert_or_update_handle) in archive vmlinux.a This error occurs because even though the functions are declared by include/linux/find.h, the definition is #ifdef'd out on 32-bit ARM. This is because arch/arm/include/asm/bitops.h contains: #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz) #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off) #define find_first_bit(p,sz) _find_first_bit_le(p,sz) #define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off) And the underscore-prefixed function is conditional on #ifndef of the non-underscore-prefixed name, but the declaration in find.h is *not* conditional on that #ifndef. To fix the linker error, we ensure that the symbols in question exist when compiling Rust code. We do this by defining them in rust/helpers/ whenever the normal definition is #ifndef'd out. Note that these helpers are somewhat unusual in that they do not have the rust_helper_ prefix that most helpers have. Adding the rust_helper_ prefix does not compile, as 'bindings::_find_next_zero_bit()' will result in a call to a symbol called _find_next_zero_bit as defined by include/linux/find.h rather than a symbol with the rust_helper_ prefix. This is because when a symbol is present in both include/ and rust/helpers/, the one from include/ wins under the assumption that the current configuration is one where that helper is unnecessary. This heuristic fails for _find_next_zero_bit() because the header file always declares it even if the symbol does not exist. The functions still use the __rust_helper annotation. This lets the wrapper function be inlined into Rust code even if full kernel LTO is not used once the patch series for that feature lands. Yury: arches are free to implement they own find_bit() functions. Most rely on generic implementation, but arm32 and m86k - not; so they require custom handling. Alice confirmed it fixes the build for both. Cc: [email protected] Fixes: 6cf93a9 ("rust: add bindings for bitops.h") Reported-by: Andreas Hindborg <[email protected]> Closes: https://rust-for-linux.zulipchat.com/#narrow/channel/x/topic/x/near/561677301 Tested-by: Andreas Hindborg <[email protected]> Reviewed-by: Dirk Behme <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Signed-off-by: Yury Norov (NVIDIA) <[email protected]>
1 parent b543459 commit 6a06987

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

rust/helpers/bitops.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#include <linux/bitops.h>
4+
#include <linux/find.h>
45

56
void rust_helper___set_bit(unsigned long nr, unsigned long *addr)
67
{
@@ -21,3 +22,44 @@ void rust_helper_clear_bit(unsigned long nr, volatile unsigned long *addr)
2122
{
2223
clear_bit(nr, addr);
2324
}
25+
26+
/*
27+
* The rust_helper_ prefix is intentionally omitted below so that the
28+
* declarations in include/linux/find.h are compatible with these helpers.
29+
*
30+
* Note that the below #ifdefs mean that the helper is only created if C does
31+
* not provide a definition.
32+
*/
33+
#ifdef find_first_zero_bit
34+
__rust_helper
35+
unsigned long _find_first_zero_bit(const unsigned long *p, unsigned long size)
36+
{
37+
return find_first_zero_bit(p, size);
38+
}
39+
#endif /* find_first_zero_bit */
40+
41+
#ifdef find_next_zero_bit
42+
__rust_helper
43+
unsigned long _find_next_zero_bit(const unsigned long *addr,
44+
unsigned long size, unsigned long offset)
45+
{
46+
return find_next_zero_bit(addr, size, offset);
47+
}
48+
#endif /* find_next_zero_bit */
49+
50+
#ifdef find_first_bit
51+
__rust_helper
52+
unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
53+
{
54+
return find_first_bit(addr, size);
55+
}
56+
#endif /* find_first_bit */
57+
58+
#ifdef find_next_bit
59+
__rust_helper
60+
unsigned long _find_next_bit(const unsigned long *addr, unsigned long size,
61+
unsigned long offset)
62+
{
63+
return find_next_bit(addr, size, offset);
64+
}
65+
#endif /* find_next_bit */

0 commit comments

Comments
 (0)