public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Aaron Lewis <aaronlewis@google.com>
Cc: kvm@vger.kernel.org, pbonzini@redhat.com, jmattson@google.com
Subject: Re: [PATCH v2 2/6] KVM: selftests: Add kvm_snprintf() to KVM selftests
Date: Mon, 5 Jun 2023 17:05:46 -0700	[thread overview]
Message-ID: <ZH54WgNDADIhX2wx@google.com> (raw)
In-Reply-To: <20230424225854.4023978-3-aaronlewis@google.com>

On Mon, Apr 24, 2023, Aaron Lewis wrote:
> diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
> index a6e9f215ce70..45cb0dd41412 100644
> --- a/tools/testing/selftests/kvm/include/test_util.h
> +++ b/tools/testing/selftests/kvm/include/test_util.h
> @@ -186,4 +186,7 @@ static inline uint32_t atoi_non_negative(const char *name, const char *num_str)
>  	return num;
>  }
>  
> +int kvm_vsnprintf(char *buf, int n, const char *fmt, va_list args);
> +int kvm_snprintf(char *buf, int n, const char *fmt, ...);
> +
>  #endif /* SELFTEST_KVM_TEST_UTIL_H */
> diff --git a/tools/testing/selftests/kvm/lib/kvm_sprintf.c b/tools/testing/selftests/kvm/lib/kvm_sprintf.c
> new file mode 100644
> index 000000000000..db369e00a6fc
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/lib/kvm_sprintf.c
> @@ -0,0 +1,313 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include "test_util.h"
> +
> +#define ASSIGN_AND_INC_SAFE(str, end, v) \
> +({					 \
> +	if (str < end)			 \

This should assert instead of hiding the error and forcing the caller to detect
the problem, which may or may not actually happen.  The easiest thing is to just
thie guest only, e.g. name the helpers guest_*snprintf() and then do

#define APPEND_BUFFER_SAFE(str, end, v)	\
do {					\
	GUEST_ASSERT(str < end);	\
	*str++ = (v);			\
} while (0)

I doubt there will be a use case for using the custom snprintf in a helper that
is common to guest and host, and if someone does need/want that functionality,
they can add a global flag to track whether or not selftests is running guest or
host code.

And I would deliberately use GUEST_ASSERT() instead of a GUEST_UCALL_FAILED.
KVM carves out enough space for KVM_MAX_VCPUS concurrent/nested ucalls, so for
the vast majority of tests using GUEST_ASSERT() will succeed and be useful.  And
if some test exhausts the ucall pool, it'll end up with GUEST_UCALL_FAILED anyways.

  reply	other threads:[~2023-06-06  0:05 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-24 22:58 [PATCH v2 0/6] Add printf and formatted asserts in the guest Aaron Lewis
2023-04-24 22:58 ` [PATCH v2 1/6] KVM: selftests: Add strnlen() to the string overrides Aaron Lewis
2023-04-24 22:58 ` [PATCH v2 2/6] KVM: selftests: Add kvm_snprintf() to KVM selftests Aaron Lewis
2023-06-06  0:05   ` Sean Christopherson [this message]
2023-04-24 22:58 ` [PATCH v2 3/6] KVM: selftests: Add additional pages to the guest to accommodate ucall Aaron Lewis
2023-06-05 20:43   ` Sean Christopherson
2023-04-24 22:58 ` [PATCH v2 4/6] KVM: selftests: Add string formatting options to ucall Aaron Lewis
2023-06-05 21:44   ` Sean Christopherson
2023-06-07 16:55     ` Aaron Lewis
2023-04-24 22:58 ` [PATCH v2 5/6] KVM: selftests: Add ucall_fmt2() Aaron Lewis
2023-06-05 22:41   ` Sean Christopherson
2023-06-07 16:55     ` Aaron Lewis
2023-04-24 22:58 ` [PATCH v2 6/6] KVM: selftests: Add a selftest for guest prints and formatted asserts Aaron Lewis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZH54WgNDADIhX2wx@google.com \
    --to=seanjc@google.com \
    --cc=aaronlewis@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox