blob: 92d3c03ae1bd533870148e897fdea7956d74d27f [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 *
Thorsten Blum84373132024-04-11 22:54:28 +02007 * Even though Rust kernel modules should never use the bindings directly, some
Miguel Ojeda12f57722021-07-03 16:52:41 +02008 * 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.
Ariel Miculas917b2e02023-04-26 23:49:23 +030019 *
20 * Sorted alphabetically.
Miguel Ojeda12f57722021-07-03 16:52:41 +020021 */
22
Miguel Ojedaa66d7332023-07-18 07:27:51 +020023#include <kunit/test-bug.h>
Miguel Ojeda12f57722021-07-03 16:52:41 +020024#include <linux/bug.h>
25#include <linux/build_bug.h>
Danilo Krummricha674fef2024-06-18 17:48:34 +020026#include <linux/device.h>
Asahi Linac7e20fa2023-04-03 18:48:11 +090027#include <linux/err.h>
Gary Guod2e31152023-05-31 17:44:50 +000028#include <linux/errname.h>
Alice Ryhlfc6e66f2024-05-28 14:58:05 +000029#include <linux/gfp.h>
30#include <linux/highmem.h>
Wedson Almeida Filho6d20d622023-04-11 02:45:33 -030031#include <linux/mutex.h>
Ariel Miculas917b2e02023-04-26 23:49:23 +030032#include <linux/refcount.h>
Wedson Almeida Filho313c4282023-04-11 02:45:39 -030033#include <linux/sched/signal.h>
Kent Overstreet53ed0af2024-03-21 09:36:46 -070034#include <linux/slab.h>
Ariel Miculas917b2e02023-04-26 23:49:23 +030035#include <linux/spinlock.h>
Wedson Almeida Filho19096bc2023-03-26 00:57:38 -030036#include <linux/wait.h>
Alice Ryhl7324b882023-08-28 10:48:04 +000037#include <linux/workqueue.h>
Miguel Ojeda12f57722021-07-03 16:52:41 +020038
39__noreturn void rust_helper_BUG(void)
40{
41 BUG();
42}
43EXPORT_SYMBOL_GPL(rust_helper_BUG);
44
Wedson Almeida Filho1b580e72024-05-28 14:58:02 +000045unsigned long rust_helper_copy_from_user(void *to, const void __user *from,
46 unsigned long n)
47{
48 return copy_from_user(to, from, n);
49}
50EXPORT_SYMBOL_GPL(rust_helper_copy_from_user);
51
52unsigned long rust_helper_copy_to_user(void __user *to, const void *from,
53 unsigned long n)
54{
55 return copy_to_user(to, from, n);
56}
57EXPORT_SYMBOL_GPL(rust_helper_copy_to_user);
58
Wedson Almeida Filho6d20d622023-04-11 02:45:33 -030059void rust_helper_mutex_lock(struct mutex *lock)
60{
61 mutex_lock(lock);
62}
63EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
64
Wedson Almeida Filhoc6d917a2023-04-19 14:44:26 -030065void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
66 struct lock_class_key *key)
67{
68#ifdef CONFIG_DEBUG_SPINLOCK
69 __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
70#else
71 spin_lock_init(lock);
72#endif
73}
74EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
75
76void rust_helper_spin_lock(spinlock_t *lock)
77{
78 spin_lock(lock);
79}
80EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
81
82void rust_helper_spin_unlock(spinlock_t *lock)
83{
84 spin_unlock(lock);
85}
86EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
87
Wedson Almeida Filho19096bc2023-03-26 00:57:38 -030088void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
89{
90 init_wait(wq_entry);
91}
92EXPORT_SYMBOL_GPL(rust_helper_init_wait);
93
Wedson Almeida Filho313c4282023-04-11 02:45:39 -030094int rust_helper_signal_pending(struct task_struct *t)
95{
96 return signal_pending(t);
97}
98EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
99
Alice Ryhlfc6e66f2024-05-28 14:58:05 +0000100struct page *rust_helper_alloc_pages(gfp_t gfp_mask, unsigned int order)
101{
102 return alloc_pages(gfp_mask, order);
103}
104EXPORT_SYMBOL_GPL(rust_helper_alloc_pages);
105
106void *rust_helper_kmap_local_page(struct page *page)
107{
108 return kmap_local_page(page);
109}
110EXPORT_SYMBOL_GPL(rust_helper_kmap_local_page);
111
112void rust_helper_kunmap_local(const void *addr)
113{
114 kunmap_local(addr);
115}
116EXPORT_SYMBOL_GPL(rust_helper_kunmap_local);
117
Wedson Almeida Filho9dc04362022-12-28 06:03:40 +0000118refcount_t rust_helper_REFCOUNT_INIT(int n)
119{
120 return (refcount_t)REFCOUNT_INIT(n);
121}
122EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
123
124void rust_helper_refcount_inc(refcount_t *r)
125{
126 refcount_inc(r);
127}
128EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
129
130bool rust_helper_refcount_dec_and_test(refcount_t *r)
131{
132 return refcount_dec_and_test(r);
133}
134EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
135
Asahi Linac7e20fa2023-04-03 18:48:11 +0900136__force void *rust_helper_ERR_PTR(long err)
137{
138 return ERR_PTR(err);
139}
140EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
141
Sven Van Asbroeck752417b2023-04-03 18:48:14 +0900142bool rust_helper_IS_ERR(__force const void *ptr)
143{
144 return IS_ERR(ptr);
145}
146EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
147
148long rust_helper_PTR_ERR(__force const void *ptr)
149{
150 return PTR_ERR(ptr);
151}
152EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
153
Gary Guod2e31152023-05-31 17:44:50 +0000154const char *rust_helper_errname(int err)
155{
156 return errname(err);
157}
158EXPORT_SYMBOL_GPL(rust_helper_errname);
159
Wedson Almeida Filho8da7a2b2023-04-11 02:45:40 -0300160struct task_struct *rust_helper_get_current(void)
161{
162 return current;
163}
164EXPORT_SYMBOL_GPL(rust_helper_get_current);
165
Wedson Almeida Filho313c4282023-04-11 02:45:39 -0300166void rust_helper_get_task_struct(struct task_struct *t)
167{
168 get_task_struct(t);
169}
170EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
171
172void rust_helper_put_task_struct(struct task_struct *t)
173{
174 put_task_struct(t);
175}
176EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
177
Miguel Ojedaa66d7332023-07-18 07:27:51 +0200178struct kunit *rust_helper_kunit_get_current_test(void)
179{
180 return kunit_get_current_test();
181}
182EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);
183
Alice Ryhl7324b882023-08-28 10:48:04 +0000184void 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)
187{
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);
192 work->func = func;
193}
194EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);
195
Kent Overstreet53ed0af2024-03-21 09:36:46 -0700196void * __must_check __realloc_size(2)
197rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags)
198{
199 return krealloc(objp, new_size, flags);
200}
201EXPORT_SYMBOL_GPL(rust_helper_krealloc);
202
Miguel Ojeda12f57722021-07-03 16:52:41 +0200203/*
Aakash Sen Sharma08ab7862023-06-13 01:13:11 +0530204 * `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
Miguel Ojeda12f57722021-07-03 16:52:41 +0200209 * 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.
212 *
213 * If your platform fails this assertion, it means that you are in
Aakash Sen Sharma08ab7862023-06-13 01:13:11 +0530214 * 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
Miguel Ojeda12f57722021-07-03 16:52:41 +0200216 * 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`).
218 */
219static_assert(
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`"
223);
Andreas Hindborg3253aba2024-06-11 13:45:49 +0200224
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>
228
229void *rust_helper_blk_mq_rq_to_pdu(struct request *rq)
230{
231 return blk_mq_rq_to_pdu(rq);
232}
233EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_to_pdu);
234
235struct request *rust_helper_blk_mq_rq_from_pdu(void *pdu)
236{
237 return blk_mq_rq_from_pdu(pdu);
238}
239EXPORT_SYMBOL_GPL(rust_helper_blk_mq_rq_from_pdu);