Merge tag 'erofs-for-6.10-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / rust / helpers.c
CommitLineData
12f57721
MO
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 *
84373132 7 * Even though Rust kernel modules should never use the bindings directly, some
12f57721
MO
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.
917b2e00
AM
19 *
20 * Sorted alphabetically.
12f57721
MO
21 */
22
a66d733d 23#include <kunit/test-bug.h>
12f57721
MO
24#include <linux/bug.h>
25#include <linux/build_bug.h>
c7e20faa 26#include <linux/err.h>
d2e3115d 27#include <linux/errname.h>
6d20d629 28#include <linux/mutex.h>
917b2e00 29#include <linux/refcount.h>
313c4281 30#include <linux/sched/signal.h>
53ed0af4 31#include <linux/slab.h>
917b2e00 32#include <linux/spinlock.h>
19096bce 33#include <linux/wait.h>
7324b889 34#include <linux/workqueue.h>
12f57721
MO
35
36__noreturn void rust_helper_BUG(void)
37{
38 BUG();
39}
40EXPORT_SYMBOL_GPL(rust_helper_BUG);
41
6d20d629
WAF
42void rust_helper_mutex_lock(struct mutex *lock)
43{
44 mutex_lock(lock);
45}
46EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
47
c6d917a4
WAF
48void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
49 struct lock_class_key *key)
50{
51#ifdef CONFIG_DEBUG_SPINLOCK
52 __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
53#else
54 spin_lock_init(lock);
55#endif
56}
57EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
58
59void rust_helper_spin_lock(spinlock_t *lock)
60{
61 spin_lock(lock);
62}
63EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
64
65void rust_helper_spin_unlock(spinlock_t *lock)
66{
67 spin_unlock(lock);
68}
69EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
70
19096bce
WAF
71void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
72{
73 init_wait(wq_entry);
74}
75EXPORT_SYMBOL_GPL(rust_helper_init_wait);
76
313c4281
WAF
77int rust_helper_signal_pending(struct task_struct *t)
78{
79 return signal_pending(t);
80}
81EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
82
9dc04365
WAF
83refcount_t rust_helper_REFCOUNT_INIT(int n)
84{
85 return (refcount_t)REFCOUNT_INIT(n);
86}
87EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
88
89void rust_helper_refcount_inc(refcount_t *r)
90{
91 refcount_inc(r);
92}
93EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
94
95bool rust_helper_refcount_dec_and_test(refcount_t *r)
96{
97 return refcount_dec_and_test(r);
98}
99EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
100
c7e20faa
AL
101__force void *rust_helper_ERR_PTR(long err)
102{
103 return ERR_PTR(err);
104}
105EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
106
752417b3
SVA
107bool rust_helper_IS_ERR(__force const void *ptr)
108{
109 return IS_ERR(ptr);
110}
111EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
112
113long rust_helper_PTR_ERR(__force const void *ptr)
114{
115 return PTR_ERR(ptr);
116}
117EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
118
d2e3115d
GG
119const char *rust_helper_errname(int err)
120{
121 return errname(err);
122}
123EXPORT_SYMBOL_GPL(rust_helper_errname);
124
8da7a2b7
WAF
125struct task_struct *rust_helper_get_current(void)
126{
127 return current;
128}
129EXPORT_SYMBOL_GPL(rust_helper_get_current);
130
313c4281
WAF
131void rust_helper_get_task_struct(struct task_struct *t)
132{
133 get_task_struct(t);
134}
135EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
136
137void rust_helper_put_task_struct(struct task_struct *t)
138{
139 put_task_struct(t);
140}
141EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
142
a66d733d
MO
143struct kunit *rust_helper_kunit_get_current_test(void)
144{
145 return kunit_get_current_test();
146}
147EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);
148
7324b889
AR
149void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
150 bool onstack, const char *name,
151 struct lock_class_key *key)
152{
153 __init_work(work, onstack);
154 work->data = (atomic_long_t)WORK_DATA_INIT();
155 lockdep_init_map(&work->lockdep_map, name, key, 0);
156 INIT_LIST_HEAD(&work->entry);
157 work->func = func;
158}
159EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);
160
53ed0af4
KO
161void * __must_check __realloc_size(2)
162rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
163{
164 return krealloc(objp, new_size, flags);
165}
166EXPORT_SYMBOL_GPL(rust_helper_krealloc);
167
12f57721 168/*
08ab7865
ASS
169 * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
170 * use it in contexts where Rust expects a `usize` like slice (array) indices.
171 * `usize` is defined to be the same as C's `uintptr_t` type (can hold any
172 * pointer) but not necessarily the same as `size_t` (can hold the size of any
173 * single object). Most modern platforms use the same concrete integer type for
12f57721
MO
174 * both of them, but in case we find ourselves on a platform where
175 * that's not true, fail early instead of risking ABI or
176 * integer-overflow issues.
177 *
178 * If your platform fails this assertion, it means that you are in
08ab7865
ASS
179 * danger of integer-overflow bugs (even if you attempt to add
180 * `--no-size_t-is-usize`). It may be easiest to change the kernel ABI on
12f57721
MO
181 * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
182 * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
183 */
184static_assert(
185 sizeof(size_t) == sizeof(uintptr_t) &&
186 __alignof__(size_t) == __alignof__(uintptr_t),
187 "Rust code expects C `size_t` to match Rust `usize`"
188);