From patchwork Wed Apr 26 17:17:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 87909 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp397774vqo; Wed, 26 Apr 2023 10:19:47 -0700 (PDT) X-Google-Smtp-Source: AKy350bYXHeThYeDc/V5/vGowvgJG3GoIyTc/YXc1jIKZJrroTXlxheko1+9v5bAr9zBcBIMHoKr X-Received: by 2002:a05:6a21:798:b0:c6:c0c1:b1fe with SMTP id mg24-20020a056a21079800b000c6c0c1b1femr23561595pzb.57.1682529586809; Wed, 26 Apr 2023 10:19:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682529586; cv=none; d=google.com; s=arc-20160816; b=F2WP3tHy0hIuekKvdqo4wvu3+MevCkXVYvI5DBJfC6uEXjDFvPZ34d8awKMzoKVJPV pB4B22x0VtJs6/RUT5yNImQEWQptfA2Y3vZ+W3Gu0Xlen+kA+7ez4mjcMt6ikol5H06H GoOdi3t1vDdtYcsrcEitQ1EL8Bl+iILDsSJd3+uIV8lg4jwgLKVVOPsiqwg+jmD1MkA3 nKTFdeCLLtReZ/dC/N2q3QDMyc2MAeE/f9A9YpxmORpAb2c0qeSU+5QEfV9LLZFdRKOd OpM5oz03cPIL7hTBot1fCmxDv8659dCd8LWBQDcSxe4LgKpG/FHp1HP4iSItSN/ZHtSU VY1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id; bh=Fj3O8Wkn1ABsQB0hg/xqdYc0OsL2aWiXoQtz2xhsL5g=; b=O9z/aTKg9UHRkSXRAO3wVm+lPWJ/v1MFbhrL4Povs6lHXezOGvCQ00osUAmTpJhFOn p7Jdqquv3yAro/neBXKD02OLZ0CjIGlHOffH7poEgjF9WUhC4tMMUxpIhRY/G48uvq0L n6kI5ELl3Lsq5vptrRJzhv23izoodUBDATZeMSK3lxJxQ0LRojOtNPxWKY5+Fj6E7lQq Wg50XdyZlxaGUIYsClAQUTdQGyZIsrIJEUHgAONFr+qDGHEdo0CvaUB9WYQ0vTiq4SzQ zb/CjuVFSSPfMeaYsn2UcEDVT8W/UBS38tjlBsALTOzTeMckJpznJbDM89s3o54UCAl6 QmZw== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v20-20020a17090a899400b00246cad7fab9si16112013pjn.180.2023.04.26.10.19.31; Wed, 26 Apr 2023 10:19:46 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234867AbjDZRSC (ORCPT + 99 others); Wed, 26 Apr 2023 13:18:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234139AbjDZRRw (ORCPT ); Wed, 26 Apr 2023 13:17:52 -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 0C18965B7 for ; Wed, 26 Apr 2023 10:17:52 -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 9BB9A636FF for ; Wed, 26 Apr 2023 17:17:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0FD34C433EF; Wed, 26 Apr 2023 17:17:51 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.96) (envelope-from ) id 1prim6-005KZJ-02; Wed, 26 Apr 2023 13:17:50 -0400 Message-ID: <20230426171749.823208218@goodmis.org> User-Agent: quilt/0.66 Date: Wed, 26 Apr 2023 13:17:07 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Andrew Morton , Sergey Senozhatsky , Petr Mladek , Yosry Ahmed Subject: [for-next][PATCH 04/11] seq_buf: Add seq_buf_do_printk() helper References: <20230426171703.202523909@goodmis.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764260143959016385?= X-GMAIL-MSGID: =?utf-8?q?1764260143959016385?= From: Sergey Senozhatsky Sometimes we use seq_buf to format a string buffer, which we then pass to printk(). However, in certain situations the seq_buf string buffer can get too big, exceeding the PRINTKRB_RECORD_MAX bytes limit, and causing printk() to truncate the string. Add a new seq_buf helper. This helper prints the seq_buf string buffer line by line, using \n as a delimiter, rather than passing the whole string buffer to printk() at once. Link: https://lkml.kernel.org/r/20230415100110.1419872-1-senozhatsky@chromium.org Cc: Andrew Morton Signed-off-by: Sergey Senozhatsky Reviewed-by: Petr Mladek Tested-by: Yosry Ahmed Signed-off-by: Steven Rostedt (Google) --- include/linux/seq_buf.h | 2 ++ lib/seq_buf.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h index 5b31c5147969..515d7fcb9634 100644 --- a/include/linux/seq_buf.h +++ b/include/linux/seq_buf.h @@ -159,4 +159,6 @@ extern int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary); #endif +void seq_buf_do_printk(struct seq_buf *s, const char *lvl); + #endif /* _LINUX_SEQ_BUF_H */ diff --git a/lib/seq_buf.c b/lib/seq_buf.c index 0a68f7aa85d6..45c450f423fa 100644 --- a/lib/seq_buf.c +++ b/lib/seq_buf.c @@ -93,6 +93,38 @@ int seq_buf_printf(struct seq_buf *s, const char *fmt, ...) } EXPORT_SYMBOL_GPL(seq_buf_printf); +/** + * seq_buf_do_printk - printk seq_buf line by line + * @s: seq_buf descriptor + * @lvl: printk level + * + * printk()-s a multi-line sequential buffer line by line. The function + * makes sure that the buffer in @s is nul terminated and safe to read + * as a string. + */ +void seq_buf_do_printk(struct seq_buf *s, const char *lvl) +{ + const char *start, *lf; + + if (s->size == 0 || s->len == 0) + return; + + seq_buf_terminate(s); + + start = s->buffer; + while ((lf = strchr(start, '\n'))) { + int len = lf - start + 1; + + printk("%s%.*s", lvl, len, start); + start = ++lf; + } + + /* No trailing LF */ + if (start < s->buffer + s->len) + printk("%s%s\n", lvl, start); +} +EXPORT_SYMBOL_GPL(seq_buf_do_printk); + #ifdef CONFIG_BINARY_PRINTF /** * seq_buf_bprintf - Write the printf string from binary arguments