Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp259354rdb; Thu, 30 Nov 2023 04:17:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFFwvvb3m3Gkl3FXaISTRLXJOcY9Scaqm6M4c0hjfYTy5D9fDiqBSEVOUkg7QkHluORmYmt X-Received: by 2002:a17:90b:3b4d:b0:285:b876:5033 with SMTP id ot13-20020a17090b3b4d00b00285b8765033mr13255178pjb.49.1701346665855; Thu, 30 Nov 2023 04:17:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701346665; cv=none; d=google.com; s=arc-20160816; b=sKkjk870SNamT6MAs6O4YIfpHa1GztMHvQjaoLmiTdqB4SyxpdXgobt6qRN1ImZTGC QrM+px2y5vJTiT/spH/u3KCUrZhMeGHuRpm2lq1S7NFszoFe3/kOZ4y+FsXu0aKW8Zxd bTpbwkX+8sk83LsGO57jcZqL4clQ/Q5yjmuSwf8t1k9FXF8APRwP/k0jZ7dV6Wjj2Emd IHPf2zDeOlITZ8Vh9lPsOcljDwKGn2ac+ehHvQbEoiI2I7tWySbM89Qhy8C51PX6NTau lD7cIPIl3Ixt8xdtEbheakHfF5g024YI4nyv+iR2+9MjwdsJk1T3UFhV/c3ajefAOTdZ g4vQ== 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=dlgoIC/cWlhFA8U/2kxuaUxiujj/QS0YBwzrP3FBLaI=; fh=YQNu/qd5xsKucRUuP5LJL3TRwcBnmkib1ekrqfEMTnk=; b=lofeIXIo0PzYsbD4tbBqP35dh8sV2x67m5HasFmSzMc93gHw6vC/X1KdxaRxZCmmTI 3FHV24iwI0E8/AYP8XkRetWUmBeQqKh9sVjyw0EywxoDxPPFon0D0ehXvpBs0mHlwzVR m1jUOXY7APSMP2THeNIqc2sDG5Ib0fovhLUFX7nAJgkcN7u6cYx8gU0DQcMHT6pOkdPe xSyZTecyvD8q8/UwgZQVtH3XUjwBZqC8uAe1P+zXxW7RAi7gmwQ+1NEPhhTa2K0u9CzR VnlSrHlaWJflLmy2C3Mkj7ng0GvvAWb2PNqiK3l8Up0yHzrfHfUY/C+QPQPY3u2KJ0QX ugnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=k6ULomu+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id ft10-20020a17090b0f8a00b00285763ca04dsi1237090pjb.134.2023.11.30.04.17.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 04:17:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=k6ULomu+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 5C2D8804DD91; Thu, 30 Nov 2023 04:17:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345297AbjK3MRb (ORCPT + 99 others); Thu, 30 Nov 2023 07:17:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231887AbjK3MRY (ORCPT ); Thu, 30 Nov 2023 07:17:24 -0500 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63AD010F1 for ; Thu, 30 Nov 2023 04:17:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1701346639; x=1701605839; bh=dlgoIC/cWlhFA8U/2kxuaUxiujj/QS0YBwzrP3FBLaI=; 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=k6ULomu+kgyTAuukqBhyciOebVjjGZaMvKMo1C8smeeSSoLuPMkTudT4SPqHdzZhb uXYxQ+0d2qNku34cK8jD1/whBNndv+QgyP3RKVg7Dg6c/ZOSVx9PphH73jLDer3KdY 508UOnAwQGMxSm48M8FJj1RHHwtxx8UEEYXoLvcmrPgzhJA4b+q2P5Dei+0neQtoZm gPOnF+0u1SsuRyAG3vy8+PMwXxQls5Wq+Q7zvS9FdMLs7mgUd97eYRFuxS2xUzBVJ3 3JaQWy9wFsaV1SefC6RRDtmViVTWhuHT81LKHAZlszCYV53SscxDC3QhxcWRYePsZJ CYOjyn1Dn/ABQ== Date: Thu, 30 Nov 2023 12:17:14 +0000 To: Alice Ryhl From: Benno Lossin Cc: brauner@kernel.org, a.hindborg@samsung.com, alex.gaynor@gmail.com, arve@android.com, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, cmllamas@google.com, dan.j.williams@intel.com, dxu@dxuuu.xyz, gary@garyguo.net, gregkh@linuxfoundation.org, joel@joelfernandes.org, keescook@chromium.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, maco@android.com, ojeda@kernel.org, peterz@infradead.org, rust-for-linux@vger.kernel.org, surenb@google.com, tglx@linutronix.de, tkjos@android.com, viro@zeniv.linux.org.uk, wedsonaf@gmail.com, willy@infradead.org Subject: Re: [PATCH 4/7] rust: file: add `FileDescriptorReservation` Message-ID: In-Reply-To: <20231130115451.138496-1-aliceryhl@google.com> References: <20231130-windungen-flogen-7b92c4013b82@brauner> <20231130115451.138496-1-aliceryhl@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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 30 Nov 2023 04:17:44 -0800 (PST) On 30.11.23 12:54, Alice Ryhl wrote: > Christian Brauner writes: >> On Thu, Nov 30, 2023 at 09:17:56AM +0000, Alice Ryhl wrote: >>> Christian Brauner writes: >>>>>>> + /// Prevent values of this type from being moved to a differen= t task. >>>>>>> + /// >>>>>>> + /// This is necessary because the C FFI calls assume that `cur= rent` is set to the task that >>>>>>> + /// owns the fd in question. >>>>>>> + _not_send_sync: PhantomData<*mut ()>, >>>>>> >>>>>> I don't fully understand this. Can you explain in a little more deta= il >>>>>> what you mean by this and how this works? >>>>> >>>>> Yeah, so, this has to do with the Rust trait `Send` that controls >>>>> whether it's okay for a value to get moved from one thread to another= . >>>>> In this case, we don't want it to be `Send` so that it can't be moved= to >>>>> another thread, since current might be different there. >>>>> >>>>> The `Send` trait is automatically applied to structs whenever *all* >>>>> fields of the struct are `Send`. So to ensure that a struct is not >>>>> `Send`, you add a field that is not `Send`. >>>>> >>>>> The `PhantomData` type used here is a special zero-sized type. >>>>> Basically, it says "pretend this struct has a field of type `*mut ()`= , >>>>> but don't actually add the field". So for the purposes of `Send`, it = has >>>>> a non-Send field, but since its wrapped in `PhantomData`, the field i= s >>>>> not there at runtime. >>>> >>>> This probably a stupid suggestion, question. But while PhantomData giv= es >>>> the right hint of what is happening I wouldn't mind if that was very >>>> explicitly called NoSendTrait or just add the explanatory comment. Yes= , >>>> that's a lot of verbiage but you'd help us a lot. >>> >>> I suppose we could add a typedef: >>> >>> type NoSendTrait =3D PhantomData<*mut ()>; >>> >>> and use that as the field type. The way I did it here is the "standard" >>> way of doing it, and if you look at code outside the kernel, you will >>> also find them using `PhantomData` like this. However, I don't mind >>> adding the typedef if you think it is helpful. >> >> I'm fine with just a comment as well. I just need to be able to read >> this a bit faster. I'm basically losing half a day just dealing with >> this patchset and that's not realistic if I want to keep up with other >> patches that get sent. >> >> And if you resend and someone else review you might have to answer the >> same question again. >=20 > What do you think about this wording? >=20 > /// Prevent values of this type from being moved to a different task. > /// > /// This field has the type `PhantomData<*mut ()>`, which does not > /// implement the Send trait. By adding a field with this property, we > /// ensure that the `FileDescriptorReservation` struct will not > /// implement the Send trait either. This has the consequence that the > /// compiler will prevent you from moving values of type > /// `FileDescriptorReservation` into a different task, which we want > /// because other tasks might have a different value of `current`. We > /// want to avoid that because `fd_install` assumes that the value of > /// `current` is unchanged since the call to `get_unused_fd_flags`. > /// > /// The `PhantomData` type has size zero, so the field does not exist at > /// runtime. >=20 > Alice I don't think it is a good idea to add this big comment to every `PhantomData` field. I would much rather have a type alias: /// Zero-sized type to mark types not [`Send`]. /// /// Add this type as a field to your struct if your type should not be = sent to a different task. /// Since [`Send`] is an auto trait, adding a single field that is [`!S= end`] will ensure that the /// whole type is [`!Send`]. /// /// If a type is [`!Send`] it is impossible to give control over an ins= tance of the type to another /// task. This is useful when a type stores task-local information for = example file descriptors. pub type NotSend =3D PhantomData<*mut ()>; If you have suggestions for improving the doc comment or the name, please go ahead. This doesn't mean that there should be no comment on the `NotSend` field of `FileDescriptorReservation`, but I don't want to repeat the `Send` stuff all over the place (since it comes up a lot): /// Ensure that `FileDescriptorReservation` cannot be sent to a differe= nt task, since there the /// value of `current` is different. We want to avoid that because `fd_= install` assumes that the /// value of `current` is unchanged since the call to `get_unused_fd_fl= ags`. _not_send: NotSend, --=20 Cheers, Benno