Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2174791rdb; Thu, 7 Dec 2023 23:37:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IH2L4I2VUgV7yd5VVrRDL4uZcnRsSga4TrEh7lvPftVnN6nN+9a9r4mPpzdnNzkZyw8IHzo X-Received: by 2002:a17:902:b705:b0:1d0:6ffd:8348 with SMTP id d5-20020a170902b70500b001d06ffd8348mr2324024pls.83.1702021046316; Thu, 07 Dec 2023 23:37:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702021046; cv=none; d=google.com; s=arc-20160816; b=N6H/l/1XCVawE1KdTyArKJ/1absnOaxVgvo7jsUYIKMlE62IbVv9gYnbMaiRGW0oyF f4CO2AlrqQfJPslqshZpvTuVaAo+D9A/zZrp/tTCsY2a2PFfNCKaVSugDozXZLIZlDr5 t4LyGWS72iBEFQAWaPuEeynXsZW9SKidQKzY8T9oCKhrQTFdXEyEvj73wWkvO4eUTsEX 9fMNQzDanB21eVqBKqbGhlDNlr8E939i7N4A1N1C3zwpcycRg/bYNQYf2jX05W3iGEHA A8+I3G3V52eu157PLgNfdRciJp00bSr5Bx5lJWxpPH8c0vodORUNu3kyOgdc4EHws0rD +KVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :date:dkim-signature; bh=ZZHZA4Gcm1UpAbYA1AbNrAIVMwWMUdvwEKstGqvJtBA=; fh=yGC6hLtEKxG/qAMK6tCmLY72Pr3WjPw6U6qgCoktHOQ=; b=gEeUSBmO6FfN3rhj3QAn3Noe38Da9sFruno9JAElZRQ86qSFadelpaFaZpaeg/fpmd Xyqww0zGmrFdO6MFlOo3V5zRzRLHoSd6YXuJPh7yYZlH4hCSh+ZSQ1qU7/KLPoDZ2NNu y3TKk63zku+NSKejb/GbONXU7LFUhC8OWQdX9BOvijFSn7CvBGRHgs/nV1iNkXEQmxCM bvixoGDao4IQGtKjnxaFwHGlVqGpp35iX8HAXGy6bO7GjN/TFt2/ovjmpc9ZKELeFuy7 J30xhbRxvq5Iq2aQleCnJt4QKlxvE1UNxVSqB0YDURlj1UUqRpwKc2TjreQI/4ghFfqn PnBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=WNENKgdH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id b16-20020a170903229000b001d00ff4bf6csi1160637plh.466.2023.12.07.23.37.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 23:37:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=WNENKgdH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id BF8A481489C3; Thu, 7 Dec 2023 23:37:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573249AbjLHHhJ (ORCPT + 99 others); Fri, 8 Dec 2023 02:37:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232216AbjLHHhI (ORCPT ); Fri, 8 Dec 2023 02:37:08 -0500 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EFF71720; Thu, 7 Dec 2023 23:37:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1702021030; x=1702280230; bh=ZZHZA4Gcm1UpAbYA1AbNrAIVMwWMUdvwEKstGqvJtBA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=WNENKgdH1uy5nNR/BW1az5Ut181MDrpK5hzs+KBapoRlvuX+aNaGE0lqCI+/IYGPR CUej+4t5yvzRThFiGhy0zbW7Eva0OsO7yGRFUjp+sdRkrEMSUoIWjdp6nj+ZpHpw38 KZnlDxLAcWCeP/CzDQ+QC+YKt3Fuoe3L3YKQD6UFLd7BKuv36MUx1W++hlsVqOVRDi CiIjhNNNoRcGfSeZN8J12w2tyXOgwVT/hF/VI2pTUvZK1x2i5DUeZxqW2avJJqfl51 +cA69HuuXZ/en2XpmdeGCxyVt+x/ZrydtI9RHEPJCE/kOx6SuCjWgrleWCjNDsbuKZ ZIk3iJaJpH00A== Date: Fri, 08 Dec 2023 07:37:03 +0000 To: Alice Ryhl From: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Peter Zijlstra , Alexander Viro , Christian Brauner , Greg Kroah-Hartman , =?utf-8?Q?Arve_Hj=C3=B8nnev=C3=A5g?= , 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 Subject: Re: [PATCH v2 4/7] rust: file: add `FileDescriptorReservation` Message-ID: In-Reply-To: <20231206-alice-file-v2-4-af617c0d9d94@google.com> References: <20231206-alice-file-v2-0-af617c0d9d94@google.com> <20231206-alice-file-v2-4-af617c0d9d94@google.com> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 07 Dec 2023 23:37:23 -0800 (PST) On 12/6/23 12:59, Alice Ryhl wrote: > + /// Commits the reservation. > + /// > + /// The previously reserved file descriptor is bound to `file`. This= method consumes the > + /// [`FileDescriptorReservation`], so it will not be usable after th= is call. > + pub fn fd_install(self, file: ARef) { > + // SAFETY: `self.fd` was previously returned by `get_unused_fd_f= lags`, and `file.ptr` is > + // guaranteed to have an owned ref count by its type invariants. There is no mention of the requirement that `current` has not changed (you do mention it on the `_not_send` field, but I think it should also be in the safety comment here). > + unsafe { bindings::fd_install(self.fd, file.0.get()) }; > + > + // `fd_install` consumes both the file descriptor and the file r= eference, so we cannot run > + // the destructors. > + core::mem::forget(self); > + core::mem::forget(file); > + } > +} > + > +impl Drop for FileDescriptorReservation { > + fn drop(&mut self) { > + // SAFETY: `self.fd` was returned by a previous call to `get_unu= sed_fd_flags`. Ditto. > + unsafe { bindings::put_unused_fd(self.fd) }; > + } > +} > + > /// Represents the `EBADF` error code. > /// > /// Used for methods that can only fail with `EBADF`. > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index fdb778e65d79..a4584d6b26c0 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -387,3 +387,13 @@ pub enum Either { > /// Constructs an instance of [`Either`] containing a value of type = `R`. > Right(R), > } > + > +/// 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. > +pub type NotThreadSafe =3D PhantomData<*mut ()>; This should be in its own commit. Then you can also change the usages of `PhantomData<*mut ()>` in `Guard` and `TaskRef`. It would be nice to use `NotThreadSafe` as the value instead of `PhantomData`, since it is a bit confusing...=20 I think we might be able to also have a constant with the same name that is just `pub const NotThreadSafe: NotThreadSafe =3D PhantomData;`. --=20 Cheers, Benno