Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp254581rdb; Thu, 30 Nov 2023 04:10:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxEcNu59QrFeyygV2Natv/Q55qvI27+31JGnur9DRUWPVNIcPZwKxqTl27ltZrYH0ZAX67 X-Received: by 2002:a17:903:32c8:b0:1cc:3544:ea41 with SMTP id i8-20020a17090332c800b001cc3544ea41mr28277247plr.46.1701346236815; Thu, 30 Nov 2023 04:10:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701346236; cv=none; d=google.com; s=arc-20160816; b=lLGBMvYY0BTBBxAE+uYxmvxdgfKAkrwlh7f8AV9ikIq5dtWVOtNeNaWy4uMS0Tfoga KCGidPq3+xY5Vt3LDGxZiYXeikseTO/5h46BageAdX5CdKTfuaEHjYQaX3fMGMEfXhoe vbUnrFsR4hTPosPMNr8ZhV1sMwjRwsUzqmYnXdunXtRkwg77B2CllzGJB8V/8WIBeRYh NoP8NfJFelo6FGsATzDy2UoCh87Z7hNOI3RlAV0W9k6js3l3geakFXTFeADCUoWAn6Za 6EuuHmxzGyldesuFi5Lk9mhdZB9z8LRYh9L4v/YWmmokIXxWXnczfKrJylZG6+7USbCv TLeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=MpC0qbPCJ3yCkzqZmHvjb+j6E99/8etcnMaQGxE98xo=; fh=iehEewRXubtisj4ShmZfjRd9hhpUPlnu/+o8FDS+QQ8=; b=s4XHFk8TG7u/urfnM1Mbj9JtmtfjwTjS6PpZuXw32z9SQqn/GoftFJwq0/E9RFV9rx rNtKEFCbcYsB2s4pRFQclY33zxe3G2/urInnirvLLZOSO3a73QHZo+bTwGnnckdrlUta mBIoDHp5ubnD+d8ej3//egxJBhUs3XKQa7h5dVvUHfX1hNL4jRGj9vqPEeyYjjD3MbWX 9gzm15kC38xCbaoTFodpkp7BXNSSZ1812enw4bRscmn/d5jWaLrsQ1fP5fTbV6pCThoe YPkpHTRDefmK4ySTNGRo8E43PeM9wq05w7fVco9GPHh14YtcJBAKNlyhwa0MgrPeiqSc xzqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DauTORjd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id jj14-20020a170903048e00b001cfd0ddc5d6si1016572plb.348.2023.11.30.04.10.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 04:10:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DauTORjd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id BE0E58080E15; Thu, 30 Nov 2023 04:10:33 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345359AbjK3MKP (ORCPT + 99 others); Thu, 30 Nov 2023 07:10:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345361AbjK3MKM (ORCPT ); Thu, 30 Nov 2023 07:10:12 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3093C10F2 for ; Thu, 30 Nov 2023 04:10:16 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d064f9e2a1so13149857b3.1 for ; Thu, 30 Nov 2023 04:10:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701346215; x=1701951015; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MpC0qbPCJ3yCkzqZmHvjb+j6E99/8etcnMaQGxE98xo=; b=DauTORjdSDyxQtj+qiiDb77b8RnHJnJIteI5c5uNh0A2d2yX3qioZQcqPjT/A9Slyg xItc14+OiF8pZIHGxa9AJyxGnFBF1CJhFt5EIYucVBMfd9f/ybSoYSuRe4qtpxBXTB8n YDg3HfBncegdPfJQcDvGdNTKT7avSitiK8qOjYtRtWDJrWsAW2JH7ZipVY4Mb7MfOdVr kZO4M+bzTCaHQkqSkkmbM7fViY9g8ifo7fIEEaDCr73dfNqmiyZvfpStYumXinSeYRO6 dJ039K/GVq/whYpEtsc2ps81RMg10mSrWIIHNyCMTTTFFvPdOaQP/eIxy5B+RIZE/3RX pb+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701346215; x=1701951015; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MpC0qbPCJ3yCkzqZmHvjb+j6E99/8etcnMaQGxE98xo=; b=XbeqWjOiKWLXMAnOsJAk+mN29wjfrLxvBL/JFLTZVWmKR9tmlJWuhLU4Iq9FwGi1Ut /HxMrKuOH4TWBM6JDeWIWqpzTdXPEIvnuX04fAi56u0Ss7XOi6zz8q60QPDSPFRkexZk PP8aWNtJ/J9+jULJPTVUEEjNxJNwFIr3UIGWf3kXvylk16jS/musCMWGEF7ONT0i9OTe fImjoHPRV728AkS7+ewAjBNhG/MO+6mShX0IQCLgkBT229YGEID1OfaK2HB2YOJExK74 sS41l7e/2azpzB7V8uaynegOOimIQhEmTpJc8uBM98HYfHzBLNNrL6X0ImEiAKtkG9vI FM7Q== X-Gm-Message-State: AOJu0YzoGcK6CovaWKRRngeumkuYEXYjd1xN2k47BGsd69ENNUiASgry 8/EYvG4Mf81fp9oEStyN5WFAOBYrHbZFWbo= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:690c:844:b0:5ca:8462:670f with SMTP id bz4-20020a05690c084400b005ca8462670fmr667597ywb.0.1701346215441; Thu, 30 Nov 2023 04:10:15 -0800 (PST) Date: Thu, 30 Nov 2023 12:10:12 +0000 In-Reply-To: <20231130-sackgasse-abdichtung-62c23edd9a9f@brauner> Mime-Version: 1.0 References: <20231130-sackgasse-abdichtung-62c23edd9a9f@brauner> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231130121013.140671-1-aliceryhl@google.com> Subject: Re: [PATCH 1/7] rust: file: add Rust abstraction for `struct file` From: Alice Ryhl To: brauner@kernel.org Cc: a.hindborg@samsung.com, alex.gaynor@gmail.com, aliceryhl@google.com, arve@android.com, benno.lossin@proton.me, 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 Content-Type: text/plain; charset="utf-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 30 Nov 2023 04:10:33 -0800 (PST) Christian Brauner writes: >> This is the backdoor. You use it when *you* know that the file is okay > > And a huge one. > >> to access, but Rust doesn't. It's unsafe because it's not checked by >> Rust. >> >> For example you could do this: >> >> let ptr = unsafe { bindings::fdget(fd) }; >> >> // SAFETY: We just called `fdget`. >> let file = unsafe { File::from_ptr(ptr) }; >> use_file(file); >> >> // SAFETY: We're not using `file` after this call. >> unsafe { bindings::fdput(ptr) }; >> >> It's used in Binder here: >> https://github.com/Darksonn/linux/blob/dca45e6c7848e024709b165a306cdbe88e5b086a/drivers/android/rust_binder.rs#L331-L332 >> >> Basically, I use it to say "C code has called fdget for us so it's okay >> to access the file", whenever userspace uses a syscall to call into the >> driver. > > Yeah, ok, because the fd you're operating on may be coming from fdget(). Iirc, > binder is almost by default used multi-threaded with a shared file descriptor > table? But while that means fdget() will usually bump the reference count you > can't be sure. Hmkay. Even if the syscall used `fget` instead of `fdget`, I would still be using `from_ptr` here. The `ARef` type only really makes sense when *we* have ownership of the ref-count, but in this case we don't own it. We're just given a promise that the caller is keeping it alive for us using some mechanism or another. >>>> +// SAFETY: It's OK to access `File` through shared references from other threads because we're >>>> +// either accessing properties that don't change or that are properly synchronised by C code. >>> >>> Uhm, what guarantees are you talking about specifically, please? >>> Examples would help. >>> >>>> +unsafe impl Sync for File {} >> >> The Sync trait defines whether a value may be accessed from several >> threads in parallel (using shared/immutable references). In our case, > > So let me put this into my own words and you correct me, please: > > So, this really just means that if I have two processes both with their own > fdtable and they happen to hold fds that refer to the same @file: > > P1 P2 > struct fd fd1 = fdget(1234); > struct fd fd2 = fdget(5678); > if (!fd1.file) if (!fd2.file) > return -EBADF; return -EBADF; > > // fd1.file == fd2.file > > the only if the Sync trait is implemented both P1 and P2 can in parallel call > file->f_op->poll(@file)? > > So if the Sync trait isn't implemented then the compiler will prohibit that P1 > and P2 at the same time call file->f_op->poll(@file)? And that's all that's > meant by a shared reference? It's really about sharing the pointer. Yeah, what you're saying sounds correct. For a type that is not Sync, you would need a lock around the call to `poll` before the compiler would accept the call. (Or some other mechanism to convince the compiler that no other thread is looking at the file at the same time. Of course, a lock is just one way to do that.) > The thing is that "shared reference" gets a bit in our way here: > > (1) If you have SCM_RIGHTs in the mix then P1 can open fd1 to @file and then > send that @file to P2 which now has fd2 refering to @file as well. The > @file->f_count is bumped in that process. So @file->f_count is now 2. > > Now both P1 and P2 call fdget(). Since they don't have a shared fdtable > none of them take an additional reference to @file. IOW, @file->f_count > may remain 2 all throughout the @file->f_op->*() operation. > > So they share a reference to that file and elide both the > atomic_inc_not_zero() and the atomic_dec_not_zero(). > > (2) io_uring has fixed files whose reference count always stays at 1. > So all io_uring operations on such fixed files share a single reference. > > So that's why this is a bit confusing at first to read "shared reference". > > Please add a comment on top of unsafe impl Sync for File {} > explaining/clarifying this a little that it's about calling methods on the same > file. Yeah, I agree, the terminology gets a bit mixed up here because we both use the word "reference" for different things. How about this comment? /// All methods defined on `File` that take `&self` are safe to call even if /// other threads are concurrently accessing the same `struct file`, because /// those methods either access immutable properties or have proper /// synchronization to ensure that such accesses are safe. Note: Here, I say "take &self" to refer to methods with &self in the signature. This signature means that you pass a &File to the method when you call it. Alice