News for hackers. Stuff that matters.
Re:Website hosted on a 24 year old Linux server by andoma on Thursday April 06, 2023 @06:12PM (#35475242)
Genuine question: Does it have to be mlocked for this to work?
Re:Website hosted on a 24 year old Linux server by wahern on Thursday April 06, 2023 @08:37PM (#35476569)
You don't need mlock for the interprocess atomicity. There may be a race if you intend the file to be persistent; write-back to disk wouldn't necessarily be atomic. But mlock doesn't solve that problem, it just might make it slightly less likely to result in corruption if it means less flushing. To truly solve it you need a proper synchronization mechanism, e.g. a mutex to quiet all writers, then a blocking sync. OTOH, write-back for a single native integer may actually be atomic, assuming it doesn't cross a cache line, so in practice a simple mmap may be sufficient. (I emphasize may, though. I'm not that familiar with the inner mechanics of DMA, block device buffering, etc.)

You don't need assembly, either, as standard atomic built-ins in C, C++, etc, will work just fine, as will FFI to such built-ins.

Re:Website hosted on a 24 year old Linux server by jcalvinowens on Friday April 07, 2023 @09:15PM (#35489374)
Great question: it does not! It just removes a potential source of stalls.

If the page isn't resident in the page cache, the thread(s) executing the atomic increment will take a page fault and be blocked until the file data is read from storage. The latency of the fault might be significant, but the counter will remain accurate.

There's an important caveat I should have added: the counter file might be very stale after a power failure. If you care about the counter integrity, you have to msync(MS_SYNC) periodically, and that's expensive. It might actually stall all threads interacting with the page, depending on the filesystem; that used to be true but I'm not certain it still is (see https://lwn.net/Articles/486311/). Where writeback is allowed to race with writes, whether you would be guaranteed the 8-byte value written back wasn't "torn" without explicitly blocking increments while syncing is also an interesting question if DMA is involved...