From patchwork Thu Jan 18 14:36:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 189230 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp384448dyb; Thu, 18 Jan 2024 06:39:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IEXwCVmwzaSiXoM2LJQ4XQ7/4JgPJbdM8BWyOQQLg7kcCoLRb66qWYu1CjXBiSkjyFv1Pho X-Received: by 2002:a05:6402:b9e:b0:559:e789:dbfc with SMTP id cf30-20020a0564020b9e00b00559e789dbfcmr405470edb.98.1705588782860; Thu, 18 Jan 2024 06:39:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705588782; cv=pass; d=google.com; s=arc-20160816; b=P0y0h8xXbVI+2Sr9GxWKFKq6cH1S85IGyf8VluWITY5wZ44LSXOJ+u89376OGcmwYd sTLFZnmFyJErMP603GE0Myc/OJ+vmnQ9hcjwscHQ35H5RwbETpdbvo80ILKCRx8//97O gsbzEtQad8LreeSRyQJvDkJ1NK0D7/ey52WwneBmXWhbwifAtY1W24Tt05MEVPYS2iSp GrJPvpbuGSU72glyf5f+7le3XNRsrDPjDytMLJEilGtUx4IJy1zm2ffzAiebkO4JRDbL 4cCNPpoT+S7uPSqjwT7MKUBe2bGk5rsQiDMZwmO+7SvxxHTCMWbRWPzOFw55LXicyAJ0 d2Cg== 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=uzVko5ey5XomiaTP/3j+Aylzt4fKZA9AvfFuwkATRlQ=; fh=OP+vvG9ih/wBlPG6J6WwyVN77+HGWDbQuR6Uf4lsYyg=; b=kRQ4KK1yhYGadyy3Pj0vATezRbnctK9kd6vNupDP10JymTsqlMochslM0N6Mb6sDbd gwJncyqVxHTTQXCeEcmoG2qmA+jXFI5HHqMrJkFm+CI+RZ4De7HUO5pVV2xjz8F7PQ3t rAzWNJn0mFt9qDIn3TIe30p5qSOQbJKoH1gSpat3Ns5s1PttDJyQCU7ZFFFq2EcBihhJ nTJN2giQHaC/k3bPLc1+0PT0TXDUZyO2bhopu2Hr6WSz7qehe36BuOIQMdvVSMznJcK+ Pq9BHoZYWOk1HrWLC9Zz2cAhU6mtU26WKheFT0tRV0na1w3wS6Sf5RseJBFmuE2xH5u4 /rtQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AthP+9F8; 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-30207-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30207-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id en13-20020a056402528d00b00558740d4f83si7111747edb.101.2024.01.18.06.39.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 06:39:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-30207-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AthP+9F8; 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-30207-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30207-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 515491F23E2D for ; Thu, 18 Jan 2024 14:39:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D21E2D03D; Thu, 18 Jan 2024 14:37:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AthP+9F8" 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 E1E892C847 for ; Thu, 18 Jan 2024 14:37:10 +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=1705588632; cv=none; b=rh64uWO2Tk2XNbNSdST1Im96ZWF9gxAweP5fgZSm5HvRSISALEM/dsQ0XRX6IUffnOfxk242XN0+eypxLsXVidwvprsQdDth3CDfS0ppZ3DwMB0WPQ2Cm8NnkKZYx539aV4Ge5tku3M22bwZGUPeIEmHsslPVszDM6lO3F3TM00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705588632; c=relaxed/simple; bh=VXii4VQu3CEARTA//TXCHQIIjHMrXvbh1ihXw7gxrpE=; h=Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Google-Smtp-Source:X-Received:Date: In-Reply-To:Mime-Version:References:X-Developer-Key: X-Developer-Signature:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; b=rhJuxNHtqlIKFRCUF56+Jw0+5zFFvaOZ9qQICJOFRdP23gBxAIJaORNwiobNOqiAwAZe4gwU3VIkCr162GREaAuT0q1gZgLVP62R6Vb7eaQb0g3FN6Bu0rCv/JVY7qEBkdZAj4Zql9Lo6vUe19oyJcX87RFk3N13X2T1gpLbe+Y= 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=AthP+9F8; 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-50e73a3a280so8731831e87.3 for ; Thu, 18 Jan 2024 06:37:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705588629; x=1706193429; 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=uzVko5ey5XomiaTP/3j+Aylzt4fKZA9AvfFuwkATRlQ=; b=AthP+9F8Cv7XJAh0jqkXGp+bUtR+oa68RlHq8DpxraDmMIkr7fhbfyZ+lfzJv5FIMk nm05ItDqVYiHoUounpGaiCioHLWBNGuZOLaK0Y/29ocNMQkxe1qhwoKAN0q5HrQjbxZY 4EZi2j3x1si96F6nDOqEWEG5geaHHub0DeuxbRyVc1DJGYTU5IODRDJdIf5wQ7pHYLv1 kNpSseVG3FQE+l0Akds0r5+L2LagQLFlTjdtnPnplElruax06cqLl7rE/P9491gDksO9 rRJYW6Ki9YdUevBizHR89fxckEvpDyUOwZm/jjTKW8yPyjy5X2R7s12UHofEPmoQC53A paPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705588629; x=1706193429; 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=uzVko5ey5XomiaTP/3j+Aylzt4fKZA9AvfFuwkATRlQ=; b=wJBFmZuKUxpwzqsPA6U9tjz+osLzPYkY6icY1JOBXFMVyFUGE6SYak8J8S/UQGFHu/ XNH6obhnDmbFQlXC0zyIDX7fC64VaXqzgwZkvi6tvRUbuqRkHO6D8JqJTHUIs0J/9cOb LGWMCGkiFPvmDxw1dYpSUcllYWF6bJtP/T7ole8WAimqmtmySo5/eDWEwi0CSUIUz46n +WM0nZr0tFQkg7iUFslOhmvlpN+OSRCfMP/OLFiyWmxhc0QifmE/0O+VAcyhZK3LlFOP xVdMogT+5VBUaaTsA/aUP7YEVD0kpqaq5uRPWdTYz71hJtjCKHfUi++hhf6aXqa2iWfQ l7oQ== X-Gm-Message-State: AOJu0YwzXPuAPm7ncS75RKj1ls/9HGYzbEEC9jhi+UFR/kDUNivPxxwG YD+EnTm88sgrWKSkhXQCoEDihlxsIwoStCKfDWZ7/5zrG3CWp1dESCQt9pZQ6gOoe9mwgDKJt36 YbUsprbS+Cj+waQ== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:1326:b0:50f:14fd:5a2 with SMTP id x38-20020a056512132600b0050f14fd05a2mr1242lfu.9.1705588629212; Thu, 18 Jan 2024 06:37:09 -0800 (PST) Date: Thu, 18 Jan 2024 14:36:47 +0000 In-Reply-To: <20240118-alice-file-v3-0-9694b6f9580c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240118-alice-file-v3-0-9694b6f9580c@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2220; i=aliceryhl@google.com; h=from:subject; bh=VXii4VQu3CEARTA//TXCHQIIjHMrXvbh1ihXw7gxrpE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlqTHPE2OQ5MxNmkDx/d0OyvyE9y0jK5TyOb1gz zrC083CUEyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZakxzwAKCRAEWL7uWMY5 RjpdEACVtZzvrDY55UxSJ0r3VfIxbuH2lJ60e5lYPYESp/NSHZUevhadVVJ5vNaqGItTbasjvh1 tGN5hPIH0oOAfcQsOueBMQHjqksSSKFZDTtPMJWj020IPO38EU7p+4nmFMCIb+jaImA97N+1DDW A/MR6DzqWFkHMa1/+Xe/Zc4VgvPz5+SWEiCR0jU2im3moJN1uX8ZqHU51N9EhAo0VBkT8nkYOyI bCDJrRgssbd663cT0mZhWamzB8IiQYOTgGMmrOZ0XwBxVQsVFKK8apkEI8ApgNyat+Co9T6p1cF wkzcoRcGLHp7298srMnW5NNnvwpnCCGaP7CzswE7C9UD6ghCiPEa/55CT0UoOWaZ4+JXZHnvq4j Igre90ckd2KIf+IK1g6j8r1QelcY3EOI0xFQzO4inFl1luCvZ7/C5hez22JzuBzjgxU/rf3LfXA eoWczckrhL9h5W9YYcCgJcdeclNGH9gs6bitekVktpOadWm9GTk1aq3O1yMOHuVDqq/AnUlyKiJ QZvuEWyK51mlWtYlf7TCjiHYanRUrnRmF6e+iNWQkE/3/sIf/X0P+izYMzfgLlltS5tTC1Dn3ct XctOVPFmJAjZf4gWlkQ6goBCTyyda6IjbMK0zeVA/+X162OA8vdYa3lL30+c0L/bfjUO6ZJOt9p SHBCBHHndkwyigg== X-Mailer: git-send-email 2.43.0.381.gb435a96ce8-goog Message-ID: <20240118-alice-file-v3-6-9694b6f9580c@google.com> Subject: [PATCH v3 6/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_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Peter Zijlstra , Alexander Viro , Christian Brauner , Greg Kroah-Hartman , " =?utf-8?q?Arve_Hj=C3=B8n?= =?utf-8?q?nev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan Cc: Dan Williams , Kees Cook , Matthew Wilcox , Thomas Gleixner , Daniel Xu , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788439463307580631 X-GMAIL-MSGID: 1788439463307580631 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/ Signed-off-by: Alice Ryhl Reviewed-by: Benno Lossin --- 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 4665ff86ec00..396fe8154832 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -82,6 +82,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 @@ -104,14 +113,12 @@ fn deref(&self) -> &Self::Target { } } - // 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, } }