* [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 01/11] rust: bindings: Support some " Alistair Francis
` (11 more replies)
0 siblings, 12 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis
The kernel includes a large number of static inline functions that are
defined in header files. One example is the crypto_shash_descsize()
function which is defined in hash.h as
```
static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
{
return tfm->descsize;
}
```
bindgen is currently unable to generate bindings to these functions as
they are not publically exposed (they are static after all).
The Rust code currently uses rust_helper_* functions, such as
rust_helper_alloc_pages() for example to call the static inline
functions. But this is a hassle as someone needs to write a C helper
function.
Instead we can use the bindgen wrap-static-fns feature. The feature
is marked as experimental, but has recently been promoted to
non-experimental (depending on your version of bindgen).
By supporting wrap-static-fns we automatically generate a C file called
extern.c that exposes the static inline functions, for example like this
```
unsigned int crypto_shash_descsize__extern(struct crypto_shash *tfm) { return crypto_shash_descsize(tfm); }
```
The nice part is that this is auto-generated.
We then also get a bindings_generate_static.rs file with the Rust
binding, like this
```
extern "C" {
#[link_name = "crypto_shash_descsize__extern"]
pub fn crypto_shash_descsize(tfm: *mut crypto_shash) -> core::ffi::c_uint;
}
```
So now we can use the static inline functions just like normal
functions.
There are a bunch of static inline functions that don't work though, because
the C compiler fails to build extern.c:
* functions with inline asm generate "operand probably does not match constraints"
errors (rip_rel_ptr() for example)
* functions with bit masks (u32_encode_bits() and friends) result in
"call to ‘__bad_mask’ declared with attribute error: bad bitfield mask"
errors
As well as that any static inline function that calls a function that has been
kconfig-ed out will fail to link as the function being called isn't built
(mdio45_ethtool_gset_npage for example)
Due to these failures we use a allow-list system (where functions must
be manually enabled).
This series adds support for bindgen generating wrappers for inline statics and
then converts the existing helper functions to this new method. This doesn't
work for C macros, so we can't reamove all of the helper functions, but we
can remove most.
v5:
- Rebase on latest rust-next on top of v6.13-rc6
- Allow support for LTO inlining (not in this series, see
https://github.com/alistair23/linux/commit/e6b847324b4f5e904e007c0e288c88d2483928a8)
v4:
- Fix out of tree builds
v3:
- Change SoB email address to match from address
- Fixup kunit test build failure
- Update Rust binding documentation
v2:
- Fixup build failures report by test bots
- Rebase on rust-next (ae7851c29747fa376)
Alistair Francis (11):
rust: bindings: Support some inline static functions
rust: helpers: Remove blk helper
rust: helpers: Remove err helper
rust: helpers: Remove kunit helper
rust: helpers: Remove some page helpers
rust: helpers: Remove rbtree helper
rust: helpers: Remove some refcount helpers
rust: helpers: Remove signal helper
rust: helpers: Remove some spinlock helpers
rust: helpers: Remove some task helpers
rust: helpers: Remove uaccess helpers
Documentation/rust/general-information.rst | 10 +++---
rust/.gitignore | 2 ++
rust/Makefile | 37 ++++++++++++++++++++--
rust/bindgen_static_functions | 32 +++++++++++++++++++
rust/bindings/lib.rs | 4 +++
rust/exports.c | 1 +
rust/helpers/blk.c | 14 --------
rust/helpers/err.c | 18 -----------
rust/helpers/helpers.c | 11 ++-----
rust/helpers/kunit.c | 8 -----
rust/helpers/page.c | 5 ---
rust/helpers/rbtree.c | 9 ------
rust/helpers/refcount.c | 10 ------
rust/helpers/signal.c | 8 -----
rust/helpers/spinlock.c | 15 ---------
rust/helpers/task.c | 10 ------
rust/helpers/uaccess.c | 15 ---------
rust/kernel/uaccess.rs | 5 +--
18 files changed, 84 insertions(+), 130 deletions(-)
create mode 100644 rust/bindgen_static_functions
delete mode 100644 rust/helpers/blk.c
delete mode 100644 rust/helpers/err.c
delete mode 100644 rust/helpers/kunit.c
delete mode 100644 rust/helpers/rbtree.c
delete mode 100644 rust/helpers/signal.c
delete mode 100644 rust/helpers/uaccess.c
--
2.47.1
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v5 01/11] rust: bindings: Support some inline static functions
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 02/11] rust: helpers: Remove blk helper Alistair Francis
` (10 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
The kernel includes a large number of static inline functions that are
defined in header files. One example is the crypto_shash_descsize()
function which is defined in hash.h as
static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
{
return tfm->descsize;
}
bindgen is currently unable to generate bindings to these functions as
they are not publically exposed (they are static after all).
The Rust code currently uses rust_helper_* functions, such as
rust_helper_alloc_pages() for example to call the static inline
functions. But this is a hassle as someone needs to write a C helper
function.
Instead we can use the bindgen wrap-static-fns feature. The feature
is marked as experimental, but has recently been promoted to
non-experimental (depending on your version of bindgen).
By supporting wrap-static-fns we automatically generate a C file called
extern.c that exposes the static inline functions, for example like this
```
unsigned int crypto_shash_descsize__extern(struct crypto_shash *tfm) { return crypto_shash_descsize(tfm); }
```
The nice part is that this is auto-generated.
We then also get a bindings_generate_static.rs file with the Rust
binding, like this
```
extern "C" {
#[link_name = "crypto_shash_descsize__extern"]
pub fn crypto_shash_descsize(tfm: *mut crypto_shash) -> core::ffi::c_uint;
}
```
So now we can use the static inline functions just like normal
functions.
There are a bunch of static inline functions that don't work though, because
the C compiler fails to build extern.c:
* functions with inline asm generate "operand probably does not match constraints"
errors (rip_rel_ptr() for example)
* functions with bit masks (u32_encode_bits() and friends) result in
"call to ‘__bad_mask’ declared with attribute error: bad bitfield mask"
errors
As well as that any static inline function that calls a function that has been
kconfig-ed out will fail to link as the function being called isn't built
(mdio45_ethtool_gset_npage for example)
Due to these failures we use a allow-list system (where functions must
be manually enabled).
Link: https://github.com/rust-lang/rust-bindgen/discussions/2405
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
Documentation/rust/general-information.rst | 10 +++---
rust/.gitignore | 2 ++
rust/Makefile | 37 ++++++++++++++++++++--
rust/bindgen_static_functions | 6 ++++
rust/bindings/lib.rs | 4 +++
rust/exports.c | 1 +
6 files changed, 53 insertions(+), 7 deletions(-)
create mode 100644 rust/bindgen_static_functions
diff --git a/Documentation/rust/general-information.rst b/Documentation/rust/general-information.rst
index 6146b49b6a98..632a60703c96 100644
--- a/Documentation/rust/general-information.rst
+++ b/Documentation/rust/general-information.rst
@@ -119,10 +119,12 @@ By including a C header from ``include/`` into
bindings for the included subsystem. After building, see the ``*_generated.rs``
output files in the ``rust/bindings/`` directory.
-For parts of the C header that ``bindgen`` does not auto generate, e.g. C
-``inline`` functions or non-trivial macros, it is acceptable to add a small
-wrapper function to ``rust/helpers/`` to make it available for the Rust side as
-well.
+C ``inline`` functions will only be generated if the function name is
+specified in ``rust/bindgen_static_functions``.
+
+For parts of the C header that ``bindgen`` does not auto generate, e.g.
+non-trivial macros, it is acceptable to add a small wrapper function
+to ``rust/helpers/`` to make it available for the Rust side as well.
Abstractions
~~~~~~~~~~~~
diff --git a/rust/.gitignore b/rust/.gitignore
index d3829ffab80b..741a18023801 100644
--- a/rust/.gitignore
+++ b/rust/.gitignore
@@ -1,10 +1,12 @@
# SPDX-License-Identifier: GPL-2.0
bindings_generated.rs
+bindings_generated_static.rs
bindings_helpers_generated.rs
doctests_kernel_generated.rs
doctests_kernel_generated_kunit.c
uapi_generated.rs
exports_*_generated.h
+extern.c
doc/
test/
diff --git a/rust/Makefile b/rust/Makefile
index a40a3936126d..578fec15480f 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -10,14 +10,17 @@ always-$(CONFIG_RUST) += exports_core_generated.h
# for Rust only, thus there is no header nor prototypes.
obj-$(CONFIG_RUST) += helpers/helpers.o
CFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
+CFLAGS_REMOVE_extern.o = -Wmissing-prototypes -Wmissing-declarations -Wdiscarded-qualifiers
always-$(CONFIG_RUST) += libmacros.so
no-clean-files += libmacros.so
-always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
-obj-$(CONFIG_RUST) += bindings.o kernel.o
+always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs \
+ bindings/bindings_generated_static.rs
+obj-$(CONFIG_RUST) += bindings.o kernel.o extern.o
always-$(CONFIG_RUST) += exports_helpers_generated.h \
- exports_bindings_generated.h exports_kernel_generated.h
+ exports_bindings_generated.h exports_bindings_static_generated.h \
+ exports_kernel_generated.h
always-$(CONFIG_RUST) += uapi/uapi_generated.rs
obj-$(CONFIG_RUST) += uapi.o
@@ -301,6 +304,21 @@ quiet_cmd_bindgen = BINDGEN $@
-o $@ -- $(bindgen_c_flags_final) -DMODULE \
$(bindgen_target_cflags) $(bindgen_target_extra)
+quiet_cmd_bindgen_static = BINDGEN $@
+ cmd_bindgen_static = \
+ $(BINDGEN) $< $(bindgen_target_flags) \
+ --use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
+ --no-debug '.*' --enable-function-attribute-detection \
+ --experimental --wrap-static-fns \
+ --wrap-static-fns-path $(src)/extern.c \
+ $(bindgen_static_functions) \
+ -o $@ -- $(bindgen_c_flags_final) -DMODULE \
+ $(bindgen_target_cflags) $(bindgen_target_extra)
+
+$(src)/extern.c: $(obj)/bindings/bindings_generated_static.rs \
+ $(src)/bindings/bindings_helper.h
+ @sed -i 's|#include ".*|#include "bindings/bindings_helper.h"|g' $@
+
$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
$(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
@@ -309,6 +327,15 @@ $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
$(src)/bindgen_parameters FORCE
$(call if_changed_dep,bindgen)
+$(obj)/bindings/bindings_generated_static.rs: private bindgen_target_flags = \
+ $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
+$(obj)/bindings/bindings_generated_static.rs: private bindgen_static_functions = \
+ $(shell grep -Ev '^#|^$$' $(src)/bindgen_static_functions)
+$(obj)/bindings/bindings_generated_static.rs: $(src)/bindings/bindings_helper.h \
+ $(src)/bindgen_static_functions \
+ $(src)/bindgen_parameters FORCE
+ $(call if_changed_dep,bindgen_static)
+
$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
$(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
@@ -352,6 +379,9 @@ $(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
$(call if_changed,exports)
+$(obj)/exports_bindings_static_generated.h: $(obj)/extern.o FORCE
+ $(call if_changed,exports)
+
$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
$(call if_changed,exports)
@@ -431,6 +461,7 @@ $(obj)/bindings.o: private rustc_target_flags = --extern ffi
$(obj)/bindings.o: $(src)/bindings/lib.rs \
$(obj)/ffi.o \
$(obj)/bindings/bindings_generated.rs \
+ $(obj)/bindings/bindings_generated_static.rs \
$(obj)/bindings/bindings_helpers_generated.rs FORCE
+$(call if_changed_rule,rustc_library)
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
new file mode 100644
index 000000000000..1f24360daeb3
--- /dev/null
+++ b/rust/bindgen_static_functions
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# Don't generate structs
+--blocklist-type '.*'
+
+--allowlist-function blk_mq_rq_to_pdu
diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs
index 014af0d1fc70..02f789221025 100644
--- a/rust/bindings/lib.rs
+++ b/rust/bindings/lib.rs
@@ -39,6 +39,10 @@ mod bindings_raw {
env!("OBJTREE"),
"/rust/bindings/bindings_generated.rs"
));
+ include!(concat!(
+ env!("OBJTREE"),
+ "/rust/bindings/bindings_generated_static.rs"
+ ));
}
// When both a directly exposed symbol and a helper exists for the same function,
diff --git a/rust/exports.c b/rust/exports.c
index 587f0e776aba..288958d2ebea 100644
--- a/rust/exports.c
+++ b/rust/exports.c
@@ -18,6 +18,7 @@
#include "exports_core_generated.h"
#include "exports_helpers_generated.h"
#include "exports_bindings_generated.h"
+#include "exports_bindings_static_generated.h"
#include "exports_kernel_generated.h"
// For modules using `rust/build_error.rs`.
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 02/11] rust: helpers: Remove blk helper
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
2025-01-07 3:50 ` [PATCH v5 01/11] rust: bindings: Support some " Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 03/11] rust: helpers: Remove err helper Alistair Francis
` (9 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helper.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 1 +
rust/helpers/blk.c | 14 --------------
rust/helpers/helpers.c | 1 -
3 files changed, 1 insertion(+), 15 deletions(-)
delete mode 100644 rust/helpers/blk.c
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index 1f24360daeb3..42e45ce34221 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -4,3 +4,4 @@
--blocklist-type '.*'
--allowlist-function blk_mq_rq_to_pdu
+--allowlist-function blk_mq_rq_from_pdu
diff --git a/rust/helpers/blk.c b/rust/helpers/blk.c
deleted file mode 100644
index cc9f4e6a2d23..000000000000
--- a/rust/helpers/blk.c
+++ /dev/null
@@ -1,14 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-#include <linux/blk-mq.h>
-#include <linux/blkdev.h>
-
-void *rust_helper_blk_mq_rq_to_pdu(struct request *rq)
-{
- return blk_mq_rq_to_pdu(rq);
-}
-
-struct request *rust_helper_blk_mq_rq_from_pdu(void *pdu)
-{
- return blk_mq_rq_from_pdu(pdu);
-}
diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index dcf827a61b52..1d31672c147c 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -7,7 +7,6 @@
* Sorted alphabetically.
*/
-#include "blk.c"
#include "bug.c"
#include "build_assert.c"
#include "build_bug.c"
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 03/11] rust: helpers: Remove err helper
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
2025-01-07 3:50 ` [PATCH v5 01/11] rust: bindings: Support some " Alistair Francis
2025-01-07 3:50 ` [PATCH v5 02/11] rust: helpers: Remove blk helper Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 04/11] rust: helpers: Remove kunit helper Alistair Francis
` (8 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helper.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 4 ++++
rust/helpers/err.c | 18 ------------------
rust/helpers/helpers.c | 1 -
3 files changed, 4 insertions(+), 19 deletions(-)
delete mode 100644 rust/helpers/err.c
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index 42e45ce34221..0269efa83c61 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -5,3 +5,7 @@
--allowlist-function blk_mq_rq_to_pdu
--allowlist-function blk_mq_rq_from_pdu
+
+--allowlist-function ERR_PTR
+--allowlist-function IS_ERR
+--allowlist-function PTR_ERR
diff --git a/rust/helpers/err.c b/rust/helpers/err.c
deleted file mode 100644
index 544c7cb86632..000000000000
--- a/rust/helpers/err.c
+++ /dev/null
@@ -1,18 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-#include <linux/err.h>
-
-__force void *rust_helper_ERR_PTR(long err)
-{
- return ERR_PTR(err);
-}
-
-bool rust_helper_IS_ERR(__force const void *ptr)
-{
- return IS_ERR(ptr);
-}
-
-long rust_helper_PTR_ERR(__force const void *ptr)
-{
- return PTR_ERR(ptr);
-}
diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 1d31672c147c..675f8eae0475 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -11,7 +11,6 @@
#include "build_assert.c"
#include "build_bug.c"
#include "cred.c"
-#include "err.c"
#include "fs.c"
#include "jump_label.c"
#include "kunit.c"
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 04/11] rust: helpers: Remove kunit helper
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (2 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 03/11] rust: helpers: Remove err helper Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 05/11] rust: helpers: Remove some page helpers Alistair Francis
` (7 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helper.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 2 ++
rust/helpers/helpers.c | 1 -
rust/helpers/kunit.c | 8 --------
3 files changed, 2 insertions(+), 9 deletions(-)
delete mode 100644 rust/helpers/kunit.c
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index 0269efa83c61..b4032d277e72 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -9,3 +9,5 @@
--allowlist-function ERR_PTR
--allowlist-function IS_ERR
--allowlist-function PTR_ERR
+
+--allowlist-function kunit_get_current_test
diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 675f8eae0475..9e6c6c9c67d2 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -13,7 +13,6 @@
#include "cred.c"
#include "fs.c"
#include "jump_label.c"
-#include "kunit.c"
#include "mutex.c"
#include "page.c"
#include "pid_namespace.c"
diff --git a/rust/helpers/kunit.c b/rust/helpers/kunit.c
deleted file mode 100644
index b85a4d394c11..000000000000
--- a/rust/helpers/kunit.c
+++ /dev/null
@@ -1,8 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-#include <kunit/test-bug.h>
-
-struct kunit *rust_helper_kunit_get_current_test(void)
-{
- return kunit_get_current_test();
-}
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 05/11] rust: helpers: Remove some page helpers
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (3 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 04/11] rust: helpers: Remove kunit helper Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 06/11] rust: helpers: Remove rbtree helper Alistair Francis
` (6 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helpers.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 2 ++
rust/helpers/page.c | 5 -----
2 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index b4032d277e72..ded5b816f304 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -11,3 +11,5 @@
--allowlist-function PTR_ERR
--allowlist-function kunit_get_current_test
+
+--allowlist-function kmap_local_page
diff --git a/rust/helpers/page.c b/rust/helpers/page.c
index b3f2b8fbf87f..52ebec9d7186 100644
--- a/rust/helpers/page.c
+++ b/rust/helpers/page.c
@@ -8,11 +8,6 @@ struct page *rust_helper_alloc_pages(gfp_t gfp_mask, unsigned int order)
return alloc_pages(gfp_mask, order);
}
-void *rust_helper_kmap_local_page(struct page *page)
-{
- return kmap_local_page(page);
-}
-
void rust_helper_kunmap_local(const void *addr)
{
kunmap_local(addr);
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 06/11] rust: helpers: Remove rbtree helper
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (4 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 05/11] rust: helpers: Remove some page helpers Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 07/11] rust: helpers: Remove some refcount helpers Alistair Francis
` (5 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helper.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 2 ++
rust/helpers/helpers.c | 1 -
rust/helpers/rbtree.c | 9 ---------
3 files changed, 2 insertions(+), 10 deletions(-)
delete mode 100644 rust/helpers/rbtree.c
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index ded5b816f304..e464dc1f5682 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -13,3 +13,5 @@
--allowlist-function kunit_get_current_test
--allowlist-function kmap_local_page
+
+--allowlist-function rb_link_node
diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 9e6c6c9c67d2..54c52ceab77a 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -16,7 +16,6 @@
#include "mutex.c"
#include "page.c"
#include "pid_namespace.c"
-#include "rbtree.c"
#include "refcount.c"
#include "security.c"
#include "signal.c"
diff --git a/rust/helpers/rbtree.c b/rust/helpers/rbtree.c
deleted file mode 100644
index 6d404b84a9b5..000000000000
--- a/rust/helpers/rbtree.c
+++ /dev/null
@@ -1,9 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-#include <linux/rbtree.h>
-
-void rust_helper_rb_link_node(struct rb_node *node, struct rb_node *parent,
- struct rb_node **rb_link)
-{
- rb_link_node(node, parent, rb_link);
-}
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 07/11] rust: helpers: Remove some refcount helpers
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (5 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 06/11] rust: helpers: Remove rbtree helper Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 08/11] rust: helpers: Remove signal helper Alistair Francis
` (4 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helpers.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 3 +++
rust/helpers/refcount.c | 10 ----------
2 files changed, 3 insertions(+), 10 deletions(-)
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index e464dc1f5682..9c40a867a64d 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -15,3 +15,6 @@
--allowlist-function kmap_local_page
--allowlist-function rb_link_node
+
+--allowlist-function refcount_inc
+--allowlist-function refcount_dec_and_test
diff --git a/rust/helpers/refcount.c b/rust/helpers/refcount.c
index d6adbd2e45a1..ed13236246d8 100644
--- a/rust/helpers/refcount.c
+++ b/rust/helpers/refcount.c
@@ -6,13 +6,3 @@ refcount_t rust_helper_REFCOUNT_INIT(int n)
{
return (refcount_t)REFCOUNT_INIT(n);
}
-
-void rust_helper_refcount_inc(refcount_t *r)
-{
- refcount_inc(r);
-}
-
-bool rust_helper_refcount_dec_and_test(refcount_t *r)
-{
- return refcount_dec_and_test(r);
-}
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 08/11] rust: helpers: Remove signal helper
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (6 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 07/11] rust: helpers: Remove some refcount helpers Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 09/11] rust: helpers: Remove some spinlock helpers Alistair Francis
` (3 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helper.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 2 ++
rust/helpers/helpers.c | 1 -
rust/helpers/signal.c | 8 --------
3 files changed, 2 insertions(+), 9 deletions(-)
delete mode 100644 rust/helpers/signal.c
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index 9c40a867a64d..407dd091ddec 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -18,3 +18,5 @@
--allowlist-function refcount_inc
--allowlist-function refcount_dec_and_test
+
+--allowlist-function signal_pending
diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 54c52ceab77a..2731ddf736dd 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -18,7 +18,6 @@
#include "pid_namespace.c"
#include "refcount.c"
#include "security.c"
-#include "signal.c"
#include "slab.c"
#include "spinlock.c"
#include "task.c"
diff --git a/rust/helpers/signal.c b/rust/helpers/signal.c
deleted file mode 100644
index 1a6bbe9438e2..000000000000
--- a/rust/helpers/signal.c
+++ /dev/null
@@ -1,8 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-#include <linux/sched/signal.h>
-
-int rust_helper_signal_pending(struct task_struct *t)
-{
- return signal_pending(t);
-}
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 09/11] rust: helpers: Remove some spinlock helpers
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (7 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 08/11] rust: helpers: Remove signal helper Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 10/11] rust: helpers: Remove some task helpers Alistair Francis
` (2 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helpers.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 4 ++++
rust/helpers/spinlock.c | 15 ---------------
2 files changed, 4 insertions(+), 15 deletions(-)
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index 407dd091ddec..9d6c44e277b5 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -20,3 +20,7 @@
--allowlist-function refcount_dec_and_test
--allowlist-function signal_pending
+
+--allowlist-function spin_lock
+--allowlist-function spin_unlock
+--allowlist-function spin_trylock
diff --git a/rust/helpers/spinlock.c b/rust/helpers/spinlock.c
index 5971fdf6f755..72ca0208ed10 100644
--- a/rust/helpers/spinlock.c
+++ b/rust/helpers/spinlock.c
@@ -15,18 +15,3 @@ void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
spin_lock_init(lock);
#endif /* CONFIG_DEBUG_SPINLOCK */
}
-
-void rust_helper_spin_lock(spinlock_t *lock)
-{
- spin_lock(lock);
-}
-
-void rust_helper_spin_unlock(spinlock_t *lock)
-{
- spin_unlock(lock);
-}
-
-int rust_helper_spin_trylock(spinlock_t *lock)
-{
- return spin_trylock(lock);
-}
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 10/11] rust: helpers: Remove some task helpers
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (8 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 09/11] rust: helpers: Remove some spinlock helpers Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 3:50 ` [PATCH v5 11/11] rust: helpers: Remove uaccess helpers Alistair Francis
2025-01-07 11:47 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Andreas Hindborg
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helpers.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 3 +++
rust/helpers/task.c | 10 ----------
2 files changed, 3 insertions(+), 10 deletions(-)
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index 9d6c44e277b5..8bc291a7a799 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -24,3 +24,6 @@
--allowlist-function spin_lock
--allowlist-function spin_unlock
--allowlist-function spin_trylock
+
+--allowlist-function get_task_struct
+--allowlist-function put_task_struct
diff --git a/rust/helpers/task.c b/rust/helpers/task.c
index 31c33ea2dce6..38a7765d9915 100644
--- a/rust/helpers/task.c
+++ b/rust/helpers/task.c
@@ -7,16 +7,6 @@ struct task_struct *rust_helper_get_current(void)
return current;
}
-void rust_helper_get_task_struct(struct task_struct *t)
-{
- get_task_struct(t);
-}
-
-void rust_helper_put_task_struct(struct task_struct *t)
-{
- put_task_struct(t);
-}
-
kuid_t rust_helper_task_uid(struct task_struct *task)
{
return task_uid(task);
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v5 11/11] rust: helpers: Remove uaccess helpers
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (9 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 10/11] rust: helpers: Remove some task helpers Alistair Francis
@ 2025-01-07 3:50 ` Alistair Francis
2025-01-07 11:47 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Andreas Hindborg
11 siblings, 0 replies; 16+ messages in thread
From: Alistair Francis @ 2025-01-07 3:50 UTC (permalink / raw)
To: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, a.hindborg, me, linux-kernel,
aliceryhl, alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron
Cc: alistair23, wilfred.mallawa, Alistair Francis, Dirk Behme
Now that we support wrap-static-fns we no longer need the custom helper.
Signed-off-by: Alistair Francis <alistair@alistair23.me>
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
---
rust/bindgen_static_functions | 3 +++
rust/helpers/helpers.c | 6 ++----
rust/helpers/uaccess.c | 15 ---------------
rust/kernel/uaccess.rs | 5 +++--
4 files changed, 8 insertions(+), 21 deletions(-)
delete mode 100644 rust/helpers/uaccess.c
diff --git a/rust/bindgen_static_functions b/rust/bindgen_static_functions
index 8bc291a7a799..ec48ad2e8c78 100644
--- a/rust/bindgen_static_functions
+++ b/rust/bindgen_static_functions
@@ -27,3 +27,6 @@
--allowlist-function get_task_struct
--allowlist-function put_task_struct
+
+--allowlist-function copy_from_user
+--allowlist-function copy_to_user
diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 2731ddf736dd..eae067fe2528 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
/*
- * Non-trivial C macros cannot be used in Rust. Similarly, inlined C functions
- * cannot be called either. This file explicitly creates functions ("helpers")
- * that wrap those so that they can be called from Rust.
+ * Non-trivial C macros cannot be used in Rust. This file explicitly creates
+ * functions ("helpers") that wrap those so that they can be called from Rust.
*
* Sorted alphabetically.
*/
@@ -21,7 +20,6 @@
#include "slab.c"
#include "spinlock.c"
#include "task.c"
-#include "uaccess.c"
#include "vmalloc.c"
#include "wait.c"
#include "workqueue.c"
diff --git a/rust/helpers/uaccess.c b/rust/helpers/uaccess.c
deleted file mode 100644
index f49076f813cd..000000000000
--- a/rust/helpers/uaccess.c
+++ /dev/null
@@ -1,15 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-#include <linux/uaccess.h>
-
-unsigned long rust_helper_copy_from_user(void *to, const void __user *from,
- unsigned long n)
-{
- return copy_from_user(to, from, n);
-}
-
-unsigned long rust_helper_copy_to_user(void __user *to, const void *from,
- unsigned long n)
-{
- return copy_to_user(to, from, n);
-}
diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index cc044924867b..d8f75de93073 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -226,7 +226,8 @@ pub fn read_raw(&mut self, out: &mut [MaybeUninit<u8>]) -> Result {
}
// SAFETY: `out_ptr` points into a mutable slice of length `len`, so we may write
// that many bytes to it.
- let res = unsafe { bindings::copy_from_user(out_ptr, self.ptr as *const c_void, len) };
+ let res =
+ unsafe { bindings::copy_from_user(out_ptr, self.ptr as *const c_void, len as u64) };
if res != 0 {
return Err(EFAULT);
}
@@ -330,7 +331,7 @@ pub fn write_slice(&mut self, data: &[u8]) -> Result {
}
// SAFETY: `data_ptr` points into an immutable slice of length `len`, so we may read
// that many bytes from it.
- let res = unsafe { bindings::copy_to_user(self.ptr as *mut c_void, data_ptr, len) };
+ let res = unsafe { bindings::copy_to_user(self.ptr as *mut c_void, data_ptr, len as u64) };
if res != 0 {
return Err(EFAULT);
}
--
2.47.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
` (10 preceding siblings ...)
2025-01-07 3:50 ` [PATCH v5 11/11] rust: helpers: Remove uaccess helpers Alistair Francis
@ 2025-01-07 11:47 ` Andreas Hindborg
2025-01-14 6:02 ` Alistair Francis
11 siblings, 1 reply; 16+ messages in thread
From: Andreas Hindborg @ 2025-01-07 11:47 UTC (permalink / raw)
To: Alistair Francis
Cc: bhelgaas, rust-for-linux, lukas, gary, akpm, tmgross, boqun.feng,
ojeda, linux-cxl, bjorn3_gh, me, linux-kernel, aliceryhl,
alistair.francis, linux-pci, benno.lossin, alex.gaynor,
Jonathan.Cameron, alistair23, wilfred.mallawa
"Alistair Francis" <alistair@alistair23.me> writes:
> The kernel includes a large number of static inline functions that are
> defined in header files. One example is the crypto_shash_descsize()
> function which is defined in hash.h as
>
> ```
> static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
> {
> return tfm->descsize;
> }
> ```
>
> bindgen is currently unable to generate bindings to these functions as
> they are not publically exposed (they are static after all).
>
> The Rust code currently uses rust_helper_* functions, such as
> rust_helper_alloc_pages() for example to call the static inline
> functions. But this is a hassle as someone needs to write a C helper
> function.
>
> Instead we can use the bindgen wrap-static-fns feature. The feature
> is marked as experimental, but has recently been promoted to
> non-experimental (depending on your version of bindgen).
>
> By supporting wrap-static-fns we automatically generate a C file called
> extern.c that exposes the static inline functions, for example like this
>
> ```
> unsigned int crypto_shash_descsize__extern(struct crypto_shash *tfm) { return crypto_shash_descsize(tfm); }
> ```
>
> The nice part is that this is auto-generated.
>
> We then also get a bindings_generate_static.rs file with the Rust
> binding, like this
>
> ```
> extern "C" {
> #[link_name = "crypto_shash_descsize__extern"]
> pub fn crypto_shash_descsize(tfm: *mut crypto_shash) -> core::ffi::c_uint;
> }
> ```
>
> So now we can use the static inline functions just like normal
> functions.
>
> There are a bunch of static inline functions that don't work though, because
> the C compiler fails to build extern.c:
> * functions with inline asm generate "operand probably does not match constraints"
> errors (rip_rel_ptr() for example)
> * functions with bit masks (u32_encode_bits() and friends) result in
> "call to ‘__bad_mask’ declared with attribute error: bad bitfield mask"
> errors
>
> As well as that any static inline function that calls a function that has been
> kconfig-ed out will fail to link as the function being called isn't built
> (mdio45_ethtool_gset_npage for example)
>
> Due to these failures we use a allow-list system (where functions must
> be manually enabled).
>
> This series adds support for bindgen generating wrappers for inline statics and
> then converts the existing helper functions to this new method. This doesn't
> work for C macros, so we can't reamove all of the helper functions, but we
> can remove most.
>
> v5:
> - Rebase on latest rust-next on top of v6.13-rc6
> - Allow support for LTO inlining (not in this series, see
> https://github.com/alistair23/linux/commit/e6b847324b4f5e904e007c0e288c88d2483928a8)
Thanks! Since Gary just sent v2 of the LTO patch [1], could you rebase
on that and list it as a dependency? If you are using b4 there is a nice
feature for that [2].
Best regards,
Andreas Hindborg
[1] https://lore.kernel.org/all/20250105194054.545201-1-gary@garyguo.net/
[2] https://b4.docs.kernel.org/en/latest/contributor/prep.html#working-with-series-dependencies
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions
2025-01-07 11:47 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Andreas Hindborg
@ 2025-01-14 6:02 ` Alistair Francis
2025-03-18 6:53 ` Alistair Francis
0 siblings, 1 reply; 16+ messages in thread
From: Alistair Francis @ 2025-01-14 6:02 UTC (permalink / raw)
To: Andreas Hindborg
Cc: Alistair Francis, bhelgaas, rust-for-linux, lukas, gary, akpm,
tmgross, boqun.feng, ojeda, linux-cxl, bjorn3_gh, me,
linux-kernel, aliceryhl, alistair.francis, linux-pci,
benno.lossin, alex.gaynor, Jonathan.Cameron, wilfred.mallawa
On Tue, Jan 7, 2025 at 9:48 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> "Alistair Francis" <alistair@alistair23.me> writes:
>
> > The kernel includes a large number of static inline functions that are
> > defined in header files. One example is the crypto_shash_descsize()
> > function which is defined in hash.h as
> >
> > ```
> > static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
> > {
> > return tfm->descsize;
> > }
> > ```
> >
> > bindgen is currently unable to generate bindings to these functions as
> > they are not publically exposed (they are static after all).
> >
> > The Rust code currently uses rust_helper_* functions, such as
> > rust_helper_alloc_pages() for example to call the static inline
> > functions. But this is a hassle as someone needs to write a C helper
> > function.
> >
> > Instead we can use the bindgen wrap-static-fns feature. The feature
> > is marked as experimental, but has recently been promoted to
> > non-experimental (depending on your version of bindgen).
> >
> > By supporting wrap-static-fns we automatically generate a C file called
> > extern.c that exposes the static inline functions, for example like this
> >
> > ```
> > unsigned int crypto_shash_descsize__extern(struct crypto_shash *tfm) { return crypto_shash_descsize(tfm); }
> > ```
> >
> > The nice part is that this is auto-generated.
> >
> > We then also get a bindings_generate_static.rs file with the Rust
> > binding, like this
> >
> > ```
> > extern "C" {
> > #[link_name = "crypto_shash_descsize__extern"]
> > pub fn crypto_shash_descsize(tfm: *mut crypto_shash) -> core::ffi::c_uint;
> > }
> > ```
> >
> > So now we can use the static inline functions just like normal
> > functions.
> >
> > There are a bunch of static inline functions that don't work though, because
> > the C compiler fails to build extern.c:
> > * functions with inline asm generate "operand probably does not match constraints"
> > errors (rip_rel_ptr() for example)
> > * functions with bit masks (u32_encode_bits() and friends) result in
> > "call to ‘__bad_mask’ declared with attribute error: bad bitfield mask"
> > errors
> >
> > As well as that any static inline function that calls a function that has been
> > kconfig-ed out will fail to link as the function being called isn't built
> > (mdio45_ethtool_gset_npage for example)
> >
> > Due to these failures we use a allow-list system (where functions must
> > be manually enabled).
> >
> > This series adds support for bindgen generating wrappers for inline statics and
> > then converts the existing helper functions to this new method. This doesn't
> > work for C macros, so we can't reamove all of the helper functions, but we
> > can remove most.
> >
> > v5:
> > - Rebase on latest rust-next on top of v6.13-rc6
> > - Allow support for LTO inlining (not in this series, see
> > https://github.com/alistair23/linux/commit/e6b847324b4f5e904e007c0e288c88d2483928a8)
>
> Thanks! Since Gary just sent v2 of the LTO patch [1], could you rebase
> on that and list it as a dependency? If you are using b4 there is a nice
I can't get Gary's series to apply on rust-next (it does apply on
master though).
I might just wait until Gary's series gets picked up to rust-next as
there is already a lot of manual rebasing going on and my series
currently applies on rust-next.
Unfortunately there are just constant conflicts as the number of
manual C helpers are continually growing.
Obviously when/if this series is approved I can do a final rebase, I
would just like to avoid unnecessary churn in the meantime
Alistair
> feature for that [2].
>
>
> Best regards,
> Andreas Hindborg
>
>
> [1] https://lore.kernel.org/all/20250105194054.545201-1-gary@garyguo.net/
> [2] https://b4.docs.kernel.org/en/latest/contributor/prep.html#working-with-series-dependencies
>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions
2025-01-14 6:02 ` Alistair Francis
@ 2025-03-18 6:53 ` Alistair Francis
2025-03-18 14:50 ` Tamir Duberstein
0 siblings, 1 reply; 16+ messages in thread
From: Alistair Francis @ 2025-03-18 6:53 UTC (permalink / raw)
To: Andreas Hindborg
Cc: Alistair Francis, bhelgaas, rust-for-linux, lukas, gary, akpm,
tmgross, boqun.feng, ojeda, linux-cxl, bjorn3_gh, me,
linux-kernel, aliceryhl, alistair.francis, linux-pci,
benno.lossin, alex.gaynor, Jonathan.Cameron, wilfred.mallawa
On Tue, Jan 14, 2025 at 4:02 PM Alistair Francis <alistair23@gmail.com> wrote:
>
> On Tue, Jan 7, 2025 at 9:48 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
> >
> > "Alistair Francis" <alistair@alistair23.me> writes:
> >
> > > The kernel includes a large number of static inline functions that are
> > > defined in header files. One example is the crypto_shash_descsize()
> > > function which is defined in hash.h as
> > >
> > > ```
> > > static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
> > > {
> > > return tfm->descsize;
> > > }
> > > ```
> > >
> > > bindgen is currently unable to generate bindings to these functions as
> > > they are not publically exposed (they are static after all).
> > >
> > > The Rust code currently uses rust_helper_* functions, such as
> > > rust_helper_alloc_pages() for example to call the static inline
> > > functions. But this is a hassle as someone needs to write a C helper
> > > function.
> > >
> > > Instead we can use the bindgen wrap-static-fns feature. The feature
> > > is marked as experimental, but has recently been promoted to
> > > non-experimental (depending on your version of bindgen).
> > >
> > > By supporting wrap-static-fns we automatically generate a C file called
> > > extern.c that exposes the static inline functions, for example like this
> > >
> > > ```
> > > unsigned int crypto_shash_descsize__extern(struct crypto_shash *tfm) { return crypto_shash_descsize(tfm); }
> > > ```
> > >
> > > The nice part is that this is auto-generated.
> > >
> > > We then also get a bindings_generate_static.rs file with the Rust
> > > binding, like this
> > >
> > > ```
> > > extern "C" {
> > > #[link_name = "crypto_shash_descsize__extern"]
> > > pub fn crypto_shash_descsize(tfm: *mut crypto_shash) -> core::ffi::c_uint;
> > > }
> > > ```
> > >
> > > So now we can use the static inline functions just like normal
> > > functions.
> > >
> > > There are a bunch of static inline functions that don't work though, because
> > > the C compiler fails to build extern.c:
> > > * functions with inline asm generate "operand probably does not match constraints"
> > > errors (rip_rel_ptr() for example)
> > > * functions with bit masks (u32_encode_bits() and friends) result in
> > > "call to ‘__bad_mask’ declared with attribute error: bad bitfield mask"
> > > errors
> > >
> > > As well as that any static inline function that calls a function that has been
> > > kconfig-ed out will fail to link as the function being called isn't built
> > > (mdio45_ethtool_gset_npage for example)
> > >
> > > Due to these failures we use a allow-list system (where functions must
> > > be manually enabled).
> > >
> > > This series adds support for bindgen generating wrappers for inline statics and
> > > then converts the existing helper functions to this new method. This doesn't
> > > work for C macros, so we can't reamove all of the helper functions, but we
> > > can remove most.
> > >
> > > v5:
> > > - Rebase on latest rust-next on top of v6.13-rc6
> > > - Allow support for LTO inlining (not in this series, see
> > > https://github.com/alistair23/linux/commit/e6b847324b4f5e904e007c0e288c88d2483928a8)
> >
> > Thanks! Since Gary just sent v2 of the LTO patch [1], could you rebase
> > on that and list it as a dependency? If you are using b4 there is a nice
>
> I can't get Gary's series to apply on rust-next (it does apply on
> master though).
>
> I might just wait until Gary's series gets picked up to rust-next as
> there is already a lot of manual rebasing going on and my series
> currently applies on rust-next.
>
> Unfortunately there are just constant conflicts as the number of
> manual C helpers are continually growing.
>
> Obviously when/if this series is approved I can do a final rebase, I
> would just like to avoid unnecessary churn in the meantime
Any more thoughts on this?
Alistair
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions
2025-03-18 6:53 ` Alistair Francis
@ 2025-03-18 14:50 ` Tamir Duberstein
0 siblings, 0 replies; 16+ messages in thread
From: Tamir Duberstein @ 2025-03-18 14:50 UTC (permalink / raw)
To: Alistair Francis
Cc: Andreas Hindborg, Alistair Francis, bhelgaas, rust-for-linux,
lukas, gary, akpm, tmgross, boqun.feng, ojeda, linux-cxl,
bjorn3_gh, me, linux-kernel, aliceryhl, alistair.francis,
linux-pci, benno.lossin, alex.gaynor, Jonathan.Cameron,
wilfred.mallawa
On Tue, Mar 18, 2025 at 2:53 AM Alistair Francis <alistair23@gmail.com> wrote:
>
> On Tue, Jan 14, 2025 at 4:02 PM Alistair Francis <alistair23@gmail.com> wrote:
> >
> > On Tue, Jan 7, 2025 at 9:48 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
> > >
> > > Thanks! Since Gary just sent v2 of the LTO patch [1], could you rebase
> > > on that and list it as a dependency? If you are using b4 there is a nice
> >
> > I can't get Gary's series to apply on rust-next (it does apply on
> > master though).
> >
> > I might just wait until Gary's series gets picked up to rust-next as
> > there is already a lot of manual rebasing going on and my series
> > currently applies on rust-next.
> >
> > Unfortunately there are just constant conflicts as the number of
> > manual C helpers are continually growing.
> >
> > Obviously when/if this series is approved I can do a final rebase, I
> > would just like to avoid unnecessary churn in the meantime
>
> Any more thoughts on this?
Hi Alistair,
I can't speak for Gary as I don't know what his plans are for that LTO
series, but I did review that series and this one, and at first glance
the two seem orthogonal. The goal of Gary's series is to LTO C helpers
into Rust, while the goal of this series is to machine-generate those
helpers. Do I have that right?
If yes, I think it's important to think about how the two fit
together, at least conceptually. Mechanically I think there's an issue
with this trick:
#ifdef __BINDGEN__
#define __rust_helper
#else
#define __rust_helper inline
#endif
which the LTO series uses to generate Rust stubs for these functions
while also keeping them marked inline in LLVM IR/bitcode.
That said, I see some discussion of cross-language LTO in "How to
handle static inline functions" [1], so maybe there's another way that
makes more sense in light of this series.
Updating the cover letter to elucidate these details is where I'd
suggest you start.
Cheers.
Tamir
[1] https://github.com/rust-lang/rust-bindgen/discussions/2405
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2025-03-18 14:50 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <PsAMnW6hScU1fLV8ucb6wOkHECGXCrwXeSEfeVs3Hc-zbwrML674jGT8H_XNvWiF6EdymYJZSusanBrtAsZjAg==@protonmail.internalid>
2025-01-07 3:50 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Alistair Francis
2025-01-07 3:50 ` [PATCH v5 01/11] rust: bindings: Support some " Alistair Francis
2025-01-07 3:50 ` [PATCH v5 02/11] rust: helpers: Remove blk helper Alistair Francis
2025-01-07 3:50 ` [PATCH v5 03/11] rust: helpers: Remove err helper Alistair Francis
2025-01-07 3:50 ` [PATCH v5 04/11] rust: helpers: Remove kunit helper Alistair Francis
2025-01-07 3:50 ` [PATCH v5 05/11] rust: helpers: Remove some page helpers Alistair Francis
2025-01-07 3:50 ` [PATCH v5 06/11] rust: helpers: Remove rbtree helper Alistair Francis
2025-01-07 3:50 ` [PATCH v5 07/11] rust: helpers: Remove some refcount helpers Alistair Francis
2025-01-07 3:50 ` [PATCH v5 08/11] rust: helpers: Remove signal helper Alistair Francis
2025-01-07 3:50 ` [PATCH v5 09/11] rust: helpers: Remove some spinlock helpers Alistair Francis
2025-01-07 3:50 ` [PATCH v5 10/11] rust: helpers: Remove some task helpers Alistair Francis
2025-01-07 3:50 ` [PATCH v5 11/11] rust: helpers: Remove uaccess helpers Alistair Francis
2025-01-07 11:47 ` [PATCH v5 00/11] rust: bindings: Auto-generate inline static functions Andreas Hindborg
2025-01-14 6:02 ` Alistair Francis
2025-03-18 6:53 ` Alistair Francis
2025-03-18 14:50 ` Tamir Duberstein
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).