From: Sean Christopherson <seanjc@google.com>
To: Gavin Shan <gshan@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
mathieu.desnoyers@efficios.com, shuah@kernel.org, maz@kernel.org,
oliver.upton@linux.dev, shan.gavin@gmail.com
Subject: Re: [PATCH] KVM: selftests: Double check on the current CPU in rseq_test
Date: Fri, 15 Jul 2022 14:32:33 +0000 [thread overview]
Message-ID: <YtF6gVYgMhoiD0Pe@google.com> (raw)
In-Reply-To: <be806f9c-861a-8da8-d42e-1d4271c3a326@redhat.com>
On Fri, Jul 15, 2022, Gavin Shan wrote:
> Hi Paolo and Sean,
>
> On 7/15/22 1:35 AM, Sean Christopherson wrote:
> > On Thu, Jul 14, 2022, Paolo Bonzini wrote:
> Well, I don't think migration_worker() does correct thing, if I'm understanding
> correctly. The intention seems to force migration on 'main' thread by 'migration'
> thread? If that is the case, I don't think the following function call has correct
> parameters.
>
> r = sched_setaffinity(0, sizeof(allowed_mask), &allowed_mask);
>
> it should be something like:
>
> r = sched_setaffinity(getpid(), sizeof(allowed_mask), &allowed_mask);
>
> If we're using sched_setaffinity(0, ...) in the 'migration' thread, the CPU
> affinity of 'main' thread won't be affected. It means 'main' thread can be
> migrated from one CPU to another at any time, even in the following point:
>
> int main(...)
> {
> :
> /*
> * migration can happen immediately after sched_getcpu(). If
> * CPU affinity of 'main' thread is sticky to one particular
> * CPU, which 'migration' thread supposes to do, then there
> * should have no migration.
> */
> cpu = sched_getcpu();
> rseq_cpu = READ_ONCE(__rseq.cpu_id);
> :
> }
>
> So I think the correct fix is to have sched_setaffinity(getpid(), ...) ?
> Please refer to the manpage.
>
> https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html
> 'If pid is zero, then the calling thread is used'
Oof, and more explicitly the rest of that sentence clarifies that the result of
getpid() will target the main thread (I assume "main" means thread group leader).
Specifying pid as 0 will set the attribute for the calling thread, and passing
the value returned from a call to getpid(2) will set the attribute for the main
thread of the thread group.
I'm guessing my test worked (in that it reproduced the bug) by virtue of the
scheduler trying to colocate all threads in the process.
In my defense, the die.net copy of the manpages quite clearly uses "process"[1],
but that was fixed in the manpages in 2013[2]!?!!? So I guess the takeaway is
to use only the official manpages.
Anyways, for the code, my preference would be to snapshot gettid() in main() before
spawning the migration worker. Same result, but I would rather the test explicitly
target the thread doing rseq instead of relying on (a) getpid() targeting only the
main thread and (b) the main thread always being the rseq thread. E.g. if for some
reason a future patch moves the rseq code to its own worker thread, then getpid()
would be incorrect.
Thanks for figuring this out!
[1] https://linux.die.net/man/2/sched_setaffinity
[2] 6a7fcf3cc ("sched_setaffinity.2: Clarify that these system calls affect a per-thread attribute")
next prev parent reply other threads:[~2022-07-15 14:32 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-14 8:06 [PATCH] KVM: selftests: Double check on the current CPU in rseq_test Gavin Shan
2022-07-14 13:52 ` Paolo Bonzini
2022-07-14 14:03 ` Paolo Bonzini
2022-07-14 15:35 ` Sean Christopherson
2022-07-14 15:42 ` Paolo Bonzini
2022-07-15 2:21 ` Gavin Shan
2022-07-15 14:32 ` Sean Christopherson [this message]
2022-07-16 14:46 ` Gavin Shan
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=YtF6gVYgMhoiD0Pe@google.com \
--to=seanjc@google.com \
--cc=gshan@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.cs.columbia.edu \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=pbonzini@redhat.com \
--cc=shan.gavin@gmail.com \
--cc=shuah@kernel.org \
/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