Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp325859rdb; Thu, 8 Feb 2024 07:07:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IF889JIerJJShaUP6hZTH+Pn2YGX86A/aYu+GfxLZoGmkkIZUDnwGPqRyIGZzXJs7t3xqwp X-Received: by 2002:a17:903:40c3:b0:1d9:8ddf:5fa0 with SMTP id t3-20020a17090340c300b001d98ddf5fa0mr10660902pld.62.1707404821803; Thu, 08 Feb 2024 07:07:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707404821; cv=pass; d=google.com; s=arc-20160816; b=R3V199BboOp6mRJtd4DBO1BRCJ3kWVVVahaMYNuS65rEiszlHsyEUHjHgzaVDszRkn /gwjTpD3Ct8Ba/oDxak1I7WSnMctMk8RRG5tLqdUy0CZeiseScDNRAT+xaOeR/Q4WRO6 5MYVV7RLpu9RrcaPpqIO93sPHyj6B+1j/EOGFKVjwaUJWU4JMW1Q1TyNyTp/hCXRt1jh bCKvrbhTeIcKLBvdIhTiscKooke2YdN/bDlWJISGEHucB3wBbXWmidD1hZAOPaoJOk1I 0CyQTbArCfOPkMg80qfyqjgQBEiBypA9Ghazr0iuSVF9T5orT8uBYdQGHp/SMHl1svuK jJrQ== 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=5qzJG2kbNcKXxgGCq8nsdXz25PJyvZWTgNzBaOYeMow=; fh=o1TL6Kaib46KhJPB5QNQfFlj18Po0OUjVMHHHn6VChI=; b=okBDUr3TOqDqcll7tw0/u7JECNqveTm1ukf5hJ+n03/20hMUYlEgLW1BuOVERv3vd4 WdFCzpG1Giryhl6hZrXI3zj8c7VwPZTp+/gH2op7p2vLCbmCmfUcewl5rcOmOOfo13y9 onYzWlxVrcpRzx8oNDJMg+Rq2i68DoVHmFXB1l2sFPF5tW8fdOxF3J5KbaZUlXDm3uJg POtOyIGPYzro7F5fbv97Q6uvD8ERyOBiX3dInYUarGQqM4bLzUPpmqrfatUkLC9OXZb5 Ow6LjQeuyD7gBSBtc4V8jaUW8bd5edstZakapK80tWJ1h3MyBgOCqlOkDS7ds7oFm9SZ A/FQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="jpsqcyo/"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-58265-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58265-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXD0fmrjsGJGLhFJypQbh6jM6UGjnTAyjo2aee8vVr5NssiFNZKouak3fQijPqj2zSp4l8W5P7bHzLWfWQ4ohskHCtnBHdr3V6i7bjQxw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ck6-20020a056a02090600b005d8e2a73c93si4586587pgb.386.2024.02.08.07.07.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 07:07:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58265-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="jpsqcyo/"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-58265-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58265-linux.lists.archive=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 5BC5628ACCF for ; Thu, 8 Feb 2024 15:06:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 123F47B3EA; Thu, 8 Feb 2024 15:06:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jpsqcyo/" Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (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 613E677F33 for ; Thu, 8 Feb 2024 15:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707404774; cv=none; b=pwp9C6fOLRvPWxrKVH2K6MQmp+hbG+WPmnRtnTsB9GFrEkfSloUSSSfbuFdKJNRC+qvV4W03BkanXJgHFN0dF5El/74WGOrXGQivzroINDsYYuuSLO6b9QmwprU9WgpLVMqx0BTQNPXzybwbMSUKGvSt728CMXF5fTgrM8aoAzk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707404774; c=relaxed/simple; bh=p7O5ChPBChshUWpDCRZGifl5wJ5DNvQULZn1nSKXFKE=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=KOqnDJFT0+XCd1eDLUb2IGwalFPCNx1mlI1pX5a0mAy0ZZvwRo3HjBL/dXzENm1fBAdi5QO/tPpa4hy9XiGH6lfN0u9wZkadYSUhfYzVWZSbIWLNAfVEsPVV8V8c+J6QdE3pl0TYFSS4WHgbidzEEmZEVGR2X9k7wi65TbXQJ8Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jpsqcyo/; arc=none smtp.client-ip=209.85.161.52 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=google.com Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-59a99ef8c7fso601520eaf.0 for ; Thu, 08 Feb 2024 07:06:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707404771; x=1708009571; 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=5qzJG2kbNcKXxgGCq8nsdXz25PJyvZWTgNzBaOYeMow=; b=jpsqcyo/Q4t0hmUdXu6q8T8VtRaZxnjDpLh12T00rAgxfsqmy++RM1LWHGTy9SRRyG GeaYtGH54FNk5EuWHL9SVyq+tQa6ssH+psjYqWQjVeVDkqoRixdqQjs7henReqHwP/iD 2qYxm/s7Q7HiBMAQQbe4ZevuZ3BOWv61MxGMFT7i2f0Etn6Tiy2S9M9uiqKdD5TlteEP Wgktaa0q/zbnKMzJ5+fgRUG2+Fde2L8894XR8ERyOiGM5+mP/p5opAzqFm96TRdvNLhw T2jL9KfJwcZj5BijBwugDLwPT1sskB4twUtJHclz+F2PDwfc4qBE2AIDLzSrYXkuFTzT rIxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707404771; x=1708009571; 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=5qzJG2kbNcKXxgGCq8nsdXz25PJyvZWTgNzBaOYeMow=; b=lUte4zZu8JQuNrzF8LYm/l+LGer19puabBsN7fvZjMBNSjoLd0P7QzVIESZFMcP/9D UBkO5HsAQWlVidoVK+YEsyHoUequE2tjqVV0mu4W81Qp8oeJ1hSR1dx7NBaKnDs0tjDG MaKmYBMMEcfC59+EEqo9gs0eU9IuODhGPZ5ZjyatoND8hRuNRBwhmH/9EQSLgWJvwM/A aBd06l0efKn6l4oJzXcUAiKRFGJtCSzI6KlabZ7uFdDoYexakgz713DwcQ7HOajnBBk0 jp1fA7gpqqWiUpedpPvq0KTO7nzC2IYJWOydzkRneRzG6EIFupiVnY72i8I1k45nvEe3 GRug== X-Gm-Message-State: AOJu0Yxcw6t2tA7MhQKs30h+eRcdWh+hcjFpkB5rVJskuSLoO89hHyLw pJ6D2GIWh9Yj4KvtjegkT187MBCQVCWlHd2pzpckybre8numgrGRorcngUxsSpqTTmwTHmE5I0V HH56JheSnOf8EmoGBASj+FxR94SedA0ghwzTp X-Received: by 2002:a05:6358:2c82:b0:176:2852:3ac1 with SMTP id l2-20020a0563582c8200b0017628523ac1mr6819717rwm.28.1707404771171; Thu, 08 Feb 2024 07:06:11 -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-3-fc9c2080663b@google.com> <09db8a4c-f471-4ff6-aa14-864697772bd0@gmail.com> In-Reply-To: <09db8a4c-f471-4ff6-aa14-864697772bd0@gmail.com> From: Alice Ryhl Date: Thu, 8 Feb 2024 16:06:00 +0100 Message-ID: Subject: Re: [PATCH v4 3/9] rust: file: add Rust abstraction for `struct file` To: Martin Rodriguez Reboredo 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 Thu, Feb 8, 2024 at 1:35=E2=80=AFAM Martin Rodriguez Reboredo wrote: > > On 2/2/24 07:55, Alice Ryhl wrote: > > From: Wedson Almeida Filho > > > > This abstraction makes it possible to manipulate the open files for a > > process. The new `File` struct wraps the C `struct file`. When accessin= g > > it using the smart pointer `ARef`, the pointer will own a > > reference count to the file. When accessing it as `&File`, then the > > reference does not own a refcount, but the borrow checker will ensure > > that the reference count does not hit zero while the `&File` is live. > > > > Since this is intended to manipulate the open files of a process, we > > introduce an `fget` constructor that corresponds to the C `fget` > > method. In future patches, it will become possible to create a new fd i= n > > a process and bind it to a `File`. Rust Binder will use these to send > > fds from one process to another. > > > > We also provide a method for accessing the file's flags. Rust Binder > > will use this to access the flags of the Binder fd to check whether the > > non-blocking flag is set, which affects what the Binder ioctl does. > > > > This introduces a struct for the EBADF error type, rather than just > > using the Error type directly. This has two advantages: > > * `File::from_fd` returns a `Result, BadFdError>`, which the > > compiler will represent as a single pointer, with null being an erro= r. > > This is possible because the compiler understands that `BadFdError` > > has only one possible value, and it also understands that the > > `ARef` smart pointer is guaranteed non-null. > > * Additionally, we promise to users of the method that the method can > > only fail with EBADF, which means that they can rely on this promise > > without having to inspect its implementation. > > That said, there are also two disadvantages: > > * Defining additional error types involves boilerplate. > > * The question mark operator will only utilize the `From` trait once, > > which prevents you from using the question mark operator on > > `BadFdError` in methods that return some third error type that the > > kernel `Error` is convertible into. (However, it works fine in metho= ds > > that return `Error`.) > > > > Signed-off-by: Wedson Almeida Filho > > Co-developed-by: Daniel Xu > > Signed-off-by: Daniel Xu > > Co-developed-by: Alice Ryhl > > Signed-off-by: Alice Ryhl > > --- > > [...] > > +/// ## Rust references > > +/// > > +/// The reference type `&File` is similar to light refcounts: > > +/// > > +/// * `&File` references don't own a reference count. They can only ex= ist as long as the reference > > +/// count stays positive, and can only be created when there is some= mechanism in place to ensure > > +/// this. > > +/// > > +/// * The Rust borrow-checker normally ensures this by enforcing that = the `ARef` from which > > +/// a `&File` is created outlives the `&File`. > > +/// > > +/// * Using the unsafe [`File::from_ptr`] means that it is up to the c= aller to ensure that the > > +/// `&File` only exists while the reference count is positive. > > +/// > > +/// * You can think of `fdget` as using an fd to look up an `ARef` in the `struct > > +/// files_struct` and create an `&File` from it. The "fd cannot be c= losed" rule is like the Rust > > +/// rule "the `ARef` must outlive the `&File`". > > I find it kinda odd that this unordered list interspaces elements with > blank lines as opposed to the following one, though, I don't see it as > rather a big deal. Shrug. I did it here because I found it more readable. > > +/// > > +/// # Invariants > > +/// > > +/// * Instances of this type are refcounted using the `f_count` field. > > +/// * If an fd with active light refcounts is closed, then it must be = the case that the file > > +/// refcount is positive until all light refcounts of the fd have be= en dropped. > > +/// * A light refcount must be dropped before returning to userspace. > > [...] > > Reviewed-by: Martin Rodriguez Reboredo