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