From patchwork Tue Aug 8 12:35:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 132608 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2287399vqr; Tue, 8 Aug 2023 10:42:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEnG2oUxEFdwZKaT4KRfaXq3VVqX7FSkMfLQOw3/MbUGXiMDWbY/iemkJh2G48Pt3BlvMOO X-Received: by 2002:aa7:d912:0:b0:51d:95ac:22ed with SMTP id a18-20020aa7d912000000b0051d95ac22edmr732372edr.1.1691516576084; Tue, 08 Aug 2023 10:42:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691516576; cv=none; d=google.com; s=arc-20160816; b=Iw7KlbPW5EiG4G97QCtaJEJDx8d2p4B3QePImEC3SlykMxtU9swsT3NH0J+ggUHthk lSc7DdFrwbToeiR6LK3L++KF5qhD29oAI0oo0XWLO/Xm1NYGliooFsvqynkgeT1PFsEF 2fSvy9K+QTNcUKIeM3HT8VrCTjWlT61obalLFg80hm9o4A8mF4aTnsBhLRR7/UxnKRx3 5Qzzqhs/qoQZm7leKoSt6S4zbrPPreuyM0vzGb5KBH7KNnNU6EFylLROmF0ukAldz8aM /oXwiaGUhMBWGd86+Y6e5SlF/gP1jh6JonFLnc6aXW4jyyQlqC7x1fpt/HycwCKz5h66 s30A== 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=x990Uk6hehcrnWErRAWJUHOGT9awu9qgQMLD7h7nmi4=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=WqmGCCj9+sS+aKq24micgN88xMZkTbtfZn7X0Xdjbqsphb45SeeTkctCqjm0q7bDjc Sbujp7wT/SE7kkG9z6Nmy/pb8Qi0NqOGSQtR3yPYrJiZvMZ7G4IUdVwjumLVWSpucj3Y XS9CWZlYhcWVnFcudGH/pkEY/9ZfMbbrGpXcakHjvZvECsZ/Uls0BmWyIw4oKK+JLa8A 6w/Lyg3Uqeec2g8WgJNQ0zF82zTLuz3VovxoyN0oB4yJed+CYAmnrtn8dAKU21tI6/vJ uvcuJqjMu2IGlTROI7gJ3HXVJhnSWqgQllht1+LTgQBD6aPCAd+A/OrXMtpivX3+IpMr EErA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=QMS5zZTv; 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 f19-20020aa7d853000000b005231fc89319si5063751eds.8.2023.08.08.10.42.30; Tue, 08 Aug 2023 10:42:56 -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=QMS5zZTv; 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 S232186AbjHHQN1 (ORCPT + 99 others); Tue, 8 Aug 2023 12:13:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232063AbjHHQLb (ORCPT ); Tue, 8 Aug 2023 12:11:31 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3328F7D94; Tue, 8 Aug 2023 08:46:47 -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 378CIdoO005342; Tue, 8 Aug 2023 07:35:34 -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=x990Uk6hehcrnWErRAWJUHOGT9awu9qgQMLD7h7nmi4=; b= QMS5zZTvdpscgRk6Lh3iVwN5t8j8L9JNiZztSiJ9oKYJ17WBjrxY83Mw+2JLUOEl 3MWBIEdXv3eX0xYh0plgXO1lHp0eIjeHtA4OV6TWZ3lxn1Y9vt5Tx2SEuG+0bHEo 5B8Jwk5kKOhdOEqiXSPElJYpkdVMkCcMEST+/OC6X0HH2A8w3CYLz9Gb3D1Pjo68 tB3nsSRTsiLDLQHy5tqUv8ysFbcKaqAGDYV7B0wQfPAeql4b7rJzaIi0gkZqIJ7Q +G6srev9lRGupBYisbDD+NfvDKLjwe6t4+8rBLbBl00+3+T/T0eVrfL5wxVpsHdD z3gb6tWLGWDNMojTBxmlrw== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhtjv4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Aug 2023 07:35:34 -0500 (CDT) Received: from ediex01.ad.cirrus.com (198.61.84.80) 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; Tue, 8 Aug 2023 13:35:32 +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; Tue, 8 Aug 2023 13:35:32 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 2012715B4; Tue, 8 Aug 2023 12:35:32 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v2 1/6] kunit: Replace fixed-size log with dynamically-extending buffer Date: Tue, 8 Aug 2023 13:35:24 +0100 Message-ID: <20230808123529.4725-2-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230808123529.4725-1-rf@opensource.cirrus.com> References: <20230808123529.4725-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: PEqapEZ5fAEkN0Zg1lF6YZeLmuggleCB X-Proofpoint-ORIG-GUID: PEqapEZ5fAEkN0Zg1lF6YZeLmuggleCB 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: 1773683685326918085 X-GMAIL-MSGID: 1773683685326918085 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. Signed-off-by: Richard Fitzgerald Reviewed-by: David Gow --- include/kunit/test.h | 25 +++++++++++----- lib/kunit/debugfs.c | 65 ++++++++++++++++++++++++++++++++++-------- lib/kunit/kunit-test.c | 29 +++++++++++++------ lib/kunit/test.c | 63 ++++++++++++++++++++++++++++------------ 4 files changed, 136 insertions(+), 46 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 011e0d6bb506..ef8e09aafe1b 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,11 @@ struct kunit_attributes { enum kunit_speed speed; }; +struct kunit_log_frag { + struct list_head list; + char buf[KUNIT_LOG_FRAGMENT_SIZE]; +}; + /** * struct kunit_case - represents an individual test case. * @@ -132,7 +137,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 +257,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; }; @@ -272,7 +277,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; @@ -304,7 +309,7 @@ static inline void kunit_set_failure(struct kunit *test) bool kunit_enabled(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); @@ -317,6 +322,12 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites); +static inline void kunit_init_log_frag(struct kunit_log_frag *frag) +{ + INIT_LIST_HEAD(&frag->list); + frag->buf[0] = '\0'; +} + #if IS_BUILTIN(CONFIG_KUNIT) int kunit_run_all_tests(void); #else @@ -451,7 +462,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..54dc011c8980 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -533,9 +533,16 @@ static struct kunit_suite kunit_resource_test_suite = { 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 +550,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 +568,14 @@ 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", frag->buf); + 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 cb9797fa6303..bdb361741214 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 Tue Aug 8 12:35:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 132549 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2237275vqr; Tue, 8 Aug 2023 09:28:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHP/kHsO4a+3yYdUOc9B9Ujh0CJMqYkecXQm40Kc6i/8z78nXNY4iKPEOJiF/pby443Hbdy X-Received: by 2002:a17:907:78c7:b0:99b:e04d:3079 with SMTP id kv7-20020a17090778c700b0099be04d3079mr91708ejc.19.1691512085263; Tue, 08 Aug 2023 09:28:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691512085; cv=none; d=google.com; s=arc-20160816; b=tyLKXOLmhDJkBggxis66DOPysh3JYOnm8eA2vUPtuHRCZIZGVE66emWSw6+FfADX0A nNRIfrYt0YV4bxP92L5yLFF3ETcjzhRpdV/b3ze9Mkqofv4jER+MaNbSSgIHDjyCAdYK B4d12nUDDngTGMwuxyn6koqb5maBuBEH95kQGANR8WWyjebeHrGOiBCs2xXO3EB6PtgH zRanS3kfSVPfYkgextIewZWcn+gpd1bT1rHzl6Cuq0yRwfoIWe15+lPXPCyjIqUaqFB7 l3iqUjopEU73ttR2k6q8ObHRlgyTfGX2+AYwu3qyo6aeiv2qc0l21pnQVss3WmQc2jUY BR9Q== 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=+4e/xqTw8lqmf/V+eCtyHeNZMgbrGjTO2UMnqK+W1hQ=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=Gp9Dg3uvyoctSAkP9EsLqPOW886eZtNDjkf3jGEAIgkoalNl+b/w/igwYv8KPxdMYd 5KnOyGAHJnY9YTrIKA3StoOOA/Ohyr7LYIRtCAW454FtV2DG0nqJW/8nKMCJJ0wUw2Yf divshjE7kaIVT9RRgyaag5vfSIlWyc6bU4k9UneF8zzGxNa1jSqvMjNWMnTICM9hLhFf ahXxVia404VWJ7KYVebKPQ0/2XNk9qSrhexxwCQHqwSVZ0NnfmDhm6vbHuvCn6iGMa9v dEqdll7vCJwSI6latj0kE9D1AEgnNonrt4s9qhS6PYnVpwMs7+//lAqnzG9rgoyIP5/0 oNuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b="nDUc3qF/"; 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 lf9-20020a170906ae4900b00991fcf4687dsi6954479ejb.186.2023.08.08.09.27.38; Tue, 08 Aug 2023 09:28:05 -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="nDUc3qF/"; 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 S231640AbjHHQJd (ORCPT + 99 others); Tue, 8 Aug 2023 12:09:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231609AbjHHQHa (ORCPT ); Tue, 8 Aug 2023 12:07:30 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79A8D7694; Tue, 8 Aug 2023 08:46:09 -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 378CIdoP005342; Tue, 8 Aug 2023 07:35:35 -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=+4e/xqTw8lqmf/V+eCtyHeNZMgbrGjTO2UMnqK+W1hQ=; b= nDUc3qF/yME8qTvJXN3/kKjZl2M6EyhFLQ6s4GP9LRluQdqwkZ/YXgzqJk9Op+xe WfrhCaX+QuKtAGnLYnRqJgC0p+Z781A580+sqcKejX6kb4kCRDchi+c965GPjz4F HLdzcatRfafU9QEeS0ULkgI/zoU7Y/5AtedDVwWh+Z8btT3qZWaI7w5KLXpIlQi0 LGru0xPBfkdYPMT7VLOETP13X9lAHG7N8JReJsq9ra7LyjS08Z7JPxEwfMVi1B/9 9RCvuX6rW4zUJ+8eUUiu+PnFB/KheAhP+GDLglZv0Bog5Rf20p9fH2A1aurSNzWU fQW/JJjzD7pO73CHXdfyVw== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhtjv4-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Aug 2023 07:35:34 -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; Tue, 8 Aug 2023 13:35:33 +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; Tue, 8 Aug 2023 13:35:32 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 92E8B15B7; Tue, 8 Aug 2023 12:35:32 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v2 2/6] kunit: kunit-test: Add test cases for extending log buffer Date: Tue, 8 Aug 2023 13:35:25 +0100 Message-ID: <20230808123529.4725-3-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230808123529.4725-1-rf@opensource.cirrus.com> References: <20230808123529.4725-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: EPww4PnPDzjn6O5PameaDZYxTpJvY-lx X-Proofpoint-ORIG-GUID: EPww4PnPDzjn6O5PameaDZYxTpJvY-lx 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: 1773678976340097236 X-GMAIL-MSGID: 1773678976340097236 Add test cases for the dynamically-extending log buffer. 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_frag_sized_line_test() logs a line that exactly fills a fragment. This should not cause an extension of the log or truncation of the line. 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 | 204 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 54dc011c8980..48967d12e053 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,6 +531,31 @@ 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, + 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) + len += strlen(frag->buf); + + len++; /* for terminating '\0' */ + p = kunit_kmalloc(test, len, GFP_KERNEL); + + 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) { struct kunit_suite suite; @@ -568,7 +594,9 @@ 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) { @@ -576,14 +604,190 @@ static void kunit_log_newline_test(struct kunit *test) KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(frag->buf, "Add newline\n"), "Missing log line, full log:\n%s", frag->buf); 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_kmalloc(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_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); + + 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_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); + + /* 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 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_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); + + 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); +#else + kunit_skip(test, "only useful when debugfs is enabled"); +#endif +} + static struct kunit_case kunit_log_test_cases[] = { 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 Tue Aug 8 12:35:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 132541 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2225760vqr; Tue, 8 Aug 2023 09:10:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHSc3socyqVE2uJDtQ0aYcK4lbxPH2fHfyxESJJfVakTiVKXvL8Iy4q0vhj/ajRbFO3nAOC X-Received: by 2002:a17:906:739a:b0:97c:64bd:50a5 with SMTP id f26-20020a170906739a00b0097c64bd50a5mr37634ejl.53.1691511028077; Tue, 08 Aug 2023 09:10:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691511028; cv=none; d=google.com; s=arc-20160816; b=r08O6ovx9Npa/OzaiCiDKJVoydCUktvPpdmFPXUYUgzk5Y80qmbuEXmzw2V1XU5hY2 4RRzHr38jUOSCWviTrawZmsYQ47To1RpKeoPKfQlv5lCZq57iWx4UrEx2xwn7jCdCMAN V2qHLOXgDkLzng6sKdtLbJXYJoOXVbpaeFMscNl3WaR4RrPvRbFmgG2It9gmmYvPYxip S8zJHSgYkhFqWjboZZg03SThmtv2ud9MyJXzd1nh1ELE91tON9sDmmKgqRfVeFyfm21U CUFq3Yn9Gr7cbx7WQzDFqkqrrmaxZtwLHo28gjpIxFlaXPIv8dHkW53oHgWRxplHt34C Uihg== 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=80ZNfd5TqM1BuLBGdIdobGNbNPgNN96mZEH8yR3aJxU=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=sQOijICOQhl9SMERvUpEDu3/s5NBYhyC3LxglsbkxrH0uNQ1D2hOF+121CkDG6rIuG ulNvvaC8lOy37/+QRUVF8L5zAxzI9uvKNmY4HKDBnFmsgkpDdkzf/6x8ocicZib1wPVW bnLc080TaiozIjTppNQP7QYmzw6HshIteI0TJ8ep5aL3hSxzxAcBWSOMiTjMNszpxDPA gkKV9eS7HynAGSlbqLXEHQwhk+WQO5hXhYdPQAuaL84tdnRHsedgbOsJ8oePj1grqiLn 3IaDts9s6q1ma4Bjj/7TMG+9nSjfZzb6t74iE+ohKSuiI0ZX7kC9BFsTmgMTqrN9KMC/ xymA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=DiQYlkLI; 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 n22-20020a170906379600b0099cb991ba16si3757751ejc.314.2023.08.08.09.09.57; Tue, 08 Aug 2023 09:10:28 -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=DiQYlkLI; 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 S231928AbjHHQI7 (ORCPT + 99 others); Tue, 8 Aug 2023 12:08:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231843AbjHHQHG (ORCPT ); Tue, 8 Aug 2023 12:07:06 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E88307680; Tue, 8 Aug 2023 08:45: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 378CIdoQ005342; Tue, 8 Aug 2023 07:35:35 -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=80ZNfd5TqM1BuLBGdIdobGNbNPgNN96mZEH8yR3aJxU=; b= DiQYlkLIMMd+lAQCgmfULlBHKhe00ogbEo6vtipWKJkm34shrI+9ORuwRE5Sx9Ay fAn7LLLNB1CB8S5rLokZA7zVpV29cnsQorq1kxn6VU4ZuEAeKgtHBs0/PgktBA9t U0fO6nW8wxLnF2eecFQnuw0PB2uwe+P1QC9mPXAVl2ZrDF4rKw6Jdp4GqE/34/jV BlVnP9hXnmLFoQOnv8SY3HUKPCyAkFWyMHIDFr9k9II1kh9KctCAD4hkzO/zvW/+ ljCcrMqYgg8PsTibec0DXLDGmTiGULagFTZfqf8kx0W89o0SWdFuKDw12+wY0AV1 iBJAQrzCCpwcmNe4B36hRQ== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhtjv4-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Aug 2023 07:35:35 -0500 (CDT) Received: from ediex01.ad.cirrus.com (198.61.84.80) 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; Tue, 8 Aug 2023 13:35:33 +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; Tue, 8 Aug 2023 13:35:33 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 0F2FD15B4; Tue, 8 Aug 2023 12:35:33 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v2 3/6] kunit: Handle logging of lines longer than the fragment buffer size Date: Tue, 8 Aug 2023 13:35:26 +0100 Message-ID: <20230808123529.4725-4-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230808123529.4725-1-rf@opensource.cirrus.com> References: <20230808123529.4725-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: dYUMqVNlabesl7AZGC82aVtnDnoCYPC1 X-Proofpoint-ORIG-GUID: dYUMqVNlabesl7AZGC82aVtnDnoCYPC1 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: 1773677867832481058 X-GMAIL-MSGID: 1773677867832481058 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 --- 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 bdb361741214..b00f077314e3 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 Tue Aug 8 12:35:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 132654 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2296236vqr; Tue, 8 Aug 2023 10:59:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHibJ81viocA2U1iNYgzEN80UwVKW1Z2m754qiZ3mkKi38lUUKIp/CUZETJp7X2ThO1ufHG X-Received: by 2002:a05:6830:101a:b0:6bc:ea33:2799 with SMTP id a26-20020a056830101a00b006bcea332799mr308404otp.26.1691517547175; Tue, 08 Aug 2023 10:59:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691517547; cv=none; d=google.com; s=arc-20160816; b=Mr9t+ahKgCOraQhvIMpGDOkYDmMyn5YKaDvDY4OR5m7N2vNzWY0lvg6VQ8F9qnvNYj JkP/PNx+C69APvSdZ0N32QbO8dNqkjUnogdVoyhtA1EayvYnRXbRyl/Jn+Mm6TNqfT69 vE9eow9t9xVoF8f/8op7qoX6F96zdBApO60F9GsUrtdAreU2LWojKhx4o6+0GdMvMMxH 7/0Y6n133PwDOiU/Ke2gEoTuPRF9ZxEjQ2HaPIKNjkj1uMa1RtnFLQVykTfbrszwvg4a KQfoqkjuF48ck5JdsrC0qhaL17VfLS0nqbvWSXK0lLuYoVZ6MoFr8IViliDOMQYV/BDs XP0A== 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=QfY4W86wf1IIrXB3Is+gekQWjYV5Fth3XKbMFEMODbM=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=opcU/02Z1oLHz3crWVdWr+Me45WihSkTPuADaq8IWgV0F6ASX4NLIW+zpswe/yqvcJ INAyJMeybeDKvb9MIJC8q89HiMm/tM4yOtaY2lBOkznU+ZMg0brmvs0W75LjWegcVltl SuarwT6hPly/EO+Nl23EuttYLSVOm/ZqOXvI9nsW5B7ZZIEKs74XVzvcux13KnbYtN+K acpuQM2Rko16Atbwj8oLsLplNCdpALqdQLFC7IQi2MFaQfZaGjSp69yDF7Ilzdjwbi2o 0rHGMtyNBbcV6hp+QoeL2fhUe+w9asVm5FiotC0hvR+rUUJDRWMbaX3HOXDQBzlhCaGm 1bIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=N4q6gaDX; 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 r142-20020a632b94000000b0056419b1dc1bsi7624716pgr.740.2023.08.08.10.58.53; Tue, 08 Aug 2023 10:59:07 -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=N4q6gaDX; 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 S232237AbjHHQPc (ORCPT + 99 others); Tue, 8 Aug 2023 12:15:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232218AbjHHQNn (ORCPT ); Tue, 8 Aug 2023 12:13:43 -0400 Received: from mx0b-001ae601.pphosted.com (mx0a-001ae601.pphosted.com [67.231.149.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A8917EF1; Tue, 8 Aug 2023 08:47:52 -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 378BloE5011155; Tue, 8 Aug 2023 07:35:36 -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=QfY4W86wf1IIrXB3Is+gekQWjYV5Fth3XKbMFEMODbM=; b= N4q6gaDXWe5y5BxkYc6bg/JWw1JhU2RIHmQNHp2DOeKYlZKhdq5nguGIP95JOluo mV+fwRC5NV7oMr/EA6X0AhOf/xEtW6gZl36xbPbJv1KaGrNA4F1YGCaVPQNOios7 ke8DxtOwsF5LufuAdTHvREmAUCIFKGkwHfvIWv0OwFgXHkrI8/VOvNppchfjz3BC KNXHXOy81HDiNO2MqUh1p+7E6CIWiiRJ924KXKjH+JTHidHk0VgVdrhVZHsfPGF/ ddRqLQPswLDfbSFnMMoYaCnx8EnrNe1dWf4PmX7rYklLx1JrUQ4W17fzBJIbT1xu /4o9BoVJ+UeP2qGGqj7kAw== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0a-001ae601.pphosted.com (PPS) with ESMTPS id 3sb7vt8uka-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Aug 2023 07:35:36 -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; Tue, 8 Aug 2023 13:35:33 +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; Tue, 8 Aug 2023 13:35:33 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 7F09015B7; Tue, 8 Aug 2023 12:35:33 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v2 4/6] kunit: kunit-test: Add test cases for logging very long lines Date: Tue, 8 Aug 2023 13:35:27 +0100 Message-ID: <20230808123529.4725-5-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230808123529.4725-1-rf@opensource.cirrus.com> References: <20230808123529.4725-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: CSr2h__a2sFg0G1LAYE9otyur7FfnoU0 X-Proofpoint-ORIG-GUID: CSr2h__a2sFg0G1LAYE9otyur7FfnoU0 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: 1773684703465593771 X-GMAIL-MSGID: 1773684703465593771 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 --- 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 48967d12e053..621acdb5385e 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -596,7 +596,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) { @@ -621,6 +621,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"); } @@ -782,12 +809,67 @@ 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_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), + KUNIT_CASE(kunit_log_long_line_test), {} }; From patchwork Tue Aug 8 12:35:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 132746 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2330351vqr; Tue, 8 Aug 2023 11:58:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGi9CfKlJQiXMUhswsDijiQWmeV/NI23VDphJdu2KOdEQ5Yr06OLPrawHghSHa9GvgPKj9w X-Received: by 2002:a05:6a20:7d9f:b0:13e:2080:ab91 with SMTP id v31-20020a056a207d9f00b0013e2080ab91mr717338pzj.28.1691521131593; Tue, 08 Aug 2023 11:58:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691521131; cv=none; d=google.com; s=arc-20160816; b=XTalscZi+smlZ3qWHmEEs/gZp0rT4xoASVVpB1yKwmbwgodOH8GH3Ap63OPgBzGj+g DFWpy1cSTFSaff5CFLS5jQrXwNUywb4IrTpdW1AaPL1f+d71U91CMjQiT7knfd0BIv5b I2ft0yOFnyeXI7GMQUMgdKhtqJ8BVd3t0446KgS5symPXKk/4h1UCsJenvz2qU0VEkuh jKMGudcvmI+ofPsxqV+TsLWxZX5pohvzBWcl4Q76RoS9LTN4PGBTeC/B2DbzAyD9uFO5 JNbJdUG9F2ZXDCuK2EIp7aZZApvtLg57jn12UMU20Sa4tApb2KGx/353K5PXauTOrYlJ MyvQ== 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=borUz3+VefVCpATITwUMnBd9+od/g4pGsJIfb5Jw47M=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=z6Qd6bzqsVERO4WWCrV8xqna4+EH3m+4AADj2AvH1CimjfmVgNE1LmEpqUVdIIo8SA FkJCJT099s8/oE9M32gjtTSsrZHTCwjBW41fHlken4fG2A+mPT1/qEr8oZkNKVlX9DqH HISfoN2if5BZ2zLcPFd/NwWpkaexj8SLJfY1dA+VmAeL1gPu58tXf5XnLHYyDRm7+Tde YlLyKww6+xoTJzAqwEMowust+rb00z+Rwwa5ftZ8oOekjqdUY1O4YynB64HvE+bLzFb4 8bu5JX76giN0thSN1VetOfdxTamzGbo03t9yk2LOM1jxgmpGYifqIG1H+TIYnoyHJgqn XVYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=hH+Rz+iU; 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 d9-20020a056a0024c900b006875f6c46b9si7747315pfv.141.2023.08.08.11.58.37; Tue, 08 Aug 2023 11:58:51 -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=hH+Rz+iU; 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 S231890AbjHHQIs (ORCPT + 99 others); Tue, 8 Aug 2023 12:08:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231776AbjHHQG5 (ORCPT ); Tue, 8 Aug 2023 12:06:57 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 303AB72B9; Tue, 8 Aug 2023 08:45: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 378CIdoR005342; Tue, 8 Aug 2023 07:35:36 -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=borUz3+VefVCpATITwUMnBd9+od/g4pGsJIfb5Jw47M=; b= hH+Rz+iUusN9POr1S0eKxSMvEbeDhEuHhxrjK7B7xgjQN5suVaAVGeS7siU1nJKE McdZ00cHUq9YB07sq0DRRf8jqHnJCbJ3iPmnvs0XrBWQdNuBzTaPJ01nCX+DmcvG UQiNV+L/yXsqKHGQem3B8capb170IwsXWeNf3s+uxvDN04KGN+6/wwDOUC5WqzK7 37OSHqKTBmXJeENb3GjW5Ehr12alro+4zvXmJgmGJs3FjNRwYz8NcZ+1xgM6ze0W bSXhidfDmoR/RJubB4Z/hDDTPhCZwrkFgFVtDqye87BBlRKJ2x9Y6z47OkaQOvq+ 0tmfwnNy9hz7sPD0Wa9cfg== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhtjv4-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Aug 2023 07:35:36 -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; Tue, 8 Aug 2023 13:35:34 +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; Tue, 8 Aug 2023 13:35:34 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 000B015B4; Tue, 8 Aug 2023 12:35:33 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v2 5/6] kunit: kunit-test: Add test of logging only a newline Date: Tue, 8 Aug 2023 13:35:28 +0100 Message-ID: <20230808123529.4725-6-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230808123529.4725-1-rf@opensource.cirrus.com> References: <20230808123529.4725-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: o_vKgZy7nYdYophLMrm6peb-vY_dkOju X-Proofpoint-ORIG-GUID: o_vKgZy7nYdYophLMrm6peb-vY_dkOju 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: 1773688462227445134 X-GMAIL-MSGID: 1773688462227445134 Add a test that logging a string containing only a newline appends one newline to the log. Signed-off-by: Richard Fitzgerald --- 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 621acdb5385e..ce80fb42128c 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -613,6 +613,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 Tue Aug 8 12:35:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Fitzgerald X-Patchwork-Id: 132638 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2293072vqr; Tue, 8 Aug 2023 10:52:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8y2jFthj3YxMpH/7aVF7myqYLL5BSIU9/8Dc9piWqCXMFui4hNu7RBF45z0hDi+JnyWsR X-Received: by 2002:a05:6a20:4320:b0:118:e70:6f7d with SMTP id h32-20020a056a20432000b001180e706f7dmr291568pzk.10.1691517169853; Tue, 08 Aug 2023 10:52:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691517169; cv=none; d=google.com; s=arc-20160816; b=u6NUz7efMTmpVl2G0f73R0Ga+O/5k4D+dkqPwumpK6SdPnEWxthJn8ZA385aU+1LTb xOUSBpxVZbEZ7IU0asH3G3tecEu8AX/D3QhHa1z0+BxDzySWjSC25uzcXLYtw5nMTC+h ijv5dOWRA9P+cYZyC1Q2OwOmrrOONMQr4Uk5NFXgL75DNk7k5Q9I1Lkq6qx+dhq0uM/N V+V37Zs63gymMZTcWgYqzoiVMy4IdY3pgEA1j0Mfb87tKg4NwpPhUi1ruWgAukGZXGo6 V4hcSyhDVq6hzdaE5WQ3jDpUcQGqo02ZHaYgozoiTsaYyrzcAPC8/Imu3r5RMa1zvSb4 9qwg== 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=GgFdDnzz+7JAwjRJW8zhNTbwVgS2JIBiaL8XcC78l4c=; fh=bGF4ESczbIrmrNxFOdEMfzJNgTVkRsqDRt+LRN0CeSg=; b=aw7g9bz6Z1wwQ+VpLxSecoufI90QSHauwSuy/xfMero8tzrhjKeuBtTewP52HIKec0 rM4hzFh5Gxdw0IiyG7PDhpuNncpsrOzUziqRmY2JAlUhPLGFGUiVjce4Fh+T76K+O7JO dh5hbEqx/fJE9lGTpZumxM2TpIIP1av0HV8wSmKm3vxWmtswxfAh0n5V8yfGD32Fx5Og 7TyHlNC3AZJY2bPTjwSkQlPTnIpOHe9bP1Ol1ccyftMZaK/pCW65vs4ycvkBO36mkBGW ZBRQVFoWoeCuDpLlMfTUt6jMFbJvoRFZrc4yTQqwx1q/0rWNSNHXj9IKJXToStzZM34C MBbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=pmIzygmB; 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 n1-20020a632701000000b0055aeedd94d3si7651872pgn.58.2023.08.08.10.52.36; Tue, 08 Aug 2023 10:52:49 -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=pmIzygmB; 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 S231955AbjHHQJT (ORCPT + 99 others); Tue, 8 Aug 2023 12:09:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231891AbjHHQHQ (ORCPT ); Tue, 8 Aug 2023 12:07:16 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAB9835B0; Tue, 8 Aug 2023 08:46:03 -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 378CIdoS005342; Tue, 8 Aug 2023 07:35:37 -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=GgFdDnzz+7JAwjRJW8zhNTbwVgS2JIBiaL8XcC78l4c=; b= pmIzygmBfsn3qRVVIz4bdaVjtyFst7C1dcTzcU/48cjJB+HasIM7iWvDD7m1e7wC z6GhITDLraCZ3f+ZNFrNojdHzykijI0pg860a53NnerHgWrUDMrWbIBc74qV8aux 6WDtjwqG1EuCoiWxQLFiNL4j/JIJN0rVHt+PV3joHlUFvNYffOyMxgW7WUg+i7te ujwHuVmFZ6J+w+4E6xw32cNmmSDWsugrSdL8dU0xGpvAXCFUei8ubaEz3+ZuxgZO WfHN92dDYMVQBhTttxc5zge2WuBBxJ7nCha49loe/p/iTDjL2FS2/uquCu48d7y8 9+IOuXlxNLPanSciy3YUbQ== Received: from ediex02.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3s9juhtjv4-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 08 Aug 2023 07:35:37 -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; Tue, 8 Aug 2023 13:35:34 +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; Tue, 8 Aug 2023 13:35:34 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.64.220]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 6EFFF15B7; Tue, 8 Aug 2023 12:35:34 +0000 (UTC) From: Richard Fitzgerald To: , , CC: , , , , Richard Fitzgerald Subject: [PATCH v2 6/6] kunit: Don't waste first attempt to format string in kunit_log_append() Date: Tue, 8 Aug 2023 13:35:29 +0100 Message-ID: <20230808123529.4725-7-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230808123529.4725-1-rf@opensource.cirrus.com> References: <20230808123529.4725-1-rf@opensource.cirrus.com> MIME-Version: 1.0 X-Proofpoint-GUID: Osq8-YE0_Ig-_LR4re6_Fn4RC4EyohRg X-Proofpoint-ORIG-GUID: Osq8-YE0_Ig-_LR4re6_Fn4RC4EyohRg 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: 1773684307733708230 X-GMAIL-MSGID: 1773684307733708230 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 --- 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 b00f077314e3..e7f0a46d184a 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); }