Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp3258790lfo; Mon, 23 May 2022 00:11:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5gBkP91XZYLtHWodQTZJBTjcoOJkCw5IykvXA6WvlxSQEQAQ9UB5seCOrqEJnPmQiHwRp X-Received: by 2002:a65:5a41:0:b0:3fa:8780:9268 with SMTP id z1-20020a655a41000000b003fa87809268mr405360pgs.14.1653289864975; Mon, 23 May 2022 00:11:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653289864; cv=none; d=google.com; s=arc-20160816; b=k/QlprhRLXnIJ59mR+tPkOjnySxtRjKj18wH10R4unew+EPYPKLeVIZM6UxH9gD1th UhQHV1yOF1oAGXTWsJDr4P38/dyCBWQGx2vczWzfZlEzDZXgJTRZprTfSicDTNxi/dYc 7MyViEYj3C4hxdlKuEsEpxaO1QO4/gZE3FxbeWP7WFp4OySsgK7hUR68hZPDm7a7icon xheIpiNY/vjSuTXeKRSGGNIALbPTUIVCecN7IlG7Ay4qXLrKirtQINaVDaKdHHil/6lQ d0v3K3MlVt3XmPkajoXoOyxijTcgULX2xhcp3AK2H/vb2Mq6KK8h7KGCvEPr6xUEnQlK GyhQ== 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=x09c4xhGh7uQKLbu3XotLzWvZZC2IJi6w2Vg4GvGjRw=; b=rGMgEDs5szuQLcRzv2usWuRgsUqrHI+JsEJ08x5XT4a4vYCk8XOL9am15RFktnLyvr C+wO3Pgx1a76pWLfn6Dhbuq5WuceTJetlu96qbAamMBaWMjMVUYK/Kk5i8rrlwl6/tL5 a706oZsCcw67KsRWNY2QXXSxAiQR2iKYC+MIhw7zqzEgWkV9lTPGb+VASnGDjLKQFqdZ FtfZ988Ba0ZgP4Ww83CgYkNU4cI3SAyVErE7hpk757s2zax7p6CQiKCAJYhDNFTgncbQ shEBGNyvsYJ4HKAyJEdJ+EyyfOFlEt0SR1f5Maq+l9/kRH7lpIH34IwD2sYscEpI39sW fcww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZoctJ3Yp; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id m1-20020a635801000000b003fa387b1989si4596232pgb.369.2022.05.23.00.11.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 00:11:04 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZoctJ3Yp; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 21935719F2; Sun, 22 May 2022 23:30:31 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352698AbiEWCFW (ORCPT + 99 others); Sun, 22 May 2022 22:05:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352706AbiEWCEX (ORCPT ); Sun, 22 May 2022 22:04:23 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46B90393E7; Sun, 22 May 2022 19:04:05 -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 E6486B80EA4; Mon, 23 May 2022 02:04:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0E213C385B8; Mon, 23 May 2022 02:03:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653271442; bh=1Qao3u36Cz5T23DpMMNUsVv6keklDYRYkLsx0OSQpNo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZoctJ3YpVcA+W5+lfXZqA+GgtbSCWRQlkXwRvBtXbSNpX1rpozoAd1RLuOnQ1Ph3n tTkm+t5dZYWTphQgsGn2puM7URM83bjbC5dQXzKfDtctecQsEMwwyqh5Fw82GwM6L3 CZf7nUKCGt9za5oEKqKAPc3AhJp7tRE2ef11APWlZhZKu8E64Avo6/uPIqMQ3aP4pT J4okDU0UopO3IAo4wr+5FoIdWY4fO1xzWX5Y90tabnm0miS/4QmkJL5GsOO4hG3dKk vlp6fRYoCu8dhtXMnJpFs2IEm2Ejg7IxY2N+9+bWrruz/7f+yoCynS0XaTbxRbBVyM TyPvmSPY8w2uA== 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 , 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 v7 14/25] vsprintf: add new `%pA` format specifier Date: Mon, 23 May 2022 04:01:27 +0200 Message-Id: <20220523020209.11810-15-ojeda@kernel.org> In-Reply-To: <20220523020209.11810-1-ojeda@kernel.org> References: <20220523020209.11810-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. 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 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.36.1