Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp1895337rwb; Fri, 5 Aug 2022 09:28:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR766VIwMvvJTejsXA0+/4eCbIRV7wFUsfhAwqafLbS0svrqThDOFskaVciNteNwOwtnYHQZ X-Received: by 2002:a63:1e10:0:b0:41a:ee1c:290a with SMTP id e16-20020a631e10000000b0041aee1c290amr6323984pge.196.1659716928265; Fri, 05 Aug 2022 09:28:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659716928; cv=none; d=google.com; s=arc-20160816; b=aBgUSKoPqQYjQLJq64Ljmr0oXmHm0quKWlEdQthZr7SNPnTPZRPxR+yzpeuz5dGNpx v+7Pfbxwq6wJz4tezQv4q1Zezyy46eKOGzqB0OEdxYfa8KuL6sYpT3zoQaZ8Cv4K8J3e lJSzeq1wHdhilctP1Nkb+/mcmepo+NNY7HwfbQYjDYVJOrZb86Bevi80YpX/Sk0xPS/j 6leko6bXUN4dnZR2iPGWfyGNa7eDmtkRzs1gVUi5nALagCzZA7emEqYrjm3n3jY0lp6M odmp4KWU088/H5TGMYIjHxIoGAAtF/ak3kWlDqHQDWayuS0afVpoLMtsgk77usursLuP l0FA== 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=dL/wxhsGi328H1DLZB9a6brc+a2+bfrAFqfravxZx1Q=; b=nP31aEND2E8KQnMqy1ESmB4aYIwI+0f/HW0XVBBoXPsjJX1H1YKWLMQ6ikjTgkNuBh R48OPsD7W7wis98frrMJ4kXXnZmwo9burhCGCviLSbVM1qerXECt3WWehKFBLC+C/sqB Osao8XlGv23qT9l2p7bIbteTUBss5TdHsZDV1ed0O9rSaLIaxqFlAGci9gu8Y8XLwgJ4 343KHh3XGlwUV6GQFyrwJNgzaHHylzdyHoOTN15g0xEBl4K1bfruBdn+e6ICa4AXol9f Au+mS3uEWgm2ruLrL3lWZF6H7jco23TvmD6IJhHyvoAJLX1UZFXULdSmAaLWiMO8Au+J aB6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="PKp/4Vv0"; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 187-20020a6303c4000000b00415d3667232si3671103pgd.63.2022.08.05.09.28.33; Fri, 05 Aug 2022 09:28:48 -0700 (PDT) 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="PKp/4Vv0"; 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 S237153AbiHEPqH (ORCPT + 99 others); Fri, 5 Aug 2022 11:46:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241244AbiHEPo3 (ORCPT ); Fri, 5 Aug 2022 11:44:29 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D81C651A1C; Fri, 5 Aug 2022 08:44:00 -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 ams.source.kernel.org (Postfix) with ESMTPS id 98555B82986; Fri, 5 Aug 2022 15:43:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57C69C43470; Fri, 5 Aug 2022 15:43:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659714238; bh=LJMbIbsmGPh8jLekYZBgD3WYeoa1VFA5q4dxEXu4mdw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PKp/4Vv0TnHtJlfe+/ahygOjq18Jn5dHvo9ossvP42owNhBfsYn0CTcEWgJTXOlJm Vh3R1XXKxCeODfbBShRuyUGaH1hIZTgLUndM488qsYF0qz5/Th1uWck4dyCCo0jfey dTZHmlmiiE0kdfHg0FwaSbpteeBAvQawqKiC0eR57OiE7R95wwoZQ+w9kwfTEbTQAT 8kDIDVEVzKMEEd5DWPEfBPPmDbrWBOEjQaEYVUr/cQaOZdQ6l7cWg4oPCQFBm0jvd3 UVlbxQbYshIE5QxG5wTgZJKN0IRwoJJY2lVZ+JvGI5mPmrKbFvNMTuX4SxihvteUBE cRaMGDshcJB+w== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, patches@lists.linux.dev, Jarkko Sakkinen , Miguel Ojeda , Gary Guo , Kees Cook , Petr Mladek , Alex Gaynor , Wedson Almeida Filho , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes , Jonathan Corbet , linux-doc@vger.kernel.org Subject: [PATCH v9 14/27] vsprintf: add new `%pA` format specifier Date: Fri, 5 Aug 2022 17:41:59 +0200 Message-Id: <20220805154231.31257-15-ojeda@kernel.org> In-Reply-To: <20220805154231.31257-1-ojeda@kernel.org> References: <20220805154231.31257-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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,T_SCC_BODY_TEXT_LINE 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 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. Reviewed-by: Kees Cook Acked-by: Petr Mladek 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 --- Documentation/core-api/printk-formats.rst | 10 ++++++++++ lib/vsprintf.c | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst index 5e89497ba314..dbe1aacc79d0 100644 --- a/Documentation/core-api/printk-formats.rst +++ b/Documentation/core-api/printk-formats.rst @@ -625,6 +625,16 @@ Examples:: %p4cc Y10 little-endian (0x20303159) %p4cc NV12 big-endian (0xb231564e) +Rust +---- + +:: + + %pA + +Only intended to be used from Rust code to format ``core::fmt::Arguments``. +Do *not* use it from C. + Thanks ====== diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3c1853a9d1c0..c414a8d9f1ea 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2246,6 +2246,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 @@ -2372,6 +2375,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, @@ -2444,6 +2451,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.37.1