Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp749655rdb; Fri, 2 Feb 2024 02:58:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHD7Tisb3KQPucVsIMietkLMFYSqLbRHJu2CVUSg4+2Ulq71HXi7k8X/mr5ilAggtgCyk6p X-Received: by 2002:a05:622a:1341:b0:42b:ed14:eaf7 with SMTP id w1-20020a05622a134100b0042bed14eaf7mr6775021qtk.7.1706871527345; Fri, 02 Feb 2024 02:58:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706871527; cv=pass; d=google.com; s=arc-20160816; b=WA2fzIEsiBC8cuyvGpAlumfORLo9EVmvYY0VSYjeETdD5I7T59/yO5Tee2IFTydCW5 +FKs34lXGedBEZGoue3gLxdAkZX+6Q5IDe17tIHMUIOtQTgRxRDgLz+a/WLdHxOs4t3K czG7oCwhNdnJR9VNn32aQk1AxWS7HmC8RFE8PVfa4ohzHVv+pkKolgbnN9N/mFKrYuBD jCqCUFPLXnh5J0P/6S1chQaJNCYXrTtwVNyg2/AR9c5wv9GFds/bfIJ5vuoncAFUUs/I 51Tc87IT8fP0IzpNaTBtlc0B8txwTjp3hddR1xh2fSaeoVDVLayshRwJewHLV4QEkG/K EGwg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=/T3POnWw3qzAjUziQFt05EJyPrMhtND82GObaMSRXeI=; fh=tn3xwvfGBbDVbqcWfHTPyICrOhjRddtEcoZL4dBEwdM=; b=KJ1mMaA0cu56IhMjWDwa3Fb8IgbrmsH9hE0MrRwugBxAIsswKzWHOoAbM+LVimACTg H0bdll9jrLyecMlrhMseeYA6DFiRj/ezVoS4tKAift9cz6mTIH918WBaQgZzRZxDp7TW Ully1Xve9k7n5TtHpdAdK4X/ORWuyHC0uTyH1ffSJdNK+5Fm6SYPJ6QJIc/CrxYDJ6v1 1IvtswrWfJVJn0xzPl8UVJMQ89HudYYeLUqnkA6yjtH8mhGvkTQOU4apzd72WmWhtrLa CC1qshstMTxkJiAR/NFgFj0yH/AXue9SMAlcZJGQmhGNhWQ8az4SNFbWSLcljVkNuQb3 WC9w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oDgxrgn2; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-49714-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49714-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVyDC6MW6bpUkeNFwRJ2xvedmNop541GZ/Gu8cDzuda8WS3lhHdwkYlnWDugABeEuPPUXvK4zKPqJJW3jA2SrA1jfHJX1yIJQaQNJ0KSA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o8-20020a05622a138800b0042aa70b95c9si1697816qtk.489.2024.02.02.02.58.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 02:58:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-49714-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=@google.com header.s=20230601 header.b=oDgxrgn2; arc=pass (i=1 spf=pass spfdomain=flex--aliceryhl.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-49714-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-49714-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id ED82D1C20F5B for ; Fri, 2 Feb 2024 10:58:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6212F13DB99; Fri, 2 Feb 2024 10:56:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oDgxrgn2" Received: from mail-lf1-f74.google.com (mail-lf1-f74.google.com [209.85.167.74]) (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 4C4BC1419A8 for ; Fri, 2 Feb 2024 10:56:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706871376; cv=none; b=JBFuIZFIcg3IrmNS7VoGEJ/CVpUp6QcfMoMKQ6BPjAtJFonmptlGw45wxHrsxe+N6a/qiu5tToaAbZ1ta1aqk4xSEMpEqfOvNDlsRQhBaMqixEhGqMPdSZpeAOE6GpPcTTq62b63VU2xRbmr7QuHAbVxfZrqhrMDofM8+cTGJm0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706871376; c=relaxed/simple; bh=/9E8jl2L0BIX668zHWJR7ToHHbaSj1Hq8IfbP9A+fIg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pV77fANkS+PzBwFw/+ekK2sKKBd5I4XopT9DvlAI2RA4iRK8GMC+/tSsK7VLObyi0zzAeXdHx4F5+o82wQyA4CglNtq4XyPuYcrJ0ME4Bvchy1CV2fS+Y28QMKwbDp37WO60gRuBTGAl2ilij6Kc0w+pfgPMXqvqunnocn1yaQc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oDgxrgn2; arc=none smtp.client-ip=209.85.167.74 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=flex--aliceryhl.bounces.google.com Received: by mail-lf1-f74.google.com with SMTP id 2adb3069b0e04-50e91f9d422so2207029e87.2 for ; Fri, 02 Feb 2024 02:56:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706871372; x=1707476172; 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=/T3POnWw3qzAjUziQFt05EJyPrMhtND82GObaMSRXeI=; b=oDgxrgn2Oiu7po0wHn4a+p4ke9dei6b/0am+R/LY3cUQlngRKQYnjcDC8Hp7R/3nIM yLSwHIZx7BunaHUGQ66lOmxafX2OUj4nGTHiz3DO9T/EeQYC2rdbYEEHc91NdJ20TfTr UBcNRQ5LSpjAn8tEBe9PeCivkoZ3YP4q8l9vzJkvrEIZi8eT0hxyNuX03fGRQ5mJYK8W VraW3qqS1o6bgDwUZbMlYzQ/Xhf3a60knR8trVk2dyPafXWJ575yzbRPVlfCP6FH0I3h sVxIcZqkv9I9xz4PirmXIF7kgdDzE4nqFvW42D/SIsvQJrjIMKH9ayaQqwixUhQTIZt+ E+fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706871372; x=1707476172; 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=/T3POnWw3qzAjUziQFt05EJyPrMhtND82GObaMSRXeI=; b=edIC42PcFoa3J0TnMq5IxJrQiNE2KTsFDBU6EtKPmmCP5BtG2EjdQJP2P0B/EVyjNV NoFtSUimOV3Ph+UOohoPLdvm99ttMZ3wHv1TO23Jxu7iIcb+vgp+tpyszoXm4RFyN/B4 UEdQDjHs1EkVv5psVUZfQCHhXhk4nAMosck0OHOTtoP7WK3zinWE8w+3OVxsbybYRngJ qSSBzrTXynfnQRtlPyf2BveykhYTgTUC4fmewBnZP74o6bEgHBBfaNgVFV8DfJ+90+Va RwOPV8+8Sqv7THuomgLn4n5D0zaIQnhnJLlcpl4cuQbjdI1Dmgr5cCK8Qd65U1NKRiI2 OOBw== X-Gm-Message-State: AOJu0YzXMa5f68BSA9N2fuLAWIprYpT5/GVoiJhPvNt5iSJi/Qk4zMWq Ju01Ry+VOiIviNCgf0RLBgA3hTLADAJQp//byoArKu//BaguqrNVo4+A80W5GlUQjQEvF/8IZOG C6J8rT5bRlQrQSA== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:3451:b0:511:b40:8202 with SMTP id j17-20020a056512345100b005110b408202mr1955lfr.13.1706871371849; Fri, 02 Feb 2024 02:56:11 -0800 (PST) Date: Fri, 2 Feb 2024 10:55:43 +0000 In-Reply-To: <20240202-alice-file-v4-0-fc9c2080663b@google.com> 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> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7079; i=aliceryhl@google.com; h=from:subject:message-id; bh=/9E8jl2L0BIX668zHWJR7ToHHbaSj1Hq8IfbP9A+fIg=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlvMjJeoAg3CjA+Q8HoANiJqIfHhAzETLQqjC3A qx9NpkZ3AmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZbzIyQAKCRAEWL7uWMY5 RlXoD/sGN5Q4XsAlugRqT0Wr/qujcbxi+PwWXhHAB2Om5c2Jh4e00/uxSkhy1Hf4OIVlqG8aigU Ajb+uW7ABF4dCChXejn/wWF5tJ5mn5DrVL/buuyXdsfSPaolQukZEjFN9FU+Bs7dOgCMvJXIK+V 1gVwbxEg1jpJTso8gfhl9XV2/BSSdmINBdNmTafSuvCAtD/TIPAq4+JBXzK4xsfqNx7SXLArmCX WTnUy5BVZNSzk6bPNK5vyWNSmEC83ZBCkF0u/8gCtS8nJtMONJIozFMqO/XEMmbL8RQDwottXFp N+Cv7Lwv3yFWXLSB4CYWITqntU119Ck/Z9R3hT5r7cSQO6Fjh9c0lnLDNMGUVmHlOjuynpDsba/ F9rO9YQQMyBqnSeVbPa0tD3kOVVPfdsfb9ni5R+nIEeL7ru0XOmA2KbWg5JMSK2Q1Rfz3WdAzWw yQXKkEwJWkLKrk89xLiP3pFhrjXB4II1Odk44sFxoeVgjQIJU+DCH75GGeCDQ7LX083MII7D78r gXjUJeWbCbxbwDWqnrTMTKQReUwzpLts51b7vV/cZVG6XE25kyHSlso0Oi2LSXjJlQTZy4aWkNS A6RJ+zsustodt8E4lBs9Q0cpXutnd1EZgNjgy5uRYpDkLcTpCzoSwdNWphWBVdIDV4LqTkehaXn SXwLTwkCDJmFXCQ== X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240202-alice-file-v4-9-fc9c2080663b@google.com> Subject: [PATCH v4 9/9] rust: file: add abstraction for `poll_table` From: Alice Ryhl To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Peter Zijlstra , Alexander Viro , Christian Brauner , Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Dan Williams , Kees Cook , Matthew Wilcox , Thomas Gleixner Cc: Daniel Xu , Alice Ryhl , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" The existing `CondVar` abstraction is a wrapper around `wait_queue_head`, but it does not support all use-cases of the C `wait_queue_head` type. To be specific, a `CondVar` cannot be registered with a `struct poll_table`. This limitation has the advantage that you do not need to call `synchronize_rcu` when destroying a `CondVar`. However, we need the ability to register a `poll_table` with a `wait_queue_head` in Rust Binder. To enable this, introduce a type called `PollCondVar`, which is like `CondVar` except that you can register a `poll_table`. We also introduce `PollTable`, which is a safe wrapper around `poll_table` that is intended to be used with `PollCondVar`. The destructor of `PollCondVar` unconditionally calls `synchronize_rcu` to ensure that the removal of epoll waiters has fully completed before the `wait_queue_head` is destroyed. That said, `synchronize_rcu` is rather expensive and is not needed in all cases: If we have never registered a `poll_table` with the `wait_queue_head`, then we don't need to call `synchronize_rcu`. (And this is a common case in Binder - not all processes use Binder with epoll.) The current implementation does not account for this, but if we find that it is necessary to improve this, a future patch could store a boolean next to the `wait_queue_head` to keep track of whether a `poll_table` has ever been registered. Signed-off-by: Alice Ryhl --- rust/bindings/bindings_helper.h | 1 + rust/kernel/sync.rs | 1 + rust/kernel/sync/poll.rs | 117 ++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 rust/kernel/sync/poll.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index f4d9d04333c0..c651d38e5dd6 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index c1fb10fc64f4..84b69e337a55 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -11,6 +11,7 @@ mod condvar; pub mod lock; mod locked_by; +pub mod poll; pub use arc::{Arc, ArcBorrow, UniqueArc}; pub use condvar::{CondVar, CondVarTimeoutResult}; diff --git a/rust/kernel/sync/poll.rs b/rust/kernel/sync/poll.rs new file mode 100644 index 000000000000..a0e4f3de109a --- /dev/null +++ b/rust/kernel/sync/poll.rs @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Utilities for working with `struct poll_table`. + +use crate::{ + bindings, + file::File, + prelude::*, + sync::{CondVar, LockClassKey}, + types::Opaque, +}; +use core::ops::Deref; + +/// Creates a [`PollCondVar`] initialiser with the given name and a newly-created lock class. +#[macro_export] +macro_rules! new_poll_condvar { + ($($name:literal)?) => { + $crate::sync::poll::PollCondVar::new($crate::optional_name!($($name)?), $crate::static_lock_class!()) + }; +} + +/// Wraps the kernel's `struct poll_table`. +/// +/// # Invariants +/// +/// This struct contains a valid `struct poll_table`. +/// +/// For a `struct poll_table` to be valid, its `_qproc` function must follow the safety +/// requirements of `_qproc` functions: +/// +/// * The `_qproc` function is given permission to enqueue a waiter to the provided `poll_table` +/// during the call. Once the waiter is removed and an rcu grace period has passed, it must no +/// longer access the `wait_queue_head`. +#[repr(transparent)] +pub struct PollTable(Opaque); + +impl PollTable { + /// Creates a reference to a [`PollTable`] from a valid pointer. + /// + /// # Safety + /// + /// The caller must ensure that for the duration of 'a, the pointer will point at a valid poll + /// table (as defined in the type invariants). + /// + /// The caller must also ensure that the `poll_table` is only accessed via the returned + /// reference for the duration of 'a. + pub unsafe fn from_ptr<'a>(ptr: *mut bindings::poll_table) -> &'a mut PollTable { + // SAFETY: The safety requirements guarantee the validity of the dereference, while the + // `PollTable` type being transparent makes the cast ok. + unsafe { &mut *ptr.cast() } + } + + fn get_qproc(&self) -> bindings::poll_queue_proc { + let ptr = self.0.get(); + // SAFETY: The `ptr` is valid because it originates from a reference, and the `_qproc` + // field is not modified concurrently with this call since we have an immutable reference. + unsafe { (*ptr)._qproc } + } + + /// Register this [`PollTable`] with the provided [`PollCondVar`], so that it can be notified + /// using the condition variable. + pub fn register_wait(&mut self, file: &File, cv: &PollCondVar) { + if let Some(qproc) = self.get_qproc() { + // SAFETY: The pointers to `file` and `self` need to be valid for the duration of this + // call to `qproc`, which they are because they are references. + // + // The `cv.wait_queue_head` pointer must be valid until an rcu grace period after the + // waiter is removed. The `PollCondVar` is pinned, so before `cv.wait_queue_head` can + // be destroyed, the destructor must run. That destructor first removes all waiters, + // and then waits for an rcu grace period. Therefore, `cv.wait_queue_head` is valid for + // long enough. + unsafe { qproc(file.as_ptr() as _, cv.wait_queue_head.get(), self.0.get()) }; + } + } +} + +/// A wrapper around [`CondVar`] that makes it usable with [`PollTable`]. +/// +/// [`CondVar`]: crate::sync::CondVar +#[pin_data(PinnedDrop)] +pub struct PollCondVar { + #[pin] + inner: CondVar, +} + +impl PollCondVar { + /// Constructs a new condvar initialiser. + pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit { + pin_init!(Self { + inner <- CondVar::new(name, key), + }) + } +} + +// Make the `CondVar` methods callable on `PollCondVar`. +impl Deref for PollCondVar { + type Target = CondVar; + + fn deref(&self) -> &CondVar { + &self.inner + } +} + +#[pinned_drop] +impl PinnedDrop for PollCondVar { + fn drop(self: Pin<&mut Self>) { + // Clear anything registered using `register_wait`. + // + // SAFETY: The pointer points at a valid `wait_queue_head`. + unsafe { bindings::__wake_up_pollfree(self.inner.wait_queue_head.get()) }; + + // Wait for epoll items to be properly removed. + // + // SAFETY: Just an FFI call. + unsafe { bindings::synchronize_rcu() }; + } +} -- 2.43.0.594.gd9cf4e227d-goog