From patchwork Fri Dec 2 16:14:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 28972 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp948031wrr; Fri, 2 Dec 2022 08:19:03 -0800 (PST) X-Google-Smtp-Source: AA0mqf7mgj2e0K0iIyY8KZ5GnYnVPGYPw33hVIcOxRtChAUeRG4crloa86aZqdxgerTnzHSEJSJu X-Received: by 2002:a05:6a00:32c7:b0:565:f8bb:96f8 with SMTP id cl7-20020a056a0032c700b00565f8bb96f8mr52873180pfb.45.1669997943176; Fri, 02 Dec 2022 08:19:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669997943; cv=none; d=google.com; s=arc-20160816; b=CDOdpMcFwbvjtFyFzY2M0OP3hqDWgyYRAnf4wl6UJis8PdCIEPvSYCBKx7V9JFELox 6/wAGtp9EGByBybd4OutlgGPqHyw/Gtn2hMrp3WTcEFwgqsNdfACeKL9ShHVOcaAGovb rgtxWJgpcQ6TncmbV0DkH6q4rP/dfpTb74QsV9gnnfVRvQK55psKm/62CxTZyAwsvQnq M91AzCCdLLYjrcKRzHu+lLlun5Crf1Fc0KPFzW40xD+1Qn++7IgtjB8+zhGoCTTkRxvw pFdIAVWtxCgE+iPn8NDij9RZs+CVXzVK9xeOTq1EAmTz2z5tBrKyzyNOflmhhnT6YzcR gmaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=R5pWSh7qEY9z88C3y677Nqp9ptnKnyF/XTUSx+C3Yyc=; b=iW5xRGZDPMRVzcwX0d2WgKWJEGoaqcROmy1ZQOhsxgY4NluQmBsxLsXltOBFNfwa8o OOPhbrSRIXjNupy8A1A3ZY3LOZCMUu2A+ySBD7XNrpDG1Fcxfsu4dQ3qwMHjzUfnszUD /RZQEMEJqwZpZDQ2vPUkEm1Y2UKucrMJlwvYPq6WyVzEmc/5YFdYmdKbROTWbLDJ0CCO yfqBf1S5VS8a+h+DAU4kKSOGcL6FbWoSuOLmtWsF58EGLo8VZnjPxqPX5bzsj1zRMbVx cKFrD7qycWJuPlR1+TYDNfmaJ0k+9w+nZNoebo8uyUa3tKj4lhPRQttxQ4rl5lTBudH8 bL0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HvSJwTYw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w2-20020a634742000000b0043c474c8942si7618354pgk.673.2022.12.02.08.18.44; Fri, 02 Dec 2022 08:19:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HvSJwTYw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233794AbiLBQPv (ORCPT + 99 others); Fri, 2 Dec 2022 11:15:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233804AbiLBQPs (ORCPT ); Fri, 2 Dec 2022 11:15:48 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBF77BF672; Fri, 2 Dec 2022 08:15:39 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A77AAB82149; Fri, 2 Dec 2022 16:15:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B90A5C433B5; Fri, 2 Dec 2022 16:15:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997737; bh=KgBgOHXymfu3Q4eF7TqCl7XFCIPthuyaHkGisjhFOJU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HvSJwTYwz3NOYYnSx08OJX8VHQx7tH1bvPKzRc+tiNRu91a1p/z9Uql8RBXudanLJ 4G5wCXmEDEm62GzPAo84iWJEBbOOiNt/cNgJ6S5WMmGJwANNpyN9dRtEoUifRyWzw1 fkJlOEQ+uz6rVdyfVKfl5nijV1E9j6BeFfODghP0anUI86dh/7koMb5+rIAElNmjuk 5wd/Wf+tNLyUFQrIDrmHCdKNTafdFDpf95szcMXEPe10AIdzW9cIIUbIZA7tqGgb1p gaJ37RnxKVm87j+LwSNJ7DwInuaLhZrhPNdoX51idPSSPEjiB7bs4COzUFHqWiS8at Z9imFPWsKyLGA== From: ojeda@kernel.org To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Adam Bratschi-Kaye , Wei Liu , =?utf-8?q?Sergio_Gonz=C3=A1lez_Collado?= Subject: [PATCH v2 03/28] rust: print: add `pr_cont!` macro Date: Fri, 2 Dec 2022 17:14:34 +0100 Message-Id: <20221202161502.385525-4-ojeda@kernel.org> In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org> References: <20221202161502.385525-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751119763362122874?= X-GMAIL-MSGID: =?utf-8?q?1751119763362122874?= From: Miguel Ojeda This level is a bit different from the rest since it does not pass the module name to the `_printk()` call. Thus add a new parameter to the general `print_macro!` to handle it differently. Co-developed-by: Adam Bratschi-Kaye Signed-off-by: Adam Bratschi-Kaye Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Gary Guo Signed-off-by: Gary Guo Reviewed-by: Wei Liu Reviewed-by: Sergio González Collado Signed-off-by: Miguel Ojeda --- rust/kernel/print.rs | 72 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 694f51c6da5c..29bf9c2e8aee 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -81,6 +81,7 @@ pub mod format_strings { pub static NOTICE: [u8; LENGTH] = generate(false, bindings::KERN_NOTICE); pub static INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO); pub static DEBUG: [u8; LENGTH] = generate(false, bindings::KERN_DEBUG); + pub static CONT: [u8; LENGTH] = generate(true, bindings::KERN_CONT); } /// Prints a message via the kernel's [`_printk`]. @@ -111,6 +112,26 @@ pub unsafe fn call_printk( } } +/// Prints a message via the kernel's [`_printk`] for the `CONT` level. +/// +/// Public but hidden since it should only be used from public macros. +/// +/// [`_printk`]: ../../../../include/linux/printk.h +#[doc(hidden)] +#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))] +pub fn call_printk_cont(args: fmt::Arguments<'_>) { + // `_printk` does not seem to fail in any path. + // + // SAFETY: The format string is fixed. + #[cfg(CONFIG_PRINTK)] + unsafe { + bindings::_printk( + format_strings::CONT.as_ptr() as _, + &args as *const _ as *const c_void, + ); + } +} + /// Performs formatting and forwards the string to [`call_printk`]. /// /// Public but hidden since it should only be used from public macros. @@ -120,7 +141,7 @@ pub unsafe fn call_printk( #[allow(clippy::crate_in_macro_def)] macro_rules! print_macro ( // The non-continuation cases (most of them, e.g. `INFO`). - ($format_string:path, $($arg:tt)+) => ( + ($format_string:path, false, $($arg:tt)+) => ( // SAFETY: This hidden macro should only be called by the documented // printing macros which ensure the format string is one of the fixed // ones. All `__LOG_PREFIX`s are null-terminated as they are generated @@ -134,6 +155,13 @@ macro_rules! print_macro ( ); } ); + + // The `CONT` case. + ($format_string:path, true, $($arg:tt)+) => ( + $crate::print::call_printk_cont( + format_args!($($arg)+), + ); + ); ); /// Stub for doctests @@ -174,7 +202,7 @@ macro_rules! print_macro ( #[macro_export] macro_rules! pr_emerg ( ($($arg:tt)*) => ( - $crate::print_macro!($crate::print::format_strings::EMERG, $($arg)*) + $crate::print_macro!($crate::print::format_strings::EMERG, false, $($arg)*) ) ); @@ -198,7 +226,7 @@ macro_rules! pr_emerg ( #[macro_export] macro_rules! pr_alert ( ($($arg:tt)*) => ( - $crate::print_macro!($crate::print::format_strings::ALERT, $($arg)*) + $crate::print_macro!($crate::print::format_strings::ALERT, false, $($arg)*) ) ); @@ -222,7 +250,7 @@ macro_rules! pr_alert ( #[macro_export] macro_rules! pr_crit ( ($($arg:tt)*) => ( - $crate::print_macro!($crate::print::format_strings::CRIT, $($arg)*) + $crate::print_macro!($crate::print::format_strings::CRIT, false, $($arg)*) ) ); @@ -246,7 +274,7 @@ macro_rules! pr_crit ( #[macro_export] macro_rules! pr_err ( ($($arg:tt)*) => ( - $crate::print_macro!($crate::print::format_strings::ERR, $($arg)*) + $crate::print_macro!($crate::print::format_strings::ERR, false, $($arg)*) ) ); @@ -270,7 +298,7 @@ macro_rules! pr_err ( #[macro_export] macro_rules! pr_warn ( ($($arg:tt)*) => ( - $crate::print_macro!($crate::print::format_strings::WARNING, $($arg)*) + $crate::print_macro!($crate::print::format_strings::WARNING, false, $($arg)*) ) ); @@ -294,7 +322,7 @@ macro_rules! pr_warn ( #[macro_export] macro_rules! pr_notice ( ($($arg:tt)*) => ( - $crate::print_macro!($crate::print::format_strings::NOTICE, $($arg)*) + $crate::print_macro!($crate::print::format_strings::NOTICE, false, $($arg)*) ) ); @@ -319,7 +347,7 @@ macro_rules! pr_notice ( #[doc(alias = "print")] macro_rules! pr_info ( ($($arg:tt)*) => ( - $crate::print_macro!($crate::print::format_strings::INFO, $($arg)*) + $crate::print_macro!($crate::print::format_strings::INFO, false, $($arg)*) ) ); @@ -346,7 +374,33 @@ macro_rules! pr_info ( macro_rules! pr_debug ( ($($arg:tt)*) => ( if cfg!(debug_assertions) { - $crate::print_macro!($crate::print::format_strings::DEBUG, $($arg)*) + $crate::print_macro!($crate::print::format_strings::DEBUG, false, $($arg)*) } ) ); + +/// Continues a previous log message in the same line. +/// +/// Use only when continuing a previous `pr_*!` macro (e.g. [`pr_info!`]). +/// +/// Equivalent to the kernel's [`pr_cont`] macro. +/// +/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and +/// `alloc::format!` for information about the formatting syntax. +/// +/// [`pr_cont`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_cont +/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html +/// +/// # Examples +/// +/// ``` +/// # use kernel::pr_cont; +/// pr_info!("hello"); +/// pr_cont!(" {}\n", "there"); +/// ``` +#[macro_export] +macro_rules! pr_cont ( + ($($arg:tt)*) => ( + $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*) + ) +);