runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Implement FlushProcessWriteBuffers in PAL

Open sergiy-k opened this issue 11 years ago • 8 comments

Currently we have an empty stub in coreclr/src/pal/src/thread/process.cpp.

This function is called by GC as part of synchronization process between GC and managed threads. We need to provide proper implementation for Linux and Mac and add a test under coreclr/src/pal/tests/palsuite.

sergiy-k avatar Feb 11 '15 05:02 sergiy-k

I'm not really sure what to do here. AFAICT there really isn't an analogue on Linux or MAC. We probably need to drop this function, and audit the call sites to see what they really need (note, I haven't audited the callsites yet)

  • Is this just a matter of appropriate fencing?
  • Does it actually need cache invalidation to ensure coherency across clusters?
  • Is this called due to weak memory ordering on ARM?
  • something else?

kangaroo avatar Feb 15 '15 02:02 kangaroo

The crucial callsite is in https://github.com/dotnet/coreclr/blob/master/src/vm/threadsuspend.cpp. The resolution for this issue may dependent on design for dotnet/runtime#3947.

This API does not need to invalidate cache. It just needs to flush pending writes (and invalidate speculative reads) on all cores. It is needed for correctness on all common platforms, including x86/x64.

Conceptually, this API makes every core in the system execute memory barrier. Windows kernel implements it by sending Inter-processor interrupt (http://en.wikipedia.org/wiki/Inter-processor_interrupt) to all cores in the system.

jkotas avatar Feb 15 '15 06:02 jkotas

I have spent some time thinking about how to implement the FlushProcessWriteBuffers functionality on Linux, OSX and FreeBSD. I have also considered a possibility to remove the need for this function on non-Windows systems by adding some fences, but it turns out there are other places where fences would cause performance degradation and it would also be quite error prone due to the complexity of the codes and possibly many places that would have to be touched. See this comment in gc.cpp right before a FlushProcessWriteBuffers call :

// We need to make sure that other threads executing checked write barriers // will see the g_card_table update before g_lowest/highest_address updates. // Otherwise, the checked write barrier may AV accessing the old card table // with address that it does not cover. Write barriers access card table // without memory barriers for performance reasons, so we need to flush // the store buffers here.

Here are possible solutions that I've figured. I currently tend to the second solution.

1. Helper threads

Have a set of helper threads, one per each CPU on the machine, each affinitized to one of the CPUs. All the threads would be waiting on an event. The FlushProcessWriteBuffers would set a global counter to the number of CPUs - 1 (to exclude itself) and signal the event.The helper threads would wake up on the event, decrement using interlocked operation the global counter (which both flushes the buffers and ensures atomicity) and go to wait on the event again. The thread running the FlushProcessWriteBuffers would wait until the global counter reaches zero, indicating that all the threads have ran. It seems there is an issue on OSX though. Its affinity APIs don't allow specifying direct affinity to specific CPU. It only allows specifying affinity groups and if there are more threads then the number of processors, I'm not sure if the OS will honor the even spreading, which is required for the functionality to work.

2. Signals

Use signals on Linux/FreeBSD and similar functionality on OSX. The FlushProcessWriteBuffers would first call the PROCProcessLock to ensure the list of PAL threads won't change under our hands. Then we would iterate over the list of threads and consider only runnable / running threads since the others don't have anything to flush. In fact, we really care only about running threads, but on Linux, there is no way to distinguish between those two states. It seems that on OSX and FreeBSD, we can distinguish between them, which will make it simpler. First, we zero a global counter. Then as we scan the list of threads, we would send a realtime signal to the first thread we find running on each CPU. Before sending the signal, we would atomically increment the global counter. On Linux, it gets a bit more complicated due to the fact that we cannot distinguish between running / runnable threads. So we set the affinity of the thread before we send it a signal to the CPU it was running on and reset the affinity after all signals are processed or right at the end of the signal handler. The signal handler would just atomically decrement the global counter and return. The FlushProcessWriteBuffers would finish iterating over the threads and then wait until the global counter reaches zero, indicating that all the signals were processed. Finally, we would release the process lock.

You may wonder if it is ok to rely on reading the thread's state and its CPU when they can change anytime, even during the thread enumeration. The reason why it is ok is that we care about flushing buffers that were on each CPU at the time the FlushProcessWriteBuffers was called. So we have the following possible cases:

  1. A thread was running on a processor, but before we checked its state, it got into waiting state. This is ok, since the fact that it went into the waiting state means its buffers were flushed.
  2. A thread was waiting, but before we checked its state, it started to run. We will send a signal unnecessarily here.
  3. A thread was waiting, but before we checked its state, it became runnable. On Linux, we will send a signal unnecessarily here. On OSX / FreeBSD, we won't.
  4. A thread was runnable, but before we checked its state, it started to run. That's the same as 2.
  5. A thread was running, but before we checked its state, it became runnable. That's the same as 1. on OSX / FreeBSD and same as 2. on Linux. So there are few cases when we send a signal unnecessarily, but the overhead should be really low since it happens only when thread state changes.

@kangaroo I have one unknown here though. I wasn't able to find a way to get a CPU a thread is running on yet for OSX. Do you know how we can get it? If there is no way, then it would still work, but we would send more signals in case when a thread 1 was running on CPU1 and while we were iterating the thread list, another thread started to run on CPU1 instead.

janvorli avatar May 15 '15 21:05 janvorli

Before FlushProcessWriteBuffers was added as Windows API, we used to simulate it by flipping protection on a page.

We are not the only ones solving this problem. There is a long discussion about it in the context of liburcu library: http://www.bing.com/search?q=liburcu%2Bsys_membarrier. This discussion suggests that patch to introduce sys_membarrier syscall to Linux kernel is going to be accepted soon. @compudj - thanks a lot for making it happen!

I think we should simulate the FlushProcessWriteBuffers using the page protection flip for now - it is fragile, but it has the best performance characteristics among the various options and it should be reliable on all current system. And switch over to sys_membarrier() once it becomes available in the Linux kernel, and hopefully other OSes will eventually follow the suit too.

jkotas avatar May 16 '15 07:05 jkotas

Implemented in dotnet/coreclr#1057 using the page protection changes.

janvorli avatar May 27 '15 20:05 janvorli

FYI, sys_membarrier has been merged into Linux 4.3-rc1 (currently x86 32/64 support, other architectures should be wired up soon).

compudj avatar Sep 16 '15 14:09 compudj

@compudj Perfect - I have opened issue for CoreCLR to take advantage of it.

jkotas avatar Sep 16 '15 16:09 jkotas

:mips-interest

xiangzhai avatar Apr 27 '20 01:04 xiangzhai