From: Alvin Sun Date: Sun, 22 Nov 2026 20:50:21 +0800 Subject: [PATCH v7 02/10] rust: task: add task_size() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261122-tyr-ioctls-v7-2-3a2af624b992@linux.dev> References: <20261122-tyr-ioctls-v7-0-3a2af624b992@linux.dev> In-Reply-To: <20261122-tyr-ioctls-v7-0-3a2af624b992@linux.dev> To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Lyude Paul , David Airlie , Simona Vetter , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johan Hovold Cc: Sami Tolvanen , rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, driver-core@lists.linux.dev, Ke Sun , Alvin Sun X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795352551; l=2517; i=alvin.sun@linux.dev; s=20260317; h=from:subject:message-id; bh=ltCybRExPQSKXsCnz8UljjoTEH8sGi6OL1QQ7zSPhcQ=; b=MPmHqFSk+yRPtfW83Pc/pd82Q4DbwpLmiDzOi0fF6lOklYZd8u8u+Xam9npIlRYQXF+I6n1pH B8rf8k8DbUbCN4XFzWqSQ4gVBeCWMqacwuEOwjUjzM8RuLEZoJaNdgH X-Developer-Key: i=alvin.sun@linux.dev; a=ed25519; pk=CHcwQp8GSoj25V/L1ZWNSQjWp9eSIb0s9LKr0Nm3WuE= X-Endpoint-Received: by B4 Relay for alvin.sun@linux.dev/20260317 with auth_id=684 List-Id: B4 Relay Submissions From: Sami Tolvanen A DRM driver that lets userspace pick GPU virtual addresses needs the address space size of the calling task, so it can offer a GPU range that the CPU can map at the same addresses. TASK_SIZE_OF() is a macro, so reading it from Rust needs a helper. Add CurrentTask::task_size(). It is bound to the current task because TASK_SIZE_OF() falls back to TASK_SIZE on architectures that do not define it, and TASK_SIZE describes current. Signed-off-by: Sami Tolvanen [ Add the include, needed where TASK_SIZE is defined in (e.g. riscv). - Alvin ] Co-developed-by: Alvin Sun Signed-off-by: Alvin Sun --- rust/helpers/task.c | 6 ++++++ rust/kernel/task.rs | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/rust/helpers/task.c b/rust/helpers/task.c index b46b1433a67e8..b49bd40027154 100644 --- a/rust/helpers/task.c +++ b/rust/helpers/task.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include __rust_helper void rust_helper_might_resched(void) @@ -13,6 +14,11 @@ __rust_helper struct task_struct *rust_helper_get_current(void) return current; } +__rust_helper unsigned long rust_helper_current_task_size(void) +{ + return TASK_SIZE_OF(current); +} + __rust_helper void rust_helper_get_task_struct(struct task_struct *t) { get_task_struct(t); diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index a733816fd5dd4..1978f54fe5897 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -317,6 +317,18 @@ pub fn mm(&self) -> Option<&MmWithUser> { Some(unsafe { MmWithUser::from_raw(mm) }) } + /// Returns the size of the userspace address space of the current task. + /// + /// This is the first address above the userspace range, so it is smaller + /// for a task running in compat mode than for a native one. See + /// `TASK_SIZE_OF()` in [`include/linux/sched.h`](srctree/include/linux/sched.h). + #[doc(alias = "TASK_SIZE_OF")] + #[inline] + pub fn task_size(&self) -> usize { + // SAFETY: FFI call without preconditions, valid in any context. + unsafe { bindings::current_task_size() as usize } + } + /// Access the pid namespace of the current task. /// /// This function does not touch the refcount of the namespace or use RCU protection. -- 2.43.0