From nobody Tue Apr 7 18:46:39 2026 Received: from mail-dy1-f180.google.com (mail-dy1-f180.google.com [74.125.82.180]) (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 CEC8F331A5D for ; Thu, 26 Feb 2026 23:47:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772149662; cv=none; b=liLGjxhJK0zPsGct18xzNdXBVQ51hLYMnGYYrOOthX7yOEDBhYp0Gb/X8UPW6IYC73v4OrSxDwCf6hMYLGQVpJclPyj+iorDAuB5yHanaksnIC1/T1GCB+wgSBAFp9y5wX7acKHeGN7RP8WtMr1gI2bIpqvviLFLXbBioTsgsWs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772149662; c=relaxed/simple; bh=muxh3fCFRhSf9Unv2vBe0W+v1Z0hoW/CIg7AHgZRxaA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mlPzmeWzhDDM4NHLC+NlvW0ImNghSSYZp6F2gDHKRYttjk9JuFxai6pbPPkRT0RuuU2W8u5Q0b2fAEkoo1SFivKWFPrnx5vkP3tU4tdBRc9zUCRul/DAGNEI4sZQ42rv6d2ozeO7B5CXZes/m6ijGuDc5Oc3VA2RNGS8UF87YR0= 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=EX/VZ7n4; arc=none smtp.client-ip=74.125.82.180 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EX/VZ7n4" Received: by mail-dy1-f180.google.com with SMTP id 5a478bee46e88-2bd9a485bd6so2505479eec.1 for ; Thu, 26 Feb 2026 15:47:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772149660; x=1772754460; 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=iOJAuKmw4q0dzp0Ep92fm1clLy9bI5rwzposWVs1Z0k=; b=EX/VZ7n48a7dg7o4vgOtulAQFW7a9o8lBRcfmJ0uIbupnWZswyr7EOzckc3ZO59nm2 nKdbuYWIVYTmhP+M3cXwh2s7QW4O+W5NTvUdGq0D3lujNhRghg4ABgcE6KdmE9mn/36q A6h9m/P4sl4+Pkj32k+hAFPTCvXi7Y5gzuzXuX//gWB19cyvqEYw19oKmsHDeeC/8/0p KGMbvGkUdHbmWV/ceBFFqvKm/Q28HpqpQTbBIU4eYHgrxi58vPlqBVQtD6yFxrm6chhr cZ+SPiTsqOhwlAQqlK+Wr9OtkUdiQo24XN4Jn/utzAe37IphwIV0KiaEJET+vCmjKYb0 AgHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772149660; x=1772754460; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=iOJAuKmw4q0dzp0Ep92fm1clLy9bI5rwzposWVs1Z0k=; b=a8W/MJ5mXPsPC4PpR406dTDkzQgQ4QKq7DJM+lKP6MdajSN1TUQ6az6Wl8Scjk+l45 ej8euFg15hE9LVCVF32UXVuFnzfyS+RZz1soI9bqe8ZTzgOD2JZX8VDoj8G+VCZI89AH sbqApBvt1sLig3wjTKU9URYvihaNHeYGzUHRhCGTMfWPTcXQSnA9VPAHJ8lR1XpBB/qh q3C9CEuFhgy6a9jO1KmZEs2IFPa03WQpMVrZcclkR9wTjWSzff+kmouVsJnW/FTb1kbP ZJqxwEtqzEC3zyDZ6ezdbaglmpgHI7lO7MC9MAf5nsZYzspc8AW7lkon4EFr2N/oN/bm 4PIw== X-Forwarded-Encrypted: i=1; AJvYcCW3o/5gldUJ5K0jmogi6H74AlqEc40XWXl9rqfzRZlBzgBMATxG7oWPbHiQi6FDz4XzmNqtn8en2bF7DBI=@vger.kernel.org X-Gm-Message-State: AOJu0YxvxDqh3I/hq+rEl42x7W9boHzi1CGJa26Z6E41CTjDwxn+GBjV 8uB3pl1LD5FnN8C3xuWzdOrH7fNb2o+7k2NGM4fOROlP+/WNN3XuQ8U5 X-Gm-Gg: ATEYQzynv1K7Svnub/6uCcXCo2Es+DfUtr/OHidDAyI0NtuITdg39odOcL0r2udDsjz OuBBAoJqVJrlhgYsJQ0sen98gYanqkYC3GlUqe5FmML9ST+t3PC7D1U1GQKzjZK0ldO2WyqTHCv QzmA8i/jB8l+Y91gheqlpRAQv9T4+KHnRveDfQXNbIO5A+exrrQ8xYiQslsI6GaVGGp2/F8QAsh q5ZIbWswg4urct7YcXKaMQLyjB+Oh96WijoUuYOjHjjCIo1qVSQPx00CsHGWo4m6b1vNVEn7HAa HRNDFJEVF49ARv2jUVihDpktjAqjjmHGNCr4Aky5CDvzwkHFQTg/JxGEhdMXo9djBFiVZczu6rw bgIpTj5EBS+vBpJg0RElVbkoKxqa96ZtxHFTo7Z0pq8kOngukZwUA7p/PjHNjg0y+ZpmzKhX82R wXHTeeWBRqMg1h9H8gmiypceh0HKI+awQH5nit4Y3Tk8bu03pwFHgkpVKotzBfS4o6R5BPQJvjX oZZ7h47CGN+ X-Received: by 2002:a05:7022:4583:b0:127:5cda:fb7d with SMTP id a92af1059eb24-1278fc2c5fbmr491168c88.6.1772149659700; Thu, 26 Feb 2026 15:47:39 -0800 (PST) Received: from localhost (99-122-55-39.lightspeed.sntcca.sbcglobal.net. [99.122.55.39]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-127899ea1e5sm4259846c88.7.2026.02.26.15.47.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Feb 2026 15:47:39 -0800 (PST) From: Matthew Wood To: Miguel Ojeda , Luis Chamberlain , Petr Pavlu , Daniel Gomez , Sami Tolvanen Cc: Aaron Tomlin , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Tamir Duberstein , David Gow , =?UTF-8?q?Jos=C3=A9=20Exp=C3=B3sito?= , linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/8] rust: module_param: wire StringParam into the module! macro Date: Thu, 26 Feb 2026 15:47:28 -0800 Message-ID: <20260226234736.428341-3-thepacketgeek@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260226234736.428341-1-thepacketgeek@gmail.com> References: <20260226234736.428341-1-thepacketgeek@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add support for `string` as a parameter type in the module! macro. On the runtime side, add: - set_string_param(): an extern "C" callback matching the kernel_param_ops::set signature that stores the raw C string pointer directly into the SetOnce container, avoiding an unnecessary copy-and-parse round-trip. - PARAM_OPS_STRING: a static kernel_param_ops that uses set_string_param as its setter. - ModuleParam impl for StringParam with try_from_param_arg() returning -EINVAL, since string parameters are populated exclusively through the kernel's set callback. On the macro side: - Change the Parameter::ptype field from Ident to syn::Type to support path-qualified types. - Recognize the `string` shorthand and resolve it to the fully qualified ::kernel::module_param::StringParam type during code generation. - Wrap string default values with StringParam::from_c_str(c_str!(...)) to produce a compile-time CStr-backed default. - Route `string` to PARAM_OPS_STRING in param_ops_path(). Signed-off-by: Matthew Wood --- rust/kernel/module_param.rs | 48 +++++++++++++++++++++++++++++++++++++ rust/macros/module.rs | 42 +++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 9 deletions(-) diff --git a/rust/kernel/module_param.rs b/rust/kernel/module_param.rs index 80fe8643c0ab..67ff6f2ea9c2 100644 --- a/rust/kernel/module_param.rs +++ b/rust/kernel/module_param.rs @@ -86,6 +86,36 @@ pub trait ModuleParam: Sized + Copy { }) } =20 +/// Set a string module parameter from a string. +/// +/// Similar to [`set_param`] but for [`StringParam`]. +/// +/// # Safety +/// +/// Same requirements as [`set_param`]. +unsafe extern "C" fn set_string_param( + val: *const c_char, + param: *const bindings::kernel_param, +) -> c_int { + if val.is_null() { + crate::pr_warn!("Null pointer passed to `module_param::set_string_= param`"); + return EINVAL.to_errno(); + } + + crate::error::from_result(|| { + // SAFETY: val points to a valid C string from the kernel. + let cstr_param =3D unsafe { StringParam::from_ptr(val) }; + + // SAFETY: By function safety requirements, param.arg points to ou= r SetOnce. + let container =3D unsafe { &*((*param).__bindgen_anon_1.arg.cast::= >()) }; + + container + .populate(cstr_param) + .then_some(0) + .ok_or(kernel::error::code::EEXIST) + }) +} + macro_rules! impl_int_module_param { ($ty:ident) =3D> { impl ModuleParam for $ty { @@ -175,6 +205,15 @@ pub fn as_bytes(&self) -> Option<&[u8]> { unsafe impl Send for StringParam {} unsafe impl Sync for StringParam {} =20 +impl ModuleParam for StringParam { + fn try_from_param_arg(_arg: &BStr) -> Result { + // For StringParam, we don't parse here - the kernel's set callback + // directly stores the pointer. This method should not be called + // when using PARAM_OPS_STRING. + Err(EINVAL) + } +} + /// A wrapper for kernel parameters. /// /// This type is instantiated by the [`module!`] macro when module paramet= ers are @@ -249,3 +288,12 @@ macro_rules! make_param_ops { make_param_ops!(PARAM_OPS_U64, u64); make_param_ops!(PARAM_OPS_ISIZE, isize); make_param_ops!(PARAM_OPS_USIZE, usize); + +/// Parameter ops for string parameters. +#[doc(hidden)] +pub static PARAM_OPS_STRING: bindings::kernel_param_ops =3D bindings::kern= el_param_ops { + flags: 0, + set: Some(set_string_param), + get: None, + free: None, +}; diff --git a/rust/macros/module.rs b/rust/macros/module.rs index e16298e520c7..0d76743741fb 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -8,7 +8,8 @@ }; use quote::{ format_ident, - quote, // + quote, + ToTokens, // }; use syn::{ braced, @@ -120,13 +121,15 @@ fn emit_params(&mut self, info: &ModuleInfo) { =20 for param in params { let param_name_str =3D param.name.to_string(); - let param_type_str =3D param.ptype.to_string(); + let param_type_str =3D param.ptype.to_token_stream().to_string= (); + // Clean up the type string for modinfo (remove spaces around = ::) + let param_type_clean =3D param_type_str.replace(" ", ""); =20 let ops =3D param_ops_path(¶m_type_str); =20 // Note: The spelling of these fields is dictated by the user = space // tool `modinfo`. - self.emit_param("parmtype", ¶m_name_str, ¶m_type_str); + self.emit_param("parmtype", ¶m_name_str, ¶m_type_clean= ); self.emit_param("parm", ¶m_name_str, ¶m.description.va= lue()); =20 let static_name =3D format_ident!("__{}_{}_struct", self.modul= e, param.name); @@ -137,14 +140,32 @@ fn emit_params(&mut self, info: &ModuleInfo) { .expect("name contains NUL-terminator"); =20 let param_name =3D ¶m.name; - let param_type =3D ¶m.ptype; let param_default =3D ¶m.default; =20 + // `string` is a shorthand for `StringParam` in the macro =E2= =80=94 resolve to + // the real type for code generation. + let is_str_param =3D param_type_str =3D=3D "string"; + let actual_type: Type =3D if is_str_param { + parse_quote!(::kernel::module_param::StringParam) + } else { + param.ptype.clone() + }; + + // For `string` params the default is always a string literal = which + // gets wrapped with StringParam::from_c_str(kernel::c_str!(..= .)). + let default_expr =3D if is_str_param { + quote! { + ::kernel::module_param::StringParam::from_c_str(::kern= el::c_str!(#param_default)) + } + } else { + quote!(#param_default) + }; + self.param_ts.extend(quote! { #[allow(non_upper_case_globals)] pub(crate) static #param_name: - ::kernel::module_param::ModuleParamAccess<#param_type>= =3D - ::kernel::module_param::ModuleParamAccess::new(#pa= ram_default); + ::kernel::module_param::ModuleParamAccess<#actual_type= > =3D + ::kernel::module_param::ModuleParamAccess::new(#de= fault_expr); =20 const _: () =3D { #[allow(non_upper_case_globals)] @@ -186,7 +207,9 @@ fn emit_params(&mut self, info: &ModuleInfo) { } =20 fn param_ops_path(param_type: &str) -> Path { - match param_type { + let type_name =3D param_type.rsplit("::").next().unwrap_or(param_type)= .trim(); + + match type_name { "i8" =3D> parse_quote!(::kernel::module_param::PARAM_OPS_I8), "u8" =3D> parse_quote!(::kernel::module_param::PARAM_OPS_U8), "i16" =3D> parse_quote!(::kernel::module_param::PARAM_OPS_I16), @@ -197,6 +220,7 @@ fn param_ops_path(param_type: &str) -> Path { "u64" =3D> parse_quote!(::kernel::module_param::PARAM_OPS_U64), "isize" =3D> parse_quote!(::kernel::module_param::PARAM_OPS_ISIZE), "usize" =3D> parse_quote!(::kernel::module_param::PARAM_OPS_USIZE), + "string" =3D> parse_quote!(::kernel::module_param::PARAM_OPS_STRIN= G), t =3D> panic!("Unsupported parameter type {}", t), } } @@ -340,7 +364,7 @@ macro_rules! parse_ordered_fields { =20 struct Parameter { name: Ident, - ptype: Ident, + ptype: Type, default: Expr, description: LitStr, } @@ -349,7 +373,7 @@ impl Parse for Parameter { fn parse(input: ParseStream<'_>) -> Result { let name =3D input.parse()?; input.parse::()?; - let ptype =3D input.parse()?; + let ptype: Type =3D input.parse()?; =20 let fields; braced!(fields in input); --=20 2.52.0