From nobody Fri Dec 19 08:04:42 2025 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 E4BEF3035D for ; Sat, 16 Dec 2023 15:31:53 +0000 (UTC) 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="m8Y3+oUm" Received: by mail-lf1-f74.google.com with SMTP id 2adb3069b0e04-50bfc3c3c5eso1476224e87.0 for ; Sat, 16 Dec 2023 07:31:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702740712; x=1703345512; 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=n21sZvx4OGz0fMgIbSc81FSV19tv9Tbnfac5je6hStI=; b=m8Y3+oUmLqKfjT0EcFYFUxcsv3jKvWJdMInKvCL/SSkihaOFi8KAPuuvicPlxFw/4h rD/R2MV0DmqJ9a6oyXo7s0Ny3grC+QoGsYXGbuVAuonST1a1WlXIiiwCfxFBlk8B494u xRYWn6OCBD2AQvgv5uZmkHJD+jo8u+VPpUmS1H3MKTx0izb+RCC/normYydep16boR1A CKHplCN3jZlE11upMPykQ+HoyyJxJEn392FMCYfo3R1WIktn4XJbdsEh/R3JPG/jrpPv dYZ0IVFs2BDguI/7po7j1EET1fjUDIqEaZTI3Y4qxtl1FixYXM43zAHBA61Wwt9R0iDX xghw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702740712; x=1703345512; 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=n21sZvx4OGz0fMgIbSc81FSV19tv9Tbnfac5je6hStI=; b=ka/ROpYV88o5fhIpfgyiZqhTI+b5PrgLWgYsJ66qAXNZH7jA4i4wAWME9aObI2NO8y eTxDHe8k2P2EuEM1YhMUBuH9+ltMAScSDlFBNuTDro0JseHH9r9PzVvAMzHvVafyninX IVKa7KVdV6TxbuGJYWj4nbVjyNFK/2OpUYwXzpVLR0fjXWmnsiMmKDGeHNZaxHo1xYWX Cb3gggKaKLuVxLsAOuzBrJ5DvwVCVezxLu2hwTJydNdIN+lVR4tzfKPGPcnLiXArnQub B0psOJDBy/5TTALWXjpT2wq4UoWFgqtOXaplomld/Uf9zvLjwX+u31OgXOryoEnoxcqC nBHA== X-Gm-Message-State: AOJu0YwrUxuVKAx4Uh58LmkH6+Z23G2CEdCCgKFGp5LKwi+CkQUp108F ScHRSv1aknGv8f3T3UeOGMDw0UxnLwKE0T0= X-Google-Smtp-Source: AGHT+IFPb67LbaPE6VM7e+gjZkusqwNzggRxuRvHf6JAMNULNkMI2Zdv1CznDmLXgtlPx/vQv8kTuZ84bMJe+jc= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:118a:b0:50c:21d8:40da with SMTP id g10-20020a056512118a00b0050c21d840damr273029lfr.1.1702740711767; Sat, 16 Dec 2023 07:31:51 -0800 (PST) Date: Sat, 16 Dec 2023 15:31:39 +0000 In-Reply-To: <20231216-rb-new-condvar-methods-v2-0-b05ab61e6d5b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231216-rb-new-condvar-methods-v2-0-b05ab61e6d5b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1451; i=aliceryhl@google.com; h=from:subject:message-id; bh=sq4o+0fCxkYUC2mgTGvD+vhZ6Pja4Fjl7gEOQStlLeY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlfcLf7qSifHao52i6f1nLRLHyYtY4Wn5qsuFpj TxPIj7uwFSJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZX3C3wAKCRAEWL7uWMY5 RjJRD/4nBUX0NzHvzLrX/JKnInVQ1ftZ0lCrZTOqIzVNp3nZ5eAoilchBKaN9oS9VxiWwpjJpQ5 LyW/kPMb2mqVh/qRKz82S1MjhDHKQXy66HQ43BscfEstQEgIj+hxNWJ+c03r9jpXTjo92s4WPM8 CUM3Dc5ojYbE4IGW+5xi2KnXTCiRomyWmmxrJpNchhQ9w3TNxkxFwHsDD/gWXMlCJvRC2VnPnGY HUKLB4S2cruSCqRFQnkFhSBxfo0BME2OW0VWzhJUiHG1rp0yjgYjjApMFfPwghwUcHqKWvPsDvW 1MsOc5CbsN06/WNCNJz956fVkSRrm03cxbRlG4UuuZ9wBKOj5ePmYSkN47e5HgsvYPKyASjtPp4 W4FM4V9TPqN9d9RubDZCYsAzS74AMtzOOdcPoC40k6kFmiSBLh+RZnk3bKw4HPrnH45ZgUqaRch V9BVCM8G5p2ZhroNbe4FOt1ek56hhP1iftlgzESoNLUA/7+tH8ULFeIfNOjVAaSPx/YO85eLZE/ xFWkTbzFTFzD+tYvYVbOmDPbTMFY+TLFvMHjHMZf7CXqD42DsM3O3hO8oPmd2N81WawVLWE19WO 7UHAHYzQmOfwHYx7DstBYzg1pnkJQgJofA646MiFp61+oG9lfeVh9gKMFuabZlnv8jynOaluQYi tV0RYgXEtK0WPzw== X-Mailer: b4 0.13-dev-26615 Message-ID: <20231216-rb-new-condvar-methods-v2-1-b05ab61e6d5b@google.com> Subject: [PATCH v2 1/4] rust: sync: add `CondVar::notify_sync` From: Alice Ryhl To: 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 , Ingo Molnar , Will Deacon , Waiman Long , Tiago Lam , Thomas Gleixner Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl , Martin Rodriguez Reboredo Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Wake up another thread synchronously. This method behaves like `notify_one`, except that it hints to the scheduler that the current thread is about to go to sleep, so it should schedule the target thread on the same CPU. This is used by Rust Binder as a performance optimization. When sending a transaction to a different process, we usually know which thread will handle it, so we can schedule that thread for execution next on this CPU for better cache locality. Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Alice Ryhl Reviewed-by: Tiago Lam --- rust/kernel/sync/condvar.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index 8630faa29b78..9331eb606738 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -155,6 +155,12 @@ fn notify(&self, count: i32, flags: u32) { }; } =20 + /// Calls the kernel function to notify one thread synchronously. + pub fn notify_sync(&self) { + // SAFETY: `wait_list` points to valid memory. + unsafe { bindings::__wake_up_sync(self.wait_list.get(), bindings::= TASK_NORMAL) }; + } + /// Wakes a single waiter up, if any. /// /// This is not 'sticky' in the sense that if no thread is waiting, th= e notification is lost --=20 2.43.0.472.g3155946c3a-goog From nobody Fri Dec 19 08:04:42 2025 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 CD6F830677 for ; Sat, 16 Dec 2023 15:31:56 +0000 (UTC) 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zFK+QRWX" Received: by mail-lf1-f74.google.com with SMTP id 2adb3069b0e04-50be5bdae9fso1422176e87.1 for ; Sat, 16 Dec 2023 07:31:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702740715; x=1703345515; 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=5XdxQEaIrLyM2shqQ86h8R9r1OVkGiHgwK8DLLX+9PM=; b=zFK+QRWXtRe2wERG8G50NQvXPYv05St1zsnKjVU4S6dYvFqPZf7CHiBO4Cc5+sM1WB uKhIQLt2wNPjwkv1RpRfRD49stc/4VZE+yoZyGdrT8ms7lIlLHLoUT7Q1RAs9IpYoKbh rnP9XAvnckJ2g+CjGR84rI+Bamd3YWXbhqEz016xJ2BVLkppldBg16k2r/sE0uaVfgxN g2lKNN5JcDK6Q4jsA84S/NAH9XvWBMQUzOdTHnhHuaJH08VmXnF4+O/QSpxjt1KH6WZE nx2v+IcESJU7EN7G3RwASBtaBQcwuivzgoOC2+HfzTVqY/itiio2KzDRLvev5vA8a9un ad2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702740715; x=1703345515; 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=5XdxQEaIrLyM2shqQ86h8R9r1OVkGiHgwK8DLLX+9PM=; b=KuKQLfFs9nxDkEjvLfTbiEugKo9lVEY8t76ygGHUxaZJN5NBFrrkR62fOxKo35zcXp g1TE9xz3Uhe48g70NekmrL+/qa6JU+ZPqPw3JhvmuiP12qVhjjZQBLwiS14dtO3asnGt 0SUGsNYDwWJ3g2DVgDoEa5LeCF8gy0slcjP5T59dJqgYHUXKlwgYQ4z2CP+crV2Cfqua FJbFim2/IZGdQ6amZSNFYdPcIKdHToXdVfIM5/NzTUR18ZEzaHRJHUvNuxmWMg9VkyFZ rwgncW5nuQl8gFtuswq6OBKaMYq8dB70OBbV7GkAQVB7/7wHRnhve79P0us5DZBDdk78 u6pA== X-Gm-Message-State: AOJu0YwxULy1S9e8+Vd2+L6zb7AWBnvjgMwI8WfEQ0nuuNFZuEps2aSO gUj0TWscv6OxKfshxTu7433sQkVgXAjpO+M= X-Google-Smtp-Source: AGHT+IEthAYpaHDCMjYR8nUdZ+/YnPDTYSr6eCrXIphwuuiveREFJUiTzweZakr2KUYW/17OoW2BModVj1nx+Sw= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:3ba8:b0:50b:e73f:da50 with SMTP id g40-20020a0565123ba800b0050be73fda50mr120667lfv.1.1702740714659; Sat, 16 Dec 2023 07:31:54 -0800 (PST) Date: Sat, 16 Dec 2023 15:31:40 +0000 In-Reply-To: <20231216-rb-new-condvar-methods-v2-0-b05ab61e6d5b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231216-rb-new-condvar-methods-v2-0-b05ab61e6d5b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2389; i=aliceryhl@google.com; h=from:subject:message-id; bh=TlYeKLnmj5lAoBU99t4m6Fy/w/vV4RiIS0XGhAp4JT4=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlfcLgl4BrfTd4DVnj7BHnDlDKdqC79XAmSxNM+ 8SzFF0LZ0+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZX3C4AAKCRAEWL7uWMY5 RmufD/4/uTwFp1pjkeBY8c/cz9iCxJLYt+glC3U5xg6hjNcM0khejVJBGTqPetTD2JgWYkqWfy6 vXCxpFAgeSdTRH4+ChLY+DmfKlp+V3rUKRN4gNEgzKgIK6tovaHzx3ooH7Q5V+Jqb+jH6aRR8gX 2s8VGYTDnLT8r2ahWai+tHJCuqr4gEt8rn/DMJgpIJCDA8XreDpDGwmvW1Esc3aLL9OJlHV2d1x Jpx67gsRo8Y9xRZLTXV+qQEQe31fP+BjWdRsuyKiQjWJc9hh31QhtpbxpYjeSl7yFg9J/XoAXXw 3KO22adl2WDRbfTPlw+zL/S01N4EVTxe+nS9BcQdE7aMCigLwOJWFq3D6uewR1Uj+fW3WSy7Bgk C82NwBXOe6iJz1BsfF2n+iaVquFpXURwlfUJDkbk/s60IpjZ8/XovZOdZ2s4ArxrZTxBSvv6yjk 97XvPnZXeC4T2cwp/pxZDl/okOpGj5ylmt0ozU5bs6Ks45cX3Q0vFhonlbJwa4eLx8raLkF9ttX OI2jp3S6PJA3KnUiNNNsMZdl1kDQkxq9xc4Frm9no4K9iH1/0uhAinxYqUJJg5aH3ct+mj0s3GV 9X0p4FSoXpLwUj21QJBj52KSO8TogltLSmZZUX520Mm0J15waUfpjGq5MlLQAvALZTBov2yt4k1 nQRtMKZRz2kiilw== X-Mailer: b4 0.13-dev-26615 Message-ID: <20231216-rb-new-condvar-methods-v2-2-b05ab61e6d5b@google.com> Subject: [PATCH v2 2/4] rust: time: add msecs to jiffies conversion From: Alice Ryhl To: 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 , Ingo Molnar , Will Deacon , Waiman Long , Tiago Lam , Thomas Gleixner Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Defines type aliases and conversions for msecs and jiffies. This is used by Rust Binder for process freezing. There, we want to sleep until the freeze operation completes, but we want to be able to abort the process freezing if it doesn't complete within some timeout. The freeze timeout is supplied in msecs. Note that we need to convert to jiffies in Binder. It is not enough to introduce a variant of `CondVar::wait_timeout` that takes the timeout in msecs because we need to be able to restart the sleep with the remaining sleep duration if it is interrupted, and if the API takes msecs rather than jiffies, then that would require a conversion roundtrip jiffies-> msecs->jiffies that is best avoided. Suggested-by: Boqun Feng Signed-off-by: Alice Ryhl Reviewed-by: Benno Lossin Reviewed-by: Boqun Feng Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Tiago Lam --- rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 1 + rust/kernel/time.rs | 17 +++++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 85f013ed4ca4..c482c8018f3d 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -8,6 +8,7 @@ =20 #include #include +#include #include #include #include diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index e6aff80b521f..d4f90acdd517 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -46,6 +46,7 @@ pub mod str; pub mod sync; pub mod task; +pub mod time; pub mod types; pub mod workqueue; =20 diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs new file mode 100644 index 000000000000..23c4d1a74f68 --- /dev/null +++ b/rust/kernel/time.rs @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Timers. + +/// The time unit of Linux kernel. One jiffy equals (1/HZ) second. +pub type Jiffies =3D core::ffi::c_ulong; + +/// The millisecond time unit. +pub type Msecs =3D core::ffi::c_uint; + +/// Converts milliseconds to jiffies. +#[inline] +pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { + // SAFETY: The `__msecs_to_jiffies` function is always safe to call no + // matter what the argument is. + unsafe { bindings::__msecs_to_jiffies(msecs) } +} --=20 2.43.0.472.g3155946c3a-goog From nobody Fri Dec 19 08:04:42 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 159D730F8F for ; Sat, 16 Dec 2023 15:31:58 +0000 (UTC) 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="O4GYevy6" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5cf4696e202so19858577b3.2 for ; Sat, 16 Dec 2023 07:31:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702740718; x=1703345518; 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=T/ddlBRhTE0D+FYTRDCtGuJMObI89/Ad+6YQTmYW7YA=; b=O4GYevy65C8yLpH5APpvEknmJnjmCzn5uPsm6HRyvcXFutxrvG+DD3zjZkZJFwul/K MT8Q10Oy2QEaKCVt0VjmO/lJWJ+GPihxMjbOw78/hqscF8miJnL2NuCEjnhiZGND6gh1 PvoxwtY7u2sFdDs2g0bHTCyF8zbQo2qmUTKHtIpaYgaeYy0SVGBNH5LGWgEHf4G32IAT JKlGQGOV1d+Ud60nYOonqGBLPwU9+bQYNLaLc6GLBtVMi6qBux5y+5Lt0XAcGSPoW8sR nxv5j+1sUNhL9dZWa/sJtlGTiJGe65V3Y+peLa7xUun412cqe1yEJjmze5ksk+uT/BpL HCuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702740718; x=1703345518; 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=T/ddlBRhTE0D+FYTRDCtGuJMObI89/Ad+6YQTmYW7YA=; b=Fu7ksUgvk8AYFWJVAo+qGQ15WuZWxFDz6EXbjGlEJy0QmZUFE4lj9IND0toeSEJfWP v3YK7ZOazks3WWAGGJVzvvGSSQ8JtkkULMPhA0IznXIXJ8LFqy0Achrb2EvrQqvIBlO8 RYmYhTEaJCytPLMQJ2sZfMxzTx05XSn8y1zI8RTI2MLJVaELfHoxIyuwx9NlYD98/9aV 4+hJaK0MBQR4hHrTk31t/ZRETeo1mrFR83MUuGACtPdXA5Sv1MeHmYetld0h37Cu6U/5 izHyZgs8ZU39Hb9BZ0YxgVwlnECfpj/lxj/EqR9D7YwXz+mQ14/uGU/0gh8sg9VKUKfC fQUA== X-Gm-Message-State: AOJu0Yz92rXejU6i6MCcHj/ABOye3D+6VHxJfoOBgrNgWXrDrUFSAwiA yrAacR98O1t0B6J7YazgPaVJIWJn/tr+5Hs= X-Google-Smtp-Source: AGHT+IGVKMCUskWZKxPFNRIG6Xo50mlBjoKSY7G0F4Z6hsa3Ztbi2UoKKWL4LxG+4j3srMjp3appHKyqifwdlX8= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:690c:a8c:b0:5d7:8bf2:de42 with SMTP id ci12-20020a05690c0a8c00b005d78bf2de42mr772473ywb.9.1702740718105; Sat, 16 Dec 2023 07:31:58 -0800 (PST) Date: Sat, 16 Dec 2023 15:31:41 +0000 In-Reply-To: <20231216-rb-new-condvar-methods-v2-0-b05ab61e6d5b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231216-rb-new-condvar-methods-v2-0-b05ab61e6d5b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6461; i=aliceryhl@google.com; h=from:subject:message-id; bh=uF4Q1+xxQjulRgS6F2spL0AmSmj4UawnBJX/beYz82U=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlfcLg/1Olosyur+zr0QwGGVAhRokjwG/43yx/u 5HP8Eqs2T6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZX3C4AAKCRAEWL7uWMY5 RpgkD/0VzqMF0pkgpx5FVS6mKRSKNy2iC81KBiuS3iCje6w1qE7cQUwVUzztbmbMKxUTot5eDeB GWkUEmNPv9wamZc6JOd9Qp/lE/IusUyh3KX+EjcI3uu2ez2WDSd0TpDxrZe6zYJUAlDWcIfLY/d Dt+pSdSF/kgbWFMugAapMON6+YIAjBwD++SogKtA7b85i7OgnwT6Zz73a71hD9IUa4GxznEX0VN WXKt8QclmH2JMbcXzn5OSYid9VmUh3ip61MXHMQYoKC6Kt9XYBfX2KVZJmIgxsIIW3OcUf9lArf wPsIUR65sh7J+p4xCiKGz+uEAGn9/Eqw1mZyq3L1/0Hze2+0S+ZmsWcVP5WeoRqEUbmQIyck36R F9qLgwehSaLY01SV7MVjzBK/78ViN2ficq8TrJZ2hPlJtvWOJ2Gf/MGXWexUuHTMOKUSqq5uZon r0ZqUQGhbqu6bcMuG+U04nWwSPrIpdWTIJ5HY+uDVs+pSHQtCDJA0CBapRW0/BjrpaJq1WQkj6x l4DvpAp28A7PeVcp3HDnyOHDg0pNmN16D4XdnC+lUD+EAOht3KIVHZ45JglrrkRVy7g2fxPzlsE xCc61pfs9BenRJBOHFuGaKersznboXJoZq5wJu+V6h9MHSuKUS3t/hZHgOopjVL9TfsfEZgJeHn wOQfQLCEr6hFYhg== X-Mailer: b4 0.13-dev-26615 Message-ID: <20231216-rb-new-condvar-methods-v2-3-b05ab61e6d5b@google.com> Subject: [PATCH v2 3/4] rust: sync: add `CondVar::wait_timeout` From: Alice Ryhl To: 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 , Ingo Molnar , Will Deacon , Waiman Long , Tiago Lam , Thomas Gleixner Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sleep on a condition variable with a timeout. This is used by Rust Binder for process freezing. There, we want to sleep until the freeze operation completes, but we want to be able to abort the process freezing if it doesn't complete within some timeout. Signed-off-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Tiago Lam --- rust/kernel/sync/condvar.rs | 59 ++++++++++++++++++++++++++++++++++++++++-= ---- rust/kernel/sync/lock.rs | 4 +-- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index 9331eb606738..0176cdfced6c 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -6,7 +6,8 @@ //! variable. =20 use super::{lock::Backend, lock::Guard, LockClassKey}; -use crate::{bindings, init::PinInit, pin_init, str::CStr, types::Opaque}; +use crate::{bindings, init::PinInit, pin_init, str::CStr, time::Jiffies, t= ypes::Opaque}; +use core::ffi::c_long; use core::marker::PhantomPinned; use macros::pin_data; =20 @@ -18,6 +19,8 @@ macro_rules! new_condvar { }; } =20 +const MAX_SCHEDULE_TIMEOUT: c_long =3D c_long::MAX; + /// A conditional variable. /// /// Exposes the kernel's [`struct wait_queue_head`] as a condition variabl= e. It allows the caller to @@ -102,7 +105,12 @@ pub fn new(name: &'static CStr, key: &'static LockClas= sKey) -> impl PinInit(&self, wait_state: u32, guard:= &mut Guard<'_, T, B>) { + fn wait_internal( + &self, + wait_state: u32, + guard: &mut Guard<'_, T, B>, + timeout: c_long, + ) -> c_long { let wait =3D Opaque::::uninit(); =20 // SAFETY: `wait` points to valid memory. @@ -113,11 +121,13 @@ fn wait_internal(&self, wait_s= tate: u32, guard: &mut Guar bindings::prepare_to_wait_exclusive(self.wait_list.get(), wait= .get(), wait_state as _) }; =20 - // SAFETY: No arguments, switches to another thread. - guard.do_unlocked(|| unsafe { bindings::schedule() }); + // SAFETY: Switches to another thread. The timeout can be any numb= er. + let ret =3D guard.do_unlocked(|| unsafe { bindings::schedule_timeo= ut(timeout) }); =20 // SAFETY: Both `wait` and `wait_list` point to valid memory. unsafe { bindings::finish_wait(self.wait_list.get(), wait.get()) }; + + ret } =20 /// Releases the lock and waits for a notification in uninterruptible = mode. @@ -127,7 +137,7 @@ fn wait_internal(&self, wait_sta= te: u32, guard: &mut Guar /// [`CondVar::notify_one`] or [`CondVar::notify_all`]. Note that it m= ay also wake up /// spuriously. pub fn wait(&self, guard: &mut Guard<'_, T, B>)= { - self.wait_internal(bindings::TASK_UNINTERRUPTIBLE, guard); + self.wait_internal(bindings::TASK_UNINTERRUPTIBLE, guard, MAX_SCHE= DULE_TIMEOUT); } =20 /// Releases the lock and waits for a notification in interruptible mo= de. @@ -138,10 +148,31 @@ pub fn wait(&self, guard: &mut= Guard<'_, T, B>) { /// Returns whether there is a signal pending. #[must_use =3D "wait_interruptible returns if a signal is pending, so = the caller must check the return value"] pub fn wait_interruptible(&self, guard: &mut Gu= ard<'_, T, B>) -> bool { - self.wait_internal(bindings::TASK_INTERRUPTIBLE, guard); + self.wait_internal(bindings::TASK_INTERRUPTIBLE, guard, MAX_SCHEDU= LE_TIMEOUT); crate::current!().signal_pending() } =20 + /// Releases the lock and waits for a notification in interruptible mo= de. + /// + /// Atomically releases the given lock (whose ownership is proven by t= he guard) and puts the + /// thread to sleep. It wakes up when notified by [`CondVar::notify_on= e`] or + /// [`CondVar::notify_all`], or when a timeout occurs, or when the thr= ead receives a signal. + #[must_use =3D "wait_interruptible_timeout returns if a signal is pend= ing, so the caller must check the return value"] + pub fn wait_interruptible_timeout( + &self, + guard: &mut Guard<'_, T, B>, + jiffies: Jiffies, + ) -> CondVarTimeoutResult { + let jiffies =3D jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT); + let res =3D self.wait_internal(bindings::TASK_INTERRUPTIBLE, guard= , jiffies); + + match (res as Jiffies, crate::current!().signal_pending()) { + (jiffies, true) =3D> CondVarTimeoutResult::Signal { jiffies }, + (0, false) =3D> CondVarTimeoutResult::Timeout, + (jiffies, false) =3D> CondVarTimeoutResult::Woken { jiffies }, + } + } + /// Calls the kernel function to notify the appropriate number of thre= ads with the given flags. fn notify(&self, count: i32, flags: u32) { // SAFETY: `wait_list` points to valid memory. @@ -177,3 +208,19 @@ pub fn notify_all(&self) { self.notify(0, 0); } } + +/// The return type of `wait_timeout`. +pub enum CondVarTimeoutResult { + /// The timeout was reached. + Timeout, + /// Somebody woke us up. + Woken { + /// Remaining sleep duration. + jiffies: Jiffies, + }, + /// A signal occurred. + Signal { + /// Remaining sleep duration. + jiffies: Jiffies, + }, +} diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index f12a684bc957..149a5259d431 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -139,7 +139,7 @@ pub struct Guard<'a, T: ?Sized, B: Backend> { unsafe impl Sync for Guard<'_, T, B> {} =20 impl Guard<'_, T, B> { - pub(crate) fn do_unlocked(&mut self, cb: impl FnOnce()) { + pub(crate) fn do_unlocked(&mut self, cb: impl FnOnce() -> U) -> U { // SAFETY: The caller owns the lock, so it is safe to unlock it. unsafe { B::unlock(self.lock.state.get(), &self.state) }; =20 @@ -147,7 +147,7 @@ pub(crate) fn do_unlocked(&mut self, cb: impl FnOnce())= { let _relock =3D ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &= mut self.state) }); =20 - cb(); + cb() } } =20 --=20 2.43.0.472.g3155946c3a-goog From nobody Fri Dec 19 08:04:42 2025 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 E4CD731A62 for ; Sat, 16 Dec 2023 15:32:02 +0000 (UTC) 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="H1Z3UEkM" Received: by mail-lf1-f74.google.com with SMTP id 2adb3069b0e04-50e186d2e54so855161e87.0 for ; Sat, 16 Dec 2023 07:32:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702740721; x=1703345521; 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=LT/VzEMQfjBunMwUHNaq3eIbr3dP2qdLLUarTDZYcms=; b=H1Z3UEkMZFREGggHV7ZQ0QIaHzNazUnLuhx1EwBokOoSEKK/SjxLiRxWY7wrYd9ij0 yfLbWBml/KhASTzvK1RjwhwW2Z0QG+uZE+WjcXgbl2vaACOP24f6v5Foolp90Bpjz6yO wScuYmPwbnr9rAzschBJZuLx5NGWffScyJGRK6PH50LTQ6WcY+FV02OQa139S8JbXCX0 Y/7be6hkO16OX1tkfngfxta1gcMvxJRRziHDGtgzBBZ/MvGqnqZ03gIfnbQTFFbIFipl C4HiTWWukjZoqdvG76+ofzQCTiChbsJl0dxiG/bCKEklHeCgXvIDftJxadmWskCCsxjx e1hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702740721; x=1703345521; 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=LT/VzEMQfjBunMwUHNaq3eIbr3dP2qdLLUarTDZYcms=; b=qAN0LwQ6g7dsuWLKXbk2eVcZQHpNc9UI+VgrS9NXoyY5kOtS+1fRHw+2yMmeLFej/a lIoili/4UWOethKU+Ezsxa3XoipR/vSNxrx2FIOezonAf0AQUudMgFbQGv4dZeSZmEd6 t92Srez2IZhozDwaL/YG242Uzo5G/bL6yU9VlIJnEFK1w/S1Q//4fDd4pAbMq1y40TlO 0WLwSlf/HOPhV0iZKCKVqkyKEC4SQInhskq1sMtyNEuEiAUpNMAgnDCA4BNBIkToaPMF 6XdAuqi/pl2Xjt3rufRjUISCpAGr1p+WDdgHWpjqDR/NQRnF8uM7golgFm943bVI34s1 chXw== X-Gm-Message-State: AOJu0YwtfV7vv4ByiarZR82qnvyKi4vhwWICne6uynyEsSJhzhYsm5X4 bcEjxaKTvGWW2Y+/PD79h4VJOe1D31Y2R+U= X-Google-Smtp-Source: AGHT+IEeQkujUyn7byhfH//rFftMEYy8PmIQJoAfSRVwDhH+9P8dJYHuhDG6N2lAzW2iKGo8kQoZ2mdSjHFCJ8c= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:3d26:b0:50e:d49:156b with SMTP id d38-20020a0565123d2600b0050e0d49156bmr157415lfv.4.1702740720972; Sat, 16 Dec 2023 07:32:00 -0800 (PST) Date: Sat, 16 Dec 2023 15:31:42 +0000 In-Reply-To: <20231216-rb-new-condvar-methods-v2-0-b05ab61e6d5b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231216-rb-new-condvar-methods-v2-0-b05ab61e6d5b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6401; i=aliceryhl@google.com; h=from:subject:message-id; bh=KG/slwqGOF9oUMO0B4L9hVCJ3TxK7vKmJVhXl8LrvtA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlfcLhMVsGyqik/ZrsWXlhk/NLtw/iwmomeJiSF M9Y9/WNHquJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZX3C4QAKCRAEWL7uWMY5 RtqyD/4lcnppEcnJRpQ1qOnneQhj2yuOzr8/sqfKCZHmi9EULNaKY8oViaX769lgBQw3xoFs0rA qkmUcCCExApGShw3/uhfkrDxmnmph14066YT95tCVqgF9nRu+Y7ZtLQcUM2rDqh8LVnIHwTkff9 iQDMIV03bTXVeE5WSJ+Ccpvu2hM0EHWFFwETNtwGYEzXObSyL3Wi+nPxrj1ZYn6+/smCeF1iGTn CqV5NVg4pNjaKRtJU+taWKyZ8Nd9zgAeu4HX8MVDeqT4oklz1aMqmyg0vNwbYMYM5bmKWHEB0KM qEg9PEMZaKVd10f+g3Uf6LMlipdZcDxuAnoIrAAz0veeA228JLqm+SZS/kOFwymH9dNq5uqi50j 5osHDO1X7ONdB4+B/YhJAqRVvTSV9t25NXlwOxY9fqwk26hT2YDkLLRsyXhM314P0a6pADYPgvj ZRMRJ85+b2NL4CMo0l9XzYs7j5nFs5avS2VILaaBUflkYfItkMLEuvjbhfBuGTQ+auF+Ac8HFPs VMyAjRP8csGxJXL/2q29Rem472/mYn23SKlcvBmqwTNUiLV2G/qcTaxvPZ6SOZhgixarjUz9ZMb lEoPCvfNVnpcRTyEHlwFHcwXQOwvjbUzTGGqLWYIROztiXE4VbyC4q/eDSJiHiolB2eejS3U2Gv s2KVDfd4Qpn6ZaA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20231216-rb-new-condvar-methods-v2-4-b05ab61e6d5b@google.com> Subject: [PATCH v2 4/4] rust: sync: update integer types in CondVar From: Alice Ryhl To: 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 , Ingo Molnar , Will Deacon , Waiman Long , Tiago Lam , Thomas Gleixner Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Reduce the chances of compilation failures due to integer type mismatches in `CondVar`. When an integer is defined using a #define in C, bindgen doesn't know which integer type it is supposed to be, so it will just use `u32` by default (if it fits in an u32). Whenever the right type is something else, we insert a cast in Rust. However, this means that the code has a lot of extra casts, and sometimes the code will be missing casts if u32 happens to be correct on the developer's machine, even though the type might be something else on a different platform. This patch updates all uses of such constants in `rust/kernel/sync/condvar.rs` to use constants defined with the right type. This allows us to remove various unnecessary casts, while also future-proofing for the case where `unsigned int !=3D u32`. I wrote this patch at the suggestion of Benno in [1]. Link: https://lore.kernel.org/all/nAEg-6vbtX72ZY3oirDhrSEf06TBWmMiTt73EklMz= EAzN4FD4mF3TPEyAOxBZgZtjzoiaBYtYr3s8sa9wp1uYH9vEWRf2M-Lf4I0BY9rAgk=3D@proto= n.me/ [1] Suggested-by: Benno Lossin Signed-off-by: Alice Ryhl Reviewed-by: Benno Lossin Reviewed-by: Boqun Feng Reviewed-by: Tiago Lam --- rust/kernel/sync/condvar.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index 0176cdfced6c..a0d45dc97661 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -7,7 +7,7 @@ =20 use super::{lock::Backend, lock::Guard, LockClassKey}; use crate::{bindings, init::PinInit, pin_init, str::CStr, time::Jiffies, t= ypes::Opaque}; -use core::ffi::c_long; +use core::ffi::{c_int, c_long, c_uint}; use core::marker::PhantomPinned; use macros::pin_data; =20 @@ -21,6 +21,10 @@ macro_rules! new_condvar { =20 const MAX_SCHEDULE_TIMEOUT: c_long =3D c_long::MAX; =20 +const TASK_NORMAL: c_uint =3D bindings::TASK_NORMAL as c_uint; +const TASK_INTERRUPTIBLE: c_int =3D bindings::TASK_INTERRUPTIBLE as c_int; +const TASK_UNINTERRUPTIBLE: c_int =3D bindings::TASK_UNINTERRUPTIBLE as c_= int; + /// A conditional variable. /// /// Exposes the kernel's [`struct wait_queue_head`] as a condition variabl= e. It allows the caller to @@ -107,7 +111,7 @@ pub fn new(name: &'static CStr, key: &'static LockClass= Key) -> impl PinInit( &self, - wait_state: u32, + wait_state: c_int, guard: &mut Guard<'_, T, B>, timeout: c_long, ) -> c_long { @@ -118,7 +122,7 @@ fn wait_internal( =20 // SAFETY: Both `wait` and `wait_list` point to valid memory. unsafe { - bindings::prepare_to_wait_exclusive(self.wait_list.get(), wait= .get(), wait_state as _) + bindings::prepare_to_wait_exclusive(self.wait_list.get(), wait= .get(), wait_state) }; =20 // SAFETY: Switches to another thread. The timeout can be any numb= er. @@ -137,7 +141,7 @@ fn wait_internal( /// [`CondVar::notify_one`] or [`CondVar::notify_all`]. Note that it m= ay also wake up /// spuriously. pub fn wait(&self, guard: &mut Guard<'_, T, B>)= { - self.wait_internal(bindings::TASK_UNINTERRUPTIBLE, guard, MAX_SCHE= DULE_TIMEOUT); + self.wait_internal(TASK_UNINTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEO= UT); } =20 /// Releases the lock and waits for a notification in interruptible mo= de. @@ -148,7 +152,7 @@ pub fn wait(&self, guard: &mut G= uard<'_, T, B>) { /// Returns whether there is a signal pending. #[must_use =3D "wait_interruptible returns if a signal is pending, so = the caller must check the return value"] pub fn wait_interruptible(&self, guard: &mut Gu= ard<'_, T, B>) -> bool { - self.wait_internal(bindings::TASK_INTERRUPTIBLE, guard, MAX_SCHEDU= LE_TIMEOUT); + self.wait_internal(TASK_INTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT= ); crate::current!().signal_pending() } =20 @@ -164,7 +168,7 @@ pub fn wait_interruptible_timeout( jiffies: Jiffies, ) -> CondVarTimeoutResult { let jiffies =3D jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT); - let res =3D self.wait_internal(bindings::TASK_INTERRUPTIBLE, guard= , jiffies); + let res =3D self.wait_internal(TASK_INTERRUPTIBLE, guard, jiffies); =20 match (res as Jiffies, crate::current!().signal_pending()) { (jiffies, true) =3D> CondVarTimeoutResult::Signal { jiffies }, @@ -174,22 +178,15 @@ pub fn wait_interruptible_timeout( } =20 /// Calls the kernel function to notify the appropriate number of thre= ads with the given flags. - fn notify(&self, count: i32, flags: u32) { + fn notify(&self, count: c_int) { // SAFETY: `wait_list` points to valid memory. - unsafe { - bindings::__wake_up( - self.wait_list.get(), - bindings::TASK_NORMAL, - count, - flags as _, - ) - }; + unsafe { bindings::__wake_up(self.wait_list.get(), TASK_NORMAL, co= unt, ptr::null_mut()) }; } =20 /// Calls the kernel function to notify one thread synchronously. pub fn notify_sync(&self) { // SAFETY: `wait_list` points to valid memory. - unsafe { bindings::__wake_up_sync(self.wait_list.get(), bindings::= TASK_NORMAL) }; + unsafe { bindings::__wake_up_sync(self.wait_list.get(), TASK_NORMA= L) }; } =20 /// Wakes a single waiter up, if any. @@ -197,7 +194,7 @@ pub fn notify_sync(&self) { /// This is not 'sticky' in the sense that if no thread is waiting, th= e notification is lost /// completely (as opposed to automatically waking up the next waiter). pub fn notify_one(&self) { - self.notify(1, 0); + self.notify(1); } =20 /// Wakes all waiters up, if any. @@ -205,7 +202,7 @@ pub fn notify_one(&self) { /// This is not 'sticky' in the sense that if no thread is waiting, th= e notification is lost /// completely (as opposed to automatically waking up the next waiter). pub fn notify_all(&self) { - self.notify(0, 0); + self.notify(0); } } =20 --=20 2.43.0.472.g3155946c3a-goog