blob: 2cb7b82053d8182ef1d46567a5504496e3f52ddc [file] [log] [blame]
Miguel Ojeda12f57722021-07-03 16:52:41 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Non-trivial C macros cannot be used in Rust. Similarly, inlined C functions
4 * cannot be called either. This file explicitly creates functions ("helpers")
5 * that wrap those so that they can be called from Rust.
6 *
7 * Even though Rust kernel modules should never use directly the bindings, some
8 * of these helpers need to be exported because Rust generics and inlined
9 * functions may not get their code generated in the crate where they are
10 * defined. Other helpers, called from non-inline functions, may not be
11 * exported, in principle. However, in general, the Rust compiler does not
12 * guarantee codegen will be performed for a non-inline function either.
13 * Therefore, this file exports all the helpers. In the future, this may be
14 * revisited to reduce the number of exports after the compiler is informed
15 * about the places codegen is required.
16 *
17 * All symbols are exported as GPL-only to guarantee no GPL-only feature is
18 * accidentally exposed.
19 */
20
21#include <linux/bug.h>
22#include <linux/build_bug.h>
Asahi Linac7e20fa2023-04-03 18:48:11 +090023#include <linux/err.h>
Wedson Almeida Filho9dc04362022-12-28 06:03:40 +000024#include <linux/refcount.h>
Wedson Almeida Filho6d20d622023-04-11 02:45:33 -030025#include <linux/mutex.h>
Wedson Almeida Filhoc6d917a2023-04-19 14:44:26 -030026#include <linux/spinlock.h>
Wedson Almeida Filho313c4282023-04-11 02:45:39 -030027#include <linux/sched/signal.h>
Miguel Ojeda12f57722021-07-03 16:52:41 +020028
29__noreturn void rust_helper_BUG(void)
30{
31 BUG();
32}
33EXPORT_SYMBOL_GPL(rust_helper_BUG);
34
Wedson Almeida Filho6d20d622023-04-11 02:45:33 -030035void rust_helper_mutex_lock(struct mutex *lock)
36{
37 mutex_lock(lock);
38}
39EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
40
Wedson Almeida Filhoc6d917a2023-04-19 14:44:26 -030041void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
42 struct lock_class_key *key)
43{
44#ifdef CONFIG_DEBUG_SPINLOCK
45 __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
46#else
47 spin_lock_init(lock);
48#endif
49}
50EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
51
52void rust_helper_spin_lock(spinlock_t *lock)
53{
54 spin_lock(lock);
55}
56EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
57
58void rust_helper_spin_unlock(spinlock_t *lock)
59{
60 spin_unlock(lock);
61}
62EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
63
Wedson Almeida Filho313c4282023-04-11 02:45:39 -030064int rust_helper_signal_pending(struct task_struct *t)
65{
66 return signal_pending(t);
67}
68EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
69
Wedson Almeida Filho9dc04362022-12-28 06:03:40 +000070refcount_t rust_helper_REFCOUNT_INIT(int n)
71{
72 return (refcount_t)REFCOUNT_INIT(n);
73}
74EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
75
76void rust_helper_refcount_inc(refcount_t *r)
77{
78 refcount_inc(r);
79}
80EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
81
82bool rust_helper_refcount_dec_and_test(refcount_t *r)
83{
84 return refcount_dec_and_test(r);
85}
86EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
87
Asahi Linac7e20fa2023-04-03 18:48:11 +090088__force void *rust_helper_ERR_PTR(long err)
89{
90 return ERR_PTR(err);
91}
92EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
93
Sven Van Asbroeck752417b2023-04-03 18:48:14 +090094bool rust_helper_IS_ERR(__force const void *ptr)
95{
96 return IS_ERR(ptr);
97}
98EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
99
100long rust_helper_PTR_ERR(__force const void *ptr)
101{
102 return PTR_ERR(ptr);
103}
104EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
105
Wedson Almeida Filho8da7a2b2023-04-11 02:45:40 -0300106struct task_struct *rust_helper_get_current(void)
107{
108 return current;
109}
110EXPORT_SYMBOL_GPL(rust_helper_get_current);
111
Wedson Almeida Filho313c4282023-04-11 02:45:39 -0300112void rust_helper_get_task_struct(struct task_struct *t)
113{
114 get_task_struct(t);
115}
116EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
117
118void rust_helper_put_task_struct(struct task_struct *t)
119{
120 put_task_struct(t);
121}
122EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
123
Miguel Ojeda12f57722021-07-03 16:52:41 +0200124/*
125 * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
126 * as the Rust `usize` type, so we can use it in contexts where Rust
127 * expects a `usize` like slice (array) indices. `usize` is defined to be
128 * the same as C's `uintptr_t` type (can hold any pointer) but not
129 * necessarily the same as `size_t` (can hold the size of any single
130 * object). Most modern platforms use the same concrete integer type for
131 * both of them, but in case we find ourselves on a platform where
132 * that's not true, fail early instead of risking ABI or
133 * integer-overflow issues.
134 *
135 * If your platform fails this assertion, it means that you are in
136 * danger of integer-overflow bugs (even if you attempt to remove
137 * `--size_t-is-usize`). It may be easiest to change the kernel ABI on
138 * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
139 * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
140 */
141static_assert(
142 sizeof(size_t) == sizeof(uintptr_t) &&
143 __alignof__(size_t) == __alignof__(uintptr_t),
144 "Rust code expects C `size_t` to match Rust `usize`"
145);