Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp748848rdb; Fri, 2 Feb 2024 02:56:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IH6Bc7aawSQiITuCGcfDb+wK8aKuecbWNVZN+AzFt+KpENRvhBvOGeHfMaUyksQIOvdy7da X-Received: by 2002:a05:622a:a:b0:42a:ad63:6c52 with SMTP id x10-20020a05622a000a00b0042aad636c52mr9060379qtw.44.1706871400485; Fri, 02 Feb 2024 02:56:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706871400; cv=pass; d=google.com; s=arc-20160816; b=uMcVgbf36PSueTuicbGx6CfJzJjWDjQFnN+dNCXkSoQ6ZlPNusmbY6aa3xmXizwrIX 4GKZrMu/ZXccb5bssi97gvQQ7w5rKTeKg26R+AeqTNHZ7gzXuiU/PRTVF4j6vMUl+5+h KBLM31qTFleUKERiL7RzkR74xt5+Vzwulb5NcWuEOvgSNP6iVyxyZtxdTVHfOdAZMrgn h9S8jz8IeVJnpKWQmERhtFmqI2T+I8Zpk2MQ9CY0YmAKUHooZv7XClNj3vMWKVNxjXWH lCjmtL4CxiwgNq6C/Z7aGTSauVGXNXdYu7KdO1Gu4oRVWcUCH/BTx0g0ESQ05hzqlDQX dFtA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=/ApmdViD79PTF2wWIJgz6LFiyNcf37NPzHjT72tU4L0=; fh=g0S7b4lwYxZRKFW/YmkiN/0w6EVjqdu8tRwCRXLAwDU=; b=i2QXwXYm3i+ku54d8AU/54NQ8G+So/J2/AHN8n3NGZtGS7R1YpWIV+Sb6qy86FI/j+ zFDnxceZeFxCMyXw5VksueOuYFtUvZOfsONFeoeY2p6idzimDiZhalR/gvQAFCEhtB0w dxBqsTT13pnhL5kcyEteyAgEixC5k+F1x6/o7/AFygRUHGHdc6Su6lEIfRzQsgps+iS2 bdPZIoewZWwS1Tm5oPzwcUge83fqh/3/E4NPYjLKd92U883CpGzcM5K4bwXdmSFIYLgc uw3Ketdof7piXfHIX5nfsEGNDNyR1UPbg4olvn9r4KJF2EObBQ21oPFzs8tN8orzjQo0 Fthw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xFIj4sol; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-49707-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49707-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVrbL5Y9ERK8Ple8jknr6zVqaAzGHkAb2tL0lbnwvp9Q5q4o1t6XXorpZ3JAJrrHpMGyBegw3DIx93qEGncPy5zR4zIeR+79Zz7SeSoQw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t8-20020a05622a148800b0042a9bd3f95asi1790694qtx.210.2024.02.02.02.56.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 02:56:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-49707-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xFIj4sol; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-49707-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49707-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id EBD4C1C2563F for ; Fri, 2 Feb 2024 10:56:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2CFC913E21C; Fri, 2 Feb 2024 10:55:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xFIj4sol" Received: from mail-lf1-f73.google.com (mail-lf1-f73.google.com [209.85.167.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 905C413DBA0 for ; Fri, 2 Feb 2024 10:55:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706871355; cv=none; b=gz7bLKGMuYYbOq4TMLF89XRXFYYbKz5bbiGwD1PGxQJ78ZkkTPZOF7VZz3+FDmzwkgXmLDHW0AUscP4+SQK1xuJRJQpFojBC2sHdWabT4Dx8T4wekprVOhe+jaE55RwXMCsile24WpEfRjBU/0AMoiMHt61YtyTNxKfv48rUB3I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706871355; c=relaxed/simple; bh=Bjk/OB+Iji4BInn1vTv4uXEE+/4bkjfr3M+GCsCoH3I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JBN9ijmDp2RgKhoYUuomm1+L5YzUHxc9BGgst0B++HtqLKx4xZ98lr2th4xdZI0Niw7hFV+w2/9n1EKljx+hzfRIuXgW+QEykO72aGFgOJ8e3ro2joslozn/P/j+LyoszSd5lOr5MVV6GL87vX/RKrDcF2UOOpI++8ToANTtOwk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xFIj4sol; arc=none smtp.client-ip=209.85.167.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Received: by mail-lf1-f73.google.com with SMTP id 2adb3069b0e04-51032b2fb9fso1486012e87.1 for ; Fri, 02 Feb 2024 02:55:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706871352; x=1707476152; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/ApmdViD79PTF2wWIJgz6LFiyNcf37NPzHjT72tU4L0=; b=xFIj4solmwkyqz0Hx99esWgEI6m0K4Gdnth+taPK7Jv+Z9ioN22ctrf00Wdm0JrTOX Nbg97B2yhsyRVC7d1QeJtE2ACK+h7xbZg8JownLqJM1UE2ZvLVSIy9bHgFqoWHuu32ms xx13uoxQK887TSE3WX1ytdIVChlE4/c7kNWx1U8I59/3tUJpVhFMwgx9AeHqR545l1qJ A+5FAO4h8ckU/KXSODxryyUjjVc/p8rwV4WxzRGfXjKLCURiDFUhxtOG6wm1yCdbyB8N uRvGN1VLnpY0APvuQGfRFoLznYc6w3mekCVrkTUKTuK3T/fjWP/wJK2UNaFuMGpNT64a Z84Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706871352; x=1707476152; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/ApmdViD79PTF2wWIJgz6LFiyNcf37NPzHjT72tU4L0=; b=ekqNLOW8u1TSkKpdpmpjaeej/yl1ZP1mbdN3ytrA7TCFhlEDdlJvM+IMWw3Liu75+5 Z434/vsLYyda/iVefzKZ8MaJROC0RCYo4qMy+BImhGmfTajTPzipeMETlMK1yXIUG4OF aSIeOUgwOzHM+NV+KPOmDkyQsJozE4WLCoW4Ni9XnhkBFmscalodE3bfgeeHLCzzPc9c ZyG7Q6tYoqelMahf6EYASF4RajCW9PYTGIWdW34uPTvCkV2VHi/C3jyg3EioOzgALUAP NhLZ6GoO2DnRhn8ySUay+Sh1zayqsWrIjwEObMYvoAqagoa88nCMgNYv1KJiO8D62M/a O6Fw== X-Gm-Message-State: AOJu0Yzz3rNcJcwAFjWy7kklbMKcHEFZ2tKhLWs0JKutrebCR6t5GeTH RqwWPHE/3Fcl8CmKZZkZmgsIqMch0u4n2NYOri3nu0eKbYQmIGXNaaMSbtQCpibbe3uKhKBUzvw 9W4+ko1JzYA+BBA== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:a449:0:b0:2d0:6086:57f7 with SMTP id v9-20020a2ea449000000b002d0608657f7mr8753ljn.2.1706871351660; Fri, 02 Feb 2024 02:55:51 -0800 (PST) Date: Fri, 2 Feb 2024 10:55:36 +0000 In-Reply-To: <20240202-alice-file-v4-0-fc9c2080663b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240202-alice-file-v4-0-fc9c2080663b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2292; i=aliceryhl@google.com; h=from:subject:message-id; bh=Bjk/OB+Iji4BInn1vTv4uXEE+/4bkjfr3M+GCsCoH3I=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlvMjJckMPHmLhUIiPgXUpw7gM92pGwvmvNEzb9 4LAYK2UX5iJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZbzIyQAKCRAEWL7uWMY5 RrAsD/4jSpaOcEIG9sCDcWDdJX+UN94jVByiIBv6iGusfSlTy/zrfh2/niLbdxLt5ihG79Kvim1 Vy/8VkquGO1Rm1Ff4oAQx9W2waylkLTFnV8IHA18le/kK9Y0P1sUSny3CO4anLD564bXrtBaxZr Fk8CTX9DbPsZCXw5aacUEFUKRk3LI5ZXFRLnKXiACAYr17InVZKyoe+UoiRvalbbzEJKqXbWlzw 352wpnmE2fcf5lWm8JxIj350VJ5YO5jmq9blleXBucfcm5Jhj5yPnfmkj4bx4FR4TEbh/Onh/HK cs0WE02UazElXGcb1W0zjVRW84gxA64KGuZe2cijY0HwL9fEH4w7Wwe/xCxDUjvq2sFkoOalGgp Zzrb/uMoO0is2D3bHXwFGx4gZ5TB+6ungdBIEUWLqb5ZZbqR2MadBJQ6+S4uKHhXVsDLCpXrLVs vium6PbmOAAGWv3R0YD9art/w9pP1JdtTXI3pGPNXt9wcbiq8bdIQ+znCFJG9BKNjuxgvDZcYu9 KTXOk4/+36IgaJm2BI8QJfdwriLQ7Y9Ol/0EzcDbBiVvokUrzJoUSpD/R1Qy7ldBOzuInua0TjX ECpBuyuQYXhjFIBTTLFWmkzM5HHK+yvWlvjaY7F+TKgyUDFyZx/uEE2D05lqho6PPLBtt2SdEHx kvILMP8qnvciJBA== X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240202-alice-file-v4-2-fc9c2080663b@google.com> Subject: [PATCH v4 2/9] rust: task: add `Task::current_raw` From: Alice Ryhl To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Peter Zijlstra , Alexander Viro , Christian Brauner , Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Dan Williams , Kees Cook , Matthew Wilcox , Thomas Gleixner Cc: Daniel Xu , Alice Ryhl , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Introduces a safe function for getting a raw pointer to the current task. When writing bindings that need to access the current task, it is often more convenient to call a method that directly returns a raw pointer than to use the existing `Task::current` method. However, the only way to do that is `bindings::get_current()` which is unsafe since it calls into C. By introducing `Task::current_raw()`, it becomes possible to obtain a pointer to the current task without using unsafe. Link: https://lore.kernel.org/all/CAH5fLgjT48X-zYtidv31mox3C4_Ogoo_2cBOCmX0Ang3tAgGHA@mail.gmail.com/ Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/task.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 148a4f4eb7a8..b579367fb923 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -97,6 +97,15 @@ unsafe impl Sync for Task {} type Pid = bindings::pid_t; impl Task { + /// Returns a raw pointer to the current task. + /// + /// It is up to the user to use the pointer correctly. + #[inline] + pub fn current_raw() -> *mut bindings::task_struct { + // SAFETY: Getting the current pointer is always safe. + unsafe { bindings::get_current() } + } + /// Returns a task reference for the currently executing task/thread. /// /// The recommended way to get the current task/thread is to use the @@ -119,14 +128,12 @@ pub unsafe fn current() -> impl Deref { } } - // SAFETY: Just an FFI call with no additional safety requirements. - let ptr = unsafe { bindings::get_current() }; - + let current = Task::current_raw(); TaskRef { // SAFETY: If the current thread is still running, the current task is valid. Given // that `TaskRef` is not `Send`, we know it cannot be transferred to another thread // (where it could potentially outlive the caller). - task: unsafe { &*ptr.cast() }, + task: unsafe { &*current.cast() }, _not_send: NotThreadSafe, } } -- 2.43.0.594.gd9cf4e227d-goog