Skip to content

Commit 5ff646c

Browse files
rust: task: add as_raw() to Task
Added new function `Task::as_raw()` which returns the raw pointer for the underlying task struct. I also refactored `Task` to instead use the newly created function instead of `self.0.get()` as I feel like `self.as_raw()` is more intuitive. Signed-off-by: Antonio Hickey <[email protected]>
1 parent 8f7e376 commit 5ff646c

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

rust/kernel/task.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,20 @@ impl Task {
124124
}
125125
}
126126

127+
/// Returns a raw pointer to the underlying C task struct.
128+
///
129+
/// # Safety
130+
///
131+
/// Callers must ensure that the returned pointer doesn't outlive the current task/thread.
132+
pub unsafe fn as_raw(&self) -> *mut bindings::task_struct {
133+
self.0.get()
134+
}
135+
127136
/// Returns the group leader of the given task.
128137
pub fn group_leader(&self) -> &Task {
129-
// SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
138+
// SAFETY: By the type invariant, we know that `self.as_raw()` is a valid task. Valid tasks always
130139
// have a valid group_leader.
131-
let ptr = unsafe { *ptr::addr_of!((*self.0.get()).group_leader) };
140+
let ptr = unsafe { *ptr::addr_of!((*self.as_raw()).group_leader) };
132141

133142
// SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
134143
// and given that a task has a reference to its group leader, we know it must be valid for
@@ -138,43 +147,43 @@ impl Task {
138147

139148
/// Returns the PID of the given task.
140149
pub fn pid(&self) -> Pid {
141-
// SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
150+
// SAFETY: By the type invariant, we know that `self.as_raw()` is a valid task. Valid tasks always
142151
// have a valid pid.
143-
unsafe { *ptr::addr_of!((*self.0.get()).pid) }
152+
unsafe { *ptr::addr_of!((*self.as_raw()).pid) }
144153
}
145154

146155
/// Returns the UID of the given task.
147156
pub fn uid(&self) -> Kuid {
148-
// SAFETY: By the type invariant, we know that `self.0` is valid.
149-
Kuid::from_raw(unsafe { bindings::task_uid(self.0.get()) })
157+
// SAFETY: By the type invariant, we know that `self.as_raw()` is valid.
158+
Kuid::from_raw(unsafe { bindings::task_uid(self.as_raw()) })
150159
}
151160

152161
/// Returns the effective UID of the given task.
153162
pub fn euid(&self) -> Kuid {
154-
// SAFETY: By the type invariant, we know that `self.0` is valid.
155-
Kuid::from_raw(unsafe { bindings::task_euid(self.0.get()) })
163+
// SAFETY: By the type invariant, we know that `self.as_raw()` is valid.
164+
Kuid::from_raw(unsafe { bindings::task_euid(self.as_raw()) })
156165
}
157166

158167
/// Determines whether the given task has pending signals.
159168
pub fn signal_pending(&self) -> bool {
160-
// SAFETY: By the type invariant, we know that `self.0` is valid.
161-
unsafe { bindings::signal_pending(self.0.get()) != 0 }
169+
// SAFETY: By the type invariant, we know that `self.as_raw()` is valid.
170+
unsafe { bindings::signal_pending(self.as_raw()) != 0 }
162171
}
163172

164173
/// Returns the given task's pid in the current pid namespace.
165174
pub fn pid_in_current_ns(&self) -> Pid {
166175
// SAFETY: Calling `task_active_pid_ns` with the current task is always safe.
167176
let namespace = unsafe { bindings::task_active_pid_ns(bindings::get_current()) };
168-
// SAFETY: We know that `self.0.get()` is valid by the type invariant.
169-
unsafe { bindings::task_tgid_nr_ns(self.0.get(), namespace) }
177+
// SAFETY: We know that `self.raw()` is valid by the type invariant.
178+
unsafe { bindings::task_tgid_nr_ns(self.as_raw(), namespace) }
170179
}
171180

172181
/// Wakes up the task.
173182
pub fn wake_up(&self) {
174-
// SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
183+
// SAFETY: By the type invariant, we know that `self.raw()` is non-null and valid.
175184
// And `wake_up_process` is safe to be called for any valid task, even if the task is
176185
// running.
177-
unsafe { bindings::wake_up_process(self.0.get()) };
186+
unsafe { bindings::wake_up_process(self.as_raw()) };
178187
}
179188
}
180189

0 commit comments

Comments
 (0)