Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2365263rdb; Mon, 5 Feb 2024 04:34:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IHccAw+JKaEi8JRcy6hF/ZWyrMgbewczFHz28GmI75qrd6aDPwIfodsC2snH6Gk6qi6g5hc X-Received: by 2002:a05:6122:3683:b0:4c0:da2:76d9 with SMTP id ec3-20020a056122368300b004c00da276d9mr5607068vkb.7.1707136444465; Mon, 05 Feb 2024 04:34:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707136444; cv=pass; d=google.com; s=arc-20160816; b=cYhodPKRY5/LwUT2KswqY2tt+LQp35igjnuJvDFStfU38jX+uf0aVCmghTST6f9K1k cqAtB1d1yWzeUcQtjoPuylFwAyvraobX2JpjkgPa468FaqmGFHr/amGu5K0NnU32vfAb U5/3uAIN0rVn1p5GpLyD5YAwcF5R/3b1f+x4lRFq1mmdSDBwC0AhQcnLUBhgiVn0Z84X 2hsHf3qyhSiCjt3GsLBrBbHZvaR2LjLQ2raRQ53fgNjAfZPIzu3HFD64S3eq8mCxmoUN WI/nZBKyPGQ9ugmXbtQ7KQCMD/a65rffcV8eI6FDOwkDmr3SBsl9o7A9VoSUfz2WUEW5 ju1w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=Koptk02WV6BeYNID6QKo1BWynpaJzVcgM9vAm9rLNbY=; fh=pR8zyHWZ3Zusd25ttw10ONMzZZMeRBYdqmnx3MIjn9M=; b=t3hM3PsqfD1ca9GU1Tb+XfwMc9S0MvQinYQvWeCsNV1CjJEvcClLvW9bc+/kXEMxYZ 9xLtJ4i+w/bPobNil+vqe1UKaMd6fbfG/pUV/j94TPjTmgWihXZ4cJahNPixWJWTeKq7 D/QOytUuth5Dr9b3yJBmvbiOW7/suJJUJWgLdwRkdjiWyorrXUORgANSupCcyIlsJ1mu PNGYVmsASziuo2krp1yyc1vG6E5tJhELMSL6cFzS36yeyW6M34Mk26iGorDlcfpzmk55 yrp7KVtGYNy9UGUbJKIrnTc5Zgc4BrJ70C0UjD6x6O+BNWwsUc0A1bl134gxFHLw1hgc jM7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=iBUOeyK3; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-52605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52605-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me X-Forwarded-Encrypted: i=1; AJvYcCXjDhrUdIOUsl+hc70xDqvIwBgRQ1aFmu5/+vOW0MvP7X1vW3FFyePNZBvJ/ROFbY6Le4ZAE92Cm/GW/Qrnqk5QlqHZ68AiVtWiS9o0RQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t6-20020a0cef06000000b0067f6f705a9asi8499948qvr.101.2024.02.05.04.34.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 04:34:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52605-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=@proton.me header.s=protonmail header.b=iBUOeyK3; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-52605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52605-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me 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 22C451C22DEA for ; Mon, 5 Feb 2024 12:34:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1BD571BDD6; Mon, 5 Feb 2024 12:33:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="iBUOeyK3" Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A31861BC3D; Mon, 5 Feb 2024 12:33:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136430; cv=none; b=VnktjnX13JwxB98ZS73hZwhAEo4QfPrZ02pXjEyq1iZL5lTXwDvXM1WTC+Z4eubYvvD1BXkpBM1fHshSxs88pgKmf+9N4W2rP6cfENuQbKPBz1wfFLBB3bfs4U36IXGjn6RQPCQkzQcG0viHlA4mhd4fZWlSiCwpMJfohPYILWU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707136430; c=relaxed/simple; bh=ZCWpwwDP32QW23OHxqcS6dyIhG+EZnGUM+vdeUCbChQ=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=V7SmIVp0gP4d+fbwFrY+mJJD1d/QwBXH8/IRc+9re6gOfTEB4KkAAcNJ82qysgJNpHwNP6Cj+WTpKRNXH2LPRihIjPzq6JYoCMZvX0NbAj6rA4MByQOzOvKhNOUVqDzC/EnXv79oUE6LikYkraWuE0GvIeTuzEvWFf9FUKOgWNo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=iBUOeyK3; arc=none smtp.client-ip=185.70.40.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1707136426; x=1707395626; bh=Koptk02WV6BeYNID6QKo1BWynpaJzVcgM9vAm9rLNbY=; 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=iBUOeyK3HhTdc8lgsE31Vu767CZcsaMMDP2tX5btt8UO6ecAU3r5SIFXNFT8CxKmW UDvqP7UFoBd5+PLkiet7e8Srvgz9iCnpUha32FSvKqm6wqIpetUlXLeHnVOK0cVti3 Z8Kp9lbiwZ3rD/YKzyroZkhAgyclBEOIrzk7SmLjN/Q+B7GwocbhuiOR7ZgIQGViu6 2vcp2bI1QtvMuR5R5O8Pp/9ZvyL2OiA18NuOn/739FzH8ZLBpdiQTCEIc67EBprkI9 pFP8AsdiGUxdpNRtsxxBUP3m5aAAjYur3XL0aAcuukoNAN1PULQUfNwsOoUxxu2Xwp YLTQ3nFKlq+ng== Date: Mon, 05 Feb 2024 12:33:27 +0000 To: Alice Ryhl , 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 From: Benno Lossin Cc: Daniel Xu , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v4 3/9] rust: file: add Rust abstraction for `struct file` Message-ID: <5f261c6a-785b-4bc5-974f-ad43c3a312b9@proton.me> In-Reply-To: <20240202-alice-file-v4-3-fc9c2080663b@google.com> References: <20240202-alice-file-v4-0-fc9c2080663b@google.com> <20240202-alice-file-v4-3-fc9c2080663b@google.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 2/2/24 11:55, Alice Ryhl wrote: > From: Wedson Almeida Filho >=20 > This abstraction makes it possible to manipulate the open files for a > process. The new `File` struct wraps the C `struct file`. When accessing > 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. >=20 > 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 in > a process and bind it to a `File`. Rust Binder will use these to send > fds from one process to another. >=20 > 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. >=20 > 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 error. > 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 methods > that return `Error`.) >=20 > 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 > --- > fs/file.c | 7 + > rust/bindings/bindings_helper.h | 2 + > rust/helpers.c | 7 + > rust/kernel/file.rs | 249 ++++++++++++++++++++++++++++++++ > rust/kernel/lib.rs | 1 + > 5 files changed, 266 insertions(+) > create mode 100644 rust/kernel/file.rs Reviewed-by: Benno Lossin --=20 Cheers, Benno