News for hackers. Stuff that matters.
Re:What's a CPU to do when it has nothing to do? by IcePic on Thursday October 11, 2018 @02:45AM (#18191396)
I have no idea on how many other OSes do this, but at least OpenBSD will use idle time to pre-clear memory pages that have been returned to the OS, so that when the next process has them mapped, it doesn't have to zero-fill-on-demand at the very latest moment. It has to be done at some point, but if you keep a queue of pages needing to be cleared and dealing with as many as you can while any core is idle, a non-100% busy system can be seen as to "improve" performance a bit by timeshifting the task to not-directly-after-former process but also not when the new process is calling for memory to be mapped in.

But when that list is empty, doing the right kind of CPU sleep is totally worthwhile of course.

Re:What's a CPU to do when it has nothing to do? by masklinn on Thursday October 11, 2018 @04:17AM (#18191745)
I expect most BSDs do this.

Dragonfly actually removed it two years ago: http://lists.dragonflybsd.org/pipermail/commits/2016-August/...

  - Pre-zeroing a page only takes 80ns on a modern cpu.  vm_fault overhead
    in general is ~at least 1 microscond.
  - Pre-zeroing a page leads to a cold-cache case on-use, forcing the fault
    source (e.g. a userland program) to actually get the data from main
    memory in its likely immediate use of the faulted page, reducing
    performance.
  - Zeroing the page at fault-time is actually more optimal because it does
    not require any reading of dynamic ram and leaves the cache hot.
  - Multiple synth and build tests show that active idle-time zeroing of
    pages actually reduces performance somewhat and incidental allocations
    of already-zerod pages (from page-table tear-downs) do not affect
    performance in any meaningful way.
