Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2914727lqp; Mon, 25 Mar 2024 12:56:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU4+YWcniBAltQIvGNoqVP737VPqhCTFuN7/H319hKREHliks37VANSii5Cgg+GBsZK91/g4Mo8qv43hmgbTgwNKmaHGt8NyuMvRXxRfA== X-Google-Smtp-Source: AGHT+IHxuZdpqzyEqjvTW6++DkvCPgOF2qH1s8Ry+pUIAn6QnN9QfmJy3cZpeofj+FQjYiPNIJ8a X-Received: by 2002:a05:6a20:a11e:b0:1a3:c58b:db13 with SMTP id q30-20020a056a20a11e00b001a3c58bdb13mr4853734pzk.49.1711396619563; Mon, 25 Mar 2024 12:56:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711396619; cv=pass; d=google.com; s=arc-20160816; b=YKkgQPCwkkDSTJ6B8f4I3E7mD43PDl+8/4RIPW/xhzq4yCmegiE1aS3PaLl7brBhT+ yV7psfF+9wMtmRn7uXnXtactmNdoqINWpk/MlKkG3y+E2QBopzY1dDjtICXPrymEVRbx KuHH9npFx2KMG9Fzg2gU4cNwV5MPTGbcuMmi6Ow08HQ/tZ/lubR11sFn7BFy8jDkeiqB c0U+Um64EG6ddHIsJOevWTAUN7FCOGdcKn7qATlB7jc2EnYnTB1gFfJv0e+3a+/wlbus IkVn+kNc6vZ/k5CNXOrI1QsDGi45frj2Z0r/PSqsEdaex79LtHePg9HWQqrAsjret2/d yhJA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=21P3jOn14ZtLRVVWjd/aZah81bx8Fq/6mZQF26h4i3s=; fh=jnkn0E5cmmzbyh2AaOnPbn21RDFraRPsfJ48ksbofRY=; b=jFENdk9EqFR5IVoklMG4fFR6nNYdWovo/C6f+NQwBKVeC74XD/iE4x2VIoy5vrQOhJ rASrRULXczslUt1LsD1wfCb99+YM9ikoYNFmFgCIJm6sJe9llLjni6e2VfyDMTUwRRw2 OawFwzR2STGRohjvN4KSVL7fvZhAQ7agTg3+IhX24NiwR7rOnhQs0t9MbwJSUMPCLln8 5TndIKN7OWTE8DLiR4ip4apZZ7lcFil3Q3Z65wWCOiAk34Cllt3KP23T0mr9tLESDPc9 g2/cxbKb+vVe5p1Cy2jHTXQ5RT9EW8hV4hJk9UrHTzEKaTBI24/tWXXsTcw94MYtyMUm 4bvQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Wio0193K; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-117866-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117866-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id p187-20020a625bc4000000b006ea790f3132si5676356pfb.163.2024.03.25.12.56.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 12:56:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-117866-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Wio0193K; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-117866-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117866-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 3D6312C7AF7 for ; Mon, 25 Mar 2024 19:56:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D5A335647E; Mon, 25 Mar 2024 19:55:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Wio0193K" Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 6633954BF5; Mon, 25 Mar 2024 19:55:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711396518; cv=none; b=d+im4W9G/jnkjaKR6bfDY2E5Mg8wIKV2yGFQTkmPguqTKks9tpsOwh5tF9nl9BjqrlAGjEIVm3q9q8i39mk36IdZyblBQ4sZFf8sx/I90DtbXmzcYJUCxhUsSzpExD+TODcTfK8Yw6dJyHPaCA5PvAHsYolA0PSDx226SyfH4pQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711396518; c=relaxed/simple; bh=/QTypASE8D93iYLvREHDwsB5dHZ8V87dnuUHykxExTA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IbCXhFb+gJeeJcDF9C5QVDD4KnSq4Qb8+XIDlEQNmsziix3gqX6/eNTfmw8gwI+FRCVAiHpXgUloHwuSCqJADR/biAHzKxnC4PuTRBilTON05Nd0RJduGcuGgUW4kZ7km/b99iTtHHNwv8rfJiycupvjSyjLUMCyK9ymxBahY5o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Wio0193K; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1e0d82c529fso5206815ad.2; Mon, 25 Mar 2024 12:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711396516; x=1712001316; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=21P3jOn14ZtLRVVWjd/aZah81bx8Fq/6mZQF26h4i3s=; b=Wio0193KwilUmo0sOPuKthcv4ZMDfprGQnA4vWh7McVb7DOQoJxK8bPGaHaDt56Zes wWKcYp5wTfxaeFf1ZW5PuBTSFJeRW+VysJ1pkIxIzpOs1zPI9mqijPnnfbSoqU/S30Gf aIhmHu/vnqKIPjbi/m4GTUUTeRfWUWbus44jv9V2sJfppQsn9wrzfglum7Tt7yCjY8lI ogBuAQrRGDrkn8vizuYrBwn5nhpIhjO3K367CRkgOMrxeo9pKCrujEzmFMGJscEaJJuq ZGTRLPRQOwZu4gj41HsoHZ8wbhDLspV1ypraAKMgRSDUkLv7fj3gymwyOAfBTz/o5NBs zrhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711396516; x=1712001316; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=21P3jOn14ZtLRVVWjd/aZah81bx8Fq/6mZQF26h4i3s=; b=Pg8+9Rb4fZzNsAay7CK4eWETg/4NkCeWEqnXrh+AjgTGLGMcdTGwxYi7TWf8yT9BC9 m9EFDjBFr/4yzABBUJtu5Ifsebhyykld+NS1QjVu16Tr5G9XcArtrNYpC0Hl/k03aaaL qbpVvCa+0UKKa8sVli5cLkhoHFwz7uDLyi4YTPhuoZVhZ7SMExLe1Bd+L6wy6na3q+1h okMhFvgmTcbAVaIT+tRu07G1r3jp7K5xhIRbPE6+U0a3ZBh7HI3Q0WmGP7hwalDlF7g6 DxPck5xaJLSGq/Chf/EN7hUDEPMCehuRzJ1HRDdIL4a3AHDMSNYPvijZcDZztARC4d5t jSDA== X-Forwarded-Encrypted: i=1; AJvYcCXJYX4HXLesAh7yC5ktIkcZd6RvgxEQ2WCbpFJ2i1clrCaSa0/aPVe31Q6DG6JKY9sSkaY6v7n7Mod0H8/B80bNEg9iBRSIwYG0lpQQ X-Gm-Message-State: AOJu0YzyK7/5JIkWwRYXIY9aSsgPRTAY+PP1xa1XulwZu2rl8KqirqZX LG1GNT5lahdAggtp+GK41uGpUySnwcY5837Bhk7qHi0Ii01dWaifoj/NV4NM X-Received: by 2002:a17:902:e80d:b0:1e0:afa0:d515 with SMTP id u13-20020a170902e80d00b001e0afa0d515mr5664995plg.29.1711396516462; Mon, 25 Mar 2024 12:55:16 -0700 (PDT) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id q1-20020a170902dac100b001e06c1eee22sm5099585plx.74.2024.03.25.12.55.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 12:55:16 -0700 (PDT) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: [PATCH 05/10] rust: alloc: introduce allocation flags Date: Mon, 25 Mar 2024 16:54:13 -0300 Message-Id: <20240325195418.166013-6-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240325195418.166013-1-wedsonaf@gmail.com> References: <20240325195418.166013-1-wedsonaf@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Wedson Almeida Filho We'll use them when allocating `Box`, `Arc`, and `UniqueArc` instances, as well as when allocating memory for `Vec` elements. These changes will come in subsequent patches. Signed-off-by: Wedson Almeida Filho --- rust/bindings/bindings_helper.h | 3 ++ rust/kernel/alloc.rs | 56 +++++++++++++++++++++++++++++++++ rust/kernel/prelude.rs | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 65b98831b975..ddb5644d4fd9 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -20,5 +20,8 @@ /* `bindgen` gets confused at certain things. */ const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN; +const gfp_t RUST_CONST_HELPER_GFP_ATOMIC = GFP_ATOMIC; const gfp_t RUST_CONST_HELPER_GFP_KERNEL = GFP_KERNEL; +const gfp_t RUST_CONST_HELPER_GFP_KERNEL_ACCOUNT = GFP_KERNEL_ACCOUNT; +const gfp_t RUST_CONST_HELPER_GFP_NOWAIT = GFP_NOWAIT; const gfp_t RUST_CONST_HELPER___GFP_ZERO = __GFP_ZERO; diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 8ad57a2e693e..ad48ac8dc13d 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -6,3 +6,59 @@ #[cfg(not(testlib))] mod allocator; pub mod vecext; + +/// Flags to be used when allocating memory. +/// +/// They can be combined with the operators `|`, `&`, and `!`. +/// +/// Values can be used from the [`flags`] module. +#[derive(Clone, Copy)] +pub struct Flags(u32); + +impl core::ops::BitOr for Flags { + type Output = Self; + fn bitor(self, rhs: Self) -> Self::Output { + Self(self.0 | rhs.0) + } +} + +impl core::ops::BitAnd for Flags { + type Output = Self; + fn bitand(self, rhs: Self) -> Self::Output { + Self(self.0 & rhs.0) + } +} + +impl core::ops::Not for Flags { + type Output = Self; + fn not(self) -> Self::Output { + Self(!self.0) + } +} + +/// Allocation flags. +/// +/// These are meant to be used in functions that can allocate memory. +pub mod flags { + use super::Flags; + use crate::bindings; + + /// Users can not sleep and need the allocation to succeed. + /// + /// A lower watermark is applied to allow access to "atomic reserves". The current + /// implementation doesn't support NMI and few other strict non-preemptive contexts (e.g. + /// raw_spin_lock). The same applies to [`GFP_NOWAIT`]. + pub const GFP_ATOMIC: Flags = Flags(bindings::GFP_ATOMIC); + + /// Typical for kernel-internal allocations. The caller requires ZONE_NORMAL or a lower zone + /// for direct access but can direct reclaim. + pub const GFP_KERNEL: Flags = Flags(bindings::GFP_KERNEL); + + /// The same as [`GFP_KERNEL`], except the allocation is accounted to kmemcg. + pub const GFP_KERNEL_ACCOUNT: Flags = Flags(bindings::GFP_KERNEL_ACCOUNT); + + /// Ror kernel allocations that should not stall for direct reclaim, start physical IO or + /// use any filesystem callback. It is very likely to fail to allocate memory, even for very + /// small allocations. + pub const GFP_NOWAIT: Flags = Flags(bindings::GFP_NOWAIT); +} diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index a0177f195dec..a7b203f87aa1 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -14,7 +14,7 @@ #[doc(no_inline)] pub use core::pin::Pin; -pub use crate::alloc::vecext::VecExt; +pub use crate::alloc::{flags::*, vecext::VecExt}; #[doc(no_inline)] pub use alloc::{boxed::Box, vec::Vec}; -- 2.34.1