From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 3C48D30149A for ; Mon, 20 Oct 2025 13:33:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760967238; cv=none; b=r8Temhw5uQxj4zMonAXBmx/alsTOwXfHjZtjmr07M5+jMzbh/cAKNloPaJmubPZu7W7mH1A/at0U0rJApPqOpPv3O2nETuWTZmZVYuMkrhk6pLxOTJ5dv6qng6szrAU89DAzwQXbpQT21c6t+/acW88RNCuPKNLBMHW3a8BDtSY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760967238; c=relaxed/simple; bh=thSFFHc18AWUj5aqWdgPfPpfkLxR0GCl0hag2FFKB38=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rjBJPMssr4Y1kZvzLuv+dq62cNlOU1G9YxuIJpuq4wT2SWDC4S6oxVlT7Sefuw3XcDlcV5MjJmDnhK4w5F9ww4FkENUL97x1BE7jKrYhYETpekeQsgFgdGPP2aOt13DyLyzjzE7o5g9Z1Bg71hY3ovOoHBmyFBr2gcItIey9kec= 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=HyXqJntx; arc=none smtp.client-ip=209.85.128.73 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="HyXqJntx" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-470fd5ba449so15485165e9.0 for ; Mon, 20 Oct 2025 06:33:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1760967235; x=1761572035; 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=hP+6efxF2qsVAZJqN5GtRW+FGzYHXSfL66ojkDSC4QY=; b=HyXqJntxTBZrmupaWyHiKmWsHteFX01f6iC/70eNIa9nDb6j+9nQtVcT3XnSJW9Tr0 8g7MV7JMqDfx1AsW5xCw5f61ZGpJ1eQx8vZhbKukKvreziTSiXqMv7NNypAXObuI88J1 pgBZ3v9eVtyLEJF4FtWlV7aPOPCMNNkFgR/+oH+f7TCA7/a9TTjiyJ/HHyIwDzOR5C/s VeBNkJugs0HBYj+mkfX7oWHWT2EptfkcEWI5JDUnIykLLdLZqeI2zEHvtKkLY+0Q1QKO +7o82UIXFpk3F/NZjKhcn855RqKmt1dNVU0VCLqqrvH31DYqcpDnS0LWk67N/gdayr2g gvgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760967235; x=1761572035; 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=hP+6efxF2qsVAZJqN5GtRW+FGzYHXSfL66ojkDSC4QY=; b=dsHISeBSK6o2fuLBGvFMcuOkEdETOgfDvw2MtQEmoCv+IvHqFu3xFDtFJzSWvK/P+4 str4EJQspCDd2FSebR4epN0W9ETRzzmbPi4O3EU1eZUp4hpIaMl8YsHScO9v1syxVYo5 rVpzzjrUIVwKEPc14OphnbwGA2z9uxfVoTLrKBrrM+GBW3U+0PlJAXkJs4MDtXR9GAyf USx8VDpMR10xbRx76m0N8YRYv+VES/Xdi/Ar0qBjBngbl7i1z6fu0tm3pW2xdEnZS42s +fOx+Gxx41LB1GfsfgsYa2Ugcx2vCd0azlO6LM7RYRO0YcPYz2LhzW/guww3FiriYXPC Qalw== X-Forwarded-Encrypted: i=1; AJvYcCWdtOKMkI11cPcShLzGXuoRj9VDRXHbQa/FZlEJKzU5aEwgPr3DRbC2X6J3THllkD2a9JzORA+/8y8PfDxRCg==@vger.kernel.org X-Gm-Message-State: AOJu0YwQ5NZJS2wrS1/ajmkLtoj8P9RLuyEs+ayMbhcZxOKKv324c0Dm 5T/rXlZ279tY+BGP3RPOBOivptn6H7OrZ+rpKrZOLGTT5i2veQUBMpEaTAi5n9mOTZMMqPr+fT5 ibf7eV1cVEOCr15uZJA== X-Google-Smtp-Source: AGHT+IGsJYu7ivmzqWkEMta1m78AwxdpOsRB8GnsOYJkNeIzCPPDV8Af/U2XzI+UoFLtGfkAHK/DNZqKYu4z+S4= X-Received: from wmqg20.prod.google.com ([2002:a05:600c:4ed4:b0:45d:dc32:3d4a]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8b0c:b0:46e:2815:8568 with SMTP id 5b1f17b1804b1-471172d7e15mr102633925e9.10.1760967235581; Mon, 20 Oct 2025 06:33:55 -0700 (PDT) Date: Mon, 20 Oct 2025 13:33:41 +0000 In-Reply-To: <20251020-binder-bitmap-v1-0-879bec9cddc1@google.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251020-binder-bitmap-v1-0-879bec9cddc1@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2529; i=aliceryhl@google.com; h=from:subject:message-id; bh=thSFFHc18AWUj5aqWdgPfPpfkLxR0GCl0hag2FFKB38=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBo9jpBcxwwb8kMGMy/7zHQfhjjqF1x9qVmgLLPY TMj/oGIL3qJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaPY6QQAKCRAEWL7uWMY5 RrDUD/4llmvaB9bgfQsY+fQaRuNtXWlK4O9049eKPnNRPZaQdzfVJz2+jOFGLpEQwNBPa/NDWnh /1oB5DtKs6n6lvRSfHA4szqtfxBtXaC9kPqRCeRC5ANyINOlYb0RV7xl5K/3RfGk6d9/n7xRmOq iqJzVB2XBumsU2QsGM1IxE+66DkgFrTyNz1mjBkmmsO3EbSNOHEJI55GIe0hLDm+gbMypPzzeLY uzjRjryoB+hX2UWhrdLI+cvxzHZx0NMDTWiKV3ojcj09wHExwZCputoKRROyCTXqOdkjNUZkNK+ VJYqTe0CT6+pKm5Gun6NG4hlWePYV7QV+Hkd21rOXjwlPz6r3u1/4ckfWkISmw2Pv6OVEHg6EdQ ATrrHWdJ9BnMUSfKQNNqjPgLq0MUmxviZjXCC8aK/LAqX3m5revkl/mbigHy29p6Fv9DiFQcngG ToUFw6LUznMznOdf77zH3vfpwYLEeihQ+fWMCMURj8yZMsIeGaid08FR/70OCH5Aa7MT/AdeTVt sltd1T3M1ORWsqFl196kYBoY8IUsW/cvC4ZOEQ7kNNYwa6RpPISJOo0sgM8YQDGvB/3doZJmX6H grcDQcWQBjUVgR5ytVWkGiTSGqsQMJ67wPOe6EP/cV27nuFAzAVyFThtiWhe3bbm7ZFzU9q+bdo /lvTLmZ2kX83S4w== X-Mailer: b4 0.14.2 Message-ID: <20251020-binder-bitmap-v1-1-879bec9cddc1@google.com> Subject: [PATCH 1/2] rust: bitmap: add MAX_LEN and NO_ALLOC_MAX_LEN constants From: Alice Ryhl To: Greg Kroah-Hartman , Yury Norov Cc: "=?utf-8?q?Arve_Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan , Burak Emir , Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" To avoid hard-coding these values in drivers, define constants for them that drivers can reference. Signed-off-by: Alice Ryhl --- rust/kernel/bitmap.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs index aa8fc7bf06fc99865ae755d8694e4bec3dc8e7f0..15fa23b45054b9272415fcc000e3e3b52c74d7c1 100644 --- a/rust/kernel/bitmap.rs +++ b/rust/kernel/bitmap.rs @@ -149,14 +149,14 @@ macro_rules! bitmap_assert_return { /// /// # Invariants /// -/// * `nbits` is `<= i32::MAX` and never changes. +/// * `nbits` is `<= MAX_LEN`. /// * if `nbits <= bindings::BITS_PER_LONG`, then `repr` is a `usize`. /// * otherwise, `repr` holds a non-null pointer to an initialized /// array of `unsigned long` that is large enough to hold `nbits` bits. pub struct BitmapVec { /// Representation of bitmap. repr: BitmapRepr, - /// Length of this bitmap. Must be `<= i32::MAX`. + /// Length of this bitmap. Must be `<= MAX_LEN`. nbits: usize, } @@ -226,10 +226,16 @@ fn drop(&mut self) { } impl BitmapVec { + /// The maximum possible length of a `BitmapVec`. + pub const MAX_LEN: usize = i32::MAX as usize; + + /// The maximum length that avoids allocating. + pub const NO_ALLOC_MAX_LEN: usize = BITS_PER_LONG; + /// Constructs a new [`BitmapVec`]. /// /// Fails with [`AllocError`] when the [`BitmapVec`] could not be allocated. This - /// includes the case when `nbits` is greater than `i32::MAX`. + /// includes the case when `nbits` is greater than `MAX_LEN`. #[inline] pub fn new(nbits: usize, flags: Flags) -> Result { if nbits <= BITS_PER_LONG { @@ -238,11 +244,11 @@ pub fn new(nbits: usize, flags: Flags) -> Result { nbits, }); } - if nbits > i32::MAX.try_into().unwrap() { + if nbits > Self::MAX_LEN { return Err(AllocError); } let nbits_u32 = u32::try_from(nbits).unwrap(); - // SAFETY: `BITS_PER_LONG < nbits` and `nbits <= i32::MAX`. + // SAFETY: `BITS_PER_LONG < nbits` and `nbits <= MAX_LEN`. let ptr = unsafe { bindings::bitmap_zalloc(nbits_u32, flags.as_raw()) }; let ptr = NonNull::new(ptr).ok_or(AllocError)?; // INVARIANT: `ptr` returned by C `bitmap_zalloc` and `nbits` checked. -- 2.51.0.915.g61a8936c21-goog