linux-yocto/rust
Alice Ryhl 6711f72301 rust: bitops: fix missing _find_* functions on 32-bit ARM
commit 6a069876eb1402478900ee0eb7d7fe276bb1f4e3 upstream.

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: stable@vger.kernel.org
Fixes: 6cf93a9ed3 ("rust: add bindings for bitops.h")
Reported-by: Andreas Hindborg <a.hindborg@kernel.org>
Closes: https://rust-for-linux.zulipchat.com/#narrow/channel/x/topic/x/near/561677301
Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2026-01-23 11:21:12 +01:00
..
bindings mm/ksm: fix flag-dropping behavior in ksm_madvise 2025-10-07 14:01:12 -07:00
helpers rust: bitops: fix missing _find_* functions on 32-bit ARM 2026-01-23 11:21:12 +01:00
kernel rust: maple_tree: rcu_read_lock() in destructor to silence lockdep 2026-01-08 10:17:11 +01:00
macros linux_kselftest-kunit-6.18-rc1 2025-10-01 19:15:11 -07:00
pin-init rust: pin-init: add references to previously initialized fields 2025-09-11 23:30:02 +02:00
uapi Char/Misc/IIO/Binder changes for 6.18-rc1 2025-10-04 16:26:32 -07:00
.gitignore rust: support running Rust documentation tests as KUnit ones 2023-07-19 09:32:53 -06:00
.kunitconfig rust: add kunitconfig 2025-03-19 23:22:21 +01:00
bindgen_parameters rust: derive Zeroable for all structs & unions generated by bindgen where possible 2025-09-08 14:03:17 +02:00
build_error.rs rust: add build_error crate 2022-12-04 01:59:16 +01:00
compiler_builtins.rs ARM: 9441/1: rust: Enable Rust support for ARMv7 2025-03-26 13:31:16 +00:00
exports.c rust: exports: improve grammar in commentary 2024-10-21 17:35:33 +02:00
ffi.rs rust: use absolute paths in macros referencing core and kernel 2025-05-23 00:12:14 +02:00
Makefile rust: Add -fno-isolate-erroneous-paths-dereference to bindgen_skip_c_flags 2025-11-10 08:37:06 +08:00