From patchwork Wed Oct 16 11:34:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13838218 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF079D1AD43 for ; Wed, 16 Oct 2024 11:34:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 516756B007B; Wed, 16 Oct 2024 07:34:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C7096B0082; Wed, 16 Oct 2024 07:34:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38E806B0083; Wed, 16 Oct 2024 07:34:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1BE2B6B007B for ; Wed, 16 Oct 2024 07:34:45 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 943621C69A1 for ; Wed, 16 Oct 2024 11:34:33 +0000 (UTC) X-FDA: 82679257962.02.87C5F93 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf29.hostedemail.com (Postfix) with ESMTP id 5983612000C for ; Wed, 16 Oct 2024 11:34:31 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=DDqRPaFo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 30aQPZwkKCJg2DA46JQ9D8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--aliceryhl.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=30aQPZwkKCJg2DA46JQ9D8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729078450; a=rsa-sha256; cv=none; b=Js7/HQdmKl4zYJfYWMsSUJZIh9OZvk6Kq49KJQnCYYP/5Eg43VyCv5szCftoDSI06/S9IA ma9r533yz2WGHkVcDVXTUQmQkbejLbs5S7tY07kd2iCIy9ftwq6oz89KiPv/2Vdtt2wnOJ ANN18xBQoW+GpJtK4p2A/OP5LNc6Co8= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=DDqRPaFo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 30aQPZwkKCJg2DA46JQ9D8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--aliceryhl.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=30aQPZwkKCJg2DA46JQ9D8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729078450; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=MALXvk2pH8I2k2u/nrpnO/yJTvhOpCgZg6TPQyQ9Tdg=; b=s998WqlotT1SkKiyD17q/zUFuMzHP489IIYXQtwe+EU56PSyp7AAbyqve/iHbTnC7wmMk2 KSzt0m3H30QVFh4nf58OiyS/e81F8xRKgr5p56gmH+7ulhdLqPPLVul1072RwL4GZVxCNb FCduzz91cyC75NNAwN5iGy+WGVj45Sg= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e29bcb5591cso806181276.1 for ; Wed, 16 Oct 2024 04:34:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729078481; x=1729683281; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=MALXvk2pH8I2k2u/nrpnO/yJTvhOpCgZg6TPQyQ9Tdg=; b=DDqRPaFoSqelb9ibdTGALKeaD2OHwRhWsyHCmrveJyzmG22PobS4yQwyTTiYfyHgBr y1j6/8rqeTq6AnExJp62o2ozfJ6qEMGUcGVybjNse7E9jH/RuYJiK6pu5rMIhXsIJ+St ASbcBYCmbRnaOXcwYa1fXZRLTNIby1QHOMBqiFsPJE3FOWOgNVFwVz/K1gKxOT/7IW1f REqd3yx0v0Q74XPvClvWJyi445ubrEsypBce27d9RcVv6R6NPcwTQiC+T2Mn8862mcYu gdQZdqgWK6CGAtt5grMi9rw8Keq1qizEeDFUDFnsSyyv1thEF2MViJC3BTxKMEHdWuha 8Cyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729078481; x=1729683281; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=MALXvk2pH8I2k2u/nrpnO/yJTvhOpCgZg6TPQyQ9Tdg=; b=i9dk9AGBwAOqAlY0nHNotmBZHQLSIjgMKPQC0h3WsPBa+ek/LuuV1Q5BHfVgvWGgkN Y/1sqtOb9hWmarCFXCGpesIbZmRXvKzTPanElONFkBgdzIb7ZfO6pxIIeZt8mxWnn4LK 52BUR/ZDLO3+8QHIy2JYCa1mWfB1WCB0NaCPGVqPERlUq9RUJgj1Ff/4rz0N89/JEhF/ lDIMuuxMs7xo8bp2jebNUlxpLEqnZ1Vwg/twRqWUQHeYgN51bT+mx1ASN+5ToFjW8ZxK mmCBbrbQS8fakphUX8U/DaIwy1rj0A3zZe+bl+95Z1XiEk6hn7Oiitn4oqztZLtcP9aj 8Zdw== X-Forwarded-Encrypted: i=1; AJvYcCXZWxbZSVYp/YBl9v+ZGOMlJRQ1t1beOqa0jVN4hUyGeP35LW9AdlJJK2FApjqrF4u+dLn4mL1zzg==@kvack.org X-Gm-Message-State: AOJu0YwGB32Khiq9B+8sqtqKB6yaZnZ8N/GyIyTpPdKiIXHBE5CiP/6k zogc0lfAwAH3G9dPQgzKAFe6gOb0IQfpV+fjPihQetWvqrdNN26NjIgVZiKZVwn8LlT1sITUFpd Q7DHYEMs048nxxA== X-Google-Smtp-Source: AGHT+IGfRBqsOD2gGU0Q/tMktiVo9//GJQ0UbhR+dW9OD/x0wD05SndfGUmQvGwGLXuBq1JNpcneuDfyEmwqT64= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a5b:546:0:b0:e28:f19d:ed4d with SMTP id 3f1490d57ef6-e297856f63fmr2163276.6.1729078481439; Wed, 16 Oct 2024 04:34:41 -0700 (PDT) Date: Wed, 16 Oct 2024 11:34:25 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAMCkD2cC/22MywrDIBAAfyXsuRZX8qKn/kfJwehqFtIYtEhL8 N9rc+5xBmYOSBSZEtyaAyJlThy2CurSgFn05kmwrQxKqhYldmLX1emV/SYG6pxuzdDhTFCDPZL j9zl7TJUXTq8QP+c748/+3WQUKPrRzXace4vS3H0IfqWrCU+YSilfqC4cwaUAAAA= X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1520; i=aliceryhl@google.com; h=from:subject:message-id; bh=FASyUf1saphfaZbw5HzqQHkzRL2v/xQtO8LX2AzMsdU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnD6TDRlj6OCtorsbkl4Zx1Ncki8FT20tIyymhr t0bMM3J99OJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZw+kwwAKCRAEWL7uWMY5 RjzsD/9MGtoHTc3tWy/FcEwTs19PhkvgcFc4rA9v2okPpqYrHx+wNdDavdQuuxysi7X/oi/Kw9N Tah4eilyoGLrxKSzQ+/kA3p9haCeodxjLtzf7sK8JVhpq7Pwd0Aa5cGv4kvFch3Zu9y9Q7etpOg 8sqgNXLswBGwFZoYaYOup1erzww1h6uajLSoaYK4zVQxXow240EKaqLgZIrgIBIBe7/fUg1U0xq 4iYCczcu+kPnoomPiT6lCCYUedEUa+lQejHmXcVE+Qc5J+cAKk0wcz2y5/VjXG4l0UaaEGKyG5D LXHYh+ZBb22eQSRWsKXAkvI7X4z9LP8RqzcQxEqrsJPFcLqePfJ6yDLg9H8GCy/F8VSgHLoK+Ru bSSNENSd6kBEyNyXWPlRQIxByzx3QOhc/KYExoIa1WSG4GhIyCBns120U5GC2waj675Jj9XDqYw v6TGLRbTcfyoSf5XwFdhYC0Fbk4lBYOv806NoFhWYPwPlzKKth9xC+pwSo5ZEpLcCi+GoxSfjMY GX1Wg4+7p1ma+0dTDAjA2Dz0zhjOihmF4ye/AOyaSBSIFLfD6R8VzLTw3MqKza8z6Y3PfQYhWcw iOYVnvJgZeFgpe96jDP/4sPiga272g0ey+Ep4swWlbrd/glIXe70FCOYeHP87BXOnRAvAASAjiR NC+MM5j4FhDPoEA== X-Mailer: b4 0.13.0 Message-ID: <20241016-page-align-v2-1-e0afe85fc4b4@google.com> Subject: [PATCH v2] rust: page: add Rust version of PAGE_ALIGN From: Alice Ryhl To: Miguel Ojeda , Andrew Morton , "Matthew Wilcox (Oracle)" Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-mm@kvack.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl X-Rspam-User: X-Stat-Signature: efb4a7skjqiwn6yund7wgishkwczstrc X-Rspamd-Queue-Id: 5983612000C X-Rspamd-Server: rspam02 X-HE-Tag: 1729078471-186141 X-HE-Meta: U2FsdG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DIsoRg5hqPLDefpnukstA9b5ZKh13rSd1P4naePA/XHz+hS225YOQVlw8S1fJ0xmrQM867yOAq+pk5vEOJHjyX48wVqbZTqCkvlppjis9k11Jsmc/nOgod35X3KK9MraTL+6SQV13K5U5UryElfFPzCsMDvf+Mb3psUcmFy5cpUqEmXAinXk7DXFTrRiKRj7yfQ3Dee3K3AMDUiiJRbpLL9A+DhqTTY4xR9XZ3qMgNuodYBJU8te1Nao0wFiFNuhyJEdbA5Q5iXcx8H7TPF7zLBBVdZpHdkcCdCDKGvvvCFgdgjocfAVWVYvcEkTGPy1+ihLfO98UXn23BlqrwJDT/YD7ByjalI4ZaVLMTnDIuJ831c9eWfbQBT3J4uRB0pasaRdybn5YW82EwXlD2U/wxKsPoLQFkgKVWJpWs/vXR8gsgfo1C/OK+u8bq9QqxsUFYqvzHVv2bZ+c0Sk/89mCs+K4wNho/QglFPYsYN/Jar72gY52n1Q3h2YvgtaSN93HMFD+SyCOUs3dYL02+xs8xlZHDysna3UalHvh1F2MpFcxSjCiDXDDeicy9S7pQDAYoXqURpynZfRe33OI80aaAYU44B2tWDCbuxP7ij076cE9MUi/pqEIr06/B+w1yDEzdVuQx X-Bogosity: Ham, tests=bogofilter, spamicity=0.000581, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This is a useful for helper for working with indices into buffers that consist of several pages. I forgot to include it when I added PAGE_SIZE and PAGE_MASK for the same purpose in commit fc6e66f4696b ("rust: add abstraction for `struct page`"). Reviewed-by: Boqun Feng Signed-off-by: Alice Ryhl --- Changes in v2: - Make the function const. - Address integer overflow in docs. - Link to v1: https://lore.kernel.org/r/20241015-page-align-v1-1-68fbd8b6d10c@google.com --- rust/kernel/page.rs | 9 +++++++++ 1 file changed, 9 insertions(+) --- base-commit: 8d8f785ceb21b9a0de11e05b811cc52d6fa79318 change-id: 20241015-page-align-7e5fa4c751be Best regards, diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index 208a006d587c..9ef01929e7d0 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -20,6 +20,15 @@ /// A bitmask that gives the page containing a given address. pub const PAGE_MASK: usize = !(PAGE_SIZE - 1); +/// Round up the given number to the next multiple of `PAGE_SIZE`. +/// +/// It is incorrect to pass an address where the next multiple of `PAGE_SIZE` doesn't fit in a +/// `usize`. +pub const fn page_align(addr: usize) -> usize { + // Brackets around PAGE_SIZE-1 to avoid triggering overflow sanitizers in the wrong cases. + (addr + (PAGE_SIZE - 1)) & PAGE_MASK +} + /// A pointer to a page that owns the page allocation. /// /// # Invariants