Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1648063pxp; Thu, 17 Mar 2022 13:33:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz+NPxREKyH5wvkvrMTTc2kIhS0idHpcsazKDxO1NCHi12CAAAHAHoQdQcvPLHTttGDW0e9 X-Received: by 2002:a62:6410:0:b0:4fa:6d21:139e with SMTP id y16-20020a626410000000b004fa6d21139emr363159pfb.13.1647549195736; Thu, 17 Mar 2022 13:33:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647549195; cv=none; d=google.com; s=arc-20160816; b=Q5vuXTjgnoysYXdfcC4a5h4wj6i6G8E8eVylVAhEDEQ7ocEvt/s/kjuA+nQ2l1bBLc M/mp2helk4OjiFYE/dehj0svJneGiI5SCVJghvWqDEccz/nlr7EjDY6IYAFeBIpx8y/3 1Llv5YgSclRcgtdDEmxWZD8ZCXL3fC3eDWXjyAAtq78+yTNwUdTIdnpRjbBouZuqlUDP BJPuC5LzN84MHBgmkr6F1/mr4w5YPXnfnPq59yTMSyA7Sek8OiHYmQCX72rwoeTAH+Ux FKMmArQVMrF3Sp3IHgHnlccvNoliPwDr6224E7tYF7sV/XkPxNGd/BlLKhHIPBWomAo6 OR1g== 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=VQUBSwGvkib5BriaUPkwdH3aY6TwK72Quw4gH80Z/X0=; b=TLFjaH1dGTvfJThJpfBgNSzE3Zsk+21dK5ERyYkQBDExYy1BPfo5bpQolSjSJCcSs6 1Q007sqAYN8oqLsoDqzPZLuKtTStn6d08byyxrTPe48q7blFvAJDrJNhE8z+TDJfPr4v ezIGRvvD/WgM1fCT9R5vKC/+gWk4c+n3Qwi25vo7gBBMJk6KxfyiU5+10jhMygh2IRo7 wdlBoBmh7WDlGuapaS8bQw8IMrEy2mOGQWMTbnuSzSs6aGOXXO2DE8spxoHEUtebVpVu EjDQ5+pVnpuykNz33qLezWet0zqwbC0elWvnJvwdLrLcxbqOfHbEVRXnA25y2EIanHqc RoVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="rVt/2dLc"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id c4-20020a170902b68400b00153b2d165d9si87708pls.481.2022.03.17.13.33.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 13:33:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="rVt/2dLc"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4D8A92DFA45; Thu, 17 Mar 2022 13:07:30 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237316AbiCQSOM (ORCPT + 99 others); Thu, 17 Mar 2022 14:14:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237341AbiCQSOC (ORCPT ); Thu, 17 Mar 2022 14:14:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5736B21FF4C; Thu, 17 Mar 2022 11:12:13 -0700 (PDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id E1DA761686; Thu, 17 Mar 2022 18:12:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12CABC36AE2; Thu, 17 Mar 2022 18:12:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1647540732; bh=MONFdng+YEgoSrYdaPFkWXax0hsmifCzL0A6RLvcm+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rVt/2dLcRokncILwXtYrQQr4gbwT7NDlT81c5TT4WRbW7DH4XfrLKCPNPeHTsSnTL s9AML9o6irDb5KmMBUN/mlo5oRp+AQyxTPdWDAUlxMhLFeM7UadLvgCi483eQDzOrh PxwweT2Y7OvY3j56RjOZ9rhsdrLDSiHagAJmAbt8xUM4sWPIil662C55RtsvCnnzkb U+yzZ9GJh8WQ0bJKxEv/eEFgQMQ/RRcPyQhp+FL+qVT+DOizV/Ev36T+l5porbai04 aviBw/sQKvB0xz378HS1YdynTlRDRXu+goLJdRYVZD0jgJLue8zxkLBwM7JFO5G/n/ Hyj0OYP01jIiw== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Miguel Ojeda , Gary Guo , Alex Gaynor , Wedson Almeida Filho , Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes Subject: [PATCH v5 12/20] vsprintf: add new `%pA` format specifier Date: Thu, 17 Mar 2022 19:10:00 +0100 Message-Id: <20220317181032.15436-13-ojeda@kernel.org> In-Reply-To: <20220317181032.15436-1-ojeda@kernel.org> References: <20220317181032.15436-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Gary Guo This patch adds a format specifier `%pA` to `vsprintf` which formats a pointer as `core::fmt::Arguments`. Doing so allows us to directly format to the internal buffer of `printf`, so we do not have to use a temporary buffer on the stack to pre-assemble the message on the Rust side. This specifier is intended only to be used from Rust and not for C, so `checkpatch.pl` is intentionally unchanged to catch any misuse. Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Gary Guo Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda --- lib/vsprintf.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3b8129dd374c..881f982897b8 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2252,6 +2252,9 @@ int __init no_hash_pointers_enable(char *str) } early_param("no_hash_pointers", no_hash_pointers_enable); +/* Used for Rust formatting ('%pA'). */ +char *rust_fmt_argument(char* buf, char* end, void *ptr); + /* * Show a '%p' thing. A kernel extension is that the '%p' is followed * by an extra set of alphanumeric characters that are extended format @@ -2378,6 +2381,10 @@ early_param("no_hash_pointers", no_hash_pointers_enable); * * Note: The default behaviour (unadorned %p) is to hash the address, * rendering it useful as a unique identifier. + * + * There is also a '%pA' format specifier, but it is only intended to be used + * from Rust code to format core::fmt::Arguments. Do *not* use it from C. + * See rust/kernel/print.rs for details. */ static noinline_for_stack char *pointer(const char *fmt, char *buf, char *end, void *ptr, @@ -2450,6 +2457,12 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr, return device_node_string(buf, end, ptr, spec, fmt + 1); case 'f': return fwnode_string(buf, end, ptr, spec, fmt + 1); + case 'A': + if (!IS_ENABLED(CONFIG_RUST)) { + WARN_ONCE(1, "Please remove %%pA from non-Rust code\n"); + return error_string(buf, end, "(%pA?)", spec); + } + return rust_fmt_argument(buf, end, ptr); case 'x': return pointer_string(buf, end, ptr, spec); case 'e': -- 2.35.1