Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4813574iob; Mon, 9 May 2022 02:10:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKNO8Qz1svn35wWMUf7GT2+yePoPp3JGyjM9O5PXwXCwpmOAutY1HMW0BnL8iWQCR6fgDE X-Received: by 2002:a17:902:9b93:b0:15f:17ce:3b97 with SMTP id y19-20020a1709029b9300b0015f17ce3b97mr2334371plp.174.1652087408604; Mon, 09 May 2022 02:10:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652087408; cv=none; d=google.com; s=arc-20160816; b=KXXCMplfA4+37wLflA8HuxvVjtvmE3jWEUOWwT0VRK8B7uiJEV6vevmLr3T3dRyj8c UB/qjwgGZldUrr9yEIXz5gfmyt2S0L+OJlx2xabuYjwp0sAWBs4rPsLkvvPDiNnsw0Uj xX07GBgViDT3Q6USX+iAiuCAzHtdaiJG4MvQsIR2ML9JjY+D74r2IedKbX90n1OpTFvI J/x7v+Kmtz7ma/n9+dCnv+aUjaQ8r70mmLsloOOuwM5hEqtPIxjWYokLWc0QoTYtoyPY Fxs3ugUoq21SGNLbukSptu5kXUcvZaSMKM2bU9rVvQK2I4HuDh8avZCgg3LVDhwI0Lod 7oTA== 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=mOb3qnVI1ZnxQZh10Nfh7eNaPUbai9eVOql8o3CGMyY=; b=j5QYkWpt3JqnsHFNuQC8gQgHL4FLxmN6Ds0aLzt+kRA+YRCAfpTfQG9FiQJVoM5kMR i4pUAUVPQNtCEgkjxnAUzrVTSqYiTIi5mKPh2NanUKqwtJ+OH94z5fdW90+VdTlfI2Yv vt/sDO2sqQbNxOEFHAXuFMnAge1AU5scSeIRHwPgo7bdrQSGdz3ulUjGxkIV+Qy4CiFM i+vcC8KJQB8p0GXrtUOX2O5+aI+tLOnNEan/B7X4sNRqmkOB5fKHHU8wUOU86FUm502Q MqX3aNrHEo1A3zb3mZhNVTb+z1+477JyjbqCSax/uNfCK48fIC4WpweQZxjjBxos9770 0RdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a5A3lwsg; 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 l1-20020a654c41000000b003ab8abd36afsi14525112pgr.381.2022.05.09.02.10.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 02:10:08 -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=a5A3lwsg; 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 09F551912EF; Mon, 9 May 2022 01:57:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1445640AbiEGFcA (ORCPT + 99 others); Sat, 7 May 2022 01:32:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1445691AbiEGFaz (ORCPT ); Sat, 7 May 2022 01:30:55 -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 745656A066; Fri, 6 May 2022 22:27:01 -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 110B76009B; Sat, 7 May 2022 05:27:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9EBA4C385AE; Sat, 7 May 2022 05:26:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651901220; bh=FMEGfemoYzGIDwcOWskeGKpazApsC3L5MMeQ97qcFSo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a5A3lwsg8rVuyN4K+kI6MYS0Ydm+J7L3tI6MPHnWLL4VhjJ73nbbJENuL9iJX/HxI ZykjGTwQG1Vk7Z2gS1Uen7nWRaWkN/6iwY6f3qULVQZDy4Uz6Hs/K1a8acFNipWZV8 lRhcIocdnwSGD3F93IBqJHQVXEcnX0H4nA5itArMFnr6Gjrg6mMPwqIzlhMtEOBvE1 53qMU+7ul7+NYIbzEGoFifLaZrAgT6helOWFpjQQ4aRQ4aQEqcXBci5jDxTq0FYt0y FhVCeGtPLYj9a8TCOoWThMaDomex4JG7lj5M2Mb6mC4of6lhNYmwMG3ImQkWl8fvrO Dxbfx8JxTdKVw== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Jarkko Sakkinen , Miguel Ojeda , Gary Guo , Alex Gaynor , Wedson Almeida Filho , Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes Subject: [PATCH v6 14/23] vsprintf: add new `%pA` format specifier Date: Sat, 7 May 2022 07:24:12 +0200 Message-Id: <20220507052451.12890-15-ojeda@kernel.org> In-Reply-To: <20220507052451.12890-1-ojeda@kernel.org> References: <20220507052451.12890-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 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 40d26a07a133..00f71f91d991 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2270,6 +2270,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 @@ -2396,6 +2399,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, @@ -2468,6 +2475,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.3