Commit e0f08b93 authored by Russ Cox's avatar Russ Cox

runtime: use cas loop to coordinate with sigprof

sigprof and setcpuprofilerate coordinate the enabling/disabling
of the handler using a Mutex. This has always been a bit dodgy:
setcpuprofilerate must be careful to turn off signals before acquiring
the lock to avoid a deadlock.

Now the lock implementations use onM, and onM isn't okay on the
signal stack. We know how to make it okay, but it's more work than
is probably worth doing.

Since this is super-dodgy anyway, replace the lock with a simple
cas loop. It is only contended if setcpuprofilerate is being called,
and that doesn't happen frequently enough to care about the
raw speed or about using futexes/semaphores.

TBR to fix freebsd/amd64 and dragonfly/amd64 builds.
Happy to make changes in a follow-up CL.

TBR=dvyukov
CC=golang-codereviews
https://golang.org/cl/141080044
parent 83824639
...@@ -2630,7 +2630,7 @@ runtime·badreflectcall(void) // called from assembly ...@@ -2630,7 +2630,7 @@ runtime·badreflectcall(void) // called from assembly
} }
static struct { static struct {
Mutex lock; uint32 lock;
int32 hz; int32 hz;
} prof; } prof;
...@@ -2774,10 +2774,12 @@ runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp, M *mp) ...@@ -2774,10 +2774,12 @@ runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp, M *mp)
} }
if(prof.hz != 0) { if(prof.hz != 0) {
runtime·lock(&prof.lock); // Simple cas-lock to coordinate with setcpuprofilerate.
while(!runtime·cas(&prof.lock, 0, 1))
runtime·osyield();
if(prof.hz != 0) if(prof.hz != 0)
runtime·cpuproftick(stk, n); runtime·cpuproftick(stk, n);
runtime·unlock(&prof.lock); runtime·atomicstore(&prof.lock, 0);
} }
mp->mallocing--; mp->mallocing--;
} }
...@@ -2804,9 +2806,11 @@ runtime·setcpuprofilerate_m(void) ...@@ -2804,9 +2806,11 @@ runtime·setcpuprofilerate_m(void)
// it would deadlock. // it would deadlock.
runtime·resetcpuprofiler(0); runtime·resetcpuprofiler(0);
runtime·lock(&prof.lock); while(!runtime·cas(&prof.lock, 0, 1))
runtime·osyield();
prof.hz = hz; prof.hz = hz;
runtime·unlock(&prof.lock); runtime·atomicstore(&prof.lock, 0);
runtime·lock(&runtime·sched.lock); runtime·lock(&runtime·sched.lock);
runtime·sched.profilehz = hz; runtime·sched.profilehz = hz;
runtime·unlock(&runtime·sched.lock); runtime·unlock(&runtime·sched.lock);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment