Re: [PATCH v11 33/46] KVM: selftests: Hyper-V PV IPI selftest

From: Sean Christopherson
Date: Fri Oct 21 2022 - 12:14:48 EST


On Fri, Oct 21, 2022, Vitaly Kuznetsov wrote:
> Vitaly Kuznetsov <vkuznets@xxxxxxxxxx> writes:
>
> > Sean Christopherson <seanjc@xxxxxxxxxx> writes:
> >> Do you happen to know if errno is preserved? I.e. if TEST_ASSERT()'s print of
> >> errno will capture the right errno? If so, this and the pthread_join() assert
> >> can be:
> >>
> >> TEST_ASSERT(!r, pthread_cancel() failed on vcpu_id=%d, vcpu->id);
> >>
> >
> > The example from 'man 3 pthread_cancel' makes me think errno is not
> > set. 'man 3 errno' confirms that:
> >
> > "
> > Note that the POSIX threads APIs do not set errno on error.

Ah, that's annoying.

> > Instead, on failure they return an error number as the function result.
> > These error numbers have the same meanings as the error numbers returned
> > in errno by other APIs.
> > "
> >
> > but nothing stops us from doing something like
> >
> > #include <errno.h>
> > ...
> >
> > errno = pthread_cancel(thread);
> > TEST_ASSERT(!errno, pthread_cancel() failed on vcpu_id=%d, vcpu->id);
> >
> > I believe.
>
> ... only the fact that this won't be thread safe :-( i.e. if we also try
> setting 'errno' from vcpu_thread() (where the pattern for
> pthread_setcanceltype() is exactly the same), we will likely be
> reporting the wrong errno.

errno is thread safe. From https://man7.org/linux/man-pages/man3/errno.3.html:

errno is defined by the ISO C standard to be a modifiable lvalue
of type int, and must not be explicitly declared; errno may be a
macro. errno is thread-local; setting it in one thread does not
affect its value in any other thread.


> I think it's better to keep reporting 'r' for now (and maybe think about
> pthread* wrappers later).

Yeah, definitely a future problem. pthread wrappers are a good idea, I doubt
there's a single KVM selftest that wants to do anything but assert on failure.