Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 091FBC64EC4 for ; Wed, 8 Mar 2023 05:05:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229743AbjCHFFk (ORCPT ); Wed, 8 Mar 2023 00:05:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229792AbjCHFFe (ORCPT ); Wed, 8 Mar 2023 00:05:34 -0500 Received: from mail-vs1-xe34.google.com (mail-vs1-xe34.google.com [IPv6:2607:f8b0:4864:20::e34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63877A3B74 for ; Tue, 7 Mar 2023 21:05:19 -0800 (PST) Received: by mail-vs1-xe34.google.com with SMTP id s1so14392817vsk.5 for ; Tue, 07 Mar 2023 21:05:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678251918; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=mKAkMNy8OXMf+KTboaBCvf0w0hNZbKrxY1TKD5wptAo=; b=SUhE+/m8Yqbee27RurVKsPpgiUnVoNGiR9ZR8O5zKH3TRBYVRZpEbpK/Conrow9itX +fDzXVUFT630FZZE8vFbxw/Hv008Iesux4OvkhXfEALbIPDR4SAgmotowjvoEjbxcBoS HgfX0VVIJhGdTnaNoANpaBfs5Ok7kZnWp0UHGRLmOkYA5TEQ9rtD7IvY7DXvRPtH5gAp 3JVoiciksFFWb8kbdZbbCmdL8/1kOP07OUIW5xOQXdCSbVp8nsiCJngX+ODjZ4uYfhG8 5gircNPXNbBqFmDpcL0UpVszc6Hiw3UkhT0KaiWcMkI2q6eqji0gspOnEDcf+RGPdbUq WYqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678251918; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mKAkMNy8OXMf+KTboaBCvf0w0hNZbKrxY1TKD5wptAo=; b=HZvfIpBvCFH3o+IzhBUwX5renJe2ekpELB1NhJppc0nCl51T4Sb3eOiaTQVhXmX9b9 thL3N7UGT6y8S1QwZQwYhU1wlQwPKvtT9lgs7oRaHM7nJFkWYA0T4zEyQoUFr0jSi2sB fBfkWTX8ssqtD1cnKqO5cVWoCT60aKP0Qw/hB2je5FVGOnHjudGew4CZr79nafUojF1N vb/qoe0sNCOCp9YwJNzZuo2SGHutUoo1SaBgXE8kgy7u/SWJZ5xrxKWkP6N40blLeHNE vnYGv87sWb5raA+M3c8NM8b1Zr6uYwPsAWWmM3gL3zQO5Azndxo0gomKtEWzttVKSlPC ksSQ== X-Gm-Message-State: AO0yUKXdP+QVgtWC80ACrcIzVT5oUT8BgihXtJkKoRwb3mJMhxbQQjSy Yl5zy64FQAc4SZvYafIyA9VDnii2ckrOiNDC3rjs9A== X-Google-Smtp-Source: AK7set+m9UfoInekb41pOvyMngyCYf6YcpXBmSWH7rkYaQvJPk7PT3SRG6A8k6Eq7fE+yTCynyUVeInebRRDWqyWFF4= X-Received: by 2002:a67:ff1a:0:b0:422:1687:f239 with SMTP id v26-20020a67ff1a000000b004221687f239mr636084vsp.2.1678251917952; Tue, 07 Mar 2023 21:05:17 -0800 (PST) MIME-Version: 1.0 References: <20230307223937.2892762-1-rmoar@google.com> <20230307223937.2892762-3-rmoar@google.com> In-Reply-To: <20230307223937.2892762-3-rmoar@google.com> From: David Gow Date: Wed, 8 Mar 2023 13:05:06 +0800 Message-ID: Subject: Re: [PATCH v3 3/3] kunit: fix bug of extra newline characters in debugfs logs To: Rae Moar Cc: brendanhiggins@google.com, dlatypov@google.com, skhan@linuxfoundation.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-256; boundary="0000000000006fcef105f65c790d" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --0000000000006fcef105f65c790d Content-Type: text/plain; charset="UTF-8" On Wed, 8 Mar 2023 at 06:39, Rae Moar wrote: > > Fix bug of the extra newline characters in debugfs logs. When a > line is added to debugfs with a newline character at the end, > an extra line appears in the debugfs log. > > This is due to a discrepancy between how the lines are printed and how they > are added to the logs. Remove this discrepancy by checking if a newline > character is present before adding a newline character. This should closely > match the printk behavior. > > Add kunit_log_newline_test to provide test coverage for this issue. (Also, > move kunit_log_test above suite definition to remove the unnecessary > declaration prior to the suite definition) > > As an example, say we add these two lines to the log: > > kunit_log(..., "KTAP version 1\n"); > kunit_log(..., "1..1"); > > The debugfs log before this fix: > > KTAP version 1 > > 1..1 > > The debugfs log after this fix: > > KTAP version 1 > 1..1 > > Signed-off-by: Rae Moar > --- Thanks, this is looking good to me! One very minor formatting issue below, otherwise: Reviewed-by: David Gow Cheers, -- David > > Changes from v2 -> v3: > - Changes to commit message. > > Changes from v1 -> v2: > - Changed the way extra newlines are removed. Instead of removing extra > newline characters, add a newline if one is not present. This is a bit > cleaner. > - Note: I looked into using KERN_CONT to match the printk behavior > to vsnprintf but this could cause issues with KTAP printing on the same > line as interrupting kernel messages. I also looked at just adding > KERN_CONT functionality to kunit_log and I did get this to work but it > was a bit messy because it required a few calls to kunit_log_newline in > kunit_run_tests. If this is very desired functionality, happy to add this > to version 3. > > include/kunit/test.h | 2 +- > lib/kunit/kunit-test.c | 35 +++++++++++++++++++++++------------ > lib/kunit/test.c | 18 ++++++++++++++++++ > 3 files changed, 42 insertions(+), 13 deletions(-) > > diff --git a/include/kunit/test.h b/include/kunit/test.h > index 0668d29f3453..bd9dbae5e48d 100644 > --- a/include/kunit/test.h > +++ b/include/kunit/test.h > @@ -420,7 +420,7 @@ void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); > #define kunit_log(lvl, test_or_suite, fmt, ...) \ > do { \ > printk(lvl fmt, ##__VA_ARGS__); \ > - kunit_log_append((test_or_suite)->log, fmt "\n", \ > + kunit_log_append((test_or_suite)->log, fmt, \ Nit: the trailing '\' here is misaligned. > ##__VA_ARGS__); \ > } while (0) > > diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c > index 4df0335d0d06..b63595d3e241 100644 > --- a/lib/kunit/kunit-test.c > +++ b/lib/kunit/kunit-test.c > @@ -443,18 +443,6 @@ static struct kunit_suite kunit_resource_test_suite = { > .test_cases = kunit_resource_test_cases, > }; > > -static void kunit_log_test(struct kunit *test); > - > -static struct kunit_case kunit_log_test_cases[] = { > - KUNIT_CASE(kunit_log_test), > - {} > -}; > - > -static struct kunit_suite kunit_log_test_suite = { > - .name = "kunit-log-test", > - .test_cases = kunit_log_test_cases, > -}; > - > static void kunit_log_test(struct kunit *test) > { > struct kunit_suite suite; > @@ -481,6 +469,29 @@ static void kunit_log_test(struct kunit *test) > #endif > } > > +static void kunit_log_newline_test(struct kunit *test) > +{ > + 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")); > + } else { > + kunit_skip(test, "only useful when debugfs is enabled"); > + } > +} > + > +static struct kunit_case kunit_log_test_cases[] = { > + KUNIT_CASE(kunit_log_test), > + KUNIT_CASE(kunit_log_newline_test), > + {} > +}; > + > +static struct kunit_suite kunit_log_test_suite = { > + .name = "kunit-log-test", > + .test_cases = kunit_log_test_cases, > +}; > + > static void kunit_status_set_failure_test(struct kunit *test) > { > struct kunit fake; > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > index 811fcc376d2f..e2910b261112 100644 > --- a/lib/kunit/test.c > +++ b/lib/kunit/test.c > @@ -108,6 +108,22 @@ static void kunit_print_test_stats(struct kunit *test, > stats.total); > } > > +/** > + * kunit_log_newline() - Add newline to the end of log if one is not > + * already present. > + * @log: The log to add the newline to. > + */ > +static void kunit_log_newline(char *log) > +{ > + int log_len, len_left; > + > + log_len = strlen(log); > + len_left = KUNIT_LOG_SIZE - log_len - 1; > + > + if (log_len > 0 && log[log_len - 1] != '\n') > + strncat(log, "\n", len_left); > +} > + > /* > * Append formatted message to log, size of which is limited to > * KUNIT_LOG_SIZE bytes (including null terminating byte). > @@ -135,6 +151,8 @@ void kunit_log_append(char *log, const char *fmt, ...) > vsnprintf(log + 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); > } > EXPORT_SYMBOL_GPL(kunit_log_append); > > -- > 2.40.0.rc0.216.gc4246ad0f0-goog > --0000000000006fcef105f65c790d Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIIPnwYJKoZIhvcNAQcCoIIPkDCCD4wCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg ggz5MIIEtjCCA56gAwIBAgIQeAMYYHb81ngUVR0WyMTzqzANBgkqhkiG9w0BAQsFADBMMSAwHgYD VQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMzETMBEGA1UEChMKR2xvYmFsU2lnbjETMBEGA1UE AxMKR2xvYmFsU2lnbjAeFw0yMDA3MjgwMDAwMDBaFw0yOTAzMTgwMDAwMDBaMFQxCzAJBgNVBAYT AkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMSowKAYDVQQDEyFHbG9iYWxTaWduIEF0bGFz IFIzIFNNSU1FIENBIDIwMjAwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCvLe9xPU9W dpiHLAvX7kFnaFZPuJLey7LYaMO8P/xSngB9IN73m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