Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp673027rdg; Thu, 10 Aug 2023 16:04:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF63mhvuVEHN7GhX7/jpSktf+5NX0RsC5ic6q92DKeXa9LvinoMO1EDrVG3yUC5MM2vqZMY X-Received: by 2002:a17:902:d386:b0:1b8:41d4:89f with SMTP id e6-20020a170902d38600b001b841d4089fmr205398pld.4.1691708694323; Thu, 10 Aug 2023 16:04:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691708694; cv=none; d=google.com; s=arc-20160816; b=aN24mNzBCoArl0dtyk+FfMTljG+/OKe6fnRx7SzL4LOpIV2BidgzlRCERSaVCCVYe5 kxR2M4DlDG65mm9ftduOrt+mRQpz6SIMm3JCrl9rbfZDq6DptgPUHjq7piUDfnaOY0Th BeRCW7wBLh3qb3D7TFmWaYxFd0KuVMvX4X5CLX2afz9dq5Jz+iTnrTj4QHZaOgXMLNS/ 7yuYjMXA5bxGC1QroX3CfoGbxptHy3gLLiPvPk2IYZQjO9w77sa2oNiDbNj4Xp3MvhI4 As64o/HHot9k0PL7svCPmUevT2fzGKpI7fQvsaONmsloRl+vNWik57YRTGhH6Hy2+e/a tNCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=V6HsG3TdTc3uIggYTt+QrxTqSYca2qRoq6w/qVYjf+Y=; fh=ySsBksMhzzKkl2K4N6YwPRSDhNc6HDe7zOLg0GOa7gw=; b=bcuscAhSev4fMuR10tCMeLmQWL384u1XwXMtrDjMTFnkTXTNex1O5utRKsf4JvcfRI ah8TABuQ41GbXh6NcMdELKjAoB7lVk+GL/GG4v+2QfK2ls6KMQ9JZPlncgKDksbUd0tp Pt+qosVP5CPa1N6U/hlFRNTVr8MH50tKkLcIMcKxnAiwDrSJDm9kYaey7n/vzsiY9GMO OsJjT7Fg4I/ALNs8MDuHddWisJQok82Ks4zvIgkN7ohAmHTZrlCSYtekiFJVXxvfa3hw LXvWM+vYSTlYUxioXQtSZL4+DsWigFdq5xFvp+9LkEY7g+OpN8KH3EP5e4pEpg0723r1 FR/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=rNJKqfKw; 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=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q4-20020a170902c9c400b001bb1d188d9csi2233829pld.77.2023.08.10.16.04.42; Thu, 10 Aug 2023 16:04:54 -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=@google.com header.s=20221208 header.b=rNJKqfKw; 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=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229659AbjHJWlu (ORCPT + 99 others); Thu, 10 Aug 2023 18:41:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbjHJWlu (ORCPT ); Thu, 10 Aug 2023 18:41:50 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 004C0272E for ; Thu, 10 Aug 2023 15:41:48 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3fe32ec7201so12635e9.1 for ; Thu, 10 Aug 2023 15:41:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691707307; x=1692312107; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=V6HsG3TdTc3uIggYTt+QrxTqSYca2qRoq6w/qVYjf+Y=; b=rNJKqfKw6LNTRuuAjrZpZT9+NKLnqervN6pWlJxOaqsv7hISsBMN5ua/rlwKRZDiZ0 9gEaKX3ZxyuydkALn99gKGqo6FxadmRSgok+OJNAtzRnmhK0bqC0TT3YfMbQspdk4J8M 6y3Dgz1hf3/+rMgRE0VgCzH2Tj7KPl8MOSpsgVKsIyk9pXhi77wqfVQqHUZU1WP21wFC L6hEG/J1lU+Tlm8jCSgMOrX1ojFoiDM3wszbu5tcrTMRg4yEa6ksD6oRDTejBBn8ar01 pMzrjpOMZZUHwnfkL7rVqhmwcs6PhY0Vy9B+TMBJQSVFFLcg6DIm9gZFLAth5x+aKw9u qrAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691707307; x=1692312107; h=content-transfer-encoding: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=V6HsG3TdTc3uIggYTt+QrxTqSYca2qRoq6w/qVYjf+Y=; b=HflKCmDLqYmG9i8GhOpwu1uOhl084xnYMsA531saNCf8BbLca7iObJPQdiMtVlsjY3 52Hkqpf8GfA7/r2k90QW737C3/DCgOiuCztZG1Eu3hod/ujsixzaew1E8++o/mIMcDkk zablG32ucUn/Gos6xKktaUkvtRyOayfr/V70PmvNfLJgAkLvMaKhJVXULYIJUCREmG9g f0Bhxs58DeLdRWeI1WTIXLyy7XEdIIWRROi8JrxN+FjIgAGaZjKTQPp5FOIQyQ81yZVC vlfm/9wiD4RG8Ed2lY31hLEUtQergGfEL2JqQpLI6fvy6uSlXvom3ozG+eMoisvX5hOW UaxQ== X-Gm-Message-State: AOJu0YwCc63/EDr9Qa/ORzDmuhdQ1Sda6gCG8XWb8tFJPsDFBQBbJ+mS YYtVCrKbPDHdyyOD+cP3yWYcVenK4r9wSGTTYhx9Aw== X-Received: by 2002:a05:600c:1e23:b0:3f1:9a3d:4f7f with SMTP id ay35-20020a05600c1e2300b003f19a3d4f7fmr23322wmb.1.1691707307300; Thu, 10 Aug 2023 15:41:47 -0700 (PDT) MIME-Version: 1.0 References: <20230809155438.22470-1-rf@opensource.cirrus.com> <20230809155438.22470-4-rf@opensource.cirrus.com> In-Reply-To: From: Rae Moar Date: Thu, 10 Aug 2023 17:41:34 -0500 Message-ID: Subject: Re: [PATCH v3 3/7] kunit: Handle logging of lines longer than the fragment buffer size To: David Gow Cc: Richard Fitzgerald , brendan.higgins@linux.dev, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, patches@opensource.cirrus.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 On Thu, Aug 10, 2023 at 9:38=E2=80=AFAM David Gow wro= te: > > On Wed, 9 Aug 2023 at 23:54, Richard Fitzgerald > wrote: > > > > Add handling to kunit_log_append() for log messages that are longer tha= n > > a single buffer fragment. > > > > The initial implementation of fragmented buffers did not change the log= ic > > 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 temporar= y > > buffer and that content is then split across as many fragments as > > necessary. > > > > Signed-off-by: Richard Fitzgerald > > --- > > I think this looks good (and is a long-overdue addition to the logging > functionality). > > One thought I have (and I'm kicking myself for not thinking of it > earlier) is that this is starting to get very similar to the "string > stream" functionality in lib/kunit/string-stream.{h,c}. Now, I > actually think this implementation is much more efficient (using > larger fragments, whereas the string stream uses variable-sized ones). > Particularly since there are a lot of cases where string streams are > created, converted to a string, and then logged, there's almost > certainly a bunch of redundant work being done here. > > My gut feeling is that we should stick with this as-is, and maybe try > to either work out some better integration between string streams and > logging (to avoid that extra string allocation) or find some way of > combining them. > > Regardless, this seems good as-is to me. There are some minor comments > below, but nothing disastrous. I'll let Rae have a look over the > various strscpy and strlcat calls, though, as while I did check them > carefully (and KASAN hasn't complained), it's always best to have as > many eyes on C string code as possible. :-) > Hello! I have tested and taken a look at this and it looks overall good to me. I think all of the string copying and concatenating is right. Other than David's comments below, especially whether we should do this with string-stream, I am pretty happy to accept this as is. Thanks! Rae > But in my eyes, this is > Reviewed-by: David Gow > > Cheers, > -- David > > > 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(st= ruct 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 =3D list_last_entry(log, struct kunit_log_frag, list); > > + log_len =3D strlen(frag->buf); > > + bytes_left =3D sizeof(frag->buf) - log_len; > > + > > + written =3D strscpy(frag->buf + log_len, src, bytes_left); > > + if (written !=3D -E2BIG) > > + goto newline; > > + > > + src +=3D bytes_left - 1; > > + do { > > + new_frag =3D kunit_log_extend(log); > > + if (!new_frag) > > + goto newline; > > + > > + frag =3D new_frag; > > + written =3D strscpy(frag->buf, src, sizeof(frag->buf)); > > + src +=3D sizeof(frag->buf) - 1; > > + } while (written =3D=3D -E2BIG); > > + > > +newline: > > + if (written =3D=3D -E2BIG) > > I think that this can only be true if kunit_log_extend() fails. If the > do/while loop ends naturally, then written !=3D -E2BIG, as is the case > with the strscpy goto above. > > Do you think it's cleaner to move the 'written =3D strlen(frag->buf) > into the if (!new_frag) statement within the loop? > > > + written =3D strlen(frag->buf); > > + > > + p =3D &frag->buf[written - 1]; > > + if (*p !=3D '\n') { > > + if (strlcat(frag->buf, "\n", sizeof(frag->buf)) >=3D si= zeof(frag->buf)) { > > + frag =3D kunit_log_extend(log); > > + if (!frag) { > > A comment here could be useful. Something like "If we can't extend the > log to add a newline, truncate the last message". > > > + *p =3D '\n'; > > + return; > > + } > > + > > + frag->buf[0] =3D '\n'; > > + frag->buf[1] =3D '\0'; > > + } > > + } > > +} > > + > > /* Append formatted message to log, extending the log buffer if necess= ary. */ > > 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 =3D NULL; > > > > if (!log) > > return; > > > > - frag =3D list_last_entry(log, struct kunit_log_frag, list); > > - log_len =3D strlen(frag->buf); > > - len_left =3D sizeof(frag->buf) - log_len - 1; > > - > > /* Evaluate length of line to add to log */ > > va_start(args, fmt); > > len =3D vsnprintf(NULL, 0, fmt, args) + 1; > > va_end(args); > > > > + if (len > sizeof(frag->buf) - 1) { > > + va_start(args, fmt); > > + tmp =3D kvasprintf(GFP_KERNEL, fmt, args); > > + va_end(args); > > + > > Should we handle the case where kvasprintf() fails here and drop the > log message? > > > > + kunit_log_append_string(log, tmp); > > + kfree(tmp); > > + > > + return; > > + } > > + > > + frag =3D list_last_entry(log, struct kunit_log_frag, list); > > + log_len =3D strlen(frag->buf); > > + len_left =3D sizeof(frag->buf) - log_len - 1; > > + > > if (len > len_left) { > > frag =3D kunit_log_extend(log); > > if (!frag) > > -- > > 2.30.2 > >