1 // SPDX-License-Identifier: GPL-2.0
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.
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.
17 * All symbols are exported as GPL-only to guarantee no GPL-only feature is
18 * accidentally exposed.
20 * Sorted alphabetically.
23 #include <kunit/test-bug.h>
24 #include <linux/bug.h>
25 #include <linux/build_bug.h>
26 #include <linux/device.h>
27 #include <linux/err.h>
28 #include <linux/errname.h>
29 #include <linux/gfp.h>
30 #include <linux/highmem.h>
31 #include <linux/mutex.h>
32 #include <linux/refcount.h>
33 #include <linux/sched/signal.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/wait.h>
37 #include <linux/workqueue.h>
39 __noreturn void rust_helper_BUG(void)
43 EXPORT_SYMBOL_GPL(rust_helper_BUG);
45 unsigned long rust_helper_copy_from_user(void *to, const void __user *from,
48 return copy_from_user(to, from, n);
50 EXPORT_SYMBOL_GPL(rust_helper_copy_from_user);
52 unsigned long rust_helper_copy_to_user(void __user *to, const void *from,
55 return copy_to_user(to, from, n);
57 EXPORT_SYMBOL_GPL(rust_helper_copy_to_user);
59 void rust_helper_mutex_lock(struct mutex *lock)
63 EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
65 void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
66 struct lock_class_key *key)
68 #ifdef CONFIG_DEBUG_SPINLOCK
69 __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
74 EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
76 void rust_helper_spin_lock(spinlock_t *lock)
80 EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
82 void rust_helper_spin_unlock(spinlock_t *lock)
86 EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
88 void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
92 EXPORT_SYMBOL_GPL(rust_helper_init_wait);
94 int rust_helper_signal_pending(struct task_struct *t)
96 return signal_pending(t);
98 EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
100 struct page *rust_helper_alloc_pages(gfp_t gfp_mask, unsigned int order)
102 return alloc_pages(gfp_mask, order);
104 EXPORT_SYMBOL_GPL(rust_helper_alloc_pages);
106 void *rust_helper_kmap_local_page(struct page *page)
108 return kmap_local_page(page);
110 EXPORT_SYMBOL_GPL(rust_helper_kmap_local_page);
112 void rust_helper_kunmap_local(const void *addr)
116 EXPORT_SYMBOL_GPL(rust_helper_kunmap_local);
118 refcount_t rust_helper_REFCOUNT_INIT(int n)
120 return (refcount_t)REFCOUNT_INIT(n);
122 EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
124 void rust_helper_refcount_inc(refcount_t *r)
128 EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
130 bool rust_helper_refcount_dec_and_test(refcount_t *r)
132 return refcount_dec_and_test(r);
134 EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
136 __force void *rust_helper_ERR_PTR(long err)
140 EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
142 bool rust_helper_IS_ERR(__force const void *ptr)
146 EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
148 long rust_helper_PTR_ERR(__force const void *ptr)
152 EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
154 const char *rust_helper_errname(int err)
158 EXPORT_SYMBOL_GPL(rust_helper_errname);
160 struct task_struct *rust_helper_get_current(void)
164 EXPORT_SYMBOL_GPL(rust_helper_get_current);
166 void rust_helper_get_task_struct(struct task_struct *t)
170 EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
172 void rust_helper_put_task_struct(struct task_struct *t)
176 EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
178 struct kunit *rust_helper_kunit_get_current_test(void)
180 return kunit_get_current_test();
182 EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);
184 void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
185 bool onstack, const char *name,
186 struct lock_class_key *key)
188 __init_work(work, onstack);
189 work->data = (atomic_long_t)WORK_DATA_INIT();
190 lockdep_init_map(&work->lockdep_map, name, key, 0);
191 INIT_LIST_HEAD(&work->entry);
194 EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);
196 void * __must_check __realloc_size(2)
197 rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
199 return krealloc(objp, new_size, flags);
201 EXPORT_SYMBOL_GPL(rust_helper_krealloc);
204 * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
205 * use it in contexts where Rust expects a `usize` like slice (array) indices.
206 * `usize` is defined to be the same as C's `uintptr_t` type (can hold any
207 * pointer) but not necessarily the same as `size_t` (can hold the size of any
208 * single object). Most modern platforms use the same concrete integer type for
209 * both of them, but in case we find ourselves on a platform where
210 * that's not true, fail early instead of risking ABI or
211 * integer-overflow issues.
213 * If your platform fails this assertion, it means that you are in
214 * danger of integer-overflow bugs (even if you attempt to add
215 * `--no-size_t-is-usize`). It may be easiest to change the kernel ABI on
216 * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
217 * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
220 sizeof(size_t) == sizeof(uintptr_t) &&
221 __alignof__(size_t) == __alignof__(uintptr_t),
222 "Rust code expects C `size_t` to match Rust `usize`"
225 // This will soon be moved to a separate file, so no need to merge with above.
226 #include <linux/blk-mq.h>
227 #include <linux/blkdev.h>
229 void *rust_helper_blk_mq_rq_to_pdu(struct request *rq)
231 return blk_mq_rq_to_pdu(rq);
233 EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_to_pdu);
235 struct request *rust_helper_blk_mq_rq_from_pdu(void *pdu)
237 return blk_mq_rq_from_pdu(pdu);
239 EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_from_pdu);