From patchwork Wed Aug 9 15:54:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 133345 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2942392vqr; Wed, 9 Aug 2023 10:05:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHI9CBUN8FeDBYgpCJIgRUG/spk6y2K6xtTuFs/r3++hIkdlTm6xtMm+U16u64DddbwtRqU X-Received: by 2002:a17:906:10cb:b0:99b:e6ec:752c with SMTP id v11-20020a17090610cb00b0099be6ec752cmr3078756ejv.70.1691600714788; Wed, 09 Aug 2023 10:05:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691600714; cv=none; d=google.com; s=arc-20160816; b=gtzrC5g2v5maCqW3C4KZp8GVNu+0qm//3HAib+XMKyvbgzLkRbOdYWW9bONJh4k0Ix rosEVMmf6Hp2FE4+Hx0c71zUhRSjNreuGcKLMoo6f6Xf+ZhrAo2sjmGl3U2AQurubAYX 3nuKDhH19umxMJqmZ0518QQnZ266zIPH0oTspHyy5NNI4KZPPGhsPsHYcqBV4p9lrtV1 Xw1m7UL9GGYebKyc6BSP13Y5xNQ3sJzUkUsxuYvUv0Hr4AYSOYcY+71QIcwH+/7DPsee cwemLE52DKhDFwJILAKhtptQcvxc91sDp7CcjsG0x2YpgkvhF5yYDFDtvtpO8lFmPMQw VfYw== 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=+CEzH+rL6x1SVzo0jVm7sIO4j6kpVtWp3r0+9eaMezs=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=zW/HTGy5uexqAtxmcLS0dWJE0RZQrDQUir3lorcTO1xn0vt/B5qc2+HYUQxAzeIVYM OhWJYn2yl1Gzj6mSs/aCNwOJmvQJMnyVXKAGGaZrjNSUm7X6IixKThUP1ydQdxAF9ubl GZR5nf3BPzdBVJl1RRSOb4H69z6ef1kTr0tsvIUiWG+O0OvBp1t5EsVSyy7rDaB8C9HH 6qwzWST0lEpDg1NBxF416N6fPYhrhdr+EW93bxbCSwLDpcr1U7pa7AMDS9etJICfEeY8 RkptpmCQ2fU2SgDArZB3e1PnGL4O+tHlcpBsUev9HuawVN3aZq6oOsL6cQmDV7IG5dBn uQag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=YqWwbgN+; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q21-20020a170906771500b00992bbdebdbesi8876636ejm.785.2023.08.09.10.04.49; Wed, 09 Aug 2023 10:05:14 -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=@cirrus.com header.s=PODMain02222019 header.b=YqWwbgN+; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232755AbjHIPzE (ORCPT + 99 others); Wed, 9 Aug 2023 11:55:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231290AbjHIPy7 (ORCPT ); Wed, 9 Aug 2023 11:54:59 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7119E1FF9; Wed, 9 Aug 2023 08:54:58 -0700 (PDT) Received: from pps.filterd (m0077474.ppops.net [127.0.0.1]) by mx0b-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 379EvsGK002928; Wed, 9 Aug 2023 10:54:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=+CEzH+rL6x1SVzo0jVm7sIO4j6kpVtWp3r0+9eaMezs=; b= YqWwbgN+4r1+7vpX5GKG6vN0967F2QKalGYYhjsTZkcplF+5JsykTU4dEHM6Kc+s ZJGre2b9BSUr5EPSBFQSyJjglnf6KTe/CwXxid3WbxPJAoiLDt+k+6/BHA8Solla fzwb1xOfe6VS+LZ/Ziv1N1wCxGmZDbNLSk5Gr2iRfFj3qZwIlSRmD7yE8CJu7wHq 3X7xEzC/zryPU+C6C10bNailca0Q9ENZ7qMeiaj+tUNSczv1gb/1qQmRx7TUIUa8 5Lmiv4p4WacFESbqgJdB1vvAowGEoeRaMGpMdURvi2AKcuWC5RNTIxC4qP3TBQRF 628ZLl3ImMVeIuWxDRhKuw== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhuvg7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 09 Aug 2023 10:54:47 -0500 (CDT) Received: from ediex01.ad.cirrus.com (198.61.84.80) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 9 Aug 2023 16:54:46 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Wed, 9 Aug 2023 16:54:46 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 9D12E3578; Wed, 9 Aug 2023 15:54:45 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v3 1/7] kunit: Replace fixed-size log with dynamically-extending buffer Date: Wed, 9 Aug 2023 16:54:32 +0100 Message-ID: <20230809155438.22470-2-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com> References: <20230809155438.22470-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: Nf0KBYJ2lHGpFOy5JOMIOqxElt1ZMGuA X-Proofpoint-ORIG-GUID: Nf0KBYJ2lHGpFOy5JOMIOqxElt1ZMGuA X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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: INBOX X-GMAIL-THRID: 1773771911330634819 X-GMAIL-MSGID: 1773771911330634819 Re-implement the log buffer as a list of buffer fragments that can be extended as the size of the log info grows. When using parameterization the test case can run many times and create a large amount of log. It's not really practical to keep increasing the size of the fixed buffer every time a test needs more space. And a big fixed buffer wastes memory. The original char *log pointer is replaced by a pointer to a list of struct kunit_log_frag, each containing a fixed-size buffer. kunit_log_append() now attempts to append to the last kunit_log_frag in the list. If there isn't enough space it will append a new kunit_log_frag to the list. This simple implementation does not attempt to completely fill the buffer in every kunit_log_frag. The 'log' member of kunit_suite, kunit_test_case and kunit_suite must be a pointer because the API of kunit_log() requires that is the same type in all three structs. As kunit.log is a pointer to the 'log' of the current kunit_case, it must be a pointer in the other two structs. The existing kunit-test.c log tests have been updated to build against the new fragmented log implementation. If the test fails the new function get_concatenated_log() constructs a single contiguous string from the log fragments so that the whole log can be emitted in the failure message. Signed-off-by: Richard Fitzgerald Reviewed-by: Rae Moar --- include/kunit/test.h | 25 +++++++++++----- lib/kunit/debugfs.c | 65 ++++++++++++++++++++++++++++++++++-------- lib/kunit/kunit-test.c | 48 +++++++++++++++++++++++++------ lib/kunit/test.c | 63 ++++++++++++++++++++++++++++------------ 4 files changed, 155 insertions(+), 46 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index d33114097d0d..cb5082efc91c 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -33,8 +33,8 @@ DECLARE_STATIC_KEY_FALSE(kunit_running); struct kunit; -/* Size of log associated with test. */ -#define KUNIT_LOG_SIZE 2048 +/* Size of log buffer fragments. */ +#define KUNIT_LOG_FRAGMENT_SIZE (256 - sizeof(struct list_head)) /* Maximum size of parameter description string. */ #define KUNIT_PARAM_DESC_SIZE 128 @@ -85,6 +85,17 @@ struct kunit_attributes { enum kunit_speed speed; }; +struct kunit_log_frag { + struct list_head list; + char buf[KUNIT_LOG_FRAGMENT_SIZE]; +}; + +static inline void kunit_init_log_frag(struct kunit_log_frag *frag) +{ + INIT_LIST_HEAD(&frag->list); + frag->buf[0] = '\0'; +} + /** * struct kunit_case - represents an individual test case. * @@ -132,7 +143,7 @@ struct kunit_case { /* private: internal use only. */ enum kunit_status status; char *module_name; - char *log; + struct list_head *log; }; static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) @@ -252,7 +263,7 @@ struct kunit_suite { /* private: internal use only */ char status_comment[KUNIT_STATUS_COMMENT_SIZE]; struct dentry *debugfs; - char *log; + struct list_head *log; int suite_init_err; }; @@ -278,7 +289,7 @@ struct kunit { /* private: internal use only. */ const char *name; /* Read only after initialization! */ - char *log; /* Points at case log after initialization */ + struct list_head *log; /* Points at case log after initialization */ struct kunit_try_catch try_catch; /* param_value is the current parameter value for a test case. */ const void *param_value; @@ -314,7 +325,7 @@ const char *kunit_filter_glob(void); char *kunit_filter(void); char *kunit_filter_action(void); -void kunit_init_test(struct kunit *test, const char *name, char *log); +void kunit_init_test(struct kunit *test, const char *name, struct list_head *log); int kunit_run_tests(struct kunit_suite *suite); @@ -472,7 +483,7 @@ static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp void kunit_cleanup(struct kunit *test); -void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); +void __printf(2, 3) kunit_log_append(struct list_head *log, const char *fmt, ...); /** * kunit_mark_skipped() - Marks @test_or_suite as skipped diff --git a/lib/kunit/debugfs.c b/lib/kunit/debugfs.c index 22c5c496a68f..a26b6d31bd2f 100644 --- a/lib/kunit/debugfs.c +++ b/lib/kunit/debugfs.c @@ -5,6 +5,7 @@ */ #include +#include #include #include @@ -37,14 +38,15 @@ void kunit_debugfs_init(void) debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL); } -static void debugfs_print_result(struct seq_file *seq, - struct kunit_suite *suite, - struct kunit_case *test_case) +static void debugfs_print_log(struct seq_file *seq, const struct list_head *log) { - if (!test_case || !test_case->log) + struct kunit_log_frag *frag; + + if (!log) return; - seq_printf(seq, "%s", test_case->log); + list_for_each_entry(frag, log, list) + seq_puts(seq, frag->buf); } /* @@ -69,10 +71,9 @@ static int debugfs_print_results(struct seq_file *seq, void *v) seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite)); kunit_suite_for_each_test_case(suite, test_case) - debugfs_print_result(seq, suite, test_case); + debugfs_print_log(seq, test_case->log); - if (suite->log) - seq_printf(seq, "%s", suite->log); + debugfs_print_log(seq, suite->log); seq_printf(seq, "%s %d %s\n", kunit_status_to_ok_not_ok(success), 1, suite->name); @@ -100,14 +101,53 @@ static const struct file_operations debugfs_results_fops = { .release = debugfs_release, }; +static struct list_head *kunit_debugfs_alloc_log(void) +{ + struct list_head *log; + struct kunit_log_frag *frag; + + log = kzalloc(sizeof(*log), GFP_KERNEL); + if (!log) + return NULL; + + INIT_LIST_HEAD(log); + + frag = kmalloc(sizeof(*frag), GFP_KERNEL); + if (!frag) { + kfree(log); + return NULL; + } + + kunit_init_log_frag(frag); + list_add_tail(&frag->list, log); + + return log; +} + +static void kunit_debugfs_free_log(struct list_head *log) +{ + struct kunit_log_frag *frag, *n; + + if (!log) + return; + + list_for_each_entry_safe(frag, n, log, list) { + list_del(&frag->list); + kfree(frag); + } + + kfree(log); +} + void kunit_debugfs_create_suite(struct kunit_suite *suite) { struct kunit_case *test_case; /* Allocate logs before creating debugfs representation. */ - suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL); + suite->log = kunit_debugfs_alloc_log(); + kunit_suite_for_each_test_case(suite, test_case) - test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL); + test_case->log = kunit_debugfs_alloc_log(); suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir); @@ -121,7 +161,8 @@ void kunit_debugfs_destroy_suite(struct kunit_suite *suite) struct kunit_case *test_case; debugfs_remove_recursive(suite->debugfs); - kfree(suite->log); + kunit_debugfs_free_log(suite->log); + kunit_suite_for_each_test_case(suite, test_case) - kfree(test_case->log); + kunit_debugfs_free_log(test_case->log); } diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 83d8e90ca7a2..a199f83bac67 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -530,12 +530,37 @@ static struct kunit_suite kunit_resource_test_suite = { .test_cases = kunit_resource_test_cases, }; +static char *get_concatenated_log(struct kunit *test, const struct list_head *log) +{ + struct kunit_log_frag *frag; + size_t len = 0; + char *p; + + list_for_each_entry(frag, log, list) + len += strlen(frag->buf); + + len++; /* for terminating '\0' */ + p = kunit_kzalloc(test, len, GFP_KERNEL); + + list_for_each_entry(frag, log, list) + strlcat(p, frag->buf, len); + + return p; +} + static void kunit_log_test(struct kunit *test) { struct kunit_suite suite; + struct kunit_log_frag *frag; - suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL); + suite.log = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); + INIT_LIST_HEAD(suite.log); + frag = kunit_kmalloc(test, sizeof(*frag), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); + kunit_init_log_frag(frag); + KUNIT_EXPECT_EQ(test, frag->buf[0], '\0'); + list_add_tail(&frag->list, suite.log); kunit_log(KERN_INFO, test, "put this in log."); kunit_log(KERN_INFO, test, "this too."); @@ -543,14 +568,17 @@ static void kunit_log_test(struct kunit *test) kunit_log(KERN_INFO, &suite, "along with this."); #ifdef CONFIG_KUNIT_DEBUGFS + frag = list_first_entry(test->log, struct kunit_log_frag, list); KUNIT_EXPECT_NOT_ERR_OR_NULL(test, - strstr(test->log, "put this in log.")); + strstr(frag->buf, "put this in log.")); KUNIT_EXPECT_NOT_ERR_OR_NULL(test, - strstr(test->log, "this too.")); + strstr(frag->buf, "this too.")); + + frag = list_first_entry(suite.log, struct kunit_log_frag, list); KUNIT_EXPECT_NOT_ERR_OR_NULL(test, - strstr(suite.log, "add to suite log.")); + strstr(frag->buf, "add to suite log.")); KUNIT_EXPECT_NOT_ERR_OR_NULL(test, - strstr(suite.log, "along with this.")); + strstr(frag->buf, "along with this.")); #else KUNIT_EXPECT_NULL(test, test->log); #endif @@ -558,11 +586,15 @@ static void kunit_log_test(struct kunit *test) static void kunit_log_newline_test(struct kunit *test) { + struct kunit_log_frag *frag; + kunit_info(test, "Add newline\n"); if (test->log) { - KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"), - "Missing log line, full log:\n%s", test->log); - KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n")); + frag = list_first_entry(test->log, struct kunit_log_frag, list); + KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(frag->buf, "Add newline\n"), + "Missing log line, full log:\n%s", + get_concatenated_log(test, test->log)); + KUNIT_EXPECT_NULL(test, strstr(frag->buf, "Add newline\n\n")); } else { kunit_skip(test, "only useful when debugfs is enabled"); } diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 49698a168437..dfe51bc2b387 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -114,46 +115,66 @@ static void kunit_print_test_stats(struct kunit *test, * already present. * @log: The log to add the newline to. */ -static void kunit_log_newline(char *log) +static void kunit_log_newline(struct kunit_log_frag *frag) { int log_len, len_left; - log_len = strlen(log); - len_left = KUNIT_LOG_SIZE - log_len - 1; + log_len = strlen(frag->buf); + len_left = sizeof(frag->buf) - log_len - 1; - if (log_len > 0 && log[log_len - 1] != '\n') - strncat(log, "\n", len_left); + if (log_len > 0 && frag->buf[log_len - 1] != '\n') + strncat(frag->buf, "\n", len_left); } -/* - * Append formatted message to log, size of which is limited to - * KUNIT_LOG_SIZE bytes (including null terminating byte). - */ -void kunit_log_append(char *log, const char *fmt, ...) +static struct kunit_log_frag *kunit_log_extend(struct list_head *log) +{ + struct kunit_log_frag *frag; + + frag = kmalloc(sizeof(*frag), GFP_KERNEL); + if (!frag) + return NULL; + + kunit_init_log_frag(frag); + list_add_tail(&frag->list, log); + + return frag; +} + +/* Append formatted message to log, extending the log buffer if necessary. */ +void kunit_log_append(struct list_head *log, const char *fmt, ...) { va_list args; + struct kunit_log_frag *frag; int len, log_len, len_left; if (!log) return; - log_len = strlen(log); - len_left = KUNIT_LOG_SIZE - log_len - 1; - if (len_left <= 0) - return; + frag = list_last_entry(log, struct kunit_log_frag, list); + log_len = strlen(frag->buf); + len_left = sizeof(frag->buf) - log_len - 1; /* Evaluate length of line to add to log */ va_start(args, fmt); len = vsnprintf(NULL, 0, fmt, args) + 1; va_end(args); + if (len > len_left) { + frag = kunit_log_extend(log); + if (!frag) + return; + + len_left = sizeof(frag->buf) - 1; + log_len = 0; + } + /* Print formatted line to the log */ va_start(args, fmt); - vsnprintf(log + log_len, min(len, len_left), fmt, args); + vsnprintf(frag->buf + log_len, min(len, len_left), fmt, args); va_end(args); /* Add newline to end of log if not already present. */ - kunit_log_newline(log); + kunit_log_newline(frag); } EXPORT_SYMBOL_GPL(kunit_log_append); @@ -359,14 +380,18 @@ void __kunit_do_failed_assertion(struct kunit *test, } EXPORT_SYMBOL_GPL(__kunit_do_failed_assertion); -void kunit_init_test(struct kunit *test, const char *name, char *log) +void kunit_init_test(struct kunit *test, const char *name, struct list_head *log) { spin_lock_init(&test->lock); INIT_LIST_HEAD(&test->resources); test->name = name; test->log = log; - if (test->log) - test->log[0] = '\0'; + if (test->log) { + struct kunit_log_frag *frag = list_first_entry(test->log, + struct kunit_log_frag, + list); + frag->buf[0] = '\0'; + } test->status = KUNIT_SUCCESS; test->status_comment[0] = '\0'; } From patchwork Wed Aug 9 15:54:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 133337 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2934816vqr; Wed, 9 Aug 2023 09:56:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6l5OI9mdg1NUTrfVuIM+A4v8VrudpH1Bxdk3kqA1yCZEuMyYI6zN6HBEHtP+SXwch0rIV X-Received: by 2002:a17:906:8a6e:b0:994:4095:3abf with SMTP id hy14-20020a1709068a6e00b0099440953abfmr2489384ejc.14.1691600185651; Wed, 09 Aug 2023 09:56:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691600185; cv=none; d=google.com; s=arc-20160816; b=IWKddaJnr4JyofBTTiv+PAKPzBlYhDlMttOdqHAYdty53mx8zltdxoJP3CQLP2qDSx vspIdM9yp21ic/75wQJNYetWzlVxAToGRB8oxmmylZP0udaNdlFylt5U6PYb3iI5OH6+ smNJOxKmU0QkVd97NseNvBjalS+ZSkU7ZpOCfPMpz2ZJd9h6FQi682uBvIT96RGzGdqh jlFFCD5vch5HrVZ3VhR+P+nxnEIQw3MNKi3+q/mX0fkXQFPQsoo4EoDp2R/hTlVU8w1T 0XHW/oVndwVZq60e2xsUbn3hTMHfIfcKrfzrv9FS7WMe9w8J3/tyHH4E7n7U213p3Y7+ fl8w== 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=c0+NukA+EoHsX21OuWFjmky1fG8j/3yMLBFVBs2CY6w=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=dYSjAoMWOEgYCjdNqr9MYBQAup8N7admJQBjX4M20jQBogFaEVb94Uw/CTxfeK/xdR N6p+4OM859MX5EFmxmzvsH8SeXUxCA+DiN82xw5WgAw/RzryzNdsVcJpbjp7R+g727fC pks5hnLK1xX8otA+aN0zpeHUxkZ5NtBRKSbelLzGKNqP8LRbRiKAFFQrXbpS2SZiBJxa uYhXAErkYUqhQFS+M7Yd/S/Z4AMqBibZyu+D0HosQ1veJoIzKYuAXK68if5vTIGOKz9r +fpJNx72gBWDMmzYNJkOfHy/Qo0F2YzNCftJ3jaNDeKgq6zaMz/v063ETDg2fJkbItxI QAng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=Q6AhjrDu; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j10-20020a170906094a00b0099bcae257e3si9056492ejd.832.2023.08.09.09.56.00; Wed, 09 Aug 2023 09:56:25 -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=@cirrus.com header.s=PODMain02222019 header.b=Q6AhjrDu; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234486AbjHIPzH (ORCPT + 99 others); Wed, 9 Aug 2023 11:55:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233721AbjHIPzA (ORCPT ); Wed, 9 Aug 2023 11:55:00 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 985B21729; Wed, 9 Aug 2023 08:54:59 -0700 (PDT) Received: from pps.filterd (m0077474.ppops.net [127.0.0.1]) by mx0b-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 379EvsGL002928; Wed, 9 Aug 2023 10:54:49 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=c0+NukA+EoHsX21OuWFjmky1fG8j/3yMLBFVBs2CY6w=; b= Q6AhjrDu2RhqQq2gB1DNxz86p6FTnuVuStz1XHuVbFLBTevyr3kJBcXSKYuN1APe m+v+QkBxmGeyQT8NfdWLvBgpo0RJ81ilp26WFahZQhi5n7XURTmZ6m75dr7ngaNn Drz2MG3KuexPhnYqMNjEulFsbWf7Bb/Rl1Jgt39iyWnLYTO+S/+YbruXYvRnPraN qrTcEx6BmFa0WxWVEFiefGVoW34haiJXl0AatyakI0eM4ae5qcF/PEBaiwInqTDs q0PligxtP9CftIsqLA/j18Rq7PQXhpMfWm+kWRRbIQSkvc4peSla2RvUkaRAoxqJ 5Z1jHi/eHdNHWeY4CPzBGg== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhuvg7-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 09 Aug 2023 10:54:48 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 9 Aug 2023 16:54:46 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Wed, 9 Aug 2023 16:54:46 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 1CB2945D; Wed, 9 Aug 2023 15:54:46 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v3 2/7] kunit: kunit-test: Add test cases for extending log buffer Date: Wed, 9 Aug 2023 16:54:33 +0100 Message-ID: <20230809155438.22470-3-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com> References: <20230809155438.22470-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: ctyYVCGN5YvT4vffDpuyZ-Y5sQ2Z8oMC X-Proofpoint-ORIG-GUID: ctyYVCGN5YvT4vffDpuyZ-Y5sQ2Z8oMC X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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: INBOX X-GMAIL-THRID: 1773771356408089244 X-GMAIL-MSGID: 1773771356408089244 Add test cases for the dynamically-extending log buffer. kunit_log_init_frag_test() tests that kunit_init_log_frag() correctly initializes new struct kunit_log_frag. kunit_log_extend_test_1() logs a series of numbered lines then tests that the resulting log contains all the lines. kunit_log_extend_test_2() logs a large number of lines of varying length to create many fragments, then tests that all lines are present. kunit_log_newline_test() has a new test to append a line that is exactly the length of the available space in the current fragment and check that the resulting log has a trailing '\n'. Signed-off-by: Richard Fitzgerald --- lib/kunit/kunit-test.c | 182 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 174 insertions(+), 8 deletions(-) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index a199f83bac67..c0ee33a8031e 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -7,6 +7,7 @@ */ #include #include +#include #include "try-catch-impl.h" @@ -530,10 +531,12 @@ static struct kunit_suite kunit_resource_test_suite = { .test_cases = kunit_resource_test_cases, }; -static char *get_concatenated_log(struct kunit *test, const struct list_head *log) +static char *get_concatenated_log(struct kunit *test, const struct list_head *log, + int *num_frags) { struct kunit_log_frag *frag; size_t len = 0; + int frag_count = 0; char *p; list_for_each_entry(frag, log, list) @@ -542,24 +545,42 @@ static char *get_concatenated_log(struct kunit *test, const struct list_head *lo len++; /* for terminating '\0' */ p = kunit_kzalloc(test, len, GFP_KERNEL); - list_for_each_entry(frag, log, list) + list_for_each_entry(frag, log, list) { strlcat(p, frag->buf, len); + ++frag_count; + } + + if (num_frags) + *num_frags = frag_count; return p; } -static void kunit_log_test(struct kunit *test) +static void kunit_log_init_frag_test(struct kunit *test) { - struct kunit_suite suite; struct kunit_log_frag *frag; - suite.log = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); - INIT_LIST_HEAD(suite.log); frag = kunit_kmalloc(test, sizeof(*frag), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); + memset(frag, 0x5a, sizeof(*frag)); + kunit_init_log_frag(frag); KUNIT_EXPECT_EQ(test, frag->buf[0], '\0'); + KUNIT_EXPECT_TRUE(test, list_is_first(&frag->list, &frag->list)); + KUNIT_EXPECT_TRUE(test, list_is_last(&frag->list, &frag->list)); +} + +static void kunit_log_test(struct kunit *test) +{ + struct kunit_suite suite; + struct kunit_log_frag *frag; + + suite.log = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); + INIT_LIST_HEAD(suite.log); + frag = kunit_kzalloc(test, sizeof(*frag), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); + kunit_init_log_frag(frag); list_add_tail(&frag->list, suite.log); kunit_log(KERN_INFO, test, "put this in log."); @@ -586,23 +607,168 @@ static void kunit_log_test(struct kunit *test) static void kunit_log_newline_test(struct kunit *test) { + struct kunit_suite suite; struct kunit_log_frag *frag; + char *p; kunit_info(test, "Add newline\n"); if (test->log) { frag = list_first_entry(test->log, struct kunit_log_frag, list); KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(frag->buf, "Add newline\n"), "Missing log line, full log:\n%s", - get_concatenated_log(test, test->log)); + get_concatenated_log(test, test->log, NULL)); KUNIT_EXPECT_NULL(test, strstr(frag->buf, "Add newline\n\n")); + + suite.log = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); + INIT_LIST_HEAD(suite.log); + frag = kunit_kzalloc(test, sizeof(*frag), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); + kunit_init_log_frag(frag); + list_add_tail(&frag->list, suite.log); + + /* String that exactly fills fragment leaving no room for \n */ + memset(frag->buf, 0, sizeof(frag->buf)); + memset(frag->buf, 'x', sizeof(frag->buf) - 9); + kunit_log_append(suite.log, "12345678"); + p = get_concatenated_log(test, suite.log, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); + KUNIT_EXPECT_NOT_NULL_MSG(test, strstr(p, "x12345678\n"), + "Newline not appended when fragment is full. Log is:\n'%s'", p); } else { kunit_skip(test, "only useful when debugfs is enabled"); } } +static void kunit_log_extend_test_1(struct kunit *test) +{ +#ifdef CONFIG_KUNIT_DEBUGFS + struct kunit_suite suite; + struct kunit_log_frag *frag; + char line[60]; + char *p, *pn; + size_t len, n; + int num_lines, num_frags, i; + + suite.log = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); + INIT_LIST_HEAD(suite.log); + frag = kunit_kzalloc(test, sizeof(*frag), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); + kunit_init_log_frag(frag); + list_add_tail(&frag->list, suite.log); + + i = 0; + len = 0; + do { + n = snprintf(line, sizeof(line), + "The quick brown fox jumps over the lazy penguin %d\n", i); + KUNIT_ASSERT_LT(test, n, sizeof(line)); + kunit_log_append(suite.log, line); + ++i; + len += n; + } while (len < (sizeof(frag->buf) * 30)); + num_lines = i; + + p = get_concatenated_log(test, suite.log, &num_frags); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); + KUNIT_EXPECT_GT(test, num_frags, 1); + + kunit_info(test, "num lines:%d num_frags:%d total len:%zu\n", + num_lines, num_frags, strlen(p)); + + i = 0; + while ((pn = strchr(p, '\n')) != NULL) { + *pn = '\0'; + snprintf(line, sizeof(line), + "The quick brown fox jumps over the lazy penguin %d", i); + KUNIT_EXPECT_STREQ(test, p, line); + p = pn + 1; + ++i; + } + KUNIT_EXPECT_EQ(test, i, num_lines); +#else + kunit_skip(test, "only useful when debugfs is enabled"); +#endif +} + +static void kunit_log_extend_test_2(struct kunit *test) +{ +#ifdef CONFIG_KUNIT_DEBUGFS + struct kunit_suite suite; + struct kunit_log_frag *frag; + struct rnd_state rnd; + char line[101]; + char *p, *pn; + size_t len; + int num_lines, num_frags, n, i; + + suite.log = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); + INIT_LIST_HEAD(suite.log); + frag = kunit_kzalloc(test, sizeof(*frag), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); + kunit_init_log_frag(frag); + list_add_tail(&frag->list, suite.log); + + /* Build log line of varying content */ + line[0] = '\0'; + i = 0; + do { + char tmp[9]; + + snprintf(tmp, sizeof(tmp), "%x", i++); + len = strlcat(line, tmp, sizeof(line)); + } while (len < sizeof(line) - 1); + + /* + * Log lines of different lengths until we have created + * many fragments. + * The "randomness" must be repeatable. + */ + prandom_seed_state(&rnd, 3141592653589793238ULL); + i = 0; + len = 0; + num_lines = 0; + do { + kunit_log_append(suite.log, "%s\n", &line[i]); + len += sizeof(line) - i; + num_lines++; + i = prandom_u32_state(&rnd) % (sizeof(line) - 1); + } while (len < (sizeof(frag->buf) * 30)); + + /* There must be more than one buffer fragment now */ + KUNIT_EXPECT_FALSE(test, list_is_singular(suite.log)); + + p = get_concatenated_log(test, suite.log, &num_frags); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); + KUNIT_EXPECT_GT(test, num_frags, 1); + + kunit_info(test, "num lines:%d num_frags:%d total len:%zu\n", + num_lines, num_frags, strlen(p)); + + prandom_seed_state(&rnd, 3141592653589793238ULL); + i = 0; + n = 0; + while ((pn = strchr(p, '\n')) != NULL) { + *pn = '\0'; + KUNIT_EXPECT_STREQ(test, p, &line[i]); + p = pn + 1; + n++; + i = prandom_u32_state(&rnd) % (sizeof(line) - 1); + } + KUNIT_EXPECT_EQ_MSG(test, n, num_lines, "Not enough lines."); +#else + kunit_skip(test, "only useful when debugfs is enabled"); +#endif +} + static struct kunit_case kunit_log_test_cases[] = { + KUNIT_CASE(kunit_log_init_frag_test), KUNIT_CASE(kunit_log_test), KUNIT_CASE(kunit_log_newline_test), + KUNIT_CASE(kunit_log_extend_test_1), + KUNIT_CASE(kunit_log_extend_test_2), {} }; From patchwork Wed Aug 9 15:54:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 133341 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2940091vqr; Wed, 9 Aug 2023 10:02:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGEXvSXzYAhBIOW3c3O2/dMjtfJmGxBOMT9JWdhv+rIPUfYJeh7rtkW9F0WNEbDjKj8KYvx X-Received: by 2002:a2e:7d09:0:b0:2b6:c0c5:1085 with SMTP id y9-20020a2e7d09000000b002b6c0c51085mr2224437ljc.19.1691600573886; Wed, 09 Aug 2023 10:02:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691600573; cv=none; d=google.com; s=arc-20160816; b=0aZB2VODFHQFDGKCZMi99zvYz414rUbnZ5NHMGZp4HTZVL+04mlayKmM7iBJR8WB0v n5QGU2Hev4m9LqWtDFLAwea2M4c3tXi1Ue+0aDHwMnxO5hc4invV023LVUQ8FP9rkTtv Qi3X8uI9YCrdLak2zUrKmpmpopIo/z9gkar6oI9z9Rp+tFlhgu+zbIOZ8sWfl2aewtlk X0NiTIolxvR5z7SCnIaD6y4ul2nd4ThPolaCtac226hg5iiwe95RfkL93eaJcClJFYrv UheYQAg9/cUqT4G/GrzGarJgXnRLEysldCW1v9opnNyCAhoxKnbFeTVv15N3y21Bz9I2 +Zew== 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=DDCtGnfmnKq7ILJAU5g3N9JLIdMJV17LdpZOS4jquIw=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=c9LmAGELOeJb7lU2DcZ/Vl66KxAFy9cG9nOSUdrwjopFCjWFBkGQ7oCQznZ9tHjZPr WYZ4QbQv5FZJhckL+FV9OUlLO6j5EvKDNJhUg0q25DDuAi+yjEJURj+j/C27U4YrDln+ gBGQdQK5IyRjGWMiA66HoUNdalLWsfbsQ7wbttl6JYhAccLUSb3PCYwz14T2kga2wJP4 KwSBmsb0b7SshY+p4MoVClCRPsRAkmzwPjVQkbOCpr6cTkOAOQLjUSNnM+C7WaZg3ct4 TEJeC5HelH09OvB3HMxixRx8NDJN4yNdURV3/NaVj0gmOMIy605TK5coKwO7Xb6pzl5c llvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=B03avQJT; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l9-20020a17090612c900b0098dfdc3f2d9si9440957ejb.342.2023.08.09.10.02.10; Wed, 09 Aug 2023 10:02:53 -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=@cirrus.com header.s=PODMain02222019 header.b=B03avQJT; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234009AbjHIPzC (ORCPT + 99 others); Wed, 9 Aug 2023 11:55:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbjHIPy7 (ORCPT ); Wed, 9 Aug 2023 11:54:59 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC37C1FF7; Wed, 9 Aug 2023 08:54:58 -0700 (PDT) Received: from pps.filterd (m0077474.ppops.net [127.0.0.1]) by mx0b-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 379EvsGM002928; Wed, 9 Aug 2023 10:54:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=DDCtGnfmnKq7ILJAU5g3N9JLIdMJV17LdpZOS4jquIw=; b= B03avQJTuJfS7ZeT+c2bndSNYO2fKXdJ8OhjBgY1Jo49oKvVZXBqRRm+xgsVAySI OffwD8LZrDMo51pJpOq8Apv3zCIbDZogaFXVtpmOhSC1QSg/XMaMKCo206t4cDYk wm4UgwYUPu9G3t9KPLgtt3igK5pjquD+IV63vuxDd+J34H5GeS7W/nm5+JA51IOk MsDSHhRzzZIiRHFaGw4gDzH4N0QXojzZ9Yd7wQNNKI5kkYamX70GgGvzdGlRmyGF iq0HfVgID0XCl13N2fj5s0D935i3jPwbA6V19dZm5uAzC047RHUeyBQxm8oOd1/I mTSjJE+jL2ioVkcUQQapow== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhuvg7-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 09 Aug 2023 10:54:49 -0500 (CDT) Received: from ediex01.ad.cirrus.com (198.61.84.80) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 9 Aug 2023 16:54:47 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Wed, 9 Aug 2023 16:54:47 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 9C5DA475; Wed, 9 Aug 2023 15:54:46 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v3 3/7] kunit: Handle logging of lines longer than the fragment buffer size Date: Wed, 9 Aug 2023 16:54:34 +0100 Message-ID: <20230809155438.22470-4-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com> References: <20230809155438.22470-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: x29uRc4_XgSSpCXH0mU_RayOJCfp_0a6 X-Proofpoint-ORIG-GUID: x29uRc4_XgSSpCXH0mU_RayOJCfp_0a6 X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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: INBOX X-GMAIL-THRID: 1773771763307125711 X-GMAIL-MSGID: 1773771763307125711 Add handling to kunit_log_append() for log messages that are longer than a single buffer fragment. The initial implementation of fragmented buffers did not change the logic of the original kunit_log_append(). A consequence was that it still had the original assumption that a log line will fit into one buffer. This patch checks for log messages that are larger than one fragment buffer. In that case, kvasprintf() is used to format it into a temporary buffer and that content is then split across as many fragments as necessary. Signed-off-by: Richard Fitzgerald Reviewed-by: David Gow --- lib/kunit/test.c | 65 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index dfe51bc2b387..28d0048d6171 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -140,25 +140,82 @@ static struct kunit_log_frag *kunit_log_extend(struct list_head *log) return frag; } +static void kunit_log_append_string(struct list_head *log, const char *src) +{ + struct kunit_log_frag *frag, *new_frag; + int log_len, bytes_left; + ssize_t written; + char *p; + + frag = list_last_entry(log, struct kunit_log_frag, list); + log_len = strlen(frag->buf); + bytes_left = sizeof(frag->buf) - log_len; + + written = strscpy(frag->buf + log_len, src, bytes_left); + if (written != -E2BIG) + goto newline; + + src += bytes_left - 1; + do { + new_frag = kunit_log_extend(log); + if (!new_frag) + goto newline; + + frag = new_frag; + written = strscpy(frag->buf, src, sizeof(frag->buf)); + src += sizeof(frag->buf) - 1; + } while (written == -E2BIG); + +newline: + if (written == -E2BIG) + written = strlen(frag->buf); + + p = &frag->buf[written - 1]; + if (*p != '\n') { + if (strlcat(frag->buf, "\n", sizeof(frag->buf)) >= sizeof(frag->buf)) { + frag = kunit_log_extend(log); + if (!frag) { + *p = '\n'; + return; + } + + frag->buf[0] = '\n'; + frag->buf[1] = '\0'; + } + } +} + /* Append formatted message to log, extending the log buffer if necessary. */ void kunit_log_append(struct list_head *log, const char *fmt, ...) { va_list args; struct kunit_log_frag *frag; int len, log_len, len_left; + char *tmp = NULL; if (!log) return; - frag = list_last_entry(log, struct kunit_log_frag, list); - log_len = strlen(frag->buf); - len_left = sizeof(frag->buf) - log_len - 1; - /* Evaluate length of line to add to log */ va_start(args, fmt); len = vsnprintf(NULL, 0, fmt, args) + 1; va_end(args); + if (len > sizeof(frag->buf) - 1) { + va_start(args, fmt); + tmp = kvasprintf(GFP_KERNEL, fmt, args); + va_end(args); + + kunit_log_append_string(log, tmp); + kfree(tmp); + + return; + } + + frag = list_last_entry(log, struct kunit_log_frag, list); + log_len = strlen(frag->buf); + len_left = sizeof(frag->buf) - log_len - 1; + if (len > len_left) { frag = kunit_log_extend(log); if (!frag) From patchwork Wed Aug 9 15:54:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 133363 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2957962vqr; Wed, 9 Aug 2023 10:24:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHd+Hyd02pRgvg1rL/8gNo1XvP3MS0CBPbe7ORGsx+fMUUFUhRlpbawe/BwsHU9gZ9MgPPv X-Received: by 2002:aa7:88d2:0:b0:687:4fa4:7f2e with SMTP id k18-20020aa788d2000000b006874fa47f2emr3041897pff.13.1691601857090; Wed, 09 Aug 2023 10:24:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691601857; cv=none; d=google.com; s=arc-20160816; b=UvYryZSRIYeH1mrbcHX01VAIM5Lo82a+coYxY/gtOB+/3AcZTJ1tVV3m+kRh53p60M /YTrwGsTfIzQ0d+Krb/8ghiPe1igGenvGXnmKMjdSC9TD3jtN0s8YfbXoHdYEMF3EgB2 9OAgfrkdBWZq/1us5B8aU48YtUFQdx+pF5ZaXCS2ZJJOMgCEkk8njOG7/Vl879P1fAG2 9kYUhjMkCJ/nWVEa3wIsuKI4f+Vvnj/e9A6z0woU4xryOKS7JBgBMau8T4ZcDkNwCVcQ yw5nozEPE24blrfXC3Ndl2mDfFXR+TUPd479HaTHMdD8HiiPUa2rV/WUib6I5lTRhL0f cqAQ== 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=vsT2Du1dpYSZqCMgCeFpQVAdMU8X3uG+X2sBeOR9zkc=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=Mpa3TkrE0fCqwW9d4XLCDOr4HtjdPX1LUJ/V+bM43y3KvSh5nKJTH1KRMa6eMDtumd k6NFGIiBwqNkPzZBl6cv7s+rh/2yT6E/yzXgORKwlldIzHkUPLc5WGW3zbv3Pvn8cXyc as3jmt3hBtGqiDM/t8fnNnluLQh1rtdwlSmjgy4nSz0kq6w3WTrOvS6gkCv+ZNTqplh9 KRzKzIzn2f0or/sWj7jwLsQ4q15FXHJKxbp8rMlYMeZvzIReSh6DTTS2pAuxYs/umcsQ XgQkb3bNlzTtDX3GUIcEMs9RfUEyrUox/ZhsEfSNmlNr7x4bdCwAc1aup8Lh5rz6hen7 khJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=oYWXedbE; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b15-20020a63cf4f000000b0055bc97cc885si5967593pgj.614.2023.08.09.10.24.04; Wed, 09 Aug 2023 10:24:17 -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=@cirrus.com header.s=PODMain02222019 header.b=oYWXedbE; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234351AbjHIPzJ (ORCPT + 99 others); Wed, 9 Aug 2023 11:55:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233250AbjHIPzA (ORCPT ); Wed, 9 Aug 2023 11:55:00 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D97FF1FFA; Wed, 9 Aug 2023 08:54:59 -0700 (PDT) Received: from pps.filterd (m0077474.ppops.net [127.0.0.1]) by mx0b-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 379EvsGN002928; Wed, 9 Aug 2023 10:54:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=vsT2Du1dpYSZqCMgCeFpQVAdMU8X3uG+X2sBeOR9zkc=; b= oYWXedbE0utI51TIuu4NPDiDYX1xZC01SBr5ck50i1VzJpNdrKRx8TBHr342rhdi mjb8TVMxAKDU2hk7K1TzghEGkK47Idh5bOEBt4vWlBVTRNX27sorDdNn459G/8x+ V0ifLnP6+WauVj4ck6MM3gA/wHIEpwcMTyTMoyKu5wPHpffFGxqnyshGNX7PUalV 5qyVBPPNWpwwTCAQDwbiXvmUovv6TLEpQzxWicB00tbyXD+1BZvXejZ6hEoZXx6N 4/PoGYGnCjRDsQsMl2gMN859FA15kTgszwfUwz67lORZf/zPPOcDE+xDoPRaQOVF GrmAvNOodiUBNKCriY2owg== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhuvg7-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 09 Aug 2023 10:54:50 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 9 Aug 2023 16:54:47 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Wed, 9 Aug 2023 16:54:47 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 25903357C; Wed, 9 Aug 2023 15:54:47 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v3 4/7] kunit: kunit-test: Test logging a line that exactly fills a fragment Date: Wed, 9 Aug 2023 16:54:35 +0100 Message-ID: <20230809155438.22470-5-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com> References: <20230809155438.22470-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: ZDf1cKSOuqUQ1CRxQx4_V9XNri9AY5gA X-Proofpoint-ORIG-GUID: ZDf1cKSOuqUQ1CRxQx4_V9XNri9AY5gA X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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: INBOX X-GMAIL-THRID: 1773773109224796246 X-GMAIL-MSGID: 1773773109224796246 If a log string is the exact length of a log fragment buffer kunit_log_append() should now exactly fill that fragment without extending the log. Signed-off-by: Richard Fitzgerald --- lib/kunit/kunit-test.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index c0ee33a8031e..9ac81828d018 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -763,12 +763,49 @@ static void kunit_log_extend_test_2(struct kunit *test) #endif } +static void kunit_log_frag_sized_line_test(struct kunit *test) +{ +#ifdef CONFIG_KUNIT_DEBUGFS + struct kunit_suite suite; + struct kunit_log_frag *frag, *src; + + suite.log = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); + INIT_LIST_HEAD(suite.log); + frag = kunit_kzalloc(test, sizeof(*frag), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); + kunit_init_log_frag(frag); + list_add_tail(&frag->list, suite.log); + + src = kunit_kzalloc(test, sizeof(*src), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, src); + memset(src->buf, 'x', sizeof(src->buf) - 2); + KUNIT_ASSERT_EQ(test, strlen(src->buf), sizeof(src->buf) - 2); + + /* Log a string that exactly fills the fragment */ + kunit_log_append(suite.log, "%s\n", src->buf); + KUNIT_EXPECT_TRUE(test, list_is_singular(suite.log)); + KUNIT_EXPECT_EQ(test, strlen(frag->buf), sizeof(frag->buf) - 1); + strlcat(src->buf, "\n", sizeof(src->buf)); + KUNIT_EXPECT_STREQ(test, frag->buf, src->buf); + + /* Logging another string should extend the log */ + kunit_log_append(suite.log, "Next\n"); + KUNIT_EXPECT_EQ(test, list_count_nodes(suite.log), 2); + frag = list_last_entry(suite.log, struct kunit_log_frag, list); + KUNIT_EXPECT_STREQ(test, frag->buf, "Next\n"); +#else + kunit_skip(test, "only useful when debugfs is enabled"); +#endif +} + static struct kunit_case kunit_log_test_cases[] = { KUNIT_CASE(kunit_log_init_frag_test), KUNIT_CASE(kunit_log_test), KUNIT_CASE(kunit_log_newline_test), KUNIT_CASE(kunit_log_extend_test_1), KUNIT_CASE(kunit_log_extend_test_2), + KUNIT_CASE(kunit_log_frag_sized_line_test), {} }; From patchwork Wed Aug 9 15:54:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 133428 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp3017059vqr; Wed, 9 Aug 2023 12:12:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEHR7oDF3Yx13BkVWqInFMGZn/OJs/BWdLs1SE0ysOw/PleXqvAI6ss1Co1NCWi38+Ll2ii X-Received: by 2002:a05:6512:b91:b0:4fe:a5c:efa3 with SMTP id b17-20020a0565120b9100b004fe0a5cefa3mr5090lfv.62.1691608373343; Wed, 09 Aug 2023 12:12:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691608373; cv=none; d=google.com; s=arc-20160816; b=ojLPlEVKYGYhLTHbatlrdhuTe4pndHkv9J6yINPfaIkW0A5wgaLn0XQ8L0EsaHZ78S h/2ACgLNDJXMGkx+nfFfPEC07t75c4oRDDdkX80wJDeXkANeNqK8ms8C08WLHhuLvt7U CwJtUkBK2ZFxvtxJEeOuRDfH4z61aNQIN08UDiZjTCfujtGcBVkFy/C/j4M6R8s+YHgi BJNQW/vREoiRwPFqrK1qkly0Qn1gQIt72xcqGObVk7ntfwPCbLCXTLo3wUf8NhqZQhrR 5iSone/cwoVOuQz7sIQpUxI0Xzh00qHmcPsQJJV/H9JRr9nAVlcFzZPnq3slnR0v16vw xJuA== 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=6QLn6bSvhN4NK1Fgih/1NOqLGf5bUsI0DnIadC7OYbA=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=wtL0pjpltJHgktqGJVvhQbLSnuQ0PwmCnIX/DBACd5Drqm6sKqeq6ynQxuB3cs5xE4 OE3ualxWpme2/57Zewz8ag+9Q49mzWFFr9lcwWncnZZeqDyQmuRB6F+bD/qAJVsaWsbP rjjua+5uR3qhftXZK6A9FhHiBvbtj4JbRe5j7oMGI2mUep32G7W+GUR9ATPmLIH6wNZC Qd2wi+Lf/hfIcoFTcB8+xIimk9SL6gYhAuuVLgV9w8+/bOTtg97nWSEa+82Du74vO2nb FzLVwfRnsRly4y/LaGaURhAuMnLe6vqUXrj4B1T27I69jcwQ9OpYclrImtE1e2A1pnBW TvVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=o1nRpUkE; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d6-20020a170906c20600b00991e694a0a2si9609133ejz.240.2023.08.09.12.12.25; Wed, 09 Aug 2023 12:12:53 -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=@cirrus.com header.s=PODMain02222019 header.b=o1nRpUkE; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234484AbjHIPzL (ORCPT + 99 others); Wed, 9 Aug 2023 11:55:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233722AbjHIPzB (ORCPT ); Wed, 9 Aug 2023 11:55:01 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12FDE2100; Wed, 9 Aug 2023 08:55:01 -0700 (PDT) Received: from pps.filterd (m0077474.ppops.net [127.0.0.1]) by mx0b-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 379EvsGO002928; Wed, 9 Aug 2023 10:54:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=6QLn6bSvhN4NK1Fgih/1NOqLGf5bUsI0DnIadC7OYbA=; b= o1nRpUkE1OX3f2d2OGJR6JgnofdOuSMJz5TPNrvG2yeT8nNJ9OBKG0BfpycXT28Q Fh0oWZBmzcgRuw0GRtAWenxKxWHgLbMq2xWJfWhc9XjDKLdbe68wsleIaPuMq2Bt VUhxywsNzSb54RkVdwLT/SGKjiO+sIpzgq8/4MGZ8yB2hVrysRsBiA0+zvPKpjnH 4AJette8oaveJIWeBs1t4zKuc5AURcwOE8OwT53GUtX15LnQmNt5M0hvwVdY1ZC2 tjEvYllpOoNWzwcXsj6J85STtMI/OX6H1dYQrkyQlJVM23UbDxrMedUwL+u8J3Xw of3VOlKF1VdwrtLwayt+rQ== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhuvg7-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 09 Aug 2023 10:54:51 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 9 Aug 2023 16:54:47 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Wed, 9 Aug 2023 16:54:47 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 90B0D45D; Wed, 9 Aug 2023 15:54:47 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v3 5/7] kunit: kunit-test: Add test cases for logging very long lines Date: Wed, 9 Aug 2023 16:54:36 +0100 Message-ID: <20230809155438.22470-6-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com> References: <20230809155438.22470-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: 4qyh8ChHAm8i7968d0VHjHYw4fYRL-XX X-Proofpoint-ORIG-GUID: 4qyh8ChHAm8i7968d0VHjHYw4fYRL-XX X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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: INBOX X-GMAIL-THRID: 1773779941625892993 X-GMAIL-MSGID: 1773779941625892993 Add kunit_log_long_line_test() to test that logging a line longer than the buffer fragment size doesn't truncate the line. Add extra tests to kunit_log_newline_test() for lines longer than the buffer fragment size. Signed-off-by: Richard Fitzgerald Reviewed-by: Rae Moar --- lib/kunit/kunit-test.c | 84 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 9ac81828d018..c079550c3afd 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -609,7 +609,7 @@ static void kunit_log_newline_test(struct kunit *test) { struct kunit_suite suite; struct kunit_log_frag *frag; - char *p; + char *p, *line; kunit_info(test, "Add newline\n"); if (test->log) { @@ -635,6 +635,33 @@ static void kunit_log_newline_test(struct kunit *test) KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); KUNIT_EXPECT_NOT_NULL_MSG(test, strstr(p, "x12345678\n"), "Newline not appended when fragment is full. Log is:\n'%s'", p); + kunit_kfree(test, p); + + /* String that is much longer than a fragment */ + line = kunit_kzalloc(test, sizeof(frag->buf) * 6, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, line); + memset(line, 'x', (sizeof(frag->buf) * 6) - 1); + kunit_log_append(suite.log, "%s", line); + p = get_concatenated_log(test, suite.log, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); + KUNIT_EXPECT_EQ(test, p[strlen(p) - 1], '\n'); + KUNIT_EXPECT_NULL(test, strstr(p, "\n\n")); + kunit_kfree(test, p); + + kunit_log_append(suite.log, "%s\n", line); + p = get_concatenated_log(test, suite.log, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); + KUNIT_EXPECT_EQ(test, p[strlen(p) - 1], '\n'); + KUNIT_EXPECT_NULL(test, strstr(p, "\n\n")); + kunit_kfree(test, p); + + line[strlen(line) - 1] = '\n'; + kunit_log_append(suite.log, "%s", line); + p = get_concatenated_log(test, suite.log, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); + KUNIT_EXPECT_EQ(test, p[strlen(p) - 1], '\n'); + KUNIT_EXPECT_NULL(test, strstr(p, "\n\n")); + kunit_kfree(test, p); } else { kunit_skip(test, "only useful when debugfs is enabled"); } @@ -799,6 +826,60 @@ static void kunit_log_frag_sized_line_test(struct kunit *test) #endif } +static void kunit_log_long_line_test(struct kunit *test) +{ +#ifdef CONFIG_KUNIT_DEBUGFS + struct kunit_suite suite; + struct kunit_log_frag *frag; + struct rnd_state rnd; + char *line, *p, *pn; + size_t line_buf_size, len; + int num_frags, i; + + suite.log = kunit_kzalloc(test, sizeof(*suite.log), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); + INIT_LIST_HEAD(suite.log); + frag = kunit_kmalloc(test, sizeof(*frag), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, frag); + kunit_init_log_frag(frag); + KUNIT_EXPECT_EQ(test, frag->buf[0], '\0'); + list_add_tail(&frag->list, suite.log); + + /* Create a very long string to be logged */ + line_buf_size = sizeof(frag->buf) * 6; + line = kunit_kmalloc(test, line_buf_size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, line); + line[0] = '\0'; + + prandom_seed_state(&rnd, 3141592653589793238ULL); + len = 0; + do { + static const char fill[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + i = prandom_u32_state(&rnd) % (sizeof(fill) - 1); + len = strlcat(line, &fill[i], line_buf_size); + } while (len < line_buf_size); + + kunit_log_append(suite.log, "%s\n", line); + + p = get_concatenated_log(test, suite.log, &num_frags); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); + KUNIT_EXPECT_GT(test, num_frags, 1); + + kunit_info(test, "num_frags:%d total len:%zu\n", num_frags, strlen(p)); + + /* Don't compare the trailing '\n' */ + pn = strrchr(p, '\n'); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, pn); + *pn = '\0'; + KUNIT_EXPECT_EQ(test, strlen(p), strlen(line)); + KUNIT_EXPECT_STREQ(test, p, line); +#else + kunit_skip(test, "only useful when debugfs is enabled"); +#endif +} + static struct kunit_case kunit_log_test_cases[] = { KUNIT_CASE(kunit_log_init_frag_test), KUNIT_CASE(kunit_log_test), @@ -806,6 +887,7 @@ static struct kunit_case kunit_log_test_cases[] = { KUNIT_CASE(kunit_log_extend_test_1), KUNIT_CASE(kunit_log_extend_test_2), KUNIT_CASE(kunit_log_frag_sized_line_test), + KUNIT_CASE(kunit_log_long_line_test), {} }; From patchwork Wed Aug 9 15:54:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 133353 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2956925vqr; Wed, 9 Aug 2023 10:22:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGu42tDZ6W207J+I2QY0pEwmQhvZ9J3xsM47bp0pwnL18pX6tDLPIHfwqm+J1krssLL2sU4 X-Received: by 2002:aa7:88ca:0:b0:66f:fa65:483 with SMTP id k10-20020aa788ca000000b0066ffa650483mr3432126pff.0.1691601765946; Wed, 09 Aug 2023 10:22:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691601765; cv=none; d=google.com; s=arc-20160816; b=R0vM77h5DlImBT9IM3xwF0Do8rGqT7+hP23YVPlPv9ASxvO2GgBNyJ4sB3U3o/rd9+ qv8lxfaqslQX5TlbLGgvkcU6grXyAuC0U7141Ki3pOud6OcyB+9I7QfPcIvlXRh7R7C3 iEApaFYsHzCiCE6Vqzo7VNHk4rSdK6lRKhVy+xw0AKTrR1y8xnP+okPJ2i8Ms8IPDF0S JVFipKVv87t/8WOQtEcvjteKerDLMfFzgf2y2/aap0ATqL6ITiaADzDWFRbXvcSSeu/O XiCkAUHw/cNb4XfBCOpGDDQarneyxB8M7UrChe4hmKZDBK82y7gAd1KZ0lMPVeu2dgDj EznQ== 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=s7GI22R3yH/vG/yzGrHV/vPELeeEUsf/yheeCZUF8g0=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=DiZoQzf9KQsSkbHtMQPrESyQlGFDrtWd4tt5kxPWplW0odnGPw4mWBNGLme+Kzjy/0 M34TUQdNcAYtZBPZQYPGeDvwKjppiAPqvkPm7CpDjHfbAyvqwv0Y7IyDf+jH0HFAG9k/ g9FSnxSzOQTe3rLlPrk1O//PPp48ACfL5W6wlRWZNxrcdFvT0/yVLkXLmHQiFqt7dXWr 4mQbKSgmajXgLnWohk/QFkiLoSnNRWGCQ7CNolB8W5Npx0iRjrBS7g6z4pCu6Oi1vqhr gDFtmT99Ea7he3illr5wqhwUNT8ktLcQpwXtMDJfXik+FXfKJbajU1OkPoJmUsdIRm5M Yhqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=VdX++k4b; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a19-20020a056a001d1300b0068708093a99si9400876pfx.355.2023.08.09.10.22.30; Wed, 09 Aug 2023 10:22:45 -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=@cirrus.com header.s=PODMain02222019 header.b=VdX++k4b; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233722AbjHIPzP (ORCPT + 99 others); Wed, 9 Aug 2023 11:55:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233803AbjHIPzC (ORCPT ); Wed, 9 Aug 2023 11:55:02 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AF331FF7; Wed, 9 Aug 2023 08:55:02 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 379CrDUd002197; Wed, 9 Aug 2023 10:54:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=s7GI22R3yH/vG/yzGrHV/vPELeeEUsf/yheeCZUF8g0=; b= VdX++k4bPy2xRwMBr/eJrWj5Qjcocf31cRQrN3ESC/4IWY7EV2wpkjXBrABljzgu qNAejUibJFwzwZtpuKElw6Ugf/PfWwgNCjW5Jtbxsf4PPpwJ2W1FoRUMgvVxYGQ2 HeXHHY6ZhBr+Si98LEphpNaIzhPiV3gA6RJ8gcWCIbUvw7yXOnbiC/L4CxsZacvK GdmOFBja6GfEM8BBqTzNcmGUVmdhlfTGI9qxgd1i/fWXJvPIG7SaxvIguG5T0Qn0 CMFe1m4n6B5jcl42YH5+01gtEDozaKDDX2jFK/NrfsS8slKWV+/bt4VLxqsereCt Q9Ig8HUhG3CtoBcUar2Xag== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3sb7vtanrr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 09 Aug 2023 10:54:50 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 9 Aug 2023 16:54:48 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Wed, 9 Aug 2023 16:54:48 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 01D95475; Wed, 9 Aug 2023 15:54:47 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v3 6/7] kunit: kunit-test: Add test of logging only a newline Date: Wed, 9 Aug 2023 16:54:37 +0100 Message-ID: <20230809155438.22470-7-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com> References: <20230809155438.22470-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: YMGL8l3INJVl6RSBMdHNKl7pkBt4r9hm X-Proofpoint-ORIG-GUID: YMGL8l3INJVl6RSBMdHNKl7pkBt4r9hm X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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: INBOX X-GMAIL-THRID: 1773773013372853726 X-GMAIL-MSGID: 1773773013372853726 Add a test that logging a string containing only a newline appends one newline to the log. Signed-off-by: Richard Fitzgerald Reviewed-by: Rae Moar --- lib/kunit/kunit-test.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index c079550c3afd..7e710c73c7e5 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -627,6 +627,12 @@ static void kunit_log_newline_test(struct kunit *test) kunit_init_log_frag(frag); list_add_tail(&frag->list, suite.log); + /* Log only a newline */ + kunit_log_append(suite.log, "\n"); + KUNIT_EXPECT_TRUE(test, list_is_singular(suite.log)); + KUNIT_EXPECT_STREQ(test, frag->buf, "\n"); + frag->buf[0] = '\0'; + /* String that exactly fills fragment leaving no room for \n */ memset(frag->buf, 0, sizeof(frag->buf)); memset(frag->buf, 'x', sizeof(frag->buf) - 9); From patchwork Wed Aug 9 15:54:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 133346 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2943324vqr; Wed, 9 Aug 2023 10:06:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE/tWqdPLvl6gDKVizUUHaenXRLCMj+eSlW9og8M3bJr3Y5qz90s/YL7WnWy0aot2M0Vfkq X-Received: by 2002:a05:6512:ea8:b0:4fe:676:8bf2 with SMTP id bi40-20020a0565120ea800b004fe06768bf2mr2632221lfb.10.1691600770410; Wed, 09 Aug 2023 10:06:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691600770; cv=none; d=google.com; s=arc-20160816; b=j3kDqiULUY/mqyDWdGgXefplhb/XsUirJ3FKE1VVrUSEYaHhUjYJn80PEaTEG/pVbg Drp9q0aTnhk9Fb2XLWUwaA7iJKnnmqIqLHWqJR3JyPmVx4x8sY2tGRJcnfcQcSdYvAoe AAQPYnrg97i7oSDVrv/fFo++opBHZAcokJZdMeWBaMeeXKHdkAzvYvsnPmZiXhvThVi2 goXwYDJWRft96rsCvNCtVmcNtaDB/n2eIyJSTg+9Q/kThyxBhYaUFj0GpU93gChgRJvU sk/4nsS1aJDvAzHlEaMnrM/eh182jkIjOOHNeIm784EKKhplQKRZWBXvr5DhZA9Rdf4E F0kA== 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=EkcWozHmMlEsGAMvpqTB6x4BhQ+VCXW9AKlqr4C/ae0=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=iWUXRbSwuSDeX0dp68nDPDh42H8XB4OhLaRFvC3bGwje9jZcaA7s/03HbzCxZxM9tS 8r/rRABOZHrIqkG6HmnbUXq/eA5kpxeOPc6iB6mZGn5DB6WTqSDIkfT69ddykCMMkiPZ pnXhilDzVfmuZR5V9lBvwLDoKxdElEJBOg99iXPd4pEbDm3sWfFngB6utJ7kqZtWsNpY QoO68AoOyD3A9rbAUbpRdHhklC/REtSq0BVXnMX28+pB9qovBgKB+XVeLI0QDgGLtQ+s jKipo80g0JETM0Hyv3sCOmtJWPixaXb0tIzXdvzohW+ZTj1jujSLDpqmniIa3e4W9C0L J7eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=QUwF7ePH; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e21-20020a170906249500b00992fef51a60si8993543ejb.525.2023.08.09.10.05.30; Wed, 09 Aug 2023 10:06:10 -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=@cirrus.com header.s=PODMain02222019 header.b=QUwF7ePH; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234541AbjHIPzS (ORCPT + 99 others); Wed, 9 Aug 2023 11:55:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234244AbjHIPzC (ORCPT ); Wed, 9 Aug 2023 11:55:02 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59C221FF9; Wed, 9 Aug 2023 08:55:02 -0700 (PDT) Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 379CrDUe002197; Wed, 9 Aug 2023 10:54:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= PODMain02222019; bh=EkcWozHmMlEsGAMvpqTB6x4BhQ+VCXW9AKlqr4C/ae0=; b= QUwF7ePHeVV7H0l2vQRS4JY92j3wj/W1O7Xe3tEdSzIIm15kxPQuwXGEs7tnCZan RozNdNAZFcuB3YvbSB2jb0ESmoEYM9xwmKFyXnEUzqCwhQbgpFda7JloNbH7cNlD TuuCqTbe3CSTZwN1yjlcvoEGAnvwkvktbnoV/Jr88QBuMq0/rnh9z+DluI8Q7df5 dQd8oXqUYLYxMdpy7yBXbYxOduinSDXacFiIv2pe9pbyHAzcP6/jeq8zlTQyRU1S T/xZQpg96BOqh6HzVvp6A1+3lgLn2m589oSxcjNHVlBuLjcR463Fjik0w1Etj/G8 t1n4YAvd6v+geH5f6haSzw== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3sb7vtanrr-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 09 Aug 2023 10:54:51 -0500 (CDT) Received: from ediex02.ad.cirrus.com (198.61.84.81) by ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Wed, 9 Aug 2023 16:54:48 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by anon-ediex02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server id 15.2.1118.30 via Frontend Transport; Wed, 9 Aug 2023 16:54:48 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 68A0245D; Wed, 9 Aug 2023 15:54:48 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v3 7/7] kunit: Don't waste first attempt to format string in kunit_log_append() Date: Wed, 9 Aug 2023 16:54:38 +0100 Message-ID: <20230809155438.22470-8-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230809155438.22470-1-rf@opensource.cirrus.com> References: <20230809155438.22470-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: YESuJiL3msWuAJAE0qtyL58LJfr7ndi- X-Proofpoint-ORIG-GUID: YESuJiL3msWuAJAE0qtyL58LJfr7ndi- X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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: INBOX X-GMAIL-THRID: 1773771969473459129 X-GMAIL-MSGID: 1773771969473459129 It's wasteful to call vsnprintf() only to figure out the length of the string. The string might fit in the available buffer space but we have to vsnprintf() again to do that. Instead, attempt to format the string to the available buffer at the same time as finding the string length. Only if the string didn't fit the available space is it necessary to extend the log and format the string again to a new fragment buffer. Signed-off-by: Richard Fitzgerald Reviewed-by: David Gow --- lib/kunit/test.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 28d0048d6171..230ec5e9824f 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -196,11 +196,21 @@ void kunit_log_append(struct list_head *log, const char *fmt, ...) if (!log) return; - /* Evaluate length of line to add to log */ + frag = list_last_entry(log, struct kunit_log_frag, list); + log_len = strlen(frag->buf); + len_left = sizeof(frag->buf) - log_len - 1; + + /* Attempt to print formatted line to current fragment */ va_start(args, fmt); - len = vsnprintf(NULL, 0, fmt, args) + 1; + len = vsnprintf(frag->buf + log_len, len_left, fmt, args) + 1; va_end(args); + if (len <= len_left) + goto out_newline; + + /* Abandon the truncated result of vsnprintf */ + frag->buf[log_len] = '\0'; + if (len > sizeof(frag->buf) - 1) { va_start(args, fmt); tmp = kvasprintf(GFP_KERNEL, fmt, args); @@ -212,24 +222,15 @@ void kunit_log_append(struct list_head *log, const char *fmt, ...) return; } - frag = list_last_entry(log, struct kunit_log_frag, list); - log_len = strlen(frag->buf); - len_left = sizeof(frag->buf) - log_len - 1; - - if (len > len_left) { - frag = kunit_log_extend(log); - if (!frag) - return; - - len_left = sizeof(frag->buf) - 1; - log_len = 0; - } + frag = kunit_log_extend(log); + if (!frag) + return; /* Print formatted line to the log */ va_start(args, fmt); - vsnprintf(frag->buf + log_len, min(len, len_left), fmt, args); + vsnprintf(frag->buf, sizeof(frag->buf) - 1, fmt, args); va_end(args); - +out_newline: /* Add newline to end of log if not already present. */ kunit_log_newline(frag); }