Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2771057rdb; Mon, 5 Feb 2024 18:14:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IE/X/TgstSb40W1yeE7WWMOQSsZ8FaqhEboc6fyHEM+VPX4I5sQkYfEo3noYRwxGPrLE0sD X-Received: by 2002:ad4:5aa5:0:b0:68c:89cc:1a20 with SMTP id u5-20020ad45aa5000000b0068c89cc1a20mr4009116qvg.8.1707185685130; Mon, 05 Feb 2024 18:14:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707185685; cv=pass; d=google.com; s=arc-20160816; b=FeW60w9UKRHLjS1gPc09UmFIgzIy+lbZHHeEf1gN6V+AxnqxX53COtOQ7O/e8dKhVU mRMhXwG8hXHkC3Ex6XX38NDD4YmPZoI4/EIZP+Xx2dA0HP5mOR2adSJ+djV+PZr31JRN u6YoRTbWPAZVU1xIGtWIWKLgWroYBMERKOEpAA0TOQi2WEdaOcOPbmfHNreJi++7btOy /KMH22N3/6dpnrB9kagDkj3oG2Cv3Tg6G3IzGPcVwrIiI2YgMZGad09jEoyE56LTfyoC Kwn+b8ODbhfPKtpv3yIpYDwBGqGYQSiIZQt+b4rNzFX2ydpRkXvnLN4wieE9bnX3dwHv 0XVQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=TVFUfHzouElpgk6csojgEtg0jiRXTk/Brz1iEYU1QIQ=; fh=XT9I6ZEQfafqI7oYPu6fbSf2v6JS+3oRXmbm8rlIoAI=; b=D2SZKky4ZgEBrfmVXI4eI33ooAoapLh1KAffTKpJfVtNJ8sLBFrskabBj6/DacHCSx Xhbb9cYDmp3xXuF0iMjiQpwYRuGHJ78WuBF7w30FzADLz37AyZSK2xSPOh45eDihq5G6 TAcsD9spvB0rHAqS/PLyUsakglPFExPC3KFg8Pq9HkpfJY8t8HMku75EGj/+HQ0s11mi B1MvPJK1fpw7TN3tdAbE8baJdpwp1uz7UyIechf83sLJCcJk41gM6YFDGFyjHzEEewJL oOFiJ0LMvO9OjrTLcmKAvKSd+0prOnjzTc9VNqE2UJSyK8IgBWO4izW0rddZA1uUbPD2 hqJw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@umich.edu header.s=google-2016-06-03 header.b=IlRvU4P0; arc=pass (i=1 spf=pass spfdomain=umich.edu dkim=pass dkdomain=umich.edu dmarc=pass fromdomain=umich.edu); spf=pass (google.com: domain of linux-kernel+bounces-54188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umich.edu X-Forwarded-Encrypted: i=1; AJvYcCUe2N6ntMA6djEbeWEmp84HdlAEGGJKvC58PIg/j2w4emlul6GKGRFXCwZGYUGgWRn+GSSuuapS1vpI04bPoyMyIGmm9gGNLuRCbXSMdw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ct7-20020a056214178700b00683699d4df8si1405086qvb.139.2024.02.05.18.14.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 18:14:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54188-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=@umich.edu header.s=google-2016-06-03 header.b=IlRvU4P0; arc=pass (i=1 spf=pass spfdomain=umich.edu dkim=pass dkdomain=umich.edu dmarc=pass fromdomain=umich.edu); spf=pass (google.com: domain of linux-kernel+bounces-54188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umich.edu 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 D08401C2322D for ; Tue, 6 Feb 2024 02:14:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D773756B89; Tue, 6 Feb 2024 02:14:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=umich.edu header.i=@umich.edu header.b="IlRvU4P0" Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (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 04C6C56B65 for ; Tue, 6 Feb 2024 02:14:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707185670; cv=none; b=YJurUZkBLnhFgIXKu4AKwcViUls08415IH34gU4JtvR/n6kIMKvaIPy+LtSMC9AmKdRGGXtiJN4j4tJJxSMReAkk3Ly8Vm6i66PrMPr5NUrFN8AR0+9M7vv2j5DSchU5ERsmcwFZ0a3LlveR/pEef9qCOSez0K7z8CRlA70Ypps= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707185670; c=relaxed/simple; bh=o25zqqGgJb+/DuJjbPME/jHUMuD6KdaPfV2xdG2bIQQ=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Jd58BgePOtX5kutfrJXjERuM0R0oI6Z0S7hqXTj9gsJwmgH982ur3A0U9WUkpjf/4fhHK1QgE0+qlRXj72byNvvsntY1lAuAJxvOG+vEOF6Ln77Kjsh8xMQLqOEju+WS6DzfJmUkCX9q3Joaw8lP7jV5eKshd+e25uHBGYzkXnc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=umich.edu; spf=pass smtp.mailfrom=umich.edu; dkim=pass (2048-bit key) header.d=umich.edu header.i=@umich.edu header.b=IlRvU4P0; arc=none smtp.client-ip=209.85.128.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=umich.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=umich.edu Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-603fd31f5c2so937687b3.0 for ; Mon, 05 Feb 2024 18:14:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=umich.edu; s=google-2016-06-03; t=1707185667; x=1707790467; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=TVFUfHzouElpgk6csojgEtg0jiRXTk/Brz1iEYU1QIQ=; b=IlRvU4P0yFmjhF68oSMMR9AJj4Iuw/GrVjQCyWvoEcQqF4vygb8qaUF9NigxgWrkhn IQTGMjKbwxOhrHXYq/pCXlnnm6H1oOJ212Lst9Ulfp5nZq4ycuEHT8kWr2xoPUiKwcZl T8WVRrFW9QzooB/r8IXK6hpK3gl36UpYRSb0ylrQPWlNTC43QTAmCvACBKdYusyUOiKZ udS7Nl9XYsrt8sirsSlcGEF7gAZUBg0k8FY06CGadcyG/HYoKkhD/NIXuqZZ5XCk6NSb VCzyNbUJOsIUJhavRdjqRXmwbSJ3DIld2QPWRKljSPBxtlJMvVQxYa+IhZ1E1MElrMjk 1tMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707185667; x=1707790467; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TVFUfHzouElpgk6csojgEtg0jiRXTk/Brz1iEYU1QIQ=; b=vBQAWlvNch2PmNXgpmH3AzyC86xdq9uRg85+CnOJuTW0t52wFjmvVjM3ZRePNUXwse 2rBfn+ebTodaEYq+wWlAqNJm6KlJhCHhi798nSgVGOGzpXpx+5flQQjqWQ9QrD99Ej4U +7O2AQABsKHs/5omwuRQk2wfYMeJKoonl6O3QSu9qcjITZkWPZznKWGfTGtqQX8THCeA 0X5TQDMha19Bu0Sq1a42wvfh2OcmmcgoaPu9lJaUuf4latCiBhPYd9gTZ7fmK/x1PHIq 1axKtJ8u9IgvJOVoZ0p2WzgHHxGdGLn3g3CCZz5GfIRuaVXiM2/d+Bd7OMnvX0y5pyTM Suew== X-Gm-Message-State: AOJu0YxhRUxDLotGMkMmvZXlTz82jLWklSfWr+kryEPE6wKQws9QGO0T nxPMFX5TdnaARDy5oG3l8H7aeSmzwUWdbWl2odQLCqdsUaxPtHrN4GYKOEzTdr+bmsIz4t3e8Ch snv04TjCno0fdEq4IrAsOLPC/7Yjy+i8jymrJHg== X-Received: by 2002:a81:844e:0:b0:604:1d65:b66b with SMTP id u75-20020a81844e000000b006041d65b66bmr269588ywf.9.1707185666967; Mon, 05 Feb 2024 18:14:26 -0800 (PST) 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> <20240202-alice-file-v4-1-fc9c2080663b@google.com> In-Reply-To: <20240202-alice-file-v4-1-fc9c2080663b@google.com> From: Trevor Gross Date: Mon, 5 Feb 2024 21:14:15 -0500 Message-ID: Subject: Re: [PATCH v4 1/9] rust: types: add `NotThreadSafe` To: Alice Ryhl Cc: 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?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Feb 2, 2024 at 5:56=E2=80=AFAM Alice Ryhl wr= ote: > > This introduces a new marker type for types that shouldn't be thread > safe. By adding a field of this type to a struct, it becomes non-Send > and non-Sync, which means that it cannot be accessed in any way from > threads other than the one it was created on. > > This is useful for APIs that require globals such as `current` to remain > constant while the value exists. > > We update two existing users in the Kernel to use this helper: > > * `Task::current()` - moving the return type of this value to a > different thread would not be safe as you can no longer be guaranteed > that the `current` pointer remains valid. > * Lock guards. Mutexes and spinlocks should be unlocked on the same > thread as where they were locked, so we enforce this using the Send > trait. > > There are also additional users in later patches of this patchset. See > [1] and [2] for the discussion that led to the introducion of this s/introducion/introduction > patch. > > Link: https://lore.kernel.org/all/nFDPJFnzE9Q5cqY7FwSMByRH2OAn_BpI4H53NQf= WIlN6I2qfmAqnkp2wRqn0XjMO65OyZY4h6P4K2nAGKJpAOSzksYXaiAK_FoH_8QbgBI4=3D@pro= ton.me/ [1] > Link: https://lore.kernel.org/all/nFDPJFnzE9Q5cqY7FwSMByRH2OAn_BpI4H53NQf= WIlN6I2qfmAqnkp2wRqn0XjMO65OyZY4h6P4K2nAGKJpAOSzksYXaiAK_FoH_8QbgBI4=3D@pro= ton.me/ [2] > Suggested-by: Benno Lossin > Reviewed-by: Benno Lossin > Signed-off-by: Alice Ryhl > --- > rust/kernel/sync/lock.rs | 15 +++++++++++---- > rust/kernel/task.rs | 10 ++++++---- > rust/kernel/types.rs | 17 +++++++++++++++++ > 3 files changed, 34 insertions(+), 8 deletions(-) > > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index 149a5259d431..d08d9d32915a 100644 > --- a/rust/kernel/sync/lock.rs > +++ b/rust/kernel/sync/lock.rs > @@ -6,8 +6,15 @@ > //! spinlocks, raw spinlocks) to be provided with minimal effort. > > use super::LockClassKey; > -use crate::{bindings, init::PinInit, pin_init, str::CStr, types::Opaque,= types::ScopeGuard}; > -use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned}= ; > +use crate::{ > + bindings, > + init::PinInit, > + pin_init, > + str::CStr, > + types::ScopeGuard, > + types::{NotThreadSafe, Opaque}, Formatting nit: ScopeGuard could probably be placed in the same group as NotThreadSafe & Opaque > [...] > + > +/// Zero-sized type to mark types not [`Send`]. > +/// > +/// Add this type as a field to your struct if your type should not be s= ent to a different task. > +/// Since [`Send`] is an auto trait, adding a single field that is `!Sen= d` will ensure that the > +/// whole type is `!Send`. > +/// > +/// If a type is `!Send` it is impossible to give control over an instan= ce of the type to another > +/// task. This is useful when a type stores task-local information for e= xample file descriptors. I initially read this thinking it meant to include this type if your struct also had a FD rather than being part of the FD. Maybe This is useful to include in types that store or reference task-local information. A file descriptor is an example of one such type. > +pub type NotThreadSafe =3D PhantomData<*mut ()>; > + > +/// Used to construct instances of type [`NotThreadSafe`] similar to how= we construct > +/// `PhantomData`. I think it sounds slightly better reworded from personal to passive, i.e. ... similar to how `PhantomData` is constructed. > +/// [`NotThreadSafe`]: type@NotThreadSafe > +#[allow(non_upper_case_globals)] > +pub const NotThreadSafe: NotThreadSafe =3D PhantomData; > -- > 2.43.0.594.gd9cf4e227d-goog This looks good, sounds nice to make the intent more clear. Nothing that isn't optional, so Reviewed-by: Trevor Gross