Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp1670923rdb; Mon, 8 Jan 2024 06:51:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IEynGrYtkUdHxlAhvN9jhfIyCmSg2BqfTdILLFCBMce9uKAOzXn5XJID0F/08nT9MmaMtgo X-Received: by 2002:a05:6402:2713:b0:557:7501:4242 with SMTP id y19-20020a056402271300b0055775014242mr3303676edd.12.1704725478260; Mon, 08 Jan 2024 06:51:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704725478; cv=none; d=google.com; s=arc-20160816; b=dkOYolA87GmHD7yExCCpiVCrjmUlnnZfp/C5w3pF4C5hgGceAXdfXhLc/FuEMmvVr4 xBJ0HXY1Glw6UV19WVuZ7JNyZyCUr6Ldoly0bhlM1wpJIGWNnMhRBzt4ltmQEAJZAXEJ RosGtMtYzIOoJ1psdsRqmKyC5FxOpPLP+GhyJTyPNMIIpWDSwkqndRzbBMgbuIn3D2CU fzZ9P1qTnAcAWq0uVYbK2hFae55x5Cq5BUW+yLZp9jsjaenZFmLL+IzT8SYwmYsEKwUg VJiNYK8OGXqZbNiBNhcBcykaYRt39qtYrWOiiWjP5YJ9eBrFTfvUWKHD2rnqtu3iLA01 VWVg== ARC-Message-Signature: i=1; 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=6SZoYYqOOUlMq4hjr9gywH5xihJLOMWXAYIKnq88z+Q=; fh=7JRpbRYhj2Tr+97MXP6DvbZDYiCIGWF08cmmrE5bgn0=; b=geoGh2E+aUyAu0SpBCxJAmgxyJDVRg9WuJQB3FWQ2sQuaA4b3PXfZJaRtD06wz6Ogb OxIbde7U4n/BSUR3CfPegW3A+0Y6INePZy5oPWYSeFTaW+wPckfLJIWD9ToDhPtpUaC0 RNFGBS+nxJalkHkM0itJ2jWHHPmYpDRyHtT6aBf9YHBA3g0o+kO3B1hCd6HA3U4YGaGL QsO9FAqhSxKEk8mb8vRPmy9F96tqL5lQuRUdBY1b7XJNRAKh0FoGHiqJUHOyQxiLkHwE MijI4aceUch0CUx2+odQMXg1w+eb1KRiOUQ08idWKkajE+GQlwYpLRlqAjWwvmV5znNp cSAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=f6SSQ91L; spf=pass (google.com: domain of linux-kernel+bounces-19721-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-19721-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id cf17-20020a0564020b9100b005536bd5c5a4si3215384edb.413.2024.01.08.06.51.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 06:51:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-19721-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=f6SSQ91L; spf=pass (google.com: domain of linux-kernel+bounces-19721-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-19721-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 00EB21F22AAD for ; Mon, 8 Jan 2024 14:51:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 430DA524B0; Mon, 8 Jan 2024 14:50:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="f6SSQ91L" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 E327E51C3D for ; Mon, 8 Jan 2024 14:50:15 +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 Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbeac1f5045so2322317276.1 for ; Mon, 08 Jan 2024 06:50:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704725415; x=1705330215; 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=6SZoYYqOOUlMq4hjr9gywH5xihJLOMWXAYIKnq88z+Q=; b=f6SSQ91L/S/HMh04Aa26IRBAwQY3cGZnnnSOx56sUricJIfUIDSszIFIEbqOQz7QoP UD91edN5stZe+EHlgSKFBH2bERVWNVPqzBHte9Sf2iV9sW6Qf5B4/QwxHIF6sevSwoe+ L38pvWKK19WInT6JGnC3mdhughtWqfdn8jtOYWWploFB5W/htuLgI6/supJSBQdybPXS e/oRdlATG8JQwXZpETh4LTH22qlu0MKATv2Nk4cEKvwBffMfbSc5OVkJfpO9AoiYVKji q5kMhj9katkdJjTL7/yHBAw3JORWboBO+dh2+GvcdpQVMSeNeJ4cEx+PnkldAuCbKiwl t8IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704725415; x=1705330215; 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=6SZoYYqOOUlMq4hjr9gywH5xihJLOMWXAYIKnq88z+Q=; b=RaSCK+9+Agb3fS/Ar+wKcKrtqG9HF+tmfnPoHTf/jjQgMYYpFFV/a5Gx6K2bqsD7+d +j9GSUUDPk6dQKsVnQorxr5enwrzCbWiKOccqU2CqdBadR2u/k4R0ZIMSbVyeX3K6AWS vystzy359eOsXGIlXJogZ2Qwqdrxs2x+DM2gAlzz0yKaIsR4f4/+xWOsXg6UhHJ1Ekp/ 9LoaBgdysu8CpWvnTyJMR4FS+eOsWjG9j+FyRqQRMpoiBIvsHKUr1HjGZNugjqpcAa4Z 2MK6n9/jNcl9/ODEwHKs+0l9yb9MD5HYZp1DArUSlo5Ki1ukL2CH5Cn32hiRjqIVjS/8 XzXQ== X-Gm-Message-State: AOJu0YyVvxhHQXEslZ6ohuD7BVPHzAWIv4P0qvO3Flq6GeX4/B1oyJ9w E50LJCVJ5V9t7tfd4RfvOU857bJ7B9G3k/WtLzQabw== X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:1342:b0:dbd:ee44:8908 with SMTP id g2-20020a056902134200b00dbdee448908mr1432916ybu.0.1704725415044; Mon, 08 Jan 2024 06:50:15 -0800 (PST) Date: Mon, 08 Jan 2024 14:50:00 +0000 In-Reply-To: <20240108-rb-new-condvar-methods-v4-0-88e0c871cc05@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240108-rb-new-condvar-methods-v4-0-88e0c871cc05@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7874; i=aliceryhl@google.com; h=from:subject:message-id; bh=p4ziQv4KIkMhpCT+PoV/4ZeXHtEOSN15SNY70QjJ4kk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBlnAuYNVHmKC/whrgPlUF7M5/1PQ7D0IobbMcTZ G0XbqHAVBeJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZZwLmAAKCRAEWL7uWMY5 RmZ/D/9wxhF2JuR645ZiU06XtSTBT7fKFn5jm6SHcWIrPs1lK/brMEZLknoDDugO0eNDeioMjTY hjO1ZOhWQmgGeKesSTDuolvXpqCbneXSBMAX932Fbk71jk75j1hXxVy/8pSLAKoqp+K/ayRV1vj gcPaVgxN3rFrKg8zI+bM3o9YTl2PSvY0/nugeeb2gEKj/vVD6gbpxm7bpl9nB69BjYJX8eNPskU edI/0ea6H9DEuX6d3Z8K9P4HfSQWYjt8D9r6b3jcdAKqy66TkYqPQHD16fFyYcQBwQBJaHt5gvf DLYDvQVfCf+tLHkN7ZWz4J51Ougi9vHyrOkZbQDvuRm3Wubs/vFUeRkG/CnG0PU0S06DhL3nox0 si6ZAelzmH2u3gwjdH01kSQYEpzG/FjxVlaqc5oM13Rq0f+nYfpB1oYZ60UCVc86SAch6NZ0MVJ nEL6v/Tb1veTV8mdVrF6JTNmWg2zTvMmGrhm02IAN9v4sRWmg51571911mBMuKaiPhqGj7oQbSa 9NqtZYNME1Nhx7Boom4phakBXG6+U3FJMOae4g50WTu7wkUjZ5dVTZjPFHJPgaNIbD5jmxlynQ1 GXldTJ9N8kRiNrgY045lMsvpAt2g2cr28B0r421lIFRYwxWzI9VkD/4bvOrEhw2MaPAR2wPWCzA i1qQlcxdbCLvOHg== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240108-rb-new-condvar-methods-v4-4-88e0c871cc05@google.com> Subject: [PATCH v4 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: Martin Rodriguez Reboredo , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" 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 != u32`. I wrote this patch at the suggestion of Benno in [1]. Link: https://lore.kernel.org/all/nAEg-6vbtX72ZY3oirDhrSEf06TBWmMiTt73EklMzEAzN4FD4mF3TPEyAOxBZgZtjzoiaBYtYr3s8sa9wp1uYH9vEWRf2M-Lf4I0BY9rAgk=@proton.me/ [1] Suggested-by: Benno Lossin Reviewed-by: Tiago Lam Reviewed-by: Boqun Feng Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Alice Ryhl --- rust/kernel/sync/condvar.rs | 39 +++++++++++++++++++-------------------- rust/kernel/task.rs | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index fa38c095cbe0..7f2b78e4abc7 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -7,11 +7,17 @@ use super::{lock::Backend, lock::Guard, LockClassKey}; use crate::{ - bindings, init::PinInit, pin_init, str::CStr, task::MAX_SCHEDULE_TIMEOUT, time::Jiffies, + bindings, + init::PinInit, + pin_init, + str::CStr, + task::{MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE, TASK_NORMAL, TASK_UNINTERRUPTIBLE}, + time::Jiffies, types::Opaque, }; -use core::ffi::c_long; +use core::ffi::{c_int, c_long}; use core::marker::PhantomPinned; +use core::ptr; use macros::pin_data; /// Creates a [`CondVar`] initialiser with the given name and a newly-created lock class. @@ -108,7 +114,7 @@ pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit( &self, - wait_state: u32, + wait_state: c_int, guard: &mut Guard<'_, T, B>, timeout_in_jiffies: c_long, ) -> c_long { @@ -119,7 +125,7 @@ fn wait_internal( // 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) }; // SAFETY: Switches to another thread. The timeout can be any number. @@ -138,7 +144,7 @@ fn wait_internal( /// [`CondVar::notify_one`] or [`CondVar::notify_all`]. Note that it may also wake up /// spuriously. pub fn wait(&self, guard: &mut Guard<'_, T, B>) { - self.wait_internal(bindings::TASK_UNINTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT); + self.wait_internal(TASK_UNINTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT); } /// Releases the lock and waits for a notification in interruptible mode. @@ -149,7 +155,7 @@ pub fn wait(&self, guard: &mut Guard<'_, T, B>) { /// Returns whether there is a signal pending. #[must_use = "wait_interruptible returns if a signal is pending, so the caller must check the return value"] pub fn wait_interruptible(&self, guard: &mut Guard<'_, T, B>) -> bool { - self.wait_internal(bindings::TASK_INTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT); + self.wait_internal(TASK_INTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT); crate::current!().signal_pending() } @@ -165,7 +171,7 @@ pub fn wait_interruptible_timeout( jiffies: Jiffies, ) -> CondVarTimeoutResult { let jiffies = jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT); - let res = self.wait_internal(bindings::TASK_INTERRUPTIBLE, guard, jiffies); + let res = self.wait_internal(TASK_INTERRUPTIBLE, guard, jiffies); match (res as Jiffies, crate::current!().signal_pending()) { (jiffies, true) => CondVarTimeoutResult::Signal { jiffies }, @@ -174,17 +180,10 @@ pub fn wait_interruptible_timeout( } } - /// Calls the kernel function to notify the appropriate number of threads with the given flags. - fn notify(&self, count: i32, flags: u32) { + /// Calls the kernel function to notify the appropriate number of threads. + 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, count, ptr::null_mut()) }; } /// Calls the kernel function to notify one thread synchronously. @@ -194,7 +193,7 @@ fn notify(&self, count: i32, flags: u32) { /// CPU. 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_NORMAL) }; } /// Wakes a single waiter up, if any. @@ -202,7 +201,7 @@ pub fn notify_sync(&self) { /// This is not 'sticky' in the sense that if no thread is waiting, the 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); } /// Wakes all waiters up, if any. @@ -210,7 +209,7 @@ pub fn notify_one(&self) { /// This is not 'sticky' in the sense that if no thread is waiting, the 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); } } diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 17f14ebb8f48..6072b0de4a3e 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -5,11 +5,24 @@ //! C header: [`include/linux/sched.h`](srctree/include/linux/sched.h). use crate::{bindings, types::Opaque}; -use core::{ffi::c_long, marker::PhantomData, ops::Deref, ptr}; +use core::{ + ffi::{c_int, c_long, c_uint}, + marker::PhantomData, + ops::Deref, + ptr, +}; /// A sentinal value used for infinite timeouts. pub const MAX_SCHEDULE_TIMEOUT: c_long = c_long::MAX; +/// Bitmask for tasks that are sleeping in an interruptible state. +pub const TASK_INTERRUPTIBLE: c_int = bindings::TASK_INTERRUPTIBLE as c_int; +/// Bitmask for tasks that are sleeping in an uninterruptible state. +pub const TASK_UNINTERRUPTIBLE: c_int = bindings::TASK_UNINTERRUPTIBLE as c_int; +/// Convenience constant for waking up tasks regardless of whether they are in interruptible or +/// uninterruptible sleep. +pub const TASK_NORMAL: c_uint = bindings::TASK_NORMAL as c_uint; + /// Returns the currently running task. #[macro_export] macro_rules! current { -- 2.43.0.472.g3155946c3a-goog