Re:What's a CPU to do when it has nothing to do? by bluGill on Thursday October 11, 2018 @08:58AM (#18193102)
I expect openBSD to continue to do this anyway though because there is the possibility that someone can reboot the system to a new OS (presumably designed just for this purpose) and read whatever was in RAM. Of course programs that deal with encryption zero memory before returning it (It is hard to make sure the compiler doesn't optimize this otherwise useless work out), but most other programs that deal with secrets are not so well written and will live sensitive information around.
Re:What's a CPU to do when it has nothing to do? by sigstoat on Thursday October 11, 2018 @11:52AM (#18194524)
> the possibility that someone can reboot the system to a new OS and read whatever was in RAM

or physically pull the RAM out, keeping it cold with LN2, and stick it into devices designed for reading it all out.

Re:What's a CPU to do when it has nothing to do? by hultner on Thursday October 11, 2018 @04:52PM (#18196944)
Canned air upside down works reasonably well and is easier to handle
Re:What's a CPU to do when it has nothing to do? by mehrdadn on Thursday October 11, 2018 @04:25AM (#18191775)
Why don't they just avoid the cache pollution issues with non-polluting writes? _mm_stream_si128 comes to mind.
Re:What's a CPU to do when it has nothing to do? by Tuna-Fish on Thursday October 11, 2018 @06:19AM (#18192242)
That's the exact opposite of what you want to do.

The starting point is that there is stale, useless data in ram. Then a usermode program requests an empty page, and usually when they do this they want to immediately use it.(1) Using non-polluting writes, you have to use main memory bandwidth both for clearing the page, and also for bringing the page back to cache immediately afterwards when the program uses it.

Using writes that just allocate new, cleared dirty lines in cache (like the AMD CLZERO), it avoids both the write (which will happen later, when the lines are evicted from cache, probably after the lines have been used by the program), and the read because the lines are now all in the cache.

(1) And on Linux this is trivially true, because Linux only allocates and clears the page when it is first accessed.

Re:What's a CPU to do when it has nothing to do? by mehrdadn on Thursday October 11, 2018 @07:12AM (#18192474)
I don't follow. Who is "you" here? The user-mode program, or the kernel-mode zero-page thread (or whatever its name is)? I'm talking about the zero-page thread here, which is zeroing pages in the background long before any thread has requested access. Those threads do not want to evict anything from the cache. This seems exactly what we want.
Re:What's a CPU to do when it has nothing to do? by Tuna-Fish on Thursday October 11, 2018 @07:49AM (#18192680)
The issue is that zeroing pages in the background is a pessimization, that should not ever be done. The user-mode program that allocates some memory is typically not going to be able to use only writes that allocate new cache lines without reading memory. So to compare the two systems:

Your system: memory is released, kernel clears it in the background, wasting write bandwidth (which might not matter for anything except power if the system was idle at the time), and when the user-mode program starts using it, it will start writing and every new line they write to will trigger a spurious read.

Modern Linux: memory is released, kernel lets it lie, not using any power or bandwidth to do anything to it, until an user-mode program allocates it and touches the page. Then the kernel picks up the page, writes the entire page with 0:es using whatever idiom on that CPU allows it to just allocate the page in cache without reading it from the RAM. This is really fast, faster than a single memory fetch. The user-mode program can then directly use it without having to fetch anything from DRAM.

Re:What's a CPU to do when it has nothing to do? by masklinn on Thursday October 11, 2018 @08:30AM (#18192914)
> This is really fast, faster than a single memory fetch.

Nit: it's faster than a page fault (so fault + zeroing is pretty much the same as just fault).

According to the recent-ish Latency Numbers, a main memory reference is ~100ns (variable by arch and local v remote DRAM) which is about the same as zeroing a page, at least with respect to the dragonfly numbers I posted above.

Re:What's a CPU to do when it has nothing to do? by agapon on Thursday October 11, 2018 @05:18AM (#18191985)
FreeBSD has recently removed this as well.
Re:What's a CPU to do when it has nothing to do? by masklinn on Thursday October 11, 2018 @06:42AM (#18192331)
I had no idea, do you have a link?
Re:What's a CPU to do when it has nothing to do? by joncrane on Thursday October 11, 2018 @02:53PM (#18196080)
That's neat.
Re:What's a CPU to do when it has nothing to do? by sedatk on Thursday October 11, 2018 @03:22AM (#18191527)
Windows also has a similar thread called “Zero Page Thread” which has the lowest priority on the system (even lower than IDLE, IIRC).
Re:What's a CPU to do when it has nothing to do? by ovi256 on Thursday October 11, 2018 @03:56AM (#18191669)
>lower than IDLE

Any idea why that's necessary ? So it's preempted by a IDLE process.

Re:What's a CPU to do when it has nothing to do? by JdeBP on Thursday October 11, 2018 @06:58AM (#18192403)
Windows NT thread scheduling does not work that way. If the Zero Page Thread is ready to run, the processor's Idle Thread is not scheduled. Idle Threads do not live on ready queues, and are only dispatched when there is no thread available from the processor's ready queue.

Also, do not conflate the user-thread idle priority class with the (non-)priorities of Idle Threads.

Re:What's a CPU to do when it has nothing to do? by bonzini on Thursday October 11, 2018 @04:25AM (#18191773)
IDLE is the priority of a process that runs only when the system is otherwise idle. It shouldn't be preempted by zeroing memory.
Re:What's a CPU to do when it has nothing to do? by na412 on Thursday October 11, 2018 @05:10AM (#18191962)
But then under what circumstances will the zero page thread ever be scheduled?
Re:What's a CPU to do when it has nothing to do? by masklinn on Thursday October 11, 2018 @09:53AM (#18193520)
It's confusing, but "idle" is a priority class as well as a state: https://docs.microsoft.com/en-us/windows/desktop/procthread/...

The idle task (when there's nothing to run) doesn't have a priority at all, it just gets run when nothing else is available. the "idle" priority is an actual priority, for when there's no higher-priority task to run. The ZPT is prioritized below "idle", so super-duper-low priority.

Re:What's a CPU to do when it has nothing to do? by dTal on Friday October 12, 2018 @05:09AM (#18200214)
Thank you for this clear explanation. It is confusing!
Re:What's a CPU to do when it has nothing to do? by blattimwind on Thursday October 11, 2018 @07:07AM (#18192444)
When there are no runnable idle threads.
Re:What's a CPU to do when it has nothing to do? by chaosite on Thursday October 11, 2018 @05:14AM (#18191969)
So the zero page process runs when one core is busy but the rest are free?
Re:What's a CPU to do when it has nothing to do? by klodolph on Thursday October 11, 2018 @09:29AM (#18193337)
It runs when the there are no higher-priority processes to schedule on a particular core. IDLE processes are higher priority, so as long as an IDLE process can be scheduled, the zero page process won't run.
Re:What's a CPU to do when it has nothing to do? by brann0 on Thursday October 11, 2018 @11:46AM (#18194460)
I think the article talks about idle time itself, when there's no workload. OS doing bookkeeping means CPU isn't idle.