xref: /openbmc/linux/rust/helpers.c (revision 4cfb9080)
1 // 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>
23 #include <linux/err.h>
24 #include <linux/errname.h>
25 #include <linux/refcount.h>
26 #include <linux/mutex.h>
27 #include <linux/spinlock.h>
28 #include <linux/sched/signal.h>
29 #include <linux/wait.h>
30 
31 __noreturn void rust_helper_BUG(void)
32 {
33 	BUG();
34 }
35 EXPORT_SYMBOL_GPL(rust_helper_BUG);
36 
37 void rust_helper_mutex_lock(struct mutex *lock)
38 {
39 	mutex_lock(lock);
40 }
41 EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
42 
43 void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
44 				  struct lock_class_key *key)
45 {
46 #ifdef CONFIG_DEBUG_SPINLOCK
47 	__raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
48 #else
49 	spin_lock_init(lock);
50 #endif
51 }
52 EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
53 
54 void rust_helper_spin_lock(spinlock_t *lock)
55 {
56 	spin_lock(lock);
57 }
58 EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
59 
60 void rust_helper_spin_unlock(spinlock_t *lock)
61 {
62 	spin_unlock(lock);
63 }
64 EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
65 
66 void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
67 {
68 	init_wait(wq_entry);
69 }
70 EXPORT_SYMBOL_GPL(rust_helper_init_wait);
71 
72 int rust_helper_signal_pending(struct task_struct *t)
73 {
74 	return signal_pending(t);
75 }
76 EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
77 
78 refcount_t rust_helper_REFCOUNT_INIT(int n)
79 {
80 	return (refcount_t)REFCOUNT_INIT(n);
81 }
82 EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
83 
84 void rust_helper_refcount_inc(refcount_t *r)
85 {
86 	refcount_inc(r);
87 }
88 EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
89 
90 bool rust_helper_refcount_dec_and_test(refcount_t *r)
91 {
92 	return refcount_dec_and_test(r);
93 }
94 EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
95 
96 __force void *rust_helper_ERR_PTR(long err)
97 {
98 	return ERR_PTR(err);
99 }
100 EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
101 
102 bool rust_helper_IS_ERR(__force const void *ptr)
103 {
104 	return IS_ERR(ptr);
105 }
106 EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
107 
108 long rust_helper_PTR_ERR(__force const void *ptr)
109 {
110 	return PTR_ERR(ptr);
111 }
112 EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
113 
114 const char *rust_helper_errname(int err)
115 {
116 	return errname(err);
117 }
118 EXPORT_SYMBOL_GPL(rust_helper_errname);
119 
120 struct task_struct *rust_helper_get_current(void)
121 {
122 	return current;
123 }
124 EXPORT_SYMBOL_GPL(rust_helper_get_current);
125 
126 void rust_helper_get_task_struct(struct task_struct *t)
127 {
128 	get_task_struct(t);
129 }
130 EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
131 
132 void rust_helper_put_task_struct(struct task_struct *t)
133 {
134 	put_task_struct(t);
135 }
136 EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
137 
138 /*
139  * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
140  * as the Rust `usize` type, so we can use it in contexts where Rust
141  * expects a `usize` like slice (array) indices. `usize` is defined to be
142  * the same as C's `uintptr_t` type (can hold any pointer) but not
143  * necessarily the same as `size_t` (can hold the size of any single
144  * object). Most modern platforms use the same concrete integer type for
145  * both of them, but in case we find ourselves on a platform where
146  * that's not true, fail early instead of risking ABI or
147  * integer-overflow issues.
148  *
149  * If your platform fails this assertion, it means that you are in
150  * danger of integer-overflow bugs (even if you attempt to remove
151  * `--size_t-is-usize`). It may be easiest to change the kernel ABI on
152  * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
153  * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
154  */
155 static_assert(
156 	sizeof(size_t) == sizeof(uintptr_t) &&
157 	__alignof__(size_t) == __alignof__(uintptr_t),
158 	"Rust code expects C `size_t` to match Rust `usize`"
159 );
160