]> Git Repo - J-linux.git/blob - rust/helpers.c
docs: rust: quick-start: add section on Linux distributions
[J-linux.git] / rust / helpers.c
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 the bindings directly, 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  * Sorted alphabetically.
21  */
22
23 #include <kunit/test-bug.h>
24 #include <linux/bug.h>
25 #include <linux/build_bug.h>
26 #include <linux/err.h>
27 #include <linux/errname.h>
28 #include <linux/gfp.h>
29 #include <linux/highmem.h>
30 #include <linux/mutex.h>
31 #include <linux/refcount.h>
32 #include <linux/sched/signal.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/wait.h>
36 #include <linux/workqueue.h>
37
38 __noreturn void rust_helper_BUG(void)
39 {
40         BUG();
41 }
42 EXPORT_SYMBOL_GPL(rust_helper_BUG);
43
44 unsigned long rust_helper_copy_from_user(void *to, const void __user *from,
45                                          unsigned long n)
46 {
47         return copy_from_user(to, from, n);
48 }
49 EXPORT_SYMBOL_GPL(rust_helper_copy_from_user);
50
51 unsigned long rust_helper_copy_to_user(void __user *to, const void *from,
52                                        unsigned long n)
53 {
54         return copy_to_user(to, from, n);
55 }
56 EXPORT_SYMBOL_GPL(rust_helper_copy_to_user);
57
58 void rust_helper_mutex_lock(struct mutex *lock)
59 {
60         mutex_lock(lock);
61 }
62 EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
63
64 void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
65                                   struct lock_class_key *key)
66 {
67 #ifdef CONFIG_DEBUG_SPINLOCK
68         __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
69 #else
70         spin_lock_init(lock);
71 #endif
72 }
73 EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
74
75 void rust_helper_spin_lock(spinlock_t *lock)
76 {
77         spin_lock(lock);
78 }
79 EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
80
81 void rust_helper_spin_unlock(spinlock_t *lock)
82 {
83         spin_unlock(lock);
84 }
85 EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
86
87 void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
88 {
89         init_wait(wq_entry);
90 }
91 EXPORT_SYMBOL_GPL(rust_helper_init_wait);
92
93 int rust_helper_signal_pending(struct task_struct *t)
94 {
95         return signal_pending(t);
96 }
97 EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
98
99 struct page *rust_helper_alloc_pages(gfp_t gfp_mask, unsigned int order)
100 {
101         return alloc_pages(gfp_mask, order);
102 }
103 EXPORT_SYMBOL_GPL(rust_helper_alloc_pages);
104
105 void *rust_helper_kmap_local_page(struct page *page)
106 {
107         return kmap_local_page(page);
108 }
109 EXPORT_SYMBOL_GPL(rust_helper_kmap_local_page);
110
111 void rust_helper_kunmap_local(const void *addr)
112 {
113         kunmap_local(addr);
114 }
115 EXPORT_SYMBOL_GPL(rust_helper_kunmap_local);
116
117 refcount_t rust_helper_REFCOUNT_INIT(int n)
118 {
119         return (refcount_t)REFCOUNT_INIT(n);
120 }
121 EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
122
123 void rust_helper_refcount_inc(refcount_t *r)
124 {
125         refcount_inc(r);
126 }
127 EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
128
129 bool rust_helper_refcount_dec_and_test(refcount_t *r)
130 {
131         return refcount_dec_and_test(r);
132 }
133 EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
134
135 __force void *rust_helper_ERR_PTR(long err)
136 {
137         return ERR_PTR(err);
138 }
139 EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
140
141 bool rust_helper_IS_ERR(__force const void *ptr)
142 {
143         return IS_ERR(ptr);
144 }
145 EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
146
147 long rust_helper_PTR_ERR(__force const void *ptr)
148 {
149         return PTR_ERR(ptr);
150 }
151 EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
152
153 const char *rust_helper_errname(int err)
154 {
155         return errname(err);
156 }
157 EXPORT_SYMBOL_GPL(rust_helper_errname);
158
159 struct task_struct *rust_helper_get_current(void)
160 {
161         return current;
162 }
163 EXPORT_SYMBOL_GPL(rust_helper_get_current);
164
165 void rust_helper_get_task_struct(struct task_struct *t)
166 {
167         get_task_struct(t);
168 }
169 EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
170
171 void rust_helper_put_task_struct(struct task_struct *t)
172 {
173         put_task_struct(t);
174 }
175 EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
176
177 struct kunit *rust_helper_kunit_get_current_test(void)
178 {
179         return kunit_get_current_test();
180 }
181 EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);
182
183 void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
184                                     bool onstack, const char *name,
185                                     struct lock_class_key *key)
186 {
187         __init_work(work, onstack);
188         work->data = (atomic_long_t)WORK_DATA_INIT();
189         lockdep_init_map(&work->lockdep_map, name, key, 0);
190         INIT_LIST_HEAD(&work->entry);
191         work->func = func;
192 }
193 EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);
194
195 void * __must_check __realloc_size(2)
196 rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
197 {
198         return krealloc(objp, new_size, flags);
199 }
200 EXPORT_SYMBOL_GPL(rust_helper_krealloc);
201
202 /*
203  * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
204  * use it in contexts where Rust expects a `usize` like slice (array) indices.
205  * `usize` is defined to be the same as C's `uintptr_t` type (can hold any
206  * pointer) but not necessarily the same as `size_t` (can hold the size of any
207  * single object). Most modern platforms use the same concrete integer type for
208  * both of them, but in case we find ourselves on a platform where
209  * that's not true, fail early instead of risking ABI or
210  * integer-overflow issues.
211  *
212  * If your platform fails this assertion, it means that you are in
213  * danger of integer-overflow bugs (even if you attempt to add
214  * `--no-size_t-is-usize`). It may be easiest to change the kernel ABI on
215  * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
216  * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
217  */
218 static_assert(
219         sizeof(size_t) == sizeof(uintptr_t) &&
220         __alignof__(size_t) == __alignof__(uintptr_t),
221         "Rust code expects C `size_t` to match Rust `usize`"
222 );
This page took 0.037969 seconds and 4 git commands to generate.