Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp4633057pxb; Sat, 12 Feb 2022 12:09:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJw9iG2FvG4lR6238IYXINwcEn2+Lk0U2D8d+PJEkrWMrmHFK43wMXY4CUDpt9JmNfoQl3Dr X-Received: by 2002:a17:907:d1e:: with SMTP id gn30mr6092797ejc.75.1644696591963; Sat, 12 Feb 2022 12:09:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644696591; cv=none; d=google.com; s=arc-20160816; b=IDPB9E8EeL9T9JEaPPOHVogEC5pqQaDM30zhC+mavIL7Ia8pEmXTyF8BMLkxSdRuIf fLYvLU1RZnnXxitUToWOoMF/wKuLj0RmrytRpAje/f0WtDJY7qCt07iZjIa88l8FtDsB xvwSVo7w49g9e+ThMfut6O2G3TQ0ohMFj0ah9U0jy0SQwrgS5Q0xhpmr4stwCLnCjTZL z+QZWjNBNX7+V+CqTT1HsLFMOc2EjL85ONWQ2FTnCM4ujeyDCn7O6ZZYMjVFIT/qK4qm PmMR154FxBgqinRwPvIUA+AYetIUpRh6/qI2jjG7pyTTrnw2FirJ9n/Z3NDir78P7J7Y 9siw== 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=cWA1pCT0MakJMhrCIpSUfOUIpgj5v7r5XqgdAxYjQnc=; b=lQLJ9ET2PbYQlIFOz77k4KgNGoDAsCCab6F7RcifAPU3Kg6Mb61C33PWvXvhMNwrd6 yU/Xnd6gRx0iEROsIKz99ghzwsf8XSU7//p3aD6RaJhjy2Ik8ORJsa5uYfhWdyNeJsof Lcy2I1SbBfv+QG1DpsGE5R+9TG7vnOhChQJ4MreWVhjqRPcKqT244QMpJaWCYbqxEzpN h2oHaJNEDecji2iCJRtKTq8XycYCtTlQ7jHXSBKCKt7+XJlKNoco9wmBVGoLFy7tYJqS vP1ldzVGPNss9gG+XKKoK79sAjKFEHI6G4coigmHCwFA4nvjd3qEvd6mns/kLZOTnhUm oJOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jOj7joDy; 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 n25si3186259edq.548.2022.02.12.12.09.09; Sat, 12 Feb 2022 12:09:51 -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=jOj7joDy; 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 S235439AbiBLNGU (ORCPT + 99 others); Sat, 12 Feb 2022 08:06:20 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:56194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235157AbiBLNGG (ORCPT ); Sat, 12 Feb 2022 08:06:06 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45DF927157; Sat, 12 Feb 2022 05:06:03 -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 E98C9B8047E; Sat, 12 Feb 2022 13:06:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD686C340EB; Sat, 12 Feb 2022 13:05:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1644671160; bh=BMlEDMQoYdmU2yQnqZ+p5mIOS0XWjQWV+K/PMwRTVc8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jOj7joDyq3ynSytZ/A3fPTrvjzA5YDTbJqC9qFHBpFEEMruFPDOfTGz4rtHlgufX6 duGrRVQ8JJQGTMHw2LsfXlxVJTN5u3jVNtRg3uZZWlO5qZEPewX5F1K5gDaViOXxNO qThkuU7Ub+leThdZra//ta5S71DMWNxcF6aauNzLujcYnww1PZlYL/OsPvilBm/G2Y AeMb52XefuyKoNc4qryCIIr6UDd5X4QhdwKRQI7vsEhIx1MIEPeI2wkI/Wmm0tN3Rm S5JjlsQ5dJEz27rFEF/CPtfHsbU9Y+q1/Eq8ReQxHBpCdO6yHhM8xPJxFYOh91WLV3 5OmdZR+CFIyig== 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 v4 12/20] vsprintf: add new `%pA` format specifier Date: Sat, 12 Feb 2022 14:03:38 +0100 Message-Id: <20220212130410.6901-13-ojeda@kernel.org> In-Reply-To: <20220212130410.6901-1-ojeda@kernel.org> References: <20220212130410.6901-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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,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. 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/rust.h | 14 ++++++++++++++ lib/vsprintf.c | 7 +++++++ 2 files changed, 21 insertions(+) create mode 100644 lib/rust.h diff --git a/lib/rust.h b/lib/rust.h new file mode 100644 index 000000000000..9cf0b102b496 --- /dev/null +++ b/lib/rust.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LIB_RUST_H +#define _LIB_RUST_H + +#ifdef CONFIG_RUST +char *rust_fmt_argument(char* buf, char* end, void *ptr); +#else +static inline char *rust_fmt_argument(char* buf, char* end, void *ptr) +{ + return NULL; +} +#endif + +#endif /* _LIB_RUST_H */ diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3b8129dd374c..61528094ec87 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -52,6 +52,7 @@ #include #include "kstrtox.h" +#include "rust.h" static noinline unsigned long long simple_strntoull(const char *startp, size_t max_chars, char **endp, unsigned int base) { @@ -2378,6 +2379,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 +2455,8 @@ 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': + return rust_fmt_argument(buf, end, ptr); case 'x': return pointer_string(buf, end, ptr, spec); case 'e': -- 2.35.1