From patchwork Fri Feb 9 11:18:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 198830 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp789329dyd; Fri, 9 Feb 2024 03:41:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXGfgE4N9GyTLVCCGrOrWkqTJBKqbpqB1VoxWFU4V9oSKL6RyDUYjUb0SNKh6vPSy/SuwaBtQt/MCr3Gl+lmpU/L7Gi5g== X-Google-Smtp-Source: AGHT+IEVov6HVqvp2DR8goXOgkcXImbIbeSoEGKxao73fcYu4i7PcZFjmp2BWDcYDIiQXnJ46VHq X-Received: by 2002:a17:902:fc8d:b0:1d9:6cb4:3330 with SMTP id mf13-20020a170902fc8d00b001d96cb43330mr1741486plb.12.1707478901118; Fri, 09 Feb 2024 03:41:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707478901; cv=pass; d=google.com; s=arc-20160816; b=a87POCsPQvaVhyhO/tR0k8MVGI8f8d7guUo59qvfCd65UuZ1qqB85x8x1P79F22is7 hyeq2fLRaibaSW2yUBIAU5rKou0sTOoHzF4bazaWP5j1COcIhB2yNdCWQGDc5CJhPS6R s+gQ4xCE9j6u13b3lQvwPjLfZBMdaiN+jW7xT28NWZ3rN/bQQ0F3cqQM1iMSDAjVoN2m tDppsutYUTXczONdrvJ7+QP+H08uxZI+nRqO8qr2vZkAN9ngyePEJnCtqu7s7/OxtMyi VPGNdAhDUcB53KYS9qF8+2gTFGn5534uen+n82InI0qV6TYRikqw2fwIjCUKc1abz2r2 Ukgw== 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=EJTS0bha4jjy5RqEPvTLKlFyYUs1XDYOnmkfX5fj9c0=; fh=tWTZt33+dC5cu1HM1N3we6JD7UXdaPofR9916b/efCw=; b=FIq5Gn31JywFwBs0wK9Xm/YReYmV5Wa8TrqtyXAWTvLwsnc7z27NfRxjgfWVu1pI2T QCTDKWSXQ1QUK8aL6cigD2KYdEZz2BdAAr0KplP0HGnubh7R9j1FNnXU3F1dZviL+5i1 DtBcUJK2J18derOrjT9s6ZKezvtTNNlvvFzrCAvohDuZpuOHMYSjAEidq/m/NjqV99/D bZ8DKYQFe9NGWWSOmCi1noLggkLjM+c8I1dVdj20VG67/HHcPq4MpgqeIO98WVCqkJSR GlxTS7lqi16bwFn3FKT7c1ugzg27deksipJ5iWHDXKc/a8zrAlVzy0XZuOKbCjIXgKo8 8Kog==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vQ8nZCQM; 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-59279-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59279-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXokRDJfSEVgk+Pwi7Lirf5MUQAsoJHn0ps+grRLJMqdiQcO29NzcExfnEYdjjW2ll0FYoVIA66Zb5b8ah124VW3NlAOg== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id kd14-20020a17090313ce00b001da105d6a6esi1040527plb.523.2024.02.09.03.41.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 03:41:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59279-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vQ8nZCQM; 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-59279-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59279-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 121D8B26E87 for ; Fri, 9 Feb 2024 11:20:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1768C37153; Fri, 9 Feb 2024 11:19:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vQ8nZCQM" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 6A18531A93 for ; Fri, 9 Feb 2024 11:18:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707477537; cv=none; b=K2Ic/i4A3weihjZAJx/eM50z/uHJRIKl1NTpd0Hph+1CfwfJXsIJkHYsnw4zHGBbhxWcbRtnbLsLBRmcieXuGhv9hF65OLo2+gzbSx4lZu5EErfFc+G4VxuVNffisSeNCleqyn7lVjCIAd4aza7wxuHebi8CjZ6H6lwZ28jVfIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707477537; c=relaxed/simple; bh=HqW1OxCqhXgTLw9onxFSixvmxMjeck/nA/121BxSsCU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SwmoHN0Otr/sWAPlkvPHUz9wkNwZ0IZ2moA5LYG6rYUVnJDFmIw2HDWgwdUjRlss2jjmJRMX0Jh0ESdla0jXjMcDiyaoycwklBEPv1YIzgKpqcuxFfRc0FIYGRjbJM8AISC66/pvhv+xsVqeu5B/KbUPLYj6TFSSYOXVmpBE2Co= 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=vQ8nZCQM; arc=none smtp.client-ip=209.85.219.202 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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc7489c346bso1830544276.0 for ; Fri, 09 Feb 2024 03:18:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707477534; x=1708082334; 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=EJTS0bha4jjy5RqEPvTLKlFyYUs1XDYOnmkfX5fj9c0=; b=vQ8nZCQMVeD48u13bQNQjhQCvhXFrfJ/317vfjejrwymUGw9YE1sb0VQAGRCz/m42e xfgQXP6HzlLlsikJ/mIMdzmpTEqDKs2JkApF1R28ACgi9UKz12m3UF/1ID3lAw+Wlsxv CTRshJ16/VJwD3SwlFI9u5i7bNAd6FJNtSd5qiG5WIIIVtKd+iEQckaTJS67Lf+vPsm7 +SUowlK2KIjedt5NSV/i3HO9L9SuBQ9KZPxehynwPTHBRZ4GvSgBUSCECjZrYGSy8QXk qPD1jrs1ZUEkYti7OYXqVDEuE583q6HRFhLlzdZSw3r/crzr8JRPLSMAkQCSmH1I/dUv R3MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707477534; x=1708082334; 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=EJTS0bha4jjy5RqEPvTLKlFyYUs1XDYOnmkfX5fj9c0=; b=J0Kn1Vmq4lJSHkCyhfj5HyQHOulZxwGglhOFB9S07ogaWqxTsSMNOJotRYtp6qYFR3 Gx0O2QKxZOiPfILtPxdOtbzlWYJv06LrzhSC51ITyD4YaWDkiy2Za519eoHP0Ahmbic8 xR6ZYHIDmXg8mcQsedhTZK732Vk3MAHXMC9BMlsq2ZQJ9cxQJ/41HLCvj8Hn+GenbwmH bFTrJCNAUefOcOLIANMu/dfhkoIJblw3dmpKgfeVFEep+Gkr6gwMR8BNS+i1utbHVpB2 onqaF8jAapNt2KqJ8dMCQxcfhy97aq+gU/QWtz/v8eTJfdTUzeW+lYcSEBxcx0WYH5zT CbEA== X-Forwarded-Encrypted: i=1; AJvYcCURlus473mdxvkFTd9Ja+HqmLYlI6i594tmsHuvGiIz6LwNuR4H5C/dMOGfEnBV9gzEQROt8yAfTPLv3pyz5ZX5RQcI4s3ThfJ5XTgA X-Gm-Message-State: AOJu0YwpL9uYsWtY2ryuHnHyIxog3tSjvO6n61yYNS16SrONoe7vRxgo E89xGGGzFF9Max5+Th2Yt7sJFYWqtSD6tT2AKYQwooyxoOfhn1LdBbp34FnxjpeW9eNm52lnuTD QRoKzEDolia3cuQ== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:110c:b0:dc6:5396:c0d4 with SMTP id o12-20020a056902110c00b00dc65396c0d4mr269082ybu.1.1707477534562; Fri, 09 Feb 2024 03:18:54 -0800 (PST) Date: Fri, 09 Feb 2024 11:18:15 +0000 In-Reply-To: <20240209-alice-file-v5-0-a37886783025@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240209-alice-file-v5-0-a37886783025@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2382; i=aliceryhl@google.com; h=from:subject:message-id; bh=HqW1OxCqhXgTLw9onxFSixvmxMjeck/nA/121BxSsCU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlxgoQI4IU+W0XW9N3yocqX13mqr0g65bQ44Rqb 1nUM6g4R+mJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZcYKEAAKCRAEWL7uWMY5 Rq3kD/9DzCDNzTpPSVxk82we/p3M26mN5s9Nor1fWDRM2D5T90DnGNc5ZcUuaMkmCLQ9AR/HrfK 6XbO48Iv3WM18Hp6Z7L1nmvQpREO9yEfpvEZU0/hp3fm65XQh8sgb8aw/WvPlV9EA/cOUPC1qkU 7UwWimbbAc84kcGfl6OPmwgO7LMDZVaA3ZXpM/gmvTKjNwPbhGlFIgxF1MehroH5V4Xt6ofUjLi uYFI+G2MuhLIP2wCwB0+xCaKTGm0TnCzhFsA6NapX6gYMwSv31ccsF49/VIXpEmTV2aLv3lVNL5 YpEhBLckZM5Ylx88FUkPeitP5jdZy+n6OZSvF+XbiM8WNHq80asSJb68hgUlDdp3g1Rt2U+utLk LCTwZ0sZ+RKVRXfKESJx5l5ixLcrNrVWX128W9LDehxwJNcl32krdgSPmPkSe/0VDDskdDYepLv 5ZXOwuiTaNJAMwrE+llrvF25tSESBAIfhe7j5K04armtVhpZSSmrURMVJhGEduH5AlYSjykfPQe t/nGT71DFZHFGFLRakOEacdTTgdZtLMjvTw9GEs6XX6zOW0ulTPhNuuCLG3SPDFS/VpcyfgeTt2 MaojZYlh1E5R+IjAj3b4v2cYHZcUG2Oq7DlErFTBMYzMJfppFHsFxXN94o6TFiSUiM49HBielUS JQ/rtgBVjbsIgAA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240209-alice-file-v5-2-a37886783025@google.com> Subject: [PATCH v5 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_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 , Martin Rodriguez Reboredo , Trevor Gross X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790421396211623475 X-GMAIL-MSGID: 1790421396211623475 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 Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Trevor Gross 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 @@ 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, } }